relocation.c 115 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
3
4
5
6
7
8
9
10
/*
 * Copyright (C) 2009 Oracle.  All rights reserved.
 */

#include <linux/sched.h>
#include <linux/pagemap.h>
#include <linux/writeback.h>
#include <linux/blkdev.h>
#include <linux/rbtree.h>
11
#include <linux/slab.h>
12
13
14
15
16
17
18
#include "ctree.h"
#include "disk-io.h"
#include "transaction.h"
#include "volumes.h"
#include "locking.h"
#include "btrfs_inode.h"
#include "async-thread.h"
19
#include "free-space-cache.h"
20
#include "inode-map.h"
21
#include "qgroup.h"
22
#include "print-tree.h"
23
#include "delalloc-space.h"
24
#include "block-group.h"
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

/*
 * backref_node, mapping_node and tree_block start with this
 */
struct tree_entry {
	struct rb_node rb_node;
	u64 bytenr;
};

/*
 * present a tree block in the backref cache
 */
struct backref_node {
	struct rb_node rb_node;
	u64 bytenr;
40
41
42

	u64 new_bytenr;
	/* objectid of tree block owner, can be not uptodate */
43
	u64 owner;
44
45
	/* link to pending, changed or detached list */
	struct list_head list;
46
47
48
49
50
51
52
53
54
55
	/* list of upper level blocks reference this block */
	struct list_head upper;
	/* list of child blocks in the cache */
	struct list_head lower;
	/* NULL if this node is not tree root */
	struct btrfs_root *root;
	/* extent buffer got by COW the block */
	struct extent_buffer *eb;
	/* level of tree block */
	unsigned int level:8;
56
57
58
	/* is the block in non-reference counted tree */
	unsigned int cowonly:1;
	/* 1 if no child node in the cache */
59
60
61
62
63
64
65
	unsigned int lowest:1;
	/* is the extent buffer locked */
	unsigned int locked:1;
	/* has the block been processed */
	unsigned int processed:1;
	/* have backrefs of this block been checked */
	unsigned int checked:1;
66
67
68
69
70
71
72
73
74
75
	/*
	 * 1 if corresponding block has been cowed but some upper
	 * level block pointers may not point to the new location
	 */
	unsigned int pending:1;
	/*
	 * 1 if the backref node isn't connected to any other
	 * backref node.
	 */
	unsigned int detached:1;
76
77
78
79
80
81
82
83
84
85
86
87
};

/*
 * present a block pointer in the backref cache
 */
struct backref_edge {
	struct list_head list[2];
	struct backref_node *node[2];
};

#define LOWER	0
#define UPPER	1
88
#define RELOCATION_RESERVED_NODES	256
89
90
91
92

struct backref_cache {
	/* red black tree of all backref nodes in the cache */
	struct rb_root rb_root;
93
94
95
96
97
98
99
	/* for passing backref nodes to btrfs_reloc_cow_block */
	struct backref_node *path[BTRFS_MAX_LEVEL];
	/*
	 * list of blocks that have been cowed but some block
	 * pointers in upper level blocks may not reflect the
	 * new location
	 */
100
	struct list_head pending[BTRFS_MAX_LEVEL];
101
102
103
104
105
106
107
108
109
110
111
	/* list of backref nodes with no child node */
	struct list_head leaves;
	/* list of blocks that have been cowed in current transaction */
	struct list_head changed;
	/* list of detached backref node. */
	struct list_head detached;

	u64 last_trans;

	int nr_nodes;
	int nr_edges;
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
};

/*
 * map address of tree root to tree
 */
struct mapping_node {
	struct rb_node rb_node;
	u64 bytenr;
	void *data;
};

struct mapping_tree {
	struct rb_root rb_root;
	spinlock_t lock;
};

/*
 * present a tree block to process
 */
struct tree_block {
	struct rb_node rb_node;
	u64 bytenr;
	struct btrfs_key key;
	unsigned int level:8;
	unsigned int key_ready:1;
};

139
140
141
142
143
144
145
146
147
#define MAX_EXTENTS 128

struct file_extent_cluster {
	u64 start;
	u64 end;
	u64 boundary[MAX_EXTENTS];
	unsigned int nr;
};

148
149
struct reloc_control {
	/* block group to relocate */
150
	struct btrfs_block_group *block_group;
151
152
153
154
	/* extent tree */
	struct btrfs_root *extent_root;
	/* inode for moving data */
	struct inode *data_inode;
155
156
157
158
159
160

	struct btrfs_block_rsv *block_rsv;

	struct backref_cache backref_cache;

	struct file_extent_cluster cluster;
161
162
163
164
165
166
	/* tree blocks have been processed */
	struct extent_io_tree processed_blocks;
	/* map start of tree root to corresponding reloc tree */
	struct mapping_tree reloc_root_tree;
	/* list of reloc trees */
	struct list_head reloc_roots;
167
168
	/* list of subvolume trees that get relocated */
	struct list_head dirty_subvol_roots;
169
170
171
172
	/* size of metadata reservation for merging reloc trees */
	u64 merging_rsv_size;
	/* size of relocated tree nodes */
	u64 nodes_relocated;
173
174
	/* reserved size for block group relocation*/
	u64 reserved_bytes;
175

176
177
	u64 search_start;
	u64 extents_found;
178
179
180
181

	unsigned int stage:8;
	unsigned int create_reloc_tree:1;
	unsigned int merge_reloc_tree:1;
182
183
184
185
186
187
188
	unsigned int found_file_extent:1;
};

/* stages of data relocation */
#define MOVE_DATA_EXTENTS	0
#define UPDATE_DATA_PTRS	1

189
190
191
192
static void remove_backref_node(struct backref_cache *cache,
				struct backref_node *node);
static void __mark_block_processed(struct reloc_control *rc,
				   struct backref_node *node);
193
194
195

static void mapping_tree_init(struct mapping_tree *tree)
{
196
	tree->rb_root = RB_ROOT;
197
198
199
200
201
202
	spin_lock_init(&tree->lock);
}

static void backref_cache_init(struct backref_cache *cache)
{
	int i;
203
	cache->rb_root = RB_ROOT;
204
205
	for (i = 0; i < BTRFS_MAX_LEVEL; i++)
		INIT_LIST_HEAD(&cache->pending[i]);
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
	INIT_LIST_HEAD(&cache->changed);
	INIT_LIST_HEAD(&cache->detached);
	INIT_LIST_HEAD(&cache->leaves);
}

