blob: 429c73c374b84f9bcd468067221bf99e0b9f67db [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 ||
Wang Shilong66463742013-12-10 00:14:34 +0800574 root_objectid == BTRFS_CSUM_TREE_OBJECTID ||
575 root_objectid == BTRFS_UUID_TREE_OBJECTID ||
576 root_objectid == BTRFS_QUOTA_TREE_OBJECTID)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400577 return 1;
578 return 0;
579}
580
581static struct btrfs_root *read_fs_root(struct btrfs_fs_info *fs_info,
582 u64 root_objectid)
583{
584 struct btrfs_key key;
585
586 key.objectid = root_objectid;
587 key.type = BTRFS_ROOT_ITEM_KEY;
588 if (is_cowonly_root(root_objectid))
589 key.offset = 0;
590 else
591 key.offset = (u64)-1;
592
Miao Xiec00869f2013-09-25 21:47:44 +0800593 return btrfs_get_fs_root(fs_info, &key, false);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400594}
595
596#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
597static noinline_for_stack
598struct btrfs_root *find_tree_root(struct reloc_control *rc,
599 struct extent_buffer *leaf,
600 struct btrfs_extent_ref_v0 *ref0)
601{
602 struct btrfs_root *root;
603 u64 root_objectid = btrfs_ref_root_v0(leaf, ref0);
604 u64 generation = btrfs_ref_generation_v0(leaf, ref0);
605
606 BUG_ON(root_objectid == BTRFS_TREE_RELOC_OBJECTID);
607
608 root = read_fs_root(rc->extent_root->fs_info, root_objectid);
609 BUG_ON(IS_ERR(root));
610
611 if (root->ref_cows &&
612 generation != btrfs_root_generation(&root->root_item))
613 return NULL;
614
615 return root;
616}
617#endif
618
619static noinline_for_stack
620int find_inline_backref(struct extent_buffer *leaf, int slot,
621 unsigned long *ptr, unsigned long *end)
622{
Josef Bacik3173a182013-03-07 14:22:04 -0500623 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400624 struct btrfs_extent_item *ei;
625 struct btrfs_tree_block_info *bi;
626 u32 item_size;
627
Josef Bacik3173a182013-03-07 14:22:04 -0500628 btrfs_item_key_to_cpu(leaf, &key, slot);
629
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400630 item_size = btrfs_item_size_nr(leaf, slot);
631#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
632 if (item_size < sizeof(*ei)) {
633 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
634 return 1;
635 }
636#endif
637 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
638 WARN_ON(!(btrfs_extent_flags(leaf, ei) &
639 BTRFS_EXTENT_FLAG_TREE_BLOCK));
640
Josef Bacik3173a182013-03-07 14:22:04 -0500641 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
642 item_size <= sizeof(*ei) + sizeof(*bi)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400643 WARN_ON(item_size < sizeof(*ei) + sizeof(*bi));
644 return 1;
645 }
Josef Bacikd062d132013-07-30 15:44:09 -0400646 if (key.type == BTRFS_METADATA_ITEM_KEY &&
647 item_size <= sizeof(*ei)) {
648 WARN_ON(item_size < sizeof(*ei));
649 return 1;
650 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400651
Josef Bacik3173a182013-03-07 14:22:04 -0500652 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
653 bi = (struct btrfs_tree_block_info *)(ei + 1);
654 *ptr = (unsigned long)(bi + 1);
655 } else {
656 *ptr = (unsigned long)(ei + 1);
657 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400658 *end = (unsigned long)ei + item_size;
659 return 0;
660}
661
662/*
663 * build backref tree for a given tree block. root of the backref tree
664 * corresponds the tree block, leaves of the backref tree correspond
665 * roots of b-trees that reference the tree block.
666 *
667 * the basic idea of this function is check backrefs of a given block
668 * to find upper level blocks that refernece the block, and then check
669 * bakcrefs of these upper level blocks recursively. the recursion stop
670 * when tree root is reached or backrefs for the block is cached.
671 *
672 * NOTE: if we find backrefs for a block are cached, we know backrefs
673 * for all upper level blocks that directly/indirectly reference the
674 * block are also cached.
675 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400676static noinline_for_stack
677struct backref_node *build_backref_tree(struct reloc_control *rc,
678 struct btrfs_key *node_key,
679 int level, u64 bytenr)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400680{
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400681 struct backref_cache *cache = &rc->backref_cache;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400682 struct btrfs_path *path1;
683 struct btrfs_path *path2;
684 struct extent_buffer *eb;
685 struct btrfs_root *root;
686 struct backref_node *cur;
687 struct backref_node *upper;
688 struct backref_node *lower;
689 struct backref_node *node = NULL;
690 struct backref_node *exist = NULL;
691 struct backref_edge *edge;
692 struct rb_node *rb_node;
693 struct btrfs_key key;
694 unsigned long end;
695 unsigned long ptr;
696 LIST_HEAD(list);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400697 LIST_HEAD(useless);
698 int cowonly;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400699 int ret;
700 int err = 0;
Josef Bacikb6c60c82013-07-30 16:30:30 -0400701 bool need_check = true;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400702
703 path1 = btrfs_alloc_path();
704 path2 = btrfs_alloc_path();
705 if (!path1 || !path2) {
706 err = -ENOMEM;
707 goto out;
708 }
Josef Bacik026fd312011-05-13 10:32:11 -0400709 path1->reada = 1;
710 path2->reada = 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400711
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400712 node = alloc_backref_node(cache);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400713 if (!node) {
714 err = -ENOMEM;
715 goto out;
716 }
717
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400718 node->bytenr = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400719 node->level = level;
720 node->lowest = 1;
721 cur = node;
722again:
723 end = 0;
724 ptr = 0;
725 key.objectid = cur->bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -0500726 key.type = BTRFS_METADATA_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400727 key.offset = (u64)-1;
728
729 path1->search_commit_root = 1;
730 path1->skip_locking = 1;
731 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path1,
732 0, 0);
733 if (ret < 0) {
734 err = ret;
735 goto out;
736 }
737 BUG_ON(!ret || !path1->slots[0]);
738
739 path1->slots[0]--;
740
741 WARN_ON(cur->checked);
742 if (!list_empty(&cur->upper)) {
743 /*
Justin P. Mattock70f23fd2011-05-10 10:16:21 +0200744 * the backref was added previously when processing
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400745 * backref of type BTRFS_TREE_BLOCK_REF_KEY
746 */
747 BUG_ON(!list_is_singular(&cur->upper));
748 edge = list_entry(cur->upper.next, struct backref_edge,
749 list[LOWER]);
750 BUG_ON(!list_empty(&edge->list[UPPER]));
751 exist = edge->node[UPPER];
752 /*
753 * add the upper level block to pending list if we need
754 * check its backrefs
755 */
756 if (!exist->checked)
757 list_add_tail(&edge->list[UPPER], &list);
758 } else {
759 exist = NULL;
760 }
761
762 while (1) {
763 cond_resched();
764 eb = path1->nodes[0];
765
766 if (ptr >= end) {
767 if (path1->slots[0] >= btrfs_header_nritems(eb)) {
768 ret = btrfs_next_leaf(rc->extent_root, path1);
769 if (ret < 0) {
770 err = ret;
771 goto out;
772 }
773 if (ret > 0)
774 break;
775 eb = path1->nodes[0];
776 }
777
778 btrfs_item_key_to_cpu(eb, &key, path1->slots[0]);
779 if (key.objectid != cur->bytenr) {
780 WARN_ON(exist);
781 break;
782 }
783
Josef Bacik3173a182013-03-07 14:22:04 -0500784 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
785 key.type == BTRFS_METADATA_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400786 ret = find_inline_backref(eb, path1->slots[0],
787 &ptr, &end);
788 if (ret)
789 goto next;
790 }
791 }
792
793 if (ptr < end) {
794 /* update key for inline back ref */
795 struct btrfs_extent_inline_ref *iref;
796 iref = (struct btrfs_extent_inline_ref *)ptr;
797 key.type = btrfs_extent_inline_ref_type(eb, iref);
798 key.offset = btrfs_extent_inline_ref_offset(eb, iref);
799 WARN_ON(key.type != BTRFS_TREE_BLOCK_REF_KEY &&
800 key.type != BTRFS_SHARED_BLOCK_REF_KEY);
801 }
802
803 if (exist &&
804 ((key.type == BTRFS_TREE_BLOCK_REF_KEY &&
805 exist->owner == key.offset) ||
806 (key.type == BTRFS_SHARED_BLOCK_REF_KEY &&
807 exist->bytenr == key.offset))) {
808 exist = NULL;
809 goto next;
810 }
811
812#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
813 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY ||
814 key.type == BTRFS_EXTENT_REF_V0_KEY) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400815 if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400816 struct btrfs_extent_ref_v0 *ref0;
817 ref0 = btrfs_item_ptr(eb, path1->slots[0],
818 struct btrfs_extent_ref_v0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400819 if (key.objectid == key.offset) {
Yan, Zheng046f2642010-05-31 08:58:47 +0000820 root = find_tree_root(rc, eb, ref0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400821 if (root && !should_ignore_root(root))
822 cur->root = root;
823 else
824 list_add(&cur->list, &useless);
825 break;
826 }
Yan, Zheng046f2642010-05-31 08:58:47 +0000827 if (is_cowonly_root(btrfs_ref_root_v0(eb,
828 ref0)))
829 cur->cowonly = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400830 }
831#else
832 BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY);
833 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
834#endif
835 if (key.objectid == key.offset) {
836 /*
837 * only root blocks of reloc trees use
838 * backref of this type.
839 */
840 root = find_reloc_root(rc, cur->bytenr);
841 BUG_ON(!root);
842 cur->root = root;
843 break;
844 }
845
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400846 edge = alloc_backref_edge(cache);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400847 if (!edge) {
848 err = -ENOMEM;
849 goto out;
850 }
851 rb_node = tree_search(&cache->rb_root, key.offset);
852 if (!rb_node) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400853 upper = alloc_backref_node(cache);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400854 if (!upper) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400855 free_backref_edge(cache, edge);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400856 err = -ENOMEM;
857 goto out;
858 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400859 upper->bytenr = key.offset;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400860 upper->level = cur->level + 1;
861 /*
862 * backrefs for the upper level block isn't
863 * cached, add the block to pending list
864 */
865 list_add_tail(&edge->list[UPPER], &list);
866 } else {
867 upper = rb_entry(rb_node, struct backref_node,
868 rb_node);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400869 BUG_ON(!upper->checked);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400870 INIT_LIST_HEAD(&edge->list[UPPER]);
871 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400872 list_add_tail(&edge->list[LOWER], &cur->upper);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400873 edge->node[LOWER] = cur;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400874 edge->node[UPPER] = upper;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400875
876 goto next;
877 } else if (key.type != BTRFS_TREE_BLOCK_REF_KEY) {
878 goto next;
879 }
880
881 /* key.type == BTRFS_TREE_BLOCK_REF_KEY */
882 root = read_fs_root(rc->extent_root->fs_info, key.offset);
883 if (IS_ERR(root)) {
884 err = PTR_ERR(root);
885 goto out;
886 }
887
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400888 if (!root->ref_cows)
889 cur->cowonly = 1;
890
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400891 if (btrfs_root_level(&root->root_item) == cur->level) {
892 /* tree root */
893 BUG_ON(btrfs_root_bytenr(&root->root_item) !=
894 cur->bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400895 if (should_ignore_root(root))
896 list_add(&cur->list, &useless);
897 else
898 cur->root = root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400899 break;
900 }
901
902 level = cur->level + 1;
903
904 /*
905 * searching the tree to find upper level blocks
906 * reference the block.
907 */
908 path2->search_commit_root = 1;
909 path2->skip_locking = 1;
910 path2->lowest_level = level;
911 ret = btrfs_search_slot(NULL, root, node_key, path2, 0, 0);
912 path2->lowest_level = 0;
913 if (ret < 0) {
914 err = ret;
915 goto out;
916 }
Yan Zheng33c66f42009-07-22 09:59:00 -0400917 if (ret > 0 && path2->slots[level] > 0)
918 path2->slots[level]--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400919
920 eb = path2->nodes[level];
921 WARN_ON(btrfs_node_blockptr(eb, path2->slots[level]) !=
922 cur->bytenr);
923
924 lower = cur;
Josef Bacikb6c60c82013-07-30 16:30:30 -0400925 need_check = true;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400926 for (; level < BTRFS_MAX_LEVEL; level++) {
927 if (!path2->nodes[level]) {
928 BUG_ON(btrfs_root_bytenr(&root->root_item) !=
929 lower->bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400930 if (should_ignore_root(root))
931 list_add(&lower->list, &useless);
932 else
933 lower->root = root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400934 break;
935 }
936
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400937 edge = alloc_backref_edge(cache);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400938 if (!edge) {
939 err = -ENOMEM;
940 goto out;
941 }
942
943 eb = path2->nodes[level];
944 rb_node = tree_search(&cache->rb_root, eb->start);
945 if (!rb_node) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400946 upper = alloc_backref_node(cache);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400947 if (!upper) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400948 free_backref_edge(cache, edge);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400949 err = -ENOMEM;
950 goto out;
951 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400952 upper->bytenr = eb->start;
953 upper->owner = btrfs_header_owner(eb);
954 upper->level = lower->level + 1;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400955 if (!root->ref_cows)
956 upper->cowonly = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400957
958 /*
959 * if we know the block isn't shared
960 * we can void checking its backrefs.
961 */
962 if (btrfs_block_can_be_shared(root, eb))
963 upper->checked = 0;
964 else
965 upper->checked = 1;
966
967 /*
968 * add the block to pending list if we
Josef Bacikb6c60c82013-07-30 16:30:30 -0400969 * need check its backrefs, we only do this once
970 * while walking up a tree as we will catch
971 * anything else later on.
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400972 */
Josef Bacikb6c60c82013-07-30 16:30:30 -0400973 if (!upper->checked && need_check) {
974 need_check = false;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400975 list_add_tail(&edge->list[UPPER],
976 &list);
977 } else
978 INIT_LIST_HEAD(&edge->list[UPPER]);
979 } else {
980 upper = rb_entry(rb_node, struct backref_node,
981 rb_node);
982 BUG_ON(!upper->checked);
983 INIT_LIST_HEAD(&edge->list[UPPER]);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400984 if (!upper->owner)
985 upper->owner = btrfs_header_owner(eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400986 }
987 list_add_tail(&edge->list[LOWER], &lower->upper);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400988 edge->node[LOWER] = lower;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400989 edge->node[UPPER] = upper;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400990
991 if (rb_node)
992 break;
993 lower = upper;
994 upper = NULL;
995 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200996 btrfs_release_path(path2);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400997next:
998 if (ptr < end) {
999 ptr += btrfs_extent_inline_ref_size(key.type);
1000 if (ptr >= end) {
1001 WARN_ON(ptr > end);
1002 ptr = 0;
1003 end = 0;
1004 }
1005 }
1006 if (ptr >= end)
1007 path1->slots[0]++;
1008 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001009 btrfs_release_path(path1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001010
1011 cur->checked = 1;
1012 WARN_ON(exist);
1013
1014 /* the pending list isn't empty, take the first block to process */
1015 if (!list_empty(&list)) {
1016 edge = list_entry(list.next, struct backref_edge, list[UPPER]);
1017 list_del_init(&edge->list[UPPER]);
1018 cur = edge->node[UPPER];
1019 goto again;
1020 }
1021
1022 /*
1023 * everything goes well, connect backref nodes and insert backref nodes
1024 * into the cache.
1025 */
1026 BUG_ON(!node->checked);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001027 cowonly = node->cowonly;
1028 if (!cowonly) {
1029 rb_node = tree_insert(&cache->rb_root, node->bytenr,
1030 &node->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -04001031 if (rb_node)
1032 backref_tree_panic(rb_node, -EEXIST, node->bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001033 list_add_tail(&node->lower, &cache->leaves);
1034 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001035
1036 list_for_each_entry(edge, &node->upper, list[LOWER])
1037 list_add_tail(&edge->list[UPPER], &list);
1038
1039 while (!list_empty(&list)) {
1040 edge = list_entry(list.next, struct backref_edge, list[UPPER]);
1041 list_del_init(&edge->list[UPPER]);
1042 upper = edge->node[UPPER];
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001043 if (upper->detached) {
1044 list_del(&edge->list[LOWER]);
1045 lower = edge->node[LOWER];
1046 free_backref_edge(cache, edge);
1047 if (list_empty(&lower->upper))
1048 list_add(&lower->list, &useless);
1049 continue;
1050 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001051
1052 if (!RB_EMPTY_NODE(&upper->rb_node)) {
1053 if (upper->lowest) {
1054 list_del_init(&upper->lower);
1055 upper->lowest = 0;
1056 }
1057
1058 list_add_tail(&edge->list[UPPER], &upper->lower);
1059 continue;
1060 }
1061
1062 BUG_ON(!upper->checked);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001063 BUG_ON(cowonly != upper->cowonly);
1064 if (!cowonly) {
1065 rb_node = tree_insert(&cache->rb_root, upper->bytenr,
1066 &upper->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -04001067 if (rb_node)
1068 backref_tree_panic(rb_node, -EEXIST,
1069 upper->bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001070 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001071
1072 list_add_tail(&edge->list[UPPER], &upper->lower);
1073
1074 list_for_each_entry(edge, &upper->upper, list[LOWER])
1075 list_add_tail(&edge->list[UPPER], &list);
1076 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001077 /*
1078 * process useless backref nodes. backref nodes for tree leaves
1079 * are deleted from the cache. backref nodes for upper level
1080 * tree blocks are left in the cache to avoid unnecessary backref
1081 * lookup.
1082 */
1083 while (!list_empty(&useless)) {
1084 upper = list_entry(useless.next, struct backref_node, list);
1085 list_del_init(&upper->list);
1086 BUG_ON(!list_empty(&upper->upper));
1087 if (upper == node)
1088 node = NULL;
1089 if (upper->lowest) {
1090 list_del_init(&upper->lower);
1091 upper->lowest = 0;
1092 }
1093 while (!list_empty(&upper->lower)) {
1094 edge = list_entry(upper->lower.next,
1095 struct backref_edge, list[UPPER]);
1096 list_del(&edge->list[UPPER]);
1097 list_del(&edge->list[LOWER]);
1098 lower = edge->node[LOWER];
1099 free_backref_edge(cache, edge);
1100
1101 if (list_empty(&lower->upper))
1102 list_add(&lower->list, &useless);
1103 }
1104 __mark_block_processed(rc, upper);
1105 if (upper->level > 0) {
1106 list_add(&upper->list, &cache->detached);
1107 upper->detached = 1;
1108 } else {
1109 rb_erase(&upper->rb_node, &cache->rb_root);
1110 free_backref_node(cache, upper);
1111 }
1112 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001113out:
1114 btrfs_free_path(path1);
1115 btrfs_free_path(path2);
1116 if (err) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001117 while (!list_empty(&useless)) {
1118 lower = list_entry(useless.next,
1119 struct backref_node, upper);
1120 list_del_init(&lower->upper);
1121 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001122 upper = node;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001123 INIT_LIST_HEAD(&list);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001124 while (upper) {
1125 if (RB_EMPTY_NODE(&upper->rb_node)) {
1126 list_splice_tail(&upper->upper, &list);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001127 free_backref_node(cache, upper);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001128 }
1129
1130 if (list_empty(&list))
1131 break;
1132
1133 edge = list_entry(list.next, struct backref_edge,
1134 list[LOWER]);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001135 list_del(&edge->list[LOWER]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001136 upper = edge->node[UPPER];
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001137 free_backref_edge(cache, edge);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001138 }
1139 return ERR_PTR(err);
1140 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001141 BUG_ON(node && node->detached);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001142 return node;
1143}
1144
1145/*
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001146 * helper to add backref node for the newly created snapshot.
1147 * the backref node is created by cloning backref node that
1148 * corresponds to root of source tree
1149 */
1150static int clone_backref_node(struct btrfs_trans_handle *trans,
1151 struct reloc_control *rc,
1152 struct btrfs_root *src,
1153 struct btrfs_root *dest)
1154{
1155 struct btrfs_root *reloc_root = src->reloc_root;
1156 struct backref_cache *cache = &rc->backref_cache;
1157 struct backref_node *node = NULL;
1158 struct backref_node *new_node;
1159 struct backref_edge *edge;
1160 struct backref_edge *new_edge;
1161 struct rb_node *rb_node;
1162
1163 if (cache->last_trans > 0)
1164 update_backref_cache(trans, cache);
1165
1166 rb_node = tree_search(&cache->rb_root, src->commit_root->start);
1167 if (rb_node) {
1168 node = rb_entry(rb_node, struct backref_node, rb_node);
1169 if (node->detached)
1170 node = NULL;
1171 else
1172 BUG_ON(node->new_bytenr != reloc_root->node->start);
1173 }
1174
1175 if (!node) {
1176 rb_node = tree_search(&cache->rb_root,
1177 reloc_root->commit_root->start);
1178 if (rb_node) {
1179 node = rb_entry(rb_node, struct backref_node,
1180 rb_node);
1181 BUG_ON(node->detached);
1182 }
1183 }
1184
1185 if (!node)
1186 return 0;
1187
1188 new_node = alloc_backref_node(cache);
1189 if (!new_node)
1190 return -ENOMEM;
1191
1192 new_node->bytenr = dest->node->start;
1193 new_node->level = node->level;
1194 new_node->lowest = node->lowest;
Yan, Zheng6848ad62011-02-14 16:00:03 -05001195 new_node->checked = 1;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001196 new_node->root = dest;
1197
1198 if (!node->lowest) {
1199 list_for_each_entry(edge, &node->lower, list[UPPER]) {
1200 new_edge = alloc_backref_edge(cache);
1201 if (!new_edge)
1202 goto fail;
1203
1204 new_edge->node[UPPER] = new_node;
1205 new_edge->node[LOWER] = edge->node[LOWER];
1206 list_add_tail(&new_edge->list[UPPER],
1207 &new_node->lower);
1208 }
Miao Xie76b9e232011-11-10 20:45:05 -05001209 } else {
1210 list_add_tail(&new_node->lower, &cache->leaves);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001211 }
1212
1213 rb_node = tree_insert(&cache->rb_root, new_node->bytenr,
1214 &new_node->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -04001215 if (rb_node)
1216 backref_tree_panic(rb_node, -EEXIST, new_node->bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001217
1218 if (!new_node->lowest) {
1219 list_for_each_entry(new_edge, &new_node->lower, list[UPPER]) {
1220 list_add_tail(&new_edge->list[LOWER],
1221 &new_edge->node[LOWER]->upper);
1222 }
1223 }
1224 return 0;
1225fail:
1226 while (!list_empty(&new_node->lower)) {
1227 new_edge = list_entry(new_node->lower.next,
1228 struct backref_edge, list[UPPER]);
1229 list_del(&new_edge->list[UPPER]);
1230 free_backref_edge(cache, new_edge);
1231 }
1232 free_backref_node(cache, new_node);
1233 return -ENOMEM;
1234}
1235
1236/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001237 * helper to add 'address of tree root -> reloc tree' mapping
1238 */
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001239static int __must_check __add_reloc_root(struct btrfs_root *root)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001240{
1241 struct rb_node *rb_node;
1242 struct mapping_node *node;
1243 struct reloc_control *rc = root->fs_info->reloc_ctl;
1244
1245 node = kmalloc(sizeof(*node), GFP_NOFS);
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001246 if (!node)
1247 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001248
1249 node->bytenr = root->node->start;
1250 node->data = root;
1251
1252 spin_lock(&rc->reloc_root_tree.lock);
1253 rb_node = tree_insert(&rc->reloc_root_tree.rb_root,
1254 node->bytenr, &node->rb_node);
1255 spin_unlock(&rc->reloc_root_tree.lock);
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001256 if (rb_node) {
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001257 btrfs_panic(root->fs_info, -EEXIST, "Duplicate root found "
1258 "for start=%llu while inserting into relocation "
Joe Perches533574c2012-07-30 14:40:13 -07001259 "tree\n", node->bytenr);
Dan Carpenter23291a02012-06-25 05:15:23 -06001260 kfree(node);
1261 return -EEXIST;
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001262 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001263
1264 list_add_tail(&root->root_list, &rc->reloc_roots);
1265 return 0;
1266}
1267
1268/*
Wang Shilongc974c462013-12-11 19:29:51 +08001269 * helper to delete the 'address of tree root -> reloc tree'
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001270 * mapping
1271 */
Wang Shilongc974c462013-12-11 19:29:51 +08001272static void __del_reloc_root(struct btrfs_root *root)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001273{
1274 struct rb_node *rb_node;
1275 struct mapping_node *node = NULL;
1276 struct reloc_control *rc = root->fs_info->reloc_ctl;
1277
1278 spin_lock(&rc->reloc_root_tree.lock);
1279 rb_node = tree_search(&rc->reloc_root_tree.rb_root,
Wang Shilongc974c462013-12-11 19:29:51 +08001280 root->node->start);
1281 if (rb_node) {
1282 node = rb_entry(rb_node, struct mapping_node, rb_node);
1283 rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
1284 }
1285 spin_unlock(&rc->reloc_root_tree.lock);
1286
1287 if (!node)
1288 return;
1289 BUG_ON((struct btrfs_root *)node->data != root);
1290
1291 spin_lock(&root->fs_info->trans_lock);
1292 list_del_init(&root->root_list);
1293 spin_unlock(&root->fs_info->trans_lock);
1294 kfree(node);
1295}
1296
1297/*
1298 * helper to update the 'address of tree root -> reloc tree'
1299 * mapping
1300 */
1301static int __update_reloc_root(struct btrfs_root *root, u64 new_bytenr)
1302{
1303 struct rb_node *rb_node;
1304 struct mapping_node *node = NULL;
1305 struct reloc_control *rc = root->fs_info->reloc_ctl;
1306
1307 spin_lock(&rc->reloc_root_tree.lock);
1308 rb_node = tree_search(&rc->reloc_root_tree.rb_root,
1309 root->node->start);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001310 if (rb_node) {
1311 node = rb_entry(rb_node, struct mapping_node, rb_node);
1312 rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
1313 }
1314 spin_unlock(&rc->reloc_root_tree.lock);
1315
Liu Bo8f71f3e2013-03-04 16:25:36 +00001316 if (!node)
1317 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001318 BUG_ON((struct btrfs_root *)node->data != root);
1319
Wang Shilongc974c462013-12-11 19:29:51 +08001320 spin_lock(&rc->reloc_root_tree.lock);
1321 node->bytenr = new_bytenr;
1322 rb_node = tree_insert(&rc->reloc_root_tree.rb_root,
1323 node->bytenr, &node->rb_node);
1324 spin_unlock(&rc->reloc_root_tree.lock);
1325 if (rb_node)
1326 backref_tree_panic(rb_node, -EEXIST, node->bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001327 return 0;
1328}
1329
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001330static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans,
1331 struct btrfs_root *root, u64 objectid)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001332{
1333 struct btrfs_root *reloc_root;
1334 struct extent_buffer *eb;
1335 struct btrfs_root_item *root_item;
1336 struct btrfs_key root_key;
Miao Xie5bc72472013-06-06 03:28:03 +00001337 u64 last_snap = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001338 int ret;
1339
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001340 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
1341 BUG_ON(!root_item);
1342
1343 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
1344 root_key.type = BTRFS_ROOT_ITEM_KEY;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001345 root_key.offset = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001346
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001347 if (root->root_key.objectid == objectid) {
1348 /* called by btrfs_init_reloc_root */
1349 ret = btrfs_copy_root(trans, root, root->commit_root, &eb,
1350 BTRFS_TREE_RELOC_OBJECTID);
1351 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001352
Miao Xie5bc72472013-06-06 03:28:03 +00001353 last_snap = btrfs_root_last_snapshot(&root->root_item);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001354 btrfs_set_root_last_snapshot(&root->root_item,
1355 trans->transid - 1);
1356 } else {
1357 /*
1358 * called by btrfs_reloc_post_snapshot_hook.
1359 * the source tree is a reloc tree, all tree blocks
1360 * modified after it was created have RELOC flag
1361 * set in their headers. so it's OK to not update
1362 * the 'last_snapshot'.
1363 */
1364 ret = btrfs_copy_root(trans, root, root->node, &eb,
1365 BTRFS_TREE_RELOC_OBJECTID);
1366 BUG_ON(ret);
1367 }
1368
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001369 memcpy(root_item, &root->root_item, sizeof(*root_item));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001370 btrfs_set_root_bytenr(root_item, eb->start);
1371 btrfs_set_root_level(root_item, btrfs_header_level(eb));
1372 btrfs_set_root_generation(root_item, trans->transid);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001373
1374 if (root->root_key.objectid == objectid) {
1375 btrfs_set_root_refs(root_item, 0);
1376 memset(&root_item->drop_progress, 0,
1377 sizeof(struct btrfs_disk_key));
1378 root_item->drop_level = 0;
Miao Xie5bc72472013-06-06 03:28:03 +00001379 /*
1380 * abuse rtransid, it is safe because it is impossible to
1381 * receive data into a relocation tree.
1382 */
1383 btrfs_set_root_rtransid(root_item, last_snap);
1384 btrfs_set_root_otransid(root_item, trans->transid);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001385 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001386
1387 btrfs_tree_unlock(eb);
1388 free_extent_buffer(eb);
1389
1390 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
1391 &root_key, root_item);
1392 BUG_ON(ret);
1393 kfree(root_item);
1394
Miao Xiecb517ea2013-05-15 07:48:19 +00001395 reloc_root = btrfs_read_fs_root(root->fs_info->tree_root, &root_key);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001396 BUG_ON(IS_ERR(reloc_root));
1397 reloc_root->last_trans = trans->transid;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001398 return reloc_root;
1399}
1400
1401/*
1402 * create reloc tree for a given fs tree. reloc tree is just a
1403 * snapshot of the fs tree with special root objectid.
1404 */
1405int btrfs_init_reloc_root(struct btrfs_trans_handle *trans,
1406 struct btrfs_root *root)
1407{
1408 struct btrfs_root *reloc_root;
1409 struct reloc_control *rc = root->fs_info->reloc_ctl;
Miao Xie20dd2cb2013-09-25 21:47:45 +08001410 struct btrfs_block_rsv *rsv;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001411 int clear_rsv = 0;
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001412 int ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001413
1414 if (root->reloc_root) {
1415 reloc_root = root->reloc_root;
1416 reloc_root->last_trans = trans->transid;
1417 return 0;
1418 }
1419
1420 if (!rc || !rc->create_reloc_tree ||
1421 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1422 return 0;
1423
Miao Xie20dd2cb2013-09-25 21:47:45 +08001424 if (!trans->reloc_reserved) {
1425 rsv = trans->block_rsv;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001426 trans->block_rsv = rc->block_rsv;
1427 clear_rsv = 1;
1428 }
1429 reloc_root = create_reloc_root(trans, root, root->root_key.objectid);
1430 if (clear_rsv)
Miao Xie20dd2cb2013-09-25 21:47:45 +08001431 trans->block_rsv = rsv;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001432
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001433 ret = __add_reloc_root(reloc_root);
1434 BUG_ON(ret < 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001435 root->reloc_root = reloc_root;
1436 return 0;
1437}
1438
1439/*
1440 * update root item of reloc tree
1441 */
1442int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
1443 struct btrfs_root *root)
1444{
1445 struct btrfs_root *reloc_root;
1446 struct btrfs_root_item *root_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001447 int ret;
1448
1449 if (!root->reloc_root)
Chris Mason75857172011-06-13 20:00:16 -04001450 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001451
1452 reloc_root = root->reloc_root;
1453 root_item = &reloc_root->root_item;
1454
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001455 if (root->fs_info->reloc_ctl->merge_reloc_tree &&
1456 btrfs_root_refs(root_item) == 0) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001457 root->reloc_root = NULL;
Wang Shilongc974c462013-12-11 19:29:51 +08001458 __del_reloc_root(reloc_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001459 }
1460
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001461 if (reloc_root->commit_root != reloc_root->node) {
1462 btrfs_set_root_node(root_item, reloc_root->node);
1463 free_extent_buffer(reloc_root->commit_root);
1464 reloc_root->commit_root = btrfs_root_node(reloc_root);
1465 }
1466
1467 ret = btrfs_update_root(trans, root->fs_info->tree_root,
1468 &reloc_root->root_key, root_item);
1469 BUG_ON(ret);
Chris Mason75857172011-06-13 20:00:16 -04001470
1471out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001472 return 0;
1473}
1474
1475/*
1476 * helper to find first cached inode with inode number >= objectid
1477 * in a subvolume
1478 */
1479static struct inode *find_next_inode(struct btrfs_root *root, u64 objectid)
1480{
1481 struct rb_node *node;
1482 struct rb_node *prev;
1483 struct btrfs_inode *entry;
1484 struct inode *inode;
1485
1486 spin_lock(&root->inode_lock);
1487again:
1488 node = root->inode_tree.rb_node;
1489 prev = NULL;
1490 while (node) {
1491 prev = node;
1492 entry = rb_entry(node, struct btrfs_inode, rb_node);
1493
Li Zefan33345d012011-04-20 10:31:50 +08001494 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001495 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08001496 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001497 node = node->rb_right;
1498 else
1499 break;
1500 }
1501 if (!node) {
1502 while (prev) {
1503 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08001504 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001505 node = prev;
1506 break;
1507 }
1508 prev = rb_next(prev);
1509 }
1510 }
1511 while (node) {
1512 entry = rb_entry(node, struct btrfs_inode, rb_node);
1513 inode = igrab(&entry->vfs_inode);
1514 if (inode) {
1515 spin_unlock(&root->inode_lock);
1516 return inode;
1517 }
1518
Li Zefan33345d012011-04-20 10:31:50 +08001519 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001520 if (cond_resched_lock(&root->inode_lock))
1521 goto again;
1522
1523 node = rb_next(node);
1524 }
1525 spin_unlock(&root->inode_lock);
1526 return NULL;
1527}
1528
1529static int in_block_group(u64 bytenr,
1530 struct btrfs_block_group_cache *block_group)
1531{
1532 if (bytenr >= block_group->key.objectid &&
1533 bytenr < block_group->key.objectid + block_group->key.offset)
1534 return 1;
1535 return 0;
1536}
1537
1538/*
1539 * get new location of data
1540 */
1541static int get_new_location(struct inode *reloc_inode, u64 *new_bytenr,
1542 u64 bytenr, u64 num_bytes)
1543{
1544 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
1545 struct btrfs_path *path;
1546 struct btrfs_file_extent_item *fi;
1547 struct extent_buffer *leaf;
1548 int ret;
1549
1550 path = btrfs_alloc_path();
1551 if (!path)
1552 return -ENOMEM;
1553
1554 bytenr -= BTRFS_I(reloc_inode)->index_cnt;
Li Zefan33345d012011-04-20 10:31:50 +08001555 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(reloc_inode),
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001556 bytenr, 0);
1557 if (ret < 0)
1558 goto out;
1559 if (ret > 0) {
1560 ret = -ENOENT;
1561 goto out;
1562 }
1563
1564 leaf = path->nodes[0];
1565 fi = btrfs_item_ptr(leaf, path->slots[0],
1566 struct btrfs_file_extent_item);
1567
1568 BUG_ON(btrfs_file_extent_offset(leaf, fi) ||
1569 btrfs_file_extent_compression(leaf, fi) ||
1570 btrfs_file_extent_encryption(leaf, fi) ||
1571 btrfs_file_extent_other_encoding(leaf, fi));
1572
1573 if (num_bytes != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001574 ret = -EINVAL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001575 goto out;
1576 }
1577
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001578 *new_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001579 ret = 0;
1580out:
1581 btrfs_free_path(path);
1582 return ret;
1583}
1584
1585/*
1586 * update file extent items in the tree leaf to point to
1587 * the new locations.
1588 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001589static noinline_for_stack
1590int replace_file_extents(struct btrfs_trans_handle *trans,
1591 struct reloc_control *rc,
1592 struct btrfs_root *root,
1593 struct extent_buffer *leaf)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001594{
1595 struct btrfs_key key;
1596 struct btrfs_file_extent_item *fi;
1597 struct inode *inode = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001598 u64 parent;
1599 u64 bytenr;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001600 u64 new_bytenr = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001601 u64 num_bytes;
1602 u64 end;
1603 u32 nritems;
1604 u32 i;
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001605 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001606 int first = 1;
1607 int dirty = 0;
1608
1609 if (rc->stage != UPDATE_DATA_PTRS)
1610 return 0;
1611
1612 /* reloc trees always use full backref */
1613 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1614 parent = leaf->start;
1615 else
1616 parent = 0;
1617
1618 nritems = btrfs_header_nritems(leaf);
1619 for (i = 0; i < nritems; i++) {
1620 cond_resched();
1621 btrfs_item_key_to_cpu(leaf, &key, i);
1622 if (key.type != BTRFS_EXTENT_DATA_KEY)
1623 continue;
1624 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1625 if (btrfs_file_extent_type(leaf, fi) ==
1626 BTRFS_FILE_EXTENT_INLINE)
1627 continue;
1628 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1629 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
1630 if (bytenr == 0)
1631 continue;
1632 if (!in_block_group(bytenr, rc->block_group))
1633 continue;
1634
1635 /*
1636 * if we are modifying block in fs tree, wait for readpage
1637 * to complete and drop the extent cache
1638 */
1639 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001640 if (first) {
1641 inode = find_next_inode(root, key.objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001642 first = 0;
Li Zefan33345d012011-04-20 10:31:50 +08001643 } else if (inode && btrfs_ino(inode) < key.objectid) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001644 btrfs_add_delayed_iput(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001645 inode = find_next_inode(root, key.objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001646 }
Li Zefan33345d012011-04-20 10:31:50 +08001647 if (inode && btrfs_ino(inode) == key.objectid) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001648 end = key.offset +
1649 btrfs_file_extent_num_bytes(leaf, fi);
1650 WARN_ON(!IS_ALIGNED(key.offset,
1651 root->sectorsize));
1652 WARN_ON(!IS_ALIGNED(end, root->sectorsize));
1653 end--;
1654 ret = try_lock_extent(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001655 key.offset, end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001656 if (!ret)
1657 continue;
1658
1659 btrfs_drop_extent_cache(inode, key.offset, end,
1660 1);
1661 unlock_extent(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001662 key.offset, end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001663 }
1664 }
1665
1666 ret = get_new_location(rc->data_inode, &new_bytenr,
1667 bytenr, num_bytes);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001668 if (ret) {
1669 /*
1670 * Don't have to abort since we've not changed anything
1671 * in the file extent yet.
1672 */
1673 break;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001674 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001675
1676 btrfs_set_file_extent_disk_bytenr(leaf, fi, new_bytenr);
1677 dirty = 1;
1678
1679 key.offset -= btrfs_file_extent_offset(leaf, fi);
1680 ret = btrfs_inc_extent_ref(trans, root, new_bytenr,
1681 num_bytes, parent,
1682 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001683 key.objectid, key.offset, 1);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001684 if (ret) {
1685 btrfs_abort_transaction(trans, root, ret);
1686 break;
1687 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001688
1689 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
1690 parent, btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001691 key.objectid, key.offset, 1);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001692 if (ret) {
1693 btrfs_abort_transaction(trans, root, ret);
1694 break;
1695 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001696 }
1697 if (dirty)
1698 btrfs_mark_buffer_dirty(leaf);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001699 if (inode)
1700 btrfs_add_delayed_iput(inode);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001701 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001702}
1703
1704static noinline_for_stack
1705int memcmp_node_keys(struct extent_buffer *eb, int slot,
1706 struct btrfs_path *path, int level)
1707{
1708 struct btrfs_disk_key key1;
1709 struct btrfs_disk_key key2;
1710 btrfs_node_key(eb, &key1, slot);
1711 btrfs_node_key(path->nodes[level], &key2, path->slots[level]);
1712 return memcmp(&key1, &key2, sizeof(key1));
1713}
1714
1715/*
1716 * try to replace tree blocks in fs tree with the new blocks
1717 * in reloc tree. tree blocks haven't been modified since the
1718 * reloc tree was create can be replaced.
1719 *
1720 * if a block was replaced, level of the block + 1 is returned.
1721 * if no block got replaced, 0 is returned. if there are other
1722 * errors, a negative error number is returned.
1723 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001724static noinline_for_stack
1725int replace_path(struct btrfs_trans_handle *trans,
1726 struct btrfs_root *dest, struct btrfs_root *src,
1727 struct btrfs_path *path, struct btrfs_key *next_key,
1728 int lowest_level, int max_level)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001729{
1730 struct extent_buffer *eb;
1731 struct extent_buffer *parent;
1732 struct btrfs_key key;
1733 u64 old_bytenr;
1734 u64 new_bytenr;
1735 u64 old_ptr_gen;
1736 u64 new_ptr_gen;
1737 u64 last_snapshot;
1738 u32 blocksize;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001739 int cow = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001740 int level;
1741 int ret;
1742 int slot;
1743
1744 BUG_ON(src->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
1745 BUG_ON(dest->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001746
1747 last_snapshot = btrfs_root_last_snapshot(&src->root_item);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001748again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001749 slot = path->slots[lowest_level];
1750 btrfs_node_key_to_cpu(path->nodes[lowest_level], &key, slot);
1751
1752 eb = btrfs_lock_root_node(dest);
1753 btrfs_set_lock_blocking(eb);
1754 level = btrfs_header_level(eb);
1755
1756 if (level < lowest_level) {
1757 btrfs_tree_unlock(eb);
1758 free_extent_buffer(eb);
1759 return 0;
1760 }
1761
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001762 if (cow) {
1763 ret = btrfs_cow_block(trans, dest, eb, NULL, 0, &eb);
1764 BUG_ON(ret);
1765 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001766 btrfs_set_lock_blocking(eb);
1767
1768 if (next_key) {
1769 next_key->objectid = (u64)-1;
1770 next_key->type = (u8)-1;
1771 next_key->offset = (u64)-1;
1772 }
1773
1774 parent = eb;
1775 while (1) {
1776 level = btrfs_header_level(parent);
1777 BUG_ON(level < lowest_level);
1778
1779 ret = btrfs_bin_search(parent, &key, level, &slot);
1780 if (ret && slot > 0)
1781 slot--;
1782
1783 if (next_key && slot + 1 < btrfs_header_nritems(parent))
1784 btrfs_node_key_to_cpu(parent, next_key, slot + 1);
1785
1786 old_bytenr = btrfs_node_blockptr(parent, slot);
1787 blocksize = btrfs_level_size(dest, level - 1);
1788 old_ptr_gen = btrfs_node_ptr_generation(parent, slot);
1789
1790 if (level <= max_level) {
1791 eb = path->nodes[level];
1792 new_bytenr = btrfs_node_blockptr(eb,
1793 path->slots[level]);
1794 new_ptr_gen = btrfs_node_ptr_generation(eb,
1795 path->slots[level]);
1796 } else {
1797 new_bytenr = 0;
1798 new_ptr_gen = 0;
1799 }
1800
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301801 if (WARN_ON(new_bytenr > 0 && new_bytenr == old_bytenr)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001802 ret = level;
1803 break;
1804 }
1805
1806 if (new_bytenr == 0 || old_ptr_gen > last_snapshot ||
1807 memcmp_node_keys(parent, slot, path, level)) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001808 if (level <= lowest_level) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001809 ret = 0;
1810 break;
1811 }
1812
1813 eb = read_tree_block(dest, old_bytenr, blocksize,
1814 old_ptr_gen);
Josef Bacik416bc652013-04-23 14:17:42 -04001815 if (!eb || !extent_buffer_uptodate(eb)) {
1816 ret = (!eb) ? -ENOMEM : -EIO;
1817 free_extent_buffer(eb);
Stefan Behrens379cde72013-05-08 08:56:09 +00001818 break;
Josef Bacik416bc652013-04-23 14:17:42 -04001819 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001820 btrfs_tree_lock(eb);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001821 if (cow) {
1822 ret = btrfs_cow_block(trans, dest, eb, parent,
1823 slot, &eb);
1824 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001825 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001826 btrfs_set_lock_blocking(eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001827
1828 btrfs_tree_unlock(parent);
1829 free_extent_buffer(parent);
1830
1831 parent = eb;
1832 continue;
1833 }
1834
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001835 if (!cow) {
1836 btrfs_tree_unlock(parent);
1837 free_extent_buffer(parent);
1838 cow = 1;
1839 goto again;
1840 }
1841
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001842 btrfs_node_key_to_cpu(path->nodes[level], &key,
1843 path->slots[level]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001844 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001845
1846 path->lowest_level = level;
1847 ret = btrfs_search_slot(trans, src, &key, path, 0, 1);
1848 path->lowest_level = 0;
1849 BUG_ON(ret);
1850
1851 /*
1852 * swap blocks in fs tree and reloc tree.
1853 */
1854 btrfs_set_node_blockptr(parent, slot, new_bytenr);
1855 btrfs_set_node_ptr_generation(parent, slot, new_ptr_gen);
1856 btrfs_mark_buffer_dirty(parent);
1857
1858 btrfs_set_node_blockptr(path->nodes[level],
1859 path->slots[level], old_bytenr);
1860 btrfs_set_node_ptr_generation(path->nodes[level],
1861 path->slots[level], old_ptr_gen);
1862 btrfs_mark_buffer_dirty(path->nodes[level]);
1863
1864 ret = btrfs_inc_extent_ref(trans, src, old_bytenr, blocksize,
1865 path->nodes[level]->start,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001866 src->root_key.objectid, level - 1, 0,
1867 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001868 BUG_ON(ret);
1869 ret = btrfs_inc_extent_ref(trans, dest, new_bytenr, blocksize,
1870 0, dest->root_key.objectid, level - 1,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001871 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001872 BUG_ON(ret);
1873
1874 ret = btrfs_free_extent(trans, src, new_bytenr, blocksize,
1875 path->nodes[level]->start,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001876 src->root_key.objectid, level - 1, 0,
1877 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001878 BUG_ON(ret);
1879
1880 ret = btrfs_free_extent(trans, dest, old_bytenr, blocksize,
1881 0, dest->root_key.objectid, level - 1,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001882 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001883 BUG_ON(ret);
1884
1885 btrfs_unlock_up_safe(path, 0);
1886
1887 ret = level;
1888 break;
1889 }
1890 btrfs_tree_unlock(parent);
1891 free_extent_buffer(parent);
1892 return ret;
1893}
1894
1895/*
1896 * helper to find next relocated block in reloc tree
1897 */
1898static noinline_for_stack
1899int walk_up_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
1900 int *level)
1901{
1902 struct extent_buffer *eb;
1903 int i;
1904 u64 last_snapshot;
1905 u32 nritems;
1906
1907 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1908
1909 for (i = 0; i < *level; i++) {
1910 free_extent_buffer(path->nodes[i]);
1911 path->nodes[i] = NULL;
1912 }
1913
1914 for (i = *level; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
1915 eb = path->nodes[i];
1916 nritems = btrfs_header_nritems(eb);
1917 while (path->slots[i] + 1 < nritems) {
1918 path->slots[i]++;
1919 if (btrfs_node_ptr_generation(eb, path->slots[i]) <=
1920 last_snapshot)
1921 continue;
1922
1923 *level = i;
1924 return 0;
1925 }
1926 free_extent_buffer(path->nodes[i]);
1927 path->nodes[i] = NULL;
1928 }
1929 return 1;
1930}
1931
1932/*
1933 * walk down reloc tree to find relocated block of lowest level
1934 */
1935static noinline_for_stack
1936int walk_down_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
1937 int *level)
1938{
1939 struct extent_buffer *eb = NULL;
1940 int i;
1941 u64 bytenr;
1942 u64 ptr_gen = 0;
1943 u64 last_snapshot;
1944 u32 blocksize;
1945 u32 nritems;
1946
1947 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1948
1949 for (i = *level; i > 0; i--) {
1950 eb = path->nodes[i];
1951 nritems = btrfs_header_nritems(eb);
1952 while (path->slots[i] < nritems) {
1953 ptr_gen = btrfs_node_ptr_generation(eb, path->slots[i]);
1954 if (ptr_gen > last_snapshot)
1955 break;
1956 path->slots[i]++;
1957 }
1958 if (path->slots[i] >= nritems) {
1959 if (i == *level)
1960 break;
1961 *level = i + 1;
1962 return 0;
1963 }
1964 if (i == 1) {
1965 *level = i;
1966 return 0;
1967 }
1968
1969 bytenr = btrfs_node_blockptr(eb, path->slots[i]);
1970 blocksize = btrfs_level_size(root, i - 1);
1971 eb = read_tree_block(root, bytenr, blocksize, ptr_gen);
Josef Bacik416bc652013-04-23 14:17:42 -04001972 if (!eb || !extent_buffer_uptodate(eb)) {
1973 free_extent_buffer(eb);
1974 return -EIO;
1975 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001976 BUG_ON(btrfs_header_level(eb) != i - 1);
1977 path->nodes[i - 1] = eb;
1978 path->slots[i - 1] = 0;
1979 }
1980 return 1;
1981}
1982
1983/*
1984 * invalidate extent cache for file extents whose key in range of
1985 * [min_key, max_key)
1986 */
1987static int invalidate_extent_cache(struct btrfs_root *root,
1988 struct btrfs_key *min_key,
1989 struct btrfs_key *max_key)
1990{
1991 struct inode *inode = NULL;
1992 u64 objectid;
1993 u64 start, end;
Li Zefan33345d012011-04-20 10:31:50 +08001994 u64 ino;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001995
1996 objectid = min_key->objectid;
1997 while (1) {
1998 cond_resched();
1999 iput(inode);
2000
2001 if (objectid > max_key->objectid)
2002 break;
2003
2004 inode = find_next_inode(root, objectid);
2005 if (!inode)
2006 break;
Li Zefan33345d012011-04-20 10:31:50 +08002007 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002008
Li Zefan33345d012011-04-20 10:31:50 +08002009 if (ino > max_key->objectid) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002010 iput(inode);
2011 break;
2012 }
2013
Li Zefan33345d012011-04-20 10:31:50 +08002014 objectid = ino + 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002015 if (!S_ISREG(inode->i_mode))
2016 continue;
2017
Li Zefan33345d012011-04-20 10:31:50 +08002018 if (unlikely(min_key->objectid == ino)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002019 if (min_key->type > BTRFS_EXTENT_DATA_KEY)
2020 continue;
2021 if (min_key->type < BTRFS_EXTENT_DATA_KEY)
2022 start = 0;
2023 else {
2024 start = min_key->offset;
2025 WARN_ON(!IS_ALIGNED(start, root->sectorsize));
2026 }
2027 } else {
2028 start = 0;
2029 }
2030
Li Zefan33345d012011-04-20 10:31:50 +08002031 if (unlikely(max_key->objectid == ino)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002032 if (max_key->type < BTRFS_EXTENT_DATA_KEY)
2033 continue;
2034 if (max_key->type > BTRFS_EXTENT_DATA_KEY) {
2035 end = (u64)-1;
2036 } else {
2037 if (max_key->offset == 0)
2038 continue;
2039 end = max_key->offset;
2040 WARN_ON(!IS_ALIGNED(end, root->sectorsize));
2041 end--;
2042 }
2043 } else {
2044 end = (u64)-1;
2045 }
2046
2047 /* the lock_extent waits for readpage to complete */
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002048 lock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002049 btrfs_drop_extent_cache(inode, start, end, 1);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002050 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002051 }
2052 return 0;
2053}
2054
2055static int find_next_key(struct btrfs_path *path, int level,
2056 struct btrfs_key *key)
2057
2058{
2059 while (level < BTRFS_MAX_LEVEL) {
2060 if (!path->nodes[level])
2061 break;
2062 if (path->slots[level] + 1 <
2063 btrfs_header_nritems(path->nodes[level])) {
2064 btrfs_node_key_to_cpu(path->nodes[level], key,
2065 path->slots[level] + 1);
2066 return 0;
2067 }
2068 level++;
2069 }
2070 return 1;
2071}
2072
2073/*
2074 * merge the relocated tree blocks in reloc tree with corresponding
2075 * fs tree.
2076 */
2077static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
2078 struct btrfs_root *root)
2079{
2080 LIST_HEAD(inode_list);
2081 struct btrfs_key key;
2082 struct btrfs_key next_key;
Josef Bacik9e6a0c522013-10-31 10:07:19 -04002083 struct btrfs_trans_handle *trans = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002084 struct btrfs_root *reloc_root;
2085 struct btrfs_root_item *root_item;
2086 struct btrfs_path *path;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002087 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002088 int level;
2089 int max_level;
2090 int replaced = 0;
2091 int ret;
2092 int err = 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002093 u32 min_reserved;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002094
2095 path = btrfs_alloc_path();
2096 if (!path)
2097 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04002098 path->reada = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002099
2100 reloc_root = root->reloc_root;
2101 root_item = &reloc_root->root_item;
2102
2103 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2104 level = btrfs_root_level(root_item);
2105 extent_buffer_get(reloc_root->node);
2106 path->nodes[level] = reloc_root->node;
2107 path->slots[level] = 0;
2108 } else {
2109 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2110
2111 level = root_item->drop_level;
2112 BUG_ON(level == 0);
2113 path->lowest_level = level;
2114 ret = btrfs_search_slot(NULL, reloc_root, &key, path, 0, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002115 path->lowest_level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002116 if (ret < 0) {
2117 btrfs_free_path(path);
2118 return ret;
2119 }
2120
2121 btrfs_node_key_to_cpu(path->nodes[level], &next_key,
2122 path->slots[level]);
2123 WARN_ON(memcmp(&key, &next_key, sizeof(key)));
2124
2125 btrfs_unlock_up_safe(path, 0);
2126 }
2127
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002128 min_reserved = root->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002129 memset(&next_key, 0, sizeof(next_key));
2130
2131 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00002132 ret = btrfs_block_rsv_refill(root, rc->block_rsv, min_reserved,
2133 BTRFS_RESERVE_FLUSH_ALL);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002134 if (ret) {
Josef Bacik9e6a0c522013-10-31 10:07:19 -04002135 err = ret;
2136 goto out;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002137 }
Josef Bacik9e6a0c522013-10-31 10:07:19 -04002138 trans = btrfs_start_transaction(root, 0);
2139 if (IS_ERR(trans)) {
2140 err = PTR_ERR(trans);
2141 trans = NULL;
2142 goto out;
2143 }
2144 trans->block_rsv = rc->block_rsv;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002145
2146 replaced = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002147 max_level = level;
2148
2149 ret = walk_down_reloc_tree(reloc_root, path, &level);
2150 if (ret < 0) {
2151 err = ret;
2152 goto out;
2153 }
2154 if (ret > 0)
2155 break;
2156
2157 if (!find_next_key(path, level, &key) &&
2158 btrfs_comp_cpu_keys(&next_key, &key) >= 0) {
2159 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002160 } else {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002161 ret = replace_path(trans, root, reloc_root, path,
2162 &next_key, level, max_level);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002163 }
2164 if (ret < 0) {
2165 err = ret;
2166 goto out;
2167 }
2168
2169 if (ret > 0) {
2170 level = ret;
2171 btrfs_node_key_to_cpu(path->nodes[level], &key,
2172 path->slots[level]);
2173 replaced = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002174 }
2175
2176 ret = walk_up_reloc_tree(reloc_root, path, &level);
2177 if (ret > 0)
2178 break;
2179
2180 BUG_ON(level == 0);
2181 /*
2182 * save the merging progress in the drop_progress.
2183 * this is OK since root refs == 1 in this case.
2184 */
2185 btrfs_node_key(path->nodes[level], &root_item->drop_progress,
2186 path->slots[level]);
2187 root_item->drop_level = level;
2188
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002189 btrfs_end_transaction_throttle(trans, root);
Josef Bacik9e6a0c522013-10-31 10:07:19 -04002190 trans = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002191
Liu Bob53d3f52012-11-14 14:34:34 +00002192 btrfs_btree_balance_dirty(root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002193
2194 if (replaced && rc->stage == UPDATE_DATA_PTRS)
2195 invalidate_extent_cache(root, &key, &next_key);
2196 }
2197
2198 /*
2199 * handle the case only one block in the fs tree need to be
2200 * relocated and the block is tree root.
2201 */
2202 leaf = btrfs_lock_root_node(root);
2203 ret = btrfs_cow_block(trans, root, leaf, NULL, 0, &leaf);
2204 btrfs_tree_unlock(leaf);
2205 free_extent_buffer(leaf);
2206 if (ret < 0)
2207 err = ret;
2208out:
2209 btrfs_free_path(path);
2210
2211 if (err == 0) {
2212 memset(&root_item->drop_progress, 0,
2213 sizeof(root_item->drop_progress));
2214 root_item->drop_level = 0;
2215 btrfs_set_root_refs(root_item, 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002216 btrfs_update_reloc_root(trans, root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002217 }
2218
Josef Bacik9e6a0c522013-10-31 10:07:19 -04002219 if (trans)
2220 btrfs_end_transaction_throttle(trans, root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002221
Liu Bob53d3f52012-11-14 14:34:34 +00002222 btrfs_btree_balance_dirty(root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002223
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002224 if (replaced && rc->stage == UPDATE_DATA_PTRS)
2225 invalidate_extent_cache(root, &key, &next_key);
2226
2227 return err;
2228}
2229
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002230static noinline_for_stack
2231int prepare_to_merge(struct reloc_control *rc, int err)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002232{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002233 struct btrfs_root *root = rc->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002234 struct btrfs_root *reloc_root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002235 struct btrfs_trans_handle *trans;
2236 LIST_HEAD(reloc_roots);
2237 u64 num_bytes = 0;
2238 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002239
Chris Mason75857172011-06-13 20:00:16 -04002240 mutex_lock(&root->fs_info->reloc_mutex);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002241 rc->merging_rsv_size += root->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
2242 rc->merging_rsv_size += rc->nodes_relocated * 2;
Chris Mason75857172011-06-13 20:00:16 -04002243 mutex_unlock(&root->fs_info->reloc_mutex);
2244
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002245again:
2246 if (!err) {
2247 num_bytes = rc->merging_rsv_size;
Miao Xie08e007d2012-10-16 11:33:38 +00002248 ret = btrfs_block_rsv_add(root, rc->block_rsv, num_bytes,
2249 BTRFS_RESERVE_FLUSH_ALL);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002250 if (ret)
2251 err = ret;
2252 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002253
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002254 trans = btrfs_join_transaction(rc->extent_root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00002255 if (IS_ERR(trans)) {
2256 if (!err)
2257 btrfs_block_rsv_release(rc->extent_root,
2258 rc->block_rsv, num_bytes);
2259 return PTR_ERR(trans);
2260 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002261
2262 if (!err) {
2263 if (num_bytes != rc->merging_rsv_size) {
2264 btrfs_end_transaction(trans, rc->extent_root);
2265 btrfs_block_rsv_release(rc->extent_root,
2266 rc->block_rsv, num_bytes);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002267 goto again;
2268 }
2269 }
2270
2271 rc->merge_reloc_tree = 1;
2272
2273 while (!list_empty(&rc->reloc_roots)) {
2274 reloc_root = list_entry(rc->reloc_roots.next,
2275 struct btrfs_root, root_list);
2276 list_del_init(&reloc_root->root_list);
2277
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002278 root = read_fs_root(reloc_root->fs_info,
2279 reloc_root->root_key.offset);
2280 BUG_ON(IS_ERR(root));
2281 BUG_ON(root->reloc_root != reloc_root);
2282
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002283 /*
2284 * set reference count to 1, so btrfs_recover_relocation
2285 * knows it should resumes merging
2286 */
2287 if (!err)
2288 btrfs_set_root_refs(&reloc_root->root_item, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002289 btrfs_update_reloc_root(trans, root);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002290
2291 list_add(&reloc_root->root_list, &reloc_roots);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002292 }
2293
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002294 list_splice(&reloc_roots, &rc->reloc_roots);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002295
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002296 if (!err)
2297 btrfs_commit_transaction(trans, rc->extent_root);
2298 else
2299 btrfs_end_transaction(trans, rc->extent_root);
2300 return err;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002301}
2302
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002303static noinline_for_stack
Liu Boaca1bba2013-03-04 16:25:37 +00002304void free_reloc_roots(struct list_head *list)
2305{
2306 struct btrfs_root *reloc_root;
2307
2308 while (!list_empty(list)) {
2309 reloc_root = list_entry(list->next, struct btrfs_root,
2310 root_list);
Wang Shilongc974c462013-12-11 19:29:51 +08002311 __del_reloc_root(reloc_root);
Liu Boaca1bba2013-03-04 16:25:37 +00002312 free_extent_buffer(reloc_root->node);
2313 free_extent_buffer(reloc_root->commit_root);
2314 kfree(reloc_root);
2315 }
2316}
2317
2318static noinline_for_stack
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002319int merge_reloc_roots(struct reloc_control *rc)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002320{
Miao Xie5bc72472013-06-06 03:28:03 +00002321 struct btrfs_trans_handle *trans;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002322 struct btrfs_root *root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002323 struct btrfs_root *reloc_root;
Miao Xie5bc72472013-06-06 03:28:03 +00002324 u64 last_snap;
2325 u64 otransid;
2326 u64 objectid;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002327 LIST_HEAD(reloc_roots);
2328 int found = 0;
Liu Boaca1bba2013-03-04 16:25:37 +00002329 int ret = 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002330again:
2331 root = rc->extent_root;
Chris Mason75857172011-06-13 20:00:16 -04002332
2333 /*
2334 * this serializes us with btrfs_record_root_in_transaction,
2335 * we have to make sure nobody is in the middle of
2336 * adding their roots to the list while we are
2337 * doing this splice
2338 */
2339 mutex_lock(&root->fs_info->reloc_mutex);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002340 list_splice_init(&rc->reloc_roots, &reloc_roots);
Chris Mason75857172011-06-13 20:00:16 -04002341 mutex_unlock(&root->fs_info->reloc_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002342
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002343 while (!list_empty(&reloc_roots)) {
2344 found = 1;
2345 reloc_root = list_entry(reloc_roots.next,
2346 struct btrfs_root, root_list);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002347
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002348 if (btrfs_root_refs(&reloc_root->root_item) > 0) {
2349 root = read_fs_root(reloc_root->fs_info,
2350 reloc_root->root_key.offset);
2351 BUG_ON(IS_ERR(root));
2352 BUG_ON(root->reloc_root != reloc_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002353
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002354 ret = merge_reloc_root(rc, root);
Josef Bacikb37b39c2013-07-23 16:57:15 -04002355 if (ret) {
Wang Shilongc974c462013-12-11 19:29:51 +08002356 __del_reloc_root(reloc_root);
Josef Bacikb37b39c2013-07-23 16:57:15 -04002357 free_extent_buffer(reloc_root->node);
2358 free_extent_buffer(reloc_root->commit_root);
2359 kfree(reloc_root);
Liu Boaca1bba2013-03-04 16:25:37 +00002360 goto out;
Josef Bacikb37b39c2013-07-23 16:57:15 -04002361 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002362 } else {
2363 list_del_init(&reloc_root->root_list);
2364 }
Miao Xie5bc72472013-06-06 03:28:03 +00002365
2366 /*
2367 * we keep the old last snapshod transid in rtranid when we
2368 * created the relocation tree.
2369 */
2370 last_snap = btrfs_root_rtransid(&reloc_root->root_item);
2371 otransid = btrfs_root_otransid(&reloc_root->root_item);
2372 objectid = reloc_root->root_key.offset;
2373
Jeff Mahoney2c536792011-10-03 23:22:41 -04002374 ret = btrfs_drop_snapshot(reloc_root, rc->block_rsv, 0, 1);
Liu Boaca1bba2013-03-04 16:25:37 +00002375 if (ret < 0) {
2376 if (list_empty(&reloc_root->root_list))
2377 list_add_tail(&reloc_root->root_list,
2378 &reloc_roots);
2379 goto out;
Miao Xie5bc72472013-06-06 03:28:03 +00002380 } else if (!ret) {
2381 /*
2382 * recover the last snapshot tranid to avoid
2383 * the space balance break NOCOW.
2384 */
2385 root = read_fs_root(rc->extent_root->fs_info,
2386 objectid);
2387 if (IS_ERR(root))
2388 continue;
2389
Miao Xie5bc72472013-06-06 03:28:03 +00002390 trans = btrfs_join_transaction(root);
2391 BUG_ON(IS_ERR(trans));
2392
2393 /* Check if the fs/file tree was snapshoted or not. */
2394 if (btrfs_root_last_snapshot(&root->root_item) ==
2395 otransid - 1)
2396 btrfs_set_root_last_snapshot(&root->root_item,
2397 last_snap);
2398
2399 btrfs_end_transaction(trans, root);
Liu Boaca1bba2013-03-04 16:25:37 +00002400 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002401 }
2402
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002403 if (found) {
2404 found = 0;
2405 goto again;
2406 }
Liu Boaca1bba2013-03-04 16:25:37 +00002407out:
2408 if (ret) {
2409 btrfs_std_error(root->fs_info, ret);
2410 if (!list_empty(&reloc_roots))
2411 free_reloc_roots(&reloc_roots);
Wang Shilong467bb1d2013-12-11 19:29:52 +08002412
2413 /* new reloc root may be added */
2414 mutex_lock(&root->fs_info->reloc_mutex);
2415 list_splice_init(&rc->reloc_roots, &reloc_roots);
2416 mutex_unlock(&root->fs_info->reloc_mutex);
2417 if (!list_empty(&reloc_roots))
2418 free_reloc_roots(&reloc_roots);
Liu Boaca1bba2013-03-04 16:25:37 +00002419 }
2420
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002421 BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root));
Liu Boaca1bba2013-03-04 16:25:37 +00002422 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002423}
2424
2425static void free_block_list(struct rb_root *blocks)
2426{
2427 struct tree_block *block;
2428 struct rb_node *rb_node;
2429 while ((rb_node = rb_first(blocks))) {
2430 block = rb_entry(rb_node, struct tree_block, rb_node);
2431 rb_erase(rb_node, blocks);
2432 kfree(block);
2433 }
2434}
2435
2436static int record_reloc_root_in_trans(struct btrfs_trans_handle *trans,
2437 struct btrfs_root *reloc_root)
2438{
2439 struct btrfs_root *root;
2440
2441 if (reloc_root->last_trans == trans->transid)
2442 return 0;
2443
2444 root = read_fs_root(reloc_root->fs_info, reloc_root->root_key.offset);
2445 BUG_ON(IS_ERR(root));
2446 BUG_ON(root->reloc_root != reloc_root);
2447
2448 return btrfs_record_root_in_trans(trans, root);
2449}
2450
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002451static noinline_for_stack
2452struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans,
2453 struct reloc_control *rc,
2454 struct backref_node *node,
2455 struct backref_edge *edges[], int *nr)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002456{
2457 struct backref_node *next;
2458 struct btrfs_root *root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002459 int index = 0;
2460
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002461 next = node;
2462 while (1) {
2463 cond_resched();
2464 next = walk_up_backref(next, edges, &index);
2465 root = next->root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002466 BUG_ON(!root);
2467 BUG_ON(!root->ref_cows);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002468
2469 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
2470 record_reloc_root_in_trans(trans, root);
2471 break;
2472 }
2473
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002474 btrfs_record_root_in_trans(trans, root);
2475 root = root->reloc_root;
2476
2477 if (next->new_bytenr != root->node->start) {
2478 BUG_ON(next->new_bytenr);
2479 BUG_ON(!list_empty(&next->list));
2480 next->new_bytenr = root->node->start;
2481 next->root = root;
2482 list_add_tail(&next->list,
2483 &rc->backref_cache.changed);
2484 __mark_block_processed(rc, next);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002485 break;
2486 }
2487
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002488 WARN_ON(1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002489 root = NULL;
2490 next = walk_down_backref(edges, &index);
2491 if (!next || next->level <= node->level)
2492 break;
2493 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002494 if (!root)
2495 return NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002496
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002497 *nr = index;
2498 next = node;
2499 /* setup backref node path for btrfs_reloc_cow_block */
2500 while (1) {
2501 rc->backref_cache.path[next->level] = next;
2502 if (--index < 0)
2503 break;
2504 next = edges[index]->node[UPPER];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002505 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002506 return root;
2507}
2508
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002509/*
2510 * select a tree root for relocation. return NULL if the block
2511 * is reference counted. we should use do_relocation() in this
2512 * case. return a tree root pointer if the block isn't reference
2513 * counted. return -ENOENT if the block is root of reloc tree.
2514 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002515static noinline_for_stack
2516struct btrfs_root *select_one_root(struct btrfs_trans_handle *trans,
2517 struct backref_node *node)
2518{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002519 struct backref_node *next;
2520 struct btrfs_root *root;
2521 struct btrfs_root *fs_root = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002522 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002523 int index = 0;
2524
2525 next = node;
2526 while (1) {
2527 cond_resched();
2528 next = walk_up_backref(next, edges, &index);
2529 root = next->root;
2530 BUG_ON(!root);
2531
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002532 /* no other choice for non-references counted tree */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002533 if (!root->ref_cows)
2534 return root;
2535
2536 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID)
2537 fs_root = root;
2538
2539 if (next != node)
2540 return NULL;
2541
2542 next = walk_down_backref(edges, &index);
2543 if (!next || next->level <= node->level)
2544 break;
2545 }
2546
2547 if (!fs_root)
2548 return ERR_PTR(-ENOENT);
2549 return fs_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002550}
2551
2552static noinline_for_stack
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002553u64 calcu_metadata_size(struct reloc_control *rc,
2554 struct backref_node *node, int reserve)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002555{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002556 struct backref_node *next = node;
2557 struct backref_edge *edge;
2558 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2559 u64 num_bytes = 0;
2560 int index = 0;
2561
2562 BUG_ON(reserve && node->processed);
2563
2564 while (next) {
2565 cond_resched();
2566 while (1) {
2567 if (next->processed && (reserve || next != node))
2568 break;
2569
2570 num_bytes += btrfs_level_size(rc->extent_root,
2571 next->level);
2572
2573 if (list_empty(&next->upper))
2574 break;
2575
2576 edge = list_entry(next->upper.next,
2577 struct backref_edge, list[LOWER]);
2578 edges[index++] = edge;
2579 next = edge->node[UPPER];
2580 }
2581 next = walk_down_backref(edges, &index);
2582 }
2583 return num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002584}
2585
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002586static int reserve_metadata_space(struct btrfs_trans_handle *trans,
2587 struct reloc_control *rc,
2588 struct backref_node *node)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002589{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002590 struct btrfs_root *root = rc->extent_root;
2591 u64 num_bytes;
2592 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002593
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002594 num_bytes = calcu_metadata_size(rc, node, 1) * 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002595
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002596 trans->block_rsv = rc->block_rsv;
Miao Xie08e007d2012-10-16 11:33:38 +00002597 ret = btrfs_block_rsv_add(root, rc->block_rsv, num_bytes,
2598 BTRFS_RESERVE_FLUSH_ALL);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002599 if (ret) {
2600 if (ret == -EAGAIN)
2601 rc->commit_transaction = 1;
2602 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002603 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002604
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002605 return 0;
2606}
2607
2608static void release_metadata_space(struct reloc_control *rc,
2609 struct backref_node *node)
2610{
2611 u64 num_bytes = calcu_metadata_size(rc, node, 0) * 2;
2612 btrfs_block_rsv_release(rc->extent_root, rc->block_rsv, num_bytes);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002613}
2614
2615/*
2616 * relocate a block tree, and then update pointers in upper level
2617 * blocks that reference the block to point to the new location.
2618 *
2619 * if called by link_to_upper, the block has already been relocated.
2620 * in that case this function just updates pointers.
2621 */
2622static int do_relocation(struct btrfs_trans_handle *trans,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002623 struct reloc_control *rc,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002624 struct backref_node *node,
2625 struct btrfs_key *key,
2626 struct btrfs_path *path, int lowest)
2627{
2628 struct backref_node *upper;
2629 struct backref_edge *edge;
2630 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2631 struct btrfs_root *root;
2632 struct extent_buffer *eb;
2633 u32 blocksize;
2634 u64 bytenr;
2635 u64 generation;
2636 int nr;
2637 int slot;
2638 int ret;
2639 int err = 0;
2640
2641 BUG_ON(lowest && node->eb);
2642
2643 path->lowest_level = node->level + 1;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002644 rc->backref_cache.path[node->level] = node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002645 list_for_each_entry(edge, &node->upper, list[LOWER]) {
2646 cond_resched();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002647
2648 upper = edge->node[UPPER];
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002649 root = select_reloc_root(trans, rc, upper, edges, &nr);
2650 BUG_ON(!root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002651
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002652 if (upper->eb && !upper->locked) {
2653 if (!lowest) {
2654 ret = btrfs_bin_search(upper->eb, key,
2655 upper->level, &slot);
2656 BUG_ON(ret);
2657 bytenr = btrfs_node_blockptr(upper->eb, slot);
2658 if (node->eb->start == bytenr)
2659 goto next;
2660 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002661 drop_node_buffer(upper);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002662 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002663
2664 if (!upper->eb) {
2665 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
2666 if (ret < 0) {
2667 err = ret;
2668 break;
2669 }
2670 BUG_ON(ret > 0);
2671
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002672 if (!upper->eb) {
2673 upper->eb = path->nodes[upper->level];
2674 path->nodes[upper->level] = NULL;
2675 } else {
2676 BUG_ON(upper->eb != path->nodes[upper->level]);
2677 }
2678
2679 upper->locked = 1;
2680 path->locks[upper->level] = 0;
2681
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002682 slot = path->slots[upper->level];
David Sterbab3b4aa72011-04-21 01:20:15 +02002683 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002684 } else {
2685 ret = btrfs_bin_search(upper->eb, key, upper->level,
2686 &slot);
2687 BUG_ON(ret);
2688 }
2689
2690 bytenr = btrfs_node_blockptr(upper->eb, slot);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002691 if (lowest) {
2692 BUG_ON(bytenr != node->bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002693 } else {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002694 if (node->eb->start == bytenr)
2695 goto next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002696 }
2697
2698 blocksize = btrfs_level_size(root, node->level);
2699 generation = btrfs_node_ptr_generation(upper->eb, slot);
2700 eb = read_tree_block(root, bytenr, blocksize, generation);
Josef Bacik416bc652013-04-23 14:17:42 -04002701 if (!eb || !extent_buffer_uptodate(eb)) {
2702 free_extent_buffer(eb);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00002703 err = -EIO;
2704 goto next;
2705 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002706 btrfs_tree_lock(eb);
2707 btrfs_set_lock_blocking(eb);
2708
2709 if (!node->eb) {
2710 ret = btrfs_cow_block(trans, root, eb, upper->eb,
2711 slot, &eb);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002712 btrfs_tree_unlock(eb);
2713 free_extent_buffer(eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002714 if (ret < 0) {
2715 err = ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002716 goto next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002717 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002718 BUG_ON(node->eb != eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002719 } else {
2720 btrfs_set_node_blockptr(upper->eb, slot,
2721 node->eb->start);
2722 btrfs_set_node_ptr_generation(upper->eb, slot,
2723 trans->transid);
2724 btrfs_mark_buffer_dirty(upper->eb);
2725
2726 ret = btrfs_inc_extent_ref(trans, root,
2727 node->eb->start, blocksize,
2728 upper->eb->start,
2729 btrfs_header_owner(upper->eb),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002730 node->level, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002731 BUG_ON(ret);
2732
2733 ret = btrfs_drop_subtree(trans, root, eb, upper->eb);
2734 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002735 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002736next:
2737 if (!upper->pending)
2738 drop_node_buffer(upper);
2739 else
2740 unlock_node_buffer(upper);
2741 if (err)
2742 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002743 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002744
2745 if (!err && node->pending) {
2746 drop_node_buffer(node);
2747 list_move_tail(&node->list, &rc->backref_cache.changed);
2748 node->pending = 0;
2749 }
2750
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002751 path->lowest_level = 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002752 BUG_ON(err == -ENOSPC);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002753 return err;
2754}
2755
2756static int link_to_upper(struct btrfs_trans_handle *trans,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002757 struct reloc_control *rc,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002758 struct backref_node *node,
2759 struct btrfs_path *path)
2760{
2761 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002762
2763 btrfs_node_key_to_cpu(node->eb, &key, 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002764 return do_relocation(trans, rc, node, &key, path, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002765}
2766
2767static int finish_pending_nodes(struct btrfs_trans_handle *trans,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002768 struct reloc_control *rc,
2769 struct btrfs_path *path, int err)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002770{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002771 LIST_HEAD(list);
2772 struct backref_cache *cache = &rc->backref_cache;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002773 struct backref_node *node;
2774 int level;
2775 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002776
2777 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2778 while (!list_empty(&cache->pending[level])) {
2779 node = list_entry(cache->pending[level].next,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002780 struct backref_node, list);
2781 list_move_tail(&node->list, &list);
2782 BUG_ON(!node->pending);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002783
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002784 if (!err) {
2785 ret = link_to_upper(trans, rc, node, path);
2786 if (ret < 0)
2787 err = ret;
2788 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002789 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002790 list_splice_init(&list, &cache->pending[level]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002791 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002792 return err;
2793}
2794
2795static void mark_block_processed(struct reloc_control *rc,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002796 u64 bytenr, u32 blocksize)
2797{
2798 set_extent_bits(&rc->processed_blocks, bytenr, bytenr + blocksize - 1,
2799 EXTENT_DIRTY, GFP_NOFS);
2800}
2801
2802static void __mark_block_processed(struct reloc_control *rc,
2803 struct backref_node *node)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002804{
2805 u32 blocksize;
2806 if (node->level == 0 ||
2807 in_block_group(node->bytenr, rc->block_group)) {
2808 blocksize = btrfs_level_size(rc->extent_root, node->level);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002809 mark_block_processed(rc, node->bytenr, blocksize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002810 }
2811 node->processed = 1;
2812}
2813
2814/*
2815 * mark a block and all blocks directly/indirectly reference the block
2816 * as processed.
2817 */
2818static void update_processed_blocks(struct reloc_control *rc,
2819 struct backref_node *node)
2820{
2821 struct backref_node *next = node;
2822 struct backref_edge *edge;
2823 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2824 int index = 0;
2825
2826 while (next) {
2827 cond_resched();
2828 while (1) {
2829 if (next->processed)
2830 break;
2831
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002832 __mark_block_processed(rc, next);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002833
2834 if (list_empty(&next->upper))
2835 break;
2836
2837 edge = list_entry(next->upper.next,
2838 struct backref_edge, list[LOWER]);
2839 edges[index++] = edge;
2840 next = edge->node[UPPER];
2841 }
2842 next = walk_down_backref(edges, &index);
2843 }
2844}
2845
2846static int tree_block_processed(u64 bytenr, u32 blocksize,
2847 struct reloc_control *rc)
2848{
2849 if (test_range_bit(&rc->processed_blocks, bytenr,
Chris Mason9655d292009-09-02 15:22:30 -04002850 bytenr + blocksize - 1, EXTENT_DIRTY, 1, NULL))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002851 return 1;
2852 return 0;
2853}
2854
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002855static int get_tree_block_key(struct reloc_control *rc,
2856 struct tree_block *block)
2857{
2858 struct extent_buffer *eb;
2859
2860 BUG_ON(block->key_ready);
2861 eb = read_tree_block(rc->extent_root, block->bytenr,
2862 block->key.objectid, block->key.offset);
Josef Bacik416bc652013-04-23 14:17:42 -04002863 if (!eb || !extent_buffer_uptodate(eb)) {
2864 free_extent_buffer(eb);
2865 return -EIO;
2866 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002867 WARN_ON(btrfs_header_level(eb) != block->level);
2868 if (block->level == 0)
2869 btrfs_item_key_to_cpu(eb, &block->key, 0);
2870 else
2871 btrfs_node_key_to_cpu(eb, &block->key, 0);
2872 free_extent_buffer(eb);
2873 block->key_ready = 1;
2874 return 0;
2875}
2876
2877static int reada_tree_block(struct reloc_control *rc,
2878 struct tree_block *block)
2879{
2880 BUG_ON(block->key_ready);
Josef Bacik3173a182013-03-07 14:22:04 -05002881 if (block->key.type == BTRFS_METADATA_ITEM_KEY)
2882 readahead_tree_block(rc->extent_root, block->bytenr,
2883 block->key.objectid,
2884 rc->extent_root->leafsize);
2885 else
2886 readahead_tree_block(rc->extent_root, block->bytenr,
2887 block->key.objectid, block->key.offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002888 return 0;
2889}
2890
2891/*
2892 * helper function to relocate a tree block
2893 */
2894static int relocate_tree_block(struct btrfs_trans_handle *trans,
2895 struct reloc_control *rc,
2896 struct backref_node *node,
2897 struct btrfs_key *key,
2898 struct btrfs_path *path)
2899{
2900 struct btrfs_root *root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002901 int release = 0;
2902 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002903
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002904 if (!node)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002905 return 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002906
2907 BUG_ON(node->processed);
2908 root = select_one_root(trans, node);
2909 if (root == ERR_PTR(-ENOENT)) {
2910 update_processed_blocks(rc, node);
2911 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002912 }
2913
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002914 if (!root || root->ref_cows) {
2915 ret = reserve_metadata_space(trans, rc, node);
2916 if (ret)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002917 goto out;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002918 release = 1;
2919 }
2920
2921 if (root) {
2922 if (root->ref_cows) {
2923 BUG_ON(node->new_bytenr);
2924 BUG_ON(!list_empty(&node->list));
2925 btrfs_record_root_in_trans(trans, root);
2926 root = root->reloc_root;
2927 node->new_bytenr = root->node->start;
2928 node->root = root;
2929 list_add_tail(&node->list, &rc->backref_cache.changed);
2930 } else {
2931 path->lowest_level = node->level;
2932 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
David Sterbab3b4aa72011-04-21 01:20:15 +02002933 btrfs_release_path(path);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002934 if (ret > 0)
2935 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002936 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002937 if (!ret)
2938 update_processed_blocks(rc, node);
2939 } else {
2940 ret = do_relocation(trans, rc, node, key, path, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002941 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002942out:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002943 if (ret || node->level == 0 || node->cowonly) {
2944 if (release)
2945 release_metadata_space(rc, node);
2946 remove_backref_node(&rc->backref_cache, node);
2947 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002948 return ret;
2949}
2950
2951/*
2952 * relocate a list of blocks
2953 */
2954static noinline_for_stack
2955int relocate_tree_blocks(struct btrfs_trans_handle *trans,
2956 struct reloc_control *rc, struct rb_root *blocks)
2957{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002958 struct backref_node *node;
2959 struct btrfs_path *path;
2960 struct tree_block *block;
2961 struct rb_node *rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002962 int ret;
2963 int err = 0;
2964
2965 path = btrfs_alloc_path();
Liu Boe1a12672013-03-04 16:25:38 +00002966 if (!path) {
2967 err = -ENOMEM;
David Sterba34c2b292013-04-26 12:56:04 +00002968 goto out_free_blocks;
Liu Boe1a12672013-03-04 16:25:38 +00002969 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002970
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002971 rb_node = rb_first(blocks);
2972 while (rb_node) {
2973 block = rb_entry(rb_node, struct tree_block, rb_node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002974 if (!block->key_ready)
2975 reada_tree_block(rc, block);
2976 rb_node = rb_next(rb_node);
2977 }
2978
2979 rb_node = rb_first(blocks);
2980 while (rb_node) {
2981 block = rb_entry(rb_node, struct tree_block, rb_node);
David Sterba34c2b292013-04-26 12:56:04 +00002982 if (!block->key_ready) {
2983 err = get_tree_block_key(rc, block);
2984 if (err)
2985 goto out_free_path;
2986 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002987 rb_node = rb_next(rb_node);
2988 }
2989
2990 rb_node = rb_first(blocks);
2991 while (rb_node) {
2992 block = rb_entry(rb_node, struct tree_block, rb_node);
2993
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002994 node = build_backref_tree(rc, &block->key,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002995 block->level, block->bytenr);
2996 if (IS_ERR(node)) {
2997 err = PTR_ERR(node);
2998 goto out;
2999 }
3000
3001 ret = relocate_tree_block(trans, rc, node, &block->key,
3002 path);
3003 if (ret < 0) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003004 if (ret != -EAGAIN || rb_node == rb_first(blocks))
3005 err = ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003006 goto out;
3007 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003008 rb_node = rb_next(rb_node);
3009 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003010out:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003011 err = finish_pending_nodes(trans, rc, path, err);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003012
David Sterba34c2b292013-04-26 12:56:04 +00003013out_free_path:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003014 btrfs_free_path(path);
David Sterba34c2b292013-04-26 12:56:04 +00003015out_free_blocks:
Liu Boe1a12672013-03-04 16:25:38 +00003016 free_block_list(blocks);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003017 return err;
3018}
3019
3020static noinline_for_stack
Yan, Zhengefa56462010-05-16 10:49:59 -04003021int prealloc_file_extent_cluster(struct inode *inode,
3022 struct file_extent_cluster *cluster)
3023{
3024 u64 alloc_hint = 0;
3025 u64 start;
3026 u64 end;
3027 u64 offset = BTRFS_I(inode)->index_cnt;
3028 u64 num_bytes;
3029 int nr = 0;
3030 int ret = 0;
3031
3032 BUG_ON(cluster->start != cluster->boundary[0]);
3033 mutex_lock(&inode->i_mutex);
3034
3035 ret = btrfs_check_data_free_space(inode, cluster->end +
3036 1 - cluster->start);
3037 if (ret)
3038 goto out;
3039
3040 while (nr < cluster->nr) {
3041 start = cluster->boundary[nr] - offset;
3042 if (nr + 1 < cluster->nr)
3043 end = cluster->boundary[nr + 1] - 1 - offset;
3044 else
3045 end = cluster->end - offset;
3046
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003047 lock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan, Zhengefa56462010-05-16 10:49:59 -04003048 num_bytes = end + 1 - start;
3049 ret = btrfs_prealloc_file_range(inode, 0, start,
3050 num_bytes, num_bytes,
3051 end + 1, &alloc_hint);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003052 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan, Zhengefa56462010-05-16 10:49:59 -04003053 if (ret)
3054 break;
3055 nr++;
3056 }
3057 btrfs_free_reserved_data_space(inode, cluster->end +
3058 1 - cluster->start);
3059out:
3060 mutex_unlock(&inode->i_mutex);
3061 return ret;
3062}
3063
3064static noinline_for_stack
Yan, Zheng0257bb82009-09-24 09:17:31 -04003065int setup_extent_mapping(struct inode *inode, u64 start, u64 end,
3066 u64 block_start)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003067{
3068 struct btrfs_root *root = BTRFS_I(inode)->root;
3069 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
3070 struct extent_map *em;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003071 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003072
David Sterba172ddd62011-04-21 00:48:27 +02003073 em = alloc_extent_map();
Yan, Zheng0257bb82009-09-24 09:17:31 -04003074 if (!em)
3075 return -ENOMEM;
3076
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003077 em->start = start;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003078 em->len = end + 1 - start;
3079 em->block_len = em->len;
3080 em->block_start = block_start;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003081 em->bdev = root->fs_info->fs_devices->latest_bdev;
3082 set_bit(EXTENT_FLAG_PINNED, &em->flags);
3083
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003084 lock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003085 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003086 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003087 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04003088 write_unlock(&em_tree->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003089 if (ret != -EEXIST) {
3090 free_extent_map(em);
3091 break;
3092 }
3093 btrfs_drop_extent_cache(inode, start, end, 0);
3094 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003095 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003096 return ret;
3097}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003098
Yan, Zheng0257bb82009-09-24 09:17:31 -04003099static int relocate_file_extent_cluster(struct inode *inode,
3100 struct file_extent_cluster *cluster)
3101{
3102 u64 page_start;
3103 u64 page_end;
3104 u64 offset = BTRFS_I(inode)->index_cnt;
3105 unsigned long index;
3106 unsigned long last_index;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003107 struct page *page;
3108 struct file_ra_state *ra;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003109 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003110 int nr = 0;
3111 int ret = 0;
3112
3113 if (!cluster->nr)
3114 return 0;
3115
3116 ra = kzalloc(sizeof(*ra), GFP_NOFS);
3117 if (!ra)
3118 return -ENOMEM;
3119
Yan, Zhengefa56462010-05-16 10:49:59 -04003120 ret = prealloc_file_extent_cluster(inode, cluster);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003121 if (ret)
Yan, Zhengefa56462010-05-16 10:49:59 -04003122 goto out;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003123
3124 file_ra_state_init(ra, inode->i_mapping);
3125
Yan, Zhengefa56462010-05-16 10:49:59 -04003126 ret = setup_extent_mapping(inode, cluster->start - offset,
3127 cluster->end - offset, cluster->start);
3128 if (ret)
3129 goto out;
3130
3131 index = (cluster->start - offset) >> PAGE_CACHE_SHIFT;
3132 last_index = (cluster->end - offset) >> PAGE_CACHE_SHIFT;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003133 while (index <= last_index) {
Yan, Zhengefa56462010-05-16 10:49:59 -04003134 ret = btrfs_delalloc_reserve_metadata(inode, PAGE_CACHE_SIZE);
3135 if (ret)
3136 goto out;
3137
Yan, Zheng0257bb82009-09-24 09:17:31 -04003138 page = find_lock_page(inode->i_mapping, index);
3139 if (!page) {
3140 page_cache_sync_readahead(inode->i_mapping,
3141 ra, NULL, index,
3142 last_index + 1 - index);
Josef Bacika94733d2011-07-11 10:47:06 -04003143 page = find_or_create_page(inode->i_mapping, index,
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003144 mask);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003145 if (!page) {
Yan, Zhengefa56462010-05-16 10:49:59 -04003146 btrfs_delalloc_release_metadata(inode,
3147 PAGE_CACHE_SIZE);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003148 ret = -ENOMEM;
Yan, Zhengefa56462010-05-16 10:49:59 -04003149 goto out;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003150 }
3151 }
3152
3153 if (PageReadahead(page)) {
3154 page_cache_async_readahead(inode->i_mapping,
3155 ra, NULL, page, index,
3156 last_index + 1 - index);
3157 }
3158
3159 if (!PageUptodate(page)) {
3160 btrfs_readpage(NULL, page);
3161 lock_page(page);
3162 if (!PageUptodate(page)) {
3163 unlock_page(page);
3164 page_cache_release(page);
Yan, Zhengefa56462010-05-16 10:49:59 -04003165 btrfs_delalloc_release_metadata(inode,
3166 PAGE_CACHE_SIZE);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003167 ret = -EIO;
Yan, Zhengefa56462010-05-16 10:49:59 -04003168 goto out;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003169 }
3170 }
3171
Miao Xie4eee4fa2012-12-21 09:17:45 +00003172 page_start = page_offset(page);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003173 page_end = page_start + PAGE_CACHE_SIZE - 1;
3174
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003175 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003176
3177 set_page_extent_mapped(page);
3178
3179 if (nr < cluster->nr &&
3180 page_start + offset == cluster->boundary[nr]) {
3181 set_extent_bits(&BTRFS_I(inode)->io_tree,
3182 page_start, page_end,
3183 EXTENT_BOUNDARY, GFP_NOFS);
3184 nr++;
3185 }
Yan, Zheng0257bb82009-09-24 09:17:31 -04003186
Yan, Zhengefa56462010-05-16 10:49:59 -04003187 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003188 set_page_dirty(page);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003189
3190 unlock_extent(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003191 page_start, page_end);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003192 unlock_page(page);
3193 page_cache_release(page);
3194
3195 index++;
Yan, Zhengefa56462010-05-16 10:49:59 -04003196 balance_dirty_pages_ratelimited(inode->i_mapping);
3197 btrfs_throttle(BTRFS_I(inode)->root);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003198 }
3199 WARN_ON(nr != cluster->nr);
Yan, Zhengefa56462010-05-16 10:49:59 -04003200out:
Yan, Zheng0257bb82009-09-24 09:17:31 -04003201 kfree(ra);
3202 return ret;
3203}
3204
3205static noinline_for_stack
3206int relocate_data_extent(struct inode *inode, struct btrfs_key *extent_key,
3207 struct file_extent_cluster *cluster)
3208{
3209 int ret;
3210
3211 if (cluster->nr > 0 && extent_key->objectid != cluster->end + 1) {
3212 ret = relocate_file_extent_cluster(inode, cluster);
3213 if (ret)
3214 return ret;
3215 cluster->nr = 0;
3216 }
3217
3218 if (!cluster->nr)
3219 cluster->start = extent_key->objectid;
3220 else
3221 BUG_ON(cluster->nr >= MAX_EXTENTS);
3222 cluster->end = extent_key->objectid + extent_key->offset - 1;
3223 cluster->boundary[cluster->nr] = extent_key->objectid;
3224 cluster->nr++;
3225
3226 if (cluster->nr >= MAX_EXTENTS) {
3227 ret = relocate_file_extent_cluster(inode, cluster);
3228 if (ret)
3229 return ret;
3230 cluster->nr = 0;
3231 }
3232 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003233}
3234
3235#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3236static int get_ref_objectid_v0(struct reloc_control *rc,
3237 struct btrfs_path *path,
3238 struct btrfs_key *extent_key,
3239 u64 *ref_objectid, int *path_change)
3240{
3241 struct btrfs_key key;
3242 struct extent_buffer *leaf;
3243 struct btrfs_extent_ref_v0 *ref0;
3244 int ret;
3245 int slot;
3246
3247 leaf = path->nodes[0];
3248 slot = path->slots[0];
3249 while (1) {
3250 if (slot >= btrfs_header_nritems(leaf)) {
3251 ret = btrfs_next_leaf(rc->extent_root, path);
3252 if (ret < 0)
3253 return ret;
3254 BUG_ON(ret > 0);
3255 leaf = path->nodes[0];
3256 slot = path->slots[0];
3257 if (path_change)
3258 *path_change = 1;
3259 }
3260 btrfs_item_key_to_cpu(leaf, &key, slot);
3261 if (key.objectid != extent_key->objectid)
3262 return -ENOENT;
3263
3264 if (key.type != BTRFS_EXTENT_REF_V0_KEY) {
3265 slot++;
3266 continue;
3267 }
3268 ref0 = btrfs_item_ptr(leaf, slot,
3269 struct btrfs_extent_ref_v0);
3270 *ref_objectid = btrfs_ref_objectid_v0(leaf, ref0);
3271 break;
3272 }
3273 return 0;
3274}
3275#endif
3276
3277/*
3278 * helper to add a tree block to the list.
3279 * the major work is getting the generation and level of the block
3280 */
3281static int add_tree_block(struct reloc_control *rc,
3282 struct btrfs_key *extent_key,
3283 struct btrfs_path *path,
3284 struct rb_root *blocks)
3285{
3286 struct extent_buffer *eb;
3287 struct btrfs_extent_item *ei;
3288 struct btrfs_tree_block_info *bi;
3289 struct tree_block *block;
3290 struct rb_node *rb_node;
3291 u32 item_size;
3292 int level = -1;
Wang Shilong7fdf4b62013-10-25 18:52:08 +08003293 u64 generation;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003294
3295 eb = path->nodes[0];
3296 item_size = btrfs_item_size_nr(eb, path->slots[0]);
3297
Josef Bacik3173a182013-03-07 14:22:04 -05003298 if (extent_key->type == BTRFS_METADATA_ITEM_KEY ||
3299 item_size >= sizeof(*ei) + sizeof(*bi)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003300 ei = btrfs_item_ptr(eb, path->slots[0],
3301 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05003302 if (extent_key->type == BTRFS_EXTENT_ITEM_KEY) {
3303 bi = (struct btrfs_tree_block_info *)(ei + 1);
3304 level = btrfs_tree_block_level(eb, bi);
3305 } else {
3306 level = (int)extent_key->offset;
3307 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003308 generation = btrfs_extent_generation(eb, ei);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003309 } else {
3310#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3311 u64 ref_owner;
3312 int ret;
3313
3314 BUG_ON(item_size != sizeof(struct btrfs_extent_item_v0));
3315 ret = get_ref_objectid_v0(rc, path, extent_key,
3316 &ref_owner, NULL);
Andi Kleen411fc6b2010-10-29 15:14:31 -04003317 if (ret < 0)
3318 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003319 BUG_ON(ref_owner >= BTRFS_MAX_LEVEL);
3320 level = (int)ref_owner;
3321 /* FIXME: get real generation */
3322 generation = 0;
3323#else
3324 BUG();
3325#endif
3326 }
3327
David Sterbab3b4aa72011-04-21 01:20:15 +02003328 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003329
3330 BUG_ON(level == -1);
3331
3332 block = kmalloc(sizeof(*block), GFP_NOFS);
3333 if (!block)
3334 return -ENOMEM;
3335
3336 block->bytenr = extent_key->objectid;
Josef Bacik3173a182013-03-07 14:22:04 -05003337 block->key.objectid = rc->extent_root->leafsize;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003338 block->key.offset = generation;
3339 block->level = level;
3340 block->key_ready = 0;
3341
3342 rb_node = tree_insert(blocks, block->bytenr, &block->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -04003343 if (rb_node)
3344 backref_tree_panic(rb_node, -EEXIST, block->bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003345
3346 return 0;
3347}
3348
3349/*
3350 * helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY
3351 */
3352static int __add_tree_block(struct reloc_control *rc,
3353 u64 bytenr, u32 blocksize,
3354 struct rb_root *blocks)
3355{
3356 struct btrfs_path *path;
3357 struct btrfs_key key;
3358 int ret;
Josef Bacikaee68ee2013-06-13 13:50:23 -04003359 bool skinny = btrfs_fs_incompat(rc->extent_root->fs_info,
3360 SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003361
3362 if (tree_block_processed(bytenr, blocksize, rc))
3363 return 0;
3364
3365 if (tree_search(blocks, bytenr))
3366 return 0;
3367
3368 path = btrfs_alloc_path();
3369 if (!path)
3370 return -ENOMEM;
Josef Bacikaee68ee2013-06-13 13:50:23 -04003371again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003372 key.objectid = bytenr;
Josef Bacikaee68ee2013-06-13 13:50:23 -04003373 if (skinny) {
3374 key.type = BTRFS_METADATA_ITEM_KEY;
3375 key.offset = (u64)-1;
3376 } else {
3377 key.type = BTRFS_EXTENT_ITEM_KEY;
3378 key.offset = blocksize;
3379 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003380
3381 path->search_commit_root = 1;
3382 path->skip_locking = 1;
3383 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path, 0, 0);
3384 if (ret < 0)
3385 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003386
Josef Bacikaee68ee2013-06-13 13:50:23 -04003387 if (ret > 0 && skinny) {
3388 if (path->slots[0]) {
3389 path->slots[0]--;
3390 btrfs_item_key_to_cpu(path->nodes[0], &key,
3391 path->slots[0]);
3392 if (key.objectid == bytenr &&
3393 (key.type == BTRFS_METADATA_ITEM_KEY ||
3394 (key.type == BTRFS_EXTENT_ITEM_KEY &&
3395 key.offset == blocksize)))
3396 ret = 0;
3397 }
3398
3399 if (ret) {
3400 skinny = false;
3401 btrfs_release_path(path);
3402 goto again;
3403 }
Josef Bacik3173a182013-03-07 14:22:04 -05003404 }
3405 BUG_ON(ret);
3406
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003407 ret = add_tree_block(rc, &key, path, blocks);
3408out:
3409 btrfs_free_path(path);
3410 return ret;
3411}
3412
3413/*
3414 * helper to check if the block use full backrefs for pointers in it
3415 */
3416static int block_use_full_backref(struct reloc_control *rc,
3417 struct extent_buffer *eb)
3418{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003419 u64 flags;
3420 int ret;
3421
3422 if (btrfs_header_flag(eb, BTRFS_HEADER_FLAG_RELOC) ||
3423 btrfs_header_backref_rev(eb) < BTRFS_MIXED_BACKREF_REV)
3424 return 1;
3425
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003426 ret = btrfs_lookup_extent_info(NULL, rc->extent_root,
Josef Bacik3173a182013-03-07 14:22:04 -05003427 eb->start, btrfs_header_level(eb), 1,
3428 NULL, &flags);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003429 BUG_ON(ret);
3430
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003431 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
3432 ret = 1;
3433 else
3434 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003435 return ret;
3436}
3437
Josef Bacik0af3d002010-06-21 14:48:16 -04003438static int delete_block_group_cache(struct btrfs_fs_info *fs_info,
3439 struct inode *inode, u64 ino)
3440{
3441 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04003442 struct btrfs_root *root = fs_info->tree_root;
3443 struct btrfs_trans_handle *trans;
Josef Bacik0af3d002010-06-21 14:48:16 -04003444 int ret = 0;
3445
3446 if (inode)
3447 goto truncate;
3448
3449 key.objectid = ino;
3450 key.type = BTRFS_INODE_ITEM_KEY;
3451 key.offset = 0;
3452
3453 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
Tsutomu Itohf54fb852012-09-06 03:08:59 -06003454 if (IS_ERR(inode) || is_bad_inode(inode)) {
3455 if (!IS_ERR(inode))
Josef Bacik0af3d002010-06-21 14:48:16 -04003456 iput(inode);
3457 return -ENOENT;
3458 }
3459
3460truncate:
Miao Xie7b61cd92013-05-13 13:55:09 +00003461 ret = btrfs_check_trunc_cache_free_space(root,
3462 &fs_info->global_block_rsv);
3463 if (ret)
3464 goto out;
3465
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003466 trans = btrfs_join_transaction(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04003467 if (IS_ERR(trans)) {
Tsutomu Itoh3612b492011-01-25 02:51:38 +00003468 ret = PTR_ERR(trans);
Josef Bacik0af3d002010-06-21 14:48:16 -04003469 goto out;
3470 }
3471
Filipe David Borba Manana74514322013-09-20 14:46:51 +01003472 ret = btrfs_truncate_free_space_cache(root, trans, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04003473
Josef Bacik0af3d002010-06-21 14:48:16 -04003474 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003475 btrfs_btree_balance_dirty(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04003476out:
3477 iput(inode);
3478 return ret;
3479}
3480
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003481/*
3482 * helper to add tree blocks for backref of type BTRFS_EXTENT_DATA_REF_KEY
3483 * this function scans fs tree to find blocks reference the data extent
3484 */
3485static int find_data_references(struct reloc_control *rc,
3486 struct btrfs_key *extent_key,
3487 struct extent_buffer *leaf,
3488 struct btrfs_extent_data_ref *ref,
3489 struct rb_root *blocks)
3490{
3491 struct btrfs_path *path;
3492 struct tree_block *block;
3493 struct btrfs_root *root;
3494 struct btrfs_file_extent_item *fi;
3495 struct rb_node *rb_node;
3496 struct btrfs_key key;
3497 u64 ref_root;
3498 u64 ref_objectid;
3499 u64 ref_offset;
3500 u32 ref_count;
3501 u32 nritems;
3502 int err = 0;
3503 int added = 0;
3504 int counted;
3505 int ret;
3506
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003507 ref_root = btrfs_extent_data_ref_root(leaf, ref);
3508 ref_objectid = btrfs_extent_data_ref_objectid(leaf, ref);
3509 ref_offset = btrfs_extent_data_ref_offset(leaf, ref);
3510 ref_count = btrfs_extent_data_ref_count(leaf, ref);
3511
Josef Bacik0af3d002010-06-21 14:48:16 -04003512 /*
3513 * This is an extent belonging to the free space cache, lets just delete
3514 * it and redo the search.
3515 */
3516 if (ref_root == BTRFS_ROOT_TREE_OBJECTID) {
3517 ret = delete_block_group_cache(rc->extent_root->fs_info,
3518 NULL, ref_objectid);
3519 if (ret != -ENOENT)
3520 return ret;
3521 ret = 0;
3522 }
3523
3524 path = btrfs_alloc_path();
3525 if (!path)
3526 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04003527 path->reada = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04003528
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003529 root = read_fs_root(rc->extent_root->fs_info, ref_root);
3530 if (IS_ERR(root)) {
3531 err = PTR_ERR(root);
3532 goto out;
3533 }
3534
3535 key.objectid = ref_objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003536 key.type = BTRFS_EXTENT_DATA_KEY;
Yan, Zheng84850e82011-08-29 09:25:53 +08003537 if (ref_offset > ((u64)-1 << 32))
3538 key.offset = 0;
3539 else
3540 key.offset = ref_offset;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003541
3542 path->search_commit_root = 1;
3543 path->skip_locking = 1;
3544 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3545 if (ret < 0) {
3546 err = ret;
3547 goto out;
3548 }
3549
3550 leaf = path->nodes[0];
3551 nritems = btrfs_header_nritems(leaf);
3552 /*
3553 * the references in tree blocks that use full backrefs
3554 * are not counted in
3555 */
3556 if (block_use_full_backref(rc, leaf))
3557 counted = 0;
3558 else
3559 counted = 1;
3560 rb_node = tree_search(blocks, leaf->start);
3561 if (rb_node) {
3562 if (counted)
3563 added = 1;
3564 else
3565 path->slots[0] = nritems;
3566 }
3567
3568 while (ref_count > 0) {
3569 while (path->slots[0] >= nritems) {
3570 ret = btrfs_next_leaf(root, path);
3571 if (ret < 0) {
3572 err = ret;
3573 goto out;
3574 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303575 if (WARN_ON(ret > 0))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003576 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003577
3578 leaf = path->nodes[0];
3579 nritems = btrfs_header_nritems(leaf);
3580 added = 0;
3581
3582 if (block_use_full_backref(rc, leaf))
3583 counted = 0;
3584 else
3585 counted = 1;
3586 rb_node = tree_search(blocks, leaf->start);
3587 if (rb_node) {
3588 if (counted)
3589 added = 1;
3590 else
3591 path->slots[0] = nritems;
3592 }
3593 }
3594
3595 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303596 if (WARN_ON(key.objectid != ref_objectid ||
3597 key.type != BTRFS_EXTENT_DATA_KEY))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003598 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003599
3600 fi = btrfs_item_ptr(leaf, path->slots[0],
3601 struct btrfs_file_extent_item);
3602
3603 if (btrfs_file_extent_type(leaf, fi) ==
3604 BTRFS_FILE_EXTENT_INLINE)
3605 goto next;
3606
3607 if (btrfs_file_extent_disk_bytenr(leaf, fi) !=
3608 extent_key->objectid)
3609 goto next;
3610
3611 key.offset -= btrfs_file_extent_offset(leaf, fi);
3612 if (key.offset != ref_offset)
3613 goto next;
3614
3615 if (counted)
3616 ref_count--;
3617 if (added)
3618 goto next;
3619
3620 if (!tree_block_processed(leaf->start, leaf->len, rc)) {
3621 block = kmalloc(sizeof(*block), GFP_NOFS);
3622 if (!block) {
3623 err = -ENOMEM;
3624 break;
3625 }
3626 block->bytenr = leaf->start;
3627 btrfs_item_key_to_cpu(leaf, &block->key, 0);
3628 block->level = 0;
3629 block->key_ready = 1;
3630 rb_node = tree_insert(blocks, block->bytenr,
3631 &block->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -04003632 if (rb_node)
3633 backref_tree_panic(rb_node, -EEXIST,
3634 block->bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003635 }
3636 if (counted)
3637 added = 1;
3638 else
3639 path->slots[0] = nritems;
3640next:
3641 path->slots[0]++;
3642
3643 }
3644out:
3645 btrfs_free_path(path);
3646 return err;
3647}
3648
3649/*
Liu Bo2c016dc2012-12-26 15:32:17 +08003650 * helper to find all tree blocks that reference a given data extent
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003651 */
3652static noinline_for_stack
3653int add_data_references(struct reloc_control *rc,
3654 struct btrfs_key *extent_key,
3655 struct btrfs_path *path,
3656 struct rb_root *blocks)
3657{
3658 struct btrfs_key key;
3659 struct extent_buffer *eb;
3660 struct btrfs_extent_data_ref *dref;
3661 struct btrfs_extent_inline_ref *iref;
3662 unsigned long ptr;
3663 unsigned long end;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003664 u32 blocksize = btrfs_level_size(rc->extent_root, 0);
Filipe David Borba Manana647f63b2013-07-13 12:25:15 +01003665 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003666 int err = 0;
3667
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003668 eb = path->nodes[0];
3669 ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
3670 end = ptr + btrfs_item_size_nr(eb, path->slots[0]);
3671#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3672 if (ptr + sizeof(struct btrfs_extent_item_v0) == end)
3673 ptr = end;
3674 else
3675#endif
3676 ptr += sizeof(struct btrfs_extent_item);
3677
3678 while (ptr < end) {
3679 iref = (struct btrfs_extent_inline_ref *)ptr;
3680 key.type = btrfs_extent_inline_ref_type(eb, iref);
3681 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
3682 key.offset = btrfs_extent_inline_ref_offset(eb, iref);
3683 ret = __add_tree_block(rc, key.offset, blocksize,
3684 blocks);
3685 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
3686 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
3687 ret = find_data_references(rc, extent_key,
3688 eb, dref, blocks);
3689 } else {
3690 BUG();
3691 }
Filipe David Borba Manana647f63b2013-07-13 12:25:15 +01003692 if (ret) {
3693 err = ret;
3694 goto out;
3695 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003696 ptr += btrfs_extent_inline_ref_size(key.type);
3697 }
3698 WARN_ON(ptr > end);
3699
3700 while (1) {
3701 cond_resched();
3702 eb = path->nodes[0];
3703 if (path->slots[0] >= btrfs_header_nritems(eb)) {
3704 ret = btrfs_next_leaf(rc->extent_root, path);
3705 if (ret < 0) {
3706 err = ret;
3707 break;
3708 }
3709 if (ret > 0)
3710 break;
3711 eb = path->nodes[0];
3712 }
3713
3714 btrfs_item_key_to_cpu(eb, &key, path->slots[0]);
3715 if (key.objectid != extent_key->objectid)
3716 break;
3717
3718#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3719 if (key.type == BTRFS_SHARED_DATA_REF_KEY ||
3720 key.type == BTRFS_EXTENT_REF_V0_KEY) {
3721#else
3722 BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY);
3723 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
3724#endif
3725 ret = __add_tree_block(rc, key.offset, blocksize,
3726 blocks);
3727 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
3728 dref = btrfs_item_ptr(eb, path->slots[0],
3729 struct btrfs_extent_data_ref);
3730 ret = find_data_references(rc, extent_key,
3731 eb, dref, blocks);
3732 } else {
3733 ret = 0;
3734 }
3735 if (ret) {
3736 err = ret;
3737 break;
3738 }
3739 path->slots[0]++;
3740 }
Filipe David Borba Manana647f63b2013-07-13 12:25:15 +01003741out:
David Sterbab3b4aa72011-04-21 01:20:15 +02003742 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003743 if (err)
3744 free_block_list(blocks);
3745 return err;
3746}
3747
3748/*
Liu Bo2c016dc2012-12-26 15:32:17 +08003749 * helper to find next unprocessed extent
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003750 */
3751static noinline_for_stack
3752int find_next_extent(struct btrfs_trans_handle *trans,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003753 struct reloc_control *rc, struct btrfs_path *path,
3754 struct btrfs_key *extent_key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003755{
3756 struct btrfs_key key;
3757 struct extent_buffer *leaf;
3758 u64 start, end, last;
3759 int ret;
3760
3761 last = rc->block_group->key.objectid + rc->block_group->key.offset;
3762 while (1) {
3763 cond_resched();
3764 if (rc->search_start >= last) {
3765 ret = 1;
3766 break;
3767 }
3768
3769 key.objectid = rc->search_start;
3770 key.type = BTRFS_EXTENT_ITEM_KEY;
3771 key.offset = 0;
3772
3773 path->search_commit_root = 1;
3774 path->skip_locking = 1;
3775 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path,
3776 0, 0);
3777 if (ret < 0)
3778 break;
3779next:
3780 leaf = path->nodes[0];
3781 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
3782 ret = btrfs_next_leaf(rc->extent_root, path);
3783 if (ret != 0)
3784 break;
3785 leaf = path->nodes[0];
3786 }
3787
3788 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3789 if (key.objectid >= last) {
3790 ret = 1;
3791 break;
3792 }
3793
Josef Bacik3173a182013-03-07 14:22:04 -05003794 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3795 key.type != BTRFS_METADATA_ITEM_KEY) {
3796 path->slots[0]++;
3797 goto next;
3798 }
3799
3800 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003801 key.objectid + key.offset <= rc->search_start) {
3802 path->slots[0]++;
3803 goto next;
3804 }
3805
Josef Bacik3173a182013-03-07 14:22:04 -05003806 if (key.type == BTRFS_METADATA_ITEM_KEY &&
3807 key.objectid + rc->extent_root->leafsize <=
3808 rc->search_start) {
3809 path->slots[0]++;
3810 goto next;
3811 }
3812
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003813 ret = find_first_extent_bit(&rc->processed_blocks,
3814 key.objectid, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04003815 EXTENT_DIRTY, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003816
3817 if (ret == 0 && start <= key.objectid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003818 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003819 rc->search_start = end + 1;
3820 } else {
Josef Bacik3173a182013-03-07 14:22:04 -05003821 if (key.type == BTRFS_EXTENT_ITEM_KEY)
3822 rc->search_start = key.objectid + key.offset;
3823 else
3824 rc->search_start = key.objectid +
3825 rc->extent_root->leafsize;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003826 memcpy(extent_key, &key, sizeof(key));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003827 return 0;
3828 }
3829 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003830 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003831 return ret;
3832}
3833
3834static void set_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 = rc;
Chris Mason75857172011-06-13 20:00:16 -04003840 mutex_unlock(&fs_info->reloc_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003841}
3842
3843static void unset_reloc_control(struct reloc_control *rc)
3844{
3845 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
Chris Mason75857172011-06-13 20:00:16 -04003846
3847 mutex_lock(&fs_info->reloc_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003848 fs_info->reloc_ctl = NULL;
Chris Mason75857172011-06-13 20:00:16 -04003849 mutex_unlock(&fs_info->reloc_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003850}
3851
3852static int check_extent_flags(u64 flags)
3853{
3854 if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
3855 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
3856 return 1;
3857 if (!(flags & BTRFS_EXTENT_FLAG_DATA) &&
3858 !(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
3859 return 1;
3860 if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
3861 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
3862 return 1;
3863 return 0;
3864}
3865
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003866static noinline_for_stack
3867int prepare_to_relocate(struct reloc_control *rc)
3868{
3869 struct btrfs_trans_handle *trans;
3870 int ret;
3871
Miao Xie66d8f3d2012-09-06 04:02:28 -06003872 rc->block_rsv = btrfs_alloc_block_rsv(rc->extent_root,
3873 BTRFS_BLOCK_RSV_TEMP);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003874 if (!rc->block_rsv)
3875 return -ENOMEM;
3876
3877 /*
3878 * reserve some space for creating reloc trees.
3879 * btrfs_init_reloc_root will use them when there
3880 * is no reservation in transaction handle.
3881 */
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003882 ret = btrfs_block_rsv_add(rc->extent_root, rc->block_rsv,
Miao Xie08e007d2012-10-16 11:33:38 +00003883 rc->extent_root->nodesize * 256,
3884 BTRFS_RESERVE_FLUSH_ALL);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003885 if (ret)
3886 return ret;
3887
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003888 memset(&rc->cluster, 0, sizeof(rc->cluster));
3889 rc->search_start = rc->block_group->key.objectid;
3890 rc->extents_found = 0;
3891 rc->nodes_relocated = 0;
3892 rc->merging_rsv_size = 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003893
3894 rc->create_reloc_tree = 1;
3895 set_reloc_control(rc);
3896
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003897 trans = btrfs_join_transaction(rc->extent_root);
Liu Bo28818942013-03-04 16:25:39 +00003898 if (IS_ERR(trans)) {
3899 unset_reloc_control(rc);
3900 /*
3901 * extent tree is not a ref_cow tree and has no reloc_root to
3902 * cleanup. And callers are responsible to free the above
3903 * block rsv.
3904 */
3905 return PTR_ERR(trans);
3906 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003907 btrfs_commit_transaction(trans, rc->extent_root);
3908 return 0;
3909}
Yan, Zheng76dda932009-09-21 16:00:26 -04003910
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003911static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
3912{
3913 struct rb_root blocks = RB_ROOT;
3914 struct btrfs_key key;
3915 struct btrfs_trans_handle *trans = NULL;
3916 struct btrfs_path *path;
3917 struct btrfs_extent_item *ei;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003918 u64 flags;
3919 u32 item_size;
3920 int ret;
3921 int err = 0;
Chris Masonc87f08c2011-02-16 13:57:04 -05003922 int progress = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003923
3924 path = btrfs_alloc_path();
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003925 if (!path)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003926 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04003927 path->reada = 1;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003928
3929 ret = prepare_to_relocate(rc);
3930 if (ret) {
3931 err = ret;
3932 goto out_free;
Jiri Slaby2423fdf2010-01-06 16:57:22 +00003933 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003934
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003935 while (1) {
Chris Masonc87f08c2011-02-16 13:57:04 -05003936 progress++;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003937 trans = btrfs_start_transaction(rc->extent_root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00003938 if (IS_ERR(trans)) {
3939 err = PTR_ERR(trans);
3940 trans = NULL;
3941 break;
3942 }
Chris Masonc87f08c2011-02-16 13:57:04 -05003943restart:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003944 if (update_backref_cache(trans, &rc->backref_cache)) {
3945 btrfs_end_transaction(trans, rc->extent_root);
3946 continue;
3947 }
3948
3949 ret = find_next_extent(trans, rc, path, &key);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003950 if (ret < 0)
3951 err = ret;
3952 if (ret != 0)
3953 break;
3954
3955 rc->extents_found++;
3956
3957 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3958 struct btrfs_extent_item);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003959 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003960 if (item_size >= sizeof(*ei)) {
3961 flags = btrfs_extent_flags(path->nodes[0], ei);
3962 ret = check_extent_flags(flags);
3963 BUG_ON(ret);
3964
3965 } else {
3966#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3967 u64 ref_owner;
3968 int path_change = 0;
3969
3970 BUG_ON(item_size !=
3971 sizeof(struct btrfs_extent_item_v0));
3972 ret = get_ref_objectid_v0(rc, path, &key, &ref_owner,
3973 &path_change);
3974 if (ref_owner < BTRFS_FIRST_FREE_OBJECTID)
3975 flags = BTRFS_EXTENT_FLAG_TREE_BLOCK;
3976 else
3977 flags = BTRFS_EXTENT_FLAG_DATA;
3978
3979 if (path_change) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003980 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003981
3982 path->search_commit_root = 1;
3983 path->skip_locking = 1;
3984 ret = btrfs_search_slot(NULL, rc->extent_root,
3985 &key, path, 0, 0);
3986 if (ret < 0) {
3987 err = ret;
3988 break;
3989 }
3990 BUG_ON(ret > 0);
3991 }
3992#else
3993 BUG();
3994#endif
3995 }
3996
3997 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
3998 ret = add_tree_block(rc, &key, path, &blocks);
3999 } else if (rc->stage == UPDATE_DATA_PTRS &&
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004000 (flags & BTRFS_EXTENT_FLAG_DATA)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004001 ret = add_data_references(rc, &key, path, &blocks);
4002 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +02004003 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004004 ret = 0;
4005 }
4006 if (ret < 0) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004007 err = ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004008 break;
4009 }
4010
4011 if (!RB_EMPTY_ROOT(&blocks)) {
4012 ret = relocate_tree_blocks(trans, rc, &blocks);
4013 if (ret < 0) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004014 if (ret != -EAGAIN) {
4015 err = ret;
4016 break;
4017 }
4018 rc->extents_found--;
4019 rc->search_start = key.objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004020 }
4021 }
4022
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004023 if (rc->commit_transaction) {
4024 rc->commit_transaction = 0;
4025 ret = btrfs_commit_transaction(trans, rc->extent_root);
4026 BUG_ON(ret);
4027 } else {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004028 btrfs_end_transaction_throttle(trans, rc->extent_root);
Liu Bob53d3f52012-11-14 14:34:34 +00004029 btrfs_btree_balance_dirty(rc->extent_root);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004030 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004031 trans = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004032
4033 if (rc->stage == MOVE_DATA_EXTENTS &&
4034 (flags & BTRFS_EXTENT_FLAG_DATA)) {
4035 rc->found_file_extent = 1;
Yan, Zheng0257bb82009-09-24 09:17:31 -04004036 ret = relocate_data_extent(rc->data_inode,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004037 &key, &rc->cluster);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004038 if (ret < 0) {
4039 err = ret;
4040 break;
4041 }
4042 }
4043 }
Chris Masonc87f08c2011-02-16 13:57:04 -05004044 if (trans && progress && err == -ENOSPC) {
4045 ret = btrfs_force_chunk_alloc(trans, rc->extent_root,
4046 rc->block_group->flags);
4047 if (ret == 0) {
4048 err = 0;
4049 progress = 0;
4050 goto restart;
4051 }
4052 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004053
David Sterbab3b4aa72011-04-21 01:20:15 +02004054 btrfs_release_path(path);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004055 clear_extent_bits(&rc->processed_blocks, 0, (u64)-1, EXTENT_DIRTY,
4056 GFP_NOFS);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004057
4058 if (trans) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004059 btrfs_end_transaction_throttle(trans, rc->extent_root);
Liu Bob53d3f52012-11-14 14:34:34 +00004060 btrfs_btree_balance_dirty(rc->extent_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004061 }
4062
Yan, Zheng0257bb82009-09-24 09:17:31 -04004063 if (!err) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004064 ret = relocate_file_extent_cluster(rc->data_inode,
4065 &rc->cluster);
Yan, Zheng0257bb82009-09-24 09:17:31 -04004066 if (ret < 0)
4067 err = ret;
4068 }
4069
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004070 rc->create_reloc_tree = 0;
4071 set_reloc_control(rc);
Yan, Zheng0257bb82009-09-24 09:17:31 -04004072
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004073 backref_cache_cleanup(&rc->backref_cache);
4074 btrfs_block_rsv_release(rc->extent_root, rc->block_rsv, (u64)-1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004075
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004076 err = prepare_to_merge(rc, err);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004077
4078 merge_reloc_roots(rc);
4079
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004080 rc->merge_reloc_tree = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004081 unset_reloc_control(rc);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004082 btrfs_block_rsv_release(rc->extent_root, rc->block_rsv, (u64)-1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004083
4084 /* get rid of pinned extents */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004085 trans = btrfs_join_transaction(rc->extent_root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004086 if (IS_ERR(trans))
4087 err = PTR_ERR(trans);
4088 else
4089 btrfs_commit_transaction(trans, rc->extent_root);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004090out_free:
4091 btrfs_free_block_rsv(rc->extent_root, rc->block_rsv);
4092 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004093 return err;
4094}
4095
4096static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
Yan, Zheng0257bb82009-09-24 09:17:31 -04004097 struct btrfs_root *root, u64 objectid)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004098{
4099 struct btrfs_path *path;
4100 struct btrfs_inode_item *item;
4101 struct extent_buffer *leaf;
4102 int ret;
4103
4104 path = btrfs_alloc_path();
4105 if (!path)
4106 return -ENOMEM;
4107
4108 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
4109 if (ret)
4110 goto out;
4111
4112 leaf = path->nodes[0];
4113 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
4114 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
4115 btrfs_set_inode_generation(leaf, item, 1);
Yan, Zheng0257bb82009-09-24 09:17:31 -04004116 btrfs_set_inode_size(leaf, item, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004117 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004118 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS |
4119 BTRFS_INODE_PREALLOC);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004120 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02004121 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004122out:
4123 btrfs_free_path(path);
4124 return ret;
4125}
4126
4127/*
4128 * helper to create inode for data relocation.
4129 * the inode is in data relocation tree and its link count is 0
4130 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004131static noinline_for_stack
4132struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
4133 struct btrfs_block_group_cache *group)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004134{
4135 struct inode *inode = NULL;
4136 struct btrfs_trans_handle *trans;
4137 struct btrfs_root *root;
4138 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004139 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
4140 int err = 0;
4141
4142 root = read_fs_root(fs_info, BTRFS_DATA_RELOC_TREE_OBJECTID);
4143 if (IS_ERR(root))
4144 return ERR_CAST(root);
4145
Yan, Zhenga22285a2010-05-16 10:48:46 -04004146 trans = btrfs_start_transaction(root, 6);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004147 if (IS_ERR(trans))
4148 return ERR_CAST(trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004149
Li Zefan581bb052011-04-20 10:06:11 +08004150 err = btrfs_find_free_objectid(root, &objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004151 if (err)
4152 goto out;
4153
Yan, Zheng0257bb82009-09-24 09:17:31 -04004154 err = __insert_orphan_inode(trans, root, objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004155 BUG_ON(err);
4156
4157 key.objectid = objectid;
4158 key.type = BTRFS_INODE_ITEM_KEY;
4159 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00004160 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004161 BUG_ON(IS_ERR(inode) || is_bad_inode(inode));
4162 BTRFS_I(inode)->index_cnt = group->key.objectid;
4163
4164 err = btrfs_orphan_add(trans, inode);
4165out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004166 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004167 btrfs_btree_balance_dirty(root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004168 if (err) {
4169 if (inode)
4170 iput(inode);
4171 inode = ERR_PTR(err);
4172 }
4173 return inode;
4174}
4175
Josef Bacika9995ee2013-05-31 13:04:36 -04004176static struct reloc_control *alloc_reloc_control(struct btrfs_fs_info *fs_info)
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004177{
4178 struct reloc_control *rc;
4179
4180 rc = kzalloc(sizeof(*rc), GFP_NOFS);
4181 if (!rc)
4182 return NULL;
4183
4184 INIT_LIST_HEAD(&rc->reloc_roots);
4185 backref_cache_init(&rc->backref_cache);
4186 mapping_tree_init(&rc->reloc_root_tree);
Josef Bacika9995ee2013-05-31 13:04:36 -04004187 extent_io_tree_init(&rc->processed_blocks,
4188 fs_info->btree_inode->i_mapping);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004189 return rc;
4190}
4191
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004192/*
4193 * function to relocate all extents in a block group.
4194 */
4195int btrfs_relocate_block_group(struct btrfs_root *extent_root, u64 group_start)
4196{
4197 struct btrfs_fs_info *fs_info = extent_root->fs_info;
4198 struct reloc_control *rc;
Josef Bacik0af3d002010-06-21 14:48:16 -04004199 struct inode *inode;
4200 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004201 int ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004202 int rw = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004203 int err = 0;
4204
Josef Bacika9995ee2013-05-31 13:04:36 -04004205 rc = alloc_reloc_control(fs_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004206 if (!rc)
4207 return -ENOMEM;
4208
Yan, Zhengf0486c62010-05-16 10:46:25 -04004209 rc->extent_root = extent_root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004210
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004211 rc->block_group = btrfs_lookup_block_group(fs_info, group_start);
4212 BUG_ON(!rc->block_group);
4213
Yan, Zhengf0486c62010-05-16 10:46:25 -04004214 if (!rc->block_group->ro) {
4215 ret = btrfs_set_block_group_ro(extent_root, rc->block_group);
4216 if (ret) {
4217 err = ret;
4218 goto out;
4219 }
4220 rw = 1;
4221 }
4222
Josef Bacik0af3d002010-06-21 14:48:16 -04004223 path = btrfs_alloc_path();
4224 if (!path) {
4225 err = -ENOMEM;
4226 goto out;
4227 }
4228
4229 inode = lookup_free_space_inode(fs_info->tree_root, rc->block_group,
4230 path);
4231 btrfs_free_path(path);
4232
4233 if (!IS_ERR(inode))
4234 ret = delete_block_group_cache(fs_info, inode, 0);
4235 else
4236 ret = PTR_ERR(inode);
4237
4238 if (ret && ret != -ENOENT) {
4239 err = ret;
4240 goto out;
4241 }
4242
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004243 rc->data_inode = create_reloc_inode(fs_info, rc->block_group);
4244 if (IS_ERR(rc->data_inode)) {
4245 err = PTR_ERR(rc->data_inode);
4246 rc->data_inode = NULL;
4247 goto out;
4248 }
4249
4250 printk(KERN_INFO "btrfs: relocating block group %llu flags %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004251 rc->block_group->key.objectid, rc->block_group->flags);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004252
Miao Xie91aef862013-11-04 23:13:26 +08004253 ret = btrfs_start_delalloc_roots(fs_info, 0);
Miao Xie8ccf6f192012-10-25 09:28:04 +00004254 if (ret < 0) {
4255 err = ret;
4256 goto out;
4257 }
Miao Xieb0244192013-11-04 23:13:25 +08004258 btrfs_wait_ordered_roots(fs_info, -1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004259
4260 while (1) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004261 mutex_lock(&fs_info->cleaner_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004262 ret = relocate_block_group(rc);
Yan, Zheng76dda932009-09-21 16:00:26 -04004263 mutex_unlock(&fs_info->cleaner_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004264 if (ret < 0) {
4265 err = ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004266 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004267 }
4268
4269 if (rc->extents_found == 0)
4270 break;
4271
4272 printk(KERN_INFO "btrfs: found %llu extents\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004273 rc->extents_found);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004274
4275 if (rc->stage == MOVE_DATA_EXTENTS && rc->found_file_extent) {
Josef Bacik0ef8b722013-10-25 16:13:35 -04004276 ret = btrfs_wait_ordered_range(rc->data_inode, 0,
4277 (u64)-1);
4278 if (ret) {
4279 err = ret;
4280 goto out;
4281 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004282 invalidate_mapping_pages(rc->data_inode->i_mapping,
4283 0, -1);
4284 rc->stage = UPDATE_DATA_PTRS;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004285 }
4286 }
4287
Yan, Zheng0257bb82009-09-24 09:17:31 -04004288 filemap_write_and_wait_range(fs_info->btree_inode->i_mapping,
4289 rc->block_group->key.objectid,
4290 rc->block_group->key.objectid +
4291 rc->block_group->key.offset - 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004292
4293 WARN_ON(rc->block_group->pinned > 0);
4294 WARN_ON(rc->block_group->reserved > 0);
4295 WARN_ON(btrfs_block_group_used(&rc->block_group->item) > 0);
4296out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04004297 if (err && rw)
4298 btrfs_set_block_group_rw(extent_root, rc->block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004299 iput(rc->data_inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004300 btrfs_put_block_group(rc->block_group);
4301 kfree(rc);
4302 return err;
4303}
4304
Yan, Zheng76dda932009-09-21 16:00:26 -04004305static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)
4306{
4307 struct btrfs_trans_handle *trans;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004308 int ret, err;
Yan, Zheng76dda932009-09-21 16:00:26 -04004309
Yan, Zhenga22285a2010-05-16 10:48:46 -04004310 trans = btrfs_start_transaction(root->fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004311 if (IS_ERR(trans))
4312 return PTR_ERR(trans);
Yan, Zheng76dda932009-09-21 16:00:26 -04004313
4314 memset(&root->root_item.drop_progress, 0,
4315 sizeof(root->root_item.drop_progress));
4316 root->root_item.drop_level = 0;
4317 btrfs_set_root_refs(&root->root_item, 0);
4318 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4319 &root->root_key, &root->root_item);
Yan, Zheng76dda932009-09-21 16:00:26 -04004320
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004321 err = btrfs_end_transaction(trans, root->fs_info->tree_root);
4322 if (err)
4323 return err;
4324 return ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04004325}
4326
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004327/*
4328 * recover relocation interrupted by system crash.
4329 *
4330 * this function resumes merging reloc trees with corresponding fs trees.
4331 * this is important for keeping the sharing of tree blocks
4332 */
4333int btrfs_recover_relocation(struct btrfs_root *root)
4334{
4335 LIST_HEAD(reloc_roots);
4336 struct btrfs_key key;
4337 struct btrfs_root *fs_root;
4338 struct btrfs_root *reloc_root;
4339 struct btrfs_path *path;
4340 struct extent_buffer *leaf;
4341 struct reloc_control *rc = NULL;
4342 struct btrfs_trans_handle *trans;
4343 int ret;
4344 int err = 0;
4345
4346 path = btrfs_alloc_path();
4347 if (!path)
4348 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04004349 path->reada = -1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004350
4351 key.objectid = BTRFS_TREE_RELOC_OBJECTID;
4352 key.type = BTRFS_ROOT_ITEM_KEY;
4353 key.offset = (u64)-1;
4354
4355 while (1) {
4356 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key,
4357 path, 0, 0);
4358 if (ret < 0) {
4359 err = ret;
4360 goto out;
4361 }
4362 if (ret > 0) {
4363 if (path->slots[0] == 0)
4364 break;
4365 path->slots[0]--;
4366 }
4367 leaf = path->nodes[0];
4368 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004369 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004370
4371 if (key.objectid != BTRFS_TREE_RELOC_OBJECTID ||
4372 key.type != BTRFS_ROOT_ITEM_KEY)
4373 break;
4374
Miao Xiecb517ea2013-05-15 07:48:19 +00004375 reloc_root = btrfs_read_fs_root(root, &key);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004376 if (IS_ERR(reloc_root)) {
4377 err = PTR_ERR(reloc_root);
4378 goto out;
4379 }
4380
4381 list_add(&reloc_root->root_list, &reloc_roots);
4382
4383 if (btrfs_root_refs(&reloc_root->root_item) > 0) {
4384 fs_root = read_fs_root(root->fs_info,
4385 reloc_root->root_key.offset);
4386 if (IS_ERR(fs_root)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004387 ret = PTR_ERR(fs_root);
4388 if (ret != -ENOENT) {
4389 err = ret;
4390 goto out;
4391 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004392 ret = mark_garbage_root(reloc_root);
4393 if (ret < 0) {
4394 err = ret;
4395 goto out;
4396 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004397 }
4398 }
4399
4400 if (key.offset == 0)
4401 break;
4402
4403 key.offset--;
4404 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004405 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004406
4407 if (list_empty(&reloc_roots))
4408 goto out;
4409
Josef Bacika9995ee2013-05-31 13:04:36 -04004410 rc = alloc_reloc_control(root->fs_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004411 if (!rc) {
4412 err = -ENOMEM;
4413 goto out;
4414 }
4415
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004416 rc->extent_root = root->fs_info->extent_root;
4417
4418 set_reloc_control(rc);
4419
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004420 trans = btrfs_join_transaction(rc->extent_root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004421 if (IS_ERR(trans)) {
4422 unset_reloc_control(rc);
4423 err = PTR_ERR(trans);
4424 goto out_free;
4425 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004426
4427 rc->merge_reloc_tree = 1;
4428
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004429 while (!list_empty(&reloc_roots)) {
4430 reloc_root = list_entry(reloc_roots.next,
4431 struct btrfs_root, root_list);
4432 list_del(&reloc_root->root_list);
4433
4434 if (btrfs_root_refs(&reloc_root->root_item) == 0) {
4435 list_add_tail(&reloc_root->root_list,
4436 &rc->reloc_roots);
4437 continue;
4438 }
4439
4440 fs_root = read_fs_root(root->fs_info,
4441 reloc_root->root_key.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004442 if (IS_ERR(fs_root)) {
4443 err = PTR_ERR(fs_root);
4444 goto out_free;
4445 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004446
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04004447 err = __add_reloc_root(reloc_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004448 BUG_ON(err < 0); /* -ENOMEM or logic error */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004449 fs_root->reloc_root = reloc_root;
4450 }
4451
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004452 err = btrfs_commit_transaction(trans, rc->extent_root);
4453 if (err)
4454 goto out_free;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004455
4456 merge_reloc_roots(rc);
4457
4458 unset_reloc_control(rc);
4459
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004460 trans = btrfs_join_transaction(rc->extent_root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004461 if (IS_ERR(trans))
4462 err = PTR_ERR(trans);
4463 else
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004464 err = btrfs_commit_transaction(trans, rc->extent_root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004465out_free:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004466 kfree(rc);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004467out:
Liu Boaca1bba2013-03-04 16:25:37 +00004468 if (!list_empty(&reloc_roots))
4469 free_reloc_roots(&reloc_roots);
4470
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004471 btrfs_free_path(path);
4472
4473 if (err == 0) {
4474 /* cleanup orphan inode in data relocation tree */
4475 fs_root = read_fs_root(root->fs_info,
4476 BTRFS_DATA_RELOC_TREE_OBJECTID);
4477 if (IS_ERR(fs_root))
4478 err = PTR_ERR(fs_root);
Miao Xied7ce5842010-02-02 08:46:44 +00004479 else
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004480 err = btrfs_orphan_cleanup(fs_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004481 }
4482 return err;
4483}
4484
4485/*
4486 * helper to add ordered checksum for data relocation.
4487 *
4488 * cloning checksum properly handles the nodatasum extents.
4489 * it also saves CPU time to re-calculate the checksum.
4490 */
4491int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
4492{
4493 struct btrfs_ordered_sum *sums;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004494 struct btrfs_ordered_extent *ordered;
4495 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004496 int ret;
4497 u64 disk_bytenr;
Josef Bacik4577b012013-09-27 09:33:09 -04004498 u64 new_bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004499 LIST_HEAD(list);
4500
4501 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
4502 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
4503
4504 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
4505 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01004506 disk_bytenr + len - 1, &list, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004507 if (ret)
4508 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004509
4510 while (!list_empty(&list)) {
4511 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
4512 list_del_init(&sums->list);
4513
Josef Bacik4577b012013-09-27 09:33:09 -04004514 /*
4515 * We need to offset the new_bytenr based on where the csum is.
4516 * We need to do this because we will read in entire prealloc
4517 * extents but we may have written to say the middle of the
4518 * prealloc extent, so we need to make sure the csum goes with
4519 * the right disk offset.
4520 *
4521 * We can do this because the data reloc inode refers strictly
4522 * to the on disk bytes, so we don't have to worry about
4523 * disk_len vs real len like with real inodes since it's all
4524 * disk length.
4525 */
4526 new_bytenr = ordered->start + (sums->bytenr - disk_bytenr);
4527 sums->bytenr = new_bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004528
4529 btrfs_add_ordered_sum(inode, ordered, sums);
4530 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004531out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004532 btrfs_put_ordered_extent(ordered);
Andi Kleen411fc6b2010-10-29 15:14:31 -04004533 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004534}
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004535
Josef Bacik83d4cfd2013-08-30 15:09:51 -04004536int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans,
4537 struct btrfs_root *root, struct extent_buffer *buf,
4538 struct extent_buffer *cow)
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004539{
4540 struct reloc_control *rc;
4541 struct backref_node *node;
4542 int first_cow = 0;
4543 int level;
Josef Bacik83d4cfd2013-08-30 15:09:51 -04004544 int ret = 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004545
4546 rc = root->fs_info->reloc_ctl;
4547 if (!rc)
Josef Bacik83d4cfd2013-08-30 15:09:51 -04004548 return 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004549
4550 BUG_ON(rc->stage == UPDATE_DATA_PTRS &&
4551 root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID);
4552
Wang Shilongc974c462013-12-11 19:29:51 +08004553 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
4554 if (buf == root->node)
4555 __update_reloc_root(root, cow->start);
4556 }
4557
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004558 level = btrfs_header_level(buf);
4559 if (btrfs_header_generation(buf) <=
4560 btrfs_root_last_snapshot(&root->root_item))
4561 first_cow = 1;
4562
4563 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID &&
4564 rc->create_reloc_tree) {
4565 WARN_ON(!first_cow && level == 0);
4566
4567 node = rc->backref_cache.path[level];
4568 BUG_ON(node->bytenr != buf->start &&
4569 node->new_bytenr != buf->start);
4570
4571 drop_node_buffer(node);
4572 extent_buffer_get(cow);
4573 node->eb = cow;
4574 node->new_bytenr = cow->start;
4575
4576 if (!node->pending) {
4577 list_move_tail(&node->list,
4578 &rc->backref_cache.pending[level]);
4579 node->pending = 1;
4580 }
4581
4582 if (first_cow)
4583 __mark_block_processed(rc, node);
4584
4585 if (first_cow && level > 0)
4586 rc->nodes_relocated += buf->len;
4587 }
4588
Josef Bacik83d4cfd2013-08-30 15:09:51 -04004589 if (level == 0 && first_cow && rc->stage == UPDATE_DATA_PTRS)
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004590 ret = replace_file_extents(trans, rc, root, cow);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04004591 return ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004592}
4593
4594/*
4595 * called before creating snapshot. it calculates metadata reservation
4596 * requried for relocating tree blocks in the snapshot
4597 */
4598void btrfs_reloc_pre_snapshot(struct btrfs_trans_handle *trans,
4599 struct btrfs_pending_snapshot *pending,
4600 u64 *bytes_to_reserve)
4601{
4602 struct btrfs_root *root;
4603 struct reloc_control *rc;
4604
4605 root = pending->root;
4606 if (!root->reloc_root)
4607 return;
4608
4609 rc = root->fs_info->reloc_ctl;
4610 if (!rc->merge_reloc_tree)
4611 return;
4612
4613 root = root->reloc_root;
4614 BUG_ON(btrfs_root_refs(&root->root_item) == 0);
4615 /*
4616 * relocation is in the stage of merging trees. the space
4617 * used by merging a reloc tree is twice the size of
4618 * relocated tree nodes in the worst case. half for cowing
4619 * the reloc tree, half for cowing the fs tree. the space
4620 * used by cowing the reloc tree will be freed after the
4621 * tree is dropped. if we create snapshot, cowing the fs
4622 * tree may use more space than it frees. so we need
4623 * reserve extra space.
4624 */
4625 *bytes_to_reserve += rc->nodes_relocated;
4626}
4627
4628/*
4629 * called after snapshot is created. migrate block reservation
4630 * and create reloc root for the newly created snapshot
4631 */
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004632int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004633 struct btrfs_pending_snapshot *pending)
4634{
4635 struct btrfs_root *root = pending->root;
4636 struct btrfs_root *reloc_root;
4637 struct btrfs_root *new_root;
4638 struct reloc_control *rc;
4639 int ret;
4640
4641 if (!root->reloc_root)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004642 return 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004643
4644 rc = root->fs_info->reloc_ctl;
4645 rc->merging_rsv_size += rc->nodes_relocated;
4646
4647 if (rc->merge_reloc_tree) {
4648 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
4649 rc->block_rsv,
4650 rc->nodes_relocated);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004651 if (ret)
4652 return ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004653 }
4654
4655 new_root = pending->snap;
4656 reloc_root = create_reloc_root(trans, root->reloc_root,
4657 new_root->root_key.objectid);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004658 if (IS_ERR(reloc_root))
4659 return PTR_ERR(reloc_root);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004660
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04004661 ret = __add_reloc_root(reloc_root);
4662 BUG_ON(ret < 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004663 new_root->reloc_root = reloc_root;
4664
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004665 if (rc->create_reloc_tree)
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004666 ret = clone_backref_node(trans, rc, root, reloc_root);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004667 return ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004668}