xref: /linux/fs/btrfs/disk-io.c (revision 7a40974fd0efa3698de4c6d1d0ee0436bcc4445d)
1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0
26cbd5570SChris Mason /*
36cbd5570SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
46cbd5570SChris Mason  */
56cbd5570SChris Mason 
6e20d96d6SChris Mason #include <linux/fs.h>
7d98237b3SChris Mason #include <linux/blkdev.h>
8fc7cbcd4SDavid Sterba #include <linux/radix-tree.h>
935b7e476SChris Mason #include <linux/writeback.h>
10ce9adaa5SChris Mason #include <linux/workqueue.h>
11a74a4b97SChris Mason #include <linux/kthread.h>
125a0e3ad6STejun Heo #include <linux/slab.h>
13784b4e29SChris Mason #include <linux/migrate.h>
147a36ddecSDavid Sterba #include <linux/ratelimit.h>
156463fe58SStefan Behrens #include <linux/uuid.h>
16803b2f54SStefan Behrens #include <linux/semaphore.h>
17540adea3SMasami Hiramatsu #include <linux/error-injection.h>
189678c543SNikolay Borisov #include <linux/crc32c.h>
19b89f6d1fSFilipe Manana #include <linux/sched/mm.h>
207e75bf3fSDavid Sterba #include <asm/unaligned.h>
216d97c6e3SJohannes Thumshirn #include <crypto/hash.h>
22eb60ceacSChris Mason #include "ctree.h"
23eb60ceacSChris Mason #include "disk-io.h"
24e089f05cSChris Mason #include "transaction.h"
250f7d52f4SChris Mason #include "btrfs_inode.h"
26103c1972SChristoph Hellwig #include "bio.h"
27db94535dSChris Mason #include "print-tree.h"
28925baeddSChris Mason #include "locking.h"
29e02119d5SChris Mason #include "tree-log.h"
30fa9c0d79SChris Mason #include "free-space-cache.h"
3170f6d82eSOmar Sandoval #include "free-space-tree.h"
328dabb742SStefan Behrens #include "dev-replace.h"
3353b381b3SDavid Woodhouse #include "raid56.h"
345ac1d209SJeff Mahoney #include "sysfs.h"
35fcebe456SJosef Bacik #include "qgroup.h"
36ebb8765bSAnand Jain #include "compression.h"
37557ea5ddSQu Wenruo #include "tree-checker.h"
38fd708b81SJosef Bacik #include "ref-verify.h"
39aac0023cSJosef Bacik #include "block-group.h"
40b0643e59SDennis Zhou #include "discard.h"
41f603bb94SNikolay Borisov #include "space-info.h"
42b70f5097SNaohiro Aota #include "zoned.h"
43139e8cd3SQu Wenruo #include "subpage.h"
44c7f13d42SJosef Bacik #include "fs.h"
4507e81dc9SJosef Bacik #include "accessors.h"
46a0231804SJosef Bacik #include "extent-tree.h"
4745c40c8fSJosef Bacik #include "root-tree.h"
4859b818e0SJosef Bacik #include "defrag.h"
49c7a03b52SJosef Bacik #include "uuid-tree.h"
5067707479SJosef Bacik #include "relocation.h"
512fc6822cSJosef Bacik #include "scrub.h"
52c03b2207SJosef Bacik #include "super.h"
53eb60ceacSChris Mason 
54319e4d06SQu Wenruo #define BTRFS_SUPER_FLAG_SUPP	(BTRFS_HEADER_FLAG_WRITTEN |\
55319e4d06SQu Wenruo 				 BTRFS_HEADER_FLAG_RELOC |\
56319e4d06SQu Wenruo 				 BTRFS_SUPER_FLAG_ERROR |\
57319e4d06SQu Wenruo 				 BTRFS_SUPER_FLAG_SEEDING |\
58e2731e55SAnand Jain 				 BTRFS_SUPER_FLAG_METADUMP |\
59e2731e55SAnand Jain 				 BTRFS_SUPER_FLAG_METADUMP_V2)
60319e4d06SQu Wenruo 
612ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info);
622ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info);
63ce9adaa5SChris Mason 
btrfs_free_csum_hash(struct btrfs_fs_info * fs_info)64141386e1SJosef Bacik static void btrfs_free_csum_hash(struct btrfs_fs_info *fs_info)
65141386e1SJosef Bacik {
66141386e1SJosef Bacik 	if (fs_info->csum_shash)
67141386e1SJosef Bacik 		crypto_free_shash(fs_info->csum_shash);
68141386e1SJosef Bacik }
69141386e1SJosef Bacik 
70d352ac68SChris Mason /*
712996e1f8SJohannes Thumshirn  * Compute the csum of a btree block and store the result to provided buffer.
72d352ac68SChris Mason  */
csum_tree_block(struct extent_buffer * buf,u8 * result)73c67b3892SDavid Sterba static void csum_tree_block(struct extent_buffer *buf, u8 *result)
7419c00ddcSChris Mason {
75d5178578SJohannes Thumshirn 	struct btrfs_fs_info *fs_info = buf->fs_info;
76397239edSQu Wenruo 	int num_pages;
77397239edSQu Wenruo 	u32 first_page_part;
78d5178578SJohannes Thumshirn 	SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
7919c00ddcSChris Mason 	char *kaddr;
80e9be5a30SDavid Sterba 	int i;
81d5178578SJohannes Thumshirn 
82d5178578SJohannes Thumshirn 	shash->tfm = fs_info->csum_shash;
83d5178578SJohannes Thumshirn 	crypto_shash_init(shash);
84397239edSQu Wenruo 
85397239edSQu Wenruo 	if (buf->addr) {
86397239edSQu Wenruo 		/* Pages are contiguous, handle them as a big one. */
87397239edSQu Wenruo 		kaddr = buf->addr;
88397239edSQu Wenruo 		first_page_part = fs_info->nodesize;
89397239edSQu Wenruo 		num_pages = 1;
90397239edSQu Wenruo 	} else {
91082d5bb9SQu Wenruo 		kaddr = folio_address(buf->folios[0]);
92397239edSQu Wenruo 		first_page_part = min_t(u32, PAGE_SIZE, fs_info->nodesize);
93397239edSQu Wenruo 		num_pages = num_extent_pages(buf);
94397239edSQu Wenruo 	}
95397239edSQu Wenruo 
96e9be5a30SDavid Sterba 	crypto_shash_update(shash, kaddr + BTRFS_CSUM_SIZE,
97a26663e7SQu Wenruo 			    first_page_part - BTRFS_CSUM_SIZE);
9819c00ddcSChris Mason 
9913df3775SQu Wenruo 	/*
10013df3775SQu Wenruo 	 * Multiple single-page folios case would reach here.
10113df3775SQu Wenruo 	 *
10213df3775SQu Wenruo 	 * nodesize <= PAGE_SIZE and large folio all handled by above
10313df3775SQu Wenruo 	 * crypto_shash_update() already.
10413df3775SQu Wenruo 	 */
1055ad9b471Spengfuyuan 	for (i = 1; i < num_pages && INLINE_EXTENT_BUFFER_PAGES > 1; i++) {
106082d5bb9SQu Wenruo 		kaddr = folio_address(buf->folios[i]);
107e9be5a30SDavid Sterba 		crypto_shash_update(shash, kaddr, PAGE_SIZE);
10819c00ddcSChris Mason 	}
10971a63551SDavid Sterba 	memset(result, 0, BTRFS_CSUM_SIZE);
110d5178578SJohannes Thumshirn 	crypto_shash_final(shash, result);
11119c00ddcSChris Mason }
11219c00ddcSChris Mason 
113d352ac68SChris Mason /*
114d352ac68SChris Mason  * we can't consider a given block up to date unless the transid of the
115d352ac68SChris Mason  * block matches the transid in the parent node's pointer.  This is how we
116d352ac68SChris Mason  * detect blocks that either didn't get written at all or got written
117d352ac68SChris Mason  * in the wrong place.
118d352ac68SChris Mason  */
btrfs_buffer_uptodate(struct extent_buffer * eb,u64 parent_transid,int atomic)119d87e6575SChristoph Hellwig int btrfs_buffer_uptodate(struct extent_buffer *eb, u64 parent_transid, int atomic)
1201259ab75SChris Mason {
121d87e6575SChristoph Hellwig 	if (!extent_buffer_uptodate(eb))
122d87e6575SChristoph Hellwig 		return 0;
1231259ab75SChris Mason 
1241259ab75SChris Mason 	if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
125d87e6575SChristoph Hellwig 		return 1;
1261259ab75SChris Mason 
127b9fab919SChris Mason 	if (atomic)
128b9fab919SChris Mason 		return -EAGAIN;
129b9fab919SChris Mason 
130d87e6575SChristoph Hellwig 	if (!extent_buffer_uptodate(eb) ||
131d87e6575SChristoph Hellwig 	    btrfs_header_generation(eb) != parent_transid) {
13294647322SDavid Sterba 		btrfs_err_rl(eb->fs_info,
1338f0ed7d4SQu Wenruo "parent transid verify failed on logical %llu mirror %u wanted %llu found %llu",
1348f0ed7d4SQu Wenruo 			eb->start, eb->read_mirror,
13529549aecSWang Shilong 			parent_transid, btrfs_header_generation(eb));
1360b32f4bbSJosef Bacik 		clear_extent_buffer_uptodate(eb);
1379e2aff90SChristoph Hellwig 		return 0;
138d87e6575SChristoph Hellwig 	}
1399e2aff90SChristoph Hellwig 	return 1;
1401259ab75SChris Mason }
1411259ab75SChris Mason 
btrfs_supported_super_csum(u16 csum_type)142e7e16f48SJohannes Thumshirn static bool btrfs_supported_super_csum(u16 csum_type)
143e7e16f48SJohannes Thumshirn {
144e7e16f48SJohannes Thumshirn 	switch (csum_type) {
145e7e16f48SJohannes Thumshirn 	case BTRFS_CSUM_TYPE_CRC32:
1463951e7f0SJohannes Thumshirn 	case BTRFS_CSUM_TYPE_XXHASH:
1473831bf00SJohannes Thumshirn 	case BTRFS_CSUM_TYPE_SHA256:
148352ae07bSDavid Sterba 	case BTRFS_CSUM_TYPE_BLAKE2:
149e7e16f48SJohannes Thumshirn 		return true;
150e7e16f48SJohannes Thumshirn 	default:
151e7e16f48SJohannes Thumshirn 		return false;
152e7e16f48SJohannes Thumshirn 	}
153e7e16f48SJohannes Thumshirn }
154e7e16f48SJohannes Thumshirn 
155d352ac68SChris Mason /*
1561104a885SDavid Sterba  * Return 0 if the superblock checksum type matches the checksum value of that
1571104a885SDavid Sterba  * algorithm. Pass the raw disk superblock data.
1581104a885SDavid Sterba  */
btrfs_check_super_csum(struct btrfs_fs_info * fs_info,const struct btrfs_super_block * disk_sb)1593d17adeaSQu Wenruo int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
1603d17adeaSQu Wenruo 			   const struct btrfs_super_block *disk_sb)
1611104a885SDavid Sterba {
16251bce6c9SJohannes Thumshirn 	char result[BTRFS_CSUM_SIZE];
163d5178578SJohannes Thumshirn 	SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
164d5178578SJohannes Thumshirn 
165d5178578SJohannes Thumshirn 	shash->tfm = fs_info->csum_shash;
1661104a885SDavid Sterba 
1671104a885SDavid Sterba 	/*
1681104a885SDavid Sterba 	 * The super_block structure does not span the whole
16951bce6c9SJohannes Thumshirn 	 * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space is
17051bce6c9SJohannes Thumshirn 	 * filled with zeros and is included in the checksum.
1711104a885SDavid Sterba 	 */
1723d17adeaSQu Wenruo 	crypto_shash_digest(shash, (const u8 *)disk_sb + BTRFS_CSUM_SIZE,
173fd08001fSEric Biggers 			    BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, result);
1741104a885SDavid Sterba 
17555fc29beSDavid Sterba 	if (memcmp(disk_sb->csum, result, fs_info->csum_size))
176e7e16f48SJohannes Thumshirn 		return 1;
1771104a885SDavid Sterba 
178e7e16f48SJohannes Thumshirn 	return 0;
1791104a885SDavid Sterba }
1801104a885SDavid Sterba 
btrfs_repair_eb_io_failure(const struct extent_buffer * eb,int mirror_num)181bacf60e5SChristoph Hellwig static int btrfs_repair_eb_io_failure(const struct extent_buffer *eb,
182bacf60e5SChristoph Hellwig 				      int mirror_num)
183bacf60e5SChristoph Hellwig {
184bacf60e5SChristoph Hellwig 	struct btrfs_fs_info *fs_info = eb->fs_info;
18596c36eaaSQu Wenruo 	int num_folios = num_extent_folios(eb);
186bacf60e5SChristoph Hellwig 	int ret = 0;
187bacf60e5SChristoph Hellwig 
188bacf60e5SChristoph Hellwig 	if (sb_rdonly(fs_info->sb))
189bacf60e5SChristoph Hellwig 		return -EROFS;
190bacf60e5SChristoph Hellwig 
19196c36eaaSQu Wenruo 	for (int i = 0; i < num_folios; i++) {
19296c36eaaSQu Wenruo 		struct folio *folio = eb->folios[i];
19396c36eaaSQu Wenruo 		u64 start = max_t(u64, eb->start, folio_pos(folio));
194082d5bb9SQu Wenruo 		u64 end = min_t(u64, eb->start + eb->len,
19584cda1a6SQu Wenruo 				folio_pos(folio) + eb->folio_size);
196917ac778SQu Wenruo 		u32 len = end - start;
197bacf60e5SChristoph Hellwig 
198917ac778SQu Wenruo 		ret = btrfs_repair_io_failure(fs_info, 0, start, len,
19996c36eaaSQu Wenruo 					      start, folio, offset_in_folio(folio, start),
20096c36eaaSQu Wenruo 					      mirror_num);
201bacf60e5SChristoph Hellwig 		if (ret)
202bacf60e5SChristoph Hellwig 			break;
203bacf60e5SChristoph Hellwig 	}
204bacf60e5SChristoph Hellwig 
205bacf60e5SChristoph Hellwig 	return ret;
206bacf60e5SChristoph Hellwig }
207bacf60e5SChristoph Hellwig 
2081104a885SDavid Sterba /*
209d352ac68SChris Mason  * helper to read a given tree block, doing retries as required when
210d352ac68SChris Mason  * the checksums don't match and we have alternate mirrors to try.
211581c1760SQu Wenruo  *
212789d6a3aSQu Wenruo  * @check:		expected tree parentness check, see the comments of the
213789d6a3aSQu Wenruo  *			structure for details.
214d352ac68SChris Mason  */
btrfs_read_extent_buffer(struct extent_buffer * eb,const struct btrfs_tree_parent_check * check)2156a2e9dc4SFilipe Manana int btrfs_read_extent_buffer(struct extent_buffer *eb,
2162917f741SDavid Sterba 			     const struct btrfs_tree_parent_check *check)
217f188591eSChris Mason {
2185ab12d1fSDavid Sterba 	struct btrfs_fs_info *fs_info = eb->fs_info;
219ea466794SJosef Bacik 	int failed = 0;
220f188591eSChris Mason 	int ret;
221f188591eSChris Mason 	int num_copies = 0;
222f188591eSChris Mason 	int mirror_num = 0;
223ea466794SJosef Bacik 	int failed_mirror = 0;
224f188591eSChris Mason 
225789d6a3aSQu Wenruo 	ASSERT(check);
226789d6a3aSQu Wenruo 
227f188591eSChris Mason 	while (1) {
228f8397d69SNikolay Borisov 		clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags);
229947a6299SQu Wenruo 		ret = read_extent_buffer_pages(eb, WAIT_COMPLETE, mirror_num, check);
230947a6299SQu Wenruo 		if (!ret)
231581c1760SQu Wenruo 			break;
232d397712bSChris Mason 
2330b246afaSJeff Mahoney 		num_copies = btrfs_num_copies(fs_info,
234f188591eSChris Mason 					      eb->start, eb->len);
2354235298eSChris Mason 		if (num_copies == 1)
236ea466794SJosef Bacik 			break;
2374235298eSChris Mason 
2385cf1ab56SJosef Bacik 		if (!failed_mirror) {
2395cf1ab56SJosef Bacik 			failed = 1;
2405cf1ab56SJosef Bacik 			failed_mirror = eb->read_mirror;
2415cf1ab56SJosef Bacik 		}
2425cf1ab56SJosef Bacik 
243f188591eSChris Mason 		mirror_num++;
244ea466794SJosef Bacik 		if (mirror_num == failed_mirror)
245ea466794SJosef Bacik 			mirror_num++;
246ea466794SJosef Bacik 
2474235298eSChris Mason 		if (mirror_num > num_copies)
248ea466794SJosef Bacik 			break;
249f188591eSChris Mason 	}
250ea466794SJosef Bacik 
251c0901581SStefan Behrens 	if (failed && !ret && failed_mirror)
25220a1fbf9SDavid Sterba 		btrfs_repair_eb_io_failure(eb, failed_mirror);
253ea466794SJosef Bacik 
254ea466794SJosef Bacik 	return ret;
255f188591eSChris Mason }
25619c00ddcSChris Mason 
25731d89399SChristoph Hellwig /*
25831d89399SChristoph Hellwig  * Checksum a dirty tree block before IO.
25931d89399SChristoph Hellwig  */
btree_csum_one_bio(struct btrfs_bio * bbio)26031d89399SChristoph Hellwig blk_status_t btree_csum_one_bio(struct btrfs_bio *bbio)
261eca0f6f6SQu Wenruo {
26231d89399SChristoph Hellwig 	struct extent_buffer *eb = bbio->private;
263eca0f6f6SQu Wenruo 	struct btrfs_fs_info *fs_info = eb->fs_info;
26431d89399SChristoph Hellwig 	u64 found_start = btrfs_header_bytenr(eb);
2650124855fSFilipe Manana 	u64 last_trans;
266eca0f6f6SQu Wenruo 	u8 result[BTRFS_CSUM_SIZE];
267eca0f6f6SQu Wenruo 	int ret;
268eca0f6f6SQu Wenruo 
26931d89399SChristoph Hellwig 	/* Btree blocks are always contiguous on disk. */
27031d89399SChristoph Hellwig 	if (WARN_ON_ONCE(bbio->file_offset != eb->start))
27131d89399SChristoph Hellwig 		return BLK_STS_IOERR;
27231d89399SChristoph Hellwig 	if (WARN_ON_ONCE(bbio->bio.bi_iter.bi_size != eb->len))
27331d89399SChristoph Hellwig 		return BLK_STS_IOERR;
27431d89399SChristoph Hellwig 
275aa6313e6SJohannes Thumshirn 	/*
276aa6313e6SJohannes Thumshirn 	 * If an extent_buffer is marked as EXTENT_BUFFER_ZONED_ZEROOUT, don't
277aa6313e6SJohannes Thumshirn 	 * checksum it but zero-out its content. This is done to preserve
278aa6313e6SJohannes Thumshirn 	 * ordering of I/O without unnecessarily writing out data.
279aa6313e6SJohannes Thumshirn 	 */
280cbf44cd9SJohannes Thumshirn 	if (test_bit(EXTENT_BUFFER_ZONED_ZEROOUT, &eb->bflags)) {
281aa6313e6SJohannes Thumshirn 		memzero_extent_buffer(eb, 0, eb->len);
28231d89399SChristoph Hellwig 		return BLK_STS_OK;
28331d89399SChristoph Hellwig 	}
28431d89399SChristoph Hellwig 
28531d89399SChristoph Hellwig 	if (WARN_ON_ONCE(found_start != eb->start))
28631d89399SChristoph Hellwig 		return BLK_STS_IOERR;
28755151ea9SQu Wenruo 	if (WARN_ON(!btrfs_folio_test_uptodate(fs_info, eb->folios[0],
288082d5bb9SQu Wenruo 					       eb->start, eb->len)))
28931d89399SChristoph Hellwig 		return BLK_STS_IOERR;
29031d89399SChristoph Hellwig 
291eca0f6f6SQu Wenruo 	ASSERT(memcmp_extent_buffer(eb, fs_info->fs_devices->metadata_uuid,
292eca0f6f6SQu Wenruo 				    offsetof(struct btrfs_header, fsid),
293eca0f6f6SQu Wenruo 				    BTRFS_FSID_SIZE) == 0);
294eca0f6f6SQu Wenruo 	csum_tree_block(eb, result);
295eca0f6f6SQu Wenruo 
296eca0f6f6SQu Wenruo 	if (btrfs_header_level(eb))
297eca0f6f6SQu Wenruo 		ret = btrfs_check_node(eb);
298eca0f6f6SQu Wenruo 	else
29985d8a826SJosef Bacik 		ret = btrfs_check_leaf(eb);
300eca0f6f6SQu Wenruo 
3013777369fSQu Wenruo 	if (ret < 0)
3023777369fSQu Wenruo 		goto error;
3033777369fSQu Wenruo 
3043777369fSQu Wenruo 	/*
3053777369fSQu Wenruo 	 * Also check the generation, the eb reached here must be newer than
3063777369fSQu Wenruo 	 * last committed. Or something seriously wrong happened.
3073777369fSQu Wenruo 	 */
3080124855fSFilipe Manana 	last_trans = btrfs_get_last_trans_committed(fs_info);
3090124855fSFilipe Manana 	if (unlikely(btrfs_header_generation(eb) <= last_trans)) {
3103777369fSQu Wenruo 		ret = -EUCLEAN;
311eca0f6f6SQu Wenruo 		btrfs_err(fs_info,
3123777369fSQu Wenruo 			"block=%llu bad generation, have %llu expect > %llu",
3130124855fSFilipe Manana 			  eb->start, btrfs_header_generation(eb), last_trans);
3143777369fSQu Wenruo 		goto error;
315eca0f6f6SQu Wenruo 	}
316eca0f6f6SQu Wenruo 	write_extent_buffer(eb, result, 0, fs_info->csum_size);
31731d89399SChristoph Hellwig 	return BLK_STS_OK;
3183777369fSQu Wenruo 
3193777369fSQu Wenruo error:
3203777369fSQu Wenruo 	btrfs_print_tree(eb, 0);
3213777369fSQu Wenruo 	btrfs_err(fs_info, "block=%llu write time tree block corruption detected",
3223777369fSQu Wenruo 		  eb->start);
32316199ad9SFilipe Manana 	/*
32416199ad9SFilipe Manana 	 * Be noisy if this is an extent buffer from a log tree. We don't abort
32516199ad9SFilipe Manana 	 * a transaction in case there's a bad log tree extent buffer, we just
32616199ad9SFilipe Manana 	 * fallback to a transaction commit. Still we want to know when there is
32716199ad9SFilipe Manana 	 * a bad log tree extent buffer, as that may signal a bug somewhere.
32816199ad9SFilipe Manana 	 */
32916199ad9SFilipe Manana 	WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG) ||
33016199ad9SFilipe Manana 		btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID);
331deb6216fSChristoph Hellwig 	return errno_to_blk_status(ret);
332deb6216fSChristoph Hellwig }
333deb6216fSChristoph Hellwig 
check_tree_block_fsid(struct extent_buffer * eb)334413fb1bcSAnand Jain static bool check_tree_block_fsid(struct extent_buffer *eb)
3352b82032cSYan Zheng {
336b0c9b3b0SDavid Sterba 	struct btrfs_fs_info *fs_info = eb->fs_info;
337944d3f9fSNikolay Borisov 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices, *seed_devs;
33844880fdcSAnand Jain 	u8 fsid[BTRFS_FSID_SIZE];
3392b82032cSYan Zheng 
3409a8658e3SDavid Sterba 	read_extent_buffer(eb, fsid, offsetof(struct btrfs_header, fsid),
3419a8658e3SDavid Sterba 			   BTRFS_FSID_SIZE);
3427239ff4bSNikolay Borisov 
34367bc5ad0SAnand Jain 	/*
344f7361d8cSAnand Jain 	 * alloc_fsid_devices() copies the fsid into fs_devices::metadata_uuid.
345f7361d8cSAnand Jain 	 * This is then overwritten by metadata_uuid if it is present in the
346f7361d8cSAnand Jain 	 * device_list_add(). The same true for a seed device as well. So use of
347f7361d8cSAnand Jain 	 * fs_devices::metadata_uuid is appropriate here.
34867bc5ad0SAnand Jain 	 */
34967bc5ad0SAnand Jain 	if (memcmp(fsid, fs_info->fs_devices->metadata_uuid, BTRFS_FSID_SIZE) == 0)
350413fb1bcSAnand Jain 		return false;
351944d3f9fSNikolay Borisov 
352944d3f9fSNikolay Borisov 	list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list)
353944d3f9fSNikolay Borisov 		if (!memcmp(fsid, seed_devs->fsid, BTRFS_FSID_SIZE))
354413fb1bcSAnand Jain 			return false;
355944d3f9fSNikolay Borisov 
356413fb1bcSAnand Jain 	return true;
3572b82032cSYan Zheng }
3582b82032cSYan Zheng 
35977bf40a2SQu Wenruo /* Do basic extent buffer checks at read time */
btrfs_validate_extent_buffer(struct extent_buffer * eb,const struct btrfs_tree_parent_check * check)360046b562bSChristoph Hellwig int btrfs_validate_extent_buffer(struct extent_buffer *eb,
3612917f741SDavid Sterba 				 const struct btrfs_tree_parent_check *check)
362ce9adaa5SChris Mason {
36377bf40a2SQu Wenruo 	struct btrfs_fs_info *fs_info = eb->fs_info;
364ce9adaa5SChris Mason 	u64 found_start;
36577bf40a2SQu Wenruo 	const u32 csum_size = fs_info->csum_size;
36677bf40a2SQu Wenruo 	u8 found_level;
3672996e1f8SJohannes Thumshirn 	u8 result[BTRFS_CSUM_SIZE];
368dfd29eedSDavid Sterba 	const u8 *header_csum;
36977bf40a2SQu Wenruo 	int ret = 0;
370169aaaf2SQu Wenruo 	const bool ignore_csum = btrfs_test_opt(fs_info, IGNOREMETACSUMS);
371ea466794SJosef Bacik 
372947a6299SQu Wenruo 	ASSERT(check);
373947a6299SQu Wenruo 
374ce9adaa5SChris Mason 	found_start = btrfs_header_bytenr(eb);
375727011e0SChris Mason 	if (found_start != eb->start) {
3768f0ed7d4SQu Wenruo 		btrfs_err_rl(fs_info,
3778f0ed7d4SQu Wenruo 			"bad tree block start, mirror %u want %llu have %llu",
3788f0ed7d4SQu Wenruo 			     eb->read_mirror, eb->start, found_start);
379f188591eSChris Mason 		ret = -EIO;
38077bf40a2SQu Wenruo 		goto out;
381ce9adaa5SChris Mason 	}
382b0c9b3b0SDavid Sterba 	if (check_tree_block_fsid(eb)) {
3838f0ed7d4SQu Wenruo 		btrfs_err_rl(fs_info, "bad fsid on logical %llu mirror %u",
3848f0ed7d4SQu Wenruo 			     eb->start, eb->read_mirror);
3851259ab75SChris Mason 		ret = -EIO;
38677bf40a2SQu Wenruo 		goto out;
3871259ab75SChris Mason 	}
388ce9adaa5SChris Mason 	found_level = btrfs_header_level(eb);
3891c24c3ceSJosef Bacik 	if (found_level >= BTRFS_MAX_LEVEL) {
3908f0ed7d4SQu Wenruo 		btrfs_err(fs_info,
3918f0ed7d4SQu Wenruo 			"bad tree block level, mirror %u level %d on logical %llu",
3928f0ed7d4SQu Wenruo 			eb->read_mirror, btrfs_header_level(eb), eb->start);
3931c24c3ceSJosef Bacik 		ret = -EIO;
39477bf40a2SQu Wenruo 		goto out;
3951c24c3ceSJosef Bacik 	}
396ce9adaa5SChris Mason 
397c67b3892SDavid Sterba 	csum_tree_block(eb, result);
398082d5bb9SQu Wenruo 	header_csum = folio_address(eb->folios[0]) +
3998d993618SQu Wenruo 		get_eb_offset_in_folio(eb, offsetof(struct btrfs_header, csum));
400a826d6dcSJosef Bacik 
401dfd29eedSDavid Sterba 	if (memcmp(result, header_csum, csum_size) != 0) {
4022996e1f8SJohannes Thumshirn 		btrfs_warn_rl(fs_info,
403169aaaf2SQu Wenruo "checksum verify failed on logical %llu mirror %u wanted " CSUM_FMT " found " CSUM_FMT " level %d%s",
4048f0ed7d4SQu Wenruo 			      eb->start, eb->read_mirror,
405dfd29eedSDavid Sterba 			      CSUM_FMT_VALUE(csum_size, header_csum),
40635be8851SJohannes Thumshirn 			      CSUM_FMT_VALUE(csum_size, result),
407169aaaf2SQu Wenruo 			      btrfs_header_level(eb),
408169aaaf2SQu Wenruo 			      ignore_csum ? ", ignored" : "");
409169aaaf2SQu Wenruo 		if (!ignore_csum) {
4102996e1f8SJohannes Thumshirn 			ret = -EUCLEAN;
41177bf40a2SQu Wenruo 			goto out;
4122996e1f8SJohannes Thumshirn 		}
413169aaaf2SQu Wenruo 	}
4142996e1f8SJohannes Thumshirn 
415947a6299SQu Wenruo 	if (found_level != check->level) {
41677177ed1SQu Wenruo 		btrfs_err(fs_info,
41777177ed1SQu Wenruo 		"level verify failed on logical %llu mirror %u wanted %u found %u",
41877177ed1SQu Wenruo 			  eb->start, eb->read_mirror, check->level, found_level);
419947a6299SQu Wenruo 		ret = -EIO;
420947a6299SQu Wenruo 		goto out;
421947a6299SQu Wenruo 	}
422947a6299SQu Wenruo 	if (unlikely(check->transid &&
423947a6299SQu Wenruo 		     btrfs_header_generation(eb) != check->transid)) {
424947a6299SQu Wenruo 		btrfs_err_rl(eb->fs_info,
425947a6299SQu Wenruo "parent transid verify failed on logical %llu mirror %u wanted %llu found %llu",
426947a6299SQu Wenruo 				eb->start, eb->read_mirror, check->transid,
427947a6299SQu Wenruo 				btrfs_header_generation(eb));
428947a6299SQu Wenruo 		ret = -EIO;
429947a6299SQu Wenruo 		goto out;
430947a6299SQu Wenruo 	}
431947a6299SQu Wenruo 	if (check->has_first_key) {
4322917f741SDavid Sterba 		const struct btrfs_key *expect_key = &check->first_key;
433947a6299SQu Wenruo 		struct btrfs_key found_key;
434947a6299SQu Wenruo 
435947a6299SQu Wenruo 		if (found_level)
436947a6299SQu Wenruo 			btrfs_node_key_to_cpu(eb, &found_key, 0);
437947a6299SQu Wenruo 		else
438947a6299SQu Wenruo 			btrfs_item_key_to_cpu(eb, &found_key, 0);
439947a6299SQu Wenruo 		if (unlikely(btrfs_comp_cpu_keys(expect_key, &found_key))) {
440947a6299SQu Wenruo 			btrfs_err(fs_info,
441947a6299SQu Wenruo "tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)",
442947a6299SQu Wenruo 				  eb->start, check->transid,
443947a6299SQu Wenruo 				  expect_key->objectid,
444947a6299SQu Wenruo 				  expect_key->type, expect_key->offset,
445947a6299SQu Wenruo 				  found_key.objectid, found_key.type,
446947a6299SQu Wenruo 				  found_key.offset);
447947a6299SQu Wenruo 			ret = -EUCLEAN;
448947a6299SQu Wenruo 			goto out;
449947a6299SQu Wenruo 		}
450947a6299SQu Wenruo 	}
451947a6299SQu Wenruo 	if (check->owner_root) {
452947a6299SQu Wenruo 		ret = btrfs_check_eb_owner(eb, check->owner_root);
453947a6299SQu Wenruo 		if (ret < 0)
454947a6299SQu Wenruo 			goto out;
455947a6299SQu Wenruo 	}
456947a6299SQu Wenruo 
457a826d6dcSJosef Bacik 	/*
458a826d6dcSJosef Bacik 	 * If this is a leaf block and it is corrupt, set the corrupt bit so
459a826d6dcSJosef Bacik 	 * that we don't try and read the other copies of this block, just
460a826d6dcSJosef Bacik 	 * return -EIO.
461a826d6dcSJosef Bacik 	 */
46285d8a826SJosef Bacik 	if (found_level == 0 && btrfs_check_leaf(eb)) {
463a826d6dcSJosef Bacik 		set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags);
464a826d6dcSJosef Bacik 		ret = -EIO;
465a826d6dcSJosef Bacik 	}
466ce9adaa5SChris Mason 
467813fd1dcSDavid Sterba 	if (found_level > 0 && btrfs_check_node(eb))
468053ab70fSLiu Bo 		ret = -EIO;
469053ab70fSLiu Bo 
470aebcc159SChristoph Hellwig 	if (ret)
47175391f0dSQu Wenruo 		btrfs_err(fs_info,
4728f0ed7d4SQu Wenruo 		"read time tree block corruption detected on logical %llu mirror %u",
4738f0ed7d4SQu Wenruo 			  eb->start, eb->read_mirror);
47477bf40a2SQu Wenruo out:
47577bf40a2SQu Wenruo 	return ret;
47677bf40a2SQu Wenruo }
47777bf40a2SQu Wenruo 
4783dd1462eSJan Beulich #ifdef CONFIG_MIGRATION
btree_migrate_folio(struct address_space * mapping,struct folio * dst,struct folio * src,enum migrate_mode mode)4798958b551SMatthew Wilcox (Oracle) static int btree_migrate_folio(struct address_space *mapping,
4808958b551SMatthew Wilcox (Oracle) 		struct folio *dst, struct folio *src, enum migrate_mode mode)
481784b4e29SChris Mason {
482784b4e29SChris Mason 	/*
483784b4e29SChris Mason 	 * we can't safely write a btree page from here,
484784b4e29SChris Mason 	 * we haven't done the locking hook
485784b4e29SChris Mason 	 */
4868958b551SMatthew Wilcox (Oracle) 	if (folio_test_dirty(src))
487784b4e29SChris Mason 		return -EAGAIN;
488784b4e29SChris Mason 	/*
489784b4e29SChris Mason 	 * Buffers may be managed in a filesystem specific way.
490784b4e29SChris Mason 	 * We must have no buffers or drop them.
491784b4e29SChris Mason 	 */
4928958b551SMatthew Wilcox (Oracle) 	if (folio_get_private(src) &&
4938958b551SMatthew Wilcox (Oracle) 	    !filemap_release_folio(src, GFP_KERNEL))
494784b4e29SChris Mason 		return -EAGAIN;
49554184650SMatthew Wilcox (Oracle) 	return migrate_folio(mapping, dst, src, mode);
496784b4e29SChris Mason }
4978958b551SMatthew Wilcox (Oracle) #else
4988958b551SMatthew Wilcox (Oracle) #define btree_migrate_folio NULL
4993dd1462eSJan Beulich #endif
500784b4e29SChris Mason 
btree_writepages(struct address_space * mapping,struct writeback_control * wbc)5010da5468fSChris Mason static int btree_writepages(struct address_space *mapping,
5020da5468fSChris Mason 			    struct writeback_control *wbc)
5030da5468fSChris Mason {
504e2d84521SMiao Xie 	int ret;
505e2d84521SMiao Xie 
506d8d5f3e1SChris Mason 	if (wbc->sync_mode == WB_SYNC_NONE) {
50741044b41SDavid Sterba 		struct btrfs_fs_info *fs_info;
508448d640bSChris Mason 
509448d640bSChris Mason 		if (wbc->for_kupdate)
510448d640bSChris Mason 			return 0;
511448d640bSChris Mason 
51241044b41SDavid Sterba 		fs_info = inode_to_fs_info(mapping->host);
513b9473439SChris Mason 		/* this is a bit racy, but that's ok */
514d814a491SEthan Lien 		ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes,
515d814a491SEthan Lien 					     BTRFS_DIRTY_METADATA_THRESH,
516d814a491SEthan Lien 					     fs_info->dirty_metadata_batch);
517e2d84521SMiao Xie 		if (ret < 0)
518793955bcSChris Mason 			return 0;
519793955bcSChris Mason 	}
5200b32f4bbSJosef Bacik 	return btree_write_cache_pages(mapping, wbc);
5210da5468fSChris Mason }
5220da5468fSChris Mason 
btree_release_folio(struct folio * folio,gfp_t gfp_flags)523f913cff3SMatthew Wilcox (Oracle) static bool btree_release_folio(struct folio *folio, gfp_t gfp_flags)
5245f39d397SChris Mason {
525f913cff3SMatthew Wilcox (Oracle) 	if (folio_test_writeback(folio) || folio_test_dirty(folio))
526f913cff3SMatthew Wilcox (Oracle) 		return false;
5270c4e538bSDavid Sterba 
528*b8ae2bfaSLi Zetao 	return try_release_extent_buffer(folio);
529d98237b3SChris Mason }
530d98237b3SChris Mason 
btree_invalidate_folio(struct folio * folio,size_t offset,size_t length)531895586ebSMatthew Wilcox (Oracle) static void btree_invalidate_folio(struct folio *folio, size_t offset,
532895586ebSMatthew Wilcox (Oracle) 				 size_t length)
533d98237b3SChris Mason {
534d1310b2eSChris Mason 	struct extent_io_tree *tree;
535c8293894SDavid Sterba 
536c8293894SDavid Sterba 	tree = &folio_to_inode(folio)->io_tree;
537895586ebSMatthew Wilcox (Oracle) 	extent_invalidate_folio(tree, folio, offset);
538f913cff3SMatthew Wilcox (Oracle) 	btree_release_folio(folio, GFP_NOFS);
539895586ebSMatthew Wilcox (Oracle) 	if (folio_get_private(folio)) {
540b33d2e53SDavid Sterba 		btrfs_warn(folio_to_fs_info(folio),
541895586ebSMatthew Wilcox (Oracle) 			   "folio private not zero on folio %llu",
542895586ebSMatthew Wilcox (Oracle) 			   (unsigned long long)folio_pos(folio));
543895586ebSMatthew Wilcox (Oracle) 		folio_detach_private(folio);
5449ad6b7bcSChris Mason 	}
545d98237b3SChris Mason }
546d98237b3SChris Mason 
547bb146eb2SJosef Bacik #ifdef DEBUG
btree_dirty_folio(struct address_space * mapping,struct folio * folio)5480079c3b1SMatthew Wilcox (Oracle) static bool btree_dirty_folio(struct address_space *mapping,
5490079c3b1SMatthew Wilcox (Oracle) 		struct folio *folio)
5500079c3b1SMatthew Wilcox (Oracle) {
55141044b41SDavid Sterba 	struct btrfs_fs_info *fs_info = inode_to_fs_info(mapping->host);
5525e0e8799SQu Wenruo 	struct btrfs_subpage_info *spi = fs_info->subpage_info;
553139e8cd3SQu Wenruo 	struct btrfs_subpage *subpage;
5540b32f4bbSJosef Bacik 	struct extent_buffer *eb;
555139e8cd3SQu Wenruo 	int cur_bit = 0;
5560079c3b1SMatthew Wilcox (Oracle) 	u64 page_start = folio_pos(folio);
5570b32f4bbSJosef Bacik 
558139e8cd3SQu Wenruo 	if (fs_info->sectorsize == PAGE_SIZE) {
5590079c3b1SMatthew Wilcox (Oracle) 		eb = folio_get_private(folio);
5600b32f4bbSJosef Bacik 		BUG_ON(!eb);
5610b32f4bbSJosef Bacik 		BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
5620b32f4bbSJosef Bacik 		BUG_ON(!atomic_read(&eb->refs));
56349d0c642SFilipe Manana 		btrfs_assert_tree_write_locked(eb);
5640079c3b1SMatthew Wilcox (Oracle) 		return filemap_dirty_folio(mapping, folio);
565139e8cd3SQu Wenruo 	}
5665e0e8799SQu Wenruo 
5675e0e8799SQu Wenruo 	ASSERT(spi);
5680079c3b1SMatthew Wilcox (Oracle) 	subpage = folio_get_private(folio);
569139e8cd3SQu Wenruo 
5705e0e8799SQu Wenruo 	for (cur_bit = spi->dirty_offset;
5715e0e8799SQu Wenruo 	     cur_bit < spi->dirty_offset + spi->bitmap_nr_bits;
5725e0e8799SQu Wenruo 	     cur_bit++) {
573139e8cd3SQu Wenruo 		unsigned long flags;
574139e8cd3SQu Wenruo 		u64 cur;
575139e8cd3SQu Wenruo 
576139e8cd3SQu Wenruo 		spin_lock_irqsave(&subpage->lock, flags);
5775e0e8799SQu Wenruo 		if (!test_bit(cur_bit, subpage->bitmaps)) {
578139e8cd3SQu Wenruo 			spin_unlock_irqrestore(&subpage->lock, flags);
579139e8cd3SQu Wenruo 			continue;
580139e8cd3SQu Wenruo 		}
581139e8cd3SQu Wenruo 		spin_unlock_irqrestore(&subpage->lock, flags);
582139e8cd3SQu Wenruo 		cur = page_start + cur_bit * fs_info->sectorsize;
583139e8cd3SQu Wenruo 
584139e8cd3SQu Wenruo 		eb = find_extent_buffer(fs_info, cur);
585139e8cd3SQu Wenruo 		ASSERT(eb);
586139e8cd3SQu Wenruo 		ASSERT(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
587139e8cd3SQu Wenruo 		ASSERT(atomic_read(&eb->refs));
58849d0c642SFilipe Manana 		btrfs_assert_tree_write_locked(eb);
589139e8cd3SQu Wenruo 		free_extent_buffer(eb);
590139e8cd3SQu Wenruo 
5915e0e8799SQu Wenruo 		cur_bit += (fs_info->nodesize >> fs_info->sectorsize_bits) - 1;
592139e8cd3SQu Wenruo 	}
5930079c3b1SMatthew Wilcox (Oracle) 	return filemap_dirty_folio(mapping, folio);
5940b32f4bbSJosef Bacik }
5950079c3b1SMatthew Wilcox (Oracle) #else
5960079c3b1SMatthew Wilcox (Oracle) #define btree_dirty_folio filemap_dirty_folio
5970079c3b1SMatthew Wilcox (Oracle) #endif
5980b32f4bbSJosef Bacik 
5997f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = {
6000da5468fSChris Mason 	.writepages	= btree_writepages,
601f913cff3SMatthew Wilcox (Oracle) 	.release_folio	= btree_release_folio,
602895586ebSMatthew Wilcox (Oracle) 	.invalidate_folio = btree_invalidate_folio,
6038958b551SMatthew Wilcox (Oracle) 	.migrate_folio	= btree_migrate_folio,
6040079c3b1SMatthew Wilcox (Oracle) 	.dirty_folio	= btree_dirty_folio,
605d98237b3SChris Mason };
606123abc88SChris Mason 
btrfs_find_create_tree_block(struct btrfs_fs_info * fs_info,u64 bytenr,u64 owner_root,int level)6072ff7e61eSJeff Mahoney struct extent_buffer *btrfs_find_create_tree_block(
6082ff7e61eSJeff Mahoney 						struct btrfs_fs_info *fs_info,
6093fbaf258SJosef Bacik 						u64 bytenr, u64 owner_root,
6103fbaf258SJosef Bacik 						int level)
6110999df54SChris Mason {
6120b246afaSJeff Mahoney 	if (btrfs_is_testing(fs_info))
6130b246afaSJeff Mahoney 		return alloc_test_extent_buffer(fs_info, bytenr);
6143fbaf258SJosef Bacik 	return alloc_extent_buffer(fs_info, bytenr, owner_root, level);
6150999df54SChris Mason }
6160999df54SChris Mason 
617581c1760SQu Wenruo /*
618581c1760SQu Wenruo  * Read tree block at logical address @bytenr and do variant basic but critical
619581c1760SQu Wenruo  * verification.
620581c1760SQu Wenruo  *
621789d6a3aSQu Wenruo  * @check:		expected tree parentness check, see comments of the
622789d6a3aSQu Wenruo  *			structure for details.
623581c1760SQu Wenruo  */
read_tree_block(struct btrfs_fs_info * fs_info,u64 bytenr,struct btrfs_tree_parent_check * check)6242ff7e61eSJeff Mahoney struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr,
625789d6a3aSQu Wenruo 				      struct btrfs_tree_parent_check *check)
626e20d96d6SChris Mason {
6275f39d397SChris Mason 	struct extent_buffer *buf = NULL;
62819c00ddcSChris Mason 	int ret;
62919c00ddcSChris Mason 
630789d6a3aSQu Wenruo 	ASSERT(check);
631789d6a3aSQu Wenruo 
632789d6a3aSQu Wenruo 	buf = btrfs_find_create_tree_block(fs_info, bytenr, check->owner_root,
633789d6a3aSQu Wenruo 					   check->level);
634c871b0f2SLiu Bo 	if (IS_ERR(buf))
635c871b0f2SLiu Bo 		return buf;
636e4204dedSChris Mason 
637789d6a3aSQu Wenruo 	ret = btrfs_read_extent_buffer(buf, check);
6380f0fe8f7SFilipe David Borba Manana 	if (ret) {
639537f38f0SNikolay Borisov 		free_extent_buffer_stale(buf);
64064c043deSLiu Bo 		return ERR_PTR(ret);
6410f0fe8f7SFilipe David Borba Manana 	}
6425f39d397SChris Mason 	return buf;
643ce9adaa5SChris Mason 
644eb60ceacSChris Mason }
645eb60ceacSChris Mason 
__setup_root(struct btrfs_root * root,struct btrfs_fs_info * fs_info,u64 objectid)646da17066cSJeff Mahoney static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info,
647e20d96d6SChris Mason 			 u64 objectid)
648d97e63b6SChris Mason {
649fef998d1SDavid Sterba 	bool dummy = btrfs_is_testing(fs_info);
6502e608bd1SJosef Bacik 
6512e608bd1SJosef Bacik 	memset(&root->root_key, 0, sizeof(root->root_key));
6522e608bd1SJosef Bacik 	memset(&root->root_item, 0, sizeof(root->root_item));
6532e608bd1SJosef Bacik 	memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
65496dfcb46SJosef Bacik 	root->fs_info = fs_info;
6552e608bd1SJosef Bacik 	root->root_key.objectid = objectid;
656cfaa7295SChris Mason 	root->node = NULL;
657a28ec197SChris Mason 	root->commit_root = NULL;
65827cdeb70SMiao Xie 	root->state = 0;
659abed4aaaSJosef Bacik 	RB_CLEAR_NODE(&root->rb_node);
6600b86a832SChris Mason 
661ca84529aSFilipe Manana 	btrfs_set_root_last_trans(root, 0);
6626b8fad57SNikolay Borisov 	root->free_objectid = 0;
663eb73c1b7SMiao Xie 	root->nr_delalloc_inodes = 0;
664199c2a9cSMiao Xie 	root->nr_ordered_extents = 0;
665310b2f5dSFilipe Manana 	xa_init(&root->inodes);
666905a95f3SFilipe Manana 	xa_init(&root->delayed_nodes);
6672e608bd1SJosef Bacik 
6682e608bd1SJosef Bacik 	btrfs_init_root_block_rsv(root);
6690b86a832SChris Mason 
6700b86a832SChris Mason 	INIT_LIST_HEAD(&root->dirty_list);
6715d4f98a2SYan Zheng 	INIT_LIST_HEAD(&root->root_list);
672eb73c1b7SMiao Xie 	INIT_LIST_HEAD(&root->delalloc_inodes);
673eb73c1b7SMiao Xie 	INIT_LIST_HEAD(&root->delalloc_root);
674199c2a9cSMiao Xie 	INIT_LIST_HEAD(&root->ordered_extents);
675199c2a9cSMiao Xie 	INIT_LIST_HEAD(&root->ordered_root);
676d2311e69SQu Wenruo 	INIT_LIST_HEAD(&root->reloc_dirty_list);
677eb73c1b7SMiao Xie 	spin_lock_init(&root->delalloc_lock);
678199c2a9cSMiao Xie 	spin_lock_init(&root->ordered_extent_lock);
679f0486c68SYan, Zheng 	spin_lock_init(&root->accounting_lock);
6808287475aSQu Wenruo 	spin_lock_init(&root->qgroup_meta_rsv_lock);
681a2135011SChris Mason 	mutex_init(&root->objectid_mutex);
682e02119d5SChris Mason 	mutex_init(&root->log_mutex);
68331f3d255SMiao Xie 	mutex_init(&root->ordered_extent_mutex);
684573bfb72SMiao Xie 	mutex_init(&root->delalloc_mutex);
685c53e9653SQu Wenruo 	init_waitqueue_head(&root->qgroup_flush_wait);
6867237f183SYan Zheng 	init_waitqueue_head(&root->log_writer_wait);
6877237f183SYan Zheng 	init_waitqueue_head(&root->log_commit_wait[0]);
6887237f183SYan Zheng 	init_waitqueue_head(&root->log_commit_wait[1]);
6898b050d35SMiao Xie 	INIT_LIST_HEAD(&root->log_ctxs[0]);
6908b050d35SMiao Xie 	INIT_LIST_HEAD(&root->log_ctxs[1]);
6917237f183SYan Zheng 	atomic_set(&root->log_commit[0], 0);
6927237f183SYan Zheng 	atomic_set(&root->log_commit[1], 0);
6937237f183SYan Zheng 	atomic_set(&root->log_writers, 0);
6942ecb7923SMiao Xie 	atomic_set(&root->log_batch, 0);
6950700cea7SElena Reshetova 	refcount_set(&root->refs, 1);
6968ecebf4dSRobbie Ko 	atomic_set(&root->snapshot_force_cow, 0);
697eede2bf3SOmar Sandoval 	atomic_set(&root->nr_swapfiles, 0);
6986008859bSFilipe Manana 	btrfs_set_root_log_transid(root, 0);
699d1433debSMiao Xie 	root->log_transid_committed = -1;
700f9850787SFilipe Manana 	btrfs_set_root_last_log_commit(root, 0);
7012e608bd1SJosef Bacik 	root->anon_dev = 0;
702e289f03eSFilipe Manana 	if (!dummy) {
70343eb5f29SQu Wenruo 		extent_io_tree_init(fs_info, &root->dirty_log_pages,
70435da5a7eSDavid Sterba 				    IO_TREE_ROOT_DIRTY_LOG_PAGES);
705e289f03eSFilipe Manana 		extent_io_tree_init(fs_info, &root->log_csum_range,
70635da5a7eSDavid Sterba 				    IO_TREE_LOG_CSUM_RANGE);
707e289f03eSFilipe Manana 	}
708017e5369SChris Mason 
7095f3ab90aSAnand Jain 	spin_lock_init(&root->root_item_lock);
710370a11b8SQu Wenruo 	btrfs_qgroup_init_swapped_blocks(&root->swapped_blocks);
711bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
712bd647ce3SJosef Bacik 	INIT_LIST_HEAD(&root->leak_list);
713fc7cbcd4SDavid Sterba 	spin_lock(&fs_info->fs_roots_radix_lock);
714bd647ce3SJosef Bacik 	list_add_tail(&root->leak_list, &fs_info->allocated_roots);
715fc7cbcd4SDavid Sterba 	spin_unlock(&fs_info->fs_roots_radix_lock);
716bd647ce3SJosef Bacik #endif
7173768f368SChris Mason }
7183768f368SChris Mason 
btrfs_alloc_root(struct btrfs_fs_info * fs_info,u64 objectid,gfp_t flags)71974e4d827SDavid Sterba static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info,
72096dfcb46SJosef Bacik 					   u64 objectid, gfp_t flags)
7216f07e42eSAl Viro {
72274e4d827SDavid Sterba 	struct btrfs_root *root = kzalloc(sizeof(*root), flags);
7236f07e42eSAl Viro 	if (root)
72496dfcb46SJosef Bacik 		__setup_root(root, fs_info, objectid);
7256f07e42eSAl Viro 	return root;
7266f07e42eSAl Viro }
7276f07e42eSAl Viro 
72806ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
72906ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */
btrfs_alloc_dummy_root(struct btrfs_fs_info * fs_info)730da17066cSJeff Mahoney struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info)
73106ea65a3SJosef Bacik {
73206ea65a3SJosef Bacik 	struct btrfs_root *root;
73306ea65a3SJosef Bacik 
7347c0260eeSJeff Mahoney 	if (!fs_info)
7357c0260eeSJeff Mahoney 		return ERR_PTR(-EINVAL);
7367c0260eeSJeff Mahoney 
73796dfcb46SJosef Bacik 	root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, GFP_KERNEL);
73806ea65a3SJosef Bacik 	if (!root)
73906ea65a3SJosef Bacik 		return ERR_PTR(-ENOMEM);
740da17066cSJeff Mahoney 
741b9ef22deSFeifei Xu 	/* We don't use the stripesize in selftest, set it as sectorsize */
742faa2dbf0SJosef Bacik 	root->alloc_bytenr = 0;
74306ea65a3SJosef Bacik 
74406ea65a3SJosef Bacik 	return root;
74506ea65a3SJosef Bacik }
74606ea65a3SJosef Bacik #endif
74706ea65a3SJosef Bacik 
global_root_cmp(struct rb_node * a_node,const struct rb_node * b_node)748abed4aaaSJosef Bacik static int global_root_cmp(struct rb_node *a_node, const struct rb_node *b_node)
749abed4aaaSJosef Bacik {
750abed4aaaSJosef Bacik 	const struct btrfs_root *a = rb_entry(a_node, struct btrfs_root, rb_node);
751abed4aaaSJosef Bacik 	const struct btrfs_root *b = rb_entry(b_node, struct btrfs_root, rb_node);
752abed4aaaSJosef Bacik 
753abed4aaaSJosef Bacik 	return btrfs_comp_cpu_keys(&a->root_key, &b->root_key);
754abed4aaaSJosef Bacik }
755abed4aaaSJosef Bacik 
global_root_key_cmp(const void * k,const struct rb_node * node)756abed4aaaSJosef Bacik static int global_root_key_cmp(const void *k, const struct rb_node *node)
757abed4aaaSJosef Bacik {
758abed4aaaSJosef Bacik 	const struct btrfs_key *key = k;
759abed4aaaSJosef Bacik 	const struct btrfs_root *root = rb_entry(node, struct btrfs_root, rb_node);
760abed4aaaSJosef Bacik 
761abed4aaaSJosef Bacik 	return btrfs_comp_cpu_keys(key, &root->root_key);
762abed4aaaSJosef Bacik }
763abed4aaaSJosef Bacik 
btrfs_global_root_insert(struct btrfs_root * root)764abed4aaaSJosef Bacik int btrfs_global_root_insert(struct btrfs_root *root)
765abed4aaaSJosef Bacik {
766abed4aaaSJosef Bacik 	struct btrfs_fs_info *fs_info = root->fs_info;
767abed4aaaSJosef Bacik 	struct rb_node *tmp;
768745806fbSQu Wenruo 	int ret = 0;
769abed4aaaSJosef Bacik 
770abed4aaaSJosef Bacik 	write_lock(&fs_info->global_root_lock);
771abed4aaaSJosef Bacik 	tmp = rb_find_add(&root->rb_node, &fs_info->global_root_tree, global_root_cmp);
772abed4aaaSJosef Bacik 	write_unlock(&fs_info->global_root_lock);
773abed4aaaSJosef Bacik 
774745806fbSQu Wenruo 	if (tmp) {
775745806fbSQu Wenruo 		ret = -EEXIST;
776745806fbSQu Wenruo 		btrfs_warn(fs_info, "global root %llu %llu already exists",
777e094f480SJosef Bacik 			   btrfs_root_id(root), root->root_key.offset);
778745806fbSQu Wenruo 	}
779745806fbSQu Wenruo 	return ret;
780abed4aaaSJosef Bacik }
781abed4aaaSJosef Bacik 
btrfs_global_root_delete(struct btrfs_root * root)782abed4aaaSJosef Bacik void btrfs_global_root_delete(struct btrfs_root *root)
783abed4aaaSJosef Bacik {
784abed4aaaSJosef Bacik 	struct btrfs_fs_info *fs_info = root->fs_info;
785abed4aaaSJosef Bacik 
786abed4aaaSJosef Bacik 	write_lock(&fs_info->global_root_lock);
787abed4aaaSJosef Bacik 	rb_erase(&root->rb_node, &fs_info->global_root_tree);
788abed4aaaSJosef Bacik 	write_unlock(&fs_info->global_root_lock);
789abed4aaaSJosef Bacik }
790abed4aaaSJosef Bacik 
btrfs_global_root(struct btrfs_fs_info * fs_info,struct btrfs_key * key)791abed4aaaSJosef Bacik struct btrfs_root *btrfs_global_root(struct btrfs_fs_info *fs_info,
792abed4aaaSJosef Bacik 				     struct btrfs_key *key)
793abed4aaaSJosef Bacik {
794abed4aaaSJosef Bacik 	struct rb_node *node;
795abed4aaaSJosef Bacik 	struct btrfs_root *root = NULL;
796abed4aaaSJosef Bacik 
797abed4aaaSJosef Bacik 	read_lock(&fs_info->global_root_lock);
798abed4aaaSJosef Bacik 	node = rb_find(key, &fs_info->global_root_tree, global_root_key_cmp);
799abed4aaaSJosef Bacik 	if (node)
800abed4aaaSJosef Bacik 		root = container_of(node, struct btrfs_root, rb_node);
801abed4aaaSJosef Bacik 	read_unlock(&fs_info->global_root_lock);
802abed4aaaSJosef Bacik 
803abed4aaaSJosef Bacik 	return root;
804abed4aaaSJosef Bacik }
805abed4aaaSJosef Bacik 
btrfs_global_root_id(struct btrfs_fs_info * fs_info,u64 bytenr)806f7238e50SJosef Bacik static u64 btrfs_global_root_id(struct btrfs_fs_info *fs_info, u64 bytenr)
807f7238e50SJosef Bacik {
808f7238e50SJosef Bacik 	struct btrfs_block_group *block_group;
809f7238e50SJosef Bacik 	u64 ret;
810f7238e50SJosef Bacik 
811f7238e50SJosef Bacik 	if (!btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
812f7238e50SJosef Bacik 		return 0;
813f7238e50SJosef Bacik 
814f7238e50SJosef Bacik 	if (bytenr)
815f7238e50SJosef Bacik 		block_group = btrfs_lookup_block_group(fs_info, bytenr);
816f7238e50SJosef Bacik 	else
817f7238e50SJosef Bacik 		block_group = btrfs_lookup_first_block_group(fs_info, bytenr);
818f7238e50SJosef Bacik 	ASSERT(block_group);
819f7238e50SJosef Bacik 	if (!block_group)
820f7238e50SJosef Bacik 		return 0;
821f7238e50SJosef Bacik 	ret = block_group->global_root_id;
822f7238e50SJosef Bacik 	btrfs_put_block_group(block_group);
823f7238e50SJosef Bacik 
824f7238e50SJosef Bacik 	return ret;
825f7238e50SJosef Bacik }
826f7238e50SJosef Bacik 
btrfs_csum_root(struct btrfs_fs_info * fs_info,u64 bytenr)827abed4aaaSJosef Bacik struct btrfs_root *btrfs_csum_root(struct btrfs_fs_info *fs_info, u64 bytenr)
828abed4aaaSJosef Bacik {
829abed4aaaSJosef Bacik 	struct btrfs_key key = {
830abed4aaaSJosef Bacik 		.objectid = BTRFS_CSUM_TREE_OBJECTID,
831abed4aaaSJosef Bacik 		.type = BTRFS_ROOT_ITEM_KEY,
832f7238e50SJosef Bacik 		.offset = btrfs_global_root_id(fs_info, bytenr),
833abed4aaaSJosef Bacik 	};
834abed4aaaSJosef Bacik 
835abed4aaaSJosef Bacik 	return btrfs_global_root(fs_info, &key);
836abed4aaaSJosef Bacik }
837abed4aaaSJosef Bacik 
btrfs_extent_root(struct btrfs_fs_info * fs_info,u64 bytenr)838abed4aaaSJosef Bacik struct btrfs_root *btrfs_extent_root(struct btrfs_fs_info *fs_info, u64 bytenr)
839abed4aaaSJosef Bacik {
840abed4aaaSJosef Bacik 	struct btrfs_key key = {
841abed4aaaSJosef Bacik 		.objectid = BTRFS_EXTENT_TREE_OBJECTID,
842abed4aaaSJosef Bacik 		.type = BTRFS_ROOT_ITEM_KEY,
843f7238e50SJosef Bacik 		.offset = btrfs_global_root_id(fs_info, bytenr),
844abed4aaaSJosef Bacik 	};
845abed4aaaSJosef Bacik 
846abed4aaaSJosef Bacik 	return btrfs_global_root(fs_info, &key);
847abed4aaaSJosef Bacik }
848abed4aaaSJosef Bacik 
btrfs_create_tree(struct btrfs_trans_handle * trans,u64 objectid)84920897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
85020897f5cSArne Jansen 				     u64 objectid)
85120897f5cSArne Jansen {
8529b7a2440SDavid Sterba 	struct btrfs_fs_info *fs_info = trans->fs_info;
85320897f5cSArne Jansen 	struct extent_buffer *leaf;
85420897f5cSArne Jansen 	struct btrfs_root *tree_root = fs_info->tree_root;
85520897f5cSArne Jansen 	struct btrfs_root *root;
85620897f5cSArne Jansen 	struct btrfs_key key;
857b89f6d1fSFilipe Manana 	unsigned int nofs_flag;
85820897f5cSArne Jansen 	int ret = 0;
85920897f5cSArne Jansen 
860b89f6d1fSFilipe Manana 	/*
861b89f6d1fSFilipe Manana 	 * We're holding a transaction handle, so use a NOFS memory allocation
862b89f6d1fSFilipe Manana 	 * context to avoid deadlock if reclaim happens.
863b89f6d1fSFilipe Manana 	 */
864b89f6d1fSFilipe Manana 	nofs_flag = memalloc_nofs_save();
86596dfcb46SJosef Bacik 	root = btrfs_alloc_root(fs_info, objectid, GFP_KERNEL);
866b89f6d1fSFilipe Manana 	memalloc_nofs_restore(nofs_flag);
86720897f5cSArne Jansen 	if (!root)
86820897f5cSArne Jansen 		return ERR_PTR(-ENOMEM);
86920897f5cSArne Jansen 
87020897f5cSArne Jansen 	root->root_key.objectid = objectid;
87120897f5cSArne Jansen 	root->root_key.type = BTRFS_ROOT_ITEM_KEY;
87220897f5cSArne Jansen 	root->root_key.offset = 0;
87320897f5cSArne Jansen 
8749631e4ccSJosef Bacik 	leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0,
87560ea105aSBoris Burkov 				      0, BTRFS_NESTING_NORMAL);
87620897f5cSArne Jansen 	if (IS_ERR(leaf)) {
87720897f5cSArne Jansen 		ret = PTR_ERR(leaf);
8781dd05682STsutomu Itoh 		leaf = NULL;
879c1b07854SPeng Hao 		goto fail;
88020897f5cSArne Jansen 	}
88120897f5cSArne Jansen 
88220897f5cSArne Jansen 	root->node = leaf;
88350564b65SFilipe Manana 	btrfs_mark_buffer_dirty(trans, leaf);
88420897f5cSArne Jansen 
88520897f5cSArne Jansen 	root->commit_root = btrfs_root_node(root);
88627cdeb70SMiao Xie 	set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
88720897f5cSArne Jansen 
888f944d2cbSDavid Sterba 	btrfs_set_root_flags(&root->root_item, 0);
889f944d2cbSDavid Sterba 	btrfs_set_root_limit(&root->root_item, 0);
89020897f5cSArne Jansen 	btrfs_set_root_bytenr(&root->root_item, leaf->start);
89120897f5cSArne Jansen 	btrfs_set_root_generation(&root->root_item, trans->transid);
89220897f5cSArne Jansen 	btrfs_set_root_level(&root->root_item, 0);
89320897f5cSArne Jansen 	btrfs_set_root_refs(&root->root_item, 1);
89420897f5cSArne Jansen 	btrfs_set_root_used(&root->root_item, leaf->len);
89520897f5cSArne Jansen 	btrfs_set_root_last_snapshot(&root->root_item, 0);
89620897f5cSArne Jansen 	btrfs_set_root_dirid(&root->root_item, 0);
89733d85fdaSQu Wenruo 	if (is_fstree(objectid))
898807fc790SAndy Shevchenko 		generate_random_guid(root->root_item.uuid);
899807fc790SAndy Shevchenko 	else
900807fc790SAndy Shevchenko 		export_guid(root->root_item.uuid, &guid_null);
901c8422684SDavid Sterba 	btrfs_set_root_drop_level(&root->root_item, 0);
90220897f5cSArne Jansen 
9038a6a87cdSBoris Burkov 	btrfs_tree_unlock(leaf);
9048a6a87cdSBoris Burkov 
90520897f5cSArne Jansen 	key.objectid = objectid;
90620897f5cSArne Jansen 	key.type = BTRFS_ROOT_ITEM_KEY;
90720897f5cSArne Jansen 	key.offset = 0;
90820897f5cSArne Jansen 	ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item);
90920897f5cSArne Jansen 	if (ret)
91020897f5cSArne Jansen 		goto fail;
91120897f5cSArne Jansen 
91220897f5cSArne Jansen 	return root;
9131dd05682STsutomu Itoh 
9148a6a87cdSBoris Burkov fail:
91500246528SJosef Bacik 	btrfs_put_root(root);
9161dd05682STsutomu Itoh 
9171dd05682STsutomu Itoh 	return ERR_PTR(ret);
91820897f5cSArne Jansen }
91920897f5cSArne Jansen 
alloc_log_tree(struct btrfs_trans_handle * trans,struct btrfs_fs_info * fs_info)9207237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans,
921e02119d5SChris Mason 					 struct btrfs_fs_info *fs_info)
9220f7d52f4SChris Mason {
9230f7d52f4SChris Mason 	struct btrfs_root *root;
924e02119d5SChris Mason 
92596dfcb46SJosef Bacik 	root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, GFP_NOFS);
926e02119d5SChris Mason 	if (!root)
9277237f183SYan Zheng 		return ERR_PTR(-ENOMEM);
928e02119d5SChris Mason 
929e02119d5SChris Mason 	root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID;
930e02119d5SChris Mason 	root->root_key.type = BTRFS_ROOT_ITEM_KEY;
931e02119d5SChris Mason 	root->root_key.offset = BTRFS_TREE_LOG_OBJECTID;
93227cdeb70SMiao Xie 
9336ab6ebb7SNaohiro Aota 	return root;
9346ab6ebb7SNaohiro Aota }
9356ab6ebb7SNaohiro Aota 
btrfs_alloc_log_tree_node(struct btrfs_trans_handle * trans,struct btrfs_root * root)9366ab6ebb7SNaohiro Aota int btrfs_alloc_log_tree_node(struct btrfs_trans_handle *trans,
9376ab6ebb7SNaohiro Aota 			      struct btrfs_root *root)
9386ab6ebb7SNaohiro Aota {
9396ab6ebb7SNaohiro Aota 	struct extent_buffer *leaf;
9406ab6ebb7SNaohiro Aota 
9417237f183SYan Zheng 	/*
94292a7cc42SQu Wenruo 	 * DON'T set SHAREABLE bit for log trees.
94327cdeb70SMiao Xie 	 *
94492a7cc42SQu Wenruo 	 * Log trees are not exposed to user space thus can't be snapshotted,
94592a7cc42SQu Wenruo 	 * and they go away before a real commit is actually done.
94692a7cc42SQu Wenruo 	 *
94792a7cc42SQu Wenruo 	 * They do store pointers to file data extents, and those reference
94892a7cc42SQu Wenruo 	 * counts still get updated (along with back refs to the log tree).
9497237f183SYan Zheng 	 */
950e02119d5SChris Mason 
9514d75f8a9SDavid Sterba 	leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID,
95260ea105aSBoris Burkov 			NULL, 0, 0, 0, 0, BTRFS_NESTING_NORMAL);
9536ab6ebb7SNaohiro Aota 	if (IS_ERR(leaf))
9546ab6ebb7SNaohiro Aota 		return PTR_ERR(leaf);
955e02119d5SChris Mason 
9567237f183SYan Zheng 	root->node = leaf;
957e02119d5SChris Mason 
95850564b65SFilipe Manana 	btrfs_mark_buffer_dirty(trans, root->node);
959e02119d5SChris Mason 	btrfs_tree_unlock(root->node);
9606ab6ebb7SNaohiro Aota 
9616ab6ebb7SNaohiro Aota 	return 0;
9627237f183SYan Zheng }
9637237f183SYan Zheng 
btrfs_init_log_root_tree(struct btrfs_trans_handle * trans,struct btrfs_fs_info * fs_info)9647237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans,
9657237f183SYan Zheng 			     struct btrfs_fs_info *fs_info)
9667237f183SYan Zheng {
9677237f183SYan Zheng 	struct btrfs_root *log_root;
9687237f183SYan Zheng 
9697237f183SYan Zheng 	log_root = alloc_log_tree(trans, fs_info);
9707237f183SYan Zheng 	if (IS_ERR(log_root))
9717237f183SYan Zheng 		return PTR_ERR(log_root);
9726ab6ebb7SNaohiro Aota 
9733ddebf27SNaohiro Aota 	if (!btrfs_is_zoned(fs_info)) {
9743ddebf27SNaohiro Aota 		int ret = btrfs_alloc_log_tree_node(trans, log_root);
9753ddebf27SNaohiro Aota 
9766ab6ebb7SNaohiro Aota 		if (ret) {
9776ab6ebb7SNaohiro Aota 			btrfs_put_root(log_root);
9786ab6ebb7SNaohiro Aota 			return ret;
9796ab6ebb7SNaohiro Aota 		}
9803ddebf27SNaohiro Aota 	}
9816ab6ebb7SNaohiro Aota 
9827237f183SYan Zheng 	WARN_ON(fs_info->log_root_tree);
9837237f183SYan Zheng 	fs_info->log_root_tree = log_root;
9847237f183SYan Zheng 	return 0;
9857237f183SYan Zheng }
9867237f183SYan Zheng 
btrfs_add_log_tree(struct btrfs_trans_handle * trans,struct btrfs_root * root)9877237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
9887237f183SYan Zheng 		       struct btrfs_root *root)
9897237f183SYan Zheng {
9900b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
9917237f183SYan Zheng 	struct btrfs_root *log_root;
9927237f183SYan Zheng 	struct btrfs_inode_item *inode_item;
9936ab6ebb7SNaohiro Aota 	int ret;
9947237f183SYan Zheng 
9950b246afaSJeff Mahoney 	log_root = alloc_log_tree(trans, fs_info);
9967237f183SYan Zheng 	if (IS_ERR(log_root))
9977237f183SYan Zheng 		return PTR_ERR(log_root);
9987237f183SYan Zheng 
9996ab6ebb7SNaohiro Aota 	ret = btrfs_alloc_log_tree_node(trans, log_root);
10006ab6ebb7SNaohiro Aota 	if (ret) {
10016ab6ebb7SNaohiro Aota 		btrfs_put_root(log_root);
10026ab6ebb7SNaohiro Aota 		return ret;
10036ab6ebb7SNaohiro Aota 	}
10046ab6ebb7SNaohiro Aota 
1005ca84529aSFilipe Manana 	btrfs_set_root_last_trans(log_root, trans->transid);
1006e094f480SJosef Bacik 	log_root->root_key.offset = btrfs_root_id(root);
10077237f183SYan Zheng 
10087237f183SYan Zheng 	inode_item = &log_root->root_item.inode;
10093cae210fSQu Wenruo 	btrfs_set_stack_inode_generation(inode_item, 1);
10103cae210fSQu Wenruo 	btrfs_set_stack_inode_size(inode_item, 3);
10113cae210fSQu Wenruo 	btrfs_set_stack_inode_nlink(inode_item, 1);
1012da17066cSJeff Mahoney 	btrfs_set_stack_inode_nbytes(inode_item,
10130b246afaSJeff Mahoney 				     fs_info->nodesize);
10143cae210fSQu Wenruo 	btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
10157237f183SYan Zheng 
10165d4f98a2SYan Zheng 	btrfs_set_root_node(&log_root->root_item, log_root->node);
10177237f183SYan Zheng 
10187237f183SYan Zheng 	WARN_ON(root->log_root);
10197237f183SYan Zheng 	root->log_root = log_root;
10206008859bSFilipe Manana 	btrfs_set_root_log_transid(root, 0);
1021d1433debSMiao Xie 	root->log_transid_committed = -1;
1022f9850787SFilipe Manana 	btrfs_set_root_last_log_commit(root, 0);
1023e02119d5SChris Mason 	return 0;
1024e02119d5SChris Mason }
1025e02119d5SChris Mason 
read_tree_root_path(struct btrfs_root * tree_root,struct btrfs_path * path,const struct btrfs_key * key)102649d11beaSJosef Bacik static struct btrfs_root *read_tree_root_path(struct btrfs_root *tree_root,
102749d11beaSJosef Bacik 					      struct btrfs_path *path,
10282917f741SDavid Sterba 					      const struct btrfs_key *key)
1029e02119d5SChris Mason {
1030e02119d5SChris Mason 	struct btrfs_root *root;
1031789d6a3aSQu Wenruo 	struct btrfs_tree_parent_check check = { 0 };
1032e02119d5SChris Mason 	struct btrfs_fs_info *fs_info = tree_root->fs_info;
103384234f3aSYan Zheng 	u64 generation;
1034cb517eabSMiao Xie 	int ret;
1035581c1760SQu Wenruo 	int level;
1036cb517eabSMiao Xie 
103796dfcb46SJosef Bacik 	root = btrfs_alloc_root(fs_info, key->objectid, GFP_NOFS);
103849d11beaSJosef Bacik 	if (!root)
103949d11beaSJosef Bacik 		return ERR_PTR(-ENOMEM);
10400f7d52f4SChris Mason 
1041cb517eabSMiao Xie 	ret = btrfs_find_root(tree_root, key, path,
1042cb517eabSMiao Xie 			      &root->root_item, &root->root_key);
10430f7d52f4SChris Mason 	if (ret) {
104413a8a7c8SYan, Zheng 		if (ret > 0)
104513a8a7c8SYan, Zheng 			ret = -ENOENT;
104649d11beaSJosef Bacik 		goto fail;
10470f7d52f4SChris Mason 	}
104813a8a7c8SYan, Zheng 
104984234f3aSYan Zheng 	generation = btrfs_root_generation(&root->root_item);
1050581c1760SQu Wenruo 	level = btrfs_root_level(&root->root_item);
1051789d6a3aSQu Wenruo 	check.level = level;
1052789d6a3aSQu Wenruo 	check.transid = generation;
1053789d6a3aSQu Wenruo 	check.owner_root = key->objectid;
1054789d6a3aSQu Wenruo 	root->node = read_tree_block(fs_info, btrfs_root_bytenr(&root->root_item),
1055789d6a3aSQu Wenruo 				     &check);
105664c043deSLiu Bo 	if (IS_ERR(root->node)) {
105764c043deSLiu Bo 		ret = PTR_ERR(root->node);
10588c38938cSJosef Bacik 		root->node = NULL;
105949d11beaSJosef Bacik 		goto fail;
10604eb150d6SQu Wenruo 	}
10614eb150d6SQu Wenruo 	if (!btrfs_buffer_uptodate(root->node, generation, 0)) {
1062cb517eabSMiao Xie 		ret = -EIO;
106349d11beaSJosef Bacik 		goto fail;
1064416bc658SJosef Bacik 	}
106588c602abSQu Wenruo 
106688c602abSQu Wenruo 	/*
106788c602abSQu Wenruo 	 * For real fs, and not log/reloc trees, root owner must
106888c602abSQu Wenruo 	 * match its root node owner
106988c602abSQu Wenruo 	 */
1070fef998d1SDavid Sterba 	if (!btrfs_is_testing(fs_info) &&
1071e094f480SJosef Bacik 	    btrfs_root_id(root) != BTRFS_TREE_LOG_OBJECTID &&
1072e094f480SJosef Bacik 	    btrfs_root_id(root) != BTRFS_TREE_RELOC_OBJECTID &&
1073e094f480SJosef Bacik 	    btrfs_root_id(root) != btrfs_header_owner(root->node)) {
107488c602abSQu Wenruo 		btrfs_crit(fs_info,
107588c602abSQu Wenruo "root=%llu block=%llu, tree root owner mismatch, have %llu expect %llu",
1076e094f480SJosef Bacik 			   btrfs_root_id(root), root->node->start,
107788c602abSQu Wenruo 			   btrfs_header_owner(root->node),
1078e094f480SJosef Bacik 			   btrfs_root_id(root));
107988c602abSQu Wenruo 		ret = -EUCLEAN;
108088c602abSQu Wenruo 		goto fail;
108188c602abSQu Wenruo 	}
10825d4f98a2SYan Zheng 	root->commit_root = btrfs_root_node(root);
1083cb517eabSMiao Xie 	return root;
108449d11beaSJosef Bacik fail:
108500246528SJosef Bacik 	btrfs_put_root(root);
108649d11beaSJosef Bacik 	return ERR_PTR(ret);
108749d11beaSJosef Bacik }
108849d11beaSJosef Bacik 
btrfs_read_tree_root(struct btrfs_root * tree_root,const struct btrfs_key * key)108949d11beaSJosef Bacik struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root,
10902917f741SDavid Sterba 					const struct btrfs_key *key)
109149d11beaSJosef Bacik {
109249d11beaSJosef Bacik 	struct btrfs_root *root;
109349d11beaSJosef Bacik 	struct btrfs_path *path;
109449d11beaSJosef Bacik 
109549d11beaSJosef Bacik 	path = btrfs_alloc_path();
109649d11beaSJosef Bacik 	if (!path)
109749d11beaSJosef Bacik 		return ERR_PTR(-ENOMEM);
109849d11beaSJosef Bacik 	root = read_tree_root_path(tree_root, path, key);
109949d11beaSJosef Bacik 	btrfs_free_path(path);
110049d11beaSJosef Bacik 
110149d11beaSJosef Bacik 	return root;
1102cb517eabSMiao Xie }
1103cb517eabSMiao Xie 
11042dfb1e43SQu Wenruo /*
11052dfb1e43SQu Wenruo  * Initialize subvolume root in-memory structure
11062dfb1e43SQu Wenruo  *
11072dfb1e43SQu Wenruo  * @anon_dev:	anonymous device to attach to the root, if zero, allocate new
11082dfb1e43SQu Wenruo  */
btrfs_init_fs_root(struct btrfs_root * root,dev_t anon_dev)11092dfb1e43SQu Wenruo static int btrfs_init_fs_root(struct btrfs_root *root, dev_t anon_dev)
1110cb517eabSMiao Xie {
1111cb517eabSMiao Xie 	int ret;
1112cb517eabSMiao Xie 
11130b548539SDavid Sterba 	btrfs_drew_lock_init(&root->snapshot_lock);
11148257b2dcSMiao Xie 
1115e094f480SJosef Bacik 	if (btrfs_root_id(root) != BTRFS_TREE_LOG_OBJECTID &&
11166ebcd021SQu Wenruo 	    !btrfs_is_data_reloc_root(root) &&
1117e094f480SJosef Bacik 	    is_fstree(btrfs_root_id(root))) {
111892a7cc42SQu Wenruo 		set_bit(BTRFS_ROOT_SHAREABLE, &root->state);
1119f39e4571SJosef Bacik 		btrfs_check_and_init_root_item(&root->root_item);
1120f39e4571SJosef Bacik 	}
1121f39e4571SJosef Bacik 
1122851fd730SQu Wenruo 	/*
1123851fd730SQu Wenruo 	 * Don't assign anonymous block device to roots that are not exposed to
1124851fd730SQu Wenruo 	 * userspace, the id pool is limited to 1M
1125851fd730SQu Wenruo 	 */
1126e094f480SJosef Bacik 	if (is_fstree(btrfs_root_id(root)) &&
1127851fd730SQu Wenruo 	    btrfs_root_refs(&root->root_item) > 0) {
11282dfb1e43SQu Wenruo 		if (!anon_dev) {
1129cb517eabSMiao Xie 			ret = get_anon_bdev(&root->anon_dev);
1130cb517eabSMiao Xie 			if (ret)
1131876d2cf1SLiu Bo 				goto fail;
11322dfb1e43SQu Wenruo 		} else {
11332dfb1e43SQu Wenruo 			root->anon_dev = anon_dev;
11342dfb1e43SQu Wenruo 		}
1135851fd730SQu Wenruo 	}
1136f32e48e9SChandan Rajendra 
1137f32e48e9SChandan Rajendra 	mutex_lock(&root->objectid_mutex);
1138453e4873SNikolay Borisov 	ret = btrfs_init_root_free_objectid(root);
1139f32e48e9SChandan Rajendra 	if (ret) {
1140f32e48e9SChandan Rajendra 		mutex_unlock(&root->objectid_mutex);
1141876d2cf1SLiu Bo 		goto fail;
1142f32e48e9SChandan Rajendra 	}
1143f32e48e9SChandan Rajendra 
11446b8fad57SNikolay Borisov 	ASSERT(root->free_objectid <= BTRFS_LAST_FREE_OBJECTID);
1145f32e48e9SChandan Rajendra 
1146f32e48e9SChandan Rajendra 	mutex_unlock(&root->objectid_mutex);
1147f32e48e9SChandan Rajendra 
1148cb517eabSMiao Xie 	return 0;
1149cb517eabSMiao Xie fail:
115084db5ccfSDavid Sterba 	/* The caller is responsible to call btrfs_free_fs_root */
1151cb517eabSMiao Xie 	return ret;
1152cb517eabSMiao Xie }
1153cb517eabSMiao Xie 
btrfs_lookup_fs_root(struct btrfs_fs_info * fs_info,u64 root_id)1154a98db0f3SJosef Bacik static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
1155cb517eabSMiao Xie 					       u64 root_id)
1156cb517eabSMiao Xie {
1157cb517eabSMiao Xie 	struct btrfs_root *root;
1158cb517eabSMiao Xie 
1159fc7cbcd4SDavid Sterba 	spin_lock(&fs_info->fs_roots_radix_lock);
1160fc7cbcd4SDavid Sterba 	root = radix_tree_lookup(&fs_info->fs_roots_radix,
1161fc7cbcd4SDavid Sterba 				 (unsigned long)root_id);
116200246528SJosef Bacik 	root = btrfs_grab_root(root);
1163fc7cbcd4SDavid Sterba 	spin_unlock(&fs_info->fs_roots_radix_lock);
1164cb517eabSMiao Xie 	return root;
1165cb517eabSMiao Xie }
1166cb517eabSMiao Xie 
btrfs_get_global_root(struct btrfs_fs_info * fs_info,u64 objectid)116749d11beaSJosef Bacik static struct btrfs_root *btrfs_get_global_root(struct btrfs_fs_info *fs_info,
116849d11beaSJosef Bacik 						u64 objectid)
116949d11beaSJosef Bacik {
1170abed4aaaSJosef Bacik 	struct btrfs_key key = {
1171abed4aaaSJosef Bacik 		.objectid = objectid,
1172abed4aaaSJosef Bacik 		.type = BTRFS_ROOT_ITEM_KEY,
1173abed4aaaSJosef Bacik 		.offset = 0,
1174abed4aaaSJosef Bacik 	};
1175abed4aaaSJosef Bacik 
1176e91909aaSChristoph Hellwig 	switch (objectid) {
1177e91909aaSChristoph Hellwig 	case BTRFS_ROOT_TREE_OBJECTID:
117849d11beaSJosef Bacik 		return btrfs_grab_root(fs_info->tree_root);
1179e91909aaSChristoph Hellwig 	case BTRFS_EXTENT_TREE_OBJECTID:
1180abed4aaaSJosef Bacik 		return btrfs_grab_root(btrfs_global_root(fs_info, &key));
1181e91909aaSChristoph Hellwig 	case BTRFS_CHUNK_TREE_OBJECTID:
118249d11beaSJosef Bacik 		return btrfs_grab_root(fs_info->chunk_root);
1183e91909aaSChristoph Hellwig 	case BTRFS_DEV_TREE_OBJECTID:
118449d11beaSJosef Bacik 		return btrfs_grab_root(fs_info->dev_root);
1185e91909aaSChristoph Hellwig 	case BTRFS_CSUM_TREE_OBJECTID:
1186abed4aaaSJosef Bacik 		return btrfs_grab_root(btrfs_global_root(fs_info, &key));
1187e91909aaSChristoph Hellwig 	case BTRFS_QUOTA_TREE_OBJECTID:
118885724171SChristoph Hellwig 		return btrfs_grab_root(fs_info->quota_root);
1189e91909aaSChristoph Hellwig 	case BTRFS_UUID_TREE_OBJECTID:
119085724171SChristoph Hellwig 		return btrfs_grab_root(fs_info->uuid_root);
1191e91909aaSChristoph Hellwig 	case BTRFS_BLOCK_GROUP_TREE_OBJECTID:
119285724171SChristoph Hellwig 		return btrfs_grab_root(fs_info->block_group_root);
1193e91909aaSChristoph Hellwig 	case BTRFS_FREE_SPACE_TREE_OBJECTID:
119485724171SChristoph Hellwig 		return btrfs_grab_root(btrfs_global_root(fs_info, &key));
119551502090SJohannes Thumshirn 	case BTRFS_RAID_STRIPE_TREE_OBJECTID:
119651502090SJohannes Thumshirn 		return btrfs_grab_root(fs_info->stripe_root);
1197e91909aaSChristoph Hellwig 	default:
119849d11beaSJosef Bacik 		return NULL;
119949d11beaSJosef Bacik 	}
1200e91909aaSChristoph Hellwig }
120149d11beaSJosef Bacik 
btrfs_insert_fs_root(struct btrfs_fs_info * fs_info,struct btrfs_root * root)1202cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info,
1203cb517eabSMiao Xie 			 struct btrfs_root *root)
1204cb517eabSMiao Xie {
1205cb517eabSMiao Xie 	int ret;
1206cb517eabSMiao Xie 
1207fc7cbcd4SDavid Sterba 	ret = radix_tree_preload(GFP_NOFS);
1208fc7cbcd4SDavid Sterba 	if (ret)
1209fc7cbcd4SDavid Sterba 		return ret;
1210fc7cbcd4SDavid Sterba 
1211fc7cbcd4SDavid Sterba 	spin_lock(&fs_info->fs_roots_radix_lock);
1212fc7cbcd4SDavid Sterba 	ret = radix_tree_insert(&fs_info->fs_roots_radix,
1213e094f480SJosef Bacik 				(unsigned long)btrfs_root_id(root),
1214fc7cbcd4SDavid Sterba 				root);
1215af01d2e5SJosef Bacik 	if (ret == 0) {
121600246528SJosef Bacik 		btrfs_grab_root(root);
1217fc7cbcd4SDavid Sterba 		set_bit(BTRFS_ROOT_IN_RADIX, &root->state);
1218af01d2e5SJosef Bacik 	}
1219fc7cbcd4SDavid Sterba 	spin_unlock(&fs_info->fs_roots_radix_lock);
1220fc7cbcd4SDavid Sterba 	radix_tree_preload_end();
1221cb517eabSMiao Xie 
1222cb517eabSMiao Xie 	return ret;
1223cb517eabSMiao Xie }
1224cb517eabSMiao Xie 
btrfs_check_leaked_roots(const struct btrfs_fs_info * fs_info)12252917f741SDavid Sterba void btrfs_check_leaked_roots(const struct btrfs_fs_info *fs_info)
1226bd647ce3SJosef Bacik {
1227bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
1228bd647ce3SJosef Bacik 	struct btrfs_root *root;
1229bd647ce3SJosef Bacik 
1230bd647ce3SJosef Bacik 	while (!list_empty(&fs_info->allocated_roots)) {
1231457f1864SJosef Bacik 		char buf[BTRFS_ROOT_NAME_BUF_LEN];
1232457f1864SJosef Bacik 
1233bd647ce3SJosef Bacik 		root = list_first_entry(&fs_info->allocated_roots,
1234bd647ce3SJosef Bacik 					struct btrfs_root, leak_list);
1235457f1864SJosef Bacik 		btrfs_err(fs_info, "leaked root %s refcount %d",
123671008734SJosef Bacik 			  btrfs_root_name(&root->root_key, buf),
1237bd647ce3SJosef Bacik 			  refcount_read(&root->refs));
12388fd2b12eSJosef Bacik 		WARN_ON_ONCE(1);
1239bd647ce3SJosef Bacik 		while (refcount_read(&root->refs) > 1)
124000246528SJosef Bacik 			btrfs_put_root(root);
124100246528SJosef Bacik 		btrfs_put_root(root);
1242bd647ce3SJosef Bacik 	}
1243bd647ce3SJosef Bacik #endif
1244bd647ce3SJosef Bacik }
1245bd647ce3SJosef Bacik 
free_global_roots(struct btrfs_fs_info * fs_info)1246abed4aaaSJosef Bacik static void free_global_roots(struct btrfs_fs_info *fs_info)
1247abed4aaaSJosef Bacik {
1248abed4aaaSJosef Bacik 	struct btrfs_root *root;
1249abed4aaaSJosef Bacik 	struct rb_node *node;
1250abed4aaaSJosef Bacik 
1251abed4aaaSJosef Bacik 	while ((node = rb_first_postorder(&fs_info->global_root_tree)) != NULL) {
1252abed4aaaSJosef Bacik 		root = rb_entry(node, struct btrfs_root, rb_node);
1253abed4aaaSJosef Bacik 		rb_erase(&root->rb_node, &fs_info->global_root_tree);
1254abed4aaaSJosef Bacik 		btrfs_put_root(root);
1255abed4aaaSJosef Bacik 	}
1256abed4aaaSJosef Bacik }
1257abed4aaaSJosef Bacik 
btrfs_free_fs_info(struct btrfs_fs_info * fs_info)12580d4b0463SJosef Bacik void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
12590d4b0463SJosef Bacik {
1260f1d97e76SFilipe Manana 	struct percpu_counter *em_counter = &fs_info->evictable_extent_maps;
1261f1d97e76SFilipe Manana 
1262141386e1SJosef Bacik 	percpu_counter_destroy(&fs_info->dirty_metadata_bytes);
1263141386e1SJosef Bacik 	percpu_counter_destroy(&fs_info->delalloc_bytes);
12645deb17e1SJosef Bacik 	percpu_counter_destroy(&fs_info->ordered_bytes);
1265f1d97e76SFilipe Manana 	if (percpu_counter_initialized(em_counter))
1266f1d97e76SFilipe Manana 		ASSERT(percpu_counter_sum_positive(em_counter) == 0);
1267f1d97e76SFilipe Manana 	percpu_counter_destroy(em_counter);
1268141386e1SJosef Bacik 	percpu_counter_destroy(&fs_info->dev_replace.bio_counter);
1269141386e1SJosef Bacik 	btrfs_free_csum_hash(fs_info);
1270141386e1SJosef Bacik 	btrfs_free_stripe_hash_table(fs_info);
1271141386e1SJosef Bacik 	btrfs_free_ref_cache(fs_info);
12720d4b0463SJosef Bacik 	kfree(fs_info->balance_ctl);
12730d4b0463SJosef Bacik 	kfree(fs_info->delayed_root);
1274abed4aaaSJosef Bacik 	free_global_roots(fs_info);
127500246528SJosef Bacik 	btrfs_put_root(fs_info->tree_root);
127600246528SJosef Bacik 	btrfs_put_root(fs_info->chunk_root);
127700246528SJosef Bacik 	btrfs_put_root(fs_info->dev_root);
127800246528SJosef Bacik 	btrfs_put_root(fs_info->quota_root);
127900246528SJosef Bacik 	btrfs_put_root(fs_info->uuid_root);
128000246528SJosef Bacik 	btrfs_put_root(fs_info->fs_root);
1281aeb935a4SQu Wenruo 	btrfs_put_root(fs_info->data_reloc_root);
12829c54e80dSJosef Bacik 	btrfs_put_root(fs_info->block_group_root);
128351502090SJohannes Thumshirn 	btrfs_put_root(fs_info->stripe_root);
1284bd647ce3SJosef Bacik 	btrfs_check_leaked_roots(fs_info);
12853fd63727SJosef Bacik 	btrfs_extent_buffer_leak_debug_check(fs_info);
12860d4b0463SJosef Bacik 	kfree(fs_info->super_copy);
12870d4b0463SJosef Bacik 	kfree(fs_info->super_for_commit);
12880d4b0463SJosef Bacik 	kvfree(fs_info);
12890d4b0463SJosef Bacik }
12900d4b0463SJosef Bacik 
12910d4b0463SJosef Bacik 
12922dfb1e43SQu Wenruo /*
12932dfb1e43SQu Wenruo  * Get an in-memory reference of a root structure.
12942dfb1e43SQu Wenruo  *
12952dfb1e43SQu Wenruo  * For essential trees like root/extent tree, we grab it from fs_info directly.
12962dfb1e43SQu Wenruo  * For subvolume trees, we check the cached filesystem roots first. If not
12972dfb1e43SQu Wenruo  * found, then read it from disk and add it to cached fs roots.
12982dfb1e43SQu Wenruo  *
12992dfb1e43SQu Wenruo  * Caller should release the root by calling btrfs_put_root() after the usage.
13002dfb1e43SQu Wenruo  *
13012dfb1e43SQu Wenruo  * NOTE: Reloc and log trees can't be read by this function as they share the
13022dfb1e43SQu Wenruo  *	 same root objectid.
13032dfb1e43SQu Wenruo  *
13042dfb1e43SQu Wenruo  * @objectid:	root id
13052dfb1e43SQu Wenruo  * @anon_dev:	preallocated anonymous block device number for new roots,
1306e2b54eafSFilipe Manana  *		pass NULL for a new allocation.
13072dfb1e43SQu Wenruo  * @check_ref:	whether to check root item references, If true, return -ENOENT
13082dfb1e43SQu Wenruo  *		for orphan roots
13092dfb1e43SQu Wenruo  */
btrfs_get_root_ref(struct btrfs_fs_info * fs_info,u64 objectid,dev_t * anon_dev,bool check_ref)13102dfb1e43SQu Wenruo static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info,
1311e2b54eafSFilipe Manana 					     u64 objectid, dev_t *anon_dev,
13122dfb1e43SQu Wenruo 					     bool check_ref)
13135eda7b5eSChris Mason {
13145eda7b5eSChris Mason 	struct btrfs_root *root;
1315381cf658SDavid Sterba 	struct btrfs_path *path;
13161d4c08e0SDavid Sterba 	struct btrfs_key key;
13175eda7b5eSChris Mason 	int ret;
13185eda7b5eSChris Mason 
131949d11beaSJosef Bacik 	root = btrfs_get_global_root(fs_info, objectid);
132049d11beaSJosef Bacik 	if (root)
132149d11beaSJosef Bacik 		return root;
1322773e722aSQu Wenruo 
1323773e722aSQu Wenruo 	/*
1324773e722aSQu Wenruo 	 * If we're called for non-subvolume trees, and above function didn't
1325773e722aSQu Wenruo 	 * find one, do not try to read it from disk.
1326773e722aSQu Wenruo 	 *
1327773e722aSQu Wenruo 	 * This is namely for free-space-tree and quota tree, which can change
1328773e722aSQu Wenruo 	 * at runtime and should only be grabbed from fs_info.
1329773e722aSQu Wenruo 	 */
1330773e722aSQu Wenruo 	if (!is_fstree(objectid) && objectid != BTRFS_DATA_RELOC_TREE_OBJECTID)
1331773e722aSQu Wenruo 		return ERR_PTR(-ENOENT);
13324df27c4dSYan, Zheng again:
133356e9357aSDavid Sterba 	root = btrfs_lookup_fs_root(fs_info, objectid);
133448475471SStefan Behrens 	if (root) {
1335e03ee2feSQu Wenruo 		/*
1336e03ee2feSQu Wenruo 		 * Some other caller may have read out the newly inserted
1337e03ee2feSQu Wenruo 		 * subvolume already (for things like backref walk etc).  Not
1338e03ee2feSQu Wenruo 		 * that common but still possible.  In that case, we just need
1339e03ee2feSQu Wenruo 		 * to free the anon_dev.
1340e03ee2feSQu Wenruo 		 */
1341e2b54eafSFilipe Manana 		if (unlikely(anon_dev && *anon_dev)) {
1342e2b54eafSFilipe Manana 			free_anon_bdev(*anon_dev);
1343e2b54eafSFilipe Manana 			*anon_dev = 0;
1344e03ee2feSQu Wenruo 		}
1345e03ee2feSQu Wenruo 
1346bc44d7c4SJosef Bacik 		if (check_ref && btrfs_root_refs(&root->root_item) == 0) {
134700246528SJosef Bacik 			btrfs_put_root(root);
134848475471SStefan Behrens 			return ERR_PTR(-ENOENT);
1349bc44d7c4SJosef Bacik 		}
13505eda7b5eSChris Mason 		return root;
135148475471SStefan Behrens 	}
13525eda7b5eSChris Mason 
135356e9357aSDavid Sterba 	key.objectid = objectid;
135456e9357aSDavid Sterba 	key.type = BTRFS_ROOT_ITEM_KEY;
135556e9357aSDavid Sterba 	key.offset = (u64)-1;
135656e9357aSDavid Sterba 	root = btrfs_read_tree_root(fs_info->tree_root, &key);
13575eda7b5eSChris Mason 	if (IS_ERR(root))
13585eda7b5eSChris Mason 		return root;
13593394e160SChris Mason 
1360c00869f1SMiao Xie 	if (check_ref && btrfs_root_refs(&root->root_item) == 0) {
1361d68fc57bSYan, Zheng 		ret = -ENOENT;
1362d68fc57bSYan, Zheng 		goto fail;
1363d68fc57bSYan, Zheng 	}
1364d68fc57bSYan, Zheng 
1365e2b54eafSFilipe Manana 	ret = btrfs_init_fs_root(root, anon_dev ? *anon_dev : 0);
1366cb517eabSMiao Xie 	if (ret)
1367cb517eabSMiao Xie 		goto fail;
1368cb517eabSMiao Xie 
1369381cf658SDavid Sterba 	path = btrfs_alloc_path();
1370381cf658SDavid Sterba 	if (!path) {
1371381cf658SDavid Sterba 		ret = -ENOMEM;
1372381cf658SDavid Sterba 		goto fail;
1373381cf658SDavid Sterba 	}
13741d4c08e0SDavid Sterba 	key.objectid = BTRFS_ORPHAN_OBJECTID;
13751d4c08e0SDavid Sterba 	key.type = BTRFS_ORPHAN_ITEM_KEY;
137656e9357aSDavid Sterba 	key.offset = objectid;
13771d4c08e0SDavid Sterba 
13781d4c08e0SDavid Sterba 	ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
1379381cf658SDavid Sterba 	btrfs_free_path(path);
1380d68fc57bSYan, Zheng 	if (ret < 0)
1381d68fc57bSYan, Zheng 		goto fail;
1382d68fc57bSYan, Zheng 	if (ret == 0)
138327cdeb70SMiao Xie 		set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state);
1384d68fc57bSYan, Zheng 
1385cb517eabSMiao Xie 	ret = btrfs_insert_fs_root(fs_info, root);
13860f7d52f4SChris Mason 	if (ret) {
1387168a2f77SJia-Ju Bai 		if (ret == -EEXIST) {
138800246528SJosef Bacik 			btrfs_put_root(root);
13894df27c4dSYan, Zheng 			goto again;
1390168a2f77SJia-Ju Bai 		}
13914df27c4dSYan, Zheng 		goto fail;
13924df27c4dSYan, Zheng 	}
1393edbd8d4eSChris Mason 	return root;
13944df27c4dSYan, Zheng fail:
139533fab972SFilipe Manana 	/*
139633fab972SFilipe Manana 	 * If our caller provided us an anonymous device, then it's his
1397143823cfSDavid Sterba 	 * responsibility to free it in case we fail. So we have to set our
139833fab972SFilipe Manana 	 * root's anon_dev to 0 to avoid a double free, once by btrfs_put_root()
139933fab972SFilipe Manana 	 * and once again by our caller.
140033fab972SFilipe Manana 	 */
1401e2b54eafSFilipe Manana 	if (anon_dev && *anon_dev)
140233fab972SFilipe Manana 		root->anon_dev = 0;
14038c38938cSJosef Bacik 	btrfs_put_root(root);
14044df27c4dSYan, Zheng 	return ERR_PTR(ret);
1405edbd8d4eSChris Mason }
1406edbd8d4eSChris Mason 
14072dfb1e43SQu Wenruo /*
14082dfb1e43SQu Wenruo  * Get in-memory reference of a root structure
14092dfb1e43SQu Wenruo  *
14102dfb1e43SQu Wenruo  * @objectid:	tree objectid
14112dfb1e43SQu Wenruo  * @check_ref:	if set, verify that the tree exists and the item has at least
14122dfb1e43SQu Wenruo  *		one reference
14132dfb1e43SQu Wenruo  */
btrfs_get_fs_root(struct btrfs_fs_info * fs_info,u64 objectid,bool check_ref)14142dfb1e43SQu Wenruo struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info,
14152dfb1e43SQu Wenruo 				     u64 objectid, bool check_ref)
14162dfb1e43SQu Wenruo {
1417e2b54eafSFilipe Manana 	return btrfs_get_root_ref(fs_info, objectid, NULL, check_ref);
14182dfb1e43SQu Wenruo }
14192dfb1e43SQu Wenruo 
14202dfb1e43SQu Wenruo /*
14212dfb1e43SQu Wenruo  * Get in-memory reference of a root structure, created as new, optionally pass
14222dfb1e43SQu Wenruo  * the anonymous block device id
14232dfb1e43SQu Wenruo  *
14242dfb1e43SQu Wenruo  * @objectid:	tree objectid
1425e2b54eafSFilipe Manana  * @anon_dev:	if NULL, allocate a new anonymous block device or use the
1426e2b54eafSFilipe Manana  *		parameter value if not NULL
14272dfb1e43SQu Wenruo  */
btrfs_get_new_fs_root(struct btrfs_fs_info * fs_info,u64 objectid,dev_t * anon_dev)14282dfb1e43SQu Wenruo struct btrfs_root *btrfs_get_new_fs_root(struct btrfs_fs_info *fs_info,
1429e2b54eafSFilipe Manana 					 u64 objectid, dev_t *anon_dev)
14302dfb1e43SQu Wenruo {
14312dfb1e43SQu Wenruo 	return btrfs_get_root_ref(fs_info, objectid, anon_dev, true);
14322dfb1e43SQu Wenruo }
14332dfb1e43SQu Wenruo 
14348b712842SChris Mason /*
14359580503bSDavid Sterba  * Return a root for the given objectid.
14369580503bSDavid Sterba  *
143749d11beaSJosef Bacik  * @fs_info:	the fs_info
143849d11beaSJosef Bacik  * @objectid:	the objectid we need to lookup
143949d11beaSJosef Bacik  *
144049d11beaSJosef Bacik  * This is exclusively used for backref walking, and exists specifically because
144149d11beaSJosef Bacik  * of how qgroups does lookups.  Qgroups will do a backref lookup at delayed ref
144249d11beaSJosef Bacik  * creation time, which means we may have to read the tree_root in order to look
144349d11beaSJosef Bacik  * up a fs root that is not in memory.  If the root is not in memory we will
144449d11beaSJosef Bacik  * read the tree root commit root and look up the fs root from there.  This is a
144549d11beaSJosef Bacik  * temporary root, it will not be inserted into the radix tree as it doesn't
144649d11beaSJosef Bacik  * have the most uptodate information, it'll simply be discarded once the
144749d11beaSJosef Bacik  * backref code is finished using the root.
144849d11beaSJosef Bacik  */
btrfs_get_fs_root_commit_root(struct btrfs_fs_info * fs_info,struct btrfs_path * path,u64 objectid)144949d11beaSJosef Bacik struct btrfs_root *btrfs_get_fs_root_commit_root(struct btrfs_fs_info *fs_info,
145049d11beaSJosef Bacik 						 struct btrfs_path *path,
145149d11beaSJosef Bacik 						 u64 objectid)
145249d11beaSJosef Bacik {
145349d11beaSJosef Bacik 	struct btrfs_root *root;
145449d11beaSJosef Bacik 	struct btrfs_key key;
145549d11beaSJosef Bacik 
145649d11beaSJosef Bacik 	ASSERT(path->search_commit_root && path->skip_locking);
145749d11beaSJosef Bacik 
145849d11beaSJosef Bacik 	/*
145949d11beaSJosef Bacik 	 * This can return -ENOENT if we ask for a root that doesn't exist, but
146049d11beaSJosef Bacik 	 * since this is called via the backref walking code we won't be looking
146149d11beaSJosef Bacik 	 * up a root that doesn't exist, unless there's corruption.  So if root
146249d11beaSJosef Bacik 	 * != NULL just return it.
146349d11beaSJosef Bacik 	 */
146449d11beaSJosef Bacik 	root = btrfs_get_global_root(fs_info, objectid);
146549d11beaSJosef Bacik 	if (root)
146649d11beaSJosef Bacik 		return root;
146749d11beaSJosef Bacik 
146849d11beaSJosef Bacik 	root = btrfs_lookup_fs_root(fs_info, objectid);
146949d11beaSJosef Bacik 	if (root)
147049d11beaSJosef Bacik 		return root;
147149d11beaSJosef Bacik 
147249d11beaSJosef Bacik 	key.objectid = objectid;
147349d11beaSJosef Bacik 	key.type = BTRFS_ROOT_ITEM_KEY;
147449d11beaSJosef Bacik 	key.offset = (u64)-1;
147549d11beaSJosef Bacik 	root = read_tree_root_path(fs_info->tree_root, path, &key);
147649d11beaSJosef Bacik 	btrfs_release_path(path);
147749d11beaSJosef Bacik 
147849d11beaSJosef Bacik 	return root;
147949d11beaSJosef Bacik }
148049d11beaSJosef Bacik 
cleaner_kthread(void * arg)1481a74a4b97SChris Mason static int cleaner_kthread(void *arg)
1482a74a4b97SChris Mason {
14830d031dc4SYu Zhe 	struct btrfs_fs_info *fs_info = arg;
1484d0278245SMiao Xie 	int again;
1485a74a4b97SChris Mason 
1486d6fd0ae2SOmar Sandoval 	while (1) {
1487d0278245SMiao Xie 		again = 0;
14889d1a2a3aSDavid Sterba 
1489fd340d0fSJosef Bacik 		set_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags);
1490fd340d0fSJosef Bacik 
1491d0278245SMiao Xie 		/* Make the cleaner go to sleep early. */
14922ff7e61eSJeff Mahoney 		if (btrfs_need_cleaner_sleep(fs_info))
1493d0278245SMiao Xie 			goto sleep;
1494d0278245SMiao Xie 
149590c711abSZygo Blaxell 		/*
149690c711abSZygo Blaxell 		 * Do not do anything if we might cause open_ctree() to block
149790c711abSZygo Blaxell 		 * before we have finished mounting the filesystem.
149890c711abSZygo Blaxell 		 */
14990b246afaSJeff Mahoney 		if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
150090c711abSZygo Blaxell 			goto sleep;
150190c711abSZygo Blaxell 
15020b246afaSJeff Mahoney 		if (!mutex_trylock(&fs_info->cleaner_mutex))
1503d0278245SMiao Xie 			goto sleep;
1504d0278245SMiao Xie 
1505dc7f370cSMiao Xie 		/*
1506dc7f370cSMiao Xie 		 * Avoid the problem that we change the status of the fs
1507dc7f370cSMiao Xie 		 * during the above check and trylock.
1508dc7f370cSMiao Xie 		 */
15092ff7e61eSJeff Mahoney 		if (btrfs_need_cleaner_sleep(fs_info)) {
15100b246afaSJeff Mahoney 			mutex_unlock(&fs_info->cleaner_mutex);
1511dc7f370cSMiao Xie 			goto sleep;
1512dc7f370cSMiao Xie 		}
1513dc7f370cSMiao Xie 
1514b7625f46SQu Wenruo 		if (test_and_clear_bit(BTRFS_FS_FEATURE_CHANGED, &fs_info->flags))
1515b7625f46SQu Wenruo 			btrfs_sysfs_feature_update(fs_info);
1516b7625f46SQu Wenruo 
15172ff7e61eSJeff Mahoney 		btrfs_run_delayed_iputs(fs_info);
1518c2d6cb16SFilipe Manana 
151933c44184SJosef Bacik 		again = btrfs_clean_one_deleted_snapshot(fs_info);
15200b246afaSJeff Mahoney 		mutex_unlock(&fs_info->cleaner_mutex);
1521a74a4b97SChris Mason 
1522d0278245SMiao Xie 		/*
152305323cd1SMiao Xie 		 * The defragger has dealt with the R/O remount and umount,
152405323cd1SMiao Xie 		 * needn't do anything special here.
1525d0278245SMiao Xie 		 */
15260b246afaSJeff Mahoney 		btrfs_run_defrag_inodes(fs_info);
152767c5e7d4SFilipe Manana 
152867c5e7d4SFilipe Manana 		/*
1529f3372065SJohannes Thumshirn 		 * Acquires fs_info->reclaim_bgs_lock to avoid racing
153067c5e7d4SFilipe Manana 		 * with relocation (btrfs_relocate_chunk) and relocation
153167c5e7d4SFilipe Manana 		 * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group)
1532f3372065SJohannes Thumshirn 		 * after acquiring fs_info->reclaim_bgs_lock. So we
153367c5e7d4SFilipe Manana 		 * can't hold, nor need to, fs_info->cleaner_mutex when deleting
153467c5e7d4SFilipe Manana 		 * unused block groups.
153567c5e7d4SFilipe Manana 		 */
15360b246afaSJeff Mahoney 		btrfs_delete_unused_bgs(fs_info);
153718bb8bbfSJohannes Thumshirn 
153818bb8bbfSJohannes Thumshirn 		/*
153918bb8bbfSJohannes Thumshirn 		 * Reclaim block groups in the reclaim_bgs list after we deleted
154018bb8bbfSJohannes Thumshirn 		 * all unused block_groups. This possibly gives us some more free
154118bb8bbfSJohannes Thumshirn 		 * space.
154218bb8bbfSJohannes Thumshirn 		 */
154318bb8bbfSJohannes Thumshirn 		btrfs_reclaim_bgs(fs_info);
1544d0278245SMiao Xie sleep:
1545a0a1db70SFilipe Manana 		clear_and_wake_up_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags);
1546d6fd0ae2SOmar Sandoval 		if (kthread_should_park())
1547d6fd0ae2SOmar Sandoval 			kthread_parkme();
1548d6fd0ae2SOmar Sandoval 		if (kthread_should_stop())
1549d6fd0ae2SOmar Sandoval 			return 0;
1550838fe188SJiri Kosina 		if (!again) {
1551a74a4b97SChris Mason 			set_current_state(TASK_INTERRUPTIBLE);
1552a74a4b97SChris Mason 			schedule();
1553a74a4b97SChris Mason 			__set_current_state(TASK_RUNNING);
1554a74a4b97SChris Mason 		}
1555da288d28SFilipe Manana 	}
1556a74a4b97SChris Mason }
1557a74a4b97SChris Mason 
transaction_kthread(void * arg)1558a74a4b97SChris Mason static int transaction_kthread(void *arg)
1559a74a4b97SChris Mason {
1560a74a4b97SChris Mason 	struct btrfs_root *root = arg;
15610b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
1562a74a4b97SChris Mason 	struct btrfs_trans_handle *trans;
1563a74a4b97SChris Mason 	struct btrfs_transaction *cur;
15648929ecfaSYan, Zheng 	u64 transid;
1565643900beSNikolay Borisov 	time64_t delta;
1566a74a4b97SChris Mason 	unsigned long delay;
1567914b2007SJan Kara 	bool cannot_commit;
1568a74a4b97SChris Mason 
1569a74a4b97SChris Mason 	do {
1570914b2007SJan Kara 		cannot_commit = false;
1571ba1bc00fSNikolay Borisov 		delay = msecs_to_jiffies(fs_info->commit_interval * 1000);
15720b246afaSJeff Mahoney 		mutex_lock(&fs_info->transaction_kthread_mutex);
1573a74a4b97SChris Mason 
15740b246afaSJeff Mahoney 		spin_lock(&fs_info->trans_lock);
15750b246afaSJeff Mahoney 		cur = fs_info->running_transaction;
1576a74a4b97SChris Mason 		if (!cur) {
15770b246afaSJeff Mahoney 			spin_unlock(&fs_info->trans_lock);
1578a74a4b97SChris Mason 			goto sleep;
1579a74a4b97SChris Mason 		}
158031153d81SYan Zheng 
1581643900beSNikolay Borisov 		delta = ktime_get_seconds() - cur->start_time;
1582fdfbf020SJosef Bacik 		if (!test_and_clear_bit(BTRFS_FS_COMMIT_TRANS, &fs_info->flags) &&
158377d20c68SJosef Bacik 		    cur->state < TRANS_STATE_COMMIT_PREP &&
1584643900beSNikolay Borisov 		    delta < fs_info->commit_interval) {
15850b246afaSJeff Mahoney 			spin_unlock(&fs_info->trans_lock);
1586fb8a7e94SNikolay Borisov 			delay -= msecs_to_jiffies((delta - 1) * 1000);
1587fb8a7e94SNikolay Borisov 			delay = min(delay,
1588fb8a7e94SNikolay Borisov 				    msecs_to_jiffies(fs_info->commit_interval * 1000));
1589a74a4b97SChris Mason 			goto sleep;
1590a74a4b97SChris Mason 		}
15918929ecfaSYan, Zheng 		transid = cur->transid;
15920b246afaSJeff Mahoney 		spin_unlock(&fs_info->trans_lock);
159356bec294SChris Mason 
159479787eaaSJeff Mahoney 		/* If the file system is aborted, this will always fail. */
1595354aa0fbSMiao Xie 		trans = btrfs_attach_transaction(root);
1596914b2007SJan Kara 		if (IS_ERR(trans)) {
1597354aa0fbSMiao Xie 			if (PTR_ERR(trans) != -ENOENT)
1598914b2007SJan Kara 				cannot_commit = true;
159979787eaaSJeff Mahoney 			goto sleep;
1600914b2007SJan Kara 		}
16018929ecfaSYan, Zheng 		if (transid == trans->transid) {
16023a45bb20SJeff Mahoney 			btrfs_commit_transaction(trans);
16038929ecfaSYan, Zheng 		} else {
16043a45bb20SJeff Mahoney 			btrfs_end_transaction(trans);
16058929ecfaSYan, Zheng 		}
1606a74a4b97SChris Mason sleep:
16070b246afaSJeff Mahoney 		wake_up_process(fs_info->cleaner_kthread);
16080b246afaSJeff Mahoney 		mutex_unlock(&fs_info->transaction_kthread_mutex);
1609a74a4b97SChris Mason 
161084961539SJosef Bacik 		if (BTRFS_FS_ERROR(fs_info))
16112ff7e61eSJeff Mahoney 			btrfs_cleanup_transaction(fs_info);
16128929ecfaSYan, Zheng 		if (!kthread_should_stop() &&
16130b246afaSJeff Mahoney 				(!btrfs_transaction_blocked(fs_info) ||
1614914b2007SJan Kara 				 cannot_commit))
1615bc5511d0SNikolay Borisov 			schedule_timeout_interruptible(delay);
1616a74a4b97SChris Mason 	} while (!kthread_should_stop());
1617a74a4b97SChris Mason 	return 0;
1618a74a4b97SChris Mason }
1619a74a4b97SChris Mason 
1620af31f5e5SChris Mason /*
162101f0f9daSNikolay Borisov  * This will find the highest generation in the array of root backups.  The
162201f0f9daSNikolay Borisov  * index of the highest array is returned, or -EINVAL if we can't find
162301f0f9daSNikolay Borisov  * anything.
1624af31f5e5SChris Mason  *
1625af31f5e5SChris Mason  * We check to make sure the array is valid by comparing the
1626af31f5e5SChris Mason  * generation of the latest  root in the array with the generation
1627af31f5e5SChris Mason  * in the super block.  If they don't match we pitch it.
1628af31f5e5SChris Mason  */
find_newest_super_backup(struct btrfs_fs_info * info)162901f0f9daSNikolay Borisov static int find_newest_super_backup(struct btrfs_fs_info *info)
1630af31f5e5SChris Mason {
163101f0f9daSNikolay Borisov 	const u64 newest_gen = btrfs_super_generation(info->super_copy);
1632af31f5e5SChris Mason 	u64 cur;
1633af31f5e5SChris Mason 	struct btrfs_root_backup *root_backup;
1634af31f5e5SChris Mason 	int i;
1635af31f5e5SChris Mason 
1636af31f5e5SChris Mason 	for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
1637af31f5e5SChris Mason 		root_backup = info->super_copy->super_roots + i;
1638af31f5e5SChris Mason 		cur = btrfs_backup_tree_root_gen(root_backup);
1639af31f5e5SChris Mason 		if (cur == newest_gen)
164001f0f9daSNikolay Borisov 			return i;
1641af31f5e5SChris Mason 	}
1642af31f5e5SChris Mason 
164301f0f9daSNikolay Borisov 	return -EINVAL;
1644af31f5e5SChris Mason }
1645af31f5e5SChris Mason 
1646af31f5e5SChris Mason /*
1647af31f5e5SChris Mason  * copy all the root pointers into the super backup array.
1648af31f5e5SChris Mason  * this will bump the backup pointer by one when it is
1649af31f5e5SChris Mason  * done
1650af31f5e5SChris Mason  */
backup_super_roots(struct btrfs_fs_info * info)1651af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info)
1652af31f5e5SChris Mason {
16536ef108ddSNikolay Borisov 	const int next_backup = info->backup_root_index;
1654af31f5e5SChris Mason 	struct btrfs_root_backup *root_backup;
1655af31f5e5SChris Mason 
1656af31f5e5SChris Mason 	root_backup = info->super_for_commit->super_roots + next_backup;
1657af31f5e5SChris Mason 
1658af31f5e5SChris Mason 	/*
1659af31f5e5SChris Mason 	 * make sure all of our padding and empty slots get zero filled
1660af31f5e5SChris Mason 	 * regardless of which ones we use today
1661af31f5e5SChris Mason 	 */
1662af31f5e5SChris Mason 	memset(root_backup, 0, sizeof(*root_backup));
1663af31f5e5SChris Mason 
1664af31f5e5SChris Mason 	info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS;
1665af31f5e5SChris Mason 
1666af31f5e5SChris Mason 	btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start);
1667af31f5e5SChris Mason 	btrfs_set_backup_tree_root_gen(root_backup,
1668af31f5e5SChris Mason 			       btrfs_header_generation(info->tree_root->node));
1669af31f5e5SChris Mason 
1670af31f5e5SChris Mason 	btrfs_set_backup_tree_root_level(root_backup,
1671af31f5e5SChris Mason 			       btrfs_header_level(info->tree_root->node));
1672af31f5e5SChris Mason 
1673af31f5e5SChris Mason 	btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start);
1674af31f5e5SChris Mason 	btrfs_set_backup_chunk_root_gen(root_backup,
1675af31f5e5SChris Mason 			       btrfs_header_generation(info->chunk_root->node));
1676af31f5e5SChris Mason 	btrfs_set_backup_chunk_root_level(root_backup,
1677af31f5e5SChris Mason 			       btrfs_header_level(info->chunk_root->node));
1678af31f5e5SChris Mason 
16791c56ab99SQu Wenruo 	if (!btrfs_fs_compat_ro(info, BLOCK_GROUP_TREE)) {
16809c54e80dSJosef Bacik 		struct btrfs_root *extent_root = btrfs_extent_root(info, 0);
1681f7238e50SJosef Bacik 		struct btrfs_root *csum_root = btrfs_csum_root(info, 0);
16829c54e80dSJosef Bacik 
16839c54e80dSJosef Bacik 		btrfs_set_backup_extent_root(root_backup,
16849c54e80dSJosef Bacik 					     extent_root->node->start);
1685af31f5e5SChris Mason 		btrfs_set_backup_extent_root_gen(root_backup,
168629cbcf40SJosef Bacik 				btrfs_header_generation(extent_root->node));
1687af31f5e5SChris Mason 		btrfs_set_backup_extent_root_level(root_backup,
168829cbcf40SJosef Bacik 					btrfs_header_level(extent_root->node));
1689af31f5e5SChris Mason 
1690f7238e50SJosef Bacik 		btrfs_set_backup_csum_root(root_backup, csum_root->node->start);
1691f7238e50SJosef Bacik 		btrfs_set_backup_csum_root_gen(root_backup,
1692f7238e50SJosef Bacik 					       btrfs_header_generation(csum_root->node));
1693f7238e50SJosef Bacik 		btrfs_set_backup_csum_root_level(root_backup,
1694f7238e50SJosef Bacik 						 btrfs_header_level(csum_root->node));
16959c54e80dSJosef Bacik 	}
1696af31f5e5SChris Mason 
16977c7e82a7SChris Mason 	/*
16987c7e82a7SChris Mason 	 * we might commit during log recovery, which happens before we set
16997c7e82a7SChris Mason 	 * the fs_root.  Make sure it is valid before we fill it in.
17007c7e82a7SChris Mason 	 */
17017c7e82a7SChris Mason 	if (info->fs_root && info->fs_root->node) {
17027c7e82a7SChris Mason 		btrfs_set_backup_fs_root(root_backup,
17037c7e82a7SChris Mason 					 info->fs_root->node->start);
1704af31f5e5SChris Mason 		btrfs_set_backup_fs_root_gen(root_backup,
1705af31f5e5SChris Mason 			       btrfs_header_generation(info->fs_root->node));
1706af31f5e5SChris Mason 		btrfs_set_backup_fs_root_level(root_backup,
1707af31f5e5SChris Mason 			       btrfs_header_level(info->fs_root->node));
17087c7e82a7SChris Mason 	}
1709af31f5e5SChris Mason 
1710af31f5e5SChris Mason 	btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start);
1711af31f5e5SChris Mason 	btrfs_set_backup_dev_root_gen(root_backup,
1712af31f5e5SChris Mason 			       btrfs_header_generation(info->dev_root->node));
1713af31f5e5SChris Mason 	btrfs_set_backup_dev_root_level(root_backup,
1714af31f5e5SChris Mason 				       btrfs_header_level(info->dev_root->node));
1715af31f5e5SChris Mason 
1716af31f5e5SChris Mason 	btrfs_set_backup_total_bytes(root_backup,
1717af31f5e5SChris Mason 			     btrfs_super_total_bytes(info->super_copy));
1718af31f5e5SChris Mason 	btrfs_set_backup_bytes_used(root_backup,
1719af31f5e5SChris Mason 			     btrfs_super_bytes_used(info->super_copy));
1720af31f5e5SChris Mason 	btrfs_set_backup_num_devices(root_backup,
1721af31f5e5SChris Mason 			     btrfs_super_num_devices(info->super_copy));
1722af31f5e5SChris Mason 
1723af31f5e5SChris Mason 	/*
1724af31f5e5SChris Mason 	 * if we don't copy this out to the super_copy, it won't get remembered
1725af31f5e5SChris Mason 	 * for the next commit
1726af31f5e5SChris Mason 	 */
1727af31f5e5SChris Mason 	memcpy(&info->super_copy->super_roots,
1728af31f5e5SChris Mason 	       &info->super_for_commit->super_roots,
1729af31f5e5SChris Mason 	       sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS);
1730af31f5e5SChris Mason }
1731af31f5e5SChris Mason 
1732af31f5e5SChris Mason /*
17339580503bSDavid Sterba  * Reads a backup root based on the passed priority. Prio 0 is the newest, prio
17349580503bSDavid Sterba  * 1/2/3 are 2nd newest/3rd newest/4th (oldest) backup roots
1735bd2336b2SNikolay Borisov  *
17369580503bSDavid Sterba  * @fs_info:  filesystem whose backup roots need to be read
17379580503bSDavid Sterba  * @priority: priority of backup root required
1738bd2336b2SNikolay Borisov  *
1739bd2336b2SNikolay Borisov  * Returns backup root index on success and -EINVAL otherwise.
1740bd2336b2SNikolay Borisov  */
read_backup_root(struct btrfs_fs_info * fs_info,u8 priority)1741bd2336b2SNikolay Borisov static int read_backup_root(struct btrfs_fs_info *fs_info, u8 priority)
1742bd2336b2SNikolay Borisov {
1743bd2336b2SNikolay Borisov 	int backup_index = find_newest_super_backup(fs_info);
1744bd2336b2SNikolay Borisov 	struct btrfs_super_block *super = fs_info->super_copy;
1745bd2336b2SNikolay Borisov 	struct btrfs_root_backup *root_backup;
1746bd2336b2SNikolay Borisov 
1747bd2336b2SNikolay Borisov 	if (priority < BTRFS_NUM_BACKUP_ROOTS && backup_index >= 0) {
1748bd2336b2SNikolay Borisov 		if (priority == 0)
1749bd2336b2SNikolay Borisov 			return backup_index;
1750bd2336b2SNikolay Borisov 
1751bd2336b2SNikolay Borisov 		backup_index = backup_index + BTRFS_NUM_BACKUP_ROOTS - priority;
1752bd2336b2SNikolay Borisov 		backup_index %= BTRFS_NUM_BACKUP_ROOTS;
1753bd2336b2SNikolay Borisov 	} else {
1754bd2336b2SNikolay Borisov 		return -EINVAL;
1755bd2336b2SNikolay Borisov 	}
1756bd2336b2SNikolay Borisov 
1757bd2336b2SNikolay Borisov 	root_backup = super->super_roots + backup_index;
1758bd2336b2SNikolay Borisov 
1759bd2336b2SNikolay Borisov 	btrfs_set_super_generation(super,
1760bd2336b2SNikolay Borisov 				   btrfs_backup_tree_root_gen(root_backup));
1761bd2336b2SNikolay Borisov 	btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup));
1762bd2336b2SNikolay Borisov 	btrfs_set_super_root_level(super,
1763bd2336b2SNikolay Borisov 				   btrfs_backup_tree_root_level(root_backup));
1764bd2336b2SNikolay Borisov 	btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup));
1765bd2336b2SNikolay Borisov 
1766bd2336b2SNikolay Borisov 	/*
1767bd2336b2SNikolay Borisov 	 * Fixme: the total bytes and num_devices need to match or we should
1768bd2336b2SNikolay Borisov 	 * need a fsck
1769bd2336b2SNikolay Borisov 	 */
1770bd2336b2SNikolay Borisov 	btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup));
1771bd2336b2SNikolay Borisov 	btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup));
1772bd2336b2SNikolay Borisov 
1773bd2336b2SNikolay Borisov 	return backup_index;
1774bd2336b2SNikolay Borisov }
1775bd2336b2SNikolay Borisov 
17767abadb64SLiu Bo /* helper to cleanup workers */
btrfs_stop_all_workers(struct btrfs_fs_info * fs_info)17777abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info)
17787abadb64SLiu Bo {
1779dc6e3209SQu Wenruo 	btrfs_destroy_workqueue(fs_info->fixup_workers);
1780afe3d242SQu Wenruo 	btrfs_destroy_workqueue(fs_info->delalloc_workers);
17815cdc7ad3SQu Wenruo 	btrfs_destroy_workqueue(fs_info->workers);
1782d7b9416fSChristoph Hellwig 	if (fs_info->endio_workers)
1783d7b9416fSChristoph Hellwig 		destroy_workqueue(fs_info->endio_workers);
1784385de0efSChristoph Hellwig 	if (fs_info->rmw_workers)
1785385de0efSChristoph Hellwig 		destroy_workqueue(fs_info->rmw_workers);
1786fed8a72dSChristoph Hellwig 	if (fs_info->compressed_write_workers)
1787fed8a72dSChristoph Hellwig 		destroy_workqueue(fs_info->compressed_write_workers);
1788fccb5d86SQu Wenruo 	btrfs_destroy_workqueue(fs_info->endio_write_workers);
1789fccb5d86SQu Wenruo 	btrfs_destroy_workqueue(fs_info->endio_freespace_worker);
17905b3bc44eSQu Wenruo 	btrfs_destroy_workqueue(fs_info->delayed_workers);
1791e66f0bb1SQu Wenruo 	btrfs_destroy_workqueue(fs_info->caching_workers);
1792a44903abSQu Wenruo 	btrfs_destroy_workqueue(fs_info->flush_workers);
1793fc97fab0SQu Wenruo 	btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers);
1794b0643e59SDennis Zhou 	if (fs_info->discard_ctl.discard_workers)
1795b0643e59SDennis Zhou 		destroy_workqueue(fs_info->discard_ctl.discard_workers);
1796a9b9477dSFilipe Manana 	/*
1797a9b9477dSFilipe Manana 	 * Now that all other work queues are destroyed, we can safely destroy
1798a9b9477dSFilipe Manana 	 * the queues used for metadata I/O, since tasks from those other work
1799a9b9477dSFilipe Manana 	 * queues can do metadata I/O operations.
1800a9b9477dSFilipe Manana 	 */
1801d7b9416fSChristoph Hellwig 	if (fs_info->endio_meta_workers)
1802d7b9416fSChristoph Hellwig 		destroy_workqueue(fs_info->endio_meta_workers);
18037abadb64SLiu Bo }
18047abadb64SLiu Bo 
free_root_extent_buffers(struct btrfs_root * root)18052e9f5954SRashika static void free_root_extent_buffers(struct btrfs_root *root)
18062e9f5954SRashika {
18072e9f5954SRashika 	if (root) {
18082e9f5954SRashika 		free_extent_buffer(root->node);
18092e9f5954SRashika 		free_extent_buffer(root->commit_root);
18102e9f5954SRashika 		root->node = NULL;
18112e9f5954SRashika 		root->commit_root = NULL;
18122e9f5954SRashika 	}
18132e9f5954SRashika }
18142e9f5954SRashika 
free_global_root_pointers(struct btrfs_fs_info * fs_info)1815abed4aaaSJosef Bacik static void free_global_root_pointers(struct btrfs_fs_info *fs_info)
1816abed4aaaSJosef Bacik {
1817abed4aaaSJosef Bacik 	struct btrfs_root *root, *tmp;
1818abed4aaaSJosef Bacik 
1819abed4aaaSJosef Bacik 	rbtree_postorder_for_each_entry_safe(root, tmp,
1820abed4aaaSJosef Bacik 					     &fs_info->global_root_tree,
1821abed4aaaSJosef Bacik 					     rb_node)
1822abed4aaaSJosef Bacik 		free_root_extent_buffers(root);
1823abed4aaaSJosef Bacik }
1824abed4aaaSJosef Bacik 
1825af31f5e5SChris Mason /* helper to cleanup tree roots */
free_root_pointers(struct btrfs_fs_info * info,bool free_chunk_root)18264273eaffSAnand Jain static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root)
1827af31f5e5SChris Mason {
18282e9f5954SRashika 	free_root_extent_buffers(info->tree_root);
1829655b09feSJosef Bacik 
1830abed4aaaSJosef Bacik 	free_global_root_pointers(info);
18312e9f5954SRashika 	free_root_extent_buffers(info->dev_root);
18322e9f5954SRashika 	free_root_extent_buffers(info->quota_root);
18332e9f5954SRashika 	free_root_extent_buffers(info->uuid_root);
18348c38938cSJosef Bacik 	free_root_extent_buffers(info->fs_root);
1835aeb935a4SQu Wenruo 	free_root_extent_buffers(info->data_reloc_root);
18369c54e80dSJosef Bacik 	free_root_extent_buffers(info->block_group_root);
183751502090SJohannes Thumshirn 	free_root_extent_buffers(info->stripe_root);
18384273eaffSAnand Jain 	if (free_chunk_root)
18392e9f5954SRashika 		free_root_extent_buffers(info->chunk_root);
1840af31f5e5SChris Mason }
1841af31f5e5SChris Mason 
btrfs_put_root(struct btrfs_root * root)18428c38938cSJosef Bacik void btrfs_put_root(struct btrfs_root *root)
18438c38938cSJosef Bacik {
18448c38938cSJosef Bacik 	if (!root)
18458c38938cSJosef Bacik 		return;
18468c38938cSJosef Bacik 
18478c38938cSJosef Bacik 	if (refcount_dec_and_test(&root->refs)) {
1848310b2f5dSFilipe Manana 		if (WARN_ON(!xa_empty(&root->inodes)))
1849310b2f5dSFilipe Manana 			xa_destroy(&root->inodes);
18501dae7e0eSQu Wenruo 		WARN_ON(test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state));
18518c38938cSJosef Bacik 		if (root->anon_dev)
18528c38938cSJosef Bacik 			free_anon_bdev(root->anon_dev);
1853923eb523SJohannes Thumshirn 		free_root_extent_buffers(root);
18548c38938cSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
1855fc7cbcd4SDavid Sterba 		spin_lock(&root->fs_info->fs_roots_radix_lock);
18568c38938cSJosef Bacik 		list_del_init(&root->leak_list);
1857fc7cbcd4SDavid Sterba 		spin_unlock(&root->fs_info->fs_roots_radix_lock);
18588c38938cSJosef Bacik #endif
18598c38938cSJosef Bacik 		kfree(root);
18608c38938cSJosef Bacik 	}
18618c38938cSJosef Bacik }
18628c38938cSJosef Bacik 
btrfs_free_fs_roots(struct btrfs_fs_info * fs_info)1863faa2dbf0SJosef Bacik void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info)
1864171f6537SJosef Bacik {
1865fc7cbcd4SDavid Sterba 	int ret;
1866fc7cbcd4SDavid Sterba 	struct btrfs_root *gang[8];
1867fc7cbcd4SDavid Sterba 	int i;
1868171f6537SJosef Bacik 
1869171f6537SJosef Bacik 	while (!list_empty(&fs_info->dead_roots)) {
1870fc7cbcd4SDavid Sterba 		gang[0] = list_entry(fs_info->dead_roots.next,
1871171f6537SJosef Bacik 				     struct btrfs_root, root_list);
1872fc7cbcd4SDavid Sterba 		list_del(&gang[0]->root_list);
1873171f6537SJosef Bacik 
1874fc7cbcd4SDavid Sterba 		if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state))
1875fc7cbcd4SDavid Sterba 			btrfs_drop_and_free_fs_root(fs_info, gang[0]);
1876fc7cbcd4SDavid Sterba 		btrfs_put_root(gang[0]);
1877171f6537SJosef Bacik 	}
1878171f6537SJosef Bacik 
1879fc7cbcd4SDavid Sterba 	while (1) {
1880fc7cbcd4SDavid Sterba 		ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
1881fc7cbcd4SDavid Sterba 					     (void **)gang, 0,
1882fc7cbcd4SDavid Sterba 					     ARRAY_SIZE(gang));
1883fc7cbcd4SDavid Sterba 		if (!ret)
1884fc7cbcd4SDavid Sterba 			break;
1885fc7cbcd4SDavid Sterba 		for (i = 0; i < ret; i++)
1886fc7cbcd4SDavid Sterba 			btrfs_drop_and_free_fs_root(fs_info, gang[i]);
1887171f6537SJosef Bacik 	}
1888171f6537SJosef Bacik }
1889af31f5e5SChris Mason 
btrfs_init_scrub(struct btrfs_fs_info * fs_info)1890638aa7edSEric Sandeen static void btrfs_init_scrub(struct btrfs_fs_info *fs_info)
1891638aa7edSEric Sandeen {
1892638aa7edSEric Sandeen 	mutex_init(&fs_info->scrub_lock);
1893638aa7edSEric Sandeen 	atomic_set(&fs_info->scrubs_running, 0);
1894638aa7edSEric Sandeen 	atomic_set(&fs_info->scrub_pause_req, 0);
1895638aa7edSEric Sandeen 	atomic_set(&fs_info->scrubs_paused, 0);
1896638aa7edSEric Sandeen 	atomic_set(&fs_info->scrub_cancel_req, 0);
1897638aa7edSEric Sandeen 	init_waitqueue_head(&fs_info->scrub_pause_wait);
1898ff09c4caSAnand Jain 	refcount_set(&fs_info->scrub_workers_refcnt, 0);
1899638aa7edSEric Sandeen }
1900638aa7edSEric Sandeen 
btrfs_init_balance(struct btrfs_fs_info * fs_info)1901779a65a4SEric Sandeen static void btrfs_init_balance(struct btrfs_fs_info *fs_info)
1902779a65a4SEric Sandeen {
1903779a65a4SEric Sandeen 	spin_lock_init(&fs_info->balance_lock);
1904779a65a4SEric Sandeen 	mutex_init(&fs_info->balance_mutex);
1905779a65a4SEric Sandeen 	atomic_set(&fs_info->balance_pause_req, 0);
1906779a65a4SEric Sandeen 	atomic_set(&fs_info->balance_cancel_req, 0);
1907779a65a4SEric Sandeen 	fs_info->balance_ctl = NULL;
1908779a65a4SEric Sandeen 	init_waitqueue_head(&fs_info->balance_wait_q);
1909907d2710SDavid Sterba 	atomic_set(&fs_info->reloc_cancel_req, 0);
1910779a65a4SEric Sandeen }
1911779a65a4SEric Sandeen 
btrfs_init_btree_inode(struct super_block * sb)1912dcb2137cSChristoph Hellwig static int btrfs_init_btree_inode(struct super_block *sb)
1913f37938e0SEric Sandeen {
1914dcb2137cSChristoph Hellwig 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1915e256927bSJosef Bacik 	unsigned long hash = btrfs_inode_hash(BTRFS_BTREE_INODE_OBJECTID,
1916e256927bSJosef Bacik 					      fs_info->tree_root);
1917dcb2137cSChristoph Hellwig 	struct inode *inode;
1918dcb2137cSChristoph Hellwig 
1919dcb2137cSChristoph Hellwig 	inode = new_inode(sb);
1920dcb2137cSChristoph Hellwig 	if (!inode)
1921dcb2137cSChristoph Hellwig 		return -ENOMEM;
19222ff7e61eSJeff Mahoney 
19237a7bc214SFilipe Manana 	btrfs_set_inode_number(BTRFS_I(inode), BTRFS_BTREE_INODE_OBJECTID);
19242ff7e61eSJeff Mahoney 	set_nlink(inode, 1);
1925f37938e0SEric Sandeen 	/*
1926f37938e0SEric Sandeen 	 * we set the i_size on the btree inode to the max possible int.
1927f37938e0SEric Sandeen 	 * the real end of the address space is determined by all of
1928f37938e0SEric Sandeen 	 * the devices in the system
1929f37938e0SEric Sandeen 	 */
19302ff7e61eSJeff Mahoney 	inode->i_size = OFFSET_MAX;
19312ff7e61eSJeff Mahoney 	inode->i_mapping->a_ops = &btree_aops;
1932dcb2137cSChristoph Hellwig 	mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
1933f37938e0SEric Sandeen 
193443eb5f29SQu Wenruo 	extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree,
193535da5a7eSDavid Sterba 			    IO_TREE_BTREE_INODE_IO);
19362ff7e61eSJeff Mahoney 	extent_map_tree_init(&BTRFS_I(inode)->extent_tree);
1937f37938e0SEric Sandeen 
19385c8fd99fSJosef Bacik 	BTRFS_I(inode)->root = btrfs_grab_root(fs_info->tree_root);
19392ff7e61eSJeff Mahoney 	set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
1940e256927bSJosef Bacik 	__insert_inode_hash(inode, hash);
1941dcb2137cSChristoph Hellwig 	fs_info->btree_inode = inode;
1942dcb2137cSChristoph Hellwig 
1943dcb2137cSChristoph Hellwig 	return 0;
1944f37938e0SEric Sandeen }
1945f37938e0SEric Sandeen 
btrfs_init_dev_replace_locks(struct btrfs_fs_info * fs_info)1946ad618368SEric Sandeen static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info)
1947ad618368SEric Sandeen {
1948ad618368SEric Sandeen 	mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount);
1949129827e3SDavid Sterba 	init_rwsem(&fs_info->dev_replace.rwsem);
19507f8d236aSDavid Sterba 	init_waitqueue_head(&fs_info->dev_replace.replace_wait);
1951ad618368SEric Sandeen }
1952ad618368SEric Sandeen 
btrfs_init_qgroup(struct btrfs_fs_info * fs_info)1953f9e92e40SEric Sandeen static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info)
1954f9e92e40SEric Sandeen {
1955f9e92e40SEric Sandeen 	spin_lock_init(&fs_info->qgroup_lock);
1956f9e92e40SEric Sandeen 	mutex_init(&fs_info->qgroup_ioctl_lock);
1957f9e92e40SEric Sandeen 	fs_info->qgroup_tree = RB_ROOT;
1958f9e92e40SEric Sandeen 	INIT_LIST_HEAD(&fs_info->dirty_qgroups);
1959f9e92e40SEric Sandeen 	fs_info->qgroup_seq = 1;
1960f9e92e40SEric Sandeen 	fs_info->qgroup_ulist = NULL;
1961d2c609b8SJeff Mahoney 	fs_info->qgroup_rescan_running = false;
1962011b46c3SQu Wenruo 	fs_info->qgroup_drop_subtree_thres = BTRFS_MAX_LEVEL;
1963f9e92e40SEric Sandeen 	mutex_init(&fs_info->qgroup_rescan_lock);
1964f9e92e40SEric Sandeen }
1965f9e92e40SEric Sandeen 
btrfs_init_workqueues(struct btrfs_fs_info * fs_info)1966d21deec5SSu Yue static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info)
19672a458198SEric Sandeen {
1968f7b885beSAnand Jain 	u32 max_active = fs_info->thread_pool_size;
19696f011058SDavid Sterba 	unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND;
197058e814fcSTejun Heo 	unsigned int ordered_flags = WQ_MEM_RECLAIM | WQ_FREEZABLE;
19712a458198SEric Sandeen 
19722a458198SEric Sandeen 	fs_info->workers =
1973a31b4a43SChristoph Hellwig 		btrfs_alloc_workqueue(fs_info, "worker", flags, max_active, 16);
19742a458198SEric Sandeen 
19752a458198SEric Sandeen 	fs_info->delalloc_workers =
1976cb001095SJeff Mahoney 		btrfs_alloc_workqueue(fs_info, "delalloc",
1977cb001095SJeff Mahoney 				      flags, max_active, 2);
19782a458198SEric Sandeen 
19792a458198SEric Sandeen 	fs_info->flush_workers =
1980cb001095SJeff Mahoney 		btrfs_alloc_workqueue(fs_info, "flush_delalloc",
1981cb001095SJeff Mahoney 				      flags, max_active, 0);
19822a458198SEric Sandeen 
19832a458198SEric Sandeen 	fs_info->caching_workers =
1984cb001095SJeff Mahoney 		btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0);
19852a458198SEric Sandeen 
19862a458198SEric Sandeen 	fs_info->fixup_workers =
198758e814fcSTejun Heo 		btrfs_alloc_ordered_workqueue(fs_info, "fixup", ordered_flags);
19882a458198SEric Sandeen 
19892a458198SEric Sandeen 	fs_info->endio_workers =
1990d7b9416fSChristoph Hellwig 		alloc_workqueue("btrfs-endio", flags, max_active);
19912a458198SEric Sandeen 	fs_info->endio_meta_workers =
1992d7b9416fSChristoph Hellwig 		alloc_workqueue("btrfs-endio-meta", flags, max_active);
1993385de0efSChristoph Hellwig 	fs_info->rmw_workers = alloc_workqueue("btrfs-rmw", flags, max_active);
19942a458198SEric Sandeen 	fs_info->endio_write_workers =
1995cb001095SJeff Mahoney 		btrfs_alloc_workqueue(fs_info, "endio-write", flags,
1996cb001095SJeff Mahoney 				      max_active, 2);
1997fed8a72dSChristoph Hellwig 	fs_info->compressed_write_workers =
1998fed8a72dSChristoph Hellwig 		alloc_workqueue("btrfs-compressed-write", flags, max_active);
19992a458198SEric Sandeen 	fs_info->endio_freespace_worker =
2000cb001095SJeff Mahoney 		btrfs_alloc_workqueue(fs_info, "freespace-write", flags,
2001cb001095SJeff Mahoney 				      max_active, 0);
20022a458198SEric Sandeen 	fs_info->delayed_workers =
2003cb001095SJeff Mahoney 		btrfs_alloc_workqueue(fs_info, "delayed-meta", flags,
2004cb001095SJeff Mahoney 				      max_active, 0);
20052a458198SEric Sandeen 	fs_info->qgroup_rescan_workers =
200658e814fcSTejun Heo 		btrfs_alloc_ordered_workqueue(fs_info, "qgroup-rescan",
200758e814fcSTejun Heo 					      ordered_flags);
2008b0643e59SDennis Zhou 	fs_info->discard_ctl.discard_workers =
200958e814fcSTejun Heo 		alloc_ordered_workqueue("btrfs_discard", WQ_FREEZABLE);
20102a458198SEric Sandeen 
20118bfec2e4SChristoph Hellwig 	if (!(fs_info->workers &&
2012a31b4a43SChristoph Hellwig 	      fs_info->delalloc_workers && fs_info->flush_workers &&
20132a458198SEric Sandeen 	      fs_info->endio_workers && fs_info->endio_meta_workers &&
2014fed8a72dSChristoph Hellwig 	      fs_info->compressed_write_workers &&
20151a1a2851SQu Wenruo 	      fs_info->endio_write_workers &&
20162a458198SEric Sandeen 	      fs_info->endio_freespace_worker && fs_info->rmw_workers &&
2017f26c9238SQu Wenruo 	      fs_info->caching_workers && fs_info->fixup_workers &&
2018f26c9238SQu Wenruo 	      fs_info->delayed_workers && fs_info->qgroup_rescan_workers &&
2019b0643e59SDennis Zhou 	      fs_info->discard_ctl.discard_workers)) {
20202a458198SEric Sandeen 		return -ENOMEM;
20212a458198SEric Sandeen 	}
20222a458198SEric Sandeen 
20232a458198SEric Sandeen 	return 0;
20242a458198SEric Sandeen }
20252a458198SEric Sandeen 
btrfs_init_csum_hash(struct btrfs_fs_info * fs_info,u16 csum_type)20266d97c6e3SJohannes Thumshirn static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type)
20276d97c6e3SJohannes Thumshirn {
20286d97c6e3SJohannes Thumshirn 	struct crypto_shash *csum_shash;
2029b4e967beSDavid Sterba 	const char *csum_driver = btrfs_super_csum_driver(csum_type);
20306d97c6e3SJohannes Thumshirn 
2031b4e967beSDavid Sterba 	csum_shash = crypto_alloc_shash(csum_driver, 0, 0);
20326d97c6e3SJohannes Thumshirn 
20336d97c6e3SJohannes Thumshirn 	if (IS_ERR(csum_shash)) {
20346d97c6e3SJohannes Thumshirn 		btrfs_err(fs_info, "error allocating %s hash for checksum",
2035b4e967beSDavid Sterba 			  csum_driver);
20366d97c6e3SJohannes Thumshirn 		return PTR_ERR(csum_shash);
20376d97c6e3SJohannes Thumshirn 	}
20386d97c6e3SJohannes Thumshirn 
20396d97c6e3SJohannes Thumshirn 	fs_info->csum_shash = csum_shash;
20406d97c6e3SJohannes Thumshirn 
204168d99ab0SChristoph Hellwig 	/*
204268d99ab0SChristoph Hellwig 	 * Check if the checksum implementation is a fast accelerated one.
204368d99ab0SChristoph Hellwig 	 * As-is this is a bit of a hack and should be replaced once the csum
204468d99ab0SChristoph Hellwig 	 * implementations provide that information themselves.
204568d99ab0SChristoph Hellwig 	 */
204668d99ab0SChristoph Hellwig 	switch (csum_type) {
204768d99ab0SChristoph Hellwig 	case BTRFS_CSUM_TYPE_CRC32:
204868d99ab0SChristoph Hellwig 		if (!strstr(crypto_shash_driver_name(csum_shash), "generic"))
204968d99ab0SChristoph Hellwig 			set_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags);
205068d99ab0SChristoph Hellwig 		break;
2051efcfcbc6SDavid Sterba 	case BTRFS_CSUM_TYPE_XXHASH:
2052efcfcbc6SDavid Sterba 		set_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags);
2053efcfcbc6SDavid Sterba 		break;
205468d99ab0SChristoph Hellwig 	default:
205568d99ab0SChristoph Hellwig 		break;
205668d99ab0SChristoph Hellwig 	}
205768d99ab0SChristoph Hellwig 
2058c8a5f8caSDavid Sterba 	btrfs_info(fs_info, "using %s (%s) checksum algorithm",
2059c8a5f8caSDavid Sterba 			btrfs_super_csum_name(csum_type),
2060c8a5f8caSDavid Sterba 			crypto_shash_driver_name(csum_shash));
20616d97c6e3SJohannes Thumshirn 	return 0;
20626d97c6e3SJohannes Thumshirn }
20636d97c6e3SJohannes Thumshirn 
btrfs_replay_log(struct btrfs_fs_info * fs_info,struct btrfs_fs_devices * fs_devices)206463443bf5SEric Sandeen static int btrfs_replay_log(struct btrfs_fs_info *fs_info,
206563443bf5SEric Sandeen 			    struct btrfs_fs_devices *fs_devices)
206663443bf5SEric Sandeen {
206763443bf5SEric Sandeen 	int ret;
2068789d6a3aSQu Wenruo 	struct btrfs_tree_parent_check check = { 0 };
206963443bf5SEric Sandeen 	struct btrfs_root *log_tree_root;
207063443bf5SEric Sandeen 	struct btrfs_super_block *disk_super = fs_info->super_copy;
207163443bf5SEric Sandeen 	u64 bytenr = btrfs_super_log_root(disk_super);
2072581c1760SQu Wenruo 	int level = btrfs_super_log_root_level(disk_super);
207363443bf5SEric Sandeen 
207463443bf5SEric Sandeen 	if (fs_devices->rw_devices == 0) {
2075f14d104dSDavid Sterba 		btrfs_warn(fs_info, "log replay required on RO media");
207663443bf5SEric Sandeen 		return -EIO;
207763443bf5SEric Sandeen 	}
207863443bf5SEric Sandeen 
207996dfcb46SJosef Bacik 	log_tree_root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID,
208096dfcb46SJosef Bacik 					 GFP_KERNEL);
208163443bf5SEric Sandeen 	if (!log_tree_root)
208263443bf5SEric Sandeen 		return -ENOMEM;
208363443bf5SEric Sandeen 
2084789d6a3aSQu Wenruo 	check.level = level;
2085789d6a3aSQu Wenruo 	check.transid = fs_info->generation + 1;
2086789d6a3aSQu Wenruo 	check.owner_root = BTRFS_TREE_LOG_OBJECTID;
2087789d6a3aSQu Wenruo 	log_tree_root->node = read_tree_block(fs_info, bytenr, &check);
208864c043deSLiu Bo 	if (IS_ERR(log_tree_root->node)) {
2089f14d104dSDavid Sterba 		btrfs_warn(fs_info, "failed to read log tree");
20900eeff236SLiu Bo 		ret = PTR_ERR(log_tree_root->node);
20918c38938cSJosef Bacik 		log_tree_root->node = NULL;
209200246528SJosef Bacik 		btrfs_put_root(log_tree_root);
20930eeff236SLiu Bo 		return ret;
20944eb150d6SQu Wenruo 	}
20954eb150d6SQu Wenruo 	if (!extent_buffer_uptodate(log_tree_root->node)) {
2096f14d104dSDavid Sterba 		btrfs_err(fs_info, "failed to read log tree");
209700246528SJosef Bacik 		btrfs_put_root(log_tree_root);
209863443bf5SEric Sandeen 		return -EIO;
209963443bf5SEric Sandeen 	}
21004eb150d6SQu Wenruo 
210163443bf5SEric Sandeen 	/* returns with log_tree_root freed on success */
210263443bf5SEric Sandeen 	ret = btrfs_recover_log_trees(log_tree_root);
210363443bf5SEric Sandeen 	if (ret) {
21040b246afaSJeff Mahoney 		btrfs_handle_fs_error(fs_info, ret,
210563443bf5SEric Sandeen 				      "Failed to recover log tree");
210600246528SJosef Bacik 		btrfs_put_root(log_tree_root);
210763443bf5SEric Sandeen 		return ret;
210863443bf5SEric Sandeen 	}
210963443bf5SEric Sandeen 
2110bc98a42cSDavid Howells 	if (sb_rdonly(fs_info->sb)) {
21116bccf3abSJeff Mahoney 		ret = btrfs_commit_super(fs_info);
211263443bf5SEric Sandeen 		if (ret)
211363443bf5SEric Sandeen 			return ret;
211463443bf5SEric Sandeen 	}
211563443bf5SEric Sandeen 
211663443bf5SEric Sandeen 	return 0;
211763443bf5SEric Sandeen }
211863443bf5SEric Sandeen 
load_global_roots_objectid(struct btrfs_root * tree_root,struct btrfs_path * path,u64 objectid,const char * name)2119abed4aaaSJosef Bacik static int load_global_roots_objectid(struct btrfs_root *tree_root,
2120abed4aaaSJosef Bacik 				      struct btrfs_path *path, u64 objectid,
2121abed4aaaSJosef Bacik 				      const char *name)
2122abed4aaaSJosef Bacik {
2123abed4aaaSJosef Bacik 	struct btrfs_fs_info *fs_info = tree_root->fs_info;
2124abed4aaaSJosef Bacik 	struct btrfs_root *root;
2125f7238e50SJosef Bacik 	u64 max_global_id = 0;
2126abed4aaaSJosef Bacik 	int ret;
2127abed4aaaSJosef Bacik 	struct btrfs_key key = {
2128abed4aaaSJosef Bacik 		.objectid = objectid,
2129abed4aaaSJosef Bacik 		.type = BTRFS_ROOT_ITEM_KEY,
2130abed4aaaSJosef Bacik 		.offset = 0,
2131abed4aaaSJosef Bacik 	};
2132abed4aaaSJosef Bacik 	bool found = false;
2133abed4aaaSJosef Bacik 
2134abed4aaaSJosef Bacik 	/* If we have IGNOREDATACSUMS skip loading these roots. */
2135abed4aaaSJosef Bacik 	if (objectid == BTRFS_CSUM_TREE_OBJECTID &&
2136abed4aaaSJosef Bacik 	    btrfs_test_opt(fs_info, IGNOREDATACSUMS)) {
2137169aaaf2SQu Wenruo 		set_bit(BTRFS_FS_STATE_NO_DATA_CSUMS, &fs_info->fs_state);
2138abed4aaaSJosef Bacik 		return 0;
2139abed4aaaSJosef Bacik 	}
2140abed4aaaSJosef Bacik 
2141abed4aaaSJosef Bacik 	while (1) {
2142abed4aaaSJosef Bacik 		ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
2143abed4aaaSJosef Bacik 		if (ret < 0)
2144abed4aaaSJosef Bacik 			break;
2145abed4aaaSJosef Bacik 
2146abed4aaaSJosef Bacik 		if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2147abed4aaaSJosef Bacik 			ret = btrfs_next_leaf(tree_root, path);
2148abed4aaaSJosef Bacik 			if (ret) {
2149abed4aaaSJosef Bacik 				if (ret > 0)
2150abed4aaaSJosef Bacik 					ret = 0;
2151abed4aaaSJosef Bacik 				break;
2152abed4aaaSJosef Bacik 			}
2153abed4aaaSJosef Bacik 		}
2154abed4aaaSJosef Bacik 		ret = 0;
2155abed4aaaSJosef Bacik 
2156abed4aaaSJosef Bacik 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2157abed4aaaSJosef Bacik 		if (key.objectid != objectid)
2158abed4aaaSJosef Bacik 			break;
2159abed4aaaSJosef Bacik 		btrfs_release_path(path);
2160abed4aaaSJosef Bacik 
2161f7238e50SJosef Bacik 		/*
2162f7238e50SJosef Bacik 		 * Just worry about this for extent tree, it'll be the same for
2163f7238e50SJosef Bacik 		 * everybody.
2164f7238e50SJosef Bacik 		 */
2165f7238e50SJosef Bacik 		if (objectid == BTRFS_EXTENT_TREE_OBJECTID)
2166f7238e50SJosef Bacik 			max_global_id = max(max_global_id, key.offset);
2167f7238e50SJosef Bacik 
2168abed4aaaSJosef Bacik 		found = true;
2169abed4aaaSJosef Bacik 		root = read_tree_root_path(tree_root, path, &key);
2170abed4aaaSJosef Bacik 		if (IS_ERR(root)) {
2171abed4aaaSJosef Bacik 			if (!btrfs_test_opt(fs_info, IGNOREBADROOTS))
2172abed4aaaSJosef Bacik 				ret = PTR_ERR(root);
2173abed4aaaSJosef Bacik 			break;
2174abed4aaaSJosef Bacik 		}
2175abed4aaaSJosef Bacik 		set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2176abed4aaaSJosef Bacik 		ret = btrfs_global_root_insert(root);
2177abed4aaaSJosef Bacik 		if (ret) {
2178abed4aaaSJosef Bacik 			btrfs_put_root(root);
2179abed4aaaSJosef Bacik 			break;
2180abed4aaaSJosef Bacik 		}
2181abed4aaaSJosef Bacik 		key.offset++;
2182abed4aaaSJosef Bacik 	}
2183abed4aaaSJosef Bacik 	btrfs_release_path(path);
2184abed4aaaSJosef Bacik 
2185f7238e50SJosef Bacik 	if (objectid == BTRFS_EXTENT_TREE_OBJECTID)
2186f7238e50SJosef Bacik 		fs_info->nr_global_roots = max_global_id + 1;
2187f7238e50SJosef Bacik 
2188abed4aaaSJosef Bacik 	if (!found || ret) {
2189abed4aaaSJosef Bacik 		if (objectid == BTRFS_CSUM_TREE_OBJECTID)
2190169aaaf2SQu Wenruo 			set_bit(BTRFS_FS_STATE_NO_DATA_CSUMS, &fs_info->fs_state);
2191abed4aaaSJosef Bacik 
2192abed4aaaSJosef Bacik 		if (!btrfs_test_opt(fs_info, IGNOREBADROOTS))
2193abed4aaaSJosef Bacik 			ret = ret ? ret : -ENOENT;
2194abed4aaaSJosef Bacik 		else
2195abed4aaaSJosef Bacik 			ret = 0;
2196abed4aaaSJosef Bacik 		btrfs_err(fs_info, "failed to load root %s", name);
2197abed4aaaSJosef Bacik 	}
2198abed4aaaSJosef Bacik 	return ret;
2199abed4aaaSJosef Bacik }
2200abed4aaaSJosef Bacik 
load_global_roots(struct btrfs_root * tree_root)2201abed4aaaSJosef Bacik static int load_global_roots(struct btrfs_root *tree_root)
2202abed4aaaSJosef Bacik {
2203abed4aaaSJosef Bacik 	struct btrfs_path *path;
2204abed4aaaSJosef Bacik 	int ret = 0;
2205abed4aaaSJosef Bacik 
2206abed4aaaSJosef Bacik 	path = btrfs_alloc_path();
2207abed4aaaSJosef Bacik 	if (!path)
2208abed4aaaSJosef Bacik 		return -ENOMEM;
2209abed4aaaSJosef Bacik 
2210abed4aaaSJosef Bacik 	ret = load_global_roots_objectid(tree_root, path,
2211abed4aaaSJosef Bacik 					 BTRFS_EXTENT_TREE_OBJECTID, "extent");
2212abed4aaaSJosef Bacik 	if (ret)
2213abed4aaaSJosef Bacik 		goto out;
2214abed4aaaSJosef Bacik 	ret = load_global_roots_objectid(tree_root, path,
2215abed4aaaSJosef Bacik 					 BTRFS_CSUM_TREE_OBJECTID, "csum");
2216abed4aaaSJosef Bacik 	if (ret)
2217abed4aaaSJosef Bacik 		goto out;
2218abed4aaaSJosef Bacik 	if (!btrfs_fs_compat_ro(tree_root->fs_info, FREE_SPACE_TREE))
2219abed4aaaSJosef Bacik 		goto out;
2220abed4aaaSJosef Bacik 	ret = load_global_roots_objectid(tree_root, path,
2221abed4aaaSJosef Bacik 					 BTRFS_FREE_SPACE_TREE_OBJECTID,
2222abed4aaaSJosef Bacik 					 "free space");
2223abed4aaaSJosef Bacik out:
2224abed4aaaSJosef Bacik 	btrfs_free_path(path);
2225abed4aaaSJosef Bacik 	return ret;
2226abed4aaaSJosef Bacik }
2227abed4aaaSJosef Bacik 
btrfs_read_roots(struct btrfs_fs_info * fs_info)22286bccf3abSJeff Mahoney static int btrfs_read_roots(struct btrfs_fs_info *fs_info)
22294bbcaa64SEric Sandeen {
22306bccf3abSJeff Mahoney 	struct btrfs_root *tree_root = fs_info->tree_root;
2231a4f3d2c4SDavid Sterba 	struct btrfs_root *root;
22324bbcaa64SEric Sandeen 	struct btrfs_key location;
22334bbcaa64SEric Sandeen 	int ret;
22344bbcaa64SEric Sandeen 
22352467d0feSDavid Sterba 	ASSERT(fs_info->tree_root);
22366bccf3abSJeff Mahoney 
2237abed4aaaSJosef Bacik 	ret = load_global_roots(tree_root);
2238abed4aaaSJosef Bacik 	if (ret)
2239abed4aaaSJosef Bacik 		return ret;
2240abed4aaaSJosef Bacik 
22414bbcaa64SEric Sandeen 	location.type = BTRFS_ROOT_ITEM_KEY;
22424bbcaa64SEric Sandeen 	location.offset = 0;
22434bbcaa64SEric Sandeen 
22441c56ab99SQu Wenruo 	if (btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE)) {
224514033b08SQu Wenruo 		location.objectid = BTRFS_BLOCK_GROUP_TREE_OBJECTID;
224614033b08SQu Wenruo 		root = btrfs_read_tree_root(tree_root, &location);
224714033b08SQu Wenruo 		if (IS_ERR(root)) {
224814033b08SQu Wenruo 			if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
224914033b08SQu Wenruo 				ret = PTR_ERR(root);
225014033b08SQu Wenruo 				goto out;
225114033b08SQu Wenruo 			}
225214033b08SQu Wenruo 		} else {
225314033b08SQu Wenruo 			set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
225414033b08SQu Wenruo 			fs_info->block_group_root = root;
225514033b08SQu Wenruo 		}
225614033b08SQu Wenruo 	}
225714033b08SQu Wenruo 
225814033b08SQu Wenruo 	location.objectid = BTRFS_DEV_TREE_OBJECTID;
2259a4f3d2c4SDavid Sterba 	root = btrfs_read_tree_root(tree_root, &location);
2260f50f4353SLiu Bo 	if (IS_ERR(root)) {
226142437a63SJosef Bacik 		if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
2262f50f4353SLiu Bo 			ret = PTR_ERR(root);
2263f50f4353SLiu Bo 			goto out;
2264f50f4353SLiu Bo 		}
226542437a63SJosef Bacik 	} else {
2266a4f3d2c4SDavid Sterba 		set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2267a4f3d2c4SDavid Sterba 		fs_info->dev_root = root;
226842437a63SJosef Bacik 	}
2269820a49daSJosef Bacik 	/* Initialize fs_info for all devices in any case */
2270a8d1b164SJohannes Thumshirn 	ret = btrfs_init_devices_late(fs_info);
2271a8d1b164SJohannes Thumshirn 	if (ret)
2272a8d1b164SJohannes Thumshirn 		goto out;
22734bbcaa64SEric Sandeen 
2274aeb935a4SQu Wenruo 	/*
2275aeb935a4SQu Wenruo 	 * This tree can share blocks with some other fs tree during relocation
2276aeb935a4SQu Wenruo 	 * and we need a proper setup by btrfs_get_fs_root
2277aeb935a4SQu Wenruo 	 */
227856e9357aSDavid Sterba 	root = btrfs_get_fs_root(tree_root->fs_info,
227956e9357aSDavid Sterba 				 BTRFS_DATA_RELOC_TREE_OBJECTID, true);
2280aeb935a4SQu Wenruo 	if (IS_ERR(root)) {
228142437a63SJosef Bacik 		if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
2282aeb935a4SQu Wenruo 			ret = PTR_ERR(root);
2283aeb935a4SQu Wenruo 			goto out;
2284aeb935a4SQu Wenruo 		}
228542437a63SJosef Bacik 	} else {
2286aeb935a4SQu Wenruo 		set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2287aeb935a4SQu Wenruo 		fs_info->data_reloc_root = root;
228842437a63SJosef Bacik 	}
2289aeb935a4SQu Wenruo 
22904bbcaa64SEric Sandeen 	location.objectid = BTRFS_QUOTA_TREE_OBJECTID;
2291a4f3d2c4SDavid Sterba 	root = btrfs_read_tree_root(tree_root, &location);
2292a4f3d2c4SDavid Sterba 	if (!IS_ERR(root)) {
2293a4f3d2c4SDavid Sterba 		set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2294a4f3d2c4SDavid Sterba 		fs_info->quota_root = root;
22954bbcaa64SEric Sandeen 	}
22964bbcaa64SEric Sandeen 
22974bbcaa64SEric Sandeen 	location.objectid = BTRFS_UUID_TREE_OBJECTID;
2298a4f3d2c4SDavid Sterba 	root = btrfs_read_tree_root(tree_root, &location);
2299a4f3d2c4SDavid Sterba 	if (IS_ERR(root)) {
230042437a63SJosef Bacik 		if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
2301a4f3d2c4SDavid Sterba 			ret = PTR_ERR(root);
23024bbcaa64SEric Sandeen 			if (ret != -ENOENT)
2303f50f4353SLiu Bo 				goto out;
230442437a63SJosef Bacik 		}
23054bbcaa64SEric Sandeen 	} else {
2306a4f3d2c4SDavid Sterba 		set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2307a4f3d2c4SDavid Sterba 		fs_info->uuid_root = root;
23084bbcaa64SEric Sandeen 	}
23094bbcaa64SEric Sandeen 
231051502090SJohannes Thumshirn 	if (btrfs_fs_incompat(fs_info, RAID_STRIPE_TREE)) {
231151502090SJohannes Thumshirn 		location.objectid = BTRFS_RAID_STRIPE_TREE_OBJECTID;
231251502090SJohannes Thumshirn 		root = btrfs_read_tree_root(tree_root, &location);
231351502090SJohannes Thumshirn 		if (IS_ERR(root)) {
231451502090SJohannes Thumshirn 			if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
231551502090SJohannes Thumshirn 				ret = PTR_ERR(root);
231651502090SJohannes Thumshirn 				goto out;
231751502090SJohannes Thumshirn 			}
231851502090SJohannes Thumshirn 		} else {
231951502090SJohannes Thumshirn 			set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
232051502090SJohannes Thumshirn 			fs_info->stripe_root = root;
232151502090SJohannes Thumshirn 		}
232251502090SJohannes Thumshirn 	}
232351502090SJohannes Thumshirn 
23244bbcaa64SEric Sandeen 	return 0;
2325f50f4353SLiu Bo out:
2326f50f4353SLiu Bo 	btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d",
2327f50f4353SLiu Bo 		   location.objectid, ret);
2328f50f4353SLiu Bo 	return ret;
23294bbcaa64SEric Sandeen }
23304bbcaa64SEric Sandeen 
2331069ec957SQu Wenruo /*
2332069ec957SQu Wenruo  * Real super block validation
2333069ec957SQu Wenruo  * NOTE: super csum type and incompat features will not be checked here.
2334069ec957SQu Wenruo  *
2335069ec957SQu Wenruo  * @sb:		super block to check
2336069ec957SQu Wenruo  * @mirror_num:	the super block number to check its bytenr:
2337069ec957SQu Wenruo  * 		0	the primary (1st) sb
2338069ec957SQu Wenruo  * 		1, 2	2nd and 3rd backup copy
2339069ec957SQu Wenruo  * 	       -1	skip bytenr check
2340069ec957SQu Wenruo  */
btrfs_validate_super(const struct btrfs_fs_info * fs_info,const struct btrfs_super_block * sb,int mirror_num)23412917f741SDavid Sterba int btrfs_validate_super(const struct btrfs_fs_info *fs_info,
23422917f741SDavid Sterba 			 const struct btrfs_super_block *sb, int mirror_num)
234321a852b0SQu Wenruo {
234421a852b0SQu Wenruo 	u64 nodesize = btrfs_super_nodesize(sb);
234521a852b0SQu Wenruo 	u64 sectorsize = btrfs_super_sectorsize(sb);
234621a852b0SQu Wenruo 	int ret = 0;
234732e62165SQu Wenruo 	const bool ignore_flags = btrfs_test_opt(fs_info, IGNORESUPERFLAGS);
234821a852b0SQu Wenruo 
234921a852b0SQu Wenruo 	if (btrfs_super_magic(sb) != BTRFS_MAGIC) {
235021a852b0SQu Wenruo 		btrfs_err(fs_info, "no valid FS found");
235121a852b0SQu Wenruo 		ret = -EINVAL;
235221a852b0SQu Wenruo 	}
235332e62165SQu Wenruo 	if ((btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP)) {
235432e62165SQu Wenruo 		if (!ignore_flags) {
235532e62165SQu Wenruo 			btrfs_err(fs_info,
235632e62165SQu Wenruo 			"unrecognized or unsupported super flag 0x%llx",
235721a852b0SQu Wenruo 				  btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP);
235821a852b0SQu Wenruo 			ret = -EINVAL;
235932e62165SQu Wenruo 		} else {
236032e62165SQu Wenruo 			btrfs_info(fs_info,
236132e62165SQu Wenruo 			"unrecognized or unsupported super flags: 0x%llx, ignored",
236232e62165SQu Wenruo 				   btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP);
236332e62165SQu Wenruo 		}
236421a852b0SQu Wenruo 	}
236521a852b0SQu Wenruo 	if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) {
236621a852b0SQu Wenruo 		btrfs_err(fs_info, "tree_root level too big: %d >= %d",
236721a852b0SQu Wenruo 				btrfs_super_root_level(sb), BTRFS_MAX_LEVEL);
236821a852b0SQu Wenruo 		ret = -EINVAL;
236921a852b0SQu Wenruo 	}
237021a852b0SQu Wenruo 	if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) {
237121a852b0SQu Wenruo 		btrfs_err(fs_info, "chunk_root level too big: %d >= %d",
237221a852b0SQu Wenruo 				btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL);
237321a852b0SQu Wenruo 		ret = -EINVAL;
237421a852b0SQu Wenruo 	}
237521a852b0SQu Wenruo 	if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) {
237621a852b0SQu Wenruo 		btrfs_err(fs_info, "log_root level too big: %d >= %d",
237721a852b0SQu Wenruo 				btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL);
237821a852b0SQu Wenruo 		ret = -EINVAL;
237921a852b0SQu Wenruo 	}
238021a852b0SQu Wenruo 
238121a852b0SQu Wenruo 	/*
238221a852b0SQu Wenruo 	 * Check sectorsize and nodesize first, other check will need it.
238321a852b0SQu Wenruo 	 * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here.
238421a852b0SQu Wenruo 	 */
238521a852b0SQu Wenruo 	if (!is_power_of_2(sectorsize) || sectorsize < 4096 ||
238621a852b0SQu Wenruo 	    sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) {
238721a852b0SQu Wenruo 		btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize);
238821a852b0SQu Wenruo 		ret = -EINVAL;
238921a852b0SQu Wenruo 	}
23900bb3eb3eSQu Wenruo 
23910bb3eb3eSQu Wenruo 	/*
23921a42daabSQu Wenruo 	 * We only support at most two sectorsizes: 4K and PAGE_SIZE.
23931a42daabSQu Wenruo 	 *
23941a42daabSQu Wenruo 	 * We can support 16K sectorsize with 64K page size without problem,
23951a42daabSQu Wenruo 	 * but such sectorsize/pagesize combination doesn't make much sense.
23961a42daabSQu Wenruo 	 * 4K will be our future standard, PAGE_SIZE is supported from the very
23971a42daabSQu Wenruo 	 * beginning.
23980bb3eb3eSQu Wenruo 	 */
23991a42daabSQu Wenruo 	if (sectorsize > PAGE_SIZE || (sectorsize != SZ_4K && sectorsize != PAGE_SIZE)) {
240021a852b0SQu Wenruo 		btrfs_err(fs_info,
24010bb3eb3eSQu Wenruo 			"sectorsize %llu not yet supported for page size %lu",
240221a852b0SQu Wenruo 			sectorsize, PAGE_SIZE);
240321a852b0SQu Wenruo 		ret = -EINVAL;
240421a852b0SQu Wenruo 	}
24050bb3eb3eSQu Wenruo 
240621a852b0SQu Wenruo 	if (!is_power_of_2(nodesize) || nodesize < sectorsize ||
240721a852b0SQu Wenruo 	    nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) {
240821a852b0SQu Wenruo 		btrfs_err(fs_info, "invalid nodesize %llu", nodesize);
240921a852b0SQu Wenruo 		ret = -EINVAL;
241021a852b0SQu Wenruo 	}
241121a852b0SQu Wenruo 	if (nodesize != le32_to_cpu(sb->__unused_leafsize)) {
241221a852b0SQu Wenruo 		btrfs_err(fs_info, "invalid leafsize %u, should be %llu",
241321a852b0SQu Wenruo 			  le32_to_cpu(sb->__unused_leafsize), nodesize);
241421a852b0SQu Wenruo 		ret = -EINVAL;
241521a852b0SQu Wenruo 	}
241621a852b0SQu Wenruo 
241721a852b0SQu Wenruo 	/* Root alignment check */
241821a852b0SQu Wenruo 	if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) {
241921a852b0SQu Wenruo 		btrfs_warn(fs_info, "tree_root block unaligned: %llu",
242021a852b0SQu Wenruo 			   btrfs_super_root(sb));
242121a852b0SQu Wenruo 		ret = -EINVAL;
242221a852b0SQu Wenruo 	}
242321a852b0SQu Wenruo 	if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) {
242421a852b0SQu Wenruo 		btrfs_warn(fs_info, "chunk_root block unaligned: %llu",
242521a852b0SQu Wenruo 			   btrfs_super_chunk_root(sb));
242621a852b0SQu Wenruo 		ret = -EINVAL;
242721a852b0SQu Wenruo 	}
242821a852b0SQu Wenruo 	if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) {
242921a852b0SQu Wenruo 		btrfs_warn(fs_info, "log_root block unaligned: %llu",
243021a852b0SQu Wenruo 			   btrfs_super_log_root(sb));
243121a852b0SQu Wenruo 		ret = -EINVAL;
243221a852b0SQu Wenruo 	}
243321a852b0SQu Wenruo 
2434a5b8a5f9SAnand Jain 	if (!fs_info->fs_devices->temp_fsid &&
2435a5b8a5f9SAnand Jain 	    memcmp(fs_info->fs_devices->fsid, sb->fsid, BTRFS_FSID_SIZE) != 0) {
2436aefd7f70SNikolay Borisov 		btrfs_err(fs_info,
2437aefd7f70SNikolay Borisov 		"superblock fsid doesn't match fsid of fs_devices: %pU != %pU",
2438d167aa76SAnand Jain 			  sb->fsid, fs_info->fs_devices->fsid);
2439aefd7f70SNikolay Borisov 		ret = -EINVAL;
2440aefd7f70SNikolay Borisov 	}
2441aefd7f70SNikolay Borisov 
24426bfe3959SAnand Jain 	if (memcmp(fs_info->fs_devices->metadata_uuid, btrfs_sb_fsid_ptr(sb),
24436bfe3959SAnand Jain 		   BTRFS_FSID_SIZE) != 0) {
2444aefd7f70SNikolay Borisov 		btrfs_err(fs_info,
2445aefd7f70SNikolay Borisov "superblock metadata_uuid doesn't match metadata uuid of fs_devices: %pU != %pU",
24466bfe3959SAnand Jain 			  btrfs_sb_fsid_ptr(sb), fs_info->fs_devices->metadata_uuid);
2447aefd7f70SNikolay Borisov 		ret = -EINVAL;
2448aefd7f70SNikolay Borisov 	}
2449aefd7f70SNikolay Borisov 
245025984a5aSAnand Jain 	if (memcmp(fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid,
245125984a5aSAnand Jain 		   BTRFS_FSID_SIZE) != 0) {
245225984a5aSAnand Jain 		btrfs_err(fs_info,
245325984a5aSAnand Jain 			"dev_item UUID does not match metadata fsid: %pU != %pU",
245425984a5aSAnand Jain 			fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid);
245525984a5aSAnand Jain 		ret = -EINVAL;
245625984a5aSAnand Jain 	}
245725984a5aSAnand Jain 
24581c56ab99SQu Wenruo 	/*
24591c56ab99SQu Wenruo 	 * Artificial requirement for block-group-tree to force newer features
24601c56ab99SQu Wenruo 	 * (free-space-tree, no-holes) so the test matrix is smaller.
24611c56ab99SQu Wenruo 	 */
24621c56ab99SQu Wenruo 	if (btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE) &&
24631c56ab99SQu Wenruo 	    (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID) ||
24641c56ab99SQu Wenruo 	     !btrfs_fs_incompat(fs_info, NO_HOLES))) {
24651c56ab99SQu Wenruo 		btrfs_err(fs_info,
24660102ab54SMark Harmstone 		"block-group-tree feature requires free-space-tree and no-holes");
24671c56ab99SQu Wenruo 		ret = -EINVAL;
24681c56ab99SQu Wenruo 	}
24691c56ab99SQu Wenruo 
247021a852b0SQu Wenruo 	/*
247121a852b0SQu Wenruo 	 * Hint to catch really bogus numbers, bitflips or so, more exact checks are
247221a852b0SQu Wenruo 	 * done later
247321a852b0SQu Wenruo 	 */
247421a852b0SQu Wenruo 	if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) {
247521a852b0SQu Wenruo 		btrfs_err(fs_info, "bytes_used is too small %llu",
247621a852b0SQu Wenruo 			  btrfs_super_bytes_used(sb));
247721a852b0SQu Wenruo 		ret = -EINVAL;
247821a852b0SQu Wenruo 	}
247921a852b0SQu Wenruo 	if (!is_power_of_2(btrfs_super_stripesize(sb))) {
248021a852b0SQu Wenruo 		btrfs_err(fs_info, "invalid stripesize %u",
248121a852b0SQu Wenruo 			  btrfs_super_stripesize(sb));
248221a852b0SQu Wenruo 		ret = -EINVAL;
248321a852b0SQu Wenruo 	}
248421a852b0SQu Wenruo 	if (btrfs_super_num_devices(sb) > (1UL << 31))
248521a852b0SQu Wenruo 		btrfs_warn(fs_info, "suspicious number of devices: %llu",
248621a852b0SQu Wenruo 			   btrfs_super_num_devices(sb));
248721a852b0SQu Wenruo 	if (btrfs_super_num_devices(sb) == 0) {
248821a852b0SQu Wenruo 		btrfs_err(fs_info, "number of devices is 0");
248921a852b0SQu Wenruo 		ret = -EINVAL;
249021a852b0SQu Wenruo 	}
249121a852b0SQu Wenruo 
2492069ec957SQu Wenruo 	if (mirror_num >= 0 &&
2493069ec957SQu Wenruo 	    btrfs_super_bytenr(sb) != btrfs_sb_offset(mirror_num)) {
249421a852b0SQu Wenruo 		btrfs_err(fs_info, "super offset mismatch %llu != %u",
249521a852b0SQu Wenruo 			  btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET);
249621a852b0SQu Wenruo 		ret = -EINVAL;
249721a852b0SQu Wenruo 	}
249821a852b0SQu Wenruo 
249921a852b0SQu Wenruo 	/*
250021a852b0SQu Wenruo 	 * Obvious sys_chunk_array corruptions, it must hold at least one key
250121a852b0SQu Wenruo 	 * and one chunk
250221a852b0SQu Wenruo 	 */
250321a852b0SQu Wenruo 	if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
250421a852b0SQu Wenruo 		btrfs_err(fs_info, "system chunk array too big %u > %u",
250521a852b0SQu Wenruo 			  btrfs_super_sys_array_size(sb),
250621a852b0SQu Wenruo 			  BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
250721a852b0SQu Wenruo 		ret = -EINVAL;
250821a852b0SQu Wenruo 	}
250921a852b0SQu Wenruo 	if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key)
251021a852b0SQu Wenruo 			+ sizeof(struct btrfs_chunk)) {
251121a852b0SQu Wenruo 		btrfs_err(fs_info, "system chunk array too small %u < %zu",
251221a852b0SQu Wenruo 			  btrfs_super_sys_array_size(sb),
251321a852b0SQu Wenruo 			  sizeof(struct btrfs_disk_key)
251421a852b0SQu Wenruo 			  + sizeof(struct btrfs_chunk));
251521a852b0SQu Wenruo 		ret = -EINVAL;
251621a852b0SQu Wenruo 	}
251721a852b0SQu Wenruo 
251821a852b0SQu Wenruo 	/*
251921a852b0SQu Wenruo 	 * The generation is a global counter, we'll trust it more than the others
252021a852b0SQu Wenruo 	 * but it's still possible that it's the one that's wrong.
252121a852b0SQu Wenruo 	 */
252221a852b0SQu Wenruo 	if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb))
252321a852b0SQu Wenruo 		btrfs_warn(fs_info,
252421a852b0SQu Wenruo 			"suspicious: generation < chunk_root_generation: %llu < %llu",
252521a852b0SQu Wenruo 			btrfs_super_generation(sb),
252621a852b0SQu Wenruo 			btrfs_super_chunk_root_generation(sb));
252721a852b0SQu Wenruo 	if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb)
252821a852b0SQu Wenruo 	    && btrfs_super_cache_generation(sb) != (u64)-1)
252921a852b0SQu Wenruo 		btrfs_warn(fs_info,
253021a852b0SQu Wenruo 			"suspicious: generation < cache_generation: %llu < %llu",
253121a852b0SQu Wenruo 			btrfs_super_generation(sb),
253221a852b0SQu Wenruo 			btrfs_super_cache_generation(sb));
253321a852b0SQu Wenruo 
253421a852b0SQu Wenruo 	return ret;
253521a852b0SQu Wenruo }
253621a852b0SQu Wenruo 
2537069ec957SQu Wenruo /*
2538069ec957SQu Wenruo  * Validation of super block at mount time.
2539069ec957SQu Wenruo  * Some checks already done early at mount time, like csum type and incompat
2540069ec957SQu Wenruo  * flags will be skipped.
2541069ec957SQu Wenruo  */
btrfs_validate_mount_super(struct btrfs_fs_info * fs_info)2542069ec957SQu Wenruo static int btrfs_validate_mount_super(struct btrfs_fs_info *fs_info)
2543069ec957SQu Wenruo {
2544a05d3c91SQu Wenruo 	return btrfs_validate_super(fs_info, fs_info->super_copy, 0);
2545069ec957SQu Wenruo }
2546069ec957SQu Wenruo 
254775cb857dSQu Wenruo /*
254875cb857dSQu Wenruo  * Validation of super block at write time.
254975cb857dSQu Wenruo  * Some checks like bytenr check will be skipped as their values will be
255075cb857dSQu Wenruo  * overwritten soon.
255175cb857dSQu Wenruo  * Extra checks like csum type and incompat flags will be done here.
255275cb857dSQu Wenruo  */
btrfs_validate_write_super(struct btrfs_fs_info * fs_info,struct btrfs_super_block * sb)255375cb857dSQu Wenruo static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info,
255475cb857dSQu Wenruo 				      struct btrfs_super_block *sb)
255575cb857dSQu Wenruo {
255675cb857dSQu Wenruo 	int ret;
255775cb857dSQu Wenruo 
2558a05d3c91SQu Wenruo 	ret = btrfs_validate_super(fs_info, sb, -1);
255975cb857dSQu Wenruo 	if (ret < 0)
256075cb857dSQu Wenruo 		goto out;
2561e7e16f48SJohannes Thumshirn 	if (!btrfs_supported_super_csum(btrfs_super_csum_type(sb))) {
256275cb857dSQu Wenruo 		ret = -EUCLEAN;
256375cb857dSQu Wenruo 		btrfs_err(fs_info, "invalid csum type, has %u want %u",
256475cb857dSQu Wenruo 			  btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32);
256575cb857dSQu Wenruo 		goto out;
256675cb857dSQu Wenruo 	}
256775cb857dSQu Wenruo 	if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) {
256875cb857dSQu Wenruo 		ret = -EUCLEAN;
256975cb857dSQu Wenruo 		btrfs_err(fs_info,
257075cb857dSQu Wenruo 		"invalid incompat flags, has 0x%llx valid mask 0x%llx",
257175cb857dSQu Wenruo 			  btrfs_super_incompat_flags(sb),
257275cb857dSQu Wenruo 			  (unsigned long long)BTRFS_FEATURE_INCOMPAT_SUPP);
257375cb857dSQu Wenruo 		goto out;
257475cb857dSQu Wenruo 	}
257575cb857dSQu Wenruo out:
257675cb857dSQu Wenruo 	if (ret < 0)
257775cb857dSQu Wenruo 		btrfs_err(fs_info,
257875cb857dSQu Wenruo 		"super block corruption detected before writing it to disk");
257975cb857dSQu Wenruo 	return ret;
258075cb857dSQu Wenruo }
258175cb857dSQu Wenruo 
load_super_root(struct btrfs_root * root,u64 bytenr,u64 gen,int level)2582bd676446SJosef Bacik static int load_super_root(struct btrfs_root *root, u64 bytenr, u64 gen, int level)
2583bd676446SJosef Bacik {
2584789d6a3aSQu Wenruo 	struct btrfs_tree_parent_check check = {
2585789d6a3aSQu Wenruo 		.level = level,
2586789d6a3aSQu Wenruo 		.transid = gen,
2587e094f480SJosef Bacik 		.owner_root = btrfs_root_id(root)
2588789d6a3aSQu Wenruo 	};
2589bd676446SJosef Bacik 	int ret = 0;
2590bd676446SJosef Bacik 
2591789d6a3aSQu Wenruo 	root->node = read_tree_block(root->fs_info, bytenr, &check);
2592bd676446SJosef Bacik 	if (IS_ERR(root->node)) {
2593bd676446SJosef Bacik 		ret = PTR_ERR(root->node);
2594bd676446SJosef Bacik 		root->node = NULL;
25954eb150d6SQu Wenruo 		return ret;
25964eb150d6SQu Wenruo 	}
25974eb150d6SQu Wenruo 	if (!extent_buffer_uptodate(root->node)) {
2598bd676446SJosef Bacik 		free_extent_buffer(root->node);
2599bd676446SJosef Bacik 		root->node = NULL;
26004eb150d6SQu Wenruo 		return -EIO;
2601bd676446SJosef Bacik 	}
2602bd676446SJosef Bacik 
2603bd676446SJosef Bacik 	btrfs_set_root_node(&root->root_item, root->node);
2604bd676446SJosef Bacik 	root->commit_root = btrfs_root_node(root);
2605bd676446SJosef Bacik 	btrfs_set_root_refs(&root->root_item, 1);
2606bd676446SJosef Bacik 	return ret;
2607bd676446SJosef Bacik }
2608bd676446SJosef Bacik 
load_important_roots(struct btrfs_fs_info * fs_info)2609bd676446SJosef Bacik static int load_important_roots(struct btrfs_fs_info *fs_info)
2610bd676446SJosef Bacik {
2611bd676446SJosef Bacik 	struct btrfs_super_block *sb = fs_info->super_copy;
2612bd676446SJosef Bacik 	u64 gen, bytenr;
2613bd676446SJosef Bacik 	int level, ret;
2614bd676446SJosef Bacik 
2615bd676446SJosef Bacik 	bytenr = btrfs_super_root(sb);
2616bd676446SJosef Bacik 	gen = btrfs_super_generation(sb);
2617bd676446SJosef Bacik 	level = btrfs_super_root_level(sb);
2618bd676446SJosef Bacik 	ret = load_super_root(fs_info->tree_root, bytenr, gen, level);
26199c54e80dSJosef Bacik 	if (ret) {
2620bd676446SJosef Bacik 		btrfs_warn(fs_info, "couldn't read tree root");
2621bd676446SJosef Bacik 		return ret;
2622bd676446SJosef Bacik 	}
26239c54e80dSJosef Bacik 	return 0;
26249c54e80dSJosef Bacik }
26259c54e80dSJosef Bacik 
init_tree_roots(struct btrfs_fs_info * fs_info)26266ef108ddSNikolay Borisov static int __cold init_tree_roots(struct btrfs_fs_info *fs_info)
2627b8522a1eSNikolay Borisov {
26286ef108ddSNikolay Borisov 	int backup_index = find_newest_super_backup(fs_info);
2629b8522a1eSNikolay Borisov 	struct btrfs_super_block *sb = fs_info->super_copy;
2630b8522a1eSNikolay Borisov 	struct btrfs_root *tree_root = fs_info->tree_root;
2631b8522a1eSNikolay Borisov 	bool handle_error = false;
2632b8522a1eSNikolay Borisov 	int ret = 0;
2633b8522a1eSNikolay Borisov 	int i;
2634b8522a1eSNikolay Borisov 
2635b8522a1eSNikolay Borisov 	for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
2636b8522a1eSNikolay Borisov 		if (handle_error) {
2637b8522a1eSNikolay Borisov 			if (!IS_ERR(tree_root->node))
2638b8522a1eSNikolay Borisov 				free_extent_buffer(tree_root->node);
2639b8522a1eSNikolay Borisov 			tree_root->node = NULL;
2640b8522a1eSNikolay Borisov 
2641b8522a1eSNikolay Borisov 			if (!btrfs_test_opt(fs_info, USEBACKUPROOT))
2642b8522a1eSNikolay Borisov 				break;
2643b8522a1eSNikolay Borisov 
2644b8522a1eSNikolay Borisov 			free_root_pointers(fs_info, 0);
2645b8522a1eSNikolay Borisov 
2646b8522a1eSNikolay Borisov 			/*
2647b8522a1eSNikolay Borisov 			 * Don't use the log in recovery mode, it won't be
2648b8522a1eSNikolay Borisov 			 * valid
2649b8522a1eSNikolay Borisov 			 */
2650b8522a1eSNikolay Borisov 			btrfs_set_super_log_root(sb, 0);
2651b8522a1eSNikolay Borisov 
2652745806fbSQu Wenruo 			btrfs_warn(fs_info, "try to load backup roots slot %d", i);
2653b8522a1eSNikolay Borisov 			ret = read_backup_root(fs_info, i);
26546ef108ddSNikolay Borisov 			backup_index = ret;
2655b8522a1eSNikolay Borisov 			if (ret < 0)
2656b8522a1eSNikolay Borisov 				return ret;
2657b8522a1eSNikolay Borisov 		}
2658b8522a1eSNikolay Borisov 
2659bd676446SJosef Bacik 		ret = load_important_roots(fs_info);
2660bd676446SJosef Bacik 		if (ret) {
2661217f5004SNikolay Borisov 			handle_error = true;
2662b8522a1eSNikolay Borisov 			continue;
2663b8522a1eSNikolay Borisov 		}
2664b8522a1eSNikolay Borisov 
2665336a0d8dSNikolay Borisov 		/*
2666336a0d8dSNikolay Borisov 		 * No need to hold btrfs_root::objectid_mutex since the fs
2667336a0d8dSNikolay Borisov 		 * hasn't been fully initialised and we are the only user
2668336a0d8dSNikolay Borisov 		 */
2669453e4873SNikolay Borisov 		ret = btrfs_init_root_free_objectid(tree_root);
2670b8522a1eSNikolay Borisov 		if (ret < 0) {
2671b8522a1eSNikolay Borisov 			handle_error = true;
2672b8522a1eSNikolay Borisov 			continue;
2673b8522a1eSNikolay Borisov 		}
2674b8522a1eSNikolay Borisov 
26756b8fad57SNikolay Borisov 		ASSERT(tree_root->free_objectid <= BTRFS_LAST_FREE_OBJECTID);
2676b8522a1eSNikolay Borisov 
2677b8522a1eSNikolay Borisov 		ret = btrfs_read_roots(fs_info);
2678b8522a1eSNikolay Borisov 		if (ret < 0) {
2679b8522a1eSNikolay Borisov 			handle_error = true;
2680b8522a1eSNikolay Borisov 			continue;
2681b8522a1eSNikolay Borisov 		}
2682b8522a1eSNikolay Borisov 
2683b8522a1eSNikolay Borisov 		/* All successful */
2684bd676446SJosef Bacik 		fs_info->generation = btrfs_header_generation(tree_root->node);
26850124855fSFilipe Manana 		btrfs_set_last_trans_committed(fs_info, fs_info->generation);
2686d96b3424SFilipe Manana 		fs_info->last_reloc_trans = 0;
26876ef108ddSNikolay Borisov 
26886ef108ddSNikolay Borisov 		/* Always begin writing backup roots after the one being used */
26896ef108ddSNikolay Borisov 		if (backup_index < 0) {
26906ef108ddSNikolay Borisov 			fs_info->backup_root_index = 0;
26916ef108ddSNikolay Borisov 		} else {
26926ef108ddSNikolay Borisov 			fs_info->backup_root_index = backup_index + 1;
26936ef108ddSNikolay Borisov 			fs_info->backup_root_index %= BTRFS_NUM_BACKUP_ROOTS;
26946ef108ddSNikolay Borisov 		}
2695b8522a1eSNikolay Borisov 		break;
2696b8522a1eSNikolay Borisov 	}
2697b8522a1eSNikolay Borisov 
2698b8522a1eSNikolay Borisov 	return ret;
2699b8522a1eSNikolay Borisov }
2700b8522a1eSNikolay Borisov 
btrfs_init_fs_info(struct btrfs_fs_info * fs_info)27018260edbaSJosef Bacik void btrfs_init_fs_info(struct btrfs_fs_info *fs_info)
2702eb60ceacSChris Mason {
2703fc7cbcd4SDavid Sterba 	INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC);
270401cd3909SDavid Sterba 	INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC);
27058fd17795SChris Mason 	INIT_LIST_HEAD(&fs_info->trans_list);
2706facda1e7SChris Mason 	INIT_LIST_HEAD(&fs_info->dead_roots);
270724bbcf04SYan, Zheng 	INIT_LIST_HEAD(&fs_info->delayed_iputs);
2708eb73c1b7SMiao Xie 	INIT_LIST_HEAD(&fs_info->delalloc_roots);
270911833d66SYan Zheng 	INIT_LIST_HEAD(&fs_info->caching_block_groups);
2710eb73c1b7SMiao Xie 	spin_lock_init(&fs_info->delalloc_root_lock);
2711a4abeea4SJosef Bacik 	spin_lock_init(&fs_info->trans_lock);
2712fc7cbcd4SDavid Sterba 	spin_lock_init(&fs_info->fs_roots_radix_lock);
271324bbcf04SYan, Zheng 	spin_lock_init(&fs_info->delayed_iput_lock);
27144cb5300bSChris Mason 	spin_lock_init(&fs_info->defrag_inodes_lock);
2715ceda0864SMiao Xie 	spin_lock_init(&fs_info->super_lock);
2716f28491e0SJosef Bacik 	spin_lock_init(&fs_info->buffer_lock);
271747ab2a6cSJosef Bacik 	spin_lock_init(&fs_info->unused_bgs_lock);
271840ab3be1SNaohiro Aota 	spin_lock_init(&fs_info->treelog_bg_lock);
2719afba2bc0SNaohiro Aota 	spin_lock_init(&fs_info->zone_active_bgs_lock);
2720c2707a25SJohannes Thumshirn 	spin_lock_init(&fs_info->relocation_bg_lock);
2721f29021b2SJan Schmidt 	rwlock_init(&fs_info->tree_mod_log_lock);
2722abed4aaaSJosef Bacik 	rwlock_init(&fs_info->global_root_lock);
2723d7c15171SZhao Lei 	mutex_init(&fs_info->unused_bg_unpin_mutex);
2724f3372065SJohannes Thumshirn 	mutex_init(&fs_info->reclaim_bgs_lock);
27257585717fSChris Mason 	mutex_init(&fs_info->reloc_mutex);
2726573bfb72SMiao Xie 	mutex_init(&fs_info->delalloc_root_mutex);
27270bc09ca1SNaohiro Aota 	mutex_init(&fs_info->zoned_meta_io_lock);
27285f0addf7SNaohiro Aota 	mutex_init(&fs_info->zoned_data_reloc_io_lock);
2729de98ced9SMiao Xie 	seqlock_init(&fs_info->profiles_lock);
273019c00ddcSChris Mason 
2731e1489b4fSIoannis Angelakopoulos 	btrfs_lockdep_init_map(fs_info, btrfs_trans_num_writers);
27325a9ba670SIoannis Angelakopoulos 	btrfs_lockdep_init_map(fs_info, btrfs_trans_num_extwriters);
27338b53779eSIoannis Angelakopoulos 	btrfs_lockdep_init_map(fs_info, btrfs_trans_pending_ordered);
27345f4403e1SIoannis Angelakopoulos 	btrfs_lockdep_init_map(fs_info, btrfs_ordered_extent);
273577d20c68SJosef Bacik 	btrfs_state_lockdep_init_map(fs_info, btrfs_trans_commit_prep,
273677d20c68SJosef Bacik 				     BTRFS_LOCKDEP_TRANS_COMMIT_PREP);
27373e738c53SIoannis Angelakopoulos 	btrfs_state_lockdep_init_map(fs_info, btrfs_trans_unblocked,
27383e738c53SIoannis Angelakopoulos 				     BTRFS_LOCKDEP_TRANS_UNBLOCKED);
27393e738c53SIoannis Angelakopoulos 	btrfs_state_lockdep_init_map(fs_info, btrfs_trans_super_committed,
27403e738c53SIoannis Angelakopoulos 				     BTRFS_LOCKDEP_TRANS_SUPER_COMMITTED);
27413e738c53SIoannis Angelakopoulos 	btrfs_state_lockdep_init_map(fs_info, btrfs_trans_completed,
27423e738c53SIoannis Angelakopoulos 				     BTRFS_LOCKDEP_TRANS_COMPLETED);
2743e1489b4fSIoannis Angelakopoulos 
27440b86a832SChris Mason 	INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
27456324fbf3SChris Mason 	INIT_LIST_HEAD(&fs_info->space_info);
2746f29021b2SJan Schmidt 	INIT_LIST_HEAD(&fs_info->tree_mod_seq_list);
274747ab2a6cSJosef Bacik 	INIT_LIST_HEAD(&fs_info->unused_bgs);
274818bb8bbfSJohannes Thumshirn 	INIT_LIST_HEAD(&fs_info->reclaim_bgs);
2749afba2bc0SNaohiro Aota 	INIT_LIST_HEAD(&fs_info->zone_active_bgs);
2750bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
2751bd647ce3SJosef Bacik 	INIT_LIST_HEAD(&fs_info->allocated_roots);
27523fd63727SJosef Bacik 	INIT_LIST_HEAD(&fs_info->allocated_ebs);
27533fd63727SJosef Bacik 	spin_lock_init(&fs_info->eb_leak_lock);
2754bd647ce3SJosef Bacik #endif
27557dc66abbSFilipe Manana 	fs_info->mapping_tree = RB_ROOT_CACHED;
27567dc66abbSFilipe Manana 	rwlock_init(&fs_info->mapping_tree_lock);
275766d8f3ddSMiao Xie 	btrfs_init_block_rsv(&fs_info->global_block_rsv,
275866d8f3ddSMiao Xie 			     BTRFS_BLOCK_RSV_GLOBAL);
275966d8f3ddSMiao Xie 	btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS);
276066d8f3ddSMiao Xie 	btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK);
276166d8f3ddSMiao Xie 	btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY);
276266d8f3ddSMiao Xie 	btrfs_init_block_rsv(&fs_info->delayed_block_rsv,
276366d8f3ddSMiao Xie 			     BTRFS_BLOCK_RSV_DELOPS);
2764ba2c4d4eSJosef Bacik 	btrfs_init_block_rsv(&fs_info->delayed_refs_rsv,
2765ba2c4d4eSJosef Bacik 			     BTRFS_BLOCK_RSV_DELREFS);
2766ba2c4d4eSJosef Bacik 
2767771ed689SChris Mason 	atomic_set(&fs_info->async_delalloc_pages, 0);
27684cb5300bSChris Mason 	atomic_set(&fs_info->defrag_running, 0);
2769034f784dSJosef Bacik 	atomic_set(&fs_info->nr_delayed_iputs, 0);
2770fc36ed7eSJan Schmidt 	atomic64_set(&fs_info->tree_mod_seq, 0);
2771abed4aaaSJosef Bacik 	fs_info->global_root_tree = RB_ROOT;
277295ac567aSFilipe David Borba Manana 	fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE;
27739ed74f2dSJosef Bacik 	fs_info->metadata_ratio = 0;
27744cb5300bSChris Mason 	fs_info->defrag_inodes = RB_ROOT;
2775a5ed45f8SNikolay Borisov 	atomic64_set(&fs_info->free_chunk_space, 0);
2776f29021b2SJan Schmidt 	fs_info->tree_mod_log = RB_ROOT;
27778b87dc17SDavid Sterba 	fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
2778fd708b81SJosef Bacik 	btrfs_init_ref_verify(fs_info);
2779c8b97818SChris Mason 
2780b34b086cSChris Mason 	fs_info->thread_pool_size = min_t(unsigned long,
2781b34b086cSChris Mason 					  num_online_cpus() + 2, 8);
27820afbaf8cSChris Mason 
2783199c2a9cSMiao Xie 	INIT_LIST_HEAD(&fs_info->ordered_roots);
2784199c2a9cSMiao Xie 	spin_lock_init(&fs_info->ordered_root_lock);
278569fe2d75SJosef Bacik 
2786638aa7edSEric Sandeen 	btrfs_init_scrub(fs_info);
2787779a65a4SEric Sandeen 	btrfs_init_balance(fs_info);
278857056740SJosef Bacik 	btrfs_init_async_reclaim_work(fs_info);
2789a2de733cSArne Jansen 
279016b0c258SFilipe Manana 	rwlock_init(&fs_info->block_group_cache_lock);
279108dddb29SFilipe Manana 	fs_info->block_group_cache_tree = RB_ROOT_CACHED;
27920f9dd46cSJosef Bacik 
2793fe119a6eSNikolay Borisov 	extent_io_tree_init(fs_info, &fs_info->excluded_extents,
279435da5a7eSDavid Sterba 			    IO_TREE_FS_EXCLUDED_EXTENTS);
279539279cc3SChris Mason 
27965a3f23d5SChris Mason 	mutex_init(&fs_info->ordered_operations_mutex);
2797e02119d5SChris Mason 	mutex_init(&fs_info->tree_log_mutex);
2798925baeddSChris Mason 	mutex_init(&fs_info->chunk_mutex);
2799a74a4b97SChris Mason 	mutex_init(&fs_info->transaction_kthread_mutex);
2800a74a4b97SChris Mason 	mutex_init(&fs_info->cleaner_mutex);
28011bbc621eSChris Mason 	mutex_init(&fs_info->ro_block_group_mutex);
28029e351cc8SJosef Bacik 	init_rwsem(&fs_info->commit_root_sem);
2803c71bf099SYan, Zheng 	init_rwsem(&fs_info->cleanup_work_sem);
280476dda93cSYan, Zheng 	init_rwsem(&fs_info->subvol_sem);
2805803b2f54SStefan Behrens 	sema_init(&fs_info->uuid_tree_rescan_sem, 1);
2806fa9c0d79SChris Mason 
2807ad618368SEric Sandeen 	btrfs_init_dev_replace_locks(fs_info);
2808f9e92e40SEric Sandeen 	btrfs_init_qgroup(fs_info);
2809b0643e59SDennis Zhou 	btrfs_discard_init(fs_info);
2810416ac51dSArne Jansen 
2811fa9c0d79SChris Mason 	btrfs_init_free_cluster(&fs_info->meta_alloc_cluster);
2812fa9c0d79SChris Mason 	btrfs_init_free_cluster(&fs_info->data_alloc_cluster);
2813fa9c0d79SChris Mason 
2814e6dcd2dcSChris Mason 	init_waitqueue_head(&fs_info->transaction_throttle);
2815f9295749SChris Mason 	init_waitqueue_head(&fs_info->transaction_wait);
2816bb9c12c9SSage Weil 	init_waitqueue_head(&fs_info->transaction_blocked_wait);
28174854ddd0SChris Mason 	init_waitqueue_head(&fs_info->async_submit_wait);
2818034f784dSJosef Bacik 	init_waitqueue_head(&fs_info->delayed_iputs_wait);
28193768f368SChris Mason 
2820da17066cSJeff Mahoney 	/* Usable values until the real ones are cached from the superblock */
2821da17066cSJeff Mahoney 	fs_info->nodesize = 4096;
2822da17066cSJeff Mahoney 	fs_info->sectorsize = 4096;
2823ab108d99SDavid Sterba 	fs_info->sectorsize_bits = ilog2(4096);
2824da17066cSJeff Mahoney 	fs_info->stripesize = 4096;
2825da17066cSJeff Mahoney 
28266207c9e3SJosef Bacik 	/* Default compress algorithm when user does -o compress */
28276207c9e3SJosef Bacik 	fs_info->compress_type = BTRFS_COMPRESS_ZLIB;
28286207c9e3SJosef Bacik 
2829f7b12a62SNaohiro Aota 	fs_info->max_extent_size = BTRFS_MAX_EXTENT_SIZE;
2830f7b12a62SNaohiro Aota 
2831eede2bf3SOmar Sandoval 	spin_lock_init(&fs_info->swapfile_pins_lock);
2832eede2bf3SOmar Sandoval 	fs_info->swapfile_pins = RB_ROOT;
2833eede2bf3SOmar Sandoval 
283418bb8bbfSJohannes Thumshirn 	fs_info->bg_reclaim_threshold = BTRFS_DEFAULT_RECLAIM_THRESH;
283518bb8bbfSJohannes Thumshirn 	INIT_WORK(&fs_info->reclaim_bgs_work, btrfs_reclaim_bgs_work);
28368260edbaSJosef Bacik }
28378260edbaSJosef Bacik 
init_mount_fs_info(struct btrfs_fs_info * fs_info,struct super_block * sb)28388260edbaSJosef Bacik static int init_mount_fs_info(struct btrfs_fs_info *fs_info, struct super_block *sb)
28398260edbaSJosef Bacik {
28408260edbaSJosef Bacik 	int ret;
28418260edbaSJosef Bacik 
28428260edbaSJosef Bacik 	fs_info->sb = sb;
28434e00422eSDavid Sterba 	/* Temporary fixed values for block size until we read the superblock. */
28448260edbaSJosef Bacik 	sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE;
28458260edbaSJosef Bacik 	sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE);
28469e967495SFilipe Manana 
28475deb17e1SJosef Bacik 	ret = percpu_counter_init(&fs_info->ordered_bytes, 0, GFP_KERNEL);
2848ae18c37aSJosef Bacik 	if (ret)
2849ae18c37aSJosef Bacik 		return ret;
2850ae18c37aSJosef Bacik 
2851f1d97e76SFilipe Manana 	ret = percpu_counter_init(&fs_info->evictable_extent_maps, 0, GFP_KERNEL);
2852f1d97e76SFilipe Manana 	if (ret)
2853f1d97e76SFilipe Manana 		return ret;
2854f1d97e76SFilipe Manana 
285544849405SFilipe Manana 	spin_lock_init(&fs_info->extent_map_shrinker_lock);
285644849405SFilipe Manana 
2857ae18c37aSJosef Bacik 	ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL);
2858ae18c37aSJosef Bacik 	if (ret)
2859c75e8394SJosef Bacik 		return ret;
2860ae18c37aSJosef Bacik 
2861ae18c37aSJosef Bacik 	fs_info->dirty_metadata_batch = PAGE_SIZE *
2862ae18c37aSJosef Bacik 					(1 + ilog2(nr_cpu_ids));
2863ae18c37aSJosef Bacik 
2864ae18c37aSJosef Bacik 	ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL);
2865ae18c37aSJosef Bacik 	if (ret)
2866c75e8394SJosef Bacik 		return ret;
2867ae18c37aSJosef Bacik 
2868ae18c37aSJosef Bacik 	ret = percpu_counter_init(&fs_info->dev_replace.bio_counter, 0,
2869ae18c37aSJosef Bacik 			GFP_KERNEL);
2870ae18c37aSJosef Bacik 	if (ret)
2871c75e8394SJosef Bacik 		return ret;
2872ae18c37aSJosef Bacik 
2873ae18c37aSJosef Bacik 	fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root),
2874ae18c37aSJosef Bacik 					GFP_KERNEL);
2875c75e8394SJosef Bacik 	if (!fs_info->delayed_root)
2876c75e8394SJosef Bacik 		return -ENOMEM;
2877ae18c37aSJosef Bacik 	btrfs_init_delayed_root(fs_info->delayed_root);
2878ae18c37aSJosef Bacik 
2879a0a1db70SFilipe Manana 	if (sb_rdonly(sb))
2880a0a1db70SFilipe Manana 		set_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state);
2881169aaaf2SQu Wenruo 	if (btrfs_test_opt(fs_info, IGNOREMETACSUMS))
2882169aaaf2SQu Wenruo 		set_bit(BTRFS_FS_STATE_SKIP_META_CSUMS, &fs_info->fs_state);
2883a0a1db70SFilipe Manana 
2884c75e8394SJosef Bacik 	return btrfs_alloc_stripe_hash_table(fs_info);
2885ae18c37aSJosef Bacik }
2886ae18c37aSJosef Bacik 
btrfs_uuid_rescan_kthread(void * data)288797f4dd09SNikolay Borisov static int btrfs_uuid_rescan_kthread(void *data)
288897f4dd09SNikolay Borisov {
28890d031dc4SYu Zhe 	struct btrfs_fs_info *fs_info = data;
289097f4dd09SNikolay Borisov 	int ret;
289197f4dd09SNikolay Borisov 
289297f4dd09SNikolay Borisov 	/*
289397f4dd09SNikolay Borisov 	 * 1st step is to iterate through the existing UUID tree and
289497f4dd09SNikolay Borisov 	 * to delete all entries that contain outdated data.
289597f4dd09SNikolay Borisov 	 * 2nd step is to add all missing entries to the UUID tree.
289697f4dd09SNikolay Borisov 	 */
289797f4dd09SNikolay Borisov 	ret = btrfs_uuid_tree_iterate(fs_info);
289897f4dd09SNikolay Borisov 	if (ret < 0) {
2899c94bec2cSJosef Bacik 		if (ret != -EINTR)
2900c94bec2cSJosef Bacik 			btrfs_warn(fs_info, "iterating uuid_tree failed %d",
2901c94bec2cSJosef Bacik 				   ret);
290297f4dd09SNikolay Borisov 		up(&fs_info->uuid_tree_rescan_sem);
290397f4dd09SNikolay Borisov 		return ret;
290497f4dd09SNikolay Borisov 	}
290597f4dd09SNikolay Borisov 	return btrfs_uuid_scan_kthread(data);
290697f4dd09SNikolay Borisov }
290797f4dd09SNikolay Borisov 
btrfs_check_uuid_tree(struct btrfs_fs_info * fs_info)290897f4dd09SNikolay Borisov static int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
290997f4dd09SNikolay Borisov {
291097f4dd09SNikolay Borisov 	struct task_struct *task;
291197f4dd09SNikolay Borisov 
291297f4dd09SNikolay Borisov 	down(&fs_info->uuid_tree_rescan_sem);
291397f4dd09SNikolay Borisov 	task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
291497f4dd09SNikolay Borisov 	if (IS_ERR(task)) {
291597f4dd09SNikolay Borisov 		/* fs_info->update_uuid_tree_gen remains 0 in all error case */
291697f4dd09SNikolay Borisov 		btrfs_warn(fs_info, "failed to start uuid_rescan task");
291797f4dd09SNikolay Borisov 		up(&fs_info->uuid_tree_rescan_sem);
291897f4dd09SNikolay Borisov 		return PTR_ERR(task);
291997f4dd09SNikolay Borisov 	}
292097f4dd09SNikolay Borisov 
292197f4dd09SNikolay Borisov 	return 0;
292297f4dd09SNikolay Borisov }
292397f4dd09SNikolay Borisov 
btrfs_cleanup_fs_roots(struct btrfs_fs_info * fs_info)2924504b1596SFilipe Manana static int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info)
2925504b1596SFilipe Manana {
2926504b1596SFilipe Manana 	u64 root_objectid = 0;
2927504b1596SFilipe Manana 	struct btrfs_root *gang[8];
292853d6c0daSAnand Jain 	int ret = 0;
2929504b1596SFilipe Manana 
2930504b1596SFilipe Manana 	while (1) {
293153d6c0daSAnand Jain 		unsigned int found;
293253d6c0daSAnand Jain 
2933504b1596SFilipe Manana 		spin_lock(&fs_info->fs_roots_radix_lock);
293453d6c0daSAnand Jain 		found = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
2935504b1596SFilipe Manana 					     (void **)gang, root_objectid,
2936504b1596SFilipe Manana 					     ARRAY_SIZE(gang));
293753d6c0daSAnand Jain 		if (!found) {
2938504b1596SFilipe Manana 			spin_unlock(&fs_info->fs_roots_radix_lock);
2939504b1596SFilipe Manana 			break;
2940504b1596SFilipe Manana 		}
294153d6c0daSAnand Jain 		root_objectid = btrfs_root_id(gang[found - 1]) + 1;
2942504b1596SFilipe Manana 
294353d6c0daSAnand Jain 		for (int i = 0; i < found; i++) {
2944504b1596SFilipe Manana 			/* Avoid to grab roots in dead_roots. */
2945504b1596SFilipe Manana 			if (btrfs_root_refs(&gang[i]->root_item) == 0) {
2946504b1596SFilipe Manana 				gang[i] = NULL;
2947504b1596SFilipe Manana 				continue;
2948504b1596SFilipe Manana 			}
2949504b1596SFilipe Manana 			/* Grab all the search result for later use. */
2950504b1596SFilipe Manana 			gang[i] = btrfs_grab_root(gang[i]);
2951504b1596SFilipe Manana 		}
2952504b1596SFilipe Manana 		spin_unlock(&fs_info->fs_roots_radix_lock);
2953504b1596SFilipe Manana 
295453d6c0daSAnand Jain 		for (int i = 0; i < found; i++) {
2955504b1596SFilipe Manana 			if (!gang[i])
2956504b1596SFilipe Manana 				continue;
2957e094f480SJosef Bacik 			root_objectid = btrfs_root_id(gang[i]);
295853d6c0daSAnand Jain 			/*
295953d6c0daSAnand Jain 			 * Continue to release the remaining roots after the first
296053d6c0daSAnand Jain 			 * error without cleanup and preserve the first error
296153d6c0daSAnand Jain 			 * for the return.
296253d6c0daSAnand Jain 			 */
296353d6c0daSAnand Jain 			if (!ret)
296453d6c0daSAnand Jain 				ret = btrfs_orphan_cleanup(gang[i]);
2965504b1596SFilipe Manana 			btrfs_put_root(gang[i]);
2966504b1596SFilipe Manana 		}
296753d6c0daSAnand Jain 		if (ret)
296853d6c0daSAnand Jain 			break;
296953d6c0daSAnand Jain 
2970504b1596SFilipe Manana 		root_objectid++;
2971504b1596SFilipe Manana 	}
297253d6c0daSAnand Jain 	return ret;
2973504b1596SFilipe Manana }
2974504b1596SFilipe Manana 
297544c0ca21SBoris Burkov /*
297644c0ca21SBoris Burkov  * Mounting logic specific to read-write file systems. Shared by open_ctree
297744c0ca21SBoris Burkov  * and btrfs_remount when remounting from read-only to read-write.
297844c0ca21SBoris Burkov  */
btrfs_start_pre_rw_mount(struct btrfs_fs_info * fs_info)297944c0ca21SBoris Burkov int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info)
298044c0ca21SBoris Burkov {
298144c0ca21SBoris Burkov 	int ret;
298294846229SBoris Burkov 	const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE);
29831d6a4fc8SQu Wenruo 	bool rebuild_free_space_tree = false;
29848b228324SBoris Burkov 
29858b228324SBoris Burkov 	if (btrfs_test_opt(fs_info, CLEAR_CACHE) &&
29868b228324SBoris Burkov 	    btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
2987272efa30SJosef Bacik 		if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
2988272efa30SJosef Bacik 			btrfs_warn(fs_info,
2989272efa30SJosef Bacik 				   "'clear_cache' option is ignored with extent tree v2");
2990272efa30SJosef Bacik 		else
29911d6a4fc8SQu Wenruo 			rebuild_free_space_tree = true;
29928b228324SBoris Burkov 	} else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
29938b228324SBoris Burkov 		   !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) {
29948b228324SBoris Burkov 		btrfs_warn(fs_info, "free space tree is invalid");
29951d6a4fc8SQu Wenruo 		rebuild_free_space_tree = true;
29968b228324SBoris Burkov 	}
29978b228324SBoris Burkov 
29981d6a4fc8SQu Wenruo 	if (rebuild_free_space_tree) {
29991d6a4fc8SQu Wenruo 		btrfs_info(fs_info, "rebuilding free space tree");
30001d6a4fc8SQu Wenruo 		ret = btrfs_rebuild_free_space_tree(fs_info);
30018b228324SBoris Burkov 		if (ret) {
30028b228324SBoris Burkov 			btrfs_warn(fs_info,
30031d6a4fc8SQu Wenruo 				   "failed to rebuild free space tree: %d", ret);
30041d6a4fc8SQu Wenruo 			goto out;
30051d6a4fc8SQu Wenruo 		}
30061d6a4fc8SQu Wenruo 	}
30071d6a4fc8SQu Wenruo 
30081d6a4fc8SQu Wenruo 	if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
30091d6a4fc8SQu Wenruo 	    !btrfs_test_opt(fs_info, FREE_SPACE_TREE)) {
30101d6a4fc8SQu Wenruo 		btrfs_info(fs_info, "disabling free space tree");
30111d6a4fc8SQu Wenruo 		ret = btrfs_delete_free_space_tree(fs_info);
30121d6a4fc8SQu Wenruo 		if (ret) {
30131d6a4fc8SQu Wenruo 			btrfs_warn(fs_info,
30141d6a4fc8SQu Wenruo 				   "failed to disable free space tree: %d", ret);
30158b228324SBoris Burkov 			goto out;
30168b228324SBoris Burkov 		}
30178b228324SBoris Burkov 	}
301844c0ca21SBoris Burkov 
30198d488a8cSFilipe Manana 	/*
30208d488a8cSFilipe Manana 	 * btrfs_find_orphan_roots() is responsible for finding all the dead
30218d488a8cSFilipe Manana 	 * roots (with 0 refs), flag them with BTRFS_ROOT_DEAD_TREE and load
3022fc7cbcd4SDavid Sterba 	 * them into the fs_info->fs_roots_radix tree. This must be done before
30238d488a8cSFilipe Manana 	 * calling btrfs_orphan_cleanup() on the tree root. If we don't do it
30248d488a8cSFilipe Manana 	 * first, then btrfs_orphan_cleanup() will delete a dead root's orphan
30258d488a8cSFilipe Manana 	 * item before the root's tree is deleted - this means that if we unmount
30268d488a8cSFilipe Manana 	 * or crash before the deletion completes, on the next mount we will not
30278d488a8cSFilipe Manana 	 * delete what remains of the tree because the orphan item does not
30288d488a8cSFilipe Manana 	 * exists anymore, which is what tells us we have a pending deletion.
30298d488a8cSFilipe Manana 	 */
30308d488a8cSFilipe Manana 	ret = btrfs_find_orphan_roots(fs_info);
30318d488a8cSFilipe Manana 	if (ret)
30328d488a8cSFilipe Manana 		goto out;
30338d488a8cSFilipe Manana 
303444c0ca21SBoris Burkov 	ret = btrfs_cleanup_fs_roots(fs_info);
303544c0ca21SBoris Burkov 	if (ret)
303644c0ca21SBoris Burkov 		goto out;
303744c0ca21SBoris Burkov 
30388f1c21d7SBoris Burkov 	down_read(&fs_info->cleanup_work_sem);
30398f1c21d7SBoris Burkov 	if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) ||
30408f1c21d7SBoris Burkov 	    (ret = btrfs_orphan_cleanup(fs_info->tree_root))) {
30418f1c21d7SBoris Burkov 		up_read(&fs_info->cleanup_work_sem);
30428f1c21d7SBoris Burkov 		goto out;
30438f1c21d7SBoris Burkov 	}
30448f1c21d7SBoris Burkov 	up_read(&fs_info->cleanup_work_sem);
30458f1c21d7SBoris Burkov 
304644c0ca21SBoris Burkov 	mutex_lock(&fs_info->cleaner_mutex);
30477eefae6bSJosef Bacik 	ret = btrfs_recover_relocation(fs_info);
304844c0ca21SBoris Burkov 	mutex_unlock(&fs_info->cleaner_mutex);
304944c0ca21SBoris Burkov 	if (ret < 0) {
305044c0ca21SBoris Burkov 		btrfs_warn(fs_info, "failed to recover relocation: %d", ret);
305144c0ca21SBoris Burkov 		goto out;
305244c0ca21SBoris Burkov 	}
305344c0ca21SBoris Burkov 
30545011139aSBoris Burkov 	if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) &&
30555011139aSBoris Burkov 	    !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
30565011139aSBoris Burkov 		btrfs_info(fs_info, "creating free space tree");
30575011139aSBoris Burkov 		ret = btrfs_create_free_space_tree(fs_info);
30585011139aSBoris Burkov 		if (ret) {
30595011139aSBoris Burkov 			btrfs_warn(fs_info,
30605011139aSBoris Burkov 				"failed to create free space tree: %d", ret);
30615011139aSBoris Burkov 			goto out;
30625011139aSBoris Burkov 		}
30635011139aSBoris Burkov 	}
30645011139aSBoris Burkov 
306594846229SBoris Burkov 	if (cache_opt != btrfs_free_space_cache_v1_active(fs_info)) {
306694846229SBoris Burkov 		ret = btrfs_set_free_space_cache_v1_active(fs_info, cache_opt);
306794846229SBoris Burkov 		if (ret)
306894846229SBoris Burkov 			goto out;
306994846229SBoris Burkov 	}
307094846229SBoris Burkov 
307144c0ca21SBoris Burkov 	ret = btrfs_resume_balance_async(fs_info);
307244c0ca21SBoris Burkov 	if (ret)
307344c0ca21SBoris Burkov 		goto out;
307444c0ca21SBoris Burkov 
307544c0ca21SBoris Burkov 	ret = btrfs_resume_dev_replace_async(fs_info);
307644c0ca21SBoris Burkov 	if (ret) {
307744c0ca21SBoris Burkov 		btrfs_warn(fs_info, "failed to resume dev_replace");
307844c0ca21SBoris Burkov 		goto out;
307944c0ca21SBoris Burkov 	}
308044c0ca21SBoris Burkov 
308144c0ca21SBoris Burkov 	btrfs_qgroup_rescan_resume(fs_info);
308244c0ca21SBoris Burkov 
308344c0ca21SBoris Burkov 	if (!fs_info->uuid_root) {
308444c0ca21SBoris Burkov 		btrfs_info(fs_info, "creating UUID tree");
308544c0ca21SBoris Burkov 		ret = btrfs_create_uuid_tree(fs_info);
308644c0ca21SBoris Burkov 		if (ret) {
308744c0ca21SBoris Burkov 			btrfs_warn(fs_info,
308844c0ca21SBoris Burkov 				   "failed to create the UUID tree %d", ret);
308944c0ca21SBoris Burkov 			goto out;
309044c0ca21SBoris Burkov 		}
309144c0ca21SBoris Burkov 	}
309244c0ca21SBoris Burkov 
309344c0ca21SBoris Burkov out:
309444c0ca21SBoris Burkov 	return ret;
309544c0ca21SBoris Burkov }
309644c0ca21SBoris Burkov 
3097d7f67ac9SQu Wenruo /*
3098d7f67ac9SQu Wenruo  * Do various sanity and dependency checks of different features.
3099d7f67ac9SQu Wenruo  *
31002ba48b20SQu Wenruo  * @is_rw_mount:	If the mount is read-write.
31012ba48b20SQu Wenruo  *
3102d7f67ac9SQu Wenruo  * This is the place for less strict checks (like for subpage or artificial
3103d7f67ac9SQu Wenruo  * feature dependencies).
3104d7f67ac9SQu Wenruo  *
3105d7f67ac9SQu Wenruo  * For strict checks or possible corruption detection, see
3106d7f67ac9SQu Wenruo  * btrfs_validate_super().
3107d7f67ac9SQu Wenruo  *
3108d7f67ac9SQu Wenruo  * This should be called after btrfs_parse_options(), as some mount options
3109d7f67ac9SQu Wenruo  * (space cache related) can modify on-disk format like free space tree and
3110d7f67ac9SQu Wenruo  * screw up certain feature dependencies.
3111d7f67ac9SQu Wenruo  */
btrfs_check_features(struct btrfs_fs_info * fs_info,bool is_rw_mount)31122ba48b20SQu Wenruo int btrfs_check_features(struct btrfs_fs_info *fs_info, bool is_rw_mount)
3113d7f67ac9SQu Wenruo {
3114d7f67ac9SQu Wenruo 	struct btrfs_super_block *disk_super = fs_info->super_copy;
3115d7f67ac9SQu Wenruo 	u64 incompat = btrfs_super_incompat_flags(disk_super);
3116d7f67ac9SQu Wenruo 	const u64 compat_ro = btrfs_super_compat_ro_flags(disk_super);
3117d7f67ac9SQu Wenruo 	const u64 compat_ro_unsupp = (compat_ro & ~BTRFS_FEATURE_COMPAT_RO_SUPP);
3118d7f67ac9SQu Wenruo 
3119d7f67ac9SQu Wenruo 	if (incompat & ~BTRFS_FEATURE_INCOMPAT_SUPP) {
3120d7f67ac9SQu Wenruo 		btrfs_err(fs_info,
3121d7f67ac9SQu Wenruo 		"cannot mount because of unknown incompat features (0x%llx)",
3122d7f67ac9SQu Wenruo 		    incompat);
3123d7f67ac9SQu Wenruo 		return -EINVAL;
3124d7f67ac9SQu Wenruo 	}
3125d7f67ac9SQu Wenruo 
3126d7f67ac9SQu Wenruo 	/* Runtime limitation for mixed block groups. */
3127d7f67ac9SQu Wenruo 	if ((incompat & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) &&
3128d7f67ac9SQu Wenruo 	    (fs_info->sectorsize != fs_info->nodesize)) {
3129d7f67ac9SQu Wenruo 		btrfs_err(fs_info,
3130d7f67ac9SQu Wenruo "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups",
3131d7f67ac9SQu Wenruo 			fs_info->nodesize, fs_info->sectorsize);
3132d7f67ac9SQu Wenruo 		return -EINVAL;
3133d7f67ac9SQu Wenruo 	}
3134d7f67ac9SQu Wenruo 
3135d7f67ac9SQu Wenruo 	/* Mixed backref is an always-enabled feature. */
3136d7f67ac9SQu Wenruo 	incompat |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
3137d7f67ac9SQu Wenruo 
3138d7f67ac9SQu Wenruo 	/* Set compression related flags just in case. */
3139d7f67ac9SQu Wenruo 	if (fs_info->compress_type == BTRFS_COMPRESS_LZO)
3140d7f67ac9SQu Wenruo 		incompat |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
3141d7f67ac9SQu Wenruo 	else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD)
3142d7f67ac9SQu Wenruo 		incompat |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD;
3143d7f67ac9SQu Wenruo 
3144d7f67ac9SQu Wenruo 	/*
3145d7f67ac9SQu Wenruo 	 * An ancient flag, which should really be marked deprecated.
3146d7f67ac9SQu Wenruo 	 * Such runtime limitation doesn't really need a incompat flag.
3147d7f67ac9SQu Wenruo 	 */
3148d7f67ac9SQu Wenruo 	if (btrfs_super_nodesize(disk_super) > PAGE_SIZE)
3149d7f67ac9SQu Wenruo 		incompat |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA;
3150d7f67ac9SQu Wenruo 
31512ba48b20SQu Wenruo 	if (compat_ro_unsupp && is_rw_mount) {
3152d7f67ac9SQu Wenruo 		btrfs_err(fs_info,
3153d7f67ac9SQu Wenruo 	"cannot mount read-write because of unknown compat_ro features (0x%llx)",
3154d7f67ac9SQu Wenruo 		       compat_ro);
3155d7f67ac9SQu Wenruo 		return -EINVAL;
3156d7f67ac9SQu Wenruo 	}
3157d7f67ac9SQu Wenruo 
3158d7f67ac9SQu Wenruo 	/*
3159d7f67ac9SQu Wenruo 	 * We have unsupported RO compat features, although RO mounted, we
3160d7f67ac9SQu Wenruo 	 * should not cause any metadata writes, including log replay.
3161d7f67ac9SQu Wenruo 	 * Or we could screw up whatever the new feature requires.
3162d7f67ac9SQu Wenruo 	 */
3163d7f67ac9SQu Wenruo 	if (compat_ro_unsupp && btrfs_super_log_root(disk_super) &&
3164d7f67ac9SQu Wenruo 	    !btrfs_test_opt(fs_info, NOLOGREPLAY)) {
3165d7f67ac9SQu Wenruo 		btrfs_err(fs_info,
3166d7f67ac9SQu Wenruo "cannot replay dirty log with unsupported compat_ro features (0x%llx), try rescue=nologreplay",
3167d7f67ac9SQu Wenruo 			  compat_ro);
3168d7f67ac9SQu Wenruo 		return -EINVAL;
3169d7f67ac9SQu Wenruo 	}
3170d7f67ac9SQu Wenruo 
3171d7f67ac9SQu Wenruo 	/*
3172d7f67ac9SQu Wenruo 	 * Artificial limitations for block group tree, to force
3173d7f67ac9SQu Wenruo 	 * block-group-tree to rely on no-holes and free-space-tree.
3174d7f67ac9SQu Wenruo 	 */
3175d7f67ac9SQu Wenruo 	if (btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE) &&
3176d7f67ac9SQu Wenruo 	    (!btrfs_fs_incompat(fs_info, NO_HOLES) ||
3177d7f67ac9SQu Wenruo 	     !btrfs_test_opt(fs_info, FREE_SPACE_TREE))) {
3178d7f67ac9SQu Wenruo 		btrfs_err(fs_info,
3179d7f67ac9SQu Wenruo "block-group-tree feature requires no-holes and free-space-tree features");
3180d7f67ac9SQu Wenruo 		return -EINVAL;
3181d7f67ac9SQu Wenruo 	}
3182d7f67ac9SQu Wenruo 
3183d7f67ac9SQu Wenruo 	/*
3184d7f67ac9SQu Wenruo 	 * Subpage runtime limitation on v1 cache.
3185d7f67ac9SQu Wenruo 	 *
3186d7f67ac9SQu Wenruo 	 * V1 space cache still has some hard codeed PAGE_SIZE usage, while
3187d7f67ac9SQu Wenruo 	 * we're already defaulting to v2 cache, no need to bother v1 as it's
3188d7f67ac9SQu Wenruo 	 * going to be deprecated anyway.
3189d7f67ac9SQu Wenruo 	 */
3190d7f67ac9SQu Wenruo 	if (fs_info->sectorsize < PAGE_SIZE && btrfs_test_opt(fs_info, SPACE_CACHE)) {
3191d7f67ac9SQu Wenruo 		btrfs_warn(fs_info,
3192d7f67ac9SQu Wenruo 	"v1 space cache is not supported for page size %lu with sectorsize %u",
3193d7f67ac9SQu Wenruo 			   PAGE_SIZE, fs_info->sectorsize);
3194d7f67ac9SQu Wenruo 		return -EINVAL;
3195d7f67ac9SQu Wenruo 	}
3196d7f67ac9SQu Wenruo 
3197d7f67ac9SQu Wenruo 	/* This can be called by remount, we need to protect the super block. */
3198d7f67ac9SQu Wenruo 	spin_lock(&fs_info->super_lock);
3199d7f67ac9SQu Wenruo 	btrfs_set_super_incompat_flags(disk_super, incompat);
3200d7f67ac9SQu Wenruo 	spin_unlock(&fs_info->super_lock);
3201d7f67ac9SQu Wenruo 
3202d7f67ac9SQu Wenruo 	return 0;
3203d7f67ac9SQu Wenruo }
3204d7f67ac9SQu Wenruo 
open_ctree(struct super_block * sb,struct btrfs_fs_devices * fs_devices,const char * options)3205ae18c37aSJosef Bacik int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_devices,
32062917f741SDavid Sterba 		      const char *options)
3207ae18c37aSJosef Bacik {
3208ae18c37aSJosef Bacik 	u32 sectorsize;
3209ae18c37aSJosef Bacik 	u32 nodesize;
3210ae18c37aSJosef Bacik 	u32 stripesize;
3211ae18c37aSJosef Bacik 	u64 generation;
3212ae18c37aSJosef Bacik 	u16 csum_type;
3213ae18c37aSJosef Bacik 	struct btrfs_super_block *disk_super;
3214ae18c37aSJosef Bacik 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
3215ae18c37aSJosef Bacik 	struct btrfs_root *tree_root;
3216ae18c37aSJosef Bacik 	struct btrfs_root *chunk_root;
3217ae18c37aSJosef Bacik 	int ret;
3218ae18c37aSJosef Bacik 	int level;
3219ae18c37aSJosef Bacik 
32208260edbaSJosef Bacik 	ret = init_mount_fs_info(fs_info, sb);
32214871c33bSQu Wenruo 	if (ret)
3222ae18c37aSJosef Bacik 		goto fail;
322353b381b3SDavid Woodhouse 
3224ae18c37aSJosef Bacik 	/* These need to be init'ed before we start creating inodes and such. */
3225ae18c37aSJosef Bacik 	tree_root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID,
3226ae18c37aSJosef Bacik 				     GFP_KERNEL);
3227ae18c37aSJosef Bacik 	fs_info->tree_root = tree_root;
3228ae18c37aSJosef Bacik 	chunk_root = btrfs_alloc_root(fs_info, BTRFS_CHUNK_TREE_OBJECTID,
3229ae18c37aSJosef Bacik 				      GFP_KERNEL);
3230ae18c37aSJosef Bacik 	fs_info->chunk_root = chunk_root;
3231ae18c37aSJosef Bacik 	if (!tree_root || !chunk_root) {
32324871c33bSQu Wenruo 		ret = -ENOMEM;
3233c75e8394SJosef Bacik 		goto fail;
3234ae18c37aSJosef Bacik 	}
3235ae18c37aSJosef Bacik 
3236dcb2137cSChristoph Hellwig 	ret = btrfs_init_btree_inode(sb);
32374871c33bSQu Wenruo 	if (ret)
3238c75e8394SJosef Bacik 		goto fail;
3239ae18c37aSJosef Bacik 
3240d24fa5c1SAnand Jain 	invalidate_bdev(fs_devices->latest_dev->bdev);
32411104a885SDavid Sterba 
32421104a885SDavid Sterba 	/*
32431104a885SDavid Sterba 	 * Read super block and check the signature bytes only
32441104a885SDavid Sterba 	 */
3245d24fa5c1SAnand Jain 	disk_super = btrfs_read_dev_super(fs_devices->latest_dev->bdev);
32468f32380dSJohannes Thumshirn 	if (IS_ERR(disk_super)) {
32474871c33bSQu Wenruo 		ret = PTR_ERR(disk_super);
324816cdcec7SMiao Xie 		goto fail_alloc;
324920b45077SDave Young 	}
325039279cc3SChris Mason 
32512db31320SQu Wenruo 	btrfs_info(fs_info, "first mount of filesystem %pU", disk_super->fsid);
32521104a885SDavid Sterba 	/*
3253260db43cSRandy Dunlap 	 * Verify the type first, if that or the checksum value are
32548dc3f22cSJohannes Thumshirn 	 * corrupted, we'll find out
32558dc3f22cSJohannes Thumshirn 	 */
32568f32380dSJohannes Thumshirn 	csum_type = btrfs_super_csum_type(disk_super);
325751bce6c9SJohannes Thumshirn 	if (!btrfs_supported_super_csum(csum_type)) {
32588dc3f22cSJohannes Thumshirn 		btrfs_err(fs_info, "unsupported checksum algorithm: %u",
325951bce6c9SJohannes Thumshirn 			  csum_type);
32604871c33bSQu Wenruo 		ret = -EINVAL;
32618f32380dSJohannes Thumshirn 		btrfs_release_disk_super(disk_super);
32628dc3f22cSJohannes Thumshirn 		goto fail_alloc;
32638dc3f22cSJohannes Thumshirn 	}
32648dc3f22cSJohannes Thumshirn 
326583c68bbcSSu Yue 	fs_info->csum_size = btrfs_super_csum_size(disk_super);
326683c68bbcSSu Yue 
32676d97c6e3SJohannes Thumshirn 	ret = btrfs_init_csum_hash(fs_info, csum_type);
32686d97c6e3SJohannes Thumshirn 	if (ret) {
32698f32380dSJohannes Thumshirn 		btrfs_release_disk_super(disk_super);
32706d97c6e3SJohannes Thumshirn 		goto fail_alloc;
32716d97c6e3SJohannes Thumshirn 	}
32726d97c6e3SJohannes Thumshirn 
32738dc3f22cSJohannes Thumshirn 	/*
32741104a885SDavid Sterba 	 * We want to check superblock checksum, the type is stored inside.
32751104a885SDavid Sterba 	 * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k).
32761104a885SDavid Sterba 	 */
32773d17adeaSQu Wenruo 	if (btrfs_check_super_csum(fs_info, disk_super)) {
327805135f59SDavid Sterba 		btrfs_err(fs_info, "superblock checksum mismatch");
32794871c33bSQu Wenruo 		ret = -EINVAL;
32808f32380dSJohannes Thumshirn 		btrfs_release_disk_super(disk_super);
3281141386e1SJosef Bacik 		goto fail_alloc;
32821104a885SDavid Sterba 	}
32831104a885SDavid Sterba 
32841104a885SDavid Sterba 	/*
32851104a885SDavid Sterba 	 * super_copy is zeroed at allocation time and we never touch the
32861104a885SDavid Sterba 	 * following bytes up to INFO_SIZE, the checksum is calculated from
32871104a885SDavid Sterba 	 * the whole block of INFO_SIZE
32881104a885SDavid Sterba 	 */
32898f32380dSJohannes Thumshirn 	memcpy(fs_info->super_copy, disk_super, sizeof(*fs_info->super_copy));
32908f32380dSJohannes Thumshirn 	btrfs_release_disk_super(disk_super);
32915f39d397SChris Mason 
3292fbc6feaeSNikolay Borisov 	disk_super = fs_info->super_copy;
3293fbc6feaeSNikolay Borisov 
3294fbc6feaeSNikolay Borisov 	memcpy(fs_info->super_for_commit, fs_info->super_copy,
3295fbc6feaeSNikolay Borisov 	       sizeof(*fs_info->super_for_commit));
3296de37aa51SNikolay Borisov 
3297069ec957SQu Wenruo 	ret = btrfs_validate_mount_super(fs_info);
32981104a885SDavid Sterba 	if (ret) {
329905135f59SDavid Sterba 		btrfs_err(fs_info, "superblock contains fatal errors");
33004871c33bSQu Wenruo 		ret = -EINVAL;
3301141386e1SJosef Bacik 		goto fail_alloc;
33021104a885SDavid Sterba 	}
33031104a885SDavid Sterba 
33044871c33bSQu Wenruo 	if (!btrfs_super_root(disk_super)) {
33054871c33bSQu Wenruo 		btrfs_err(fs_info, "invalid superblock tree root bytenr");
33064871c33bSQu Wenruo 		ret = -EINVAL;
3307141386e1SJosef Bacik 		goto fail_alloc;
33084871c33bSQu Wenruo 	}
33090f7d52f4SChris Mason 
3310acce952bSliubo 	/* check FS state, whether FS is broken. */
331187533c47SMiao Xie 	if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR)
3312ae3364e5SFilipe Manana 		WRITE_ONCE(fs_info->fs_error, -EUCLEAN);
3313acce952bSliubo 
33146f93e834SAnand Jain 	/* Set up fs_info before parsing mount options */
33156f93e834SAnand Jain 	nodesize = btrfs_super_nodesize(disk_super);
33166f93e834SAnand Jain 	sectorsize = btrfs_super_sectorsize(disk_super);
33176f93e834SAnand Jain 	stripesize = sectorsize;
33186f93e834SAnand Jain 	fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids));
33196f93e834SAnand Jain 	fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids));
33206f93e834SAnand Jain 
33216f93e834SAnand Jain 	fs_info->nodesize = nodesize;
33226f93e834SAnand Jain 	fs_info->sectorsize = sectorsize;
33236f93e834SAnand Jain 	fs_info->sectorsize_bits = ilog2(sectorsize);
3324ce4a71eeSQu Wenruo 	fs_info->sectors_per_page = (PAGE_SIZE >> fs_info->sectorsize_bits);
33256f93e834SAnand Jain 	fs_info->csums_per_leaf = BTRFS_MAX_ITEM_SIZE(fs_info) / fs_info->csum_size;
33266f93e834SAnand Jain 	fs_info->stripesize = stripesize;
33276f93e834SAnand Jain 
3328a6a8f22aSJosef Bacik 	/*
3329a6a8f22aSJosef Bacik 	 * Handle the space caching options appropriately now that we have the
3330a6a8f22aSJosef Bacik 	 * super block loaded and validated.
3331a6a8f22aSJosef Bacik 	 */
3332a6a8f22aSJosef Bacik 	btrfs_set_free_space_cache_settings(fs_info);
3333a6a8f22aSJosef Bacik 
3334ad21f15bSJosef Bacik 	if (!btrfs_check_options(fs_info, &fs_info->mount_opt, sb->s_flags)) {
3335ad21f15bSJosef Bacik 		ret = -EINVAL;
3336141386e1SJosef Bacik 		goto fail_alloc;
3337ad21f15bSJosef Bacik 	}
3338dfe25020SChris Mason 
33392ba48b20SQu Wenruo 	ret = btrfs_check_features(fs_info, !sb_rdonly(sb));
33404871c33bSQu Wenruo 	if (ret < 0)
3341141386e1SJosef Bacik 		goto fail_alloc;
3342f2b636e8SJosef Bacik 
3343ad21f15bSJosef Bacik 	/*
3344ad21f15bSJosef Bacik 	 * At this point our mount options are validated, if we set ->max_inline
3345ad21f15bSJosef Bacik 	 * to something non-standard make sure we truncate it to sectorsize.
3346ad21f15bSJosef Bacik 	 */
3347ad21f15bSJosef Bacik 	fs_info->max_inline = min_t(u64, fs_info->max_inline, fs_info->sectorsize);
3348ad21f15bSJosef Bacik 
3349ce4a71eeSQu Wenruo 	if (sectorsize < PAGE_SIZE)
335095ea0486SQu Wenruo 		btrfs_warn(fs_info,
335195ea0486SQu Wenruo 		"read-write for sector size %u with page size %lu is experimental",
33520bb3eb3eSQu Wenruo 			   sectorsize, PAGE_SIZE);
33530bb3eb3eSQu Wenruo 
3354d21deec5SSu Yue 	ret = btrfs_init_workqueues(fs_info);
33554871c33bSQu Wenruo 	if (ret)
33560dc3b84aSJosef Bacik 		goto fail_sb_buffer;
33574543df7eSChris Mason 
33589e11ceeeSJan Kara 	sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super);
33599e11ceeeSJan Kara 	sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE);
33604575c9ccSChris Mason 
33614e00422eSDavid Sterba 	/* Update the values for the current filesystem. */
3362a061fc8dSChris Mason 	sb->s_blocksize = sectorsize;
3363a061fc8dSChris Mason 	sb->s_blocksize_bits = blksize_bits(sectorsize);
3364de37aa51SNikolay Borisov 	memcpy(&sb->s_uuid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE);
3365db94535dSChris Mason 
3366925baeddSChris Mason 	mutex_lock(&fs_info->chunk_mutex);
33676bccf3abSJeff Mahoney 	ret = btrfs_read_sys_array(fs_info);
3368925baeddSChris Mason 	mutex_unlock(&fs_info->chunk_mutex);
336984eed90fSChris Mason 	if (ret) {
337005135f59SDavid Sterba 		btrfs_err(fs_info, "failed to read the system array: %d", ret);
33715d4f98a2SYan Zheng 		goto fail_sb_buffer;
337284eed90fSChris Mason 	}
33730b86a832SChris Mason 
337484234f3aSYan Zheng 	generation = btrfs_super_chunk_root_generation(disk_super);
3375581c1760SQu Wenruo 	level = btrfs_super_chunk_root_level(disk_super);
3376bd676446SJosef Bacik 	ret = load_super_root(chunk_root, btrfs_super_chunk_root(disk_super),
3377bd676446SJosef Bacik 			      generation, level);
3378bd676446SJosef Bacik 	if (ret) {
337905135f59SDavid Sterba 		btrfs_err(fs_info, "failed to read chunk root");
3380af31f5e5SChris Mason 		goto fail_tree_roots;
338183121942SDavid Woodhouse 	}
33820b86a832SChris Mason 
3383e17cade2SChris Mason 	read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
3384c4ac7541SDavid Sterba 			   offsetof(struct btrfs_header, chunk_tree_uuid),
3385c4ac7541SDavid Sterba 			   BTRFS_UUID_SIZE);
3386e17cade2SChris Mason 
33875b4aacefSJeff Mahoney 	ret = btrfs_read_chunk_tree(fs_info);
33882b82032cSYan Zheng 	if (ret) {
338905135f59SDavid Sterba 		btrfs_err(fs_info, "failed to read chunk tree: %d", ret);
3390af31f5e5SChris Mason 		goto fail_tree_roots;
33912b82032cSYan Zheng 	}
33920b86a832SChris Mason 
33938dabb742SStefan Behrens 	/*
3394bacce86aSAnand Jain 	 * At this point we know all the devices that make this filesystem,
3395bacce86aSAnand Jain 	 * including the seed devices but we don't know yet if the replace
3396bacce86aSAnand Jain 	 * target is required. So free devices that are not part of this
33971a9fd417SDavid Sterba 	 * filesystem but skip the replace target device which is checked
3398bacce86aSAnand Jain 	 * below in btrfs_init_dev_replace().
33998dabb742SStefan Behrens 	 */
3400bacce86aSAnand Jain 	btrfs_free_extra_devids(fs_devices);
3401d24fa5c1SAnand Jain 	if (!fs_devices->latest_dev->bdev) {
340205135f59SDavid Sterba 		btrfs_err(fs_info, "failed to read devices");
34034871c33bSQu Wenruo 		ret = -EIO;
3404a6b0d5c8SChris Mason 		goto fail_tree_roots;
3405a6b0d5c8SChris Mason 	}
3406a6b0d5c8SChris Mason 
3407b8522a1eSNikolay Borisov 	ret = init_tree_roots(fs_info);
34084bbcaa64SEric Sandeen 	if (ret)
3409b8522a1eSNikolay Borisov 		goto fail_tree_roots;
34108929ecfaSYan, Zheng 
341175ec1db8SJosef Bacik 	/*
341273651042SNaohiro Aota 	 * Get zone type information of zoned block devices. This will also
341373651042SNaohiro Aota 	 * handle emulation of a zoned filesystem if a regular device has the
341473651042SNaohiro Aota 	 * zoned incompat feature flag set.
341573651042SNaohiro Aota 	 */
341673651042SNaohiro Aota 	ret = btrfs_get_dev_zone_info_all_devices(fs_info);
341773651042SNaohiro Aota 	if (ret) {
341873651042SNaohiro Aota 		btrfs_err(fs_info,
34194871c33bSQu Wenruo 			  "zoned: failed to read device zone info: %d", ret);
342073651042SNaohiro Aota 		goto fail_block_groups;
342173651042SNaohiro Aota 	}
342273651042SNaohiro Aota 
342373651042SNaohiro Aota 	/*
342475ec1db8SJosef Bacik 	 * If we have a uuid root and we're not being told to rescan we need to
342575ec1db8SJosef Bacik 	 * check the generation here so we can set the
342675ec1db8SJosef Bacik 	 * BTRFS_FS_UPDATE_UUID_TREE_GEN bit.  Otherwise we could commit the
342775ec1db8SJosef Bacik 	 * transaction during a balance or the log replay without updating the
342875ec1db8SJosef Bacik 	 * uuid generation, and then if we crash we would rescan the uuid tree,
342975ec1db8SJosef Bacik 	 * even though it was perfectly fine.
343075ec1db8SJosef Bacik 	 */
343175ec1db8SJosef Bacik 	if (fs_info->uuid_root && !btrfs_test_opt(fs_info, RESCAN_UUID_TREE) &&
343275ec1db8SJosef Bacik 	    fs_info->generation == btrfs_super_uuid_tree_generation(disk_super))
343375ec1db8SJosef Bacik 		set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
343475ec1db8SJosef Bacik 
3435cf90d884SQu Wenruo 	ret = btrfs_verify_dev_extents(fs_info);
3436cf90d884SQu Wenruo 	if (ret) {
3437cf90d884SQu Wenruo 		btrfs_err(fs_info,
3438cf90d884SQu Wenruo 			  "failed to verify dev extents against chunks: %d",
3439cf90d884SQu Wenruo 			  ret);
3440cf90d884SQu Wenruo 		goto fail_block_groups;
3441cf90d884SQu Wenruo 	}
344268310a5eSIlya Dryomov 	ret = btrfs_recover_balance(fs_info);
344368310a5eSIlya Dryomov 	if (ret) {
344405135f59SDavid Sterba 		btrfs_err(fs_info, "failed to recover balance: %d", ret);
344568310a5eSIlya Dryomov 		goto fail_block_groups;
344668310a5eSIlya Dryomov 	}
344768310a5eSIlya Dryomov 
3448733f4fbbSStefan Behrens 	ret = btrfs_init_dev_stats(fs_info);
3449733f4fbbSStefan Behrens 	if (ret) {
345005135f59SDavid Sterba 		btrfs_err(fs_info, "failed to init dev_stats: %d", ret);
3451733f4fbbSStefan Behrens 		goto fail_block_groups;
3452733f4fbbSStefan Behrens 	}
3453733f4fbbSStefan Behrens 
34548dabb742SStefan Behrens 	ret = btrfs_init_dev_replace(fs_info);
34558dabb742SStefan Behrens 	if (ret) {
345605135f59SDavid Sterba 		btrfs_err(fs_info, "failed to init dev_replace: %d", ret);
34578dabb742SStefan Behrens 		goto fail_block_groups;
34588dabb742SStefan Behrens 	}
34598dabb742SStefan Behrens 
3460b70f5097SNaohiro Aota 	ret = btrfs_check_zoned_mode(fs_info);
3461b70f5097SNaohiro Aota 	if (ret) {
3462b70f5097SNaohiro Aota 		btrfs_err(fs_info, "failed to initialize zoned mode: %d",
3463b70f5097SNaohiro Aota 			  ret);
3464b70f5097SNaohiro Aota 		goto fail_block_groups;
3465b70f5097SNaohiro Aota 	}
3466b70f5097SNaohiro Aota 
3467c6761a9eSAnand Jain 	ret = btrfs_sysfs_add_fsid(fs_devices);
3468b7c35e81SAnand Jain 	if (ret) {
346905135f59SDavid Sterba 		btrfs_err(fs_info, "failed to init sysfs fsid interface: %d",
347005135f59SDavid Sterba 				ret);
3471b7c35e81SAnand Jain 		goto fail_block_groups;
3472b7c35e81SAnand Jain 	}
3473b7c35e81SAnand Jain 
347496f3136eSAnand Jain 	ret = btrfs_sysfs_add_mounted(fs_info);
34755ac1d209SJeff Mahoney 	if (ret) {
347605135f59SDavid Sterba 		btrfs_err(fs_info, "failed to init sysfs interface: %d", ret);
3477b7c35e81SAnand Jain 		goto fail_fsdev_sysfs;
34785ac1d209SJeff Mahoney 	}
34795ac1d209SJeff Mahoney 
3480c59021f8Sliubo 	ret = btrfs_init_space_info(fs_info);
3481c59021f8Sliubo 	if (ret) {
348205135f59SDavid Sterba 		btrfs_err(fs_info, "failed to initialize space info: %d", ret);
34832365dd3cSAnand Jain 		goto fail_sysfs;
3484c59021f8Sliubo 	}
3485c59021f8Sliubo 
34865b4aacefSJeff Mahoney 	ret = btrfs_read_block_groups(fs_info);
34871b1d1f66SJosef Bacik 	if (ret) {
348805135f59SDavid Sterba 		btrfs_err(fs_info, "failed to read block groups: %d", ret);
34892365dd3cSAnand Jain 		goto fail_sysfs;
34901b1d1f66SJosef Bacik 	}
34914330e183SQu Wenruo 
349216beac87SNaohiro Aota 	btrfs_free_zone_cache(fs_info);
349316beac87SNaohiro Aota 
3494a7e1ac7bSNaohiro Aota 	btrfs_check_active_zone_reservation(fs_info);
3495a7e1ac7bSNaohiro Aota 
34965c78a5e7SAnand Jain 	if (!sb_rdonly(sb) && fs_info->fs_devices->missing_devices &&
34975c78a5e7SAnand Jain 	    !btrfs_check_rw_degradable(fs_info, NULL)) {
349805135f59SDavid Sterba 		btrfs_warn(fs_info,
349952042d8eSAndrea Gelmini 		"writable mount is not allowed due to too many missing devices");
35004871c33bSQu Wenruo 		ret = -EINVAL;
35012365dd3cSAnand Jain 		goto fail_sysfs;
3502292fd7fcSStefan Behrens 	}
35039078a3e1SChris Mason 
350433c44184SJosef Bacik 	fs_info->cleaner_kthread = kthread_run(cleaner_kthread, fs_info,
3505a74a4b97SChris Mason 					       "btrfs-cleaner");
35064871c33bSQu Wenruo 	if (IS_ERR(fs_info->cleaner_kthread)) {
35074871c33bSQu Wenruo 		ret = PTR_ERR(fs_info->cleaner_kthread);
35082365dd3cSAnand Jain 		goto fail_sysfs;
35094871c33bSQu Wenruo 	}
3510a74a4b97SChris Mason 
3511a74a4b97SChris Mason 	fs_info->transaction_kthread = kthread_run(transaction_kthread,
3512a74a4b97SChris Mason 						   tree_root,
3513a74a4b97SChris Mason 						   "btrfs-transaction");
35144871c33bSQu Wenruo 	if (IS_ERR(fs_info->transaction_kthread)) {
35154871c33bSQu Wenruo 		ret = PTR_ERR(fs_info->transaction_kthread);
35163f157a2fSChris Mason 		goto fail_cleaner;
35174871c33bSQu Wenruo 	}
3518a74a4b97SChris Mason 
3519bcef60f2SArne Jansen 	ret = btrfs_read_qgroup_config(fs_info);
3520bcef60f2SArne Jansen 	if (ret)
3521bcef60f2SArne Jansen 		goto fail_trans_kthread;
352221adbd5cSStefan Behrens 
3523fd708b81SJosef Bacik 	if (btrfs_build_ref_tree(fs_info))
3524fd708b81SJosef Bacik 		btrfs_err(fs_info, "couldn't build ref tree");
3525fd708b81SJosef Bacik 
352696da0919SQu Wenruo 	/* do not make disk changes in broken FS or nologreplay is given */
352796da0919SQu Wenruo 	if (btrfs_super_log_root(disk_super) != 0 &&
35280b246afaSJeff Mahoney 	    !btrfs_test_opt(fs_info, NOLOGREPLAY)) {
3529e8294f2fSDavid Sterba 		btrfs_info(fs_info, "start tree-log replay");
353063443bf5SEric Sandeen 		ret = btrfs_replay_log(fs_info, fs_devices);
35314871c33bSQu Wenruo 		if (ret)
353228c16cbbSWang Shilong 			goto fail_qgroup;
3533e556ce2cSYan Zheng 	}
35341a40e23bSZheng Yan 
353556e9357aSDavid Sterba 	fs_info->fs_root = btrfs_get_fs_root(fs_info, BTRFS_FS_TREE_OBJECTID, true);
35363140c9a3SDan Carpenter 	if (IS_ERR(fs_info->fs_root)) {
35374871c33bSQu Wenruo 		ret = PTR_ERR(fs_info->fs_root);
35384871c33bSQu Wenruo 		btrfs_warn(fs_info, "failed to read fs tree: %d", ret);
3539315bf8efSJosef Bacik 		fs_info->fs_root = NULL;
3540bcef60f2SArne Jansen 		goto fail_qgroup;
35413140c9a3SDan Carpenter 	}
3542c289811cSChris Mason 
3543bc98a42cSDavid Howells 	if (sb_rdonly(sb))
354483e3a40aSJosef Bacik 		return 0;
35452b6ba629SIlya Dryomov 
354644c0ca21SBoris Burkov 	ret = btrfs_start_pre_rw_mount(fs_info);
35472b6ba629SIlya Dryomov 	if (ret) {
35486bccf3abSJeff Mahoney 		close_ctree(fs_info);
35492b6ba629SIlya Dryomov 		return ret;
3550e3acc2a6SJosef Bacik 	}
3551b0643e59SDennis Zhou 	btrfs_discard_resume(fs_info);
3552b382a324SJan Schmidt 
355344c0ca21SBoris Burkov 	if (fs_info->uuid_root &&
355444c0ca21SBoris Burkov 	    (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) ||
355544c0ca21SBoris Burkov 	     fs_info->generation != btrfs_super_uuid_tree_generation(disk_super))) {
355605135f59SDavid Sterba 		btrfs_info(fs_info, "checking UUID tree");
355770f80175SStefan Behrens 		ret = btrfs_check_uuid_tree(fs_info);
355870f80175SStefan Behrens 		if (ret) {
355905135f59SDavid Sterba 			btrfs_warn(fs_info,
356005135f59SDavid Sterba 				"failed to check the UUID tree: %d", ret);
35616bccf3abSJeff Mahoney 			close_ctree(fs_info);
356270f80175SStefan Behrens 			return ret;
356370f80175SStefan Behrens 		}
3564f7a81ea4SStefan Behrens 	}
356594846229SBoris Burkov 
3566afcdd129SJosef Bacik 	set_bit(BTRFS_FS_OPEN, &fs_info->flags);
356747ab2a6cSJosef Bacik 
3568b4be6aefSJosef Bacik 	/* Kick the cleaner thread so it'll start deleting snapshots. */
3569b4be6aefSJosef Bacik 	if (test_bit(BTRFS_FS_UNFINISHED_DROPS, &fs_info->flags))
3570b4be6aefSJosef Bacik 		wake_up_process(fs_info->cleaner_kthread);
3571b4be6aefSJosef Bacik 
3572ad2b2c80SAl Viro 	return 0;
357339279cc3SChris Mason 
3574bcef60f2SArne Jansen fail_qgroup:
3575bcef60f2SArne Jansen 	btrfs_free_qgroup_config(fs_info);
35767c2ca468SChris Mason fail_trans_kthread:
35777c2ca468SChris Mason 	kthread_stop(fs_info->transaction_kthread);
35782ff7e61eSJeff Mahoney 	btrfs_cleanup_transaction(fs_info);
3579faa2dbf0SJosef Bacik 	btrfs_free_fs_roots(fs_info);
35803f157a2fSChris Mason fail_cleaner:
3581a74a4b97SChris Mason 	kthread_stop(fs_info->cleaner_kthread);
35827c2ca468SChris Mason 
35837c2ca468SChris Mason 	/*
35847c2ca468SChris Mason 	 * make sure we're done with the btree inode before we stop our
35857c2ca468SChris Mason 	 * kthreads
35867c2ca468SChris Mason 	 */
35877c2ca468SChris Mason 	filemap_write_and_wait(fs_info->btree_inode->i_mapping);
35887c2ca468SChris Mason 
35892365dd3cSAnand Jain fail_sysfs:
35906618a59bSAnand Jain 	btrfs_sysfs_remove_mounted(fs_info);
35912365dd3cSAnand Jain 
3592b7c35e81SAnand Jain fail_fsdev_sysfs:
3593b7c35e81SAnand Jain 	btrfs_sysfs_remove_fsid(fs_info->fs_devices);
3594b7c35e81SAnand Jain 
35951b1d1f66SJosef Bacik fail_block_groups:
359654067ae9SJosef Bacik 	btrfs_put_block_group_cache(fs_info);
3597af31f5e5SChris Mason 
3598af31f5e5SChris Mason fail_tree_roots:
35999e3aa805SJosef Bacik 	if (fs_info->data_reloc_root)
36009e3aa805SJosef Bacik 		btrfs_drop_and_free_fs_root(fs_info, fs_info->data_reloc_root);
36014273eaffSAnand Jain 	free_root_pointers(fs_info, true);
36022b8195bbSMiao Xie 	invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
3603af31f5e5SChris Mason 
360439279cc3SChris Mason fail_sb_buffer:
36057abadb64SLiu Bo 	btrfs_stop_all_workers(fs_info);
36065cdd7db6SFilipe Manana 	btrfs_free_block_groups(fs_info);
360716cdcec7SMiao Xie fail_alloc:
36087dc66abbSFilipe Manana 	btrfs_mapping_tree_free(fs_info);
3609586e46e2SIlya Dryomov 
36104543df7eSChris Mason 	iput(fs_info->btree_inode);
36117e662854SQinghuang Feng fail:
3612586e46e2SIlya Dryomov 	btrfs_close_devices(fs_info->fs_devices);
36134871c33bSQu Wenruo 	ASSERT(ret < 0);
36144871c33bSQu Wenruo 	return ret;
3615eb60ceacSChris Mason }
3616663faf9fSMasami Hiramatsu ALLOW_ERROR_INJECTION(open_ctree, ERRNO);
3617eb60ceacSChris Mason 
btrfs_end_super_write(struct bio * bio)3618314b6dd0SJohannes Thumshirn static void btrfs_end_super_write(struct bio *bio)
3619f2984462SChris Mason {
3620314b6dd0SJohannes Thumshirn 	struct btrfs_device *device = bio->bi_private;
3621617fb10eSMatthew Wilcox (Oracle) 	struct folio_iter fi;
3622442a4f63SStefan Behrens 
3623617fb10eSMatthew Wilcox (Oracle) 	bio_for_each_folio_all(fi, bio) {
3624314b6dd0SJohannes Thumshirn 		if (bio->bi_status) {
3625fb456252SJeff Mahoney 			btrfs_warn_rl_in_rcu(device->fs_info,
3626617fb10eSMatthew Wilcox (Oracle) 				"lost super block write due to IO error on %s (%d)",
3627cb3e217bSQu Wenruo 				btrfs_dev_name(device),
3628314b6dd0SJohannes Thumshirn 				blk_status_to_errno(bio->bi_status));
3629314b6dd0SJohannes Thumshirn 			btrfs_dev_stat_inc_and_print(device,
3630314b6dd0SJohannes Thumshirn 						     BTRFS_DEV_STAT_WRITE_ERRS);
3631bc00965dSMatthew Wilcox (Oracle) 			/* Ensure failure if the primary sb fails. */
3632bc00965dSMatthew Wilcox (Oracle) 			if (bio->bi_opf & REQ_FUA)
3633bc00965dSMatthew Wilcox (Oracle) 				atomic_add(BTRFS_SUPER_PRIMARY_WRITE_ERROR,
3634bc00965dSMatthew Wilcox (Oracle) 					   &device->sb_write_errors);
3635bc00965dSMatthew Wilcox (Oracle) 			else
3636bc00965dSMatthew Wilcox (Oracle) 				atomic_inc(&device->sb_write_errors);
3637f2984462SChris Mason 		}
3638617fb10eSMatthew Wilcox (Oracle) 		folio_unlock(fi.folio);
3639617fb10eSMatthew Wilcox (Oracle) 		folio_put(fi.folio);
3640314b6dd0SJohannes Thumshirn 	}
3641314b6dd0SJohannes Thumshirn 
3642314b6dd0SJohannes Thumshirn 	bio_put(bio);
3643f2984462SChris Mason }
3644f2984462SChris Mason 
btrfs_read_dev_one_super(struct block_device * bdev,int copy_num,bool drop_cache)36458f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_one_super(struct block_device *bdev,
3646a05d3c91SQu Wenruo 						   int copy_num, bool drop_cache)
364729c36d72SAnand Jain {
364829c36d72SAnand Jain 	struct btrfs_super_block *super;
36498f32380dSJohannes Thumshirn 	struct page *page;
365012659251SNaohiro Aota 	u64 bytenr, bytenr_orig;
3651224941e8SAl Viro 	struct address_space *mapping = bdev->bd_mapping;
365212659251SNaohiro Aota 	int ret;
365329c36d72SAnand Jain 
365412659251SNaohiro Aota 	bytenr_orig = btrfs_sb_offset(copy_num);
365512659251SNaohiro Aota 	ret = btrfs_sb_log_location_bdev(bdev, copy_num, READ, &bytenr);
365612659251SNaohiro Aota 	if (ret == -ENOENT)
365712659251SNaohiro Aota 		return ERR_PTR(-EINVAL);
365812659251SNaohiro Aota 	else if (ret)
365912659251SNaohiro Aota 		return ERR_PTR(ret);
366012659251SNaohiro Aota 
3661cda00ebaSChristoph Hellwig 	if (bytenr + BTRFS_SUPER_INFO_SIZE >= bdev_nr_bytes(bdev))
36628f32380dSJohannes Thumshirn 		return ERR_PTR(-EINVAL);
366329c36d72SAnand Jain 
3664a05d3c91SQu Wenruo 	if (drop_cache) {
3665a05d3c91SQu Wenruo 		/* This should only be called with the primary sb. */
3666a05d3c91SQu Wenruo 		ASSERT(copy_num == 0);
3667a05d3c91SQu Wenruo 
3668a05d3c91SQu Wenruo 		/*
3669a05d3c91SQu Wenruo 		 * Drop the page of the primary superblock, so later read will
3670a05d3c91SQu Wenruo 		 * always read from the device.
3671a05d3c91SQu Wenruo 		 */
3672a05d3c91SQu Wenruo 		invalidate_inode_pages2_range(mapping,
3673a05d3c91SQu Wenruo 				bytenr >> PAGE_SHIFT,
3674a05d3c91SQu Wenruo 				(bytenr + BTRFS_SUPER_INFO_SIZE) >> PAGE_SHIFT);
3675a05d3c91SQu Wenruo 	}
3676a05d3c91SQu Wenruo 
36778f32380dSJohannes Thumshirn 	page = read_cache_page_gfp(mapping, bytenr >> PAGE_SHIFT, GFP_NOFS);
36788f32380dSJohannes Thumshirn 	if (IS_ERR(page))
36798f32380dSJohannes Thumshirn 		return ERR_CAST(page);
368029c36d72SAnand Jain 
36818f32380dSJohannes Thumshirn 	super = page_address(page);
368296c2e067SAnand Jain 	if (btrfs_super_magic(super) != BTRFS_MAGIC) {
368396c2e067SAnand Jain 		btrfs_release_disk_super(super);
368496c2e067SAnand Jain 		return ERR_PTR(-ENODATA);
368596c2e067SAnand Jain 	}
368696c2e067SAnand Jain 
368712659251SNaohiro Aota 	if (btrfs_super_bytenr(super) != bytenr_orig) {
36888f32380dSJohannes Thumshirn 		btrfs_release_disk_super(super);
36898f32380dSJohannes Thumshirn 		return ERR_PTR(-EINVAL);
369029c36d72SAnand Jain 	}
369129c36d72SAnand Jain 
36928f32380dSJohannes Thumshirn 	return super;
369329c36d72SAnand Jain }
369429c36d72SAnand Jain 
369529c36d72SAnand Jain 
btrfs_read_dev_super(struct block_device * bdev)36968f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_super(struct block_device *bdev)
3697a512bbf8SYan Zheng {
36988f32380dSJohannes Thumshirn 	struct btrfs_super_block *super, *latest = NULL;
3699a512bbf8SYan Zheng 	int i;
3700a512bbf8SYan Zheng 	u64 transid = 0;
3701a512bbf8SYan Zheng 
3702a512bbf8SYan Zheng 	/* we would like to check all the supers, but that would make
3703a512bbf8SYan Zheng 	 * a btrfs mount succeed after a mkfs from a different FS.
3704a512bbf8SYan Zheng 	 * So, we need to add a special mount option to scan for
3705a512bbf8SYan Zheng 	 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
3706a512bbf8SYan Zheng 	 */
3707a512bbf8SYan Zheng 	for (i = 0; i < 1; i++) {
3708a05d3c91SQu Wenruo 		super = btrfs_read_dev_one_super(bdev, i, false);
37098f32380dSJohannes Thumshirn 		if (IS_ERR(super))
3710a512bbf8SYan Zheng 			continue;
3711a512bbf8SYan Zheng 
3712a512bbf8SYan Zheng 		if (!latest || btrfs_super_generation(super) > transid) {
37138f32380dSJohannes Thumshirn 			if (latest)
37148f32380dSJohannes Thumshirn 				btrfs_release_disk_super(super);
37158f32380dSJohannes Thumshirn 
37168f32380dSJohannes Thumshirn 			latest = super;
3717a512bbf8SYan Zheng 			transid = btrfs_super_generation(super);
3718a512bbf8SYan Zheng 		}
3719a512bbf8SYan Zheng 	}
372092fc03fbSAnand Jain 
37218f32380dSJohannes Thumshirn 	return super;
3722a512bbf8SYan Zheng }
3723a512bbf8SYan Zheng 
37244eedeb75SHisashi Hifumi /*
3725abbb3b8eSDavid Sterba  * Write superblock @sb to the @device. Do not wait for completion, all the
3726f93ee0dfSMatthew Wilcox (Oracle)  * folios we use for writing are locked.
37274eedeb75SHisashi Hifumi  *
3728abbb3b8eSDavid Sterba  * Write @max_mirrors copies of the superblock, where 0 means default that fit
3729abbb3b8eSDavid Sterba  * the expected device size at commit time. Note that max_mirrors must be
3730abbb3b8eSDavid Sterba  * same for write and wait phases.
37314eedeb75SHisashi Hifumi  *
3732f93ee0dfSMatthew Wilcox (Oracle)  * Return number of errors when folio is not found or submission fails.
37334eedeb75SHisashi Hifumi  */
write_dev_supers(struct btrfs_device * device,struct btrfs_super_block * sb,int max_mirrors)3734a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device,
3735abbb3b8eSDavid Sterba 			    struct btrfs_super_block *sb, int max_mirrors)
3736a512bbf8SYan Zheng {
3737d5178578SJohannes Thumshirn 	struct btrfs_fs_info *fs_info = device->fs_info;
3738224941e8SAl Viro 	struct address_space *mapping = device->bdev->bd_mapping;
3739d5178578SJohannes Thumshirn 	SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
3740a512bbf8SYan Zheng 	int i;
374112659251SNaohiro Aota 	int ret;
374212659251SNaohiro Aota 	u64 bytenr, bytenr_orig;
3743a512bbf8SYan Zheng 
3744bc00965dSMatthew Wilcox (Oracle) 	atomic_set(&device->sb_write_errors, 0);
3745bc00965dSMatthew Wilcox (Oracle) 
3746a512bbf8SYan Zheng 	if (max_mirrors == 0)
3747a512bbf8SYan Zheng 		max_mirrors = BTRFS_SUPER_MIRROR_MAX;
3748a512bbf8SYan Zheng 
3749d5178578SJohannes Thumshirn 	shash->tfm = fs_info->csum_shash;
3750d5178578SJohannes Thumshirn 
3751a512bbf8SYan Zheng 	for (i = 0; i < max_mirrors; i++) {
3752f93ee0dfSMatthew Wilcox (Oracle) 		struct folio *folio;
3753314b6dd0SJohannes Thumshirn 		struct bio *bio;
3754314b6dd0SJohannes Thumshirn 		struct btrfs_super_block *disk_super;
3755f93ee0dfSMatthew Wilcox (Oracle) 		size_t offset;
3756314b6dd0SJohannes Thumshirn 
375712659251SNaohiro Aota 		bytenr_orig = btrfs_sb_offset(i);
375812659251SNaohiro Aota 		ret = btrfs_sb_log_location(device, i, WRITE, &bytenr);
375912659251SNaohiro Aota 		if (ret == -ENOENT) {
376012659251SNaohiro Aota 			continue;
376112659251SNaohiro Aota 		} else if (ret < 0) {
376212659251SNaohiro Aota 			btrfs_err(device->fs_info,
376312659251SNaohiro Aota 				"couldn't get super block location for mirror %d",
376412659251SNaohiro Aota 				i);
3765bc00965dSMatthew Wilcox (Oracle) 			atomic_inc(&device->sb_write_errors);
376612659251SNaohiro Aota 			continue;
376712659251SNaohiro Aota 		}
3768935e5cc9SMiao Xie 		if (bytenr + BTRFS_SUPER_INFO_SIZE >=
3769935e5cc9SMiao Xie 		    device->commit_total_bytes)
3770a512bbf8SYan Zheng 			break;
3771a512bbf8SYan Zheng 
377212659251SNaohiro Aota 		btrfs_set_super_bytenr(sb, bytenr_orig);
3773a512bbf8SYan Zheng 
3774fd08001fSEric Biggers 		crypto_shash_digest(shash, (const char *)sb + BTRFS_CSUM_SIZE,
3775fd08001fSEric Biggers 				    BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE,
3776fd08001fSEric Biggers 				    sb->csum);
3777a512bbf8SYan Zheng 
3778f93ee0dfSMatthew Wilcox (Oracle) 		folio = __filemap_get_folio(mapping, bytenr >> PAGE_SHIFT,
3779f93ee0dfSMatthew Wilcox (Oracle) 					    FGP_LOCK | FGP_ACCESSED | FGP_CREAT,
3780314b6dd0SJohannes Thumshirn 					    GFP_NOFS);
3781f93ee0dfSMatthew Wilcox (Oracle) 		if (IS_ERR(folio)) {
3782fb456252SJeff Mahoney 			btrfs_err(device->fs_info,
3783314b6dd0SJohannes Thumshirn 			    "couldn't get super block page for bytenr %llu",
3784f14d104dSDavid Sterba 			    bytenr);
3785bc00965dSMatthew Wilcox (Oracle) 			atomic_inc(&device->sb_write_errors);
3786634554dcSJosef Bacik 			continue;
3787634554dcSJosef Bacik 		}
3788f93ee0dfSMatthew Wilcox (Oracle) 		ASSERT(folio_order(folio) == 0);
3789634554dcSJosef Bacik 
3790f93ee0dfSMatthew Wilcox (Oracle) 		offset = offset_in_folio(folio, bytenr);
3791f93ee0dfSMatthew Wilcox (Oracle) 		disk_super = folio_address(folio) + offset;
3792314b6dd0SJohannes Thumshirn 		memcpy(disk_super, sb, BTRFS_SUPER_INFO_SIZE);
3793a512bbf8SYan Zheng 
3794387125fcSChris Mason 		/*
3795314b6dd0SJohannes Thumshirn 		 * Directly use bios here instead of relying on the page cache
3796314b6dd0SJohannes Thumshirn 		 * to do I/O, so we don't lose the ability to do integrity
3797314b6dd0SJohannes Thumshirn 		 * checking.
3798387125fcSChris Mason 		 */
379907888c66SChristoph Hellwig 		bio = bio_alloc(device->bdev, 1,
380007888c66SChristoph Hellwig 				REQ_OP_WRITE | REQ_SYNC | REQ_META | REQ_PRIO,
380107888c66SChristoph Hellwig 				GFP_NOFS);
3802314b6dd0SJohannes Thumshirn 		bio->bi_iter.bi_sector = bytenr >> SECTOR_SHIFT;
3803314b6dd0SJohannes Thumshirn 		bio->bi_private = device;
3804314b6dd0SJohannes Thumshirn 		bio->bi_end_io = btrfs_end_super_write;
3805f93ee0dfSMatthew Wilcox (Oracle) 		bio_add_folio_nofail(bio, folio, BTRFS_SUPER_INFO_SIZE, offset);
3806314b6dd0SJohannes Thumshirn 
3807314b6dd0SJohannes Thumshirn 		/*
3808314b6dd0SJohannes Thumshirn 		 * We FUA only the first super block.  The others we allow to
3809314b6dd0SJohannes Thumshirn 		 * go down lazy and there's a short window where the on-disk
3810314b6dd0SJohannes Thumshirn 		 * copies might still contain the older version.
3811314b6dd0SJohannes Thumshirn 		 */
38121b9e619cSOmar Sandoval 		if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER))
3813314b6dd0SJohannes Thumshirn 			bio->bi_opf |= REQ_FUA;
381458ff51f1SChristoph Hellwig 		submit_bio(bio);
38158376d9e1SNaohiro Aota 
38168376d9e1SNaohiro Aota 		if (btrfs_advance_sb_log(device, i))
3817bc00965dSMatthew Wilcox (Oracle) 			atomic_inc(&device->sb_write_errors);
3818a512bbf8SYan Zheng 	}
3819bc00965dSMatthew Wilcox (Oracle) 	return atomic_read(&device->sb_write_errors) < i ? 0 : -1;
3820a512bbf8SYan Zheng }
3821a512bbf8SYan Zheng 
3822387125fcSChris Mason /*
3823abbb3b8eSDavid Sterba  * Wait for write completion of superblocks done by write_dev_supers,
3824abbb3b8eSDavid Sterba  * @max_mirrors same for write and wait phases.
3825abbb3b8eSDavid Sterba  *
3826bc00965dSMatthew Wilcox (Oracle)  * Return -1 if primary super block write failed or when there were no super block
3827bc00965dSMatthew Wilcox (Oracle)  * copies written. Otherwise 0.
3828abbb3b8eSDavid Sterba  */
wait_dev_supers(struct btrfs_device * device,int max_mirrors)3829abbb3b8eSDavid Sterba static int wait_dev_supers(struct btrfs_device *device, int max_mirrors)
3830abbb3b8eSDavid Sterba {
3831abbb3b8eSDavid Sterba 	int i;
3832abbb3b8eSDavid Sterba 	int errors = 0;
3833b6a535faSHoward McLauchlan 	bool primary_failed = false;
383412659251SNaohiro Aota 	int ret;
3835abbb3b8eSDavid Sterba 	u64 bytenr;
3836abbb3b8eSDavid Sterba 
3837abbb3b8eSDavid Sterba 	if (max_mirrors == 0)
3838abbb3b8eSDavid Sterba 		max_mirrors = BTRFS_SUPER_MIRROR_MAX;
3839abbb3b8eSDavid Sterba 
3840abbb3b8eSDavid Sterba 	for (i = 0; i < max_mirrors; i++) {
3841c94b7349SMatthew Wilcox (Oracle) 		struct folio *folio;
3842314b6dd0SJohannes Thumshirn 
384312659251SNaohiro Aota 		ret = btrfs_sb_log_location(device, i, READ, &bytenr);
384412659251SNaohiro Aota 		if (ret == -ENOENT) {
384512659251SNaohiro Aota 			break;
384612659251SNaohiro Aota 		} else if (ret < 0) {
384712659251SNaohiro Aota 			errors++;
384812659251SNaohiro Aota 			if (i == 0)
384912659251SNaohiro Aota 				primary_failed = true;
385012659251SNaohiro Aota 			continue;
385112659251SNaohiro Aota 		}
3852abbb3b8eSDavid Sterba 		if (bytenr + BTRFS_SUPER_INFO_SIZE >=
3853abbb3b8eSDavid Sterba 		    device->commit_total_bytes)
3854abbb3b8eSDavid Sterba 			break;
3855abbb3b8eSDavid Sterba 
385638da32eeSLinus Torvalds 		folio = filemap_get_folio(device->bdev->bd_mapping,
3857314b6dd0SJohannes Thumshirn 					  bytenr >> PAGE_SHIFT);
3858bc00965dSMatthew Wilcox (Oracle) 		/* If the folio has been removed, then we know it completed. */
3859bc00965dSMatthew Wilcox (Oracle) 		if (IS_ERR(folio))
3860abbb3b8eSDavid Sterba 			continue;
3861c94b7349SMatthew Wilcox (Oracle) 		ASSERT(folio_order(folio) == 0);
3862c94b7349SMatthew Wilcox (Oracle) 
3863c94b7349SMatthew Wilcox (Oracle) 		/* Folio will be unlocked once the write completes. */
3864c94b7349SMatthew Wilcox (Oracle) 		folio_wait_locked(folio);
3865bc00965dSMatthew Wilcox (Oracle) 		folio_put(folio);
3866bc00965dSMatthew Wilcox (Oracle) 	}
3867bc00965dSMatthew Wilcox (Oracle) 
3868bc00965dSMatthew Wilcox (Oracle) 	errors += atomic_read(&device->sb_write_errors);
3869bc00965dSMatthew Wilcox (Oracle) 	if (errors >= BTRFS_SUPER_PRIMARY_WRITE_ERROR)
3870b6a535faSHoward McLauchlan 		primary_failed = true;
3871b6a535faSHoward McLauchlan 	if (primary_failed) {
3872b6a535faSHoward McLauchlan 		btrfs_err(device->fs_info, "error writing primary super block to device %llu",
3873b6a535faSHoward McLauchlan 			  device->devid);
3874b6a535faSHoward McLauchlan 		return -1;
3875b6a535faSHoward McLauchlan 	}
3876b6a535faSHoward McLauchlan 
3877abbb3b8eSDavid Sterba 	return errors < i ? 0 : -1;
3878abbb3b8eSDavid Sterba }
3879abbb3b8eSDavid Sterba 
3880abbb3b8eSDavid Sterba /*
3881387125fcSChris Mason  * endio for the write_dev_flush, this will wake anyone waiting
3882387125fcSChris Mason  * for the barrier when it is done
3883387125fcSChris Mason  */
btrfs_end_empty_barrier(struct bio * bio)38844246a0b6SChristoph Hellwig static void btrfs_end_empty_barrier(struct bio *bio)
3885387125fcSChris Mason {
3886f9e69aa9SChristoph Hellwig 	bio_uninit(bio);
3887387125fcSChris Mason 	complete(bio->bi_private);
3888387125fcSChris Mason }
3889387125fcSChris Mason 
3890387125fcSChris Mason /*
38914fc6441aSAnand Jain  * Submit a flush request to the device if it supports it. Error handling is
38924fc6441aSAnand Jain  * done in the waiting counterpart.
3893387125fcSChris Mason  */
write_dev_flush(struct btrfs_device * device)38944fc6441aSAnand Jain static void write_dev_flush(struct btrfs_device *device)
3895387125fcSChris Mason {
3896f9e69aa9SChristoph Hellwig 	struct bio *bio = &device->flush_bio;
3897387125fcSChris Mason 
3898bfd3ea94SAnand Jain 	device->last_flush_error = BLK_STS_OK;
3899bfd3ea94SAnand Jain 
3900f9e69aa9SChristoph Hellwig 	bio_init(bio, device->bdev, NULL, 0,
3901f9e69aa9SChristoph Hellwig 		 REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH);
3902387125fcSChris Mason 	bio->bi_end_io = btrfs_end_empty_barrier;
3903387125fcSChris Mason 	init_completion(&device->flush_wait);
3904387125fcSChris Mason 	bio->bi_private = &device->flush_wait;
390558ff51f1SChristoph Hellwig 	submit_bio(bio);
39061c3063b6SAnand Jain 	set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state);
39074fc6441aSAnand Jain }
3908387125fcSChris Mason 
39094fc6441aSAnand Jain /*
39104fc6441aSAnand Jain  * If the flush bio has been submitted by write_dev_flush, wait for it.
39111b465784SAnand Jain  * Return true for any error, and false otherwise.
39124fc6441aSAnand Jain  */
wait_dev_flush(struct btrfs_device * device)39131b465784SAnand Jain static bool wait_dev_flush(struct btrfs_device *device)
39144fc6441aSAnand Jain {
3915f9e69aa9SChristoph Hellwig 	struct bio *bio = &device->flush_bio;
39164fc6441aSAnand Jain 
39177e812f20SAnand Jain 	if (!test_and_clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state))
39181b465784SAnand Jain 		return false;
39194fc6441aSAnand Jain 
39202980d574SDavid Sterba 	wait_for_completion_io(&device->flush_wait);
39214fc6441aSAnand Jain 
3922bfd3ea94SAnand Jain 	if (bio->bi_status) {
3923bfd3ea94SAnand Jain 		device->last_flush_error = bio->bi_status;
3924bfd3ea94SAnand Jain 		btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_FLUSH_ERRS);
39251b465784SAnand Jain 		return true;
3926bfd3ea94SAnand Jain 	}
3927bfd3ea94SAnand Jain 
39281b465784SAnand Jain 	return false;
3929387125fcSChris Mason }
3930387125fcSChris Mason 
3931387125fcSChris Mason /*
3932387125fcSChris Mason  * send an empty flush down to each device in parallel,
3933387125fcSChris Mason  * then wait for them
3934387125fcSChris Mason  */
barrier_all_devices(struct btrfs_fs_info * info)3935387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info)
3936387125fcSChris Mason {
3937387125fcSChris Mason 	struct list_head *head;
3938387125fcSChris Mason 	struct btrfs_device *dev;
39395af3e8ccSStefan Behrens 	int errors_wait = 0;
3940387125fcSChris Mason 
39411538e6c5SDavid Sterba 	lockdep_assert_held(&info->fs_devices->device_list_mutex);
3942387125fcSChris Mason 	/* send down all the barriers */
3943387125fcSChris Mason 	head = &info->fs_devices->devices;
39441538e6c5SDavid Sterba 	list_for_each_entry(dev, head, dev_list) {
3945e6e674bdSAnand Jain 		if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
3946f88ba6a2SHidetoshi Seto 			continue;
3947cea7c8bfSAnand Jain 		if (!dev->bdev)
3948387125fcSChris Mason 			continue;
3949e12c9621SAnand Jain 		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
3950ebbede42SAnand Jain 		    !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
3951387125fcSChris Mason 			continue;
3952387125fcSChris Mason 
39534fc6441aSAnand Jain 		write_dev_flush(dev);
3954387125fcSChris Mason 	}
3955387125fcSChris Mason 
3956387125fcSChris Mason 	/* wait for all the barriers */
39571538e6c5SDavid Sterba 	list_for_each_entry(dev, head, dev_list) {
3958e6e674bdSAnand Jain 		if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
3959f88ba6a2SHidetoshi Seto 			continue;
3960387125fcSChris Mason 		if (!dev->bdev) {
39615af3e8ccSStefan Behrens 			errors_wait++;
3962387125fcSChris Mason 			continue;
3963387125fcSChris Mason 		}
3964e12c9621SAnand Jain 		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
3965ebbede42SAnand Jain 		    !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
3966387125fcSChris Mason 			continue;
3967387125fcSChris Mason 
39681b465784SAnand Jain 		if (wait_dev_flush(dev))
39695af3e8ccSStefan Behrens 			errors_wait++;
3970387125fcSChris Mason 	}
3971401b41e5SAnand Jain 
3972401b41e5SAnand Jain 	/*
3973de38a206SAnand Jain 	 * Checks last_flush_error of disks in order to determine the device
3974de38a206SAnand Jain 	 * state.
3975401b41e5SAnand Jain 	 */
3976de38a206SAnand Jain 	if (errors_wait && !btrfs_check_rw_degradable(info, NULL))
3977de38a206SAnand Jain 		return -EIO;
3978de38a206SAnand Jain 
3979387125fcSChris Mason 	return 0;
3980387125fcSChris Mason }
3981387125fcSChris Mason 
btrfs_get_num_tolerated_disk_barrier_failures(u64 flags)3982943c6e99SZhao Lei int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags)
3983943c6e99SZhao Lei {
39848789f4feSZhao Lei 	int raid_type;
39858789f4feSZhao Lei 	int min_tolerated = INT_MAX;
3986943c6e99SZhao Lei 
39878789f4feSZhao Lei 	if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 ||
39888789f4feSZhao Lei 	    (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE))
39898c3e3582SDavid Sterba 		min_tolerated = min_t(int, min_tolerated,
39908789f4feSZhao Lei 				    btrfs_raid_array[BTRFS_RAID_SINGLE].
39918789f4feSZhao Lei 				    tolerated_failures);
3992943c6e99SZhao Lei 
39938789f4feSZhao Lei 	for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
39948789f4feSZhao Lei 		if (raid_type == BTRFS_RAID_SINGLE)
39958789f4feSZhao Lei 			continue;
399641a6e891SAnand Jain 		if (!(flags & btrfs_raid_array[raid_type].bg_flag))
39978789f4feSZhao Lei 			continue;
39988c3e3582SDavid Sterba 		min_tolerated = min_t(int, min_tolerated,
39998789f4feSZhao Lei 				    btrfs_raid_array[raid_type].
40008789f4feSZhao Lei 				    tolerated_failures);
40018789f4feSZhao Lei 	}
4002943c6e99SZhao Lei 
40038789f4feSZhao Lei 	if (min_tolerated == INT_MAX) {
4004ab8d0fc4SJeff Mahoney 		pr_warn("BTRFS: unknown raid flag: %llu", flags);
40058789f4feSZhao Lei 		min_tolerated = 0;
40068789f4feSZhao Lei 	}
40078789f4feSZhao Lei 
40088789f4feSZhao Lei 	return min_tolerated;
4009943c6e99SZhao Lei }
4010943c6e99SZhao Lei 
write_all_supers(struct btrfs_fs_info * fs_info,int max_mirrors)4011eece6a9cSDavid Sterba int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
4012f2984462SChris Mason {
4013e5e9a520SChris Mason 	struct list_head *head;
4014f2984462SChris Mason 	struct btrfs_device *dev;
4015a061fc8dSChris Mason 	struct btrfs_super_block *sb;
4016f2984462SChris Mason 	struct btrfs_dev_item *dev_item;
4017f2984462SChris Mason 	int ret;
4018f2984462SChris Mason 	int do_barriers;
4019a236aed1SChris Mason 	int max_errors;
4020a236aed1SChris Mason 	int total_errors = 0;
4021a061fc8dSChris Mason 	u64 flags;
4022f2984462SChris Mason 
40230b246afaSJeff Mahoney 	do_barriers = !btrfs_test_opt(fs_info, NOBARRIER);
4024fed3b381SLiu Bo 
4025fed3b381SLiu Bo 	/*
4026fed3b381SLiu Bo 	 * max_mirrors == 0 indicates we're from commit_transaction,
4027fed3b381SLiu Bo 	 * not from fsync where the tree roots in fs_info have not
4028fed3b381SLiu Bo 	 * been consistent on disk.
4029fed3b381SLiu Bo 	 */
4030fed3b381SLiu Bo 	if (max_mirrors == 0)
40310b246afaSJeff Mahoney 		backup_super_roots(fs_info);
4032f2984462SChris Mason 
40330b246afaSJeff Mahoney 	sb = fs_info->super_for_commit;
4034a061fc8dSChris Mason 	dev_item = &sb->dev_item;
4035e5e9a520SChris Mason 
40360b246afaSJeff Mahoney 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
40370b246afaSJeff Mahoney 	head = &fs_info->fs_devices->devices;
40380b246afaSJeff Mahoney 	max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1;
4039387125fcSChris Mason 
40405af3e8ccSStefan Behrens 	if (do_barriers) {
40410b246afaSJeff Mahoney 		ret = barrier_all_devices(fs_info);
40425af3e8ccSStefan Behrens 		if (ret) {
40435af3e8ccSStefan Behrens 			mutex_unlock(
40440b246afaSJeff Mahoney 				&fs_info->fs_devices->device_list_mutex);
40450b246afaSJeff Mahoney 			btrfs_handle_fs_error(fs_info, ret,
40465af3e8ccSStefan Behrens 					      "errors while submitting device barriers.");
40475af3e8ccSStefan Behrens 			return ret;
40485af3e8ccSStefan Behrens 		}
40495af3e8ccSStefan Behrens 	}
4050387125fcSChris Mason 
40511538e6c5SDavid Sterba 	list_for_each_entry(dev, head, dev_list) {
4052dfe25020SChris Mason 		if (!dev->bdev) {
4053dfe25020SChris Mason 			total_errors++;
4054dfe25020SChris Mason 			continue;
4055dfe25020SChris Mason 		}
4056e12c9621SAnand Jain 		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
4057ebbede42SAnand Jain 		    !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
4058dfe25020SChris Mason 			continue;
4059dfe25020SChris Mason 
40602b82032cSYan Zheng 		btrfs_set_stack_device_generation(dev_item, 0);
4061a061fc8dSChris Mason 		btrfs_set_stack_device_type(dev_item, dev->type);
4062a061fc8dSChris Mason 		btrfs_set_stack_device_id(dev_item, dev->devid);
40637df69d3eSMiao Xie 		btrfs_set_stack_device_total_bytes(dev_item,
4064935e5cc9SMiao Xie 						   dev->commit_total_bytes);
4065ce7213c7SMiao Xie 		btrfs_set_stack_device_bytes_used(dev_item,
4066ce7213c7SMiao Xie 						  dev->commit_bytes_used);
4067a061fc8dSChris Mason 		btrfs_set_stack_device_io_align(dev_item, dev->io_align);
4068a061fc8dSChris Mason 		btrfs_set_stack_device_io_width(dev_item, dev->io_width);
4069a061fc8dSChris Mason 		btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
4070a061fc8dSChris Mason 		memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
40717239ff4bSNikolay Borisov 		memcpy(dev_item->fsid, dev->fs_devices->metadata_uuid,
40727239ff4bSNikolay Borisov 		       BTRFS_FSID_SIZE);
4073a512bbf8SYan Zheng 
4074a061fc8dSChris Mason 		flags = btrfs_super_flags(sb);
4075a061fc8dSChris Mason 		btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
4076f2984462SChris Mason 
407775cb857dSQu Wenruo 		ret = btrfs_validate_write_super(fs_info, sb);
407875cb857dSQu Wenruo 		if (ret < 0) {
407975cb857dSQu Wenruo 			mutex_unlock(&fs_info->fs_devices->device_list_mutex);
408075cb857dSQu Wenruo 			btrfs_handle_fs_error(fs_info, -EUCLEAN,
408175cb857dSQu Wenruo 				"unexpected superblock corruption detected");
408275cb857dSQu Wenruo 			return -EUCLEAN;
408375cb857dSQu Wenruo 		}
408475cb857dSQu Wenruo 
4085abbb3b8eSDavid Sterba 		ret = write_dev_supers(dev, sb, max_mirrors);
4086a236aed1SChris Mason 		if (ret)
4087a236aed1SChris Mason 			total_errors++;
4088f2984462SChris Mason 	}
4089a236aed1SChris Mason 	if (total_errors > max_errors) {
40900b246afaSJeff Mahoney 		btrfs_err(fs_info, "%d errors while writing supers",
4091d397712bSChris Mason 			  total_errors);
40920b246afaSJeff Mahoney 		mutex_unlock(&fs_info->fs_devices->device_list_mutex);
409379787eaaSJeff Mahoney 
40949d565ba4SStefan Behrens 		/* FUA is masked off if unsupported and can't be the reason */
40950b246afaSJeff Mahoney 		btrfs_handle_fs_error(fs_info, -EIO,
40960b246afaSJeff Mahoney 				      "%d errors while writing supers",
40970b246afaSJeff Mahoney 				      total_errors);
40989d565ba4SStefan Behrens 		return -EIO;
4099a236aed1SChris Mason 	}
4100f2984462SChris Mason 
4101a512bbf8SYan Zheng 	total_errors = 0;
41021538e6c5SDavid Sterba 	list_for_each_entry(dev, head, dev_list) {
4103dfe25020SChris Mason 		if (!dev->bdev)
4104dfe25020SChris Mason 			continue;
4105e12c9621SAnand Jain 		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
4106ebbede42SAnand Jain 		    !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
4107dfe25020SChris Mason 			continue;
4108dfe25020SChris Mason 
4109abbb3b8eSDavid Sterba 		ret = wait_dev_supers(dev, max_mirrors);
4110a512bbf8SYan Zheng 		if (ret)
41111259ab75SChris Mason 			total_errors++;
4112f2984462SChris Mason 	}
41130b246afaSJeff Mahoney 	mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4114a236aed1SChris Mason 	if (total_errors > max_errors) {
41150b246afaSJeff Mahoney 		btrfs_handle_fs_error(fs_info, -EIO,
41160b246afaSJeff Mahoney 				      "%d errors while writing supers",
41170b246afaSJeff Mahoney 				      total_errors);
411879787eaaSJeff Mahoney 		return -EIO;
4119a236aed1SChris Mason 	}
4120f2984462SChris Mason 	return 0;
4121f2984462SChris Mason }
4122f2984462SChris Mason 
4123cb517eabSMiao Xie /* Drop a fs root from the radix tree and free it. */
btrfs_drop_and_free_fs_root(struct btrfs_fs_info * fs_info,struct btrfs_root * root)4124cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info,
4125cb517eabSMiao Xie 				  struct btrfs_root *root)
41262619ba1fSChris Mason {
41274785e24fSJosef Bacik 	bool drop_ref = false;
41284785e24fSJosef Bacik 
4129fc7cbcd4SDavid Sterba 	spin_lock(&fs_info->fs_roots_radix_lock);
4130fc7cbcd4SDavid Sterba 	radix_tree_delete(&fs_info->fs_roots_radix,
4131e094f480SJosef Bacik 			  (unsigned long)btrfs_root_id(root));
4132fc7cbcd4SDavid Sterba 	if (test_and_clear_bit(BTRFS_ROOT_IN_RADIX, &root->state))
41334785e24fSJosef Bacik 		drop_ref = true;
4134fc7cbcd4SDavid Sterba 	spin_unlock(&fs_info->fs_roots_radix_lock);
413576dda93cSYan, Zheng 
413684961539SJosef Bacik 	if (BTRFS_FS_ERROR(fs_info)) {
4137ef67963dSJosef Bacik 		ASSERT(root->log_root == NULL);
41381c1ea4f7SLiu Bo 		if (root->reloc_root) {
413900246528SJosef Bacik 			btrfs_put_root(root->reloc_root);
41401c1ea4f7SLiu Bo 			root->reloc_root = NULL;
41411c1ea4f7SLiu Bo 		}
41421c1ea4f7SLiu Bo 	}
41433321719eSLiu Bo 
41444785e24fSJosef Bacik 	if (drop_ref)
414500246528SJosef Bacik 		btrfs_put_root(root);
41462619ba1fSChris Mason }
41472619ba1fSChris Mason 
btrfs_commit_super(struct btrfs_fs_info * fs_info)41486bccf3abSJeff Mahoney int btrfs_commit_super(struct btrfs_fs_info *fs_info)
4149c146afadSYan Zheng {
41500b246afaSJeff Mahoney 	mutex_lock(&fs_info->cleaner_mutex);
41512ff7e61eSJeff Mahoney 	btrfs_run_delayed_iputs(fs_info);
41520b246afaSJeff Mahoney 	mutex_unlock(&fs_info->cleaner_mutex);
41530b246afaSJeff Mahoney 	wake_up_process(fs_info->cleaner_kthread);
4154c71bf099SYan, Zheng 
4155c71bf099SYan, Zheng 	/* wait until ongoing cleanup work done */
41560b246afaSJeff Mahoney 	down_write(&fs_info->cleanup_work_sem);
41570b246afaSJeff Mahoney 	up_write(&fs_info->cleanup_work_sem);
4158c71bf099SYan, Zheng 
4159ded980ebSFilipe Manana 	return btrfs_commit_current_transaction(fs_info->tree_root);
4160c146afadSYan Zheng }
4161c146afadSYan Zheng 
warn_about_uncommitted_trans(struct btrfs_fs_info * fs_info)416236c86a9eSQu Wenruo static void warn_about_uncommitted_trans(struct btrfs_fs_info *fs_info)
416336c86a9eSQu Wenruo {
416436c86a9eSQu Wenruo 	struct btrfs_transaction *trans;
416536c86a9eSQu Wenruo 	struct btrfs_transaction *tmp;
416636c86a9eSQu Wenruo 	bool found = false;
416736c86a9eSQu Wenruo 
416836c86a9eSQu Wenruo 	/*
416936c86a9eSQu Wenruo 	 * This function is only called at the very end of close_ctree(),
417036c86a9eSQu Wenruo 	 * thus no other running transaction, no need to take trans_lock.
417136c86a9eSQu Wenruo 	 */
417236c86a9eSQu Wenruo 	ASSERT(test_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags));
417336c86a9eSQu Wenruo 	list_for_each_entry_safe(trans, tmp, &fs_info->trans_list, list) {
417436c86a9eSQu Wenruo 		struct extent_state *cached = NULL;
417536c86a9eSQu Wenruo 		u64 dirty_bytes = 0;
417636c86a9eSQu Wenruo 		u64 cur = 0;
417736c86a9eSQu Wenruo 		u64 found_start;
417836c86a9eSQu Wenruo 		u64 found_end;
417936c86a9eSQu Wenruo 
418036c86a9eSQu Wenruo 		found = true;
4181e5860f82SFilipe Manana 		while (find_first_extent_bit(&trans->dirty_pages, cur,
418236c86a9eSQu Wenruo 			&found_start, &found_end, EXTENT_DIRTY, &cached)) {
418336c86a9eSQu Wenruo 			dirty_bytes += found_end + 1 - found_start;
418436c86a9eSQu Wenruo 			cur = found_end + 1;
418536c86a9eSQu Wenruo 		}
418636c86a9eSQu Wenruo 		btrfs_warn(fs_info,
418736c86a9eSQu Wenruo 	"transaction %llu (with %llu dirty metadata bytes) is not committed",
418836c86a9eSQu Wenruo 			   trans->transid, dirty_bytes);
418936c86a9eSQu Wenruo 		btrfs_cleanup_one_transaction(trans, fs_info);
419036c86a9eSQu Wenruo 
419136c86a9eSQu Wenruo 		if (trans == fs_info->running_transaction)
419236c86a9eSQu Wenruo 			fs_info->running_transaction = NULL;
419336c86a9eSQu Wenruo 		list_del_init(&trans->list);
419436c86a9eSQu Wenruo 
419536c86a9eSQu Wenruo 		btrfs_put_transaction(trans);
419636c86a9eSQu Wenruo 		trace_btrfs_transaction_commit(fs_info);
419736c86a9eSQu Wenruo 	}
419836c86a9eSQu Wenruo 	ASSERT(!found);
419936c86a9eSQu Wenruo }
420036c86a9eSQu Wenruo 
close_ctree(struct btrfs_fs_info * fs_info)4201b105e927SDavid Sterba void __cold close_ctree(struct btrfs_fs_info *fs_info)
4202eb60ceacSChris Mason {
4203c146afadSYan Zheng 	int ret;
4204e089f05cSChris Mason 
4205afcdd129SJosef Bacik 	set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags);
420631e70e52SFilipe Manana 
420731e70e52SFilipe Manana 	/*
42088a1f1e3dSFilipe Manana 	 * If we had UNFINISHED_DROPS we could still be processing them, so
42098a1f1e3dSFilipe Manana 	 * clear that bit and wake up relocation so it can stop.
42108a1f1e3dSFilipe Manana 	 * We must do this before stopping the block group reclaim task, because
42118a1f1e3dSFilipe Manana 	 * at btrfs_relocate_block_group() we wait for this bit, and after the
42128a1f1e3dSFilipe Manana 	 * wait we stop with -EINTR if btrfs_fs_closing() returns non-zero - we
42138a1f1e3dSFilipe Manana 	 * have just set BTRFS_FS_CLOSING_START, so btrfs_fs_closing() will
42148a1f1e3dSFilipe Manana 	 * return 1.
42158a1f1e3dSFilipe Manana 	 */
42168a1f1e3dSFilipe Manana 	btrfs_wake_unfinished_drop(fs_info);
42178a1f1e3dSFilipe Manana 
42188a1f1e3dSFilipe Manana 	/*
421931e70e52SFilipe Manana 	 * We may have the reclaim task running and relocating a data block group,
422031e70e52SFilipe Manana 	 * in which case it may create delayed iputs. So stop it before we park
422131e70e52SFilipe Manana 	 * the cleaner kthread otherwise we can get new delayed iputs after
422231e70e52SFilipe Manana 	 * parking the cleaner, and that can make the async reclaim task to hang
422331e70e52SFilipe Manana 	 * if it's waiting for delayed iputs to complete, since the cleaner is
422431e70e52SFilipe Manana 	 * parked and can not run delayed iputs - this will make us hang when
422531e70e52SFilipe Manana 	 * trying to stop the async reclaim task.
422631e70e52SFilipe Manana 	 */
422731e70e52SFilipe Manana 	cancel_work_sync(&fs_info->reclaim_bgs_work);
4228d6fd0ae2SOmar Sandoval 	/*
4229d6fd0ae2SOmar Sandoval 	 * We don't want the cleaner to start new transactions, add more delayed
4230d6fd0ae2SOmar Sandoval 	 * iputs, etc. while we're closing. We can't use kthread_stop() yet
4231d6fd0ae2SOmar Sandoval 	 * because that frees the task_struct, and the transaction kthread might
4232d6fd0ae2SOmar Sandoval 	 * still try to wake up the cleaner.
4233d6fd0ae2SOmar Sandoval 	 */
4234d6fd0ae2SOmar Sandoval 	kthread_park(fs_info->cleaner_kthread);
4235a2135011SChris Mason 
42367343dd61SJustin Maggard 	/* wait for the qgroup rescan worker to stop */
4237d06f23d6SJeff Mahoney 	btrfs_qgroup_wait_for_completion(fs_info, false);
42387343dd61SJustin Maggard 
4239803b2f54SStefan Behrens 	/* wait for the uuid_scan task to finish */
4240803b2f54SStefan Behrens 	down(&fs_info->uuid_tree_rescan_sem);
4241803b2f54SStefan Behrens 	/* avoid complains from lockdep et al., set sem back to initial state */
4242803b2f54SStefan Behrens 	up(&fs_info->uuid_tree_rescan_sem);
4243803b2f54SStefan Behrens 
4244837d5b6eSIlya Dryomov 	/* pause restriper - we want to resume on mount */
4245aa1b8cd4SStefan Behrens 	btrfs_pause_balance(fs_info);
4246837d5b6eSIlya Dryomov 
42478dabb742SStefan Behrens 	btrfs_dev_replace_suspend_for_unmount(fs_info);
42488dabb742SStefan Behrens 
4249aa1b8cd4SStefan Behrens 	btrfs_scrub_cancel(fs_info);
42504cb5300bSChris Mason 
42514cb5300bSChris Mason 	/* wait for any defraggers to finish */
42524cb5300bSChris Mason 	wait_event(fs_info->transaction_wait,
42534cb5300bSChris Mason 		   (atomic_read(&fs_info->defrag_running) == 0));
42544cb5300bSChris Mason 
42554cb5300bSChris Mason 	/* clear out the rbtree of defraggable inodes */
425626176e7cSMiao Xie 	btrfs_cleanup_defrag_inodes(fs_info);
42574cb5300bSChris Mason 
4258a362bb86SFilipe Manana 	/*
4259a362bb86SFilipe Manana 	 * After we parked the cleaner kthread, ordered extents may have
4260a362bb86SFilipe Manana 	 * completed and created new delayed iputs. If one of the async reclaim
4261a362bb86SFilipe Manana 	 * tasks is running and in the RUN_DELAYED_IPUTS flush state, then we
4262a362bb86SFilipe Manana 	 * can hang forever trying to stop it, because if a delayed iput is
4263a362bb86SFilipe Manana 	 * added after it ran btrfs_run_delayed_iputs() and before it called
4264a362bb86SFilipe Manana 	 * btrfs_wait_on_delayed_iputs(), it will hang forever since there is
4265a362bb86SFilipe Manana 	 * no one else to run iputs.
4266a362bb86SFilipe Manana 	 *
4267a362bb86SFilipe Manana 	 * So wait for all ongoing ordered extents to complete and then run
4268a362bb86SFilipe Manana 	 * delayed iputs. This works because once we reach this point no one
4269a362bb86SFilipe Manana 	 * can either create new ordered extents nor create delayed iputs
4270a362bb86SFilipe Manana 	 * through some other means.
4271a362bb86SFilipe Manana 	 *
4272a362bb86SFilipe Manana 	 * Also note that btrfs_wait_ordered_roots() is not safe here, because
4273a362bb86SFilipe Manana 	 * it waits for BTRFS_ORDERED_COMPLETE to be set on an ordered extent,
4274a362bb86SFilipe Manana 	 * but the delayed iput for the respective inode is made only when doing
4275a362bb86SFilipe Manana 	 * the final btrfs_put_ordered_extent() (which must happen at
4276a362bb86SFilipe Manana 	 * btrfs_finish_ordered_io() when we are unmounting).
4277a362bb86SFilipe Manana 	 */
4278a362bb86SFilipe Manana 	btrfs_flush_workqueue(fs_info->endio_write_workers);
4279a362bb86SFilipe Manana 	/* Ordered extents for free space inodes. */
4280a362bb86SFilipe Manana 	btrfs_flush_workqueue(fs_info->endio_freespace_worker);
4281a362bb86SFilipe Manana 	btrfs_run_delayed_iputs(fs_info);
4282a362bb86SFilipe Manana 
428321c7e756SMiao Xie 	cancel_work_sync(&fs_info->async_reclaim_work);
428457056740SJosef Bacik 	cancel_work_sync(&fs_info->async_data_reclaim_work);
4285576fa348SJosef Bacik 	cancel_work_sync(&fs_info->preempt_reclaim_work);
428621c7e756SMiao Xie 
4287b0643e59SDennis Zhou 	/* Cancel or finish ongoing discard work */
4288b0643e59SDennis Zhou 	btrfs_discard_cleanup(fs_info);
4289b0643e59SDennis Zhou 
4290bc98a42cSDavid Howells 	if (!sb_rdonly(fs_info->sb)) {
4291e44163e1SJeff Mahoney 		/*
4292d6fd0ae2SOmar Sandoval 		 * The cleaner kthread is stopped, so do one final pass over
4293d6fd0ae2SOmar Sandoval 		 * unused block groups.
4294e44163e1SJeff Mahoney 		 */
42950b246afaSJeff Mahoney 		btrfs_delete_unused_bgs(fs_info);
4296e44163e1SJeff Mahoney 
4297f0cc2cd7SFilipe Manana 		/*
4298f0cc2cd7SFilipe Manana 		 * There might be existing delayed inode workers still running
4299f0cc2cd7SFilipe Manana 		 * and holding an empty delayed inode item. We must wait for
4300f0cc2cd7SFilipe Manana 		 * them to complete first because they can create a transaction.
4301f0cc2cd7SFilipe Manana 		 * This happens when someone calls btrfs_balance_delayed_items()
4302f0cc2cd7SFilipe Manana 		 * and then a transaction commit runs the same delayed nodes
4303f0cc2cd7SFilipe Manana 		 * before any delayed worker has done something with the nodes.
4304f0cc2cd7SFilipe Manana 		 * We must wait for any worker here and not at transaction
4305f0cc2cd7SFilipe Manana 		 * commit time since that could cause a deadlock.
4306f0cc2cd7SFilipe Manana 		 * This is a very rare case.
4307f0cc2cd7SFilipe Manana 		 */
4308f0cc2cd7SFilipe Manana 		btrfs_flush_workqueue(fs_info->delayed_workers);
4309f0cc2cd7SFilipe Manana 
43106bccf3abSJeff Mahoney 		ret = btrfs_commit_super(fs_info);
4311d397712bSChris Mason 		if (ret)
431204892340SEric Sandeen 			btrfs_err(fs_info, "commit super ret %d", ret);
4313c146afadSYan Zheng 	}
4314ed2ff2cbSChris Mason 
431584961539SJosef Bacik 	if (BTRFS_FS_ERROR(fs_info))
43162ff7e61eSJeff Mahoney 		btrfs_error_commit_super(fs_info);
4317acce952bSliubo 
4318e3029d9fSAl Viro 	kthread_stop(fs_info->transaction_kthread);
4319e3029d9fSAl Viro 	kthread_stop(fs_info->cleaner_kthread);
43208929ecfaSYan, Zheng 
4321e187831eSJosef Bacik 	ASSERT(list_empty(&fs_info->delayed_iputs));
4322afcdd129SJosef Bacik 	set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags);
4323f25784b3SYan Zheng 
43245958253cSQu Wenruo 	if (btrfs_check_quota_leak(fs_info)) {
43255958253cSQu Wenruo 		WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
43265958253cSQu Wenruo 		btrfs_err(fs_info, "qgroup reserved space leaked");
43275958253cSQu Wenruo 	}
43285958253cSQu Wenruo 
432904892340SEric Sandeen 	btrfs_free_qgroup_config(fs_info);
4330fe816d0fSNikolay Borisov 	ASSERT(list_empty(&fs_info->delalloc_roots));
4331bcef60f2SArne Jansen 
4332963d678bSMiao Xie 	if (percpu_counter_sum(&fs_info->delalloc_bytes)) {
433304892340SEric Sandeen 		btrfs_info(fs_info, "at unmount delalloc count %lld",
4334963d678bSMiao Xie 		       percpu_counter_sum(&fs_info->delalloc_bytes));
4335b0c68f8bSChris Mason 	}
433631153d81SYan Zheng 
43375deb17e1SJosef Bacik 	if (percpu_counter_sum(&fs_info->ordered_bytes))
43384297ff84SJosef Bacik 		btrfs_info(fs_info, "at unmount dio bytes count %lld",
43395deb17e1SJosef Bacik 			   percpu_counter_sum(&fs_info->ordered_bytes));
43404297ff84SJosef Bacik 
43416618a59bSAnand Jain 	btrfs_sysfs_remove_mounted(fs_info);
4342b7c35e81SAnand Jain 	btrfs_sysfs_remove_fsid(fs_info->fs_devices);
43435ac1d209SJeff Mahoney 
43441a4319ccSLiu Bo 	btrfs_put_block_group_cache(fs_info);
43451a4319ccSLiu Bo 
4346de348ee0SWang Shilong 	/*
4347de348ee0SWang Shilong 	 * we must make sure there is not any read request to
4348de348ee0SWang Shilong 	 * submit after we stopping all workers.
4349de348ee0SWang Shilong 	 */
4350de348ee0SWang Shilong 	invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
435196192499SJosef Bacik 	btrfs_stop_all_workers(fs_info);
435296192499SJosef Bacik 
43530a31daa4SFilipe Manana 	/* We shouldn't have any transaction open at this point */
435436c86a9eSQu Wenruo 	warn_about_uncommitted_trans(fs_info);
43550a31daa4SFilipe Manana 
4356afcdd129SJosef Bacik 	clear_bit(BTRFS_FS_OPEN, &fs_info->flags);
43574273eaffSAnand Jain 	free_root_pointers(fs_info, true);
43588c38938cSJosef Bacik 	btrfs_free_fs_roots(fs_info);
43599ad6b7bcSChris Mason 
43604e19443dSJosef Bacik 	/*
43614e19443dSJosef Bacik 	 * We must free the block groups after dropping the fs_roots as we could
43624e19443dSJosef Bacik 	 * have had an IO error and have left over tree log blocks that aren't
43634e19443dSJosef Bacik 	 * cleaned up until the fs roots are freed.  This makes the block group
43644e19443dSJosef Bacik 	 * accounting appear to be wrong because there's pending reserved bytes,
43654e19443dSJosef Bacik 	 * so make sure we do the block group cleanup afterwards.
43664e19443dSJosef Bacik 	 */
43674e19443dSJosef Bacik 	btrfs_free_block_groups(fs_info);
43684e19443dSJosef Bacik 
436913e6c37bSJosef Bacik 	iput(fs_info->btree_inode);
4370d6bfde87SChris Mason 
43717dc66abbSFilipe Manana 	btrfs_mapping_tree_free(fs_info);
437268c94e55SNikolay Borisov 	btrfs_close_devices(fs_info->fs_devices);
4373eb60ceacSChris Mason }
4374eb60ceacSChris Mason 
btrfs_mark_buffer_dirty(struct btrfs_trans_handle * trans,struct extent_buffer * buf)437550564b65SFilipe Manana void btrfs_mark_buffer_dirty(struct btrfs_trans_handle *trans,
437650564b65SFilipe Manana 			     struct extent_buffer *buf)
43775f39d397SChris Mason {
43782f4d60dfSQu Wenruo 	struct btrfs_fs_info *fs_info = buf->fs_info;
43795f39d397SChris Mason 	u64 transid = btrfs_header_generation(buf);
4380b4ce94deSChris Mason 
438106ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
438206ea65a3SJosef Bacik 	/*
438306ea65a3SJosef Bacik 	 * This is a fast path so only do this check if we have sanity tests
438452042d8eSAndrea Gelmini 	 * enabled.  Normal people shouldn't be using unmapped buffers as dirty
438506ea65a3SJosef Bacik 	 * outside of the sanity tests.
438606ea65a3SJosef Bacik 	 */
4387b0132a3bSNikolay Borisov 	if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags)))
438806ea65a3SJosef Bacik 		return;
438906ea65a3SJosef Bacik #endif
439050564b65SFilipe Manana 	/* This is an active transaction (its state < TRANS_STATE_UNBLOCKED). */
439150564b65SFilipe Manana 	ASSERT(trans->transid == fs_info->generation);
439249d0c642SFilipe Manana 	btrfs_assert_tree_write_locked(buf);
43934ebe8d47SFilipe Manana 	if (unlikely(transid != fs_info->generation)) {
439450564b65SFilipe Manana 		btrfs_abort_transaction(trans, -EUCLEAN);
439520cbe460SFilipe Manana 		btrfs_crit(fs_info,
439620cbe460SFilipe Manana "dirty buffer transid mismatch, logical %llu found transid %llu running transid %llu",
439720cbe460SFilipe Manana 			   buf->start, transid, fs_info->generation);
439850564b65SFilipe Manana 	}
4399f18cc978SChristoph Hellwig 	set_extent_buffer_dirty(buf);
4400eb60ceacSChris Mason }
4401eb60ceacSChris Mason 
__btrfs_btree_balance_dirty(struct btrfs_fs_info * fs_info,int flush_delayed)44022ff7e61eSJeff Mahoney static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info,
4403b53d3f5dSLiu Bo 					int flush_delayed)
440435b7e476SChris Mason {
4405188de649SChris Mason 	/*
4406188de649SChris Mason 	 * looks as though older kernels can get into trouble with
4407188de649SChris Mason 	 * this code, they end up stuck in balance_dirty_pages forever
4408188de649SChris Mason 	 */
4409e2d84521SMiao Xie 	int ret;
4410d6bfde87SChris Mason 
44116933c02eSJens Axboe 	if (current->flags & PF_MEMALLOC)
4412d6bfde87SChris Mason 		return;
4413d6bfde87SChris Mason 
4414b53d3f5dSLiu Bo 	if (flush_delayed)
44152ff7e61eSJeff Mahoney 		btrfs_balance_delayed_items(fs_info);
441616cdcec7SMiao Xie 
4417d814a491SEthan Lien 	ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes,
4418d814a491SEthan Lien 				     BTRFS_DIRTY_METADATA_THRESH,
4419d814a491SEthan Lien 				     fs_info->dirty_metadata_batch);
4420e2d84521SMiao Xie 	if (ret > 0) {
44210b246afaSJeff Mahoney 		balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping);
442216cdcec7SMiao Xie 	}
442316cdcec7SMiao Xie }
442416cdcec7SMiao Xie 
btrfs_btree_balance_dirty(struct btrfs_fs_info * fs_info)44252ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info)
442616cdcec7SMiao Xie {
44272ff7e61eSJeff Mahoney 	__btrfs_btree_balance_dirty(fs_info, 1);
442835b7e476SChris Mason }
4429b53d3f5dSLiu Bo 
btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info * fs_info)44302ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info)
4431b53d3f5dSLiu Bo {
44322ff7e61eSJeff Mahoney 	__btrfs_btree_balance_dirty(fs_info, 0);
4433d6bfde87SChris Mason }
44346b80053dSChris Mason 
btrfs_error_commit_super(struct btrfs_fs_info * fs_info)44352ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info)
4436acce952bSliubo {
4437fe816d0fSNikolay Borisov 	/* cleanup FS via transaction */
4438fe816d0fSNikolay Borisov 	btrfs_cleanup_transaction(fs_info);
4439fe816d0fSNikolay Borisov 
44400b246afaSJeff Mahoney 	mutex_lock(&fs_info->cleaner_mutex);
44412ff7e61eSJeff Mahoney 	btrfs_run_delayed_iputs(fs_info);
44420b246afaSJeff Mahoney 	mutex_unlock(&fs_info->cleaner_mutex);
4443acce952bSliubo 
44440b246afaSJeff Mahoney 	down_write(&fs_info->cleanup_work_sem);
44450b246afaSJeff Mahoney 	up_write(&fs_info->cleanup_work_sem);
4446acce952bSliubo }
4447acce952bSliubo 
btrfs_drop_all_logs(struct btrfs_fs_info * fs_info)4448ef67963dSJosef Bacik static void btrfs_drop_all_logs(struct btrfs_fs_info *fs_info)
4449ef67963dSJosef Bacik {
4450fc7cbcd4SDavid Sterba 	struct btrfs_root *gang[8];
4451fc7cbcd4SDavid Sterba 	u64 root_objectid = 0;
4452fc7cbcd4SDavid Sterba 	int ret;
4453ef67963dSJosef Bacik 
4454fc7cbcd4SDavid Sterba 	spin_lock(&fs_info->fs_roots_radix_lock);
4455fc7cbcd4SDavid Sterba 	while ((ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
4456fc7cbcd4SDavid Sterba 					     (void **)gang, root_objectid,
4457fc7cbcd4SDavid Sterba 					     ARRAY_SIZE(gang))) != 0) {
4458fc7cbcd4SDavid Sterba 		int i;
4459ef67963dSJosef Bacik 
4460fc7cbcd4SDavid Sterba 		for (i = 0; i < ret; i++)
4461fc7cbcd4SDavid Sterba 			gang[i] = btrfs_grab_root(gang[i]);
4462fc7cbcd4SDavid Sterba 		spin_unlock(&fs_info->fs_roots_radix_lock);
4463fc7cbcd4SDavid Sterba 
4464fc7cbcd4SDavid Sterba 		for (i = 0; i < ret; i++) {
4465fc7cbcd4SDavid Sterba 			if (!gang[i])
4466ef67963dSJosef Bacik 				continue;
4467e094f480SJosef Bacik 			root_objectid = btrfs_root_id(gang[i]);
4468fc7cbcd4SDavid Sterba 			btrfs_free_log(NULL, gang[i]);
4469fc7cbcd4SDavid Sterba 			btrfs_put_root(gang[i]);
4470ef67963dSJosef Bacik 		}
4471fc7cbcd4SDavid Sterba 		root_objectid++;
4472fc7cbcd4SDavid Sterba 		spin_lock(&fs_info->fs_roots_radix_lock);
4473ef67963dSJosef Bacik 	}
4474fc7cbcd4SDavid Sterba 	spin_unlock(&fs_info->fs_roots_radix_lock);
4475ef67963dSJosef Bacik 	btrfs_free_log_root_tree(NULL, fs_info);
4476ef67963dSJosef Bacik }
4477ef67963dSJosef Bacik 
btrfs_destroy_ordered_extents(struct btrfs_root * root)4478143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root)
4479acce952bSliubo {
4480acce952bSliubo 	struct btrfs_ordered_extent *ordered;
4481acce952bSliubo 
4482199c2a9cSMiao Xie 	spin_lock(&root->ordered_extent_lock);
4483779880efSJosef Bacik 	/*
4484779880efSJosef Bacik 	 * This will just short circuit the ordered completion stuff which will
4485779880efSJosef Bacik 	 * make sure the ordered extent gets properly cleaned up.
4486779880efSJosef Bacik 	 */
4487199c2a9cSMiao Xie 	list_for_each_entry(ordered, &root->ordered_extents,
4488779880efSJosef Bacik 			    root_extent_list)
4489779880efSJosef Bacik 		set_bit(BTRFS_ORDERED_IOERR, &ordered->flags);
4490199c2a9cSMiao Xie 	spin_unlock(&root->ordered_extent_lock);
4491199c2a9cSMiao Xie }
4492199c2a9cSMiao Xie 
btrfs_destroy_all_ordered_extents(struct btrfs_fs_info * fs_info)4493199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info)
4494199c2a9cSMiao Xie {
4495199c2a9cSMiao Xie 	struct btrfs_root *root;
449684af994bSRuan Jinjie 	LIST_HEAD(splice);
4497199c2a9cSMiao Xie 
4498199c2a9cSMiao Xie 	spin_lock(&fs_info->ordered_root_lock);
4499199c2a9cSMiao Xie 	list_splice_init(&fs_info->ordered_roots, &splice);
4500199c2a9cSMiao Xie 	while (!list_empty(&splice)) {
4501199c2a9cSMiao Xie 		root = list_first_entry(&splice, struct btrfs_root,
4502199c2a9cSMiao Xie 					ordered_root);
45031de2cfdeSJosef Bacik 		list_move_tail(&root->ordered_root,
45041de2cfdeSJosef Bacik 			       &fs_info->ordered_roots);
4505199c2a9cSMiao Xie 
45062a85d9caSLiu Bo 		spin_unlock(&fs_info->ordered_root_lock);
4507199c2a9cSMiao Xie 		btrfs_destroy_ordered_extents(root);
4508199c2a9cSMiao Xie 
45092a85d9caSLiu Bo 		cond_resched();
45102a85d9caSLiu Bo 		spin_lock(&fs_info->ordered_root_lock);
4511199c2a9cSMiao Xie 	}
4512199c2a9cSMiao Xie 	spin_unlock(&fs_info->ordered_root_lock);
451374d5d229SJosef Bacik 
451474d5d229SJosef Bacik 	/*
451574d5d229SJosef Bacik 	 * We need this here because if we've been flipped read-only we won't
451674d5d229SJosef Bacik 	 * get sync() from the umount, so we need to make sure any ordered
451774d5d229SJosef Bacik 	 * extents that haven't had their dirty pages IO start writeout yet
451874d5d229SJosef Bacik 	 * actually get run and error out properly.
451974d5d229SJosef Bacik 	 */
452042317ab4SDavid Sterba 	btrfs_wait_ordered_roots(fs_info, U64_MAX, NULL);
4521acce952bSliubo }
4522acce952bSliubo 
btrfs_destroy_delayed_refs(struct btrfs_transaction * trans,struct btrfs_fs_info * fs_info)452399f09ce3SFilipe Manana static void btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
45242ff7e61eSJeff Mahoney 				       struct btrfs_fs_info *fs_info)
4525acce952bSliubo {
4526acce952bSliubo 	struct rb_node *node;
4527fb33eb2eSFilipe Manana 	struct btrfs_delayed_ref_root *delayed_refs = &trans->delayed_refs;
4528acce952bSliubo 	struct btrfs_delayed_ref_node *ref;
4529acce952bSliubo 
4530acce952bSliubo 	spin_lock(&delayed_refs->lock);
45315c9d028bSLiu Bo 	while ((node = rb_first_cached(&delayed_refs->href_root)) != NULL) {
4532d7df2c79SJosef Bacik 		struct btrfs_delayed_ref_head *head;
45330e0adbcfSJosef Bacik 		struct rb_node *n;
4534e78417d1SJosef Bacik 		bool pin_bytes = false;
4535acce952bSliubo 
4536d7df2c79SJosef Bacik 		head = rb_entry(node, struct btrfs_delayed_ref_head,
4537d7df2c79SJosef Bacik 				href_node);
45383069bd26SJosef Bacik 		if (btrfs_delayed_ref_lock(delayed_refs, head))
4539b939d1abSJosef Bacik 			continue;
45403069bd26SJosef Bacik 
4541d7df2c79SJosef Bacik 		spin_lock(&head->lock);
4542e3d03965SLiu Bo 		while ((n = rb_first_cached(&head->ref_tree)) != NULL) {
45430e0adbcfSJosef Bacik 			ref = rb_entry(n, struct btrfs_delayed_ref_node,
45440e0adbcfSJosef Bacik 				       ref_node);
4545e3d03965SLiu Bo 			rb_erase_cached(&ref->ref_node, &head->ref_tree);
45460e0adbcfSJosef Bacik 			RB_CLEAR_NODE(&ref->ref_node);
45471d57ee94SWang Xiaoguang 			if (!list_empty(&ref->add_list))
45481d57ee94SWang Xiaoguang 				list_del(&ref->add_list);
4549d7df2c79SJosef Bacik 			atomic_dec(&delayed_refs->num_entries);
4550d7df2c79SJosef Bacik 			btrfs_put_delayed_ref(ref);
4551adb86dbeSFilipe Manana 			btrfs_delayed_refs_rsv_release(fs_info, 1, 0);
4552d7df2c79SJosef Bacik 		}
455354067ae9SJosef Bacik 		if (head->must_insert_reserved)
4554e78417d1SJosef Bacik 			pin_bytes = true;
455578a6184aSMiao Xie 		btrfs_free_delayed_extent_op(head->extent_op);
4556fa781ceaSJosef Bacik 		btrfs_delete_ref_head(delayed_refs, head);
4557d7df2c79SJosef Bacik 		spin_unlock(&head->lock);
4558acce952bSliubo 		spin_unlock(&delayed_refs->lock);
4559e78417d1SJosef Bacik 		mutex_unlock(&head->mutex);
4560acce952bSliubo 
4561f603bb94SNikolay Borisov 		if (pin_bytes) {
4562f603bb94SNikolay Borisov 			struct btrfs_block_group *cache;
4563f603bb94SNikolay Borisov 
4564f603bb94SNikolay Borisov 			cache = btrfs_lookup_block_group(fs_info, head->bytenr);
4565f603bb94SNikolay Borisov 			BUG_ON(!cache);
4566f603bb94SNikolay Borisov 
4567f603bb94SNikolay Borisov 			spin_lock(&cache->space_info->lock);
4568f603bb94SNikolay Borisov 			spin_lock(&cache->lock);
4569f603bb94SNikolay Borisov 			cache->pinned += head->num_bytes;
4570f603bb94SNikolay Borisov 			btrfs_space_info_update_bytes_pinned(fs_info,
4571f603bb94SNikolay Borisov 				cache->space_info, head->num_bytes);
4572f603bb94SNikolay Borisov 			cache->reserved -= head->num_bytes;
4573f603bb94SNikolay Borisov 			cache->space_info->bytes_reserved -= head->num_bytes;
4574f603bb94SNikolay Borisov 			spin_unlock(&cache->lock);
4575f603bb94SNikolay Borisov 			spin_unlock(&cache->space_info->lock);
4576f603bb94SNikolay Borisov 
4577f603bb94SNikolay Borisov 			btrfs_put_block_group(cache);
4578f603bb94SNikolay Borisov 
4579f603bb94SNikolay Borisov 			btrfs_error_unpin_extent_range(fs_info, head->bytenr,
4580f603bb94SNikolay Borisov 				head->bytenr + head->num_bytes - 1);
4581f603bb94SNikolay Borisov 		}
458231890da0SJosef Bacik 		btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head);
4583d278850eSJosef Bacik 		btrfs_put_delayed_ref_head(head);
4584acce952bSliubo 		cond_resched();
4585acce952bSliubo 		spin_lock(&delayed_refs->lock);
4586acce952bSliubo 	}
458781f7eb00SJeff Mahoney 	btrfs_qgroup_destroy_extent_records(trans);
4588acce952bSliubo 
4589acce952bSliubo 	spin_unlock(&delayed_refs->lock);
4590acce952bSliubo }
4591acce952bSliubo 
btrfs_destroy_delalloc_inodes(struct btrfs_root * root)4592143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root)
4593acce952bSliubo {
4594acce952bSliubo 	struct btrfs_inode *btrfs_inode;
459584af994bSRuan Jinjie 	LIST_HEAD(splice);
4596acce952bSliubo 
4597eb73c1b7SMiao Xie 	spin_lock(&root->delalloc_lock);
4598eb73c1b7SMiao Xie 	list_splice_init(&root->delalloc_inodes, &splice);
4599acce952bSliubo 
4600acce952bSliubo 	while (!list_empty(&splice)) {
4601fe816d0fSNikolay Borisov 		struct inode *inode = NULL;
4602eb73c1b7SMiao Xie 		btrfs_inode = list_first_entry(&splice, struct btrfs_inode,
4603acce952bSliubo 					       delalloc_inodes);
46045a8a57f9SDavid Sterba 		btrfs_del_delalloc_inode(btrfs_inode);
4605eb73c1b7SMiao Xie 		spin_unlock(&root->delalloc_lock);
4606acce952bSliubo 
4607fe816d0fSNikolay Borisov 		/*
4608fe816d0fSNikolay Borisov 		 * Make sure we get a live inode and that it'll not disappear
4609fe816d0fSNikolay Borisov 		 * meanwhile.
4610fe816d0fSNikolay Borisov 		 */
4611fe816d0fSNikolay Borisov 		inode = igrab(&btrfs_inode->vfs_inode);
4612fe816d0fSNikolay Borisov 		if (inode) {
4613597441b3SJosef Bacik 			unsigned int nofs_flag;
4614597441b3SJosef Bacik 
4615597441b3SJosef Bacik 			nofs_flag = memalloc_nofs_save();
4616fe816d0fSNikolay Borisov 			invalidate_inode_pages2(inode->i_mapping);
4617597441b3SJosef Bacik 			memalloc_nofs_restore(nofs_flag);
4618fe816d0fSNikolay Borisov 			iput(inode);
4619fe816d0fSNikolay Borisov 		}
4620eb73c1b7SMiao Xie 		spin_lock(&root->delalloc_lock);
4621acce952bSliubo 	}
4622eb73c1b7SMiao Xie 	spin_unlock(&root->delalloc_lock);
4623eb73c1b7SMiao Xie }
4624eb73c1b7SMiao Xie 
btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info * fs_info)4625eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info)
4626eb73c1b7SMiao Xie {
4627eb73c1b7SMiao Xie 	struct btrfs_root *root;
462884af994bSRuan Jinjie 	LIST_HEAD(splice);
4629eb73c1b7SMiao Xie 
4630eb73c1b7SMiao Xie 	spin_lock(&fs_info->delalloc_root_lock);
4631eb73c1b7SMiao Xie 	list_splice_init(&fs_info->delalloc_roots, &splice);
4632eb73c1b7SMiao Xie 	while (!list_empty(&splice)) {
4633eb73c1b7SMiao Xie 		root = list_first_entry(&splice, struct btrfs_root,
4634eb73c1b7SMiao Xie 					 delalloc_root);
463500246528SJosef Bacik 		root = btrfs_grab_root(root);
4636eb73c1b7SMiao Xie 		BUG_ON(!root);
4637eb73c1b7SMiao Xie 		spin_unlock(&fs_info->delalloc_root_lock);
4638eb73c1b7SMiao Xie 
4639eb73c1b7SMiao Xie 		btrfs_destroy_delalloc_inodes(root);
464000246528SJosef Bacik 		btrfs_put_root(root);
4641eb73c1b7SMiao Xie 
4642eb73c1b7SMiao Xie 		spin_lock(&fs_info->delalloc_root_lock);
4643eb73c1b7SMiao Xie 	}
4644eb73c1b7SMiao Xie 	spin_unlock(&fs_info->delalloc_root_lock);
4645acce952bSliubo }
4646acce952bSliubo 
btrfs_destroy_marked_extents(struct btrfs_fs_info * fs_info,struct extent_io_tree * dirty_pages,int mark)4647aec5716cSFilipe Manana static void btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info,
4648acce952bSliubo 					 struct extent_io_tree *dirty_pages,
4649acce952bSliubo 					 int mark)
4650acce952bSliubo {
4651acce952bSliubo 	struct extent_buffer *eb;
4652acce952bSliubo 	u64 start = 0;
4653acce952bSliubo 	u64 end;
4654acce952bSliubo 
4655e5860f82SFilipe Manana 	while (find_first_extent_bit(dirty_pages, start, &start, &end,
4656aec5716cSFilipe Manana 				     mark, NULL)) {
465791166212SDavid Sterba 		clear_extent_bits(dirty_pages, start, end, mark);
4658acce952bSliubo 		while (start <= end) {
46590b246afaSJeff Mahoney 			eb = find_extent_buffer(fs_info, start);
46600b246afaSJeff Mahoney 			start += fs_info->nodesize;
4661fd8b2b61SJosef Bacik 			if (!eb)
4662acce952bSliubo 				continue;
4663acce952bSliubo 
4664c4e54a65SJosef Bacik 			btrfs_tree_lock(eb);
4665c4e54a65SJosef Bacik 			wait_on_extent_buffer_writeback(eb);
4666190a8339SJosef Bacik 			btrfs_clear_buffer_dirty(NULL, eb);
4667c4e54a65SJosef Bacik 			btrfs_tree_unlock(eb);
4668c4e54a65SJosef Bacik 
4669fd8b2b61SJosef Bacik 			free_extent_buffer_stale(eb);
4670acce952bSliubo 		}
4671acce952bSliubo 	}
4672acce952bSliubo }
4673acce952bSliubo 
btrfs_destroy_pinned_extent(struct btrfs_fs_info * fs_info,struct extent_io_tree * unpin)467446d81ebdSFilipe Manana static void btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info,
4675fe119a6eSNikolay Borisov 					struct extent_io_tree *unpin)
4676acce952bSliubo {
4677acce952bSliubo 	u64 start;
4678acce952bSliubo 	u64 end;
4679acce952bSliubo 
4680acce952bSliubo 	while (1) {
46810e6ec385SFilipe Manana 		struct extent_state *cached_state = NULL;
46820e6ec385SFilipe Manana 
4683fcd5e742SLu Fengqi 		/*
4684fcd5e742SLu Fengqi 		 * The btrfs_finish_extent_commit() may get the same range as
4685fcd5e742SLu Fengqi 		 * ours between find_first_extent_bit and clear_extent_dirty.
4686fcd5e742SLu Fengqi 		 * Hence, hold the unused_bg_unpin_mutex to avoid double unpin
4687fcd5e742SLu Fengqi 		 * the same extent range.
4688fcd5e742SLu Fengqi 		 */
4689fcd5e742SLu Fengqi 		mutex_lock(&fs_info->unused_bg_unpin_mutex);
4690e5860f82SFilipe Manana 		if (!find_first_extent_bit(unpin, 0, &start, &end,
469146d81ebdSFilipe Manana 					   EXTENT_DIRTY, &cached_state)) {
4692fcd5e742SLu Fengqi 			mutex_unlock(&fs_info->unused_bg_unpin_mutex);
4693acce952bSliubo 			break;
4694fcd5e742SLu Fengqi 		}
4695acce952bSliubo 
46960e6ec385SFilipe Manana 		clear_extent_dirty(unpin, start, end, &cached_state);
46970e6ec385SFilipe Manana 		free_extent_state(cached_state);
46982ff7e61eSJeff Mahoney 		btrfs_error_unpin_extent_range(fs_info, start, end);
4699fcd5e742SLu Fengqi 		mutex_unlock(&fs_info->unused_bg_unpin_mutex);
4700acce952bSliubo 		cond_resched();
4701acce952bSliubo 	}
4702acce952bSliubo }
4703acce952bSliubo 
btrfs_cleanup_bg_io(struct btrfs_block_group * cache)470432da5386SDavid Sterba static void btrfs_cleanup_bg_io(struct btrfs_block_group *cache)
4705c79a1751SLiu Bo {
4706c79a1751SLiu Bo 	struct inode *inode;
4707c79a1751SLiu Bo 
4708c79a1751SLiu Bo 	inode = cache->io_ctl.inode;
4709c79a1751SLiu Bo 	if (inode) {
4710597441b3SJosef Bacik 		unsigned int nofs_flag;
4711597441b3SJosef Bacik 
4712597441b3SJosef Bacik 		nofs_flag = memalloc_nofs_save();
4713c79a1751SLiu Bo 		invalidate_inode_pages2(inode->i_mapping);
4714597441b3SJosef Bacik 		memalloc_nofs_restore(nofs_flag);
4715597441b3SJosef Bacik 
4716c79a1751SLiu Bo 		BTRFS_I(inode)->generation = 0;
4717c79a1751SLiu Bo 		cache->io_ctl.inode = NULL;
4718c79a1751SLiu Bo 		iput(inode);
4719c79a1751SLiu Bo 	}
4720bbc37d6eSFilipe Manana 	ASSERT(cache->io_ctl.pages == NULL);
4721c79a1751SLiu Bo 	btrfs_put_block_group(cache);
4722c79a1751SLiu Bo }
4723c79a1751SLiu Bo 
btrfs_cleanup_dirty_bgs(struct btrfs_transaction * cur_trans,struct btrfs_fs_info * fs_info)4724c79a1751SLiu Bo void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans,
47252ff7e61eSJeff Mahoney 			     struct btrfs_fs_info *fs_info)
4726c79a1751SLiu Bo {
472732da5386SDavid Sterba 	struct btrfs_block_group *cache;
4728c79a1751SLiu Bo 
4729c79a1751SLiu Bo 	spin_lock(&cur_trans->dirty_bgs_lock);
4730c79a1751SLiu Bo 	while (!list_empty(&cur_trans->dirty_bgs)) {
4731c79a1751SLiu Bo 		cache = list_first_entry(&cur_trans->dirty_bgs,
473232da5386SDavid Sterba 					 struct btrfs_block_group,
4733c79a1751SLiu Bo 					 dirty_list);
4734c79a1751SLiu Bo 
4735c79a1751SLiu Bo 		if (!list_empty(&cache->io_list)) {
4736c79a1751SLiu Bo 			spin_unlock(&cur_trans->dirty_bgs_lock);
4737c79a1751SLiu Bo 			list_del_init(&cache->io_list);
4738c79a1751SLiu Bo 			btrfs_cleanup_bg_io(cache);
4739c79a1751SLiu Bo 			spin_lock(&cur_trans->dirty_bgs_lock);
4740c79a1751SLiu Bo 		}
4741c79a1751SLiu Bo 
4742c79a1751SLiu Bo 		list_del_init(&cache->dirty_list);
4743c79a1751SLiu Bo 		spin_lock(&cache->lock);
4744c79a1751SLiu Bo 		cache->disk_cache_state = BTRFS_DC_ERROR;
4745c79a1751SLiu Bo 		spin_unlock(&cache->lock);
4746c79a1751SLiu Bo 
4747c79a1751SLiu Bo 		spin_unlock(&cur_trans->dirty_bgs_lock);
4748c79a1751SLiu Bo 		btrfs_put_block_group(cache);
4749f66e0209SFilipe Manana 		btrfs_dec_delayed_refs_rsv_bg_updates(fs_info);
4750c79a1751SLiu Bo 		spin_lock(&cur_trans->dirty_bgs_lock);
4751c79a1751SLiu Bo 	}
4752c79a1751SLiu Bo 	spin_unlock(&cur_trans->dirty_bgs_lock);
4753c79a1751SLiu Bo 
475445ae2c18SNikolay Borisov 	/*
475545ae2c18SNikolay Borisov 	 * Refer to the definition of io_bgs member for details why it's safe
475645ae2c18SNikolay Borisov 	 * to use it without any locking
475745ae2c18SNikolay Borisov 	 */
4758c79a1751SLiu Bo 	while (!list_empty(&cur_trans->io_bgs)) {
4759c79a1751SLiu Bo 		cache = list_first_entry(&cur_trans->io_bgs,
476032da5386SDavid Sterba 					 struct btrfs_block_group,
4761c79a1751SLiu Bo 					 io_list);
4762c79a1751SLiu Bo 
4763c79a1751SLiu Bo 		list_del_init(&cache->io_list);
4764c79a1751SLiu Bo 		spin_lock(&cache->lock);
4765c79a1751SLiu Bo 		cache->disk_cache_state = BTRFS_DC_ERROR;
4766c79a1751SLiu Bo 		spin_unlock(&cache->lock);
4767c79a1751SLiu Bo 		btrfs_cleanup_bg_io(cache);
4768c79a1751SLiu Bo 	}
4769c79a1751SLiu Bo }
4770c79a1751SLiu Bo 
btrfs_free_all_qgroup_pertrans(struct btrfs_fs_info * fs_info)4771b321a52cSBoris Burkov static void btrfs_free_all_qgroup_pertrans(struct btrfs_fs_info *fs_info)
4772b321a52cSBoris Burkov {
4773b321a52cSBoris Burkov 	struct btrfs_root *gang[8];
4774b321a52cSBoris Burkov 	int i;
4775b321a52cSBoris Burkov 	int ret;
4776b321a52cSBoris Burkov 
4777b321a52cSBoris Burkov 	spin_lock(&fs_info->fs_roots_radix_lock);
4778b321a52cSBoris Burkov 	while (1) {
4779b321a52cSBoris Burkov 		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
4780b321a52cSBoris Burkov 						 (void **)gang, 0,
4781b321a52cSBoris Burkov 						 ARRAY_SIZE(gang),
4782b321a52cSBoris Burkov 						 BTRFS_ROOT_TRANS_TAG);
4783b321a52cSBoris Burkov 		if (ret == 0)
4784b321a52cSBoris Burkov 			break;
4785b321a52cSBoris Burkov 		for (i = 0; i < ret; i++) {
4786b321a52cSBoris Burkov 			struct btrfs_root *root = gang[i];
4787b321a52cSBoris Burkov 
4788b321a52cSBoris Burkov 			btrfs_qgroup_free_meta_all_pertrans(root);
4789b321a52cSBoris Burkov 			radix_tree_tag_clear(&fs_info->fs_roots_radix,
4790e094f480SJosef Bacik 					(unsigned long)btrfs_root_id(root),
4791b321a52cSBoris Burkov 					BTRFS_ROOT_TRANS_TAG);
4792b321a52cSBoris Burkov 		}
4793b321a52cSBoris Burkov 	}
4794b321a52cSBoris Burkov 	spin_unlock(&fs_info->fs_roots_radix_lock);
4795b321a52cSBoris Burkov }
4796b321a52cSBoris Burkov 
btrfs_cleanup_one_transaction(struct btrfs_transaction * cur_trans,struct btrfs_fs_info * fs_info)479749b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
47982ff7e61eSJeff Mahoney 				   struct btrfs_fs_info *fs_info)
479949b25e05SJeff Mahoney {
4800bbbf7243SNikolay Borisov 	struct btrfs_device *dev, *tmp;
4801bbbf7243SNikolay Borisov 
48022ff7e61eSJeff Mahoney 	btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
4803c79a1751SLiu Bo 	ASSERT(list_empty(&cur_trans->dirty_bgs));
4804c79a1751SLiu Bo 	ASSERT(list_empty(&cur_trans->io_bgs));
4805c79a1751SLiu Bo 
4806bbbf7243SNikolay Borisov 	list_for_each_entry_safe(dev, tmp, &cur_trans->dev_update_list,
4807bbbf7243SNikolay Borisov 				 post_commit_list) {
4808bbbf7243SNikolay Borisov 		list_del_init(&dev->post_commit_list);
4809bbbf7243SNikolay Borisov 	}
4810bbbf7243SNikolay Borisov 
48112ff7e61eSJeff Mahoney 	btrfs_destroy_delayed_refs(cur_trans, fs_info);
481249b25e05SJeff Mahoney 
48134a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_COMMIT_START;
48140b246afaSJeff Mahoney 	wake_up(&fs_info->transaction_blocked_wait);
481549b25e05SJeff Mahoney 
48164a9d8bdeSMiao Xie 	cur_trans->state = TRANS_STATE_UNBLOCKED;
48170b246afaSJeff Mahoney 	wake_up(&fs_info->transaction_wait);
481849b25e05SJeff Mahoney 
48192ff7e61eSJeff Mahoney 	btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages,
482049b25e05SJeff Mahoney 				     EXTENT_DIRTY);
4821fe119a6eSNikolay Borisov 	btrfs_destroy_pinned_extent(fs_info, &cur_trans->pinned_extents);
482249b25e05SJeff Mahoney 
48234a9d8bdeSMiao Xie 	cur_trans->state =TRANS_STATE_COMPLETED;
48244a9d8bdeSMiao Xie 	wake_up(&cur_trans->commit_wait);
482549b25e05SJeff Mahoney }
482649b25e05SJeff Mahoney 
btrfs_cleanup_transaction(struct btrfs_fs_info * fs_info)48272ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info)
4828acce952bSliubo {
4829acce952bSliubo 	struct btrfs_transaction *t;
4830acce952bSliubo 
48310b246afaSJeff Mahoney 	mutex_lock(&fs_info->transaction_kthread_mutex);
4832acce952bSliubo 
48330b246afaSJeff Mahoney 	spin_lock(&fs_info->trans_lock);
48340b246afaSJeff Mahoney 	while (!list_empty(&fs_info->trans_list)) {
48350b246afaSJeff Mahoney 		t = list_first_entry(&fs_info->trans_list,
4836724e2315SJosef Bacik 				     struct btrfs_transaction, list);
483777d20c68SJosef Bacik 		if (t->state >= TRANS_STATE_COMMIT_PREP) {
48389b64f57dSElena Reshetova 			refcount_inc(&t->use_count);
48390b246afaSJeff Mahoney 			spin_unlock(&fs_info->trans_lock);
48402ff7e61eSJeff Mahoney 			btrfs_wait_for_commit(fs_info, t->transid);
4841724e2315SJosef Bacik 			btrfs_put_transaction(t);
48420b246afaSJeff Mahoney 			spin_lock(&fs_info->trans_lock);
4843724e2315SJosef Bacik 			continue;
4844724e2315SJosef Bacik 		}
48450b246afaSJeff Mahoney 		if (t == fs_info->running_transaction) {
4846724e2315SJosef Bacik 			t->state = TRANS_STATE_COMMIT_DOING;
48470b246afaSJeff Mahoney 			spin_unlock(&fs_info->trans_lock);
4848724e2315SJosef Bacik 			/*
4849724e2315SJosef Bacik 			 * We wait for 0 num_writers since we don't hold a trans
4850724e2315SJosef Bacik 			 * handle open currently for this transaction.
4851724e2315SJosef Bacik 			 */
4852724e2315SJosef Bacik 			wait_event(t->writer_wait,
4853724e2315SJosef Bacik 				   atomic_read(&t->num_writers) == 0);
4854724e2315SJosef Bacik 		} else {
48550b246afaSJeff Mahoney 			spin_unlock(&fs_info->trans_lock);
4856724e2315SJosef Bacik 		}
48572ff7e61eSJeff Mahoney 		btrfs_cleanup_one_transaction(t, fs_info);
4858724e2315SJosef Bacik 
48590b246afaSJeff Mahoney 		spin_lock(&fs_info->trans_lock);
48600b246afaSJeff Mahoney 		if (t == fs_info->running_transaction)
48610b246afaSJeff Mahoney 			fs_info->running_transaction = NULL;
4862724e2315SJosef Bacik 		list_del_init(&t->list);
48630b246afaSJeff Mahoney 		spin_unlock(&fs_info->trans_lock);
4864a4abeea4SJosef Bacik 
4865724e2315SJosef Bacik 		btrfs_put_transaction(t);
48662e4e97abSJosef Bacik 		trace_btrfs_transaction_commit(fs_info);
48670b246afaSJeff Mahoney 		spin_lock(&fs_info->trans_lock);
4868724e2315SJosef Bacik 	}
48690b246afaSJeff Mahoney 	spin_unlock(&fs_info->trans_lock);
48700b246afaSJeff Mahoney 	btrfs_destroy_all_ordered_extents(fs_info);
4871ccdf9b30SJeff Mahoney 	btrfs_destroy_delayed_inodes(fs_info);
4872ccdf9b30SJeff Mahoney 	btrfs_assert_delayed_root_empty(fs_info);
48730b246afaSJeff Mahoney 	btrfs_destroy_all_delalloc_inodes(fs_info);
4874ef67963dSJosef Bacik 	btrfs_drop_all_logs(fs_info);
48755f2fb819SBoris Burkov 	btrfs_free_all_qgroup_pertrans(fs_info);
48760b246afaSJeff Mahoney 	mutex_unlock(&fs_info->transaction_kthread_mutex);
4877acce952bSliubo 
4878acce952bSliubo 	return 0;
4879acce952bSliubo }
4880ec7d6dfdSNikolay Borisov 
btrfs_init_root_free_objectid(struct btrfs_root * root)4881453e4873SNikolay Borisov int btrfs_init_root_free_objectid(struct btrfs_root *root)
4882ec7d6dfdSNikolay Borisov {
4883ec7d6dfdSNikolay Borisov 	struct btrfs_path *path;
4884ec7d6dfdSNikolay Borisov 	int ret;
4885ec7d6dfdSNikolay Borisov 	struct extent_buffer *l;
4886ec7d6dfdSNikolay Borisov 	struct btrfs_key search_key;
4887ec7d6dfdSNikolay Borisov 	struct btrfs_key found_key;
4888ec7d6dfdSNikolay Borisov 	int slot;
4889ec7d6dfdSNikolay Borisov 
4890ec7d6dfdSNikolay Borisov 	path = btrfs_alloc_path();
4891ec7d6dfdSNikolay Borisov 	if (!path)
4892ec7d6dfdSNikolay Borisov 		return -ENOMEM;
4893ec7d6dfdSNikolay Borisov 
4894ec7d6dfdSNikolay Borisov 	search_key.objectid = BTRFS_LAST_FREE_OBJECTID;
4895ec7d6dfdSNikolay Borisov 	search_key.type = -1;
4896ec7d6dfdSNikolay Borisov 	search_key.offset = (u64)-1;
4897ec7d6dfdSNikolay Borisov 	ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
4898ec7d6dfdSNikolay Borisov 	if (ret < 0)
4899ec7d6dfdSNikolay Borisov 		goto error;
4900a6724290SDavid Sterba 	if (ret == 0) {
4901a6724290SDavid Sterba 		/*
4902a6724290SDavid Sterba 		 * Key with offset -1 found, there would have to exist a root
4903a6724290SDavid Sterba 		 * with such id, but this is out of valid range.
4904a6724290SDavid Sterba 		 */
4905a6724290SDavid Sterba 		ret = -EUCLEAN;
4906a6724290SDavid Sterba 		goto error;
4907a6724290SDavid Sterba 	}
4908ec7d6dfdSNikolay Borisov 	if (path->slots[0] > 0) {
4909ec7d6dfdSNikolay Borisov 		slot = path->slots[0] - 1;
4910ec7d6dfdSNikolay Borisov 		l = path->nodes[0];
4911ec7d6dfdSNikolay Borisov 		btrfs_item_key_to_cpu(l, &found_key, slot);
491223125104SNikolay Borisov 		root->free_objectid = max_t(u64, found_key.objectid + 1,
491323125104SNikolay Borisov 					    BTRFS_FIRST_FREE_OBJECTID);
4914ec7d6dfdSNikolay Borisov 	} else {
491523125104SNikolay Borisov 		root->free_objectid = BTRFS_FIRST_FREE_OBJECTID;
4916ec7d6dfdSNikolay Borisov 	}
4917ec7d6dfdSNikolay Borisov 	ret = 0;
4918ec7d6dfdSNikolay Borisov error:
4919ec7d6dfdSNikolay Borisov 	btrfs_free_path(path);
4920ec7d6dfdSNikolay Borisov 	return ret;
4921ec7d6dfdSNikolay Borisov }
4922ec7d6dfdSNikolay Borisov 
btrfs_get_free_objectid(struct btrfs_root * root,u64 * objectid)4923543068a2SNikolay Borisov int btrfs_get_free_objectid(struct btrfs_root *root, u64 *objectid)
4924ec7d6dfdSNikolay Borisov {
4925ec7d6dfdSNikolay Borisov 	int ret;
4926ec7d6dfdSNikolay Borisov 	mutex_lock(&root->objectid_mutex);
4927ec7d6dfdSNikolay Borisov 
49286b8fad57SNikolay Borisov 	if (unlikely(root->free_objectid >= BTRFS_LAST_FREE_OBJECTID)) {
4929ec7d6dfdSNikolay Borisov 		btrfs_warn(root->fs_info,
4930ec7d6dfdSNikolay Borisov 			   "the objectid of root %llu reaches its highest value",
4931e094f480SJosef Bacik 			   btrfs_root_id(root));
4932ec7d6dfdSNikolay Borisov 		ret = -ENOSPC;
4933ec7d6dfdSNikolay Borisov 		goto out;
4934ec7d6dfdSNikolay Borisov 	}
4935ec7d6dfdSNikolay Borisov 
493623125104SNikolay Borisov 	*objectid = root->free_objectid++;
4937ec7d6dfdSNikolay Borisov 	ret = 0;
4938ec7d6dfdSNikolay Borisov out:
4939ec7d6dfdSNikolay Borisov 	mutex_unlock(&root->objectid_mutex);
4940ec7d6dfdSNikolay Borisov 	return ret;
4941ec7d6dfdSNikolay Borisov }
4942