static void backref_cache_cleanup(struct backref_cache *cache)
{
	struct backref_node *node;
	int i;

	while (!list_empty(&cache->detached)) {
		node = list_entry(cache->detached.next,
				  struct backref_node, list);
		remove_backref_node(cache, node);
	}

	while (!list_empty(&cache->leaves)) {
		node = list_entry(cache->leaves.next,
				  struct backref_node, lower);
		remove_backref_node(cache, node);
	}

	cache->last_trans = 0;

	for (i = 0; i < BTRFS_MAX_LEVEL; i++)
231
232
233
234
235
236
		ASSERT(list_empty(&cache->pending[i]));
	ASSERT(list_empty(&cache->changed));
	ASSERT(list_empty(&cache->detached));
	ASSERT(RB_EMPTY_ROOT(&cache->rb_root));
	ASSERT(!cache->nr_nodes);
	ASSERT(!cache->nr_edges);
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
}

static struct backref_node *alloc_backref_node(struct backref_cache *cache)
{
	struct backref_node *node;

	node = kzalloc(sizeof(*node), GFP_NOFS);
	if (node) {
		INIT_LIST_HEAD(&node->list);
		INIT_LIST_HEAD(&node->upper);
		INIT_LIST_HEAD(&node->lower);
		RB_CLEAR_NODE(&node->rb_node);
		cache->nr_nodes++;
	}
	return node;
}

static void free_backref_node(struct backref_cache *cache,
			      struct backref_node *node)
{
	if (node) {
		cache->nr_nodes--;
		kfree(node);
	}
}

static struct backref_edge *alloc_backref_edge(struct backref_cache *cache)
{
	struct backref_edge *edge;

	edge = kzalloc(sizeof(*edge), GFP_NOFS);
	if (edge)
		cache->nr_edges++;
	return edge;
271
272
}

273
274
static void free_backref_edge(struct backref_cache *cache,
			      struct backref_edge *edge)
275
{
276
277
278
279
	if (edge) {
		cache->nr_edges--;
		kfree(edge);
	}
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
}

static struct rb_node *tree_insert(struct rb_root *root, u64 bytenr,
				   struct rb_node *node)
{
	struct rb_node **p = &root->rb_node;
	struct rb_node *parent = NULL;
	struct tree_entry *entry;

	while (*p) {
		parent = *p;
		entry = rb_entry(parent, struct tree_entry, rb_node);

		if (bytenr < entry->bytenr)
			p = &(*p)->rb_left;
		else if (bytenr > entry->bytenr)
			p = &(*p)->rb_right;
		else
			return parent;
	}

	rb_link_node(node, parent, p);
	rb_insert_color(node, root);
	return NULL;
}

static struct rb_node *tree_search(struct rb_root *root, u64 bytenr)
{
	struct rb_node *n = root->rb_node;
	struct tree_entry *entry;

	while (n) {
		entry = rb_entry(n, struct tree_entry, rb_node);

		if (bytenr < entry->bytenr)
			n = n->rb_left;
		else if (bytenr > entry->bytenr)
			n = n->rb_right;
		else
			return n;
	}
	return NULL;
}

324
static void backref_tree_panic(struct rb_node *rb_node, int errno, u64 bytenr)
325
326
327
328
329
330
331
{

	struct btrfs_fs_info *fs_info = NULL;
	struct backref_node *bnode = rb_entry(rb_node, struct backref_node,
					      rb_node);
	if (bnode->root)
		fs_info = bnode->root->fs_info;
Jeff Mahoney's avatar
Jeff Mahoney committed
332
333
334
	btrfs_panic(fs_info, errno,
		    "Inconsistency in backref cache found at offset %llu",
		    bytenr);
335
336
}

337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * walk up backref nodes until reach node presents tree root
 */
static struct backref_node *walk_up_backref(struct backref_node *node,
					    struct backref_edge *edges[],
					    int *index)
{
	struct backref_edge *edge;
	int idx = *index;

	while (!list_empty(&node->upper)) {
		edge = list_entry(node->upper.next,
				  struct backref_edge, list[LOWER]);
		edges[idx++] = edge;
		node = edge->node[UPPER];
	}
353
	BUG_ON(node->detached);
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
	*index = idx;
	return node;
}

/*
 * walk down backref nodes to find start of next reference path
 */
static struct backref_node *walk_down_backref(struct backref_edge *edges[],
					      int *index)
{
	struct backref_edge *edge;
	struct backref_node *lower;
	int idx = *index;

	while (idx > 0) {
		edge = edges[idx - 1];
		lower = edge->node[LOWER];
		if (list_is_last(&edge->list[LOWER], &lower->upper)) {
			idx--;
			continue;
		}
		edge = list_entry(edge->list[LOWER].next,
				  struct backref_edge, list[LOWER]);
		edges[idx - 1] = edge;
		*index = idx;
		return edge->node[UPPER];
	}
	*index = 0;
	return NULL;
}

385
386
387
388
389
390
391
392
static void unlock_node_buffer(struct backref_node *node)
{
	if (node->locked) {
		btrfs_tree_unlock(node->eb);
		node->locked = 0;
	}
}

393
394
395
static void drop_node_buffer(struct backref_node *node)
{
	if (node->eb) {
396
		unlock_node_buffer(node);
397
398
399
400
401
402
403
404
405
406
407
		free_extent_buffer(node->eb);
		node->eb = NULL;
	}
}

static void drop_backref_node(struct backref_cache *tree,
			      struct backref_node *node)
{
	BUG_ON(!list_empty(&node->upper));

	drop_node_buffer(node);
408
	list_del(&node->list);
409
	list_del(&node->lower);
410
411
412
	if (!RB_EMPTY_NODE(&node->rb_node))
		rb_erase(&node->rb_node, &tree->rb_root);
	free_backref_node(tree, node);
413
414
415
416
417
418
419
420
421
422
423
424
425
426
}

/*
 * remove a backref node from the backref cache
 */
static void remove_backref_node(struct backref_cache *cache,
				struct backref_node *node)
{
	struct backref_node *upper;
	struct backref_edge *edge;

