xref: /linux/fs/btrfs/super.c (revision 2018ef1d9ac3e95448b9206adc3425b0431c2411)
1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0
26cbd5570SChris Mason /*
36cbd5570SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
46cbd5570SChris Mason  */
56cbd5570SChris Mason 
64b82d6e4SYan #include <linux/blkdev.h>
72e635a27SChris Mason #include <linux/module.h>
82e635a27SChris Mason #include <linux/fs.h>
92e635a27SChris Mason #include <linux/pagemap.h>
102e635a27SChris Mason #include <linux/highmem.h>
112e635a27SChris Mason #include <linux/time.h>
122e635a27SChris Mason #include <linux/init.h>
13a9572a15SEric Paris #include <linux/seq_file.h>
142e635a27SChris Mason #include <linux/string.h>
152e635a27SChris Mason #include <linux/backing-dev.h>
164b82d6e4SYan #include <linux/mount.h>
1775dfe396SChris Mason #include <linux/writeback.h>
188fd17795SChris Mason #include <linux/statfs.h>
1908607c1bSChris Mason #include <linux/compat.h>
2095e05289SChris Mason #include <linux/parser.h>
21c59f8951SChris Mason #include <linux/ctype.h>
226da6abaeSChris Mason #include <linux/namei.h>
23a9218f6bSChris Mason #include <linux/miscdevice.h>
241bcbf313SQinghuang Feng #include <linux/magic.h>
255a0e3ad6STejun Heo #include <linux/slab.h>
2622c44fe6SJosef Bacik #include <linux/ratelimit.h>
279678c543SNikolay Borisov #include <linux/crc32c.h>
2855e301fdSFilipe Brandenburger #include <linux/btrfs.h>
29c60a2880SJosef Bacik #include <linux/security.h>
3015ddcdd3SJosef Bacik #include <linux/fs_parser.h>
319b569ea0SJosef Bacik #include "messages.h"
3216cdcec7SMiao Xie #include "delayed-inode.h"
332e635a27SChris Mason #include "ctree.h"
34e20d96d6SChris Mason #include "disk-io.h"
35d5719762SChris Mason #include "transaction.h"
362c90e5d6SChris Mason #include "btrfs_inode.h"
373a686375SChris Mason #include "print-tree.h"
3863541927SFilipe David Borba Manana #include "props.h"
395103e947SJosef Bacik #include "xattr.h"
40103c1972SChristoph Hellwig #include "bio.h"
41be6e8dc0SBalaji Rao #include "export.h"
42c8b97818SChris Mason #include "compression.h"
439c5085c1SJosef Bacik #include "rcu-string.h"
448dabb742SStefan Behrens #include "dev-replace.h"
4574255aa0SJosef Bacik #include "free-space-cache.h"
46b9e9a6cbSWang Shilong #include "backref.h"
478719aaaeSJosef Bacik #include "space-info.h"
4889439109SDavid Sterba #include "sysfs.h"
49b70f5097SNaohiro Aota #include "zoned.h"
50dc11dd5dSJosef Bacik #include "tests/btrfs-tests.h"
51aac0023cSJosef Bacik #include "block-group.h"
52b0643e59SDennis Zhou #include "discard.h"
53d3982100SMark Fasheh #include "qgroup.h"
54b8bea09aSQu Wenruo #include "raid56.h"
55c7f13d42SJosef Bacik #include "fs.h"
5607e81dc9SJosef Bacik #include "accessors.h"
5759b818e0SJosef Bacik #include "defrag.h"
58f2b39277SJosef Bacik #include "dir-item.h"
597572dec8SJosef Bacik #include "ioctl.h"
602fc6822cSJosef Bacik #include "scrub.h"
615c11adccSJosef Bacik #include "verity.h"
62c03b2207SJosef Bacik #include "super.h"
63cfc2de0fSBoris Burkov #include "extent-tree.h"
641abe9b8aSliubo #define CREATE_TRACE_POINTS
651abe9b8aSliubo #include <trace/events/btrfs.h>
661abe9b8aSliubo 
67b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops;
68830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type;
690723a047SHarald Hoyer 
70e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb)
71e20d96d6SChris Mason {
722db31320SQu Wenruo 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
732db31320SQu Wenruo 
742db31320SQu Wenruo 	btrfs_info(fs_info, "last unmount of filesystem %pU", fs_info->fs_devices->fsid);
752db31320SQu Wenruo 	close_ctree(fs_info);
76e20d96d6SChris Mason }
772e635a27SChris Mason 
7817b36120SJosef Bacik /* Store the mount options related information. */
7917b36120SJosef Bacik struct btrfs_fs_context {
8017b36120SJosef Bacik 	char *subvol_name;
8117b36120SJosef Bacik 	u64 subvol_objectid;
8217b36120SJosef Bacik 	u64 max_inline;
8317b36120SJosef Bacik 	u32 commit_interval;
8417b36120SJosef Bacik 	u32 metadata_ratio;
8517b36120SJosef Bacik 	u32 thread_pool_size;
8617b36120SJosef Bacik 	unsigned long mount_opt;
8717b36120SJosef Bacik 	unsigned long compress_type:4;
8817b36120SJosef Bacik 	unsigned int compress_level;
893bb17a25SJosef Bacik 	refcount_t refs;
9017b36120SJosef Bacik };
9117b36120SJosef Bacik 
9295e05289SChris Mason enum {
936941823cSJosef Bacik 	Opt_acl,
94416a7202SDavid Sterba 	Opt_clear_cache,
95416a7202SDavid Sterba 	Opt_commit_interval,
96416a7202SDavid Sterba 	Opt_compress,
97416a7202SDavid Sterba 	Opt_compress_force,
98416a7202SDavid Sterba 	Opt_compress_force_type,
99416a7202SDavid Sterba 	Opt_compress_type,
100416a7202SDavid Sterba 	Opt_degraded,
101416a7202SDavid Sterba 	Opt_device,
102416a7202SDavid Sterba 	Opt_fatal_errors,
1036941823cSJosef Bacik 	Opt_flushoncommit,
104416a7202SDavid Sterba 	Opt_max_inline,
1056941823cSJosef Bacik 	Opt_barrier,
1066941823cSJosef Bacik 	Opt_datacow,
1076941823cSJosef Bacik 	Opt_datasum,
1086941823cSJosef Bacik 	Opt_defrag,
1096941823cSJosef Bacik 	Opt_discard,
110b0643e59SDennis Zhou 	Opt_discard_mode,
111416a7202SDavid Sterba 	Opt_ratio,
112416a7202SDavid Sterba 	Opt_rescan_uuid_tree,
113416a7202SDavid Sterba 	Opt_skip_balance,
1146941823cSJosef Bacik 	Opt_space_cache,
115416a7202SDavid Sterba 	Opt_space_cache_version,
1166941823cSJosef Bacik 	Opt_ssd,
1176941823cSJosef Bacik 	Opt_ssd_spread,
118416a7202SDavid Sterba 	Opt_subvol,
11937becec9SOmar Sandoval 	Opt_subvol_empty,
120416a7202SDavid Sterba 	Opt_subvolid,
121416a7202SDavid Sterba 	Opt_thread_pool,
1226941823cSJosef Bacik 	Opt_treelog,
123416a7202SDavid Sterba 	Opt_user_subvol_rm_allowed,
124416a7202SDavid Sterba 
12574ef0018SQu Wenruo 	/* Rescue options */
12674ef0018SQu Wenruo 	Opt_rescue,
12774ef0018SQu Wenruo 	Opt_usebackuproot,
12874ef0018SQu Wenruo 	Opt_nologreplay,
12942437a63SJosef Bacik 	Opt_ignorebadroots,
130882dbe0cSJosef Bacik 	Opt_ignoredatacsums,
1319037d3cbSJosef Bacik 	Opt_rescue_all,
13274ef0018SQu Wenruo 
133416a7202SDavid Sterba 	/* Debugging options */
1346941823cSJosef Bacik 	Opt_enospc_debug,
135d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
13615ddcdd3SJosef Bacik 	Opt_fragment, Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
137d0bd4560SJosef Bacik #endif
138fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
139fb592373SJosef Bacik 	Opt_ref_verify,
140fb592373SJosef Bacik #endif
1419555c6c1SIlya Dryomov 	Opt_err,
14295e05289SChris Mason };
14395e05289SChris Mason 
14415ddcdd3SJosef Bacik enum {
14515ddcdd3SJosef Bacik 	Opt_fatal_errors_panic,
14615ddcdd3SJosef Bacik 	Opt_fatal_errors_bug,
14715ddcdd3SJosef Bacik };
14815ddcdd3SJosef Bacik 
14915ddcdd3SJosef Bacik static const struct constant_table btrfs_parameter_fatal_errors[] = {
15015ddcdd3SJosef Bacik 	{ "panic", Opt_fatal_errors_panic },
15115ddcdd3SJosef Bacik 	{ "bug", Opt_fatal_errors_bug },
15215ddcdd3SJosef Bacik 	{}
15315ddcdd3SJosef Bacik };
15415ddcdd3SJosef Bacik 
15515ddcdd3SJosef Bacik enum {
15615ddcdd3SJosef Bacik 	Opt_discard_sync,
15715ddcdd3SJosef Bacik 	Opt_discard_async,
15815ddcdd3SJosef Bacik };
15915ddcdd3SJosef Bacik 
16015ddcdd3SJosef Bacik static const struct constant_table btrfs_parameter_discard[] = {
16115ddcdd3SJosef Bacik 	{ "sync", Opt_discard_sync },
16215ddcdd3SJosef Bacik 	{ "async", Opt_discard_async },
16315ddcdd3SJosef Bacik 	{}
16415ddcdd3SJosef Bacik };
16515ddcdd3SJosef Bacik 
16615ddcdd3SJosef Bacik enum {
16715ddcdd3SJosef Bacik 	Opt_space_cache_v1,
16815ddcdd3SJosef Bacik 	Opt_space_cache_v2,
16915ddcdd3SJosef Bacik };
17015ddcdd3SJosef Bacik 
17115ddcdd3SJosef Bacik static const struct constant_table btrfs_parameter_space_cache[] = {
17215ddcdd3SJosef Bacik 	{ "v1", Opt_space_cache_v1 },
17315ddcdd3SJosef Bacik 	{ "v2", Opt_space_cache_v2 },
17415ddcdd3SJosef Bacik 	{}
17515ddcdd3SJosef Bacik };
17615ddcdd3SJosef Bacik 
17715ddcdd3SJosef Bacik enum {
17815ddcdd3SJosef Bacik 	Opt_rescue_usebackuproot,
17915ddcdd3SJosef Bacik 	Opt_rescue_nologreplay,
18015ddcdd3SJosef Bacik 	Opt_rescue_ignorebadroots,
18115ddcdd3SJosef Bacik 	Opt_rescue_ignoredatacsums,
18215ddcdd3SJosef Bacik 	Opt_rescue_parameter_all,
18315ddcdd3SJosef Bacik };
18415ddcdd3SJosef Bacik 
18515ddcdd3SJosef Bacik static const struct constant_table btrfs_parameter_rescue[] = {
18615ddcdd3SJosef Bacik 	{ "usebackuproot", Opt_rescue_usebackuproot },
18715ddcdd3SJosef Bacik 	{ "nologreplay", Opt_rescue_nologreplay },
18815ddcdd3SJosef Bacik 	{ "ignorebadroots", Opt_rescue_ignorebadroots },
18915ddcdd3SJosef Bacik 	{ "ibadroots", Opt_rescue_ignorebadroots },
19015ddcdd3SJosef Bacik 	{ "ignoredatacsums", Opt_rescue_ignoredatacsums },
19115ddcdd3SJosef Bacik 	{ "idatacsums", Opt_rescue_ignoredatacsums },
19215ddcdd3SJosef Bacik 	{ "all", Opt_rescue_parameter_all },
19315ddcdd3SJosef Bacik 	{}
19415ddcdd3SJosef Bacik };
19515ddcdd3SJosef Bacik 
19615ddcdd3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
19715ddcdd3SJosef Bacik enum {
19815ddcdd3SJosef Bacik 	Opt_fragment_parameter_data,
19915ddcdd3SJosef Bacik 	Opt_fragment_parameter_metadata,
20015ddcdd3SJosef Bacik 	Opt_fragment_parameter_all,
20115ddcdd3SJosef Bacik };
20215ddcdd3SJosef Bacik 
20315ddcdd3SJosef Bacik static const struct constant_table btrfs_parameter_fragment[] = {
20415ddcdd3SJosef Bacik 	{ "data", Opt_fragment_parameter_data },
20515ddcdd3SJosef Bacik 	{ "metadata", Opt_fragment_parameter_metadata },
20615ddcdd3SJosef Bacik 	{ "all", Opt_fragment_parameter_all },
20715ddcdd3SJosef Bacik 	{}
20815ddcdd3SJosef Bacik };
20915ddcdd3SJosef Bacik #endif
21015ddcdd3SJosef Bacik 
211ad21f15bSJosef Bacik static const struct fs_parameter_spec btrfs_fs_parameters[] = {
21215ddcdd3SJosef Bacik 	fsparam_flag_no("acl", Opt_acl),
21315ddcdd3SJosef Bacik 	fsparam_flag_no("autodefrag", Opt_defrag),
21415ddcdd3SJosef Bacik 	fsparam_flag_no("barrier", Opt_barrier),
21515ddcdd3SJosef Bacik 	fsparam_flag("clear_cache", Opt_clear_cache),
21615ddcdd3SJosef Bacik 	fsparam_u32("commit", Opt_commit_interval),
21715ddcdd3SJosef Bacik 	fsparam_flag("compress", Opt_compress),
21815ddcdd3SJosef Bacik 	fsparam_string("compress", Opt_compress_type),
21915ddcdd3SJosef Bacik 	fsparam_flag("compress-force", Opt_compress_force),
22015ddcdd3SJosef Bacik 	fsparam_string("compress-force", Opt_compress_force_type),
22115ddcdd3SJosef Bacik 	fsparam_flag_no("datacow", Opt_datacow),
22215ddcdd3SJosef Bacik 	fsparam_flag_no("datasum", Opt_datasum),
22315ddcdd3SJosef Bacik 	fsparam_flag("degraded", Opt_degraded),
22415ddcdd3SJosef Bacik 	fsparam_string("device", Opt_device),
22515ddcdd3SJosef Bacik 	fsparam_flag_no("discard", Opt_discard),
22615ddcdd3SJosef Bacik 	fsparam_enum("discard", Opt_discard_mode, btrfs_parameter_discard),
22715ddcdd3SJosef Bacik 	fsparam_enum("fatal_errors", Opt_fatal_errors, btrfs_parameter_fatal_errors),
22815ddcdd3SJosef Bacik 	fsparam_flag_no("flushoncommit", Opt_flushoncommit),
22915ddcdd3SJosef Bacik 	fsparam_string("max_inline", Opt_max_inline),
23015ddcdd3SJosef Bacik 	fsparam_u32("metadata_ratio", Opt_ratio),
23115ddcdd3SJosef Bacik 	fsparam_flag("rescan_uuid_tree", Opt_rescan_uuid_tree),
23215ddcdd3SJosef Bacik 	fsparam_flag("skip_balance", Opt_skip_balance),
23315ddcdd3SJosef Bacik 	fsparam_flag_no("space_cache", Opt_space_cache),
23415ddcdd3SJosef Bacik 	fsparam_enum("space_cache", Opt_space_cache_version, btrfs_parameter_space_cache),
23515ddcdd3SJosef Bacik 	fsparam_flag_no("ssd", Opt_ssd),
23615ddcdd3SJosef Bacik 	fsparam_flag_no("ssd_spread", Opt_ssd_spread),
23715ddcdd3SJosef Bacik 	fsparam_string("subvol", Opt_subvol),
23815ddcdd3SJosef Bacik 	fsparam_flag("subvol=", Opt_subvol_empty),
23915ddcdd3SJosef Bacik 	fsparam_u64("subvolid", Opt_subvolid),
24015ddcdd3SJosef Bacik 	fsparam_u32("thread_pool", Opt_thread_pool),
24115ddcdd3SJosef Bacik 	fsparam_flag_no("treelog", Opt_treelog),
24215ddcdd3SJosef Bacik 	fsparam_flag("user_subvol_rm_allowed", Opt_user_subvol_rm_allowed),
24315ddcdd3SJosef Bacik 
24415ddcdd3SJosef Bacik 	/* Rescue options. */
24515ddcdd3SJosef Bacik 	fsparam_enum("rescue", Opt_rescue, btrfs_parameter_rescue),
24615ddcdd3SJosef Bacik 	/* Deprecated, with alias rescue=nologreplay */
24715ddcdd3SJosef Bacik 	__fsparam(NULL, "nologreplay", Opt_nologreplay, fs_param_deprecated, NULL),
24815ddcdd3SJosef Bacik 	/* Deprecated, with alias rescue=usebackuproot */
24915ddcdd3SJosef Bacik 	__fsparam(NULL, "usebackuproot", Opt_usebackuproot, fs_param_deprecated, NULL),
25015ddcdd3SJosef Bacik 
25115ddcdd3SJosef Bacik 	/* Debugging options. */
25215ddcdd3SJosef Bacik 	fsparam_flag_no("enospc_debug", Opt_enospc_debug),
25315ddcdd3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
25415ddcdd3SJosef Bacik 	fsparam_enum("fragment", Opt_fragment, btrfs_parameter_fragment),
25515ddcdd3SJosef Bacik #endif
25615ddcdd3SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
25715ddcdd3SJosef Bacik 	fsparam_flag("ref_verify", Opt_ref_verify),
25815ddcdd3SJosef Bacik #endif
25915ddcdd3SJosef Bacik 	{}
26015ddcdd3SJosef Bacik };
26115ddcdd3SJosef Bacik 
26217b36120SJosef Bacik static int btrfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
26317b36120SJosef Bacik {
26417b36120SJosef Bacik 	struct btrfs_fs_context *ctx = fc->fs_private;
26517b36120SJosef Bacik 	struct fs_parse_result result;
26617b36120SJosef Bacik 	int opt;
26717b36120SJosef Bacik 
26817b36120SJosef Bacik 	opt = fs_parse(fc, btrfs_fs_parameters, param, &result);
26917b36120SJosef Bacik 	if (opt < 0)
27017b36120SJosef Bacik 		return opt;
27117b36120SJosef Bacik 
27217b36120SJosef Bacik 	switch (opt) {
27317b36120SJosef Bacik 	case Opt_degraded:
27417b36120SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, DEGRADED);
27517b36120SJosef Bacik 		break;
27617b36120SJosef Bacik 	case Opt_subvol_empty:
27717b36120SJosef Bacik 		/*
27817b36120SJosef Bacik 		 * This exists because we used to allow it on accident, so we're
27917b36120SJosef Bacik 		 * keeping it to maintain ABI.  See 37becec95ac3 ("Btrfs: allow
28017b36120SJosef Bacik 		 * empty subvol= again").
28117b36120SJosef Bacik 		 */
28217b36120SJosef Bacik 		break;
28317b36120SJosef Bacik 	case Opt_subvol:
28417b36120SJosef Bacik 		kfree(ctx->subvol_name);
28517b36120SJosef Bacik 		ctx->subvol_name = kstrdup(param->string, GFP_KERNEL);
28617b36120SJosef Bacik 		if (!ctx->subvol_name)
28717b36120SJosef Bacik 			return -ENOMEM;
28817b36120SJosef Bacik 		break;
28917b36120SJosef Bacik 	case Opt_subvolid:
29017b36120SJosef Bacik 		ctx->subvol_objectid = result.uint_64;
29117b36120SJosef Bacik 
29217b36120SJosef Bacik 		/* subvolid=0 means give me the original fs_tree. */
29317b36120SJosef Bacik 		if (!ctx->subvol_objectid)
29417b36120SJosef Bacik 			ctx->subvol_objectid = BTRFS_FS_TREE_OBJECTID;
29517b36120SJosef Bacik 		break;
29617b36120SJosef Bacik 	case Opt_device: {
29717b36120SJosef Bacik 		struct btrfs_device *device;
29817b36120SJosef Bacik 		blk_mode_t mode = sb_open_mode(fc->sb_flags);
29917b36120SJosef Bacik 
30017b36120SJosef Bacik 		mutex_lock(&uuid_mutex);
30117b36120SJosef Bacik 		device = btrfs_scan_one_device(param->string, mode, false);
30217b36120SJosef Bacik 		mutex_unlock(&uuid_mutex);
30317b36120SJosef Bacik 		if (IS_ERR(device))
30417b36120SJosef Bacik 			return PTR_ERR(device);
30517b36120SJosef Bacik 		break;
30617b36120SJosef Bacik 	}
30717b36120SJosef Bacik 	case Opt_datasum:
30817b36120SJosef Bacik 		if (result.negated) {
30917b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NODATASUM);
31017b36120SJosef Bacik 		} else {
31117b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATACOW);
31217b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATASUM);
31317b36120SJosef Bacik 		}
31417b36120SJosef Bacik 		break;
31517b36120SJosef Bacik 	case Opt_datacow:
31617b36120SJosef Bacik 		if (result.negated) {
31717b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, COMPRESS);
31817b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
31917b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NODATACOW);
32017b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NODATASUM);
32117b36120SJosef Bacik 		} else {
32217b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATACOW);
32317b36120SJosef Bacik 		}
32417b36120SJosef Bacik 		break;
32517b36120SJosef Bacik 	case Opt_compress_force:
32617b36120SJosef Bacik 	case Opt_compress_force_type:
32717b36120SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, FORCE_COMPRESS);
32817b36120SJosef Bacik 		fallthrough;
32917b36120SJosef Bacik 	case Opt_compress:
33017b36120SJosef Bacik 	case Opt_compress_type:
33117b36120SJosef Bacik 		if (opt == Opt_compress || opt == Opt_compress_force) {
33217b36120SJosef Bacik 			ctx->compress_type = BTRFS_COMPRESS_ZLIB;
33317b36120SJosef Bacik 			ctx->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
33417b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, COMPRESS);
33517b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATACOW);
33617b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATASUM);
33717b36120SJosef Bacik 		} else if (strncmp(param->string, "zlib", 4) == 0) {
33817b36120SJosef Bacik 			ctx->compress_type = BTRFS_COMPRESS_ZLIB;
33917b36120SJosef Bacik 			ctx->compress_level =
34017b36120SJosef Bacik 				btrfs_compress_str2level(BTRFS_COMPRESS_ZLIB,
34117b36120SJosef Bacik 							 param->string + 4);
34217b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, COMPRESS);
34317b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATACOW);
34417b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATASUM);
34517b36120SJosef Bacik 		} else if (strncmp(param->string, "lzo", 3) == 0) {
34617b36120SJosef Bacik 			ctx->compress_type = BTRFS_COMPRESS_LZO;
34717b36120SJosef Bacik 			ctx->compress_level = 0;
34817b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, COMPRESS);
34917b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATACOW);
35017b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATASUM);
35117b36120SJosef Bacik 		} else if (strncmp(param->string, "zstd", 4) == 0) {
35217b36120SJosef Bacik 			ctx->compress_type = BTRFS_COMPRESS_ZSTD;
35317b36120SJosef Bacik 			ctx->compress_level =
35417b36120SJosef Bacik 				btrfs_compress_str2level(BTRFS_COMPRESS_ZSTD,
35517b36120SJosef Bacik 							 param->string + 4);
35617b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, COMPRESS);
35717b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATACOW);
35817b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATASUM);
35917b36120SJosef Bacik 		} else if (strncmp(param->string, "no", 2) == 0) {
36017b36120SJosef Bacik 			ctx->compress_level = 0;
36117b36120SJosef Bacik 			ctx->compress_type = 0;
36217b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, COMPRESS);
36317b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
36417b36120SJosef Bacik 		} else {
36517b36120SJosef Bacik 			btrfs_err(NULL, "unrecognized compression value %s",
36617b36120SJosef Bacik 				  param->string);
36717b36120SJosef Bacik 			return -EINVAL;
36817b36120SJosef Bacik 		}
36917b36120SJosef Bacik 		break;
37017b36120SJosef Bacik 	case Opt_ssd:
37117b36120SJosef Bacik 		if (result.negated) {
37217b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NOSSD);
37317b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, SSD);
37417b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, SSD_SPREAD);
37517b36120SJosef Bacik 		} else {
37617b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, SSD);
37717b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NOSSD);
37817b36120SJosef Bacik 		}
37917b36120SJosef Bacik 		break;
38017b36120SJosef Bacik 	case Opt_ssd_spread:
38117b36120SJosef Bacik 		if (result.negated) {
38217b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, SSD_SPREAD);
38317b36120SJosef Bacik 		} else {
38417b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, SSD);
38517b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, SSD_SPREAD);
38617b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NOSSD);
38717b36120SJosef Bacik 		}
38817b36120SJosef Bacik 		break;
38917b36120SJosef Bacik 	case Opt_barrier:
39017b36120SJosef Bacik 		if (result.negated)
39117b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NOBARRIER);
39217b36120SJosef Bacik 		else
39317b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NOBARRIER);
39417b36120SJosef Bacik 		break;
39517b36120SJosef Bacik 	case Opt_thread_pool:
39617b36120SJosef Bacik 		if (result.uint_32 == 0) {
39717b36120SJosef Bacik 			btrfs_err(NULL, "invalid value 0 for thread_pool");
39817b36120SJosef Bacik 			return -EINVAL;
39917b36120SJosef Bacik 		}
40017b36120SJosef Bacik 		ctx->thread_pool_size = result.uint_32;
40117b36120SJosef Bacik 		break;
40217b36120SJosef Bacik 	case Opt_max_inline:
40317b36120SJosef Bacik 		ctx->max_inline = memparse(param->string, NULL);
40417b36120SJosef Bacik 		break;
40517b36120SJosef Bacik 	case Opt_acl:
40617b36120SJosef Bacik 		if (result.negated) {
40717b36120SJosef Bacik 			fc->sb_flags &= ~SB_POSIXACL;
40817b36120SJosef Bacik 		} else {
40917b36120SJosef Bacik #ifdef CONFIG_BTRFS_FS_POSIX_ACL
41017b36120SJosef Bacik 			fc->sb_flags |= SB_POSIXACL;
41117b36120SJosef Bacik #else
41217b36120SJosef Bacik 			btrfs_err(NULL, "support for ACL not compiled in");
41317b36120SJosef Bacik 			return -EINVAL;
41417b36120SJosef Bacik #endif
41517b36120SJosef Bacik 		}
41617b36120SJosef Bacik 		/*
41717b36120SJosef Bacik 		 * VFS limits the ability to toggle ACL on and off via remount,
41817b36120SJosef Bacik 		 * despite every file system allowing this.  This seems to be
41917b36120SJosef Bacik 		 * an oversight since we all do, but it'll fail if we're
42017b36120SJosef Bacik 		 * remounting.  So don't set the mask here, we'll check it in
42117b36120SJosef Bacik 		 * btrfs_reconfigure and do the toggling ourselves.
42217b36120SJosef Bacik 		 */
42317b36120SJosef Bacik 		if (fc->purpose != FS_CONTEXT_FOR_RECONFIGURE)
42417b36120SJosef Bacik 			fc->sb_flags_mask |= SB_POSIXACL;
42517b36120SJosef Bacik 		break;
42617b36120SJosef Bacik 	case Opt_treelog:
42717b36120SJosef Bacik 		if (result.negated)
42817b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NOTREELOG);
42917b36120SJosef Bacik 		else
43017b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NOTREELOG);
43117b36120SJosef Bacik 		break;
43217b36120SJosef Bacik 	case Opt_nologreplay:
43317b36120SJosef Bacik 		btrfs_warn(NULL,
43417b36120SJosef Bacik 		"'nologreplay' is deprecated, use 'rescue=nologreplay' instead");
43517b36120SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, NOLOGREPLAY);
43617b36120SJosef Bacik 		break;
43717b36120SJosef Bacik 	case Opt_flushoncommit:
43817b36120SJosef Bacik 		if (result.negated)
43917b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, FLUSHONCOMMIT);
44017b36120SJosef Bacik 		else
44117b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, FLUSHONCOMMIT);
44217b36120SJosef Bacik 		break;
44317b36120SJosef Bacik 	case Opt_ratio:
44417b36120SJosef Bacik 		ctx->metadata_ratio = result.uint_32;
44517b36120SJosef Bacik 		break;
44617b36120SJosef Bacik 	case Opt_discard:
44717b36120SJosef Bacik 		if (result.negated) {
44817b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, DISCARD_SYNC);
44917b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, DISCARD_ASYNC);
45017b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NODISCARD);
45117b36120SJosef Bacik 		} else {
45217b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, DISCARD_SYNC);
45317b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, DISCARD_ASYNC);
45417b36120SJosef Bacik 		}
45517b36120SJosef Bacik 		break;
45617b36120SJosef Bacik 	case Opt_discard_mode:
45717b36120SJosef Bacik 		switch (result.uint_32) {
45817b36120SJosef Bacik 		case Opt_discard_sync:
45917b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, DISCARD_ASYNC);
46017b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, DISCARD_SYNC);
46117b36120SJosef Bacik 			break;
46217b36120SJosef Bacik 		case Opt_discard_async:
46317b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, DISCARD_SYNC);
46417b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, DISCARD_ASYNC);
46517b36120SJosef Bacik 			break;
46617b36120SJosef Bacik 		default:
46717b36120SJosef Bacik 			btrfs_err(NULL, "unrecognized discard mode value %s",
46817b36120SJosef Bacik 				  param->key);
46917b36120SJosef Bacik 			return -EINVAL;
47017b36120SJosef Bacik 		}
47117b36120SJosef Bacik 		btrfs_clear_opt(ctx->mount_opt, NODISCARD);
47217b36120SJosef Bacik 		break;
47317b36120SJosef Bacik 	case Opt_space_cache:
47417b36120SJosef Bacik 		if (result.negated) {
47517b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NOSPACECACHE);
47617b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, SPACE_CACHE);
47717b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, FREE_SPACE_TREE);
47817b36120SJosef Bacik 		} else {
47917b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, FREE_SPACE_TREE);
48017b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, SPACE_CACHE);
48117b36120SJosef Bacik 		}
48217b36120SJosef Bacik 		break;
48317b36120SJosef Bacik 	case Opt_space_cache_version:
48417b36120SJosef Bacik 		switch (result.uint_32) {
48517b36120SJosef Bacik 		case Opt_space_cache_v1:
48617b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, SPACE_CACHE);
48717b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, FREE_SPACE_TREE);
48817b36120SJosef Bacik 			break;
48917b36120SJosef Bacik 		case Opt_space_cache_v2:
49017b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, SPACE_CACHE);
49117b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, FREE_SPACE_TREE);
49217b36120SJosef Bacik 			break;
49317b36120SJosef Bacik 		default:
49417b36120SJosef Bacik 			btrfs_err(NULL, "unrecognized space_cache value %s",
49517b36120SJosef Bacik 				  param->key);
49617b36120SJosef Bacik 			return -EINVAL;
49717b36120SJosef Bacik 		}
49817b36120SJosef Bacik 		break;
49917b36120SJosef Bacik 	case Opt_rescan_uuid_tree:
50017b36120SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, RESCAN_UUID_TREE);
50117b36120SJosef Bacik 		break;
50217b36120SJosef Bacik 	case Opt_clear_cache:
50317b36120SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, CLEAR_CACHE);
50417b36120SJosef Bacik 		break;
50517b36120SJosef Bacik 	case Opt_user_subvol_rm_allowed:
50617b36120SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, USER_SUBVOL_RM_ALLOWED);
50717b36120SJosef Bacik 		break;
50817b36120SJosef Bacik 	case Opt_enospc_debug:
50917b36120SJosef Bacik 		if (result.negated)
51017b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, ENOSPC_DEBUG);
51117b36120SJosef Bacik 		else
51217b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, ENOSPC_DEBUG);
51317b36120SJosef Bacik 		break;
51417b36120SJosef Bacik 	case Opt_defrag:
51517b36120SJosef Bacik 		if (result.negated)
51617b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, AUTO_DEFRAG);
51717b36120SJosef Bacik 		else
51817b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, AUTO_DEFRAG);
51917b36120SJosef Bacik 		break;
52017b36120SJosef Bacik 	case Opt_usebackuproot:
52117b36120SJosef Bacik 		btrfs_warn(NULL,
52217b36120SJosef Bacik 			   "'usebackuproot' is deprecated, use 'rescue=usebackuproot' instead");
52317b36120SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, USEBACKUPROOT);
5249fb3b1a7SJosef Bacik 
5259fb3b1a7SJosef Bacik 		/* If we're loading the backup roots we can't trust the space cache. */
5269fb3b1a7SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, CLEAR_CACHE);
52717b36120SJosef Bacik 		break;
52817b36120SJosef Bacik 	case Opt_skip_balance:
52917b36120SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, SKIP_BALANCE);
53017b36120SJosef Bacik 		break;
53117b36120SJosef Bacik 	case Opt_fatal_errors:
53217b36120SJosef Bacik 		switch (result.uint_32) {
53317b36120SJosef Bacik 		case Opt_fatal_errors_panic:
53417b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, PANIC_ON_FATAL_ERROR);
53517b36120SJosef Bacik 			break;
53617b36120SJosef Bacik 		case Opt_fatal_errors_bug:
53717b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, PANIC_ON_FATAL_ERROR);
53817b36120SJosef Bacik 			break;
53917b36120SJosef Bacik 		default:
54017b36120SJosef Bacik 			btrfs_err(NULL, "unrecognized fatal_errors value %s",
54117b36120SJosef Bacik 				  param->key);
54217b36120SJosef Bacik 			return -EINVAL;
54317b36120SJosef Bacik 		}
54417b36120SJosef Bacik 		break;
54517b36120SJosef Bacik 	case Opt_commit_interval:
54617b36120SJosef Bacik 		ctx->commit_interval = result.uint_32;
54717b36120SJosef Bacik 		if (ctx->commit_interval == 0)
54817b36120SJosef Bacik 			ctx->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
54917b36120SJosef Bacik 		break;
55017b36120SJosef Bacik 	case Opt_rescue:
55117b36120SJosef Bacik 		switch (result.uint_32) {
55217b36120SJosef Bacik 		case Opt_rescue_usebackuproot:
55317b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, USEBACKUPROOT);
55417b36120SJosef Bacik 			break;
55517b36120SJosef Bacik 		case Opt_rescue_nologreplay:
55617b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NOLOGREPLAY);
55717b36120SJosef Bacik 			break;
55817b36120SJosef Bacik 		case Opt_rescue_ignorebadroots:
55917b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, IGNOREBADROOTS);
56017b36120SJosef Bacik 			break;
56117b36120SJosef Bacik 		case Opt_rescue_ignoredatacsums:
56217b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, IGNOREDATACSUMS);
56317b36120SJosef Bacik 			break;
56417b36120SJosef Bacik 		case Opt_rescue_parameter_all:
56517b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, IGNOREDATACSUMS);
56617b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, IGNOREBADROOTS);
56717b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NOLOGREPLAY);
56817b36120SJosef Bacik 			break;
56917b36120SJosef Bacik 		default:
57017b36120SJosef Bacik 			btrfs_info(NULL, "unrecognized rescue option '%s'",
57117b36120SJosef Bacik 				   param->key);
57217b36120SJosef Bacik 			return -EINVAL;
57317b36120SJosef Bacik 		}
57417b36120SJosef Bacik 		break;
57517b36120SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
57617b36120SJosef Bacik 	case Opt_fragment:
57717b36120SJosef Bacik 		switch (result.uint_32) {
57817b36120SJosef Bacik 		case Opt_fragment_parameter_all:
57917b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, FRAGMENT_DATA);
58017b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, FRAGMENT_METADATA);
58117b36120SJosef Bacik 			break;
58217b36120SJosef Bacik 		case Opt_fragment_parameter_metadata:
58317b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, FRAGMENT_METADATA);
58417b36120SJosef Bacik 			break;
58517b36120SJosef Bacik 		case Opt_fragment_parameter_data:
58617b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, FRAGMENT_DATA);
58717b36120SJosef Bacik 			break;
58817b36120SJosef Bacik 		default:
58917b36120SJosef Bacik 			btrfs_info(NULL, "unrecognized fragment option '%s'",
59017b36120SJosef Bacik 				   param->key);
59117b36120SJosef Bacik 			return -EINVAL;
59217b36120SJosef Bacik 		}
59317b36120SJosef Bacik 		break;
59417b36120SJosef Bacik #endif
59517b36120SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
59617b36120SJosef Bacik 	case Opt_ref_verify:
59717b36120SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, REF_VERIFY);
59817b36120SJosef Bacik 		break;
59917b36120SJosef Bacik #endif
60017b36120SJosef Bacik 	default:
60117b36120SJosef Bacik 		btrfs_err(NULL, "unrecognized mount option '%s'", param->key);
60217b36120SJosef Bacik 		return -EINVAL;
60317b36120SJosef Bacik 	}
60417b36120SJosef Bacik 
60517b36120SJosef Bacik 	return 0;
60617b36120SJosef Bacik }
60717b36120SJosef Bacik 
60883e3a40aSJosef Bacik /*
60983e3a40aSJosef Bacik  * Some options only have meaning at mount time and shouldn't persist across
61083e3a40aSJosef Bacik  * remounts, or be displayed. Clear these at the end of mount and remount code
61183e3a40aSJosef Bacik  * paths.
61283e3a40aSJosef Bacik  */
61383e3a40aSJosef Bacik static void btrfs_clear_oneshot_options(struct btrfs_fs_info *fs_info)
61483e3a40aSJosef Bacik {
61583e3a40aSJosef Bacik 	btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT);
61683e3a40aSJosef Bacik 	btrfs_clear_opt(fs_info->mount_opt, CLEAR_CACHE);
61783e3a40aSJosef Bacik 	btrfs_clear_opt(fs_info->mount_opt, NOSPACECACHE);
61883e3a40aSJosef Bacik }
61983e3a40aSJosef Bacik 
620eddb1a43SJosef Bacik static bool check_ro_option(struct btrfs_fs_info *fs_info,
621eddb1a43SJosef Bacik 			    unsigned long mount_opt, unsigned long opt,
622d70bf748SJosef Bacik 			    const char *opt_name)
623d70bf748SJosef Bacik {
624eddb1a43SJosef Bacik 	if (mount_opt & opt) {
625d70bf748SJosef Bacik 		btrfs_err(fs_info, "%s must be used with ro mount option",
626d70bf748SJosef Bacik 			  opt_name);
627d70bf748SJosef Bacik 		return true;
628d70bf748SJosef Bacik 	}
629d70bf748SJosef Bacik 	return false;
630d70bf748SJosef Bacik }
631d70bf748SJosef Bacik 
632ad21f15bSJosef Bacik bool btrfs_check_options(struct btrfs_fs_info *info, unsigned long *mount_opt,
633eddb1a43SJosef Bacik 			 unsigned long flags)
6342b41b19dSJosef Bacik {
6352b41b19dSJosef Bacik 	bool ret = true;
6362b41b19dSJosef Bacik 
6372b41b19dSJosef Bacik 	if (!(flags & SB_RDONLY) &&
638eddb1a43SJosef Bacik 	    (check_ro_option(info, *mount_opt, BTRFS_MOUNT_NOLOGREPLAY, "nologreplay") ||
639eddb1a43SJosef Bacik 	     check_ro_option(info, *mount_opt, BTRFS_MOUNT_IGNOREBADROOTS, "ignorebadroots") ||
640eddb1a43SJosef Bacik 	     check_ro_option(info, *mount_opt, BTRFS_MOUNT_IGNOREDATACSUMS, "ignoredatacsums")))
6412b41b19dSJosef Bacik 		ret = false;
6422b41b19dSJosef Bacik 
6432b41b19dSJosef Bacik 	if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) &&
644eddb1a43SJosef Bacik 	    !btrfs_raw_test_opt(*mount_opt, FREE_SPACE_TREE) &&
645eddb1a43SJosef Bacik 	    !btrfs_raw_test_opt(*mount_opt, CLEAR_CACHE)) {
6462b41b19dSJosef Bacik 		btrfs_err(info, "cannot disable free-space-tree");
6472b41b19dSJosef Bacik 		ret = false;
6482b41b19dSJosef Bacik 	}
6492b41b19dSJosef Bacik 	if (btrfs_fs_compat_ro(info, BLOCK_GROUP_TREE) &&
650eddb1a43SJosef Bacik 	     !btrfs_raw_test_opt(*mount_opt, FREE_SPACE_TREE)) {
6512b41b19dSJosef Bacik 		btrfs_err(info, "cannot disable free-space-tree with block-group-tree feature");
6522b41b19dSJosef Bacik 		ret = false;
6532b41b19dSJosef Bacik 	}
6542b41b19dSJosef Bacik 
655eddb1a43SJosef Bacik 	if (btrfs_check_mountopts_zoned(info, mount_opt))
6562b41b19dSJosef Bacik 		ret = false;
6572b41b19dSJosef Bacik 
6582b41b19dSJosef Bacik 	if (!test_bit(BTRFS_FS_STATE_REMOUNTING, &info->fs_state)) {
659eddb1a43SJosef Bacik 		if (btrfs_raw_test_opt(*mount_opt, SPACE_CACHE))
6602b41b19dSJosef Bacik 			btrfs_info(info, "disk space caching is enabled");
661eddb1a43SJosef Bacik 		if (btrfs_raw_test_opt(*mount_opt, FREE_SPACE_TREE))
6622b41b19dSJosef Bacik 			btrfs_info(info, "using free-space-tree");
6632b41b19dSJosef Bacik 	}
6642b41b19dSJosef Bacik 
6652b41b19dSJosef Bacik 	return ret;
6662b41b19dSJosef Bacik }
6672b41b19dSJosef Bacik 
668a6a8f22aSJosef Bacik /*
669a6a8f22aSJosef Bacik  * This is subtle, we only call this during open_ctree().  We need to pre-load
670a6a8f22aSJosef Bacik  * the mount options with the on-disk settings.  Before the new mount API took
671a6a8f22aSJosef Bacik  * effect we would do this on mount and remount.  With the new mount API we'll
672a6a8f22aSJosef Bacik  * only do this on the initial mount.
673a6a8f22aSJosef Bacik  *
674a6a8f22aSJosef Bacik  * This isn't a change in behavior, because we're using the current state of the
675a6a8f22aSJosef Bacik  * file system to set the current mount options.  If you mounted with special
676a6a8f22aSJosef Bacik  * options to disable these features and then remounted we wouldn't revert the
677a6a8f22aSJosef Bacik  * settings, because mounting without these features cleared the on-disk
678a6a8f22aSJosef Bacik  * settings, so this being called on re-mount is not needed.
679a6a8f22aSJosef Bacik  */
680a6a8f22aSJosef Bacik void btrfs_set_free_space_cache_settings(struct btrfs_fs_info *fs_info)
681a6a8f22aSJosef Bacik {
682a6a8f22aSJosef Bacik 	if (fs_info->sectorsize < PAGE_SIZE) {
683a6a8f22aSJosef Bacik 		btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
684a6a8f22aSJosef Bacik 		if (!btrfs_test_opt(fs_info, FREE_SPACE_TREE)) {
685a6a8f22aSJosef Bacik 			btrfs_info(fs_info,
686a6a8f22aSJosef Bacik 				   "forcing free space tree for sector size %u with page size %lu",
687a6a8f22aSJosef Bacik 				   fs_info->sectorsize, PAGE_SIZE);
688a6a8f22aSJosef Bacik 			btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
689a6a8f22aSJosef Bacik 		}
690a6a8f22aSJosef Bacik 	}
691ad21f15bSJosef Bacik 
692ad21f15bSJosef Bacik 	/*
693ad21f15bSJosef Bacik 	 * At this point our mount options are populated, so we only mess with
694ad21f15bSJosef Bacik 	 * these settings if we don't have any settings already.
695ad21f15bSJosef Bacik 	 */
696ad21f15bSJosef Bacik 	if (btrfs_test_opt(fs_info, FREE_SPACE_TREE))
697ad21f15bSJosef Bacik 		return;
698ad21f15bSJosef Bacik 
699ad21f15bSJosef Bacik 	if (btrfs_is_zoned(fs_info) &&
700ad21f15bSJosef Bacik 	    btrfs_free_space_cache_v1_active(fs_info)) {
701ad21f15bSJosef Bacik 		btrfs_info(fs_info, "zoned: clearing existing space cache");
702ad21f15bSJosef Bacik 		btrfs_set_super_cache_generation(fs_info->super_copy, 0);
703ad21f15bSJosef Bacik 		return;
704ad21f15bSJosef Bacik 	}
705ad21f15bSJosef Bacik 
706ad21f15bSJosef Bacik 	if (btrfs_test_opt(fs_info, SPACE_CACHE))
707ad21f15bSJosef Bacik 		return;
708ad21f15bSJosef Bacik 
709ad21f15bSJosef Bacik 	if (btrfs_test_opt(fs_info, NOSPACECACHE))
710ad21f15bSJosef Bacik 		return;
711ad21f15bSJosef Bacik 
712ad21f15bSJosef Bacik 	/*
713ad21f15bSJosef Bacik 	 * At this point we don't have explicit options set by the user, set
714ad21f15bSJosef Bacik 	 * them ourselves based on the state of the file system.
715ad21f15bSJosef Bacik 	 */
716ad21f15bSJosef Bacik 	if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
717ad21f15bSJosef Bacik 		btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
718ad21f15bSJosef Bacik 	else if (btrfs_free_space_cache_v1_active(fs_info))
719ad21f15bSJosef Bacik 		btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE);
720a6a8f22aSJosef Bacik }
721a6a8f22aSJosef Bacik 
72241d46b29SJosef Bacik static void set_device_specific_options(struct btrfs_fs_info *fs_info)
72341d46b29SJosef Bacik {
72441d46b29SJosef Bacik 	if (!btrfs_test_opt(fs_info, NOSSD) &&
72541d46b29SJosef Bacik 	    !fs_info->fs_devices->rotating)
72641d46b29SJosef Bacik 		btrfs_set_opt(fs_info->mount_opt, SSD);
72741d46b29SJosef Bacik 
72841d46b29SJosef Bacik 	/*
72941d46b29SJosef Bacik 	 * For devices supporting discard turn on discard=async automatically,
73041d46b29SJosef Bacik 	 * unless it's already set or disabled. This could be turned off by
73141d46b29SJosef Bacik 	 * nodiscard for the same mount.
73241d46b29SJosef Bacik 	 *
73341d46b29SJosef Bacik 	 * The zoned mode piggy backs on the discard functionality for
73441d46b29SJosef Bacik 	 * resetting a zone. There is no reason to delay the zone reset as it is
73541d46b29SJosef Bacik 	 * fast enough. So, do not enable async discard for zoned mode.
73641d46b29SJosef Bacik 	 */
73741d46b29SJosef Bacik 	if (!(btrfs_test_opt(fs_info, DISCARD_SYNC) ||
73841d46b29SJosef Bacik 	      btrfs_test_opt(fs_info, DISCARD_ASYNC) ||
73941d46b29SJosef Bacik 	      btrfs_test_opt(fs_info, NODISCARD)) &&
74041d46b29SJosef Bacik 	    fs_info->fs_devices->discardable &&
74141d46b29SJosef Bacik 	    !btrfs_is_zoned(fs_info))
74241d46b29SJosef Bacik 		btrfs_set_opt(fs_info->mount_opt, DISCARD_ASYNC);
74341d46b29SJosef Bacik }
74441d46b29SJosef Bacik 
745c0c907a4SMarcos Paulo de Souza char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
74673f73415SJosef Bacik 					  u64 subvol_objectid)
74773f73415SJosef Bacik {
748815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
7495168489aSJosef Bacik 	struct btrfs_root *fs_root = NULL;
75005dbe683SOmar Sandoval 	struct btrfs_root_ref *root_ref;
75105dbe683SOmar Sandoval 	struct btrfs_inode_ref *inode_ref;
75205dbe683SOmar Sandoval 	struct btrfs_key key;
75305dbe683SOmar Sandoval 	struct btrfs_path *path = NULL;
75405dbe683SOmar Sandoval 	char *name = NULL, *ptr;
75505dbe683SOmar Sandoval 	u64 dirid;
75605dbe683SOmar Sandoval 	int len;
75705dbe683SOmar Sandoval 	int ret;
75805dbe683SOmar Sandoval 
75905dbe683SOmar Sandoval 	path = btrfs_alloc_path();
76005dbe683SOmar Sandoval 	if (!path) {
76105dbe683SOmar Sandoval 		ret = -ENOMEM;
76205dbe683SOmar Sandoval 		goto err;
76305dbe683SOmar Sandoval 	}
76405dbe683SOmar Sandoval 
7653ec83621SDavid Sterba 	name = kmalloc(PATH_MAX, GFP_KERNEL);
76605dbe683SOmar Sandoval 	if (!name) {
76705dbe683SOmar Sandoval 		ret = -ENOMEM;
76805dbe683SOmar Sandoval 		goto err;
76905dbe683SOmar Sandoval 	}
77005dbe683SOmar Sandoval 	ptr = name + PATH_MAX - 1;
77105dbe683SOmar Sandoval 	ptr[0] = '\0';
77205dbe683SOmar Sandoval 
77305dbe683SOmar Sandoval 	/*
77405dbe683SOmar Sandoval 	 * Walk up the subvolume trees in the tree of tree roots by root
77505dbe683SOmar Sandoval 	 * backrefs until we hit the top-level subvolume.
77605dbe683SOmar Sandoval 	 */
77705dbe683SOmar Sandoval 	while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
77805dbe683SOmar Sandoval 		key.objectid = subvol_objectid;
77905dbe683SOmar Sandoval 		key.type = BTRFS_ROOT_BACKREF_KEY;
78005dbe683SOmar Sandoval 		key.offset = (u64)-1;
78105dbe683SOmar Sandoval 
7820ff40a91SMarcos Paulo de Souza 		ret = btrfs_search_backwards(root, &key, path);
78305dbe683SOmar Sandoval 		if (ret < 0) {
78405dbe683SOmar Sandoval 			goto err;
78505dbe683SOmar Sandoval 		} else if (ret > 0) {
78605dbe683SOmar Sandoval 			ret = -ENOENT;
78705dbe683SOmar Sandoval 			goto err;
78805dbe683SOmar Sandoval 		}
78905dbe683SOmar Sandoval 
79005dbe683SOmar Sandoval 		subvol_objectid = key.offset;
79105dbe683SOmar Sandoval 
79205dbe683SOmar Sandoval 		root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
79305dbe683SOmar Sandoval 					  struct btrfs_root_ref);
79405dbe683SOmar Sandoval 		len = btrfs_root_ref_name_len(path->nodes[0], root_ref);
79505dbe683SOmar Sandoval 		ptr -= len + 1;
79605dbe683SOmar Sandoval 		if (ptr < name) {
79705dbe683SOmar Sandoval 			ret = -ENAMETOOLONG;
79805dbe683SOmar Sandoval 			goto err;
79905dbe683SOmar Sandoval 		}
80005dbe683SOmar Sandoval 		read_extent_buffer(path->nodes[0], ptr + 1,
80105dbe683SOmar Sandoval 				   (unsigned long)(root_ref + 1), len);
80205dbe683SOmar Sandoval 		ptr[0] = '/';
80305dbe683SOmar Sandoval 		dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);
80405dbe683SOmar Sandoval 		btrfs_release_path(path);
80505dbe683SOmar Sandoval 
80656e9357aSDavid Sterba 		fs_root = btrfs_get_fs_root(fs_info, subvol_objectid, true);
80705dbe683SOmar Sandoval 		if (IS_ERR(fs_root)) {
80805dbe683SOmar Sandoval 			ret = PTR_ERR(fs_root);
8095168489aSJosef Bacik 			fs_root = NULL;
8105168489aSJosef Bacik 			goto err;
8115168489aSJosef Bacik 		}
81205dbe683SOmar Sandoval 
81305dbe683SOmar Sandoval 		/*
81405dbe683SOmar Sandoval 		 * Walk up the filesystem tree by inode refs until we hit the
81505dbe683SOmar Sandoval 		 * root directory.
81605dbe683SOmar Sandoval 		 */
81705dbe683SOmar Sandoval 		while (dirid != BTRFS_FIRST_FREE_OBJECTID) {
81805dbe683SOmar Sandoval 			key.objectid = dirid;
81905dbe683SOmar Sandoval 			key.type = BTRFS_INODE_REF_KEY;
82005dbe683SOmar Sandoval 			key.offset = (u64)-1;
82105dbe683SOmar Sandoval 
8220ff40a91SMarcos Paulo de Souza 			ret = btrfs_search_backwards(fs_root, &key, path);
82305dbe683SOmar Sandoval 			if (ret < 0) {
82405dbe683SOmar Sandoval 				goto err;
82505dbe683SOmar Sandoval 			} else if (ret > 0) {
82605dbe683SOmar Sandoval 				ret = -ENOENT;
82705dbe683SOmar Sandoval 				goto err;
82805dbe683SOmar Sandoval 			}
82905dbe683SOmar Sandoval 
83005dbe683SOmar Sandoval 			dirid = key.offset;
83105dbe683SOmar Sandoval 
83205dbe683SOmar Sandoval 			inode_ref = btrfs_item_ptr(path->nodes[0],
83305dbe683SOmar Sandoval 						   path->slots[0],
83405dbe683SOmar Sandoval 						   struct btrfs_inode_ref);
83505dbe683SOmar Sandoval 			len = btrfs_inode_ref_name_len(path->nodes[0],
83605dbe683SOmar Sandoval 						       inode_ref);
83705dbe683SOmar Sandoval 			ptr -= len + 1;
83805dbe683SOmar Sandoval 			if (ptr < name) {
83905dbe683SOmar Sandoval 				ret = -ENAMETOOLONG;
84005dbe683SOmar Sandoval 				goto err;
84105dbe683SOmar Sandoval 			}
84205dbe683SOmar Sandoval 			read_extent_buffer(path->nodes[0], ptr + 1,
84305dbe683SOmar Sandoval 					   (unsigned long)(inode_ref + 1), len);
84405dbe683SOmar Sandoval 			ptr[0] = '/';
84505dbe683SOmar Sandoval 			btrfs_release_path(path);
84605dbe683SOmar Sandoval 		}
84700246528SJosef Bacik 		btrfs_put_root(fs_root);
8485168489aSJosef Bacik 		fs_root = NULL;
84905dbe683SOmar Sandoval 	}
85005dbe683SOmar Sandoval 
85105dbe683SOmar Sandoval 	btrfs_free_path(path);
85205dbe683SOmar Sandoval 	if (ptr == name + PATH_MAX - 1) {
85305dbe683SOmar Sandoval 		name[0] = '/';
85405dbe683SOmar Sandoval 		name[1] = '\0';
85505dbe683SOmar Sandoval 	} else {
85605dbe683SOmar Sandoval 		memmove(name, ptr, name + PATH_MAX - ptr);
85705dbe683SOmar Sandoval 	}
85805dbe683SOmar Sandoval 	return name;
85905dbe683SOmar Sandoval 
86005dbe683SOmar Sandoval err:
86100246528SJosef Bacik 	btrfs_put_root(fs_root);
86205dbe683SOmar Sandoval 	btrfs_free_path(path);
86305dbe683SOmar Sandoval 	kfree(name);
86405dbe683SOmar Sandoval 	return ERR_PTR(ret);
86505dbe683SOmar Sandoval }
86605dbe683SOmar Sandoval 
86705dbe683SOmar Sandoval static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid)
86805dbe683SOmar Sandoval {
86905dbe683SOmar Sandoval 	struct btrfs_root *root = fs_info->tree_root;
87073f73415SJosef Bacik 	struct btrfs_dir_item *di;
87173f73415SJosef Bacik 	struct btrfs_path *path;
87273f73415SJosef Bacik 	struct btrfs_key location;
8736db75318SSweet Tea Dorminy 	struct fscrypt_str name = FSTR_INIT("default", 7);
87473f73415SJosef Bacik 	u64 dir_id;
87573f73415SJosef Bacik 
87673f73415SJosef Bacik 	path = btrfs_alloc_path();
87773f73415SJosef Bacik 	if (!path)
87805dbe683SOmar Sandoval 		return -ENOMEM;
87973f73415SJosef Bacik 
88073f73415SJosef Bacik 	/*
88173f73415SJosef Bacik 	 * Find the "default" dir item which points to the root item that we
88273f73415SJosef Bacik 	 * will mount by default if we haven't been given a specific subvolume
88373f73415SJosef Bacik 	 * to mount.
88473f73415SJosef Bacik 	 */
885815745cfSAl Viro 	dir_id = btrfs_super_root_dir(fs_info->super_copy);
886e43eec81SSweet Tea Dorminy 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, &name, 0);
887b0839166SJulia Lawall 	if (IS_ERR(di)) {
888b0839166SJulia Lawall 		btrfs_free_path(path);
88905dbe683SOmar Sandoval 		return PTR_ERR(di);
890b0839166SJulia Lawall 	}
89173f73415SJosef Bacik 	if (!di) {
89273f73415SJosef Bacik 		/*
89373f73415SJosef Bacik 		 * Ok the default dir item isn't there.  This is weird since
89473f73415SJosef Bacik 		 * it's always been there, but don't freak out, just try and
89505dbe683SOmar Sandoval 		 * mount the top-level subvolume.
89673f73415SJosef Bacik 		 */
89773f73415SJosef Bacik 		btrfs_free_path(path);
89805dbe683SOmar Sandoval 		*objectid = BTRFS_FS_TREE_OBJECTID;
89905dbe683SOmar Sandoval 		return 0;
90073f73415SJosef Bacik 	}
90173f73415SJosef Bacik 
90273f73415SJosef Bacik 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
90373f73415SJosef Bacik 	btrfs_free_path(path);
90405dbe683SOmar Sandoval 	*objectid = location.objectid;
90505dbe683SOmar Sandoval 	return 0;
90673f73415SJosef Bacik }
90773f73415SJosef Bacik 
9088a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb,
9098a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
91056e033a7SDavid Sterba 			    void *data)
9112e635a27SChris Mason {
9122e635a27SChris Mason 	struct inode *inode;
913815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
91439279cc3SChris Mason 	int err;
9152e635a27SChris Mason 
9162e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
9172e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
918e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
919af53d29aSAl Viro 	sb->s_d_op = &btrfs_dentry_operations;
920be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
92114605409SBoris Burkov #ifdef CONFIG_FS_VERITY
92214605409SBoris Burkov 	sb->s_vop = &btrfs_verityops;
92314605409SBoris Burkov #endif
9245103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
9252e635a27SChris Mason 	sb->s_time_gran = 1;
926da2f0f74SChris Mason 	sb->s_iflags |= SB_I_CGROUPWB;
9279e11ceeeSJan Kara 
9289e11ceeeSJan Kara 	err = super_setup_bdi(sb);
9299e11ceeeSJan Kara 	if (err) {
9309e11ceeeSJan Kara 		btrfs_err(fs_info, "super_setup_bdi failed");
9319e11ceeeSJan Kara 		return err;
9329e11ceeeSJan Kara 	}
9339e11ceeeSJan Kara 
934ad2b2c80SAl Viro 	err = open_ctree(sb, fs_devices, (char *)data);
935ad2b2c80SAl Viro 	if (err) {
936ab8d0fc4SJeff Mahoney 		btrfs_err(fs_info, "open_ctree failed");
937ad2b2c80SAl Viro 		return err;
938e20d96d6SChris Mason 	}
939b888db2bSChris Mason 
9400202e83fSDavid Sterba 	inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, fs_info->fs_root);
9415d4f98a2SYan Zheng 	if (IS_ERR(inode)) {
9425d4f98a2SYan Zheng 		err = PTR_ERR(inode);
94313b98989SJosef Bacik 		btrfs_handle_fs_error(fs_info, err, NULL);
94439279cc3SChris Mason 		goto fail_close;
94539279cc3SChris Mason 	}
9462e635a27SChris Mason 
94748fde701SAl Viro 	sb->s_root = d_make_root(inode);
94848fde701SAl Viro 	if (!sb->s_root) {
94939279cc3SChris Mason 		err = -ENOMEM;
95039279cc3SChris Mason 		goto fail_close;
9512e635a27SChris Mason 	}
95258176a96SJosef Bacik 
9531751e8a6SLinus Torvalds 	sb->s_flags |= SB_ACTIVE;
9542e635a27SChris Mason 	return 0;
9552e635a27SChris Mason 
95639279cc3SChris Mason fail_close:
9576bccf3abSJeff Mahoney 	close_ctree(fs_info);
958d5719762SChris Mason 	return err;
959d5719762SChris Mason }
960d5719762SChris Mason 
9616bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
962d5719762SChris Mason {
963d5719762SChris Mason 	struct btrfs_trans_handle *trans;
964815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
965815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
966df2ce34cSChris Mason 
967bc074524SJeff Mahoney 	trace_btrfs_sync_fs(fs_info, wait);
9681abe9b8aSliubo 
969d561c025SChris Mason 	if (!wait) {
970815745cfSAl Viro 		filemap_flush(fs_info->btree_inode->i_mapping);
971df2ce34cSChris Mason 		return 0;
972d561c025SChris Mason 	}
973771ed689SChris Mason 
9746374e57aSChris Mason 	btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
975771ed689SChris Mason 
976d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
97760376ce4SJosef Bacik 	if (IS_ERR(trans)) {
978354aa0fbSMiao Xie 		/* no transaction, don't bother */
9796b5fe46dSDavid Sterba 		if (PTR_ERR(trans) == -ENOENT) {
9806b5fe46dSDavid Sterba 			/*
9816b5fe46dSDavid Sterba 			 * Exit unless we have some pending changes
9826b5fe46dSDavid Sterba 			 * that need to go through commit
9836b5fe46dSDavid Sterba 			 */
984c52cc7b7SJosef Bacik 			if (!test_bit(BTRFS_FS_NEED_TRANS_COMMIT,
985c52cc7b7SJosef Bacik 				      &fs_info->flags))
986bd7de2c9SJosef Bacik 				return 0;
987a53f4f8eSQu Wenruo 			/*
988a53f4f8eSQu Wenruo 			 * A non-blocking test if the fs is frozen. We must not
989a53f4f8eSQu Wenruo 			 * start a new transaction here otherwise a deadlock
990a53f4f8eSQu Wenruo 			 * happens. The pending operations are delayed to the
991a53f4f8eSQu Wenruo 			 * next commit after thawing.
992a53f4f8eSQu Wenruo 			 */
993a7e3c5f2SRakesh Pandit 			if (sb_start_write_trylock(sb))
994a7e3c5f2SRakesh Pandit 				sb_end_write(sb);
995a53f4f8eSQu Wenruo 			else
996a53f4f8eSQu Wenruo 				return 0;
9976b5fe46dSDavid Sterba 			trans = btrfs_start_transaction(root, 0);
99860376ce4SJosef Bacik 		}
99998bd5c54SDavid Sterba 		if (IS_ERR(trans))
100098bd5c54SDavid Sterba 			return PTR_ERR(trans);
10016b5fe46dSDavid Sterba 	}
10023a45bb20SJeff Mahoney 	return btrfs_commit_transaction(trans);
1003d5719762SChris Mason }
1004d5719762SChris Mason 
1005ab0b4a3eSJosef Bacik static void print_rescue_option(struct seq_file *seq, const char *s, bool *printed)
1006ab0b4a3eSJosef Bacik {
1007ab0b4a3eSJosef Bacik 	seq_printf(seq, "%s%s", (*printed) ? ":" : ",rescue=", s);
1008ab0b4a3eSJosef Bacik 	*printed = true;
1009ab0b4a3eSJosef Bacik }
1010ab0b4a3eSJosef Bacik 
101134c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
1012a9572a15SEric Paris {
1013815745cfSAl Viro 	struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
10140f628c63SDavid Sterba 	const char *compress_type;
10153ef3959bSJosef Bacik 	const char *subvol_name;
1016ab0b4a3eSJosef Bacik 	bool printed = false;
1017a9572a15SEric Paris 
10183cdde224SJeff Mahoney 	if (btrfs_test_opt(info, DEGRADED))
1019a9572a15SEric Paris 		seq_puts(seq, ",degraded");
10203cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NODATASUM))
1021a9572a15SEric Paris 		seq_puts(seq, ",nodatasum");
10223cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NODATACOW))
1023a9572a15SEric Paris 		seq_puts(seq, ",nodatacow");
10243cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOBARRIER))
1025a9572a15SEric Paris 		seq_puts(seq, ",nobarrier");
102695ac567aSFilipe David Borba Manana 	if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
1027c1c9ff7cSGeert Uytterhoeven 		seq_printf(seq, ",max_inline=%llu", info->max_inline);
1028a9572a15SEric Paris 	if (info->thread_pool_size !=  min_t(unsigned long,
1029a9572a15SEric Paris 					     num_online_cpus() + 2, 8))
1030f7b885beSAnand Jain 		seq_printf(seq, ",thread_pool=%u", info->thread_pool_size);
10313cdde224SJeff Mahoney 	if (btrfs_test_opt(info, COMPRESS)) {
10320f628c63SDavid Sterba 		compress_type = btrfs_compress_type2str(info->compress_type);
10333cdde224SJeff Mahoney 		if (btrfs_test_opt(info, FORCE_COMPRESS))
1034200da64eSTsutomu Itoh 			seq_printf(seq, ",compress-force=%s", compress_type);
1035200da64eSTsutomu Itoh 		else
1036200da64eSTsutomu Itoh 			seq_printf(seq, ",compress=%s", compress_type);
1037f51d2b59SDavid Sterba 		if (info->compress_level)
1038fa4d885aSAdam Borowski 			seq_printf(seq, ":%d", info->compress_level);
1039200da64eSTsutomu Itoh 	}
10403cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOSSD))
1041c289811cSChris Mason 		seq_puts(seq, ",nossd");
10423cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SSD_SPREAD))
1043451d7585SChris Mason 		seq_puts(seq, ",ssd_spread");
10443cdde224SJeff Mahoney 	else if (btrfs_test_opt(info, SSD))
1045a9572a15SEric Paris 		seq_puts(seq, ",ssd");
10463cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOTREELOG))
10476b65c5c6SSage Weil 		seq_puts(seq, ",notreelog");
10483cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOLOGREPLAY))
1049ab0b4a3eSJosef Bacik 		print_rescue_option(seq, "nologreplay", &printed);
105068319c18SJosef Bacik 	if (btrfs_test_opt(info, USEBACKUPROOT))
105168319c18SJosef Bacik 		print_rescue_option(seq, "usebackuproot", &printed);
105242437a63SJosef Bacik 	if (btrfs_test_opt(info, IGNOREBADROOTS))
105342437a63SJosef Bacik 		print_rescue_option(seq, "ignorebadroots", &printed);
1054882dbe0cSJosef Bacik 	if (btrfs_test_opt(info, IGNOREDATACSUMS))
1055882dbe0cSJosef Bacik 		print_rescue_option(seq, "ignoredatacsums", &printed);
10563cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FLUSHONCOMMIT))
10576b65c5c6SSage Weil 		seq_puts(seq, ",flushoncommit");
105846b27f50SDennis Zhou 	if (btrfs_test_opt(info, DISCARD_SYNC))
105920a5239aSMatthew Wilcox 		seq_puts(seq, ",discard");
1060b0643e59SDennis Zhou 	if (btrfs_test_opt(info, DISCARD_ASYNC))
1061b0643e59SDennis Zhou 		seq_puts(seq, ",discard=async");
10621751e8a6SLinus Torvalds 	if (!(info->sb->s_flags & SB_POSIXACL))
1063a9572a15SEric Paris 		seq_puts(seq, ",noacl");
106404c41559SBoris Burkov 	if (btrfs_free_space_cache_v1_active(info))
1065200da64eSTsutomu Itoh 		seq_puts(seq, ",space_cache");
106604c41559SBoris Burkov 	else if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
106770f6d82eSOmar Sandoval 		seq_puts(seq, ",space_cache=v2");
106873bc1876SJosef Bacik 	else
10698965593eSDavid Sterba 		seq_puts(seq, ",nospace_cache");
10703cdde224SJeff Mahoney 	if (btrfs_test_opt(info, RESCAN_UUID_TREE))
1071f420ee1eSStefan Behrens 		seq_puts(seq, ",rescan_uuid_tree");
10723cdde224SJeff Mahoney 	if (btrfs_test_opt(info, CLEAR_CACHE))
1073200da64eSTsutomu Itoh 		seq_puts(seq, ",clear_cache");
10743cdde224SJeff Mahoney 	if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED))
1075200da64eSTsutomu Itoh 		seq_puts(seq, ",user_subvol_rm_allowed");
10763cdde224SJeff Mahoney 	if (btrfs_test_opt(info, ENOSPC_DEBUG))
10770942caa3SDavid Sterba 		seq_puts(seq, ",enospc_debug");
10783cdde224SJeff Mahoney 	if (btrfs_test_opt(info, AUTO_DEFRAG))
10790942caa3SDavid Sterba 		seq_puts(seq, ",autodefrag");
10803cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SKIP_BALANCE))
10819555c6c1SIlya Dryomov 		seq_puts(seq, ",skip_balance");
10828507d216SWang Shilong 	if (info->metadata_ratio)
1083764cb8b4SAnand Jain 		seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio);
10843cdde224SJeff Mahoney 	if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR))
10858c342930SJeff Mahoney 		seq_puts(seq, ",fatal_errors=panic");
10868b87dc17SDavid Sterba 	if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
1087d3740608SAnand Jain 		seq_printf(seq, ",commit=%u", info->commit_interval);
1088d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
10893cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FRAGMENT_DATA))
1090d0bd4560SJosef Bacik 		seq_puts(seq, ",fragment=data");
10913cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FRAGMENT_METADATA))
1092d0bd4560SJosef Bacik 		seq_puts(seq, ",fragment=metadata");
1093d0bd4560SJosef Bacik #endif
1094fb592373SJosef Bacik 	if (btrfs_test_opt(info, REF_VERIFY))
1095fb592373SJosef Bacik 		seq_puts(seq, ",ref_verify");
1096c8d3fe02SOmar Sandoval 	seq_printf(seq, ",subvolid=%llu",
1097c8d3fe02SOmar Sandoval 		  BTRFS_I(d_inode(dentry))->root->root_key.objectid);
10983ef3959bSJosef Bacik 	subvol_name = btrfs_get_subvol_name_from_objectid(info,
10993ef3959bSJosef Bacik 			BTRFS_I(d_inode(dentry))->root->root_key.objectid);
11003ef3959bSJosef Bacik 	if (!IS_ERR(subvol_name)) {
1101c8d3fe02SOmar Sandoval 		seq_puts(seq, ",subvol=");
11023ef3959bSJosef Bacik 		seq_escape(seq, subvol_name, " \t\n\\");
11033ef3959bSJosef Bacik 		kfree(subvol_name);
11043ef3959bSJosef Bacik 	}
1105a9572a15SEric Paris 	return 0;
1106a9572a15SEric Paris }
1107a9572a15SEric Paris 
1108830c4adbSJosef Bacik /*
1109f9d9ef62SDavid Sterba  * subvolumes are identified by ino 256
1110f9d9ef62SDavid Sterba  */
1111f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode)
1112f9d9ef62SDavid Sterba {
1113f9d9ef62SDavid Sterba 	if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
1114f9d9ef62SDavid Sterba 		return 1;
1115f9d9ef62SDavid Sterba 	return 0;
1116f9d9ef62SDavid Sterba }
1117f9d9ef62SDavid Sterba 
1118bb289b7bSOmar Sandoval static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
1119ae0bc863SAnand Jain 				   struct vfsmount *mnt)
1120830c4adbSJosef Bacik {
1121830c4adbSJosef Bacik 	struct dentry *root;
1122fa330659SOmar Sandoval 	int ret;
1123830c4adbSJosef Bacik 
112405dbe683SOmar Sandoval 	if (!subvol_name) {
112505dbe683SOmar Sandoval 		if (!subvol_objectid) {
112605dbe683SOmar Sandoval 			ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
112705dbe683SOmar Sandoval 							  &subvol_objectid);
112805dbe683SOmar Sandoval 			if (ret) {
112905dbe683SOmar Sandoval 				root = ERR_PTR(ret);
113005dbe683SOmar Sandoval 				goto out;
113105dbe683SOmar Sandoval 			}
113205dbe683SOmar Sandoval 		}
1133c0c907a4SMarcos Paulo de Souza 		subvol_name = btrfs_get_subvol_name_from_objectid(
1134c0c907a4SMarcos Paulo de Souza 					btrfs_sb(mnt->mnt_sb), subvol_objectid);
113505dbe683SOmar Sandoval 		if (IS_ERR(subvol_name)) {
113605dbe683SOmar Sandoval 			root = ERR_CAST(subvol_name);
113705dbe683SOmar Sandoval 			subvol_name = NULL;
113805dbe683SOmar Sandoval 			goto out;
113905dbe683SOmar Sandoval 		}
114005dbe683SOmar Sandoval 
114105dbe683SOmar Sandoval 	}
114205dbe683SOmar Sandoval 
1143ea441d11SAl Viro 	root = mount_subtree(mnt, subvol_name);
1144fa330659SOmar Sandoval 	/* mount_subtree() drops our reference on the vfsmount. */
1145fa330659SOmar Sandoval 	mnt = NULL;
1146830c4adbSJosef Bacik 
1147bb289b7bSOmar Sandoval 	if (!IS_ERR(root)) {
1148ea441d11SAl Viro 		struct super_block *s = root->d_sb;
1149ab8d0fc4SJeff Mahoney 		struct btrfs_fs_info *fs_info = btrfs_sb(s);
1150bb289b7bSOmar Sandoval 		struct inode *root_inode = d_inode(root);
1151bb289b7bSOmar Sandoval 		u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid;
1152bb289b7bSOmar Sandoval 
1153bb289b7bSOmar Sandoval 		ret = 0;
1154bb289b7bSOmar Sandoval 		if (!is_subvolume_inode(root_inode)) {
1155ab8d0fc4SJeff Mahoney 			btrfs_err(fs_info, "'%s' is not a valid subvolume",
1156bb289b7bSOmar Sandoval 			       subvol_name);
1157bb289b7bSOmar Sandoval 			ret = -EINVAL;
1158bb289b7bSOmar Sandoval 		}
1159bb289b7bSOmar Sandoval 		if (subvol_objectid && root_objectid != subvol_objectid) {
116005dbe683SOmar Sandoval 			/*
116105dbe683SOmar Sandoval 			 * This will also catch a race condition where a
116205dbe683SOmar Sandoval 			 * subvolume which was passed by ID is renamed and
116305dbe683SOmar Sandoval 			 * another subvolume is renamed over the old location.
116405dbe683SOmar Sandoval 			 */
1165ab8d0fc4SJeff Mahoney 			btrfs_err(fs_info,
1166ab8d0fc4SJeff Mahoney 				  "subvol '%s' does not match subvolid %llu",
1167bb289b7bSOmar Sandoval 				  subvol_name, subvol_objectid);
1168bb289b7bSOmar Sandoval 			ret = -EINVAL;
1169bb289b7bSOmar Sandoval 		}
1170bb289b7bSOmar Sandoval 		if (ret) {
1171ea441d11SAl Viro 			dput(root);
1172bb289b7bSOmar Sandoval 			root = ERR_PTR(ret);
1173ea441d11SAl Viro 			deactivate_locked_super(s);
1174bb289b7bSOmar Sandoval 		}
1175f9d9ef62SDavid Sterba 	}
1176f9d9ef62SDavid Sterba 
1177fa330659SOmar Sandoval out:
1178fa330659SOmar Sandoval 	mntput(mnt);
1179fa330659SOmar Sandoval 	kfree(subvol_name);
1180830c4adbSJosef Bacik 	return root;
1181830c4adbSJosef Bacik }
1182450ba0eaSJosef Bacik 
11830d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
1184f7b885beSAnand Jain 				     u32 new_pool_size, u32 old_pool_size)
11850d2450abSSergei Trofimovich {
11860d2450abSSergei Trofimovich 	if (new_pool_size == old_pool_size)
11870d2450abSSergei Trofimovich 		return;
11880d2450abSSergei Trofimovich 
11890d2450abSSergei Trofimovich 	fs_info->thread_pool_size = new_pool_size;
11900d2450abSSergei Trofimovich 
1191efe120a0SFrank Holton 	btrfs_info(fs_info, "resize thread pool %d -> %d",
11920d2450abSSergei Trofimovich 	       old_pool_size, new_pool_size);
11930d2450abSSergei Trofimovich 
11945cdc7ad3SQu Wenruo 	btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
1195afe3d242SQu Wenruo 	btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
1196e66f0bb1SQu Wenruo 	btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
119740fac647SChristoph Hellwig 	workqueue_set_max_active(fs_info->endio_workers, new_pool_size);
119840fac647SChristoph Hellwig 	workqueue_set_max_active(fs_info->endio_meta_workers, new_pool_size);
1199fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
1200fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
12015b3bc44eSQu Wenruo 	btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
12020d2450abSSergei Trofimovich }
12030d2450abSSergei Trofimovich 
1204f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1205f42a34b2SMiao Xie 				       unsigned long old_opts, int flags)
1206f42a34b2SMiao Xie {
1207dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1208dc81cdc5SMiao Xie 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
12091751e8a6SLinus Torvalds 	     (flags & SB_RDONLY))) {
1210dc81cdc5SMiao Xie 		/* wait for any defraggers to finish */
1211dc81cdc5SMiao Xie 		wait_event(fs_info->transaction_wait,
1212dc81cdc5SMiao Xie 			   (atomic_read(&fs_info->defrag_running) == 0));
12131751e8a6SLinus Torvalds 		if (flags & SB_RDONLY)
1214dc81cdc5SMiao Xie 			sync_filesystem(fs_info->sb);
1215dc81cdc5SMiao Xie 	}
1216dc81cdc5SMiao Xie }
1217dc81cdc5SMiao Xie 
1218dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1219dc81cdc5SMiao Xie 					 unsigned long old_opts)
1220dc81cdc5SMiao Xie {
122194846229SBoris Burkov 	const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE);
122294846229SBoris Burkov 
1223dc81cdc5SMiao Xie 	/*
1224180e4d47SLuis de Bethencourt 	 * We need to cleanup all defragable inodes if the autodefragment is
1225180e4d47SLuis de Bethencourt 	 * close or the filesystem is read only.
1226dc81cdc5SMiao Xie 	 */
1227dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1228bc98a42cSDavid Howells 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) {
1229dc81cdc5SMiao Xie 		btrfs_cleanup_defrag_inodes(fs_info);
1230dc81cdc5SMiao Xie 	}
1231dc81cdc5SMiao Xie 
1232b0643e59SDennis Zhou 	/* If we toggled discard async */
1233b0643e59SDennis Zhou 	if (!btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1234b0643e59SDennis Zhou 	    btrfs_test_opt(fs_info, DISCARD_ASYNC))
1235b0643e59SDennis Zhou 		btrfs_discard_resume(fs_info);
1236b0643e59SDennis Zhou 	else if (btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1237b0643e59SDennis Zhou 		 !btrfs_test_opt(fs_info, DISCARD_ASYNC))
1238b0643e59SDennis Zhou 		btrfs_discard_cleanup(fs_info);
123994846229SBoris Burkov 
124094846229SBoris Burkov 	/* If we toggled space cache */
124194846229SBoris Burkov 	if (cache_opt != btrfs_free_space_cache_v1_active(fs_info))
124294846229SBoris Burkov 		btrfs_set_free_space_cache_v1_active(fs_info, cache_opt);
1243dc81cdc5SMiao Xie }
1244dc81cdc5SMiao Xie 
12459ef40c2eSJosef Bacik static int btrfs_remount_rw(struct btrfs_fs_info *fs_info)
12469ef40c2eSJosef Bacik {
12479ef40c2eSJosef Bacik 	int ret;
12489ef40c2eSJosef Bacik 
12499ef40c2eSJosef Bacik 	if (BTRFS_FS_ERROR(fs_info)) {
12509ef40c2eSJosef Bacik 		btrfs_err(fs_info,
12519ef40c2eSJosef Bacik 			  "remounting read-write after error is not allowed");
12529ef40c2eSJosef Bacik 		return -EINVAL;
12539ef40c2eSJosef Bacik 	}
12549ef40c2eSJosef Bacik 
12559ef40c2eSJosef Bacik 	if (fs_info->fs_devices->rw_devices == 0)
12569ef40c2eSJosef Bacik 		return -EACCES;
12579ef40c2eSJosef Bacik 
12589ef40c2eSJosef Bacik 	if (!btrfs_check_rw_degradable(fs_info, NULL)) {
12599ef40c2eSJosef Bacik 		btrfs_warn(fs_info,
12609ef40c2eSJosef Bacik 			   "too many missing devices, writable remount is not allowed");
12619ef40c2eSJosef Bacik 		return -EACCES;
12629ef40c2eSJosef Bacik 	}
12639ef40c2eSJosef Bacik 
12649ef40c2eSJosef Bacik 	if (btrfs_super_log_root(fs_info->super_copy) != 0) {
12659ef40c2eSJosef Bacik 		btrfs_warn(fs_info,
12669ef40c2eSJosef Bacik 			   "mount required to replay tree-log, cannot remount read-write");
12679ef40c2eSJosef Bacik 		return -EINVAL;
12689ef40c2eSJosef Bacik 	}
12699ef40c2eSJosef Bacik 
12709ef40c2eSJosef Bacik 	/*
12719ef40c2eSJosef Bacik 	 * NOTE: when remounting with a change that does writes, don't put it
12729ef40c2eSJosef Bacik 	 * anywhere above this point, as we are not sure to be safe to write
12739ef40c2eSJosef Bacik 	 * until we pass the above checks.
12749ef40c2eSJosef Bacik 	 */
12759ef40c2eSJosef Bacik 	ret = btrfs_start_pre_rw_mount(fs_info);
12769ef40c2eSJosef Bacik 	if (ret)
12779ef40c2eSJosef Bacik 		return ret;
12789ef40c2eSJosef Bacik 
12799ef40c2eSJosef Bacik 	btrfs_clear_sb_rdonly(fs_info->sb);
12809ef40c2eSJosef Bacik 
12819ef40c2eSJosef Bacik 	set_bit(BTRFS_FS_OPEN, &fs_info->flags);
12829ef40c2eSJosef Bacik 
12839ef40c2eSJosef Bacik 	/*
12849ef40c2eSJosef Bacik 	 * If we've gone from readonly -> read-write, we need to get our
12859ef40c2eSJosef Bacik 	 * sync/async discard lists in the right state.
12869ef40c2eSJosef Bacik 	 */
12879ef40c2eSJosef Bacik 	btrfs_discard_resume(fs_info);
12889ef40c2eSJosef Bacik 
12899ef40c2eSJosef Bacik 	return 0;
12909ef40c2eSJosef Bacik }
12919ef40c2eSJosef Bacik 
12929ef40c2eSJosef Bacik static int btrfs_remount_ro(struct btrfs_fs_info *fs_info)
12939ef40c2eSJosef Bacik {
12949ef40c2eSJosef Bacik 	/*
12959ef40c2eSJosef Bacik 	 * This also happens on 'umount -rf' or on shutdown, when the
12969ef40c2eSJosef Bacik 	 * filesystem is busy.
12979ef40c2eSJosef Bacik 	 */
12989ef40c2eSJosef Bacik 	cancel_work_sync(&fs_info->async_reclaim_work);
12999ef40c2eSJosef Bacik 	cancel_work_sync(&fs_info->async_data_reclaim_work);
13009ef40c2eSJosef Bacik 
13019ef40c2eSJosef Bacik 	btrfs_discard_cleanup(fs_info);
13029ef40c2eSJosef Bacik 
13039ef40c2eSJosef Bacik 	/* Wait for the uuid_scan task to finish */
13049ef40c2eSJosef Bacik 	down(&fs_info->uuid_tree_rescan_sem);
13059ef40c2eSJosef Bacik 	/* Avoid complains from lockdep et al. */
13069ef40c2eSJosef Bacik 	up(&fs_info->uuid_tree_rescan_sem);
13079ef40c2eSJosef Bacik 
13089ef40c2eSJosef Bacik 	btrfs_set_sb_rdonly(fs_info->sb);
13099ef40c2eSJosef Bacik 
13109ef40c2eSJosef Bacik 	/*
13119ef40c2eSJosef Bacik 	 * Setting SB_RDONLY will put the cleaner thread to sleep at the next
13129ef40c2eSJosef Bacik 	 * loop if it's already active.  If it's already asleep, we'll leave
13139ef40c2eSJosef Bacik 	 * unused block groups on disk until we're mounted read-write again
13149ef40c2eSJosef Bacik 	 * unless we clean them up here.
13159ef40c2eSJosef Bacik 	 */
13169ef40c2eSJosef Bacik 	btrfs_delete_unused_bgs(fs_info);
13179ef40c2eSJosef Bacik 
13189ef40c2eSJosef Bacik 	/*
13199ef40c2eSJosef Bacik 	 * The cleaner task could be already running before we set the flag
13209ef40c2eSJosef Bacik 	 * BTRFS_FS_STATE_RO (and SB_RDONLY in the superblock).  We must make
13219ef40c2eSJosef Bacik 	 * sure that after we finish the remount, i.e. after we call
13229ef40c2eSJosef Bacik 	 * btrfs_commit_super(), the cleaner can no longer start a transaction
13239ef40c2eSJosef Bacik 	 * - either because it was dropping a dead root, running delayed iputs
13249ef40c2eSJosef Bacik 	 *   or deleting an unused block group (the cleaner picked a block
13259ef40c2eSJosef Bacik 	 *   group from the list of unused block groups before we were able to
13269ef40c2eSJosef Bacik 	 *   in the previous call to btrfs_delete_unused_bgs()).
13279ef40c2eSJosef Bacik 	 */
13289ef40c2eSJosef Bacik 	wait_on_bit(&fs_info->flags, BTRFS_FS_CLEANER_RUNNING, TASK_UNINTERRUPTIBLE);
13299ef40c2eSJosef Bacik 
13309ef40c2eSJosef Bacik 	/*
13319ef40c2eSJosef Bacik 	 * We've set the superblock to RO mode, so we might have made the
13329ef40c2eSJosef Bacik 	 * cleaner task sleep without running all pending delayed iputs. Go
13339ef40c2eSJosef Bacik 	 * through all the delayed iputs here, so that if an unmount happens
13349ef40c2eSJosef Bacik 	 * without remounting RW we don't end up at finishing close_ctree()
13359ef40c2eSJosef Bacik 	 * with a non-empty list of delayed iputs.
13369ef40c2eSJosef Bacik 	 */
13379ef40c2eSJosef Bacik 	btrfs_run_delayed_iputs(fs_info);
13389ef40c2eSJosef Bacik 
13399ef40c2eSJosef Bacik 	btrfs_dev_replace_suspend_for_unmount(fs_info);
13409ef40c2eSJosef Bacik 	btrfs_scrub_cancel(fs_info);
13419ef40c2eSJosef Bacik 	btrfs_pause_balance(fs_info);
13429ef40c2eSJosef Bacik 
13439ef40c2eSJosef Bacik 	/*
13449ef40c2eSJosef Bacik 	 * Pause the qgroup rescan worker if it is running. We don't want it to
13459ef40c2eSJosef Bacik 	 * be still running after we are in RO mode, as after that, by the time
13469ef40c2eSJosef Bacik 	 * we unmount, it might have left a transaction open, so we would leak
13479ef40c2eSJosef Bacik 	 * the transaction and/or crash.
13489ef40c2eSJosef Bacik 	 */
13499ef40c2eSJosef Bacik 	btrfs_qgroup_wait_for_completion(fs_info, false);
13509ef40c2eSJosef Bacik 
13519ef40c2eSJosef Bacik 	return btrfs_commit_super(fs_info);
13529ef40c2eSJosef Bacik }
13539ef40c2eSJosef Bacik 
1354eddb1a43SJosef Bacik static void btrfs_ctx_to_info(struct btrfs_fs_info *fs_info, struct btrfs_fs_context *ctx)
1355eddb1a43SJosef Bacik {
1356eddb1a43SJosef Bacik 	fs_info->max_inline = ctx->max_inline;
1357eddb1a43SJosef Bacik 	fs_info->commit_interval = ctx->commit_interval;
1358eddb1a43SJosef Bacik 	fs_info->metadata_ratio = ctx->metadata_ratio;
1359eddb1a43SJosef Bacik 	fs_info->thread_pool_size = ctx->thread_pool_size;
1360eddb1a43SJosef Bacik 	fs_info->mount_opt = ctx->mount_opt;
1361eddb1a43SJosef Bacik 	fs_info->compress_type = ctx->compress_type;
1362eddb1a43SJosef Bacik 	fs_info->compress_level = ctx->compress_level;
1363eddb1a43SJosef Bacik }
1364eddb1a43SJosef Bacik 
1365eddb1a43SJosef Bacik static void btrfs_info_to_ctx(struct btrfs_fs_info *fs_info, struct btrfs_fs_context *ctx)
1366eddb1a43SJosef Bacik {
1367eddb1a43SJosef Bacik 	ctx->max_inline = fs_info->max_inline;
1368eddb1a43SJosef Bacik 	ctx->commit_interval = fs_info->commit_interval;
1369eddb1a43SJosef Bacik 	ctx->metadata_ratio = fs_info->metadata_ratio;
1370eddb1a43SJosef Bacik 	ctx->thread_pool_size = fs_info->thread_pool_size;
1371eddb1a43SJosef Bacik 	ctx->mount_opt = fs_info->mount_opt;
1372eddb1a43SJosef Bacik 	ctx->compress_type = fs_info->compress_type;
1373eddb1a43SJosef Bacik 	ctx->compress_level = fs_info->compress_level;
1374eddb1a43SJosef Bacik }
1375eddb1a43SJosef Bacik 
1376eddb1a43SJosef Bacik #define btrfs_info_if_set(fs_info, old_ctx, opt, fmt, args...)			\
1377eddb1a43SJosef Bacik do {										\
1378eddb1a43SJosef Bacik 	if ((!old_ctx || !btrfs_raw_test_opt(old_ctx->mount_opt, opt)) &&	\
1379eddb1a43SJosef Bacik 	    btrfs_raw_test_opt(fs_info->mount_opt, opt))			\
1380eddb1a43SJosef Bacik 		btrfs_info(fs_info, fmt, ##args);				\
1381eddb1a43SJosef Bacik } while (0)
1382eddb1a43SJosef Bacik 
1383eddb1a43SJosef Bacik #define btrfs_info_if_unset(fs_info, old_ctx, opt, fmt, args...)	\
1384eddb1a43SJosef Bacik do {									\
1385eddb1a43SJosef Bacik 	if ((old_ctx && btrfs_raw_test_opt(old_ctx->mount_opt, opt)) &&	\
1386eddb1a43SJosef Bacik 	    !btrfs_raw_test_opt(fs_info->mount_opt, opt))		\
1387eddb1a43SJosef Bacik 		btrfs_info(fs_info, fmt, ##args);			\
1388eddb1a43SJosef Bacik } while (0)
1389eddb1a43SJosef Bacik 
1390eddb1a43SJosef Bacik static void btrfs_emit_options(struct btrfs_fs_info *info,
1391eddb1a43SJosef Bacik 			       struct btrfs_fs_context *old)
1392eddb1a43SJosef Bacik {
1393eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, NODATASUM, "setting nodatasum");
1394eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, DEGRADED, "allowing degraded mounts");
1395eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, NODATASUM, "setting nodatasum");
1396eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, SSD, "enabling ssd optimizations");
1397eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, SSD_SPREAD, "using spread ssd allocation scheme");
1398eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, NOBARRIER, "turning off barriers");
1399eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, NOTREELOG, "disabling tree log");
1400eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, NOLOGREPLAY, "disabling log replay at mount time");
1401eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, FLUSHONCOMMIT, "turning on flush-on-commit");
1402eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, DISCARD_SYNC, "turning on sync discard");
1403eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, DISCARD_ASYNC, "turning on async discard");
1404eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, FREE_SPACE_TREE, "enabling free space tree");
1405eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, SPACE_CACHE, "enabling disk space caching");
1406eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, CLEAR_CACHE, "force clearing of disk cache");
1407eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, AUTO_DEFRAG, "enabling auto defrag");
1408eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, FRAGMENT_DATA, "fragmenting data");
1409eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, FRAGMENT_METADATA, "fragmenting metadata");
1410eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, REF_VERIFY, "doing ref verification");
1411eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, USEBACKUPROOT, "trying to use backup root at mount time");
1412eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, IGNOREBADROOTS, "ignoring bad roots");
1413eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, IGNOREDATACSUMS, "ignoring data csums");
1414eddb1a43SJosef Bacik 
1415eddb1a43SJosef Bacik 	btrfs_info_if_unset(info, old, NODATACOW, "setting datacow");
1416eddb1a43SJosef Bacik 	btrfs_info_if_unset(info, old, SSD, "not using ssd optimizations");
1417eddb1a43SJosef Bacik 	btrfs_info_if_unset(info, old, SSD_SPREAD, "not using spread ssd allocation scheme");
1418eddb1a43SJosef Bacik 	btrfs_info_if_unset(info, old, NOBARRIER, "turning off barriers");
1419eddb1a43SJosef Bacik 	btrfs_info_if_unset(info, old, NOTREELOG, "enabling tree log");
1420eddb1a43SJosef Bacik 	btrfs_info_if_unset(info, old, SPACE_CACHE, "disabling disk space caching");
1421eddb1a43SJosef Bacik 	btrfs_info_if_unset(info, old, FREE_SPACE_TREE, "disabling free space tree");
1422eddb1a43SJosef Bacik 	btrfs_info_if_unset(info, old, AUTO_DEFRAG, "disabling auto defrag");
1423eddb1a43SJosef Bacik 	btrfs_info_if_unset(info, old, COMPRESS, "use no compression");
1424eddb1a43SJosef Bacik 
1425eddb1a43SJosef Bacik 	/* Did the compression settings change? */
1426eddb1a43SJosef Bacik 	if (btrfs_test_opt(info, COMPRESS) &&
1427eddb1a43SJosef Bacik 	    (!old ||
1428eddb1a43SJosef Bacik 	     old->compress_type != info->compress_type ||
1429eddb1a43SJosef Bacik 	     old->compress_level != info->compress_level ||
1430eddb1a43SJosef Bacik 	     (!btrfs_raw_test_opt(old->mount_opt, FORCE_COMPRESS) &&
1431eddb1a43SJosef Bacik 	      btrfs_raw_test_opt(info->mount_opt, FORCE_COMPRESS)))) {
1432eddb1a43SJosef Bacik 		const char *compress_type = btrfs_compress_type2str(info->compress_type);
1433eddb1a43SJosef Bacik 
1434eddb1a43SJosef Bacik 		btrfs_info(info, "%s %s compression, level %d",
1435eddb1a43SJosef Bacik 			   btrfs_test_opt(info, FORCE_COMPRESS) ? "force" : "use",
1436eddb1a43SJosef Bacik 			   compress_type, info->compress_level);
1437eddb1a43SJosef Bacik 	}
1438eddb1a43SJosef Bacik 
1439eddb1a43SJosef Bacik 	if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
1440eddb1a43SJosef Bacik 		btrfs_info(info, "max_inline set to %llu", info->max_inline);
1441eddb1a43SJosef Bacik }
1442eddb1a43SJosef Bacik 
1443eddb1a43SJosef Bacik static int btrfs_reconfigure(struct fs_context *fc)
1444eddb1a43SJosef Bacik {
1445eddb1a43SJosef Bacik 	struct super_block *sb = fc->root->d_sb;
1446eddb1a43SJosef Bacik 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1447eddb1a43SJosef Bacik 	struct btrfs_fs_context *ctx = fc->fs_private;
1448eddb1a43SJosef Bacik 	struct btrfs_fs_context old_ctx;
1449eddb1a43SJosef Bacik 	int ret = 0;
1450f044b318SJosef Bacik 	bool mount_reconfigure = (fc->s_fs_info != NULL);
1451eddb1a43SJosef Bacik 
1452eddb1a43SJosef Bacik 	btrfs_info_to_ctx(fs_info, &old_ctx);
1453eddb1a43SJosef Bacik 
1454*2018ef1dSJosef Bacik 	/*
1455*2018ef1dSJosef Bacik 	 * This is our "bind mount" trick, we don't want to allow the user to do
1456*2018ef1dSJosef Bacik 	 * anything other than mount a different ro/rw and a different subvol,
1457*2018ef1dSJosef Bacik 	 * all of the mount options should be maintained.
1458*2018ef1dSJosef Bacik 	 */
1459*2018ef1dSJosef Bacik 	if (mount_reconfigure)
1460*2018ef1dSJosef Bacik 		ctx->mount_opt = old_ctx.mount_opt;
1461*2018ef1dSJosef Bacik 
1462eddb1a43SJosef Bacik 	sync_filesystem(sb);
1463eddb1a43SJosef Bacik 	set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1464eddb1a43SJosef Bacik 
1465f044b318SJosef Bacik 	if (!mount_reconfigure &&
1466ad21f15bSJosef Bacik 	    !btrfs_check_options(fs_info, &ctx->mount_opt, fc->sb_flags))
1467eddb1a43SJosef Bacik 		return -EINVAL;
1468eddb1a43SJosef Bacik 
1469eddb1a43SJosef Bacik 	ret = btrfs_check_features(fs_info, !(fc->sb_flags & SB_RDONLY));
1470eddb1a43SJosef Bacik 	if (ret < 0)
1471eddb1a43SJosef Bacik 		return ret;
1472eddb1a43SJosef Bacik 
1473eddb1a43SJosef Bacik 	btrfs_ctx_to_info(fs_info, ctx);
1474eddb1a43SJosef Bacik 	btrfs_remount_begin(fs_info, old_ctx.mount_opt, fc->sb_flags);
1475eddb1a43SJosef Bacik 	btrfs_resize_thread_pool(fs_info, fs_info->thread_pool_size,
1476eddb1a43SJosef Bacik 				 old_ctx.thread_pool_size);
1477eddb1a43SJosef Bacik 
1478eddb1a43SJosef Bacik 	if ((bool)btrfs_test_opt(fs_info, FREE_SPACE_TREE) !=
1479eddb1a43SJosef Bacik 	    (bool)btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
1480eddb1a43SJosef Bacik 	    (!sb_rdonly(sb) || (fc->sb_flags & SB_RDONLY))) {
1481eddb1a43SJosef Bacik 		btrfs_warn(fs_info,
1482eddb1a43SJosef Bacik 		"remount supports changing free space tree only from RO to RW");
1483eddb1a43SJosef Bacik 		/* Make sure free space cache options match the state on disk. */
1484eddb1a43SJosef Bacik 		if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
1485eddb1a43SJosef Bacik 			btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
1486eddb1a43SJosef Bacik 			btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
1487eddb1a43SJosef Bacik 		}
1488eddb1a43SJosef Bacik 		if (btrfs_free_space_cache_v1_active(fs_info)) {
1489eddb1a43SJosef Bacik 			btrfs_clear_opt(fs_info->mount_opt, FREE_SPACE_TREE);
1490eddb1a43SJosef Bacik 			btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE);
1491eddb1a43SJosef Bacik 		}
1492eddb1a43SJosef Bacik 	}
1493eddb1a43SJosef Bacik 
1494eddb1a43SJosef Bacik 	ret = 0;
1495eddb1a43SJosef Bacik 	if (!sb_rdonly(sb) && (fc->sb_flags & SB_RDONLY))
1496eddb1a43SJosef Bacik 		ret = btrfs_remount_ro(fs_info);
1497eddb1a43SJosef Bacik 	else if (sb_rdonly(sb) && !(fc->sb_flags & SB_RDONLY))
1498eddb1a43SJosef Bacik 		ret = btrfs_remount_rw(fs_info);
1499eddb1a43SJosef Bacik 	if (ret)
1500eddb1a43SJosef Bacik 		goto restore;
1501eddb1a43SJosef Bacik 
1502eddb1a43SJosef Bacik 	/*
1503eddb1a43SJosef Bacik 	 * If we set the mask during the parameter parsing VFS would reject the
1504eddb1a43SJosef Bacik 	 * remount.  Here we can set the mask and the value will be updated
1505eddb1a43SJosef Bacik 	 * appropriately.
1506eddb1a43SJosef Bacik 	 */
1507eddb1a43SJosef Bacik 	if ((fc->sb_flags & SB_POSIXACL) != (sb->s_flags & SB_POSIXACL))
1508eddb1a43SJosef Bacik 		fc->sb_flags_mask |= SB_POSIXACL;
1509eddb1a43SJosef Bacik 
1510eddb1a43SJosef Bacik 	btrfs_emit_options(fs_info, &old_ctx);
1511eddb1a43SJosef Bacik 	wake_up_process(fs_info->transaction_kthread);
1512eddb1a43SJosef Bacik 	btrfs_remount_cleanup(fs_info, old_ctx.mount_opt);
1513eddb1a43SJosef Bacik 	btrfs_clear_oneshot_options(fs_info);
1514eddb1a43SJosef Bacik 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1515eddb1a43SJosef Bacik 
1516eddb1a43SJosef Bacik 	return 0;
1517eddb1a43SJosef Bacik restore:
1518eddb1a43SJosef Bacik 	btrfs_ctx_to_info(fs_info, &old_ctx);
1519eddb1a43SJosef Bacik 	btrfs_remount_cleanup(fs_info, old_ctx.mount_opt);
1520eddb1a43SJosef Bacik 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1521eddb1a43SJosef Bacik 	return ret;
1522eddb1a43SJosef Bacik }
1523eddb1a43SJosef Bacik 
1524bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */
1525214cc184SDavid Sterba static int btrfs_cmp_device_free_bytes(const void *a, const void *b)
1526bcd53741SArne Jansen {
1527214cc184SDavid Sterba 	const struct btrfs_device_info *dev_info1 = a;
1528214cc184SDavid Sterba 	const struct btrfs_device_info *dev_info2 = b;
1529214cc184SDavid Sterba 
1530214cc184SDavid Sterba 	if (dev_info1->max_avail > dev_info2->max_avail)
1531bcd53741SArne Jansen 		return -1;
1532214cc184SDavid Sterba 	else if (dev_info1->max_avail < dev_info2->max_avail)
1533bcd53741SArne Jansen 		return 1;
1534bcd53741SArne Jansen 	return 0;
1535bcd53741SArne Jansen }
1536bcd53741SArne Jansen 
1537bcd53741SArne Jansen /*
1538bcd53741SArne Jansen  * sort the devices by max_avail, in which max free extent size of each device
1539bcd53741SArne Jansen  * is stored.(Descending Sort)
1540bcd53741SArne Jansen  */
1541bcd53741SArne Jansen static inline void btrfs_descending_sort_devices(
1542bcd53741SArne Jansen 					struct btrfs_device_info *devices,
1543bcd53741SArne Jansen 					size_t nr_devices)
1544bcd53741SArne Jansen {
1545bcd53741SArne Jansen 	sort(devices, nr_devices, sizeof(struct btrfs_device_info),
1546bcd53741SArne Jansen 	     btrfs_cmp_device_free_bytes, NULL);
1547bcd53741SArne Jansen }
1548bcd53741SArne Jansen 
15496d07bcecSMiao Xie /*
15506d07bcecSMiao Xie  * The helper to calc the free space on the devices that can be used to store
15516d07bcecSMiao Xie  * file data.
15526d07bcecSMiao Xie  */
15537e17916bSArnd Bergmann static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
15546bccf3abSJeff Mahoney 					      u64 *free_bytes)
15556d07bcecSMiao Xie {
15566d07bcecSMiao Xie 	struct btrfs_device_info *devices_info;
15576d07bcecSMiao Xie 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
15586d07bcecSMiao Xie 	struct btrfs_device *device;
15596d07bcecSMiao Xie 	u64 type;
15606d07bcecSMiao Xie 	u64 avail_space;
15616d07bcecSMiao Xie 	u64 min_stripe_size;
1562559ca6eaSNikolay Borisov 	int num_stripes = 1;
15636d07bcecSMiao Xie 	int i = 0, nr_devices;
15644f080f57SDavid Sterba 	const struct btrfs_raid_attr *rattr;
15656d07bcecSMiao Xie 
15667e33fd99SJosef Bacik 	/*
156701327610SNicholas D Steeves 	 * We aren't under the device list lock, so this is racy-ish, but good
15687e33fd99SJosef Bacik 	 * enough for our purposes.
15697e33fd99SJosef Bacik 	 */
1570b772a86eSLi Zefan 	nr_devices = fs_info->fs_devices->open_devices;
15717e33fd99SJosef Bacik 	if (!nr_devices) {
15727e33fd99SJosef Bacik 		smp_mb();
15737e33fd99SJosef Bacik 		nr_devices = fs_info->fs_devices->open_devices;
15747e33fd99SJosef Bacik 		ASSERT(nr_devices);
15757e33fd99SJosef Bacik 		if (!nr_devices) {
15767e33fd99SJosef Bacik 			*free_bytes = 0;
15777e33fd99SJosef Bacik 			return 0;
15787e33fd99SJosef Bacik 		}
15797e33fd99SJosef Bacik 	}
15806d07bcecSMiao Xie 
1581d9b0d9baSDulshani Gunawardhana 	devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
15826a44517dSDavid Sterba 			       GFP_KERNEL);
15836d07bcecSMiao Xie 	if (!devices_info)
15846d07bcecSMiao Xie 		return -ENOMEM;
15856d07bcecSMiao Xie 
158601327610SNicholas D Steeves 	/* calc min stripe number for data space allocation */
15871b86826dSJeff Mahoney 	type = btrfs_data_alloc_profile(fs_info);
15884f080f57SDavid Sterba 	rattr = &btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)];
15894f080f57SDavid Sterba 
1590e1ea2beeSDavid Sterba 	if (type & BTRFS_BLOCK_GROUP_RAID0)
159139fb26c3SMiao Xie 		num_stripes = nr_devices;
1592d09cb9e1SDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID1_MASK)
1593d09cb9e1SDavid Sterba 		num_stripes = rattr->ncopies;
1594e1ea2beeSDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID10)
159539fb26c3SMiao Xie 		num_stripes = 4;
15966d07bcecSMiao Xie 
15974f080f57SDavid Sterba 	/* Adjust for more than 1 stripe per device */
15984f080f57SDavid Sterba 	min_stripe_size = rattr->dev_stripes * BTRFS_STRIPE_LEN;
15996d07bcecSMiao Xie 
16007e33fd99SJosef Bacik 	rcu_read_lock();
16017e33fd99SJosef Bacik 	list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
1602e12c9621SAnand Jain 		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
1603e12c9621SAnand Jain 						&device->dev_state) ||
1604401e29c1SAnand Jain 		    !device->bdev ||
1605401e29c1SAnand Jain 		    test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
16066d07bcecSMiao Xie 			continue;
16076d07bcecSMiao Xie 
16087e33fd99SJosef Bacik 		if (i >= nr_devices)
16097e33fd99SJosef Bacik 			break;
16107e33fd99SJosef Bacik 
16116d07bcecSMiao Xie 		avail_space = device->total_bytes - device->bytes_used;
16126d07bcecSMiao Xie 
16136d07bcecSMiao Xie 		/* align with stripe_len */
1614559ca6eaSNikolay Borisov 		avail_space = rounddown(avail_space, BTRFS_STRIPE_LEN);
16156d07bcecSMiao Xie 
16166d07bcecSMiao Xie 		/*
161737f85ec3SQu Wenruo 		 * Ensure we have at least min_stripe_size on top of the
161837f85ec3SQu Wenruo 		 * reserved space on the device.
16196d07bcecSMiao Xie 		 */
162037f85ec3SQu Wenruo 		if (avail_space <= BTRFS_DEVICE_RANGE_RESERVED + min_stripe_size)
16216d07bcecSMiao Xie 			continue;
16226d07bcecSMiao Xie 
162337f85ec3SQu Wenruo 		avail_space -= BTRFS_DEVICE_RANGE_RESERVED;
1624559ca6eaSNikolay Borisov 
16256d07bcecSMiao Xie 		devices_info[i].dev = device;
16266d07bcecSMiao Xie 		devices_info[i].max_avail = avail_space;
16276d07bcecSMiao Xie 
16286d07bcecSMiao Xie 		i++;
16296d07bcecSMiao Xie 	}
16307e33fd99SJosef Bacik 	rcu_read_unlock();
16316d07bcecSMiao Xie 
16326d07bcecSMiao Xie 	nr_devices = i;
16336d07bcecSMiao Xie 
16346d07bcecSMiao Xie 	btrfs_descending_sort_devices(devices_info, nr_devices);
16356d07bcecSMiao Xie 
16366d07bcecSMiao Xie 	i = nr_devices - 1;
16376d07bcecSMiao Xie 	avail_space = 0;
1638559ca6eaSNikolay Borisov 	while (nr_devices >= rattr->devs_min) {
1639559ca6eaSNikolay Borisov 		num_stripes = min(num_stripes, nr_devices);
164039fb26c3SMiao Xie 
16416d07bcecSMiao Xie 		if (devices_info[i].max_avail >= min_stripe_size) {
16426d07bcecSMiao Xie 			int j;
16436d07bcecSMiao Xie 			u64 alloc_size;
16446d07bcecSMiao Xie 
164539fb26c3SMiao Xie 			avail_space += devices_info[i].max_avail * num_stripes;
16466d07bcecSMiao Xie 			alloc_size = devices_info[i].max_avail;
164739fb26c3SMiao Xie 			for (j = i + 1 - num_stripes; j <= i; j++)
16486d07bcecSMiao Xie 				devices_info[j].max_avail -= alloc_size;
16496d07bcecSMiao Xie 		}
16506d07bcecSMiao Xie 		i--;
16516d07bcecSMiao Xie 		nr_devices--;
16526d07bcecSMiao Xie 	}
16536d07bcecSMiao Xie 
16546d07bcecSMiao Xie 	kfree(devices_info);
16556d07bcecSMiao Xie 	*free_bytes = avail_space;
16566d07bcecSMiao Xie 	return 0;
16576d07bcecSMiao Xie }
16586d07bcecSMiao Xie 
1659ba7b6e62SDavid Sterba /*
1660ba7b6e62SDavid Sterba  * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
1661ba7b6e62SDavid Sterba  *
1662ba7b6e62SDavid Sterba  * If there's a redundant raid level at DATA block groups, use the respective
1663ba7b6e62SDavid Sterba  * multiplier to scale the sizes.
1664ba7b6e62SDavid Sterba  *
1665ba7b6e62SDavid Sterba  * Unused device space usage is based on simulating the chunk allocator
16660d0c71b3SDavid Sterba  * algorithm that respects the device sizes and order of allocations.  This is
16670d0c71b3SDavid Sterba  * a close approximation of the actual use but there are other factors that may
16680d0c71b3SDavid Sterba  * change the result (like a new metadata chunk).
1669ba7b6e62SDavid Sterba  *
1670ca8a51b3SDavid Sterba  * If metadata is exhausted, f_bavail will be 0.
1671ba7b6e62SDavid Sterba  */
16728fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
16738fd17795SChris Mason {
1674815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
1675815745cfSAl Viro 	struct btrfs_super_block *disk_super = fs_info->super_copy;
1676bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
1677bd4d1088SJosef Bacik 	u64 total_used = 0;
16786d07bcecSMiao Xie 	u64 total_free_data = 0;
1679ca8a51b3SDavid Sterba 	u64 total_free_meta = 0;
1680265fdfa6SDavid Sterba 	u32 bits = fs_info->sectorsize_bits;
1681de37aa51SNikolay Borisov 	__be32 *fsid = (__be32 *)fs_info->fs_devices->fsid;
1682ba7b6e62SDavid Sterba 	unsigned factor = 1;
1683ba7b6e62SDavid Sterba 	struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
16846d07bcecSMiao Xie 	int ret;
1685ca8a51b3SDavid Sterba 	u64 thresh = 0;
1686ae02d1bdSLuis de Bethencourt 	int mixed = 0;
16878fd17795SChris Mason 
168872804905SJosef Bacik 	list_for_each_entry(found, &fs_info->space_info, list) {
16896d07bcecSMiao Xie 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
1690ba7b6e62SDavid Sterba 			int i;
1691ba7b6e62SDavid Sterba 
16926d07bcecSMiao Xie 			total_free_data += found->disk_total - found->disk_used;
16936d07bcecSMiao Xie 			total_free_data -=
16946d07bcecSMiao Xie 				btrfs_account_ro_block_groups_free_space(found);
1695ba7b6e62SDavid Sterba 
1696ba7b6e62SDavid Sterba 			for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
169746df06b8SDavid Sterba 				if (!list_empty(&found->block_groups[i]))
169846df06b8SDavid Sterba 					factor = btrfs_bg_type_to_factor(
169946df06b8SDavid Sterba 						btrfs_raid_array[i].bg_flag);
1700ba7b6e62SDavid Sterba 			}
17016d07bcecSMiao Xie 		}
1702ae02d1bdSLuis de Bethencourt 
1703ae02d1bdSLuis de Bethencourt 		/*
170467da05b3SColin Ian King 		 * Metadata in mixed block group profiles are accounted in data
1705ae02d1bdSLuis de Bethencourt 		 */
1706ae02d1bdSLuis de Bethencourt 		if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) {
1707ae02d1bdSLuis de Bethencourt 			if (found->flags & BTRFS_BLOCK_GROUP_DATA)
1708ae02d1bdSLuis de Bethencourt 				mixed = 1;
1709ae02d1bdSLuis de Bethencourt 			else
1710ae02d1bdSLuis de Bethencourt 				total_free_meta += found->disk_total -
1711ae02d1bdSLuis de Bethencourt 					found->disk_used;
1712ae02d1bdSLuis de Bethencourt 		}
17136d07bcecSMiao Xie 
1714b742bb82SYan, Zheng 		total_used += found->disk_used;
171589a55897SJosef Bacik 	}
1716ba7b6e62SDavid Sterba 
1717ba7b6e62SDavid Sterba 	buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
1718ba7b6e62SDavid Sterba 	buf->f_blocks >>= bits;
1719ba7b6e62SDavid Sterba 	buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
1720ba7b6e62SDavid Sterba 
1721ba7b6e62SDavid Sterba 	/* Account global block reserve as used, it's in logical size already */
1722ba7b6e62SDavid Sterba 	spin_lock(&block_rsv->lock);
172341b34accSLuis de Bethencourt 	/* Mixed block groups accounting is not byte-accurate, avoid overflow */
172441b34accSLuis de Bethencourt 	if (buf->f_bfree >= block_rsv->size >> bits)
1725ba7b6e62SDavid Sterba 		buf->f_bfree -= block_rsv->size >> bits;
172641b34accSLuis de Bethencourt 	else
172741b34accSLuis de Bethencourt 		buf->f_bfree = 0;
1728ba7b6e62SDavid Sterba 	spin_unlock(&block_rsv->lock);
1729ba7b6e62SDavid Sterba 
17300d95c1beSDavid Sterba 	buf->f_bavail = div_u64(total_free_data, factor);
17316bccf3abSJeff Mahoney 	ret = btrfs_calc_avail_data_space(fs_info, &total_free_data);
17327e33fd99SJosef Bacik 	if (ret)
17336d07bcecSMiao Xie 		return ret;
1734ba7b6e62SDavid Sterba 	buf->f_bavail += div_u64(total_free_data, factor);
17356d07bcecSMiao Xie 	buf->f_bavail = buf->f_bavail >> bits;
1736d397712bSChris Mason 
1737ca8a51b3SDavid Sterba 	/*
1738ca8a51b3SDavid Sterba 	 * We calculate the remaining metadata space minus global reserve. If
1739ca8a51b3SDavid Sterba 	 * this is (supposedly) smaller than zero, there's no space. But this
1740ca8a51b3SDavid Sterba 	 * does not hold in practice, the exhausted state happens where's still
1741ca8a51b3SDavid Sterba 	 * some positive delta. So we apply some guesswork and compare the
1742ca8a51b3SDavid Sterba 	 * delta to a 4M threshold.  (Practically observed delta was ~2M.)
1743ca8a51b3SDavid Sterba 	 *
1744ca8a51b3SDavid Sterba 	 * We probably cannot calculate the exact threshold value because this
1745ca8a51b3SDavid Sterba 	 * depends on the internal reservations requested by various
1746ca8a51b3SDavid Sterba 	 * operations, so some operations that consume a few metadata will
1747ca8a51b3SDavid Sterba 	 * succeed even if the Avail is zero. But this is better than the other
1748ca8a51b3SDavid Sterba 	 * way around.
1749ca8a51b3SDavid Sterba 	 */
1750d4417e22SNikolay Borisov 	thresh = SZ_4M;
1751ca8a51b3SDavid Sterba 
1752d55966c4SJosef Bacik 	/*
1753d55966c4SJosef Bacik 	 * We only want to claim there's no available space if we can no longer
1754d55966c4SJosef Bacik 	 * allocate chunks for our metadata profile and our global reserve will
1755d55966c4SJosef Bacik 	 * not fit in the free metadata space.  If we aren't ->full then we
1756d55966c4SJosef Bacik 	 * still can allocate chunks and thus are fine using the currently
1757d55966c4SJosef Bacik 	 * calculated f_bavail.
1758d55966c4SJosef Bacik 	 */
1759d55966c4SJosef Bacik 	if (!mixed && block_rsv->space_info->full &&
176058bfe2ccSJosef Bacik 	    (total_free_meta < thresh || total_free_meta - thresh < block_rsv->size))
1761ca8a51b3SDavid Sterba 		buf->f_bavail = 0;
1762ca8a51b3SDavid Sterba 
1763ba7b6e62SDavid Sterba 	buf->f_type = BTRFS_SUPER_MAGIC;
1764ba7b6e62SDavid Sterba 	buf->f_bsize = dentry->d_sb->s_blocksize;
1765ba7b6e62SDavid Sterba 	buf->f_namelen = BTRFS_NAME_LEN;
1766ba7b6e62SDavid Sterba 
17679d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
17689d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
17699d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
17709d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
17719d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
177232d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
17734fd786e6SMisono Tomohiro 	buf->f_fsid.val[0] ^=
17744fd786e6SMisono Tomohiro 		BTRFS_I(d_inode(dentry))->root->root_key.objectid >> 32;
17754fd786e6SMisono Tomohiro 	buf->f_fsid.val[1] ^=
17764fd786e6SMisono Tomohiro 		BTRFS_I(d_inode(dentry))->root->root_key.objectid;
177732d48fa1SDavid Woodhouse 
17788fd17795SChris Mason 	return 0;
17798fd17795SChris Mason }
1780b5133862SChris Mason 
17813bb17a25SJosef Bacik static int btrfs_fc_test_super(struct super_block *sb, struct fs_context *fc)
17823bb17a25SJosef Bacik {
17833bb17a25SJosef Bacik 	struct btrfs_fs_info *p = fc->s_fs_info;
17843bb17a25SJosef Bacik 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
17853bb17a25SJosef Bacik 
17863bb17a25SJosef Bacik 	return fs_info->fs_devices == p->fs_devices;
17873bb17a25SJosef Bacik }
17883bb17a25SJosef Bacik 
17893bb17a25SJosef Bacik static int btrfs_get_tree_super(struct fs_context *fc)
17903bb17a25SJosef Bacik {
17913bb17a25SJosef Bacik 	struct btrfs_fs_info *fs_info = fc->s_fs_info;
17923bb17a25SJosef Bacik 	struct btrfs_fs_context *ctx = fc->fs_private;
17933bb17a25SJosef Bacik 	struct btrfs_fs_devices *fs_devices = NULL;
17943bb17a25SJosef Bacik 	struct block_device *bdev;
17953bb17a25SJosef Bacik 	struct btrfs_device *device;
17963bb17a25SJosef Bacik 	struct super_block *sb;
17973bb17a25SJosef Bacik 	blk_mode_t mode = sb_open_mode(fc->sb_flags);
17983bb17a25SJosef Bacik 	int ret;
17993bb17a25SJosef Bacik 
18003bb17a25SJosef Bacik 	btrfs_ctx_to_info(fs_info, ctx);
18013bb17a25SJosef Bacik 	mutex_lock(&uuid_mutex);
18023bb17a25SJosef Bacik 
18033bb17a25SJosef Bacik 	/*
18043bb17a25SJosef Bacik 	 * With 'true' passed to btrfs_scan_one_device() (mount time) we expect
18053bb17a25SJosef Bacik 	 * either a valid device or an error.
18063bb17a25SJosef Bacik 	 */
18073bb17a25SJosef Bacik 	device = btrfs_scan_one_device(fc->source, mode, true);
18083bb17a25SJosef Bacik 	ASSERT(device != NULL);
18093bb17a25SJosef Bacik 	if (IS_ERR(device)) {
18103bb17a25SJosef Bacik 		mutex_unlock(&uuid_mutex);
18113bb17a25SJosef Bacik 		return PTR_ERR(device);
18123bb17a25SJosef Bacik 	}
18133bb17a25SJosef Bacik 
18143bb17a25SJosef Bacik 	fs_devices = device->fs_devices;
18153bb17a25SJosef Bacik 	fs_info->fs_devices = fs_devices;
18163bb17a25SJosef Bacik 
18173bb17a25SJosef Bacik 	ret = btrfs_open_devices(fs_devices, mode, &btrfs_fs_type);
18183bb17a25SJosef Bacik 	mutex_unlock(&uuid_mutex);
18193bb17a25SJosef Bacik 	if (ret)
18203bb17a25SJosef Bacik 		return ret;
18213bb17a25SJosef Bacik 
18223bb17a25SJosef Bacik 	if (!(fc->sb_flags & SB_RDONLY) && fs_devices->rw_devices == 0) {
18233bb17a25SJosef Bacik 		ret = -EACCES;
18243bb17a25SJosef Bacik 		goto error;
18253bb17a25SJosef Bacik 	}
18263bb17a25SJosef Bacik 
18273bb17a25SJosef Bacik 	bdev = fs_devices->latest_dev->bdev;
18283bb17a25SJosef Bacik 
18293bb17a25SJosef Bacik 	/*
18303bb17a25SJosef Bacik 	 * From now on the error handling is not straightforward.
18313bb17a25SJosef Bacik 	 *
18323bb17a25SJosef Bacik 	 * If successful, this will transfer the fs_info into the super block,
18333bb17a25SJosef Bacik 	 * and fc->s_fs_info will be NULL.  However if there's an existing
18343bb17a25SJosef Bacik 	 * super, we'll still have fc->s_fs_info populated.  If we error
18353bb17a25SJosef Bacik 	 * completely out it'll be cleaned up when we drop the fs_context,
18363bb17a25SJosef Bacik 	 * otherwise it's tied to the lifetime of the super_block.
18373bb17a25SJosef Bacik 	 */
18383bb17a25SJosef Bacik 	sb = sget_fc(fc, btrfs_fc_test_super, set_anon_super_fc);
18393bb17a25SJosef Bacik 	if (IS_ERR(sb)) {
18403bb17a25SJosef Bacik 		ret = PTR_ERR(sb);
18413bb17a25SJosef Bacik 		goto error;
18423bb17a25SJosef Bacik 	}
18433bb17a25SJosef Bacik 
184441d46b29SJosef Bacik 	set_device_specific_options(fs_info);
184541d46b29SJosef Bacik 
18463bb17a25SJosef Bacik 	if (sb->s_root) {
18473bb17a25SJosef Bacik 		btrfs_close_devices(fs_devices);
18483bb17a25SJosef Bacik 		if ((fc->sb_flags ^ sb->s_flags) & SB_RDONLY)
18493bb17a25SJosef Bacik 			ret = -EBUSY;
18503bb17a25SJosef Bacik 	} else {
18513bb17a25SJosef Bacik 		snprintf(sb->s_id, sizeof(sb->s_id), "%pg", bdev);
18523bb17a25SJosef Bacik 		shrinker_debugfs_rename(sb->s_shrink, "sb-btrfs:%s", sb->s_id);
18533bb17a25SJosef Bacik 		btrfs_sb(sb)->bdev_holder = &btrfs_fs_type;
18543bb17a25SJosef Bacik 		ret = btrfs_fill_super(sb, fs_devices, NULL);
18553bb17a25SJosef Bacik 	}
18563bb17a25SJosef Bacik 
18573bb17a25SJosef Bacik 	if (ret) {
18583bb17a25SJosef Bacik 		deactivate_locked_super(sb);
18593bb17a25SJosef Bacik 		return ret;
18603bb17a25SJosef Bacik 	}
18613bb17a25SJosef Bacik 
186283e3a40aSJosef Bacik 	btrfs_clear_oneshot_options(fs_info);
186383e3a40aSJosef Bacik 
18643bb17a25SJosef Bacik 	fc->root = dget(sb->s_root);
18653bb17a25SJosef Bacik 	return 0;
18663bb17a25SJosef Bacik 
18673bb17a25SJosef Bacik error:
18683bb17a25SJosef Bacik 	btrfs_close_devices(fs_devices);
18693bb17a25SJosef Bacik 	return ret;
18703bb17a25SJosef Bacik }
18713bb17a25SJosef Bacik 
1872f044b318SJosef Bacik /*
1873f044b318SJosef Bacik  * Ever since commit 0723a0473fb4 ("btrfs: allow mounting btrfs subvolumes
1874f044b318SJosef Bacik  * with different ro/rw options") the following works:
1875f044b318SJosef Bacik  *
1876f044b318SJosef Bacik  *        (i) mount /dev/sda3 -o subvol=foo,ro /mnt/foo
1877f044b318SJosef Bacik  *       (ii) mount /dev/sda3 -o subvol=bar,rw /mnt/bar
1878f044b318SJosef Bacik  *
1879f044b318SJosef Bacik  * which looks nice and innocent but is actually pretty intricate and deserves
1880f044b318SJosef Bacik  * a long comment.
1881f044b318SJosef Bacik  *
1882f044b318SJosef Bacik  * On another filesystem a subvolume mount is close to something like:
1883f044b318SJosef Bacik  *
1884f044b318SJosef Bacik  *	(iii) # create rw superblock + initial mount
1885f044b318SJosef Bacik  *	      mount -t xfs /dev/sdb /opt/
1886f044b318SJosef Bacik  *
1887f044b318SJosef Bacik  *	      # create ro bind mount
1888f044b318SJosef Bacik  *	      mount --bind -o ro /opt/foo /mnt/foo
1889f044b318SJosef Bacik  *
1890f044b318SJosef Bacik  *	      # unmount initial mount
1891f044b318SJosef Bacik  *	      umount /opt
1892f044b318SJosef Bacik  *
1893f044b318SJosef Bacik  * Of course, there's some special subvolume sauce and there's the fact that the
1894f044b318SJosef Bacik  * sb->s_root dentry is really swapped after mount_subtree(). But conceptually
1895f044b318SJosef Bacik  * it's very close and will help us understand the issue.
1896f044b318SJosef Bacik  *
1897f044b318SJosef Bacik  * The old mount API didn't cleanly distinguish between a mount being made ro
1898f044b318SJosef Bacik  * and a superblock being made ro.  The only way to change the ro state of
1899f044b318SJosef Bacik  * either object was by passing ms_rdonly. If a new mount was created via
1900f044b318SJosef Bacik  * mount(2) such as:
1901f044b318SJosef Bacik  *
1902f044b318SJosef Bacik  *      mount("/dev/sdb", "/mnt", "xfs", ms_rdonly, null);
1903f044b318SJosef Bacik  *
1904f044b318SJosef Bacik  * the MS_RDONLY flag being specified had two effects:
1905f044b318SJosef Bacik  *
1906f044b318SJosef Bacik  * (1) MNT_READONLY was raised -> the resulting mount got
1907f044b318SJosef Bacik  *     @mnt->mnt_flags |= MNT_READONLY raised.
1908f044b318SJosef Bacik  *
1909f044b318SJosef Bacik  * (2) MS_RDONLY was passed to the filesystem's mount method and the filesystems
1910f044b318SJosef Bacik  *     made the superblock ro. Note, how SB_RDONLY has the same value as
1911f044b318SJosef Bacik  *     ms_rdonly and is raised whenever MS_RDONLY is passed through mount(2).
1912f044b318SJosef Bacik  *
1913f044b318SJosef Bacik  * Creating a subtree mount via (iii) ends up leaving a rw superblock with a
1914f044b318SJosef Bacik  * subtree mounted ro.
1915f044b318SJosef Bacik  *
1916f044b318SJosef Bacik  * But consider the effect on the old mount API on btrfs subvolume mounting
1917f044b318SJosef Bacik  * which combines the distinct step in (iii) into a single step.
1918f044b318SJosef Bacik  *
1919f044b318SJosef Bacik  * By issuing (i) both the mount and the superblock are turned ro. Now when (ii)
1920f044b318SJosef Bacik  * is issued the superblock is ro and thus even if the mount created for (ii) is
1921f044b318SJosef Bacik  * rw it wouldn't help. Hence, btrfs needed to transition the superblock from ro
1922f044b318SJosef Bacik  * to rw for (ii) which it did using an internal remount call.
1923f044b318SJosef Bacik  *
1924f044b318SJosef Bacik  * IOW, subvolume mounting was inherently complicated due to the ambiguity of
1925f044b318SJosef Bacik  * MS_RDONLY in mount(2). Note, this ambiguity has mount(8) always translate
1926f044b318SJosef Bacik  * "ro" to MS_RDONLY. IOW, in both (i) and (ii) "ro" becomes MS_RDONLY when
1927f044b318SJosef Bacik  * passed by mount(8) to mount(2).
1928f044b318SJosef Bacik  *
1929f044b318SJosef Bacik  * Enter the new mount API. The new mount API disambiguates making a mount ro
1930f044b318SJosef Bacik  * and making a superblock ro.
1931f044b318SJosef Bacik  *
1932f044b318SJosef Bacik  * (3) To turn a mount ro the MOUNT_ATTR_ONLY flag can be used with either
1933f044b318SJosef Bacik  *     fsmount() or mount_setattr() this is a pure VFS level change for a
1934f044b318SJosef Bacik  *     specific mount or mount tree that is never seen by the filesystem itself.
1935f044b318SJosef Bacik  *
1936f044b318SJosef Bacik  * (4) To turn a superblock ro the "ro" flag must be used with
1937f044b318SJosef Bacik  *     fsconfig(FSCONFIG_SET_FLAG, "ro"). This option is seen by the filesystem
1938f044b318SJosef Bacik  *     in fc->sb_flags.
1939f044b318SJosef Bacik  *
1940f044b318SJosef Bacik  * This disambiguation has rather positive consequences.  Mounting a subvolume
1941f044b318SJosef Bacik  * ro will not also turn the superblock ro. Only the mount for the subvolume
1942f044b318SJosef Bacik  * will become ro.
1943f044b318SJosef Bacik  *
1944f044b318SJosef Bacik  * So, if the superblock creation request comes from the new mount API the
1945f044b318SJosef Bacik  * caller must have explicitly done:
1946f044b318SJosef Bacik  *
1947f044b318SJosef Bacik  *      fsconfig(FSCONFIG_SET_FLAG, "ro")
1948f044b318SJosef Bacik  *      fsmount/mount_setattr(MOUNT_ATTR_RDONLY)
1949f044b318SJosef Bacik  *
1950f044b318SJosef Bacik  * IOW, at some point the caller must have explicitly turned the whole
1951f044b318SJosef Bacik  * superblock ro and we shouldn't just undo it like we did for the old mount
1952f044b318SJosef Bacik  * API. In any case, it lets us avoid the hack in the new mount API.
1953f044b318SJosef Bacik  *
1954f044b318SJosef Bacik  * Consequently, the remounting hack must only be used for requests originating
1955f044b318SJosef Bacik  * from the old mount API and should be marked for full deprecation so it can be
1956f044b318SJosef Bacik  * turned off in a couple of years.
1957f044b318SJosef Bacik  *
1958f044b318SJosef Bacik  * The new mount API has no reason to support this hack.
1959f044b318SJosef Bacik  */
1960f044b318SJosef Bacik static struct vfsmount *btrfs_reconfigure_for_mount(struct fs_context *fc)
1961f044b318SJosef Bacik {
1962f044b318SJosef Bacik 	struct vfsmount *mnt;
1963f044b318SJosef Bacik 	int ret;
1964f044b318SJosef Bacik 	const bool ro2rw = !(fc->sb_flags & SB_RDONLY);
1965f044b318SJosef Bacik 
1966f044b318SJosef Bacik 	/*
1967f044b318SJosef Bacik 	 * We got an EBUSY because our SB_RDONLY flag didn't match the existing
1968f044b318SJosef Bacik 	 * super block, so invert our setting here and retry the mount so we
1969f044b318SJosef Bacik 	 * can get our vfsmount.
1970f044b318SJosef Bacik 	 */
1971f044b318SJosef Bacik 	if (ro2rw)
1972f044b318SJosef Bacik 		fc->sb_flags |= SB_RDONLY;
1973f044b318SJosef Bacik 	else
1974f044b318SJosef Bacik 		fc->sb_flags &= ~SB_RDONLY;
1975f044b318SJosef Bacik 
1976f044b318SJosef Bacik 	mnt = fc_mount(fc);
1977f044b318SJosef Bacik 	if (IS_ERR(mnt))
1978f044b318SJosef Bacik 		return mnt;
1979f044b318SJosef Bacik 
1980f044b318SJosef Bacik 	if (!fc->oldapi || !ro2rw)
1981f044b318SJosef Bacik 		return mnt;
1982f044b318SJosef Bacik 
1983f044b318SJosef Bacik 	/* We need to convert to rw, call reconfigure. */
1984f044b318SJosef Bacik 	fc->sb_flags &= ~SB_RDONLY;
1985f044b318SJosef Bacik 	down_write(&mnt->mnt_sb->s_umount);
1986f044b318SJosef Bacik 	ret = btrfs_reconfigure(fc);
1987f044b318SJosef Bacik 	up_write(&mnt->mnt_sb->s_umount);
1988f044b318SJosef Bacik 	if (ret) {
1989f044b318SJosef Bacik 		mntput(mnt);
1990f044b318SJosef Bacik 		return ERR_PTR(ret);
1991f044b318SJosef Bacik 	}
1992f044b318SJosef Bacik 	return mnt;
1993f044b318SJosef Bacik }
1994f044b318SJosef Bacik 
19953bb17a25SJosef Bacik static int btrfs_get_tree_subvol(struct fs_context *fc)
19963bb17a25SJosef Bacik {
19973bb17a25SJosef Bacik 	struct btrfs_fs_info *fs_info = NULL;
19983bb17a25SJosef Bacik 	struct btrfs_fs_context *ctx = fc->fs_private;
19993bb17a25SJosef Bacik 	struct fs_context *dup_fc;
20003bb17a25SJosef Bacik 	struct dentry *dentry;
20013bb17a25SJosef Bacik 	struct vfsmount *mnt;
20023bb17a25SJosef Bacik 
20033bb17a25SJosef Bacik 	/*
20043bb17a25SJosef Bacik 	 * Setup a dummy root and fs_info for test/set super.  This is because
20053bb17a25SJosef Bacik 	 * we don't actually fill this stuff out until open_ctree, but we need
20063bb17a25SJosef Bacik 	 * then open_ctree will properly initialize the file system specific
20073bb17a25SJosef Bacik 	 * settings later.  btrfs_init_fs_info initializes the static elements
20083bb17a25SJosef Bacik 	 * of the fs_info (locks and such) to make cleanup easier if we find a
20093bb17a25SJosef Bacik 	 * superblock with our given fs_devices later on at sget() time.
20103bb17a25SJosef Bacik 	 */
20113bb17a25SJosef Bacik 	fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL);
20123bb17a25SJosef Bacik 	if (!fs_info)
20133bb17a25SJosef Bacik 		return -ENOMEM;
20143bb17a25SJosef Bacik 
20153bb17a25SJosef Bacik 	fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
20163bb17a25SJosef Bacik 	fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
20173bb17a25SJosef Bacik 	if (!fs_info->super_copy || !fs_info->super_for_commit) {
20183bb17a25SJosef Bacik 		btrfs_free_fs_info(fs_info);
20193bb17a25SJosef Bacik 		return -ENOMEM;
20203bb17a25SJosef Bacik 	}
20213bb17a25SJosef Bacik 	btrfs_init_fs_info(fs_info);
20223bb17a25SJosef Bacik 
20233bb17a25SJosef Bacik 	dup_fc = vfs_dup_fs_context(fc);
20243bb17a25SJosef Bacik 	if (IS_ERR(dup_fc)) {
20253bb17a25SJosef Bacik 		btrfs_free_fs_info(fs_info);
20263bb17a25SJosef Bacik 		return PTR_ERR(dup_fc);
20273bb17a25SJosef Bacik 	}
20283bb17a25SJosef Bacik 
20293bb17a25SJosef Bacik 	/*
20303bb17a25SJosef Bacik 	 * When we do the sget_fc this gets transferred to the sb, so we only
20313bb17a25SJosef Bacik 	 * need to set it on the dup_fc as this is what creates the super block.
20323bb17a25SJosef Bacik 	 */
20333bb17a25SJosef Bacik 	dup_fc->s_fs_info = fs_info;
20343bb17a25SJosef Bacik 
20353bb17a25SJosef Bacik 	/*
20363bb17a25SJosef Bacik 	 * We'll do the security settings in our btrfs_get_tree_super() mount
20373bb17a25SJosef Bacik 	 * loop, they were duplicated into dup_fc, we can drop the originals
20383bb17a25SJosef Bacik 	 * here.
20393bb17a25SJosef Bacik 	 */
20403bb17a25SJosef Bacik 	security_free_mnt_opts(&fc->security);
20413bb17a25SJosef Bacik 	fc->security = NULL;
20423bb17a25SJosef Bacik 
20433bb17a25SJosef Bacik 	mnt = fc_mount(dup_fc);
2044f044b318SJosef Bacik 	if (PTR_ERR_OR_ZERO(mnt) == -EBUSY)
2045f044b318SJosef Bacik 		mnt = btrfs_reconfigure_for_mount(dup_fc);
20463bb17a25SJosef Bacik 	put_fs_context(dup_fc);
20473bb17a25SJosef Bacik 	if (IS_ERR(mnt))
20483bb17a25SJosef Bacik 		return PTR_ERR(mnt);
20493bb17a25SJosef Bacik 
20503bb17a25SJosef Bacik 	/*
20513bb17a25SJosef Bacik 	 * This free's ->subvol_name, because if it isn't set we have to
20523bb17a25SJosef Bacik 	 * allocate a buffer to hold the subvol_name, so we just drop our
20533bb17a25SJosef Bacik 	 * reference to it here.
20543bb17a25SJosef Bacik 	 */
20553bb17a25SJosef Bacik 	dentry = mount_subvol(ctx->subvol_name, ctx->subvol_objectid, mnt);
20563bb17a25SJosef Bacik 	ctx->subvol_name = NULL;
20573bb17a25SJosef Bacik 	if (IS_ERR(dentry))
20583bb17a25SJosef Bacik 		return PTR_ERR(dentry);
20593bb17a25SJosef Bacik 
20603bb17a25SJosef Bacik 	fc->root = dentry;
20613bb17a25SJosef Bacik 	return 0;
20623bb17a25SJosef Bacik }
20633bb17a25SJosef Bacik 
20643bb17a25SJosef Bacik static int btrfs_get_tree(struct fs_context *fc)
20653bb17a25SJosef Bacik {
20663bb17a25SJosef Bacik 	/*
20673bb17a25SJosef Bacik 	 * Since we use mount_subtree to mount the default/specified subvol, we
20683bb17a25SJosef Bacik 	 * have to do mounts in two steps.
20693bb17a25SJosef Bacik 	 *
20703bb17a25SJosef Bacik 	 * First pass through we call btrfs_get_tree_subvol(), this is just a
20713bb17a25SJosef Bacik 	 * wrapper around fc_mount() to call back into here again, and this time
20723bb17a25SJosef Bacik 	 * we'll call btrfs_get_tree_super().  This will do the open_ctree() and
20733bb17a25SJosef Bacik 	 * everything to open the devices and file system.  Then we return back
20743bb17a25SJosef Bacik 	 * with a fully constructed vfsmount in btrfs_get_tree_subvol(), and
20753bb17a25SJosef Bacik 	 * from there we can do our mount_subvol() call, which will lookup
20763bb17a25SJosef Bacik 	 * whichever subvol we're mounting and setup this fc with the
20773bb17a25SJosef Bacik 	 * appropriate dentry for the subvol.
20783bb17a25SJosef Bacik 	 */
20793bb17a25SJosef Bacik 	if (fc->s_fs_info)
20803bb17a25SJosef Bacik 		return btrfs_get_tree_super(fc);
20813bb17a25SJosef Bacik 	return btrfs_get_tree_subvol(fc);
20823bb17a25SJosef Bacik }
20833bb17a25SJosef Bacik 
2084aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb)
2085aea52e19SAl Viro {
2086815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2087aea52e19SAl Viro 	kill_anon_super(sb);
20880d4b0463SJosef Bacik 	btrfs_free_fs_info(fs_info);
2089aea52e19SAl Viro }
2090aea52e19SAl Viro 
20910f85e244SJosef Bacik static void btrfs_free_fs_context(struct fs_context *fc)
20920f85e244SJosef Bacik {
20930f85e244SJosef Bacik 	struct btrfs_fs_context *ctx = fc->fs_private;
20943bb17a25SJosef Bacik 	struct btrfs_fs_info *fs_info = fc->s_fs_info;
20950f85e244SJosef Bacik 
20963bb17a25SJosef Bacik 	if (fs_info)
20973bb17a25SJosef Bacik 		btrfs_free_fs_info(fs_info);
20980f85e244SJosef Bacik 
20993bb17a25SJosef Bacik 	if (ctx && refcount_dec_and_test(&ctx->refs)) {
21000f85e244SJosef Bacik 		kfree(ctx->subvol_name);
21010f85e244SJosef Bacik 		kfree(ctx);
21020f85e244SJosef Bacik 	}
21033bb17a25SJosef Bacik }
21043bb17a25SJosef Bacik 
21053bb17a25SJosef Bacik static int btrfs_dup_fs_context(struct fs_context *fc, struct fs_context *src_fc)
21063bb17a25SJosef Bacik {
21073bb17a25SJosef Bacik 	struct btrfs_fs_context *ctx = src_fc->fs_private;
21083bb17a25SJosef Bacik 
21093bb17a25SJosef Bacik 	/*
21103bb17a25SJosef Bacik 	 * Give a ref to our ctx to this dup, as we want to keep it around for
21113bb17a25SJosef Bacik 	 * our original fc so we can have the subvolume name or objectid.
21123bb17a25SJosef Bacik 	 *
21133bb17a25SJosef Bacik 	 * We unset ->source in the original fc because the dup needs it for
21143bb17a25SJosef Bacik 	 * mounting, and then once we free the dup it'll free ->source, so we
21153bb17a25SJosef Bacik 	 * need to make sure we're only pointing to it in one fc.
21163bb17a25SJosef Bacik 	 */
21173bb17a25SJosef Bacik 	refcount_inc(&ctx->refs);
21183bb17a25SJosef Bacik 	fc->fs_private = ctx;
21193bb17a25SJosef Bacik 	fc->source = src_fc->source;
21203bb17a25SJosef Bacik 	src_fc->source = NULL;
21213bb17a25SJosef Bacik 	return 0;
21223bb17a25SJosef Bacik }
21230f85e244SJosef Bacik 
21240f85e244SJosef Bacik static const struct fs_context_operations btrfs_fs_context_ops = {
212517b36120SJosef Bacik 	.parse_param	= btrfs_parse_param,
2126eddb1a43SJosef Bacik 	.reconfigure	= btrfs_reconfigure,
21273bb17a25SJosef Bacik 	.get_tree	= btrfs_get_tree,
21283bb17a25SJosef Bacik 	.dup		= btrfs_dup_fs_context,
21290f85e244SJosef Bacik 	.free		= btrfs_free_fs_context,
213017b36120SJosef Bacik };
213117b36120SJosef Bacik 
2132ad21f15bSJosef Bacik static int btrfs_init_fs_context(struct fs_context *fc)
21330f85e244SJosef Bacik {
21340f85e244SJosef Bacik 	struct btrfs_fs_context *ctx;
21350f85e244SJosef Bacik 
21360f85e244SJosef Bacik 	ctx = kzalloc(sizeof(struct btrfs_fs_context), GFP_KERNEL);
21370f85e244SJosef Bacik 	if (!ctx)
21380f85e244SJosef Bacik 		return -ENOMEM;
21390f85e244SJosef Bacik 
21403bb17a25SJosef Bacik 	refcount_set(&ctx->refs, 1);
21410f85e244SJosef Bacik 	fc->fs_private = ctx;
21420f85e244SJosef Bacik 	fc->ops = &btrfs_fs_context_ops;
21430f85e244SJosef Bacik 
2144eddb1a43SJosef Bacik 	if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
2145eddb1a43SJosef Bacik 		btrfs_info_to_ctx(btrfs_sb(fc->root->d_sb), ctx);
2146eddb1a43SJosef Bacik 	} else {
2147eddb1a43SJosef Bacik 		ctx->thread_pool_size =
2148eddb1a43SJosef Bacik 			min_t(unsigned long, num_online_cpus() + 2, 8);
2149eddb1a43SJosef Bacik 		ctx->max_inline = BTRFS_DEFAULT_MAX_INLINE;
2150eddb1a43SJosef Bacik 		ctx->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
2151eddb1a43SJosef Bacik 	}
2152eddb1a43SJosef Bacik 
2153ad21f15bSJosef Bacik #ifdef CONFIG_BTRFS_FS_POSIX_ACL
2154ad21f15bSJosef Bacik 	fc->sb_flags |= SB_POSIXACL;
2155ad21f15bSJosef Bacik #endif
2156ad21f15bSJosef Bacik 	fc->sb_flags |= SB_I_VERSION;
2157ad21f15bSJosef Bacik 
21580f85e244SJosef Bacik 	return 0;
21590f85e244SJosef Bacik }
21600f85e244SJosef Bacik 
21612e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
21622e635a27SChris Mason 	.owner			= THIS_MODULE,
21632e635a27SChris Mason 	.name			= "btrfs",
2164ad21f15bSJosef Bacik 	.init_fs_context	= btrfs_init_fs_context,
2165ad21f15bSJosef Bacik 	.parameters		= btrfs_fs_parameters,
216672fa39f5SMisono, Tomohiro 	.kill_sb		= btrfs_kill_super,
21675b9b26f5SChristian Brauner 	.fs_flags		= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA | FS_ALLOW_IDMAP,
216872fa39f5SMisono, Tomohiro  };
216972fa39f5SMisono, Tomohiro 
21707f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs");
2171a9218f6bSChris Mason 
2172d8620958STom Van Braeckel static int btrfs_control_open(struct inode *inode, struct file *file)
2173d8620958STom Van Braeckel {
2174d8620958STom Van Braeckel 	/*
2175d8620958STom Van Braeckel 	 * The control file's private_data is used to hold the
2176d8620958STom Van Braeckel 	 * transaction when it is started and is used to keep
2177d8620958STom Van Braeckel 	 * track of whether a transaction is already in progress.
2178d8620958STom Van Braeckel 	 */
2179d8620958STom Van Braeckel 	file->private_data = NULL;
2180d8620958STom Van Braeckel 	return 0;
2181d8620958STom Van Braeckel }
2182d8620958STom Van Braeckel 
2183d352ac68SChris Mason /*
2184cfe953c8SSu Yue  * Used by /dev/btrfs-control for devices ioctls.
2185d352ac68SChris Mason  */
21868a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
21878a4b83ccSChris Mason 				unsigned long arg)
21888a4b83ccSChris Mason {
21898a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
219036350e95SGu Jinxiang 	struct btrfs_device *device = NULL;
219116cab91aSAnand Jain 	dev_t devt = 0;
2192c071fcfdSChris Mason 	int ret = -ENOTTY;
21938a4b83ccSChris Mason 
2194e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2195e441d54dSChris Mason 		return -EPERM;
2196e441d54dSChris Mason 
2197dae7b665SLi Zefan 	vol = memdup_user((void __user *)arg, sizeof(*vol));
2198dae7b665SLi Zefan 	if (IS_ERR(vol))
2199dae7b665SLi Zefan 		return PTR_ERR(vol);
2200f505754fSFilipe Manana 	vol->name[BTRFS_PATH_NAME_MAX] = '\0';
2201c071fcfdSChris Mason 
22028a4b83ccSChris Mason 	switch (cmd) {
22038a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
2204899f9307SDavid Sterba 		mutex_lock(&uuid_mutex);
2205bc27d6f0SAnand Jain 		/*
2206bc27d6f0SAnand Jain 		 * Scanning outside of mount can return NULL which would turn
2207bc27d6f0SAnand Jain 		 * into 0 error code.
2208bc27d6f0SAnand Jain 		 */
2209bc27d6f0SAnand Jain 		device = btrfs_scan_one_device(vol->name, BLK_OPEN_READ, false);
221036350e95SGu Jinxiang 		ret = PTR_ERR_OR_ZERO(device);
2211899f9307SDavid Sterba 		mutex_unlock(&uuid_mutex);
22128a4b83ccSChris Mason 		break;
2213228a73abSAnand Jain 	case BTRFS_IOC_FORGET_DEV:
221416cab91aSAnand Jain 		if (vol->name[0] != 0) {
221516cab91aSAnand Jain 			ret = lookup_bdev(vol->name, &devt);
221616cab91aSAnand Jain 			if (ret)
221716cab91aSAnand Jain 				break;
221816cab91aSAnand Jain 		}
221916cab91aSAnand Jain 		ret = btrfs_forget_devices(devt);
2220228a73abSAnand Jain 		break;
222102db0844SJosef Bacik 	case BTRFS_IOC_DEVICES_READY:
2222899f9307SDavid Sterba 		mutex_lock(&uuid_mutex);
2223bc27d6f0SAnand Jain 		/*
2224bc27d6f0SAnand Jain 		 * Scanning outside of mount can return NULL which would turn
2225bc27d6f0SAnand Jain 		 * into 0 error code.
2226bc27d6f0SAnand Jain 		 */
2227bc27d6f0SAnand Jain 		device = btrfs_scan_one_device(vol->name, BLK_OPEN_READ, false);
2228bc27d6f0SAnand Jain 		if (IS_ERR_OR_NULL(device)) {
2229899f9307SDavid Sterba 			mutex_unlock(&uuid_mutex);
223036350e95SGu Jinxiang 			ret = PTR_ERR(device);
223102db0844SJosef Bacik 			break;
2232899f9307SDavid Sterba 		}
223336350e95SGu Jinxiang 		ret = !(device->fs_devices->num_devices ==
223436350e95SGu Jinxiang 			device->fs_devices->total_devices);
2235899f9307SDavid Sterba 		mutex_unlock(&uuid_mutex);
223602db0844SJosef Bacik 		break;
2237c5868f83SDavid Sterba 	case BTRFS_IOC_GET_SUPPORTED_FEATURES:
2238d5131b65SDavid Sterba 		ret = btrfs_ioctl_get_supported_features((void __user*)arg);
2239c5868f83SDavid Sterba 		break;
22408a4b83ccSChris Mason 	}
2241dae7b665SLi Zefan 
22428a4b83ccSChris Mason 	kfree(vol);
2243f819d837SLinda Knippers 	return ret;
22448a4b83ccSChris Mason }
22458a4b83ccSChris Mason 
22460176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
2247ed0dab6bSYan {
2248354aa0fbSMiao Xie 	struct btrfs_trans_handle *trans;
22490b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
22500b246afaSJeff Mahoney 	struct btrfs_root *root = fs_info->tree_root;
2251354aa0fbSMiao Xie 
2252fac03c8dSDavid Sterba 	set_bit(BTRFS_FS_FROZEN, &fs_info->flags);
22539e7cc91aSWang Xiaoguang 	/*
22549e7cc91aSWang Xiaoguang 	 * We don't need a barrier here, we'll wait for any transaction that
22559e7cc91aSWang Xiaoguang 	 * could be in progress on other threads (and do delayed iputs that
22569e7cc91aSWang Xiaoguang 	 * we want to avoid on a frozen filesystem), or do the commit
22579e7cc91aSWang Xiaoguang 	 * ourselves.
22589e7cc91aSWang Xiaoguang 	 */
2259d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
2260354aa0fbSMiao Xie 	if (IS_ERR(trans)) {
2261354aa0fbSMiao Xie 		/* no transaction, don't bother */
2262354aa0fbSMiao Xie 		if (PTR_ERR(trans) == -ENOENT)
22630176260fSLinus Torvalds 			return 0;
2264354aa0fbSMiao Xie 		return PTR_ERR(trans);
2265354aa0fbSMiao Xie 	}
22663a45bb20SJeff Mahoney 	return btrfs_commit_transaction(trans);
2267ed0dab6bSYan }
2268ed0dab6bSYan 
2269a05d3c91SQu Wenruo static int check_dev_super(struct btrfs_device *dev)
2270a05d3c91SQu Wenruo {
2271a05d3c91SQu Wenruo 	struct btrfs_fs_info *fs_info = dev->fs_info;
2272a05d3c91SQu Wenruo 	struct btrfs_super_block *sb;
22730124855fSFilipe Manana 	u64 last_trans;
22743d17adeaSQu Wenruo 	u16 csum_type;
2275a05d3c91SQu Wenruo 	int ret = 0;
2276a05d3c91SQu Wenruo 
2277a05d3c91SQu Wenruo 	/* This should be called with fs still frozen. */
2278a05d3c91SQu Wenruo 	ASSERT(test_bit(BTRFS_FS_FROZEN, &fs_info->flags));
2279a05d3c91SQu Wenruo 
2280a05d3c91SQu Wenruo 	/* Missing dev, no need to check. */
2281a05d3c91SQu Wenruo 	if (!dev->bdev)
2282a05d3c91SQu Wenruo 		return 0;
2283a05d3c91SQu Wenruo 
2284a05d3c91SQu Wenruo 	/* Only need to check the primary super block. */
2285a05d3c91SQu Wenruo 	sb = btrfs_read_dev_one_super(dev->bdev, 0, true);
2286a05d3c91SQu Wenruo 	if (IS_ERR(sb))
2287a05d3c91SQu Wenruo 		return PTR_ERR(sb);
2288a05d3c91SQu Wenruo 
22893d17adeaSQu Wenruo 	/* Verify the checksum. */
22903d17adeaSQu Wenruo 	csum_type = btrfs_super_csum_type(sb);
22913d17adeaSQu Wenruo 	if (csum_type != btrfs_super_csum_type(fs_info->super_copy)) {
22923d17adeaSQu Wenruo 		btrfs_err(fs_info, "csum type changed, has %u expect %u",
22933d17adeaSQu Wenruo 			  csum_type, btrfs_super_csum_type(fs_info->super_copy));
22943d17adeaSQu Wenruo 		ret = -EUCLEAN;
22953d17adeaSQu Wenruo 		goto out;
22963d17adeaSQu Wenruo 	}
22973d17adeaSQu Wenruo 
22983d17adeaSQu Wenruo 	if (btrfs_check_super_csum(fs_info, sb)) {
22993d17adeaSQu Wenruo 		btrfs_err(fs_info, "csum for on-disk super block no longer matches");
23003d17adeaSQu Wenruo 		ret = -EUCLEAN;
23013d17adeaSQu Wenruo 		goto out;
23023d17adeaSQu Wenruo 	}
23033d17adeaSQu Wenruo 
2304a05d3c91SQu Wenruo 	/* Btrfs_validate_super() includes fsid check against super->fsid. */
2305a05d3c91SQu Wenruo 	ret = btrfs_validate_super(fs_info, sb, 0);
2306a05d3c91SQu Wenruo 	if (ret < 0)
2307a05d3c91SQu Wenruo 		goto out;
2308a05d3c91SQu Wenruo 
23090124855fSFilipe Manana 	last_trans = btrfs_get_last_trans_committed(fs_info);
23100124855fSFilipe Manana 	if (btrfs_super_generation(sb) != last_trans) {
2311a05d3c91SQu Wenruo 		btrfs_err(fs_info, "transid mismatch, has %llu expect %llu",
23120124855fSFilipe Manana 			  btrfs_super_generation(sb), last_trans);
2313a05d3c91SQu Wenruo 		ret = -EUCLEAN;
2314a05d3c91SQu Wenruo 		goto out;
2315a05d3c91SQu Wenruo 	}
2316a05d3c91SQu Wenruo out:
2317a05d3c91SQu Wenruo 	btrfs_release_disk_super(sb);
2318a05d3c91SQu Wenruo 	return ret;
2319a05d3c91SQu Wenruo }
2320a05d3c91SQu Wenruo 
23219e7cc91aSWang Xiaoguang static int btrfs_unfreeze(struct super_block *sb)
23229e7cc91aSWang Xiaoguang {
2323fac03c8dSDavid Sterba 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2324a05d3c91SQu Wenruo 	struct btrfs_device *device;
2325a05d3c91SQu Wenruo 	int ret = 0;
2326fac03c8dSDavid Sterba 
2327a05d3c91SQu Wenruo 	/*
2328a05d3c91SQu Wenruo 	 * Make sure the fs is not changed by accident (like hibernation then
2329a05d3c91SQu Wenruo 	 * modified by other OS).
2330a05d3c91SQu Wenruo 	 * If we found anything wrong, we mark the fs error immediately.
2331a05d3c91SQu Wenruo 	 *
2332a05d3c91SQu Wenruo 	 * And since the fs is frozen, no one can modify the fs yet, thus
2333a05d3c91SQu Wenruo 	 * we don't need to hold device_list_mutex.
2334a05d3c91SQu Wenruo 	 */
2335a05d3c91SQu Wenruo 	list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
2336a05d3c91SQu Wenruo 		ret = check_dev_super(device);
2337a05d3c91SQu Wenruo 		if (ret < 0) {
2338a05d3c91SQu Wenruo 			btrfs_handle_fs_error(fs_info, ret,
2339a05d3c91SQu Wenruo 				"super block on devid %llu got modified unexpectedly",
2340a05d3c91SQu Wenruo 				device->devid);
2341a05d3c91SQu Wenruo 			break;
2342a05d3c91SQu Wenruo 		}
2343a05d3c91SQu Wenruo 	}
2344fac03c8dSDavid Sterba 	clear_bit(BTRFS_FS_FROZEN, &fs_info->flags);
2345a05d3c91SQu Wenruo 
2346a05d3c91SQu Wenruo 	/*
2347a05d3c91SQu Wenruo 	 * We still return 0, to allow VFS layer to unfreeze the fs even the
2348a05d3c91SQu Wenruo 	 * above checks failed. Since the fs is either fine or read-only, we're
2349a05d3c91SQu Wenruo 	 * safe to continue, without causing further damage.
2350a05d3c91SQu Wenruo 	 */
23519e7cc91aSWang Xiaoguang 	return 0;
23529e7cc91aSWang Xiaoguang }
23539e7cc91aSWang Xiaoguang 
23549c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
23559c5085c1SJosef Bacik {
23569c5085c1SJosef Bacik 	struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
23579c5085c1SJosef Bacik 
235888c14590SDavid Sterba 	/*
23596605fd2fSAnand Jain 	 * There should be always a valid pointer in latest_dev, it may be stale
23606605fd2fSAnand Jain 	 * for a short moment in case it's being deleted but still valid until
23616605fd2fSAnand Jain 	 * the end of RCU grace period.
236288c14590SDavid Sterba 	 */
236388c14590SDavid Sterba 	rcu_read_lock();
2364cb3e217bSQu Wenruo 	seq_escape(m, btrfs_dev_name(fs_info->fs_devices->latest_dev), " \t\n\\");
236588c14590SDavid Sterba 	rcu_read_unlock();
23666605fd2fSAnand Jain 
23679c5085c1SJosef Bacik 	return 0;
23689c5085c1SJosef Bacik }
23699c5085c1SJosef Bacik 
2370b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
237176dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
2372bd555975SAl Viro 	.evict_inode	= btrfs_evict_inode,
2373e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
2374d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
2375a9572a15SEric Paris 	.show_options	= btrfs_show_options,
23769c5085c1SJosef Bacik 	.show_devname	= btrfs_show_devname,
23772c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
23782c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
237926602cabSAl Viro 	.free_inode	= btrfs_free_inode,
23808fd17795SChris Mason 	.statfs		= btrfs_statfs,
23810176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
23829e7cc91aSWang Xiaoguang 	.unfreeze_fs	= btrfs_unfreeze,
2383e20d96d6SChris Mason };
2384a9218f6bSChris Mason 
2385a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
2386d8620958STom Van Braeckel 	.open = btrfs_control_open,
2387a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
23881832f2d8SArnd Bergmann 	.compat_ioctl = compat_ptr_ioctl,
2389a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
23906038f373SArnd Bergmann 	.llseek = noop_llseek,
2391a9218f6bSChris Mason };
2392a9218f6bSChris Mason 
2393a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
2394578454ffSKay Sievers 	.minor		= BTRFS_MINOR,
2395a9218f6bSChris Mason 	.name		= "btrfs-control",
2396a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
2397a9218f6bSChris Mason };
2398a9218f6bSChris Mason 
2399578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
2400578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control");
2401578454ffSKay Sievers 
2402f5c29bd9SLiu Bo static int __init btrfs_interface_init(void)
2403a9218f6bSChris Mason {
2404a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
2405a9218f6bSChris Mason }
2406a9218f6bSChris Mason 
2407e67c718bSDavid Sterba static __cold void btrfs_interface_exit(void)
2408a9218f6bSChris Mason {
2409f368ed60SGreg Kroah-Hartman 	misc_deregister(&btrfs_misc);
2410a9218f6bSChris Mason }
2411a9218f6bSChris Mason 
24125565b8e0SQu Wenruo static int __init btrfs_print_mod_info(void)
241385965600SDavid Sterba {
2414edf57cbfSBart Van Assche 	static const char options[] = ""
241585965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG
241685965600SDavid Sterba 			", debug=on"
241785965600SDavid Sterba #endif
241879556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT
241979556c3dSStefan Behrens 			", assert=on"
242079556c3dSStefan Behrens #endif
2421fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
2422fb592373SJosef Bacik 			", ref-verify=on"
2423fb592373SJosef Bacik #endif
24245b316468SNaohiro Aota #ifdef CONFIG_BLK_DEV_ZONED
24255b316468SNaohiro Aota 			", zoned=yes"
24265b316468SNaohiro Aota #else
24275b316468SNaohiro Aota 			", zoned=no"
24285b316468SNaohiro Aota #endif
2429ea3dc7d2SDavid Sterba #ifdef CONFIG_FS_VERITY
2430ea3dc7d2SDavid Sterba 			", fsverity=yes"
2431ea3dc7d2SDavid Sterba #else
2432ea3dc7d2SDavid Sterba 			", fsverity=no"
2433ea3dc7d2SDavid Sterba #endif
2434edf57cbfSBart Van Assche 			;
24356e7a367eSChristoph Hellwig 	pr_info("Btrfs loaded%s\n", options);
24365565b8e0SQu Wenruo 	return 0;
24375565b8e0SQu Wenruo }
24385565b8e0SQu Wenruo 
24395565b8e0SQu Wenruo static int register_btrfs(void)
24405565b8e0SQu Wenruo {
24415565b8e0SQu Wenruo 	return register_filesystem(&btrfs_fs_type);
24425565b8e0SQu Wenruo }
24435565b8e0SQu Wenruo 
24445565b8e0SQu Wenruo static void unregister_btrfs(void)
24455565b8e0SQu Wenruo {
24465565b8e0SQu Wenruo 	unregister_filesystem(&btrfs_fs_type);
24475565b8e0SQu Wenruo }
24485565b8e0SQu Wenruo 
24495565b8e0SQu Wenruo /* Helper structure for long init/exit functions. */
24505565b8e0SQu Wenruo struct init_sequence {
24515565b8e0SQu Wenruo 	int (*init_func)(void);
24525565b8e0SQu Wenruo 	/* Can be NULL if the init_func doesn't need cleanup. */
24535565b8e0SQu Wenruo 	void (*exit_func)(void);
24545565b8e0SQu Wenruo };
24555565b8e0SQu Wenruo 
24565565b8e0SQu Wenruo static const struct init_sequence mod_init_seq[] = {
24575565b8e0SQu Wenruo 	{
24585565b8e0SQu Wenruo 		.init_func = btrfs_props_init,
24595565b8e0SQu Wenruo 		.exit_func = NULL,
24605565b8e0SQu Wenruo 	}, {
24615565b8e0SQu Wenruo 		.init_func = btrfs_init_sysfs,
24625565b8e0SQu Wenruo 		.exit_func = btrfs_exit_sysfs,
24635565b8e0SQu Wenruo 	}, {
24645565b8e0SQu Wenruo 		.init_func = btrfs_init_compress,
24655565b8e0SQu Wenruo 		.exit_func = btrfs_exit_compress,
24665565b8e0SQu Wenruo 	}, {
24675565b8e0SQu Wenruo 		.init_func = btrfs_init_cachep,
24685565b8e0SQu Wenruo 		.exit_func = btrfs_destroy_cachep,
24695565b8e0SQu Wenruo 	}, {
24705565b8e0SQu Wenruo 		.init_func = btrfs_transaction_init,
24715565b8e0SQu Wenruo 		.exit_func = btrfs_transaction_exit,
24725565b8e0SQu Wenruo 	}, {
24735565b8e0SQu Wenruo 		.init_func = btrfs_ctree_init,
24745565b8e0SQu Wenruo 		.exit_func = btrfs_ctree_exit,
24755565b8e0SQu Wenruo 	}, {
24765565b8e0SQu Wenruo 		.init_func = btrfs_free_space_init,
24775565b8e0SQu Wenruo 		.exit_func = btrfs_free_space_exit,
24785565b8e0SQu Wenruo 	}, {
24795565b8e0SQu Wenruo 		.init_func = extent_state_init_cachep,
24805565b8e0SQu Wenruo 		.exit_func = extent_state_free_cachep,
24815565b8e0SQu Wenruo 	}, {
24825565b8e0SQu Wenruo 		.init_func = extent_buffer_init_cachep,
24835565b8e0SQu Wenruo 		.exit_func = extent_buffer_free_cachep,
24845565b8e0SQu Wenruo 	}, {
24855565b8e0SQu Wenruo 		.init_func = btrfs_bioset_init,
24865565b8e0SQu Wenruo 		.exit_func = btrfs_bioset_exit,
24875565b8e0SQu Wenruo 	}, {
24885565b8e0SQu Wenruo 		.init_func = extent_map_init,
24895565b8e0SQu Wenruo 		.exit_func = extent_map_exit,
24905565b8e0SQu Wenruo 	}, {
24915565b8e0SQu Wenruo 		.init_func = ordered_data_init,
24925565b8e0SQu Wenruo 		.exit_func = ordered_data_exit,
24935565b8e0SQu Wenruo 	}, {
24945565b8e0SQu Wenruo 		.init_func = btrfs_delayed_inode_init,
24955565b8e0SQu Wenruo 		.exit_func = btrfs_delayed_inode_exit,
24965565b8e0SQu Wenruo 	}, {
24975565b8e0SQu Wenruo 		.init_func = btrfs_auto_defrag_init,
24985565b8e0SQu Wenruo 		.exit_func = btrfs_auto_defrag_exit,
24995565b8e0SQu Wenruo 	}, {
25005565b8e0SQu Wenruo 		.init_func = btrfs_delayed_ref_init,
25015565b8e0SQu Wenruo 		.exit_func = btrfs_delayed_ref_exit,
25025565b8e0SQu Wenruo 	}, {
25035565b8e0SQu Wenruo 		.init_func = btrfs_prelim_ref_init,
25045565b8e0SQu Wenruo 		.exit_func = btrfs_prelim_ref_exit,
25055565b8e0SQu Wenruo 	}, {
25065565b8e0SQu Wenruo 		.init_func = btrfs_interface_init,
25075565b8e0SQu Wenruo 		.exit_func = btrfs_interface_exit,
25085565b8e0SQu Wenruo 	}, {
25095565b8e0SQu Wenruo 		.init_func = btrfs_print_mod_info,
25105565b8e0SQu Wenruo 		.exit_func = NULL,
25115565b8e0SQu Wenruo 	}, {
25125565b8e0SQu Wenruo 		.init_func = btrfs_run_sanity_tests,
25135565b8e0SQu Wenruo 		.exit_func = NULL,
25145565b8e0SQu Wenruo 	}, {
25155565b8e0SQu Wenruo 		.init_func = register_btrfs,
25165565b8e0SQu Wenruo 		.exit_func = unregister_btrfs,
25175565b8e0SQu Wenruo 	}
25185565b8e0SQu Wenruo };
25195565b8e0SQu Wenruo 
25205565b8e0SQu Wenruo static bool mod_init_result[ARRAY_SIZE(mod_init_seq)];
25215565b8e0SQu Wenruo 
252282c0efd3SAnand Jain static __always_inline void btrfs_exit_btrfs_fs(void)
25235565b8e0SQu Wenruo {
25245565b8e0SQu Wenruo 	int i;
25255565b8e0SQu Wenruo 
25265565b8e0SQu Wenruo 	for (i = ARRAY_SIZE(mod_init_seq) - 1; i >= 0; i--) {
25275565b8e0SQu Wenruo 		if (!mod_init_result[i])
25285565b8e0SQu Wenruo 			continue;
25295565b8e0SQu Wenruo 		if (mod_init_seq[i].exit_func)
25305565b8e0SQu Wenruo 			mod_init_seq[i].exit_func();
25315565b8e0SQu Wenruo 		mod_init_result[i] = false;
25325565b8e0SQu Wenruo 	}
253385965600SDavid Sterba }
253485965600SDavid Sterba 
253582c0efd3SAnand Jain static void __exit exit_btrfs_fs(void)
253682c0efd3SAnand Jain {
253782c0efd3SAnand Jain 	btrfs_exit_btrfs_fs();
2538c68f7290SFilipe Manana 	btrfs_cleanup_fs_uuids();
253982c0efd3SAnand Jain }
254082c0efd3SAnand Jain 
25412e635a27SChris Mason static int __init init_btrfs_fs(void)
25422e635a27SChris Mason {
25435565b8e0SQu Wenruo 	int ret;
25445565b8e0SQu Wenruo 	int i;
254558176a96SJosef Bacik 
25465565b8e0SQu Wenruo 	for (i = 0; i < ARRAY_SIZE(mod_init_seq); i++) {
25475565b8e0SQu Wenruo 		ASSERT(!mod_init_result[i]);
25485565b8e0SQu Wenruo 		ret = mod_init_seq[i].init_func();
254982c0efd3SAnand Jain 		if (ret < 0) {
255082c0efd3SAnand Jain 			btrfs_exit_btrfs_fs();
255182c0efd3SAnand Jain 			return ret;
255282c0efd3SAnand Jain 		}
25535565b8e0SQu Wenruo 		mod_init_result[i] = true;
25545565b8e0SQu Wenruo 	}
25552f4cbe64SWyatt Banks 	return 0;
25562e635a27SChris Mason }
25572e635a27SChris Mason 
255860efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs);
25592e635a27SChris Mason module_exit(exit_btrfs_fs)
25602e635a27SChris Mason 
25612e635a27SChris Mason MODULE_LICENSE("GPL");
2562d5178578SJohannes Thumshirn MODULE_SOFTDEP("pre: crc32c");
25633951e7f0SJohannes Thumshirn MODULE_SOFTDEP("pre: xxhash64");
25643831bf00SJohannes Thumshirn MODULE_SOFTDEP("pre: sha256");
2565352ae07bSDavid Sterba MODULE_SOFTDEP("pre: blake2b-256");
2566