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"
3221adbd5cSStefan Behrens #include "check-integrity.h"
33606686eeSJosef Bacik #include "rcu-string.h"
348dabb742SStefan Behrens #include "dev-replace.h"
3553b381b3SDavid Woodhouse #include "raid56.h"
365ac1d209SJeff Mahoney #include "sysfs.h"
37fcebe456SJosef Bacik #include "qgroup.h"
38ebb8765bSAnand Jain #include "compression.h"
39557ea5ddSQu Wenruo #include "tree-checker.h"
40fd708b81SJosef Bacik #include "ref-verify.h"
41aac0023cSJosef Bacik #include "block-group.h"
42b0643e59SDennis Zhou #include "discard.h"
43f603bb94SNikolay Borisov #include "space-info.h"
44b70f5097SNaohiro Aota #include "zoned.h"
45139e8cd3SQu Wenruo #include "subpage.h"
46c7f13d42SJosef Bacik #include "fs.h"
4707e81dc9SJosef Bacik #include "accessors.h"
48a0231804SJosef Bacik #include "extent-tree.h"
4945c40c8fSJosef Bacik #include "root-tree.h"
5059b818e0SJosef Bacik #include "defrag.h"
51c7a03b52SJosef Bacik #include "uuid-tree.h"
5267707479SJosef Bacik #include "relocation.h"
532fc6822cSJosef Bacik #include "scrub.h"
54c03b2207SJosef Bacik #include "super.h"
55eb60ceacSChris Mason
56319e4d06SQu Wenruo #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\
57319e4d06SQu Wenruo BTRFS_HEADER_FLAG_RELOC |\
58319e4d06SQu Wenruo BTRFS_SUPER_FLAG_ERROR |\
59319e4d06SQu Wenruo BTRFS_SUPER_FLAG_SEEDING |\
60e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP |\
61e2731e55SAnand Jain BTRFS_SUPER_FLAG_METADUMP_V2)
62319e4d06SQu Wenruo
632ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info);
642ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info);
65ce9adaa5SChris Mason
btrfs_free_csum_hash(struct btrfs_fs_info * fs_info)66141386e1SJosef Bacik static void btrfs_free_csum_hash(struct btrfs_fs_info *fs_info)
67141386e1SJosef Bacik {
68141386e1SJosef Bacik if (fs_info->csum_shash)
69141386e1SJosef Bacik crypto_free_shash(fs_info->csum_shash);
70141386e1SJosef Bacik }
71141386e1SJosef Bacik
72d352ac68SChris Mason /*
732996e1f8SJohannes Thumshirn * Compute the csum of a btree block and store the result to provided buffer.
74d352ac68SChris Mason */
csum_tree_block(struct extent_buffer * buf,u8 * result)75c67b3892SDavid Sterba static void csum_tree_block(struct extent_buffer *buf, u8 *result)
7619c00ddcSChris Mason {
77d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = buf->fs_info;
787280305eSDavid Sterba const int num_pages = num_extent_pages(buf);
79a26663e7SQu Wenruo const int first_page_part = min_t(u32, PAGE_SIZE, fs_info->nodesize);
80d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
8119c00ddcSChris Mason char *kaddr;
82e9be5a30SDavid Sterba int i;
83d5178578SJohannes Thumshirn
84d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash;
85d5178578SJohannes Thumshirn crypto_shash_init(shash);
86a26663e7SQu Wenruo kaddr = page_address(buf->pages[0]) + offset_in_page(buf->start);
87e9be5a30SDavid Sterba crypto_shash_update(shash, kaddr + BTRFS_CSUM_SIZE,
88a26663e7SQu Wenruo first_page_part - BTRFS_CSUM_SIZE);
8919c00ddcSChris Mason
905ad9b471Spengfuyuan for (i = 1; i < num_pages && INLINE_EXTENT_BUFFER_PAGES > 1; i++) {
91e9be5a30SDavid Sterba kaddr = page_address(buf->pages[i]);
92e9be5a30SDavid Sterba crypto_shash_update(shash, kaddr, PAGE_SIZE);
9319c00ddcSChris Mason }
9471a63551SDavid Sterba memset(result, 0, BTRFS_CSUM_SIZE);
95d5178578SJohannes Thumshirn crypto_shash_final(shash, result);
9619c00ddcSChris Mason }
9719c00ddcSChris Mason
98d352ac68SChris Mason /*
99d352ac68SChris Mason * we can't consider a given block up to date unless the transid of the
100d352ac68SChris Mason * block matches the transid in the parent node's pointer. This is how we
101d352ac68SChris Mason * detect blocks that either didn't get written at all or got written
102d352ac68SChris Mason * in the wrong place.
103d352ac68SChris Mason */
btrfs_buffer_uptodate(struct extent_buffer * eb,u64 parent_transid,int atomic)104d87e6575SChristoph Hellwig int btrfs_buffer_uptodate(struct extent_buffer *eb, u64 parent_transid, int atomic)
1051259ab75SChris Mason {
106d87e6575SChristoph Hellwig if (!extent_buffer_uptodate(eb))
107d87e6575SChristoph Hellwig return 0;
1081259ab75SChris Mason
1091259ab75SChris Mason if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
110d87e6575SChristoph Hellwig return 1;
1111259ab75SChris Mason
112b9fab919SChris Mason if (atomic)
113b9fab919SChris Mason return -EAGAIN;
114b9fab919SChris Mason
115d87e6575SChristoph Hellwig if (!extent_buffer_uptodate(eb) ||
116d87e6575SChristoph Hellwig btrfs_header_generation(eb) != parent_transid) {
11794647322SDavid Sterba btrfs_err_rl(eb->fs_info,
1188f0ed7d4SQu Wenruo "parent transid verify failed on logical %llu mirror %u wanted %llu found %llu",
1198f0ed7d4SQu Wenruo eb->start, eb->read_mirror,
12029549aecSWang Shilong parent_transid, btrfs_header_generation(eb));
1210b32f4bbSJosef Bacik clear_extent_buffer_uptodate(eb);
1229e2aff90SChristoph Hellwig return 0;
123d87e6575SChristoph Hellwig }
1249e2aff90SChristoph Hellwig return 1;
1251259ab75SChris Mason }
1261259ab75SChris Mason
btrfs_supported_super_csum(u16 csum_type)127e7e16f48SJohannes Thumshirn static bool btrfs_supported_super_csum(u16 csum_type)
128e7e16f48SJohannes Thumshirn {
129e7e16f48SJohannes Thumshirn switch (csum_type) {
130e7e16f48SJohannes Thumshirn case BTRFS_CSUM_TYPE_CRC32:
1313951e7f0SJohannes Thumshirn case BTRFS_CSUM_TYPE_XXHASH:
1323831bf00SJohannes Thumshirn case BTRFS_CSUM_TYPE_SHA256:
133352ae07bSDavid Sterba case BTRFS_CSUM_TYPE_BLAKE2:
134e7e16f48SJohannes Thumshirn return true;
135e7e16f48SJohannes Thumshirn default:
136e7e16f48SJohannes Thumshirn return false;
137e7e16f48SJohannes Thumshirn }
138e7e16f48SJohannes Thumshirn }
139e7e16f48SJohannes Thumshirn
140d352ac68SChris Mason /*
1411104a885SDavid Sterba * Return 0 if the superblock checksum type matches the checksum value of that
1421104a885SDavid Sterba * algorithm. Pass the raw disk superblock data.
1431104a885SDavid Sterba */
btrfs_check_super_csum(struct btrfs_fs_info * fs_info,const struct btrfs_super_block * disk_sb)1443d17adeaSQu Wenruo int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
1453d17adeaSQu Wenruo const struct btrfs_super_block *disk_sb)
1461104a885SDavid Sterba {
14751bce6c9SJohannes Thumshirn char result[BTRFS_CSUM_SIZE];
148d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
149d5178578SJohannes Thumshirn
150d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash;
1511104a885SDavid Sterba
1521104a885SDavid Sterba /*
1531104a885SDavid Sterba * The super_block structure does not span the whole
15451bce6c9SJohannes Thumshirn * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space is
15551bce6c9SJohannes Thumshirn * filled with zeros and is included in the checksum.
1561104a885SDavid Sterba */
1573d17adeaSQu Wenruo crypto_shash_digest(shash, (const u8 *)disk_sb + BTRFS_CSUM_SIZE,
158fd08001fSEric Biggers BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, result);
1591104a885SDavid Sterba
16055fc29beSDavid Sterba if (memcmp(disk_sb->csum, result, fs_info->csum_size))
161e7e16f48SJohannes Thumshirn return 1;
1621104a885SDavid Sterba
163e7e16f48SJohannes Thumshirn return 0;
1641104a885SDavid Sterba }
1651104a885SDavid Sterba
btrfs_repair_eb_io_failure(const struct extent_buffer * eb,int mirror_num)166bacf60e5SChristoph Hellwig static int btrfs_repair_eb_io_failure(const struct extent_buffer *eb,
167bacf60e5SChristoph Hellwig int mirror_num)
168bacf60e5SChristoph Hellwig {
169bacf60e5SChristoph Hellwig struct btrfs_fs_info *fs_info = eb->fs_info;
170bacf60e5SChristoph Hellwig int i, num_pages = num_extent_pages(eb);
171bacf60e5SChristoph Hellwig int ret = 0;
172bacf60e5SChristoph Hellwig
173bacf60e5SChristoph Hellwig if (sb_rdonly(fs_info->sb))
174bacf60e5SChristoph Hellwig return -EROFS;
175bacf60e5SChristoph Hellwig
176bacf60e5SChristoph Hellwig for (i = 0; i < num_pages; i++) {
177bacf60e5SChristoph Hellwig struct page *p = eb->pages[i];
178917ac778SQu Wenruo u64 start = max_t(u64, eb->start, page_offset(p));
179917ac778SQu Wenruo u64 end = min_t(u64, eb->start + eb->len, page_offset(p) + PAGE_SIZE);
180917ac778SQu Wenruo u32 len = end - start;
181bacf60e5SChristoph Hellwig
182917ac778SQu Wenruo ret = btrfs_repair_io_failure(fs_info, 0, start, len,
183917ac778SQu Wenruo start, p, offset_in_page(start), mirror_num);
184bacf60e5SChristoph Hellwig if (ret)
185bacf60e5SChristoph Hellwig break;
186bacf60e5SChristoph Hellwig }
187bacf60e5SChristoph Hellwig
188bacf60e5SChristoph Hellwig return ret;
189bacf60e5SChristoph Hellwig }
190bacf60e5SChristoph Hellwig
1911104a885SDavid Sterba /*
192d352ac68SChris Mason * helper to read a given tree block, doing retries as required when
193d352ac68SChris Mason * the checksums don't match and we have alternate mirrors to try.
194581c1760SQu Wenruo *
195789d6a3aSQu Wenruo * @check: expected tree parentness check, see the comments of the
196789d6a3aSQu Wenruo * structure for details.
197d352ac68SChris Mason */
btrfs_read_extent_buffer(struct extent_buffer * eb,struct btrfs_tree_parent_check * check)1986a2e9dc4SFilipe Manana int btrfs_read_extent_buffer(struct extent_buffer *eb,
199789d6a3aSQu Wenruo struct btrfs_tree_parent_check *check)
200f188591eSChris Mason {
2015ab12d1fSDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info;
202ea466794SJosef Bacik int failed = 0;
203f188591eSChris Mason int ret;
204f188591eSChris Mason int num_copies = 0;
205f188591eSChris Mason int mirror_num = 0;
206ea466794SJosef Bacik int failed_mirror = 0;
207f188591eSChris Mason
208789d6a3aSQu Wenruo ASSERT(check);
209789d6a3aSQu Wenruo
210f188591eSChris Mason while (1) {
211f8397d69SNikolay Borisov clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags);
212947a6299SQu Wenruo ret = read_extent_buffer_pages(eb, WAIT_COMPLETE, mirror_num, check);
213947a6299SQu Wenruo if (!ret)
214581c1760SQu Wenruo break;
215d397712bSChris Mason
2160b246afaSJeff Mahoney num_copies = btrfs_num_copies(fs_info,
217f188591eSChris Mason eb->start, eb->len);
2184235298eSChris Mason if (num_copies == 1)
219ea466794SJosef Bacik break;
2204235298eSChris Mason
2215cf1ab56SJosef Bacik if (!failed_mirror) {
2225cf1ab56SJosef Bacik failed = 1;
2235cf1ab56SJosef Bacik failed_mirror = eb->read_mirror;
2245cf1ab56SJosef Bacik }
2255cf1ab56SJosef Bacik
226f188591eSChris Mason mirror_num++;
227ea466794SJosef Bacik if (mirror_num == failed_mirror)
228ea466794SJosef Bacik mirror_num++;
229ea466794SJosef Bacik
2304235298eSChris Mason if (mirror_num > num_copies)
231ea466794SJosef Bacik break;
232f188591eSChris Mason }
233ea466794SJosef Bacik
234c0901581SStefan Behrens if (failed && !ret && failed_mirror)
23520a1fbf9SDavid Sterba btrfs_repair_eb_io_failure(eb, failed_mirror);
236ea466794SJosef Bacik
237ea466794SJosef Bacik return ret;
238f188591eSChris Mason }
23919c00ddcSChris Mason
24031d89399SChristoph Hellwig /*
24131d89399SChristoph Hellwig * Checksum a dirty tree block before IO.
24231d89399SChristoph Hellwig */
btree_csum_one_bio(struct btrfs_bio * bbio)24331d89399SChristoph Hellwig blk_status_t btree_csum_one_bio(struct btrfs_bio *bbio)
244eca0f6f6SQu Wenruo {
24531d89399SChristoph Hellwig struct extent_buffer *eb = bbio->private;
246eca0f6f6SQu Wenruo struct btrfs_fs_info *fs_info = eb->fs_info;
24731d89399SChristoph Hellwig u64 found_start = btrfs_header_bytenr(eb);
248eca0f6f6SQu Wenruo u8 result[BTRFS_CSUM_SIZE];
249eca0f6f6SQu Wenruo int ret;
250eca0f6f6SQu Wenruo
25131d89399SChristoph Hellwig /* Btree blocks are always contiguous on disk. */
25231d89399SChristoph Hellwig if (WARN_ON_ONCE(bbio->file_offset != eb->start))
25331d89399SChristoph Hellwig return BLK_STS_IOERR;
25431d89399SChristoph Hellwig if (WARN_ON_ONCE(bbio->bio.bi_iter.bi_size != eb->len))
25531d89399SChristoph Hellwig return BLK_STS_IOERR;
25631d89399SChristoph Hellwig
25731d89399SChristoph Hellwig if (test_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags)) {
25831d89399SChristoph Hellwig WARN_ON_ONCE(found_start != 0);
25931d89399SChristoph Hellwig return BLK_STS_OK;
26031d89399SChristoph Hellwig }
26131d89399SChristoph Hellwig
26231d89399SChristoph Hellwig if (WARN_ON_ONCE(found_start != eb->start))
26331d89399SChristoph Hellwig return BLK_STS_IOERR;
26431d89399SChristoph Hellwig if (WARN_ON(!btrfs_page_test_uptodate(fs_info, eb->pages[0], eb->start,
26531d89399SChristoph Hellwig eb->len)))
26631d89399SChristoph Hellwig return BLK_STS_IOERR;
26731d89399SChristoph Hellwig
268eca0f6f6SQu Wenruo ASSERT(memcmp_extent_buffer(eb, fs_info->fs_devices->metadata_uuid,
269eca0f6f6SQu Wenruo offsetof(struct btrfs_header, fsid),
270eca0f6f6SQu Wenruo BTRFS_FSID_SIZE) == 0);
271eca0f6f6SQu Wenruo csum_tree_block(eb, result);
272eca0f6f6SQu Wenruo
273eca0f6f6SQu Wenruo if (btrfs_header_level(eb))
274eca0f6f6SQu Wenruo ret = btrfs_check_node(eb);
275eca0f6f6SQu Wenruo else
27685d8a826SJosef Bacik ret = btrfs_check_leaf(eb);
277eca0f6f6SQu Wenruo
2783777369fSQu Wenruo if (ret < 0)
2793777369fSQu Wenruo goto error;
2803777369fSQu Wenruo
2813777369fSQu Wenruo /*
2823777369fSQu Wenruo * Also check the generation, the eb reached here must be newer than
2833777369fSQu Wenruo * last committed. Or something seriously wrong happened.
2843777369fSQu Wenruo */
2853777369fSQu Wenruo if (unlikely(btrfs_header_generation(eb) <= fs_info->last_trans_committed)) {
2863777369fSQu Wenruo ret = -EUCLEAN;
287eca0f6f6SQu Wenruo btrfs_err(fs_info,
2883777369fSQu Wenruo "block=%llu bad generation, have %llu expect > %llu",
2893777369fSQu Wenruo eb->start, btrfs_header_generation(eb),
2903777369fSQu Wenruo fs_info->last_trans_committed);
2913777369fSQu Wenruo goto error;
292eca0f6f6SQu Wenruo }
293eca0f6f6SQu Wenruo write_extent_buffer(eb, result, 0, fs_info->csum_size);
29431d89399SChristoph Hellwig return BLK_STS_OK;
2953777369fSQu Wenruo
2963777369fSQu Wenruo error:
2973777369fSQu Wenruo btrfs_print_tree(eb, 0);
2983777369fSQu Wenruo btrfs_err(fs_info, "block=%llu write time tree block corruption detected",
2993777369fSQu Wenruo eb->start);
30016199ad9SFilipe Manana /*
30116199ad9SFilipe Manana * Be noisy if this is an extent buffer from a log tree. We don't abort
30216199ad9SFilipe Manana * a transaction in case there's a bad log tree extent buffer, we just
30316199ad9SFilipe Manana * fallback to a transaction commit. Still we want to know when there is
30416199ad9SFilipe Manana * a bad log tree extent buffer, as that may signal a bug somewhere.
30516199ad9SFilipe Manana */
30616199ad9SFilipe Manana WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG) ||
30716199ad9SFilipe Manana btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID);
308deb6216fSChristoph Hellwig return errno_to_blk_status(ret);
309deb6216fSChristoph Hellwig }
310deb6216fSChristoph Hellwig
check_tree_block_fsid(struct extent_buffer * eb)311413fb1bcSAnand Jain static bool check_tree_block_fsid(struct extent_buffer *eb)
3122b82032cSYan Zheng {
313b0c9b3b0SDavid Sterba struct btrfs_fs_info *fs_info = eb->fs_info;
314944d3f9fSNikolay Borisov struct btrfs_fs_devices *fs_devices = fs_info->fs_devices, *seed_devs;
31544880fdcSAnand Jain u8 fsid[BTRFS_FSID_SIZE];
3162b82032cSYan Zheng
3179a8658e3SDavid Sterba read_extent_buffer(eb, fsid, offsetof(struct btrfs_header, fsid),
3189a8658e3SDavid Sterba BTRFS_FSID_SIZE);
3197239ff4bSNikolay Borisov
32067bc5ad0SAnand Jain /*
32167bc5ad0SAnand Jain * alloc_fs_devices() copies the fsid into metadata_uuid if the
32267bc5ad0SAnand Jain * metadata_uuid is unset in the superblock, including for a seed device.
32367bc5ad0SAnand Jain * So, we can use fs_devices->metadata_uuid.
32467bc5ad0SAnand Jain */
32567bc5ad0SAnand Jain if (memcmp(fsid, fs_info->fs_devices->metadata_uuid, BTRFS_FSID_SIZE) == 0)
326413fb1bcSAnand Jain return false;
327944d3f9fSNikolay Borisov
328944d3f9fSNikolay Borisov list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list)
329944d3f9fSNikolay Borisov if (!memcmp(fsid, seed_devs->fsid, BTRFS_FSID_SIZE))
330413fb1bcSAnand Jain return false;
331944d3f9fSNikolay Borisov
332413fb1bcSAnand Jain return true;
3332b82032cSYan Zheng }
3342b82032cSYan Zheng
33577bf40a2SQu Wenruo /* Do basic extent buffer checks at read time */
btrfs_validate_extent_buffer(struct extent_buffer * eb,struct btrfs_tree_parent_check * check)336046b562bSChristoph Hellwig int btrfs_validate_extent_buffer(struct extent_buffer *eb,
337947a6299SQu Wenruo struct btrfs_tree_parent_check *check)
338ce9adaa5SChris Mason {
33977bf40a2SQu Wenruo struct btrfs_fs_info *fs_info = eb->fs_info;
340ce9adaa5SChris Mason u64 found_start;
34177bf40a2SQu Wenruo const u32 csum_size = fs_info->csum_size;
34277bf40a2SQu Wenruo u8 found_level;
3432996e1f8SJohannes Thumshirn u8 result[BTRFS_CSUM_SIZE];
344dfd29eedSDavid Sterba const u8 *header_csum;
34577bf40a2SQu Wenruo int ret = 0;
346ea466794SJosef Bacik
347947a6299SQu Wenruo ASSERT(check);
348947a6299SQu Wenruo
349ce9adaa5SChris Mason found_start = btrfs_header_bytenr(eb);
350727011e0SChris Mason if (found_start != eb->start) {
3518f0ed7d4SQu Wenruo btrfs_err_rl(fs_info,
3528f0ed7d4SQu Wenruo "bad tree block start, mirror %u want %llu have %llu",
3538f0ed7d4SQu Wenruo eb->read_mirror, eb->start, found_start);
354f188591eSChris Mason ret = -EIO;
35577bf40a2SQu Wenruo goto out;
356ce9adaa5SChris Mason }
357b0c9b3b0SDavid Sterba if (check_tree_block_fsid(eb)) {
3588f0ed7d4SQu Wenruo btrfs_err_rl(fs_info, "bad fsid on logical %llu mirror %u",
3598f0ed7d4SQu Wenruo eb->start, eb->read_mirror);
3601259ab75SChris Mason ret = -EIO;
36177bf40a2SQu Wenruo goto out;
3621259ab75SChris Mason }
363ce9adaa5SChris Mason found_level = btrfs_header_level(eb);
3641c24c3ceSJosef Bacik if (found_level >= BTRFS_MAX_LEVEL) {
3658f0ed7d4SQu Wenruo btrfs_err(fs_info,
3668f0ed7d4SQu Wenruo "bad tree block level, mirror %u level %d on logical %llu",
3678f0ed7d4SQu Wenruo eb->read_mirror, btrfs_header_level(eb), eb->start);
3681c24c3ceSJosef Bacik ret = -EIO;
36977bf40a2SQu Wenruo goto out;
3701c24c3ceSJosef Bacik }
371ce9adaa5SChris Mason
372c67b3892SDavid Sterba csum_tree_block(eb, result);
373dfd29eedSDavid Sterba header_csum = page_address(eb->pages[0]) +
374dfd29eedSDavid Sterba get_eb_offset_in_page(eb, offsetof(struct btrfs_header, csum));
375a826d6dcSJosef Bacik
376dfd29eedSDavid Sterba if (memcmp(result, header_csum, csum_size) != 0) {
3772996e1f8SJohannes Thumshirn btrfs_warn_rl(fs_info,
3788f0ed7d4SQu Wenruo "checksum verify failed on logical %llu mirror %u wanted " CSUM_FMT " found " CSUM_FMT " level %d",
3798f0ed7d4SQu Wenruo eb->start, eb->read_mirror,
380dfd29eedSDavid Sterba CSUM_FMT_VALUE(csum_size, header_csum),
38135be8851SJohannes Thumshirn CSUM_FMT_VALUE(csum_size, result),
38235be8851SJohannes Thumshirn btrfs_header_level(eb));
3832996e1f8SJohannes Thumshirn ret = -EUCLEAN;
38477bf40a2SQu Wenruo goto out;
3852996e1f8SJohannes Thumshirn }
3862996e1f8SJohannes Thumshirn
387947a6299SQu Wenruo if (found_level != check->level) {
38877177ed1SQu Wenruo btrfs_err(fs_info,
38977177ed1SQu Wenruo "level verify failed on logical %llu mirror %u wanted %u found %u",
39077177ed1SQu Wenruo eb->start, eb->read_mirror, check->level, found_level);
391947a6299SQu Wenruo ret = -EIO;
392947a6299SQu Wenruo goto out;
393947a6299SQu Wenruo }
394947a6299SQu Wenruo if (unlikely(check->transid &&
395947a6299SQu Wenruo btrfs_header_generation(eb) != check->transid)) {
396947a6299SQu Wenruo btrfs_err_rl(eb->fs_info,
397947a6299SQu Wenruo "parent transid verify failed on logical %llu mirror %u wanted %llu found %llu",
398947a6299SQu Wenruo eb->start, eb->read_mirror, check->transid,
399947a6299SQu Wenruo btrfs_header_generation(eb));
400947a6299SQu Wenruo ret = -EIO;
401947a6299SQu Wenruo goto out;
402947a6299SQu Wenruo }
403947a6299SQu Wenruo if (check->has_first_key) {
404947a6299SQu Wenruo struct btrfs_key *expect_key = &check->first_key;
405947a6299SQu Wenruo struct btrfs_key found_key;
406947a6299SQu Wenruo
407947a6299SQu Wenruo if (found_level)
408947a6299SQu Wenruo btrfs_node_key_to_cpu(eb, &found_key, 0);
409947a6299SQu Wenruo else
410947a6299SQu Wenruo btrfs_item_key_to_cpu(eb, &found_key, 0);
411947a6299SQu Wenruo if (unlikely(btrfs_comp_cpu_keys(expect_key, &found_key))) {
412947a6299SQu Wenruo btrfs_err(fs_info,
413947a6299SQu Wenruo "tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)",
414947a6299SQu Wenruo eb->start, check->transid,
415947a6299SQu Wenruo expect_key->objectid,
416947a6299SQu Wenruo expect_key->type, expect_key->offset,
417947a6299SQu Wenruo found_key.objectid, found_key.type,
418947a6299SQu Wenruo found_key.offset);
419947a6299SQu Wenruo ret = -EUCLEAN;
420947a6299SQu Wenruo goto out;
421947a6299SQu Wenruo }
422947a6299SQu Wenruo }
423947a6299SQu Wenruo if (check->owner_root) {
424947a6299SQu Wenruo ret = btrfs_check_eb_owner(eb, check->owner_root);
425947a6299SQu Wenruo if (ret < 0)
426947a6299SQu Wenruo goto out;
427947a6299SQu Wenruo }
428947a6299SQu Wenruo
429a826d6dcSJosef Bacik /*
430a826d6dcSJosef Bacik * If this is a leaf block and it is corrupt, set the corrupt bit so
431a826d6dcSJosef Bacik * that we don't try and read the other copies of this block, just
432a826d6dcSJosef Bacik * return -EIO.
433a826d6dcSJosef Bacik */
43485d8a826SJosef Bacik if (found_level == 0 && btrfs_check_leaf(eb)) {
435a826d6dcSJosef Bacik set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags);
436a826d6dcSJosef Bacik ret = -EIO;
437a826d6dcSJosef Bacik }
438ce9adaa5SChris Mason
439813fd1dcSDavid Sterba if (found_level > 0 && btrfs_check_node(eb))
440053ab70fSLiu Bo ret = -EIO;
441053ab70fSLiu Bo
442aebcc159SChristoph Hellwig if (ret)
44375391f0dSQu Wenruo btrfs_err(fs_info,
4448f0ed7d4SQu Wenruo "read time tree block corruption detected on logical %llu mirror %u",
4458f0ed7d4SQu Wenruo eb->start, eb->read_mirror);
44677bf40a2SQu Wenruo out:
44777bf40a2SQu Wenruo return ret;
44877bf40a2SQu Wenruo }
44977bf40a2SQu Wenruo
4503dd1462eSJan Beulich #ifdef CONFIG_MIGRATION
btree_migrate_folio(struct address_space * mapping,struct folio * dst,struct folio * src,enum migrate_mode mode)4518958b551SMatthew Wilcox (Oracle) static int btree_migrate_folio(struct address_space *mapping,
4528958b551SMatthew Wilcox (Oracle) struct folio *dst, struct folio *src, enum migrate_mode mode)
453784b4e29SChris Mason {
454784b4e29SChris Mason /*
455784b4e29SChris Mason * we can't safely write a btree page from here,
456784b4e29SChris Mason * we haven't done the locking hook
457784b4e29SChris Mason */
4588958b551SMatthew Wilcox (Oracle) if (folio_test_dirty(src))
459784b4e29SChris Mason return -EAGAIN;
460784b4e29SChris Mason /*
461784b4e29SChris Mason * Buffers may be managed in a filesystem specific way.
462784b4e29SChris Mason * We must have no buffers or drop them.
463784b4e29SChris Mason */
4648958b551SMatthew Wilcox (Oracle) if (folio_get_private(src) &&
4658958b551SMatthew Wilcox (Oracle) !filemap_release_folio(src, GFP_KERNEL))
466784b4e29SChris Mason return -EAGAIN;
46754184650SMatthew Wilcox (Oracle) return migrate_folio(mapping, dst, src, mode);
468784b4e29SChris Mason }
4698958b551SMatthew Wilcox (Oracle) #else
4708958b551SMatthew Wilcox (Oracle) #define btree_migrate_folio NULL
4713dd1462eSJan Beulich #endif
472784b4e29SChris Mason
btree_writepages(struct address_space * mapping,struct writeback_control * wbc)4730da5468fSChris Mason static int btree_writepages(struct address_space *mapping,
4740da5468fSChris Mason struct writeback_control *wbc)
4750da5468fSChris Mason {
476e2d84521SMiao Xie struct btrfs_fs_info *fs_info;
477e2d84521SMiao Xie int ret;
478e2d84521SMiao Xie
479d8d5f3e1SChris Mason if (wbc->sync_mode == WB_SYNC_NONE) {
480448d640bSChris Mason
481448d640bSChris Mason if (wbc->for_kupdate)
482448d640bSChris Mason return 0;
483448d640bSChris Mason
484e2d84521SMiao Xie fs_info = BTRFS_I(mapping->host)->root->fs_info;
485b9473439SChris Mason /* this is a bit racy, but that's ok */
486d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes,
487d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH,
488d814a491SEthan Lien fs_info->dirty_metadata_batch);
489e2d84521SMiao Xie if (ret < 0)
490793955bcSChris Mason return 0;
491793955bcSChris Mason }
4920b32f4bbSJosef Bacik return btree_write_cache_pages(mapping, wbc);
4930da5468fSChris Mason }
4940da5468fSChris Mason
btree_release_folio(struct folio * folio,gfp_t gfp_flags)495f913cff3SMatthew Wilcox (Oracle) static bool btree_release_folio(struct folio *folio, gfp_t gfp_flags)
4965f39d397SChris Mason {
497f913cff3SMatthew Wilcox (Oracle) if (folio_test_writeback(folio) || folio_test_dirty(folio))
498f913cff3SMatthew Wilcox (Oracle) return false;
4990c4e538bSDavid Sterba
500f913cff3SMatthew Wilcox (Oracle) return try_release_extent_buffer(&folio->page);
501d98237b3SChris Mason }
502d98237b3SChris Mason
btree_invalidate_folio(struct folio * folio,size_t offset,size_t length)503895586ebSMatthew Wilcox (Oracle) static void btree_invalidate_folio(struct folio *folio, size_t offset,
504895586ebSMatthew Wilcox (Oracle) size_t length)
505d98237b3SChris Mason {
506d1310b2eSChris Mason struct extent_io_tree *tree;
507895586ebSMatthew Wilcox (Oracle) tree = &BTRFS_I(folio->mapping->host)->io_tree;
508895586ebSMatthew Wilcox (Oracle) extent_invalidate_folio(tree, folio, offset);
509f913cff3SMatthew Wilcox (Oracle) btree_release_folio(folio, GFP_NOFS);
510895586ebSMatthew Wilcox (Oracle) if (folio_get_private(folio)) {
511895586ebSMatthew Wilcox (Oracle) btrfs_warn(BTRFS_I(folio->mapping->host)->root->fs_info,
512895586ebSMatthew Wilcox (Oracle) "folio private not zero on folio %llu",
513895586ebSMatthew Wilcox (Oracle) (unsigned long long)folio_pos(folio));
514895586ebSMatthew Wilcox (Oracle) folio_detach_private(folio);
5159ad6b7bcSChris Mason }
516d98237b3SChris Mason }
517d98237b3SChris Mason
518bb146eb2SJosef Bacik #ifdef DEBUG
btree_dirty_folio(struct address_space * mapping,struct folio * folio)5190079c3b1SMatthew Wilcox (Oracle) static bool btree_dirty_folio(struct address_space *mapping,
5200079c3b1SMatthew Wilcox (Oracle) struct folio *folio)
5210079c3b1SMatthew Wilcox (Oracle) {
5220079c3b1SMatthew Wilcox (Oracle) struct btrfs_fs_info *fs_info = btrfs_sb(mapping->host->i_sb);
5235e0e8799SQu Wenruo struct btrfs_subpage_info *spi = fs_info->subpage_info;
524139e8cd3SQu Wenruo struct btrfs_subpage *subpage;
5250b32f4bbSJosef Bacik struct extent_buffer *eb;
526139e8cd3SQu Wenruo int cur_bit = 0;
5270079c3b1SMatthew Wilcox (Oracle) u64 page_start = folio_pos(folio);
5280b32f4bbSJosef Bacik
529139e8cd3SQu Wenruo if (fs_info->sectorsize == PAGE_SIZE) {
5300079c3b1SMatthew Wilcox (Oracle) eb = folio_get_private(folio);
5310b32f4bbSJosef Bacik BUG_ON(!eb);
5320b32f4bbSJosef Bacik BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
5330b32f4bbSJosef Bacik BUG_ON(!atomic_read(&eb->refs));
53449d0c642SFilipe Manana btrfs_assert_tree_write_locked(eb);
5350079c3b1SMatthew Wilcox (Oracle) return filemap_dirty_folio(mapping, folio);
536139e8cd3SQu Wenruo }
5375e0e8799SQu Wenruo
5385e0e8799SQu Wenruo ASSERT(spi);
5390079c3b1SMatthew Wilcox (Oracle) subpage = folio_get_private(folio);
540139e8cd3SQu Wenruo
5415e0e8799SQu Wenruo for (cur_bit = spi->dirty_offset;
5425e0e8799SQu Wenruo cur_bit < spi->dirty_offset + spi->bitmap_nr_bits;
5435e0e8799SQu Wenruo cur_bit++) {
544139e8cd3SQu Wenruo unsigned long flags;
545139e8cd3SQu Wenruo u64 cur;
546139e8cd3SQu Wenruo
547139e8cd3SQu Wenruo spin_lock_irqsave(&subpage->lock, flags);
5485e0e8799SQu Wenruo if (!test_bit(cur_bit, subpage->bitmaps)) {
549139e8cd3SQu Wenruo spin_unlock_irqrestore(&subpage->lock, flags);
550139e8cd3SQu Wenruo continue;
551139e8cd3SQu Wenruo }
552139e8cd3SQu Wenruo spin_unlock_irqrestore(&subpage->lock, flags);
553139e8cd3SQu Wenruo cur = page_start + cur_bit * fs_info->sectorsize;
554139e8cd3SQu Wenruo
555139e8cd3SQu Wenruo eb = find_extent_buffer(fs_info, cur);
556139e8cd3SQu Wenruo ASSERT(eb);
557139e8cd3SQu Wenruo ASSERT(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
558139e8cd3SQu Wenruo ASSERT(atomic_read(&eb->refs));
55949d0c642SFilipe Manana btrfs_assert_tree_write_locked(eb);
560139e8cd3SQu Wenruo free_extent_buffer(eb);
561139e8cd3SQu Wenruo
5625e0e8799SQu Wenruo cur_bit += (fs_info->nodesize >> fs_info->sectorsize_bits) - 1;
563139e8cd3SQu Wenruo }
5640079c3b1SMatthew Wilcox (Oracle) return filemap_dirty_folio(mapping, folio);
5650b32f4bbSJosef Bacik }
5660079c3b1SMatthew Wilcox (Oracle) #else
5670079c3b1SMatthew Wilcox (Oracle) #define btree_dirty_folio filemap_dirty_folio
5680079c3b1SMatthew Wilcox (Oracle) #endif
5690b32f4bbSJosef Bacik
5707f09410bSAlexey Dobriyan static const struct address_space_operations btree_aops = {
5710da5468fSChris Mason .writepages = btree_writepages,
572f913cff3SMatthew Wilcox (Oracle) .release_folio = btree_release_folio,
573895586ebSMatthew Wilcox (Oracle) .invalidate_folio = btree_invalidate_folio,
5748958b551SMatthew Wilcox (Oracle) .migrate_folio = btree_migrate_folio,
5750079c3b1SMatthew Wilcox (Oracle) .dirty_folio = btree_dirty_folio,
576d98237b3SChris Mason };
577123abc88SChris Mason
btrfs_find_create_tree_block(struct btrfs_fs_info * fs_info,u64 bytenr,u64 owner_root,int level)5782ff7e61eSJeff Mahoney struct extent_buffer *btrfs_find_create_tree_block(
5792ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info,
5803fbaf258SJosef Bacik u64 bytenr, u64 owner_root,
5813fbaf258SJosef Bacik int level)
5820999df54SChris Mason {
5830b246afaSJeff Mahoney if (btrfs_is_testing(fs_info))
5840b246afaSJeff Mahoney return alloc_test_extent_buffer(fs_info, bytenr);
5853fbaf258SJosef Bacik return alloc_extent_buffer(fs_info, bytenr, owner_root, level);
5860999df54SChris Mason }
5870999df54SChris Mason
588581c1760SQu Wenruo /*
589581c1760SQu Wenruo * Read tree block at logical address @bytenr and do variant basic but critical
590581c1760SQu Wenruo * verification.
591581c1760SQu Wenruo *
592789d6a3aSQu Wenruo * @check: expected tree parentness check, see comments of the
593789d6a3aSQu Wenruo * structure for details.
594581c1760SQu Wenruo */
read_tree_block(struct btrfs_fs_info * fs_info,u64 bytenr,struct btrfs_tree_parent_check * check)5952ff7e61eSJeff Mahoney struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr,
596789d6a3aSQu Wenruo struct btrfs_tree_parent_check *check)
597e20d96d6SChris Mason {
5985f39d397SChris Mason struct extent_buffer *buf = NULL;
59919c00ddcSChris Mason int ret;
60019c00ddcSChris Mason
601789d6a3aSQu Wenruo ASSERT(check);
602789d6a3aSQu Wenruo
603789d6a3aSQu Wenruo buf = btrfs_find_create_tree_block(fs_info, bytenr, check->owner_root,
604789d6a3aSQu Wenruo check->level);
605c871b0f2SLiu Bo if (IS_ERR(buf))
606c871b0f2SLiu Bo return buf;
607e4204dedSChris Mason
608789d6a3aSQu Wenruo ret = btrfs_read_extent_buffer(buf, check);
6090f0fe8f7SFilipe David Borba Manana if (ret) {
610537f38f0SNikolay Borisov free_extent_buffer_stale(buf);
61164c043deSLiu Bo return ERR_PTR(ret);
6120f0fe8f7SFilipe David Borba Manana }
613789d6a3aSQu Wenruo if (btrfs_check_eb_owner(buf, check->owner_root)) {
61488c602abSQu Wenruo free_extent_buffer_stale(buf);
61588c602abSQu Wenruo return ERR_PTR(-EUCLEAN);
61688c602abSQu Wenruo }
6175f39d397SChris Mason return buf;
618ce9adaa5SChris Mason
619eb60ceacSChris Mason }
620eb60ceacSChris Mason
__setup_root(struct btrfs_root * root,struct btrfs_fs_info * fs_info,u64 objectid)621da17066cSJeff Mahoney static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info,
622e20d96d6SChris Mason u64 objectid)
623d97e63b6SChris Mason {
6247c0260eeSJeff Mahoney bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
6252e608bd1SJosef Bacik
6262e608bd1SJosef Bacik memset(&root->root_key, 0, sizeof(root->root_key));
6272e608bd1SJosef Bacik memset(&root->root_item, 0, sizeof(root->root_item));
6282e608bd1SJosef Bacik memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
62996dfcb46SJosef Bacik root->fs_info = fs_info;
6302e608bd1SJosef Bacik root->root_key.objectid = objectid;
631cfaa7295SChris Mason root->node = NULL;
632a28ec197SChris Mason root->commit_root = NULL;
63327cdeb70SMiao Xie root->state = 0;
634abed4aaaSJosef Bacik RB_CLEAR_NODE(&root->rb_node);
6350b86a832SChris Mason
6360f7d52f4SChris Mason root->last_trans = 0;
6376b8fad57SNikolay Borisov root->free_objectid = 0;
638eb73c1b7SMiao Xie root->nr_delalloc_inodes = 0;
639199c2a9cSMiao Xie root->nr_ordered_extents = 0;
6406bef4d31SEric Paris root->inode_tree = RB_ROOT;
641088aea3bSDavid Sterba INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC);
6422e608bd1SJosef Bacik
6432e608bd1SJosef Bacik btrfs_init_root_block_rsv(root);
6440b86a832SChris Mason
6450b86a832SChris Mason INIT_LIST_HEAD(&root->dirty_list);
6465d4f98a2SYan Zheng INIT_LIST_HEAD(&root->root_list);
647eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_inodes);
648eb73c1b7SMiao Xie INIT_LIST_HEAD(&root->delalloc_root);
649199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_extents);
650199c2a9cSMiao Xie INIT_LIST_HEAD(&root->ordered_root);
651d2311e69SQu Wenruo INIT_LIST_HEAD(&root->reloc_dirty_list);
6522ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[0]);
6532ab28f32SJosef Bacik INIT_LIST_HEAD(&root->logged_list[1]);
6545d4f98a2SYan Zheng spin_lock_init(&root->inode_lock);
655eb73c1b7SMiao Xie spin_lock_init(&root->delalloc_lock);
656199c2a9cSMiao Xie spin_lock_init(&root->ordered_extent_lock);
657f0486c68SYan, Zheng spin_lock_init(&root->accounting_lock);
6582ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[0]);
6592ab28f32SJosef Bacik spin_lock_init(&root->log_extents_lock[1]);
6608287475aSQu Wenruo spin_lock_init(&root->qgroup_meta_rsv_lock);
661a2135011SChris Mason mutex_init(&root->objectid_mutex);
662e02119d5SChris Mason mutex_init(&root->log_mutex);
66331f3d255SMiao Xie mutex_init(&root->ordered_extent_mutex);
664573bfb72SMiao Xie mutex_init(&root->delalloc_mutex);
665c53e9653SQu Wenruo init_waitqueue_head(&root->qgroup_flush_wait);
6667237f183SYan Zheng init_waitqueue_head(&root->log_writer_wait);
6677237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[0]);
6687237f183SYan Zheng init_waitqueue_head(&root->log_commit_wait[1]);
6698b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[0]);
6708b050d35SMiao Xie INIT_LIST_HEAD(&root->log_ctxs[1]);
6717237f183SYan Zheng atomic_set(&root->log_commit[0], 0);
6727237f183SYan Zheng atomic_set(&root->log_commit[1], 0);
6737237f183SYan Zheng atomic_set(&root->log_writers, 0);
6742ecb7923SMiao Xie atomic_set(&root->log_batch, 0);
6750700cea7SElena Reshetova refcount_set(&root->refs, 1);
6768ecebf4dSRobbie Ko atomic_set(&root->snapshot_force_cow, 0);
677eede2bf3SOmar Sandoval atomic_set(&root->nr_swapfiles, 0);
6787237f183SYan Zheng root->log_transid = 0;
679d1433debSMiao Xie root->log_transid_committed = -1;
680257c62e1SChris Mason root->last_log_commit = 0;
6812e608bd1SJosef Bacik root->anon_dev = 0;
682e289f03eSFilipe Manana if (!dummy) {
68343eb5f29SQu Wenruo extent_io_tree_init(fs_info, &root->dirty_log_pages,
68435da5a7eSDavid Sterba IO_TREE_ROOT_DIRTY_LOG_PAGES);
685e289f03eSFilipe Manana extent_io_tree_init(fs_info, &root->log_csum_range,
68635da5a7eSDavid Sterba IO_TREE_LOG_CSUM_RANGE);
687e289f03eSFilipe Manana }
688017e5369SChris Mason
6895f3ab90aSAnand Jain spin_lock_init(&root->root_item_lock);
690370a11b8SQu Wenruo btrfs_qgroup_init_swapped_blocks(&root->swapped_blocks);
691bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
692bd647ce3SJosef Bacik INIT_LIST_HEAD(&root->leak_list);
693fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock);
694bd647ce3SJosef Bacik list_add_tail(&root->leak_list, &fs_info->allocated_roots);
695fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock);
696bd647ce3SJosef Bacik #endif
6973768f368SChris Mason }
6983768f368SChris Mason
btrfs_alloc_root(struct btrfs_fs_info * fs_info,u64 objectid,gfp_t flags)69974e4d827SDavid Sterba static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info,
70096dfcb46SJosef Bacik u64 objectid, gfp_t flags)
7016f07e42eSAl Viro {
70274e4d827SDavid Sterba struct btrfs_root *root = kzalloc(sizeof(*root), flags);
7036f07e42eSAl Viro if (root)
70496dfcb46SJosef Bacik __setup_root(root, fs_info, objectid);
7056f07e42eSAl Viro return root;
7066f07e42eSAl Viro }
7076f07e42eSAl Viro
70806ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
70906ea65a3SJosef Bacik /* Should only be used by the testing infrastructure */
btrfs_alloc_dummy_root(struct btrfs_fs_info * fs_info)710da17066cSJeff Mahoney struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info)
71106ea65a3SJosef Bacik {
71206ea65a3SJosef Bacik struct btrfs_root *root;
71306ea65a3SJosef Bacik
7147c0260eeSJeff Mahoney if (!fs_info)
7157c0260eeSJeff Mahoney return ERR_PTR(-EINVAL);
7167c0260eeSJeff Mahoney
71796dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, GFP_KERNEL);
71806ea65a3SJosef Bacik if (!root)
71906ea65a3SJosef Bacik return ERR_PTR(-ENOMEM);
720da17066cSJeff Mahoney
721b9ef22deSFeifei Xu /* We don't use the stripesize in selftest, set it as sectorsize */
722faa2dbf0SJosef Bacik root->alloc_bytenr = 0;
72306ea65a3SJosef Bacik
72406ea65a3SJosef Bacik return root;
72506ea65a3SJosef Bacik }
72606ea65a3SJosef Bacik #endif
72706ea65a3SJosef Bacik
global_root_cmp(struct rb_node * a_node,const struct rb_node * b_node)728abed4aaaSJosef Bacik static int global_root_cmp(struct rb_node *a_node, const struct rb_node *b_node)
729abed4aaaSJosef Bacik {
730abed4aaaSJosef Bacik const struct btrfs_root *a = rb_entry(a_node, struct btrfs_root, rb_node);
731abed4aaaSJosef Bacik const struct btrfs_root *b = rb_entry(b_node, struct btrfs_root, rb_node);
732abed4aaaSJosef Bacik
733abed4aaaSJosef Bacik return btrfs_comp_cpu_keys(&a->root_key, &b->root_key);
734abed4aaaSJosef Bacik }
735abed4aaaSJosef Bacik
global_root_key_cmp(const void * k,const struct rb_node * node)736abed4aaaSJosef Bacik static int global_root_key_cmp(const void *k, const struct rb_node *node)
737abed4aaaSJosef Bacik {
738abed4aaaSJosef Bacik const struct btrfs_key *key = k;
739abed4aaaSJosef Bacik const struct btrfs_root *root = rb_entry(node, struct btrfs_root, rb_node);
740abed4aaaSJosef Bacik
741abed4aaaSJosef Bacik return btrfs_comp_cpu_keys(key, &root->root_key);
742abed4aaaSJosef Bacik }
743abed4aaaSJosef Bacik
btrfs_global_root_insert(struct btrfs_root * root)744abed4aaaSJosef Bacik int btrfs_global_root_insert(struct btrfs_root *root)
745abed4aaaSJosef Bacik {
746abed4aaaSJosef Bacik struct btrfs_fs_info *fs_info = root->fs_info;
747abed4aaaSJosef Bacik struct rb_node *tmp;
748745806fbSQu Wenruo int ret = 0;
749abed4aaaSJosef Bacik
750abed4aaaSJosef Bacik write_lock(&fs_info->global_root_lock);
751abed4aaaSJosef Bacik tmp = rb_find_add(&root->rb_node, &fs_info->global_root_tree, global_root_cmp);
752abed4aaaSJosef Bacik write_unlock(&fs_info->global_root_lock);
753abed4aaaSJosef Bacik
754745806fbSQu Wenruo if (tmp) {
755745806fbSQu Wenruo ret = -EEXIST;
756745806fbSQu Wenruo btrfs_warn(fs_info, "global root %llu %llu already exists",
757745806fbSQu Wenruo root->root_key.objectid, root->root_key.offset);
758745806fbSQu Wenruo }
759745806fbSQu Wenruo return ret;
760abed4aaaSJosef Bacik }
761abed4aaaSJosef Bacik
btrfs_global_root_delete(struct btrfs_root * root)762abed4aaaSJosef Bacik void btrfs_global_root_delete(struct btrfs_root *root)
763abed4aaaSJosef Bacik {
764abed4aaaSJosef Bacik struct btrfs_fs_info *fs_info = root->fs_info;
765abed4aaaSJosef Bacik
766abed4aaaSJosef Bacik write_lock(&fs_info->global_root_lock);
767abed4aaaSJosef Bacik rb_erase(&root->rb_node, &fs_info->global_root_tree);
768abed4aaaSJosef Bacik write_unlock(&fs_info->global_root_lock);
769abed4aaaSJosef Bacik }
770abed4aaaSJosef Bacik
btrfs_global_root(struct btrfs_fs_info * fs_info,struct btrfs_key * key)771abed4aaaSJosef Bacik struct btrfs_root *btrfs_global_root(struct btrfs_fs_info *fs_info,
772abed4aaaSJosef Bacik struct btrfs_key *key)
773abed4aaaSJosef Bacik {
774abed4aaaSJosef Bacik struct rb_node *node;
775abed4aaaSJosef Bacik struct btrfs_root *root = NULL;
776abed4aaaSJosef Bacik
777abed4aaaSJosef Bacik read_lock(&fs_info->global_root_lock);
778abed4aaaSJosef Bacik node = rb_find(key, &fs_info->global_root_tree, global_root_key_cmp);
779abed4aaaSJosef Bacik if (node)
780abed4aaaSJosef Bacik root = container_of(node, struct btrfs_root, rb_node);
781abed4aaaSJosef Bacik read_unlock(&fs_info->global_root_lock);
782abed4aaaSJosef Bacik
783abed4aaaSJosef Bacik return root;
784abed4aaaSJosef Bacik }
785abed4aaaSJosef Bacik
btrfs_global_root_id(struct btrfs_fs_info * fs_info,u64 bytenr)786f7238e50SJosef Bacik static u64 btrfs_global_root_id(struct btrfs_fs_info *fs_info, u64 bytenr)
787f7238e50SJosef Bacik {
788f7238e50SJosef Bacik struct btrfs_block_group *block_group;
789f7238e50SJosef Bacik u64 ret;
790f7238e50SJosef Bacik
791f7238e50SJosef Bacik if (!btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
792f7238e50SJosef Bacik return 0;
793f7238e50SJosef Bacik
794f7238e50SJosef Bacik if (bytenr)
795f7238e50SJosef Bacik block_group = btrfs_lookup_block_group(fs_info, bytenr);
796f7238e50SJosef Bacik else
797f7238e50SJosef Bacik block_group = btrfs_lookup_first_block_group(fs_info, bytenr);
798f7238e50SJosef Bacik ASSERT(block_group);
799f7238e50SJosef Bacik if (!block_group)
800f7238e50SJosef Bacik return 0;
801f7238e50SJosef Bacik ret = block_group->global_root_id;
802f7238e50SJosef Bacik btrfs_put_block_group(block_group);
803f7238e50SJosef Bacik
804f7238e50SJosef Bacik return ret;
805f7238e50SJosef Bacik }
806f7238e50SJosef Bacik
btrfs_csum_root(struct btrfs_fs_info * fs_info,u64 bytenr)807abed4aaaSJosef Bacik struct btrfs_root *btrfs_csum_root(struct btrfs_fs_info *fs_info, u64 bytenr)
808abed4aaaSJosef Bacik {
809abed4aaaSJosef Bacik struct btrfs_key key = {
810abed4aaaSJosef Bacik .objectid = BTRFS_CSUM_TREE_OBJECTID,
811abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY,
812f7238e50SJosef Bacik .offset = btrfs_global_root_id(fs_info, bytenr),
813abed4aaaSJosef Bacik };
814abed4aaaSJosef Bacik
815abed4aaaSJosef Bacik return btrfs_global_root(fs_info, &key);
816abed4aaaSJosef Bacik }
817abed4aaaSJosef Bacik
btrfs_extent_root(struct btrfs_fs_info * fs_info,u64 bytenr)818abed4aaaSJosef Bacik struct btrfs_root *btrfs_extent_root(struct btrfs_fs_info *fs_info, u64 bytenr)
819abed4aaaSJosef Bacik {
820abed4aaaSJosef Bacik struct btrfs_key key = {
821abed4aaaSJosef Bacik .objectid = BTRFS_EXTENT_TREE_OBJECTID,
822abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY,
823f7238e50SJosef Bacik .offset = btrfs_global_root_id(fs_info, bytenr),
824abed4aaaSJosef Bacik };
825abed4aaaSJosef Bacik
826abed4aaaSJosef Bacik return btrfs_global_root(fs_info, &key);
827abed4aaaSJosef Bacik }
828abed4aaaSJosef Bacik
btrfs_block_group_root(struct btrfs_fs_info * fs_info)82951129b33SJosef Bacik struct btrfs_root *btrfs_block_group_root(struct btrfs_fs_info *fs_info)
83051129b33SJosef Bacik {
83151129b33SJosef Bacik if (btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE))
83251129b33SJosef Bacik return fs_info->block_group_root;
83351129b33SJosef Bacik return btrfs_extent_root(fs_info, 0);
83451129b33SJosef Bacik }
83551129b33SJosef Bacik
btrfs_create_tree(struct btrfs_trans_handle * trans,u64 objectid)83620897f5cSArne Jansen struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
83720897f5cSArne Jansen u64 objectid)
83820897f5cSArne Jansen {
8399b7a2440SDavid Sterba struct btrfs_fs_info *fs_info = trans->fs_info;
84020897f5cSArne Jansen struct extent_buffer *leaf;
84120897f5cSArne Jansen struct btrfs_root *tree_root = fs_info->tree_root;
84220897f5cSArne Jansen struct btrfs_root *root;
84320897f5cSArne Jansen struct btrfs_key key;
844b89f6d1fSFilipe Manana unsigned int nofs_flag;
84520897f5cSArne Jansen int ret = 0;
84620897f5cSArne Jansen
847b89f6d1fSFilipe Manana /*
848b89f6d1fSFilipe Manana * We're holding a transaction handle, so use a NOFS memory allocation
849b89f6d1fSFilipe Manana * context to avoid deadlock if reclaim happens.
850b89f6d1fSFilipe Manana */
851b89f6d1fSFilipe Manana nofs_flag = memalloc_nofs_save();
85296dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, objectid, GFP_KERNEL);
853b89f6d1fSFilipe Manana memalloc_nofs_restore(nofs_flag);
85420897f5cSArne Jansen if (!root)
85520897f5cSArne Jansen return ERR_PTR(-ENOMEM);
85620897f5cSArne Jansen
85720897f5cSArne Jansen root->root_key.objectid = objectid;
85820897f5cSArne Jansen root->root_key.type = BTRFS_ROOT_ITEM_KEY;
85920897f5cSArne Jansen root->root_key.offset = 0;
86020897f5cSArne Jansen
8619631e4ccSJosef Bacik leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0,
8629631e4ccSJosef Bacik BTRFS_NESTING_NORMAL);
86320897f5cSArne Jansen if (IS_ERR(leaf)) {
86420897f5cSArne Jansen ret = PTR_ERR(leaf);
8651dd05682STsutomu Itoh leaf = NULL;
866c1b07854SPeng Hao goto fail;
86720897f5cSArne Jansen }
86820897f5cSArne Jansen
86920897f5cSArne Jansen root->node = leaf;
870d5e09e38SFilipe Manana btrfs_mark_buffer_dirty(trans, leaf);
87120897f5cSArne Jansen
87220897f5cSArne Jansen root->commit_root = btrfs_root_node(root);
87327cdeb70SMiao Xie set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
87420897f5cSArne Jansen
875f944d2cbSDavid Sterba btrfs_set_root_flags(&root->root_item, 0);
876f944d2cbSDavid Sterba btrfs_set_root_limit(&root->root_item, 0);
87720897f5cSArne Jansen btrfs_set_root_bytenr(&root->root_item, leaf->start);
87820897f5cSArne Jansen btrfs_set_root_generation(&root->root_item, trans->transid);
87920897f5cSArne Jansen btrfs_set_root_level(&root->root_item, 0);
88020897f5cSArne Jansen btrfs_set_root_refs(&root->root_item, 1);
88120897f5cSArne Jansen btrfs_set_root_used(&root->root_item, leaf->len);
88220897f5cSArne Jansen btrfs_set_root_last_snapshot(&root->root_item, 0);
88320897f5cSArne Jansen btrfs_set_root_dirid(&root->root_item, 0);
88433d85fdaSQu Wenruo if (is_fstree(objectid))
885807fc790SAndy Shevchenko generate_random_guid(root->root_item.uuid);
886807fc790SAndy Shevchenko else
887807fc790SAndy Shevchenko export_guid(root->root_item.uuid, &guid_null);
888c8422684SDavid Sterba btrfs_set_root_drop_level(&root->root_item, 0);
88920897f5cSArne Jansen
8908a6a87cdSBoris Burkov btrfs_tree_unlock(leaf);
8918a6a87cdSBoris Burkov
89220897f5cSArne Jansen key.objectid = objectid;
89320897f5cSArne Jansen key.type = BTRFS_ROOT_ITEM_KEY;
89420897f5cSArne Jansen key.offset = 0;
89520897f5cSArne Jansen ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item);
89620897f5cSArne Jansen if (ret)
89720897f5cSArne Jansen goto fail;
89820897f5cSArne Jansen
89920897f5cSArne Jansen return root;
9001dd05682STsutomu Itoh
9018a6a87cdSBoris Burkov fail:
90200246528SJosef Bacik btrfs_put_root(root);
9031dd05682STsutomu Itoh
9041dd05682STsutomu Itoh return ERR_PTR(ret);
90520897f5cSArne Jansen }
90620897f5cSArne Jansen
alloc_log_tree(struct btrfs_trans_handle * trans,struct btrfs_fs_info * fs_info)9077237f183SYan Zheng static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans,
908e02119d5SChris Mason struct btrfs_fs_info *fs_info)
9090f7d52f4SChris Mason {
9100f7d52f4SChris Mason struct btrfs_root *root;
911e02119d5SChris Mason
91296dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, GFP_NOFS);
913e02119d5SChris Mason if (!root)
9147237f183SYan Zheng return ERR_PTR(-ENOMEM);
915e02119d5SChris Mason
916e02119d5SChris Mason root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID;
917e02119d5SChris Mason root->root_key.type = BTRFS_ROOT_ITEM_KEY;
918e02119d5SChris Mason root->root_key.offset = BTRFS_TREE_LOG_OBJECTID;
91927cdeb70SMiao Xie
9206ab6ebb7SNaohiro Aota return root;
9216ab6ebb7SNaohiro Aota }
9226ab6ebb7SNaohiro Aota
btrfs_alloc_log_tree_node(struct btrfs_trans_handle * trans,struct btrfs_root * root)9236ab6ebb7SNaohiro Aota int btrfs_alloc_log_tree_node(struct btrfs_trans_handle *trans,
9246ab6ebb7SNaohiro Aota struct btrfs_root *root)
9256ab6ebb7SNaohiro Aota {
9266ab6ebb7SNaohiro Aota struct extent_buffer *leaf;
9276ab6ebb7SNaohiro Aota
9287237f183SYan Zheng /*
92992a7cc42SQu Wenruo * DON'T set SHAREABLE bit for log trees.
93027cdeb70SMiao Xie *
93192a7cc42SQu Wenruo * Log trees are not exposed to user space thus can't be snapshotted,
93292a7cc42SQu Wenruo * and they go away before a real commit is actually done.
93392a7cc42SQu Wenruo *
93492a7cc42SQu Wenruo * They do store pointers to file data extents, and those reference
93592a7cc42SQu Wenruo * counts still get updated (along with back refs to the log tree).
9367237f183SYan Zheng */
937e02119d5SChris Mason
9384d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID,
9399631e4ccSJosef Bacik NULL, 0, 0, 0, BTRFS_NESTING_NORMAL);
9406ab6ebb7SNaohiro Aota if (IS_ERR(leaf))
9416ab6ebb7SNaohiro Aota return PTR_ERR(leaf);
942e02119d5SChris Mason
9437237f183SYan Zheng root->node = leaf;
944e02119d5SChris Mason
945d5e09e38SFilipe Manana btrfs_mark_buffer_dirty(trans, root->node);
946e02119d5SChris Mason btrfs_tree_unlock(root->node);
9476ab6ebb7SNaohiro Aota
9486ab6ebb7SNaohiro Aota return 0;
9497237f183SYan Zheng }
9507237f183SYan Zheng
btrfs_init_log_root_tree(struct btrfs_trans_handle * trans,struct btrfs_fs_info * fs_info)9517237f183SYan Zheng int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans,
9527237f183SYan Zheng struct btrfs_fs_info *fs_info)
9537237f183SYan Zheng {
9547237f183SYan Zheng struct btrfs_root *log_root;
9557237f183SYan Zheng
9567237f183SYan Zheng log_root = alloc_log_tree(trans, fs_info);
9577237f183SYan Zheng if (IS_ERR(log_root))
9587237f183SYan Zheng return PTR_ERR(log_root);
9596ab6ebb7SNaohiro Aota
9603ddebf27SNaohiro Aota if (!btrfs_is_zoned(fs_info)) {
9613ddebf27SNaohiro Aota int ret = btrfs_alloc_log_tree_node(trans, log_root);
9623ddebf27SNaohiro Aota
9636ab6ebb7SNaohiro Aota if (ret) {
9646ab6ebb7SNaohiro Aota btrfs_put_root(log_root);
9656ab6ebb7SNaohiro Aota return ret;
9666ab6ebb7SNaohiro Aota }
9673ddebf27SNaohiro Aota }
9686ab6ebb7SNaohiro Aota
9697237f183SYan Zheng WARN_ON(fs_info->log_root_tree);
9707237f183SYan Zheng fs_info->log_root_tree = log_root;
9717237f183SYan Zheng return 0;
9727237f183SYan Zheng }
9737237f183SYan Zheng
btrfs_add_log_tree(struct btrfs_trans_handle * trans,struct btrfs_root * root)9747237f183SYan Zheng int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
9757237f183SYan Zheng struct btrfs_root *root)
9767237f183SYan Zheng {
9770b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info;
9787237f183SYan Zheng struct btrfs_root *log_root;
9797237f183SYan Zheng struct btrfs_inode_item *inode_item;
9806ab6ebb7SNaohiro Aota int ret;
9817237f183SYan Zheng
9820b246afaSJeff Mahoney log_root = alloc_log_tree(trans, fs_info);
9837237f183SYan Zheng if (IS_ERR(log_root))
9847237f183SYan Zheng return PTR_ERR(log_root);
9857237f183SYan Zheng
9866ab6ebb7SNaohiro Aota ret = btrfs_alloc_log_tree_node(trans, log_root);
9876ab6ebb7SNaohiro Aota if (ret) {
9886ab6ebb7SNaohiro Aota btrfs_put_root(log_root);
9896ab6ebb7SNaohiro Aota return ret;
9906ab6ebb7SNaohiro Aota }
9916ab6ebb7SNaohiro Aota
9927237f183SYan Zheng log_root->last_trans = trans->transid;
9937237f183SYan Zheng log_root->root_key.offset = root->root_key.objectid;
9947237f183SYan Zheng
9957237f183SYan Zheng inode_item = &log_root->root_item.inode;
9963cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1);
9973cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3);
9983cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1);
999da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item,
10000b246afaSJeff Mahoney fs_info->nodesize);
10013cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
10027237f183SYan Zheng
10035d4f98a2SYan Zheng btrfs_set_root_node(&log_root->root_item, log_root->node);
10047237f183SYan Zheng
10057237f183SYan Zheng WARN_ON(root->log_root);
10067237f183SYan Zheng root->log_root = log_root;
10077237f183SYan Zheng root->log_transid = 0;
1008d1433debSMiao Xie root->log_transid_committed = -1;
1009257c62e1SChris Mason root->last_log_commit = 0;
1010e02119d5SChris Mason return 0;
1011e02119d5SChris Mason }
1012e02119d5SChris Mason
read_tree_root_path(struct btrfs_root * tree_root,struct btrfs_path * path,struct btrfs_key * key)101349d11beaSJosef Bacik static struct btrfs_root *read_tree_root_path(struct btrfs_root *tree_root,
101449d11beaSJosef Bacik struct btrfs_path *path,
1015cb517eabSMiao Xie struct btrfs_key *key)
1016e02119d5SChris Mason {
1017e02119d5SChris Mason struct btrfs_root *root;
1018789d6a3aSQu Wenruo struct btrfs_tree_parent_check check = { 0 };
1019e02119d5SChris Mason struct btrfs_fs_info *fs_info = tree_root->fs_info;
102084234f3aSYan Zheng u64 generation;
1021cb517eabSMiao Xie int ret;
1022581c1760SQu Wenruo int level;
1023cb517eabSMiao Xie
102496dfcb46SJosef Bacik root = btrfs_alloc_root(fs_info, key->objectid, GFP_NOFS);
102549d11beaSJosef Bacik if (!root)
102649d11beaSJosef Bacik return ERR_PTR(-ENOMEM);
10270f7d52f4SChris Mason
1028cb517eabSMiao Xie ret = btrfs_find_root(tree_root, key, path,
1029cb517eabSMiao Xie &root->root_item, &root->root_key);
10300f7d52f4SChris Mason if (ret) {
103113a8a7c8SYan, Zheng if (ret > 0)
103213a8a7c8SYan, Zheng ret = -ENOENT;
103349d11beaSJosef Bacik goto fail;
10340f7d52f4SChris Mason }
103513a8a7c8SYan, Zheng
103684234f3aSYan Zheng generation = btrfs_root_generation(&root->root_item);
1037581c1760SQu Wenruo level = btrfs_root_level(&root->root_item);
1038789d6a3aSQu Wenruo check.level = level;
1039789d6a3aSQu Wenruo check.transid = generation;
1040789d6a3aSQu Wenruo check.owner_root = key->objectid;
1041789d6a3aSQu Wenruo root->node = read_tree_block(fs_info, btrfs_root_bytenr(&root->root_item),
1042789d6a3aSQu Wenruo &check);
104364c043deSLiu Bo if (IS_ERR(root->node)) {
104464c043deSLiu Bo ret = PTR_ERR(root->node);
10458c38938cSJosef Bacik root->node = NULL;
104649d11beaSJosef Bacik goto fail;
10474eb150d6SQu Wenruo }
10484eb150d6SQu Wenruo if (!btrfs_buffer_uptodate(root->node, generation, 0)) {
1049cb517eabSMiao Xie ret = -EIO;
105049d11beaSJosef Bacik goto fail;
1051416bc658SJosef Bacik }
105288c602abSQu Wenruo
105388c602abSQu Wenruo /*
105488c602abSQu Wenruo * For real fs, and not log/reloc trees, root owner must
105588c602abSQu Wenruo * match its root node owner
105688c602abSQu Wenruo */
105788c602abSQu Wenruo if (!test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state) &&
105888c602abSQu Wenruo root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID &&
105988c602abSQu Wenruo root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
106088c602abSQu Wenruo root->root_key.objectid != btrfs_header_owner(root->node)) {
106188c602abSQu Wenruo btrfs_crit(fs_info,
106288c602abSQu Wenruo "root=%llu block=%llu, tree root owner mismatch, have %llu expect %llu",
106388c602abSQu Wenruo root->root_key.objectid, root->node->start,
106488c602abSQu Wenruo btrfs_header_owner(root->node),
106588c602abSQu Wenruo root->root_key.objectid);
106688c602abSQu Wenruo ret = -EUCLEAN;
106788c602abSQu Wenruo goto fail;
106888c602abSQu Wenruo }
10695d4f98a2SYan Zheng root->commit_root = btrfs_root_node(root);
1070cb517eabSMiao Xie return root;
107149d11beaSJosef Bacik fail:
107200246528SJosef Bacik btrfs_put_root(root);
107349d11beaSJosef Bacik return ERR_PTR(ret);
107449d11beaSJosef Bacik }
107549d11beaSJosef Bacik
btrfs_read_tree_root(struct btrfs_root * tree_root,struct btrfs_key * key)107649d11beaSJosef Bacik struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root,
107749d11beaSJosef Bacik struct btrfs_key *key)
107849d11beaSJosef Bacik {
107949d11beaSJosef Bacik struct btrfs_root *root;
108049d11beaSJosef Bacik struct btrfs_path *path;
108149d11beaSJosef Bacik
108249d11beaSJosef Bacik path = btrfs_alloc_path();
108349d11beaSJosef Bacik if (!path)
108449d11beaSJosef Bacik return ERR_PTR(-ENOMEM);
108549d11beaSJosef Bacik root = read_tree_root_path(tree_root, path, key);
108649d11beaSJosef Bacik btrfs_free_path(path);
108749d11beaSJosef Bacik
108849d11beaSJosef Bacik return root;
1089cb517eabSMiao Xie }
1090cb517eabSMiao Xie
10912dfb1e43SQu Wenruo /*
10922dfb1e43SQu Wenruo * Initialize subvolume root in-memory structure
10932dfb1e43SQu Wenruo *
10942dfb1e43SQu Wenruo * @anon_dev: anonymous device to attach to the root, if zero, allocate new
10952dfb1e43SQu Wenruo */
btrfs_init_fs_root(struct btrfs_root * root,dev_t anon_dev)10962dfb1e43SQu Wenruo static int btrfs_init_fs_root(struct btrfs_root *root, dev_t anon_dev)
1097cb517eabSMiao Xie {
1098cb517eabSMiao Xie int ret;
1099cb517eabSMiao Xie
11000b548539SDavid Sterba btrfs_drew_lock_init(&root->snapshot_lock);
11018257b2dcSMiao Xie
1102aeb935a4SQu Wenruo if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID &&
11036ebcd021SQu Wenruo !btrfs_is_data_reloc_root(root) &&
11046ebcd021SQu Wenruo is_fstree(root->root_key.objectid)) {
110592a7cc42SQu Wenruo set_bit(BTRFS_ROOT_SHAREABLE, &root->state);
1106f39e4571SJosef Bacik btrfs_check_and_init_root_item(&root->root_item);
1107f39e4571SJosef Bacik }
1108f39e4571SJosef Bacik
1109851fd730SQu Wenruo /*
1110851fd730SQu Wenruo * Don't assign anonymous block device to roots that are not exposed to
1111851fd730SQu Wenruo * userspace, the id pool is limited to 1M
1112851fd730SQu Wenruo */
1113851fd730SQu Wenruo if (is_fstree(root->root_key.objectid) &&
1114851fd730SQu Wenruo btrfs_root_refs(&root->root_item) > 0) {
11152dfb1e43SQu Wenruo if (!anon_dev) {
1116cb517eabSMiao Xie ret = get_anon_bdev(&root->anon_dev);
1117cb517eabSMiao Xie if (ret)
1118876d2cf1SLiu Bo goto fail;
11192dfb1e43SQu Wenruo } else {
11202dfb1e43SQu Wenruo root->anon_dev = anon_dev;
11212dfb1e43SQu Wenruo }
1122851fd730SQu Wenruo }
1123f32e48e9SChandan Rajendra
1124f32e48e9SChandan Rajendra mutex_lock(&root->objectid_mutex);
1125453e4873SNikolay Borisov ret = btrfs_init_root_free_objectid(root);
1126f32e48e9SChandan Rajendra if (ret) {
1127f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex);
1128876d2cf1SLiu Bo goto fail;
1129f32e48e9SChandan Rajendra }
1130f32e48e9SChandan Rajendra
11316b8fad57SNikolay Borisov ASSERT(root->free_objectid <= BTRFS_LAST_FREE_OBJECTID);
1132f32e48e9SChandan Rajendra
1133f32e48e9SChandan Rajendra mutex_unlock(&root->objectid_mutex);
1134f32e48e9SChandan Rajendra
1135cb517eabSMiao Xie return 0;
1136cb517eabSMiao Xie fail:
113784db5ccfSDavid Sterba /* The caller is responsible to call btrfs_free_fs_root */
1138cb517eabSMiao Xie return ret;
1139cb517eabSMiao Xie }
1140cb517eabSMiao Xie
btrfs_lookup_fs_root(struct btrfs_fs_info * fs_info,u64 root_id)1141a98db0f3SJosef Bacik static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
1142cb517eabSMiao Xie u64 root_id)
1143cb517eabSMiao Xie {
1144cb517eabSMiao Xie struct btrfs_root *root;
1145cb517eabSMiao Xie
1146fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock);
1147fc7cbcd4SDavid Sterba root = radix_tree_lookup(&fs_info->fs_roots_radix,
1148fc7cbcd4SDavid Sterba (unsigned long)root_id);
114900246528SJosef Bacik root = btrfs_grab_root(root);
1150fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock);
1151cb517eabSMiao Xie return root;
1152cb517eabSMiao Xie }
1153cb517eabSMiao Xie
btrfs_get_global_root(struct btrfs_fs_info * fs_info,u64 objectid)115449d11beaSJosef Bacik static struct btrfs_root *btrfs_get_global_root(struct btrfs_fs_info *fs_info,
115549d11beaSJosef Bacik u64 objectid)
115649d11beaSJosef Bacik {
1157abed4aaaSJosef Bacik struct btrfs_key key = {
1158abed4aaaSJosef Bacik .objectid = objectid,
1159abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY,
1160abed4aaaSJosef Bacik .offset = 0,
1161abed4aaaSJosef Bacik };
1162abed4aaaSJosef Bacik
1163e91909aaSChristoph Hellwig switch (objectid) {
1164e91909aaSChristoph Hellwig case BTRFS_ROOT_TREE_OBJECTID:
116549d11beaSJosef Bacik return btrfs_grab_root(fs_info->tree_root);
1166e91909aaSChristoph Hellwig case BTRFS_EXTENT_TREE_OBJECTID:
1167abed4aaaSJosef Bacik return btrfs_grab_root(btrfs_global_root(fs_info, &key));
1168e91909aaSChristoph Hellwig case BTRFS_CHUNK_TREE_OBJECTID:
116949d11beaSJosef Bacik return btrfs_grab_root(fs_info->chunk_root);
1170e91909aaSChristoph Hellwig case BTRFS_DEV_TREE_OBJECTID:
117149d11beaSJosef Bacik return btrfs_grab_root(fs_info->dev_root);
1172e91909aaSChristoph Hellwig case BTRFS_CSUM_TREE_OBJECTID:
1173abed4aaaSJosef Bacik return btrfs_grab_root(btrfs_global_root(fs_info, &key));
1174e91909aaSChristoph Hellwig case BTRFS_QUOTA_TREE_OBJECTID:
117585724171SChristoph Hellwig return btrfs_grab_root(fs_info->quota_root);
1176e91909aaSChristoph Hellwig case BTRFS_UUID_TREE_OBJECTID:
117785724171SChristoph Hellwig return btrfs_grab_root(fs_info->uuid_root);
1178e91909aaSChristoph Hellwig case BTRFS_BLOCK_GROUP_TREE_OBJECTID:
117985724171SChristoph Hellwig return btrfs_grab_root(fs_info->block_group_root);
1180e91909aaSChristoph Hellwig case BTRFS_FREE_SPACE_TREE_OBJECTID:
118185724171SChristoph Hellwig return btrfs_grab_root(btrfs_global_root(fs_info, &key));
1182e91909aaSChristoph Hellwig default:
118349d11beaSJosef Bacik return NULL;
118449d11beaSJosef Bacik }
1185e91909aaSChristoph Hellwig }
118649d11beaSJosef Bacik
btrfs_insert_fs_root(struct btrfs_fs_info * fs_info,struct btrfs_root * root)1187cb517eabSMiao Xie int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info,
1188cb517eabSMiao Xie struct btrfs_root *root)
1189cb517eabSMiao Xie {
1190cb517eabSMiao Xie int ret;
1191cb517eabSMiao Xie
1192fc7cbcd4SDavid Sterba ret = radix_tree_preload(GFP_NOFS);
1193fc7cbcd4SDavid Sterba if (ret)
1194fc7cbcd4SDavid Sterba return ret;
1195fc7cbcd4SDavid Sterba
1196fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock);
1197fc7cbcd4SDavid Sterba ret = radix_tree_insert(&fs_info->fs_roots_radix,
1198fc7cbcd4SDavid Sterba (unsigned long)root->root_key.objectid,
1199fc7cbcd4SDavid Sterba root);
1200af01d2e5SJosef Bacik if (ret == 0) {
120100246528SJosef Bacik btrfs_grab_root(root);
1202fc7cbcd4SDavid Sterba set_bit(BTRFS_ROOT_IN_RADIX, &root->state);
1203af01d2e5SJosef Bacik }
1204fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock);
1205fc7cbcd4SDavid Sterba radix_tree_preload_end();
1206cb517eabSMiao Xie
1207cb517eabSMiao Xie return ret;
1208cb517eabSMiao Xie }
1209cb517eabSMiao Xie
btrfs_check_leaked_roots(struct btrfs_fs_info * fs_info)1210bd647ce3SJosef Bacik void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info)
1211bd647ce3SJosef Bacik {
1212bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
1213bd647ce3SJosef Bacik struct btrfs_root *root;
1214bd647ce3SJosef Bacik
1215bd647ce3SJosef Bacik while (!list_empty(&fs_info->allocated_roots)) {
1216457f1864SJosef Bacik char buf[BTRFS_ROOT_NAME_BUF_LEN];
1217457f1864SJosef Bacik
1218bd647ce3SJosef Bacik root = list_first_entry(&fs_info->allocated_roots,
1219bd647ce3SJosef Bacik struct btrfs_root, leak_list);
1220457f1864SJosef Bacik btrfs_err(fs_info, "leaked root %s refcount %d",
122171008734SJosef Bacik btrfs_root_name(&root->root_key, buf),
1222bd647ce3SJosef Bacik refcount_read(&root->refs));
1223bd647ce3SJosef Bacik while (refcount_read(&root->refs) > 1)
122400246528SJosef Bacik btrfs_put_root(root);
122500246528SJosef Bacik btrfs_put_root(root);
1226bd647ce3SJosef Bacik }
1227bd647ce3SJosef Bacik #endif
1228bd647ce3SJosef Bacik }
1229bd647ce3SJosef Bacik
free_global_roots(struct btrfs_fs_info * fs_info)1230abed4aaaSJosef Bacik static void free_global_roots(struct btrfs_fs_info *fs_info)
1231abed4aaaSJosef Bacik {
1232abed4aaaSJosef Bacik struct btrfs_root *root;
1233abed4aaaSJosef Bacik struct rb_node *node;
1234abed4aaaSJosef Bacik
1235abed4aaaSJosef Bacik while ((node = rb_first_postorder(&fs_info->global_root_tree)) != NULL) {
1236abed4aaaSJosef Bacik root = rb_entry(node, struct btrfs_root, rb_node);
1237abed4aaaSJosef Bacik rb_erase(&root->rb_node, &fs_info->global_root_tree);
1238abed4aaaSJosef Bacik btrfs_put_root(root);
1239abed4aaaSJosef Bacik }
1240abed4aaaSJosef Bacik }
1241abed4aaaSJosef Bacik
btrfs_free_fs_info(struct btrfs_fs_info * fs_info)12420d4b0463SJosef Bacik void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
12430d4b0463SJosef Bacik {
1244141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dirty_metadata_bytes);
1245141386e1SJosef Bacik percpu_counter_destroy(&fs_info->delalloc_bytes);
12465deb17e1SJosef Bacik percpu_counter_destroy(&fs_info->ordered_bytes);
1247141386e1SJosef Bacik percpu_counter_destroy(&fs_info->dev_replace.bio_counter);
1248141386e1SJosef Bacik btrfs_free_csum_hash(fs_info);
1249141386e1SJosef Bacik btrfs_free_stripe_hash_table(fs_info);
1250141386e1SJosef Bacik btrfs_free_ref_cache(fs_info);
12510d4b0463SJosef Bacik kfree(fs_info->balance_ctl);
12520d4b0463SJosef Bacik kfree(fs_info->delayed_root);
1253abed4aaaSJosef Bacik free_global_roots(fs_info);
125400246528SJosef Bacik btrfs_put_root(fs_info->tree_root);
125500246528SJosef Bacik btrfs_put_root(fs_info->chunk_root);
125600246528SJosef Bacik btrfs_put_root(fs_info->dev_root);
125700246528SJosef Bacik btrfs_put_root(fs_info->quota_root);
125800246528SJosef Bacik btrfs_put_root(fs_info->uuid_root);
125900246528SJosef Bacik btrfs_put_root(fs_info->fs_root);
1260aeb935a4SQu Wenruo btrfs_put_root(fs_info->data_reloc_root);
12619c54e80dSJosef Bacik btrfs_put_root(fs_info->block_group_root);
1262bd647ce3SJosef Bacik btrfs_check_leaked_roots(fs_info);
12633fd63727SJosef Bacik btrfs_extent_buffer_leak_debug_check(fs_info);
12640d4b0463SJosef Bacik kfree(fs_info->super_copy);
12650d4b0463SJosef Bacik kfree(fs_info->super_for_commit);
12668481dd80SQu Wenruo kfree(fs_info->subpage_info);
12670d4b0463SJosef Bacik kvfree(fs_info);
12680d4b0463SJosef Bacik }
12690d4b0463SJosef Bacik
12700d4b0463SJosef Bacik
12712dfb1e43SQu Wenruo /*
12722dfb1e43SQu Wenruo * Get an in-memory reference of a root structure.
12732dfb1e43SQu Wenruo *
12742dfb1e43SQu Wenruo * For essential trees like root/extent tree, we grab it from fs_info directly.
12752dfb1e43SQu Wenruo * For subvolume trees, we check the cached filesystem roots first. If not
12762dfb1e43SQu Wenruo * found, then read it from disk and add it to cached fs roots.
12772dfb1e43SQu Wenruo *
12782dfb1e43SQu Wenruo * Caller should release the root by calling btrfs_put_root() after the usage.
12792dfb1e43SQu Wenruo *
12802dfb1e43SQu Wenruo * NOTE: Reloc and log trees can't be read by this function as they share the
12812dfb1e43SQu Wenruo * same root objectid.
12822dfb1e43SQu Wenruo *
12832dfb1e43SQu Wenruo * @objectid: root id
12842dfb1e43SQu Wenruo * @anon_dev: preallocated anonymous block device number for new roots,
1285*eb344109SFilipe Manana * pass NULL for a new allocation.
12862dfb1e43SQu Wenruo * @check_ref: whether to check root item references, If true, return -ENOENT
12872dfb1e43SQu Wenruo * for orphan roots
12882dfb1e43SQu Wenruo */
btrfs_get_root_ref(struct btrfs_fs_info * fs_info,u64 objectid,dev_t * anon_dev,bool check_ref)12892dfb1e43SQu Wenruo static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info,
1290*eb344109SFilipe Manana u64 objectid, dev_t *anon_dev,
12912dfb1e43SQu Wenruo bool check_ref)
12925eda7b5eSChris Mason {
12935eda7b5eSChris Mason struct btrfs_root *root;
1294381cf658SDavid Sterba struct btrfs_path *path;
12951d4c08e0SDavid Sterba struct btrfs_key key;
12965eda7b5eSChris Mason int ret;
12975eda7b5eSChris Mason
129849d11beaSJosef Bacik root = btrfs_get_global_root(fs_info, objectid);
129949d11beaSJosef Bacik if (root)
130049d11beaSJosef Bacik return root;
1301773e722aSQu Wenruo
1302773e722aSQu Wenruo /*
1303773e722aSQu Wenruo * If we're called for non-subvolume trees, and above function didn't
1304773e722aSQu Wenruo * find one, do not try to read it from disk.
1305773e722aSQu Wenruo *
1306773e722aSQu Wenruo * This is namely for free-space-tree and quota tree, which can change
1307773e722aSQu Wenruo * at runtime and should only be grabbed from fs_info.
1308773e722aSQu Wenruo */
1309773e722aSQu Wenruo if (!is_fstree(objectid) && objectid != BTRFS_DATA_RELOC_TREE_OBJECTID)
1310773e722aSQu Wenruo return ERR_PTR(-ENOENT);
13114df27c4dSYan, Zheng again:
131256e9357aSDavid Sterba root = btrfs_lookup_fs_root(fs_info, objectid);
131348475471SStefan Behrens if (root) {
131483377565SQu Wenruo /*
131583377565SQu Wenruo * Some other caller may have read out the newly inserted
131683377565SQu Wenruo * subvolume already (for things like backref walk etc). Not
131783377565SQu Wenruo * that common but still possible. In that case, we just need
131883377565SQu Wenruo * to free the anon_dev.
131983377565SQu Wenruo */
1320*eb344109SFilipe Manana if (unlikely(anon_dev && *anon_dev)) {
1321*eb344109SFilipe Manana free_anon_bdev(*anon_dev);
1322*eb344109SFilipe Manana *anon_dev = 0;
132383377565SQu Wenruo }
132483377565SQu Wenruo
1325bc44d7c4SJosef Bacik if (check_ref && btrfs_root_refs(&root->root_item) == 0) {
132600246528SJosef Bacik btrfs_put_root(root);
132748475471SStefan Behrens return ERR_PTR(-ENOENT);
1328bc44d7c4SJosef Bacik }
13295eda7b5eSChris Mason return root;
133048475471SStefan Behrens }
13315eda7b5eSChris Mason
133256e9357aSDavid Sterba key.objectid = objectid;
133356e9357aSDavid Sterba key.type = BTRFS_ROOT_ITEM_KEY;
133456e9357aSDavid Sterba key.offset = (u64)-1;
133556e9357aSDavid Sterba root = btrfs_read_tree_root(fs_info->tree_root, &key);
13365eda7b5eSChris Mason if (IS_ERR(root))
13375eda7b5eSChris Mason return root;
13383394e160SChris Mason
1339c00869f1SMiao Xie if (check_ref && btrfs_root_refs(&root->root_item) == 0) {
1340d68fc57bSYan, Zheng ret = -ENOENT;
1341d68fc57bSYan, Zheng goto fail;
1342d68fc57bSYan, Zheng }
1343d68fc57bSYan, Zheng
1344*eb344109SFilipe Manana ret = btrfs_init_fs_root(root, anon_dev ? *anon_dev : 0);
1345cb517eabSMiao Xie if (ret)
1346cb517eabSMiao Xie goto fail;
1347cb517eabSMiao Xie
1348381cf658SDavid Sterba path = btrfs_alloc_path();
1349381cf658SDavid Sterba if (!path) {
1350381cf658SDavid Sterba ret = -ENOMEM;
1351381cf658SDavid Sterba goto fail;
1352381cf658SDavid Sterba }
13531d4c08e0SDavid Sterba key.objectid = BTRFS_ORPHAN_OBJECTID;
13541d4c08e0SDavid Sterba key.type = BTRFS_ORPHAN_ITEM_KEY;
135556e9357aSDavid Sterba key.offset = objectid;
13561d4c08e0SDavid Sterba
13571d4c08e0SDavid Sterba ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
1358381cf658SDavid Sterba btrfs_free_path(path);
1359d68fc57bSYan, Zheng if (ret < 0)
1360d68fc57bSYan, Zheng goto fail;
1361d68fc57bSYan, Zheng if (ret == 0)
136227cdeb70SMiao Xie set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state);
1363d68fc57bSYan, Zheng
1364cb517eabSMiao Xie ret = btrfs_insert_fs_root(fs_info, root);
13650f7d52f4SChris Mason if (ret) {
1366168a2f77SJia-Ju Bai if (ret == -EEXIST) {
136700246528SJosef Bacik btrfs_put_root(root);
13684df27c4dSYan, Zheng goto again;
1369168a2f77SJia-Ju Bai }
13704df27c4dSYan, Zheng goto fail;
13714df27c4dSYan, Zheng }
1372edbd8d4eSChris Mason return root;
13734df27c4dSYan, Zheng fail:
137433fab972SFilipe Manana /*
137533fab972SFilipe Manana * If our caller provided us an anonymous device, then it's his
1376143823cfSDavid Sterba * responsibility to free it in case we fail. So we have to set our
137733fab972SFilipe Manana * root's anon_dev to 0 to avoid a double free, once by btrfs_put_root()
137833fab972SFilipe Manana * and once again by our caller.
137933fab972SFilipe Manana */
1380*eb344109SFilipe Manana if (anon_dev && *anon_dev)
138133fab972SFilipe Manana root->anon_dev = 0;
13828c38938cSJosef Bacik btrfs_put_root(root);
13834df27c4dSYan, Zheng return ERR_PTR(ret);
1384edbd8d4eSChris Mason }
1385edbd8d4eSChris Mason
13862dfb1e43SQu Wenruo /*
13872dfb1e43SQu Wenruo * Get in-memory reference of a root structure
13882dfb1e43SQu Wenruo *
13892dfb1e43SQu Wenruo * @objectid: tree objectid
13902dfb1e43SQu Wenruo * @check_ref: if set, verify that the tree exists and the item has at least
13912dfb1e43SQu Wenruo * one reference
13922dfb1e43SQu Wenruo */
btrfs_get_fs_root(struct btrfs_fs_info * fs_info,u64 objectid,bool check_ref)13932dfb1e43SQu Wenruo struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info,
13942dfb1e43SQu Wenruo u64 objectid, bool check_ref)
13952dfb1e43SQu Wenruo {
1396*eb344109SFilipe Manana return btrfs_get_root_ref(fs_info, objectid, NULL, check_ref);
13972dfb1e43SQu Wenruo }
13982dfb1e43SQu Wenruo
13992dfb1e43SQu Wenruo /*
14002dfb1e43SQu Wenruo * Get in-memory reference of a root structure, created as new, optionally pass
14012dfb1e43SQu Wenruo * the anonymous block device id
14022dfb1e43SQu Wenruo *
14032dfb1e43SQu Wenruo * @objectid: tree objectid
1404*eb344109SFilipe Manana * @anon_dev: if NULL, allocate a new anonymous block device or use the
1405*eb344109SFilipe Manana * parameter value if not NULL
14062dfb1e43SQu Wenruo */
btrfs_get_new_fs_root(struct btrfs_fs_info * fs_info,u64 objectid,dev_t * anon_dev)14072dfb1e43SQu Wenruo struct btrfs_root *btrfs_get_new_fs_root(struct btrfs_fs_info *fs_info,
1408*eb344109SFilipe Manana u64 objectid, dev_t *anon_dev)
14092dfb1e43SQu Wenruo {
14102dfb1e43SQu Wenruo return btrfs_get_root_ref(fs_info, objectid, anon_dev, true);
14112dfb1e43SQu Wenruo }
14122dfb1e43SQu Wenruo
14138b712842SChris Mason /*
141449d11beaSJosef Bacik * btrfs_get_fs_root_commit_root - return a root for the given objectid
141549d11beaSJosef Bacik * @fs_info: the fs_info
141649d11beaSJosef Bacik * @objectid: the objectid we need to lookup
141749d11beaSJosef Bacik *
141849d11beaSJosef Bacik * This is exclusively used for backref walking, and exists specifically because
141949d11beaSJosef Bacik * of how qgroups does lookups. Qgroups will do a backref lookup at delayed ref
142049d11beaSJosef Bacik * creation time, which means we may have to read the tree_root in order to look
142149d11beaSJosef Bacik * up a fs root that is not in memory. If the root is not in memory we will
142249d11beaSJosef Bacik * read the tree root commit root and look up the fs root from there. This is a
142349d11beaSJosef Bacik * temporary root, it will not be inserted into the radix tree as it doesn't
142449d11beaSJosef Bacik * have the most uptodate information, it'll simply be discarded once the
142549d11beaSJosef Bacik * backref code is finished using the root.
142649d11beaSJosef Bacik */
btrfs_get_fs_root_commit_root(struct btrfs_fs_info * fs_info,struct btrfs_path * path,u64 objectid)142749d11beaSJosef Bacik struct btrfs_root *btrfs_get_fs_root_commit_root(struct btrfs_fs_info *fs_info,
142849d11beaSJosef Bacik struct btrfs_path *path,
142949d11beaSJosef Bacik u64 objectid)
143049d11beaSJosef Bacik {
143149d11beaSJosef Bacik struct btrfs_root *root;
143249d11beaSJosef Bacik struct btrfs_key key;
143349d11beaSJosef Bacik
143449d11beaSJosef Bacik ASSERT(path->search_commit_root && path->skip_locking);
143549d11beaSJosef Bacik
143649d11beaSJosef Bacik /*
143749d11beaSJosef Bacik * This can return -ENOENT if we ask for a root that doesn't exist, but
143849d11beaSJosef Bacik * since this is called via the backref walking code we won't be looking
143949d11beaSJosef Bacik * up a root that doesn't exist, unless there's corruption. So if root
144049d11beaSJosef Bacik * != NULL just return it.
144149d11beaSJosef Bacik */
144249d11beaSJosef Bacik root = btrfs_get_global_root(fs_info, objectid);
144349d11beaSJosef Bacik if (root)
144449d11beaSJosef Bacik return root;
144549d11beaSJosef Bacik
144649d11beaSJosef Bacik root = btrfs_lookup_fs_root(fs_info, objectid);
144749d11beaSJosef Bacik if (root)
144849d11beaSJosef Bacik return root;
144949d11beaSJosef Bacik
145049d11beaSJosef Bacik key.objectid = objectid;
145149d11beaSJosef Bacik key.type = BTRFS_ROOT_ITEM_KEY;
145249d11beaSJosef Bacik key.offset = (u64)-1;
145349d11beaSJosef Bacik root = read_tree_root_path(fs_info->tree_root, path, &key);
145449d11beaSJosef Bacik btrfs_release_path(path);
145549d11beaSJosef Bacik
145649d11beaSJosef Bacik return root;
145749d11beaSJosef Bacik }
145849d11beaSJosef Bacik
cleaner_kthread(void * arg)1459a74a4b97SChris Mason static int cleaner_kthread(void *arg)
1460a74a4b97SChris Mason {
14610d031dc4SYu Zhe struct btrfs_fs_info *fs_info = arg;
1462d0278245SMiao Xie int again;
1463a74a4b97SChris Mason
1464d6fd0ae2SOmar Sandoval while (1) {
1465d0278245SMiao Xie again = 0;
14669d1a2a3aSDavid Sterba
1467fd340d0fSJosef Bacik set_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags);
1468fd340d0fSJosef Bacik
1469d0278245SMiao Xie /* Make the cleaner go to sleep early. */
14702ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info))
1471d0278245SMiao Xie goto sleep;
1472d0278245SMiao Xie
147390c711abSZygo Blaxell /*
147490c711abSZygo Blaxell * Do not do anything if we might cause open_ctree() to block
147590c711abSZygo Blaxell * before we have finished mounting the filesystem.
147690c711abSZygo Blaxell */
14770b246afaSJeff Mahoney if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
147890c711abSZygo Blaxell goto sleep;
147990c711abSZygo Blaxell
14800b246afaSJeff Mahoney if (!mutex_trylock(&fs_info->cleaner_mutex))
1481d0278245SMiao Xie goto sleep;
1482d0278245SMiao Xie
1483dc7f370cSMiao Xie /*
1484dc7f370cSMiao Xie * Avoid the problem that we change the status of the fs
1485dc7f370cSMiao Xie * during the above check and trylock.
1486dc7f370cSMiao Xie */
14872ff7e61eSJeff Mahoney if (btrfs_need_cleaner_sleep(fs_info)) {
14880b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex);
1489dc7f370cSMiao Xie goto sleep;
1490dc7f370cSMiao Xie }
1491dc7f370cSMiao Xie
1492b7625f46SQu Wenruo if (test_and_clear_bit(BTRFS_FS_FEATURE_CHANGED, &fs_info->flags))
1493b7625f46SQu Wenruo btrfs_sysfs_feature_update(fs_info);
1494b7625f46SQu Wenruo
14952ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info);
1496c2d6cb16SFilipe Manana
149733c44184SJosef Bacik again = btrfs_clean_one_deleted_snapshot(fs_info);
14980b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex);
1499a74a4b97SChris Mason
1500d0278245SMiao Xie /*
150105323cd1SMiao Xie * The defragger has dealt with the R/O remount and umount,
150205323cd1SMiao Xie * needn't do anything special here.
1503d0278245SMiao Xie */
15040b246afaSJeff Mahoney btrfs_run_defrag_inodes(fs_info);
150567c5e7d4SFilipe Manana
150667c5e7d4SFilipe Manana /*
1507f3372065SJohannes Thumshirn * Acquires fs_info->reclaim_bgs_lock to avoid racing
150867c5e7d4SFilipe Manana * with relocation (btrfs_relocate_chunk) and relocation
150967c5e7d4SFilipe Manana * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group)
1510f3372065SJohannes Thumshirn * after acquiring fs_info->reclaim_bgs_lock. So we
151167c5e7d4SFilipe Manana * can't hold, nor need to, fs_info->cleaner_mutex when deleting
151267c5e7d4SFilipe Manana * unused block groups.
151367c5e7d4SFilipe Manana */
15140b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info);
151518bb8bbfSJohannes Thumshirn
151618bb8bbfSJohannes Thumshirn /*
151718bb8bbfSJohannes Thumshirn * Reclaim block groups in the reclaim_bgs list after we deleted
151818bb8bbfSJohannes Thumshirn * all unused block_groups. This possibly gives us some more free
151918bb8bbfSJohannes Thumshirn * space.
152018bb8bbfSJohannes Thumshirn */
152118bb8bbfSJohannes Thumshirn btrfs_reclaim_bgs(fs_info);
1522d0278245SMiao Xie sleep:
1523a0a1db70SFilipe Manana clear_and_wake_up_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags);
1524d6fd0ae2SOmar Sandoval if (kthread_should_park())
1525d6fd0ae2SOmar Sandoval kthread_parkme();
1526d6fd0ae2SOmar Sandoval if (kthread_should_stop())
1527d6fd0ae2SOmar Sandoval return 0;
1528838fe188SJiri Kosina if (!again) {
1529a74a4b97SChris Mason set_current_state(TASK_INTERRUPTIBLE);
1530a74a4b97SChris Mason schedule();
1531a74a4b97SChris Mason __set_current_state(TASK_RUNNING);
1532a74a4b97SChris Mason }
1533da288d28SFilipe Manana }
1534a74a4b97SChris Mason }
1535a74a4b97SChris Mason
transaction_kthread(void * arg)1536a74a4b97SChris Mason static int transaction_kthread(void *arg)
1537a74a4b97SChris Mason {
1538a74a4b97SChris Mason struct btrfs_root *root = arg;
15390b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info;
1540a74a4b97SChris Mason struct btrfs_trans_handle *trans;
1541a74a4b97SChris Mason struct btrfs_transaction *cur;
15428929ecfaSYan, Zheng u64 transid;
1543643900beSNikolay Borisov time64_t delta;
1544a74a4b97SChris Mason unsigned long delay;
1545914b2007SJan Kara bool cannot_commit;
1546a74a4b97SChris Mason
1547a74a4b97SChris Mason do {
1548914b2007SJan Kara cannot_commit = false;
1549ba1bc00fSNikolay Borisov delay = msecs_to_jiffies(fs_info->commit_interval * 1000);
15500b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex);
1551a74a4b97SChris Mason
15520b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock);
15530b246afaSJeff Mahoney cur = fs_info->running_transaction;
1554a74a4b97SChris Mason if (!cur) {
15550b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock);
1556a74a4b97SChris Mason goto sleep;
1557a74a4b97SChris Mason }
155831153d81SYan Zheng
1559643900beSNikolay Borisov delta = ktime_get_seconds() - cur->start_time;
1560fdfbf020SJosef Bacik if (!test_and_clear_bit(BTRFS_FS_COMMIT_TRANS, &fs_info->flags) &&
156177d20c68SJosef Bacik cur->state < TRANS_STATE_COMMIT_PREP &&
1562643900beSNikolay Borisov delta < fs_info->commit_interval) {
15630b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock);
1564fb8a7e94SNikolay Borisov delay -= msecs_to_jiffies((delta - 1) * 1000);
1565fb8a7e94SNikolay Borisov delay = min(delay,
1566fb8a7e94SNikolay Borisov msecs_to_jiffies(fs_info->commit_interval * 1000));
1567a74a4b97SChris Mason goto sleep;
1568a74a4b97SChris Mason }
15698929ecfaSYan, Zheng transid = cur->transid;
15700b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock);
157156bec294SChris Mason
157279787eaaSJeff Mahoney /* If the file system is aborted, this will always fail. */
1573354aa0fbSMiao Xie trans = btrfs_attach_transaction(root);
1574914b2007SJan Kara if (IS_ERR(trans)) {
1575354aa0fbSMiao Xie if (PTR_ERR(trans) != -ENOENT)
1576914b2007SJan Kara cannot_commit = true;
157779787eaaSJeff Mahoney goto sleep;
1578914b2007SJan Kara }
15798929ecfaSYan, Zheng if (transid == trans->transid) {
15803a45bb20SJeff Mahoney btrfs_commit_transaction(trans);
15818929ecfaSYan, Zheng } else {
15823a45bb20SJeff Mahoney btrfs_end_transaction(trans);
15838929ecfaSYan, Zheng }
1584a74a4b97SChris Mason sleep:
15850b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread);
15860b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex);
1587a74a4b97SChris Mason
158884961539SJosef Bacik if (BTRFS_FS_ERROR(fs_info))
15892ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info);
15908929ecfaSYan, Zheng if (!kthread_should_stop() &&
15910b246afaSJeff Mahoney (!btrfs_transaction_blocked(fs_info) ||
1592914b2007SJan Kara cannot_commit))
1593bc5511d0SNikolay Borisov schedule_timeout_interruptible(delay);
1594a74a4b97SChris Mason } while (!kthread_should_stop());
1595a74a4b97SChris Mason return 0;
1596a74a4b97SChris Mason }
1597a74a4b97SChris Mason
1598af31f5e5SChris Mason /*
159901f0f9daSNikolay Borisov * This will find the highest generation in the array of root backups. The
160001f0f9daSNikolay Borisov * index of the highest array is returned, or -EINVAL if we can't find
160101f0f9daSNikolay Borisov * anything.
1602af31f5e5SChris Mason *
1603af31f5e5SChris Mason * We check to make sure the array is valid by comparing the
1604af31f5e5SChris Mason * generation of the latest root in the array with the generation
1605af31f5e5SChris Mason * in the super block. If they don't match we pitch it.
1606af31f5e5SChris Mason */
find_newest_super_backup(struct btrfs_fs_info * info)160701f0f9daSNikolay Borisov static int find_newest_super_backup(struct btrfs_fs_info *info)
1608af31f5e5SChris Mason {
160901f0f9daSNikolay Borisov const u64 newest_gen = btrfs_super_generation(info->super_copy);
1610af31f5e5SChris Mason u64 cur;
1611af31f5e5SChris Mason struct btrfs_root_backup *root_backup;
1612af31f5e5SChris Mason int i;
1613af31f5e5SChris Mason
1614af31f5e5SChris Mason for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
1615af31f5e5SChris Mason root_backup = info->super_copy->super_roots + i;
1616af31f5e5SChris Mason cur = btrfs_backup_tree_root_gen(root_backup);
1617af31f5e5SChris Mason if (cur == newest_gen)
161801f0f9daSNikolay Borisov return i;
1619af31f5e5SChris Mason }
1620af31f5e5SChris Mason
162101f0f9daSNikolay Borisov return -EINVAL;
1622af31f5e5SChris Mason }
1623af31f5e5SChris Mason
1624af31f5e5SChris Mason /*
1625af31f5e5SChris Mason * copy all the root pointers into the super backup array.
1626af31f5e5SChris Mason * this will bump the backup pointer by one when it is
1627af31f5e5SChris Mason * done
1628af31f5e5SChris Mason */
backup_super_roots(struct btrfs_fs_info * info)1629af31f5e5SChris Mason static void backup_super_roots(struct btrfs_fs_info *info)
1630af31f5e5SChris Mason {
16316ef108ddSNikolay Borisov const int next_backup = info->backup_root_index;
1632af31f5e5SChris Mason struct btrfs_root_backup *root_backup;
1633af31f5e5SChris Mason
1634af31f5e5SChris Mason root_backup = info->super_for_commit->super_roots + next_backup;
1635af31f5e5SChris Mason
1636af31f5e5SChris Mason /*
1637af31f5e5SChris Mason * make sure all of our padding and empty slots get zero filled
1638af31f5e5SChris Mason * regardless of which ones we use today
1639af31f5e5SChris Mason */
1640af31f5e5SChris Mason memset(root_backup, 0, sizeof(*root_backup));
1641af31f5e5SChris Mason
1642af31f5e5SChris Mason info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS;
1643af31f5e5SChris Mason
1644af31f5e5SChris Mason btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start);
1645af31f5e5SChris Mason btrfs_set_backup_tree_root_gen(root_backup,
1646af31f5e5SChris Mason btrfs_header_generation(info->tree_root->node));
1647af31f5e5SChris Mason
1648af31f5e5SChris Mason btrfs_set_backup_tree_root_level(root_backup,
1649af31f5e5SChris Mason btrfs_header_level(info->tree_root->node));
1650af31f5e5SChris Mason
1651af31f5e5SChris Mason btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start);
1652af31f5e5SChris Mason btrfs_set_backup_chunk_root_gen(root_backup,
1653af31f5e5SChris Mason btrfs_header_generation(info->chunk_root->node));
1654af31f5e5SChris Mason btrfs_set_backup_chunk_root_level(root_backup,
1655af31f5e5SChris Mason btrfs_header_level(info->chunk_root->node));
1656af31f5e5SChris Mason
16571c56ab99SQu Wenruo if (!btrfs_fs_compat_ro(info, BLOCK_GROUP_TREE)) {
16589c54e80dSJosef Bacik struct btrfs_root *extent_root = btrfs_extent_root(info, 0);
1659f7238e50SJosef Bacik struct btrfs_root *csum_root = btrfs_csum_root(info, 0);
16609c54e80dSJosef Bacik
16619c54e80dSJosef Bacik btrfs_set_backup_extent_root(root_backup,
16629c54e80dSJosef Bacik extent_root->node->start);
1663af31f5e5SChris Mason btrfs_set_backup_extent_root_gen(root_backup,
166429cbcf40SJosef Bacik btrfs_header_generation(extent_root->node));
1665af31f5e5SChris Mason btrfs_set_backup_extent_root_level(root_backup,
166629cbcf40SJosef Bacik btrfs_header_level(extent_root->node));
1667af31f5e5SChris Mason
1668f7238e50SJosef Bacik btrfs_set_backup_csum_root(root_backup, csum_root->node->start);
1669f7238e50SJosef Bacik btrfs_set_backup_csum_root_gen(root_backup,
1670f7238e50SJosef Bacik btrfs_header_generation(csum_root->node));
1671f7238e50SJosef Bacik btrfs_set_backup_csum_root_level(root_backup,
1672f7238e50SJosef Bacik btrfs_header_level(csum_root->node));
16739c54e80dSJosef Bacik }
1674af31f5e5SChris Mason
16757c7e82a7SChris Mason /*
16767c7e82a7SChris Mason * we might commit during log recovery, which happens before we set
16777c7e82a7SChris Mason * the fs_root. Make sure it is valid before we fill it in.
16787c7e82a7SChris Mason */
16797c7e82a7SChris Mason if (info->fs_root && info->fs_root->node) {
16807c7e82a7SChris Mason btrfs_set_backup_fs_root(root_backup,
16817c7e82a7SChris Mason info->fs_root->node->start);
1682af31f5e5SChris Mason btrfs_set_backup_fs_root_gen(root_backup,
1683af31f5e5SChris Mason btrfs_header_generation(info->fs_root->node));
1684af31f5e5SChris Mason btrfs_set_backup_fs_root_level(root_backup,
1685af31f5e5SChris Mason btrfs_header_level(info->fs_root->node));
16867c7e82a7SChris Mason }
1687af31f5e5SChris Mason
1688af31f5e5SChris Mason btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start);
1689af31f5e5SChris Mason btrfs_set_backup_dev_root_gen(root_backup,
1690af31f5e5SChris Mason btrfs_header_generation(info->dev_root->node));
1691af31f5e5SChris Mason btrfs_set_backup_dev_root_level(root_backup,
1692af31f5e5SChris Mason btrfs_header_level(info->dev_root->node));
1693af31f5e5SChris Mason
1694af31f5e5SChris Mason btrfs_set_backup_total_bytes(root_backup,
1695af31f5e5SChris Mason btrfs_super_total_bytes(info->super_copy));
1696af31f5e5SChris Mason btrfs_set_backup_bytes_used(root_backup,
1697af31f5e5SChris Mason btrfs_super_bytes_used(info->super_copy));
1698af31f5e5SChris Mason btrfs_set_backup_num_devices(root_backup,
1699af31f5e5SChris Mason btrfs_super_num_devices(info->super_copy));
1700af31f5e5SChris Mason
1701af31f5e5SChris Mason /*
1702af31f5e5SChris Mason * if we don't copy this out to the super_copy, it won't get remembered
1703af31f5e5SChris Mason * for the next commit
1704af31f5e5SChris Mason */
1705af31f5e5SChris Mason memcpy(&info->super_copy->super_roots,
1706af31f5e5SChris Mason &info->super_for_commit->super_roots,
1707af31f5e5SChris Mason sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS);
1708af31f5e5SChris Mason }
1709af31f5e5SChris Mason
1710af31f5e5SChris Mason /*
1711bd2336b2SNikolay Borisov * read_backup_root - Reads a backup root based on the passed priority. Prio 0
1712bd2336b2SNikolay Borisov * is the newest, prio 1/2/3 are 2nd newest/3rd newest/4th (oldest) backup roots
1713bd2336b2SNikolay Borisov *
1714bd2336b2SNikolay Borisov * fs_info - filesystem whose backup roots need to be read
1715bd2336b2SNikolay Borisov * priority - priority of backup root required
1716bd2336b2SNikolay Borisov *
1717bd2336b2SNikolay Borisov * Returns backup root index on success and -EINVAL otherwise.
1718bd2336b2SNikolay Borisov */
read_backup_root(struct btrfs_fs_info * fs_info,u8 priority)1719bd2336b2SNikolay Borisov static int read_backup_root(struct btrfs_fs_info *fs_info, u8 priority)
1720bd2336b2SNikolay Borisov {
1721bd2336b2SNikolay Borisov int backup_index = find_newest_super_backup(fs_info);
1722bd2336b2SNikolay Borisov struct btrfs_super_block *super = fs_info->super_copy;
1723bd2336b2SNikolay Borisov struct btrfs_root_backup *root_backup;
1724bd2336b2SNikolay Borisov
1725bd2336b2SNikolay Borisov if (priority < BTRFS_NUM_BACKUP_ROOTS && backup_index >= 0) {
1726bd2336b2SNikolay Borisov if (priority == 0)
1727bd2336b2SNikolay Borisov return backup_index;
1728bd2336b2SNikolay Borisov
1729bd2336b2SNikolay Borisov backup_index = backup_index + BTRFS_NUM_BACKUP_ROOTS - priority;
1730bd2336b2SNikolay Borisov backup_index %= BTRFS_NUM_BACKUP_ROOTS;
1731bd2336b2SNikolay Borisov } else {
1732bd2336b2SNikolay Borisov return -EINVAL;
1733bd2336b2SNikolay Borisov }
1734bd2336b2SNikolay Borisov
1735bd2336b2SNikolay Borisov root_backup = super->super_roots + backup_index;
1736bd2336b2SNikolay Borisov
1737bd2336b2SNikolay Borisov btrfs_set_super_generation(super,
1738bd2336b2SNikolay Borisov btrfs_backup_tree_root_gen(root_backup));
1739bd2336b2SNikolay Borisov btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup));
1740bd2336b2SNikolay Borisov btrfs_set_super_root_level(super,
1741bd2336b2SNikolay Borisov btrfs_backup_tree_root_level(root_backup));
1742bd2336b2SNikolay Borisov btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup));
1743bd2336b2SNikolay Borisov
1744bd2336b2SNikolay Borisov /*
1745bd2336b2SNikolay Borisov * Fixme: the total bytes and num_devices need to match or we should
1746bd2336b2SNikolay Borisov * need a fsck
1747bd2336b2SNikolay Borisov */
1748bd2336b2SNikolay Borisov btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup));
1749bd2336b2SNikolay Borisov btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup));
1750bd2336b2SNikolay Borisov
1751bd2336b2SNikolay Borisov return backup_index;
1752bd2336b2SNikolay Borisov }
1753bd2336b2SNikolay Borisov
17547abadb64SLiu Bo /* helper to cleanup workers */
btrfs_stop_all_workers(struct btrfs_fs_info * fs_info)17557abadb64SLiu Bo static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info)
17567abadb64SLiu Bo {
1757dc6e3209SQu Wenruo btrfs_destroy_workqueue(fs_info->fixup_workers);
1758afe3d242SQu Wenruo btrfs_destroy_workqueue(fs_info->delalloc_workers);
17595cdc7ad3SQu Wenruo btrfs_destroy_workqueue(fs_info->workers);
1760d7b9416fSChristoph Hellwig if (fs_info->endio_workers)
1761d7b9416fSChristoph Hellwig destroy_workqueue(fs_info->endio_workers);
1762385de0efSChristoph Hellwig if (fs_info->rmw_workers)
1763385de0efSChristoph Hellwig destroy_workqueue(fs_info->rmw_workers);
1764fed8a72dSChristoph Hellwig if (fs_info->compressed_write_workers)
1765fed8a72dSChristoph Hellwig destroy_workqueue(fs_info->compressed_write_workers);
1766fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_write_workers);
1767fccb5d86SQu Wenruo btrfs_destroy_workqueue(fs_info->endio_freespace_worker);
17685b3bc44eSQu Wenruo btrfs_destroy_workqueue(fs_info->delayed_workers);
1769e66f0bb1SQu Wenruo btrfs_destroy_workqueue(fs_info->caching_workers);
1770a44903abSQu Wenruo btrfs_destroy_workqueue(fs_info->flush_workers);
1771fc97fab0SQu Wenruo btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers);
1772b0643e59SDennis Zhou if (fs_info->discard_ctl.discard_workers)
1773b0643e59SDennis Zhou destroy_workqueue(fs_info->discard_ctl.discard_workers);
1774a9b9477dSFilipe Manana /*
1775a9b9477dSFilipe Manana * Now that all other work queues are destroyed, we can safely destroy
1776a9b9477dSFilipe Manana * the queues used for metadata I/O, since tasks from those other work
1777a9b9477dSFilipe Manana * queues can do metadata I/O operations.
1778a9b9477dSFilipe Manana */
1779d7b9416fSChristoph Hellwig if (fs_info->endio_meta_workers)
1780d7b9416fSChristoph Hellwig destroy_workqueue(fs_info->endio_meta_workers);
17817abadb64SLiu Bo }
17827abadb64SLiu Bo
free_root_extent_buffers(struct btrfs_root * root)17832e9f5954SRashika static void free_root_extent_buffers(struct btrfs_root *root)
17842e9f5954SRashika {
17852e9f5954SRashika if (root) {
17862e9f5954SRashika free_extent_buffer(root->node);
17872e9f5954SRashika free_extent_buffer(root->commit_root);
17882e9f5954SRashika root->node = NULL;
17892e9f5954SRashika root->commit_root = NULL;
17902e9f5954SRashika }
17912e9f5954SRashika }
17922e9f5954SRashika
free_global_root_pointers(struct btrfs_fs_info * fs_info)1793abed4aaaSJosef Bacik static void free_global_root_pointers(struct btrfs_fs_info *fs_info)
1794abed4aaaSJosef Bacik {
1795abed4aaaSJosef Bacik struct btrfs_root *root, *tmp;
1796abed4aaaSJosef Bacik
1797abed4aaaSJosef Bacik rbtree_postorder_for_each_entry_safe(root, tmp,
1798abed4aaaSJosef Bacik &fs_info->global_root_tree,
1799abed4aaaSJosef Bacik rb_node)
1800abed4aaaSJosef Bacik free_root_extent_buffers(root);
1801abed4aaaSJosef Bacik }
1802abed4aaaSJosef Bacik
1803af31f5e5SChris Mason /* helper to cleanup tree roots */
free_root_pointers(struct btrfs_fs_info * info,bool free_chunk_root)18044273eaffSAnand Jain static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root)
1805af31f5e5SChris Mason {
18062e9f5954SRashika free_root_extent_buffers(info->tree_root);
1807655b09feSJosef Bacik
1808abed4aaaSJosef Bacik free_global_root_pointers(info);
18092e9f5954SRashika free_root_extent_buffers(info->dev_root);
18102e9f5954SRashika free_root_extent_buffers(info->quota_root);
18112e9f5954SRashika free_root_extent_buffers(info->uuid_root);
18128c38938cSJosef Bacik free_root_extent_buffers(info->fs_root);
1813aeb935a4SQu Wenruo free_root_extent_buffers(info->data_reloc_root);
18149c54e80dSJosef Bacik free_root_extent_buffers(info->block_group_root);
18154273eaffSAnand Jain if (free_chunk_root)
18162e9f5954SRashika free_root_extent_buffers(info->chunk_root);
1817af31f5e5SChris Mason }
1818af31f5e5SChris Mason
btrfs_put_root(struct btrfs_root * root)18198c38938cSJosef Bacik void btrfs_put_root(struct btrfs_root *root)
18208c38938cSJosef Bacik {
18218c38938cSJosef Bacik if (!root)
18228c38938cSJosef Bacik return;
18238c38938cSJosef Bacik
18248c38938cSJosef Bacik if (refcount_dec_and_test(&root->refs)) {
18258c38938cSJosef Bacik WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree));
18261dae7e0eSQu Wenruo WARN_ON(test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state));
18278c38938cSJosef Bacik if (root->anon_dev)
18288c38938cSJosef Bacik free_anon_bdev(root->anon_dev);
1829923eb523SJohannes Thumshirn free_root_extent_buffers(root);
18308c38938cSJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
1831fc7cbcd4SDavid Sterba spin_lock(&root->fs_info->fs_roots_radix_lock);
18328c38938cSJosef Bacik list_del_init(&root->leak_list);
1833fc7cbcd4SDavid Sterba spin_unlock(&root->fs_info->fs_roots_radix_lock);
18348c38938cSJosef Bacik #endif
18358c38938cSJosef Bacik kfree(root);
18368c38938cSJosef Bacik }
18378c38938cSJosef Bacik }
18388c38938cSJosef Bacik
btrfs_free_fs_roots(struct btrfs_fs_info * fs_info)1839faa2dbf0SJosef Bacik void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info)
1840171f6537SJosef Bacik {
1841fc7cbcd4SDavid Sterba int ret;
1842fc7cbcd4SDavid Sterba struct btrfs_root *gang[8];
1843fc7cbcd4SDavid Sterba int i;
1844171f6537SJosef Bacik
1845171f6537SJosef Bacik while (!list_empty(&fs_info->dead_roots)) {
1846fc7cbcd4SDavid Sterba gang[0] = list_entry(fs_info->dead_roots.next,
1847171f6537SJosef Bacik struct btrfs_root, root_list);
1848fc7cbcd4SDavid Sterba list_del(&gang[0]->root_list);
1849171f6537SJosef Bacik
1850fc7cbcd4SDavid Sterba if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state))
1851fc7cbcd4SDavid Sterba btrfs_drop_and_free_fs_root(fs_info, gang[0]);
1852fc7cbcd4SDavid Sterba btrfs_put_root(gang[0]);
1853171f6537SJosef Bacik }
1854171f6537SJosef Bacik
1855fc7cbcd4SDavid Sterba while (1) {
1856fc7cbcd4SDavid Sterba ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
1857fc7cbcd4SDavid Sterba (void **)gang, 0,
1858fc7cbcd4SDavid Sterba ARRAY_SIZE(gang));
1859fc7cbcd4SDavid Sterba if (!ret)
1860fc7cbcd4SDavid Sterba break;
1861fc7cbcd4SDavid Sterba for (i = 0; i < ret; i++)
1862fc7cbcd4SDavid Sterba btrfs_drop_and_free_fs_root(fs_info, gang[i]);
1863171f6537SJosef Bacik }
1864171f6537SJosef Bacik }
1865af31f5e5SChris Mason
btrfs_init_scrub(struct btrfs_fs_info * fs_info)1866638aa7edSEric Sandeen static void btrfs_init_scrub(struct btrfs_fs_info *fs_info)
1867638aa7edSEric Sandeen {
1868638aa7edSEric Sandeen mutex_init(&fs_info->scrub_lock);
1869638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_running, 0);
1870638aa7edSEric Sandeen atomic_set(&fs_info->scrub_pause_req, 0);
1871638aa7edSEric Sandeen atomic_set(&fs_info->scrubs_paused, 0);
1872638aa7edSEric Sandeen atomic_set(&fs_info->scrub_cancel_req, 0);
1873638aa7edSEric Sandeen init_waitqueue_head(&fs_info->scrub_pause_wait);
1874ff09c4caSAnand Jain refcount_set(&fs_info->scrub_workers_refcnt, 0);
1875638aa7edSEric Sandeen }
1876638aa7edSEric Sandeen
btrfs_init_balance(struct btrfs_fs_info * fs_info)1877779a65a4SEric Sandeen static void btrfs_init_balance(struct btrfs_fs_info *fs_info)
1878779a65a4SEric Sandeen {
1879779a65a4SEric Sandeen spin_lock_init(&fs_info->balance_lock);
1880779a65a4SEric Sandeen mutex_init(&fs_info->balance_mutex);
1881779a65a4SEric Sandeen atomic_set(&fs_info->balance_pause_req, 0);
1882779a65a4SEric Sandeen atomic_set(&fs_info->balance_cancel_req, 0);
1883779a65a4SEric Sandeen fs_info->balance_ctl = NULL;
1884779a65a4SEric Sandeen init_waitqueue_head(&fs_info->balance_wait_q);
1885907d2710SDavid Sterba atomic_set(&fs_info->reloc_cancel_req, 0);
1886779a65a4SEric Sandeen }
1887779a65a4SEric Sandeen
btrfs_init_btree_inode(struct super_block * sb)1888dcb2137cSChristoph Hellwig static int btrfs_init_btree_inode(struct super_block *sb)
1889f37938e0SEric Sandeen {
1890dcb2137cSChristoph Hellwig struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1891e256927bSJosef Bacik unsigned long hash = btrfs_inode_hash(BTRFS_BTREE_INODE_OBJECTID,
1892e256927bSJosef Bacik fs_info->tree_root);
1893dcb2137cSChristoph Hellwig struct inode *inode;
1894dcb2137cSChristoph Hellwig
1895dcb2137cSChristoph Hellwig inode = new_inode(sb);
1896dcb2137cSChristoph Hellwig if (!inode)
1897dcb2137cSChristoph Hellwig return -ENOMEM;
18982ff7e61eSJeff Mahoney
18992ff7e61eSJeff Mahoney inode->i_ino = BTRFS_BTREE_INODE_OBJECTID;
19002ff7e61eSJeff Mahoney set_nlink(inode, 1);
1901f37938e0SEric Sandeen /*
1902f37938e0SEric Sandeen * we set the i_size on the btree inode to the max possible int.
1903f37938e0SEric Sandeen * the real end of the address space is determined by all of
1904f37938e0SEric Sandeen * the devices in the system
1905f37938e0SEric Sandeen */
19062ff7e61eSJeff Mahoney inode->i_size = OFFSET_MAX;
19072ff7e61eSJeff Mahoney inode->i_mapping->a_ops = &btree_aops;
1908dcb2137cSChristoph Hellwig mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
1909f37938e0SEric Sandeen
19102ff7e61eSJeff Mahoney RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
191143eb5f29SQu Wenruo extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree,
191235da5a7eSDavid Sterba IO_TREE_BTREE_INODE_IO);
19132ff7e61eSJeff Mahoney extent_map_tree_init(&BTRFS_I(inode)->extent_tree);
1914f37938e0SEric Sandeen
19155c8fd99fSJosef Bacik BTRFS_I(inode)->root = btrfs_grab_root(fs_info->tree_root);
1916adac5584SFilipe Manana BTRFS_I(inode)->location.objectid = BTRFS_BTREE_INODE_OBJECTID;
1917adac5584SFilipe Manana BTRFS_I(inode)->location.type = 0;
1918adac5584SFilipe Manana BTRFS_I(inode)->location.offset = 0;
19192ff7e61eSJeff Mahoney set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
1920e256927bSJosef Bacik __insert_inode_hash(inode, hash);
1921dcb2137cSChristoph Hellwig fs_info->btree_inode = inode;
1922dcb2137cSChristoph Hellwig
1923dcb2137cSChristoph Hellwig return 0;
1924f37938e0SEric Sandeen }
1925f37938e0SEric Sandeen
btrfs_init_dev_replace_locks(struct btrfs_fs_info * fs_info)1926ad618368SEric Sandeen static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info)
1927ad618368SEric Sandeen {
1928ad618368SEric Sandeen mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount);
1929129827e3SDavid Sterba init_rwsem(&fs_info->dev_replace.rwsem);
19307f8d236aSDavid Sterba init_waitqueue_head(&fs_info->dev_replace.replace_wait);
1931ad618368SEric Sandeen }
1932ad618368SEric Sandeen
btrfs_init_qgroup(struct btrfs_fs_info * fs_info)1933f9e92e40SEric Sandeen static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info)
1934f9e92e40SEric Sandeen {
1935f9e92e40SEric Sandeen spin_lock_init(&fs_info->qgroup_lock);
1936f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_ioctl_lock);
1937f9e92e40SEric Sandeen fs_info->qgroup_tree = RB_ROOT;
1938f9e92e40SEric Sandeen INIT_LIST_HEAD(&fs_info->dirty_qgroups);
1939f9e92e40SEric Sandeen fs_info->qgroup_seq = 1;
1940f9e92e40SEric Sandeen fs_info->qgroup_ulist = NULL;
1941d2c609b8SJeff Mahoney fs_info->qgroup_rescan_running = false;
1942011b46c3SQu Wenruo fs_info->qgroup_drop_subtree_thres = BTRFS_MAX_LEVEL;
1943f9e92e40SEric Sandeen mutex_init(&fs_info->qgroup_rescan_lock);
1944f9e92e40SEric Sandeen }
1945f9e92e40SEric Sandeen
btrfs_init_workqueues(struct btrfs_fs_info * fs_info)1946d21deec5SSu Yue static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info)
19472a458198SEric Sandeen {
1948f7b885beSAnand Jain u32 max_active = fs_info->thread_pool_size;
19496f011058SDavid Sterba unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND;
195058e814fcSTejun Heo unsigned int ordered_flags = WQ_MEM_RECLAIM | WQ_FREEZABLE;
19512a458198SEric Sandeen
19522a458198SEric Sandeen fs_info->workers =
1953a31b4a43SChristoph Hellwig btrfs_alloc_workqueue(fs_info, "worker", flags, max_active, 16);
19542a458198SEric Sandeen
19552a458198SEric Sandeen fs_info->delalloc_workers =
1956cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delalloc",
1957cb001095SJeff Mahoney flags, max_active, 2);
19582a458198SEric Sandeen
19592a458198SEric Sandeen fs_info->flush_workers =
1960cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "flush_delalloc",
1961cb001095SJeff Mahoney flags, max_active, 0);
19622a458198SEric Sandeen
19632a458198SEric Sandeen fs_info->caching_workers =
1964cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0);
19652a458198SEric Sandeen
19662a458198SEric Sandeen fs_info->fixup_workers =
196758e814fcSTejun Heo btrfs_alloc_ordered_workqueue(fs_info, "fixup", ordered_flags);
19682a458198SEric Sandeen
19692a458198SEric Sandeen fs_info->endio_workers =
1970d7b9416fSChristoph Hellwig alloc_workqueue("btrfs-endio", flags, max_active);
19712a458198SEric Sandeen fs_info->endio_meta_workers =
1972d7b9416fSChristoph Hellwig alloc_workqueue("btrfs-endio-meta", flags, max_active);
1973385de0efSChristoph Hellwig fs_info->rmw_workers = alloc_workqueue("btrfs-rmw", flags, max_active);
19742a458198SEric Sandeen fs_info->endio_write_workers =
1975cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "endio-write", flags,
1976cb001095SJeff Mahoney max_active, 2);
1977fed8a72dSChristoph Hellwig fs_info->compressed_write_workers =
1978fed8a72dSChristoph Hellwig alloc_workqueue("btrfs-compressed-write", flags, max_active);
19792a458198SEric Sandeen fs_info->endio_freespace_worker =
1980cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "freespace-write", flags,
1981cb001095SJeff Mahoney max_active, 0);
19822a458198SEric Sandeen fs_info->delayed_workers =
1983cb001095SJeff Mahoney btrfs_alloc_workqueue(fs_info, "delayed-meta", flags,
1984cb001095SJeff Mahoney max_active, 0);
19852a458198SEric Sandeen fs_info->qgroup_rescan_workers =
198658e814fcSTejun Heo btrfs_alloc_ordered_workqueue(fs_info, "qgroup-rescan",
198758e814fcSTejun Heo ordered_flags);
1988b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers =
198958e814fcSTejun Heo alloc_ordered_workqueue("btrfs_discard", WQ_FREEZABLE);
19902a458198SEric Sandeen
19918bfec2e4SChristoph Hellwig if (!(fs_info->workers &&
1992a31b4a43SChristoph Hellwig fs_info->delalloc_workers && fs_info->flush_workers &&
19932a458198SEric Sandeen fs_info->endio_workers && fs_info->endio_meta_workers &&
1994fed8a72dSChristoph Hellwig fs_info->compressed_write_workers &&
19951a1a2851SQu Wenruo fs_info->endio_write_workers &&
19962a458198SEric Sandeen fs_info->endio_freespace_worker && fs_info->rmw_workers &&
1997f26c9238SQu Wenruo fs_info->caching_workers && fs_info->fixup_workers &&
1998f26c9238SQu Wenruo fs_info->delayed_workers && fs_info->qgroup_rescan_workers &&
1999b0643e59SDennis Zhou fs_info->discard_ctl.discard_workers)) {
20002a458198SEric Sandeen return -ENOMEM;
20012a458198SEric Sandeen }
20022a458198SEric Sandeen
20032a458198SEric Sandeen return 0;
20042a458198SEric Sandeen }
20052a458198SEric Sandeen
btrfs_init_csum_hash(struct btrfs_fs_info * fs_info,u16 csum_type)20066d97c6e3SJohannes Thumshirn static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type)
20076d97c6e3SJohannes Thumshirn {
20086d97c6e3SJohannes Thumshirn struct crypto_shash *csum_shash;
2009b4e967beSDavid Sterba const char *csum_driver = btrfs_super_csum_driver(csum_type);
20106d97c6e3SJohannes Thumshirn
2011b4e967beSDavid Sterba csum_shash = crypto_alloc_shash(csum_driver, 0, 0);
20126d97c6e3SJohannes Thumshirn
20136d97c6e3SJohannes Thumshirn if (IS_ERR(csum_shash)) {
20146d97c6e3SJohannes Thumshirn btrfs_err(fs_info, "error allocating %s hash for checksum",
2015b4e967beSDavid Sterba csum_driver);
20166d97c6e3SJohannes Thumshirn return PTR_ERR(csum_shash);
20176d97c6e3SJohannes Thumshirn }
20186d97c6e3SJohannes Thumshirn
20196d97c6e3SJohannes Thumshirn fs_info->csum_shash = csum_shash;
20206d97c6e3SJohannes Thumshirn
202168d99ab0SChristoph Hellwig /*
202268d99ab0SChristoph Hellwig * Check if the checksum implementation is a fast accelerated one.
202368d99ab0SChristoph Hellwig * As-is this is a bit of a hack and should be replaced once the csum
202468d99ab0SChristoph Hellwig * implementations provide that information themselves.
202568d99ab0SChristoph Hellwig */
202668d99ab0SChristoph Hellwig switch (csum_type) {
202768d99ab0SChristoph Hellwig case BTRFS_CSUM_TYPE_CRC32:
202868d99ab0SChristoph Hellwig if (!strstr(crypto_shash_driver_name(csum_shash), "generic"))
202968d99ab0SChristoph Hellwig set_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags);
203068d99ab0SChristoph Hellwig break;
2031efcfcbc6SDavid Sterba case BTRFS_CSUM_TYPE_XXHASH:
2032efcfcbc6SDavid Sterba set_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags);
2033efcfcbc6SDavid Sterba break;
203468d99ab0SChristoph Hellwig default:
203568d99ab0SChristoph Hellwig break;
203668d99ab0SChristoph Hellwig }
203768d99ab0SChristoph Hellwig
2038c8a5f8caSDavid Sterba btrfs_info(fs_info, "using %s (%s) checksum algorithm",
2039c8a5f8caSDavid Sterba btrfs_super_csum_name(csum_type),
2040c8a5f8caSDavid Sterba crypto_shash_driver_name(csum_shash));
20416d97c6e3SJohannes Thumshirn return 0;
20426d97c6e3SJohannes Thumshirn }
20436d97c6e3SJohannes Thumshirn
btrfs_replay_log(struct btrfs_fs_info * fs_info,struct btrfs_fs_devices * fs_devices)204463443bf5SEric Sandeen static int btrfs_replay_log(struct btrfs_fs_info *fs_info,
204563443bf5SEric Sandeen struct btrfs_fs_devices *fs_devices)
204663443bf5SEric Sandeen {
204763443bf5SEric Sandeen int ret;
2048789d6a3aSQu Wenruo struct btrfs_tree_parent_check check = { 0 };
204963443bf5SEric Sandeen struct btrfs_root *log_tree_root;
205063443bf5SEric Sandeen struct btrfs_super_block *disk_super = fs_info->super_copy;
205163443bf5SEric Sandeen u64 bytenr = btrfs_super_log_root(disk_super);
2052581c1760SQu Wenruo int level = btrfs_super_log_root_level(disk_super);
205363443bf5SEric Sandeen
205463443bf5SEric Sandeen if (fs_devices->rw_devices == 0) {
2055f14d104dSDavid Sterba btrfs_warn(fs_info, "log replay required on RO media");
205663443bf5SEric Sandeen return -EIO;
205763443bf5SEric Sandeen }
205863443bf5SEric Sandeen
205996dfcb46SJosef Bacik log_tree_root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID,
206096dfcb46SJosef Bacik GFP_KERNEL);
206163443bf5SEric Sandeen if (!log_tree_root)
206263443bf5SEric Sandeen return -ENOMEM;
206363443bf5SEric Sandeen
2064789d6a3aSQu Wenruo check.level = level;
2065789d6a3aSQu Wenruo check.transid = fs_info->generation + 1;
2066789d6a3aSQu Wenruo check.owner_root = BTRFS_TREE_LOG_OBJECTID;
2067789d6a3aSQu Wenruo log_tree_root->node = read_tree_block(fs_info, bytenr, &check);
206864c043deSLiu Bo if (IS_ERR(log_tree_root->node)) {
2069f14d104dSDavid Sterba btrfs_warn(fs_info, "failed to read log tree");
20700eeff236SLiu Bo ret = PTR_ERR(log_tree_root->node);
20718c38938cSJosef Bacik log_tree_root->node = NULL;
207200246528SJosef Bacik btrfs_put_root(log_tree_root);
20730eeff236SLiu Bo return ret;
20744eb150d6SQu Wenruo }
20754eb150d6SQu Wenruo if (!extent_buffer_uptodate(log_tree_root->node)) {
2076f14d104dSDavid Sterba btrfs_err(fs_info, "failed to read log tree");
207700246528SJosef Bacik btrfs_put_root(log_tree_root);
207863443bf5SEric Sandeen return -EIO;
207963443bf5SEric Sandeen }
20804eb150d6SQu Wenruo
208163443bf5SEric Sandeen /* returns with log_tree_root freed on success */
208263443bf5SEric Sandeen ret = btrfs_recover_log_trees(log_tree_root);
208363443bf5SEric Sandeen if (ret) {
20840b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret,
208563443bf5SEric Sandeen "Failed to recover log tree");
208600246528SJosef Bacik btrfs_put_root(log_tree_root);
208763443bf5SEric Sandeen return ret;
208863443bf5SEric Sandeen }
208963443bf5SEric Sandeen
2090bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) {
20916bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info);
209263443bf5SEric Sandeen if (ret)
209363443bf5SEric Sandeen return ret;
209463443bf5SEric Sandeen }
209563443bf5SEric Sandeen
209663443bf5SEric Sandeen return 0;
209763443bf5SEric Sandeen }
209863443bf5SEric Sandeen
load_global_roots_objectid(struct btrfs_root * tree_root,struct btrfs_path * path,u64 objectid,const char * name)2099abed4aaaSJosef Bacik static int load_global_roots_objectid(struct btrfs_root *tree_root,
2100abed4aaaSJosef Bacik struct btrfs_path *path, u64 objectid,
2101abed4aaaSJosef Bacik const char *name)
2102abed4aaaSJosef Bacik {
2103abed4aaaSJosef Bacik struct btrfs_fs_info *fs_info = tree_root->fs_info;
2104abed4aaaSJosef Bacik struct btrfs_root *root;
2105f7238e50SJosef Bacik u64 max_global_id = 0;
2106abed4aaaSJosef Bacik int ret;
2107abed4aaaSJosef Bacik struct btrfs_key key = {
2108abed4aaaSJosef Bacik .objectid = objectid,
2109abed4aaaSJosef Bacik .type = BTRFS_ROOT_ITEM_KEY,
2110abed4aaaSJosef Bacik .offset = 0,
2111abed4aaaSJosef Bacik };
2112abed4aaaSJosef Bacik bool found = false;
2113abed4aaaSJosef Bacik
2114abed4aaaSJosef Bacik /* If we have IGNOREDATACSUMS skip loading these roots. */
2115abed4aaaSJosef Bacik if (objectid == BTRFS_CSUM_TREE_OBJECTID &&
2116abed4aaaSJosef Bacik btrfs_test_opt(fs_info, IGNOREDATACSUMS)) {
2117abed4aaaSJosef Bacik set_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state);
2118abed4aaaSJosef Bacik return 0;
2119abed4aaaSJosef Bacik }
2120abed4aaaSJosef Bacik
2121abed4aaaSJosef Bacik while (1) {
2122abed4aaaSJosef Bacik ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
2123abed4aaaSJosef Bacik if (ret < 0)
2124abed4aaaSJosef Bacik break;
2125abed4aaaSJosef Bacik
2126abed4aaaSJosef Bacik if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2127abed4aaaSJosef Bacik ret = btrfs_next_leaf(tree_root, path);
2128abed4aaaSJosef Bacik if (ret) {
2129abed4aaaSJosef Bacik if (ret > 0)
2130abed4aaaSJosef Bacik ret = 0;
2131abed4aaaSJosef Bacik break;
2132abed4aaaSJosef Bacik }
2133abed4aaaSJosef Bacik }
2134abed4aaaSJosef Bacik ret = 0;
2135abed4aaaSJosef Bacik
2136abed4aaaSJosef Bacik btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2137abed4aaaSJosef Bacik if (key.objectid != objectid)
2138abed4aaaSJosef Bacik break;
2139abed4aaaSJosef Bacik btrfs_release_path(path);
2140abed4aaaSJosef Bacik
2141f7238e50SJosef Bacik /*
2142f7238e50SJosef Bacik * Just worry about this for extent tree, it'll be the same for
2143f7238e50SJosef Bacik * everybody.
2144f7238e50SJosef Bacik */
2145f7238e50SJosef Bacik if (objectid == BTRFS_EXTENT_TREE_OBJECTID)
2146f7238e50SJosef Bacik max_global_id = max(max_global_id, key.offset);
2147f7238e50SJosef Bacik
2148abed4aaaSJosef Bacik found = true;
2149abed4aaaSJosef Bacik root = read_tree_root_path(tree_root, path, &key);
2150abed4aaaSJosef Bacik if (IS_ERR(root)) {
2151abed4aaaSJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS))
2152abed4aaaSJosef Bacik ret = PTR_ERR(root);
2153abed4aaaSJosef Bacik break;
2154abed4aaaSJosef Bacik }
2155abed4aaaSJosef Bacik set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2156abed4aaaSJosef Bacik ret = btrfs_global_root_insert(root);
2157abed4aaaSJosef Bacik if (ret) {
2158abed4aaaSJosef Bacik btrfs_put_root(root);
2159abed4aaaSJosef Bacik break;
2160abed4aaaSJosef Bacik }
2161abed4aaaSJosef Bacik key.offset++;
2162abed4aaaSJosef Bacik }
2163abed4aaaSJosef Bacik btrfs_release_path(path);
2164abed4aaaSJosef Bacik
2165f7238e50SJosef Bacik if (objectid == BTRFS_EXTENT_TREE_OBJECTID)
2166f7238e50SJosef Bacik fs_info->nr_global_roots = max_global_id + 1;
2167f7238e50SJosef Bacik
2168abed4aaaSJosef Bacik if (!found || ret) {
2169abed4aaaSJosef Bacik if (objectid == BTRFS_CSUM_TREE_OBJECTID)
2170abed4aaaSJosef Bacik set_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state);
2171abed4aaaSJosef Bacik
2172abed4aaaSJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS))
2173abed4aaaSJosef Bacik ret = ret ? ret : -ENOENT;
2174abed4aaaSJosef Bacik else
2175abed4aaaSJosef Bacik ret = 0;
2176abed4aaaSJosef Bacik btrfs_err(fs_info, "failed to load root %s", name);
2177abed4aaaSJosef Bacik }
2178abed4aaaSJosef Bacik return ret;
2179abed4aaaSJosef Bacik }
2180abed4aaaSJosef Bacik
load_global_roots(struct btrfs_root * tree_root)2181abed4aaaSJosef Bacik static int load_global_roots(struct btrfs_root *tree_root)
2182abed4aaaSJosef Bacik {
2183abed4aaaSJosef Bacik struct btrfs_path *path;
2184abed4aaaSJosef Bacik int ret = 0;
2185abed4aaaSJosef Bacik
2186abed4aaaSJosef Bacik path = btrfs_alloc_path();
2187abed4aaaSJosef Bacik if (!path)
2188abed4aaaSJosef Bacik return -ENOMEM;
2189abed4aaaSJosef Bacik
2190abed4aaaSJosef Bacik ret = load_global_roots_objectid(tree_root, path,
2191abed4aaaSJosef Bacik BTRFS_EXTENT_TREE_OBJECTID, "extent");
2192abed4aaaSJosef Bacik if (ret)
2193abed4aaaSJosef Bacik goto out;
2194abed4aaaSJosef Bacik ret = load_global_roots_objectid(tree_root, path,
2195abed4aaaSJosef Bacik BTRFS_CSUM_TREE_OBJECTID, "csum");
2196abed4aaaSJosef Bacik if (ret)
2197abed4aaaSJosef Bacik goto out;
2198abed4aaaSJosef Bacik if (!btrfs_fs_compat_ro(tree_root->fs_info, FREE_SPACE_TREE))
2199abed4aaaSJosef Bacik goto out;
2200abed4aaaSJosef Bacik ret = load_global_roots_objectid(tree_root, path,
2201abed4aaaSJosef Bacik BTRFS_FREE_SPACE_TREE_OBJECTID,
2202abed4aaaSJosef Bacik "free space");
2203abed4aaaSJosef Bacik out:
2204abed4aaaSJosef Bacik btrfs_free_path(path);
2205abed4aaaSJosef Bacik return ret;
2206abed4aaaSJosef Bacik }
2207abed4aaaSJosef Bacik
btrfs_read_roots(struct btrfs_fs_info * fs_info)22086bccf3abSJeff Mahoney static int btrfs_read_roots(struct btrfs_fs_info *fs_info)
22094bbcaa64SEric Sandeen {
22106bccf3abSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root;
2211a4f3d2c4SDavid Sterba struct btrfs_root *root;
22124bbcaa64SEric Sandeen struct btrfs_key location;
22134bbcaa64SEric Sandeen int ret;
22144bbcaa64SEric Sandeen
22156bccf3abSJeff Mahoney BUG_ON(!fs_info->tree_root);
22166bccf3abSJeff Mahoney
2217abed4aaaSJosef Bacik ret = load_global_roots(tree_root);
2218abed4aaaSJosef Bacik if (ret)
2219abed4aaaSJosef Bacik return ret;
2220abed4aaaSJosef Bacik
22214bbcaa64SEric Sandeen location.type = BTRFS_ROOT_ITEM_KEY;
22224bbcaa64SEric Sandeen location.offset = 0;
22234bbcaa64SEric Sandeen
22241c56ab99SQu Wenruo if (btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE)) {
222514033b08SQu Wenruo location.objectid = BTRFS_BLOCK_GROUP_TREE_OBJECTID;
222614033b08SQu Wenruo root = btrfs_read_tree_root(tree_root, &location);
222714033b08SQu Wenruo if (IS_ERR(root)) {
222814033b08SQu Wenruo if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
222914033b08SQu Wenruo ret = PTR_ERR(root);
223014033b08SQu Wenruo goto out;
223114033b08SQu Wenruo }
223214033b08SQu Wenruo } else {
223314033b08SQu Wenruo set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
223414033b08SQu Wenruo fs_info->block_group_root = root;
223514033b08SQu Wenruo }
223614033b08SQu Wenruo }
223714033b08SQu Wenruo
223814033b08SQu Wenruo location.objectid = BTRFS_DEV_TREE_OBJECTID;
2239a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location);
2240f50f4353SLiu Bo if (IS_ERR(root)) {
224142437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
2242f50f4353SLiu Bo ret = PTR_ERR(root);
2243f50f4353SLiu Bo goto out;
2244f50f4353SLiu Bo }
224542437a63SJosef Bacik } else {
2246a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2247a4f3d2c4SDavid Sterba fs_info->dev_root = root;
224842437a63SJosef Bacik }
2249820a49daSJosef Bacik /* Initialize fs_info for all devices in any case */
2250a8d1b164SJohannes Thumshirn ret = btrfs_init_devices_late(fs_info);
2251a8d1b164SJohannes Thumshirn if (ret)
2252a8d1b164SJohannes Thumshirn goto out;
22534bbcaa64SEric Sandeen
2254aeb935a4SQu Wenruo /*
2255aeb935a4SQu Wenruo * This tree can share blocks with some other fs tree during relocation
2256aeb935a4SQu Wenruo * and we need a proper setup by btrfs_get_fs_root
2257aeb935a4SQu Wenruo */
225856e9357aSDavid Sterba root = btrfs_get_fs_root(tree_root->fs_info,
225956e9357aSDavid Sterba BTRFS_DATA_RELOC_TREE_OBJECTID, true);
2260aeb935a4SQu Wenruo if (IS_ERR(root)) {
226142437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
2262aeb935a4SQu Wenruo ret = PTR_ERR(root);
2263aeb935a4SQu Wenruo goto out;
2264aeb935a4SQu Wenruo }
226542437a63SJosef Bacik } else {
2266aeb935a4SQu Wenruo set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2267aeb935a4SQu Wenruo fs_info->data_reloc_root = root;
226842437a63SJosef Bacik }
2269aeb935a4SQu Wenruo
22704bbcaa64SEric Sandeen location.objectid = BTRFS_QUOTA_TREE_OBJECTID;
2271a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location);
2272a4f3d2c4SDavid Sterba if (!IS_ERR(root)) {
2273a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2274afcdd129SJosef Bacik set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
2275a4f3d2c4SDavid Sterba fs_info->quota_root = root;
22764bbcaa64SEric Sandeen }
22774bbcaa64SEric Sandeen
22784bbcaa64SEric Sandeen location.objectid = BTRFS_UUID_TREE_OBJECTID;
2279a4f3d2c4SDavid Sterba root = btrfs_read_tree_root(tree_root, &location);
2280a4f3d2c4SDavid Sterba if (IS_ERR(root)) {
228142437a63SJosef Bacik if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
2282a4f3d2c4SDavid Sterba ret = PTR_ERR(root);
22834bbcaa64SEric Sandeen if (ret != -ENOENT)
2284f50f4353SLiu Bo goto out;
228542437a63SJosef Bacik }
22864bbcaa64SEric Sandeen } else {
2287a4f3d2c4SDavid Sterba set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2288a4f3d2c4SDavid Sterba fs_info->uuid_root = root;
22894bbcaa64SEric Sandeen }
22904bbcaa64SEric Sandeen
22914bbcaa64SEric Sandeen return 0;
2292f50f4353SLiu Bo out:
2293f50f4353SLiu Bo btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d",
2294f50f4353SLiu Bo location.objectid, ret);
2295f50f4353SLiu Bo return ret;
22964bbcaa64SEric Sandeen }
22974bbcaa64SEric Sandeen
2298069ec957SQu Wenruo /*
2299069ec957SQu Wenruo * Real super block validation
2300069ec957SQu Wenruo * NOTE: super csum type and incompat features will not be checked here.
2301069ec957SQu Wenruo *
2302069ec957SQu Wenruo * @sb: super block to check
2303069ec957SQu Wenruo * @mirror_num: the super block number to check its bytenr:
2304069ec957SQu Wenruo * 0 the primary (1st) sb
2305069ec957SQu Wenruo * 1, 2 2nd and 3rd backup copy
2306069ec957SQu Wenruo * -1 skip bytenr check
2307069ec957SQu Wenruo */
btrfs_validate_super(struct btrfs_fs_info * fs_info,struct btrfs_super_block * sb,int mirror_num)2308a05d3c91SQu Wenruo int btrfs_validate_super(struct btrfs_fs_info *fs_info,
2309069ec957SQu Wenruo struct btrfs_super_block *sb, int mirror_num)
231021a852b0SQu Wenruo {
231121a852b0SQu Wenruo u64 nodesize = btrfs_super_nodesize(sb);
231221a852b0SQu Wenruo u64 sectorsize = btrfs_super_sectorsize(sb);
231321a852b0SQu Wenruo int ret = 0;
231421a852b0SQu Wenruo
231521a852b0SQu Wenruo if (btrfs_super_magic(sb) != BTRFS_MAGIC) {
231621a852b0SQu Wenruo btrfs_err(fs_info, "no valid FS found");
231721a852b0SQu Wenruo ret = -EINVAL;
231821a852b0SQu Wenruo }
231921a852b0SQu Wenruo if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) {
232021a852b0SQu Wenruo btrfs_err(fs_info, "unrecognized or unsupported super flag: %llu",
232121a852b0SQu Wenruo btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP);
232221a852b0SQu Wenruo ret = -EINVAL;
232321a852b0SQu Wenruo }
232421a852b0SQu Wenruo if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) {
232521a852b0SQu Wenruo btrfs_err(fs_info, "tree_root level too big: %d >= %d",
232621a852b0SQu Wenruo btrfs_super_root_level(sb), BTRFS_MAX_LEVEL);
232721a852b0SQu Wenruo ret = -EINVAL;
232821a852b0SQu Wenruo }
232921a852b0SQu Wenruo if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) {
233021a852b0SQu Wenruo btrfs_err(fs_info, "chunk_root level too big: %d >= %d",
233121a852b0SQu Wenruo btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL);
233221a852b0SQu Wenruo ret = -EINVAL;
233321a852b0SQu Wenruo }
233421a852b0SQu Wenruo if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) {
233521a852b0SQu Wenruo btrfs_err(fs_info, "log_root level too big: %d >= %d",
233621a852b0SQu Wenruo btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL);
233721a852b0SQu Wenruo ret = -EINVAL;
233821a852b0SQu Wenruo }
233921a852b0SQu Wenruo
234021a852b0SQu Wenruo /*
234121a852b0SQu Wenruo * Check sectorsize and nodesize first, other check will need it.
234221a852b0SQu Wenruo * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here.
234321a852b0SQu Wenruo */
234421a852b0SQu Wenruo if (!is_power_of_2(sectorsize) || sectorsize < 4096 ||
234521a852b0SQu Wenruo sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) {
234621a852b0SQu Wenruo btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize);
234721a852b0SQu Wenruo ret = -EINVAL;
234821a852b0SQu Wenruo }
23490bb3eb3eSQu Wenruo
23500bb3eb3eSQu Wenruo /*
23511a42daabSQu Wenruo * We only support at most two sectorsizes: 4K and PAGE_SIZE.
23521a42daabSQu Wenruo *
23531a42daabSQu Wenruo * We can support 16K sectorsize with 64K page size without problem,
23541a42daabSQu Wenruo * but such sectorsize/pagesize combination doesn't make much sense.
23551a42daabSQu Wenruo * 4K will be our future standard, PAGE_SIZE is supported from the very
23561a42daabSQu Wenruo * beginning.
23570bb3eb3eSQu Wenruo */
23581a42daabSQu Wenruo if (sectorsize > PAGE_SIZE || (sectorsize != SZ_4K && sectorsize != PAGE_SIZE)) {
235921a852b0SQu Wenruo btrfs_err(fs_info,
23600bb3eb3eSQu Wenruo "sectorsize %llu not yet supported for page size %lu",
236121a852b0SQu Wenruo sectorsize, PAGE_SIZE);
236221a852b0SQu Wenruo ret = -EINVAL;
236321a852b0SQu Wenruo }
23640bb3eb3eSQu Wenruo
236521a852b0SQu Wenruo if (!is_power_of_2(nodesize) || nodesize < sectorsize ||
236621a852b0SQu Wenruo nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) {
236721a852b0SQu Wenruo btrfs_err(fs_info, "invalid nodesize %llu", nodesize);
236821a852b0SQu Wenruo ret = -EINVAL;
236921a852b0SQu Wenruo }
237021a852b0SQu Wenruo if (nodesize != le32_to_cpu(sb->__unused_leafsize)) {
237121a852b0SQu Wenruo btrfs_err(fs_info, "invalid leafsize %u, should be %llu",
237221a852b0SQu Wenruo le32_to_cpu(sb->__unused_leafsize), nodesize);
237321a852b0SQu Wenruo ret = -EINVAL;
237421a852b0SQu Wenruo }
237521a852b0SQu Wenruo
237621a852b0SQu Wenruo /* Root alignment check */
237721a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) {
237821a852b0SQu Wenruo btrfs_warn(fs_info, "tree_root block unaligned: %llu",
237921a852b0SQu Wenruo btrfs_super_root(sb));
238021a852b0SQu Wenruo ret = -EINVAL;
238121a852b0SQu Wenruo }
238221a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) {
238321a852b0SQu Wenruo btrfs_warn(fs_info, "chunk_root block unaligned: %llu",
238421a852b0SQu Wenruo btrfs_super_chunk_root(sb));
238521a852b0SQu Wenruo ret = -EINVAL;
238621a852b0SQu Wenruo }
238721a852b0SQu Wenruo if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) {
238821a852b0SQu Wenruo btrfs_warn(fs_info, "log_root block unaligned: %llu",
238921a852b0SQu Wenruo btrfs_super_log_root(sb));
239021a852b0SQu Wenruo ret = -EINVAL;
239121a852b0SQu Wenruo }
239221a852b0SQu Wenruo
2393d167aa76SAnand Jain if (memcmp(fs_info->fs_devices->fsid, sb->fsid, BTRFS_FSID_SIZE) != 0) {
2394aefd7f70SNikolay Borisov btrfs_err(fs_info,
2395aefd7f70SNikolay Borisov "superblock fsid doesn't match fsid of fs_devices: %pU != %pU",
2396d167aa76SAnand Jain sb->fsid, fs_info->fs_devices->fsid);
2397aefd7f70SNikolay Borisov ret = -EINVAL;
2398aefd7f70SNikolay Borisov }
2399aefd7f70SNikolay Borisov
24006bfe3959SAnand Jain if (memcmp(fs_info->fs_devices->metadata_uuid, btrfs_sb_fsid_ptr(sb),
24016bfe3959SAnand Jain BTRFS_FSID_SIZE) != 0) {
2402aefd7f70SNikolay Borisov btrfs_err(fs_info,
2403aefd7f70SNikolay Borisov "superblock metadata_uuid doesn't match metadata uuid of fs_devices: %pU != %pU",
24046bfe3959SAnand Jain btrfs_sb_fsid_ptr(sb), fs_info->fs_devices->metadata_uuid);
2405aefd7f70SNikolay Borisov ret = -EINVAL;
2406aefd7f70SNikolay Borisov }
2407aefd7f70SNikolay Borisov
240825984a5aSAnand Jain if (memcmp(fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid,
240925984a5aSAnand Jain BTRFS_FSID_SIZE) != 0) {
241025984a5aSAnand Jain btrfs_err(fs_info,
241125984a5aSAnand Jain "dev_item UUID does not match metadata fsid: %pU != %pU",
241225984a5aSAnand Jain fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid);
241325984a5aSAnand Jain ret = -EINVAL;
241425984a5aSAnand Jain }
241525984a5aSAnand Jain
24161c56ab99SQu Wenruo /*
24171c56ab99SQu Wenruo * Artificial requirement for block-group-tree to force newer features
24181c56ab99SQu Wenruo * (free-space-tree, no-holes) so the test matrix is smaller.
24191c56ab99SQu Wenruo */
24201c56ab99SQu Wenruo if (btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE) &&
24211c56ab99SQu Wenruo (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID) ||
24221c56ab99SQu Wenruo !btrfs_fs_incompat(fs_info, NO_HOLES))) {
24231c56ab99SQu Wenruo btrfs_err(fs_info,
24241c56ab99SQu Wenruo "block-group-tree feature requires fres-space-tree and no-holes");
24251c56ab99SQu Wenruo ret = -EINVAL;
24261c56ab99SQu Wenruo }
24271c56ab99SQu Wenruo
242821a852b0SQu Wenruo /*
242921a852b0SQu Wenruo * Hint to catch really bogus numbers, bitflips or so, more exact checks are
243021a852b0SQu Wenruo * done later
243121a852b0SQu Wenruo */
243221a852b0SQu Wenruo if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) {
243321a852b0SQu Wenruo btrfs_err(fs_info, "bytes_used is too small %llu",
243421a852b0SQu Wenruo btrfs_super_bytes_used(sb));
243521a852b0SQu Wenruo ret = -EINVAL;
243621a852b0SQu Wenruo }
243721a852b0SQu Wenruo if (!is_power_of_2(btrfs_super_stripesize(sb))) {
243821a852b0SQu Wenruo btrfs_err(fs_info, "invalid stripesize %u",
243921a852b0SQu Wenruo btrfs_super_stripesize(sb));
244021a852b0SQu Wenruo ret = -EINVAL;
244121a852b0SQu Wenruo }
244221a852b0SQu Wenruo if (btrfs_super_num_devices(sb) > (1UL << 31))
244321a852b0SQu Wenruo btrfs_warn(fs_info, "suspicious number of devices: %llu",
244421a852b0SQu Wenruo btrfs_super_num_devices(sb));
244521a852b0SQu Wenruo if (btrfs_super_num_devices(sb) == 0) {
244621a852b0SQu Wenruo btrfs_err(fs_info, "number of devices is 0");
244721a852b0SQu Wenruo ret = -EINVAL;
244821a852b0SQu Wenruo }
244921a852b0SQu Wenruo
2450069ec957SQu Wenruo if (mirror_num >= 0 &&
2451069ec957SQu Wenruo btrfs_super_bytenr(sb) != btrfs_sb_offset(mirror_num)) {
245221a852b0SQu Wenruo btrfs_err(fs_info, "super offset mismatch %llu != %u",
245321a852b0SQu Wenruo btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET);
245421a852b0SQu Wenruo ret = -EINVAL;
245521a852b0SQu Wenruo }
245621a852b0SQu Wenruo
245721a852b0SQu Wenruo /*
245821a852b0SQu Wenruo * Obvious sys_chunk_array corruptions, it must hold at least one key
245921a852b0SQu Wenruo * and one chunk
246021a852b0SQu Wenruo */
246121a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
246221a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too big %u > %u",
246321a852b0SQu Wenruo btrfs_super_sys_array_size(sb),
246421a852b0SQu Wenruo BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
246521a852b0SQu Wenruo ret = -EINVAL;
246621a852b0SQu Wenruo }
246721a852b0SQu Wenruo if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key)
246821a852b0SQu Wenruo + sizeof(struct btrfs_chunk)) {
246921a852b0SQu Wenruo btrfs_err(fs_info, "system chunk array too small %u < %zu",
247021a852b0SQu Wenruo btrfs_super_sys_array_size(sb),
247121a852b0SQu Wenruo sizeof(struct btrfs_disk_key)
247221a852b0SQu Wenruo + sizeof(struct btrfs_chunk));
247321a852b0SQu Wenruo ret = -EINVAL;
247421a852b0SQu Wenruo }
247521a852b0SQu Wenruo
247621a852b0SQu Wenruo /*
247721a852b0SQu Wenruo * The generation is a global counter, we'll trust it more than the others
247821a852b0SQu Wenruo * but it's still possible that it's the one that's wrong.
247921a852b0SQu Wenruo */
248021a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb))
248121a852b0SQu Wenruo btrfs_warn(fs_info,
248221a852b0SQu Wenruo "suspicious: generation < chunk_root_generation: %llu < %llu",
248321a852b0SQu Wenruo btrfs_super_generation(sb),
248421a852b0SQu Wenruo btrfs_super_chunk_root_generation(sb));
248521a852b0SQu Wenruo if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb)
248621a852b0SQu Wenruo && btrfs_super_cache_generation(sb) != (u64)-1)
248721a852b0SQu Wenruo btrfs_warn(fs_info,
248821a852b0SQu Wenruo "suspicious: generation < cache_generation: %llu < %llu",
248921a852b0SQu Wenruo btrfs_super_generation(sb),
249021a852b0SQu Wenruo btrfs_super_cache_generation(sb));
249121a852b0SQu Wenruo
249221a852b0SQu Wenruo return ret;
249321a852b0SQu Wenruo }
249421a852b0SQu Wenruo
2495069ec957SQu Wenruo /*
2496069ec957SQu Wenruo * Validation of super block at mount time.
2497069ec957SQu Wenruo * Some checks already done early at mount time, like csum type and incompat
2498069ec957SQu Wenruo * flags will be skipped.
2499069ec957SQu Wenruo */
btrfs_validate_mount_super(struct btrfs_fs_info * fs_info)2500069ec957SQu Wenruo static int btrfs_validate_mount_super(struct btrfs_fs_info *fs_info)
2501069ec957SQu Wenruo {
2502a05d3c91SQu Wenruo return btrfs_validate_super(fs_info, fs_info->super_copy, 0);
2503069ec957SQu Wenruo }
2504069ec957SQu Wenruo
250575cb857dSQu Wenruo /*
250675cb857dSQu Wenruo * Validation of super block at write time.
250775cb857dSQu Wenruo * Some checks like bytenr check will be skipped as their values will be
250875cb857dSQu Wenruo * overwritten soon.
250975cb857dSQu Wenruo * Extra checks like csum type and incompat flags will be done here.
251075cb857dSQu Wenruo */
btrfs_validate_write_super(struct btrfs_fs_info * fs_info,struct btrfs_super_block * sb)251175cb857dSQu Wenruo static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info,
251275cb857dSQu Wenruo struct btrfs_super_block *sb)
251375cb857dSQu Wenruo {
251475cb857dSQu Wenruo int ret;
251575cb857dSQu Wenruo
2516a05d3c91SQu Wenruo ret = btrfs_validate_super(fs_info, sb, -1);
251775cb857dSQu Wenruo if (ret < 0)
251875cb857dSQu Wenruo goto out;
2519e7e16f48SJohannes Thumshirn if (!btrfs_supported_super_csum(btrfs_super_csum_type(sb))) {
252075cb857dSQu Wenruo ret = -EUCLEAN;
252175cb857dSQu Wenruo btrfs_err(fs_info, "invalid csum type, has %u want %u",
252275cb857dSQu Wenruo btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32);
252375cb857dSQu Wenruo goto out;
252475cb857dSQu Wenruo }
252575cb857dSQu Wenruo if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) {
252675cb857dSQu Wenruo ret = -EUCLEAN;
252775cb857dSQu Wenruo btrfs_err(fs_info,
252875cb857dSQu Wenruo "invalid incompat flags, has 0x%llx valid mask 0x%llx",
252975cb857dSQu Wenruo btrfs_super_incompat_flags(sb),
253075cb857dSQu Wenruo (unsigned long long)BTRFS_FEATURE_INCOMPAT_SUPP);
253175cb857dSQu Wenruo goto out;
253275cb857dSQu Wenruo }
253375cb857dSQu Wenruo out:
253475cb857dSQu Wenruo if (ret < 0)
253575cb857dSQu Wenruo btrfs_err(fs_info,
253675cb857dSQu Wenruo "super block corruption detected before writing it to disk");
253775cb857dSQu Wenruo return ret;
253875cb857dSQu Wenruo }
253975cb857dSQu Wenruo
load_super_root(struct btrfs_root * root,u64 bytenr,u64 gen,int level)2540bd676446SJosef Bacik static int load_super_root(struct btrfs_root *root, u64 bytenr, u64 gen, int level)
2541bd676446SJosef Bacik {
2542789d6a3aSQu Wenruo struct btrfs_tree_parent_check check = {
2543789d6a3aSQu Wenruo .level = level,
2544789d6a3aSQu Wenruo .transid = gen,
2545789d6a3aSQu Wenruo .owner_root = root->root_key.objectid
2546789d6a3aSQu Wenruo };
2547bd676446SJosef Bacik int ret = 0;
2548bd676446SJosef Bacik
2549789d6a3aSQu Wenruo root->node = read_tree_block(root->fs_info, bytenr, &check);
2550bd676446SJosef Bacik if (IS_ERR(root->node)) {
2551bd676446SJosef Bacik ret = PTR_ERR(root->node);
2552bd676446SJosef Bacik root->node = NULL;
25534eb150d6SQu Wenruo return ret;
25544eb150d6SQu Wenruo }
25554eb150d6SQu Wenruo if (!extent_buffer_uptodate(root->node)) {
2556bd676446SJosef Bacik free_extent_buffer(root->node);
2557bd676446SJosef Bacik root->node = NULL;
25584eb150d6SQu Wenruo return -EIO;
2559bd676446SJosef Bacik }
2560bd676446SJosef Bacik
2561bd676446SJosef Bacik btrfs_set_root_node(&root->root_item, root->node);
2562bd676446SJosef Bacik root->commit_root = btrfs_root_node(root);
2563bd676446SJosef Bacik btrfs_set_root_refs(&root->root_item, 1);
2564bd676446SJosef Bacik return ret;
2565bd676446SJosef Bacik }
2566bd676446SJosef Bacik
load_important_roots(struct btrfs_fs_info * fs_info)2567bd676446SJosef Bacik static int load_important_roots(struct btrfs_fs_info *fs_info)
2568bd676446SJosef Bacik {
2569bd676446SJosef Bacik struct btrfs_super_block *sb = fs_info->super_copy;
2570bd676446SJosef Bacik u64 gen, bytenr;
2571bd676446SJosef Bacik int level, ret;
2572bd676446SJosef Bacik
2573bd676446SJosef Bacik bytenr = btrfs_super_root(sb);
2574bd676446SJosef Bacik gen = btrfs_super_generation(sb);
2575bd676446SJosef Bacik level = btrfs_super_root_level(sb);
2576bd676446SJosef Bacik ret = load_super_root(fs_info->tree_root, bytenr, gen, level);
25779c54e80dSJosef Bacik if (ret) {
2578bd676446SJosef Bacik btrfs_warn(fs_info, "couldn't read tree root");
2579bd676446SJosef Bacik return ret;
2580bd676446SJosef Bacik }
25819c54e80dSJosef Bacik return 0;
25829c54e80dSJosef Bacik }
25839c54e80dSJosef Bacik
init_tree_roots(struct btrfs_fs_info * fs_info)25846ef108ddSNikolay Borisov static int __cold init_tree_roots(struct btrfs_fs_info *fs_info)
2585b8522a1eSNikolay Borisov {
25866ef108ddSNikolay Borisov int backup_index = find_newest_super_backup(fs_info);
2587b8522a1eSNikolay Borisov struct btrfs_super_block *sb = fs_info->super_copy;
2588b8522a1eSNikolay Borisov struct btrfs_root *tree_root = fs_info->tree_root;
2589b8522a1eSNikolay Borisov bool handle_error = false;
2590b8522a1eSNikolay Borisov int ret = 0;
2591b8522a1eSNikolay Borisov int i;
2592b8522a1eSNikolay Borisov
2593b8522a1eSNikolay Borisov for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
2594b8522a1eSNikolay Borisov if (handle_error) {
2595b8522a1eSNikolay Borisov if (!IS_ERR(tree_root->node))
2596b8522a1eSNikolay Borisov free_extent_buffer(tree_root->node);
2597b8522a1eSNikolay Borisov tree_root->node = NULL;
2598b8522a1eSNikolay Borisov
2599b8522a1eSNikolay Borisov if (!btrfs_test_opt(fs_info, USEBACKUPROOT))
2600b8522a1eSNikolay Borisov break;
2601b8522a1eSNikolay Borisov
2602b8522a1eSNikolay Borisov free_root_pointers(fs_info, 0);
2603b8522a1eSNikolay Borisov
2604b8522a1eSNikolay Borisov /*
2605b8522a1eSNikolay Borisov * Don't use the log in recovery mode, it won't be
2606b8522a1eSNikolay Borisov * valid
2607b8522a1eSNikolay Borisov */
2608b8522a1eSNikolay Borisov btrfs_set_super_log_root(sb, 0);
2609b8522a1eSNikolay Borisov
2610b8522a1eSNikolay Borisov /* We can't trust the free space cache either */
2611b8522a1eSNikolay Borisov btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE);
2612b8522a1eSNikolay Borisov
2613745806fbSQu Wenruo btrfs_warn(fs_info, "try to load backup roots slot %d", i);
2614b8522a1eSNikolay Borisov ret = read_backup_root(fs_info, i);
26156ef108ddSNikolay Borisov backup_index = ret;
2616b8522a1eSNikolay Borisov if (ret < 0)
2617b8522a1eSNikolay Borisov return ret;
2618b8522a1eSNikolay Borisov }
2619b8522a1eSNikolay Borisov
2620bd676446SJosef Bacik ret = load_important_roots(fs_info);
2621bd676446SJosef Bacik if (ret) {
2622217f5004SNikolay Borisov handle_error = true;
2623b8522a1eSNikolay Borisov continue;
2624b8522a1eSNikolay Borisov }
2625b8522a1eSNikolay Borisov
2626336a0d8dSNikolay Borisov /*
2627336a0d8dSNikolay Borisov * No need to hold btrfs_root::objectid_mutex since the fs
2628336a0d8dSNikolay Borisov * hasn't been fully initialised and we are the only user
2629336a0d8dSNikolay Borisov */
2630453e4873SNikolay Borisov ret = btrfs_init_root_free_objectid(tree_root);
2631b8522a1eSNikolay Borisov if (ret < 0) {
2632b8522a1eSNikolay Borisov handle_error = true;
2633b8522a1eSNikolay Borisov continue;
2634b8522a1eSNikolay Borisov }
2635b8522a1eSNikolay Borisov
26366b8fad57SNikolay Borisov ASSERT(tree_root->free_objectid <= BTRFS_LAST_FREE_OBJECTID);
2637b8522a1eSNikolay Borisov
2638b8522a1eSNikolay Borisov ret = btrfs_read_roots(fs_info);
2639b8522a1eSNikolay Borisov if (ret < 0) {
2640b8522a1eSNikolay Borisov handle_error = true;
2641b8522a1eSNikolay Borisov continue;
2642b8522a1eSNikolay Borisov }
2643b8522a1eSNikolay Borisov
2644b8522a1eSNikolay Borisov /* All successful */
2645bd676446SJosef Bacik fs_info->generation = btrfs_header_generation(tree_root->node);
2646bd676446SJosef Bacik fs_info->last_trans_committed = fs_info->generation;
2647d96b3424SFilipe Manana fs_info->last_reloc_trans = 0;
26486ef108ddSNikolay Borisov
26496ef108ddSNikolay Borisov /* Always begin writing backup roots after the one being used */
26506ef108ddSNikolay Borisov if (backup_index < 0) {
26516ef108ddSNikolay Borisov fs_info->backup_root_index = 0;
26526ef108ddSNikolay Borisov } else {
26536ef108ddSNikolay Borisov fs_info->backup_root_index = backup_index + 1;
26546ef108ddSNikolay Borisov fs_info->backup_root_index %= BTRFS_NUM_BACKUP_ROOTS;
26556ef108ddSNikolay Borisov }
2656b8522a1eSNikolay Borisov break;
2657b8522a1eSNikolay Borisov }
2658b8522a1eSNikolay Borisov
2659b8522a1eSNikolay Borisov return ret;
2660b8522a1eSNikolay Borisov }
2661b8522a1eSNikolay Borisov
btrfs_init_fs_info(struct btrfs_fs_info * fs_info)26628260edbaSJosef Bacik void btrfs_init_fs_info(struct btrfs_fs_info *fs_info)
2663eb60ceacSChris Mason {
2664fc7cbcd4SDavid Sterba INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC);
266501cd3909SDavid Sterba INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC);
26668fd17795SChris Mason INIT_LIST_HEAD(&fs_info->trans_list);
2667facda1e7SChris Mason INIT_LIST_HEAD(&fs_info->dead_roots);
266824bbcf04SYan, Zheng INIT_LIST_HEAD(&fs_info->delayed_iputs);
2669eb73c1b7SMiao Xie INIT_LIST_HEAD(&fs_info->delalloc_roots);
267011833d66SYan Zheng INIT_LIST_HEAD(&fs_info->caching_block_groups);
2671eb73c1b7SMiao Xie spin_lock_init(&fs_info->delalloc_root_lock);
2672a4abeea4SJosef Bacik spin_lock_init(&fs_info->trans_lock);
2673fc7cbcd4SDavid Sterba spin_lock_init(&fs_info->fs_roots_radix_lock);
267424bbcf04SYan, Zheng spin_lock_init(&fs_info->delayed_iput_lock);
26754cb5300bSChris Mason spin_lock_init(&fs_info->defrag_inodes_lock);
2676ceda0864SMiao Xie spin_lock_init(&fs_info->super_lock);
2677f28491e0SJosef Bacik spin_lock_init(&fs_info->buffer_lock);
267847ab2a6cSJosef Bacik spin_lock_init(&fs_info->unused_bgs_lock);
267940ab3be1SNaohiro Aota spin_lock_init(&fs_info->treelog_bg_lock);
2680afba2bc0SNaohiro Aota spin_lock_init(&fs_info->zone_active_bgs_lock);
2681c2707a25SJohannes Thumshirn spin_lock_init(&fs_info->relocation_bg_lock);
2682f29021b2SJan Schmidt rwlock_init(&fs_info->tree_mod_log_lock);
2683abed4aaaSJosef Bacik rwlock_init(&fs_info->global_root_lock);
2684d7c15171SZhao Lei mutex_init(&fs_info->unused_bg_unpin_mutex);
2685f3372065SJohannes Thumshirn mutex_init(&fs_info->reclaim_bgs_lock);
26867585717fSChris Mason mutex_init(&fs_info->reloc_mutex);
2687573bfb72SMiao Xie mutex_init(&fs_info->delalloc_root_mutex);
26880bc09ca1SNaohiro Aota mutex_init(&fs_info->zoned_meta_io_lock);
26895f0addf7SNaohiro Aota mutex_init(&fs_info->zoned_data_reloc_io_lock);
2690de98ced9SMiao Xie seqlock_init(&fs_info->profiles_lock);
269119c00ddcSChris Mason
2692e1489b4fSIoannis Angelakopoulos btrfs_lockdep_init_map(fs_info, btrfs_trans_num_writers);
26935a9ba670SIoannis Angelakopoulos btrfs_lockdep_init_map(fs_info, btrfs_trans_num_extwriters);
26948b53779eSIoannis Angelakopoulos btrfs_lockdep_init_map(fs_info, btrfs_trans_pending_ordered);
26955f4403e1SIoannis Angelakopoulos btrfs_lockdep_init_map(fs_info, btrfs_ordered_extent);
269677d20c68SJosef Bacik btrfs_state_lockdep_init_map(fs_info, btrfs_trans_commit_prep,
269777d20c68SJosef Bacik BTRFS_LOCKDEP_TRANS_COMMIT_PREP);
26983e738c53SIoannis Angelakopoulos btrfs_state_lockdep_init_map(fs_info, btrfs_trans_unblocked,
26993e738c53SIoannis Angelakopoulos BTRFS_LOCKDEP_TRANS_UNBLOCKED);
27003e738c53SIoannis Angelakopoulos btrfs_state_lockdep_init_map(fs_info, btrfs_trans_super_committed,
27013e738c53SIoannis Angelakopoulos BTRFS_LOCKDEP_TRANS_SUPER_COMMITTED);
27023e738c53SIoannis Angelakopoulos btrfs_state_lockdep_init_map(fs_info, btrfs_trans_completed,
27033e738c53SIoannis Angelakopoulos BTRFS_LOCKDEP_TRANS_COMPLETED);
2704e1489b4fSIoannis Angelakopoulos
27050b86a832SChris Mason INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
27066324fbf3SChris Mason INIT_LIST_HEAD(&fs_info->space_info);
2707f29021b2SJan Schmidt INIT_LIST_HEAD(&fs_info->tree_mod_seq_list);
270847ab2a6cSJosef Bacik INIT_LIST_HEAD(&fs_info->unused_bgs);
270918bb8bbfSJohannes Thumshirn INIT_LIST_HEAD(&fs_info->reclaim_bgs);
2710afba2bc0SNaohiro Aota INIT_LIST_HEAD(&fs_info->zone_active_bgs);
2711bd647ce3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
2712bd647ce3SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_roots);
27133fd63727SJosef Bacik INIT_LIST_HEAD(&fs_info->allocated_ebs);
27143fd63727SJosef Bacik spin_lock_init(&fs_info->eb_leak_lock);
2715bd647ce3SJosef Bacik #endif
2716c8bf1b67SDavid Sterba extent_map_tree_init(&fs_info->mapping_tree);
271766d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->global_block_rsv,
271866d8f3ddSMiao Xie BTRFS_BLOCK_RSV_GLOBAL);
271966d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS);
272066d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK);
272166d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY);
272266d8f3ddSMiao Xie btrfs_init_block_rsv(&fs_info->delayed_block_rsv,
272366d8f3ddSMiao Xie BTRFS_BLOCK_RSV_DELOPS);
2724ba2c4d4eSJosef Bacik btrfs_init_block_rsv(&fs_info->delayed_refs_rsv,
2725ba2c4d4eSJosef Bacik BTRFS_BLOCK_RSV_DELREFS);
2726ba2c4d4eSJosef Bacik
2727771ed689SChris Mason atomic_set(&fs_info->async_delalloc_pages, 0);
27284cb5300bSChris Mason atomic_set(&fs_info->defrag_running, 0);
2729034f784dSJosef Bacik atomic_set(&fs_info->nr_delayed_iputs, 0);
2730fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0);
2731abed4aaaSJosef Bacik fs_info->global_root_tree = RB_ROOT;
273295ac567aSFilipe David Borba Manana fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE;
27339ed74f2dSJosef Bacik fs_info->metadata_ratio = 0;
27344cb5300bSChris Mason fs_info->defrag_inodes = RB_ROOT;
2735a5ed45f8SNikolay Borisov atomic64_set(&fs_info->free_chunk_space, 0);
2736f29021b2SJan Schmidt fs_info->tree_mod_log = RB_ROOT;
27378b87dc17SDavid Sterba fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
2738fd708b81SJosef Bacik btrfs_init_ref_verify(fs_info);
2739c8b97818SChris Mason
2740b34b086cSChris Mason fs_info->thread_pool_size = min_t(unsigned long,
2741b34b086cSChris Mason num_online_cpus() + 2, 8);
27420afbaf8cSChris Mason
2743199c2a9cSMiao Xie INIT_LIST_HEAD(&fs_info->ordered_roots);
2744199c2a9cSMiao Xie spin_lock_init(&fs_info->ordered_root_lock);
274569fe2d75SJosef Bacik
2746638aa7edSEric Sandeen btrfs_init_scrub(fs_info);
274721adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
274821adbd5cSStefan Behrens fs_info->check_integrity_print_mask = 0;
274921adbd5cSStefan Behrens #endif
2750779a65a4SEric Sandeen btrfs_init_balance(fs_info);
275157056740SJosef Bacik btrfs_init_async_reclaim_work(fs_info);
2752a2de733cSArne Jansen
275316b0c258SFilipe Manana rwlock_init(&fs_info->block_group_cache_lock);
275408dddb29SFilipe Manana fs_info->block_group_cache_tree = RB_ROOT_CACHED;
27550f9dd46cSJosef Bacik
2756fe119a6eSNikolay Borisov extent_io_tree_init(fs_info, &fs_info->excluded_extents,
275735da5a7eSDavid Sterba IO_TREE_FS_EXCLUDED_EXTENTS);
275839279cc3SChris Mason
27595a3f23d5SChris Mason mutex_init(&fs_info->ordered_operations_mutex);
2760e02119d5SChris Mason mutex_init(&fs_info->tree_log_mutex);
2761925baeddSChris Mason mutex_init(&fs_info->chunk_mutex);
2762a74a4b97SChris Mason mutex_init(&fs_info->transaction_kthread_mutex);
2763a74a4b97SChris Mason mutex_init(&fs_info->cleaner_mutex);
27641bbc621eSChris Mason mutex_init(&fs_info->ro_block_group_mutex);
27659e351cc8SJosef Bacik init_rwsem(&fs_info->commit_root_sem);
2766c71bf099SYan, Zheng init_rwsem(&fs_info->cleanup_work_sem);
276776dda93cSYan, Zheng init_rwsem(&fs_info->subvol_sem);
2768803b2f54SStefan Behrens sema_init(&fs_info->uuid_tree_rescan_sem, 1);
2769fa9c0d79SChris Mason
2770ad618368SEric Sandeen btrfs_init_dev_replace_locks(fs_info);
2771f9e92e40SEric Sandeen btrfs_init_qgroup(fs_info);
2772b0643e59SDennis Zhou btrfs_discard_init(fs_info);
2773416ac51dSArne Jansen
2774fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->meta_alloc_cluster);
2775fa9c0d79SChris Mason btrfs_init_free_cluster(&fs_info->data_alloc_cluster);
2776fa9c0d79SChris Mason
2777e6dcd2dcSChris Mason init_waitqueue_head(&fs_info->transaction_throttle);
2778f9295749SChris Mason init_waitqueue_head(&fs_info->transaction_wait);
2779bb9c12c9SSage Weil init_waitqueue_head(&fs_info->transaction_blocked_wait);
27804854ddd0SChris Mason init_waitqueue_head(&fs_info->async_submit_wait);
2781034f784dSJosef Bacik init_waitqueue_head(&fs_info->delayed_iputs_wait);
27823768f368SChris Mason
2783da17066cSJeff Mahoney /* Usable values until the real ones are cached from the superblock */
2784da17066cSJeff Mahoney fs_info->nodesize = 4096;
2785da17066cSJeff Mahoney fs_info->sectorsize = 4096;
2786ab108d99SDavid Sterba fs_info->sectorsize_bits = ilog2(4096);
2787da17066cSJeff Mahoney fs_info->stripesize = 4096;
2788da17066cSJeff Mahoney
2789f7b12a62SNaohiro Aota fs_info->max_extent_size = BTRFS_MAX_EXTENT_SIZE;
2790f7b12a62SNaohiro Aota
2791eede2bf3SOmar Sandoval spin_lock_init(&fs_info->swapfile_pins_lock);
2792eede2bf3SOmar Sandoval fs_info->swapfile_pins = RB_ROOT;
2793eede2bf3SOmar Sandoval
279418bb8bbfSJohannes Thumshirn fs_info->bg_reclaim_threshold = BTRFS_DEFAULT_RECLAIM_THRESH;
279518bb8bbfSJohannes Thumshirn INIT_WORK(&fs_info->reclaim_bgs_work, btrfs_reclaim_bgs_work);
27968260edbaSJosef Bacik }
27978260edbaSJosef Bacik
init_mount_fs_info(struct btrfs_fs_info * fs_info,struct super_block * sb)27988260edbaSJosef Bacik static int init_mount_fs_info(struct btrfs_fs_info *fs_info, struct super_block *sb)
27998260edbaSJosef Bacik {
28008260edbaSJosef Bacik int ret;
28018260edbaSJosef Bacik
28028260edbaSJosef Bacik fs_info->sb = sb;
28038260edbaSJosef Bacik sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE;
28048260edbaSJosef Bacik sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE);
28059e967495SFilipe Manana
28065deb17e1SJosef Bacik ret = percpu_counter_init(&fs_info->ordered_bytes, 0, GFP_KERNEL);
2807ae18c37aSJosef Bacik if (ret)
2808ae18c37aSJosef Bacik return ret;
2809ae18c37aSJosef Bacik
2810ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL);
2811ae18c37aSJosef Bacik if (ret)
2812c75e8394SJosef Bacik return ret;
2813ae18c37aSJosef Bacik
2814ae18c37aSJosef Bacik fs_info->dirty_metadata_batch = PAGE_SIZE *
2815ae18c37aSJosef Bacik (1 + ilog2(nr_cpu_ids));
2816ae18c37aSJosef Bacik
2817ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL);
2818ae18c37aSJosef Bacik if (ret)
2819c75e8394SJosef Bacik return ret;
2820ae18c37aSJosef Bacik
2821ae18c37aSJosef Bacik ret = percpu_counter_init(&fs_info->dev_replace.bio_counter, 0,
2822ae18c37aSJosef Bacik GFP_KERNEL);
2823ae18c37aSJosef Bacik if (ret)
2824c75e8394SJosef Bacik return ret;
2825ae18c37aSJosef Bacik
2826ae18c37aSJosef Bacik fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root),
2827ae18c37aSJosef Bacik GFP_KERNEL);
2828c75e8394SJosef Bacik if (!fs_info->delayed_root)
2829c75e8394SJosef Bacik return -ENOMEM;
2830ae18c37aSJosef Bacik btrfs_init_delayed_root(fs_info->delayed_root);
2831ae18c37aSJosef Bacik
2832a0a1db70SFilipe Manana if (sb_rdonly(sb))
2833a0a1db70SFilipe Manana set_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state);
2834a0a1db70SFilipe Manana
2835c75e8394SJosef Bacik return btrfs_alloc_stripe_hash_table(fs_info);
2836ae18c37aSJosef Bacik }
2837ae18c37aSJosef Bacik
btrfs_uuid_rescan_kthread(void * data)283897f4dd09SNikolay Borisov static int btrfs_uuid_rescan_kthread(void *data)
283997f4dd09SNikolay Borisov {
28400d031dc4SYu Zhe struct btrfs_fs_info *fs_info = data;
284197f4dd09SNikolay Borisov int ret;
284297f4dd09SNikolay Borisov
284397f4dd09SNikolay Borisov /*
284497f4dd09SNikolay Borisov * 1st step is to iterate through the existing UUID tree and
284597f4dd09SNikolay Borisov * to delete all entries that contain outdated data.
284697f4dd09SNikolay Borisov * 2nd step is to add all missing entries to the UUID tree.
284797f4dd09SNikolay Borisov */
284897f4dd09SNikolay Borisov ret = btrfs_uuid_tree_iterate(fs_info);
284997f4dd09SNikolay Borisov if (ret < 0) {
2850c94bec2cSJosef Bacik if (ret != -EINTR)
2851c94bec2cSJosef Bacik btrfs_warn(fs_info, "iterating uuid_tree failed %d",
2852c94bec2cSJosef Bacik ret);
285397f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem);
285497f4dd09SNikolay Borisov return ret;
285597f4dd09SNikolay Borisov }
285697f4dd09SNikolay Borisov return btrfs_uuid_scan_kthread(data);
285797f4dd09SNikolay Borisov }
285897f4dd09SNikolay Borisov
btrfs_check_uuid_tree(struct btrfs_fs_info * fs_info)285997f4dd09SNikolay Borisov static int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
286097f4dd09SNikolay Borisov {
286197f4dd09SNikolay Borisov struct task_struct *task;
286297f4dd09SNikolay Borisov
286397f4dd09SNikolay Borisov down(&fs_info->uuid_tree_rescan_sem);
286497f4dd09SNikolay Borisov task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
286597f4dd09SNikolay Borisov if (IS_ERR(task)) {
286697f4dd09SNikolay Borisov /* fs_info->update_uuid_tree_gen remains 0 in all error case */
286797f4dd09SNikolay Borisov btrfs_warn(fs_info, "failed to start uuid_rescan task");
286897f4dd09SNikolay Borisov up(&fs_info->uuid_tree_rescan_sem);
286997f4dd09SNikolay Borisov return PTR_ERR(task);
287097f4dd09SNikolay Borisov }
287197f4dd09SNikolay Borisov
287297f4dd09SNikolay Borisov return 0;
287397f4dd09SNikolay Borisov }
287497f4dd09SNikolay Borisov
btrfs_cleanup_fs_roots(struct btrfs_fs_info * fs_info)2875504b1596SFilipe Manana static int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info)
2876504b1596SFilipe Manana {
2877504b1596SFilipe Manana u64 root_objectid = 0;
2878504b1596SFilipe Manana struct btrfs_root *gang[8];
2879504b1596SFilipe Manana int i = 0;
2880504b1596SFilipe Manana int err = 0;
2881504b1596SFilipe Manana unsigned int ret = 0;
2882504b1596SFilipe Manana
2883504b1596SFilipe Manana while (1) {
2884504b1596SFilipe Manana spin_lock(&fs_info->fs_roots_radix_lock);
2885504b1596SFilipe Manana ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
2886504b1596SFilipe Manana (void **)gang, root_objectid,
2887504b1596SFilipe Manana ARRAY_SIZE(gang));
2888504b1596SFilipe Manana if (!ret) {
2889504b1596SFilipe Manana spin_unlock(&fs_info->fs_roots_radix_lock);
2890504b1596SFilipe Manana break;
2891504b1596SFilipe Manana }
2892504b1596SFilipe Manana root_objectid = gang[ret - 1]->root_key.objectid + 1;
2893504b1596SFilipe Manana
2894504b1596SFilipe Manana for (i = 0; i < ret; i++) {
2895504b1596SFilipe Manana /* Avoid to grab roots in dead_roots. */
2896504b1596SFilipe Manana if (btrfs_root_refs(&gang[i]->root_item) == 0) {
2897504b1596SFilipe Manana gang[i] = NULL;
2898504b1596SFilipe Manana continue;
2899504b1596SFilipe Manana }
2900504b1596SFilipe Manana /* Grab all the search result for later use. */
2901504b1596SFilipe Manana gang[i] = btrfs_grab_root(gang[i]);
2902504b1596SFilipe Manana }
2903504b1596SFilipe Manana spin_unlock(&fs_info->fs_roots_radix_lock);
2904504b1596SFilipe Manana
2905504b1596SFilipe Manana for (i = 0; i < ret; i++) {
2906504b1596SFilipe Manana if (!gang[i])
2907504b1596SFilipe Manana continue;
2908504b1596SFilipe Manana root_objectid = gang[i]->root_key.objectid;
2909504b1596SFilipe Manana err = btrfs_orphan_cleanup(gang[i]);
2910504b1596SFilipe Manana if (err)
2911504b1596SFilipe Manana goto out;
2912504b1596SFilipe Manana btrfs_put_root(gang[i]);
2913504b1596SFilipe Manana }
2914504b1596SFilipe Manana root_objectid++;
2915504b1596SFilipe Manana }
2916504b1596SFilipe Manana out:
2917504b1596SFilipe Manana /* Release the uncleaned roots due to error. */
2918504b1596SFilipe Manana for (; i < ret; i++) {
2919504b1596SFilipe Manana if (gang[i])
2920504b1596SFilipe Manana btrfs_put_root(gang[i]);
2921504b1596SFilipe Manana }
2922504b1596SFilipe Manana return err;
2923504b1596SFilipe Manana }
2924504b1596SFilipe Manana
292544c0ca21SBoris Burkov /*
29268cd29088SBoris Burkov * Some options only have meaning at mount time and shouldn't persist across
29278cd29088SBoris Burkov * remounts, or be displayed. Clear these at the end of mount and remount
29288cd29088SBoris Burkov * code paths.
29298cd29088SBoris Burkov */
btrfs_clear_oneshot_options(struct btrfs_fs_info * fs_info)29308cd29088SBoris Burkov void btrfs_clear_oneshot_options(struct btrfs_fs_info *fs_info)
29318cd29088SBoris Burkov {
29328cd29088SBoris Burkov btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT);
29338b228324SBoris Burkov btrfs_clear_opt(fs_info->mount_opt, CLEAR_CACHE);
29348cd29088SBoris Burkov }
29358cd29088SBoris Burkov
29368cd29088SBoris Burkov /*
293744c0ca21SBoris Burkov * Mounting logic specific to read-write file systems. Shared by open_ctree
293844c0ca21SBoris Burkov * and btrfs_remount when remounting from read-only to read-write.
293944c0ca21SBoris Burkov */
btrfs_start_pre_rw_mount(struct btrfs_fs_info * fs_info)294044c0ca21SBoris Burkov int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info)
294144c0ca21SBoris Burkov {
294244c0ca21SBoris Burkov int ret;
294394846229SBoris Burkov const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE);
29441d6a4fc8SQu Wenruo bool rebuild_free_space_tree = false;
29458b228324SBoris Burkov
29468b228324SBoris Burkov if (btrfs_test_opt(fs_info, CLEAR_CACHE) &&
29478b228324SBoris Burkov btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
29481d6a4fc8SQu Wenruo rebuild_free_space_tree = true;
29498b228324SBoris Burkov } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
29508b228324SBoris Burkov !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) {
29518b228324SBoris Burkov btrfs_warn(fs_info, "free space tree is invalid");
29521d6a4fc8SQu Wenruo rebuild_free_space_tree = true;
29538b228324SBoris Burkov }
29548b228324SBoris Burkov
29551d6a4fc8SQu Wenruo if (rebuild_free_space_tree) {
29561d6a4fc8SQu Wenruo btrfs_info(fs_info, "rebuilding free space tree");
29571d6a4fc8SQu Wenruo ret = btrfs_rebuild_free_space_tree(fs_info);
29588b228324SBoris Burkov if (ret) {
29598b228324SBoris Burkov btrfs_warn(fs_info,
29601d6a4fc8SQu Wenruo "failed to rebuild free space tree: %d", ret);
29611d6a4fc8SQu Wenruo goto out;
29621d6a4fc8SQu Wenruo }
29631d6a4fc8SQu Wenruo }
29641d6a4fc8SQu Wenruo
29651d6a4fc8SQu Wenruo if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
29661d6a4fc8SQu Wenruo !btrfs_test_opt(fs_info, FREE_SPACE_TREE)) {
29671d6a4fc8SQu Wenruo btrfs_info(fs_info, "disabling free space tree");
29681d6a4fc8SQu Wenruo ret = btrfs_delete_free_space_tree(fs_info);
29691d6a4fc8SQu Wenruo if (ret) {
29701d6a4fc8SQu Wenruo btrfs_warn(fs_info,
29711d6a4fc8SQu Wenruo "failed to disable free space tree: %d", ret);
29728b228324SBoris Burkov goto out;
29738b228324SBoris Burkov }
29748b228324SBoris Burkov }
297544c0ca21SBoris Burkov
29768d488a8cSFilipe Manana /*
29778d488a8cSFilipe Manana * btrfs_find_orphan_roots() is responsible for finding all the dead
29788d488a8cSFilipe Manana * roots (with 0 refs), flag them with BTRFS_ROOT_DEAD_TREE and load
2979fc7cbcd4SDavid Sterba * them into the fs_info->fs_roots_radix tree. This must be done before
29808d488a8cSFilipe Manana * calling btrfs_orphan_cleanup() on the tree root. If we don't do it
29818d488a8cSFilipe Manana * first, then btrfs_orphan_cleanup() will delete a dead root's orphan
29828d488a8cSFilipe Manana * item before the root's tree is deleted - this means that if we unmount
29838d488a8cSFilipe Manana * or crash before the deletion completes, on the next mount we will not
29848d488a8cSFilipe Manana * delete what remains of the tree because the orphan item does not
29858d488a8cSFilipe Manana * exists anymore, which is what tells us we have a pending deletion.
29868d488a8cSFilipe Manana */
29878d488a8cSFilipe Manana ret = btrfs_find_orphan_roots(fs_info);
29888d488a8cSFilipe Manana if (ret)
29898d488a8cSFilipe Manana goto out;
29908d488a8cSFilipe Manana
299144c0ca21SBoris Burkov ret = btrfs_cleanup_fs_roots(fs_info);
299244c0ca21SBoris Burkov if (ret)
299344c0ca21SBoris Burkov goto out;
299444c0ca21SBoris Burkov
29958f1c21d7SBoris Burkov down_read(&fs_info->cleanup_work_sem);
29968f1c21d7SBoris Burkov if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) ||
29978f1c21d7SBoris Burkov (ret = btrfs_orphan_cleanup(fs_info->tree_root))) {
29988f1c21d7SBoris Burkov up_read(&fs_info->cleanup_work_sem);
29998f1c21d7SBoris Burkov goto out;
30008f1c21d7SBoris Burkov }
30018f1c21d7SBoris Burkov up_read(&fs_info->cleanup_work_sem);
30028f1c21d7SBoris Burkov
300344c0ca21SBoris Burkov mutex_lock(&fs_info->cleaner_mutex);
30047eefae6bSJosef Bacik ret = btrfs_recover_relocation(fs_info);
300544c0ca21SBoris Burkov mutex_unlock(&fs_info->cleaner_mutex);
300644c0ca21SBoris Burkov if (ret < 0) {
300744c0ca21SBoris Burkov btrfs_warn(fs_info, "failed to recover relocation: %d", ret);
300844c0ca21SBoris Burkov goto out;
300944c0ca21SBoris Burkov }
301044c0ca21SBoris Burkov
30115011139aSBoris Burkov if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) &&
30125011139aSBoris Burkov !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
30135011139aSBoris Burkov btrfs_info(fs_info, "creating free space tree");
30145011139aSBoris Burkov ret = btrfs_create_free_space_tree(fs_info);
30155011139aSBoris Burkov if (ret) {
30165011139aSBoris Burkov btrfs_warn(fs_info,
30175011139aSBoris Burkov "failed to create free space tree: %d", ret);
30185011139aSBoris Burkov goto out;
30195011139aSBoris Burkov }
30205011139aSBoris Burkov }
30215011139aSBoris Burkov
302294846229SBoris Burkov if (cache_opt != btrfs_free_space_cache_v1_active(fs_info)) {
302394846229SBoris Burkov ret = btrfs_set_free_space_cache_v1_active(fs_info, cache_opt);
302494846229SBoris Burkov if (ret)
302594846229SBoris Burkov goto out;
302694846229SBoris Burkov }
302794846229SBoris Burkov
302844c0ca21SBoris Burkov ret = btrfs_resume_balance_async(fs_info);
302944c0ca21SBoris Burkov if (ret)
303044c0ca21SBoris Burkov goto out;
303144c0ca21SBoris Burkov
303244c0ca21SBoris Burkov ret = btrfs_resume_dev_replace_async(fs_info);
303344c0ca21SBoris Burkov if (ret) {
303444c0ca21SBoris Burkov btrfs_warn(fs_info, "failed to resume dev_replace");
303544c0ca21SBoris Burkov goto out;
303644c0ca21SBoris Burkov }
303744c0ca21SBoris Burkov
303844c0ca21SBoris Burkov btrfs_qgroup_rescan_resume(fs_info);
303944c0ca21SBoris Burkov
304044c0ca21SBoris Burkov if (!fs_info->uuid_root) {
304144c0ca21SBoris Burkov btrfs_info(fs_info, "creating UUID tree");
304244c0ca21SBoris Burkov ret = btrfs_create_uuid_tree(fs_info);
304344c0ca21SBoris Burkov if (ret) {
304444c0ca21SBoris Burkov btrfs_warn(fs_info,
304544c0ca21SBoris Burkov "failed to create the UUID tree %d", ret);
304644c0ca21SBoris Burkov goto out;
304744c0ca21SBoris Burkov }
304844c0ca21SBoris Burkov }
304944c0ca21SBoris Burkov
305044c0ca21SBoris Burkov out:
305144c0ca21SBoris Burkov return ret;
305244c0ca21SBoris Burkov }
305344c0ca21SBoris Burkov
3054d7f67ac9SQu Wenruo /*
3055d7f67ac9SQu Wenruo * Do various sanity and dependency checks of different features.
3056d7f67ac9SQu Wenruo *
30572ba48b20SQu Wenruo * @is_rw_mount: If the mount is read-write.
30582ba48b20SQu Wenruo *
3059d7f67ac9SQu Wenruo * This is the place for less strict checks (like for subpage or artificial
3060d7f67ac9SQu Wenruo * feature dependencies).
3061d7f67ac9SQu Wenruo *
3062d7f67ac9SQu Wenruo * For strict checks or possible corruption detection, see
3063d7f67ac9SQu Wenruo * btrfs_validate_super().
3064d7f67ac9SQu Wenruo *
3065d7f67ac9SQu Wenruo * This should be called after btrfs_parse_options(), as some mount options
3066d7f67ac9SQu Wenruo * (space cache related) can modify on-disk format like free space tree and
3067d7f67ac9SQu Wenruo * screw up certain feature dependencies.
3068d7f67ac9SQu Wenruo */
btrfs_check_features(struct btrfs_fs_info * fs_info,bool is_rw_mount)30692ba48b20SQu Wenruo int btrfs_check_features(struct btrfs_fs_info *fs_info, bool is_rw_mount)
3070d7f67ac9SQu Wenruo {
3071d7f67ac9SQu Wenruo struct btrfs_super_block *disk_super = fs_info->super_copy;
3072d7f67ac9SQu Wenruo u64 incompat = btrfs_super_incompat_flags(disk_super);
3073d7f67ac9SQu Wenruo const u64 compat_ro = btrfs_super_compat_ro_flags(disk_super);
3074d7f67ac9SQu Wenruo const u64 compat_ro_unsupp = (compat_ro & ~BTRFS_FEATURE_COMPAT_RO_SUPP);
3075d7f67ac9SQu Wenruo
3076d7f67ac9SQu Wenruo if (incompat & ~BTRFS_FEATURE_INCOMPAT_SUPP) {
3077d7f67ac9SQu Wenruo btrfs_err(fs_info,
3078d7f67ac9SQu Wenruo "cannot mount because of unknown incompat features (0x%llx)",
3079d7f67ac9SQu Wenruo incompat);
3080d7f67ac9SQu Wenruo return -EINVAL;
3081d7f67ac9SQu Wenruo }
3082d7f67ac9SQu Wenruo
3083d7f67ac9SQu Wenruo /* Runtime limitation for mixed block groups. */
3084d7f67ac9SQu Wenruo if ((incompat & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) &&
3085d7f67ac9SQu Wenruo (fs_info->sectorsize != fs_info->nodesize)) {
3086d7f67ac9SQu Wenruo btrfs_err(fs_info,
3087d7f67ac9SQu Wenruo "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups",
3088d7f67ac9SQu Wenruo fs_info->nodesize, fs_info->sectorsize);
3089d7f67ac9SQu Wenruo return -EINVAL;
3090d7f67ac9SQu Wenruo }
3091d7f67ac9SQu Wenruo
3092d7f67ac9SQu Wenruo /* Mixed backref is an always-enabled feature. */
3093d7f67ac9SQu Wenruo incompat |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
3094d7f67ac9SQu Wenruo
3095d7f67ac9SQu Wenruo /* Set compression related flags just in case. */
3096d7f67ac9SQu Wenruo if (fs_info->compress_type == BTRFS_COMPRESS_LZO)
3097d7f67ac9SQu Wenruo incompat |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
3098d7f67ac9SQu Wenruo else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD)
3099d7f67ac9SQu Wenruo incompat |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD;
3100d7f67ac9SQu Wenruo
3101d7f67ac9SQu Wenruo /*
3102d7f67ac9SQu Wenruo * An ancient flag, which should really be marked deprecated.
3103d7f67ac9SQu Wenruo * Such runtime limitation doesn't really need a incompat flag.
3104d7f67ac9SQu Wenruo */
3105d7f67ac9SQu Wenruo if (btrfs_super_nodesize(disk_super) > PAGE_SIZE)
3106d7f67ac9SQu Wenruo incompat |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA;
3107d7f67ac9SQu Wenruo
31082ba48b20SQu Wenruo if (compat_ro_unsupp && is_rw_mount) {
3109d7f67ac9SQu Wenruo btrfs_err(fs_info,
3110d7f67ac9SQu Wenruo "cannot mount read-write because of unknown compat_ro features (0x%llx)",
3111d7f67ac9SQu Wenruo compat_ro);
3112d7f67ac9SQu Wenruo return -EINVAL;
3113d7f67ac9SQu Wenruo }
3114d7f67ac9SQu Wenruo
3115d7f67ac9SQu Wenruo /*
3116d7f67ac9SQu Wenruo * We have unsupported RO compat features, although RO mounted, we
3117d7f67ac9SQu Wenruo * should not cause any metadata writes, including log replay.
3118d7f67ac9SQu Wenruo * Or we could screw up whatever the new feature requires.
3119d7f67ac9SQu Wenruo */
3120d7f67ac9SQu Wenruo if (compat_ro_unsupp && btrfs_super_log_root(disk_super) &&
3121d7f67ac9SQu Wenruo !btrfs_test_opt(fs_info, NOLOGREPLAY)) {
3122d7f67ac9SQu Wenruo btrfs_err(fs_info,
3123d7f67ac9SQu Wenruo "cannot replay dirty log with unsupported compat_ro features (0x%llx), try rescue=nologreplay",
3124d7f67ac9SQu Wenruo compat_ro);
3125d7f67ac9SQu Wenruo return -EINVAL;
3126d7f67ac9SQu Wenruo }
3127d7f67ac9SQu Wenruo
3128d7f67ac9SQu Wenruo /*
3129d7f67ac9SQu Wenruo * Artificial limitations for block group tree, to force
3130d7f67ac9SQu Wenruo * block-group-tree to rely on no-holes and free-space-tree.
3131d7f67ac9SQu Wenruo */
3132d7f67ac9SQu Wenruo if (btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE) &&
3133d7f67ac9SQu Wenruo (!btrfs_fs_incompat(fs_info, NO_HOLES) ||
3134d7f67ac9SQu Wenruo !btrfs_test_opt(fs_info, FREE_SPACE_TREE))) {
3135d7f67ac9SQu Wenruo btrfs_err(fs_info,
3136d7f67ac9SQu Wenruo "block-group-tree feature requires no-holes and free-space-tree features");
3137d7f67ac9SQu Wenruo return -EINVAL;
3138d7f67ac9SQu Wenruo }
3139d7f67ac9SQu Wenruo
3140d7f67ac9SQu Wenruo /*
3141d7f67ac9SQu Wenruo * Subpage runtime limitation on v1 cache.
3142d7f67ac9SQu Wenruo *
3143d7f67ac9SQu Wenruo * V1 space cache still has some hard codeed PAGE_SIZE usage, while
3144d7f67ac9SQu Wenruo * we're already defaulting to v2 cache, no need to bother v1 as it's
3145d7f67ac9SQu Wenruo * going to be deprecated anyway.
3146d7f67ac9SQu Wenruo */
3147d7f67ac9SQu Wenruo if (fs_info->sectorsize < PAGE_SIZE && btrfs_test_opt(fs_info, SPACE_CACHE)) {
3148d7f67ac9SQu Wenruo btrfs_warn(fs_info,
3149d7f67ac9SQu Wenruo "v1 space cache is not supported for page size %lu with sectorsize %u",
3150d7f67ac9SQu Wenruo PAGE_SIZE, fs_info->sectorsize);
3151d7f67ac9SQu Wenruo return -EINVAL;
3152d7f67ac9SQu Wenruo }
3153d7f67ac9SQu Wenruo
3154d7f67ac9SQu Wenruo /* This can be called by remount, we need to protect the super block. */
3155d7f67ac9SQu Wenruo spin_lock(&fs_info->super_lock);
3156d7f67ac9SQu Wenruo btrfs_set_super_incompat_flags(disk_super, incompat);
3157d7f67ac9SQu Wenruo spin_unlock(&fs_info->super_lock);
3158d7f67ac9SQu Wenruo
3159d7f67ac9SQu Wenruo return 0;
3160d7f67ac9SQu Wenruo }
3161d7f67ac9SQu Wenruo
open_ctree(struct super_block * sb,struct btrfs_fs_devices * fs_devices,char * options)3162ae18c37aSJosef Bacik int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_devices,
3163ae18c37aSJosef Bacik char *options)
3164ae18c37aSJosef Bacik {
3165ae18c37aSJosef Bacik u32 sectorsize;
3166ae18c37aSJosef Bacik u32 nodesize;
3167ae18c37aSJosef Bacik u32 stripesize;
3168ae18c37aSJosef Bacik u64 generation;
3169ae18c37aSJosef Bacik u64 features;
3170ae18c37aSJosef Bacik u16 csum_type;
3171ae18c37aSJosef Bacik struct btrfs_super_block *disk_super;
3172ae18c37aSJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(sb);
3173ae18c37aSJosef Bacik struct btrfs_root *tree_root;
3174ae18c37aSJosef Bacik struct btrfs_root *chunk_root;
3175ae18c37aSJosef Bacik int ret;
3176ae18c37aSJosef Bacik int level;
3177ae18c37aSJosef Bacik
31788260edbaSJosef Bacik ret = init_mount_fs_info(fs_info, sb);
31794871c33bSQu Wenruo if (ret)
3180ae18c37aSJosef Bacik goto fail;
318153b381b3SDavid Woodhouse
3182ae18c37aSJosef Bacik /* These need to be init'ed before we start creating inodes and such. */
3183ae18c37aSJosef Bacik tree_root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID,
3184ae18c37aSJosef Bacik GFP_KERNEL);
3185ae18c37aSJosef Bacik fs_info->tree_root = tree_root;
3186ae18c37aSJosef Bacik chunk_root = btrfs_alloc_root(fs_info, BTRFS_CHUNK_TREE_OBJECTID,
3187ae18c37aSJosef Bacik GFP_KERNEL);
3188ae18c37aSJosef Bacik fs_info->chunk_root = chunk_root;
3189ae18c37aSJosef Bacik if (!tree_root || !chunk_root) {
31904871c33bSQu Wenruo ret = -ENOMEM;
3191c75e8394SJosef Bacik goto fail;
3192ae18c37aSJosef Bacik }
3193ae18c37aSJosef Bacik
3194dcb2137cSChristoph Hellwig ret = btrfs_init_btree_inode(sb);
31954871c33bSQu Wenruo if (ret)
3196c75e8394SJosef Bacik goto fail;
3197ae18c37aSJosef Bacik
3198d24fa5c1SAnand Jain invalidate_bdev(fs_devices->latest_dev->bdev);
31991104a885SDavid Sterba
32001104a885SDavid Sterba /*
32011104a885SDavid Sterba * Read super block and check the signature bytes only
32021104a885SDavid Sterba */
3203d24fa5c1SAnand Jain disk_super = btrfs_read_dev_super(fs_devices->latest_dev->bdev);
32048f32380dSJohannes Thumshirn if (IS_ERR(disk_super)) {
32054871c33bSQu Wenruo ret = PTR_ERR(disk_super);
320616cdcec7SMiao Xie goto fail_alloc;
320720b45077SDave Young }
320839279cc3SChris Mason
32099f7894e2SQu Wenruo btrfs_info(fs_info, "first mount of filesystem %pU", disk_super->fsid);
32101104a885SDavid Sterba /*
3211260db43cSRandy Dunlap * Verify the type first, if that or the checksum value are
32128dc3f22cSJohannes Thumshirn * corrupted, we'll find out
32138dc3f22cSJohannes Thumshirn */
32148f32380dSJohannes Thumshirn csum_type = btrfs_super_csum_type(disk_super);
321551bce6c9SJohannes Thumshirn if (!btrfs_supported_super_csum(csum_type)) {
32168dc3f22cSJohannes Thumshirn btrfs_err(fs_info, "unsupported checksum algorithm: %u",
321751bce6c9SJohannes Thumshirn csum_type);
32184871c33bSQu Wenruo ret = -EINVAL;
32198f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super);
32208dc3f22cSJohannes Thumshirn goto fail_alloc;
32218dc3f22cSJohannes Thumshirn }
32228dc3f22cSJohannes Thumshirn
322383c68bbcSSu Yue fs_info->csum_size = btrfs_super_csum_size(disk_super);
322483c68bbcSSu Yue
32256d97c6e3SJohannes Thumshirn ret = btrfs_init_csum_hash(fs_info, csum_type);
32266d97c6e3SJohannes Thumshirn if (ret) {
32278f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super);
32286d97c6e3SJohannes Thumshirn goto fail_alloc;
32296d97c6e3SJohannes Thumshirn }
32306d97c6e3SJohannes Thumshirn
32318dc3f22cSJohannes Thumshirn /*
32321104a885SDavid Sterba * We want to check superblock checksum, the type is stored inside.
32331104a885SDavid Sterba * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k).
32341104a885SDavid Sterba */
32353d17adeaSQu Wenruo if (btrfs_check_super_csum(fs_info, disk_super)) {
323605135f59SDavid Sterba btrfs_err(fs_info, "superblock checksum mismatch");
32374871c33bSQu Wenruo ret = -EINVAL;
32388f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super);
3239141386e1SJosef Bacik goto fail_alloc;
32401104a885SDavid Sterba }
32411104a885SDavid Sterba
32421104a885SDavid Sterba /*
32431104a885SDavid Sterba * super_copy is zeroed at allocation time and we never touch the
32441104a885SDavid Sterba * following bytes up to INFO_SIZE, the checksum is calculated from
32451104a885SDavid Sterba * the whole block of INFO_SIZE
32461104a885SDavid Sterba */
32478f32380dSJohannes Thumshirn memcpy(fs_info->super_copy, disk_super, sizeof(*fs_info->super_copy));
32488f32380dSJohannes Thumshirn btrfs_release_disk_super(disk_super);
32495f39d397SChris Mason
3250fbc6feaeSNikolay Borisov disk_super = fs_info->super_copy;
3251fbc6feaeSNikolay Borisov
32520b86a832SChris Mason
3253fbc6feaeSNikolay Borisov features = btrfs_super_flags(disk_super);
3254fbc6feaeSNikolay Borisov if (features & BTRFS_SUPER_FLAG_CHANGING_FSID_V2) {
3255fbc6feaeSNikolay Borisov features &= ~BTRFS_SUPER_FLAG_CHANGING_FSID_V2;
3256fbc6feaeSNikolay Borisov btrfs_set_super_flags(disk_super, features);
3257fbc6feaeSNikolay Borisov btrfs_info(fs_info,
3258fbc6feaeSNikolay Borisov "found metadata UUID change in progress flag, clearing");
3259fbc6feaeSNikolay Borisov }
3260fbc6feaeSNikolay Borisov
3261fbc6feaeSNikolay Borisov memcpy(fs_info->super_for_commit, fs_info->super_copy,
3262fbc6feaeSNikolay Borisov sizeof(*fs_info->super_for_commit));
3263de37aa51SNikolay Borisov
3264069ec957SQu Wenruo ret = btrfs_validate_mount_super(fs_info);
32651104a885SDavid Sterba if (ret) {
326605135f59SDavid Sterba btrfs_err(fs_info, "superblock contains fatal errors");
32674871c33bSQu Wenruo ret = -EINVAL;
3268141386e1SJosef Bacik goto fail_alloc;
32691104a885SDavid Sterba }
32701104a885SDavid Sterba
32714871c33bSQu Wenruo if (!btrfs_super_root(disk_super)) {
32724871c33bSQu Wenruo btrfs_err(fs_info, "invalid superblock tree root bytenr");
32734871c33bSQu Wenruo ret = -EINVAL;
3274141386e1SJosef Bacik goto fail_alloc;
32754871c33bSQu Wenruo }
32760f7d52f4SChris Mason
3277acce952bSliubo /* check FS state, whether FS is broken. */
327887533c47SMiao Xie if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR)
3279ae3364e5SFilipe Manana WRITE_ONCE(fs_info->fs_error, -EUCLEAN);
3280acce952bSliubo
328175e7cb7fSLiu Bo /*
328275e7cb7fSLiu Bo * In the long term, we'll store the compression type in the super
328375e7cb7fSLiu Bo * block, and it'll be used for per file compression control.
328475e7cb7fSLiu Bo */
328575e7cb7fSLiu Bo fs_info->compress_type = BTRFS_COMPRESS_ZLIB;
328675e7cb7fSLiu Bo
32876f93e834SAnand Jain
32886f93e834SAnand Jain /* Set up fs_info before parsing mount options */
32896f93e834SAnand Jain nodesize = btrfs_super_nodesize(disk_super);
32906f93e834SAnand Jain sectorsize = btrfs_super_sectorsize(disk_super);
32916f93e834SAnand Jain stripesize = sectorsize;
32926f93e834SAnand Jain fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids));
32936f93e834SAnand Jain fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids));
32946f93e834SAnand Jain
32956f93e834SAnand Jain fs_info->nodesize = nodesize;
32966f93e834SAnand Jain fs_info->sectorsize = sectorsize;
32976f93e834SAnand Jain fs_info->sectorsize_bits = ilog2(sectorsize);
32986f93e834SAnand Jain fs_info->csums_per_leaf = BTRFS_MAX_ITEM_SIZE(fs_info) / fs_info->csum_size;
32996f93e834SAnand Jain fs_info->stripesize = stripesize;
33006f93e834SAnand Jain
33012ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, options, sb->s_flags);
33024871c33bSQu Wenruo if (ret)
3303141386e1SJosef Bacik goto fail_alloc;
3304dfe25020SChris Mason
33052ba48b20SQu Wenruo ret = btrfs_check_features(fs_info, !sb_rdonly(sb));
33064871c33bSQu Wenruo if (ret < 0)
3307141386e1SJosef Bacik goto fail_alloc;
3308f2b636e8SJosef Bacik
33098481dd80SQu Wenruo if (sectorsize < PAGE_SIZE) {
33108481dd80SQu Wenruo struct btrfs_subpage_info *subpage_info;
33118481dd80SQu Wenruo
33129f73f1aeSQu Wenruo /*
33139f73f1aeSQu Wenruo * V1 space cache has some hardcoded PAGE_SIZE usage, and is
33149f73f1aeSQu Wenruo * going to be deprecated.
33159f73f1aeSQu Wenruo *
33169f73f1aeSQu Wenruo * Force to use v2 cache for subpage case.
33179f73f1aeSQu Wenruo */
33189f73f1aeSQu Wenruo btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
33199f73f1aeSQu Wenruo btrfs_set_and_info(fs_info, FREE_SPACE_TREE,
33209f73f1aeSQu Wenruo "forcing free space tree for sector size %u with page size %lu",
33219f73f1aeSQu Wenruo sectorsize, PAGE_SIZE);
33229f73f1aeSQu Wenruo
332395ea0486SQu Wenruo btrfs_warn(fs_info,
332495ea0486SQu Wenruo "read-write for sector size %u with page size %lu is experimental",
33250bb3eb3eSQu Wenruo sectorsize, PAGE_SIZE);
33268481dd80SQu Wenruo subpage_info = kzalloc(sizeof(*subpage_info), GFP_KERNEL);
33274871c33bSQu Wenruo if (!subpage_info) {
33284871c33bSQu Wenruo ret = -ENOMEM;
33298481dd80SQu Wenruo goto fail_alloc;
33304871c33bSQu Wenruo }
33318481dd80SQu Wenruo btrfs_init_subpage_info(subpage_info, sectorsize);
33328481dd80SQu Wenruo fs_info->subpage_info = subpage_info;
3333c8050b3bSQu Wenruo }
33340bb3eb3eSQu Wenruo
3335d21deec5SSu Yue ret = btrfs_init_workqueues(fs_info);
33364871c33bSQu Wenruo if (ret)
33370dc3b84aSJosef Bacik goto fail_sb_buffer;
33384543df7eSChris Mason
33399e11ceeeSJan Kara sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super);
33409e11ceeeSJan Kara sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE);
33414575c9ccSChris Mason
3342a061fc8dSChris Mason sb->s_blocksize = sectorsize;
3343a061fc8dSChris Mason sb->s_blocksize_bits = blksize_bits(sectorsize);
3344de37aa51SNikolay Borisov memcpy(&sb->s_uuid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE);
3345db94535dSChris Mason
3346925baeddSChris Mason mutex_lock(&fs_info->chunk_mutex);
33476bccf3abSJeff Mahoney ret = btrfs_read_sys_array(fs_info);
3348925baeddSChris Mason mutex_unlock(&fs_info->chunk_mutex);
334984eed90fSChris Mason if (ret) {
335005135f59SDavid Sterba btrfs_err(fs_info, "failed to read the system array: %d", ret);
33515d4f98a2SYan Zheng goto fail_sb_buffer;
335284eed90fSChris Mason }
33530b86a832SChris Mason
335484234f3aSYan Zheng generation = btrfs_super_chunk_root_generation(disk_super);
3355581c1760SQu Wenruo level = btrfs_super_chunk_root_level(disk_super);
3356bd676446SJosef Bacik ret = load_super_root(chunk_root, btrfs_super_chunk_root(disk_super),
3357bd676446SJosef Bacik generation, level);
3358bd676446SJosef Bacik if (ret) {
335905135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk root");
3360af31f5e5SChris Mason goto fail_tree_roots;
336183121942SDavid Woodhouse }
33620b86a832SChris Mason
3363e17cade2SChris Mason read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
3364c4ac7541SDavid Sterba offsetof(struct btrfs_header, chunk_tree_uuid),
3365c4ac7541SDavid Sterba BTRFS_UUID_SIZE);
3366e17cade2SChris Mason
33675b4aacefSJeff Mahoney ret = btrfs_read_chunk_tree(fs_info);
33682b82032cSYan Zheng if (ret) {
336905135f59SDavid Sterba btrfs_err(fs_info, "failed to read chunk tree: %d", ret);
3370af31f5e5SChris Mason goto fail_tree_roots;
33712b82032cSYan Zheng }
33720b86a832SChris Mason
33738dabb742SStefan Behrens /*
3374bacce86aSAnand Jain * At this point we know all the devices that make this filesystem,
3375bacce86aSAnand Jain * including the seed devices but we don't know yet if the replace
3376bacce86aSAnand Jain * target is required. So free devices that are not part of this
33771a9fd417SDavid Sterba * filesystem but skip the replace target device which is checked
3378bacce86aSAnand Jain * below in btrfs_init_dev_replace().
33798dabb742SStefan Behrens */
3380bacce86aSAnand Jain btrfs_free_extra_devids(fs_devices);
3381d24fa5c1SAnand Jain if (!fs_devices->latest_dev->bdev) {
338205135f59SDavid Sterba btrfs_err(fs_info, "failed to read devices");
33834871c33bSQu Wenruo ret = -EIO;
3384a6b0d5c8SChris Mason goto fail_tree_roots;
3385a6b0d5c8SChris Mason }
3386a6b0d5c8SChris Mason
3387b8522a1eSNikolay Borisov ret = init_tree_roots(fs_info);
33884bbcaa64SEric Sandeen if (ret)
3389b8522a1eSNikolay Borisov goto fail_tree_roots;
33908929ecfaSYan, Zheng
339175ec1db8SJosef Bacik /*
339273651042SNaohiro Aota * Get zone type information of zoned block devices. This will also
339373651042SNaohiro Aota * handle emulation of a zoned filesystem if a regular device has the
339473651042SNaohiro Aota * zoned incompat feature flag set.
339573651042SNaohiro Aota */
339673651042SNaohiro Aota ret = btrfs_get_dev_zone_info_all_devices(fs_info);
339773651042SNaohiro Aota if (ret) {
339873651042SNaohiro Aota btrfs_err(fs_info,
33994871c33bSQu Wenruo "zoned: failed to read device zone info: %d", ret);
340073651042SNaohiro Aota goto fail_block_groups;
340173651042SNaohiro Aota }
340273651042SNaohiro Aota
340373651042SNaohiro Aota /*
340475ec1db8SJosef Bacik * If we have a uuid root and we're not being told to rescan we need to
340575ec1db8SJosef Bacik * check the generation here so we can set the
340675ec1db8SJosef Bacik * BTRFS_FS_UPDATE_UUID_TREE_GEN bit. Otherwise we could commit the
340775ec1db8SJosef Bacik * transaction during a balance or the log replay without updating the
340875ec1db8SJosef Bacik * uuid generation, and then if we crash we would rescan the uuid tree,
340975ec1db8SJosef Bacik * even though it was perfectly fine.
341075ec1db8SJosef Bacik */
341175ec1db8SJosef Bacik if (fs_info->uuid_root && !btrfs_test_opt(fs_info, RESCAN_UUID_TREE) &&
341275ec1db8SJosef Bacik fs_info->generation == btrfs_super_uuid_tree_generation(disk_super))
341375ec1db8SJosef Bacik set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
341475ec1db8SJosef Bacik
3415cf90d884SQu Wenruo ret = btrfs_verify_dev_extents(fs_info);
3416cf90d884SQu Wenruo if (ret) {
3417cf90d884SQu Wenruo btrfs_err(fs_info,
3418cf90d884SQu Wenruo "failed to verify dev extents against chunks: %d",
3419cf90d884SQu Wenruo ret);
3420cf90d884SQu Wenruo goto fail_block_groups;
3421cf90d884SQu Wenruo }
342268310a5eSIlya Dryomov ret = btrfs_recover_balance(fs_info);
342368310a5eSIlya Dryomov if (ret) {
342405135f59SDavid Sterba btrfs_err(fs_info, "failed to recover balance: %d", ret);
342568310a5eSIlya Dryomov goto fail_block_groups;
342668310a5eSIlya Dryomov }
342768310a5eSIlya Dryomov
3428733f4fbbSStefan Behrens ret = btrfs_init_dev_stats(fs_info);
3429733f4fbbSStefan Behrens if (ret) {
343005135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_stats: %d", ret);
3431733f4fbbSStefan Behrens goto fail_block_groups;
3432733f4fbbSStefan Behrens }
3433733f4fbbSStefan Behrens
34348dabb742SStefan Behrens ret = btrfs_init_dev_replace(fs_info);
34358dabb742SStefan Behrens if (ret) {
343605135f59SDavid Sterba btrfs_err(fs_info, "failed to init dev_replace: %d", ret);
34378dabb742SStefan Behrens goto fail_block_groups;
34388dabb742SStefan Behrens }
34398dabb742SStefan Behrens
3440b70f5097SNaohiro Aota ret = btrfs_check_zoned_mode(fs_info);
3441b70f5097SNaohiro Aota if (ret) {
3442b70f5097SNaohiro Aota btrfs_err(fs_info, "failed to initialize zoned mode: %d",
3443b70f5097SNaohiro Aota ret);
3444b70f5097SNaohiro Aota goto fail_block_groups;
3445b70f5097SNaohiro Aota }
3446b70f5097SNaohiro Aota
3447c6761a9eSAnand Jain ret = btrfs_sysfs_add_fsid(fs_devices);
3448b7c35e81SAnand Jain if (ret) {
344905135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs fsid interface: %d",
345005135f59SDavid Sterba ret);
3451b7c35e81SAnand Jain goto fail_block_groups;
3452b7c35e81SAnand Jain }
3453b7c35e81SAnand Jain
345496f3136eSAnand Jain ret = btrfs_sysfs_add_mounted(fs_info);
34555ac1d209SJeff Mahoney if (ret) {
345605135f59SDavid Sterba btrfs_err(fs_info, "failed to init sysfs interface: %d", ret);
3457b7c35e81SAnand Jain goto fail_fsdev_sysfs;
34585ac1d209SJeff Mahoney }
34595ac1d209SJeff Mahoney
3460c59021f8Sliubo ret = btrfs_init_space_info(fs_info);
3461c59021f8Sliubo if (ret) {
346205135f59SDavid Sterba btrfs_err(fs_info, "failed to initialize space info: %d", ret);
34632365dd3cSAnand Jain goto fail_sysfs;
3464c59021f8Sliubo }
3465c59021f8Sliubo
34665b4aacefSJeff Mahoney ret = btrfs_read_block_groups(fs_info);
34671b1d1f66SJosef Bacik if (ret) {
346805135f59SDavid Sterba btrfs_err(fs_info, "failed to read block groups: %d", ret);
34692365dd3cSAnand Jain goto fail_sysfs;
34701b1d1f66SJosef Bacik }
34714330e183SQu Wenruo
347216beac87SNaohiro Aota btrfs_free_zone_cache(fs_info);
347316beac87SNaohiro Aota
3474a7e1ac7bSNaohiro Aota btrfs_check_active_zone_reservation(fs_info);
3475a7e1ac7bSNaohiro Aota
34765c78a5e7SAnand Jain if (!sb_rdonly(sb) && fs_info->fs_devices->missing_devices &&
34775c78a5e7SAnand Jain !btrfs_check_rw_degradable(fs_info, NULL)) {
347805135f59SDavid Sterba btrfs_warn(fs_info,
347952042d8eSAndrea Gelmini "writable mount is not allowed due to too many missing devices");
34804871c33bSQu Wenruo ret = -EINVAL;
34812365dd3cSAnand Jain goto fail_sysfs;
3482292fd7fcSStefan Behrens }
34839078a3e1SChris Mason
348433c44184SJosef Bacik fs_info->cleaner_kthread = kthread_run(cleaner_kthread, fs_info,
3485a74a4b97SChris Mason "btrfs-cleaner");
34864871c33bSQu Wenruo if (IS_ERR(fs_info->cleaner_kthread)) {
34874871c33bSQu Wenruo ret = PTR_ERR(fs_info->cleaner_kthread);
34882365dd3cSAnand Jain goto fail_sysfs;
34894871c33bSQu Wenruo }
3490a74a4b97SChris Mason
3491a74a4b97SChris Mason fs_info->transaction_kthread = kthread_run(transaction_kthread,
3492a74a4b97SChris Mason tree_root,
3493a74a4b97SChris Mason "btrfs-transaction");
34944871c33bSQu Wenruo if (IS_ERR(fs_info->transaction_kthread)) {
34954871c33bSQu Wenruo ret = PTR_ERR(fs_info->transaction_kthread);
34963f157a2fSChris Mason goto fail_cleaner;
34974871c33bSQu Wenruo }
3498a74a4b97SChris Mason
3499583b7231SHans van Kranenburg if (!btrfs_test_opt(fs_info, NOSSD) &&
3500c289811cSChris Mason !fs_info->fs_devices->rotating) {
3501583b7231SHans van Kranenburg btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations");
3502c289811cSChris Mason }
3503c289811cSChris Mason
3504572d9ab7SDavid Sterba /*
350563a7cb13SDavid Sterba * For devices supporting discard turn on discard=async automatically,
350663a7cb13SDavid Sterba * unless it's already set or disabled. This could be turned off by
350763a7cb13SDavid Sterba * nodiscard for the same mount.
350895ca6599SNaohiro Aota *
350995ca6599SNaohiro Aota * The zoned mode piggy backs on the discard functionality for
351095ca6599SNaohiro Aota * resetting a zone. There is no reason to delay the zone reset as it is
351195ca6599SNaohiro Aota * fast enough. So, do not enable async discard for zoned mode.
351263a7cb13SDavid Sterba */
351363a7cb13SDavid Sterba if (!(btrfs_test_opt(fs_info, DISCARD_SYNC) ||
351463a7cb13SDavid Sterba btrfs_test_opt(fs_info, DISCARD_ASYNC) ||
351563a7cb13SDavid Sterba btrfs_test_opt(fs_info, NODISCARD)) &&
351695ca6599SNaohiro Aota fs_info->fs_devices->discardable &&
351795ca6599SNaohiro Aota !btrfs_is_zoned(fs_info)) {
351863a7cb13SDavid Sterba btrfs_set_and_info(fs_info, DISCARD_ASYNC,
351963a7cb13SDavid Sterba "auto enabling async discard");
352063a7cb13SDavid Sterba }
352163a7cb13SDavid Sterba
352221adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
35230b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) {
35242ff7e61eSJeff Mahoney ret = btrfsic_mount(fs_info, fs_devices,
35250b246afaSJeff Mahoney btrfs_test_opt(fs_info,
3526cbeaae4fSDavid Sterba CHECK_INTEGRITY_DATA) ? 1 : 0,
352721adbd5cSStefan Behrens fs_info->check_integrity_print_mask);
352821adbd5cSStefan Behrens if (ret)
352905135f59SDavid Sterba btrfs_warn(fs_info,
353005135f59SDavid Sterba "failed to initialize integrity check module: %d",
353105135f59SDavid Sterba ret);
353221adbd5cSStefan Behrens }
353321adbd5cSStefan Behrens #endif
3534bcef60f2SArne Jansen ret = btrfs_read_qgroup_config(fs_info);
3535bcef60f2SArne Jansen if (ret)
3536bcef60f2SArne Jansen goto fail_trans_kthread;
353721adbd5cSStefan Behrens
3538fd708b81SJosef Bacik if (btrfs_build_ref_tree(fs_info))
3539fd708b81SJosef Bacik btrfs_err(fs_info, "couldn't build ref tree");
3540fd708b81SJosef Bacik
354196da0919SQu Wenruo /* do not make disk changes in broken FS or nologreplay is given */
354296da0919SQu Wenruo if (btrfs_super_log_root(disk_super) != 0 &&
35430b246afaSJeff Mahoney !btrfs_test_opt(fs_info, NOLOGREPLAY)) {
3544e8294f2fSDavid Sterba btrfs_info(fs_info, "start tree-log replay");
354563443bf5SEric Sandeen ret = btrfs_replay_log(fs_info, fs_devices);
35464871c33bSQu Wenruo if (ret)
354728c16cbbSWang Shilong goto fail_qgroup;
3548e556ce2cSYan Zheng }
35491a40e23bSZheng Yan
355056e9357aSDavid Sterba fs_info->fs_root = btrfs_get_fs_root(fs_info, BTRFS_FS_TREE_OBJECTID, true);
35513140c9a3SDan Carpenter if (IS_ERR(fs_info->fs_root)) {
35524871c33bSQu Wenruo ret = PTR_ERR(fs_info->fs_root);
35534871c33bSQu Wenruo btrfs_warn(fs_info, "failed to read fs tree: %d", ret);
3554315bf8efSJosef Bacik fs_info->fs_root = NULL;
3555bcef60f2SArne Jansen goto fail_qgroup;
35563140c9a3SDan Carpenter }
3557c289811cSChris Mason
3558bc98a42cSDavid Howells if (sb_rdonly(sb))
35598cd29088SBoris Burkov goto clear_oneshot;
35602b6ba629SIlya Dryomov
356144c0ca21SBoris Burkov ret = btrfs_start_pre_rw_mount(fs_info);
35622b6ba629SIlya Dryomov if (ret) {
35636bccf3abSJeff Mahoney close_ctree(fs_info);
35642b6ba629SIlya Dryomov return ret;
3565e3acc2a6SJosef Bacik }
3566b0643e59SDennis Zhou btrfs_discard_resume(fs_info);
3567b382a324SJan Schmidt
356844c0ca21SBoris Burkov if (fs_info->uuid_root &&
356944c0ca21SBoris Burkov (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) ||
357044c0ca21SBoris Burkov fs_info->generation != btrfs_super_uuid_tree_generation(disk_super))) {
357105135f59SDavid Sterba btrfs_info(fs_info, "checking UUID tree");
357270f80175SStefan Behrens ret = btrfs_check_uuid_tree(fs_info);
357370f80175SStefan Behrens if (ret) {
357405135f59SDavid Sterba btrfs_warn(fs_info,
357505135f59SDavid Sterba "failed to check the UUID tree: %d", ret);
35766bccf3abSJeff Mahoney close_ctree(fs_info);
357770f80175SStefan Behrens return ret;
357870f80175SStefan Behrens }
3579f7a81ea4SStefan Behrens }
358094846229SBoris Burkov
3581afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags);
358247ab2a6cSJosef Bacik
3583b4be6aefSJosef Bacik /* Kick the cleaner thread so it'll start deleting snapshots. */
3584b4be6aefSJosef Bacik if (test_bit(BTRFS_FS_UNFINISHED_DROPS, &fs_info->flags))
3585b4be6aefSJosef Bacik wake_up_process(fs_info->cleaner_kthread);
3586b4be6aefSJosef Bacik
35878cd29088SBoris Burkov clear_oneshot:
35888cd29088SBoris Burkov btrfs_clear_oneshot_options(fs_info);
3589ad2b2c80SAl Viro return 0;
359039279cc3SChris Mason
3591bcef60f2SArne Jansen fail_qgroup:
3592bcef60f2SArne Jansen btrfs_free_qgroup_config(fs_info);
35937c2ca468SChris Mason fail_trans_kthread:
35947c2ca468SChris Mason kthread_stop(fs_info->transaction_kthread);
35952ff7e61eSJeff Mahoney btrfs_cleanup_transaction(fs_info);
3596faa2dbf0SJosef Bacik btrfs_free_fs_roots(fs_info);
35973f157a2fSChris Mason fail_cleaner:
3598a74a4b97SChris Mason kthread_stop(fs_info->cleaner_kthread);
35997c2ca468SChris Mason
36007c2ca468SChris Mason /*
36017c2ca468SChris Mason * make sure we're done with the btree inode before we stop our
36027c2ca468SChris Mason * kthreads
36037c2ca468SChris Mason */
36047c2ca468SChris Mason filemap_write_and_wait(fs_info->btree_inode->i_mapping);
36057c2ca468SChris Mason
36062365dd3cSAnand Jain fail_sysfs:
36076618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info);
36082365dd3cSAnand Jain
3609b7c35e81SAnand Jain fail_fsdev_sysfs:
3610b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices);
3611b7c35e81SAnand Jain
36121b1d1f66SJosef Bacik fail_block_groups:
361354067ae9SJosef Bacik btrfs_put_block_group_cache(fs_info);
3614af31f5e5SChris Mason
3615af31f5e5SChris Mason fail_tree_roots:
36169e3aa805SJosef Bacik if (fs_info->data_reloc_root)
36179e3aa805SJosef Bacik btrfs_drop_and_free_fs_root(fs_info, fs_info->data_reloc_root);
36184273eaffSAnand Jain free_root_pointers(fs_info, true);
36192b8195bbSMiao Xie invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
3620af31f5e5SChris Mason
362139279cc3SChris Mason fail_sb_buffer:
36227abadb64SLiu Bo btrfs_stop_all_workers(fs_info);
36235cdd7db6SFilipe Manana btrfs_free_block_groups(fs_info);
362416cdcec7SMiao Xie fail_alloc:
3625586e46e2SIlya Dryomov btrfs_mapping_tree_free(&fs_info->mapping_tree);
3626586e46e2SIlya Dryomov
36274543df7eSChris Mason iput(fs_info->btree_inode);
36287e662854SQinghuang Feng fail:
3629586e46e2SIlya Dryomov btrfs_close_devices(fs_info->fs_devices);
36304871c33bSQu Wenruo ASSERT(ret < 0);
36314871c33bSQu Wenruo return ret;
3632eb60ceacSChris Mason }
3633663faf9fSMasami Hiramatsu ALLOW_ERROR_INJECTION(open_ctree, ERRNO);
3634eb60ceacSChris Mason
btrfs_end_super_write(struct bio * bio)3635314b6dd0SJohannes Thumshirn static void btrfs_end_super_write(struct bio *bio)
3636f2984462SChris Mason {
3637314b6dd0SJohannes Thumshirn struct btrfs_device *device = bio->bi_private;
3638314b6dd0SJohannes Thumshirn struct bio_vec *bvec;
3639314b6dd0SJohannes Thumshirn struct bvec_iter_all iter_all;
3640314b6dd0SJohannes Thumshirn struct page *page;
3641442a4f63SStefan Behrens
3642314b6dd0SJohannes Thumshirn bio_for_each_segment_all(bvec, bio, iter_all) {
3643314b6dd0SJohannes Thumshirn page = bvec->bv_page;
3644314b6dd0SJohannes Thumshirn
3645314b6dd0SJohannes Thumshirn if (bio->bi_status) {
3646fb456252SJeff Mahoney btrfs_warn_rl_in_rcu(device->fs_info,
3647314b6dd0SJohannes Thumshirn "lost page write due to IO error on %s (%d)",
3648cb3e217bSQu Wenruo btrfs_dev_name(device),
3649314b6dd0SJohannes Thumshirn blk_status_to_errno(bio->bi_status));
3650314b6dd0SJohannes Thumshirn ClearPageUptodate(page);
3651314b6dd0SJohannes Thumshirn SetPageError(page);
3652314b6dd0SJohannes Thumshirn btrfs_dev_stat_inc_and_print(device,
3653314b6dd0SJohannes Thumshirn BTRFS_DEV_STAT_WRITE_ERRS);
3654314b6dd0SJohannes Thumshirn } else {
3655314b6dd0SJohannes Thumshirn SetPageUptodate(page);
3656f2984462SChris Mason }
3657314b6dd0SJohannes Thumshirn
3658314b6dd0SJohannes Thumshirn put_page(page);
3659314b6dd0SJohannes Thumshirn unlock_page(page);
3660314b6dd0SJohannes Thumshirn }
3661314b6dd0SJohannes Thumshirn
3662314b6dd0SJohannes Thumshirn bio_put(bio);
3663f2984462SChris Mason }
3664f2984462SChris Mason
btrfs_read_dev_one_super(struct block_device * bdev,int copy_num,bool drop_cache)36658f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_one_super(struct block_device *bdev,
3666a05d3c91SQu Wenruo int copy_num, bool drop_cache)
366729c36d72SAnand Jain {
366829c36d72SAnand Jain struct btrfs_super_block *super;
36698f32380dSJohannes Thumshirn struct page *page;
367012659251SNaohiro Aota u64 bytenr, bytenr_orig;
36718f32380dSJohannes Thumshirn struct address_space *mapping = bdev->bd_inode->i_mapping;
367212659251SNaohiro Aota int ret;
367329c36d72SAnand Jain
367412659251SNaohiro Aota bytenr_orig = btrfs_sb_offset(copy_num);
367512659251SNaohiro Aota ret = btrfs_sb_log_location_bdev(bdev, copy_num, READ, &bytenr);
367612659251SNaohiro Aota if (ret == -ENOENT)
367712659251SNaohiro Aota return ERR_PTR(-EINVAL);
367812659251SNaohiro Aota else if (ret)
367912659251SNaohiro Aota return ERR_PTR(ret);
368012659251SNaohiro Aota
3681cda00ebaSChristoph Hellwig if (bytenr + BTRFS_SUPER_INFO_SIZE >= bdev_nr_bytes(bdev))
36828f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL);
368329c36d72SAnand Jain
3684a05d3c91SQu Wenruo if (drop_cache) {
3685a05d3c91SQu Wenruo /* This should only be called with the primary sb. */
3686a05d3c91SQu Wenruo ASSERT(copy_num == 0);
3687a05d3c91SQu Wenruo
3688a05d3c91SQu Wenruo /*
3689a05d3c91SQu Wenruo * Drop the page of the primary superblock, so later read will
3690a05d3c91SQu Wenruo * always read from the device.
3691a05d3c91SQu Wenruo */
3692a05d3c91SQu Wenruo invalidate_inode_pages2_range(mapping,
3693a05d3c91SQu Wenruo bytenr >> PAGE_SHIFT,
3694a05d3c91SQu Wenruo (bytenr + BTRFS_SUPER_INFO_SIZE) >> PAGE_SHIFT);
3695a05d3c91SQu Wenruo }
3696a05d3c91SQu Wenruo
36978f32380dSJohannes Thumshirn page = read_cache_page_gfp(mapping, bytenr >> PAGE_SHIFT, GFP_NOFS);
36988f32380dSJohannes Thumshirn if (IS_ERR(page))
36998f32380dSJohannes Thumshirn return ERR_CAST(page);
370029c36d72SAnand Jain
37018f32380dSJohannes Thumshirn super = page_address(page);
370296c2e067SAnand Jain if (btrfs_super_magic(super) != BTRFS_MAGIC) {
370396c2e067SAnand Jain btrfs_release_disk_super(super);
370496c2e067SAnand Jain return ERR_PTR(-ENODATA);
370596c2e067SAnand Jain }
370696c2e067SAnand Jain
370712659251SNaohiro Aota if (btrfs_super_bytenr(super) != bytenr_orig) {
37088f32380dSJohannes Thumshirn btrfs_release_disk_super(super);
37098f32380dSJohannes Thumshirn return ERR_PTR(-EINVAL);
371029c36d72SAnand Jain }
371129c36d72SAnand Jain
37128f32380dSJohannes Thumshirn return super;
371329c36d72SAnand Jain }
371429c36d72SAnand Jain
371529c36d72SAnand Jain
btrfs_read_dev_super(struct block_device * bdev)37168f32380dSJohannes Thumshirn struct btrfs_super_block *btrfs_read_dev_super(struct block_device *bdev)
3717a512bbf8SYan Zheng {
37188f32380dSJohannes Thumshirn struct btrfs_super_block *super, *latest = NULL;
3719a512bbf8SYan Zheng int i;
3720a512bbf8SYan Zheng u64 transid = 0;
3721a512bbf8SYan Zheng
3722a512bbf8SYan Zheng /* we would like to check all the supers, but that would make
3723a512bbf8SYan Zheng * a btrfs mount succeed after a mkfs from a different FS.
3724a512bbf8SYan Zheng * So, we need to add a special mount option to scan for
3725a512bbf8SYan Zheng * later supers, using BTRFS_SUPER_MIRROR_MAX instead
3726a512bbf8SYan Zheng */
3727a512bbf8SYan Zheng for (i = 0; i < 1; i++) {
3728a05d3c91SQu Wenruo super = btrfs_read_dev_one_super(bdev, i, false);
37298f32380dSJohannes Thumshirn if (IS_ERR(super))
3730a512bbf8SYan Zheng continue;
3731a512bbf8SYan Zheng
3732a512bbf8SYan Zheng if (!latest || btrfs_super_generation(super) > transid) {
37338f32380dSJohannes Thumshirn if (latest)
37348f32380dSJohannes Thumshirn btrfs_release_disk_super(super);
37358f32380dSJohannes Thumshirn
37368f32380dSJohannes Thumshirn latest = super;
3737a512bbf8SYan Zheng transid = btrfs_super_generation(super);
3738a512bbf8SYan Zheng }
3739a512bbf8SYan Zheng }
374092fc03fbSAnand Jain
37418f32380dSJohannes Thumshirn return super;
3742a512bbf8SYan Zheng }
3743a512bbf8SYan Zheng
37444eedeb75SHisashi Hifumi /*
3745abbb3b8eSDavid Sterba * Write superblock @sb to the @device. Do not wait for completion, all the
3746314b6dd0SJohannes Thumshirn * pages we use for writing are locked.
37474eedeb75SHisashi Hifumi *
3748abbb3b8eSDavid Sterba * Write @max_mirrors copies of the superblock, where 0 means default that fit
3749abbb3b8eSDavid Sterba * the expected device size at commit time. Note that max_mirrors must be
3750abbb3b8eSDavid Sterba * same for write and wait phases.
37514eedeb75SHisashi Hifumi *
3752314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or submission fails.
37534eedeb75SHisashi Hifumi */
write_dev_supers(struct btrfs_device * device,struct btrfs_super_block * sb,int max_mirrors)3754a512bbf8SYan Zheng static int write_dev_supers(struct btrfs_device *device,
3755abbb3b8eSDavid Sterba struct btrfs_super_block *sb, int max_mirrors)
3756a512bbf8SYan Zheng {
3757d5178578SJohannes Thumshirn struct btrfs_fs_info *fs_info = device->fs_info;
3758314b6dd0SJohannes Thumshirn struct address_space *mapping = device->bdev->bd_inode->i_mapping;
3759d5178578SJohannes Thumshirn SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
3760a512bbf8SYan Zheng int i;
3761a512bbf8SYan Zheng int errors = 0;
376212659251SNaohiro Aota int ret;
376312659251SNaohiro Aota u64 bytenr, bytenr_orig;
3764a512bbf8SYan Zheng
3765a512bbf8SYan Zheng if (max_mirrors == 0)
3766a512bbf8SYan Zheng max_mirrors = BTRFS_SUPER_MIRROR_MAX;
3767a512bbf8SYan Zheng
3768d5178578SJohannes Thumshirn shash->tfm = fs_info->csum_shash;
3769d5178578SJohannes Thumshirn
3770a512bbf8SYan Zheng for (i = 0; i < max_mirrors; i++) {
3771314b6dd0SJohannes Thumshirn struct page *page;
3772314b6dd0SJohannes Thumshirn struct bio *bio;
3773314b6dd0SJohannes Thumshirn struct btrfs_super_block *disk_super;
3774314b6dd0SJohannes Thumshirn
377512659251SNaohiro Aota bytenr_orig = btrfs_sb_offset(i);
377612659251SNaohiro Aota ret = btrfs_sb_log_location(device, i, WRITE, &bytenr);
377712659251SNaohiro Aota if (ret == -ENOENT) {
377812659251SNaohiro Aota continue;
377912659251SNaohiro Aota } else if (ret < 0) {
378012659251SNaohiro Aota btrfs_err(device->fs_info,
378112659251SNaohiro Aota "couldn't get super block location for mirror %d",
378212659251SNaohiro Aota i);
378312659251SNaohiro Aota errors++;
378412659251SNaohiro Aota continue;
378512659251SNaohiro Aota }
3786935e5cc9SMiao Xie if (bytenr + BTRFS_SUPER_INFO_SIZE >=
3787935e5cc9SMiao Xie device->commit_total_bytes)
3788a512bbf8SYan Zheng break;
3789a512bbf8SYan Zheng
379012659251SNaohiro Aota btrfs_set_super_bytenr(sb, bytenr_orig);
3791a512bbf8SYan Zheng
3792fd08001fSEric Biggers crypto_shash_digest(shash, (const char *)sb + BTRFS_CSUM_SIZE,
3793fd08001fSEric Biggers BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE,
3794fd08001fSEric Biggers sb->csum);
3795a512bbf8SYan Zheng
3796314b6dd0SJohannes Thumshirn page = find_or_create_page(mapping, bytenr >> PAGE_SHIFT,
3797314b6dd0SJohannes Thumshirn GFP_NOFS);
3798314b6dd0SJohannes Thumshirn if (!page) {
3799fb456252SJeff Mahoney btrfs_err(device->fs_info,
3800314b6dd0SJohannes Thumshirn "couldn't get super block page for bytenr %llu",
3801f14d104dSDavid Sterba bytenr);
3802634554dcSJosef Bacik errors++;
3803634554dcSJosef Bacik continue;
3804634554dcSJosef Bacik }
3805634554dcSJosef Bacik
3806314b6dd0SJohannes Thumshirn /* Bump the refcount for wait_dev_supers() */
3807314b6dd0SJohannes Thumshirn get_page(page);
3808a512bbf8SYan Zheng
3809314b6dd0SJohannes Thumshirn disk_super = page_address(page);
3810314b6dd0SJohannes Thumshirn memcpy(disk_super, sb, BTRFS_SUPER_INFO_SIZE);
3811a512bbf8SYan Zheng
3812387125fcSChris Mason /*
3813314b6dd0SJohannes Thumshirn * Directly use bios here instead of relying on the page cache
3814314b6dd0SJohannes Thumshirn * to do I/O, so we don't lose the ability to do integrity
3815314b6dd0SJohannes Thumshirn * checking.
3816387125fcSChris Mason */
381707888c66SChristoph Hellwig bio = bio_alloc(device->bdev, 1,
381807888c66SChristoph Hellwig REQ_OP_WRITE | REQ_SYNC | REQ_META | REQ_PRIO,
381907888c66SChristoph Hellwig GFP_NOFS);
3820314b6dd0SJohannes Thumshirn bio->bi_iter.bi_sector = bytenr >> SECTOR_SHIFT;
3821314b6dd0SJohannes Thumshirn bio->bi_private = device;
3822314b6dd0SJohannes Thumshirn bio->bi_end_io = btrfs_end_super_write;
3823314b6dd0SJohannes Thumshirn __bio_add_page(bio, page, BTRFS_SUPER_INFO_SIZE,
3824314b6dd0SJohannes Thumshirn offset_in_page(bytenr));
3825314b6dd0SJohannes Thumshirn
3826314b6dd0SJohannes Thumshirn /*
3827314b6dd0SJohannes Thumshirn * We FUA only the first super block. The others we allow to
3828314b6dd0SJohannes Thumshirn * go down lazy and there's a short window where the on-disk
3829314b6dd0SJohannes Thumshirn * copies might still contain the older version.
3830314b6dd0SJohannes Thumshirn */
38311b9e619cSOmar Sandoval if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER))
3832314b6dd0SJohannes Thumshirn bio->bi_opf |= REQ_FUA;
3833314b6dd0SJohannes Thumshirn
383458ff51f1SChristoph Hellwig btrfsic_check_bio(bio);
383558ff51f1SChristoph Hellwig submit_bio(bio);
38368376d9e1SNaohiro Aota
38378376d9e1SNaohiro Aota if (btrfs_advance_sb_log(device, i))
38388376d9e1SNaohiro Aota errors++;
3839a512bbf8SYan Zheng }
3840a512bbf8SYan Zheng return errors < i ? 0 : -1;
3841a512bbf8SYan Zheng }
3842a512bbf8SYan Zheng
3843387125fcSChris Mason /*
3844abbb3b8eSDavid Sterba * Wait for write completion of superblocks done by write_dev_supers,
3845abbb3b8eSDavid Sterba * @max_mirrors same for write and wait phases.
3846abbb3b8eSDavid Sterba *
3847314b6dd0SJohannes Thumshirn * Return number of errors when page is not found or not marked up to
3848abbb3b8eSDavid Sterba * date.
3849abbb3b8eSDavid Sterba */
wait_dev_supers(struct btrfs_device * device,int max_mirrors)3850abbb3b8eSDavid Sterba static int wait_dev_supers(struct btrfs_device *device, int max_mirrors)
3851abbb3b8eSDavid Sterba {
3852abbb3b8eSDavid Sterba int i;
3853abbb3b8eSDavid Sterba int errors = 0;
3854b6a535faSHoward McLauchlan bool primary_failed = false;
385512659251SNaohiro Aota int ret;
3856abbb3b8eSDavid Sterba u64 bytenr;
3857abbb3b8eSDavid Sterba
3858abbb3b8eSDavid Sterba if (max_mirrors == 0)
3859abbb3b8eSDavid Sterba max_mirrors = BTRFS_SUPER_MIRROR_MAX;
3860abbb3b8eSDavid Sterba
3861abbb3b8eSDavid Sterba for (i = 0; i < max_mirrors; i++) {
3862314b6dd0SJohannes Thumshirn struct page *page;
3863314b6dd0SJohannes Thumshirn
386412659251SNaohiro Aota ret = btrfs_sb_log_location(device, i, READ, &bytenr);
386512659251SNaohiro Aota if (ret == -ENOENT) {
386612659251SNaohiro Aota break;
386712659251SNaohiro Aota } else if (ret < 0) {
386812659251SNaohiro Aota errors++;
386912659251SNaohiro Aota if (i == 0)
387012659251SNaohiro Aota primary_failed = true;
387112659251SNaohiro Aota continue;
387212659251SNaohiro Aota }
3873abbb3b8eSDavid Sterba if (bytenr + BTRFS_SUPER_INFO_SIZE >=
3874abbb3b8eSDavid Sterba device->commit_total_bytes)
3875abbb3b8eSDavid Sterba break;
3876abbb3b8eSDavid Sterba
3877314b6dd0SJohannes Thumshirn page = find_get_page(device->bdev->bd_inode->i_mapping,
3878314b6dd0SJohannes Thumshirn bytenr >> PAGE_SHIFT);
3879314b6dd0SJohannes Thumshirn if (!page) {
3880abbb3b8eSDavid Sterba errors++;
3881b6a535faSHoward McLauchlan if (i == 0)
3882b6a535faSHoward McLauchlan primary_failed = true;
3883abbb3b8eSDavid Sterba continue;
3884abbb3b8eSDavid Sterba }
3885314b6dd0SJohannes Thumshirn /* Page is submitted locked and unlocked once the IO completes */
3886314b6dd0SJohannes Thumshirn wait_on_page_locked(page);
3887314b6dd0SJohannes Thumshirn if (PageError(page)) {
3888abbb3b8eSDavid Sterba errors++;
3889b6a535faSHoward McLauchlan if (i == 0)
3890b6a535faSHoward McLauchlan primary_failed = true;
3891b6a535faSHoward McLauchlan }
3892abbb3b8eSDavid Sterba
3893314b6dd0SJohannes Thumshirn /* Drop our reference */
3894314b6dd0SJohannes Thumshirn put_page(page);
3895abbb3b8eSDavid Sterba
3896314b6dd0SJohannes Thumshirn /* Drop the reference from the writing run */
3897314b6dd0SJohannes Thumshirn put_page(page);
3898abbb3b8eSDavid Sterba }
3899abbb3b8eSDavid Sterba
3900b6a535faSHoward McLauchlan /* log error, force error return */
3901b6a535faSHoward McLauchlan if (primary_failed) {
3902b6a535faSHoward McLauchlan btrfs_err(device->fs_info, "error writing primary super block to device %llu",
3903b6a535faSHoward McLauchlan device->devid);
3904b6a535faSHoward McLauchlan return -1;
3905b6a535faSHoward McLauchlan }
3906b6a535faSHoward McLauchlan
3907abbb3b8eSDavid Sterba return errors < i ? 0 : -1;
3908abbb3b8eSDavid Sterba }
3909abbb3b8eSDavid Sterba
3910abbb3b8eSDavid Sterba /*
3911387125fcSChris Mason * endio for the write_dev_flush, this will wake anyone waiting
3912387125fcSChris Mason * for the barrier when it is done
3913387125fcSChris Mason */
btrfs_end_empty_barrier(struct bio * bio)39144246a0b6SChristoph Hellwig static void btrfs_end_empty_barrier(struct bio *bio)
3915387125fcSChris Mason {
3916f9e69aa9SChristoph Hellwig bio_uninit(bio);
3917387125fcSChris Mason complete(bio->bi_private);
3918387125fcSChris Mason }
3919387125fcSChris Mason
3920387125fcSChris Mason /*
39214fc6441aSAnand Jain * Submit a flush request to the device if it supports it. Error handling is
39224fc6441aSAnand Jain * done in the waiting counterpart.
3923387125fcSChris Mason */
write_dev_flush(struct btrfs_device * device)39244fc6441aSAnand Jain static void write_dev_flush(struct btrfs_device *device)
3925387125fcSChris Mason {
3926f9e69aa9SChristoph Hellwig struct bio *bio = &device->flush_bio;
3927387125fcSChris Mason
3928bfd3ea94SAnand Jain device->last_flush_error = BLK_STS_OK;
3929bfd3ea94SAnand Jain
3930a91cf0ffSWang Yugui #ifndef CONFIG_BTRFS_FS_CHECK_INTEGRITY
3931a91cf0ffSWang Yugui /*
3932a91cf0ffSWang Yugui * When a disk has write caching disabled, we skip submission of a bio
3933a91cf0ffSWang Yugui * with flush and sync requests before writing the superblock, since
3934a91cf0ffSWang Yugui * it's not needed. However when the integrity checker is enabled, this
3935a91cf0ffSWang Yugui * results in reports that there are metadata blocks referred by a
3936a91cf0ffSWang Yugui * superblock that were not properly flushed. So don't skip the bio
3937a91cf0ffSWang Yugui * submission only when the integrity checker is enabled for the sake
3938a91cf0ffSWang Yugui * of simplicity, since this is a debug tool and not meant for use in
3939a91cf0ffSWang Yugui * non-debug builds.
3940a91cf0ffSWang Yugui */
394108e688fdSChristoph Hellwig if (!bdev_write_cache(device->bdev))
39424fc6441aSAnand Jain return;
3943a91cf0ffSWang Yugui #endif
3944387125fcSChris Mason
3945f9e69aa9SChristoph Hellwig bio_init(bio, device->bdev, NULL, 0,
3946f9e69aa9SChristoph Hellwig REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH);
3947387125fcSChris Mason bio->bi_end_io = btrfs_end_empty_barrier;
3948387125fcSChris Mason init_completion(&device->flush_wait);
3949387125fcSChris Mason bio->bi_private = &device->flush_wait;
3950387125fcSChris Mason
395158ff51f1SChristoph Hellwig btrfsic_check_bio(bio);
395258ff51f1SChristoph Hellwig submit_bio(bio);
39531c3063b6SAnand Jain set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state);
39544fc6441aSAnand Jain }
3955387125fcSChris Mason
39564fc6441aSAnand Jain /*
39574fc6441aSAnand Jain * If the flush bio has been submitted by write_dev_flush, wait for it.
39581b465784SAnand Jain * Return true for any error, and false otherwise.
39594fc6441aSAnand Jain */
wait_dev_flush(struct btrfs_device * device)39601b465784SAnand Jain static bool wait_dev_flush(struct btrfs_device *device)
39614fc6441aSAnand Jain {
3962f9e69aa9SChristoph Hellwig struct bio *bio = &device->flush_bio;
39634fc6441aSAnand Jain
39647e812f20SAnand Jain if (!test_and_clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state))
39651b465784SAnand Jain return false;
39664fc6441aSAnand Jain
39672980d574SDavid Sterba wait_for_completion_io(&device->flush_wait);
39684fc6441aSAnand Jain
3969bfd3ea94SAnand Jain if (bio->bi_status) {
3970bfd3ea94SAnand Jain device->last_flush_error = bio->bi_status;
3971bfd3ea94SAnand Jain btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_FLUSH_ERRS);
39721b465784SAnand Jain return true;
3973bfd3ea94SAnand Jain }
3974bfd3ea94SAnand Jain
39751b465784SAnand Jain return false;
3976387125fcSChris Mason }
3977387125fcSChris Mason
3978387125fcSChris Mason /*
3979387125fcSChris Mason * send an empty flush down to each device in parallel,
3980387125fcSChris Mason * then wait for them
3981387125fcSChris Mason */
barrier_all_devices(struct btrfs_fs_info * info)3982387125fcSChris Mason static int barrier_all_devices(struct btrfs_fs_info *info)
3983387125fcSChris Mason {
3984387125fcSChris Mason struct list_head *head;
3985387125fcSChris Mason struct btrfs_device *dev;
39865af3e8ccSStefan Behrens int errors_wait = 0;
3987387125fcSChris Mason
39881538e6c5SDavid Sterba lockdep_assert_held(&info->fs_devices->device_list_mutex);
3989387125fcSChris Mason /* send down all the barriers */
3990387125fcSChris Mason head = &info->fs_devices->devices;
39911538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) {
3992e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
3993f88ba6a2SHidetoshi Seto continue;
3994cea7c8bfSAnand Jain if (!dev->bdev)
3995387125fcSChris Mason continue;
3996e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
3997ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
3998387125fcSChris Mason continue;
3999387125fcSChris Mason
40004fc6441aSAnand Jain write_dev_flush(dev);
4001387125fcSChris Mason }
4002387125fcSChris Mason
4003387125fcSChris Mason /* wait for all the barriers */
40041538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) {
4005e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
4006f88ba6a2SHidetoshi Seto continue;
4007387125fcSChris Mason if (!dev->bdev) {
40085af3e8ccSStefan Behrens errors_wait++;
4009387125fcSChris Mason continue;
4010387125fcSChris Mason }
4011e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
4012ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
4013387125fcSChris Mason continue;
4014387125fcSChris Mason
40151b465784SAnand Jain if (wait_dev_flush(dev))
40165af3e8ccSStefan Behrens errors_wait++;
4017387125fcSChris Mason }
4018401b41e5SAnand Jain
4019401b41e5SAnand Jain /*
4020de38a206SAnand Jain * Checks last_flush_error of disks in order to determine the device
4021de38a206SAnand Jain * state.
4022401b41e5SAnand Jain */
4023de38a206SAnand Jain if (errors_wait && !btrfs_check_rw_degradable(info, NULL))
4024de38a206SAnand Jain return -EIO;
4025de38a206SAnand Jain
4026387125fcSChris Mason return 0;
4027387125fcSChris Mason }
4028387125fcSChris Mason
btrfs_get_num_tolerated_disk_barrier_failures(u64 flags)4029943c6e99SZhao Lei int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags)
4030943c6e99SZhao Lei {
40318789f4feSZhao Lei int raid_type;
40328789f4feSZhao Lei int min_tolerated = INT_MAX;
4033943c6e99SZhao Lei
40348789f4feSZhao Lei if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 ||
40358789f4feSZhao Lei (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE))
40368c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated,
40378789f4feSZhao Lei btrfs_raid_array[BTRFS_RAID_SINGLE].
40388789f4feSZhao Lei tolerated_failures);
4039943c6e99SZhao Lei
40408789f4feSZhao Lei for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
40418789f4feSZhao Lei if (raid_type == BTRFS_RAID_SINGLE)
40428789f4feSZhao Lei continue;
404341a6e891SAnand Jain if (!(flags & btrfs_raid_array[raid_type].bg_flag))
40448789f4feSZhao Lei continue;
40458c3e3582SDavid Sterba min_tolerated = min_t(int, min_tolerated,
40468789f4feSZhao Lei btrfs_raid_array[raid_type].
40478789f4feSZhao Lei tolerated_failures);
40488789f4feSZhao Lei }
4049943c6e99SZhao Lei
40508789f4feSZhao Lei if (min_tolerated == INT_MAX) {
4051ab8d0fc4SJeff Mahoney pr_warn("BTRFS: unknown raid flag: %llu", flags);
40528789f4feSZhao Lei min_tolerated = 0;
40538789f4feSZhao Lei }
40548789f4feSZhao Lei
40558789f4feSZhao Lei return min_tolerated;
4056943c6e99SZhao Lei }
4057943c6e99SZhao Lei
write_all_supers(struct btrfs_fs_info * fs_info,int max_mirrors)4058eece6a9cSDavid Sterba int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
4059f2984462SChris Mason {
4060e5e9a520SChris Mason struct list_head *head;
4061f2984462SChris Mason struct btrfs_device *dev;
4062a061fc8dSChris Mason struct btrfs_super_block *sb;
4063f2984462SChris Mason struct btrfs_dev_item *dev_item;
4064f2984462SChris Mason int ret;
4065f2984462SChris Mason int do_barriers;
4066a236aed1SChris Mason int max_errors;
4067a236aed1SChris Mason int total_errors = 0;
4068a061fc8dSChris Mason u64 flags;
4069f2984462SChris Mason
40700b246afaSJeff Mahoney do_barriers = !btrfs_test_opt(fs_info, NOBARRIER);
4071fed3b381SLiu Bo
4072fed3b381SLiu Bo /*
4073fed3b381SLiu Bo * max_mirrors == 0 indicates we're from commit_transaction,
4074fed3b381SLiu Bo * not from fsync where the tree roots in fs_info have not
4075fed3b381SLiu Bo * been consistent on disk.
4076fed3b381SLiu Bo */
4077fed3b381SLiu Bo if (max_mirrors == 0)
40780b246afaSJeff Mahoney backup_super_roots(fs_info);
4079f2984462SChris Mason
40800b246afaSJeff Mahoney sb = fs_info->super_for_commit;
4081a061fc8dSChris Mason dev_item = &sb->dev_item;
4082e5e9a520SChris Mason
40830b246afaSJeff Mahoney mutex_lock(&fs_info->fs_devices->device_list_mutex);
40840b246afaSJeff Mahoney head = &fs_info->fs_devices->devices;
40850b246afaSJeff Mahoney max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1;
4086387125fcSChris Mason
40875af3e8ccSStefan Behrens if (do_barriers) {
40880b246afaSJeff Mahoney ret = barrier_all_devices(fs_info);
40895af3e8ccSStefan Behrens if (ret) {
40905af3e8ccSStefan Behrens mutex_unlock(
40910b246afaSJeff Mahoney &fs_info->fs_devices->device_list_mutex);
40920b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret,
40935af3e8ccSStefan Behrens "errors while submitting device barriers.");
40945af3e8ccSStefan Behrens return ret;
40955af3e8ccSStefan Behrens }
40965af3e8ccSStefan Behrens }
4097387125fcSChris Mason
40981538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) {
4099dfe25020SChris Mason if (!dev->bdev) {
4100dfe25020SChris Mason total_errors++;
4101dfe25020SChris Mason continue;
4102dfe25020SChris Mason }
4103e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
4104ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
4105dfe25020SChris Mason continue;
4106dfe25020SChris Mason
41072b82032cSYan Zheng btrfs_set_stack_device_generation(dev_item, 0);
4108a061fc8dSChris Mason btrfs_set_stack_device_type(dev_item, dev->type);
4109a061fc8dSChris Mason btrfs_set_stack_device_id(dev_item, dev->devid);
41107df69d3eSMiao Xie btrfs_set_stack_device_total_bytes(dev_item,
4111935e5cc9SMiao Xie dev->commit_total_bytes);
4112ce7213c7SMiao Xie btrfs_set_stack_device_bytes_used(dev_item,
4113ce7213c7SMiao Xie dev->commit_bytes_used);
4114a061fc8dSChris Mason btrfs_set_stack_device_io_align(dev_item, dev->io_align);
4115a061fc8dSChris Mason btrfs_set_stack_device_io_width(dev_item, dev->io_width);
4116a061fc8dSChris Mason btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
4117a061fc8dSChris Mason memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
41187239ff4bSNikolay Borisov memcpy(dev_item->fsid, dev->fs_devices->metadata_uuid,
41197239ff4bSNikolay Borisov BTRFS_FSID_SIZE);
4120a512bbf8SYan Zheng
4121a061fc8dSChris Mason flags = btrfs_super_flags(sb);
4122a061fc8dSChris Mason btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
4123f2984462SChris Mason
412475cb857dSQu Wenruo ret = btrfs_validate_write_super(fs_info, sb);
412575cb857dSQu Wenruo if (ret < 0) {
412675cb857dSQu Wenruo mutex_unlock(&fs_info->fs_devices->device_list_mutex);
412775cb857dSQu Wenruo btrfs_handle_fs_error(fs_info, -EUCLEAN,
412875cb857dSQu Wenruo "unexpected superblock corruption detected");
412975cb857dSQu Wenruo return -EUCLEAN;
413075cb857dSQu Wenruo }
413175cb857dSQu Wenruo
4132abbb3b8eSDavid Sterba ret = write_dev_supers(dev, sb, max_mirrors);
4133a236aed1SChris Mason if (ret)
4134a236aed1SChris Mason total_errors++;
4135f2984462SChris Mason }
4136a236aed1SChris Mason if (total_errors > max_errors) {
41370b246afaSJeff Mahoney btrfs_err(fs_info, "%d errors while writing supers",
4138d397712bSChris Mason total_errors);
41390b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex);
414079787eaaSJeff Mahoney
41419d565ba4SStefan Behrens /* FUA is masked off if unsupported and can't be the reason */
41420b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO,
41430b246afaSJeff Mahoney "%d errors while writing supers",
41440b246afaSJeff Mahoney total_errors);
41459d565ba4SStefan Behrens return -EIO;
4146a236aed1SChris Mason }
4147f2984462SChris Mason
4148a512bbf8SYan Zheng total_errors = 0;
41491538e6c5SDavid Sterba list_for_each_entry(dev, head, dev_list) {
4150dfe25020SChris Mason if (!dev->bdev)
4151dfe25020SChris Mason continue;
4152e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
4153ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
4154dfe25020SChris Mason continue;
4155dfe25020SChris Mason
4156abbb3b8eSDavid Sterba ret = wait_dev_supers(dev, max_mirrors);
4157a512bbf8SYan Zheng if (ret)
41581259ab75SChris Mason total_errors++;
4159f2984462SChris Mason }
41600b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4161a236aed1SChris Mason if (total_errors > max_errors) {
41620b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, -EIO,
41630b246afaSJeff Mahoney "%d errors while writing supers",
41640b246afaSJeff Mahoney total_errors);
416579787eaaSJeff Mahoney return -EIO;
4166a236aed1SChris Mason }
4167f2984462SChris Mason return 0;
4168f2984462SChris Mason }
4169f2984462SChris Mason
4170cb517eabSMiao 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)4171cb517eabSMiao Xie void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info,
4172cb517eabSMiao Xie struct btrfs_root *root)
41732619ba1fSChris Mason {
41744785e24fSJosef Bacik bool drop_ref = false;
41754785e24fSJosef Bacik
4176fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock);
4177fc7cbcd4SDavid Sterba radix_tree_delete(&fs_info->fs_roots_radix,
4178fc7cbcd4SDavid Sterba (unsigned long)root->root_key.objectid);
4179fc7cbcd4SDavid Sterba if (test_and_clear_bit(BTRFS_ROOT_IN_RADIX, &root->state))
41804785e24fSJosef Bacik drop_ref = true;
4181fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock);
418276dda93cSYan, Zheng
418384961539SJosef Bacik if (BTRFS_FS_ERROR(fs_info)) {
4184ef67963dSJosef Bacik ASSERT(root->log_root == NULL);
41851c1ea4f7SLiu Bo if (root->reloc_root) {
418600246528SJosef Bacik btrfs_put_root(root->reloc_root);
41871c1ea4f7SLiu Bo root->reloc_root = NULL;
41881c1ea4f7SLiu Bo }
41891c1ea4f7SLiu Bo }
41903321719eSLiu Bo
41914785e24fSJosef Bacik if (drop_ref)
419200246528SJosef Bacik btrfs_put_root(root);
41932619ba1fSChris Mason }
41942619ba1fSChris Mason
btrfs_commit_super(struct btrfs_fs_info * fs_info)41956bccf3abSJeff Mahoney int btrfs_commit_super(struct btrfs_fs_info *fs_info)
4196c146afadSYan Zheng {
41976bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root;
4198c146afadSYan Zheng struct btrfs_trans_handle *trans;
4199c146afadSYan Zheng
42000b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex);
42012ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info);
42020b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex);
42030b246afaSJeff Mahoney wake_up_process(fs_info->cleaner_kthread);
4204c71bf099SYan, Zheng
4205c71bf099SYan, Zheng /* wait until ongoing cleanup work done */
42060b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem);
42070b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem);
4208c71bf099SYan, Zheng
42097a7eaa40SJosef Bacik trans = btrfs_join_transaction(root);
42103612b495STsutomu Itoh if (IS_ERR(trans))
42113612b495STsutomu Itoh return PTR_ERR(trans);
42123a45bb20SJeff Mahoney return btrfs_commit_transaction(trans);
4213c146afadSYan Zheng }
4214c146afadSYan Zheng
warn_about_uncommitted_trans(struct btrfs_fs_info * fs_info)421536c86a9eSQu Wenruo static void warn_about_uncommitted_trans(struct btrfs_fs_info *fs_info)
421636c86a9eSQu Wenruo {
421736c86a9eSQu Wenruo struct btrfs_transaction *trans;
421836c86a9eSQu Wenruo struct btrfs_transaction *tmp;
421936c86a9eSQu Wenruo bool found = false;
422036c86a9eSQu Wenruo
422136c86a9eSQu Wenruo if (list_empty(&fs_info->trans_list))
422236c86a9eSQu Wenruo return;
422336c86a9eSQu Wenruo
422436c86a9eSQu Wenruo /*
422536c86a9eSQu Wenruo * This function is only called at the very end of close_ctree(),
422636c86a9eSQu Wenruo * thus no other running transaction, no need to take trans_lock.
422736c86a9eSQu Wenruo */
422836c86a9eSQu Wenruo ASSERT(test_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags));
422936c86a9eSQu Wenruo list_for_each_entry_safe(trans, tmp, &fs_info->trans_list, list) {
423036c86a9eSQu Wenruo struct extent_state *cached = NULL;
423136c86a9eSQu Wenruo u64 dirty_bytes = 0;
423236c86a9eSQu Wenruo u64 cur = 0;
423336c86a9eSQu Wenruo u64 found_start;
423436c86a9eSQu Wenruo u64 found_end;
423536c86a9eSQu Wenruo
423636c86a9eSQu Wenruo found = true;
4237e5860f82SFilipe Manana while (find_first_extent_bit(&trans->dirty_pages, cur,
423836c86a9eSQu Wenruo &found_start, &found_end, EXTENT_DIRTY, &cached)) {
423936c86a9eSQu Wenruo dirty_bytes += found_end + 1 - found_start;
424036c86a9eSQu Wenruo cur = found_end + 1;
424136c86a9eSQu Wenruo }
424236c86a9eSQu Wenruo btrfs_warn(fs_info,
424336c86a9eSQu Wenruo "transaction %llu (with %llu dirty metadata bytes) is not committed",
424436c86a9eSQu Wenruo trans->transid, dirty_bytes);
424536c86a9eSQu Wenruo btrfs_cleanup_one_transaction(trans, fs_info);
424636c86a9eSQu Wenruo
424736c86a9eSQu Wenruo if (trans == fs_info->running_transaction)
424836c86a9eSQu Wenruo fs_info->running_transaction = NULL;
424936c86a9eSQu Wenruo list_del_init(&trans->list);
425036c86a9eSQu Wenruo
425136c86a9eSQu Wenruo btrfs_put_transaction(trans);
425236c86a9eSQu Wenruo trace_btrfs_transaction_commit(fs_info);
425336c86a9eSQu Wenruo }
425436c86a9eSQu Wenruo ASSERT(!found);
425536c86a9eSQu Wenruo }
425636c86a9eSQu Wenruo
close_ctree(struct btrfs_fs_info * fs_info)4257b105e927SDavid Sterba void __cold close_ctree(struct btrfs_fs_info *fs_info)
4258eb60ceacSChris Mason {
4259c146afadSYan Zheng int ret;
4260e089f05cSChris Mason
4261afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags);
426231e70e52SFilipe Manana
426331e70e52SFilipe Manana /*
42648a1f1e3dSFilipe Manana * If we had UNFINISHED_DROPS we could still be processing them, so
42658a1f1e3dSFilipe Manana * clear that bit and wake up relocation so it can stop.
42668a1f1e3dSFilipe Manana * We must do this before stopping the block group reclaim task, because
42678a1f1e3dSFilipe Manana * at btrfs_relocate_block_group() we wait for this bit, and after the
42688a1f1e3dSFilipe Manana * wait we stop with -EINTR if btrfs_fs_closing() returns non-zero - we
42698a1f1e3dSFilipe Manana * have just set BTRFS_FS_CLOSING_START, so btrfs_fs_closing() will
42708a1f1e3dSFilipe Manana * return 1.
42718a1f1e3dSFilipe Manana */
42728a1f1e3dSFilipe Manana btrfs_wake_unfinished_drop(fs_info);
42738a1f1e3dSFilipe Manana
42748a1f1e3dSFilipe Manana /*
427531e70e52SFilipe Manana * We may have the reclaim task running and relocating a data block group,
427631e70e52SFilipe Manana * in which case it may create delayed iputs. So stop it before we park
427731e70e52SFilipe Manana * the cleaner kthread otherwise we can get new delayed iputs after
427831e70e52SFilipe Manana * parking the cleaner, and that can make the async reclaim task to hang
427931e70e52SFilipe Manana * if it's waiting for delayed iputs to complete, since the cleaner is
428031e70e52SFilipe Manana * parked and can not run delayed iputs - this will make us hang when
428131e70e52SFilipe Manana * trying to stop the async reclaim task.
428231e70e52SFilipe Manana */
428331e70e52SFilipe Manana cancel_work_sync(&fs_info->reclaim_bgs_work);
4284d6fd0ae2SOmar Sandoval /*
4285d6fd0ae2SOmar Sandoval * We don't want the cleaner to start new transactions, add more delayed
4286d6fd0ae2SOmar Sandoval * iputs, etc. while we're closing. We can't use kthread_stop() yet
4287d6fd0ae2SOmar Sandoval * because that frees the task_struct, and the transaction kthread might
4288d6fd0ae2SOmar Sandoval * still try to wake up the cleaner.
4289d6fd0ae2SOmar Sandoval */
4290d6fd0ae2SOmar Sandoval kthread_park(fs_info->cleaner_kthread);
4291a2135011SChris Mason
42927343dd61SJustin Maggard /* wait for the qgroup rescan worker to stop */
4293d06f23d6SJeff Mahoney btrfs_qgroup_wait_for_completion(fs_info, false);
42947343dd61SJustin Maggard
4295803b2f54SStefan Behrens /* wait for the uuid_scan task to finish */
4296803b2f54SStefan Behrens down(&fs_info->uuid_tree_rescan_sem);
4297803b2f54SStefan Behrens /* avoid complains from lockdep et al., set sem back to initial state */
4298803b2f54SStefan Behrens up(&fs_info->uuid_tree_rescan_sem);
4299803b2f54SStefan Behrens
4300837d5b6eSIlya Dryomov /* pause restriper - we want to resume on mount */
4301aa1b8cd4SStefan Behrens btrfs_pause_balance(fs_info);
4302837d5b6eSIlya Dryomov
43038dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info);
43048dabb742SStefan Behrens
4305aa1b8cd4SStefan Behrens btrfs_scrub_cancel(fs_info);
43064cb5300bSChris Mason
43074cb5300bSChris Mason /* wait for any defraggers to finish */
43084cb5300bSChris Mason wait_event(fs_info->transaction_wait,
43094cb5300bSChris Mason (atomic_read(&fs_info->defrag_running) == 0));
43104cb5300bSChris Mason
43114cb5300bSChris Mason /* clear out the rbtree of defraggable inodes */
431226176e7cSMiao Xie btrfs_cleanup_defrag_inodes(fs_info);
43134cb5300bSChris Mason
4314a362bb86SFilipe Manana /*
4315a362bb86SFilipe Manana * After we parked the cleaner kthread, ordered extents may have
4316a362bb86SFilipe Manana * completed and created new delayed iputs. If one of the async reclaim
4317a362bb86SFilipe Manana * tasks is running and in the RUN_DELAYED_IPUTS flush state, then we
4318a362bb86SFilipe Manana * can hang forever trying to stop it, because if a delayed iput is
4319a362bb86SFilipe Manana * added after it ran btrfs_run_delayed_iputs() and before it called
4320a362bb86SFilipe Manana * btrfs_wait_on_delayed_iputs(), it will hang forever since there is
4321a362bb86SFilipe Manana * no one else to run iputs.
4322a362bb86SFilipe Manana *
4323a362bb86SFilipe Manana * So wait for all ongoing ordered extents to complete and then run
4324a362bb86SFilipe Manana * delayed iputs. This works because once we reach this point no one
4325a362bb86SFilipe Manana * can either create new ordered extents nor create delayed iputs
4326a362bb86SFilipe Manana * through some other means.
4327a362bb86SFilipe Manana *
4328a362bb86SFilipe Manana * Also note that btrfs_wait_ordered_roots() is not safe here, because
4329a362bb86SFilipe Manana * it waits for BTRFS_ORDERED_COMPLETE to be set on an ordered extent,
4330a362bb86SFilipe Manana * but the delayed iput for the respective inode is made only when doing
4331a362bb86SFilipe Manana * the final btrfs_put_ordered_extent() (which must happen at
4332a362bb86SFilipe Manana * btrfs_finish_ordered_io() when we are unmounting).
4333a362bb86SFilipe Manana */
4334a362bb86SFilipe Manana btrfs_flush_workqueue(fs_info->endio_write_workers);
4335a362bb86SFilipe Manana /* Ordered extents for free space inodes. */
4336a362bb86SFilipe Manana btrfs_flush_workqueue(fs_info->endio_freespace_worker);
4337a362bb86SFilipe Manana btrfs_run_delayed_iputs(fs_info);
4338a362bb86SFilipe Manana
433921c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work);
434057056740SJosef Bacik cancel_work_sync(&fs_info->async_data_reclaim_work);
4341576fa348SJosef Bacik cancel_work_sync(&fs_info->preempt_reclaim_work);
434221c7e756SMiao Xie
4343b0643e59SDennis Zhou /* Cancel or finish ongoing discard work */
4344b0643e59SDennis Zhou btrfs_discard_cleanup(fs_info);
4345b0643e59SDennis Zhou
4346bc98a42cSDavid Howells if (!sb_rdonly(fs_info->sb)) {
4347e44163e1SJeff Mahoney /*
4348d6fd0ae2SOmar Sandoval * The cleaner kthread is stopped, so do one final pass over
4349d6fd0ae2SOmar Sandoval * unused block groups.
4350e44163e1SJeff Mahoney */
43510b246afaSJeff Mahoney btrfs_delete_unused_bgs(fs_info);
4352e44163e1SJeff Mahoney
4353f0cc2cd7SFilipe Manana /*
4354f0cc2cd7SFilipe Manana * There might be existing delayed inode workers still running
4355f0cc2cd7SFilipe Manana * and holding an empty delayed inode item. We must wait for
4356f0cc2cd7SFilipe Manana * them to complete first because they can create a transaction.
4357f0cc2cd7SFilipe Manana * This happens when someone calls btrfs_balance_delayed_items()
4358f0cc2cd7SFilipe Manana * and then a transaction commit runs the same delayed nodes
4359f0cc2cd7SFilipe Manana * before any delayed worker has done something with the nodes.
4360f0cc2cd7SFilipe Manana * We must wait for any worker here and not at transaction
4361f0cc2cd7SFilipe Manana * commit time since that could cause a deadlock.
4362f0cc2cd7SFilipe Manana * This is a very rare case.
4363f0cc2cd7SFilipe Manana */
4364f0cc2cd7SFilipe Manana btrfs_flush_workqueue(fs_info->delayed_workers);
4365f0cc2cd7SFilipe Manana
43666bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info);
4367d397712bSChris Mason if (ret)
436804892340SEric Sandeen btrfs_err(fs_info, "commit super ret %d", ret);
4369c146afadSYan Zheng }
4370ed2ff2cbSChris Mason
437184961539SJosef Bacik if (BTRFS_FS_ERROR(fs_info))
43722ff7e61eSJeff Mahoney btrfs_error_commit_super(fs_info);
4373acce952bSliubo
4374e3029d9fSAl Viro kthread_stop(fs_info->transaction_kthread);
4375e3029d9fSAl Viro kthread_stop(fs_info->cleaner_kthread);
43768929ecfaSYan, Zheng
4377e187831eSJosef Bacik ASSERT(list_empty(&fs_info->delayed_iputs));
4378afcdd129SJosef Bacik set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags);
4379f25784b3SYan Zheng
43805958253cSQu Wenruo if (btrfs_check_quota_leak(fs_info)) {
43815958253cSQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
43825958253cSQu Wenruo btrfs_err(fs_info, "qgroup reserved space leaked");
43835958253cSQu Wenruo }
43845958253cSQu Wenruo
438504892340SEric Sandeen btrfs_free_qgroup_config(fs_info);
4386fe816d0fSNikolay Borisov ASSERT(list_empty(&fs_info->delalloc_roots));
4387bcef60f2SArne Jansen
4388963d678bSMiao Xie if (percpu_counter_sum(&fs_info->delalloc_bytes)) {
438904892340SEric Sandeen btrfs_info(fs_info, "at unmount delalloc count %lld",
4390963d678bSMiao Xie percpu_counter_sum(&fs_info->delalloc_bytes));
4391b0c68f8bSChris Mason }
439231153d81SYan Zheng
43935deb17e1SJosef Bacik if (percpu_counter_sum(&fs_info->ordered_bytes))
43944297ff84SJosef Bacik btrfs_info(fs_info, "at unmount dio bytes count %lld",
43955deb17e1SJosef Bacik percpu_counter_sum(&fs_info->ordered_bytes));
43964297ff84SJosef Bacik
43976618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info);
4398b7c35e81SAnand Jain btrfs_sysfs_remove_fsid(fs_info->fs_devices);
43995ac1d209SJeff Mahoney
44001a4319ccSLiu Bo btrfs_put_block_group_cache(fs_info);
44011a4319ccSLiu Bo
4402de348ee0SWang Shilong /*
4403de348ee0SWang Shilong * we must make sure there is not any read request to
4404de348ee0SWang Shilong * submit after we stopping all workers.
4405de348ee0SWang Shilong */
4406de348ee0SWang Shilong invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
440796192499SJosef Bacik btrfs_stop_all_workers(fs_info);
440896192499SJosef Bacik
44090a31daa4SFilipe Manana /* We shouldn't have any transaction open at this point */
441036c86a9eSQu Wenruo warn_about_uncommitted_trans(fs_info);
44110a31daa4SFilipe Manana
4412afcdd129SJosef Bacik clear_bit(BTRFS_FS_OPEN, &fs_info->flags);
44134273eaffSAnand Jain free_root_pointers(fs_info, true);
44148c38938cSJosef Bacik btrfs_free_fs_roots(fs_info);
44159ad6b7bcSChris Mason
44164e19443dSJosef Bacik /*
44174e19443dSJosef Bacik * We must free the block groups after dropping the fs_roots as we could
44184e19443dSJosef Bacik * have had an IO error and have left over tree log blocks that aren't
44194e19443dSJosef Bacik * cleaned up until the fs roots are freed. This makes the block group
44204e19443dSJosef Bacik * accounting appear to be wrong because there's pending reserved bytes,
44214e19443dSJosef Bacik * so make sure we do the block group cleanup afterwards.
44224e19443dSJosef Bacik */
44234e19443dSJosef Bacik btrfs_free_block_groups(fs_info);
44244e19443dSJosef Bacik
442513e6c37bSJosef Bacik iput(fs_info->btree_inode);
4426d6bfde87SChris Mason
442721adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
44280b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, CHECK_INTEGRITY))
44292ff7e61eSJeff Mahoney btrfsic_unmount(fs_info->fs_devices);
443021adbd5cSStefan Behrens #endif
443121adbd5cSStefan Behrens
44320b86a832SChris Mason btrfs_mapping_tree_free(&fs_info->mapping_tree);
443368c94e55SNikolay Borisov btrfs_close_devices(fs_info->fs_devices);
4434eb60ceacSChris Mason }
4435eb60ceacSChris Mason
btrfs_mark_buffer_dirty(struct btrfs_trans_handle * trans,struct extent_buffer * buf)4436d5e09e38SFilipe Manana void btrfs_mark_buffer_dirty(struct btrfs_trans_handle *trans,
4437d5e09e38SFilipe Manana struct extent_buffer *buf)
44385f39d397SChris Mason {
44392f4d60dfSQu Wenruo struct btrfs_fs_info *fs_info = buf->fs_info;
44405f39d397SChris Mason u64 transid = btrfs_header_generation(buf);
4441b4ce94deSChris Mason
444206ea65a3SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
444306ea65a3SJosef Bacik /*
444406ea65a3SJosef Bacik * This is a fast path so only do this check if we have sanity tests
444552042d8eSAndrea Gelmini * enabled. Normal people shouldn't be using unmapped buffers as dirty
444606ea65a3SJosef Bacik * outside of the sanity tests.
444706ea65a3SJosef Bacik */
4448b0132a3bSNikolay Borisov if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags)))
444906ea65a3SJosef Bacik return;
445006ea65a3SJosef Bacik #endif
4451d5e09e38SFilipe Manana /* This is an active transaction (its state < TRANS_STATE_UNBLOCKED). */
4452d5e09e38SFilipe Manana ASSERT(trans->transid == fs_info->generation);
445349d0c642SFilipe Manana btrfs_assert_tree_write_locked(buf);
4454d5e09e38SFilipe Manana if (transid != fs_info->generation) {
44555d163e0eSJeff Mahoney WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n",
44560b246afaSJeff Mahoney buf->start, transid, fs_info->generation);
4457d5e09e38SFilipe Manana btrfs_abort_transaction(trans, -EUCLEAN);
4458d5e09e38SFilipe Manana }
4459f18cc978SChristoph Hellwig set_extent_buffer_dirty(buf);
44601f21ef0aSFilipe Manana #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
446169fc6cbbSQu Wenruo /*
446285d8a826SJosef Bacik * btrfs_check_leaf() won't check item data if we don't have WRITTEN
446385d8a826SJosef Bacik * set, so this will only validate the basic structure of the items.
446469fc6cbbSQu Wenruo */
446585d8a826SJosef Bacik if (btrfs_header_level(buf) == 0 && btrfs_check_leaf(buf)) {
4466a4f78750SDavid Sterba btrfs_print_leaf(buf);
44671f21ef0aSFilipe Manana ASSERT(0);
44681f21ef0aSFilipe Manana }
44691f21ef0aSFilipe Manana #endif
4470eb60ceacSChris Mason }
4471eb60ceacSChris Mason
__btrfs_btree_balance_dirty(struct btrfs_fs_info * fs_info,int flush_delayed)44722ff7e61eSJeff Mahoney static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info,
4473b53d3f5dSLiu Bo int flush_delayed)
447435b7e476SChris Mason {
4475188de649SChris Mason /*
4476188de649SChris Mason * looks as though older kernels can get into trouble with
4477188de649SChris Mason * this code, they end up stuck in balance_dirty_pages forever
4478188de649SChris Mason */
4479e2d84521SMiao Xie int ret;
4480d6bfde87SChris Mason
44816933c02eSJens Axboe if (current->flags & PF_MEMALLOC)
4482d6bfde87SChris Mason return;
4483d6bfde87SChris Mason
4484b53d3f5dSLiu Bo if (flush_delayed)
44852ff7e61eSJeff Mahoney btrfs_balance_delayed_items(fs_info);
448616cdcec7SMiao Xie
4487d814a491SEthan Lien ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes,
4488d814a491SEthan Lien BTRFS_DIRTY_METADATA_THRESH,
4489d814a491SEthan Lien fs_info->dirty_metadata_batch);
4490e2d84521SMiao Xie if (ret > 0) {
44910b246afaSJeff Mahoney balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping);
449216cdcec7SMiao Xie }
449316cdcec7SMiao Xie }
449416cdcec7SMiao Xie
btrfs_btree_balance_dirty(struct btrfs_fs_info * fs_info)44952ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info)
449616cdcec7SMiao Xie {
44972ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 1);
449835b7e476SChris Mason }
4499b53d3f5dSLiu Bo
btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info * fs_info)45002ff7e61eSJeff Mahoney void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info)
4501b53d3f5dSLiu Bo {
45022ff7e61eSJeff Mahoney __btrfs_btree_balance_dirty(fs_info, 0);
4503d6bfde87SChris Mason }
45046b80053dSChris Mason
btrfs_error_commit_super(struct btrfs_fs_info * fs_info)45052ff7e61eSJeff Mahoney static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info)
4506acce952bSliubo {
4507fe816d0fSNikolay Borisov /* cleanup FS via transaction */
4508fe816d0fSNikolay Borisov btrfs_cleanup_transaction(fs_info);
4509fe816d0fSNikolay Borisov
45100b246afaSJeff Mahoney mutex_lock(&fs_info->cleaner_mutex);
45112ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info);
45120b246afaSJeff Mahoney mutex_unlock(&fs_info->cleaner_mutex);
4513acce952bSliubo
45140b246afaSJeff Mahoney down_write(&fs_info->cleanup_work_sem);
45150b246afaSJeff Mahoney up_write(&fs_info->cleanup_work_sem);
4516acce952bSliubo }
4517acce952bSliubo
btrfs_drop_all_logs(struct btrfs_fs_info * fs_info)4518ef67963dSJosef Bacik static void btrfs_drop_all_logs(struct btrfs_fs_info *fs_info)
4519ef67963dSJosef Bacik {
4520fc7cbcd4SDavid Sterba struct btrfs_root *gang[8];
4521fc7cbcd4SDavid Sterba u64 root_objectid = 0;
4522fc7cbcd4SDavid Sterba int ret;
4523ef67963dSJosef Bacik
4524fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock);
4525fc7cbcd4SDavid Sterba while ((ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
4526fc7cbcd4SDavid Sterba (void **)gang, root_objectid,
4527fc7cbcd4SDavid Sterba ARRAY_SIZE(gang))) != 0) {
4528fc7cbcd4SDavid Sterba int i;
4529ef67963dSJosef Bacik
4530fc7cbcd4SDavid Sterba for (i = 0; i < ret; i++)
4531fc7cbcd4SDavid Sterba gang[i] = btrfs_grab_root(gang[i]);
4532fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock);
4533fc7cbcd4SDavid Sterba
4534fc7cbcd4SDavid Sterba for (i = 0; i < ret; i++) {
4535fc7cbcd4SDavid Sterba if (!gang[i])
4536ef67963dSJosef Bacik continue;
4537fc7cbcd4SDavid Sterba root_objectid = gang[i]->root_key.objectid;
4538fc7cbcd4SDavid Sterba btrfs_free_log(NULL, gang[i]);
4539fc7cbcd4SDavid Sterba btrfs_put_root(gang[i]);
4540ef67963dSJosef Bacik }
4541fc7cbcd4SDavid Sterba root_objectid++;
4542fc7cbcd4SDavid Sterba spin_lock(&fs_info->fs_roots_radix_lock);
4543ef67963dSJosef Bacik }
4544fc7cbcd4SDavid Sterba spin_unlock(&fs_info->fs_roots_radix_lock);
4545ef67963dSJosef Bacik btrfs_free_log_root_tree(NULL, fs_info);
4546ef67963dSJosef Bacik }
4547ef67963dSJosef Bacik
btrfs_destroy_ordered_extents(struct btrfs_root * root)4548143bede5SJeff Mahoney static void btrfs_destroy_ordered_extents(struct btrfs_root *root)
4549acce952bSliubo {
4550acce952bSliubo struct btrfs_ordered_extent *ordered;
4551acce952bSliubo
4552199c2a9cSMiao Xie spin_lock(&root->ordered_extent_lock);
4553779880efSJosef Bacik /*
4554779880efSJosef Bacik * This will just short circuit the ordered completion stuff which will
4555779880efSJosef Bacik * make sure the ordered extent gets properly cleaned up.
4556779880efSJosef Bacik */
4557199c2a9cSMiao Xie list_for_each_entry(ordered, &root->ordered_extents,
4558779880efSJosef Bacik root_extent_list)
4559779880efSJosef Bacik set_bit(BTRFS_ORDERED_IOERR, &ordered->flags);
4560199c2a9cSMiao Xie spin_unlock(&root->ordered_extent_lock);
4561199c2a9cSMiao Xie }
4562199c2a9cSMiao Xie
btrfs_destroy_all_ordered_extents(struct btrfs_fs_info * fs_info)4563199c2a9cSMiao Xie static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info)
4564199c2a9cSMiao Xie {
4565199c2a9cSMiao Xie struct btrfs_root *root;
456684af994bSRuan Jinjie LIST_HEAD(splice);
4567199c2a9cSMiao Xie
4568199c2a9cSMiao Xie spin_lock(&fs_info->ordered_root_lock);
4569199c2a9cSMiao Xie list_splice_init(&fs_info->ordered_roots, &splice);
4570199c2a9cSMiao Xie while (!list_empty(&splice)) {
4571199c2a9cSMiao Xie root = list_first_entry(&splice, struct btrfs_root,
4572199c2a9cSMiao Xie ordered_root);
45731de2cfdeSJosef Bacik list_move_tail(&root->ordered_root,
45741de2cfdeSJosef Bacik &fs_info->ordered_roots);
4575199c2a9cSMiao Xie
45762a85d9caSLiu Bo spin_unlock(&fs_info->ordered_root_lock);
4577199c2a9cSMiao Xie btrfs_destroy_ordered_extents(root);
4578199c2a9cSMiao Xie
45792a85d9caSLiu Bo cond_resched();
45802a85d9caSLiu Bo spin_lock(&fs_info->ordered_root_lock);
4581199c2a9cSMiao Xie }
4582199c2a9cSMiao Xie spin_unlock(&fs_info->ordered_root_lock);
458374d5d229SJosef Bacik
458474d5d229SJosef Bacik /*
458574d5d229SJosef Bacik * We need this here because if we've been flipped read-only we won't
458674d5d229SJosef Bacik * get sync() from the umount, so we need to make sure any ordered
458774d5d229SJosef Bacik * extents that haven't had their dirty pages IO start writeout yet
458874d5d229SJosef Bacik * actually get run and error out properly.
458974d5d229SJosef Bacik */
459074d5d229SJosef Bacik btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
4591acce952bSliubo }
4592acce952bSliubo
btrfs_destroy_delayed_refs(struct btrfs_transaction * trans,struct btrfs_fs_info * fs_info)459399f09ce3SFilipe Manana static void btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
45942ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info)
4595acce952bSliubo {
4596acce952bSliubo struct rb_node *node;
4597acce952bSliubo struct btrfs_delayed_ref_root *delayed_refs;
4598acce952bSliubo struct btrfs_delayed_ref_node *ref;
4599acce952bSliubo
4600acce952bSliubo delayed_refs = &trans->delayed_refs;
4601acce952bSliubo
4602acce952bSliubo spin_lock(&delayed_refs->lock);
4603d7df2c79SJosef Bacik if (atomic_read(&delayed_refs->num_entries) == 0) {
4604cfece4dbSDavid Sterba spin_unlock(&delayed_refs->lock);
4605b79ce3ddSDavid Sterba btrfs_debug(fs_info, "delayed_refs has NO entry");
460699f09ce3SFilipe Manana return;
4607acce952bSliubo }
4608acce952bSliubo
46095c9d028bSLiu Bo while ((node = rb_first_cached(&delayed_refs->href_root)) != NULL) {
4610d7df2c79SJosef Bacik struct btrfs_delayed_ref_head *head;
46110e0adbcfSJosef Bacik struct rb_node *n;
4612e78417d1SJosef Bacik bool pin_bytes = false;
4613acce952bSliubo
4614d7df2c79SJosef Bacik head = rb_entry(node, struct btrfs_delayed_ref_head,
4615d7df2c79SJosef Bacik href_node);
46163069bd26SJosef Bacik if (btrfs_delayed_ref_lock(delayed_refs, head))
4617b939d1abSJosef Bacik continue;
46183069bd26SJosef Bacik
4619d7df2c79SJosef Bacik spin_lock(&head->lock);
4620e3d03965SLiu Bo while ((n = rb_first_cached(&head->ref_tree)) != NULL) {
46210e0adbcfSJosef Bacik ref = rb_entry(n, struct btrfs_delayed_ref_node,
46220e0adbcfSJosef Bacik ref_node);
4623e3d03965SLiu Bo rb_erase_cached(&ref->ref_node, &head->ref_tree);
46240e0adbcfSJosef Bacik RB_CLEAR_NODE(&ref->ref_node);
46251d57ee94SWang Xiaoguang if (!list_empty(&ref->add_list))
46261d57ee94SWang Xiaoguang list_del(&ref->add_list);
4627d7df2c79SJosef Bacik atomic_dec(&delayed_refs->num_entries);
4628d7df2c79SJosef Bacik btrfs_put_delayed_ref(ref);
4629d7df2c79SJosef Bacik }
463054067ae9SJosef Bacik if (head->must_insert_reserved)
4631e78417d1SJosef Bacik pin_bytes = true;
463278a6184aSMiao Xie btrfs_free_delayed_extent_op(head->extent_op);
4633fa781ceaSJosef Bacik btrfs_delete_ref_head(delayed_refs, head);
4634d7df2c79SJosef Bacik spin_unlock(&head->lock);
4635acce952bSliubo spin_unlock(&delayed_refs->lock);
4636e78417d1SJosef Bacik mutex_unlock(&head->mutex);
4637acce952bSliubo
4638f603bb94SNikolay Borisov if (pin_bytes) {
4639f603bb94SNikolay Borisov struct btrfs_block_group *cache;
4640f603bb94SNikolay Borisov
4641f603bb94SNikolay Borisov cache = btrfs_lookup_block_group(fs_info, head->bytenr);
4642f603bb94SNikolay Borisov BUG_ON(!cache);
4643f603bb94SNikolay Borisov
4644f603bb94SNikolay Borisov spin_lock(&cache->space_info->lock);
4645f603bb94SNikolay Borisov spin_lock(&cache->lock);
4646f603bb94SNikolay Borisov cache->pinned += head->num_bytes;
4647f603bb94SNikolay Borisov btrfs_space_info_update_bytes_pinned(fs_info,
4648f603bb94SNikolay Borisov cache->space_info, head->num_bytes);
4649f603bb94SNikolay Borisov cache->reserved -= head->num_bytes;
4650f603bb94SNikolay Borisov cache->space_info->bytes_reserved -= head->num_bytes;
4651f603bb94SNikolay Borisov spin_unlock(&cache->lock);
4652f603bb94SNikolay Borisov spin_unlock(&cache->space_info->lock);
4653f603bb94SNikolay Borisov
4654f603bb94SNikolay Borisov btrfs_put_block_group(cache);
4655f603bb94SNikolay Borisov
4656f603bb94SNikolay Borisov btrfs_error_unpin_extent_range(fs_info, head->bytenr,
4657f603bb94SNikolay Borisov head->bytenr + head->num_bytes - 1);
4658f603bb94SNikolay Borisov }
465931890da0SJosef Bacik btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head);
4660d278850eSJosef Bacik btrfs_put_delayed_ref_head(head);
4661acce952bSliubo cond_resched();
4662acce952bSliubo spin_lock(&delayed_refs->lock);
4663acce952bSliubo }
466481f7eb00SJeff Mahoney btrfs_qgroup_destroy_extent_records(trans);
4665acce952bSliubo
4666acce952bSliubo spin_unlock(&delayed_refs->lock);
4667acce952bSliubo }
4668acce952bSliubo
btrfs_destroy_delalloc_inodes(struct btrfs_root * root)4669143bede5SJeff Mahoney static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root)
4670acce952bSliubo {
4671acce952bSliubo struct btrfs_inode *btrfs_inode;
467284af994bSRuan Jinjie LIST_HEAD(splice);
4673acce952bSliubo
4674eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock);
4675eb73c1b7SMiao Xie list_splice_init(&root->delalloc_inodes, &splice);
4676acce952bSliubo
4677acce952bSliubo while (!list_empty(&splice)) {
4678fe816d0fSNikolay Borisov struct inode *inode = NULL;
4679eb73c1b7SMiao Xie btrfs_inode = list_first_entry(&splice, struct btrfs_inode,
4680acce952bSliubo delalloc_inodes);
4681fe816d0fSNikolay Borisov __btrfs_del_delalloc_inode(root, btrfs_inode);
4682eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock);
4683acce952bSliubo
4684fe816d0fSNikolay Borisov /*
4685fe816d0fSNikolay Borisov * Make sure we get a live inode and that it'll not disappear
4686fe816d0fSNikolay Borisov * meanwhile.
4687fe816d0fSNikolay Borisov */
4688fe816d0fSNikolay Borisov inode = igrab(&btrfs_inode->vfs_inode);
4689fe816d0fSNikolay Borisov if (inode) {
4690597441b3SJosef Bacik unsigned int nofs_flag;
4691597441b3SJosef Bacik
4692597441b3SJosef Bacik nofs_flag = memalloc_nofs_save();
4693fe816d0fSNikolay Borisov invalidate_inode_pages2(inode->i_mapping);
4694597441b3SJosef Bacik memalloc_nofs_restore(nofs_flag);
4695fe816d0fSNikolay Borisov iput(inode);
4696fe816d0fSNikolay Borisov }
4697eb73c1b7SMiao Xie spin_lock(&root->delalloc_lock);
4698acce952bSliubo }
4699eb73c1b7SMiao Xie spin_unlock(&root->delalloc_lock);
4700eb73c1b7SMiao Xie }
4701eb73c1b7SMiao Xie
btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info * fs_info)4702eb73c1b7SMiao Xie static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info)
4703eb73c1b7SMiao Xie {
4704eb73c1b7SMiao Xie struct btrfs_root *root;
470584af994bSRuan Jinjie LIST_HEAD(splice);
4706eb73c1b7SMiao Xie
4707eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock);
4708eb73c1b7SMiao Xie list_splice_init(&fs_info->delalloc_roots, &splice);
4709eb73c1b7SMiao Xie while (!list_empty(&splice)) {
4710eb73c1b7SMiao Xie root = list_first_entry(&splice, struct btrfs_root,
4711eb73c1b7SMiao Xie delalloc_root);
471200246528SJosef Bacik root = btrfs_grab_root(root);
4713eb73c1b7SMiao Xie BUG_ON(!root);
4714eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock);
4715eb73c1b7SMiao Xie
4716eb73c1b7SMiao Xie btrfs_destroy_delalloc_inodes(root);
471700246528SJosef Bacik btrfs_put_root(root);
4718eb73c1b7SMiao Xie
4719eb73c1b7SMiao Xie spin_lock(&fs_info->delalloc_root_lock);
4720eb73c1b7SMiao Xie }
4721eb73c1b7SMiao Xie spin_unlock(&fs_info->delalloc_root_lock);
4722acce952bSliubo }
4723acce952bSliubo
btrfs_destroy_marked_extents(struct btrfs_fs_info * fs_info,struct extent_io_tree * dirty_pages,int mark)4724aec5716cSFilipe Manana static void btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info,
4725acce952bSliubo struct extent_io_tree *dirty_pages,
4726acce952bSliubo int mark)
4727acce952bSliubo {
4728acce952bSliubo struct extent_buffer *eb;
4729acce952bSliubo u64 start = 0;
4730acce952bSliubo u64 end;
4731acce952bSliubo
4732e5860f82SFilipe Manana while (find_first_extent_bit(dirty_pages, start, &start, &end,
4733aec5716cSFilipe Manana mark, NULL)) {
473491166212SDavid Sterba clear_extent_bits(dirty_pages, start, end, mark);
4735acce952bSliubo while (start <= end) {
47360b246afaSJeff Mahoney eb = find_extent_buffer(fs_info, start);
47370b246afaSJeff Mahoney start += fs_info->nodesize;
4738fd8b2b61SJosef Bacik if (!eb)
4739acce952bSliubo continue;
4740acce952bSliubo
4741c4e54a65SJosef Bacik btrfs_tree_lock(eb);
4742c4e54a65SJosef Bacik wait_on_extent_buffer_writeback(eb);
4743190a8339SJosef Bacik btrfs_clear_buffer_dirty(NULL, eb);
4744c4e54a65SJosef Bacik btrfs_tree_unlock(eb);
4745c4e54a65SJosef Bacik
4746fd8b2b61SJosef Bacik free_extent_buffer_stale(eb);
4747acce952bSliubo }
4748acce952bSliubo }
4749acce952bSliubo }
4750acce952bSliubo
btrfs_destroy_pinned_extent(struct btrfs_fs_info * fs_info,struct extent_io_tree * unpin)475146d81ebdSFilipe Manana static void btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info,
4752fe119a6eSNikolay Borisov struct extent_io_tree *unpin)
4753acce952bSliubo {
4754acce952bSliubo u64 start;
4755acce952bSliubo u64 end;
4756acce952bSliubo
4757acce952bSliubo while (1) {
47580e6ec385SFilipe Manana struct extent_state *cached_state = NULL;
47590e6ec385SFilipe Manana
4760fcd5e742SLu Fengqi /*
4761fcd5e742SLu Fengqi * The btrfs_finish_extent_commit() may get the same range as
4762fcd5e742SLu Fengqi * ours between find_first_extent_bit and clear_extent_dirty.
4763fcd5e742SLu Fengqi * Hence, hold the unused_bg_unpin_mutex to avoid double unpin
4764fcd5e742SLu Fengqi * the same extent range.
4765fcd5e742SLu Fengqi */
4766fcd5e742SLu Fengqi mutex_lock(&fs_info->unused_bg_unpin_mutex);
4767e5860f82SFilipe Manana if (!find_first_extent_bit(unpin, 0, &start, &end,
476846d81ebdSFilipe Manana EXTENT_DIRTY, &cached_state)) {
4769fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex);
4770acce952bSliubo break;
4771fcd5e742SLu Fengqi }
4772acce952bSliubo
47730e6ec385SFilipe Manana clear_extent_dirty(unpin, start, end, &cached_state);
47740e6ec385SFilipe Manana free_extent_state(cached_state);
47752ff7e61eSJeff Mahoney btrfs_error_unpin_extent_range(fs_info, start, end);
4776fcd5e742SLu Fengqi mutex_unlock(&fs_info->unused_bg_unpin_mutex);
4777acce952bSliubo cond_resched();
4778acce952bSliubo }
4779acce952bSliubo }
4780acce952bSliubo
btrfs_cleanup_bg_io(struct btrfs_block_group * cache)478132da5386SDavid Sterba static void btrfs_cleanup_bg_io(struct btrfs_block_group *cache)
4782c79a1751SLiu Bo {
4783c79a1751SLiu Bo struct inode *inode;
4784c79a1751SLiu Bo
4785c79a1751SLiu Bo inode = cache->io_ctl.inode;
4786c79a1751SLiu Bo if (inode) {
4787597441b3SJosef Bacik unsigned int nofs_flag;
4788597441b3SJosef Bacik
4789597441b3SJosef Bacik nofs_flag = memalloc_nofs_save();
4790c79a1751SLiu Bo invalidate_inode_pages2(inode->i_mapping);
4791597441b3SJosef Bacik memalloc_nofs_restore(nofs_flag);
4792597441b3SJosef Bacik
4793c79a1751SLiu Bo BTRFS_I(inode)->generation = 0;
4794c79a1751SLiu Bo cache->io_ctl.inode = NULL;
4795c79a1751SLiu Bo iput(inode);
4796c79a1751SLiu Bo }
4797bbc37d6eSFilipe Manana ASSERT(cache->io_ctl.pages == NULL);
4798c79a1751SLiu Bo btrfs_put_block_group(cache);
4799c79a1751SLiu Bo }
4800c79a1751SLiu Bo
btrfs_cleanup_dirty_bgs(struct btrfs_transaction * cur_trans,struct btrfs_fs_info * fs_info)4801c79a1751SLiu Bo void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans,
48022ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info)
4803c79a1751SLiu Bo {
480432da5386SDavid Sterba struct btrfs_block_group *cache;
4805c79a1751SLiu Bo
4806c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock);
4807c79a1751SLiu Bo while (!list_empty(&cur_trans->dirty_bgs)) {
4808c79a1751SLiu Bo cache = list_first_entry(&cur_trans->dirty_bgs,
480932da5386SDavid Sterba struct btrfs_block_group,
4810c79a1751SLiu Bo dirty_list);
4811c79a1751SLiu Bo
4812c79a1751SLiu Bo if (!list_empty(&cache->io_list)) {
4813c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock);
4814c79a1751SLiu Bo list_del_init(&cache->io_list);
4815c79a1751SLiu Bo btrfs_cleanup_bg_io(cache);
4816c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock);
4817c79a1751SLiu Bo }
4818c79a1751SLiu Bo
4819c79a1751SLiu Bo list_del_init(&cache->dirty_list);
4820c79a1751SLiu Bo spin_lock(&cache->lock);
4821c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR;
4822c79a1751SLiu Bo spin_unlock(&cache->lock);
4823c79a1751SLiu Bo
4824c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock);
4825c79a1751SLiu Bo btrfs_put_block_group(cache);
4826ba2c4d4eSJosef Bacik btrfs_delayed_refs_rsv_release(fs_info, 1);
4827c79a1751SLiu Bo spin_lock(&cur_trans->dirty_bgs_lock);
4828c79a1751SLiu Bo }
4829c79a1751SLiu Bo spin_unlock(&cur_trans->dirty_bgs_lock);
4830c79a1751SLiu Bo
483145ae2c18SNikolay Borisov /*
483245ae2c18SNikolay Borisov * Refer to the definition of io_bgs member for details why it's safe
483345ae2c18SNikolay Borisov * to use it without any locking
483445ae2c18SNikolay Borisov */
4835c79a1751SLiu Bo while (!list_empty(&cur_trans->io_bgs)) {
4836c79a1751SLiu Bo cache = list_first_entry(&cur_trans->io_bgs,
483732da5386SDavid Sterba struct btrfs_block_group,
4838c79a1751SLiu Bo io_list);
4839c79a1751SLiu Bo
4840c79a1751SLiu Bo list_del_init(&cache->io_list);
4841c79a1751SLiu Bo spin_lock(&cache->lock);
4842c79a1751SLiu Bo cache->disk_cache_state = BTRFS_DC_ERROR;
4843c79a1751SLiu Bo spin_unlock(&cache->lock);
4844c79a1751SLiu Bo btrfs_cleanup_bg_io(cache);
4845c79a1751SLiu Bo }
4846c79a1751SLiu Bo }
4847c79a1751SLiu Bo
btrfs_free_all_qgroup_pertrans(struct btrfs_fs_info * fs_info)4848624bc6f6SBoris Burkov static void btrfs_free_all_qgroup_pertrans(struct btrfs_fs_info *fs_info)
4849624bc6f6SBoris Burkov {
4850624bc6f6SBoris Burkov struct btrfs_root *gang[8];
4851624bc6f6SBoris Burkov int i;
4852624bc6f6SBoris Burkov int ret;
4853624bc6f6SBoris Burkov
4854624bc6f6SBoris Burkov spin_lock(&fs_info->fs_roots_radix_lock);
4855624bc6f6SBoris Burkov while (1) {
4856624bc6f6SBoris Burkov ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
4857624bc6f6SBoris Burkov (void **)gang, 0,
4858624bc6f6SBoris Burkov ARRAY_SIZE(gang),
4859624bc6f6SBoris Burkov BTRFS_ROOT_TRANS_TAG);
4860624bc6f6SBoris Burkov if (ret == 0)
4861624bc6f6SBoris Burkov break;
4862624bc6f6SBoris Burkov for (i = 0; i < ret; i++) {
4863624bc6f6SBoris Burkov struct btrfs_root *root = gang[i];
4864624bc6f6SBoris Burkov
4865624bc6f6SBoris Burkov btrfs_qgroup_free_meta_all_pertrans(root);
4866624bc6f6SBoris Burkov radix_tree_tag_clear(&fs_info->fs_roots_radix,
4867624bc6f6SBoris Burkov (unsigned long)root->root_key.objectid,
4868624bc6f6SBoris Burkov BTRFS_ROOT_TRANS_TAG);
4869624bc6f6SBoris Burkov }
4870624bc6f6SBoris Burkov }
4871624bc6f6SBoris Burkov spin_unlock(&fs_info->fs_roots_radix_lock);
4872624bc6f6SBoris Burkov }
4873624bc6f6SBoris Burkov
btrfs_cleanup_one_transaction(struct btrfs_transaction * cur_trans,struct btrfs_fs_info * fs_info)487449b25e05SJeff Mahoney void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
48752ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info)
487649b25e05SJeff Mahoney {
4877bbbf7243SNikolay Borisov struct btrfs_device *dev, *tmp;
4878bbbf7243SNikolay Borisov
48792ff7e61eSJeff Mahoney btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
4880c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->dirty_bgs));
4881c79a1751SLiu Bo ASSERT(list_empty(&cur_trans->io_bgs));
4882c79a1751SLiu Bo
4883bbbf7243SNikolay Borisov list_for_each_entry_safe(dev, tmp, &cur_trans->dev_update_list,
4884bbbf7243SNikolay Borisov post_commit_list) {
4885bbbf7243SNikolay Borisov list_del_init(&dev->post_commit_list);
4886bbbf7243SNikolay Borisov }
4887bbbf7243SNikolay Borisov
48882ff7e61eSJeff Mahoney btrfs_destroy_delayed_refs(cur_trans, fs_info);
488949b25e05SJeff Mahoney
48904a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START;
48910b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait);
489249b25e05SJeff Mahoney
48934a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED;
48940b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait);
489549b25e05SJeff Mahoney
4896ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info);
489767cde344SMiao Xie
48982ff7e61eSJeff Mahoney btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages,
489949b25e05SJeff Mahoney EXTENT_DIRTY);
4900fe119a6eSNikolay Borisov btrfs_destroy_pinned_extent(fs_info, &cur_trans->pinned_extents);
490149b25e05SJeff Mahoney
4902624bc6f6SBoris Burkov btrfs_free_all_qgroup_pertrans(fs_info);
4903624bc6f6SBoris Burkov
49044a9d8bdeSMiao Xie cur_trans->state =TRANS_STATE_COMPLETED;
49054a9d8bdeSMiao Xie wake_up(&cur_trans->commit_wait);
490649b25e05SJeff Mahoney }
490749b25e05SJeff Mahoney
btrfs_cleanup_transaction(struct btrfs_fs_info * fs_info)49082ff7e61eSJeff Mahoney static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info)
4909acce952bSliubo {
4910acce952bSliubo struct btrfs_transaction *t;
4911acce952bSliubo
49120b246afaSJeff Mahoney mutex_lock(&fs_info->transaction_kthread_mutex);
4913acce952bSliubo
49140b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock);
49150b246afaSJeff Mahoney while (!list_empty(&fs_info->trans_list)) {
49160b246afaSJeff Mahoney t = list_first_entry(&fs_info->trans_list,
4917724e2315SJosef Bacik struct btrfs_transaction, list);
491877d20c68SJosef Bacik if (t->state >= TRANS_STATE_COMMIT_PREP) {
49199b64f57dSElena Reshetova refcount_inc(&t->use_count);
49200b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock);
49212ff7e61eSJeff Mahoney btrfs_wait_for_commit(fs_info, t->transid);
4922724e2315SJosef Bacik btrfs_put_transaction(t);
49230b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock);
4924724e2315SJosef Bacik continue;
4925724e2315SJosef Bacik }
49260b246afaSJeff Mahoney if (t == fs_info->running_transaction) {
4927724e2315SJosef Bacik t->state = TRANS_STATE_COMMIT_DOING;
49280b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock);
4929724e2315SJosef Bacik /*
4930724e2315SJosef Bacik * We wait for 0 num_writers since we don't hold a trans
4931724e2315SJosef Bacik * handle open currently for this transaction.
4932724e2315SJosef Bacik */
4933724e2315SJosef Bacik wait_event(t->writer_wait,
4934724e2315SJosef Bacik atomic_read(&t->num_writers) == 0);
4935724e2315SJosef Bacik } else {
49360b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock);
4937724e2315SJosef Bacik }
49382ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(t, fs_info);
4939724e2315SJosef Bacik
49400b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock);
49410b246afaSJeff Mahoney if (t == fs_info->running_transaction)
49420b246afaSJeff Mahoney fs_info->running_transaction = NULL;
4943724e2315SJosef Bacik list_del_init(&t->list);
49440b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock);
4945a4abeea4SJosef Bacik
4946724e2315SJosef Bacik btrfs_put_transaction(t);
49472e4e97abSJosef Bacik trace_btrfs_transaction_commit(fs_info);
49480b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock);
4949724e2315SJosef Bacik }
49500b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock);
49510b246afaSJeff Mahoney btrfs_destroy_all_ordered_extents(fs_info);
4952ccdf9b30SJeff Mahoney btrfs_destroy_delayed_inodes(fs_info);
4953ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info);
49540b246afaSJeff Mahoney btrfs_destroy_all_delalloc_inodes(fs_info);
4955ef67963dSJosef Bacik btrfs_drop_all_logs(fs_info);
49560b246afaSJeff Mahoney mutex_unlock(&fs_info->transaction_kthread_mutex);
4957acce952bSliubo
4958acce952bSliubo return 0;
4959acce952bSliubo }
4960ec7d6dfdSNikolay Borisov
btrfs_init_root_free_objectid(struct btrfs_root * root)4961453e4873SNikolay Borisov int btrfs_init_root_free_objectid(struct btrfs_root *root)
4962ec7d6dfdSNikolay Borisov {
4963ec7d6dfdSNikolay Borisov struct btrfs_path *path;
4964ec7d6dfdSNikolay Borisov int ret;
4965ec7d6dfdSNikolay Borisov struct extent_buffer *l;
4966ec7d6dfdSNikolay Borisov struct btrfs_key search_key;
4967ec7d6dfdSNikolay Borisov struct btrfs_key found_key;
4968ec7d6dfdSNikolay Borisov int slot;
4969ec7d6dfdSNikolay Borisov
4970ec7d6dfdSNikolay Borisov path = btrfs_alloc_path();
4971ec7d6dfdSNikolay Borisov if (!path)
4972ec7d6dfdSNikolay Borisov return -ENOMEM;
4973ec7d6dfdSNikolay Borisov
4974ec7d6dfdSNikolay Borisov search_key.objectid = BTRFS_LAST_FREE_OBJECTID;
4975ec7d6dfdSNikolay Borisov search_key.type = -1;
4976ec7d6dfdSNikolay Borisov search_key.offset = (u64)-1;
4977ec7d6dfdSNikolay Borisov ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
4978ec7d6dfdSNikolay Borisov if (ret < 0)
4979ec7d6dfdSNikolay Borisov goto error;
4980ec7d6dfdSNikolay Borisov BUG_ON(ret == 0); /* Corruption */
4981ec7d6dfdSNikolay Borisov if (path->slots[0] > 0) {
4982ec7d6dfdSNikolay Borisov slot = path->slots[0] - 1;
4983ec7d6dfdSNikolay Borisov l = path->nodes[0];
4984ec7d6dfdSNikolay Borisov btrfs_item_key_to_cpu(l, &found_key, slot);
498523125104SNikolay Borisov root->free_objectid = max_t(u64, found_key.objectid + 1,
498623125104SNikolay Borisov BTRFS_FIRST_FREE_OBJECTID);
4987ec7d6dfdSNikolay Borisov } else {
498823125104SNikolay Borisov root->free_objectid = BTRFS_FIRST_FREE_OBJECTID;
4989ec7d6dfdSNikolay Borisov }
4990ec7d6dfdSNikolay Borisov ret = 0;
4991ec7d6dfdSNikolay Borisov error:
4992ec7d6dfdSNikolay Borisov btrfs_free_path(path);
4993ec7d6dfdSNikolay Borisov return ret;
4994ec7d6dfdSNikolay Borisov }
4995ec7d6dfdSNikolay Borisov
btrfs_get_free_objectid(struct btrfs_root * root,u64 * objectid)4996543068a2SNikolay Borisov int btrfs_get_free_objectid(struct btrfs_root *root, u64 *objectid)
4997ec7d6dfdSNikolay Borisov {
4998ec7d6dfdSNikolay Borisov int ret;
4999ec7d6dfdSNikolay Borisov mutex_lock(&root->objectid_mutex);
5000ec7d6dfdSNikolay Borisov
50016b8fad57SNikolay Borisov if (unlikely(root->free_objectid >= BTRFS_LAST_FREE_OBJECTID)) {
5002ec7d6dfdSNikolay Borisov btrfs_warn(root->fs_info,
5003ec7d6dfdSNikolay Borisov "the objectid of root %llu reaches its highest value",
5004ec7d6dfdSNikolay Borisov root->root_key.objectid);
5005ec7d6dfdSNikolay Borisov ret = -ENOSPC;
5006ec7d6dfdSNikolay Borisov goto out;
5007ec7d6dfdSNikolay Borisov }
5008ec7d6dfdSNikolay Borisov
500923125104SNikolay Borisov *objectid = root->free_objectid++;
5010ec7d6dfdSNikolay Borisov ret = 0;
5011ec7d6dfdSNikolay Borisov out:
5012ec7d6dfdSNikolay Borisov mutex_unlock(&root->objectid_mutex);
5013ec7d6dfdSNikolay Borisov return ret;
5014ec7d6dfdSNikolay Borisov }
5015