	if (!node)
		return;

427
	BUG_ON(!node->lowest && !node->detached);
428
429
430
431
432
433
	while (!list_empty(&node->upper)) {
		edge = list_entry(node->upper.next, struct backref_edge,
				  list[LOWER]);
		upper = edge->node[UPPER];
		list_del(&edge->list[LOWER]);
		list_del(&edge->list[UPPER]);
434
435
436
437
438
439
440
441
442
		free_backref_edge(cache, edge);

		if (RB_EMPTY_NODE(&upper->rb_node)) {
			BUG_ON(!list_empty(&node->upper));
			drop_backref_node(cache, node);
			node = upper;
			node->lowest = 1;
			continue;
		}
443
		/*
444
		 * add the node to leaf node list if no other
445
446
447
		 * child block cached.
		 */
		if (list_empty(&upper->lower)) {
448
			list_add_tail(&upper->lower, &cache->leaves);
449
450
451
			upper->lowest = 1;
		}
	}
452

453
454
455
	drop_backref_node(cache, node);
}

456
457
458
459
460
461
462
static void update_backref_node(struct backref_cache *cache,
				struct backref_node *node, u64 bytenr)
{
	struct rb_node *rb_node;
	rb_erase(&node->rb_node, &cache->rb_root);
	node->bytenr = bytenr;
	rb_node = tree_insert(&cache->rb_root, node->bytenr, &node->rb_node);
463
464
	if (rb_node)
		backref_tree_panic(rb_node, -EEXIST, bytenr);
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
}

/*
 * update backref cache after a transaction commit
 */
static int update_backref_cache(struct btrfs_trans_handle *trans,
				struct backref_cache *cache)
{
	struct backref_node *node;
	int level = 0;

	if (cache->last_trans == 0) {
		cache->last_trans = trans->transid;
		return 0;
	}

	if (cache->last_trans == trans->transid)
		return 0;

	/*
	 * detached nodes are used to avoid unnecessary backref
	 * lookup. transaction commit changes the extent tree.
	 * so the detached nodes are no longer useful.
	 */
	while (!list_empty(&cache->detached)) {
		node = list_entry(cache->detached.next,
				  struct backref_node, list);
		remove_backref_node(cache, node);
	}

	while (!list_empty(&cache->changed)) {
		node = list_entry(cache->changed.next,
				  struct backref_node, list);
		list_del_init(&node->list);
		BUG_ON(node->pending);
		update_backref_node(cache, node, node->new_bytenr);
	}

	/*
	 * some nodes can be left in the pending list if there were
	 * errors during processing the pending nodes.
	 */
	for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
		list_for_each_entry(node, &cache->pending[level], list) {
			BUG_ON(!node->pending);
			if (node->bytenr == node->new_bytenr)
				continue;
			update_backref_node(cache, node, node->new_bytenr);
		}
	}

	cache->last_trans = 0;
	return 1;
}

520

521
522
523
524
static int should_ignore_root(struct btrfs_root *root)
{
	struct btrfs_root *reloc_root;

525
	if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
		return 0;

	reloc_root = root->reloc_root;
	if (!reloc_root)
		return 0;

	if (btrfs_root_last_snapshot(&reloc_root->root_item) ==
	    root->fs_info->running_transaction->transid - 1)
		return 0;
	/*
	 * if there is reloc tree and it was created in previous
	 * transaction backref lookup can find the reloc tree,
	 * so backref node for the fs tree root is useless for
	 * relocation.
	 */
	return 1;
}
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*
 * find reloc tree by address of tree root
 */
static struct btrfs_root *find_reloc_root(struct reloc_control *rc,
					  u64 bytenr)
{
	struct rb_node *rb_node;
	struct mapping_node *node;
	struct btrfs_root *root = NULL;

	spin_lock(&rc->reloc_root_tree.lock);
	rb_node = tree_search(&rc->reloc_root_tree.rb_root, bytenr);
	if (rb_node) {
		node = rb_entry(rb_node, struct mapping_node, rb_node);
		root = (struct btrfs_root *)node->data;
	}
	spin_unlock(&rc->reloc_root_tree.lock);
	return root;
}

static int is_cowonly_root(u64 root_objectid)
{
	if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
	    root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
	    root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
	    root_objectid == BTRFS_DEV_TREE_OBJECTID ||
	    root_objectid == BTRFS_TREE_LOG_OBJECTID ||
570
571
	    root_objectid == BTRFS_CSUM_TREE_OBJECTID ||
	    root_objectid == BTRFS_UUID_TREE_OBJECTID ||
572
573
	    root_objectid == BTRFS_QUOTA_TREE_OBJECTID ||
	    root_objectid == BTRFS_FREE_SPACE_TREE_OBJECTID)
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
		return 1;
	return 0;
}

static struct btrfs_root *read_fs_root(struct btrfs_fs_info *fs_info,
					u64 root_objectid)
{
	struct btrfs_key key;

	key.objectid = root_objectid;
	key.type = BTRFS_ROOT_ITEM_KEY;
	if (is_cowonly_root(root_objectid))
		key.offset = 0;
	else
		key.offset = (u64)-1;

590
	return btrfs_get_fs_root(fs_info, &key, false);
591
592
593
594
595
596
}

static noinline_for_stack
int find_inline_backref(struct extent_buffer *leaf, int slot,
			unsigned long *ptr, unsigned long *end)
{
597
	struct btrfs_key key;
598
599
600
601
	struct btrfs_extent_item *ei;
	struct btrfs_tree_block_info *bi;
	u32 item_size;

602
603
	btrfs_item_key_to_cpu(leaf, &key, slot);

604
	item_size = btrfs_item_size_nr(leaf, slot);
605
606
607
608
609
	if (item_size < sizeof(*ei)) {
		btrfs_print_v0_err(leaf->fs_info);
		btrfs_handle_fs_error(leaf->fs_info, -EINVAL, NULL);
		return 1;
	}
610
611
612
613
	ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
	WARN_ON(!(btrfs_extent_flags(leaf, ei) &
		  BTRFS_EXTENT_FLAG_TREE_BLOCK));

614
615
	if (key.type == BTRFS_EXTENT_ITEM_KEY &&
	    item_size <= sizeof(*ei) + sizeof(*bi)) {
616
617
618
		WARN_ON(item_size < sizeof(*ei) + sizeof(*bi));
		return 1;
	}
619
620
621
622
623
	if (key.type == BTRFS_METADATA_ITEM_KEY &&
	    item_size <= sizeof(*ei)) {
		WARN_ON(item_size < sizeof(*ei));
		return 1;
	}
624

625
626
627
628
629
630
	if (key.type == BTRFS_EXTENT_ITEM_KEY) {
		bi = (struct btrfs_tree_block_info *)(ei + 1);
		*ptr = (unsigned long)(bi + 1);
	} else {
		*ptr = (unsigned long)(ei + 1);
	}
631
632
633
634
635
636
637
638
639
640
	*end = (unsigned long)ei + item_size;
	return 0;
}

