blob: 5872b41581f42a5ec355f53c37180be4c73df7cd [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"
Yan Zheng5d4f98a2009-06-10 10:45:14 -040033
34/*
35 * backref_node, mapping_node and tree_block start with this
36 */
37struct tree_entry {
38 struct rb_node rb_node;
39 u64 bytenr;
40};
41
42/*
43 * present a tree block in the backref cache
44 */
45struct backref_node {
46 struct rb_node rb_node;
47 u64 bytenr;
Yan, Zheng3fd0a552010-05-16 10:49:59 -040048
49 u64 new_bytenr;
50 /* objectid of tree block owner, can be not uptodate */
Yan Zheng5d4f98a2009-06-10 10:45:14 -040051 u64 owner;
Yan, Zheng3fd0a552010-05-16 10:49:59 -040052 /* link to pending, changed or detached list */
53 struct list_head list;
Yan Zheng5d4f98a2009-06-10 10:45:14 -040054 /* list of upper level blocks reference this block */
55 struct list_head upper;
56 /* list of child blocks in the cache */
57 struct list_head lower;
58 /* NULL if this node is not tree root */
59 struct btrfs_root *root;
60 /* extent buffer got by COW the block */
61 struct extent_buffer *eb;
62 /* level of tree block */
63 unsigned int level:8;
Yan, Zheng3fd0a552010-05-16 10:49:59 -040064 /* is the block in non-reference counted tree */
65 unsigned int cowonly:1;
66 /* 1 if no child node in the cache */
Yan Zheng5d4f98a2009-06-10 10:45:14 -040067 unsigned int lowest:1;
68 /* is the extent buffer locked */
69 unsigned int locked:1;
70 /* has the block been processed */
71 unsigned int processed:1;
72 /* have backrefs of this block been checked */
73 unsigned int checked:1;
Yan, Zheng3fd0a552010-05-16 10:49:59 -040074 /*
75 * 1 if corresponding block has been cowed but some upper
76 * level block pointers may not point to the new location
77 */
78 unsigned int pending:1;
79 /*
80 * 1 if the backref node isn't connected to any other
81 * backref node.
82 */
83 unsigned int detached:1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -040084};
85
86/*
87 * present a block pointer in the backref cache
88 */
89struct backref_edge {
90 struct list_head list[2];
91 struct backref_node *node[2];
Yan Zheng5d4f98a2009-06-10 10:45:14 -040092};
93
94#define LOWER 0
95#define UPPER 1
96
97struct backref_cache {
98 /* red black tree of all backref nodes in the cache */
99 struct rb_root rb_root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400100 /* for passing backref nodes to btrfs_reloc_cow_block */
101 struct backref_node *path[BTRFS_MAX_LEVEL];
102 /*
103 * list of blocks that have been cowed but some block
104 * pointers in upper level blocks may not reflect the
105 * new location
106 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400107 struct list_head pending[BTRFS_MAX_LEVEL];
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400108 /* list of backref nodes with no child node */
109 struct list_head leaves;
110 /* list of blocks that have been cowed in current transaction */
111 struct list_head changed;
112 /* list of detached backref node. */
113 struct list_head detached;
114
115 u64 last_trans;
116
117 int nr_nodes;
118 int nr_edges;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400119};
120
121/*
122 * map address of tree root to tree
123 */
124struct mapping_node {
125 struct rb_node rb_node;
126 u64 bytenr;
127 void *data;
128};
129
130struct mapping_tree {
131 struct rb_root rb_root;
132 spinlock_t lock;
133};
134
135/*
136 * present a tree block to process
137 */
138struct tree_block {
139 struct rb_node rb_node;
140 u64 bytenr;
141 struct btrfs_key key;
142 unsigned int level:8;
143 unsigned int key_ready:1;
144};
145
Yan, Zheng0257bb82009-09-24 09:17:31 -0400146#define MAX_EXTENTS 128
147
148struct file_extent_cluster {
149 u64 start;
150 u64 end;
151 u64 boundary[MAX_EXTENTS];
152 unsigned int nr;
153};
154
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400155struct reloc_control {
156 /* block group to relocate */
157 struct btrfs_block_group_cache *block_group;
158 /* extent tree */
159 struct btrfs_root *extent_root;
160 /* inode for moving data */
161 struct inode *data_inode;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400162
163 struct btrfs_block_rsv *block_rsv;
164
165 struct backref_cache backref_cache;
166
167 struct file_extent_cluster cluster;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400168 /* tree blocks have been processed */
169 struct extent_io_tree processed_blocks;
170 /* map start of tree root to corresponding reloc tree */
171 struct mapping_tree reloc_root_tree;
172 /* list of reloc trees */
173 struct list_head reloc_roots;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400174 /* size of metadata reservation for merging reloc trees */
175 u64 merging_rsv_size;
176 /* size of relocated tree nodes */
177 u64 nodes_relocated;
178
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400179 u64 search_start;
180 u64 extents_found;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400181
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400182 unsigned int stage:8;
183 unsigned int create_reloc_tree:1;
184 unsigned int merge_reloc_tree:1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400185 unsigned int found_file_extent:1;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400186 unsigned int commit_transaction:1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400187};
188
189/* stages of data relocation */
190#define MOVE_DATA_EXTENTS 0
191#define UPDATE_DATA_PTRS 1
192
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400193static void remove_backref_node(struct backref_cache *cache,
194 struct backref_node *node);
195static void __mark_block_processed(struct reloc_control *rc,
196 struct backref_node *node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400197
198static void mapping_tree_init(struct mapping_tree *tree)
199{
Eric Paris6bef4d32010-02-23 19:43:04 +0000200 tree->rb_root = RB_ROOT;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400201 spin_lock_init(&tree->lock);
202}
203
204static void backref_cache_init(struct backref_cache *cache)
205{
206 int i;
Eric Paris6bef4d32010-02-23 19:43:04 +0000207 cache->rb_root = RB_ROOT;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400208 for (i = 0; i < BTRFS_MAX_LEVEL; i++)
209 INIT_LIST_HEAD(&cache->pending[i]);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400210 INIT_LIST_HEAD(&cache->changed);
211 INIT_LIST_HEAD(&cache->detached);
212 INIT_LIST_HEAD(&cache->leaves);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400213}
214
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400215static void backref_cache_cleanup(struct backref_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400216{
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400217 struct backref_node *node;
218 int i;
219
220 while (!list_empty(&cache->detached)) {
221 node = list_entry(cache->detached.next,
222 struct backref_node, list);
223 remove_backref_node(cache, node);
224 }
225
226 while (!list_empty(&cache->leaves)) {
227 node = list_entry(cache->leaves.next,
228 struct backref_node, lower);
229 remove_backref_node(cache, node);
230 }
231
232 cache->last_trans = 0;
233
234 for (i = 0; i < BTRFS_MAX_LEVEL; i++)
235 BUG_ON(!list_empty(&cache->pending[i]));
236 BUG_ON(!list_empty(&cache->changed));
237 BUG_ON(!list_empty(&cache->detached));
238 BUG_ON(!RB_EMPTY_ROOT(&cache->rb_root));
239 BUG_ON(cache->nr_nodes);
240 BUG_ON(cache->nr_edges);
241}
242
243static struct backref_node *alloc_backref_node(struct backref_cache *cache)
244{
245 struct backref_node *node;
246
247 node = kzalloc(sizeof(*node), GFP_NOFS);
248 if (node) {
249 INIT_LIST_HEAD(&node->list);
250 INIT_LIST_HEAD(&node->upper);
251 INIT_LIST_HEAD(&node->lower);
252 RB_CLEAR_NODE(&node->rb_node);
253 cache->nr_nodes++;
254 }
255 return node;
256}
257
258static void free_backref_node(struct backref_cache *cache,
259 struct backref_node *node)
260{
261 if (node) {
262 cache->nr_nodes--;
263 kfree(node);
264 }
265}
266
267static struct backref_edge *alloc_backref_edge(struct backref_cache *cache)
268{
269 struct backref_edge *edge;
270
271 edge = kzalloc(sizeof(*edge), GFP_NOFS);
272 if (edge)
273 cache->nr_edges++;
274 return edge;
275}
276
277static void free_backref_edge(struct backref_cache *cache,
278 struct backref_edge *edge)
279{
280 if (edge) {
281 cache->nr_edges--;
282 kfree(edge);
283 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400284}
285
286static struct rb_node *tree_insert(struct rb_root *root, u64 bytenr,
287 struct rb_node *node)
288{
289 struct rb_node **p = &root->rb_node;
290 struct rb_node *parent = NULL;
291 struct tree_entry *entry;
292
293 while (*p) {
294 parent = *p;
295 entry = rb_entry(parent, struct tree_entry, rb_node);
296
297 if (bytenr < entry->bytenr)
298 p = &(*p)->rb_left;
299 else if (bytenr > entry->bytenr)
300 p = &(*p)->rb_right;
301 else
302 return parent;
303 }
304
305 rb_link_node(node, parent, p);
306 rb_insert_color(node, root);
307 return NULL;
308}
309
310static struct rb_node *tree_search(struct rb_root *root, u64 bytenr)
311{
312 struct rb_node *n = root->rb_node;
313 struct tree_entry *entry;
314
315 while (n) {
316 entry = rb_entry(n, struct tree_entry, rb_node);
317
318 if (bytenr < entry->bytenr)
319 n = n->rb_left;
320 else if (bytenr > entry->bytenr)
321 n = n->rb_right;
322 else
323 return n;
324 }
325 return NULL;
326}
327
328/*
329 * walk up backref nodes until reach node presents tree root
330 */
331static struct backref_node *walk_up_backref(struct backref_node *node,
332 struct backref_edge *edges[],
333 int *index)
334{
335 struct backref_edge *edge;
336 int idx = *index;
337
338 while (!list_empty(&node->upper)) {
339 edge = list_entry(node->upper.next,
340 struct backref_edge, list[LOWER]);
341 edges[idx++] = edge;
342 node = edge->node[UPPER];
343 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400344 BUG_ON(node->detached);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400345 *index = idx;
346 return node;
347}
348
349/*
350 * walk down backref nodes to find start of next reference path
351 */
352static struct backref_node *walk_down_backref(struct backref_edge *edges[],
353 int *index)
354{
355 struct backref_edge *edge;
356 struct backref_node *lower;
357 int idx = *index;
358
359 while (idx > 0) {
360 edge = edges[idx - 1];
361 lower = edge->node[LOWER];
362 if (list_is_last(&edge->list[LOWER], &lower->upper)) {
363 idx--;
364 continue;
365 }
366 edge = list_entry(edge->list[LOWER].next,
367 struct backref_edge, list[LOWER]);
368 edges[idx - 1] = edge;
369 *index = idx;
370 return edge->node[UPPER];
371 }
372 *index = 0;
373 return NULL;
374}
375
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400376static void unlock_node_buffer(struct backref_node *node)
377{
378 if (node->locked) {
379 btrfs_tree_unlock(node->eb);
380 node->locked = 0;
381 }
382}
383
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400384static void drop_node_buffer(struct backref_node *node)
385{
386 if (node->eb) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400387 unlock_node_buffer(node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400388 free_extent_buffer(node->eb);
389 node->eb = NULL;
390 }
391}
392
393static void drop_backref_node(struct backref_cache *tree,
394 struct backref_node *node)
395{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400396 BUG_ON(!list_empty(&node->upper));
397
398 drop_node_buffer(node);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400399 list_del(&node->list);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400400 list_del(&node->lower);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400401 if (!RB_EMPTY_NODE(&node->rb_node))
402 rb_erase(&node->rb_node, &tree->rb_root);
403 free_backref_node(tree, node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400404}
405
406/*
407 * remove a backref node from the backref cache
408 */
409static void remove_backref_node(struct backref_cache *cache,
410 struct backref_node *node)
411{
412 struct backref_node *upper;
413 struct backref_edge *edge;
414
415 if (!node)
416 return;
417
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400418 BUG_ON(!node->lowest && !node->detached);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400419 while (!list_empty(&node->upper)) {
420 edge = list_entry(node->upper.next, struct backref_edge,
421 list[LOWER]);
422 upper = edge->node[UPPER];
423 list_del(&edge->list[LOWER]);
424 list_del(&edge->list[UPPER]);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400425 free_backref_edge(cache, edge);
426
427 if (RB_EMPTY_NODE(&upper->rb_node)) {
428 BUG_ON(!list_empty(&node->upper));
429 drop_backref_node(cache, node);
430 node = upper;
431 node->lowest = 1;
432 continue;
433 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400434 /*
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400435 * add the node to leaf node list if no other
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400436 * child block cached.
437 */
438 if (list_empty(&upper->lower)) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400439 list_add_tail(&upper->lower, &cache->leaves);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400440 upper->lowest = 1;
441 }
442 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400443
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400444 drop_backref_node(cache, node);
445}
446
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400447static void update_backref_node(struct backref_cache *cache,
448 struct backref_node *node, u64 bytenr)
449{
450 struct rb_node *rb_node;
451 rb_erase(&node->rb_node, &cache->rb_root);
452 node->bytenr = bytenr;
453 rb_node = tree_insert(&cache->rb_root, node->bytenr, &node->rb_node);
454 BUG_ON(rb_node);
455}
456
457/*
458 * update backref cache after a transaction commit
459 */
460static int update_backref_cache(struct btrfs_trans_handle *trans,
461 struct backref_cache *cache)
462{
463 struct backref_node *node;
464 int level = 0;
465
466 if (cache->last_trans == 0) {
467 cache->last_trans = trans->transid;
468 return 0;
469 }
470
471 if (cache->last_trans == trans->transid)
472 return 0;
473
474 /*
475 * detached nodes are used to avoid unnecessary backref
476 * lookup. transaction commit changes the extent tree.
477 * so the detached nodes are no longer useful.
478 */
479 while (!list_empty(&cache->detached)) {
480 node = list_entry(cache->detached.next,
481 struct backref_node, list);
482 remove_backref_node(cache, node);
483 }
484
485 while (!list_empty(&cache->changed)) {
486 node = list_entry(cache->changed.next,
487 struct backref_node, list);
488 list_del_init(&node->list);
489 BUG_ON(node->pending);
490 update_backref_node(cache, node, node->new_bytenr);
491 }
492
493 /*
494 * some nodes can be left in the pending list if there were
495 * errors during processing the pending nodes.
496 */
497 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
498 list_for_each_entry(node, &cache->pending[level], list) {
499 BUG_ON(!node->pending);
500 if (node->bytenr == node->new_bytenr)
501 continue;
502 update_backref_node(cache, node, node->new_bytenr);
503 }
504 }
505
506 cache->last_trans = 0;
507 return 1;
508}
509
510static int should_ignore_root(struct btrfs_root *root)
511{
512 struct btrfs_root *reloc_root;
513
514 if (!root->ref_cows)
515 return 0;
516
517 reloc_root = root->reloc_root;
518 if (!reloc_root)
519 return 0;
520
521 if (btrfs_root_last_snapshot(&reloc_root->root_item) ==
522 root->fs_info->running_transaction->transid - 1)
523 return 0;
524 /*
525 * if there is reloc tree and it was created in previous
526 * transaction backref lookup can find the reloc tree,
527 * so backref node for the fs tree root is useless for
528 * relocation.
529 */
530 return 1;
531}
532
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400533/*
534 * find reloc tree by address of tree root
535 */
536static struct btrfs_root *find_reloc_root(struct reloc_control *rc,
537 u64 bytenr)
538{
539 struct rb_node *rb_node;
540 struct mapping_node *node;
541 struct btrfs_root *root = NULL;
542
543 spin_lock(&rc->reloc_root_tree.lock);
544 rb_node = tree_search(&rc->reloc_root_tree.rb_root, bytenr);
545 if (rb_node) {
546 node = rb_entry(rb_node, struct mapping_node, rb_node);
547 root = (struct btrfs_root *)node->data;
548 }
549 spin_unlock(&rc->reloc_root_tree.lock);
550 return root;
551}
552
553static int is_cowonly_root(u64 root_objectid)
554{
555 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
556 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
557 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
558 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
559 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
560 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
561 return 1;
562 return 0;
563}
564
565static struct btrfs_root *read_fs_root(struct btrfs_fs_info *fs_info,
566 u64 root_objectid)
567{
568 struct btrfs_key key;
569
570 key.objectid = root_objectid;
571 key.type = BTRFS_ROOT_ITEM_KEY;
572 if (is_cowonly_root(root_objectid))
573 key.offset = 0;
574 else
575 key.offset = (u64)-1;
576
577 return btrfs_read_fs_root_no_name(fs_info, &key);
578}
579
580#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
581static noinline_for_stack
582struct btrfs_root *find_tree_root(struct reloc_control *rc,
583 struct extent_buffer *leaf,
584 struct btrfs_extent_ref_v0 *ref0)
585{
586 struct btrfs_root *root;
587 u64 root_objectid = btrfs_ref_root_v0(leaf, ref0);
588 u64 generation = btrfs_ref_generation_v0(leaf, ref0);
589
590 BUG_ON(root_objectid == BTRFS_TREE_RELOC_OBJECTID);
591
592 root = read_fs_root(rc->extent_root->fs_info, root_objectid);
593 BUG_ON(IS_ERR(root));
594
595 if (root->ref_cows &&
596 generation != btrfs_root_generation(&root->root_item))
597 return NULL;
598
599 return root;
600}
601#endif
602
603static noinline_for_stack
604int find_inline_backref(struct extent_buffer *leaf, int slot,
605 unsigned long *ptr, unsigned long *end)
606{
607 struct btrfs_extent_item *ei;
608 struct btrfs_tree_block_info *bi;
609 u32 item_size;
610
611 item_size = btrfs_item_size_nr(leaf, slot);
612#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
613 if (item_size < sizeof(*ei)) {
614 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
615 return 1;
616 }
617#endif
618 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
619 WARN_ON(!(btrfs_extent_flags(leaf, ei) &
620 BTRFS_EXTENT_FLAG_TREE_BLOCK));
621
622 if (item_size <= sizeof(*ei) + sizeof(*bi)) {
623 WARN_ON(item_size < sizeof(*ei) + sizeof(*bi));
624 return 1;
625 }
626
627 bi = (struct btrfs_tree_block_info *)(ei + 1);
628 *ptr = (unsigned long)(bi + 1);
629 *end = (unsigned long)ei + item_size;
630 return 0;
631}
632
633/*
634 * build backref tree for a given tree block. root of the backref tree
635 * corresponds the tree block, leaves of the backref tree correspond
636 * roots of b-trees that reference the tree block.
637 *
638 * the basic idea of this function is check backrefs of a given block
639 * to find upper level blocks that refernece the block, and then check
640 * bakcrefs of these upper level blocks recursively. the recursion stop
641 * when tree root is reached or backrefs for the block is cached.
642 *
643 * NOTE: if we find backrefs for a block are cached, we know backrefs
644 * for all upper level blocks that directly/indirectly reference the
645 * block are also cached.
646 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400647static noinline_for_stack
648struct backref_node *build_backref_tree(struct reloc_control *rc,
649 struct btrfs_key *node_key,
650 int level, u64 bytenr)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400651{
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400652 struct backref_cache *cache = &rc->backref_cache;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400653 struct btrfs_path *path1;
654 struct btrfs_path *path2;
655 struct extent_buffer *eb;
656 struct btrfs_root *root;
657 struct backref_node *cur;
658 struct backref_node *upper;
659 struct backref_node *lower;
660 struct backref_node *node = NULL;
661 struct backref_node *exist = NULL;
662 struct backref_edge *edge;
663 struct rb_node *rb_node;
664 struct btrfs_key key;
665 unsigned long end;
666 unsigned long ptr;
667 LIST_HEAD(list);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400668 LIST_HEAD(useless);
669 int cowonly;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400670 int ret;
671 int err = 0;
672
673 path1 = btrfs_alloc_path();
674 path2 = btrfs_alloc_path();
675 if (!path1 || !path2) {
676 err = -ENOMEM;
677 goto out;
678 }
Josef Bacik026fd312011-05-13 10:32:11 -0400679 path1->reada = 1;
680 path2->reada = 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400681
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400682 node = alloc_backref_node(cache);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400683 if (!node) {
684 err = -ENOMEM;
685 goto out;
686 }
687
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400688 node->bytenr = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400689 node->level = level;
690 node->lowest = 1;
691 cur = node;
692again:
693 end = 0;
694 ptr = 0;
695 key.objectid = cur->bytenr;
696 key.type = BTRFS_EXTENT_ITEM_KEY;
697 key.offset = (u64)-1;
698
699 path1->search_commit_root = 1;
700 path1->skip_locking = 1;
701 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path1,
702 0, 0);
703 if (ret < 0) {
704 err = ret;
705 goto out;
706 }
707 BUG_ON(!ret || !path1->slots[0]);
708
709 path1->slots[0]--;
710
711 WARN_ON(cur->checked);
712 if (!list_empty(&cur->upper)) {
713 /*
714 * the backref was added previously when processsing
715 * backref of type BTRFS_TREE_BLOCK_REF_KEY
716 */
717 BUG_ON(!list_is_singular(&cur->upper));
718 edge = list_entry(cur->upper.next, struct backref_edge,
719 list[LOWER]);
720 BUG_ON(!list_empty(&edge->list[UPPER]));
721 exist = edge->node[UPPER];
722 /*
723 * add the upper level block to pending list if we need
724 * check its backrefs
725 */
726 if (!exist->checked)
727 list_add_tail(&edge->list[UPPER], &list);
728 } else {
729 exist = NULL;
730 }
731
732 while (1) {
733 cond_resched();
734 eb = path1->nodes[0];
735
736 if (ptr >= end) {
737 if (path1->slots[0] >= btrfs_header_nritems(eb)) {
738 ret = btrfs_next_leaf(rc->extent_root, path1);
739 if (ret < 0) {
740 err = ret;
741 goto out;
742 }
743 if (ret > 0)
744 break;
745 eb = path1->nodes[0];
746 }
747
748 btrfs_item_key_to_cpu(eb, &key, path1->slots[0]);
749 if (key.objectid != cur->bytenr) {
750 WARN_ON(exist);
751 break;
752 }
753
754 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
755 ret = find_inline_backref(eb, path1->slots[0],
756 &ptr, &end);
757 if (ret)
758 goto next;
759 }
760 }
761
762 if (ptr < end) {
763 /* update key for inline back ref */
764 struct btrfs_extent_inline_ref *iref;
765 iref = (struct btrfs_extent_inline_ref *)ptr;
766 key.type = btrfs_extent_inline_ref_type(eb, iref);
767 key.offset = btrfs_extent_inline_ref_offset(eb, iref);
768 WARN_ON(key.type != BTRFS_TREE_BLOCK_REF_KEY &&
769 key.type != BTRFS_SHARED_BLOCK_REF_KEY);
770 }
771
772 if (exist &&
773 ((key.type == BTRFS_TREE_BLOCK_REF_KEY &&
774 exist->owner == key.offset) ||
775 (key.type == BTRFS_SHARED_BLOCK_REF_KEY &&
776 exist->bytenr == key.offset))) {
777 exist = NULL;
778 goto next;
779 }
780
781#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
782 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY ||
783 key.type == BTRFS_EXTENT_REF_V0_KEY) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400784 if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400785 struct btrfs_extent_ref_v0 *ref0;
786 ref0 = btrfs_item_ptr(eb, path1->slots[0],
787 struct btrfs_extent_ref_v0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400788 if (key.objectid == key.offset) {
Yan, Zheng046f2642010-05-31 08:58:47 +0000789 root = find_tree_root(rc, eb, ref0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400790 if (root && !should_ignore_root(root))
791 cur->root = root;
792 else
793 list_add(&cur->list, &useless);
794 break;
795 }
Yan, Zheng046f2642010-05-31 08:58:47 +0000796 if (is_cowonly_root(btrfs_ref_root_v0(eb,
797 ref0)))
798 cur->cowonly = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400799 }
800#else
801 BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY);
802 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
803#endif
804 if (key.objectid == key.offset) {
805 /*
806 * only root blocks of reloc trees use
807 * backref of this type.
808 */
809 root = find_reloc_root(rc, cur->bytenr);
810 BUG_ON(!root);
811 cur->root = root;
812 break;
813 }
814
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400815 edge = alloc_backref_edge(cache);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400816 if (!edge) {
817 err = -ENOMEM;
818 goto out;
819 }
820 rb_node = tree_search(&cache->rb_root, key.offset);
821 if (!rb_node) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400822 upper = alloc_backref_node(cache);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400823 if (!upper) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400824 free_backref_edge(cache, edge);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400825 err = -ENOMEM;
826 goto out;
827 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400828 upper->bytenr = key.offset;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400829 upper->level = cur->level + 1;
830 /*
831 * backrefs for the upper level block isn't
832 * cached, add the block to pending list
833 */
834 list_add_tail(&edge->list[UPPER], &list);
835 } else {
836 upper = rb_entry(rb_node, struct backref_node,
837 rb_node);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400838 BUG_ON(!upper->checked);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400839 INIT_LIST_HEAD(&edge->list[UPPER]);
840 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400841 list_add_tail(&edge->list[LOWER], &cur->upper);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400842 edge->node[LOWER] = cur;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400843 edge->node[UPPER] = upper;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400844
845 goto next;
846 } else if (key.type != BTRFS_TREE_BLOCK_REF_KEY) {
847 goto next;
848 }
849
850 /* key.type == BTRFS_TREE_BLOCK_REF_KEY */
851 root = read_fs_root(rc->extent_root->fs_info, key.offset);
852 if (IS_ERR(root)) {
853 err = PTR_ERR(root);
854 goto out;
855 }
856
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400857 if (!root->ref_cows)
858 cur->cowonly = 1;
859
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400860 if (btrfs_root_level(&root->root_item) == cur->level) {
861 /* tree root */
862 BUG_ON(btrfs_root_bytenr(&root->root_item) !=
863 cur->bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400864 if (should_ignore_root(root))
865 list_add(&cur->list, &useless);
866 else
867 cur->root = root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400868 break;
869 }
870
871 level = cur->level + 1;
872
873 /*
874 * searching the tree to find upper level blocks
875 * reference the block.
876 */
877 path2->search_commit_root = 1;
878 path2->skip_locking = 1;
879 path2->lowest_level = level;
880 ret = btrfs_search_slot(NULL, root, node_key, path2, 0, 0);
881 path2->lowest_level = 0;
882 if (ret < 0) {
883 err = ret;
884 goto out;
885 }
Yan Zheng33c66f42009-07-22 09:59:00 -0400886 if (ret > 0 && path2->slots[level] > 0)
887 path2->slots[level]--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400888
889 eb = path2->nodes[level];
890 WARN_ON(btrfs_node_blockptr(eb, path2->slots[level]) !=
891 cur->bytenr);
892
893 lower = cur;
894 for (; level < BTRFS_MAX_LEVEL; level++) {
895 if (!path2->nodes[level]) {
896 BUG_ON(btrfs_root_bytenr(&root->root_item) !=
897 lower->bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400898 if (should_ignore_root(root))
899 list_add(&lower->list, &useless);
900 else
901 lower->root = root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400902 break;
903 }
904
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400905 edge = alloc_backref_edge(cache);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400906 if (!edge) {
907 err = -ENOMEM;
908 goto out;
909 }
910
911 eb = path2->nodes[level];
912 rb_node = tree_search(&cache->rb_root, eb->start);
913 if (!rb_node) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400914 upper = alloc_backref_node(cache);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400915 if (!upper) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400916 free_backref_edge(cache, edge);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400917 err = -ENOMEM;
918 goto out;
919 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400920 upper->bytenr = eb->start;
921 upper->owner = btrfs_header_owner(eb);
922 upper->level = lower->level + 1;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400923 if (!root->ref_cows)
924 upper->cowonly = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400925
926 /*
927 * if we know the block isn't shared
928 * we can void checking its backrefs.
929 */
930 if (btrfs_block_can_be_shared(root, eb))
931 upper->checked = 0;
932 else
933 upper->checked = 1;
934
935 /*
936 * add the block to pending list if we
937 * need check its backrefs. only block
938 * at 'cur->level + 1' is added to the
939 * tail of pending list. this guarantees
940 * we check backrefs from lower level
941 * blocks to upper level blocks.
942 */
943 if (!upper->checked &&
944 level == cur->level + 1) {
945 list_add_tail(&edge->list[UPPER],
946 &list);
947 } else
948 INIT_LIST_HEAD(&edge->list[UPPER]);
949 } else {
950 upper = rb_entry(rb_node, struct backref_node,
951 rb_node);
952 BUG_ON(!upper->checked);
953 INIT_LIST_HEAD(&edge->list[UPPER]);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400954 if (!upper->owner)
955 upper->owner = btrfs_header_owner(eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400956 }
957 list_add_tail(&edge->list[LOWER], &lower->upper);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400958 edge->node[LOWER] = lower;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400959 edge->node[UPPER] = upper;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400960
961 if (rb_node)
962 break;
963 lower = upper;
964 upper = NULL;
965 }
966 btrfs_release_path(root, path2);
967next:
968 if (ptr < end) {
969 ptr += btrfs_extent_inline_ref_size(key.type);
970 if (ptr >= end) {
971 WARN_ON(ptr > end);
972 ptr = 0;
973 end = 0;
974 }
975 }
976 if (ptr >= end)
977 path1->slots[0]++;
978 }
979 btrfs_release_path(rc->extent_root, path1);
980
981 cur->checked = 1;
982 WARN_ON(exist);
983
984 /* the pending list isn't empty, take the first block to process */
985 if (!list_empty(&list)) {
986 edge = list_entry(list.next, struct backref_edge, list[UPPER]);
987 list_del_init(&edge->list[UPPER]);
988 cur = edge->node[UPPER];
989 goto again;
990 }
991
992 /*
993 * everything goes well, connect backref nodes and insert backref nodes
994 * into the cache.
995 */
996 BUG_ON(!node->checked);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400997 cowonly = node->cowonly;
998 if (!cowonly) {
999 rb_node = tree_insert(&cache->rb_root, node->bytenr,
1000 &node->rb_node);
1001 BUG_ON(rb_node);
1002 list_add_tail(&node->lower, &cache->leaves);
1003 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001004
1005 list_for_each_entry(edge, &node->upper, list[LOWER])
1006 list_add_tail(&edge->list[UPPER], &list);
1007
1008 while (!list_empty(&list)) {
1009 edge = list_entry(list.next, struct backref_edge, list[UPPER]);
1010 list_del_init(&edge->list[UPPER]);
1011 upper = edge->node[UPPER];
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001012 if (upper->detached) {
1013 list_del(&edge->list[LOWER]);
1014 lower = edge->node[LOWER];
1015 free_backref_edge(cache, edge);
1016 if (list_empty(&lower->upper))
1017 list_add(&lower->list, &useless);
1018 continue;
1019 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001020
1021 if (!RB_EMPTY_NODE(&upper->rb_node)) {
1022 if (upper->lowest) {
1023 list_del_init(&upper->lower);
1024 upper->lowest = 0;
1025 }
1026
1027 list_add_tail(&edge->list[UPPER], &upper->lower);
1028 continue;
1029 }
1030
1031 BUG_ON(!upper->checked);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001032 BUG_ON(cowonly != upper->cowonly);
1033 if (!cowonly) {
1034 rb_node = tree_insert(&cache->rb_root, upper->bytenr,
1035 &upper->rb_node);
1036 BUG_ON(rb_node);
1037 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001038
1039 list_add_tail(&edge->list[UPPER], &upper->lower);
1040
1041 list_for_each_entry(edge, &upper->upper, list[LOWER])
1042 list_add_tail(&edge->list[UPPER], &list);
1043 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001044 /*
1045 * process useless backref nodes. backref nodes for tree leaves
1046 * are deleted from the cache. backref nodes for upper level
1047 * tree blocks are left in the cache to avoid unnecessary backref
1048 * lookup.
1049 */
1050 while (!list_empty(&useless)) {
1051 upper = list_entry(useless.next, struct backref_node, list);
1052 list_del_init(&upper->list);
1053 BUG_ON(!list_empty(&upper->upper));
1054 if (upper == node)
1055 node = NULL;
1056 if (upper->lowest) {
1057 list_del_init(&upper->lower);
1058 upper->lowest = 0;
1059 }
1060 while (!list_empty(&upper->lower)) {
1061 edge = list_entry(upper->lower.next,
1062 struct backref_edge, list[UPPER]);
1063 list_del(&edge->list[UPPER]);
1064 list_del(&edge->list[LOWER]);
1065 lower = edge->node[LOWER];
1066 free_backref_edge(cache, edge);
1067
1068 if (list_empty(&lower->upper))
1069 list_add(&lower->list, &useless);
1070 }
1071 __mark_block_processed(rc, upper);
1072 if (upper->level > 0) {
1073 list_add(&upper->list, &cache->detached);
1074 upper->detached = 1;
1075 } else {
1076 rb_erase(&upper->rb_node, &cache->rb_root);
1077 free_backref_node(cache, upper);
1078 }
1079 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001080out:
1081 btrfs_free_path(path1);
1082 btrfs_free_path(path2);
1083 if (err) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001084 while (!list_empty(&useless)) {
1085 lower = list_entry(useless.next,
1086 struct backref_node, upper);
1087 list_del_init(&lower->upper);
1088 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001089 upper = node;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001090 INIT_LIST_HEAD(&list);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001091 while (upper) {
1092 if (RB_EMPTY_NODE(&upper->rb_node)) {
1093 list_splice_tail(&upper->upper, &list);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001094 free_backref_node(cache, upper);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001095 }
1096
1097 if (list_empty(&list))
1098 break;
1099
1100 edge = list_entry(list.next, struct backref_edge,
1101 list[LOWER]);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001102 list_del(&edge->list[LOWER]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001103 upper = edge->node[UPPER];
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001104 free_backref_edge(cache, edge);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001105 }
1106 return ERR_PTR(err);
1107 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001108 BUG_ON(node && node->detached);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001109 return node;
1110}
1111
1112/*
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001113 * helper to add backref node for the newly created snapshot.
1114 * the backref node is created by cloning backref node that
1115 * corresponds to root of source tree
1116 */
1117static int clone_backref_node(struct btrfs_trans_handle *trans,
1118 struct reloc_control *rc,
1119 struct btrfs_root *src,
1120 struct btrfs_root *dest)
1121{
1122 struct btrfs_root *reloc_root = src->reloc_root;
1123 struct backref_cache *cache = &rc->backref_cache;
1124 struct backref_node *node = NULL;
1125 struct backref_node *new_node;
1126 struct backref_edge *edge;
1127 struct backref_edge *new_edge;
1128 struct rb_node *rb_node;
1129
1130 if (cache->last_trans > 0)
1131 update_backref_cache(trans, cache);
1132
1133 rb_node = tree_search(&cache->rb_root, src->commit_root->start);
1134 if (rb_node) {
1135 node = rb_entry(rb_node, struct backref_node, rb_node);
1136 if (node->detached)
1137 node = NULL;
1138 else
1139 BUG_ON(node->new_bytenr != reloc_root->node->start);
1140 }
1141
1142 if (!node) {
1143 rb_node = tree_search(&cache->rb_root,
1144 reloc_root->commit_root->start);
1145 if (rb_node) {
1146 node = rb_entry(rb_node, struct backref_node,
1147 rb_node);
1148 BUG_ON(node->detached);
1149 }
1150 }
1151
1152 if (!node)
1153 return 0;
1154
1155 new_node = alloc_backref_node(cache);
1156 if (!new_node)
1157 return -ENOMEM;
1158
1159 new_node->bytenr = dest->node->start;
1160 new_node->level = node->level;
1161 new_node->lowest = node->lowest;
Yan, Zheng6848ad62011-02-14 16:00:03 -05001162 new_node->checked = 1;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001163 new_node->root = dest;
1164
1165 if (!node->lowest) {
1166 list_for_each_entry(edge, &node->lower, list[UPPER]) {
1167 new_edge = alloc_backref_edge(cache);
1168 if (!new_edge)
1169 goto fail;
1170
1171 new_edge->node[UPPER] = new_node;
1172 new_edge->node[LOWER] = edge->node[LOWER];
1173 list_add_tail(&new_edge->list[UPPER],
1174 &new_node->lower);
1175 }
1176 }
1177
1178 rb_node = tree_insert(&cache->rb_root, new_node->bytenr,
1179 &new_node->rb_node);
1180 BUG_ON(rb_node);
1181
1182 if (!new_node->lowest) {
1183 list_for_each_entry(new_edge, &new_node->lower, list[UPPER]) {
1184 list_add_tail(&new_edge->list[LOWER],
1185 &new_edge->node[LOWER]->upper);
1186 }
1187 }
1188 return 0;
1189fail:
1190 while (!list_empty(&new_node->lower)) {
1191 new_edge = list_entry(new_node->lower.next,
1192 struct backref_edge, list[UPPER]);
1193 list_del(&new_edge->list[UPPER]);
1194 free_backref_edge(cache, new_edge);
1195 }
1196 free_backref_node(cache, new_node);
1197 return -ENOMEM;
1198}
1199
1200/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001201 * helper to add 'address of tree root -> reloc tree' mapping
1202 */
1203static int __add_reloc_root(struct btrfs_root *root)
1204{
1205 struct rb_node *rb_node;
1206 struct mapping_node *node;
1207 struct reloc_control *rc = root->fs_info->reloc_ctl;
1208
1209 node = kmalloc(sizeof(*node), GFP_NOFS);
1210 BUG_ON(!node);
1211
1212 node->bytenr = root->node->start;
1213 node->data = root;
1214
1215 spin_lock(&rc->reloc_root_tree.lock);
1216 rb_node = tree_insert(&rc->reloc_root_tree.rb_root,
1217 node->bytenr, &node->rb_node);
1218 spin_unlock(&rc->reloc_root_tree.lock);
1219 BUG_ON(rb_node);
1220
1221 list_add_tail(&root->root_list, &rc->reloc_roots);
1222 return 0;
1223}
1224
1225/*
1226 * helper to update/delete the 'address of tree root -> reloc tree'
1227 * mapping
1228 */
1229static int __update_reloc_root(struct btrfs_root *root, int del)
1230{
1231 struct rb_node *rb_node;
1232 struct mapping_node *node = NULL;
1233 struct reloc_control *rc = root->fs_info->reloc_ctl;
1234
1235 spin_lock(&rc->reloc_root_tree.lock);
1236 rb_node = tree_search(&rc->reloc_root_tree.rb_root,
1237 root->commit_root->start);
1238 if (rb_node) {
1239 node = rb_entry(rb_node, struct mapping_node, rb_node);
1240 rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
1241 }
1242 spin_unlock(&rc->reloc_root_tree.lock);
1243
1244 BUG_ON((struct btrfs_root *)node->data != root);
1245
1246 if (!del) {
1247 spin_lock(&rc->reloc_root_tree.lock);
1248 node->bytenr = root->node->start;
1249 rb_node = tree_insert(&rc->reloc_root_tree.rb_root,
1250 node->bytenr, &node->rb_node);
1251 spin_unlock(&rc->reloc_root_tree.lock);
1252 BUG_ON(rb_node);
1253 } else {
1254 list_del_init(&root->root_list);
1255 kfree(node);
1256 }
1257 return 0;
1258}
1259
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001260static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans,
1261 struct btrfs_root *root, u64 objectid)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001262{
1263 struct btrfs_root *reloc_root;
1264 struct extent_buffer *eb;
1265 struct btrfs_root_item *root_item;
1266 struct btrfs_key root_key;
1267 int ret;
1268
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001269 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
1270 BUG_ON(!root_item);
1271
1272 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
1273 root_key.type = BTRFS_ROOT_ITEM_KEY;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001274 root_key.offset = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001275
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001276 if (root->root_key.objectid == objectid) {
1277 /* called by btrfs_init_reloc_root */
1278 ret = btrfs_copy_root(trans, root, root->commit_root, &eb,
1279 BTRFS_TREE_RELOC_OBJECTID);
1280 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001281
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001282 btrfs_set_root_last_snapshot(&root->root_item,
1283 trans->transid - 1);
1284 } else {
1285 /*
1286 * called by btrfs_reloc_post_snapshot_hook.
1287 * the source tree is a reloc tree, all tree blocks
1288 * modified after it was created have RELOC flag
1289 * set in their headers. so it's OK to not update
1290 * the 'last_snapshot'.
1291 */
1292 ret = btrfs_copy_root(trans, root, root->node, &eb,
1293 BTRFS_TREE_RELOC_OBJECTID);
1294 BUG_ON(ret);
1295 }
1296
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001297 memcpy(root_item, &root->root_item, sizeof(*root_item));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001298 btrfs_set_root_bytenr(root_item, eb->start);
1299 btrfs_set_root_level(root_item, btrfs_header_level(eb));
1300 btrfs_set_root_generation(root_item, trans->transid);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001301
1302 if (root->root_key.objectid == objectid) {
1303 btrfs_set_root_refs(root_item, 0);
1304 memset(&root_item->drop_progress, 0,
1305 sizeof(struct btrfs_disk_key));
1306 root_item->drop_level = 0;
1307 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001308
1309 btrfs_tree_unlock(eb);
1310 free_extent_buffer(eb);
1311
1312 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
1313 &root_key, root_item);
1314 BUG_ON(ret);
1315 kfree(root_item);
1316
1317 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
1318 &root_key);
1319 BUG_ON(IS_ERR(reloc_root));
1320 reloc_root->last_trans = trans->transid;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001321 return reloc_root;
1322}
1323
1324/*
1325 * create reloc tree for a given fs tree. reloc tree is just a
1326 * snapshot of the fs tree with special root objectid.
1327 */
1328int btrfs_init_reloc_root(struct btrfs_trans_handle *trans,
1329 struct btrfs_root *root)
1330{
1331 struct btrfs_root *reloc_root;
1332 struct reloc_control *rc = root->fs_info->reloc_ctl;
1333 int clear_rsv = 0;
1334
1335 if (root->reloc_root) {
1336 reloc_root = root->reloc_root;
1337 reloc_root->last_trans = trans->transid;
1338 return 0;
1339 }
1340
1341 if (!rc || !rc->create_reloc_tree ||
1342 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1343 return 0;
1344
1345 if (!trans->block_rsv) {
1346 trans->block_rsv = rc->block_rsv;
1347 clear_rsv = 1;
1348 }
1349 reloc_root = create_reloc_root(trans, root, root->root_key.objectid);
1350 if (clear_rsv)
1351 trans->block_rsv = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001352
1353 __add_reloc_root(reloc_root);
1354 root->reloc_root = reloc_root;
1355 return 0;
1356}
1357
1358/*
1359 * update root item of reloc tree
1360 */
1361int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
1362 struct btrfs_root *root)
1363{
1364 struct btrfs_root *reloc_root;
1365 struct btrfs_root_item *root_item;
1366 int del = 0;
1367 int ret;
1368
1369 if (!root->reloc_root)
1370 return 0;
1371
1372 reloc_root = root->reloc_root;
1373 root_item = &reloc_root->root_item;
1374
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001375 if (root->fs_info->reloc_ctl->merge_reloc_tree &&
1376 btrfs_root_refs(root_item) == 0) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001377 root->reloc_root = NULL;
1378 del = 1;
1379 }
1380
1381 __update_reloc_root(reloc_root, del);
1382
1383 if (reloc_root->commit_root != reloc_root->node) {
1384 btrfs_set_root_node(root_item, reloc_root->node);
1385 free_extent_buffer(reloc_root->commit_root);
1386 reloc_root->commit_root = btrfs_root_node(reloc_root);
1387 }
1388
1389 ret = btrfs_update_root(trans, root->fs_info->tree_root,
1390 &reloc_root->root_key, root_item);
1391 BUG_ON(ret);
1392 return 0;
1393}
1394
1395/*
1396 * helper to find first cached inode with inode number >= objectid
1397 * in a subvolume
1398 */
1399static struct inode *find_next_inode(struct btrfs_root *root, u64 objectid)
1400{
1401 struct rb_node *node;
1402 struct rb_node *prev;
1403 struct btrfs_inode *entry;
1404 struct inode *inode;
1405
1406 spin_lock(&root->inode_lock);
1407again:
1408 node = root->inode_tree.rb_node;
1409 prev = NULL;
1410 while (node) {
1411 prev = node;
1412 entry = rb_entry(node, struct btrfs_inode, rb_node);
1413
1414 if (objectid < entry->vfs_inode.i_ino)
1415 node = node->rb_left;
1416 else if (objectid > entry->vfs_inode.i_ino)
1417 node = node->rb_right;
1418 else
1419 break;
1420 }
1421 if (!node) {
1422 while (prev) {
1423 entry = rb_entry(prev, struct btrfs_inode, rb_node);
1424 if (objectid <= entry->vfs_inode.i_ino) {
1425 node = prev;
1426 break;
1427 }
1428 prev = rb_next(prev);
1429 }
1430 }
1431 while (node) {
1432 entry = rb_entry(node, struct btrfs_inode, rb_node);
1433 inode = igrab(&entry->vfs_inode);
1434 if (inode) {
1435 spin_unlock(&root->inode_lock);
1436 return inode;
1437 }
1438
1439 objectid = entry->vfs_inode.i_ino + 1;
1440 if (cond_resched_lock(&root->inode_lock))
1441 goto again;
1442
1443 node = rb_next(node);
1444 }
1445 spin_unlock(&root->inode_lock);
1446 return NULL;
1447}
1448
1449static int in_block_group(u64 bytenr,
1450 struct btrfs_block_group_cache *block_group)
1451{
1452 if (bytenr >= block_group->key.objectid &&
1453 bytenr < block_group->key.objectid + block_group->key.offset)
1454 return 1;
1455 return 0;
1456}
1457
1458/*
1459 * get new location of data
1460 */
1461static int get_new_location(struct inode *reloc_inode, u64 *new_bytenr,
1462 u64 bytenr, u64 num_bytes)
1463{
1464 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
1465 struct btrfs_path *path;
1466 struct btrfs_file_extent_item *fi;
1467 struct extent_buffer *leaf;
1468 int ret;
1469
1470 path = btrfs_alloc_path();
1471 if (!path)
1472 return -ENOMEM;
1473
1474 bytenr -= BTRFS_I(reloc_inode)->index_cnt;
1475 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
1476 bytenr, 0);
1477 if (ret < 0)
1478 goto out;
1479 if (ret > 0) {
1480 ret = -ENOENT;
1481 goto out;
1482 }
1483
1484 leaf = path->nodes[0];
1485 fi = btrfs_item_ptr(leaf, path->slots[0],
1486 struct btrfs_file_extent_item);
1487
1488 BUG_ON(btrfs_file_extent_offset(leaf, fi) ||
1489 btrfs_file_extent_compression(leaf, fi) ||
1490 btrfs_file_extent_encryption(leaf, fi) ||
1491 btrfs_file_extent_other_encoding(leaf, fi));
1492
1493 if (num_bytes != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
1494 ret = 1;
1495 goto out;
1496 }
1497
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001498 *new_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001499 ret = 0;
1500out:
1501 btrfs_free_path(path);
1502 return ret;
1503}
1504
1505/*
1506 * update file extent items in the tree leaf to point to
1507 * the new locations.
1508 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001509static noinline_for_stack
1510int replace_file_extents(struct btrfs_trans_handle *trans,
1511 struct reloc_control *rc,
1512 struct btrfs_root *root,
1513 struct extent_buffer *leaf)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001514{
1515 struct btrfs_key key;
1516 struct btrfs_file_extent_item *fi;
1517 struct inode *inode = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001518 u64 parent;
1519 u64 bytenr;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001520 u64 new_bytenr = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001521 u64 num_bytes;
1522 u64 end;
1523 u32 nritems;
1524 u32 i;
1525 int ret;
1526 int first = 1;
1527 int dirty = 0;
1528
1529 if (rc->stage != UPDATE_DATA_PTRS)
1530 return 0;
1531
1532 /* reloc trees always use full backref */
1533 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1534 parent = leaf->start;
1535 else
1536 parent = 0;
1537
1538 nritems = btrfs_header_nritems(leaf);
1539 for (i = 0; i < nritems; i++) {
1540 cond_resched();
1541 btrfs_item_key_to_cpu(leaf, &key, i);
1542 if (key.type != BTRFS_EXTENT_DATA_KEY)
1543 continue;
1544 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1545 if (btrfs_file_extent_type(leaf, fi) ==
1546 BTRFS_FILE_EXTENT_INLINE)
1547 continue;
1548 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1549 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
1550 if (bytenr == 0)
1551 continue;
1552 if (!in_block_group(bytenr, rc->block_group))
1553 continue;
1554
1555 /*
1556 * if we are modifying block in fs tree, wait for readpage
1557 * to complete and drop the extent cache
1558 */
1559 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001560 if (first) {
1561 inode = find_next_inode(root, key.objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001562 first = 0;
1563 } else if (inode && inode->i_ino < key.objectid) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001564 btrfs_add_delayed_iput(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001565 inode = find_next_inode(root, key.objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001566 }
1567 if (inode && inode->i_ino == key.objectid) {
1568 end = key.offset +
1569 btrfs_file_extent_num_bytes(leaf, fi);
1570 WARN_ON(!IS_ALIGNED(key.offset,
1571 root->sectorsize));
1572 WARN_ON(!IS_ALIGNED(end, root->sectorsize));
1573 end--;
1574 ret = try_lock_extent(&BTRFS_I(inode)->io_tree,
1575 key.offset, end,
1576 GFP_NOFS);
1577 if (!ret)
1578 continue;
1579
1580 btrfs_drop_extent_cache(inode, key.offset, end,
1581 1);
1582 unlock_extent(&BTRFS_I(inode)->io_tree,
1583 key.offset, end, GFP_NOFS);
1584 }
1585 }
1586
1587 ret = get_new_location(rc->data_inode, &new_bytenr,
1588 bytenr, num_bytes);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001589 if (ret > 0) {
1590 WARN_ON(1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001591 continue;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001592 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001593 BUG_ON(ret < 0);
1594
1595 btrfs_set_file_extent_disk_bytenr(leaf, fi, new_bytenr);
1596 dirty = 1;
1597
1598 key.offset -= btrfs_file_extent_offset(leaf, fi);
1599 ret = btrfs_inc_extent_ref(trans, root, new_bytenr,
1600 num_bytes, parent,
1601 btrfs_header_owner(leaf),
1602 key.objectid, key.offset);
1603 BUG_ON(ret);
1604
1605 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
1606 parent, btrfs_header_owner(leaf),
1607 key.objectid, key.offset);
1608 BUG_ON(ret);
1609 }
1610 if (dirty)
1611 btrfs_mark_buffer_dirty(leaf);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001612 if (inode)
1613 btrfs_add_delayed_iput(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001614 return 0;
1615}
1616
1617static noinline_for_stack
1618int memcmp_node_keys(struct extent_buffer *eb, int slot,
1619 struct btrfs_path *path, int level)
1620{
1621 struct btrfs_disk_key key1;
1622 struct btrfs_disk_key key2;
1623 btrfs_node_key(eb, &key1, slot);
1624 btrfs_node_key(path->nodes[level], &key2, path->slots[level]);
1625 return memcmp(&key1, &key2, sizeof(key1));
1626}
1627
1628/*
1629 * try to replace tree blocks in fs tree with the new blocks
1630 * in reloc tree. tree blocks haven't been modified since the
1631 * reloc tree was create can be replaced.
1632 *
1633 * if a block was replaced, level of the block + 1 is returned.
1634 * if no block got replaced, 0 is returned. if there are other
1635 * errors, a negative error number is returned.
1636 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001637static noinline_for_stack
1638int replace_path(struct btrfs_trans_handle *trans,
1639 struct btrfs_root *dest, struct btrfs_root *src,
1640 struct btrfs_path *path, struct btrfs_key *next_key,
1641 int lowest_level, int max_level)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001642{
1643 struct extent_buffer *eb;
1644 struct extent_buffer *parent;
1645 struct btrfs_key key;
1646 u64 old_bytenr;
1647 u64 new_bytenr;
1648 u64 old_ptr_gen;
1649 u64 new_ptr_gen;
1650 u64 last_snapshot;
1651 u32 blocksize;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001652 int cow = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001653 int level;
1654 int ret;
1655 int slot;
1656
1657 BUG_ON(src->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
1658 BUG_ON(dest->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001659
1660 last_snapshot = btrfs_root_last_snapshot(&src->root_item);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001661again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001662 slot = path->slots[lowest_level];
1663 btrfs_node_key_to_cpu(path->nodes[lowest_level], &key, slot);
1664
1665 eb = btrfs_lock_root_node(dest);
1666 btrfs_set_lock_blocking(eb);
1667 level = btrfs_header_level(eb);
1668
1669 if (level < lowest_level) {
1670 btrfs_tree_unlock(eb);
1671 free_extent_buffer(eb);
1672 return 0;
1673 }
1674
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001675 if (cow) {
1676 ret = btrfs_cow_block(trans, dest, eb, NULL, 0, &eb);
1677 BUG_ON(ret);
1678 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001679 btrfs_set_lock_blocking(eb);
1680
1681 if (next_key) {
1682 next_key->objectid = (u64)-1;
1683 next_key->type = (u8)-1;
1684 next_key->offset = (u64)-1;
1685 }
1686
1687 parent = eb;
1688 while (1) {
1689 level = btrfs_header_level(parent);
1690 BUG_ON(level < lowest_level);
1691
1692 ret = btrfs_bin_search(parent, &key, level, &slot);
1693 if (ret && slot > 0)
1694 slot--;
1695
1696 if (next_key && slot + 1 < btrfs_header_nritems(parent))
1697 btrfs_node_key_to_cpu(parent, next_key, slot + 1);
1698
1699 old_bytenr = btrfs_node_blockptr(parent, slot);
1700 blocksize = btrfs_level_size(dest, level - 1);
1701 old_ptr_gen = btrfs_node_ptr_generation(parent, slot);
1702
1703 if (level <= max_level) {
1704 eb = path->nodes[level];
1705 new_bytenr = btrfs_node_blockptr(eb,
1706 path->slots[level]);
1707 new_ptr_gen = btrfs_node_ptr_generation(eb,
1708 path->slots[level]);
1709 } else {
1710 new_bytenr = 0;
1711 new_ptr_gen = 0;
1712 }
1713
1714 if (new_bytenr > 0 && new_bytenr == old_bytenr) {
1715 WARN_ON(1);
1716 ret = level;
1717 break;
1718 }
1719
1720 if (new_bytenr == 0 || old_ptr_gen > last_snapshot ||
1721 memcmp_node_keys(parent, slot, path, level)) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001722 if (level <= lowest_level) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001723 ret = 0;
1724 break;
1725 }
1726
1727 eb = read_tree_block(dest, old_bytenr, blocksize,
1728 old_ptr_gen);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001729 BUG_ON(!eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001730 btrfs_tree_lock(eb);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001731 if (cow) {
1732 ret = btrfs_cow_block(trans, dest, eb, parent,
1733 slot, &eb);
1734 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001735 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001736 btrfs_set_lock_blocking(eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001737
1738 btrfs_tree_unlock(parent);
1739 free_extent_buffer(parent);
1740
1741 parent = eb;
1742 continue;
1743 }
1744
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001745 if (!cow) {
1746 btrfs_tree_unlock(parent);
1747 free_extent_buffer(parent);
1748 cow = 1;
1749 goto again;
1750 }
1751
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001752 btrfs_node_key_to_cpu(path->nodes[level], &key,
1753 path->slots[level]);
1754 btrfs_release_path(src, path);
1755
1756 path->lowest_level = level;
1757 ret = btrfs_search_slot(trans, src, &key, path, 0, 1);
1758 path->lowest_level = 0;
1759 BUG_ON(ret);
1760
1761 /*
1762 * swap blocks in fs tree and reloc tree.
1763 */
1764 btrfs_set_node_blockptr(parent, slot, new_bytenr);
1765 btrfs_set_node_ptr_generation(parent, slot, new_ptr_gen);
1766 btrfs_mark_buffer_dirty(parent);
1767
1768 btrfs_set_node_blockptr(path->nodes[level],
1769 path->slots[level], old_bytenr);
1770 btrfs_set_node_ptr_generation(path->nodes[level],
1771 path->slots[level], old_ptr_gen);
1772 btrfs_mark_buffer_dirty(path->nodes[level]);
1773
1774 ret = btrfs_inc_extent_ref(trans, src, old_bytenr, blocksize,
1775 path->nodes[level]->start,
1776 src->root_key.objectid, level - 1, 0);
1777 BUG_ON(ret);
1778 ret = btrfs_inc_extent_ref(trans, dest, new_bytenr, blocksize,
1779 0, dest->root_key.objectid, level - 1,
1780 0);
1781 BUG_ON(ret);
1782
1783 ret = btrfs_free_extent(trans, src, new_bytenr, blocksize,
1784 path->nodes[level]->start,
1785 src->root_key.objectid, level - 1, 0);
1786 BUG_ON(ret);
1787
1788 ret = btrfs_free_extent(trans, dest, old_bytenr, blocksize,
1789 0, dest->root_key.objectid, level - 1,
1790 0);
1791 BUG_ON(ret);
1792
1793 btrfs_unlock_up_safe(path, 0);
1794
1795 ret = level;
1796 break;
1797 }
1798 btrfs_tree_unlock(parent);
1799 free_extent_buffer(parent);
1800 return ret;
1801}
1802
1803/*
1804 * helper to find next relocated block in reloc tree
1805 */
1806static noinline_for_stack
1807int walk_up_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
1808 int *level)
1809{
1810 struct extent_buffer *eb;
1811 int i;
1812 u64 last_snapshot;
1813 u32 nritems;
1814
1815 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1816
1817 for (i = 0; i < *level; i++) {
1818 free_extent_buffer(path->nodes[i]);
1819 path->nodes[i] = NULL;
1820 }
1821
1822 for (i = *level; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
1823 eb = path->nodes[i];
1824 nritems = btrfs_header_nritems(eb);
1825 while (path->slots[i] + 1 < nritems) {
1826 path->slots[i]++;
1827 if (btrfs_node_ptr_generation(eb, path->slots[i]) <=
1828 last_snapshot)
1829 continue;
1830
1831 *level = i;
1832 return 0;
1833 }
1834 free_extent_buffer(path->nodes[i]);
1835 path->nodes[i] = NULL;
1836 }
1837 return 1;
1838}
1839
1840/*
1841 * walk down reloc tree to find relocated block of lowest level
1842 */
1843static noinline_for_stack
1844int walk_down_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
1845 int *level)
1846{
1847 struct extent_buffer *eb = NULL;
1848 int i;
1849 u64 bytenr;
1850 u64 ptr_gen = 0;
1851 u64 last_snapshot;
1852 u32 blocksize;
1853 u32 nritems;
1854
1855 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1856
1857 for (i = *level; i > 0; i--) {
1858 eb = path->nodes[i];
1859 nritems = btrfs_header_nritems(eb);
1860 while (path->slots[i] < nritems) {
1861 ptr_gen = btrfs_node_ptr_generation(eb, path->slots[i]);
1862 if (ptr_gen > last_snapshot)
1863 break;
1864 path->slots[i]++;
1865 }
1866 if (path->slots[i] >= nritems) {
1867 if (i == *level)
1868 break;
1869 *level = i + 1;
1870 return 0;
1871 }
1872 if (i == 1) {
1873 *level = i;
1874 return 0;
1875 }
1876
1877 bytenr = btrfs_node_blockptr(eb, path->slots[i]);
1878 blocksize = btrfs_level_size(root, i - 1);
1879 eb = read_tree_block(root, bytenr, blocksize, ptr_gen);
1880 BUG_ON(btrfs_header_level(eb) != i - 1);
1881 path->nodes[i - 1] = eb;
1882 path->slots[i - 1] = 0;
1883 }
1884 return 1;
1885}
1886
1887/*
1888 * invalidate extent cache for file extents whose key in range of
1889 * [min_key, max_key)
1890 */
1891static int invalidate_extent_cache(struct btrfs_root *root,
1892 struct btrfs_key *min_key,
1893 struct btrfs_key *max_key)
1894{
1895 struct inode *inode = NULL;
1896 u64 objectid;
1897 u64 start, end;
1898
1899 objectid = min_key->objectid;
1900 while (1) {
1901 cond_resched();
1902 iput(inode);
1903
1904 if (objectid > max_key->objectid)
1905 break;
1906
1907 inode = find_next_inode(root, objectid);
1908 if (!inode)
1909 break;
1910
1911 if (inode->i_ino > max_key->objectid) {
1912 iput(inode);
1913 break;
1914 }
1915
1916 objectid = inode->i_ino + 1;
1917 if (!S_ISREG(inode->i_mode))
1918 continue;
1919
1920 if (unlikely(min_key->objectid == inode->i_ino)) {
1921 if (min_key->type > BTRFS_EXTENT_DATA_KEY)
1922 continue;
1923 if (min_key->type < BTRFS_EXTENT_DATA_KEY)
1924 start = 0;
1925 else {
1926 start = min_key->offset;
1927 WARN_ON(!IS_ALIGNED(start, root->sectorsize));
1928 }
1929 } else {
1930 start = 0;
1931 }
1932
1933 if (unlikely(max_key->objectid == inode->i_ino)) {
1934 if (max_key->type < BTRFS_EXTENT_DATA_KEY)
1935 continue;
1936 if (max_key->type > BTRFS_EXTENT_DATA_KEY) {
1937 end = (u64)-1;
1938 } else {
1939 if (max_key->offset == 0)
1940 continue;
1941 end = max_key->offset;
1942 WARN_ON(!IS_ALIGNED(end, root->sectorsize));
1943 end--;
1944 }
1945 } else {
1946 end = (u64)-1;
1947 }
1948
1949 /* the lock_extent waits for readpage to complete */
1950 lock_extent(&BTRFS_I(inode)->io_tree, start, end, GFP_NOFS);
1951 btrfs_drop_extent_cache(inode, start, end, 1);
1952 unlock_extent(&BTRFS_I(inode)->io_tree, start, end, GFP_NOFS);
1953 }
1954 return 0;
1955}
1956
1957static int find_next_key(struct btrfs_path *path, int level,
1958 struct btrfs_key *key)
1959
1960{
1961 while (level < BTRFS_MAX_LEVEL) {
1962 if (!path->nodes[level])
1963 break;
1964 if (path->slots[level] + 1 <
1965 btrfs_header_nritems(path->nodes[level])) {
1966 btrfs_node_key_to_cpu(path->nodes[level], key,
1967 path->slots[level] + 1);
1968 return 0;
1969 }
1970 level++;
1971 }
1972 return 1;
1973}
1974
1975/*
1976 * merge the relocated tree blocks in reloc tree with corresponding
1977 * fs tree.
1978 */
1979static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
1980 struct btrfs_root *root)
1981{
1982 LIST_HEAD(inode_list);
1983 struct btrfs_key key;
1984 struct btrfs_key next_key;
1985 struct btrfs_trans_handle *trans;
1986 struct btrfs_root *reloc_root;
1987 struct btrfs_root_item *root_item;
1988 struct btrfs_path *path;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001989 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001990 unsigned long nr;
1991 int level;
1992 int max_level;
1993 int replaced = 0;
1994 int ret;
1995 int err = 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001996 u32 min_reserved;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001997
1998 path = btrfs_alloc_path();
1999 if (!path)
2000 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04002001 path->reada = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002002
2003 reloc_root = root->reloc_root;
2004 root_item = &reloc_root->root_item;
2005
2006 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2007 level = btrfs_root_level(root_item);
2008 extent_buffer_get(reloc_root->node);
2009 path->nodes[level] = reloc_root->node;
2010 path->slots[level] = 0;
2011 } else {
2012 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2013
2014 level = root_item->drop_level;
2015 BUG_ON(level == 0);
2016 path->lowest_level = level;
2017 ret = btrfs_search_slot(NULL, reloc_root, &key, path, 0, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002018 path->lowest_level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002019 if (ret < 0) {
2020 btrfs_free_path(path);
2021 return ret;
2022 }
2023
2024 btrfs_node_key_to_cpu(path->nodes[level], &next_key,
2025 path->slots[level]);
2026 WARN_ON(memcmp(&key, &next_key, sizeof(key)));
2027
2028 btrfs_unlock_up_safe(path, 0);
2029 }
2030
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002031 min_reserved = root->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002032 memset(&next_key, 0, sizeof(next_key));
2033
2034 while (1) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04002035 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002036 BUG_ON(IS_ERR(trans));
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002037 trans->block_rsv = rc->block_rsv;
2038
2039 ret = btrfs_block_rsv_check(trans, root, rc->block_rsv,
2040 min_reserved, 0);
2041 if (ret) {
2042 BUG_ON(ret != -EAGAIN);
2043 ret = btrfs_commit_transaction(trans, root);
2044 BUG_ON(ret);
2045 continue;
2046 }
2047
2048 replaced = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002049 max_level = level;
2050
2051 ret = walk_down_reloc_tree(reloc_root, path, &level);
2052 if (ret < 0) {
2053 err = ret;
2054 goto out;
2055 }
2056 if (ret > 0)
2057 break;
2058
2059 if (!find_next_key(path, level, &key) &&
2060 btrfs_comp_cpu_keys(&next_key, &key) >= 0) {
2061 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002062 } else {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002063 ret = replace_path(trans, root, reloc_root, path,
2064 &next_key, level, max_level);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002065 }
2066 if (ret < 0) {
2067 err = ret;
2068 goto out;
2069 }
2070
2071 if (ret > 0) {
2072 level = ret;
2073 btrfs_node_key_to_cpu(path->nodes[level], &key,
2074 path->slots[level]);
2075 replaced = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002076 }
2077
2078 ret = walk_up_reloc_tree(reloc_root, path, &level);
2079 if (ret > 0)
2080 break;
2081
2082 BUG_ON(level == 0);
2083 /*
2084 * save the merging progress in the drop_progress.
2085 * this is OK since root refs == 1 in this case.
2086 */
2087 btrfs_node_key(path->nodes[level], &root_item->drop_progress,
2088 path->slots[level]);
2089 root_item->drop_level = level;
2090
2091 nr = trans->blocks_used;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002092 btrfs_end_transaction_throttle(trans, root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002093
2094 btrfs_btree_balance_dirty(root, nr);
2095
2096 if (replaced && rc->stage == UPDATE_DATA_PTRS)
2097 invalidate_extent_cache(root, &key, &next_key);
2098 }
2099
2100 /*
2101 * handle the case only one block in the fs tree need to be
2102 * relocated and the block is tree root.
2103 */
2104 leaf = btrfs_lock_root_node(root);
2105 ret = btrfs_cow_block(trans, root, leaf, NULL, 0, &leaf);
2106 btrfs_tree_unlock(leaf);
2107 free_extent_buffer(leaf);
2108 if (ret < 0)
2109 err = ret;
2110out:
2111 btrfs_free_path(path);
2112
2113 if (err == 0) {
2114 memset(&root_item->drop_progress, 0,
2115 sizeof(root_item->drop_progress));
2116 root_item->drop_level = 0;
2117 btrfs_set_root_refs(root_item, 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002118 btrfs_update_reloc_root(trans, root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002119 }
2120
2121 nr = trans->blocks_used;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002122 btrfs_end_transaction_throttle(trans, root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002123
2124 btrfs_btree_balance_dirty(root, nr);
2125
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002126 if (replaced && rc->stage == UPDATE_DATA_PTRS)
2127 invalidate_extent_cache(root, &key, &next_key);
2128
2129 return err;
2130}
2131
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002132static noinline_for_stack
2133int prepare_to_merge(struct reloc_control *rc, int err)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002134{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002135 struct btrfs_root *root = rc->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002136 struct btrfs_root *reloc_root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002137 struct btrfs_trans_handle *trans;
2138 LIST_HEAD(reloc_roots);
2139 u64 num_bytes = 0;
2140 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002141
Josef Bacika4abeea2011-04-11 17:25:13 -04002142 spin_lock(&root->fs_info->trans_lock);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002143 rc->merging_rsv_size += root->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
2144 rc->merging_rsv_size += rc->nodes_relocated * 2;
Josef Bacika4abeea2011-04-11 17:25:13 -04002145 spin_unlock(&root->fs_info->trans_lock);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002146again:
2147 if (!err) {
2148 num_bytes = rc->merging_rsv_size;
2149 ret = btrfs_block_rsv_add(NULL, root, rc->block_rsv,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04002150 num_bytes);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002151 if (ret)
2152 err = ret;
2153 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002154
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002155 trans = btrfs_join_transaction(rc->extent_root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00002156 if (IS_ERR(trans)) {
2157 if (!err)
2158 btrfs_block_rsv_release(rc->extent_root,
2159 rc->block_rsv, num_bytes);
2160 return PTR_ERR(trans);
2161 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002162
2163 if (!err) {
2164 if (num_bytes != rc->merging_rsv_size) {
2165 btrfs_end_transaction(trans, rc->extent_root);
2166 btrfs_block_rsv_release(rc->extent_root,
2167 rc->block_rsv, num_bytes);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002168 goto again;
2169 }
2170 }
2171
2172 rc->merge_reloc_tree = 1;
2173
2174 while (!list_empty(&rc->reloc_roots)) {
2175 reloc_root = list_entry(rc->reloc_roots.next,
2176 struct btrfs_root, root_list);
2177 list_del_init(&reloc_root->root_list);
2178
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002179 root = read_fs_root(reloc_root->fs_info,
2180 reloc_root->root_key.offset);
2181 BUG_ON(IS_ERR(root));
2182 BUG_ON(root->reloc_root != reloc_root);
2183
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002184 /*
2185 * set reference count to 1, so btrfs_recover_relocation
2186 * knows it should resumes merging
2187 */
2188 if (!err)
2189 btrfs_set_root_refs(&reloc_root->root_item, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002190 btrfs_update_reloc_root(trans, root);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002191
2192 list_add(&reloc_root->root_list, &reloc_roots);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002193 }
2194
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002195 list_splice(&reloc_roots, &rc->reloc_roots);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002196
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002197 if (!err)
2198 btrfs_commit_transaction(trans, rc->extent_root);
2199 else
2200 btrfs_end_transaction(trans, rc->extent_root);
2201 return err;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002202}
2203
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002204static noinline_for_stack
2205int merge_reloc_roots(struct reloc_control *rc)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002206{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002207 struct btrfs_root *root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002208 struct btrfs_root *reloc_root;
2209 LIST_HEAD(reloc_roots);
2210 int found = 0;
2211 int ret;
2212again:
2213 root = rc->extent_root;
Josef Bacika4abeea2011-04-11 17:25:13 -04002214 spin_lock(&root->fs_info->trans_lock);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002215 list_splice_init(&rc->reloc_roots, &reloc_roots);
Josef Bacika4abeea2011-04-11 17:25:13 -04002216 spin_unlock(&root->fs_info->trans_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002217
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002218 while (!list_empty(&reloc_roots)) {
2219 found = 1;
2220 reloc_root = list_entry(reloc_roots.next,
2221 struct btrfs_root, root_list);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002222
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002223 if (btrfs_root_refs(&reloc_root->root_item) > 0) {
2224 root = read_fs_root(reloc_root->fs_info,
2225 reloc_root->root_key.offset);
2226 BUG_ON(IS_ERR(root));
2227 BUG_ON(root->reloc_root != reloc_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002228
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002229 ret = merge_reloc_root(rc, root);
2230 BUG_ON(ret);
2231 } else {
2232 list_del_init(&reloc_root->root_list);
2233 }
2234 btrfs_drop_snapshot(reloc_root, rc->block_rsv, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002235 }
2236
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002237 if (found) {
2238 found = 0;
2239 goto again;
2240 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002241 BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root));
2242 return 0;
2243}
2244
2245static void free_block_list(struct rb_root *blocks)
2246{
2247 struct tree_block *block;
2248 struct rb_node *rb_node;
2249 while ((rb_node = rb_first(blocks))) {
2250 block = rb_entry(rb_node, struct tree_block, rb_node);
2251 rb_erase(rb_node, blocks);
2252 kfree(block);
2253 }
2254}
2255
2256static int record_reloc_root_in_trans(struct btrfs_trans_handle *trans,
2257 struct btrfs_root *reloc_root)
2258{
2259 struct btrfs_root *root;
2260
2261 if (reloc_root->last_trans == trans->transid)
2262 return 0;
2263
2264 root = read_fs_root(reloc_root->fs_info, reloc_root->root_key.offset);
2265 BUG_ON(IS_ERR(root));
2266 BUG_ON(root->reloc_root != reloc_root);
2267
2268 return btrfs_record_root_in_trans(trans, root);
2269}
2270
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002271static noinline_for_stack
2272struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans,
2273 struct reloc_control *rc,
2274 struct backref_node *node,
2275 struct backref_edge *edges[], int *nr)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002276{
2277 struct backref_node *next;
2278 struct btrfs_root *root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002279 int index = 0;
2280
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002281 next = node;
2282 while (1) {
2283 cond_resched();
2284 next = walk_up_backref(next, edges, &index);
2285 root = next->root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002286 BUG_ON(!root);
2287 BUG_ON(!root->ref_cows);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002288
2289 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
2290 record_reloc_root_in_trans(trans, root);
2291 break;
2292 }
2293
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002294 btrfs_record_root_in_trans(trans, root);
2295 root = root->reloc_root;
2296
2297 if (next->new_bytenr != root->node->start) {
2298 BUG_ON(next->new_bytenr);
2299 BUG_ON(!list_empty(&next->list));
2300 next->new_bytenr = root->node->start;
2301 next->root = root;
2302 list_add_tail(&next->list,
2303 &rc->backref_cache.changed);
2304 __mark_block_processed(rc, next);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002305 break;
2306 }
2307
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002308 WARN_ON(1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002309 root = NULL;
2310 next = walk_down_backref(edges, &index);
2311 if (!next || next->level <= node->level)
2312 break;
2313 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002314 if (!root)
2315 return NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002316
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002317 *nr = index;
2318 next = node;
2319 /* setup backref node path for btrfs_reloc_cow_block */
2320 while (1) {
2321 rc->backref_cache.path[next->level] = next;
2322 if (--index < 0)
2323 break;
2324 next = edges[index]->node[UPPER];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002325 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002326 return root;
2327}
2328
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002329/*
2330 * select a tree root for relocation. return NULL if the block
2331 * is reference counted. we should use do_relocation() in this
2332 * case. return a tree root pointer if the block isn't reference
2333 * counted. return -ENOENT if the block is root of reloc tree.
2334 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002335static noinline_for_stack
2336struct btrfs_root *select_one_root(struct btrfs_trans_handle *trans,
2337 struct backref_node *node)
2338{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002339 struct backref_node *next;
2340 struct btrfs_root *root;
2341 struct btrfs_root *fs_root = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002342 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002343 int index = 0;
2344
2345 next = node;
2346 while (1) {
2347 cond_resched();
2348 next = walk_up_backref(next, edges, &index);
2349 root = next->root;
2350 BUG_ON(!root);
2351
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002352 /* no other choice for non-references counted tree */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002353 if (!root->ref_cows)
2354 return root;
2355
2356 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID)
2357 fs_root = root;
2358
2359 if (next != node)
2360 return NULL;
2361
2362 next = walk_down_backref(edges, &index);
2363 if (!next || next->level <= node->level)
2364 break;
2365 }
2366
2367 if (!fs_root)
2368 return ERR_PTR(-ENOENT);
2369 return fs_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002370}
2371
2372static noinline_for_stack
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002373u64 calcu_metadata_size(struct reloc_control *rc,
2374 struct backref_node *node, int reserve)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002375{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002376 struct backref_node *next = node;
2377 struct backref_edge *edge;
2378 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2379 u64 num_bytes = 0;
2380 int index = 0;
2381
2382 BUG_ON(reserve && node->processed);
2383
2384 while (next) {
2385 cond_resched();
2386 while (1) {
2387 if (next->processed && (reserve || next != node))
2388 break;
2389
2390 num_bytes += btrfs_level_size(rc->extent_root,
2391 next->level);
2392
2393 if (list_empty(&next->upper))
2394 break;
2395
2396 edge = list_entry(next->upper.next,
2397 struct backref_edge, list[LOWER]);
2398 edges[index++] = edge;
2399 next = edge->node[UPPER];
2400 }
2401 next = walk_down_backref(edges, &index);
2402 }
2403 return num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002404}
2405
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002406static int reserve_metadata_space(struct btrfs_trans_handle *trans,
2407 struct reloc_control *rc,
2408 struct backref_node *node)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002409{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002410 struct btrfs_root *root = rc->extent_root;
2411 u64 num_bytes;
2412 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002413
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002414 num_bytes = calcu_metadata_size(rc, node, 1) * 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002415
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002416 trans->block_rsv = rc->block_rsv;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04002417 ret = btrfs_block_rsv_add(trans, root, rc->block_rsv, num_bytes);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002418 if (ret) {
2419 if (ret == -EAGAIN)
2420 rc->commit_transaction = 1;
2421 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002422 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002423
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002424 return 0;
2425}
2426
2427static void release_metadata_space(struct reloc_control *rc,
2428 struct backref_node *node)
2429{
2430 u64 num_bytes = calcu_metadata_size(rc, node, 0) * 2;
2431 btrfs_block_rsv_release(rc->extent_root, rc->block_rsv, num_bytes);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002432}
2433
2434/*
2435 * relocate a block tree, and then update pointers in upper level
2436 * blocks that reference the block to point to the new location.
2437 *
2438 * if called by link_to_upper, the block has already been relocated.
2439 * in that case this function just updates pointers.
2440 */
2441static int do_relocation(struct btrfs_trans_handle *trans,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002442 struct reloc_control *rc,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002443 struct backref_node *node,
2444 struct btrfs_key *key,
2445 struct btrfs_path *path, int lowest)
2446{
2447 struct backref_node *upper;
2448 struct backref_edge *edge;
2449 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2450 struct btrfs_root *root;
2451 struct extent_buffer *eb;
2452 u32 blocksize;
2453 u64 bytenr;
2454 u64 generation;
2455 int nr;
2456 int slot;
2457 int ret;
2458 int err = 0;
2459
2460 BUG_ON(lowest && node->eb);
2461
2462 path->lowest_level = node->level + 1;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002463 rc->backref_cache.path[node->level] = node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002464 list_for_each_entry(edge, &node->upper, list[LOWER]) {
2465 cond_resched();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002466
2467 upper = edge->node[UPPER];
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002468 root = select_reloc_root(trans, rc, upper, edges, &nr);
2469 BUG_ON(!root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002470
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002471 if (upper->eb && !upper->locked) {
2472 if (!lowest) {
2473 ret = btrfs_bin_search(upper->eb, key,
2474 upper->level, &slot);
2475 BUG_ON(ret);
2476 bytenr = btrfs_node_blockptr(upper->eb, slot);
2477 if (node->eb->start == bytenr)
2478 goto next;
2479 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002480 drop_node_buffer(upper);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002481 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002482
2483 if (!upper->eb) {
2484 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
2485 if (ret < 0) {
2486 err = ret;
2487 break;
2488 }
2489 BUG_ON(ret > 0);
2490
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002491 if (!upper->eb) {
2492 upper->eb = path->nodes[upper->level];
2493 path->nodes[upper->level] = NULL;
2494 } else {
2495 BUG_ON(upper->eb != path->nodes[upper->level]);
2496 }
2497
2498 upper->locked = 1;
2499 path->locks[upper->level] = 0;
2500
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002501 slot = path->slots[upper->level];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002502 btrfs_release_path(NULL, path);
2503 } else {
2504 ret = btrfs_bin_search(upper->eb, key, upper->level,
2505 &slot);
2506 BUG_ON(ret);
2507 }
2508
2509 bytenr = btrfs_node_blockptr(upper->eb, slot);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002510 if (lowest) {
2511 BUG_ON(bytenr != node->bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002512 } else {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002513 if (node->eb->start == bytenr)
2514 goto next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002515 }
2516
2517 blocksize = btrfs_level_size(root, node->level);
2518 generation = btrfs_node_ptr_generation(upper->eb, slot);
2519 eb = read_tree_block(root, bytenr, blocksize, generation);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00002520 if (!eb) {
2521 err = -EIO;
2522 goto next;
2523 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002524 btrfs_tree_lock(eb);
2525 btrfs_set_lock_blocking(eb);
2526
2527 if (!node->eb) {
2528 ret = btrfs_cow_block(trans, root, eb, upper->eb,
2529 slot, &eb);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002530 btrfs_tree_unlock(eb);
2531 free_extent_buffer(eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002532 if (ret < 0) {
2533 err = ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002534 goto next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002535 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002536 BUG_ON(node->eb != eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002537 } else {
2538 btrfs_set_node_blockptr(upper->eb, slot,
2539 node->eb->start);
2540 btrfs_set_node_ptr_generation(upper->eb, slot,
2541 trans->transid);
2542 btrfs_mark_buffer_dirty(upper->eb);
2543
2544 ret = btrfs_inc_extent_ref(trans, root,
2545 node->eb->start, blocksize,
2546 upper->eb->start,
2547 btrfs_header_owner(upper->eb),
2548 node->level, 0);
2549 BUG_ON(ret);
2550
2551 ret = btrfs_drop_subtree(trans, root, eb, upper->eb);
2552 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002553 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002554next:
2555 if (!upper->pending)
2556 drop_node_buffer(upper);
2557 else
2558 unlock_node_buffer(upper);
2559 if (err)
2560 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002561 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002562
2563 if (!err && node->pending) {
2564 drop_node_buffer(node);
2565 list_move_tail(&node->list, &rc->backref_cache.changed);
2566 node->pending = 0;
2567 }
2568
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002569 path->lowest_level = 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002570 BUG_ON(err == -ENOSPC);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002571 return err;
2572}
2573
2574static int link_to_upper(struct btrfs_trans_handle *trans,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002575 struct reloc_control *rc,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002576 struct backref_node *node,
2577 struct btrfs_path *path)
2578{
2579 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002580
2581 btrfs_node_key_to_cpu(node->eb, &key, 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002582 return do_relocation(trans, rc, node, &key, path, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002583}
2584
2585static int finish_pending_nodes(struct btrfs_trans_handle *trans,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002586 struct reloc_control *rc,
2587 struct btrfs_path *path, int err)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002588{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002589 LIST_HEAD(list);
2590 struct backref_cache *cache = &rc->backref_cache;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002591 struct backref_node *node;
2592 int level;
2593 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002594
2595 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2596 while (!list_empty(&cache->pending[level])) {
2597 node = list_entry(cache->pending[level].next,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002598 struct backref_node, list);
2599 list_move_tail(&node->list, &list);
2600 BUG_ON(!node->pending);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002601
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002602 if (!err) {
2603 ret = link_to_upper(trans, rc, node, path);
2604 if (ret < 0)
2605 err = ret;
2606 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002607 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002608 list_splice_init(&list, &cache->pending[level]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002609 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002610 return err;
2611}
2612
2613static void mark_block_processed(struct reloc_control *rc,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002614 u64 bytenr, u32 blocksize)
2615{
2616 set_extent_bits(&rc->processed_blocks, bytenr, bytenr + blocksize - 1,
2617 EXTENT_DIRTY, GFP_NOFS);
2618}
2619
2620static void __mark_block_processed(struct reloc_control *rc,
2621 struct backref_node *node)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002622{
2623 u32 blocksize;
2624 if (node->level == 0 ||
2625 in_block_group(node->bytenr, rc->block_group)) {
2626 blocksize = btrfs_level_size(rc->extent_root, node->level);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002627 mark_block_processed(rc, node->bytenr, blocksize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002628 }
2629 node->processed = 1;
2630}
2631
2632/*
2633 * mark a block and all blocks directly/indirectly reference the block
2634 * as processed.
2635 */
2636static void update_processed_blocks(struct reloc_control *rc,
2637 struct backref_node *node)
2638{
2639 struct backref_node *next = node;
2640 struct backref_edge *edge;
2641 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2642 int index = 0;
2643
2644 while (next) {
2645 cond_resched();
2646 while (1) {
2647 if (next->processed)
2648 break;
2649
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002650 __mark_block_processed(rc, next);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002651
2652 if (list_empty(&next->upper))
2653 break;
2654
2655 edge = list_entry(next->upper.next,
2656 struct backref_edge, list[LOWER]);
2657 edges[index++] = edge;
2658 next = edge->node[UPPER];
2659 }
2660 next = walk_down_backref(edges, &index);
2661 }
2662}
2663
2664static int tree_block_processed(u64 bytenr, u32 blocksize,
2665 struct reloc_control *rc)
2666{
2667 if (test_range_bit(&rc->processed_blocks, bytenr,
Chris Mason9655d292009-09-02 15:22:30 -04002668 bytenr + blocksize - 1, EXTENT_DIRTY, 1, NULL))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002669 return 1;
2670 return 0;
2671}
2672
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002673static int get_tree_block_key(struct reloc_control *rc,
2674 struct tree_block *block)
2675{
2676 struct extent_buffer *eb;
2677
2678 BUG_ON(block->key_ready);
2679 eb = read_tree_block(rc->extent_root, block->bytenr,
2680 block->key.objectid, block->key.offset);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00002681 BUG_ON(!eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002682 WARN_ON(btrfs_header_level(eb) != block->level);
2683 if (block->level == 0)
2684 btrfs_item_key_to_cpu(eb, &block->key, 0);
2685 else
2686 btrfs_node_key_to_cpu(eb, &block->key, 0);
2687 free_extent_buffer(eb);
2688 block->key_ready = 1;
2689 return 0;
2690}
2691
2692static int reada_tree_block(struct reloc_control *rc,
2693 struct tree_block *block)
2694{
2695 BUG_ON(block->key_ready);
2696 readahead_tree_block(rc->extent_root, block->bytenr,
2697 block->key.objectid, block->key.offset);
2698 return 0;
2699}
2700
2701/*
2702 * helper function to relocate a tree block
2703 */
2704static int relocate_tree_block(struct btrfs_trans_handle *trans,
2705 struct reloc_control *rc,
2706 struct backref_node *node,
2707 struct btrfs_key *key,
2708 struct btrfs_path *path)
2709{
2710 struct btrfs_root *root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002711 int release = 0;
2712 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002713
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002714 if (!node)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002715 return 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002716
2717 BUG_ON(node->processed);
2718 root = select_one_root(trans, node);
2719 if (root == ERR_PTR(-ENOENT)) {
2720 update_processed_blocks(rc, node);
2721 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002722 }
2723
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002724 if (!root || root->ref_cows) {
2725 ret = reserve_metadata_space(trans, rc, node);
2726 if (ret)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002727 goto out;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002728 release = 1;
2729 }
2730
2731 if (root) {
2732 if (root->ref_cows) {
2733 BUG_ON(node->new_bytenr);
2734 BUG_ON(!list_empty(&node->list));
2735 btrfs_record_root_in_trans(trans, root);
2736 root = root->reloc_root;
2737 node->new_bytenr = root->node->start;
2738 node->root = root;
2739 list_add_tail(&node->list, &rc->backref_cache.changed);
2740 } else {
2741 path->lowest_level = node->level;
2742 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
2743 btrfs_release_path(root, path);
2744 if (ret > 0)
2745 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002746 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002747 if (!ret)
2748 update_processed_blocks(rc, node);
2749 } else {
2750 ret = do_relocation(trans, rc, node, key, path, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002751 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002752out:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002753 if (ret || node->level == 0 || node->cowonly) {
2754 if (release)
2755 release_metadata_space(rc, node);
2756 remove_backref_node(&rc->backref_cache, node);
2757 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002758 return ret;
2759}
2760
2761/*
2762 * relocate a list of blocks
2763 */
2764static noinline_for_stack
2765int relocate_tree_blocks(struct btrfs_trans_handle *trans,
2766 struct reloc_control *rc, struct rb_root *blocks)
2767{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002768 struct backref_node *node;
2769 struct btrfs_path *path;
2770 struct tree_block *block;
2771 struct rb_node *rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002772 int ret;
2773 int err = 0;
2774
2775 path = btrfs_alloc_path();
2776 if (!path)
2777 return -ENOMEM;
2778
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002779 rb_node = rb_first(blocks);
2780 while (rb_node) {
2781 block = rb_entry(rb_node, struct tree_block, rb_node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002782 if (!block->key_ready)
2783 reada_tree_block(rc, block);
2784 rb_node = rb_next(rb_node);
2785 }
2786
2787 rb_node = rb_first(blocks);
2788 while (rb_node) {
2789 block = rb_entry(rb_node, struct tree_block, rb_node);
2790 if (!block->key_ready)
2791 get_tree_block_key(rc, block);
2792 rb_node = rb_next(rb_node);
2793 }
2794
2795 rb_node = rb_first(blocks);
2796 while (rb_node) {
2797 block = rb_entry(rb_node, struct tree_block, rb_node);
2798
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002799 node = build_backref_tree(rc, &block->key,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002800 block->level, block->bytenr);
2801 if (IS_ERR(node)) {
2802 err = PTR_ERR(node);
2803 goto out;
2804 }
2805
2806 ret = relocate_tree_block(trans, rc, node, &block->key,
2807 path);
2808 if (ret < 0) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002809 if (ret != -EAGAIN || rb_node == rb_first(blocks))
2810 err = ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002811 goto out;
2812 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002813 rb_node = rb_next(rb_node);
2814 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002815out:
2816 free_block_list(blocks);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002817 err = finish_pending_nodes(trans, rc, path, err);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002818
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002819 btrfs_free_path(path);
2820 return err;
2821}
2822
2823static noinline_for_stack
Yan, Zhengefa56462010-05-16 10:49:59 -04002824int prealloc_file_extent_cluster(struct inode *inode,
2825 struct file_extent_cluster *cluster)
2826{
2827 u64 alloc_hint = 0;
2828 u64 start;
2829 u64 end;
2830 u64 offset = BTRFS_I(inode)->index_cnt;
2831 u64 num_bytes;
2832 int nr = 0;
2833 int ret = 0;
2834
2835 BUG_ON(cluster->start != cluster->boundary[0]);
2836 mutex_lock(&inode->i_mutex);
2837
2838 ret = btrfs_check_data_free_space(inode, cluster->end +
2839 1 - cluster->start);
2840 if (ret)
2841 goto out;
2842
2843 while (nr < cluster->nr) {
2844 start = cluster->boundary[nr] - offset;
2845 if (nr + 1 < cluster->nr)
2846 end = cluster->boundary[nr + 1] - 1 - offset;
2847 else
2848 end = cluster->end - offset;
2849
2850 lock_extent(&BTRFS_I(inode)->io_tree, start, end, GFP_NOFS);
2851 num_bytes = end + 1 - start;
2852 ret = btrfs_prealloc_file_range(inode, 0, start,
2853 num_bytes, num_bytes,
2854 end + 1, &alloc_hint);
2855 unlock_extent(&BTRFS_I(inode)->io_tree, start, end, GFP_NOFS);
2856 if (ret)
2857 break;
2858 nr++;
2859 }
2860 btrfs_free_reserved_data_space(inode, cluster->end +
2861 1 - cluster->start);
2862out:
2863 mutex_unlock(&inode->i_mutex);
2864 return ret;
2865}
2866
2867static noinline_for_stack
Yan, Zheng0257bb82009-09-24 09:17:31 -04002868int setup_extent_mapping(struct inode *inode, u64 start, u64 end,
2869 u64 block_start)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002870{
2871 struct btrfs_root *root = BTRFS_I(inode)->root;
2872 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2873 struct extent_map *em;
Yan, Zheng0257bb82009-09-24 09:17:31 -04002874 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002875
2876 em = alloc_extent_map(GFP_NOFS);
Yan, Zheng0257bb82009-09-24 09:17:31 -04002877 if (!em)
2878 return -ENOMEM;
2879
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002880 em->start = start;
Yan, Zheng0257bb82009-09-24 09:17:31 -04002881 em->len = end + 1 - start;
2882 em->block_len = em->len;
2883 em->block_start = block_start;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002884 em->bdev = root->fs_info->fs_devices->latest_bdev;
2885 set_bit(EXTENT_FLAG_PINNED, &em->flags);
2886
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002887 lock_extent(&BTRFS_I(inode)->io_tree, start, end, GFP_NOFS);
2888 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04002889 write_lock(&em_tree->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002890 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002891 write_unlock(&em_tree->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002892 if (ret != -EEXIST) {
2893 free_extent_map(em);
2894 break;
2895 }
2896 btrfs_drop_extent_cache(inode, start, end, 0);
2897 }
2898 unlock_extent(&BTRFS_I(inode)->io_tree, start, end, GFP_NOFS);
Yan, Zheng0257bb82009-09-24 09:17:31 -04002899 return ret;
2900}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002901
Yan, Zheng0257bb82009-09-24 09:17:31 -04002902static int relocate_file_extent_cluster(struct inode *inode,
2903 struct file_extent_cluster *cluster)
2904{
2905 u64 page_start;
2906 u64 page_end;
2907 u64 offset = BTRFS_I(inode)->index_cnt;
2908 unsigned long index;
2909 unsigned long last_index;
Yan, Zheng0257bb82009-09-24 09:17:31 -04002910 struct page *page;
2911 struct file_ra_state *ra;
2912 int nr = 0;
2913 int ret = 0;
2914
2915 if (!cluster->nr)
2916 return 0;
2917
2918 ra = kzalloc(sizeof(*ra), GFP_NOFS);
2919 if (!ra)
2920 return -ENOMEM;
2921
Yan, Zhengefa56462010-05-16 10:49:59 -04002922 ret = prealloc_file_extent_cluster(inode, cluster);
Yan, Zheng0257bb82009-09-24 09:17:31 -04002923 if (ret)
Yan, Zhengefa56462010-05-16 10:49:59 -04002924 goto out;
Yan, Zheng0257bb82009-09-24 09:17:31 -04002925
2926 file_ra_state_init(ra, inode->i_mapping);
2927
Yan, Zhengefa56462010-05-16 10:49:59 -04002928 ret = setup_extent_mapping(inode, cluster->start - offset,
2929 cluster->end - offset, cluster->start);
2930 if (ret)
2931 goto out;
2932
2933 index = (cluster->start - offset) >> PAGE_CACHE_SHIFT;
2934 last_index = (cluster->end - offset) >> PAGE_CACHE_SHIFT;
Yan, Zheng0257bb82009-09-24 09:17:31 -04002935 while (index <= last_index) {
Yan, Zhengefa56462010-05-16 10:49:59 -04002936 ret = btrfs_delalloc_reserve_metadata(inode, PAGE_CACHE_SIZE);
2937 if (ret)
2938 goto out;
2939
Yan, Zheng0257bb82009-09-24 09:17:31 -04002940 page = find_lock_page(inode->i_mapping, index);
2941 if (!page) {
2942 page_cache_sync_readahead(inode->i_mapping,
2943 ra, NULL, index,
2944 last_index + 1 - index);
2945 page = grab_cache_page(inode->i_mapping, index);
2946 if (!page) {
Yan, Zhengefa56462010-05-16 10:49:59 -04002947 btrfs_delalloc_release_metadata(inode,
2948 PAGE_CACHE_SIZE);
Yan, Zheng0257bb82009-09-24 09:17:31 -04002949 ret = -ENOMEM;
Yan, Zhengefa56462010-05-16 10:49:59 -04002950 goto out;
Yan, Zheng0257bb82009-09-24 09:17:31 -04002951 }
2952 }
2953
2954 if (PageReadahead(page)) {
2955 page_cache_async_readahead(inode->i_mapping,
2956 ra, NULL, page, index,
2957 last_index + 1 - index);
2958 }
2959
2960 if (!PageUptodate(page)) {
2961 btrfs_readpage(NULL, page);
2962 lock_page(page);
2963 if (!PageUptodate(page)) {
2964 unlock_page(page);
2965 page_cache_release(page);
Yan, Zhengefa56462010-05-16 10:49:59 -04002966 btrfs_delalloc_release_metadata(inode,
2967 PAGE_CACHE_SIZE);
Yan, Zheng0257bb82009-09-24 09:17:31 -04002968 ret = -EIO;
Yan, Zhengefa56462010-05-16 10:49:59 -04002969 goto out;
Yan, Zheng0257bb82009-09-24 09:17:31 -04002970 }
2971 }
2972
2973 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2974 page_end = page_start + PAGE_CACHE_SIZE - 1;
2975
2976 lock_extent(&BTRFS_I(inode)->io_tree,
2977 page_start, page_end, GFP_NOFS);
2978
2979 set_page_extent_mapped(page);
2980
2981 if (nr < cluster->nr &&
2982 page_start + offset == cluster->boundary[nr]) {
2983 set_extent_bits(&BTRFS_I(inode)->io_tree,
2984 page_start, page_end,
2985 EXTENT_BOUNDARY, GFP_NOFS);
2986 nr++;
2987 }
Yan, Zheng0257bb82009-09-24 09:17:31 -04002988
Yan, Zhengefa56462010-05-16 10:49:59 -04002989 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
Yan, Zheng0257bb82009-09-24 09:17:31 -04002990 set_page_dirty(page);
Yan, Zheng0257bb82009-09-24 09:17:31 -04002991
2992 unlock_extent(&BTRFS_I(inode)->io_tree,
2993 page_start, page_end, GFP_NOFS);
2994 unlock_page(page);
2995 page_cache_release(page);
2996
2997 index++;
Yan, Zhengefa56462010-05-16 10:49:59 -04002998 balance_dirty_pages_ratelimited(inode->i_mapping);
2999 btrfs_throttle(BTRFS_I(inode)->root);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003000 }
3001 WARN_ON(nr != cluster->nr);
Yan, Zhengefa56462010-05-16 10:49:59 -04003002out:
Yan, Zheng0257bb82009-09-24 09:17:31 -04003003 kfree(ra);
3004 return ret;
3005}
3006
3007static noinline_for_stack
3008int relocate_data_extent(struct inode *inode, struct btrfs_key *extent_key,
3009 struct file_extent_cluster *cluster)
3010{
3011 int ret;
3012
3013 if (cluster->nr > 0 && extent_key->objectid != cluster->end + 1) {
3014 ret = relocate_file_extent_cluster(inode, cluster);
3015 if (ret)
3016 return ret;
3017 cluster->nr = 0;
3018 }
3019
3020 if (!cluster->nr)
3021 cluster->start = extent_key->objectid;
3022 else
3023 BUG_ON(cluster->nr >= MAX_EXTENTS);
3024 cluster->end = extent_key->objectid + extent_key->offset - 1;
3025 cluster->boundary[cluster->nr] = extent_key->objectid;
3026 cluster->nr++;
3027
3028 if (cluster->nr >= MAX_EXTENTS) {
3029 ret = relocate_file_extent_cluster(inode, cluster);
3030 if (ret)
3031 return ret;
3032 cluster->nr = 0;
3033 }
3034 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003035}
3036
3037#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3038static int get_ref_objectid_v0(struct reloc_control *rc,
3039 struct btrfs_path *path,
3040 struct btrfs_key *extent_key,
3041 u64 *ref_objectid, int *path_change)
3042{
3043 struct btrfs_key key;
3044 struct extent_buffer *leaf;
3045 struct btrfs_extent_ref_v0 *ref0;
3046 int ret;
3047 int slot;
3048
3049 leaf = path->nodes[0];
3050 slot = path->slots[0];
3051 while (1) {
3052 if (slot >= btrfs_header_nritems(leaf)) {
3053 ret = btrfs_next_leaf(rc->extent_root, path);
3054 if (ret < 0)
3055 return ret;
3056 BUG_ON(ret > 0);
3057 leaf = path->nodes[0];
3058 slot = path->slots[0];
3059 if (path_change)
3060 *path_change = 1;
3061 }
3062 btrfs_item_key_to_cpu(leaf, &key, slot);
3063 if (key.objectid != extent_key->objectid)
3064 return -ENOENT;
3065
3066 if (key.type != BTRFS_EXTENT_REF_V0_KEY) {
3067 slot++;
3068 continue;
3069 }
3070 ref0 = btrfs_item_ptr(leaf, slot,
3071 struct btrfs_extent_ref_v0);
3072 *ref_objectid = btrfs_ref_objectid_v0(leaf, ref0);
3073 break;
3074 }
3075 return 0;
3076}
3077#endif
3078
3079/*
3080 * helper to add a tree block to the list.
3081 * the major work is getting the generation and level of the block
3082 */
3083static int add_tree_block(struct reloc_control *rc,
3084 struct btrfs_key *extent_key,
3085 struct btrfs_path *path,
3086 struct rb_root *blocks)
3087{
3088 struct extent_buffer *eb;
3089 struct btrfs_extent_item *ei;
3090 struct btrfs_tree_block_info *bi;
3091 struct tree_block *block;
3092 struct rb_node *rb_node;
3093 u32 item_size;
3094 int level = -1;
3095 int generation;
3096
3097 eb = path->nodes[0];
3098 item_size = btrfs_item_size_nr(eb, path->slots[0]);
3099
3100 if (item_size >= sizeof(*ei) + sizeof(*bi)) {
3101 ei = btrfs_item_ptr(eb, path->slots[0],
3102 struct btrfs_extent_item);
3103 bi = (struct btrfs_tree_block_info *)(ei + 1);
3104 generation = btrfs_extent_generation(eb, ei);
3105 level = btrfs_tree_block_level(eb, bi);
3106 } else {
3107#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3108 u64 ref_owner;
3109 int ret;
3110
3111 BUG_ON(item_size != sizeof(struct btrfs_extent_item_v0));
3112 ret = get_ref_objectid_v0(rc, path, extent_key,
3113 &ref_owner, NULL);
Andi Kleen411fc6b2010-10-29 15:14:31 -04003114 if (ret < 0)
3115 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003116 BUG_ON(ref_owner >= BTRFS_MAX_LEVEL);
3117 level = (int)ref_owner;
3118 /* FIXME: get real generation */
3119 generation = 0;
3120#else
3121 BUG();
3122#endif
3123 }
3124
3125 btrfs_release_path(rc->extent_root, path);
3126
3127 BUG_ON(level == -1);
3128
3129 block = kmalloc(sizeof(*block), GFP_NOFS);
3130 if (!block)
3131 return -ENOMEM;
3132
3133 block->bytenr = extent_key->objectid;
3134 block->key.objectid = extent_key->offset;
3135 block->key.offset = generation;
3136 block->level = level;
3137 block->key_ready = 0;
3138
3139 rb_node = tree_insert(blocks, block->bytenr, &block->rb_node);
3140 BUG_ON(rb_node);
3141
3142 return 0;
3143}
3144
3145/*
3146 * helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY
3147 */
3148static int __add_tree_block(struct reloc_control *rc,
3149 u64 bytenr, u32 blocksize,
3150 struct rb_root *blocks)
3151{
3152 struct btrfs_path *path;
3153 struct btrfs_key key;
3154 int ret;
3155
3156 if (tree_block_processed(bytenr, blocksize, rc))
3157 return 0;
3158
3159 if (tree_search(blocks, bytenr))
3160 return 0;
3161
3162 path = btrfs_alloc_path();
3163 if (!path)
3164 return -ENOMEM;
3165
3166 key.objectid = bytenr;
3167 key.type = BTRFS_EXTENT_ITEM_KEY;
3168 key.offset = blocksize;
3169
3170 path->search_commit_root = 1;
3171 path->skip_locking = 1;
3172 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path, 0, 0);
3173 if (ret < 0)
3174 goto out;
3175 BUG_ON(ret);
3176
3177 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3178 ret = add_tree_block(rc, &key, path, blocks);
3179out:
3180 btrfs_free_path(path);
3181 return ret;
3182}
3183
3184/*
3185 * helper to check if the block use full backrefs for pointers in it
3186 */
3187static int block_use_full_backref(struct reloc_control *rc,
3188 struct extent_buffer *eb)
3189{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003190 u64 flags;
3191 int ret;
3192
3193 if (btrfs_header_flag(eb, BTRFS_HEADER_FLAG_RELOC) ||
3194 btrfs_header_backref_rev(eb) < BTRFS_MIXED_BACKREF_REV)
3195 return 1;
3196
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003197 ret = btrfs_lookup_extent_info(NULL, rc->extent_root,
3198 eb->start, eb->len, NULL, &flags);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003199 BUG_ON(ret);
3200
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003201 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
3202 ret = 1;
3203 else
3204 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003205 return ret;
3206}
3207
Josef Bacik0af3d002010-06-21 14:48:16 -04003208static int delete_block_group_cache(struct btrfs_fs_info *fs_info,
3209 struct inode *inode, u64 ino)
3210{
3211 struct btrfs_key key;
3212 struct btrfs_path *path;
3213 struct btrfs_root *root = fs_info->tree_root;
3214 struct btrfs_trans_handle *trans;
3215 unsigned long nr;
3216 int ret = 0;
3217
3218 if (inode)
3219 goto truncate;
3220
3221 key.objectid = ino;
3222 key.type = BTRFS_INODE_ITEM_KEY;
3223 key.offset = 0;
3224
3225 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
3226 if (!inode || IS_ERR(inode) || is_bad_inode(inode)) {
3227 if (inode && !IS_ERR(inode))
3228 iput(inode);
3229 return -ENOENT;
3230 }
3231
3232truncate:
3233 path = btrfs_alloc_path();
3234 if (!path) {
3235 ret = -ENOMEM;
3236 goto out;
3237 }
3238
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003239 trans = btrfs_join_transaction(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04003240 if (IS_ERR(trans)) {
3241 btrfs_free_path(path);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00003242 ret = PTR_ERR(trans);
Josef Bacik0af3d002010-06-21 14:48:16 -04003243 goto out;
3244 }
3245
3246 ret = btrfs_truncate_free_space_cache(root, trans, path, inode);
3247
3248 btrfs_free_path(path);
3249 nr = trans->blocks_used;
3250 btrfs_end_transaction(trans, root);
3251 btrfs_btree_balance_dirty(root, nr);
3252out:
3253 iput(inode);
3254 return ret;
3255}
3256
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003257/*
3258 * helper to add tree blocks for backref of type BTRFS_EXTENT_DATA_REF_KEY
3259 * this function scans fs tree to find blocks reference the data extent
3260 */
3261static int find_data_references(struct reloc_control *rc,
3262 struct btrfs_key *extent_key,
3263 struct extent_buffer *leaf,
3264 struct btrfs_extent_data_ref *ref,
3265 struct rb_root *blocks)
3266{
3267 struct btrfs_path *path;
3268 struct tree_block *block;
3269 struct btrfs_root *root;
3270 struct btrfs_file_extent_item *fi;
3271 struct rb_node *rb_node;
3272 struct btrfs_key key;
3273 u64 ref_root;
3274 u64 ref_objectid;
3275 u64 ref_offset;
3276 u32 ref_count;
3277 u32 nritems;
3278 int err = 0;
3279 int added = 0;
3280 int counted;
3281 int ret;
3282
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003283 ref_root = btrfs_extent_data_ref_root(leaf, ref);
3284 ref_objectid = btrfs_extent_data_ref_objectid(leaf, ref);
3285 ref_offset = btrfs_extent_data_ref_offset(leaf, ref);
3286 ref_count = btrfs_extent_data_ref_count(leaf, ref);
3287
Josef Bacik0af3d002010-06-21 14:48:16 -04003288 /*
3289 * This is an extent belonging to the free space cache, lets just delete
3290 * it and redo the search.
3291 */
3292 if (ref_root == BTRFS_ROOT_TREE_OBJECTID) {
3293 ret = delete_block_group_cache(rc->extent_root->fs_info,
3294 NULL, ref_objectid);
3295 if (ret != -ENOENT)
3296 return ret;
3297 ret = 0;
3298 }
3299
3300 path = btrfs_alloc_path();
3301 if (!path)
3302 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04003303 path->reada = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04003304
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003305 root = read_fs_root(rc->extent_root->fs_info, ref_root);
3306 if (IS_ERR(root)) {
3307 err = PTR_ERR(root);
3308 goto out;
3309 }
3310
3311 key.objectid = ref_objectid;
3312 key.offset = ref_offset;
3313 key.type = BTRFS_EXTENT_DATA_KEY;
3314
3315 path->search_commit_root = 1;
3316 path->skip_locking = 1;
3317 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3318 if (ret < 0) {
3319 err = ret;
3320 goto out;
3321 }
3322
3323 leaf = path->nodes[0];
3324 nritems = btrfs_header_nritems(leaf);
3325 /*
3326 * the references in tree blocks that use full backrefs
3327 * are not counted in
3328 */
3329 if (block_use_full_backref(rc, leaf))
3330 counted = 0;
3331 else
3332 counted = 1;
3333 rb_node = tree_search(blocks, leaf->start);
3334 if (rb_node) {
3335 if (counted)
3336 added = 1;
3337 else
3338 path->slots[0] = nritems;
3339 }
3340
3341 while (ref_count > 0) {
3342 while (path->slots[0] >= nritems) {
3343 ret = btrfs_next_leaf(root, path);
3344 if (ret < 0) {
3345 err = ret;
3346 goto out;
3347 }
3348 if (ret > 0) {
3349 WARN_ON(1);
3350 goto out;
3351 }
3352
3353 leaf = path->nodes[0];
3354 nritems = btrfs_header_nritems(leaf);
3355 added = 0;
3356
3357 if (block_use_full_backref(rc, leaf))
3358 counted = 0;
3359 else
3360 counted = 1;
3361 rb_node = tree_search(blocks, leaf->start);
3362 if (rb_node) {
3363 if (counted)
3364 added = 1;
3365 else
3366 path->slots[0] = nritems;
3367 }
3368 }
3369
3370 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3371 if (key.objectid != ref_objectid ||
3372 key.type != BTRFS_EXTENT_DATA_KEY) {
3373 WARN_ON(1);
3374 break;
3375 }
3376
3377 fi = btrfs_item_ptr(leaf, path->slots[0],
3378 struct btrfs_file_extent_item);
3379
3380 if (btrfs_file_extent_type(leaf, fi) ==
3381 BTRFS_FILE_EXTENT_INLINE)
3382 goto next;
3383
3384 if (btrfs_file_extent_disk_bytenr(leaf, fi) !=
3385 extent_key->objectid)
3386 goto next;
3387
3388 key.offset -= btrfs_file_extent_offset(leaf, fi);
3389 if (key.offset != ref_offset)
3390 goto next;
3391
3392 if (counted)
3393 ref_count--;
3394 if (added)
3395 goto next;
3396
3397 if (!tree_block_processed(leaf->start, leaf->len, rc)) {
3398 block = kmalloc(sizeof(*block), GFP_NOFS);
3399 if (!block) {
3400 err = -ENOMEM;
3401 break;
3402 }
3403 block->bytenr = leaf->start;
3404 btrfs_item_key_to_cpu(leaf, &block->key, 0);
3405 block->level = 0;
3406 block->key_ready = 1;
3407 rb_node = tree_insert(blocks, block->bytenr,
3408 &block->rb_node);
3409 BUG_ON(rb_node);
3410 }
3411 if (counted)
3412 added = 1;
3413 else
3414 path->slots[0] = nritems;
3415next:
3416 path->slots[0]++;
3417
3418 }
3419out:
3420 btrfs_free_path(path);
3421 return err;
3422}
3423
3424/*
3425 * hepler to find all tree blocks that reference a given data extent
3426 */
3427static noinline_for_stack
3428int add_data_references(struct reloc_control *rc,
3429 struct btrfs_key *extent_key,
3430 struct btrfs_path *path,
3431 struct rb_root *blocks)
3432{
3433 struct btrfs_key key;
3434 struct extent_buffer *eb;
3435 struct btrfs_extent_data_ref *dref;
3436 struct btrfs_extent_inline_ref *iref;
3437 unsigned long ptr;
3438 unsigned long end;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003439 u32 blocksize = btrfs_level_size(rc->extent_root, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003440 int ret;
3441 int err = 0;
3442
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003443 eb = path->nodes[0];
3444 ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
3445 end = ptr + btrfs_item_size_nr(eb, path->slots[0]);
3446#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3447 if (ptr + sizeof(struct btrfs_extent_item_v0) == end)
3448 ptr = end;
3449 else
3450#endif
3451 ptr += sizeof(struct btrfs_extent_item);
3452
3453 while (ptr < end) {
3454 iref = (struct btrfs_extent_inline_ref *)ptr;
3455 key.type = btrfs_extent_inline_ref_type(eb, iref);
3456 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
3457 key.offset = btrfs_extent_inline_ref_offset(eb, iref);
3458 ret = __add_tree_block(rc, key.offset, blocksize,
3459 blocks);
3460 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
3461 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
3462 ret = find_data_references(rc, extent_key,
3463 eb, dref, blocks);
3464 } else {
3465 BUG();
3466 }
3467 ptr += btrfs_extent_inline_ref_size(key.type);
3468 }
3469 WARN_ON(ptr > end);
3470
3471 while (1) {
3472 cond_resched();
3473 eb = path->nodes[0];
3474 if (path->slots[0] >= btrfs_header_nritems(eb)) {
3475 ret = btrfs_next_leaf(rc->extent_root, path);
3476 if (ret < 0) {
3477 err = ret;
3478 break;
3479 }
3480 if (ret > 0)
3481 break;
3482 eb = path->nodes[0];
3483 }
3484
3485 btrfs_item_key_to_cpu(eb, &key, path->slots[0]);
3486 if (key.objectid != extent_key->objectid)
3487 break;
3488
3489#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3490 if (key.type == BTRFS_SHARED_DATA_REF_KEY ||
3491 key.type == BTRFS_EXTENT_REF_V0_KEY) {
3492#else
3493 BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY);
3494 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
3495#endif
3496 ret = __add_tree_block(rc, key.offset, blocksize,
3497 blocks);
3498 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
3499 dref = btrfs_item_ptr(eb, path->slots[0],
3500 struct btrfs_extent_data_ref);
3501 ret = find_data_references(rc, extent_key,
3502 eb, dref, blocks);
3503 } else {
3504 ret = 0;
3505 }
3506 if (ret) {
3507 err = ret;
3508 break;
3509 }
3510 path->slots[0]++;
3511 }
3512 btrfs_release_path(rc->extent_root, path);
3513 if (err)
3514 free_block_list(blocks);
3515 return err;
3516}
3517
3518/*
3519 * hepler to find next unprocessed extent
3520 */
3521static noinline_for_stack
3522int find_next_extent(struct btrfs_trans_handle *trans,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003523 struct reloc_control *rc, struct btrfs_path *path,
3524 struct btrfs_key *extent_key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003525{
3526 struct btrfs_key key;
3527 struct extent_buffer *leaf;
3528 u64 start, end, last;
3529 int ret;
3530
3531 last = rc->block_group->key.objectid + rc->block_group->key.offset;
3532 while (1) {
3533 cond_resched();
3534 if (rc->search_start >= last) {
3535 ret = 1;
3536 break;
3537 }
3538
3539 key.objectid = rc->search_start;
3540 key.type = BTRFS_EXTENT_ITEM_KEY;
3541 key.offset = 0;
3542
3543 path->search_commit_root = 1;
3544 path->skip_locking = 1;
3545 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path,
3546 0, 0);
3547 if (ret < 0)
3548 break;
3549next:
3550 leaf = path->nodes[0];
3551 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
3552 ret = btrfs_next_leaf(rc->extent_root, path);
3553 if (ret != 0)
3554 break;
3555 leaf = path->nodes[0];
3556 }
3557
3558 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3559 if (key.objectid >= last) {
3560 ret = 1;
3561 break;
3562 }
3563
3564 if (key.type != BTRFS_EXTENT_ITEM_KEY ||
3565 key.objectid + key.offset <= rc->search_start) {
3566 path->slots[0]++;
3567 goto next;
3568 }
3569
3570 ret = find_first_extent_bit(&rc->processed_blocks,
3571 key.objectid, &start, &end,
3572 EXTENT_DIRTY);
3573
3574 if (ret == 0 && start <= key.objectid) {
3575 btrfs_release_path(rc->extent_root, path);
3576 rc->search_start = end + 1;
3577 } else {
3578 rc->search_start = key.objectid + key.offset;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003579 memcpy(extent_key, &key, sizeof(key));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003580 return 0;
3581 }
3582 }
3583 btrfs_release_path(rc->extent_root, path);
3584 return ret;
3585}
3586
3587static void set_reloc_control(struct reloc_control *rc)
3588{
3589 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
Josef Bacika4abeea2011-04-11 17:25:13 -04003590 spin_lock(&fs_info->trans_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003591 fs_info->reloc_ctl = rc;
Josef Bacika4abeea2011-04-11 17:25:13 -04003592 spin_unlock(&fs_info->trans_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003593}
3594
3595static void unset_reloc_control(struct reloc_control *rc)
3596{
3597 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
Josef Bacika4abeea2011-04-11 17:25:13 -04003598 spin_lock(&fs_info->trans_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003599 fs_info->reloc_ctl = NULL;
Josef Bacika4abeea2011-04-11 17:25:13 -04003600 spin_unlock(&fs_info->trans_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003601}
3602
3603static int check_extent_flags(u64 flags)
3604{
3605 if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
3606 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
3607 return 1;
3608 if (!(flags & BTRFS_EXTENT_FLAG_DATA) &&
3609 !(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
3610 return 1;
3611 if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
3612 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
3613 return 1;
3614 return 0;
3615}
3616
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003617static noinline_for_stack
3618int prepare_to_relocate(struct reloc_control *rc)
3619{
3620 struct btrfs_trans_handle *trans;
3621 int ret;
3622
3623 rc->block_rsv = btrfs_alloc_block_rsv(rc->extent_root);
3624 if (!rc->block_rsv)
3625 return -ENOMEM;
3626
3627 /*
3628 * reserve some space for creating reloc trees.
3629 * btrfs_init_reloc_root will use them when there
3630 * is no reservation in transaction handle.
3631 */
3632 ret = btrfs_block_rsv_add(NULL, rc->extent_root, rc->block_rsv,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003633 rc->extent_root->nodesize * 256);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003634 if (ret)
3635 return ret;
3636
3637 rc->block_rsv->refill_used = 1;
3638 btrfs_add_durable_block_rsv(rc->extent_root->fs_info, rc->block_rsv);
3639
3640 memset(&rc->cluster, 0, sizeof(rc->cluster));
3641 rc->search_start = rc->block_group->key.objectid;
3642 rc->extents_found = 0;
3643 rc->nodes_relocated = 0;
3644 rc->merging_rsv_size = 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003645
3646 rc->create_reloc_tree = 1;
3647 set_reloc_control(rc);
3648
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003649 trans = btrfs_join_transaction(rc->extent_root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00003650 BUG_ON(IS_ERR(trans));
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003651 btrfs_commit_transaction(trans, rc->extent_root);
3652 return 0;
3653}
Yan, Zheng76dda932009-09-21 16:00:26 -04003654
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003655static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
3656{
3657 struct rb_root blocks = RB_ROOT;
3658 struct btrfs_key key;
3659 struct btrfs_trans_handle *trans = NULL;
3660 struct btrfs_path *path;
3661 struct btrfs_extent_item *ei;
3662 unsigned long nr;
3663 u64 flags;
3664 u32 item_size;
3665 int ret;
3666 int err = 0;
Chris Masonc87f08c2011-02-16 13:57:04 -05003667 int progress = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003668
3669 path = btrfs_alloc_path();
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003670 if (!path)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003671 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04003672 path->reada = 1;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003673
3674 ret = prepare_to_relocate(rc);
3675 if (ret) {
3676 err = ret;
3677 goto out_free;
Jiri Slaby2423fdf2010-01-06 16:57:22 +00003678 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003679
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003680 while (1) {
Chris Masonc87f08c2011-02-16 13:57:04 -05003681 progress++;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003682 trans = btrfs_start_transaction(rc->extent_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003683 BUG_ON(IS_ERR(trans));
Chris Masonc87f08c2011-02-16 13:57:04 -05003684restart:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003685 if (update_backref_cache(trans, &rc->backref_cache)) {
3686 btrfs_end_transaction(trans, rc->extent_root);
3687 continue;
3688 }
3689
3690 ret = find_next_extent(trans, rc, path, &key);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003691 if (ret < 0)
3692 err = ret;
3693 if (ret != 0)
3694 break;
3695
3696 rc->extents_found++;
3697
3698 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3699 struct btrfs_extent_item);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003700 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003701 if (item_size >= sizeof(*ei)) {
3702 flags = btrfs_extent_flags(path->nodes[0], ei);
3703 ret = check_extent_flags(flags);
3704 BUG_ON(ret);
3705
3706 } else {
3707#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3708 u64 ref_owner;
3709 int path_change = 0;
3710
3711 BUG_ON(item_size !=
3712 sizeof(struct btrfs_extent_item_v0));
3713 ret = get_ref_objectid_v0(rc, path, &key, &ref_owner,
3714 &path_change);
3715 if (ref_owner < BTRFS_FIRST_FREE_OBJECTID)
3716 flags = BTRFS_EXTENT_FLAG_TREE_BLOCK;
3717 else
3718 flags = BTRFS_EXTENT_FLAG_DATA;
3719
3720 if (path_change) {
3721 btrfs_release_path(rc->extent_root, path);
3722
3723 path->search_commit_root = 1;
3724 path->skip_locking = 1;
3725 ret = btrfs_search_slot(NULL, rc->extent_root,
3726 &key, path, 0, 0);
3727 if (ret < 0) {
3728 err = ret;
3729 break;
3730 }
3731 BUG_ON(ret > 0);
3732 }
3733#else
3734 BUG();
3735#endif
3736 }
3737
3738 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
3739 ret = add_tree_block(rc, &key, path, &blocks);
3740 } else if (rc->stage == UPDATE_DATA_PTRS &&
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003741 (flags & BTRFS_EXTENT_FLAG_DATA)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003742 ret = add_data_references(rc, &key, path, &blocks);
3743 } else {
3744 btrfs_release_path(rc->extent_root, path);
3745 ret = 0;
3746 }
3747 if (ret < 0) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003748 err = ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003749 break;
3750 }
3751
3752 if (!RB_EMPTY_ROOT(&blocks)) {
3753 ret = relocate_tree_blocks(trans, rc, &blocks);
3754 if (ret < 0) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003755 if (ret != -EAGAIN) {
3756 err = ret;
3757 break;
3758 }
3759 rc->extents_found--;
3760 rc->search_start = key.objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003761 }
3762 }
3763
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003764 ret = btrfs_block_rsv_check(trans, rc->extent_root,
3765 rc->block_rsv, 0, 5);
3766 if (ret < 0) {
3767 if (ret != -EAGAIN) {
3768 err = ret;
3769 WARN_ON(1);
3770 break;
3771 }
3772 rc->commit_transaction = 1;
3773 }
3774
3775 if (rc->commit_transaction) {
3776 rc->commit_transaction = 0;
3777 ret = btrfs_commit_transaction(trans, rc->extent_root);
3778 BUG_ON(ret);
3779 } else {
3780 nr = trans->blocks_used;
3781 btrfs_end_transaction_throttle(trans, rc->extent_root);
3782 btrfs_btree_balance_dirty(rc->extent_root, nr);
3783 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003784 trans = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003785
3786 if (rc->stage == MOVE_DATA_EXTENTS &&
3787 (flags & BTRFS_EXTENT_FLAG_DATA)) {
3788 rc->found_file_extent = 1;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003789 ret = relocate_data_extent(rc->data_inode,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003790 &key, &rc->cluster);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003791 if (ret < 0) {
3792 err = ret;
3793 break;
3794 }
3795 }
3796 }
Chris Masonc87f08c2011-02-16 13:57:04 -05003797 if (trans && progress && err == -ENOSPC) {
3798 ret = btrfs_force_chunk_alloc(trans, rc->extent_root,
3799 rc->block_group->flags);
3800 if (ret == 0) {
3801 err = 0;
3802 progress = 0;
3803 goto restart;
3804 }
3805 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003806
3807 btrfs_release_path(rc->extent_root, path);
3808 clear_extent_bits(&rc->processed_blocks, 0, (u64)-1, EXTENT_DIRTY,
3809 GFP_NOFS);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003810
3811 if (trans) {
3812 nr = trans->blocks_used;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003813 btrfs_end_transaction_throttle(trans, rc->extent_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003814 btrfs_btree_balance_dirty(rc->extent_root, nr);
3815 }
3816
Yan, Zheng0257bb82009-09-24 09:17:31 -04003817 if (!err) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003818 ret = relocate_file_extent_cluster(rc->data_inode,
3819 &rc->cluster);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003820 if (ret < 0)
3821 err = ret;
3822 }
3823
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003824 rc->create_reloc_tree = 0;
3825 set_reloc_control(rc);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003826
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003827 backref_cache_cleanup(&rc->backref_cache);
3828 btrfs_block_rsv_release(rc->extent_root, rc->block_rsv, (u64)-1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003829
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003830 err = prepare_to_merge(rc, err);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003831
3832 merge_reloc_roots(rc);
3833
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003834 rc->merge_reloc_tree = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003835 unset_reloc_control(rc);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003836 btrfs_block_rsv_release(rc->extent_root, rc->block_rsv, (u64)-1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003837
3838 /* get rid of pinned extents */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003839 trans = btrfs_join_transaction(rc->extent_root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00003840 if (IS_ERR(trans))
3841 err = PTR_ERR(trans);
3842 else
3843 btrfs_commit_transaction(trans, rc->extent_root);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003844out_free:
3845 btrfs_free_block_rsv(rc->extent_root, rc->block_rsv);
3846 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003847 return err;
3848}
3849
3850static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
Yan, Zheng0257bb82009-09-24 09:17:31 -04003851 struct btrfs_root *root, u64 objectid)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003852{
3853 struct btrfs_path *path;
3854 struct btrfs_inode_item *item;
3855 struct extent_buffer *leaf;
3856 int ret;
3857
3858 path = btrfs_alloc_path();
3859 if (!path)
3860 return -ENOMEM;
3861
3862 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
3863 if (ret)
3864 goto out;
3865
3866 leaf = path->nodes[0];
3867 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
3868 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
3869 btrfs_set_inode_generation(leaf, item, 1);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003870 btrfs_set_inode_size(leaf, item, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003871 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003872 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS |
3873 BTRFS_INODE_PREALLOC);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003874 btrfs_mark_buffer_dirty(leaf);
3875 btrfs_release_path(root, path);
3876out:
3877 btrfs_free_path(path);
3878 return ret;
3879}
3880
3881/*
3882 * helper to create inode for data relocation.
3883 * the inode is in data relocation tree and its link count is 0
3884 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003885static noinline_for_stack
3886struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
3887 struct btrfs_block_group_cache *group)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003888{
3889 struct inode *inode = NULL;
3890 struct btrfs_trans_handle *trans;
3891 struct btrfs_root *root;
3892 struct btrfs_key key;
3893 unsigned long nr;
3894 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
3895 int err = 0;
3896
3897 root = read_fs_root(fs_info, BTRFS_DATA_RELOC_TREE_OBJECTID);
3898 if (IS_ERR(root))
3899 return ERR_CAST(root);
3900
Yan, Zhenga22285a2010-05-16 10:48:46 -04003901 trans = btrfs_start_transaction(root, 6);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003902 if (IS_ERR(trans))
3903 return ERR_CAST(trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003904
3905 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
3906 if (err)
3907 goto out;
3908
Yan, Zheng0257bb82009-09-24 09:17:31 -04003909 err = __insert_orphan_inode(trans, root, objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003910 BUG_ON(err);
3911
3912 key.objectid = objectid;
3913 key.type = BTRFS_INODE_ITEM_KEY;
3914 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003915 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003916 BUG_ON(IS_ERR(inode) || is_bad_inode(inode));
3917 BTRFS_I(inode)->index_cnt = group->key.objectid;
3918
3919 err = btrfs_orphan_add(trans, inode);
3920out:
3921 nr = trans->blocks_used;
3922 btrfs_end_transaction(trans, root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003923 btrfs_btree_balance_dirty(root, nr);
3924 if (err) {
3925 if (inode)
3926 iput(inode);
3927 inode = ERR_PTR(err);
3928 }
3929 return inode;
3930}
3931
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003932static struct reloc_control *alloc_reloc_control(void)
3933{
3934 struct reloc_control *rc;
3935
3936 rc = kzalloc(sizeof(*rc), GFP_NOFS);
3937 if (!rc)
3938 return NULL;
3939
3940 INIT_LIST_HEAD(&rc->reloc_roots);
3941 backref_cache_init(&rc->backref_cache);
3942 mapping_tree_init(&rc->reloc_root_tree);
3943 extent_io_tree_init(&rc->processed_blocks, NULL, GFP_NOFS);
3944 return rc;
3945}
3946
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003947/*
3948 * function to relocate all extents in a block group.
3949 */
3950int btrfs_relocate_block_group(struct btrfs_root *extent_root, u64 group_start)
3951{
3952 struct btrfs_fs_info *fs_info = extent_root->fs_info;
3953 struct reloc_control *rc;
Josef Bacik0af3d002010-06-21 14:48:16 -04003954 struct inode *inode;
3955 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003956 int ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003957 int rw = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003958 int err = 0;
3959
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003960 rc = alloc_reloc_control();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003961 if (!rc)
3962 return -ENOMEM;
3963
Yan, Zhengf0486c62010-05-16 10:46:25 -04003964 rc->extent_root = extent_root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003965
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003966 rc->block_group = btrfs_lookup_block_group(fs_info, group_start);
3967 BUG_ON(!rc->block_group);
3968
Yan, Zhengf0486c62010-05-16 10:46:25 -04003969 if (!rc->block_group->ro) {
3970 ret = btrfs_set_block_group_ro(extent_root, rc->block_group);
3971 if (ret) {
3972 err = ret;
3973 goto out;
3974 }
3975 rw = 1;
3976 }
3977
Josef Bacik0af3d002010-06-21 14:48:16 -04003978 path = btrfs_alloc_path();
3979 if (!path) {
3980 err = -ENOMEM;
3981 goto out;
3982 }
3983
3984 inode = lookup_free_space_inode(fs_info->tree_root, rc->block_group,
3985 path);
3986 btrfs_free_path(path);
3987
3988 if (!IS_ERR(inode))
3989 ret = delete_block_group_cache(fs_info, inode, 0);
3990 else
3991 ret = PTR_ERR(inode);
3992
3993 if (ret && ret != -ENOENT) {
3994 err = ret;
3995 goto out;
3996 }
3997
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003998 rc->data_inode = create_reloc_inode(fs_info, rc->block_group);
3999 if (IS_ERR(rc->data_inode)) {
4000 err = PTR_ERR(rc->data_inode);
4001 rc->data_inode = NULL;
4002 goto out;
4003 }
4004
4005 printk(KERN_INFO "btrfs: relocating block group %llu flags %llu\n",
4006 (unsigned long long)rc->block_group->key.objectid,
4007 (unsigned long long)rc->block_group->flags);
4008
Yan, Zheng24bbcf02009-11-12 09:36:34 +00004009 btrfs_start_delalloc_inodes(fs_info->tree_root, 0);
4010 btrfs_wait_ordered_extents(fs_info->tree_root, 0, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004011
4012 while (1) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004013 mutex_lock(&fs_info->cleaner_mutex);
4014
4015 btrfs_clean_old_snapshots(fs_info->tree_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004016 ret = relocate_block_group(rc);
Yan, Zheng76dda932009-09-21 16:00:26 -04004017
4018 mutex_unlock(&fs_info->cleaner_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004019 if (ret < 0) {
4020 err = ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004021 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004022 }
4023
4024 if (rc->extents_found == 0)
4025 break;
4026
4027 printk(KERN_INFO "btrfs: found %llu extents\n",
4028 (unsigned long long)rc->extents_found);
4029
4030 if (rc->stage == MOVE_DATA_EXTENTS && rc->found_file_extent) {
4031 btrfs_wait_ordered_range(rc->data_inode, 0, (u64)-1);
4032 invalidate_mapping_pages(rc->data_inode->i_mapping,
4033 0, -1);
4034 rc->stage = UPDATE_DATA_PTRS;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004035 }
4036 }
4037
Yan, Zheng0257bb82009-09-24 09:17:31 -04004038 filemap_write_and_wait_range(fs_info->btree_inode->i_mapping,
4039 rc->block_group->key.objectid,
4040 rc->block_group->key.objectid +
4041 rc->block_group->key.offset - 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004042
4043 WARN_ON(rc->block_group->pinned > 0);
4044 WARN_ON(rc->block_group->reserved > 0);
4045 WARN_ON(btrfs_block_group_used(&rc->block_group->item) > 0);
4046out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04004047 if (err && rw)
4048 btrfs_set_block_group_rw(extent_root, rc->block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004049 iput(rc->data_inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004050 btrfs_put_block_group(rc->block_group);
4051 kfree(rc);
4052 return err;
4053}
4054
Yan, Zheng76dda932009-09-21 16:00:26 -04004055static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)
4056{
4057 struct btrfs_trans_handle *trans;
4058 int ret;
4059
Yan, Zhenga22285a2010-05-16 10:48:46 -04004060 trans = btrfs_start_transaction(root->fs_info->tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004061 BUG_ON(IS_ERR(trans));
Yan, Zheng76dda932009-09-21 16:00:26 -04004062
4063 memset(&root->root_item.drop_progress, 0,
4064 sizeof(root->root_item.drop_progress));
4065 root->root_item.drop_level = 0;
4066 btrfs_set_root_refs(&root->root_item, 0);
4067 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4068 &root->root_key, &root->root_item);
4069 BUG_ON(ret);
4070
4071 ret = btrfs_end_transaction(trans, root->fs_info->tree_root);
4072 BUG_ON(ret);
4073 return 0;
4074}
4075
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004076/*
4077 * recover relocation interrupted by system crash.
4078 *
4079 * this function resumes merging reloc trees with corresponding fs trees.
4080 * this is important for keeping the sharing of tree blocks
4081 */
4082int btrfs_recover_relocation(struct btrfs_root *root)
4083{
4084 LIST_HEAD(reloc_roots);
4085 struct btrfs_key key;
4086 struct btrfs_root *fs_root;
4087 struct btrfs_root *reloc_root;
4088 struct btrfs_path *path;
4089 struct extent_buffer *leaf;
4090 struct reloc_control *rc = NULL;
4091 struct btrfs_trans_handle *trans;
4092 int ret;
4093 int err = 0;
4094
4095 path = btrfs_alloc_path();
4096 if (!path)
4097 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04004098 path->reada = -1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004099
4100 key.objectid = BTRFS_TREE_RELOC_OBJECTID;
4101 key.type = BTRFS_ROOT_ITEM_KEY;
4102 key.offset = (u64)-1;
4103
4104 while (1) {
4105 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key,
4106 path, 0, 0);
4107 if (ret < 0) {
4108 err = ret;
4109 goto out;
4110 }
4111 if (ret > 0) {
4112 if (path->slots[0] == 0)
4113 break;
4114 path->slots[0]--;
4115 }
4116 leaf = path->nodes[0];
4117 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4118 btrfs_release_path(root->fs_info->tree_root, path);
4119
4120 if (key.objectid != BTRFS_TREE_RELOC_OBJECTID ||
4121 key.type != BTRFS_ROOT_ITEM_KEY)
4122 break;
4123
4124 reloc_root = btrfs_read_fs_root_no_radix(root, &key);
4125 if (IS_ERR(reloc_root)) {
4126 err = PTR_ERR(reloc_root);
4127 goto out;
4128 }
4129
4130 list_add(&reloc_root->root_list, &reloc_roots);
4131
4132 if (btrfs_root_refs(&reloc_root->root_item) > 0) {
4133 fs_root = read_fs_root(root->fs_info,
4134 reloc_root->root_key.offset);
4135 if (IS_ERR(fs_root)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004136 ret = PTR_ERR(fs_root);
4137 if (ret != -ENOENT) {
4138 err = ret;
4139 goto out;
4140 }
4141 mark_garbage_root(reloc_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004142 }
4143 }
4144
4145 if (key.offset == 0)
4146 break;
4147
4148 key.offset--;
4149 }
4150 btrfs_release_path(root->fs_info->tree_root, path);
4151
4152 if (list_empty(&reloc_roots))
4153 goto out;
4154
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004155 rc = alloc_reloc_control();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004156 if (!rc) {
4157 err = -ENOMEM;
4158 goto out;
4159 }
4160
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004161 rc->extent_root = root->fs_info->extent_root;
4162
4163 set_reloc_control(rc);
4164
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004165 trans = btrfs_join_transaction(rc->extent_root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004166 if (IS_ERR(trans)) {
4167 unset_reloc_control(rc);
4168 err = PTR_ERR(trans);
4169 goto out_free;
4170 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004171
4172 rc->merge_reloc_tree = 1;
4173
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004174 while (!list_empty(&reloc_roots)) {
4175 reloc_root = list_entry(reloc_roots.next,
4176 struct btrfs_root, root_list);
4177 list_del(&reloc_root->root_list);
4178
4179 if (btrfs_root_refs(&reloc_root->root_item) == 0) {
4180 list_add_tail(&reloc_root->root_list,
4181 &rc->reloc_roots);
4182 continue;
4183 }
4184
4185 fs_root = read_fs_root(root->fs_info,
4186 reloc_root->root_key.offset);
4187 BUG_ON(IS_ERR(fs_root));
4188
4189 __add_reloc_root(reloc_root);
4190 fs_root->reloc_root = reloc_root;
4191 }
4192
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004193 btrfs_commit_transaction(trans, rc->extent_root);
4194
4195 merge_reloc_roots(rc);
4196
4197 unset_reloc_control(rc);
4198
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004199 trans = btrfs_join_transaction(rc->extent_root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004200 if (IS_ERR(trans))
4201 err = PTR_ERR(trans);
4202 else
4203 btrfs_commit_transaction(trans, rc->extent_root);
4204out_free:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004205 kfree(rc);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004206out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004207 while (!list_empty(&reloc_roots)) {
4208 reloc_root = list_entry(reloc_roots.next,
4209 struct btrfs_root, root_list);
4210 list_del(&reloc_root->root_list);
4211 free_extent_buffer(reloc_root->node);
4212 free_extent_buffer(reloc_root->commit_root);
4213 kfree(reloc_root);
4214 }
4215 btrfs_free_path(path);
4216
4217 if (err == 0) {
4218 /* cleanup orphan inode in data relocation tree */
4219 fs_root = read_fs_root(root->fs_info,
4220 BTRFS_DATA_RELOC_TREE_OBJECTID);
4221 if (IS_ERR(fs_root))
4222 err = PTR_ERR(fs_root);
Miao Xied7ce5842010-02-02 08:46:44 +00004223 else
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004224 err = btrfs_orphan_cleanup(fs_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004225 }
4226 return err;
4227}
4228
4229/*
4230 * helper to add ordered checksum for data relocation.
4231 *
4232 * cloning checksum properly handles the nodatasum extents.
4233 * it also saves CPU time to re-calculate the checksum.
4234 */
4235int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
4236{
4237 struct btrfs_ordered_sum *sums;
4238 struct btrfs_sector_sum *sector_sum;
4239 struct btrfs_ordered_extent *ordered;
4240 struct btrfs_root *root = BTRFS_I(inode)->root;
4241 size_t offset;
4242 int ret;
4243 u64 disk_bytenr;
4244 LIST_HEAD(list);
4245
4246 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
4247 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
4248
4249 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
4250 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
4251 disk_bytenr + len - 1, &list);
4252
4253 while (!list_empty(&list)) {
4254 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
4255 list_del_init(&sums->list);
4256
4257 sector_sum = sums->sums;
4258 sums->bytenr = ordered->start;
4259
4260 offset = 0;
4261 while (offset < sums->len) {
4262 sector_sum->bytenr += ordered->start - disk_bytenr;
4263 sector_sum++;
4264 offset += root->sectorsize;
4265 }
4266
4267 btrfs_add_ordered_sum(inode, ordered, sums);
4268 }
4269 btrfs_put_ordered_extent(ordered);
Andi Kleen411fc6b2010-10-29 15:14:31 -04004270 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004271}
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004272
4273void btrfs_reloc_cow_block(struct btrfs_trans_handle *trans,
4274 struct btrfs_root *root, struct extent_buffer *buf,
4275 struct extent_buffer *cow)
4276{
4277 struct reloc_control *rc;
4278 struct backref_node *node;
4279 int first_cow = 0;
4280 int level;
4281 int ret;
4282
4283 rc = root->fs_info->reloc_ctl;
4284 if (!rc)
4285 return;
4286
4287 BUG_ON(rc->stage == UPDATE_DATA_PTRS &&
4288 root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID);
4289
4290 level = btrfs_header_level(buf);
4291 if (btrfs_header_generation(buf) <=
4292 btrfs_root_last_snapshot(&root->root_item))
4293 first_cow = 1;
4294
4295 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID &&
4296 rc->create_reloc_tree) {
4297 WARN_ON(!first_cow && level == 0);
4298
4299 node = rc->backref_cache.path[level];
4300 BUG_ON(node->bytenr != buf->start &&
4301 node->new_bytenr != buf->start);
4302
4303 drop_node_buffer(node);
4304 extent_buffer_get(cow);
4305 node->eb = cow;
4306 node->new_bytenr = cow->start;
4307
4308 if (!node->pending) {
4309 list_move_tail(&node->list,
4310 &rc->backref_cache.pending[level]);
4311 node->pending = 1;
4312 }
4313
4314 if (first_cow)
4315 __mark_block_processed(rc, node);
4316
4317 if (first_cow && level > 0)
4318 rc->nodes_relocated += buf->len;
4319 }
4320
4321 if (level == 0 && first_cow && rc->stage == UPDATE_DATA_PTRS) {
4322 ret = replace_file_extents(trans, rc, root, cow);
4323 BUG_ON(ret);
4324 }
4325}
4326
4327/*
4328 * called before creating snapshot. it calculates metadata reservation
4329 * requried for relocating tree blocks in the snapshot
4330 */
4331void btrfs_reloc_pre_snapshot(struct btrfs_trans_handle *trans,
4332 struct btrfs_pending_snapshot *pending,
4333 u64 *bytes_to_reserve)
4334{
4335 struct btrfs_root *root;
4336 struct reloc_control *rc;
4337
4338 root = pending->root;
4339 if (!root->reloc_root)
4340 return;
4341
4342 rc = root->fs_info->reloc_ctl;
4343 if (!rc->merge_reloc_tree)
4344 return;
4345
4346 root = root->reloc_root;
4347 BUG_ON(btrfs_root_refs(&root->root_item) == 0);
4348 /*
4349 * relocation is in the stage of merging trees. the space
4350 * used by merging a reloc tree is twice the size of
4351 * relocated tree nodes in the worst case. half for cowing
4352 * the reloc tree, half for cowing the fs tree. the space
4353 * used by cowing the reloc tree will be freed after the
4354 * tree is dropped. if we create snapshot, cowing the fs
4355 * tree may use more space than it frees. so we need
4356 * reserve extra space.
4357 */
4358 *bytes_to_reserve += rc->nodes_relocated;
4359}
4360
4361/*
4362 * called after snapshot is created. migrate block reservation
4363 * and create reloc root for the newly created snapshot
4364 */
4365void btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,
4366 struct btrfs_pending_snapshot *pending)
4367{
4368 struct btrfs_root *root = pending->root;
4369 struct btrfs_root *reloc_root;
4370 struct btrfs_root *new_root;
4371 struct reloc_control *rc;
4372 int ret;
4373
4374 if (!root->reloc_root)
4375 return;
4376
4377 rc = root->fs_info->reloc_ctl;
4378 rc->merging_rsv_size += rc->nodes_relocated;
4379
4380 if (rc->merge_reloc_tree) {
4381 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
4382 rc->block_rsv,
4383 rc->nodes_relocated);
4384 BUG_ON(ret);
4385 }
4386
4387 new_root = pending->snap;
4388 reloc_root = create_reloc_root(trans, root->reloc_root,
4389 new_root->root_key.objectid);
4390
4391 __add_reloc_root(reloc_root);
4392 new_root->reloc_root = reloc_root;
4393
4394 if (rc->create_reloc_tree) {
4395 ret = clone_backref_node(trans, rc, root, reloc_root);
4396 BUG_ON(ret);
4397 }
4398}