blob: 7cdc76007c6cc30ea2ceea6d41561a53d0d51dbe [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"
Yan Zheng5d4f98a2009-06-10 10:45:14 -040034
35/*
36 * backref_node, mapping_node and tree_block start with this
37 */
38struct tree_entry {
39 struct rb_node rb_node;
40 u64 bytenr;
41};
42
43/*
44 * present a tree block in the backref cache
45 */
46struct backref_node {
47 struct rb_node rb_node;
48 u64 bytenr;
Yan, Zheng3fd0a552010-05-16 10:49:59 -040049
50 u64 new_bytenr;
51 /* objectid of tree block owner, can be not uptodate */
Yan Zheng5d4f98a2009-06-10 10:45:14 -040052 u64 owner;
Yan, Zheng3fd0a552010-05-16 10:49:59 -040053 /* link to pending, changed or detached list */
54 struct list_head list;
Yan Zheng5d4f98a2009-06-10 10:45:14 -040055 /* list of upper level blocks reference this block */
56 struct list_head upper;
57 /* list of child blocks in the cache */
58 struct list_head lower;
59 /* NULL if this node is not tree root */
60 struct btrfs_root *root;
61 /* extent buffer got by COW the block */
62 struct extent_buffer *eb;
63 /* level of tree block */
64 unsigned int level:8;
Yan, Zheng3fd0a552010-05-16 10:49:59 -040065 /* is the block in non-reference counted tree */
66 unsigned int cowonly:1;
67 /* 1 if no child node in the cache */
Yan Zheng5d4f98a2009-06-10 10:45:14 -040068 unsigned int lowest:1;
69 /* is the extent buffer locked */
70 unsigned int locked:1;
71 /* has the block been processed */
72 unsigned int processed:1;
73 /* have backrefs of this block been checked */
74 unsigned int checked:1;
Yan, Zheng3fd0a552010-05-16 10:49:59 -040075 /*
76 * 1 if corresponding block has been cowed but some upper
77 * level block pointers may not point to the new location
78 */
79 unsigned int pending:1;
80 /*
81 * 1 if the backref node isn't connected to any other
82 * backref node.
83 */
84 unsigned int detached:1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -040085};
86
87/*
88 * present a block pointer in the backref cache
89 */
90struct backref_edge {
91 struct list_head list[2];
92 struct backref_node *node[2];
Yan Zheng5d4f98a2009-06-10 10:45:14 -040093};
94
95#define LOWER 0
96#define UPPER 1
97
98struct backref_cache {
99 /* red black tree of all backref nodes in the cache */
100 struct rb_root rb_root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400101 /* for passing backref nodes to btrfs_reloc_cow_block */
102 struct backref_node *path[BTRFS_MAX_LEVEL];
103 /*
104 * list of blocks that have been cowed but some block
105 * pointers in upper level blocks may not reflect the
106 * new location
107 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400108 struct list_head pending[BTRFS_MAX_LEVEL];
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400109 /* list of backref nodes with no child node */
110 struct list_head leaves;
111 /* list of blocks that have been cowed in current transaction */
112 struct list_head changed;
113 /* list of detached backref node. */
114 struct list_head detached;
115
116 u64 last_trans;
117
118 int nr_nodes;
119 int nr_edges;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400120};
121
122/*
123 * map address of tree root to tree
124 */
125struct mapping_node {
126 struct rb_node rb_node;
127 u64 bytenr;
128 void *data;
129};
130
131struct mapping_tree {
132 struct rb_root rb_root;
133 spinlock_t lock;
134};
135
136/*
137 * present a tree block to process
138 */
139struct tree_block {
140 struct rb_node rb_node;
141 u64 bytenr;
142 struct btrfs_key key;
143 unsigned int level:8;
144 unsigned int key_ready:1;
145};
146
Yan, Zheng0257bb82009-09-24 09:17:31 -0400147#define MAX_EXTENTS 128
148
149struct file_extent_cluster {
150 u64 start;
151 u64 end;
152 u64 boundary[MAX_EXTENTS];
153 unsigned int nr;
154};
155
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400156struct reloc_control {
157 /* block group to relocate */
158 struct btrfs_block_group_cache *block_group;
159 /* extent tree */
160 struct btrfs_root *extent_root;
161 /* inode for moving data */
162 struct inode *data_inode;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400163
164 struct btrfs_block_rsv *block_rsv;
165
166 struct backref_cache backref_cache;
167
168 struct file_extent_cluster cluster;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400169 /* tree blocks have been processed */
170 struct extent_io_tree processed_blocks;
171 /* map start of tree root to corresponding reloc tree */
172 struct mapping_tree reloc_root_tree;
173 /* list of reloc trees */
174 struct list_head reloc_roots;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400175 /* size of metadata reservation for merging reloc trees */
176 u64 merging_rsv_size;
177 /* size of relocated tree nodes */
178 u64 nodes_relocated;
179
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400180 u64 search_start;
181 u64 extents_found;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400182
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400183 unsigned int stage:8;
184 unsigned int create_reloc_tree:1;
185 unsigned int merge_reloc_tree:1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400186 unsigned int found_file_extent:1;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400187 unsigned int commit_transaction:1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400188};
189
190/* stages of data relocation */
191#define MOVE_DATA_EXTENTS 0
192#define UPDATE_DATA_PTRS 1
193
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400194static void remove_backref_node(struct backref_cache *cache,
195 struct backref_node *node);
196static void __mark_block_processed(struct reloc_control *rc,
197 struct backref_node *node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400198
199static void mapping_tree_init(struct mapping_tree *tree)
200{
Eric Paris6bef4d32010-02-23 19:43:04 +0000201 tree->rb_root = RB_ROOT;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400202 spin_lock_init(&tree->lock);
203}
204
205static void backref_cache_init(struct backref_cache *cache)
206{
207 int i;
Eric Paris6bef4d32010-02-23 19:43:04 +0000208 cache->rb_root = RB_ROOT;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400209 for (i = 0; i < BTRFS_MAX_LEVEL; i++)
210 INIT_LIST_HEAD(&cache->pending[i]);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400211 INIT_LIST_HEAD(&cache->changed);
212 INIT_LIST_HEAD(&cache->detached);
213 INIT_LIST_HEAD(&cache->leaves);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400214}
215
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400216static void backref_cache_cleanup(struct backref_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400217{
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400218 struct backref_node *node;
219 int i;
220
221 while (!list_empty(&cache->detached)) {
222 node = list_entry(cache->detached.next,
223 struct backref_node, list);
224 remove_backref_node(cache, node);
225 }
226
227 while (!list_empty(&cache->leaves)) {
228 node = list_entry(cache->leaves.next,
229 struct backref_node, lower);
230 remove_backref_node(cache, node);
231 }
232
233 cache->last_trans = 0;
234
235 for (i = 0; i < BTRFS_MAX_LEVEL; i++)
236 BUG_ON(!list_empty(&cache->pending[i]));
237 BUG_ON(!list_empty(&cache->changed));
238 BUG_ON(!list_empty(&cache->detached));
239 BUG_ON(!RB_EMPTY_ROOT(&cache->rb_root));
240 BUG_ON(cache->nr_nodes);
241 BUG_ON(cache->nr_edges);
242}
243
244static struct backref_node *alloc_backref_node(struct backref_cache *cache)
245{
246 struct backref_node *node;
247
248 node = kzalloc(sizeof(*node), GFP_NOFS);
249 if (node) {
250 INIT_LIST_HEAD(&node->list);
251 INIT_LIST_HEAD(&node->upper);
252 INIT_LIST_HEAD(&node->lower);
253 RB_CLEAR_NODE(&node->rb_node);
254 cache->nr_nodes++;
255 }
256 return node;
257}
258
259static void free_backref_node(struct backref_cache *cache,
260 struct backref_node *node)
261{
262 if (node) {
263 cache->nr_nodes--;
264 kfree(node);
265 }
266}
267
268static struct backref_edge *alloc_backref_edge(struct backref_cache *cache)
269{
270 struct backref_edge *edge;
271
272 edge = kzalloc(sizeof(*edge), GFP_NOFS);
273 if (edge)
274 cache->nr_edges++;
275 return edge;
276}
277
278static void free_backref_edge(struct backref_cache *cache,
279 struct backref_edge *edge)
280{
281 if (edge) {
282 cache->nr_edges--;
283 kfree(edge);
284 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400285}
286
287static struct rb_node *tree_insert(struct rb_root *root, u64 bytenr,
288 struct rb_node *node)
289{
290 struct rb_node **p = &root->rb_node;
291 struct rb_node *parent = NULL;
292 struct tree_entry *entry;
293
294 while (*p) {
295 parent = *p;
296 entry = rb_entry(parent, struct tree_entry, rb_node);
297
298 if (bytenr < entry->bytenr)
299 p = &(*p)->rb_left;
300 else if (bytenr > entry->bytenr)
301 p = &(*p)->rb_right;
302 else
303 return parent;
304 }
305
306 rb_link_node(node, parent, p);
307 rb_insert_color(node, root);
308 return NULL;
309}
310
311static struct rb_node *tree_search(struct rb_root *root, u64 bytenr)
312{
313 struct rb_node *n = root->rb_node;
314 struct tree_entry *entry;
315
316 while (n) {
317 entry = rb_entry(n, struct tree_entry, rb_node);
318
319 if (bytenr < entry->bytenr)
320 n = n->rb_left;
321 else if (bytenr > entry->bytenr)
322 n = n->rb_right;
323 else
324 return n;
325 }
326 return NULL;
327}
328
Eric Sandeen48a3b632013-04-25 20:41:01 +0000329static void backref_tree_panic(struct rb_node *rb_node, int errno, u64 bytenr)
Jeff Mahoney43c04fb2011-10-03 23:22:33 -0400330{
331
332 struct btrfs_fs_info *fs_info = NULL;
333 struct backref_node *bnode = rb_entry(rb_node, struct backref_node,
334 rb_node);
335 if (bnode->root)
336 fs_info = bnode->root->fs_info;
337 btrfs_panic(fs_info, errno, "Inconsistency in backref cache "
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200338 "found at offset %llu\n", bytenr);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -0400339}
340
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400341/*
342 * walk up backref nodes until reach node presents tree root
343 */
344static struct backref_node *walk_up_backref(struct backref_node *node,
345 struct backref_edge *edges[],
346 int *index)
347{
348 struct backref_edge *edge;
349 int idx = *index;
350
351 while (!list_empty(&node->upper)) {
352 edge = list_entry(node->upper.next,
353 struct backref_edge, list[LOWER]);
354 edges[idx++] = edge;
355 node = edge->node[UPPER];
356 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400357 BUG_ON(node->detached);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400358 *index = idx;
359 return node;
360}
361
362/*
363 * walk down backref nodes to find start of next reference path
364 */
365static struct backref_node *walk_down_backref(struct backref_edge *edges[],
366 int *index)
367{
368 struct backref_edge *edge;
369 struct backref_node *lower;
370 int idx = *index;
371
372 while (idx > 0) {
373 edge = edges[idx - 1];
374 lower = edge->node[LOWER];
375 if (list_is_last(&edge->list[LOWER], &lower->upper)) {
376 idx--;
377 continue;
378 }
379 edge = list_entry(edge->list[LOWER].next,
380 struct backref_edge, list[LOWER]);
381 edges[idx - 1] = edge;
382 *index = idx;
383 return edge->node[UPPER];
384 }
385 *index = 0;
386 return NULL;
387}
388
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400389static void unlock_node_buffer(struct backref_node *node)
390{
391 if (node->locked) {
392 btrfs_tree_unlock(node->eb);
393 node->locked = 0;
394 }
395}
396
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400397static void drop_node_buffer(struct backref_node *node)
398{
399 if (node->eb) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400400 unlock_node_buffer(node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400401 free_extent_buffer(node->eb);
402 node->eb = NULL;
403 }
404}
405
406static void drop_backref_node(struct backref_cache *tree,
407 struct backref_node *node)
408{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400409 BUG_ON(!list_empty(&node->upper));
410
411 drop_node_buffer(node);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400412 list_del(&node->list);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400413 list_del(&node->lower);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400414 if (!RB_EMPTY_NODE(&node->rb_node))
415 rb_erase(&node->rb_node, &tree->rb_root);
416 free_backref_node(tree, node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400417}
418
419/*
420 * remove a backref node from the backref cache
421 */
422static void remove_backref_node(struct backref_cache *cache,
423 struct backref_node *node)
424{
425 struct backref_node *upper;
426 struct backref_edge *edge;
427
428 if (!node)
429 return;
430
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400431 BUG_ON(!node->lowest && !node->detached);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400432 while (!list_empty(&node->upper)) {
433 edge = list_entry(node->upper.next, struct backref_edge,
434 list[LOWER]);
435 upper = edge->node[UPPER];
436 list_del(&edge->list[LOWER]);
437 list_del(&edge->list[UPPER]);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400438 free_backref_edge(cache, edge);
439
440 if (RB_EMPTY_NODE(&upper->rb_node)) {
441 BUG_ON(!list_empty(&node->upper));
442 drop_backref_node(cache, node);
443 node = upper;
444 node->lowest = 1;
445 continue;
446 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400447 /*
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400448 * add the node to leaf node list if no other
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400449 * child block cached.
450 */
451 if (list_empty(&upper->lower)) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400452 list_add_tail(&upper->lower, &cache->leaves);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400453 upper->lowest = 1;
454 }
455 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400456
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400457 drop_backref_node(cache, node);
458}
459
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400460static void update_backref_node(struct backref_cache *cache,
461 struct backref_node *node, u64 bytenr)
462{
463 struct rb_node *rb_node;
464 rb_erase(&node->rb_node, &cache->rb_root);
465 node->bytenr = bytenr;
466 rb_node = tree_insert(&cache->rb_root, node->bytenr, &node->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -0400467 if (rb_node)
468 backref_tree_panic(rb_node, -EEXIST, bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400469}
470
471/*
472 * update backref cache after a transaction commit
473 */
474static int update_backref_cache(struct btrfs_trans_handle *trans,
475 struct backref_cache *cache)
476{
477 struct backref_node *node;
478 int level = 0;
479
480 if (cache->last_trans == 0) {
481 cache->last_trans = trans->transid;
482 return 0;
483 }
484
485 if (cache->last_trans == trans->transid)
486 return 0;
487
488 /*
489 * detached nodes are used to avoid unnecessary backref
490 * lookup. transaction commit changes the extent tree.
491 * so the detached nodes are no longer useful.
492 */
493 while (!list_empty(&cache->detached)) {
494 node = list_entry(cache->detached.next,
495 struct backref_node, list);
496 remove_backref_node(cache, node);
497 }
498
499 while (!list_empty(&cache->changed)) {
500 node = list_entry(cache->changed.next,
501 struct backref_node, list);
502 list_del_init(&node->list);
503 BUG_ON(node->pending);
504 update_backref_node(cache, node, node->new_bytenr);
505 }
506
507 /*
508 * some nodes can be left in the pending list if there were
509 * errors during processing the pending nodes.
510 */
511 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
512 list_for_each_entry(node, &cache->pending[level], list) {
513 BUG_ON(!node->pending);
514 if (node->bytenr == node->new_bytenr)
515 continue;
516 update_backref_node(cache, node, node->new_bytenr);
517 }
518 }
519
520 cache->last_trans = 0;
521 return 1;
522}
523
David Sterbaf2a97a92011-05-05 12:44:41 +0200524
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400525static int should_ignore_root(struct btrfs_root *root)
526{
527 struct btrfs_root *reloc_root;
528
529 if (!root->ref_cows)
530 return 0;
531
532 reloc_root = root->reloc_root;
533 if (!reloc_root)
534 return 0;
535
536 if (btrfs_root_last_snapshot(&reloc_root->root_item) ==
537 root->fs_info->running_transaction->transid - 1)
538 return 0;
539 /*
540 * if there is reloc tree and it was created in previous
541 * transaction backref lookup can find the reloc tree,
542 * so backref node for the fs tree root is useless for
543 * relocation.
544 */
545 return 1;
546}
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400547/*
548 * find reloc tree by address of tree root
549 */
550static struct btrfs_root *find_reloc_root(struct reloc_control *rc,
551 u64 bytenr)
552{
553 struct rb_node *rb_node;
554 struct mapping_node *node;
555 struct btrfs_root *root = NULL;
556
557 spin_lock(&rc->reloc_root_tree.lock);
558 rb_node = tree_search(&rc->reloc_root_tree.rb_root, bytenr);
559 if (rb_node) {
560 node = rb_entry(rb_node, struct mapping_node, rb_node);
561 root = (struct btrfs_root *)node->data;
562 }
563 spin_unlock(&rc->reloc_root_tree.lock);
564 return root;
565}
566
567static int is_cowonly_root(u64 root_objectid)
568{
569 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
570 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
571 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
572 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
573 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
574 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
575 return 1;
576 return 0;
577}
578
579static struct btrfs_root *read_fs_root(struct btrfs_fs_info *fs_info,
580 u64 root_objectid)
581{
582 struct btrfs_key key;
583
584 key.objectid = root_objectid;
585 key.type = BTRFS_ROOT_ITEM_KEY;
586 if (is_cowonly_root(root_objectid))
587 key.offset = 0;
588 else
589 key.offset = (u64)-1;
590
Miao Xiec00869f2013-09-25 21:47:44 +0800591 return btrfs_get_fs_root(fs_info, &key, false);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400592}
593
594#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
595static noinline_for_stack
596struct btrfs_root *find_tree_root(struct reloc_control *rc,
597 struct extent_buffer *leaf,
598 struct btrfs_extent_ref_v0 *ref0)
599{
600 struct btrfs_root *root;
601 u64 root_objectid = btrfs_ref_root_v0(leaf, ref0);
602 u64 generation = btrfs_ref_generation_v0(leaf, ref0);
603
604 BUG_ON(root_objectid == BTRFS_TREE_RELOC_OBJECTID);
605
606 root = read_fs_root(rc->extent_root->fs_info, root_objectid);
607 BUG_ON(IS_ERR(root));
608
609 if (root->ref_cows &&
610 generation != btrfs_root_generation(&root->root_item))
611 return NULL;
612
613 return root;
614}
615#endif
616
617static noinline_for_stack
618int find_inline_backref(struct extent_buffer *leaf, int slot,
619 unsigned long *ptr, unsigned long *end)
620{
Josef Bacik3173a182013-03-07 14:22:04 -0500621 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400622 struct btrfs_extent_item *ei;
623 struct btrfs_tree_block_info *bi;
624 u32 item_size;
625
Josef Bacik3173a182013-03-07 14:22:04 -0500626 btrfs_item_key_to_cpu(leaf, &key, slot);
627
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400628 item_size = btrfs_item_size_nr(leaf, slot);
629#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
630 if (item_size < sizeof(*ei)) {
631 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
632 return 1;
633 }
634#endif
635 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
636 WARN_ON(!(btrfs_extent_flags(leaf, ei) &
637 BTRFS_EXTENT_FLAG_TREE_BLOCK));
638
Josef Bacik3173a182013-03-07 14:22:04 -0500639 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
640 item_size <= sizeof(*ei) + sizeof(*bi)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400641 WARN_ON(item_size < sizeof(*ei) + sizeof(*bi));
642 return 1;
643 }
Josef Bacikd062d132013-07-30 15:44:09 -0400644 if (key.type == BTRFS_METADATA_ITEM_KEY &&
645 item_size <= sizeof(*ei)) {
646 WARN_ON(item_size < sizeof(*ei));
647 return 1;
648 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400649
Josef Bacik3173a182013-03-07 14:22:04 -0500650 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
651 bi = (struct btrfs_tree_block_info *)(ei + 1);
652 *ptr = (unsigned long)(bi + 1);
653 } else {
654 *ptr = (unsigned long)(ei + 1);
655 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400656 *end = (unsigned long)ei + item_size;
657 return 0;
658}
659
660/*
661 * build backref tree for a given tree block. root of the backref tree
662 * corresponds the tree block, leaves of the backref tree correspond
663 * roots of b-trees that reference the tree block.
664 *
665 * the basic idea of this function is check backrefs of a given block
666 * to find upper level blocks that refernece the block, and then check
667 * bakcrefs of these upper level blocks recursively. the recursion stop
668 * when tree root is reached or backrefs for the block is cached.
669 *
670 * NOTE: if we find backrefs for a block are cached, we know backrefs
671 * for all upper level blocks that directly/indirectly reference the
672 * block are also cached.
673 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400674static noinline_for_stack
675struct backref_node *build_backref_tree(struct reloc_control *rc,
676 struct btrfs_key *node_key,
677 int level, u64 bytenr)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400678{
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400679 struct backref_cache *cache = &rc->backref_cache;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400680 struct btrfs_path *path1;
681 struct btrfs_path *path2;
682 struct extent_buffer *eb;
683 struct btrfs_root *root;
684 struct backref_node *cur;
685 struct backref_node *upper;
686 struct backref_node *lower;
687 struct backref_node *node = NULL;
688 struct backref_node *exist = NULL;
689 struct backref_edge *edge;
690 struct rb_node *rb_node;
691 struct btrfs_key key;
692 unsigned long end;
693 unsigned long ptr;
694 LIST_HEAD(list);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400695 LIST_HEAD(useless);
696 int cowonly;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400697 int ret;
698 int err = 0;
Josef Bacikb6c60c82013-07-30 16:30:30 -0400699 bool need_check = true;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400700
701 path1 = btrfs_alloc_path();
702 path2 = btrfs_alloc_path();
703 if (!path1 || !path2) {
704 err = -ENOMEM;
705 goto out;
706 }
Josef Bacik026fd312011-05-13 10:32:11 -0400707 path1->reada = 1;
708 path2->reada = 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400709
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400710 node = alloc_backref_node(cache);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400711 if (!node) {
712 err = -ENOMEM;
713 goto out;
714 }
715
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400716 node->bytenr = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400717 node->level = level;
718 node->lowest = 1;
719 cur = node;
720again:
721 end = 0;
722 ptr = 0;
723 key.objectid = cur->bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -0500724 key.type = BTRFS_METADATA_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400725 key.offset = (u64)-1;
726
727 path1->search_commit_root = 1;
728 path1->skip_locking = 1;
729 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path1,
730 0, 0);
731 if (ret < 0) {
732 err = ret;
733 goto out;
734 }
735 BUG_ON(!ret || !path1->slots[0]);
736
737 path1->slots[0]--;
738
739 WARN_ON(cur->checked);
740 if (!list_empty(&cur->upper)) {
741 /*
Justin P. Mattock70f23fd2011-05-10 10:16:21 +0200742 * the backref was added previously when processing
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400743 * backref of type BTRFS_TREE_BLOCK_REF_KEY
744 */
745 BUG_ON(!list_is_singular(&cur->upper));
746 edge = list_entry(cur->upper.next, struct backref_edge,
747 list[LOWER]);
748 BUG_ON(!list_empty(&edge->list[UPPER]));
749 exist = edge->node[UPPER];
750 /*
751 * add the upper level block to pending list if we need
752 * check its backrefs
753 */
754 if (!exist->checked)
755 list_add_tail(&edge->list[UPPER], &list);
756 } else {
757 exist = NULL;
758 }
759
760 while (1) {
761 cond_resched();
762 eb = path1->nodes[0];
763
764 if (ptr >= end) {
765 if (path1->slots[0] >= btrfs_header_nritems(eb)) {
766 ret = btrfs_next_leaf(rc->extent_root, path1);
767 if (ret < 0) {
768 err = ret;
769 goto out;
770 }
771 if (ret > 0)
772 break;
773 eb = path1->nodes[0];
774 }
775
776 btrfs_item_key_to_cpu(eb, &key, path1->slots[0]);
777 if (key.objectid != cur->bytenr) {
778 WARN_ON(exist);
779 break;
780 }
781
Josef Bacik3173a182013-03-07 14:22:04 -0500782 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
783 key.type == BTRFS_METADATA_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400784 ret = find_inline_backref(eb, path1->slots[0],
785 &ptr, &end);
786 if (ret)
787 goto next;
788 }
789 }
790
791 if (ptr < end) {
792 /* update key for inline back ref */
793 struct btrfs_extent_inline_ref *iref;
794 iref = (struct btrfs_extent_inline_ref *)ptr;
795 key.type = btrfs_extent_inline_ref_type(eb, iref);
796 key.offset = btrfs_extent_inline_ref_offset(eb, iref);
797 WARN_ON(key.type != BTRFS_TREE_BLOCK_REF_KEY &&
798 key.type != BTRFS_SHARED_BLOCK_REF_KEY);
799 }
800
801 if (exist &&
802 ((key.type == BTRFS_TREE_BLOCK_REF_KEY &&
803 exist->owner == key.offset) ||
804 (key.type == BTRFS_SHARED_BLOCK_REF_KEY &&
805 exist->bytenr == key.offset))) {
806 exist = NULL;
807 goto next;
808 }
809
810#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
811 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY ||
812 key.type == BTRFS_EXTENT_REF_V0_KEY) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400813 if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400814 struct btrfs_extent_ref_v0 *ref0;
815 ref0 = btrfs_item_ptr(eb, path1->slots[0],
816 struct btrfs_extent_ref_v0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400817 if (key.objectid == key.offset) {
Yan, Zheng046f2642010-05-31 08:58:47 +0000818 root = find_tree_root(rc, eb, ref0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400819 if (root && !should_ignore_root(root))
820 cur->root = root;
821 else
822 list_add(&cur->list, &useless);
823 break;
824 }
Yan, Zheng046f2642010-05-31 08:58:47 +0000825 if (is_cowonly_root(btrfs_ref_root_v0(eb,
826 ref0)))
827 cur->cowonly = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400828 }
829#else
830 BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY);
831 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
832#endif
833 if (key.objectid == key.offset) {
834 /*
835 * only root blocks of reloc trees use
836 * backref of this type.
837 */
838 root = find_reloc_root(rc, cur->bytenr);
839 BUG_ON(!root);
840 cur->root = root;
841 break;
842 }
843
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400844 edge = alloc_backref_edge(cache);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400845 if (!edge) {
846 err = -ENOMEM;
847 goto out;
848 }
849 rb_node = tree_search(&cache->rb_root, key.offset);
850 if (!rb_node) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400851 upper = alloc_backref_node(cache);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400852 if (!upper) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400853 free_backref_edge(cache, edge);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400854 err = -ENOMEM;
855 goto out;
856 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400857 upper->bytenr = key.offset;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400858 upper->level = cur->level + 1;
859 /*
860 * backrefs for the upper level block isn't
861 * cached, add the block to pending list
862 */
863 list_add_tail(&edge->list[UPPER], &list);
864 } else {
865 upper = rb_entry(rb_node, struct backref_node,
866 rb_node);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400867 BUG_ON(!upper->checked);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400868 INIT_LIST_HEAD(&edge->list[UPPER]);
869 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400870 list_add_tail(&edge->list[LOWER], &cur->upper);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400871 edge->node[LOWER] = cur;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400872 edge->node[UPPER] = upper;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400873
874 goto next;
875 } else if (key.type != BTRFS_TREE_BLOCK_REF_KEY) {
876 goto next;
877 }
878
879 /* key.type == BTRFS_TREE_BLOCK_REF_KEY */
880 root = read_fs_root(rc->extent_root->fs_info, key.offset);
881 if (IS_ERR(root)) {
882 err = PTR_ERR(root);
883 goto out;
884 }
885
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400886 if (!root->ref_cows)
887 cur->cowonly = 1;
888
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400889 if (btrfs_root_level(&root->root_item) == cur->level) {
890 /* tree root */
891 BUG_ON(btrfs_root_bytenr(&root->root_item) !=
892 cur->bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400893 if (should_ignore_root(root))
894 list_add(&cur->list, &useless);
895 else
896 cur->root = root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400897 break;
898 }
899
900 level = cur->level + 1;
901
902 /*
903 * searching the tree to find upper level blocks
904 * reference the block.
905 */
906 path2->search_commit_root = 1;
907 path2->skip_locking = 1;
908 path2->lowest_level = level;
909 ret = btrfs_search_slot(NULL, root, node_key, path2, 0, 0);
910 path2->lowest_level = 0;
911 if (ret < 0) {
912 err = ret;
913 goto out;
914 }
Yan Zheng33c66f42009-07-22 09:59:00 -0400915 if (ret > 0 && path2->slots[level] > 0)
916 path2->slots[level]--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400917
918 eb = path2->nodes[level];
919 WARN_ON(btrfs_node_blockptr(eb, path2->slots[level]) !=
920 cur->bytenr);
921
922 lower = cur;
Josef Bacikb6c60c82013-07-30 16:30:30 -0400923 need_check = true;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400924 for (; level < BTRFS_MAX_LEVEL; level++) {
925 if (!path2->nodes[level]) {
926 BUG_ON(btrfs_root_bytenr(&root->root_item) !=
927 lower->bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400928 if (should_ignore_root(root))
929 list_add(&lower->list, &useless);
930 else
931 lower->root = root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400932 break;
933 }
934
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400935 edge = alloc_backref_edge(cache);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400936 if (!edge) {
937 err = -ENOMEM;
938 goto out;
939 }
940
941 eb = path2->nodes[level];
942 rb_node = tree_search(&cache->rb_root, eb->start);
943 if (!rb_node) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400944 upper = alloc_backref_node(cache);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400945 if (!upper) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400946 free_backref_edge(cache, edge);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400947 err = -ENOMEM;
948 goto out;
949 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400950 upper->bytenr = eb->start;
951 upper->owner = btrfs_header_owner(eb);
952 upper->level = lower->level + 1;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400953 if (!root->ref_cows)
954 upper->cowonly = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400955
956 /*
957 * if we know the block isn't shared
958 * we can void checking its backrefs.
959 */
960 if (btrfs_block_can_be_shared(root, eb))
961 upper->checked = 0;
962 else
963 upper->checked = 1;
964
965 /*
966 * add the block to pending list if we
Josef Bacikb6c60c82013-07-30 16:30:30 -0400967 * need check its backrefs, we only do this once
968 * while walking up a tree as we will catch
969 * anything else later on.
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400970 */
Josef Bacikb6c60c82013-07-30 16:30:30 -0400971 if (!upper->checked && need_check) {
972 need_check = false;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400973 list_add_tail(&edge->list[UPPER],
974 &list);
975 } else
976 INIT_LIST_HEAD(&edge->list[UPPER]);
977 } else {
978 upper = rb_entry(rb_node, struct backref_node,
979 rb_node);
980 BUG_ON(!upper->checked);
981 INIT_LIST_HEAD(&edge->list[UPPER]);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400982 if (!upper->owner)
983 upper->owner = btrfs_header_owner(eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400984 }
985 list_add_tail(&edge->list[LOWER], &lower->upper);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400986 edge->node[LOWER] = lower;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400987 edge->node[UPPER] = upper;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400988
989 if (rb_node)
990 break;
991 lower = upper;
992 upper = NULL;
993 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200994 btrfs_release_path(path2);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400995next:
996 if (ptr < end) {
997 ptr += btrfs_extent_inline_ref_size(key.type);
998 if (ptr >= end) {
999 WARN_ON(ptr > end);
1000 ptr = 0;
1001 end = 0;
1002 }
1003 }
1004 if (ptr >= end)
1005 path1->slots[0]++;
1006 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001007 btrfs_release_path(path1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001008
1009 cur->checked = 1;
1010 WARN_ON(exist);
1011
1012 /* the pending list isn't empty, take the first block to process */
1013 if (!list_empty(&list)) {
1014 edge = list_entry(list.next, struct backref_edge, list[UPPER]);
1015 list_del_init(&edge->list[UPPER]);
1016 cur = edge->node[UPPER];
1017 goto again;
1018 }
1019
1020 /*
1021 * everything goes well, connect backref nodes and insert backref nodes
1022 * into the cache.
1023 */
1024 BUG_ON(!node->checked);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001025 cowonly = node->cowonly;
1026 if (!cowonly) {
1027 rb_node = tree_insert(&cache->rb_root, node->bytenr,
1028 &node->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -04001029 if (rb_node)
1030 backref_tree_panic(rb_node, -EEXIST, node->bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001031 list_add_tail(&node->lower, &cache->leaves);
1032 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001033
1034 list_for_each_entry(edge, &node->upper, list[LOWER])
1035 list_add_tail(&edge->list[UPPER], &list);
1036
1037 while (!list_empty(&list)) {
1038 edge = list_entry(list.next, struct backref_edge, list[UPPER]);
1039 list_del_init(&edge->list[UPPER]);
1040 upper = edge->node[UPPER];
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001041 if (upper->detached) {
1042 list_del(&edge->list[LOWER]);
1043 lower = edge->node[LOWER];
1044 free_backref_edge(cache, edge);
1045 if (list_empty(&lower->upper))
1046 list_add(&lower->list, &useless);
1047 continue;
1048 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001049
1050 if (!RB_EMPTY_NODE(&upper->rb_node)) {
1051 if (upper->lowest) {
1052 list_del_init(&upper->lower);
1053 upper->lowest = 0;
1054 }
1055
1056 list_add_tail(&edge->list[UPPER], &upper->lower);
1057 continue;
1058 }
1059
1060 BUG_ON(!upper->checked);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001061 BUG_ON(cowonly != upper->cowonly);
1062 if (!cowonly) {
1063 rb_node = tree_insert(&cache->rb_root, upper->bytenr,
1064 &upper->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -04001065 if (rb_node)
1066 backref_tree_panic(rb_node, -EEXIST,
1067 upper->bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001068 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001069
1070 list_add_tail(&edge->list[UPPER], &upper->lower);
1071
1072 list_for_each_entry(edge, &upper->upper, list[LOWER])
1073 list_add_tail(&edge->list[UPPER], &list);
1074 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001075 /*
1076 * process useless backref nodes. backref nodes for tree leaves
1077 * are deleted from the cache. backref nodes for upper level
1078 * tree blocks are left in the cache to avoid unnecessary backref
1079 * lookup.
1080 */
1081 while (!list_empty(&useless)) {
1082 upper = list_entry(useless.next, struct backref_node, list);
1083 list_del_init(&upper->list);
1084 BUG_ON(!list_empty(&upper->upper));
1085 if (upper == node)
1086 node = NULL;
1087 if (upper->lowest) {
1088 list_del_init(&upper->lower);
1089 upper->lowest = 0;
1090 }
1091 while (!list_empty(&upper->lower)) {
1092 edge = list_entry(upper->lower.next,
1093 struct backref_edge, list[UPPER]);
1094 list_del(&edge->list[UPPER]);
1095 list_del(&edge->list[LOWER]);
1096 lower = edge->node[LOWER];
1097 free_backref_edge(cache, edge);
1098
1099 if (list_empty(&lower->upper))
1100 list_add(&lower->list, &useless);
1101 }
1102 __mark_block_processed(rc, upper);
1103 if (upper->level > 0) {
1104 list_add(&upper->list, &cache->detached);
1105 upper->detached = 1;
1106 } else {
1107 rb_erase(&upper->rb_node, &cache->rb_root);
1108 free_backref_node(cache, upper);
1109 }
1110 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001111out:
1112 btrfs_free_path(path1);
1113 btrfs_free_path(path2);
1114 if (err) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001115 while (!list_empty(&useless)) {
1116 lower = list_entry(useless.next,
1117 struct backref_node, upper);
1118 list_del_init(&lower->upper);
1119 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001120 upper = node;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001121 INIT_LIST_HEAD(&list);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001122 while (upper) {
1123 if (RB_EMPTY_NODE(&upper->rb_node)) {
1124 list_splice_tail(&upper->upper, &list);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001125 free_backref_node(cache, upper);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001126 }
1127
1128 if (list_empty(&list))
1129 break;
1130
1131 edge = list_entry(list.next, struct backref_edge,
1132 list[LOWER]);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001133 list_del(&edge->list[LOWER]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001134 upper = edge->node[UPPER];
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001135 free_backref_edge(cache, edge);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001136 }
1137 return ERR_PTR(err);
1138 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001139 BUG_ON(node && node->detached);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001140 return node;
1141}
1142
1143/*
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001144 * helper to add backref node for the newly created snapshot.
1145 * the backref node is created by cloning backref node that
1146 * corresponds to root of source tree
1147 */
1148static int clone_backref_node(struct btrfs_trans_handle *trans,
1149 struct reloc_control *rc,
1150 struct btrfs_root *src,
1151 struct btrfs_root *dest)
1152{
1153 struct btrfs_root *reloc_root = src->reloc_root;
1154 struct backref_cache *cache = &rc->backref_cache;
1155 struct backref_node *node = NULL;
1156 struct backref_node *new_node;
1157 struct backref_edge *edge;
1158 struct backref_edge *new_edge;
1159 struct rb_node *rb_node;
1160
1161 if (cache->last_trans > 0)
1162 update_backref_cache(trans, cache);
1163
1164 rb_node = tree_search(&cache->rb_root, src->commit_root->start);
1165 if (rb_node) {
1166 node = rb_entry(rb_node, struct backref_node, rb_node);
1167 if (node->detached)
1168 node = NULL;
1169 else
1170 BUG_ON(node->new_bytenr != reloc_root->node->start);
1171 }
1172
1173 if (!node) {
1174 rb_node = tree_search(&cache->rb_root,
1175 reloc_root->commit_root->start);
1176 if (rb_node) {
1177 node = rb_entry(rb_node, struct backref_node,
1178 rb_node);
1179 BUG_ON(node->detached);
1180 }
1181 }
1182
1183 if (!node)
1184 return 0;
1185
1186 new_node = alloc_backref_node(cache);
1187 if (!new_node)
1188 return -ENOMEM;
1189
1190 new_node->bytenr = dest->node->start;
1191 new_node->level = node->level;
1192 new_node->lowest = node->lowest;
Yan, Zheng6848ad62011-02-14 16:00:03 -05001193 new_node->checked = 1;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001194 new_node->root = dest;
1195
1196 if (!node->lowest) {
1197 list_for_each_entry(edge, &node->lower, list[UPPER]) {
1198 new_edge = alloc_backref_edge(cache);
1199 if (!new_edge)
1200 goto fail;
1201
1202 new_edge->node[UPPER] = new_node;
1203 new_edge->node[LOWER] = edge->node[LOWER];
1204 list_add_tail(&new_edge->list[UPPER],
1205 &new_node->lower);
1206 }
Miao Xie76b9e232011-11-10 20:45:05 -05001207 } else {
1208 list_add_tail(&new_node->lower, &cache->leaves);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001209 }
1210
1211 rb_node = tree_insert(&cache->rb_root, new_node->bytenr,
1212 &new_node->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -04001213 if (rb_node)
1214 backref_tree_panic(rb_node, -EEXIST, new_node->bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001215
1216 if (!new_node->lowest) {
1217 list_for_each_entry(new_edge, &new_node->lower, list[UPPER]) {
1218 list_add_tail(&new_edge->list[LOWER],
1219 &new_edge->node[LOWER]->upper);
1220 }
1221 }
1222 return 0;
1223fail:
1224 while (!list_empty(&new_node->lower)) {
1225 new_edge = list_entry(new_node->lower.next,
1226 struct backref_edge, list[UPPER]);
1227 list_del(&new_edge->list[UPPER]);
1228 free_backref_edge(cache, new_edge);
1229 }
1230 free_backref_node(cache, new_node);
1231 return -ENOMEM;
1232}
1233
1234/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001235 * helper to add 'address of tree root -> reloc tree' mapping
1236 */
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001237static int __must_check __add_reloc_root(struct btrfs_root *root)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001238{
1239 struct rb_node *rb_node;
1240 struct mapping_node *node;
1241 struct reloc_control *rc = root->fs_info->reloc_ctl;
1242
1243 node = kmalloc(sizeof(*node), GFP_NOFS);
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001244 if (!node)
1245 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001246
1247 node->bytenr = root->node->start;
1248 node->data = root;
1249
1250 spin_lock(&rc->reloc_root_tree.lock);
1251 rb_node = tree_insert(&rc->reloc_root_tree.rb_root,
1252 node->bytenr, &node->rb_node);
1253 spin_unlock(&rc->reloc_root_tree.lock);
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001254 if (rb_node) {
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001255 btrfs_panic(root->fs_info, -EEXIST, "Duplicate root found "
1256 "for start=%llu while inserting into relocation "
Joe Perches533574c2012-07-30 14:40:13 -07001257 "tree\n", node->bytenr);
Dan Carpenter23291a02012-06-25 05:15:23 -06001258 kfree(node);
1259 return -EEXIST;
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001260 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001261
1262 list_add_tail(&root->root_list, &rc->reloc_roots);
1263 return 0;
1264}
1265
1266/*
Wang Shilongc974c462013-12-11 19:29:51 +08001267 * helper to delete the 'address of tree root -> reloc tree'
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001268 * mapping
1269 */
Wang Shilongc974c462013-12-11 19:29:51 +08001270static void __del_reloc_root(struct btrfs_root *root)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001271{
1272 struct rb_node *rb_node;
1273 struct mapping_node *node = NULL;
1274 struct reloc_control *rc = root->fs_info->reloc_ctl;
1275
1276 spin_lock(&rc->reloc_root_tree.lock);
1277 rb_node = tree_search(&rc->reloc_root_tree.rb_root,
Wang Shilongc974c462013-12-11 19:29:51 +08001278 root->node->start);
1279 if (rb_node) {
1280 node = rb_entry(rb_node, struct mapping_node, rb_node);
1281 rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
1282 }
1283 spin_unlock(&rc->reloc_root_tree.lock);
1284
1285 if (!node)
1286 return;
1287 BUG_ON((struct btrfs_root *)node->data != root);
1288
1289 spin_lock(&root->fs_info->trans_lock);
1290 list_del_init(&root->root_list);
1291 spin_unlock(&root->fs_info->trans_lock);
1292 kfree(node);
1293}
1294
1295/*
1296 * helper to update the 'address of tree root -> reloc tree'
1297 * mapping
1298 */
1299static int __update_reloc_root(struct btrfs_root *root, u64 new_bytenr)
1300{
1301 struct rb_node *rb_node;
1302 struct mapping_node *node = NULL;
1303 struct reloc_control *rc = root->fs_info->reloc_ctl;
1304
1305 spin_lock(&rc->reloc_root_tree.lock);
1306 rb_node = tree_search(&rc->reloc_root_tree.rb_root,
1307 root->node->start);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001308 if (rb_node) {
1309 node = rb_entry(rb_node, struct mapping_node, rb_node);
1310 rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
1311 }
1312 spin_unlock(&rc->reloc_root_tree.lock);
1313
Liu Bo8f71f3e2013-03-04 16:25:36 +00001314 if (!node)
1315 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001316 BUG_ON((struct btrfs_root *)node->data != root);
1317
Wang Shilongc974c462013-12-11 19:29:51 +08001318 spin_lock(&rc->reloc_root_tree.lock);
1319 node->bytenr = new_bytenr;
1320 rb_node = tree_insert(&rc->reloc_root_tree.rb_root,
1321 node->bytenr, &node->rb_node);
1322 spin_unlock(&rc->reloc_root_tree.lock);
1323 if (rb_node)
1324 backref_tree_panic(rb_node, -EEXIST, node->bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001325 return 0;
1326}
1327
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001328static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans,
1329 struct btrfs_root *root, u64 objectid)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001330{
1331 struct btrfs_root *reloc_root;
1332 struct extent_buffer *eb;
1333 struct btrfs_root_item *root_item;
1334 struct btrfs_key root_key;
Miao Xie5bc72472013-06-06 03:28:03 +00001335 u64 last_snap = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001336 int ret;
1337
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001338 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
1339 BUG_ON(!root_item);
1340
1341 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
1342 root_key.type = BTRFS_ROOT_ITEM_KEY;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001343 root_key.offset = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001344
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001345 if (root->root_key.objectid == objectid) {
1346 /* called by btrfs_init_reloc_root */
1347 ret = btrfs_copy_root(trans, root, root->commit_root, &eb,
1348 BTRFS_TREE_RELOC_OBJECTID);
1349 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001350
Miao Xie5bc72472013-06-06 03:28:03 +00001351 last_snap = btrfs_root_last_snapshot(&root->root_item);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001352 btrfs_set_root_last_snapshot(&root->root_item,
1353 trans->transid - 1);
1354 } else {
1355 /*
1356 * called by btrfs_reloc_post_snapshot_hook.
1357 * the source tree is a reloc tree, all tree blocks
1358 * modified after it was created have RELOC flag
1359 * set in their headers. so it's OK to not update
1360 * the 'last_snapshot'.
1361 */
1362 ret = btrfs_copy_root(trans, root, root->node, &eb,
1363 BTRFS_TREE_RELOC_OBJECTID);
1364 BUG_ON(ret);
1365 }
1366
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001367 memcpy(root_item, &root->root_item, sizeof(*root_item));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001368 btrfs_set_root_bytenr(root_item, eb->start);
1369 btrfs_set_root_level(root_item, btrfs_header_level(eb));
1370 btrfs_set_root_generation(root_item, trans->transid);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001371
1372 if (root->root_key.objectid == objectid) {
1373 btrfs_set_root_refs(root_item, 0);
1374 memset(&root_item->drop_progress, 0,
1375 sizeof(struct btrfs_disk_key));
1376 root_item->drop_level = 0;
Miao Xie5bc72472013-06-06 03:28:03 +00001377 /*
1378 * abuse rtransid, it is safe because it is impossible to
1379 * receive data into a relocation tree.
1380 */
1381 btrfs_set_root_rtransid(root_item, last_snap);
1382 btrfs_set_root_otransid(root_item, trans->transid);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001383 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001384
1385 btrfs_tree_unlock(eb);
1386 free_extent_buffer(eb);
1387
1388 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
1389 &root_key, root_item);
1390 BUG_ON(ret);
1391 kfree(root_item);
1392
Miao Xiecb517ea2013-05-15 07:48:19 +00001393 reloc_root = btrfs_read_fs_root(root->fs_info->tree_root, &root_key);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001394 BUG_ON(IS_ERR(reloc_root));
1395 reloc_root->last_trans = trans->transid;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001396 return reloc_root;
1397}
1398
1399/*
1400 * create reloc tree for a given fs tree. reloc tree is just a
1401 * snapshot of the fs tree with special root objectid.
1402 */
1403int btrfs_init_reloc_root(struct btrfs_trans_handle *trans,
1404 struct btrfs_root *root)
1405{
1406 struct btrfs_root *reloc_root;
1407 struct reloc_control *rc = root->fs_info->reloc_ctl;
Miao Xie20dd2cb2013-09-25 21:47:45 +08001408 struct btrfs_block_rsv *rsv;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001409 int clear_rsv = 0;
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001410 int ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001411
1412 if (root->reloc_root) {
1413 reloc_root = root->reloc_root;
1414 reloc_root->last_trans = trans->transid;
1415 return 0;
1416 }
1417
1418 if (!rc || !rc->create_reloc_tree ||
1419 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1420 return 0;
1421
Miao Xie20dd2cb2013-09-25 21:47:45 +08001422 if (!trans->reloc_reserved) {
1423 rsv = trans->block_rsv;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001424 trans->block_rsv = rc->block_rsv;
1425 clear_rsv = 1;
1426 }
1427 reloc_root = create_reloc_root(trans, root, root->root_key.objectid);
1428 if (clear_rsv)
Miao Xie20dd2cb2013-09-25 21:47:45 +08001429 trans->block_rsv = rsv;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001430
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001431 ret = __add_reloc_root(reloc_root);
1432 BUG_ON(ret < 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001433 root->reloc_root = reloc_root;
1434 return 0;
1435}
1436
1437/*
1438 * update root item of reloc tree
1439 */
1440int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
1441 struct btrfs_root *root)
1442{
1443 struct btrfs_root *reloc_root;
1444 struct btrfs_root_item *root_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001445 int ret;
1446
1447 if (!root->reloc_root)
Chris Mason75857172011-06-13 20:00:16 -04001448 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001449
1450 reloc_root = root->reloc_root;
1451 root_item = &reloc_root->root_item;
1452
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001453 if (root->fs_info->reloc_ctl->merge_reloc_tree &&
1454 btrfs_root_refs(root_item) == 0) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001455 root->reloc_root = NULL;
Wang Shilongc974c462013-12-11 19:29:51 +08001456 __del_reloc_root(reloc_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001457 }
1458
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001459 if (reloc_root->commit_root != reloc_root->node) {
1460 btrfs_set_root_node(root_item, reloc_root->node);
1461 free_extent_buffer(reloc_root->commit_root);
1462 reloc_root->commit_root = btrfs_root_node(reloc_root);
1463 }
1464
1465 ret = btrfs_update_root(trans, root->fs_info->tree_root,
1466 &reloc_root->root_key, root_item);
1467 BUG_ON(ret);
Chris Mason75857172011-06-13 20:00:16 -04001468
1469out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001470 return 0;
1471}
1472
1473/*
1474 * helper to find first cached inode with inode number >= objectid
1475 * in a subvolume
1476 */
1477static struct inode *find_next_inode(struct btrfs_root *root, u64 objectid)
1478{
1479 struct rb_node *node;
1480 struct rb_node *prev;
1481 struct btrfs_inode *entry;
1482 struct inode *inode;
1483
1484 spin_lock(&root->inode_lock);
1485again:
1486 node = root->inode_tree.rb_node;
1487 prev = NULL;
1488 while (node) {
1489 prev = node;
1490 entry = rb_entry(node, struct btrfs_inode, rb_node);
1491
Li Zefan33345d012011-04-20 10:31:50 +08001492 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001493 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08001494 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001495 node = node->rb_right;
1496 else
1497 break;
1498 }
1499 if (!node) {
1500 while (prev) {
1501 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08001502 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001503 node = prev;
1504 break;
1505 }
1506 prev = rb_next(prev);
1507 }
1508 }
1509 while (node) {
1510 entry = rb_entry(node, struct btrfs_inode, rb_node);
1511 inode = igrab(&entry->vfs_inode);
1512 if (inode) {
1513 spin_unlock(&root->inode_lock);
1514 return inode;
1515 }
1516
Li Zefan33345d012011-04-20 10:31:50 +08001517 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001518 if (cond_resched_lock(&root->inode_lock))
1519 goto again;
1520
1521 node = rb_next(node);
1522 }
1523 spin_unlock(&root->inode_lock);
1524 return NULL;
1525}
1526
1527static int in_block_group(u64 bytenr,
1528 struct btrfs_block_group_cache *block_group)
1529{
1530 if (bytenr >= block_group->key.objectid &&
1531 bytenr < block_group->key.objectid + block_group->key.offset)
1532 return 1;
1533 return 0;
1534}
1535
1536/*
1537 * get new location of data
1538 */
1539static int get_new_location(struct inode *reloc_inode, u64 *new_bytenr,
1540 u64 bytenr, u64 num_bytes)
1541{
1542 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
1543 struct btrfs_path *path;
1544 struct btrfs_file_extent_item *fi;
1545 struct extent_buffer *leaf;
1546 int ret;
1547
1548 path = btrfs_alloc_path();
1549 if (!path)
1550 return -ENOMEM;
1551
1552 bytenr -= BTRFS_I(reloc_inode)->index_cnt;
Li Zefan33345d012011-04-20 10:31:50 +08001553 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(reloc_inode),
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001554 bytenr, 0);
1555 if (ret < 0)
1556 goto out;
1557 if (ret > 0) {
1558 ret = -ENOENT;
1559 goto out;
1560 }
1561
1562 leaf = path->nodes[0];
1563 fi = btrfs_item_ptr(leaf, path->slots[0],
1564 struct btrfs_file_extent_item);
1565
1566 BUG_ON(btrfs_file_extent_offset(leaf, fi) ||
1567 btrfs_file_extent_compression(leaf, fi) ||
1568 btrfs_file_extent_encryption(leaf, fi) ||
1569 btrfs_file_extent_other_encoding(leaf, fi));
1570
1571 if (num_bytes != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001572 ret = -EINVAL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001573 goto out;
1574 }
1575
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001576 *new_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001577 ret = 0;
1578out:
1579 btrfs_free_path(path);
1580 return ret;
1581}
1582
1583/*
1584 * update file extent items in the tree leaf to point to
1585 * the new locations.
1586 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001587static noinline_for_stack
1588int replace_file_extents(struct btrfs_trans_handle *trans,
1589 struct reloc_control *rc,
1590 struct btrfs_root *root,
1591 struct extent_buffer *leaf)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001592{
1593 struct btrfs_key key;
1594 struct btrfs_file_extent_item *fi;
1595 struct inode *inode = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001596 u64 parent;
1597 u64 bytenr;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001598 u64 new_bytenr = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001599 u64 num_bytes;
1600 u64 end;
1601 u32 nritems;
1602 u32 i;
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001603 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001604 int first = 1;
1605 int dirty = 0;
1606
1607 if (rc->stage != UPDATE_DATA_PTRS)
1608 return 0;
1609
1610 /* reloc trees always use full backref */
1611 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1612 parent = leaf->start;
1613 else
1614 parent = 0;
1615
1616 nritems = btrfs_header_nritems(leaf);
1617 for (i = 0; i < nritems; i++) {
1618 cond_resched();
1619 btrfs_item_key_to_cpu(leaf, &key, i);
1620 if (key.type != BTRFS_EXTENT_DATA_KEY)
1621 continue;
1622 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1623 if (btrfs_file_extent_type(leaf, fi) ==
1624 BTRFS_FILE_EXTENT_INLINE)
1625 continue;
1626 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1627 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
1628 if (bytenr == 0)
1629 continue;
1630 if (!in_block_group(bytenr, rc->block_group))
1631 continue;
1632
1633 /*
1634 * if we are modifying block in fs tree, wait for readpage
1635 * to complete and drop the extent cache
1636 */
1637 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001638 if (first) {
1639 inode = find_next_inode(root, key.objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001640 first = 0;
Li Zefan33345d012011-04-20 10:31:50 +08001641 } else if (inode && btrfs_ino(inode) < key.objectid) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001642 btrfs_add_delayed_iput(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001643 inode = find_next_inode(root, key.objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001644 }
Li Zefan33345d012011-04-20 10:31:50 +08001645 if (inode && btrfs_ino(inode) == key.objectid) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001646 end = key.offset +
1647 btrfs_file_extent_num_bytes(leaf, fi);
1648 WARN_ON(!IS_ALIGNED(key.offset,
1649 root->sectorsize));
1650 WARN_ON(!IS_ALIGNED(end, root->sectorsize));
1651 end--;
1652 ret = try_lock_extent(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001653 key.offset, end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001654 if (!ret)
1655 continue;
1656
1657 btrfs_drop_extent_cache(inode, key.offset, end,
1658 1);
1659 unlock_extent(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001660 key.offset, end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001661 }
1662 }
1663
1664 ret = get_new_location(rc->data_inode, &new_bytenr,
1665 bytenr, num_bytes);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001666 if (ret) {
1667 /*
1668 * Don't have to abort since we've not changed anything
1669 * in the file extent yet.
1670 */
1671 break;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001672 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001673
1674 btrfs_set_file_extent_disk_bytenr(leaf, fi, new_bytenr);
1675 dirty = 1;
1676
1677 key.offset -= btrfs_file_extent_offset(leaf, fi);
1678 ret = btrfs_inc_extent_ref(trans, root, new_bytenr,
1679 num_bytes, parent,
1680 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001681 key.objectid, key.offset, 1);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001682 if (ret) {
1683 btrfs_abort_transaction(trans, root, ret);
1684 break;
1685 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001686
1687 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
1688 parent, btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001689 key.objectid, key.offset, 1);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001690 if (ret) {
1691 btrfs_abort_transaction(trans, root, ret);
1692 break;
1693 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001694 }
1695 if (dirty)
1696 btrfs_mark_buffer_dirty(leaf);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001697 if (inode)
1698 btrfs_add_delayed_iput(inode);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001699 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001700}
1701
1702static noinline_for_stack
1703int memcmp_node_keys(struct extent_buffer *eb, int slot,
1704 struct btrfs_path *path, int level)
1705{
1706 struct btrfs_disk_key key1;
1707 struct btrfs_disk_key key2;
1708 btrfs_node_key(eb, &key1, slot);
1709 btrfs_node_key(path->nodes[level], &key2, path->slots[level]);
1710 return memcmp(&key1, &key2, sizeof(key1));
1711}
1712
1713/*
1714 * try to replace tree blocks in fs tree with the new blocks
1715 * in reloc tree. tree blocks haven't been modified since the
1716 * reloc tree was create can be replaced.
1717 *
1718 * if a block was replaced, level of the block + 1 is returned.
1719 * if no block got replaced, 0 is returned. if there are other
1720 * errors, a negative error number is returned.
1721 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001722static noinline_for_stack
1723int replace_path(struct btrfs_trans_handle *trans,
1724 struct btrfs_root *dest, struct btrfs_root *src,
1725 struct btrfs_path *path, struct btrfs_key *next_key,
1726 int lowest_level, int max_level)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001727{
1728 struct extent_buffer *eb;
1729 struct extent_buffer *parent;
1730 struct btrfs_key key;
1731 u64 old_bytenr;
1732 u64 new_bytenr;
1733 u64 old_ptr_gen;
1734 u64 new_ptr_gen;
1735 u64 last_snapshot;
1736 u32 blocksize;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001737 int cow = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001738 int level;
1739 int ret;
1740 int slot;
1741
1742 BUG_ON(src->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
1743 BUG_ON(dest->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001744
1745 last_snapshot = btrfs_root_last_snapshot(&src->root_item);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001746again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001747 slot = path->slots[lowest_level];
1748 btrfs_node_key_to_cpu(path->nodes[lowest_level], &key, slot);
1749
1750 eb = btrfs_lock_root_node(dest);
1751 btrfs_set_lock_blocking(eb);
1752 level = btrfs_header_level(eb);
1753
1754 if (level < lowest_level) {
1755 btrfs_tree_unlock(eb);
1756 free_extent_buffer(eb);
1757 return 0;
1758 }
1759
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001760 if (cow) {
1761 ret = btrfs_cow_block(trans, dest, eb, NULL, 0, &eb);
1762 BUG_ON(ret);
1763 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001764 btrfs_set_lock_blocking(eb);
1765
1766 if (next_key) {
1767 next_key->objectid = (u64)-1;
1768 next_key->type = (u8)-1;
1769 next_key->offset = (u64)-1;
1770 }
1771
1772 parent = eb;
1773 while (1) {
1774 level = btrfs_header_level(parent);
1775 BUG_ON(level < lowest_level);
1776
1777 ret = btrfs_bin_search(parent, &key, level, &slot);
1778 if (ret && slot > 0)
1779 slot--;
1780
1781 if (next_key && slot + 1 < btrfs_header_nritems(parent))
1782 btrfs_node_key_to_cpu(parent, next_key, slot + 1);
1783
1784 old_bytenr = btrfs_node_blockptr(parent, slot);
1785 blocksize = btrfs_level_size(dest, level - 1);
1786 old_ptr_gen = btrfs_node_ptr_generation(parent, slot);
1787
1788 if (level <= max_level) {
1789 eb = path->nodes[level];
1790 new_bytenr = btrfs_node_blockptr(eb,
1791 path->slots[level]);
1792 new_ptr_gen = btrfs_node_ptr_generation(eb,
1793 path->slots[level]);
1794 } else {
1795 new_bytenr = 0;
1796 new_ptr_gen = 0;
1797 }
1798
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301799 if (WARN_ON(new_bytenr > 0 && new_bytenr == old_bytenr)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001800 ret = level;
1801 break;
1802 }
1803
1804 if (new_bytenr == 0 || old_ptr_gen > last_snapshot ||
1805 memcmp_node_keys(parent, slot, path, level)) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001806 if (level <= lowest_level) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001807 ret = 0;
1808 break;
1809 }
1810
1811 eb = read_tree_block(dest, old_bytenr, blocksize,
1812 old_ptr_gen);
Josef Bacik416bc652013-04-23 14:17:42 -04001813 if (!eb || !extent_buffer_uptodate(eb)) {
1814 ret = (!eb) ? -ENOMEM : -EIO;
1815 free_extent_buffer(eb);
Stefan Behrens379cde72013-05-08 08:56:09 +00001816 break;
Josef Bacik416bc652013-04-23 14:17:42 -04001817 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001818 btrfs_tree_lock(eb);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001819 if (cow) {
1820 ret = btrfs_cow_block(trans, dest, eb, parent,
1821 slot, &eb);
1822 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001823 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001824 btrfs_set_lock_blocking(eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001825
1826 btrfs_tree_unlock(parent);
1827 free_extent_buffer(parent);
1828
1829 parent = eb;
1830 continue;
1831 }
1832
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001833 if (!cow) {
1834 btrfs_tree_unlock(parent);
1835 free_extent_buffer(parent);
1836 cow = 1;
1837 goto again;
1838 }
1839
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001840 btrfs_node_key_to_cpu(path->nodes[level], &key,
1841 path->slots[level]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001842 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001843
1844 path->lowest_level = level;
1845 ret = btrfs_search_slot(trans, src, &key, path, 0, 1);
1846 path->lowest_level = 0;
1847 BUG_ON(ret);
1848
1849 /*
1850 * swap blocks in fs tree and reloc tree.
1851 */
1852 btrfs_set_node_blockptr(parent, slot, new_bytenr);
1853 btrfs_set_node_ptr_generation(parent, slot, new_ptr_gen);
1854 btrfs_mark_buffer_dirty(parent);
1855
1856 btrfs_set_node_blockptr(path->nodes[level],
1857 path->slots[level], old_bytenr);
1858 btrfs_set_node_ptr_generation(path->nodes[level],
1859 path->slots[level], old_ptr_gen);
1860 btrfs_mark_buffer_dirty(path->nodes[level]);
1861
1862 ret = btrfs_inc_extent_ref(trans, src, old_bytenr, blocksize,
1863 path->nodes[level]->start,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001864 src->root_key.objectid, level - 1, 0,
1865 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001866 BUG_ON(ret);
1867 ret = btrfs_inc_extent_ref(trans, dest, new_bytenr, blocksize,
1868 0, dest->root_key.objectid, level - 1,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001869 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001870 BUG_ON(ret);
1871
1872 ret = btrfs_free_extent(trans, src, new_bytenr, blocksize,
1873 path->nodes[level]->start,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001874 src->root_key.objectid, level - 1, 0,
1875 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001876 BUG_ON(ret);
1877
1878 ret = btrfs_free_extent(trans, dest, old_bytenr, blocksize,
1879 0, dest->root_key.objectid, level - 1,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001880 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001881 BUG_ON(ret);
1882
1883 btrfs_unlock_up_safe(path, 0);
1884
1885 ret = level;
1886 break;
1887 }
1888 btrfs_tree_unlock(parent);
1889 free_extent_buffer(parent);
1890 return ret;
1891}
1892
1893/*
1894 * helper to find next relocated block in reloc tree
1895 */
1896static noinline_for_stack
1897int walk_up_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
1898 int *level)
1899{
1900 struct extent_buffer *eb;
1901 int i;
1902 u64 last_snapshot;
1903 u32 nritems;
1904
1905 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1906
1907 for (i = 0; i < *level; i++) {
1908 free_extent_buffer(path->nodes[i]);
1909 path->nodes[i] = NULL;
1910 }
1911
1912 for (i = *level; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
1913 eb = path->nodes[i];
1914 nritems = btrfs_header_nritems(eb);
1915 while (path->slots[i] + 1 < nritems) {
1916 path->slots[i]++;
1917 if (btrfs_node_ptr_generation(eb, path->slots[i]) <=
1918 last_snapshot)
1919 continue;
1920
1921 *level = i;
1922 return 0;
1923 }
1924 free_extent_buffer(path->nodes[i]);
1925 path->nodes[i] = NULL;
1926 }
1927 return 1;
1928}
1929
1930/*
1931 * walk down reloc tree to find relocated block of lowest level
1932 */
1933static noinline_for_stack
1934int walk_down_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
1935 int *level)
1936{
1937 struct extent_buffer *eb = NULL;
1938 int i;
1939 u64 bytenr;
1940 u64 ptr_gen = 0;
1941 u64 last_snapshot;
1942 u32 blocksize;
1943 u32 nritems;
1944
1945 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1946
1947 for (i = *level; i > 0; i--) {
1948 eb = path->nodes[i];
1949 nritems = btrfs_header_nritems(eb);
1950 while (path->slots[i] < nritems) {
1951 ptr_gen = btrfs_node_ptr_generation(eb, path->slots[i]);
1952 if (ptr_gen > last_snapshot)
1953 break;
1954 path->slots[i]++;
1955 }
1956 if (path->slots[i] >= nritems) {
1957 if (i == *level)
1958 break;
1959 *level = i + 1;
1960 return 0;
1961 }
1962 if (i == 1) {
1963 *level = i;
1964 return 0;
1965 }
1966
1967 bytenr = btrfs_node_blockptr(eb, path->slots[i]);
1968 blocksize = btrfs_level_size(root, i - 1);
1969 eb = read_tree_block(root, bytenr, blocksize, ptr_gen);
Josef Bacik416bc652013-04-23 14:17:42 -04001970 if (!eb || !extent_buffer_uptodate(eb)) {
1971 free_extent_buffer(eb);
1972 return -EIO;
1973 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001974 BUG_ON(btrfs_header_level(eb) != i - 1);
1975 path->nodes[i - 1] = eb;
1976 path->slots[i - 1] = 0;
1977 }
1978 return 1;
1979}
1980
1981/*
1982 * invalidate extent cache for file extents whose key in range of
1983 * [min_key, max_key)
1984 */
1985static int invalidate_extent_cache(struct btrfs_root *root,
1986 struct btrfs_key *min_key,
1987 struct btrfs_key *max_key)
1988{
1989 struct inode *inode = NULL;
1990 u64 objectid;
1991 u64 start, end;
Li Zefan33345d012011-04-20 10:31:50 +08001992 u64 ino;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001993
1994 objectid = min_key->objectid;
1995 while (1) {
1996 cond_resched();
1997 iput(inode);
1998
1999 if (objectid > max_key->objectid)
2000 break;
2001
2002 inode = find_next_inode(root, objectid);
2003 if (!inode)
2004 break;
Li Zefan33345d012011-04-20 10:31:50 +08002005 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002006
Li Zefan33345d012011-04-20 10:31:50 +08002007 if (ino > max_key->objectid) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002008 iput(inode);
2009 break;
2010 }
2011
Li Zefan33345d012011-04-20 10:31:50 +08002012 objectid = ino + 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002013 if (!S_ISREG(inode->i_mode))
2014 continue;
2015
Li Zefan33345d012011-04-20 10:31:50 +08002016 if (unlikely(min_key->objectid == ino)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002017 if (min_key->type > BTRFS_EXTENT_DATA_KEY)
2018 continue;
2019 if (min_key->type < BTRFS_EXTENT_DATA_KEY)
2020 start = 0;
2021 else {
2022 start = min_key->offset;
2023 WARN_ON(!IS_ALIGNED(start, root->sectorsize));
2024 }
2025 } else {
2026 start = 0;
2027 }
2028
Li Zefan33345d012011-04-20 10:31:50 +08002029 if (unlikely(max_key->objectid == ino)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002030 if (max_key->type < BTRFS_EXTENT_DATA_KEY)
2031 continue;
2032 if (max_key->type > BTRFS_EXTENT_DATA_KEY) {
2033 end = (u64)-1;
2034 } else {
2035 if (max_key->offset == 0)
2036 continue;
2037 end = max_key->offset;
2038 WARN_ON(!IS_ALIGNED(end, root->sectorsize));
2039 end--;
2040 }
2041 } else {
2042 end = (u64)-1;
2043 }
2044
2045 /* the lock_extent waits for readpage to complete */
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002046 lock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002047 btrfs_drop_extent_cache(inode, start, end, 1);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002048 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002049 }
2050 return 0;
2051}
2052
2053static int find_next_key(struct btrfs_path *path, int level,
2054 struct btrfs_key *key)
2055
2056{
2057 while (level < BTRFS_MAX_LEVEL) {
2058 if (!path->nodes[level])
2059 break;
2060 if (path->slots[level] + 1 <
2061 btrfs_header_nritems(path->nodes[level])) {
2062 btrfs_node_key_to_cpu(path->nodes[level], key,
2063 path->slots[level] + 1);
2064 return 0;
2065 }
2066 level++;
2067 }
2068 return 1;
2069}
2070
2071/*
2072 * merge the relocated tree blocks in reloc tree with corresponding
2073 * fs tree.
2074 */
2075static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
2076 struct btrfs_root *root)
2077{
2078 LIST_HEAD(inode_list);
2079 struct btrfs_key key;
2080 struct btrfs_key next_key;
Josef Bacik9e6a0c52013-10-31 10:07:19 -04002081 struct btrfs_trans_handle *trans = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002082 struct btrfs_root *reloc_root;
2083 struct btrfs_root_item *root_item;
2084 struct btrfs_path *path;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002085 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002086 int level;
2087 int max_level;
2088 int replaced = 0;
2089 int ret;
2090 int err = 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002091 u32 min_reserved;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002092
2093 path = btrfs_alloc_path();
2094 if (!path)
2095 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04002096 path->reada = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002097
2098 reloc_root = root->reloc_root;
2099 root_item = &reloc_root->root_item;
2100
2101 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2102 level = btrfs_root_level(root_item);
2103 extent_buffer_get(reloc_root->node);
2104 path->nodes[level] = reloc_root->node;
2105 path->slots[level] = 0;
2106 } else {
2107 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2108
2109 level = root_item->drop_level;
2110 BUG_ON(level == 0);
2111 path->lowest_level = level;
2112 ret = btrfs_search_slot(NULL, reloc_root, &key, path, 0, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002113 path->lowest_level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002114 if (ret < 0) {
2115 btrfs_free_path(path);
2116 return ret;
2117 }
2118
2119 btrfs_node_key_to_cpu(path->nodes[level], &next_key,
2120 path->slots[level]);
2121 WARN_ON(memcmp(&key, &next_key, sizeof(key)));
2122
2123 btrfs_unlock_up_safe(path, 0);
2124 }
2125
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002126 min_reserved = root->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002127 memset(&next_key, 0, sizeof(next_key));
2128
2129 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00002130 ret = btrfs_block_rsv_refill(root, rc->block_rsv, min_reserved,
2131 BTRFS_RESERVE_FLUSH_ALL);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002132 if (ret) {
Josef Bacik9e6a0c52013-10-31 10:07:19 -04002133 err = ret;
2134 goto out;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002135 }
Josef Bacik9e6a0c52013-10-31 10:07:19 -04002136 trans = btrfs_start_transaction(root, 0);
2137 if (IS_ERR(trans)) {
2138 err = PTR_ERR(trans);
2139 trans = NULL;
2140 goto out;
2141 }
2142 trans->block_rsv = rc->block_rsv;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002143
2144 replaced = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002145 max_level = level;
2146
2147 ret = walk_down_reloc_tree(reloc_root, path, &level);
2148 if (ret < 0) {
2149 err = ret;
2150 goto out;
2151 }
2152 if (ret > 0)
2153 break;
2154
2155 if (!find_next_key(path, level, &key) &&
2156 btrfs_comp_cpu_keys(&next_key, &key) >= 0) {
2157 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002158 } else {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002159 ret = replace_path(trans, root, reloc_root, path,
2160 &next_key, level, max_level);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002161 }
2162 if (ret < 0) {
2163 err = ret;
2164 goto out;
2165 }
2166
2167 if (ret > 0) {
2168 level = ret;
2169 btrfs_node_key_to_cpu(path->nodes[level], &key,
2170 path->slots[level]);
2171 replaced = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002172 }
2173
2174 ret = walk_up_reloc_tree(reloc_root, path, &level);
2175 if (ret > 0)
2176 break;
2177
2178 BUG_ON(level == 0);
2179 /*
2180 * save the merging progress in the drop_progress.
2181 * this is OK since root refs == 1 in this case.
2182 */
2183 btrfs_node_key(path->nodes[level], &root_item->drop_progress,
2184 path->slots[level]);
2185 root_item->drop_level = level;
2186
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002187 btrfs_end_transaction_throttle(trans, root);
Josef Bacik9e6a0c52013-10-31 10:07:19 -04002188 trans = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002189
Liu Bob53d3f52012-11-14 14:34:34 +00002190 btrfs_btree_balance_dirty(root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002191
2192 if (replaced && rc->stage == UPDATE_DATA_PTRS)
2193 invalidate_extent_cache(root, &key, &next_key);
2194 }
2195
2196 /*
2197 * handle the case only one block in the fs tree need to be
2198 * relocated and the block is tree root.
2199 */
2200 leaf = btrfs_lock_root_node(root);
2201 ret = btrfs_cow_block(trans, root, leaf, NULL, 0, &leaf);
2202 btrfs_tree_unlock(leaf);
2203 free_extent_buffer(leaf);
2204 if (ret < 0)
2205 err = ret;
2206out:
2207 btrfs_free_path(path);
2208
2209 if (err == 0) {
2210 memset(&root_item->drop_progress, 0,
2211 sizeof(root_item->drop_progress));
2212 root_item->drop_level = 0;
2213 btrfs_set_root_refs(root_item, 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002214 btrfs_update_reloc_root(trans, root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002215 }
2216
Josef Bacik9e6a0c52013-10-31 10:07:19 -04002217 if (trans)
2218 btrfs_end_transaction_throttle(trans, root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002219
Liu Bob53d3f52012-11-14 14:34:34 +00002220 btrfs_btree_balance_dirty(root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002221
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002222 if (replaced && rc->stage == UPDATE_DATA_PTRS)
2223 invalidate_extent_cache(root, &key, &next_key);
2224
2225 return err;
2226}
2227
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002228static noinline_for_stack
2229int prepare_to_merge(struct reloc_control *rc, int err)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002230{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002231 struct btrfs_root *root = rc->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002232 struct btrfs_root *reloc_root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002233 struct btrfs_trans_handle *trans;
2234 LIST_HEAD(reloc_roots);
2235 u64 num_bytes = 0;
2236 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002237
Chris Mason75857172011-06-13 20:00:16 -04002238 mutex_lock(&root->fs_info->reloc_mutex);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002239 rc->merging_rsv_size += root->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
2240 rc->merging_rsv_size += rc->nodes_relocated * 2;
Chris Mason75857172011-06-13 20:00:16 -04002241 mutex_unlock(&root->fs_info->reloc_mutex);
2242
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002243again:
2244 if (!err) {
2245 num_bytes = rc->merging_rsv_size;
Miao Xie08e007d2012-10-16 11:33:38 +00002246 ret = btrfs_block_rsv_add(root, rc->block_rsv, num_bytes,
2247 BTRFS_RESERVE_FLUSH_ALL);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002248 if (ret)
2249 err = ret;
2250 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002251
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002252 trans = btrfs_join_transaction(rc->extent_root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00002253 if (IS_ERR(trans)) {
2254 if (!err)
2255 btrfs_block_rsv_release(rc->extent_root,
2256 rc->block_rsv, num_bytes);
2257 return PTR_ERR(trans);
2258 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002259
2260 if (!err) {
2261 if (num_bytes != rc->merging_rsv_size) {
2262 btrfs_end_transaction(trans, rc->extent_root);
2263 btrfs_block_rsv_release(rc->extent_root,
2264 rc->block_rsv, num_bytes);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002265 goto again;
2266 }
2267 }
2268
2269 rc->merge_reloc_tree = 1;
2270
2271 while (!list_empty(&rc->reloc_roots)) {
2272 reloc_root = list_entry(rc->reloc_roots.next,
2273 struct btrfs_root, root_list);
2274 list_del_init(&reloc_root->root_list);
2275
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002276 root = read_fs_root(reloc_root->fs_info,
2277 reloc_root->root_key.offset);
2278 BUG_ON(IS_ERR(root));
2279 BUG_ON(root->reloc_root != reloc_root);
2280
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002281 /*
2282 * set reference count to 1, so btrfs_recover_relocation
2283 * knows it should resumes merging
2284 */
2285 if (!err)
2286 btrfs_set_root_refs(&reloc_root->root_item, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002287 btrfs_update_reloc_root(trans, root);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002288
2289 list_add(&reloc_root->root_list, &reloc_roots);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002290 }
2291
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002292 list_splice(&reloc_roots, &rc->reloc_roots);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002293
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002294 if (!err)
2295 btrfs_commit_transaction(trans, rc->extent_root);
2296 else
2297 btrfs_end_transaction(trans, rc->extent_root);
2298 return err;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002299}
2300
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002301static noinline_for_stack
Liu Boaca1bba2013-03-04 16:25:37 +00002302void free_reloc_roots(struct list_head *list)
2303{
2304 struct btrfs_root *reloc_root;
2305
2306 while (!list_empty(list)) {
2307 reloc_root = list_entry(list->next, struct btrfs_root,
2308 root_list);
Wang Shilongc974c462013-12-11 19:29:51 +08002309 __del_reloc_root(reloc_root);
Liu Boaca1bba2013-03-04 16:25:37 +00002310 free_extent_buffer(reloc_root->node);
2311 free_extent_buffer(reloc_root->commit_root);
2312 kfree(reloc_root);
2313 }
2314}
2315
2316static noinline_for_stack
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002317int merge_reloc_roots(struct reloc_control *rc)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002318{
Miao Xie5bc72472013-06-06 03:28:03 +00002319 struct btrfs_trans_handle *trans;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002320 struct btrfs_root *root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002321 struct btrfs_root *reloc_root;
Miao Xie5bc72472013-06-06 03:28:03 +00002322 u64 last_snap;
2323 u64 otransid;
2324 u64 objectid;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002325 LIST_HEAD(reloc_roots);
2326 int found = 0;
Liu Boaca1bba2013-03-04 16:25:37 +00002327 int ret = 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002328again:
2329 root = rc->extent_root;
Chris Mason75857172011-06-13 20:00:16 -04002330
2331 /*
2332 * this serializes us with btrfs_record_root_in_transaction,
2333 * we have to make sure nobody is in the middle of
2334 * adding their roots to the list while we are
2335 * doing this splice
2336 */
2337 mutex_lock(&root->fs_info->reloc_mutex);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002338 list_splice_init(&rc->reloc_roots, &reloc_roots);
Chris Mason75857172011-06-13 20:00:16 -04002339 mutex_unlock(&root->fs_info->reloc_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002340
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002341 while (!list_empty(&reloc_roots)) {
2342 found = 1;
2343 reloc_root = list_entry(reloc_roots.next,
2344 struct btrfs_root, root_list);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002345
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002346 if (btrfs_root_refs(&reloc_root->root_item) > 0) {
2347 root = read_fs_root(reloc_root->fs_info,
2348 reloc_root->root_key.offset);
2349 BUG_ON(IS_ERR(root));
2350 BUG_ON(root->reloc_root != reloc_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002351
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002352 ret = merge_reloc_root(rc, root);
Josef Bacikb37b39c2013-07-23 16:57:15 -04002353 if (ret) {
Wang Shilongc974c462013-12-11 19:29:51 +08002354 __del_reloc_root(reloc_root);
Josef Bacikb37b39c2013-07-23 16:57:15 -04002355 free_extent_buffer(reloc_root->node);
2356 free_extent_buffer(reloc_root->commit_root);
2357 kfree(reloc_root);
Liu Boaca1bba2013-03-04 16:25:37 +00002358 goto out;
Josef Bacikb37b39c2013-07-23 16:57:15 -04002359 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002360 } else {
2361 list_del_init(&reloc_root->root_list);
2362 }
Miao Xie5bc72472013-06-06 03:28:03 +00002363
2364 /*
2365 * we keep the old last snapshod transid in rtranid when we
2366 * created the relocation tree.
2367 */
2368 last_snap = btrfs_root_rtransid(&reloc_root->root_item);
2369 otransid = btrfs_root_otransid(&reloc_root->root_item);
2370 objectid = reloc_root->root_key.offset;
2371
Jeff Mahoney2c536792011-10-03 23:22:41 -04002372 ret = btrfs_drop_snapshot(reloc_root, rc->block_rsv, 0, 1);
Liu Boaca1bba2013-03-04 16:25:37 +00002373 if (ret < 0) {
2374 if (list_empty(&reloc_root->root_list))
2375 list_add_tail(&reloc_root->root_list,
2376 &reloc_roots);
2377 goto out;
Miao Xie5bc72472013-06-06 03:28:03 +00002378 } else if (!ret) {
2379 /*
2380 * recover the last snapshot tranid to avoid
2381 * the space balance break NOCOW.
2382 */
2383 root = read_fs_root(rc->extent_root->fs_info,
2384 objectid);
2385 if (IS_ERR(root))
2386 continue;
2387
Miao Xie5bc72472013-06-06 03:28:03 +00002388 trans = btrfs_join_transaction(root);
2389 BUG_ON(IS_ERR(trans));
2390
2391 /* Check if the fs/file tree was snapshoted or not. */
2392 if (btrfs_root_last_snapshot(&root->root_item) ==
2393 otransid - 1)
2394 btrfs_set_root_last_snapshot(&root->root_item,
2395 last_snap);
2396
2397 btrfs_end_transaction(trans, root);
Liu Boaca1bba2013-03-04 16:25:37 +00002398 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002399 }
2400
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002401 if (found) {
2402 found = 0;
2403 goto again;
2404 }
Liu Boaca1bba2013-03-04 16:25:37 +00002405out:
2406 if (ret) {
2407 btrfs_std_error(root->fs_info, ret);
2408 if (!list_empty(&reloc_roots))
2409 free_reloc_roots(&reloc_roots);
2410 }
2411
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002412 BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root));
Liu Boaca1bba2013-03-04 16:25:37 +00002413 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002414}
2415
2416static void free_block_list(struct rb_root *blocks)
2417{
2418 struct tree_block *block;
2419 struct rb_node *rb_node;
2420 while ((rb_node = rb_first(blocks))) {
2421 block = rb_entry(rb_node, struct tree_block, rb_node);
2422 rb_erase(rb_node, blocks);
2423 kfree(block);
2424 }
2425}
2426
2427static int record_reloc_root_in_trans(struct btrfs_trans_handle *trans,
2428 struct btrfs_root *reloc_root)
2429{
2430 struct btrfs_root *root;
2431
2432 if (reloc_root->last_trans == trans->transid)
2433 return 0;
2434
2435 root = read_fs_root(reloc_root->fs_info, reloc_root->root_key.offset);
2436 BUG_ON(IS_ERR(root));
2437 BUG_ON(root->reloc_root != reloc_root);
2438
2439 return btrfs_record_root_in_trans(trans, root);
2440}
2441
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002442static noinline_for_stack
2443struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans,
2444 struct reloc_control *rc,
2445 struct backref_node *node,
2446 struct backref_edge *edges[], int *nr)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002447{
2448 struct backref_node *next;
2449 struct btrfs_root *root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002450 int index = 0;
2451
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002452 next = node;
2453 while (1) {
2454 cond_resched();
2455 next = walk_up_backref(next, edges, &index);
2456 root = next->root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002457 BUG_ON(!root);
2458 BUG_ON(!root->ref_cows);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002459
2460 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
2461 record_reloc_root_in_trans(trans, root);
2462 break;
2463 }
2464
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002465 btrfs_record_root_in_trans(trans, root);
2466 root = root->reloc_root;
2467
2468 if (next->new_bytenr != root->node->start) {
2469 BUG_ON(next->new_bytenr);
2470 BUG_ON(!list_empty(&next->list));
2471 next->new_bytenr = root->node->start;
2472 next->root = root;
2473 list_add_tail(&next->list,
2474 &rc->backref_cache.changed);
2475 __mark_block_processed(rc, next);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002476 break;
2477 }
2478
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002479 WARN_ON(1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002480 root = NULL;
2481 next = walk_down_backref(edges, &index);
2482 if (!next || next->level <= node->level)
2483 break;
2484 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002485 if (!root)
2486 return NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002487
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002488 *nr = index;
2489 next = node;
2490 /* setup backref node path for btrfs_reloc_cow_block */
2491 while (1) {
2492 rc->backref_cache.path[next->level] = next;
2493 if (--index < 0)
2494 break;
2495 next = edges[index]->node[UPPER];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002496 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002497 return root;
2498}
2499
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002500/*
2501 * select a tree root for relocation. return NULL if the block
2502 * is reference counted. we should use do_relocation() in this
2503 * case. return a tree root pointer if the block isn't reference
2504 * counted. return -ENOENT if the block is root of reloc tree.
2505 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002506static noinline_for_stack
2507struct btrfs_root *select_one_root(struct btrfs_trans_handle *trans,
2508 struct backref_node *node)
2509{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002510 struct backref_node *next;
2511 struct btrfs_root *root;
2512 struct btrfs_root *fs_root = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002513 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002514 int index = 0;
2515
2516 next = node;
2517 while (1) {
2518 cond_resched();
2519 next = walk_up_backref(next, edges, &index);
2520 root = next->root;
2521 BUG_ON(!root);
2522
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002523 /* no other choice for non-references counted tree */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002524 if (!root->ref_cows)
2525 return root;
2526
2527 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID)
2528 fs_root = root;
2529
2530 if (next != node)
2531 return NULL;
2532
2533 next = walk_down_backref(edges, &index);
2534 if (!next || next->level <= node->level)
2535 break;
2536 }
2537
2538 if (!fs_root)
2539 return ERR_PTR(-ENOENT);
2540 return fs_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002541}
2542
2543static noinline_for_stack
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002544u64 calcu_metadata_size(struct reloc_control *rc,
2545 struct backref_node *node, int reserve)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002546{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002547 struct backref_node *next = node;
2548 struct backref_edge *edge;
2549 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2550 u64 num_bytes = 0;
2551 int index = 0;
2552
2553 BUG_ON(reserve && node->processed);
2554
2555 while (next) {
2556 cond_resched();
2557 while (1) {
2558 if (next->processed && (reserve || next != node))
2559 break;
2560
2561 num_bytes += btrfs_level_size(rc->extent_root,
2562 next->level);
2563
2564 if (list_empty(&next->upper))
2565 break;
2566
2567 edge = list_entry(next->upper.next,
2568 struct backref_edge, list[LOWER]);
2569 edges[index++] = edge;
2570 next = edge->node[UPPER];
2571 }
2572 next = walk_down_backref(edges, &index);
2573 }
2574 return num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002575}
2576
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002577static int reserve_metadata_space(struct btrfs_trans_handle *trans,
2578 struct reloc_control *rc,
2579 struct backref_node *node)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002580{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002581 struct btrfs_root *root = rc->extent_root;
2582 u64 num_bytes;
2583 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002584
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002585 num_bytes = calcu_metadata_size(rc, node, 1) * 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002586
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002587 trans->block_rsv = rc->block_rsv;
Miao Xie08e007d2012-10-16 11:33:38 +00002588 ret = btrfs_block_rsv_add(root, rc->block_rsv, num_bytes,
2589 BTRFS_RESERVE_FLUSH_ALL);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002590 if (ret) {
2591 if (ret == -EAGAIN)
2592 rc->commit_transaction = 1;
2593 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002594 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002595
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002596 return 0;
2597}
2598
2599static void release_metadata_space(struct reloc_control *rc,
2600 struct backref_node *node)
2601{
2602 u64 num_bytes = calcu_metadata_size(rc, node, 0) * 2;
2603 btrfs_block_rsv_release(rc->extent_root, rc->block_rsv, num_bytes);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002604}
2605
2606/*
2607 * relocate a block tree, and then update pointers in upper level
2608 * blocks that reference the block to point to the new location.
2609 *
2610 * if called by link_to_upper, the block has already been relocated.
2611 * in that case this function just updates pointers.
2612 */
2613static int do_relocation(struct btrfs_trans_handle *trans,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002614 struct reloc_control *rc,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002615 struct backref_node *node,
2616 struct btrfs_key *key,
2617 struct btrfs_path *path, int lowest)
2618{
2619 struct backref_node *upper;
2620 struct backref_edge *edge;
2621 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2622 struct btrfs_root *root;
2623 struct extent_buffer *eb;
2624 u32 blocksize;
2625 u64 bytenr;
2626 u64 generation;
2627 int nr;
2628 int slot;
2629 int ret;
2630 int err = 0;
2631
2632 BUG_ON(lowest && node->eb);
2633
2634 path->lowest_level = node->level + 1;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002635 rc->backref_cache.path[node->level] = node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002636 list_for_each_entry(edge, &node->upper, list[LOWER]) {
2637 cond_resched();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002638
2639 upper = edge->node[UPPER];
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002640 root = select_reloc_root(trans, rc, upper, edges, &nr);
2641 BUG_ON(!root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002642
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002643 if (upper->eb && !upper->locked) {
2644 if (!lowest) {
2645 ret = btrfs_bin_search(upper->eb, key,
2646 upper->level, &slot);
2647 BUG_ON(ret);
2648 bytenr = btrfs_node_blockptr(upper->eb, slot);
2649 if (node->eb->start == bytenr)
2650 goto next;
2651 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002652 drop_node_buffer(upper);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002653 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002654
2655 if (!upper->eb) {
2656 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
2657 if (ret < 0) {
2658 err = ret;
2659 break;
2660 }
2661 BUG_ON(ret > 0);
2662
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002663 if (!upper->eb) {
2664 upper->eb = path->nodes[upper->level];
2665 path->nodes[upper->level] = NULL;
2666 } else {
2667 BUG_ON(upper->eb != path->nodes[upper->level]);
2668 }
2669
2670 upper->locked = 1;
2671 path->locks[upper->level] = 0;
2672
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002673 slot = path->slots[upper->level];
David Sterbab3b4aa72011-04-21 01:20:15 +02002674 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002675 } else {
2676 ret = btrfs_bin_search(upper->eb, key, upper->level,
2677 &slot);
2678 BUG_ON(ret);
2679 }
2680
2681 bytenr = btrfs_node_blockptr(upper->eb, slot);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002682 if (lowest) {
2683 BUG_ON(bytenr != node->bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002684 } else {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002685 if (node->eb->start == bytenr)
2686 goto next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002687 }
2688
2689 blocksize = btrfs_level_size(root, node->level);
2690 generation = btrfs_node_ptr_generation(upper->eb, slot);
2691 eb = read_tree_block(root, bytenr, blocksize, generation);
Josef Bacik416bc652013-04-23 14:17:42 -04002692 if (!eb || !extent_buffer_uptodate(eb)) {
2693 free_extent_buffer(eb);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00002694 err = -EIO;
2695 goto next;
2696 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002697 btrfs_tree_lock(eb);
2698 btrfs_set_lock_blocking(eb);
2699
2700 if (!node->eb) {
2701 ret = btrfs_cow_block(trans, root, eb, upper->eb,
2702 slot, &eb);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002703 btrfs_tree_unlock(eb);
2704 free_extent_buffer(eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002705 if (ret < 0) {
2706 err = ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002707 goto next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002708 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002709 BUG_ON(node->eb != eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002710 } else {
2711 btrfs_set_node_blockptr(upper->eb, slot,
2712 node->eb->start);
2713 btrfs_set_node_ptr_generation(upper->eb, slot,
2714 trans->transid);
2715 btrfs_mark_buffer_dirty(upper->eb);
2716
2717 ret = btrfs_inc_extent_ref(trans, root,
2718 node->eb->start, blocksize,
2719 upper->eb->start,
2720 btrfs_header_owner(upper->eb),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002721 node->level, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002722 BUG_ON(ret);
2723
2724 ret = btrfs_drop_subtree(trans, root, eb, upper->eb);
2725 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002726 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002727next:
2728 if (!upper->pending)
2729 drop_node_buffer(upper);
2730 else
2731 unlock_node_buffer(upper);
2732 if (err)
2733 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002734 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002735
2736 if (!err && node->pending) {
2737 drop_node_buffer(node);
2738 list_move_tail(&node->list, &rc->backref_cache.changed);
2739 node->pending = 0;
2740 }
2741
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002742 path->lowest_level = 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002743 BUG_ON(err == -ENOSPC);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002744 return err;
2745}
2746
2747static int link_to_upper(struct btrfs_trans_handle *trans,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002748 struct reloc_control *rc,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002749 struct backref_node *node,
2750 struct btrfs_path *path)
2751{
2752 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002753
2754 btrfs_node_key_to_cpu(node->eb, &key, 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002755 return do_relocation(trans, rc, node, &key, path, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002756}
2757
2758static int finish_pending_nodes(struct btrfs_trans_handle *trans,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002759 struct reloc_control *rc,
2760 struct btrfs_path *path, int err)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002761{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002762 LIST_HEAD(list);
2763 struct backref_cache *cache = &rc->backref_cache;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002764 struct backref_node *node;
2765 int level;
2766 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002767
2768 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2769 while (!list_empty(&cache->pending[level])) {
2770 node = list_entry(cache->pending[level].next,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002771 struct backref_node, list);
2772 list_move_tail(&node->list, &list);
2773 BUG_ON(!node->pending);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002774
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002775 if (!err) {
2776 ret = link_to_upper(trans, rc, node, path);
2777 if (ret < 0)
2778 err = ret;
2779 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002780 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002781 list_splice_init(&list, &cache->pending[level]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002782 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002783 return err;
2784}
2785
2786static void mark_block_processed(struct reloc_control *rc,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002787 u64 bytenr, u32 blocksize)
2788{
2789 set_extent_bits(&rc->processed_blocks, bytenr, bytenr + blocksize - 1,
2790 EXTENT_DIRTY, GFP_NOFS);
2791}
2792
2793static void __mark_block_processed(struct reloc_control *rc,
2794 struct backref_node *node)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002795{
2796 u32 blocksize;
2797 if (node->level == 0 ||
2798 in_block_group(node->bytenr, rc->block_group)) {
2799 blocksize = btrfs_level_size(rc->extent_root, node->level);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002800 mark_block_processed(rc, node->bytenr, blocksize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002801 }
2802 node->processed = 1;
2803}
2804
2805/*
2806 * mark a block and all blocks directly/indirectly reference the block
2807 * as processed.
2808 */
2809static void update_processed_blocks(struct reloc_control *rc,
2810 struct backref_node *node)
2811{
2812 struct backref_node *next = node;
2813 struct backref_edge *edge;
2814 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2815 int index = 0;
2816
2817 while (next) {
2818 cond_resched();
2819 while (1) {
2820 if (next->processed)
2821 break;
2822
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002823 __mark_block_processed(rc, next);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002824
2825 if (list_empty(&next->upper))
2826 break;
2827
2828 edge = list_entry(next->upper.next,
2829 struct backref_edge, list[LOWER]);
2830 edges[index++] = edge;
2831 next = edge->node[UPPER];
2832 }
2833 next = walk_down_backref(edges, &index);
2834 }
2835}
2836
2837static int tree_block_processed(u64 bytenr, u32 blocksize,
2838 struct reloc_control *rc)
2839{
2840 if (test_range_bit(&rc->processed_blocks, bytenr,
Chris Mason9655d292009-09-02 15:22:30 -04002841 bytenr + blocksize - 1, EXTENT_DIRTY, 1, NULL))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002842 return 1;
2843 return 0;
2844}
2845
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002846static int get_tree_block_key(struct reloc_control *rc,
2847 struct tree_block *block)
2848{
2849 struct extent_buffer *eb;
2850
2851 BUG_ON(block->key_ready);
2852 eb = read_tree_block(rc->extent_root, block->bytenr,
2853 block->key.objectid, block->key.offset);
Josef Bacik416bc652013-04-23 14:17:42 -04002854 if (!eb || !extent_buffer_uptodate(eb)) {
2855 free_extent_buffer(eb);
2856 return -EIO;
2857 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002858 WARN_ON(btrfs_header_level(eb) != block->level);
2859 if (block->level == 0)
2860 btrfs_item_key_to_cpu(eb, &block->key, 0);
2861 else
2862 btrfs_node_key_to_cpu(eb, &block->key, 0);
2863 free_extent_buffer(eb);
2864 block->key_ready = 1;
2865 return 0;
2866}
2867
2868static int reada_tree_block(struct reloc_control *rc,
2869 struct tree_block *block)
2870{
2871 BUG_ON(block->key_ready);
Josef Bacik3173a182013-03-07 14:22:04 -05002872 if (block->key.type == BTRFS_METADATA_ITEM_KEY)
2873 readahead_tree_block(rc->extent_root, block->bytenr,
2874 block->key.objectid,
2875 rc->extent_root->leafsize);
2876 else
2877 readahead_tree_block(rc->extent_root, block->bytenr,
2878 block->key.objectid, block->key.offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002879 return 0;
2880}
2881
2882/*
2883 * helper function to relocate a tree block
2884 */
2885static int relocate_tree_block(struct btrfs_trans_handle *trans,
2886 struct reloc_control *rc,
2887 struct backref_node *node,
2888 struct btrfs_key *key,
2889 struct btrfs_path *path)
2890{
2891 struct btrfs_root *root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002892 int release = 0;
2893 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002894
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002895 if (!node)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002896 return 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002897
2898 BUG_ON(node->processed);
2899 root = select_one_root(trans, node);
2900 if (root == ERR_PTR(-ENOENT)) {
2901 update_processed_blocks(rc, node);
2902 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002903 }
2904
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002905 if (!root || root->ref_cows) {
2906 ret = reserve_metadata_space(trans, rc, node);
2907 if (ret)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002908 goto out;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002909 release = 1;
2910 }
2911
2912 if (root) {
2913 if (root->ref_cows) {
2914 BUG_ON(node->new_bytenr);
2915 BUG_ON(!list_empty(&node->list));
2916 btrfs_record_root_in_trans(trans, root);
2917 root = root->reloc_root;
2918 node->new_bytenr = root->node->start;
2919 node->root = root;
2920 list_add_tail(&node->list, &rc->backref_cache.changed);
2921 } else {
2922 path->lowest_level = node->level;
2923 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
David Sterbab3b4aa72011-04-21 01:20:15 +02002924 btrfs_release_path(path);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002925 if (ret > 0)
2926 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002927 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002928 if (!ret)
2929 update_processed_blocks(rc, node);
2930 } else {
2931 ret = do_relocation(trans, rc, node, key, path, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002932 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002933out:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002934 if (ret || node->level == 0 || node->cowonly) {
2935 if (release)
2936 release_metadata_space(rc, node);
2937 remove_backref_node(&rc->backref_cache, node);
2938 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002939 return ret;
2940}
2941
2942/*
2943 * relocate a list of blocks
2944 */
2945static noinline_for_stack
2946int relocate_tree_blocks(struct btrfs_trans_handle *trans,
2947 struct reloc_control *rc, struct rb_root *blocks)
2948{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002949 struct backref_node *node;
2950 struct btrfs_path *path;
2951 struct tree_block *block;
2952 struct rb_node *rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002953 int ret;
2954 int err = 0;
2955
2956 path = btrfs_alloc_path();
Liu Boe1a12672013-03-04 16:25:38 +00002957 if (!path) {
2958 err = -ENOMEM;
David Sterba34c2b292013-04-26 12:56:04 +00002959 goto out_free_blocks;
Liu Boe1a12672013-03-04 16:25:38 +00002960 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002961
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002962 rb_node = rb_first(blocks);
2963 while (rb_node) {
2964 block = rb_entry(rb_node, struct tree_block, rb_node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002965 if (!block->key_ready)
2966 reada_tree_block(rc, block);
2967 rb_node = rb_next(rb_node);
2968 }
2969
2970 rb_node = rb_first(blocks);
2971 while (rb_node) {
2972 block = rb_entry(rb_node, struct tree_block, rb_node);
David Sterba34c2b292013-04-26 12:56:04 +00002973 if (!block->key_ready) {
2974 err = get_tree_block_key(rc, block);
2975 if (err)
2976 goto out_free_path;
2977 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002978 rb_node = rb_next(rb_node);
2979 }
2980
2981 rb_node = rb_first(blocks);
2982 while (rb_node) {
2983 block = rb_entry(rb_node, struct tree_block, rb_node);
2984
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002985 node = build_backref_tree(rc, &block->key,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002986 block->level, block->bytenr);
2987 if (IS_ERR(node)) {
2988 err = PTR_ERR(node);
2989 goto out;
2990 }
2991
2992 ret = relocate_tree_block(trans, rc, node, &block->key,
2993 path);
2994 if (ret < 0) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002995 if (ret != -EAGAIN || rb_node == rb_first(blocks))
2996 err = ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002997 goto out;
2998 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002999 rb_node = rb_next(rb_node);
3000 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003001out:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003002 err = finish_pending_nodes(trans, rc, path, err);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003003
David Sterba34c2b292013-04-26 12:56:04 +00003004out_free_path:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003005 btrfs_free_path(path);
David Sterba34c2b292013-04-26 12:56:04 +00003006out_free_blocks:
Liu Boe1a12672013-03-04 16:25:38 +00003007 free_block_list(blocks);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003008 return err;
3009}
3010
3011static noinline_for_stack
Yan, Zhengefa56462010-05-16 10:49:59 -04003012int prealloc_file_extent_cluster(struct inode *inode,
3013 struct file_extent_cluster *cluster)
3014{
3015 u64 alloc_hint = 0;
3016 u64 start;
3017 u64 end;
3018 u64 offset = BTRFS_I(inode)->index_cnt;
3019 u64 num_bytes;
3020 int nr = 0;
3021 int ret = 0;
3022
3023 BUG_ON(cluster->start != cluster->boundary[0]);
3024 mutex_lock(&inode->i_mutex);
3025
3026 ret = btrfs_check_data_free_space(inode, cluster->end +
3027 1 - cluster->start);
3028 if (ret)
3029 goto out;
3030
3031 while (nr < cluster->nr) {
3032 start = cluster->boundary[nr] - offset;
3033 if (nr + 1 < cluster->nr)
3034 end = cluster->boundary[nr + 1] - 1 - offset;
3035 else
3036 end = cluster->end - offset;
3037
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003038 lock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan, Zhengefa56462010-05-16 10:49:59 -04003039 num_bytes = end + 1 - start;
3040 ret = btrfs_prealloc_file_range(inode, 0, start,
3041 num_bytes, num_bytes,
3042 end + 1, &alloc_hint);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003043 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan, Zhengefa56462010-05-16 10:49:59 -04003044 if (ret)
3045 break;
3046 nr++;
3047 }
3048 btrfs_free_reserved_data_space(inode, cluster->end +
3049 1 - cluster->start);
3050out:
3051 mutex_unlock(&inode->i_mutex);
3052 return ret;
3053}
3054
3055static noinline_for_stack
Yan, Zheng0257bb82009-09-24 09:17:31 -04003056int setup_extent_mapping(struct inode *inode, u64 start, u64 end,
3057 u64 block_start)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003058{
3059 struct btrfs_root *root = BTRFS_I(inode)->root;
3060 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
3061 struct extent_map *em;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003062 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003063
David Sterba172ddd62011-04-21 00:48:27 +02003064 em = alloc_extent_map();
Yan, Zheng0257bb82009-09-24 09:17:31 -04003065 if (!em)
3066 return -ENOMEM;
3067
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003068 em->start = start;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003069 em->len = end + 1 - start;
3070 em->block_len = em->len;
3071 em->block_start = block_start;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003072 em->bdev = root->fs_info->fs_devices->latest_bdev;
3073 set_bit(EXTENT_FLAG_PINNED, &em->flags);
3074
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003075 lock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003076 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003077 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003078 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04003079 write_unlock(&em_tree->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003080 if (ret != -EEXIST) {
3081 free_extent_map(em);
3082 break;
3083 }
3084 btrfs_drop_extent_cache(inode, start, end, 0);
3085 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003086 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003087 return ret;
3088}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003089
Yan, Zheng0257bb82009-09-24 09:17:31 -04003090static int relocate_file_extent_cluster(struct inode *inode,
3091 struct file_extent_cluster *cluster)
3092{
3093 u64 page_start;
3094 u64 page_end;
3095 u64 offset = BTRFS_I(inode)->index_cnt;
3096 unsigned long index;
3097 unsigned long last_index;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003098 struct page *page;
3099 struct file_ra_state *ra;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003100 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003101 int nr = 0;
3102 int ret = 0;
3103
3104 if (!cluster->nr)
3105 return 0;
3106
3107 ra = kzalloc(sizeof(*ra), GFP_NOFS);
3108 if (!ra)
3109 return -ENOMEM;
3110
Yan, Zhengefa56462010-05-16 10:49:59 -04003111 ret = prealloc_file_extent_cluster(inode, cluster);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003112 if (ret)
Yan, Zhengefa56462010-05-16 10:49:59 -04003113 goto out;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003114
3115 file_ra_state_init(ra, inode->i_mapping);
3116
Yan, Zhengefa56462010-05-16 10:49:59 -04003117 ret = setup_extent_mapping(inode, cluster->start - offset,
3118 cluster->end - offset, cluster->start);
3119 if (ret)
3120 goto out;
3121
3122 index = (cluster->start - offset) >> PAGE_CACHE_SHIFT;
3123 last_index = (cluster->end - offset) >> PAGE_CACHE_SHIFT;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003124 while (index <= last_index) {
Yan, Zhengefa56462010-05-16 10:49:59 -04003125 ret = btrfs_delalloc_reserve_metadata(inode, PAGE_CACHE_SIZE);
3126 if (ret)
3127 goto out;
3128
Yan, Zheng0257bb82009-09-24 09:17:31 -04003129 page = find_lock_page(inode->i_mapping, index);
3130 if (!page) {
3131 page_cache_sync_readahead(inode->i_mapping,
3132 ra, NULL, index,
3133 last_index + 1 - index);
Josef Bacika94733d2011-07-11 10:47:06 -04003134 page = find_or_create_page(inode->i_mapping, index,
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003135 mask);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003136 if (!page) {
Yan, Zhengefa56462010-05-16 10:49:59 -04003137 btrfs_delalloc_release_metadata(inode,
3138 PAGE_CACHE_SIZE);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003139 ret = -ENOMEM;
Yan, Zhengefa56462010-05-16 10:49:59 -04003140 goto out;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003141 }
3142 }
3143
3144 if (PageReadahead(page)) {
3145 page_cache_async_readahead(inode->i_mapping,
3146 ra, NULL, page, index,
3147 last_index + 1 - index);
3148 }
3149
3150 if (!PageUptodate(page)) {
3151 btrfs_readpage(NULL, page);
3152 lock_page(page);
3153 if (!PageUptodate(page)) {
3154 unlock_page(page);
3155 page_cache_release(page);
Yan, Zhengefa56462010-05-16 10:49:59 -04003156 btrfs_delalloc_release_metadata(inode,
3157 PAGE_CACHE_SIZE);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003158 ret = -EIO;
Yan, Zhengefa56462010-05-16 10:49:59 -04003159 goto out;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003160 }
3161 }
3162
Miao Xie4eee4fa2012-12-21 09:17:45 +00003163 page_start = page_offset(page);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003164 page_end = page_start + PAGE_CACHE_SIZE - 1;
3165
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003166 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003167
3168 set_page_extent_mapped(page);
3169
3170 if (nr < cluster->nr &&
3171 page_start + offset == cluster->boundary[nr]) {
3172 set_extent_bits(&BTRFS_I(inode)->io_tree,
3173 page_start, page_end,
3174 EXTENT_BOUNDARY, GFP_NOFS);
3175 nr++;
3176 }
Yan, Zheng0257bb82009-09-24 09:17:31 -04003177
Yan, Zhengefa56462010-05-16 10:49:59 -04003178 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003179 set_page_dirty(page);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003180
3181 unlock_extent(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003182 page_start, page_end);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003183 unlock_page(page);
3184 page_cache_release(page);
3185
3186 index++;
Yan, Zhengefa56462010-05-16 10:49:59 -04003187 balance_dirty_pages_ratelimited(inode->i_mapping);
3188 btrfs_throttle(BTRFS_I(inode)->root);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003189 }
3190 WARN_ON(nr != cluster->nr);
Yan, Zhengefa56462010-05-16 10:49:59 -04003191out:
Yan, Zheng0257bb82009-09-24 09:17:31 -04003192 kfree(ra);
3193 return ret;
3194}
3195
3196static noinline_for_stack
3197int relocate_data_extent(struct inode *inode, struct btrfs_key *extent_key,
3198 struct file_extent_cluster *cluster)
3199{
3200 int ret;
3201
3202 if (cluster->nr > 0 && extent_key->objectid != cluster->end + 1) {
3203 ret = relocate_file_extent_cluster(inode, cluster);
3204 if (ret)
3205 return ret;
3206 cluster->nr = 0;
3207 }
3208
3209 if (!cluster->nr)
3210 cluster->start = extent_key->objectid;
3211 else
3212 BUG_ON(cluster->nr >= MAX_EXTENTS);
3213 cluster->end = extent_key->objectid + extent_key->offset - 1;
3214 cluster->boundary[cluster->nr] = extent_key->objectid;
3215 cluster->nr++;
3216
3217 if (cluster->nr >= MAX_EXTENTS) {
3218 ret = relocate_file_extent_cluster(inode, cluster);
3219 if (ret)
3220 return ret;
3221 cluster->nr = 0;
3222 }
3223 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003224}
3225
3226#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3227static int get_ref_objectid_v0(struct reloc_control *rc,
3228 struct btrfs_path *path,
3229 struct btrfs_key *extent_key,
3230 u64 *ref_objectid, int *path_change)
3231{
3232 struct btrfs_key key;
3233 struct extent_buffer *leaf;
3234 struct btrfs_extent_ref_v0 *ref0;
3235 int ret;
3236 int slot;
3237
3238 leaf = path->nodes[0];
3239 slot = path->slots[0];
3240 while (1) {
3241 if (slot >= btrfs_header_nritems(leaf)) {
3242 ret = btrfs_next_leaf(rc->extent_root, path);
3243 if (ret < 0)
3244 return ret;
3245 BUG_ON(ret > 0);
3246 leaf = path->nodes[0];
3247 slot = path->slots[0];
3248 if (path_change)
3249 *path_change = 1;
3250 }
3251 btrfs_item_key_to_cpu(leaf, &key, slot);
3252 if (key.objectid != extent_key->objectid)
3253 return -ENOENT;
3254
3255 if (key.type != BTRFS_EXTENT_REF_V0_KEY) {
3256 slot++;
3257 continue;
3258 }
3259 ref0 = btrfs_item_ptr(leaf, slot,
3260 struct btrfs_extent_ref_v0);
3261 *ref_objectid = btrfs_ref_objectid_v0(leaf, ref0);
3262 break;
3263 }
3264 return 0;
3265}
3266#endif
3267
3268/*
3269 * helper to add a tree block to the list.
3270 * the major work is getting the generation and level of the block
3271 */
3272static int add_tree_block(struct reloc_control *rc,
3273 struct btrfs_key *extent_key,
3274 struct btrfs_path *path,
3275 struct rb_root *blocks)
3276{
3277 struct extent_buffer *eb;
3278 struct btrfs_extent_item *ei;
3279 struct btrfs_tree_block_info *bi;
3280 struct tree_block *block;
3281 struct rb_node *rb_node;
3282 u32 item_size;
3283 int level = -1;
Wang Shilong7fdf4b62013-10-25 18:52:08 +08003284 u64 generation;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003285
3286 eb = path->nodes[0];
3287 item_size = btrfs_item_size_nr(eb, path->slots[0]);
3288
Josef Bacik3173a182013-03-07 14:22:04 -05003289 if (extent_key->type == BTRFS_METADATA_ITEM_KEY ||
3290 item_size >= sizeof(*ei) + sizeof(*bi)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003291 ei = btrfs_item_ptr(eb, path->slots[0],
3292 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05003293 if (extent_key->type == BTRFS_EXTENT_ITEM_KEY) {
3294 bi = (struct btrfs_tree_block_info *)(ei + 1);
3295 level = btrfs_tree_block_level(eb, bi);
3296 } else {
3297 level = (int)extent_key->offset;
3298 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003299 generation = btrfs_extent_generation(eb, ei);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003300 } else {
3301#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3302 u64 ref_owner;
3303 int ret;
3304
3305 BUG_ON(item_size != sizeof(struct btrfs_extent_item_v0));
3306 ret = get_ref_objectid_v0(rc, path, extent_key,
3307 &ref_owner, NULL);
Andi Kleen411fc6b2010-10-29 15:14:31 -04003308 if (ret < 0)
3309 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003310 BUG_ON(ref_owner >= BTRFS_MAX_LEVEL);
3311 level = (int)ref_owner;
3312 /* FIXME: get real generation */
3313 generation = 0;
3314#else
3315 BUG();
3316#endif
3317 }
3318
David Sterbab3b4aa72011-04-21 01:20:15 +02003319 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003320
3321 BUG_ON(level == -1);
3322
3323 block = kmalloc(sizeof(*block), GFP_NOFS);
3324 if (!block)
3325 return -ENOMEM;
3326
3327 block->bytenr = extent_key->objectid;
Josef Bacik3173a182013-03-07 14:22:04 -05003328 block->key.objectid = rc->extent_root->leafsize;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003329 block->key.offset = generation;
3330 block->level = level;
3331 block->key_ready = 0;
3332
3333 rb_node = tree_insert(blocks, block->bytenr, &block->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -04003334 if (rb_node)
3335 backref_tree_panic(rb_node, -EEXIST, block->bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003336
3337 return 0;
3338}
3339
3340/*
3341 * helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY
3342 */
3343static int __add_tree_block(struct reloc_control *rc,
3344 u64 bytenr, u32 blocksize,
3345 struct rb_root *blocks)
3346{
3347 struct btrfs_path *path;
3348 struct btrfs_key key;
3349 int ret;
Josef Bacikaee68ee2013-06-13 13:50:23 -04003350 bool skinny = btrfs_fs_incompat(rc->extent_root->fs_info,
3351 SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003352
3353 if (tree_block_processed(bytenr, blocksize, rc))
3354 return 0;
3355
3356 if (tree_search(blocks, bytenr))
3357 return 0;
3358
3359 path = btrfs_alloc_path();
3360 if (!path)
3361 return -ENOMEM;
Josef Bacikaee68ee2013-06-13 13:50:23 -04003362again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003363 key.objectid = bytenr;
Josef Bacikaee68ee2013-06-13 13:50:23 -04003364 if (skinny) {
3365 key.type = BTRFS_METADATA_ITEM_KEY;
3366 key.offset = (u64)-1;
3367 } else {
3368 key.type = BTRFS_EXTENT_ITEM_KEY;
3369 key.offset = blocksize;
3370 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003371
3372 path->search_commit_root = 1;
3373 path->skip_locking = 1;
3374 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path, 0, 0);
3375 if (ret < 0)
3376 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003377
Josef Bacikaee68ee2013-06-13 13:50:23 -04003378 if (ret > 0 && skinny) {
3379 if (path->slots[0]) {
3380 path->slots[0]--;
3381 btrfs_item_key_to_cpu(path->nodes[0], &key,
3382 path->slots[0]);
3383 if (key.objectid == bytenr &&
3384 (key.type == BTRFS_METADATA_ITEM_KEY ||
3385 (key.type == BTRFS_EXTENT_ITEM_KEY &&
3386 key.offset == blocksize)))
3387 ret = 0;
3388 }
3389
3390 if (ret) {
3391 skinny = false;
3392 btrfs_release_path(path);
3393 goto again;
3394 }
Josef Bacik3173a182013-03-07 14:22:04 -05003395 }
3396 BUG_ON(ret);
3397
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003398 ret = add_tree_block(rc, &key, path, blocks);
3399out:
3400 btrfs_free_path(path);
3401 return ret;
3402}
3403
3404/*
3405 * helper to check if the block use full backrefs for pointers in it
3406 */
3407static int block_use_full_backref(struct reloc_control *rc,
3408 struct extent_buffer *eb)
3409{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003410 u64 flags;
3411 int ret;
3412
3413 if (btrfs_header_flag(eb, BTRFS_HEADER_FLAG_RELOC) ||
3414 btrfs_header_backref_rev(eb) < BTRFS_MIXED_BACKREF_REV)
3415 return 1;
3416
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003417 ret = btrfs_lookup_extent_info(NULL, rc->extent_root,
Josef Bacik3173a182013-03-07 14:22:04 -05003418 eb->start, btrfs_header_level(eb), 1,
3419 NULL, &flags);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003420 BUG_ON(ret);
3421
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003422 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
3423 ret = 1;
3424 else
3425 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003426 return ret;
3427}
3428
Josef Bacik0af3d002010-06-21 14:48:16 -04003429static int delete_block_group_cache(struct btrfs_fs_info *fs_info,
3430 struct inode *inode, u64 ino)
3431{
3432 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04003433 struct btrfs_root *root = fs_info->tree_root;
3434 struct btrfs_trans_handle *trans;
Josef Bacik0af3d002010-06-21 14:48:16 -04003435 int ret = 0;
3436
3437 if (inode)
3438 goto truncate;
3439
3440 key.objectid = ino;
3441 key.type = BTRFS_INODE_ITEM_KEY;
3442 key.offset = 0;
3443
3444 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
Tsutomu Itohf54fb852012-09-06 03:08:59 -06003445 if (IS_ERR(inode) || is_bad_inode(inode)) {
3446 if (!IS_ERR(inode))
Josef Bacik0af3d002010-06-21 14:48:16 -04003447 iput(inode);
3448 return -ENOENT;
3449 }
3450
3451truncate:
Miao Xie7b61cd92013-05-13 13:55:09 +00003452 ret = btrfs_check_trunc_cache_free_space(root,
3453 &fs_info->global_block_rsv);
3454 if (ret)
3455 goto out;
3456
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003457 trans = btrfs_join_transaction(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04003458 if (IS_ERR(trans)) {
Tsutomu Itoh3612b492011-01-25 02:51:38 +00003459 ret = PTR_ERR(trans);
Josef Bacik0af3d002010-06-21 14:48:16 -04003460 goto out;
3461 }
3462
Filipe David Borba Manana74514322013-09-20 14:46:51 +01003463 ret = btrfs_truncate_free_space_cache(root, trans, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04003464
Josef Bacik0af3d002010-06-21 14:48:16 -04003465 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003466 btrfs_btree_balance_dirty(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04003467out:
3468 iput(inode);
3469 return ret;
3470}
3471
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003472/*
3473 * helper to add tree blocks for backref of type BTRFS_EXTENT_DATA_REF_KEY
3474 * this function scans fs tree to find blocks reference the data extent
3475 */
3476static int find_data_references(struct reloc_control *rc,
3477 struct btrfs_key *extent_key,
3478 struct extent_buffer *leaf,
3479 struct btrfs_extent_data_ref *ref,
3480 struct rb_root *blocks)
3481{
3482 struct btrfs_path *path;
3483 struct tree_block *block;
3484 struct btrfs_root *root;
3485 struct btrfs_file_extent_item *fi;
3486 struct rb_node *rb_node;
3487 struct btrfs_key key;
3488 u64 ref_root;
3489 u64 ref_objectid;
3490 u64 ref_offset;
3491 u32 ref_count;
3492 u32 nritems;
3493 int err = 0;
3494 int added = 0;
3495 int counted;
3496 int ret;
3497
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003498 ref_root = btrfs_extent_data_ref_root(leaf, ref);
3499 ref_objectid = btrfs_extent_data_ref_objectid(leaf, ref);
3500 ref_offset = btrfs_extent_data_ref_offset(leaf, ref);
3501 ref_count = btrfs_extent_data_ref_count(leaf, ref);
3502
Josef Bacik0af3d002010-06-21 14:48:16 -04003503 /*
3504 * This is an extent belonging to the free space cache, lets just delete
3505 * it and redo the search.
3506 */
3507 if (ref_root == BTRFS_ROOT_TREE_OBJECTID) {
3508 ret = delete_block_group_cache(rc->extent_root->fs_info,
3509 NULL, ref_objectid);
3510 if (ret != -ENOENT)
3511 return ret;
3512 ret = 0;
3513 }
3514
3515 path = btrfs_alloc_path();
3516 if (!path)
3517 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04003518 path->reada = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04003519
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003520 root = read_fs_root(rc->extent_root->fs_info, ref_root);
3521 if (IS_ERR(root)) {
3522 err = PTR_ERR(root);
3523 goto out;
3524 }
3525
3526 key.objectid = ref_objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003527 key.type = BTRFS_EXTENT_DATA_KEY;
Yan, Zheng84850e82011-08-29 09:25:53 +08003528 if (ref_offset > ((u64)-1 << 32))
3529 key.offset = 0;
3530 else
3531 key.offset = ref_offset;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003532
3533 path->search_commit_root = 1;
3534 path->skip_locking = 1;
3535 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3536 if (ret < 0) {
3537 err = ret;
3538 goto out;
3539 }
3540
3541 leaf = path->nodes[0];
3542 nritems = btrfs_header_nritems(leaf);
3543 /*
3544 * the references in tree blocks that use full backrefs
3545 * are not counted in
3546 */
3547 if (block_use_full_backref(rc, leaf))
3548 counted = 0;
3549 else
3550 counted = 1;
3551 rb_node = tree_search(blocks, leaf->start);
3552 if (rb_node) {
3553 if (counted)
3554 added = 1;
3555 else
3556 path->slots[0] = nritems;
3557 }
3558
3559 while (ref_count > 0) {
3560 while (path->slots[0] >= nritems) {
3561 ret = btrfs_next_leaf(root, path);
3562 if (ret < 0) {
3563 err = ret;
3564 goto out;
3565 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303566 if (WARN_ON(ret > 0))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003567 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003568
3569 leaf = path->nodes[0];
3570 nritems = btrfs_header_nritems(leaf);
3571 added = 0;
3572
3573 if (block_use_full_backref(rc, leaf))
3574 counted = 0;
3575 else
3576 counted = 1;
3577 rb_node = tree_search(blocks, leaf->start);
3578 if (rb_node) {
3579 if (counted)
3580 added = 1;
3581 else
3582 path->slots[0] = nritems;
3583 }
3584 }
3585
3586 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303587 if (WARN_ON(key.objectid != ref_objectid ||
3588 key.type != BTRFS_EXTENT_DATA_KEY))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003589 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003590
3591 fi = btrfs_item_ptr(leaf, path->slots[0],
3592 struct btrfs_file_extent_item);
3593
3594 if (btrfs_file_extent_type(leaf, fi) ==
3595 BTRFS_FILE_EXTENT_INLINE)
3596 goto next;
3597
3598 if (btrfs_file_extent_disk_bytenr(leaf, fi) !=
3599 extent_key->objectid)
3600 goto next;
3601
3602 key.offset -= btrfs_file_extent_offset(leaf, fi);
3603 if (key.offset != ref_offset)
3604 goto next;
3605
3606 if (counted)
3607 ref_count--;
3608 if (added)
3609 goto next;
3610
3611 if (!tree_block_processed(leaf->start, leaf->len, rc)) {
3612 block = kmalloc(sizeof(*block), GFP_NOFS);
3613 if (!block) {
3614 err = -ENOMEM;
3615 break;
3616 }
3617 block->bytenr = leaf->start;
3618 btrfs_item_key_to_cpu(leaf, &block->key, 0);
3619 block->level = 0;
3620 block->key_ready = 1;
3621 rb_node = tree_insert(blocks, block->bytenr,
3622 &block->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -04003623 if (rb_node)
3624 backref_tree_panic(rb_node, -EEXIST,
3625 block->bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003626 }
3627 if (counted)
3628 added = 1;
3629 else
3630 path->slots[0] = nritems;
3631next:
3632 path->slots[0]++;
3633
3634 }
3635out:
3636 btrfs_free_path(path);
3637 return err;
3638}
3639
3640/*
Liu Bo2c016dc2012-12-26 15:32:17 +08003641 * helper to find all tree blocks that reference a given data extent
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003642 */
3643static noinline_for_stack
3644int add_data_references(struct reloc_control *rc,
3645 struct btrfs_key *extent_key,
3646 struct btrfs_path *path,
3647 struct rb_root *blocks)
3648{
3649 struct btrfs_key key;
3650 struct extent_buffer *eb;
3651 struct btrfs_extent_data_ref *dref;
3652 struct btrfs_extent_inline_ref *iref;
3653 unsigned long ptr;
3654 unsigned long end;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003655 u32 blocksize = btrfs_level_size(rc->extent_root, 0);
Filipe David Borba Manana647f63b2013-07-13 12:25:15 +01003656 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003657 int err = 0;
3658
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003659 eb = path->nodes[0];
3660 ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
3661 end = ptr + btrfs_item_size_nr(eb, path->slots[0]);
3662#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3663 if (ptr + sizeof(struct btrfs_extent_item_v0) == end)
3664 ptr = end;
3665 else
3666#endif
3667 ptr += sizeof(struct btrfs_extent_item);
3668
3669 while (ptr < end) {
3670 iref = (struct btrfs_extent_inline_ref *)ptr;
3671 key.type = btrfs_extent_inline_ref_type(eb, iref);
3672 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
3673 key.offset = btrfs_extent_inline_ref_offset(eb, iref);
3674 ret = __add_tree_block(rc, key.offset, blocksize,
3675 blocks);
3676 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
3677 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
3678 ret = find_data_references(rc, extent_key,
3679 eb, dref, blocks);
3680 } else {
3681 BUG();
3682 }
Filipe David Borba Manana647f63b2013-07-13 12:25:15 +01003683 if (ret) {
3684 err = ret;
3685 goto out;
3686 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003687 ptr += btrfs_extent_inline_ref_size(key.type);
3688 }
3689 WARN_ON(ptr > end);
3690
3691 while (1) {
3692 cond_resched();
3693 eb = path->nodes[0];
3694 if (path->slots[0] >= btrfs_header_nritems(eb)) {
3695 ret = btrfs_next_leaf(rc->extent_root, path);
3696 if (ret < 0) {
3697 err = ret;
3698 break;
3699 }
3700 if (ret > 0)
3701 break;
3702 eb = path->nodes[0];
3703 }
3704
3705 btrfs_item_key_to_cpu(eb, &key, path->slots[0]);
3706 if (key.objectid != extent_key->objectid)
3707 break;
3708
3709#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3710 if (key.type == BTRFS_SHARED_DATA_REF_KEY ||
3711 key.type == BTRFS_EXTENT_REF_V0_KEY) {
3712#else
3713 BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY);
3714 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
3715#endif
3716 ret = __add_tree_block(rc, key.offset, blocksize,
3717 blocks);
3718 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
3719 dref = btrfs_item_ptr(eb, path->slots[0],
3720 struct btrfs_extent_data_ref);
3721 ret = find_data_references(rc, extent_key,
3722 eb, dref, blocks);
3723 } else {
3724 ret = 0;
3725 }
3726 if (ret) {
3727 err = ret;
3728 break;
3729 }
3730 path->slots[0]++;
3731 }
Filipe David Borba Manana647f63b2013-07-13 12:25:15 +01003732out:
David Sterbab3b4aa72011-04-21 01:20:15 +02003733 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003734 if (err)
3735 free_block_list(blocks);
3736 return err;
3737}
3738
3739/*
Liu Bo2c016dc2012-12-26 15:32:17 +08003740 * helper to find next unprocessed extent
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003741 */
3742static noinline_for_stack
3743int find_next_extent(struct btrfs_trans_handle *trans,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003744 struct reloc_control *rc, struct btrfs_path *path,
3745 struct btrfs_key *extent_key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003746{
3747 struct btrfs_key key;
3748 struct extent_buffer *leaf;
3749 u64 start, end, last;
3750 int ret;
3751
3752 last = rc->block_group->key.objectid + rc->block_group->key.offset;
3753 while (1) {
3754 cond_resched();
3755 if (rc->search_start >= last) {
3756 ret = 1;
3757 break;
3758 }
3759
3760 key.objectid = rc->search_start;
3761 key.type = BTRFS_EXTENT_ITEM_KEY;
3762 key.offset = 0;
3763
3764 path->search_commit_root = 1;
3765 path->skip_locking = 1;
3766 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path,
3767 0, 0);
3768 if (ret < 0)
3769 break;
3770next:
3771 leaf = path->nodes[0];
3772 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
3773 ret = btrfs_next_leaf(rc->extent_root, path);
3774 if (ret != 0)
3775 break;
3776 leaf = path->nodes[0];
3777 }
3778
3779 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3780 if (key.objectid >= last) {
3781 ret = 1;
3782 break;
3783 }
3784
Josef Bacik3173a182013-03-07 14:22:04 -05003785 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3786 key.type != BTRFS_METADATA_ITEM_KEY) {
3787 path->slots[0]++;
3788 goto next;
3789 }
3790
3791 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003792 key.objectid + key.offset <= rc->search_start) {
3793 path->slots[0]++;
3794 goto next;
3795 }
3796
Josef Bacik3173a182013-03-07 14:22:04 -05003797 if (key.type == BTRFS_METADATA_ITEM_KEY &&
3798 key.objectid + rc->extent_root->leafsize <=
3799 rc->search_start) {
3800 path->slots[0]++;
3801 goto next;
3802 }
3803
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003804 ret = find_first_extent_bit(&rc->processed_blocks,
3805 key.objectid, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04003806 EXTENT_DIRTY, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003807
3808 if (ret == 0 && start <= key.objectid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003809 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003810 rc->search_start = end + 1;
3811 } else {
Josef Bacik3173a182013-03-07 14:22:04 -05003812 if (key.type == BTRFS_EXTENT_ITEM_KEY)
3813 rc->search_start = key.objectid + key.offset;
3814 else
3815 rc->search_start = key.objectid +
3816 rc->extent_root->leafsize;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003817 memcpy(extent_key, &key, sizeof(key));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003818 return 0;
3819 }
3820 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003821 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003822 return ret;
3823}
3824
3825static void set_reloc_control(struct reloc_control *rc)
3826{
3827 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
Chris Mason75857172011-06-13 20:00:16 -04003828
3829 mutex_lock(&fs_info->reloc_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003830 fs_info->reloc_ctl = rc;
Chris Mason75857172011-06-13 20:00:16 -04003831 mutex_unlock(&fs_info->reloc_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003832}
3833
3834static void unset_reloc_control(struct reloc_control *rc)
3835{
3836 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
Chris Mason75857172011-06-13 20:00:16 -04003837
3838 mutex_lock(&fs_info->reloc_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003839 fs_info->reloc_ctl = NULL;
Chris Mason75857172011-06-13 20:00:16 -04003840 mutex_unlock(&fs_info->reloc_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003841}
3842
3843static int check_extent_flags(u64 flags)
3844{
3845 if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
3846 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
3847 return 1;
3848 if (!(flags & BTRFS_EXTENT_FLAG_DATA) &&
3849 !(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
3850 return 1;
3851 if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
3852 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
3853 return 1;
3854 return 0;
3855}
3856
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003857static noinline_for_stack
3858int prepare_to_relocate(struct reloc_control *rc)
3859{
3860 struct btrfs_trans_handle *trans;
3861 int ret;
3862
Miao Xie66d8f3d2012-09-06 04:02:28 -06003863 rc->block_rsv = btrfs_alloc_block_rsv(rc->extent_root,
3864 BTRFS_BLOCK_RSV_TEMP);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003865 if (!rc->block_rsv)
3866 return -ENOMEM;
3867
3868 /*
3869 * reserve some space for creating reloc trees.
3870 * btrfs_init_reloc_root will use them when there
3871 * is no reservation in transaction handle.
3872 */
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003873 ret = btrfs_block_rsv_add(rc->extent_root, rc->block_rsv,
Miao Xie08e007d2012-10-16 11:33:38 +00003874 rc->extent_root->nodesize * 256,
3875 BTRFS_RESERVE_FLUSH_ALL);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003876 if (ret)
3877 return ret;
3878
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003879 memset(&rc->cluster, 0, sizeof(rc->cluster));
3880 rc->search_start = rc->block_group->key.objectid;
3881 rc->extents_found = 0;
3882 rc->nodes_relocated = 0;
3883 rc->merging_rsv_size = 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003884
3885 rc->create_reloc_tree = 1;
3886 set_reloc_control(rc);
3887
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003888 trans = btrfs_join_transaction(rc->extent_root);
Liu Bo28818942013-03-04 16:25:39 +00003889 if (IS_ERR(trans)) {
3890 unset_reloc_control(rc);
3891 /*
3892 * extent tree is not a ref_cow tree and has no reloc_root to
3893 * cleanup. And callers are responsible to free the above
3894 * block rsv.
3895 */
3896 return PTR_ERR(trans);
3897 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003898 btrfs_commit_transaction(trans, rc->extent_root);
3899 return 0;
3900}
Yan, Zheng76dda932009-09-21 16:00:26 -04003901
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003902static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
3903{
3904 struct rb_root blocks = RB_ROOT;
3905 struct btrfs_key key;
3906 struct btrfs_trans_handle *trans = NULL;
3907 struct btrfs_path *path;
3908 struct btrfs_extent_item *ei;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003909 u64 flags;
3910 u32 item_size;
3911 int ret;
3912 int err = 0;
Chris Masonc87f08c2011-02-16 13:57:04 -05003913 int progress = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003914
3915 path = btrfs_alloc_path();
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003916 if (!path)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003917 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04003918 path->reada = 1;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003919
3920 ret = prepare_to_relocate(rc);
3921 if (ret) {
3922 err = ret;
3923 goto out_free;
Jiri Slaby2423fdf2010-01-06 16:57:22 +00003924 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003925
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003926 while (1) {
Chris Masonc87f08c2011-02-16 13:57:04 -05003927 progress++;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003928 trans = btrfs_start_transaction(rc->extent_root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00003929 if (IS_ERR(trans)) {
3930 err = PTR_ERR(trans);
3931 trans = NULL;
3932 break;
3933 }
Chris Masonc87f08c2011-02-16 13:57:04 -05003934restart:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003935 if (update_backref_cache(trans, &rc->backref_cache)) {
3936 btrfs_end_transaction(trans, rc->extent_root);
3937 continue;
3938 }
3939
3940 ret = find_next_extent(trans, rc, path, &key);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003941 if (ret < 0)
3942 err = ret;
3943 if (ret != 0)
3944 break;
3945
3946 rc->extents_found++;
3947
3948 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3949 struct btrfs_extent_item);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003950 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003951 if (item_size >= sizeof(*ei)) {
3952 flags = btrfs_extent_flags(path->nodes[0], ei);
3953 ret = check_extent_flags(flags);
3954 BUG_ON(ret);
3955
3956 } else {
3957#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3958 u64 ref_owner;
3959 int path_change = 0;
3960
3961 BUG_ON(item_size !=
3962 sizeof(struct btrfs_extent_item_v0));
3963 ret = get_ref_objectid_v0(rc, path, &key, &ref_owner,
3964 &path_change);
3965 if (ref_owner < BTRFS_FIRST_FREE_OBJECTID)
3966 flags = BTRFS_EXTENT_FLAG_TREE_BLOCK;
3967 else
3968 flags = BTRFS_EXTENT_FLAG_DATA;
3969
3970 if (path_change) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003971 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003972
3973 path->search_commit_root = 1;
3974 path->skip_locking = 1;
3975 ret = btrfs_search_slot(NULL, rc->extent_root,
3976 &key, path, 0, 0);
3977 if (ret < 0) {
3978 err = ret;
3979 break;
3980 }
3981 BUG_ON(ret > 0);
3982 }
3983#else
3984 BUG();
3985#endif
3986 }
3987
3988 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
3989 ret = add_tree_block(rc, &key, path, &blocks);
3990 } else if (rc->stage == UPDATE_DATA_PTRS &&
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003991 (flags & BTRFS_EXTENT_FLAG_DATA)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003992 ret = add_data_references(rc, &key, path, &blocks);
3993 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +02003994 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003995 ret = 0;
3996 }
3997 if (ret < 0) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003998 err = ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003999 break;
4000 }
4001
4002 if (!RB_EMPTY_ROOT(&blocks)) {
4003 ret = relocate_tree_blocks(trans, rc, &blocks);
4004 if (ret < 0) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004005 if (ret != -EAGAIN) {
4006 err = ret;
4007 break;
4008 }
4009 rc->extents_found--;
4010 rc->search_start = key.objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004011 }
4012 }
4013
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004014 if (rc->commit_transaction) {
4015 rc->commit_transaction = 0;
4016 ret = btrfs_commit_transaction(trans, rc->extent_root);
4017 BUG_ON(ret);
4018 } else {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004019 btrfs_end_transaction_throttle(trans, rc->extent_root);
Liu Bob53d3f52012-11-14 14:34:34 +00004020 btrfs_btree_balance_dirty(rc->extent_root);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004021 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004022 trans = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004023
4024 if (rc->stage == MOVE_DATA_EXTENTS &&
4025 (flags & BTRFS_EXTENT_FLAG_DATA)) {
4026 rc->found_file_extent = 1;
Yan, Zheng0257bb82009-09-24 09:17:31 -04004027 ret = relocate_data_extent(rc->data_inode,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004028 &key, &rc->cluster);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004029 if (ret < 0) {
4030 err = ret;
4031 break;
4032 }
4033 }
4034 }
Chris Masonc87f08c2011-02-16 13:57:04 -05004035 if (trans && progress && err == -ENOSPC) {
4036 ret = btrfs_force_chunk_alloc(trans, rc->extent_root,
4037 rc->block_group->flags);
4038 if (ret == 0) {
4039 err = 0;
4040 progress = 0;
4041 goto restart;
4042 }
4043 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004044
David Sterbab3b4aa72011-04-21 01:20:15 +02004045 btrfs_release_path(path);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004046 clear_extent_bits(&rc->processed_blocks, 0, (u64)-1, EXTENT_DIRTY,
4047 GFP_NOFS);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004048
4049 if (trans) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004050 btrfs_end_transaction_throttle(trans, rc->extent_root);
Liu Bob53d3f52012-11-14 14:34:34 +00004051 btrfs_btree_balance_dirty(rc->extent_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004052 }
4053
Yan, Zheng0257bb82009-09-24 09:17:31 -04004054 if (!err) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004055 ret = relocate_file_extent_cluster(rc->data_inode,
4056 &rc->cluster);
Yan, Zheng0257bb82009-09-24 09:17:31 -04004057 if (ret < 0)
4058 err = ret;
4059 }
4060
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004061 rc->create_reloc_tree = 0;
4062 set_reloc_control(rc);
Yan, Zheng0257bb82009-09-24 09:17:31 -04004063
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004064 backref_cache_cleanup(&rc->backref_cache);
4065 btrfs_block_rsv_release(rc->extent_root, rc->block_rsv, (u64)-1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004066
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004067 err = prepare_to_merge(rc, err);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004068
4069 merge_reloc_roots(rc);
4070
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004071 rc->merge_reloc_tree = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004072 unset_reloc_control(rc);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004073 btrfs_block_rsv_release(rc->extent_root, rc->block_rsv, (u64)-1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004074
4075 /* get rid of pinned extents */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004076 trans = btrfs_join_transaction(rc->extent_root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004077 if (IS_ERR(trans))
4078 err = PTR_ERR(trans);
4079 else
4080 btrfs_commit_transaction(trans, rc->extent_root);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004081out_free:
4082 btrfs_free_block_rsv(rc->extent_root, rc->block_rsv);
4083 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004084 return err;
4085}
4086
4087static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
Yan, Zheng0257bb82009-09-24 09:17:31 -04004088 struct btrfs_root *root, u64 objectid)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004089{
4090 struct btrfs_path *path;
4091 struct btrfs_inode_item *item;
4092 struct extent_buffer *leaf;
4093 int ret;
4094
4095 path = btrfs_alloc_path();
4096 if (!path)
4097 return -ENOMEM;
4098
4099 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
4100 if (ret)
4101 goto out;
4102
4103 leaf = path->nodes[0];
4104 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
4105 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
4106 btrfs_set_inode_generation(leaf, item, 1);
Yan, Zheng0257bb82009-09-24 09:17:31 -04004107 btrfs_set_inode_size(leaf, item, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004108 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004109 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS |
4110 BTRFS_INODE_PREALLOC);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004111 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02004112 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004113out:
4114 btrfs_free_path(path);
4115 return ret;
4116}
4117
4118/*
4119 * helper to create inode for data relocation.
4120 * the inode is in data relocation tree and its link count is 0
4121 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004122static noinline_for_stack
4123struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
4124 struct btrfs_block_group_cache *group)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004125{
4126 struct inode *inode = NULL;
4127 struct btrfs_trans_handle *trans;
4128 struct btrfs_root *root;
4129 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004130 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
4131 int err = 0;
4132
4133 root = read_fs_root(fs_info, BTRFS_DATA_RELOC_TREE_OBJECTID);
4134 if (IS_ERR(root))
4135 return ERR_CAST(root);
4136
Yan, Zhenga22285a2010-05-16 10:48:46 -04004137 trans = btrfs_start_transaction(root, 6);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004138 if (IS_ERR(trans))
4139 return ERR_CAST(trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004140
Li Zefan581bb052011-04-20 10:06:11 +08004141 err = btrfs_find_free_objectid(root, &objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004142 if (err)
4143 goto out;
4144
Yan, Zheng0257bb82009-09-24 09:17:31 -04004145 err = __insert_orphan_inode(trans, root, objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004146 BUG_ON(err);
4147
4148 key.objectid = objectid;
4149 key.type = BTRFS_INODE_ITEM_KEY;
4150 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00004151 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004152 BUG_ON(IS_ERR(inode) || is_bad_inode(inode));
4153 BTRFS_I(inode)->index_cnt = group->key.objectid;
4154
4155 err = btrfs_orphan_add(trans, inode);
4156out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004157 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004158 btrfs_btree_balance_dirty(root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004159 if (err) {
4160 if (inode)
4161 iput(inode);
4162 inode = ERR_PTR(err);
4163 }
4164 return inode;
4165}
4166
Josef Bacika9995ee2013-05-31 13:04:36 -04004167static struct reloc_control *alloc_reloc_control(struct btrfs_fs_info *fs_info)
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004168{
4169 struct reloc_control *rc;
4170
4171 rc = kzalloc(sizeof(*rc), GFP_NOFS);
4172 if (!rc)
4173 return NULL;
4174
4175 INIT_LIST_HEAD(&rc->reloc_roots);
4176 backref_cache_init(&rc->backref_cache);
4177 mapping_tree_init(&rc->reloc_root_tree);
Josef Bacika9995ee2013-05-31 13:04:36 -04004178 extent_io_tree_init(&rc->processed_blocks,
4179 fs_info->btree_inode->i_mapping);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004180 return rc;
4181}
4182
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004183/*
4184 * function to relocate all extents in a block group.
4185 */
4186int btrfs_relocate_block_group(struct btrfs_root *extent_root, u64 group_start)
4187{
4188 struct btrfs_fs_info *fs_info = extent_root->fs_info;
4189 struct reloc_control *rc;
Josef Bacik0af3d002010-06-21 14:48:16 -04004190 struct inode *inode;
4191 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004192 int ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004193 int rw = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004194 int err = 0;
4195
Josef Bacika9995ee2013-05-31 13:04:36 -04004196 rc = alloc_reloc_control(fs_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004197 if (!rc)
4198 return -ENOMEM;
4199
Yan, Zhengf0486c62010-05-16 10:46:25 -04004200 rc->extent_root = extent_root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004201
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004202 rc->block_group = btrfs_lookup_block_group(fs_info, group_start);
4203 BUG_ON(!rc->block_group);
4204
Yan, Zhengf0486c62010-05-16 10:46:25 -04004205 if (!rc->block_group->ro) {
4206 ret = btrfs_set_block_group_ro(extent_root, rc->block_group);
4207 if (ret) {
4208 err = ret;
4209 goto out;
4210 }
4211 rw = 1;
4212 }
4213
Josef Bacik0af3d002010-06-21 14:48:16 -04004214 path = btrfs_alloc_path();
4215 if (!path) {
4216 err = -ENOMEM;
4217 goto out;
4218 }
4219
4220 inode = lookup_free_space_inode(fs_info->tree_root, rc->block_group,
4221 path);
4222 btrfs_free_path(path);
4223
4224 if (!IS_ERR(inode))
4225 ret = delete_block_group_cache(fs_info, inode, 0);
4226 else
4227 ret = PTR_ERR(inode);
4228
4229 if (ret && ret != -ENOENT) {
4230 err = ret;
4231 goto out;
4232 }
4233
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004234 rc->data_inode = create_reloc_inode(fs_info, rc->block_group);
4235 if (IS_ERR(rc->data_inode)) {
4236 err = PTR_ERR(rc->data_inode);
4237 rc->data_inode = NULL;
4238 goto out;
4239 }
4240
4241 printk(KERN_INFO "btrfs: relocating block group %llu flags %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004242 rc->block_group->key.objectid, rc->block_group->flags);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004243
Miao Xie91aef862013-11-04 23:13:26 +08004244 ret = btrfs_start_delalloc_roots(fs_info, 0);
Miao Xie8ccf6f192012-10-25 09:28:04 +00004245 if (ret < 0) {
4246 err = ret;
4247 goto out;
4248 }
Miao Xieb0244192013-11-04 23:13:25 +08004249 btrfs_wait_ordered_roots(fs_info, -1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004250
4251 while (1) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004252 mutex_lock(&fs_info->cleaner_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004253 ret = relocate_block_group(rc);
Yan, Zheng76dda932009-09-21 16:00:26 -04004254 mutex_unlock(&fs_info->cleaner_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004255 if (ret < 0) {
4256 err = ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004257 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004258 }
4259
4260 if (rc->extents_found == 0)
4261 break;
4262
4263 printk(KERN_INFO "btrfs: found %llu extents\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004264 rc->extents_found);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004265
4266 if (rc->stage == MOVE_DATA_EXTENTS && rc->found_file_extent) {
Josef Bacik0ef8b722013-10-25 16:13:35 -04004267 ret = btrfs_wait_ordered_range(rc->data_inode, 0,
4268 (u64)-1);
4269 if (ret) {
4270 err = ret;
4271 goto out;
4272 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004273 invalidate_mapping_pages(rc->data_inode->i_mapping,
4274 0, -1);
4275 rc->stage = UPDATE_DATA_PTRS;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004276 }
4277 }
4278
Yan, Zheng0257bb82009-09-24 09:17:31 -04004279 filemap_write_and_wait_range(fs_info->btree_inode->i_mapping,
4280 rc->block_group->key.objectid,
4281 rc->block_group->key.objectid +
4282 rc->block_group->key.offset - 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004283
4284 WARN_ON(rc->block_group->pinned > 0);
4285 WARN_ON(rc->block_group->reserved > 0);
4286 WARN_ON(btrfs_block_group_used(&rc->block_group->item) > 0);
4287out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04004288 if (err && rw)
4289 btrfs_set_block_group_rw(extent_root, rc->block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004290 iput(rc->data_inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004291 btrfs_put_block_group(rc->block_group);
4292 kfree(rc);
4293 return err;
4294}
4295
Yan, Zheng76dda932009-09-21 16:00:26 -04004296static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)
4297{
4298 struct btrfs_trans_handle *trans;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004299 int ret, err;
Yan, Zheng76dda932009-09-21 16:00:26 -04004300
Yan, Zhenga22285a2010-05-16 10:48:46 -04004301 trans = btrfs_start_transaction(root->fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004302 if (IS_ERR(trans))
4303 return PTR_ERR(trans);
Yan, Zheng76dda932009-09-21 16:00:26 -04004304
4305 memset(&root->root_item.drop_progress, 0,
4306 sizeof(root->root_item.drop_progress));
4307 root->root_item.drop_level = 0;
4308 btrfs_set_root_refs(&root->root_item, 0);
4309 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4310 &root->root_key, &root->root_item);
Yan, Zheng76dda932009-09-21 16:00:26 -04004311
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004312 err = btrfs_end_transaction(trans, root->fs_info->tree_root);
4313 if (err)
4314 return err;
4315 return ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04004316}
4317
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004318/*
4319 * recover relocation interrupted by system crash.
4320 *
4321 * this function resumes merging reloc trees with corresponding fs trees.
4322 * this is important for keeping the sharing of tree blocks
4323 */
4324int btrfs_recover_relocation(struct btrfs_root *root)
4325{
4326 LIST_HEAD(reloc_roots);
4327 struct btrfs_key key;
4328 struct btrfs_root *fs_root;
4329 struct btrfs_root *reloc_root;
4330 struct btrfs_path *path;
4331 struct extent_buffer *leaf;
4332 struct reloc_control *rc = NULL;
4333 struct btrfs_trans_handle *trans;
4334 int ret;
4335 int err = 0;
4336
4337 path = btrfs_alloc_path();
4338 if (!path)
4339 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04004340 path->reada = -1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004341
4342 key.objectid = BTRFS_TREE_RELOC_OBJECTID;
4343 key.type = BTRFS_ROOT_ITEM_KEY;
4344 key.offset = (u64)-1;
4345
4346 while (1) {
4347 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key,
4348 path, 0, 0);
4349 if (ret < 0) {
4350 err = ret;
4351 goto out;
4352 }
4353 if (ret > 0) {
4354 if (path->slots[0] == 0)
4355 break;
4356 path->slots[0]--;
4357 }
4358 leaf = path->nodes[0];
4359 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004360 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004361
4362 if (key.objectid != BTRFS_TREE_RELOC_OBJECTID ||
4363 key.type != BTRFS_ROOT_ITEM_KEY)
4364 break;
4365
Miao Xiecb517ea2013-05-15 07:48:19 +00004366 reloc_root = btrfs_read_fs_root(root, &key);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004367 if (IS_ERR(reloc_root)) {
4368 err = PTR_ERR(reloc_root);
4369 goto out;
4370 }
4371
4372 list_add(&reloc_root->root_list, &reloc_roots);
4373
4374 if (btrfs_root_refs(&reloc_root->root_item) > 0) {
4375 fs_root = read_fs_root(root->fs_info,
4376 reloc_root->root_key.offset);
4377 if (IS_ERR(fs_root)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004378 ret = PTR_ERR(fs_root);
4379 if (ret != -ENOENT) {
4380 err = ret;
4381 goto out;
4382 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004383 ret = mark_garbage_root(reloc_root);
4384 if (ret < 0) {
4385 err = ret;
4386 goto out;
4387 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004388 }
4389 }
4390
4391 if (key.offset == 0)
4392 break;
4393
4394 key.offset--;
4395 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004396 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004397
4398 if (list_empty(&reloc_roots))
4399 goto out;
4400
Josef Bacika9995ee2013-05-31 13:04:36 -04004401 rc = alloc_reloc_control(root->fs_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004402 if (!rc) {
4403 err = -ENOMEM;
4404 goto out;
4405 }
4406
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004407 rc->extent_root = root->fs_info->extent_root;
4408
4409 set_reloc_control(rc);
4410
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004411 trans = btrfs_join_transaction(rc->extent_root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004412 if (IS_ERR(trans)) {
4413 unset_reloc_control(rc);
4414 err = PTR_ERR(trans);
4415 goto out_free;
4416 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004417
4418 rc->merge_reloc_tree = 1;
4419
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004420 while (!list_empty(&reloc_roots)) {
4421 reloc_root = list_entry(reloc_roots.next,
4422 struct btrfs_root, root_list);
4423 list_del(&reloc_root->root_list);
4424
4425 if (btrfs_root_refs(&reloc_root->root_item) == 0) {
4426 list_add_tail(&reloc_root->root_list,
4427 &rc->reloc_roots);
4428 continue;
4429 }
4430
4431 fs_root = read_fs_root(root->fs_info,
4432 reloc_root->root_key.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004433 if (IS_ERR(fs_root)) {
4434 err = PTR_ERR(fs_root);
4435 goto out_free;
4436 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004437
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04004438 err = __add_reloc_root(reloc_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004439 BUG_ON(err < 0); /* -ENOMEM or logic error */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004440 fs_root->reloc_root = reloc_root;
4441 }
4442
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004443 err = btrfs_commit_transaction(trans, rc->extent_root);
4444 if (err)
4445 goto out_free;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004446
4447 merge_reloc_roots(rc);
4448
4449 unset_reloc_control(rc);
4450
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004451 trans = btrfs_join_transaction(rc->extent_root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004452 if (IS_ERR(trans))
4453 err = PTR_ERR(trans);
4454 else
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004455 err = btrfs_commit_transaction(trans, rc->extent_root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004456out_free:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004457 kfree(rc);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004458out:
Liu Boaca1bba2013-03-04 16:25:37 +00004459 if (!list_empty(&reloc_roots))
4460 free_reloc_roots(&reloc_roots);
4461
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004462 btrfs_free_path(path);
4463
4464 if (err == 0) {
4465 /* cleanup orphan inode in data relocation tree */
4466 fs_root = read_fs_root(root->fs_info,
4467 BTRFS_DATA_RELOC_TREE_OBJECTID);
4468 if (IS_ERR(fs_root))
4469 err = PTR_ERR(fs_root);
Miao Xied7ce5842010-02-02 08:46:44 +00004470 else
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004471 err = btrfs_orphan_cleanup(fs_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004472 }
4473 return err;
4474}
4475
4476/*
4477 * helper to add ordered checksum for data relocation.
4478 *
4479 * cloning checksum properly handles the nodatasum extents.
4480 * it also saves CPU time to re-calculate the checksum.
4481 */
4482int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
4483{
4484 struct btrfs_ordered_sum *sums;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004485 struct btrfs_ordered_extent *ordered;
4486 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004487 int ret;
4488 u64 disk_bytenr;
Josef Bacik4577b012013-09-27 09:33:09 -04004489 u64 new_bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004490 LIST_HEAD(list);
4491
4492 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
4493 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
4494
4495 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
4496 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01004497 disk_bytenr + len - 1, &list, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004498 if (ret)
4499 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004500
4501 while (!list_empty(&list)) {
4502 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
4503 list_del_init(&sums->list);
4504
Josef Bacik4577b012013-09-27 09:33:09 -04004505 /*
4506 * We need to offset the new_bytenr based on where the csum is.
4507 * We need to do this because we will read in entire prealloc
4508 * extents but we may have written to say the middle of the
4509 * prealloc extent, so we need to make sure the csum goes with
4510 * the right disk offset.
4511 *
4512 * We can do this because the data reloc inode refers strictly
4513 * to the on disk bytes, so we don't have to worry about
4514 * disk_len vs real len like with real inodes since it's all
4515 * disk length.
4516 */
4517 new_bytenr = ordered->start + (sums->bytenr - disk_bytenr);
4518 sums->bytenr = new_bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004519
4520 btrfs_add_ordered_sum(inode, ordered, sums);
4521 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004522out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004523 btrfs_put_ordered_extent(ordered);
Andi Kleen411fc6b2010-10-29 15:14:31 -04004524 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004525}
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004526
Josef Bacik83d4cfd2013-08-30 15:09:51 -04004527int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans,
4528 struct btrfs_root *root, struct extent_buffer *buf,
4529 struct extent_buffer *cow)
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004530{
4531 struct reloc_control *rc;
4532 struct backref_node *node;
4533 int first_cow = 0;
4534 int level;
Josef Bacik83d4cfd2013-08-30 15:09:51 -04004535 int ret = 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004536
4537 rc = root->fs_info->reloc_ctl;
4538 if (!rc)
Josef Bacik83d4cfd2013-08-30 15:09:51 -04004539 return 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004540
4541 BUG_ON(rc->stage == UPDATE_DATA_PTRS &&
4542 root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID);
4543
Wang Shilongc974c462013-12-11 19:29:51 +08004544 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
4545 if (buf == root->node)
4546 __update_reloc_root(root, cow->start);
4547 }
4548
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004549 level = btrfs_header_level(buf);
4550 if (btrfs_header_generation(buf) <=
4551 btrfs_root_last_snapshot(&root->root_item))
4552 first_cow = 1;
4553
4554 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID &&
4555 rc->create_reloc_tree) {
4556 WARN_ON(!first_cow && level == 0);
4557
4558 node = rc->backref_cache.path[level];
4559 BUG_ON(node->bytenr != buf->start &&
4560 node->new_bytenr != buf->start);
4561
4562 drop_node_buffer(node);
4563 extent_buffer_get(cow);
4564 node->eb = cow;
4565 node->new_bytenr = cow->start;
4566
4567 if (!node->pending) {
4568 list_move_tail(&node->list,
4569 &rc->backref_cache.pending[level]);
4570 node->pending = 1;
4571 }
4572
4573 if (first_cow)
4574 __mark_block_processed(rc, node);
4575
4576 if (first_cow && level > 0)
4577 rc->nodes_relocated += buf->len;
4578 }
4579
Josef Bacik83d4cfd2013-08-30 15:09:51 -04004580 if (level == 0 && first_cow && rc->stage == UPDATE_DATA_PTRS)
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004581 ret = replace_file_extents(trans, rc, root, cow);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04004582 return ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004583}
4584
4585/*
4586 * called before creating snapshot. it calculates metadata reservation
4587 * requried for relocating tree blocks in the snapshot
4588 */
4589void btrfs_reloc_pre_snapshot(struct btrfs_trans_handle *trans,
4590 struct btrfs_pending_snapshot *pending,
4591 u64 *bytes_to_reserve)
4592{
4593 struct btrfs_root *root;
4594 struct reloc_control *rc;
4595
4596 root = pending->root;
4597 if (!root->reloc_root)
4598 return;
4599
4600 rc = root->fs_info->reloc_ctl;
4601 if (!rc->merge_reloc_tree)
4602 return;
4603
4604 root = root->reloc_root;
4605 BUG_ON(btrfs_root_refs(&root->root_item) == 0);
4606 /*
4607 * relocation is in the stage of merging trees. the space
4608 * used by merging a reloc tree is twice the size of
4609 * relocated tree nodes in the worst case. half for cowing
4610 * the reloc tree, half for cowing the fs tree. the space
4611 * used by cowing the reloc tree will be freed after the
4612 * tree is dropped. if we create snapshot, cowing the fs
4613 * tree may use more space than it frees. so we need
4614 * reserve extra space.
4615 */
4616 *bytes_to_reserve += rc->nodes_relocated;
4617}
4618
4619/*
4620 * called after snapshot is created. migrate block reservation
4621 * and create reloc root for the newly created snapshot
4622 */
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004623int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004624 struct btrfs_pending_snapshot *pending)
4625{
4626 struct btrfs_root *root = pending->root;
4627 struct btrfs_root *reloc_root;
4628 struct btrfs_root *new_root;
4629 struct reloc_control *rc;
4630 int ret;
4631
4632 if (!root->reloc_root)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004633 return 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004634
4635 rc = root->fs_info->reloc_ctl;
4636 rc->merging_rsv_size += rc->nodes_relocated;
4637
4638 if (rc->merge_reloc_tree) {
4639 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
4640 rc->block_rsv,
4641 rc->nodes_relocated);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004642 if (ret)
4643 return ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004644 }
4645
4646 new_root = pending->snap;
4647 reloc_root = create_reloc_root(trans, root->reloc_root,
4648 new_root->root_key.objectid);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004649 if (IS_ERR(reloc_root))
4650 return PTR_ERR(reloc_root);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004651
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04004652 ret = __add_reloc_root(reloc_root);
4653 BUG_ON(ret < 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004654 new_root->reloc_root = reloc_root;
4655
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004656 if (rc->create_reloc_tree)
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004657 ret = clone_backref_node(trans, rc, root, reloc_root);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004658 return ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004659}