/*
 * build backref tree for a given tree block. root of the backref tree
 * corresponds the tree block, leaves of the backref tree correspond
 * roots of b-trees that reference the tree block.
 *
 * the basic idea of this function is check backrefs of a given block
641
642
 * to find upper level blocks that reference the block, and then check
 * backrefs of these upper level blocks recursively. the recursion stop
643
644
645
646
647
648
 * when tree root is reached or backrefs for the block is cached.
 *
 * NOTE: if we find backrefs for a block are cached, we know backrefs
 * for all upper level blocks that directly/indirectly reference the
 * block are also cached.
 */
649
650
651
652
static noinline_for_stack
struct backref_node *build_backref_tree(struct reloc_control *rc,
					struct btrfs_key *node_key,
					int level, u64 bytenr)
653
{
654
	struct backref_cache *cache = &rc->backref_cache;
655
656
	struct btrfs_path *path1; /* For searching extent root */
	struct btrfs_path *path2; /* For searching parent of TREE_BLOCK_REF */
657
658
659
660
661
662
663
664
665
666
667
668
	struct extent_buffer *eb;
	struct btrfs_root *root;
	struct backref_node *cur;
	struct backref_node *upper;
	struct backref_node *lower;
	struct backref_node *node = NULL;
	struct backref_node *exist = NULL;
	struct backref_edge *edge;
	struct rb_node *rb_node;
	struct btrfs_key key;
	unsigned long end;
	unsigned long ptr;
669
	LIST_HEAD(list); /* Pending edge list, upper node needs to be checked */
670
671
	LIST_HEAD(useless);
	int cowonly;
672
673
	int ret;
	int err = 0;
674
	bool need_check = true;
675
676
677
678
679
680
681

	path1 = btrfs_alloc_path();
	path2 = btrfs_alloc_path();
	if (!path1 || !path2) {
		err = -ENOMEM;
		goto out;
	}
682
683
	path1->reada = READA_FORWARD;
	path2->reada = READA_FORWARD;
684

685
	node = alloc_backref_node(cache);
686
687
688
689
690
691
692
693
694
695
696
697
698
	if (!node) {
		err = -ENOMEM;
		goto out;
	}

	node->bytenr = bytenr;
	node->level = level;
	node->lowest = 1;
	cur = node;
again:
	end = 0;
	ptr = 0;
	key.objectid = cur->bytenr;
699
	key.type = BTRFS_METADATA_ITEM_KEY;
700
701
702
703
704
705
706
707
708
709
	key.offset = (u64)-1;

	path1->search_commit_root = 1;
	path1->skip_locking = 1;
	ret = btrfs_search_slot(NULL, rc->extent_root, &key, path1,
				0, 0);
	if (ret < 0) {
		err = ret;
		goto out;
	}
710
711
	ASSERT(ret);
	ASSERT(path1->slots[0]);
712
713
714
715
716
717

	path1->slots[0]--;

	WARN_ON(cur->checked);
	if (!list_empty(&cur->upper)) {
		/*
718
		 * the backref was added previously when processing
719
720
		 * backref of type BTRFS_TREE_BLOCK_REF_KEY
		 */
721
		ASSERT(list_is_singular(&cur->upper));
722
723
		edge = list_entry(cur->upper.next, struct backref_edge,
				  list[LOWER]);
724
		ASSERT(list_empty(&edge->list[UPPER]));
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
		exist = edge->node[UPPER];
		/*
		 * add the upper level block to pending list if we need
		 * check its backrefs
		 */
		if (!exist->checked)
			list_add_tail(&edge->list[UPPER], &list);
	} else {
		exist = NULL;
	}

	while (1) {
		cond_resched();
		eb = path1->nodes[0];

		if (ptr >= end) {
			if (path1->slots[0] >= btrfs_header_nritems(eb)) {
				ret = btrfs_next_leaf(rc->extent_root, path1);
				if (ret < 0) {
					err = ret;
					goto out;
				}
				if (ret > 0)
					break;
				eb = path1->nodes[0];
			}

			btrfs_item_key_to_cpu(eb, &key, path1->slots[0]);
			if (key.objectid != cur->bytenr) {
				WARN_ON(exist);
				break;
			}

758
759
			if (key.type == BTRFS_EXTENT_ITEM_KEY ||
			    key.type == BTRFS_METADATA_ITEM_KEY) {
760
761
762
763
764
765
766
767
768
769
				ret = find_inline_backref(eb, path1->slots[0],
							  &ptr, &end);
				if (ret)
					goto next;
			}
		}

		if (ptr < end) {
			/* update key for inline back ref */
			struct btrfs_extent_inline_ref *iref;
770
			int type;
771
			iref = (struct btrfs_extent_inline_ref *)ptr;
772
773
774
			type = btrfs_get_extent_inline_ref_type(eb, iref,
							BTRFS_REF_TYPE_BLOCK);
			if (type == BTRFS_REF_TYPE_INVALID) {
775
				err = -EUCLEAN;
776
777
778
				goto out;
			}
			key.type = type;
779
			key.offset = btrfs_extent_inline_ref_offset(eb, iref);
780

781
782
783
784
			WARN_ON(key.type != BTRFS_TREE_BLOCK_REF_KEY &&
				key.type != BTRFS_SHARED_BLOCK_REF_KEY);
		}

785
786
787
788
		/*
		 * Parent node found and matches current inline ref, no need to
		 * rebuild this node for this inline ref.
		 */
789
790
791
792
793
794
795
796
797
		if (exist &&
		    ((key.type == BTRFS_TREE_BLOCK_REF_KEY &&
		      exist->owner == key.offset) ||
		     (key.type == BTRFS_SHARED_BLOCK_REF_KEY &&
		      exist->bytenr == key.offset))) {
			exist = NULL;
			goto next;
		}

798
		/* SHARED_BLOCK_REF means key.offset is the parent bytenr */
799
800
801
		if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
			if (key.objectid == key.offset) {
				/*
802
803
				 * Only root blocks of reloc trees use backref
				 * pointing to itself.
804
805
				 */
				root = find_reloc_root(rc, cur->bytenr);
806
				ASSERT(root);
807
808
809
810
				cur->root = root;
				break;
			}

811
			edge = alloc_backref_edge(cache);
812
813
814
815
816
817
			if (!edge) {
				err = -ENOMEM;
				goto out;
			}
			rb_node = tree_search(&cache->rb_root, key.offset);
			if (!rb_node) {
818
				upper = alloc_backref_node(cache);
819
				if (!upper) {
820
					free_backref_edge(cache, edge);
821
822
823
824
825
826
827
828
829
830
831
832
833
					err = -ENOMEM;
					goto out;
				}
				upper->bytenr = key.offset;
				upper->level = cur->level + 1;
				/*
				 *  backrefs for the upper level block isn't
				 *  cached, add the block to pending list
				 */
				list_add_tail(&edge->list[UPPER], &list);
			} else {
				upper = rb_entry(rb_node, struct backref_node,
						 rb_node);
834
				ASSERT(upper->checked);
835
836
				INIT_LIST_HEAD(&edge->list[UPPER]);
			}
837
			list_add_tail(&edge->list[LOWER], &cur->upper);
838
			edge->node[LOWER] = cur;
839
			edge->node[UPPER] = upper;
840
841

			goto next;
842
		} else if (unlikely(key.type == BTRFS_EXTENT_REF_V0_KEY)) {
843
844
845
846
847
			err = -EINVAL;
			btrfs_print_v0_err(rc->extent_root->fs_info);
			btrfs_handle_fs_error(rc->extent_root->fs_info, err,
					      NULL);
			goto out;
848
849
850
851
		} else if (key.type != BTRFS_TREE_BLOCK_REF_KEY) {
			goto next;
		}

852
853
854
855
856
		/*
		 * key.type == BTRFS_TREE_BLOCK_REF_KEY, inline ref offset
		 * means the root objectid. We need to search the tree to get
		 * its parent bytenr.
		 */
857
858
859
860
861
862
		root = read_fs_root(rc->extent_root->fs_info, key.offset);
		if (IS_ERR(root)) {
			err = PTR_ERR(root);
			goto out;
		}

863
		if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
864
865
			cur->cowonly = 1;

866
867
		if (btrfs_root_level(&root->root_item) == cur->level) {
			/* tree root */
868
			ASSERT(btrfs_root_bytenr(&root->root_item) ==
869
			       cur->bytenr);
870
871
872
873
			if (should_ignore_root(root))
				list_add(&cur->list, &useless);
			else
				cur->root = root;
874
875
876
877
878
			break;
		}

		level = cur->level + 1;

879
		/* Search the tree to find parent blocks referring the block. */
880
881
882
883
884
885
886
887
888
		path2->search_commit_root = 1;
		path2->skip_locking = 1;
		path2->lowest_level = level;
		ret = btrfs_search_slot(NULL, root, node_key, path2, 0, 0);
		path2->lowest_level = 0;
		if (ret < 0) {
			err = ret;
			goto out;
		}
889
890
		if (ret > 0 && path2->slots[level] > 0)
			path2->slots[level]--;
891
892

		eb = path2->nodes[level];
893
894
895
896
		if (btrfs_node_blockptr(eb, path2->slots[level]) !=
		    cur->bytenr) {
			btrfs_err(root->fs_info,
	"couldn't find block (%llu) (level %d) in tree (%llu) with key (%llu %u %llu)",
897
898
				  cur->bytenr, level - 1,
				  root->root_key.objectid,
899
900
901
902
903
				  node_key->objectid, node_key->type,
				  node_key->offset);
			err = -ENOENT;
			goto out;
		}
904
		lower = cur;
905
		need_check = true;
906
907

		/* Add all nodes and edges in the path */
908
909
		for (; level < BTRFS_MAX_LEVEL; level++) {
			if (!path2->nodes[level]) {
910
				ASSERT(btrfs_root_bytenr(&root->root_item) ==
911
				       lower->bytenr);
912
913
914
915
				if (should_ignore_root(root))
					list_add(&lower->list, &useless);
				else
					lower->root = root;
916
917
918
				break;
			}

919
			edge = alloc_backref_edge(cache);
920
921
922
923
924
925
926
927
			if (!edge) {
				err = -ENOMEM;
				goto out;
			}

			eb = path2->nodes[level];
			rb_node = tree_search(&cache->rb_root, eb->start);
			if (!rb_node) {
928
				upper = alloc_backref_node(cache);
929
				if (!upper) {
930
					free_backref_edge(cache, edge);
931
932
933
934
935
936
					err = -ENOMEM;
					goto out;
				}
				upper->bytenr = eb->start;
				upper->owner = btrfs_header_owner(eb);
				upper->level = lower->level + 1;
937
938
				if (!test_bit(BTRFS_ROOT_REF_COWS,
					      &root->state))
939
					upper->cowonly = 1;
940
941
942
943
944
945
946
947
948
949
950
951

				/*
				 * if we know the block isn't shared
				 * we can void checking its backrefs.
				 */
				if (btrfs_block_can_be_shared(root, eb))
					upper->checked = 0;
				else
					upper->checked = 1;

				/*
				 * add the block to pending list if we
952
953
954
				 * need check its backrefs, we only do this once
				 * while walking up a tree as we will catch
				 * anything else later on.
955
				 */
956
957
				if (!upper->checked && need_check) {
					need_check = false;
958
959
					list_add_tail(&edge->list[UPPER],
						      &list);
960
961
962
				} else {
					if (upper->checked)
						need_check = true;
963
					INIT_LIST_HEAD(&edge->list[UPPER]);
964
				}
965
966
967
			} else {
				upper = rb_entry(rb_node, struct backref_node,
						 rb_node);
968
				ASSERT(upper->checked);
969
				INIT_LIST_HEAD(&edge->list[UPPER]);
970
971
				if (!upper->owner)
					upper->owner = btrfs_header_owner(eb);
972
973
974
			}
			list_add_tail(&edge->list[LOWER], &lower->upper);
			edge->node[LOWER] = lower;
975
			edge->node[UPPER] = upper;
976
977
978
979
980
981

			if (rb_node)
				break;
			lower = upper;
			upper = NULL;
		}
982
		btrfs_release_path(path2);
983
984
985
986
987
988
989
990
991
992
993
994
next:
		if (ptr < end) {
			ptr += btrfs_extent_inline_ref_size(key.type);
			if (ptr >= end) {
				WARN_ON(ptr > end);
				ptr = 0;
				end = 0;
			}
		}
		if (ptr >= end)
			path1->slots[0]++;
	}
995
	btrfs_release_path(path1);
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011

	cur->checked = 1;
	WARN_ON(exist);

	/* the pending list isn't empty, take the first block to process */
	if (!list_empty(&list)) {
		edge = list_entry(list.next, struct backref_edge, list[UPPER]);
		list_del_init(&edge->list[UPPER]);
		cur = edge->node[UPPER];
		goto again;
	}

	/*
	 * everything goes well, connect backref nodes and insert backref nodes
	 * into the cache.
	 */
1012
	ASSERT(node->checked);
1013
1014
1015
1016
	cowonly = node->cowonly;
	if (!cowonly) {
		rb_node = tree_insert(&cache->rb_root, node->bytenr,
				      &node->rb_node);
1017
1018
		if (rb_node)
			backref_tree_panic(rb_node, -EEXIST, node->bytenr);
1019
1020
		list_add_tail(&node->lower, &cache->leaves);
	}
1021
1022
1023
1024
1025
1026
1027
1028

	list_for_each_entry(edge, &node->upper, list[LOWER])
		list_add_tail(&edge->list[UPPER], &list);

	while (!list_empty(&list)) {
		edge = list_entry(list.next, struct backref_edge, list[UPPER]);
		list_del_init(&edge->list[UPPER]);
		upper = edge->node[UPPER];
1029
1030
1031
1032
1033
1034
1035
1036
		if (upper->detached) {
			list_del(&edge->list[LOWER]);
			lower = edge->node[LOWER];
			free_backref_edge(cache, edge);
			if (list_empty(&lower->upper))
				list_add(&lower->list, &useless);
			continue;
		}
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047

		if (!RB_EMPTY_NODE(&upper->rb_node)) {
			if (upper->lowest) {
				list_del_init(&upper->lower);
				upper->lowest = 0;
			}

			list_add_tail(&edge->list[UPPER], &upper->lower);
			continue;
		}

1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
		if (!upper->checked) {
			/*
			 * Still want to blow up for developers since this is a
			 * logic bug.
			 */
			ASSERT(0);
			err = -EINVAL;
			goto out;
		}
		if (cowonly != upper->cowonly) {
			ASSERT(0);
			err = -EINVAL;
			goto out;
		}

1063
1064
1065
		if (!cowonly) {
			rb_node = tree_insert(&cache->rb_root, upper->bytenr,
					      &upper->rb_node);
1066
1067
1068
			if (rb_node)
				backref_tree_panic(rb_node, -EEXIST,
						   upper->bytenr);
1069
		}
1070
1071
1072
1073
1074
1075

		list_add_tail(&edge->list[UPPER], &upper->lower);

		list_for_each_entry(edge, &upper->upper, list[LOWER])
			list_add_tail(&edge->list[UPPER], &list);
	}
1076
1077
1078
1079
1080
1081
1082
1083
1084
	/*
	 * process useless backref nodes. backref nodes for tree leaves
	 * are deleted from the cache. backref nodes for upper level
	 * tree blocks are left in the cache to avoid unnecessary backref
	 * lookup.
	 */
	while (!list_empty(&useless)) {
		upper = list_entry(useless.next, struct backref_node, list);
		list_del_init(&upper->list);
1085
		ASSERT(list_empty(&upper->upper));
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
		if (upper == node)
			node = NULL;
		if (upper->lowest) {
			list_del_init(&upper->lower);
			upper->lowest = 0;
		}
		while (!list_empty(&upper->lower)) {
			edge = list_entry(upper->lower.next,
					  struct backref_edge, list[UPPER]);
			list_del(&edge->list[UPPER]);
			list_del(&edge->list[LOWER]);
			lower = edge->node[LOWER];
			free_backref_edge(cache, edge);

			if (list_empty(&lower->upper))
				list_add(&lower->list, &useless);
		}
		__mark_block_processed(rc, upper);
		if (upper->level > 0) {
			list_add(&upper->list, &cache->detached);
			upper->detached = 1;
		} else {
			rb_erase(&upper->rb_node, &cache->rb_root);
			free_backref_node(cache, upper);
		}
	}
1112
1113
1114
1115
out:
	btrfs_free_path(path1);
	btrfs_free_path(path2);
	if (err) {
1116
1117
		while (!list_empty(&useless)) {
			lower = list_entry(useless.next,
1118
1119
					   struct backref_node, list);
			list_del_init(&lower->list);
1120
		}
1121
1122
1123
1124
		while (!list_empty(&list)) {
			edge = list_first_entry(&list, struct backref_edge,
						list[UPPER]);
			list_del(&edge->list[UPPER]);
1125
			list_del(&edge->list[LOWER]);
1126
			lower = edge->node[LOWER];
1127
			upper = edge->node[UPPER];
1128
			free_backref_edge(cache, edge);
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140

			/*
			 * Lower is no longer linked to any upper backref nodes
			 * and isn't in the cache, we can free it ourselves.
			 */
			if (list_empty(&lower->upper) &&
			    RB_EMPTY_NODE(&lower->rb_node))
				list_add(&lower->list, &useless);

			if (!RB_EMPTY_NODE(&upper->rb_node))
				continue;

1141
			/* Add this guy's upper edges to the list to process */
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
			list_for_each_entry(edge, &upper->upper, list[LOWER])
				list_add_tail(&edge->list[UPPER], &list);
			if (list_empty(&upper->upper))
				list_add(&upper->list, &useless);
		}

		while (!list_empty(&useless)) {
			lower = list_entry(useless.next,
					   struct backref_node, list);
			list_del_init(&lower->list);
1152
1153
			if (lower == node)
				node = NULL;
1154
			free_backref_node(cache, lower);
1155
		}
1156
1157

		free_backref_node(cache, node);
1158
1159
		return ERR_PTR(err);
	}
1160
	ASSERT(!node || !node->detached);
1161
1162
1163
	return node;
}

1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
/*
 * helper to add backref node for the newly created snapshot.
 * the backref node is created by cloning backref node that
 * corresponds to root of source tree
 */
static int clone_backref_node(struct btrfs_trans_handle *trans,
			      struct reloc_control *rc,
			      struct btrfs_root *src,
			      struct btrfs_root *dest)
{
	struct btrfs_root *reloc_root = src->reloc_root;
	struct backref_cache *cache = &rc->backref_cache;
	struct backref_node *node = NULL;
	struct backref_node *new_node;
	struct backref_edge *edge;
	struct backref_edge *new_edge;
	struct rb_node *rb_node;

	if (cache->last_trans > 0)
		update_backref_cache(trans, cache);

	rb_node = tree_search(&cache->rb_root, src->commit_root->start);
	if (rb_node) {
		node = rb_entry(rb_node, struct backref_node, rb_node);
		if (node->detached)
			node = NULL;
		else
			BUG_ON(node->new_bytenr != reloc_root->node->start);
	}

	if (!node) {
		rb_node = tree_search(&cache->rb_root,
				      reloc_root->commit_root->start);
		if (rb_node) {
			node = rb_entry(rb_node, struct backref_node,
					rb_node);
			BUG_ON(node->detached);
		}
	}

	if (!node)
		return 0;

	new_node = alloc_backref_node(cache);
	if (!new_node)
		return -ENOMEM;

	new_node->bytenr = dest->node->start;
	new_node->level = node->level;
	new_node->lowest = node->lowest;
Yan, Zheng's avatar
Yan, Zheng committed
1214
	new_node->checked = 1;
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
	new_node->root = dest;

	if (!node->lowest) {
		list_for_each_entry(edge, &node->lower, list[UPPER]) {
			new_edge = alloc_backref_edge(cache);
			if (!new_edge)
				goto fail;

			new_edge->node[UPPER] = new_node;
			new_edge->node[LOWER] = edge->node[LOWER];
			list_add_tail(&new_edge->list[UPPER],
				      &new_node->lower);
		}
Miao Xie's avatar
Miao Xie committed
1228
1229
	} else {
		list_add_tail(&new_node->lower, &cache->leaves);
1230
1231
1232
1233
	}

	rb_node = tree_insert(&cache->rb_root, new_node->bytenr,
			      &new_node->rb_node);
1234
1235
	if (rb_node)
		backref_tree_panic(rb_node, -EEXIST, new_node->bytenr);
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254

	if (!new_node->lowest) {
		list_for_each_entry(new_edge, &new_node->lower, list[UPPER]) {
			list_add_tail(&new_edge->list[LOWER],
				      &new_edge->node[LOWER]->upper);
		}
	}
	return 0;
fail:
	while (!list_empty(&new_node->lower)) {
		new_edge = list_entry(new_node->lower.next,
				      struct backref_edge, list[UPPER]);
		list_del(&new_edge->list[UPPER]);
		free_backref_edge(cache, new_edge);
	}
	free_backref_node(cache, new_node);
	return -ENOMEM;
}

1255
1256
1257
/*
 * helper to add 'address of tree root -> reloc tree' mapping
 */
1258
static int __must_check __add_reloc_root(struct btrfs_root *root)
1259
{
1260
	struct btrfs_fs_info *fs_info = root->fs_info;
1261
1262
	struct rb_node *rb_node;
	struct mapping_node *node;
1263
	struct reloc_control *rc = fs_info->reloc_ctl;
1264
1265

	node = kmalloc(sizeof(*node), GFP_NOFS);
1266
1267
	if (!node)
		return -ENOMEM;
1268
1269
1270
1271
1272
1273
1274
1275

	node->bytenr = root->node->start;
	node->data = root;

	spin_lock(&rc->reloc_root_tree.lock);
	rb_node = tree_insert(&rc->reloc_root_tree.rb_root,
			      node->bytenr, &node->rb_node);
	spin_unlock(&rc->reloc_root_tree.lock);
1276
	if (rb_node) {
1277
		btrfs_panic(fs_info, -EEXIST,
Jeff Mahoney's avatar
Jeff Mahoney committed
1278
1279
			    "Duplicate root found for start=%llu while inserting into relocation tree",
			    node->bytenr);
1280
	}
1281
1282
1283
1284
1285
1286

	list_add_tail(&root->root_list, &rc->reloc_roots);
	return 0;
}

/*
1287
 * helper to delete the 'address of tree root -> reloc tree'
1288
1289
 * mapping
 */
1290
static void __del_reloc_root(struct btrfs_root *root)
1291
{
1292
	struct btrfs_fs_info *fs_info = root->fs_info;
1293
1294
	struct rb_node *rb_node;
	struct mapping_node *node = NULL;
1295
	struct reloc_control *rc = fs_info->reloc_ctl;
1296

1297
	if (rc && root->node) {
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
		spin_lock(&rc->reloc_root_tree.lock);
		rb_node = tree_search(&rc->reloc_root_tree.rb_root,
				      root->node->start);
		if (rb_node) {
			node = rb_entry(rb_node, struct mapping_node, rb_node);
			rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
		}
		spin_unlock(&rc->reloc_root_tree.lock);
		if (!node)
			return;
		BUG_ON((struct btrfs_root *)node->data != root);
1309
1310
	}

1311
	spin_lock(&fs_info->trans_lock);
1312
	list_del_init(&root->root_list);
1313
	spin_unlock(&fs_info->trans_lock);
1314
1315
1316
1317
1318
1319
1320
1321
1322
	kfree(node);
}

/*
 * helper to update the 'address of tree root -> reloc tree'
 * mapping
 */
static int __update_reloc_root(struct btrfs_root *root, u64 new_bytenr)
{
1323
	struct btrfs_fs_info *fs_info = root->fs_info;
1324
1325
	struct rb_node *rb_node;
	struct mapping_node *node = NULL;
1326
	struct reloc_control *rc = fs_info->reloc_ctl;
1327
1328
1329
1330
1331
1332
1333

	spin_lock(&rc->reloc_root_tree.lock);
	rb_node = tree_search(&rc->reloc_root_tree.rb_root,
			      root->node->start);
	if (rb_node) {
		node = rb_entry(rb_node, struct mapping_node, rb_node);
		rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
1334
	}
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
	spin_unlock(&rc->reloc_root_tree.lock);

	if (!node)
		return 0;
	BUG_ON((struct btrfs_root *)node->data != root);

	spin_lock(&rc->reloc_root_tree.lock);
	node->bytenr = new_bytenr;
	rb_node = tree_insert(&rc->reloc_root_tree.rb_root,
			      node->bytenr, &node->rb_node);
	spin_unlock(&rc->reloc_root_tree.lock);
	if (rb_node)
		backref_tree_panic(rb_node, -EEXIST, node->bytenr);
1348
1349
1350
	return 0;
}

1351
1352
static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans,
					struct btrfs_root *root, u64 objectid)
1353
{
1354
	struct btrfs_fs_info *fs_info = root->fs_info;
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
	struct btrfs_root *reloc_root;
	struct extent_buffer *eb;
	struct btrfs_root_item *root_item;
	struct btrfs_key root_key;
	int ret;

	root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
	BUG_ON(!root_item);

	root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
	root_key.type = BTRFS_ROOT_ITEM_KEY;
1366
	root_key.offset = objectid;
1367

1368
	if (root->root_key.objectid == objectid) {
1369
1370
		u64 commit_root_gen;

1371
1372
1373
1374
		/* called by btrfs_init_reloc_root */
		ret = btrfs_copy_root(trans, root, root->commit_root, &eb,
				      BTRFS_TREE_RELOC_OBJECTID);
		BUG_ON(ret);
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
		/*
		 * Set the last_snapshot field to the generation of the commit
		 * root - like this ctree.c:btrfs_block_can_be_shared() behaves
		 * correctly (returns true) when the relocation root is created
		 * either inside the critical section of a transaction commit
		 * (through transaction.c:qgroup_account_snapshot()) and when
		 * it's created before the transaction commit is started.
		 */
		commit_root_gen = btrfs_header_generation(root->commit_root);
		btrfs_set_root_last_snapshot(&root->root_item, commit_root_gen);
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
	} else {
		/*
		 * called by btrfs_reloc_post_snapshot_hook.
		 * the source tree is a reloc tree, all tree blocks
		 * modified after it was created have RELOC flag
		 * set in their headers. so it's OK to not update
		 * the 'last_snapshot'.
		 */
		ret = btrfs_copy_root(trans, root, root->node, &eb,
				      BTRFS_TREE_RELOC_OBJECTID);
		BUG_ON(ret);
	}
1397
1398
1399
1400
1401

	memcpy(root_item, &root->root_item, sizeof(*root_item));
	btrfs_set_root_bytenr(root_item, eb->start);
	btrfs_set_root_level(root_item, btrfs_header_level(eb));
	btrfs_set_root_generation(root_item, trans->transid);
1402
1403
1404
1405
1406
1407
1408

	if (root->root_key.objectid == objectid) {
		btrfs_set_root_refs(root_item, 0);
		memset(&root_item->drop_progress, 0,
		       sizeof(struct btrfs_disk_key));
		root_item->drop_level = 0;
	}
1409
1410
1411
1412

	btrfs_tree_unlock(eb);
	free_extent_buffer(eb);

1413
	ret = btrfs_insert_root(trans, fs_info->tree_root,
1414
1415
1416
1417
				&root_key, root_item);
	BUG_ON(ret);
	kfree(root_item);

1418
	reloc_root = btrfs_read_fs_root(fs_info->tree_root, &root_key);
1419
1420
	BUG_ON(IS_ERR(reloc_root));
	reloc_root->last_trans = trans->transid;
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
	return reloc_root;
}

/*
 * create reloc tree for a given fs tree. reloc tree is just a
 * snapshot of the fs tree with special root objectid.
 */
int btrfs_init_reloc_root(struct btrfs_trans_handle *trans,
			  struct btrfs_root *root)
{
1431
	struct btrfs_fs_info *fs_info = root->fs_info;
1432
	struct btrfs_root *reloc_root;
1433
	struct reloc_control *rc = fs_info->reloc_ctl;
1434
	struct btrfs_block_rsv *rsv;
1435
	int clear_rsv = 0;
1436
	int ret;
1437

1438
1439
1440
1441
1442
1443
1444
	/*
	 * The subvolume has reloc tree but the swap is finished, no need to
	 * create/update the dead reloc tree
	 */
	if (test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state))
		return 0;

1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
	if (root->reloc_root) {
		reloc_root = root->reloc_root;
		reloc_root->last_trans = trans->transid;
		return 0;
	}

	if (!rc || !rc->create_reloc_tree ||
	    root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
		return 0;

1455
1456
	if (!trans->reloc_reserved) {
		rsv = trans->block_rsv;
1457
1458
1459
1460
1461
		trans->block_rsv = rc->block_rsv;
		clear_rsv = 1;
	}
	reloc_root = create_reloc_root(trans, root, root->root_key.objectid);
	if (clear_rsv)
1462
		trans->block_rsv = rsv;
1463

1464
1465
	ret = __add_reloc_root(reloc_root);
	BUG_ON(ret < 0);
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
	root->reloc_root = reloc_root;
	return 0;
}

/*
 * update root item of reloc tree
 */
int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
			    struct btrfs_root *root)
{
1476
	struct btrfs_fs_info *fs_info = root->fs_info;
1477
1478
1479
1480
	struct btrfs_root *reloc_root;
	struct btrfs_root_item *root_item;
	int ret;

1481
1482
	if (test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state) ||
	    !root->reloc_root)
Chris Mason's avatar
Chris Mason committed
1483
		goto out;
1484
1485
1486
1487

	reloc_root = root->reloc_root;
	root_item = &reloc_root->root_item;

1488
	/* root->reloc_root will stay until current relocation finished */
1489
	if (fs_info->reloc_ctl->merge_reloc_tree &&
1490
	    btrfs_root_refs(root_item) == 0) {
1491
		set_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state);
1492
		__del_reloc_root(reloc_root);
1493
1494
1495
1496
1497
1498
1499
1500
	}

	if (reloc_root->commit_root != reloc_root->node) {
		btrfs_set_root_node(root_item, reloc_root->node);
		free_extent_buffer(reloc_root->commit_root);
		reloc_root->commit_root = btrfs_root_node(reloc_root);
	}

1501
	ret = btrfs_update_root(trans, fs_info->tree_root,