xref: /linux/fs/btrfs/super.c (revision 57b14823ea68592bd67e4992a2bf0dd67abb68d6)
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>
31ae1e766fSFilipe Manana #include <linux/swap.h>
329b569ea0SJosef Bacik #include "messages.h"
3316cdcec7SMiao Xie #include "delayed-inode.h"
342e635a27SChris Mason #include "ctree.h"
35e20d96d6SChris Mason #include "disk-io.h"
36d5719762SChris Mason #include "transaction.h"
372c90e5d6SChris Mason #include "btrfs_inode.h"
389aa29a20SFilipe Manana #include "direct-io.h"
3963541927SFilipe David Borba Manana #include "props.h"
405103e947SJosef Bacik #include "xattr.h"
41103c1972SChristoph Hellwig #include "bio.h"
42be6e8dc0SBalaji Rao #include "export.h"
43c8b97818SChris Mason #include "compression.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 
btrfs_put_super(struct super_block * sb)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;
86c3ece6b7SQu Wenruo 	unsigned long 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,
124440861b1SQu Wenruo 	Opt_norecovery,
125416a7202SDavid Sterba 
12674ef0018SQu Wenruo 	/* Rescue options */
12774ef0018SQu Wenruo 	Opt_rescue,
12874ef0018SQu Wenruo 	Opt_usebackuproot,
12974ef0018SQu Wenruo 	Opt_nologreplay,
13074ef0018SQu Wenruo 
131416a7202SDavid Sterba 	/* Debugging options */
1326941823cSJosef Bacik 	Opt_enospc_debug,
133d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
13415ddcdd3SJosef Bacik 	Opt_fragment, Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
135d0bd4560SJosef Bacik #endif
136fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
137fb592373SJosef Bacik 	Opt_ref_verify,
138fb592373SJosef Bacik #endif
1399555c6c1SIlya Dryomov 	Opt_err,
14095e05289SChris Mason };
14195e05289SChris Mason 
14215ddcdd3SJosef Bacik enum {
14315ddcdd3SJosef Bacik 	Opt_fatal_errors_panic,
14415ddcdd3SJosef Bacik 	Opt_fatal_errors_bug,
14515ddcdd3SJosef Bacik };
146416a7202SDavid Sterba 
14715ddcdd3SJosef Bacik static const struct constant_table btrfs_parameter_fatal_errors[] = {
14815ddcdd3SJosef Bacik 	{ "panic", Opt_fatal_errors_panic },
14915ddcdd3SJosef Bacik 	{ "bug", Opt_fatal_errors_bug },
15015ddcdd3SJosef Bacik 	{}
15115ddcdd3SJosef Bacik };
15274ef0018SQu Wenruo 
15315ddcdd3SJosef Bacik enum {
15415ddcdd3SJosef Bacik 	Opt_discard_sync,
15515ddcdd3SJosef Bacik 	Opt_discard_async,
15615ddcdd3SJosef Bacik };
157416a7202SDavid Sterba 
15815ddcdd3SJosef Bacik static const struct constant_table btrfs_parameter_discard[] = {
15915ddcdd3SJosef Bacik 	{ "sync", Opt_discard_sync },
16015ddcdd3SJosef Bacik 	{ "async", Opt_discard_async },
16115ddcdd3SJosef Bacik 	{}
16215ddcdd3SJosef Bacik };
16315ddcdd3SJosef Bacik 
16415ddcdd3SJosef Bacik enum {
16515ddcdd3SJosef Bacik 	Opt_space_cache_v1,
16615ddcdd3SJosef Bacik 	Opt_space_cache_v2,
16715ddcdd3SJosef Bacik };
16815ddcdd3SJosef Bacik 
16915ddcdd3SJosef Bacik static const struct constant_table btrfs_parameter_space_cache[] = {
17015ddcdd3SJosef Bacik 	{ "v1", Opt_space_cache_v1 },
17115ddcdd3SJosef Bacik 	{ "v2", Opt_space_cache_v2 },
17215ddcdd3SJosef Bacik 	{}
17315ddcdd3SJosef Bacik };
17415ddcdd3SJosef Bacik 
17515ddcdd3SJosef Bacik enum {
17615ddcdd3SJosef Bacik 	Opt_rescue_usebackuproot,
17715ddcdd3SJosef Bacik 	Opt_rescue_nologreplay,
17815ddcdd3SJosef Bacik 	Opt_rescue_ignorebadroots,
17915ddcdd3SJosef Bacik 	Opt_rescue_ignoredatacsums,
180169aaaf2SQu Wenruo 	Opt_rescue_ignoremetacsums,
18132e62165SQu Wenruo 	Opt_rescue_ignoresuperflags,
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 },
191169aaaf2SQu Wenruo 	{ "ignoremetacsums", Opt_rescue_ignoremetacsums},
19232e62165SQu Wenruo 	{ "ignoresuperflags", Opt_rescue_ignoresuperflags},
19315ddcdd3SJosef Bacik 	{ "idatacsums", Opt_rescue_ignoredatacsums },
194169aaaf2SQu Wenruo 	{ "imetacsums", Opt_rescue_ignoremetacsums},
19532e62165SQu Wenruo 	{ "isuperflags", Opt_rescue_ignoresuperflags},
19615ddcdd3SJosef Bacik 	{ "all", Opt_rescue_parameter_all },
19715ddcdd3SJosef Bacik 	{}
19815ddcdd3SJosef Bacik };
19915ddcdd3SJosef Bacik 
200d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
20115ddcdd3SJosef Bacik enum {
20215ddcdd3SJosef Bacik 	Opt_fragment_parameter_data,
20315ddcdd3SJosef Bacik 	Opt_fragment_parameter_metadata,
20415ddcdd3SJosef Bacik 	Opt_fragment_parameter_all,
20515ddcdd3SJosef Bacik };
20615ddcdd3SJosef Bacik 
20715ddcdd3SJosef Bacik static const struct constant_table btrfs_parameter_fragment[] = {
20815ddcdd3SJosef Bacik 	{ "data", Opt_fragment_parameter_data },
20915ddcdd3SJosef Bacik 	{ "metadata", Opt_fragment_parameter_metadata },
21015ddcdd3SJosef Bacik 	{ "all", Opt_fragment_parameter_all },
21115ddcdd3SJosef Bacik 	{}
21215ddcdd3SJosef Bacik };
21315ddcdd3SJosef Bacik #endif
21415ddcdd3SJosef Bacik 
215ad21f15bSJosef Bacik static const struct fs_parameter_spec btrfs_fs_parameters[] = {
21615ddcdd3SJosef Bacik 	fsparam_flag_no("acl", Opt_acl),
21715ddcdd3SJosef Bacik 	fsparam_flag_no("autodefrag", Opt_defrag),
21815ddcdd3SJosef Bacik 	fsparam_flag_no("barrier", Opt_barrier),
21915ddcdd3SJosef Bacik 	fsparam_flag("clear_cache", Opt_clear_cache),
22015ddcdd3SJosef Bacik 	fsparam_u32("commit", Opt_commit_interval),
22115ddcdd3SJosef Bacik 	fsparam_flag("compress", Opt_compress),
22215ddcdd3SJosef Bacik 	fsparam_string("compress", Opt_compress_type),
22315ddcdd3SJosef Bacik 	fsparam_flag("compress-force", Opt_compress_force),
22415ddcdd3SJosef Bacik 	fsparam_string("compress-force", Opt_compress_force_type),
22515ddcdd3SJosef Bacik 	fsparam_flag_no("datacow", Opt_datacow),
22615ddcdd3SJosef Bacik 	fsparam_flag_no("datasum", Opt_datasum),
22715ddcdd3SJosef Bacik 	fsparam_flag("degraded", Opt_degraded),
22815ddcdd3SJosef Bacik 	fsparam_string("device", Opt_device),
22915ddcdd3SJosef Bacik 	fsparam_flag_no("discard", Opt_discard),
23015ddcdd3SJosef Bacik 	fsparam_enum("discard", Opt_discard_mode, btrfs_parameter_discard),
23115ddcdd3SJosef Bacik 	fsparam_enum("fatal_errors", Opt_fatal_errors, btrfs_parameter_fatal_errors),
23215ddcdd3SJosef Bacik 	fsparam_flag_no("flushoncommit", Opt_flushoncommit),
23315ddcdd3SJosef Bacik 	fsparam_string("max_inline", Opt_max_inline),
23415ddcdd3SJosef Bacik 	fsparam_u32("metadata_ratio", Opt_ratio),
23515ddcdd3SJosef Bacik 	fsparam_flag("rescan_uuid_tree", Opt_rescan_uuid_tree),
23615ddcdd3SJosef Bacik 	fsparam_flag("skip_balance", Opt_skip_balance),
23715ddcdd3SJosef Bacik 	fsparam_flag_no("space_cache", Opt_space_cache),
23815ddcdd3SJosef Bacik 	fsparam_enum("space_cache", Opt_space_cache_version, btrfs_parameter_space_cache),
23915ddcdd3SJosef Bacik 	fsparam_flag_no("ssd", Opt_ssd),
24015ddcdd3SJosef Bacik 	fsparam_flag_no("ssd_spread", Opt_ssd_spread),
24115ddcdd3SJosef Bacik 	fsparam_string("subvol", Opt_subvol),
24215ddcdd3SJosef Bacik 	fsparam_flag("subvol=", Opt_subvol_empty),
24315ddcdd3SJosef Bacik 	fsparam_u64("subvolid", Opt_subvolid),
24415ddcdd3SJosef Bacik 	fsparam_u32("thread_pool", Opt_thread_pool),
24515ddcdd3SJosef Bacik 	fsparam_flag_no("treelog", Opt_treelog),
24615ddcdd3SJosef Bacik 	fsparam_flag("user_subvol_rm_allowed", Opt_user_subvol_rm_allowed),
24715ddcdd3SJosef Bacik 
24815ddcdd3SJosef Bacik 	/* Rescue options. */
24915ddcdd3SJosef Bacik 	fsparam_enum("rescue", Opt_rescue, btrfs_parameter_rescue),
25015ddcdd3SJosef Bacik 	/* Deprecated, with alias rescue=nologreplay */
25115ddcdd3SJosef Bacik 	__fsparam(NULL, "nologreplay", Opt_nologreplay, fs_param_deprecated, NULL),
25215ddcdd3SJosef Bacik 	/* Deprecated, with alias rescue=usebackuproot */
25315ddcdd3SJosef Bacik 	__fsparam(NULL, "usebackuproot", Opt_usebackuproot, fs_param_deprecated, NULL),
254440861b1SQu Wenruo 	/* For compatibility only, alias for "rescue=nologreplay". */
255440861b1SQu Wenruo 	fsparam_flag("norecovery", Opt_norecovery),
25615ddcdd3SJosef Bacik 
25715ddcdd3SJosef Bacik 	/* Debugging options. */
25815ddcdd3SJosef Bacik 	fsparam_flag_no("enospc_debug", Opt_enospc_debug),
25915ddcdd3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
26015ddcdd3SJosef Bacik 	fsparam_enum("fragment", Opt_fragment, btrfs_parameter_fragment),
261d0bd4560SJosef Bacik #endif
262fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
26315ddcdd3SJosef Bacik 	fsparam_flag("ref_verify", Opt_ref_verify),
264fb592373SJosef Bacik #endif
26515ddcdd3SJosef Bacik 	{}
26695e05289SChris Mason };
26795e05289SChris Mason 
268affc5af3SLinus Torvalds /* No support for restricting writes to btrfs devices yet... */
btrfs_open_mode(struct fs_context * fc)269affc5af3SLinus Torvalds static inline blk_mode_t btrfs_open_mode(struct fs_context *fc)
270affc5af3SLinus Torvalds {
271affc5af3SLinus Torvalds 	return sb_open_mode(fc->sb_flags) & ~BLK_OPEN_RESTRICT_WRITES;
272affc5af3SLinus Torvalds }
27374ef0018SQu Wenruo 
btrfs_parse_param(struct fs_context * fc,struct fs_parameter * param)27417b36120SJosef Bacik static int btrfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
27517b36120SJosef Bacik {
27617b36120SJosef Bacik 	struct btrfs_fs_context *ctx = fc->fs_private;
27717b36120SJosef Bacik 	struct fs_parse_result result;
27817b36120SJosef Bacik 	int opt;
27917b36120SJosef Bacik 
28017b36120SJosef Bacik 	opt = fs_parse(fc, btrfs_fs_parameters, param, &result);
28117b36120SJosef Bacik 	if (opt < 0)
28217b36120SJosef Bacik 		return opt;
28317b36120SJosef Bacik 
28417b36120SJosef Bacik 	switch (opt) {
28517b36120SJosef Bacik 	case Opt_degraded:
28617b36120SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, DEGRADED);
28717b36120SJosef Bacik 		break;
28817b36120SJosef Bacik 	case Opt_subvol_empty:
28917b36120SJosef Bacik 		/*
29017b36120SJosef Bacik 		 * This exists because we used to allow it on accident, so we're
29117b36120SJosef Bacik 		 * keeping it to maintain ABI.  See 37becec95ac3 ("Btrfs: allow
29217b36120SJosef Bacik 		 * empty subvol= again").
29317b36120SJosef Bacik 		 */
29417b36120SJosef Bacik 		break;
29517b36120SJosef Bacik 	case Opt_subvol:
29617b36120SJosef Bacik 		kfree(ctx->subvol_name);
29717b36120SJosef Bacik 		ctx->subvol_name = kstrdup(param->string, GFP_KERNEL);
29817b36120SJosef Bacik 		if (!ctx->subvol_name)
29917b36120SJosef Bacik 			return -ENOMEM;
30017b36120SJosef Bacik 		break;
30117b36120SJosef Bacik 	case Opt_subvolid:
30217b36120SJosef Bacik 		ctx->subvol_objectid = result.uint_64;
30317b36120SJosef Bacik 
30417b36120SJosef Bacik 		/* subvolid=0 means give me the original fs_tree. */
30517b36120SJosef Bacik 		if (!ctx->subvol_objectid)
30617b36120SJosef Bacik 			ctx->subvol_objectid = BTRFS_FS_TREE_OBJECTID;
30717b36120SJosef Bacik 		break;
30817b36120SJosef Bacik 	case Opt_device: {
30917b36120SJosef Bacik 		struct btrfs_device *device;
310affc5af3SLinus Torvalds 		blk_mode_t mode = btrfs_open_mode(fc);
31117b36120SJosef Bacik 
31217b36120SJosef Bacik 		mutex_lock(&uuid_mutex);
31317b36120SJosef Bacik 		device = btrfs_scan_one_device(param->string, mode, false);
31417b36120SJosef Bacik 		mutex_unlock(&uuid_mutex);
31517b36120SJosef Bacik 		if (IS_ERR(device))
31617b36120SJosef Bacik 			return PTR_ERR(device);
31717b36120SJosef Bacik 		break;
31817b36120SJosef Bacik 	}
31917b36120SJosef Bacik 	case Opt_datasum:
32017b36120SJosef Bacik 		if (result.negated) {
32117b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NODATASUM);
32217b36120SJosef Bacik 		} else {
32317b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATACOW);
32417b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATASUM);
32517b36120SJosef Bacik 		}
32617b36120SJosef Bacik 		break;
32717b36120SJosef Bacik 	case Opt_datacow:
32817b36120SJosef Bacik 		if (result.negated) {
32917b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, COMPRESS);
33017b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
33117b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NODATACOW);
33217b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NODATASUM);
33317b36120SJosef Bacik 		} else {
33417b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATACOW);
33517b36120SJosef Bacik 		}
33617b36120SJosef Bacik 		break;
33717b36120SJosef Bacik 	case Opt_compress_force:
33817b36120SJosef Bacik 	case Opt_compress_force_type:
33917b36120SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, FORCE_COMPRESS);
34017b36120SJosef Bacik 		fallthrough;
34117b36120SJosef Bacik 	case Opt_compress:
34217b36120SJosef Bacik 	case Opt_compress_type:
34317b36120SJosef Bacik 		if (opt == Opt_compress || opt == Opt_compress_force) {
34417b36120SJosef Bacik 			ctx->compress_type = BTRFS_COMPRESS_ZLIB;
34517b36120SJosef Bacik 			ctx->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
34617b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, COMPRESS);
34717b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATACOW);
34817b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATASUM);
34917b36120SJosef Bacik 		} else if (strncmp(param->string, "zlib", 4) == 0) {
35017b36120SJosef Bacik 			ctx->compress_type = BTRFS_COMPRESS_ZLIB;
35117b36120SJosef Bacik 			ctx->compress_level =
35217b36120SJosef Bacik 				btrfs_compress_str2level(BTRFS_COMPRESS_ZLIB,
35317b36120SJosef Bacik 							 param->string + 4);
35417b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, COMPRESS);
35517b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATACOW);
35617b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATASUM);
35717b36120SJosef Bacik 		} else if (strncmp(param->string, "lzo", 3) == 0) {
35817b36120SJosef Bacik 			ctx->compress_type = BTRFS_COMPRESS_LZO;
35917b36120SJosef Bacik 			ctx->compress_level = 0;
36017b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, COMPRESS);
36117b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATACOW);
36217b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATASUM);
36317b36120SJosef Bacik 		} else if (strncmp(param->string, "zstd", 4) == 0) {
36417b36120SJosef Bacik 			ctx->compress_type = BTRFS_COMPRESS_ZSTD;
36517b36120SJosef Bacik 			ctx->compress_level =
36617b36120SJosef Bacik 				btrfs_compress_str2level(BTRFS_COMPRESS_ZSTD,
36717b36120SJosef Bacik 							 param->string + 4);
36817b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, COMPRESS);
36917b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATACOW);
37017b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATASUM);
37117b36120SJosef Bacik 		} else if (strncmp(param->string, "no", 2) == 0) {
37217b36120SJosef Bacik 			ctx->compress_level = 0;
37317b36120SJosef Bacik 			ctx->compress_type = 0;
37417b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, COMPRESS);
37517b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
37617b36120SJosef Bacik 		} else {
37717b36120SJosef Bacik 			btrfs_err(NULL, "unrecognized compression value %s",
37817b36120SJosef Bacik 				  param->string);
37917b36120SJosef Bacik 			return -EINVAL;
38017b36120SJosef Bacik 		}
38117b36120SJosef Bacik 		break;
38217b36120SJosef Bacik 	case Opt_ssd:
38317b36120SJosef Bacik 		if (result.negated) {
38417b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NOSSD);
38517b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, SSD);
38617b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, SSD_SPREAD);
38717b36120SJosef Bacik 		} else {
38817b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, SSD);
38917b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NOSSD);
39017b36120SJosef Bacik 		}
39117b36120SJosef Bacik 		break;
39217b36120SJosef Bacik 	case Opt_ssd_spread:
39317b36120SJosef Bacik 		if (result.negated) {
39417b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, SSD_SPREAD);
39517b36120SJosef Bacik 		} else {
39617b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, SSD);
39717b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, SSD_SPREAD);
39817b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NOSSD);
39917b36120SJosef Bacik 		}
40017b36120SJosef Bacik 		break;
40117b36120SJosef Bacik 	case Opt_barrier:
40217b36120SJosef Bacik 		if (result.negated)
40317b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NOBARRIER);
40417b36120SJosef Bacik 		else
40517b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NOBARRIER);
40617b36120SJosef Bacik 		break;
40717b36120SJosef Bacik 	case Opt_thread_pool:
40817b36120SJosef Bacik 		if (result.uint_32 == 0) {
40917b36120SJosef Bacik 			btrfs_err(NULL, "invalid value 0 for thread_pool");
41017b36120SJosef Bacik 			return -EINVAL;
41117b36120SJosef Bacik 		}
41217b36120SJosef Bacik 		ctx->thread_pool_size = result.uint_32;
41317b36120SJosef Bacik 		break;
41417b36120SJosef Bacik 	case Opt_max_inline:
41517b36120SJosef Bacik 		ctx->max_inline = memparse(param->string, NULL);
41617b36120SJosef Bacik 		break;
41717b36120SJosef Bacik 	case Opt_acl:
41817b36120SJosef Bacik 		if (result.negated) {
41917b36120SJosef Bacik 			fc->sb_flags &= ~SB_POSIXACL;
42017b36120SJosef Bacik 		} else {
42117b36120SJosef Bacik #ifdef CONFIG_BTRFS_FS_POSIX_ACL
42217b36120SJosef Bacik 			fc->sb_flags |= SB_POSIXACL;
42317b36120SJosef Bacik #else
42417b36120SJosef Bacik 			btrfs_err(NULL, "support for ACL not compiled in");
42517b36120SJosef Bacik 			return -EINVAL;
42617b36120SJosef Bacik #endif
42717b36120SJosef Bacik 		}
42817b36120SJosef Bacik 		/*
42917b36120SJosef Bacik 		 * VFS limits the ability to toggle ACL on and off via remount,
43017b36120SJosef Bacik 		 * despite every file system allowing this.  This seems to be
43117b36120SJosef Bacik 		 * an oversight since we all do, but it'll fail if we're
43217b36120SJosef Bacik 		 * remounting.  So don't set the mask here, we'll check it in
43317b36120SJosef Bacik 		 * btrfs_reconfigure and do the toggling ourselves.
43417b36120SJosef Bacik 		 */
43517b36120SJosef Bacik 		if (fc->purpose != FS_CONTEXT_FOR_RECONFIGURE)
43617b36120SJosef Bacik 			fc->sb_flags_mask |= SB_POSIXACL;
43717b36120SJosef Bacik 		break;
43817b36120SJosef Bacik 	case Opt_treelog:
43917b36120SJosef Bacik 		if (result.negated)
44017b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NOTREELOG);
44117b36120SJosef Bacik 		else
44217b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NOTREELOG);
44317b36120SJosef Bacik 		break;
44417b36120SJosef Bacik 	case Opt_nologreplay:
44517b36120SJosef Bacik 		btrfs_warn(NULL,
44617b36120SJosef Bacik 		"'nologreplay' is deprecated, use 'rescue=nologreplay' instead");
44717b36120SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, NOLOGREPLAY);
44817b36120SJosef Bacik 		break;
449440861b1SQu Wenruo 	case Opt_norecovery:
450440861b1SQu Wenruo 		btrfs_info(NULL,
451440861b1SQu Wenruo "'norecovery' is for compatibility only, recommended to use 'rescue=nologreplay'");
452440861b1SQu Wenruo 		btrfs_set_opt(ctx->mount_opt, NOLOGREPLAY);
453440861b1SQu Wenruo 		break;
45417b36120SJosef Bacik 	case Opt_flushoncommit:
45517b36120SJosef Bacik 		if (result.negated)
45617b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, FLUSHONCOMMIT);
45717b36120SJosef Bacik 		else
45817b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, FLUSHONCOMMIT);
45917b36120SJosef Bacik 		break;
46017b36120SJosef Bacik 	case Opt_ratio:
46117b36120SJosef Bacik 		ctx->metadata_ratio = result.uint_32;
46217b36120SJosef Bacik 		break;
46317b36120SJosef Bacik 	case Opt_discard:
46417b36120SJosef Bacik 		if (result.negated) {
46517b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, DISCARD_SYNC);
46617b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, DISCARD_ASYNC);
46717b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NODISCARD);
46817b36120SJosef Bacik 		} else {
46917b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, DISCARD_SYNC);
47017b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, DISCARD_ASYNC);
47117b36120SJosef Bacik 		}
47217b36120SJosef Bacik 		break;
47317b36120SJosef Bacik 	case Opt_discard_mode:
47417b36120SJosef Bacik 		switch (result.uint_32) {
47517b36120SJosef Bacik 		case Opt_discard_sync:
47617b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, DISCARD_ASYNC);
47717b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, DISCARD_SYNC);
47817b36120SJosef Bacik 			break;
47917b36120SJosef Bacik 		case Opt_discard_async:
48017b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, DISCARD_SYNC);
48117b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, DISCARD_ASYNC);
48217b36120SJosef Bacik 			break;
48317b36120SJosef Bacik 		default:
48417b36120SJosef Bacik 			btrfs_err(NULL, "unrecognized discard mode value %s",
48517b36120SJosef Bacik 				  param->key);
48617b36120SJosef Bacik 			return -EINVAL;
48717b36120SJosef Bacik 		}
48817b36120SJosef Bacik 		btrfs_clear_opt(ctx->mount_opt, NODISCARD);
48917b36120SJosef Bacik 		break;
49017b36120SJosef Bacik 	case Opt_space_cache:
49117b36120SJosef Bacik 		if (result.negated) {
49217b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NOSPACECACHE);
49317b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, SPACE_CACHE);
49417b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, FREE_SPACE_TREE);
49517b36120SJosef Bacik 		} else {
49617b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, FREE_SPACE_TREE);
49717b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, SPACE_CACHE);
49817b36120SJosef Bacik 		}
49917b36120SJosef Bacik 		break;
50017b36120SJosef Bacik 	case Opt_space_cache_version:
50117b36120SJosef Bacik 		switch (result.uint_32) {
50217b36120SJosef Bacik 		case Opt_space_cache_v1:
50317b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, SPACE_CACHE);
50417b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, FREE_SPACE_TREE);
50517b36120SJosef Bacik 			break;
50617b36120SJosef Bacik 		case Opt_space_cache_v2:
50717b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, SPACE_CACHE);
50817b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, FREE_SPACE_TREE);
50917b36120SJosef Bacik 			break;
51017b36120SJosef Bacik 		default:
51117b36120SJosef Bacik 			btrfs_err(NULL, "unrecognized space_cache value %s",
51217b36120SJosef Bacik 				  param->key);
51317b36120SJosef Bacik 			return -EINVAL;
51417b36120SJosef Bacik 		}
51517b36120SJosef Bacik 		break;
51617b36120SJosef Bacik 	case Opt_rescan_uuid_tree:
51717b36120SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, RESCAN_UUID_TREE);
51817b36120SJosef Bacik 		break;
51917b36120SJosef Bacik 	case Opt_clear_cache:
52017b36120SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, CLEAR_CACHE);
52117b36120SJosef Bacik 		break;
52217b36120SJosef Bacik 	case Opt_user_subvol_rm_allowed:
52317b36120SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, USER_SUBVOL_RM_ALLOWED);
52417b36120SJosef Bacik 		break;
52517b36120SJosef Bacik 	case Opt_enospc_debug:
52617b36120SJosef Bacik 		if (result.negated)
52717b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, ENOSPC_DEBUG);
52817b36120SJosef Bacik 		else
52917b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, ENOSPC_DEBUG);
53017b36120SJosef Bacik 		break;
53117b36120SJosef Bacik 	case Opt_defrag:
53217b36120SJosef Bacik 		if (result.negated)
53317b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, AUTO_DEFRAG);
53417b36120SJosef Bacik 		else
53517b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, AUTO_DEFRAG);
53617b36120SJosef Bacik 		break;
53717b36120SJosef Bacik 	case Opt_usebackuproot:
53817b36120SJosef Bacik 		btrfs_warn(NULL,
53917b36120SJosef Bacik 			   "'usebackuproot' is deprecated, use 'rescue=usebackuproot' instead");
54017b36120SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, USEBACKUPROOT);
5419fb3b1a7SJosef Bacik 
5429fb3b1a7SJosef Bacik 		/* If we're loading the backup roots we can't trust the space cache. */
5439fb3b1a7SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, CLEAR_CACHE);
54417b36120SJosef Bacik 		break;
54517b36120SJosef Bacik 	case Opt_skip_balance:
54617b36120SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, SKIP_BALANCE);
54717b36120SJosef Bacik 		break;
54817b36120SJosef Bacik 	case Opt_fatal_errors:
54917b36120SJosef Bacik 		switch (result.uint_32) {
55017b36120SJosef Bacik 		case Opt_fatal_errors_panic:
55117b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, PANIC_ON_FATAL_ERROR);
55217b36120SJosef Bacik 			break;
55317b36120SJosef Bacik 		case Opt_fatal_errors_bug:
55417b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, PANIC_ON_FATAL_ERROR);
55517b36120SJosef Bacik 			break;
55617b36120SJosef Bacik 		default:
55717b36120SJosef Bacik 			btrfs_err(NULL, "unrecognized fatal_errors value %s",
55817b36120SJosef Bacik 				  param->key);
55917b36120SJosef Bacik 			return -EINVAL;
56017b36120SJosef Bacik 		}
56117b36120SJosef Bacik 		break;
56217b36120SJosef Bacik 	case Opt_commit_interval:
56317b36120SJosef Bacik 		ctx->commit_interval = result.uint_32;
56417b36120SJosef Bacik 		if (ctx->commit_interval == 0)
56517b36120SJosef Bacik 			ctx->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
56617b36120SJosef Bacik 		break;
56717b36120SJosef Bacik 	case Opt_rescue:
56817b36120SJosef Bacik 		switch (result.uint_32) {
56917b36120SJosef Bacik 		case Opt_rescue_usebackuproot:
57017b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, USEBACKUPROOT);
57117b36120SJosef Bacik 			break;
57217b36120SJosef Bacik 		case Opt_rescue_nologreplay:
57317b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NOLOGREPLAY);
57417b36120SJosef Bacik 			break;
57517b36120SJosef Bacik 		case Opt_rescue_ignorebadroots:
57617b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, IGNOREBADROOTS);
57717b36120SJosef Bacik 			break;
57817b36120SJosef Bacik 		case Opt_rescue_ignoredatacsums:
57917b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, IGNOREDATACSUMS);
58017b36120SJosef Bacik 			break;
581169aaaf2SQu Wenruo 		case Opt_rescue_ignoremetacsums:
582169aaaf2SQu Wenruo 			btrfs_set_opt(ctx->mount_opt, IGNOREMETACSUMS);
583169aaaf2SQu Wenruo 			break;
58432e62165SQu Wenruo 		case Opt_rescue_ignoresuperflags:
58532e62165SQu Wenruo 			btrfs_set_opt(ctx->mount_opt, IGNORESUPERFLAGS);
58632e62165SQu Wenruo 			break;
58717b36120SJosef Bacik 		case Opt_rescue_parameter_all:
58817b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, IGNOREDATACSUMS);
589169aaaf2SQu Wenruo 			btrfs_set_opt(ctx->mount_opt, IGNOREMETACSUMS);
59032e62165SQu Wenruo 			btrfs_set_opt(ctx->mount_opt, IGNORESUPERFLAGS);
59117b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, IGNOREBADROOTS);
59217b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NOLOGREPLAY);
59317b36120SJosef Bacik 			break;
59417b36120SJosef Bacik 		default:
59517b36120SJosef Bacik 			btrfs_info(NULL, "unrecognized rescue option '%s'",
59617b36120SJosef Bacik 				   param->key);
59717b36120SJosef Bacik 			return -EINVAL;
59817b36120SJosef Bacik 		}
59917b36120SJosef Bacik 		break;
60017b36120SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
60117b36120SJosef Bacik 	case Opt_fragment:
60217b36120SJosef Bacik 		switch (result.uint_32) {
60317b36120SJosef Bacik 		case Opt_fragment_parameter_all:
60417b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, FRAGMENT_DATA);
60517b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, FRAGMENT_METADATA);
60617b36120SJosef Bacik 			break;
60717b36120SJosef Bacik 		case Opt_fragment_parameter_metadata:
60817b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, FRAGMENT_METADATA);
60917b36120SJosef Bacik 			break;
61017b36120SJosef Bacik 		case Opt_fragment_parameter_data:
61117b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, FRAGMENT_DATA);
61217b36120SJosef Bacik 			break;
61317b36120SJosef Bacik 		default:
61417b36120SJosef Bacik 			btrfs_info(NULL, "unrecognized fragment option '%s'",
61517b36120SJosef Bacik 				   param->key);
61617b36120SJosef Bacik 			return -EINVAL;
61717b36120SJosef Bacik 		}
61817b36120SJosef Bacik 		break;
61917b36120SJosef Bacik #endif
62017b36120SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
62117b36120SJosef Bacik 	case Opt_ref_verify:
62217b36120SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, REF_VERIFY);
62317b36120SJosef Bacik 		break;
62417b36120SJosef Bacik #endif
62517b36120SJosef Bacik 	default:
62617b36120SJosef Bacik 		btrfs_err(NULL, "unrecognized mount option '%s'", param->key);
62717b36120SJosef Bacik 		return -EINVAL;
62817b36120SJosef Bacik 	}
62917b36120SJosef Bacik 
63017b36120SJosef Bacik 	return 0;
63117b36120SJosef Bacik }
63217b36120SJosef Bacik 
63383e3a40aSJosef Bacik /*
63483e3a40aSJosef Bacik  * Some options only have meaning at mount time and shouldn't persist across
63583e3a40aSJosef Bacik  * remounts, or be displayed. Clear these at the end of mount and remount code
63683e3a40aSJosef Bacik  * paths.
63783e3a40aSJosef Bacik  */
btrfs_clear_oneshot_options(struct btrfs_fs_info * fs_info)63883e3a40aSJosef Bacik static void btrfs_clear_oneshot_options(struct btrfs_fs_info *fs_info)
63983e3a40aSJosef Bacik {
64083e3a40aSJosef Bacik 	btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT);
64183e3a40aSJosef Bacik 	btrfs_clear_opt(fs_info->mount_opt, CLEAR_CACHE);
64283e3a40aSJosef Bacik 	btrfs_clear_opt(fs_info->mount_opt, NOSPACECACHE);
64383e3a40aSJosef Bacik }
64483e3a40aSJosef Bacik 
check_ro_option(const struct btrfs_fs_info * fs_info,unsigned long long mount_opt,unsigned long long opt,const char * opt_name)6452917f741SDavid Sterba static bool check_ro_option(const struct btrfs_fs_info *fs_info,
646c3ece6b7SQu Wenruo 			    unsigned long long mount_opt, unsigned long long opt,
647d70bf748SJosef Bacik 			    const char *opt_name)
648d70bf748SJosef Bacik {
649eddb1a43SJosef Bacik 	if (mount_opt & opt) {
650d70bf748SJosef Bacik 		btrfs_err(fs_info, "%s must be used with ro mount option",
651d70bf748SJosef Bacik 			  opt_name);
652d70bf748SJosef Bacik 		return true;
653d70bf748SJosef Bacik 	}
654d70bf748SJosef Bacik 	return false;
655d70bf748SJosef Bacik }
656d70bf748SJosef Bacik 
btrfs_check_options(const struct btrfs_fs_info * info,unsigned long long * mount_opt,unsigned long flags)657c3ece6b7SQu Wenruo bool btrfs_check_options(const struct btrfs_fs_info *info,
658c3ece6b7SQu Wenruo 			 unsigned long long *mount_opt,
659eddb1a43SJosef Bacik 			 unsigned long flags)
66074ef0018SQu Wenruo {
6612b41b19dSJosef Bacik 	bool ret = true;
66274ef0018SQu Wenruo 
6632b41b19dSJosef Bacik 	if (!(flags & SB_RDONLY) &&
664eddb1a43SJosef Bacik 	    (check_ro_option(info, *mount_opt, BTRFS_MOUNT_NOLOGREPLAY, "nologreplay") ||
665eddb1a43SJosef Bacik 	     check_ro_option(info, *mount_opt, BTRFS_MOUNT_IGNOREBADROOTS, "ignorebadroots") ||
666169aaaf2SQu Wenruo 	     check_ro_option(info, *mount_opt, BTRFS_MOUNT_IGNOREDATACSUMS, "ignoredatacsums") ||
66732e62165SQu Wenruo 	     check_ro_option(info, *mount_opt, BTRFS_MOUNT_IGNOREMETACSUMS, "ignoremetacsums") ||
66832e62165SQu Wenruo 	     check_ro_option(info, *mount_opt, BTRFS_MOUNT_IGNORESUPERFLAGS, "ignoresuperflags")))
6692b41b19dSJosef Bacik 		ret = false;
67074ef0018SQu Wenruo 
6710b246afaSJeff Mahoney 	if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) &&
672eddb1a43SJosef Bacik 	    !btrfs_raw_test_opt(*mount_opt, FREE_SPACE_TREE) &&
673eddb1a43SJosef Bacik 	    !btrfs_raw_test_opt(*mount_opt, CLEAR_CACHE)) {
6742b41b19dSJosef Bacik 		btrfs_err(info, "cannot disable free-space-tree");
6752b41b19dSJosef Bacik 		ret = false;
67664b5d5b2SQu Wenruo 	}
67764b5d5b2SQu Wenruo 	if (btrfs_fs_compat_ro(info, BLOCK_GROUP_TREE) &&
678eddb1a43SJosef Bacik 	     !btrfs_raw_test_opt(*mount_opt, FREE_SPACE_TREE)) {
6792b41b19dSJosef Bacik 		btrfs_err(info, "cannot disable free-space-tree with block-group-tree feature");
6802b41b19dSJosef Bacik 		ret = false;
68170f6d82eSOmar Sandoval 	}
6822b41b19dSJosef Bacik 
683eddb1a43SJosef Bacik 	if (btrfs_check_mountopts_zoned(info, mount_opt))
6842b41b19dSJosef Bacik 		ret = false;
6852b41b19dSJosef Bacik 
6862b41b19dSJosef Bacik 	if (!test_bit(BTRFS_FS_STATE_REMOUNTING, &info->fs_state)) {
6871e7bec1fSJosef Bacik 		if (btrfs_raw_test_opt(*mount_opt, SPACE_CACHE)) {
6880b246afaSJeff Mahoney 			btrfs_info(info, "disk space caching is enabled");
6891e7bec1fSJosef Bacik 			btrfs_warn(info,
6901e7bec1fSJosef Bacik "space cache v1 is being deprecated and will be removed in a future release, please use -o space_cache=v2");
6911e7bec1fSJosef Bacik 		}
692eddb1a43SJosef Bacik 		if (btrfs_raw_test_opt(*mount_opt, FREE_SPACE_TREE))
6932b41b19dSJosef Bacik 			btrfs_info(info, "using free-space-tree");
694dbecac26SMaciej S. Szmigiero 	}
6952b41b19dSJosef Bacik 
696a7a3f7caSSage Weil 	return ret;
697edf24abeSChristoph Hellwig }
698edf24abeSChristoph Hellwig 
699edf24abeSChristoph Hellwig /*
700a6a8f22aSJosef Bacik  * This is subtle, we only call this during open_ctree().  We need to pre-load
701a6a8f22aSJosef Bacik  * the mount options with the on-disk settings.  Before the new mount API took
702a6a8f22aSJosef Bacik  * effect we would do this on mount and remount.  With the new mount API we'll
703a6a8f22aSJosef Bacik  * only do this on the initial mount.
704edf24abeSChristoph Hellwig  *
705a6a8f22aSJosef Bacik  * This isn't a change in behavior, because we're using the current state of the
706a6a8f22aSJosef Bacik  * file system to set the current mount options.  If you mounted with special
707a6a8f22aSJosef Bacik  * options to disable these features and then remounted we wouldn't revert the
708a6a8f22aSJosef Bacik  * settings, because mounting without these features cleared the on-disk
709a6a8f22aSJosef Bacik  * settings, so this being called on re-mount is not needed.
710edf24abeSChristoph Hellwig  */
btrfs_set_free_space_cache_settings(struct btrfs_fs_info * fs_info)711a6a8f22aSJosef Bacik void btrfs_set_free_space_cache_settings(struct btrfs_fs_info *fs_info)
712edf24abeSChristoph Hellwig {
713a6a8f22aSJosef Bacik 	if (fs_info->sectorsize < PAGE_SIZE) {
714a6a8f22aSJosef Bacik 		btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
715a6a8f22aSJosef Bacik 		if (!btrfs_test_opt(fs_info, FREE_SPACE_TREE)) {
716a6a8f22aSJosef Bacik 			btrfs_info(fs_info,
717a6a8f22aSJosef Bacik 				   "forcing free space tree for sector size %u with page size %lu",
718a6a8f22aSJosef Bacik 				   fs_info->sectorsize, PAGE_SIZE);
719a6a8f22aSJosef Bacik 			btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
720a6a8f22aSJosef Bacik 		}
721a6a8f22aSJosef Bacik 	}
722d7407606SMisono, Tomohiro 
723d7407606SMisono, Tomohiro 	/*
724ad21f15bSJosef Bacik 	 * At this point our mount options are populated, so we only mess with
725ad21f15bSJosef Bacik 	 * these settings if we don't have any settings already.
726d7407606SMisono, Tomohiro 	 */
727ad21f15bSJosef Bacik 	if (btrfs_test_opt(fs_info, FREE_SPACE_TREE))
728ad21f15bSJosef Bacik 		return;
729d7407606SMisono, Tomohiro 
730ad21f15bSJosef Bacik 	if (btrfs_is_zoned(fs_info) &&
731ad21f15bSJosef Bacik 	    btrfs_free_space_cache_v1_active(fs_info)) {
732ad21f15bSJosef Bacik 		btrfs_info(fs_info, "zoned: clearing existing space cache");
733ad21f15bSJosef Bacik 		btrfs_set_super_cache_generation(fs_info->super_copy, 0);
734ad21f15bSJosef Bacik 		return;
73536350e95SGu Jinxiang 	}
736d7407606SMisono, Tomohiro 
737ad21f15bSJosef Bacik 	if (btrfs_test_opt(fs_info, SPACE_CACHE))
738ad21f15bSJosef Bacik 		return;
739ad21f15bSJosef Bacik 
740ad21f15bSJosef Bacik 	if (btrfs_test_opt(fs_info, NOSPACECACHE))
741ad21f15bSJosef Bacik 		return;
742d7407606SMisono, Tomohiro 
743d7407606SMisono, Tomohiro 	/*
744ad21f15bSJosef Bacik 	 * At this point we don't have explicit options set by the user, set
745ad21f15bSJosef Bacik 	 * them ourselves based on the state of the file system.
746ad21f15bSJosef Bacik 	 */
747ad21f15bSJosef Bacik 	if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
748ad21f15bSJosef Bacik 		btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
749ad21f15bSJosef Bacik 	else if (btrfs_free_space_cache_v1_active(fs_info))
750ad21f15bSJosef Bacik 		btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE);
751a6a8f22aSJosef Bacik }
752a6a8f22aSJosef Bacik 
set_device_specific_options(struct btrfs_fs_info * fs_info)75341d46b29SJosef Bacik static void set_device_specific_options(struct btrfs_fs_info *fs_info)
75441d46b29SJosef Bacik {
75541d46b29SJosef Bacik 	if (!btrfs_test_opt(fs_info, NOSSD) &&
75641d46b29SJosef Bacik 	    !fs_info->fs_devices->rotating)
75741d46b29SJosef Bacik 		btrfs_set_opt(fs_info->mount_opt, SSD);
75841d46b29SJosef Bacik 
75941d46b29SJosef Bacik 	/*
76041d46b29SJosef Bacik 	 * For devices supporting discard turn on discard=async automatically,
76141d46b29SJosef Bacik 	 * unless it's already set or disabled. This could be turned off by
76241d46b29SJosef Bacik 	 * nodiscard for the same mount.
763d7407606SMisono, Tomohiro 	 *
76441d46b29SJosef Bacik 	 * The zoned mode piggy backs on the discard functionality for
76541d46b29SJosef Bacik 	 * resetting a zone. There is no reason to delay the zone reset as it is
76641d46b29SJosef Bacik 	 * fast enough. So, do not enable async discard for zoned mode.
767d7407606SMisono, Tomohiro 	 */
76841d46b29SJosef Bacik 	if (!(btrfs_test_opt(fs_info, DISCARD_SYNC) ||
76941d46b29SJosef Bacik 	      btrfs_test_opt(fs_info, DISCARD_ASYNC) ||
77041d46b29SJosef Bacik 	      btrfs_test_opt(fs_info, NODISCARD)) &&
77141d46b29SJosef Bacik 	    fs_info->fs_devices->discardable &&
77241d46b29SJosef Bacik 	    !btrfs_is_zoned(fs_info))
77341d46b29SJosef Bacik 		btrfs_set_opt(fs_info->mount_opt, DISCARD_ASYNC);
77495e05289SChris Mason }
77595e05289SChris Mason 
btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info * fs_info,u64 subvol_objectid)776c0c907a4SMarcos Paulo de Souza char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
77773f73415SJosef Bacik 					  u64 subvol_objectid)
77873f73415SJosef Bacik {
779815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
7805168489aSJosef Bacik 	struct btrfs_root *fs_root = NULL;
78105dbe683SOmar Sandoval 	struct btrfs_root_ref *root_ref;
78205dbe683SOmar Sandoval 	struct btrfs_inode_ref *inode_ref;
78305dbe683SOmar Sandoval 	struct btrfs_key key;
78405dbe683SOmar Sandoval 	struct btrfs_path *path = NULL;
78505dbe683SOmar Sandoval 	char *name = NULL, *ptr;
78605dbe683SOmar Sandoval 	u64 dirid;
78705dbe683SOmar Sandoval 	int len;
78805dbe683SOmar Sandoval 	int ret;
78905dbe683SOmar Sandoval 
79005dbe683SOmar Sandoval 	path = btrfs_alloc_path();
79105dbe683SOmar Sandoval 	if (!path) {
79205dbe683SOmar Sandoval 		ret = -ENOMEM;
79305dbe683SOmar Sandoval 		goto err;
79405dbe683SOmar Sandoval 	}
79505dbe683SOmar Sandoval 
7963ec83621SDavid Sterba 	name = kmalloc(PATH_MAX, GFP_KERNEL);
79705dbe683SOmar Sandoval 	if (!name) {
79805dbe683SOmar Sandoval 		ret = -ENOMEM;
79905dbe683SOmar Sandoval 		goto err;
80005dbe683SOmar Sandoval 	}
80105dbe683SOmar Sandoval 	ptr = name + PATH_MAX - 1;
80205dbe683SOmar Sandoval 	ptr[0] = '\0';
80305dbe683SOmar Sandoval 
80405dbe683SOmar Sandoval 	/*
80505dbe683SOmar Sandoval 	 * Walk up the subvolume trees in the tree of tree roots by root
80605dbe683SOmar Sandoval 	 * backrefs until we hit the top-level subvolume.
80705dbe683SOmar Sandoval 	 */
80805dbe683SOmar Sandoval 	while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
80905dbe683SOmar Sandoval 		key.objectid = subvol_objectid;
81005dbe683SOmar Sandoval 		key.type = BTRFS_ROOT_BACKREF_KEY;
81105dbe683SOmar Sandoval 		key.offset = (u64)-1;
81205dbe683SOmar Sandoval 
8130ff40a91SMarcos Paulo de Souza 		ret = btrfs_search_backwards(root, &key, path);
81405dbe683SOmar Sandoval 		if (ret < 0) {
81505dbe683SOmar Sandoval 			goto err;
81605dbe683SOmar Sandoval 		} else if (ret > 0) {
81705dbe683SOmar Sandoval 			ret = -ENOENT;
81805dbe683SOmar Sandoval 			goto err;
81905dbe683SOmar Sandoval 		}
82005dbe683SOmar Sandoval 
82105dbe683SOmar Sandoval 		subvol_objectid = key.offset;
82205dbe683SOmar Sandoval 
82305dbe683SOmar Sandoval 		root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
82405dbe683SOmar Sandoval 					  struct btrfs_root_ref);
82505dbe683SOmar Sandoval 		len = btrfs_root_ref_name_len(path->nodes[0], root_ref);
82605dbe683SOmar Sandoval 		ptr -= len + 1;
82705dbe683SOmar Sandoval 		if (ptr < name) {
82805dbe683SOmar Sandoval 			ret = -ENAMETOOLONG;
82905dbe683SOmar Sandoval 			goto err;
83005dbe683SOmar Sandoval 		}
83105dbe683SOmar Sandoval 		read_extent_buffer(path->nodes[0], ptr + 1,
83205dbe683SOmar Sandoval 				   (unsigned long)(root_ref + 1), len);
83305dbe683SOmar Sandoval 		ptr[0] = '/';
83405dbe683SOmar Sandoval 		dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);
83505dbe683SOmar Sandoval 		btrfs_release_path(path);
83605dbe683SOmar Sandoval 
83756e9357aSDavid Sterba 		fs_root = btrfs_get_fs_root(fs_info, subvol_objectid, true);
83805dbe683SOmar Sandoval 		if (IS_ERR(fs_root)) {
83905dbe683SOmar Sandoval 			ret = PTR_ERR(fs_root);
8405168489aSJosef Bacik 			fs_root = NULL;
8415168489aSJosef Bacik 			goto err;
8425168489aSJosef Bacik 		}
84305dbe683SOmar Sandoval 
84405dbe683SOmar Sandoval 		/*
84505dbe683SOmar Sandoval 		 * Walk up the filesystem tree by inode refs until we hit the
84605dbe683SOmar Sandoval 		 * root directory.
84705dbe683SOmar Sandoval 		 */
84805dbe683SOmar Sandoval 		while (dirid != BTRFS_FIRST_FREE_OBJECTID) {
84905dbe683SOmar Sandoval 			key.objectid = dirid;
85005dbe683SOmar Sandoval 			key.type = BTRFS_INODE_REF_KEY;
85105dbe683SOmar Sandoval 			key.offset = (u64)-1;
85205dbe683SOmar Sandoval 
8530ff40a91SMarcos Paulo de Souza 			ret = btrfs_search_backwards(fs_root, &key, path);
85405dbe683SOmar Sandoval 			if (ret < 0) {
85505dbe683SOmar Sandoval 				goto err;
85605dbe683SOmar Sandoval 			} else if (ret > 0) {
85705dbe683SOmar Sandoval 				ret = -ENOENT;
85805dbe683SOmar Sandoval 				goto err;
85905dbe683SOmar Sandoval 			}
86005dbe683SOmar Sandoval 
86105dbe683SOmar Sandoval 			dirid = key.offset;
86205dbe683SOmar Sandoval 
86305dbe683SOmar Sandoval 			inode_ref = btrfs_item_ptr(path->nodes[0],
86405dbe683SOmar Sandoval 						   path->slots[0],
86505dbe683SOmar Sandoval 						   struct btrfs_inode_ref);
86605dbe683SOmar Sandoval 			len = btrfs_inode_ref_name_len(path->nodes[0],
86705dbe683SOmar Sandoval 						       inode_ref);
86805dbe683SOmar Sandoval 			ptr -= len + 1;
86905dbe683SOmar Sandoval 			if (ptr < name) {
87005dbe683SOmar Sandoval 				ret = -ENAMETOOLONG;
87105dbe683SOmar Sandoval 				goto err;
87205dbe683SOmar Sandoval 			}
87305dbe683SOmar Sandoval 			read_extent_buffer(path->nodes[0], ptr + 1,
87405dbe683SOmar Sandoval 					   (unsigned long)(inode_ref + 1), len);
87505dbe683SOmar Sandoval 			ptr[0] = '/';
87605dbe683SOmar Sandoval 			btrfs_release_path(path);
87705dbe683SOmar Sandoval 		}
87800246528SJosef Bacik 		btrfs_put_root(fs_root);
8795168489aSJosef Bacik 		fs_root = NULL;
88005dbe683SOmar Sandoval 	}
88105dbe683SOmar Sandoval 
88205dbe683SOmar Sandoval 	btrfs_free_path(path);
88305dbe683SOmar Sandoval 	if (ptr == name + PATH_MAX - 1) {
88405dbe683SOmar Sandoval 		name[0] = '/';
88505dbe683SOmar Sandoval 		name[1] = '\0';
88605dbe683SOmar Sandoval 	} else {
88705dbe683SOmar Sandoval 		memmove(name, ptr, name + PATH_MAX - ptr);
88805dbe683SOmar Sandoval 	}
88905dbe683SOmar Sandoval 	return name;
89005dbe683SOmar Sandoval 
89105dbe683SOmar Sandoval err:
89200246528SJosef Bacik 	btrfs_put_root(fs_root);
89305dbe683SOmar Sandoval 	btrfs_free_path(path);
89405dbe683SOmar Sandoval 	kfree(name);
89505dbe683SOmar Sandoval 	return ERR_PTR(ret);
89605dbe683SOmar Sandoval }
89705dbe683SOmar Sandoval 
get_default_subvol_objectid(struct btrfs_fs_info * fs_info,u64 * objectid)89805dbe683SOmar Sandoval static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid)
89905dbe683SOmar Sandoval {
90005dbe683SOmar Sandoval 	struct btrfs_root *root = fs_info->tree_root;
90173f73415SJosef Bacik 	struct btrfs_dir_item *di;
90273f73415SJosef Bacik 	struct btrfs_path *path;
90373f73415SJosef Bacik 	struct btrfs_key location;
9046db75318SSweet Tea Dorminy 	struct fscrypt_str name = FSTR_INIT("default", 7);
90573f73415SJosef Bacik 	u64 dir_id;
90673f73415SJosef Bacik 
90773f73415SJosef Bacik 	path = btrfs_alloc_path();
90873f73415SJosef Bacik 	if (!path)
90905dbe683SOmar Sandoval 		return -ENOMEM;
91073f73415SJosef Bacik 
91173f73415SJosef Bacik 	/*
91273f73415SJosef Bacik 	 * Find the "default" dir item which points to the root item that we
91373f73415SJosef Bacik 	 * will mount by default if we haven't been given a specific subvolume
91473f73415SJosef Bacik 	 * to mount.
91573f73415SJosef Bacik 	 */
916815745cfSAl Viro 	dir_id = btrfs_super_root_dir(fs_info->super_copy);
917e43eec81SSweet Tea Dorminy 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, &name, 0);
918b0839166SJulia Lawall 	if (IS_ERR(di)) {
919b0839166SJulia Lawall 		btrfs_free_path(path);
92005dbe683SOmar Sandoval 		return PTR_ERR(di);
921b0839166SJulia Lawall 	}
92273f73415SJosef Bacik 	if (!di) {
92373f73415SJosef Bacik 		/*
92473f73415SJosef Bacik 		 * Ok the default dir item isn't there.  This is weird since
92573f73415SJosef Bacik 		 * it's always been there, but don't freak out, just try and
92605dbe683SOmar Sandoval 		 * mount the top-level subvolume.
92773f73415SJosef Bacik 		 */
92873f73415SJosef Bacik 		btrfs_free_path(path);
92905dbe683SOmar Sandoval 		*objectid = BTRFS_FS_TREE_OBJECTID;
93005dbe683SOmar Sandoval 		return 0;
93173f73415SJosef Bacik 	}
93273f73415SJosef Bacik 
93373f73415SJosef Bacik 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
93473f73415SJosef Bacik 	btrfs_free_path(path);
93505dbe683SOmar Sandoval 	*objectid = location.objectid;
93605dbe683SOmar Sandoval 	return 0;
93773f73415SJosef Bacik }
93873f73415SJosef Bacik 
btrfs_fill_super(struct super_block * sb,struct btrfs_fs_devices * fs_devices,void * data)9398a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb,
9408a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
94156e033a7SDavid Sterba 			    void *data)
9422e635a27SChris Mason {
9432e635a27SChris Mason 	struct inode *inode;
944815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
94539279cc3SChris Mason 	int err;
9462e635a27SChris Mason 
9472e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
9482e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
949e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
950af53d29aSAl Viro 	sb->s_d_op = &btrfs_dentry_operations;
951be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
95214605409SBoris Burkov #ifdef CONFIG_FS_VERITY
95314605409SBoris Burkov 	sb->s_vop = &btrfs_verityops;
95414605409SBoris Burkov #endif
9555103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
9562e635a27SChris Mason 	sb->s_time_gran = 1;
957da2f0f74SChris Mason 	sb->s_iflags |= SB_I_CGROUPWB;
9589e11ceeeSJan Kara 
9599e11ceeeSJan Kara 	err = super_setup_bdi(sb);
9609e11ceeeSJan Kara 	if (err) {
9619e11ceeeSJan Kara 		btrfs_err(fs_info, "super_setup_bdi failed");
9629e11ceeeSJan Kara 		return err;
9639e11ceeeSJan Kara 	}
9649e11ceeeSJan Kara 
965ad2b2c80SAl Viro 	err = open_ctree(sb, fs_devices, (char *)data);
966ad2b2c80SAl Viro 	if (err) {
967ab8d0fc4SJeff Mahoney 		btrfs_err(fs_info, "open_ctree failed");
968ad2b2c80SAl Viro 		return err;
969e20d96d6SChris Mason 	}
970b888db2bSChris Mason 
971d13240ddSFilipe Manana 	inode = btrfs_iget(BTRFS_FIRST_FREE_OBJECTID, fs_info->fs_root);
9725d4f98a2SYan Zheng 	if (IS_ERR(inode)) {
9735d4f98a2SYan Zheng 		err = PTR_ERR(inode);
97413b98989SJosef Bacik 		btrfs_handle_fs_error(fs_info, err, NULL);
97539279cc3SChris Mason 		goto fail_close;
97639279cc3SChris Mason 	}
9772e635a27SChris Mason 
97848fde701SAl Viro 	sb->s_root = d_make_root(inode);
97948fde701SAl Viro 	if (!sb->s_root) {
98039279cc3SChris Mason 		err = -ENOMEM;
98139279cc3SChris Mason 		goto fail_close;
9822e635a27SChris Mason 	}
98358176a96SJosef Bacik 
9841751e8a6SLinus Torvalds 	sb->s_flags |= SB_ACTIVE;
9852e635a27SChris Mason 	return 0;
9862e635a27SChris Mason 
98739279cc3SChris Mason fail_close:
9886bccf3abSJeff Mahoney 	close_ctree(fs_info);
989d5719762SChris Mason 	return err;
990d5719762SChris Mason }
991d5719762SChris Mason 
btrfs_sync_fs(struct super_block * sb,int wait)9926bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
993d5719762SChris Mason {
994d5719762SChris Mason 	struct btrfs_trans_handle *trans;
995815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
996815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
997df2ce34cSChris Mason 
998bc074524SJeff Mahoney 	trace_btrfs_sync_fs(fs_info, wait);
9991abe9b8aSliubo 
1000d561c025SChris Mason 	if (!wait) {
1001815745cfSAl Viro 		filemap_flush(fs_info->btree_inode->i_mapping);
1002df2ce34cSChris Mason 		return 0;
1003d561c025SChris Mason 	}
1004771ed689SChris Mason 
100542317ab4SDavid Sterba 	btrfs_wait_ordered_roots(fs_info, U64_MAX, NULL);
1006771ed689SChris Mason 
1007d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
100860376ce4SJosef Bacik 	if (IS_ERR(trans)) {
1009354aa0fbSMiao Xie 		/* no transaction, don't bother */
10106b5fe46dSDavid Sterba 		if (PTR_ERR(trans) == -ENOENT) {
10116b5fe46dSDavid Sterba 			/*
10126b5fe46dSDavid Sterba 			 * Exit unless we have some pending changes
10136b5fe46dSDavid Sterba 			 * that need to go through commit
10146b5fe46dSDavid Sterba 			 */
1015c52cc7b7SJosef Bacik 			if (!test_bit(BTRFS_FS_NEED_TRANS_COMMIT,
1016c52cc7b7SJosef Bacik 				      &fs_info->flags))
1017bd7de2c9SJosef Bacik 				return 0;
1018a53f4f8eSQu Wenruo 			/*
1019a53f4f8eSQu Wenruo 			 * A non-blocking test if the fs is frozen. We must not
1020a53f4f8eSQu Wenruo 			 * start a new transaction here otherwise a deadlock
1021a53f4f8eSQu Wenruo 			 * happens. The pending operations are delayed to the
1022a53f4f8eSQu Wenruo 			 * next commit after thawing.
1023a53f4f8eSQu Wenruo 			 */
1024a7e3c5f2SRakesh Pandit 			if (sb_start_write_trylock(sb))
1025a7e3c5f2SRakesh Pandit 				sb_end_write(sb);
1026a53f4f8eSQu Wenruo 			else
1027a53f4f8eSQu Wenruo 				return 0;
10286b5fe46dSDavid Sterba 			trans = btrfs_start_transaction(root, 0);
102960376ce4SJosef Bacik 		}
103098bd5c54SDavid Sterba 		if (IS_ERR(trans))
103198bd5c54SDavid Sterba 			return PTR_ERR(trans);
10326b5fe46dSDavid Sterba 	}
10333a45bb20SJeff Mahoney 	return btrfs_commit_transaction(trans);
1034d5719762SChris Mason }
1035d5719762SChris Mason 
print_rescue_option(struct seq_file * seq,const char * s,bool * printed)1036ab0b4a3eSJosef Bacik static void print_rescue_option(struct seq_file *seq, const char *s, bool *printed)
1037ab0b4a3eSJosef Bacik {
1038ab0b4a3eSJosef Bacik 	seq_printf(seq, "%s%s", (*printed) ? ":" : ",rescue=", s);
1039ab0b4a3eSJosef Bacik 	*printed = true;
1040ab0b4a3eSJosef Bacik }
1041ab0b4a3eSJosef Bacik 
btrfs_show_options(struct seq_file * seq,struct dentry * dentry)104234c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
1043a9572a15SEric Paris {
1044815745cfSAl Viro 	struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
10450f628c63SDavid Sterba 	const char *compress_type;
10463ef3959bSJosef Bacik 	const char *subvol_name;
1047ab0b4a3eSJosef Bacik 	bool printed = false;
1048a9572a15SEric Paris 
10493cdde224SJeff Mahoney 	if (btrfs_test_opt(info, DEGRADED))
1050a9572a15SEric Paris 		seq_puts(seq, ",degraded");
10513cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NODATASUM))
1052a9572a15SEric Paris 		seq_puts(seq, ",nodatasum");
10533cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NODATACOW))
1054a9572a15SEric Paris 		seq_puts(seq, ",nodatacow");
10553cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOBARRIER))
1056a9572a15SEric Paris 		seq_puts(seq, ",nobarrier");
105795ac567aSFilipe David Borba Manana 	if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
1058c1c9ff7cSGeert Uytterhoeven 		seq_printf(seq, ",max_inline=%llu", info->max_inline);
1059a9572a15SEric Paris 	if (info->thread_pool_size !=  min_t(unsigned long,
1060a9572a15SEric Paris 					     num_online_cpus() + 2, 8))
1061f7b885beSAnand Jain 		seq_printf(seq, ",thread_pool=%u", info->thread_pool_size);
10623cdde224SJeff Mahoney 	if (btrfs_test_opt(info, COMPRESS)) {
10630f628c63SDavid Sterba 		compress_type = btrfs_compress_type2str(info->compress_type);
10643cdde224SJeff Mahoney 		if (btrfs_test_opt(info, FORCE_COMPRESS))
1065200da64eSTsutomu Itoh 			seq_printf(seq, ",compress-force=%s", compress_type);
1066200da64eSTsutomu Itoh 		else
1067200da64eSTsutomu Itoh 			seq_printf(seq, ",compress=%s", compress_type);
1068f51d2b59SDavid Sterba 		if (info->compress_level)
1069fa4d885aSAdam Borowski 			seq_printf(seq, ":%d", info->compress_level);
1070200da64eSTsutomu Itoh 	}
10713cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOSSD))
1072c289811cSChris Mason 		seq_puts(seq, ",nossd");
10733cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SSD_SPREAD))
1074451d7585SChris Mason 		seq_puts(seq, ",ssd_spread");
10753cdde224SJeff Mahoney 	else if (btrfs_test_opt(info, SSD))
1076a9572a15SEric Paris 		seq_puts(seq, ",ssd");
10773cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOTREELOG))
10786b65c5c6SSage Weil 		seq_puts(seq, ",notreelog");
10793cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOLOGREPLAY))
1080ab0b4a3eSJosef Bacik 		print_rescue_option(seq, "nologreplay", &printed);
108168319c18SJosef Bacik 	if (btrfs_test_opt(info, USEBACKUPROOT))
108268319c18SJosef Bacik 		print_rescue_option(seq, "usebackuproot", &printed);
108342437a63SJosef Bacik 	if (btrfs_test_opt(info, IGNOREBADROOTS))
108442437a63SJosef Bacik 		print_rescue_option(seq, "ignorebadroots", &printed);
1085882dbe0cSJosef Bacik 	if (btrfs_test_opt(info, IGNOREDATACSUMS))
1086882dbe0cSJosef Bacik 		print_rescue_option(seq, "ignoredatacsums", &printed);
1087169aaaf2SQu Wenruo 	if (btrfs_test_opt(info, IGNOREMETACSUMS))
1088169aaaf2SQu Wenruo 		print_rescue_option(seq, "ignoremetacsums", &printed);
108932e62165SQu Wenruo 	if (btrfs_test_opt(info, IGNORESUPERFLAGS))
109032e62165SQu Wenruo 		print_rescue_option(seq, "ignoresuperflags", &printed);
10913cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FLUSHONCOMMIT))
10926b65c5c6SSage Weil 		seq_puts(seq, ",flushoncommit");
109346b27f50SDennis Zhou 	if (btrfs_test_opt(info, DISCARD_SYNC))
109420a5239aSMatthew Wilcox 		seq_puts(seq, ",discard");
1095b0643e59SDennis Zhou 	if (btrfs_test_opt(info, DISCARD_ASYNC))
1096b0643e59SDennis Zhou 		seq_puts(seq, ",discard=async");
10971751e8a6SLinus Torvalds 	if (!(info->sb->s_flags & SB_POSIXACL))
1098a9572a15SEric Paris 		seq_puts(seq, ",noacl");
109904c41559SBoris Burkov 	if (btrfs_free_space_cache_v1_active(info))
1100200da64eSTsutomu Itoh 		seq_puts(seq, ",space_cache");
110104c41559SBoris Burkov 	else if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
110270f6d82eSOmar Sandoval 		seq_puts(seq, ",space_cache=v2");
110373bc1876SJosef Bacik 	else
11048965593eSDavid Sterba 		seq_puts(seq, ",nospace_cache");
11053cdde224SJeff Mahoney 	if (btrfs_test_opt(info, RESCAN_UUID_TREE))
1106f420ee1eSStefan Behrens 		seq_puts(seq, ",rescan_uuid_tree");
11073cdde224SJeff Mahoney 	if (btrfs_test_opt(info, CLEAR_CACHE))
1108200da64eSTsutomu Itoh 		seq_puts(seq, ",clear_cache");
11093cdde224SJeff Mahoney 	if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED))
1110200da64eSTsutomu Itoh 		seq_puts(seq, ",user_subvol_rm_allowed");
11113cdde224SJeff Mahoney 	if (btrfs_test_opt(info, ENOSPC_DEBUG))
11120942caa3SDavid Sterba 		seq_puts(seq, ",enospc_debug");
11133cdde224SJeff Mahoney 	if (btrfs_test_opt(info, AUTO_DEFRAG))
11140942caa3SDavid Sterba 		seq_puts(seq, ",autodefrag");
11153cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SKIP_BALANCE))
11169555c6c1SIlya Dryomov 		seq_puts(seq, ",skip_balance");
11178507d216SWang Shilong 	if (info->metadata_ratio)
1118764cb8b4SAnand Jain 		seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio);
11193cdde224SJeff Mahoney 	if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR))
11208c342930SJeff Mahoney 		seq_puts(seq, ",fatal_errors=panic");
11218b87dc17SDavid Sterba 	if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
1122d3740608SAnand Jain 		seq_printf(seq, ",commit=%u", info->commit_interval);
1123d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
11243cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FRAGMENT_DATA))
1125d0bd4560SJosef Bacik 		seq_puts(seq, ",fragment=data");
11263cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FRAGMENT_METADATA))
1127d0bd4560SJosef Bacik 		seq_puts(seq, ",fragment=metadata");
1128d0bd4560SJosef Bacik #endif
1129fb592373SJosef Bacik 	if (btrfs_test_opt(info, REF_VERIFY))
1130fb592373SJosef Bacik 		seq_puts(seq, ",ref_verify");
1131e094f480SJosef Bacik 	seq_printf(seq, ",subvolid=%llu", btrfs_root_id(BTRFS_I(d_inode(dentry))->root));
11323ef3959bSJosef Bacik 	subvol_name = btrfs_get_subvol_name_from_objectid(info,
1133e094f480SJosef Bacik 			btrfs_root_id(BTRFS_I(d_inode(dentry))->root));
11343ef3959bSJosef Bacik 	if (!IS_ERR(subvol_name)) {
1135c8d3fe02SOmar Sandoval 		seq_puts(seq, ",subvol=");
11363ef3959bSJosef Bacik 		seq_escape(seq, subvol_name, " \t\n\\");
11373ef3959bSJosef Bacik 		kfree(subvol_name);
11383ef3959bSJosef Bacik 	}
1139a9572a15SEric Paris 	return 0;
1140a9572a15SEric Paris }
1141a9572a15SEric Paris 
1142830c4adbSJosef Bacik /*
1143f9d9ef62SDavid Sterba  * subvolumes are identified by ino 256
1144f9d9ef62SDavid Sterba  */
is_subvolume_inode(struct inode * inode)1145f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode)
1146f9d9ef62SDavid Sterba {
1147f9d9ef62SDavid Sterba 	if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
1148f9d9ef62SDavid Sterba 		return 1;
1149f9d9ef62SDavid Sterba 	return 0;
1150f9d9ef62SDavid Sterba }
1151f9d9ef62SDavid Sterba 
mount_subvol(const char * subvol_name,u64 subvol_objectid,struct vfsmount * mnt)1152bb289b7bSOmar Sandoval static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
1153ae0bc863SAnand Jain 				   struct vfsmount *mnt)
1154830c4adbSJosef Bacik {
1155830c4adbSJosef Bacik 	struct dentry *root;
1156fa330659SOmar Sandoval 	int ret;
1157830c4adbSJosef Bacik 
115805dbe683SOmar Sandoval 	if (!subvol_name) {
115905dbe683SOmar Sandoval 		if (!subvol_objectid) {
116005dbe683SOmar Sandoval 			ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
116105dbe683SOmar Sandoval 							  &subvol_objectid);
116205dbe683SOmar Sandoval 			if (ret) {
116305dbe683SOmar Sandoval 				root = ERR_PTR(ret);
116405dbe683SOmar Sandoval 				goto out;
116505dbe683SOmar Sandoval 			}
116605dbe683SOmar Sandoval 		}
1167c0c907a4SMarcos Paulo de Souza 		subvol_name = btrfs_get_subvol_name_from_objectid(
1168c0c907a4SMarcos Paulo de Souza 					btrfs_sb(mnt->mnt_sb), subvol_objectid);
116905dbe683SOmar Sandoval 		if (IS_ERR(subvol_name)) {
117005dbe683SOmar Sandoval 			root = ERR_CAST(subvol_name);
117105dbe683SOmar Sandoval 			subvol_name = NULL;
117205dbe683SOmar Sandoval 			goto out;
117305dbe683SOmar Sandoval 		}
117405dbe683SOmar Sandoval 
117505dbe683SOmar Sandoval 	}
117605dbe683SOmar Sandoval 
1177ea441d11SAl Viro 	root = mount_subtree(mnt, subvol_name);
1178fa330659SOmar Sandoval 	/* mount_subtree() drops our reference on the vfsmount. */
1179fa330659SOmar Sandoval 	mnt = NULL;
1180830c4adbSJosef Bacik 
1181bb289b7bSOmar Sandoval 	if (!IS_ERR(root)) {
1182ea441d11SAl Viro 		struct super_block *s = root->d_sb;
1183ab8d0fc4SJeff Mahoney 		struct btrfs_fs_info *fs_info = btrfs_sb(s);
1184bb289b7bSOmar Sandoval 		struct inode *root_inode = d_inode(root);
1185e094f480SJosef Bacik 		u64 root_objectid = btrfs_root_id(BTRFS_I(root_inode)->root);
1186bb289b7bSOmar Sandoval 
1187bb289b7bSOmar Sandoval 		ret = 0;
1188bb289b7bSOmar Sandoval 		if (!is_subvolume_inode(root_inode)) {
1189ab8d0fc4SJeff Mahoney 			btrfs_err(fs_info, "'%s' is not a valid subvolume",
1190bb289b7bSOmar Sandoval 			       subvol_name);
1191bb289b7bSOmar Sandoval 			ret = -EINVAL;
1192bb289b7bSOmar Sandoval 		}
1193bb289b7bSOmar Sandoval 		if (subvol_objectid && root_objectid != subvol_objectid) {
119405dbe683SOmar Sandoval 			/*
119505dbe683SOmar Sandoval 			 * This will also catch a race condition where a
119605dbe683SOmar Sandoval 			 * subvolume which was passed by ID is renamed and
119705dbe683SOmar Sandoval 			 * another subvolume is renamed over the old location.
119805dbe683SOmar Sandoval 			 */
1199ab8d0fc4SJeff Mahoney 			btrfs_err(fs_info,
1200ab8d0fc4SJeff Mahoney 				  "subvol '%s' does not match subvolid %llu",
1201bb289b7bSOmar Sandoval 				  subvol_name, subvol_objectid);
1202bb289b7bSOmar Sandoval 			ret = -EINVAL;
1203bb289b7bSOmar Sandoval 		}
1204bb289b7bSOmar Sandoval 		if (ret) {
1205ea441d11SAl Viro 			dput(root);
1206bb289b7bSOmar Sandoval 			root = ERR_PTR(ret);
1207ea441d11SAl Viro 			deactivate_locked_super(s);
1208bb289b7bSOmar Sandoval 		}
1209f9d9ef62SDavid Sterba 	}
1210f9d9ef62SDavid Sterba 
1211fa330659SOmar Sandoval out:
1212fa330659SOmar Sandoval 	mntput(mnt);
1213fa330659SOmar Sandoval 	kfree(subvol_name);
1214830c4adbSJosef Bacik 	return root;
1215830c4adbSJosef Bacik }
1216450ba0eaSJosef Bacik 
btrfs_resize_thread_pool(struct btrfs_fs_info * fs_info,u32 new_pool_size,u32 old_pool_size)12170d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
1218f7b885beSAnand Jain 				     u32 new_pool_size, u32 old_pool_size)
12190d2450abSSergei Trofimovich {
12200d2450abSSergei Trofimovich 	if (new_pool_size == old_pool_size)
12210d2450abSSergei Trofimovich 		return;
12220d2450abSSergei Trofimovich 
12230d2450abSSergei Trofimovich 	fs_info->thread_pool_size = new_pool_size;
12240d2450abSSergei Trofimovich 
1225efe120a0SFrank Holton 	btrfs_info(fs_info, "resize thread pool %d -> %d",
12260d2450abSSergei Trofimovich 	       old_pool_size, new_pool_size);
12270d2450abSSergei Trofimovich 
12285cdc7ad3SQu Wenruo 	btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
1229afe3d242SQu Wenruo 	btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
1230e66f0bb1SQu Wenruo 	btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
123140fac647SChristoph Hellwig 	workqueue_set_max_active(fs_info->endio_workers, new_pool_size);
123240fac647SChristoph Hellwig 	workqueue_set_max_active(fs_info->endio_meta_workers, new_pool_size);
1233fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
1234fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
12355b3bc44eSQu Wenruo 	btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
12360d2450abSSergei Trofimovich }
12370d2450abSSergei Trofimovich 
btrfs_remount_begin(struct btrfs_fs_info * fs_info,unsigned long long old_opts,int flags)1238f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1239c3ece6b7SQu Wenruo 				       unsigned long long old_opts, int flags)
1240f42a34b2SMiao Xie {
1241dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1242dc81cdc5SMiao Xie 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
12431751e8a6SLinus Torvalds 	     (flags & SB_RDONLY))) {
1244dc81cdc5SMiao Xie 		/* wait for any defraggers to finish */
1245dc81cdc5SMiao Xie 		wait_event(fs_info->transaction_wait,
1246dc81cdc5SMiao Xie 			   (atomic_read(&fs_info->defrag_running) == 0));
12471751e8a6SLinus Torvalds 		if (flags & SB_RDONLY)
1248dc81cdc5SMiao Xie 			sync_filesystem(fs_info->sb);
1249dc81cdc5SMiao Xie 	}
1250dc81cdc5SMiao Xie }
1251dc81cdc5SMiao Xie 
btrfs_remount_cleanup(struct btrfs_fs_info * fs_info,unsigned long long old_opts)1252dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1253c3ece6b7SQu Wenruo 					 unsigned long long old_opts)
1254dc81cdc5SMiao Xie {
125594846229SBoris Burkov 	const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE);
125694846229SBoris Burkov 
1257dc81cdc5SMiao Xie 	/*
1258180e4d47SLuis de Bethencourt 	 * We need to cleanup all defragable inodes if the autodefragment is
1259180e4d47SLuis de Bethencourt 	 * close or the filesystem is read only.
1260dc81cdc5SMiao Xie 	 */
1261dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1262bc98a42cSDavid Howells 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) {
1263dc81cdc5SMiao Xie 		btrfs_cleanup_defrag_inodes(fs_info);
1264dc81cdc5SMiao Xie 	}
1265dc81cdc5SMiao Xie 
1266b0643e59SDennis Zhou 	/* If we toggled discard async */
1267b0643e59SDennis Zhou 	if (!btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1268b0643e59SDennis Zhou 	    btrfs_test_opt(fs_info, DISCARD_ASYNC))
1269b0643e59SDennis Zhou 		btrfs_discard_resume(fs_info);
1270b0643e59SDennis Zhou 	else if (btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1271b0643e59SDennis Zhou 		 !btrfs_test_opt(fs_info, DISCARD_ASYNC))
1272b0643e59SDennis Zhou 		btrfs_discard_cleanup(fs_info);
127394846229SBoris Burkov 
127494846229SBoris Burkov 	/* If we toggled space cache */
127594846229SBoris Burkov 	if (cache_opt != btrfs_free_space_cache_v1_active(fs_info))
127694846229SBoris Burkov 		btrfs_set_free_space_cache_v1_active(fs_info, cache_opt);
1277dc81cdc5SMiao Xie }
1278dc81cdc5SMiao Xie 
btrfs_remount_rw(struct btrfs_fs_info * fs_info)12799ef40c2eSJosef Bacik static int btrfs_remount_rw(struct btrfs_fs_info *fs_info)
1280c146afadSYan Zheng {
1281c146afadSYan Zheng 	int ret;
1282c146afadSYan Zheng 
12839ef40c2eSJosef Bacik 	if (BTRFS_FS_ERROR(fs_info)) {
12849ef40c2eSJosef Bacik 		btrfs_err(fs_info,
12859ef40c2eSJosef Bacik 			  "remounting read-write after error is not allowed");
12869ef40c2eSJosef Bacik 		return -EINVAL;
12879ef40c2eSJosef Bacik 	}
12889ef40c2eSJosef Bacik 
12899ef40c2eSJosef Bacik 	if (fs_info->fs_devices->rw_devices == 0)
12909ef40c2eSJosef Bacik 		return -EACCES;
12919ef40c2eSJosef Bacik 
12929ef40c2eSJosef Bacik 	if (!btrfs_check_rw_degradable(fs_info, NULL)) {
12939ef40c2eSJosef Bacik 		btrfs_warn(fs_info,
12949ef40c2eSJosef Bacik 			   "too many missing devices, writable remount is not allowed");
12959ef40c2eSJosef Bacik 		return -EACCES;
12969ef40c2eSJosef Bacik 	}
12979ef40c2eSJosef Bacik 
12989ef40c2eSJosef Bacik 	if (btrfs_super_log_root(fs_info->super_copy) != 0) {
12999ef40c2eSJosef Bacik 		btrfs_warn(fs_info,
13009ef40c2eSJosef Bacik 			   "mount required to replay tree-log, cannot remount read-write");
13019ef40c2eSJosef Bacik 		return -EINVAL;
13029ef40c2eSJosef Bacik 	}
13039ef40c2eSJosef Bacik 
13049ef40c2eSJosef Bacik 	/*
13059ef40c2eSJosef Bacik 	 * NOTE: when remounting with a change that does writes, don't put it
13069ef40c2eSJosef Bacik 	 * anywhere above this point, as we are not sure to be safe to write
13079ef40c2eSJosef Bacik 	 * until we pass the above checks.
13089ef40c2eSJosef Bacik 	 */
13099ef40c2eSJosef Bacik 	ret = btrfs_start_pre_rw_mount(fs_info);
13109ef40c2eSJosef Bacik 	if (ret)
13119ef40c2eSJosef Bacik 		return ret;
13129ef40c2eSJosef Bacik 
13139ef40c2eSJosef Bacik 	btrfs_clear_sb_rdonly(fs_info->sb);
13149ef40c2eSJosef Bacik 
13159ef40c2eSJosef Bacik 	set_bit(BTRFS_FS_OPEN, &fs_info->flags);
13169ef40c2eSJosef Bacik 
13179ef40c2eSJosef Bacik 	/*
13189ef40c2eSJosef Bacik 	 * If we've gone from readonly -> read-write, we need to get our
13199ef40c2eSJosef Bacik 	 * sync/async discard lists in the right state.
13209ef40c2eSJosef Bacik 	 */
13219ef40c2eSJosef Bacik 	btrfs_discard_resume(fs_info);
13229ef40c2eSJosef Bacik 
13239ef40c2eSJosef Bacik 	return 0;
13249ef40c2eSJosef Bacik }
13259ef40c2eSJosef Bacik 
btrfs_remount_ro(struct btrfs_fs_info * fs_info)13269ef40c2eSJosef Bacik static int btrfs_remount_ro(struct btrfs_fs_info *fs_info)
13279ef40c2eSJosef Bacik {
13289ef40c2eSJosef Bacik 	/*
13299ef40c2eSJosef Bacik 	 * This also happens on 'umount -rf' or on shutdown, when the
13309ef40c2eSJosef Bacik 	 * filesystem is busy.
13319ef40c2eSJosef Bacik 	 */
13329ef40c2eSJosef Bacik 	cancel_work_sync(&fs_info->async_reclaim_work);
13339ef40c2eSJosef Bacik 	cancel_work_sync(&fs_info->async_data_reclaim_work);
13349ef40c2eSJosef Bacik 
13359ef40c2eSJosef Bacik 	btrfs_discard_cleanup(fs_info);
13369ef40c2eSJosef Bacik 
13379ef40c2eSJosef Bacik 	/* Wait for the uuid_scan task to finish */
13389ef40c2eSJosef Bacik 	down(&fs_info->uuid_tree_rescan_sem);
13399ef40c2eSJosef Bacik 	/* Avoid complains from lockdep et al. */
13409ef40c2eSJosef Bacik 	up(&fs_info->uuid_tree_rescan_sem);
13419ef40c2eSJosef Bacik 
13429ef40c2eSJosef Bacik 	btrfs_set_sb_rdonly(fs_info->sb);
13439ef40c2eSJosef Bacik 
13449ef40c2eSJosef Bacik 	/*
13459ef40c2eSJosef Bacik 	 * Setting SB_RDONLY will put the cleaner thread to sleep at the next
13469ef40c2eSJosef Bacik 	 * loop if it's already active.  If it's already asleep, we'll leave
13479ef40c2eSJosef Bacik 	 * unused block groups on disk until we're mounted read-write again
13489ef40c2eSJosef Bacik 	 * unless we clean them up here.
13499ef40c2eSJosef Bacik 	 */
13509ef40c2eSJosef Bacik 	btrfs_delete_unused_bgs(fs_info);
13519ef40c2eSJosef Bacik 
13529ef40c2eSJosef Bacik 	/*
13539ef40c2eSJosef Bacik 	 * The cleaner task could be already running before we set the flag
13549ef40c2eSJosef Bacik 	 * BTRFS_FS_STATE_RO (and SB_RDONLY in the superblock).  We must make
13559ef40c2eSJosef Bacik 	 * sure that after we finish the remount, i.e. after we call
13569ef40c2eSJosef Bacik 	 * btrfs_commit_super(), the cleaner can no longer start a transaction
13579ef40c2eSJosef Bacik 	 * - either because it was dropping a dead root, running delayed iputs
13589ef40c2eSJosef Bacik 	 *   or deleting an unused block group (the cleaner picked a block
13599ef40c2eSJosef Bacik 	 *   group from the list of unused block groups before we were able to
13609ef40c2eSJosef Bacik 	 *   in the previous call to btrfs_delete_unused_bgs()).
13619ef40c2eSJosef Bacik 	 */
13629ef40c2eSJosef Bacik 	wait_on_bit(&fs_info->flags, BTRFS_FS_CLEANER_RUNNING, TASK_UNINTERRUPTIBLE);
13639ef40c2eSJosef Bacik 
13649ef40c2eSJosef Bacik 	/*
13659ef40c2eSJosef Bacik 	 * We've set the superblock to RO mode, so we might have made the
13669ef40c2eSJosef Bacik 	 * cleaner task sleep without running all pending delayed iputs. Go
13679ef40c2eSJosef Bacik 	 * through all the delayed iputs here, so that if an unmount happens
13689ef40c2eSJosef Bacik 	 * without remounting RW we don't end up at finishing close_ctree()
13699ef40c2eSJosef Bacik 	 * with a non-empty list of delayed iputs.
13709ef40c2eSJosef Bacik 	 */
13719ef40c2eSJosef Bacik 	btrfs_run_delayed_iputs(fs_info);
13729ef40c2eSJosef Bacik 
13739ef40c2eSJosef Bacik 	btrfs_dev_replace_suspend_for_unmount(fs_info);
13749ef40c2eSJosef Bacik 	btrfs_scrub_cancel(fs_info);
13759ef40c2eSJosef Bacik 	btrfs_pause_balance(fs_info);
13769ef40c2eSJosef Bacik 
13779ef40c2eSJosef Bacik 	/*
13789ef40c2eSJosef Bacik 	 * Pause the qgroup rescan worker if it is running. We don't want it to
13799ef40c2eSJosef Bacik 	 * be still running after we are in RO mode, as after that, by the time
13809ef40c2eSJosef Bacik 	 * we unmount, it might have left a transaction open, so we would leak
13819ef40c2eSJosef Bacik 	 * the transaction and/or crash.
13829ef40c2eSJosef Bacik 	 */
13839ef40c2eSJosef Bacik 	btrfs_qgroup_wait_for_completion(fs_info, false);
13849ef40c2eSJosef Bacik 
13859ef40c2eSJosef Bacik 	return btrfs_commit_super(fs_info);
13869ef40c2eSJosef Bacik }
13879ef40c2eSJosef Bacik 
btrfs_ctx_to_info(struct btrfs_fs_info * fs_info,struct btrfs_fs_context * ctx)1388eddb1a43SJosef Bacik static void btrfs_ctx_to_info(struct btrfs_fs_info *fs_info, struct btrfs_fs_context *ctx)
1389eddb1a43SJosef Bacik {
1390eddb1a43SJosef Bacik 	fs_info->max_inline = ctx->max_inline;
1391eddb1a43SJosef Bacik 	fs_info->commit_interval = ctx->commit_interval;
1392eddb1a43SJosef Bacik 	fs_info->metadata_ratio = ctx->metadata_ratio;
1393eddb1a43SJosef Bacik 	fs_info->thread_pool_size = ctx->thread_pool_size;
1394eddb1a43SJosef Bacik 	fs_info->mount_opt = ctx->mount_opt;
1395eddb1a43SJosef Bacik 	fs_info->compress_type = ctx->compress_type;
1396eddb1a43SJosef Bacik 	fs_info->compress_level = ctx->compress_level;
1397eddb1a43SJosef Bacik }
1398eddb1a43SJosef Bacik 
btrfs_info_to_ctx(struct btrfs_fs_info * fs_info,struct btrfs_fs_context * ctx)1399eddb1a43SJosef Bacik static void btrfs_info_to_ctx(struct btrfs_fs_info *fs_info, struct btrfs_fs_context *ctx)
1400eddb1a43SJosef Bacik {
1401eddb1a43SJosef Bacik 	ctx->max_inline = fs_info->max_inline;
1402eddb1a43SJosef Bacik 	ctx->commit_interval = fs_info->commit_interval;
1403eddb1a43SJosef Bacik 	ctx->metadata_ratio = fs_info->metadata_ratio;
1404eddb1a43SJosef Bacik 	ctx->thread_pool_size = fs_info->thread_pool_size;
1405eddb1a43SJosef Bacik 	ctx->mount_opt = fs_info->mount_opt;
1406eddb1a43SJosef Bacik 	ctx->compress_type = fs_info->compress_type;
1407eddb1a43SJosef Bacik 	ctx->compress_level = fs_info->compress_level;
1408eddb1a43SJosef Bacik }
1409eddb1a43SJosef Bacik 
1410eddb1a43SJosef Bacik #define btrfs_info_if_set(fs_info, old_ctx, opt, fmt, args...)			\
1411eddb1a43SJosef Bacik do {										\
1412eddb1a43SJosef Bacik 	if ((!old_ctx || !btrfs_raw_test_opt(old_ctx->mount_opt, opt)) &&	\
1413eddb1a43SJosef Bacik 	    btrfs_raw_test_opt(fs_info->mount_opt, opt))			\
1414eddb1a43SJosef Bacik 		btrfs_info(fs_info, fmt, ##args);				\
1415eddb1a43SJosef Bacik } while (0)
1416eddb1a43SJosef Bacik 
1417eddb1a43SJosef Bacik #define btrfs_info_if_unset(fs_info, old_ctx, opt, fmt, args...)	\
1418eddb1a43SJosef Bacik do {									\
1419eddb1a43SJosef Bacik 	if ((old_ctx && btrfs_raw_test_opt(old_ctx->mount_opt, opt)) &&	\
1420eddb1a43SJosef Bacik 	    !btrfs_raw_test_opt(fs_info->mount_opt, opt))		\
1421eddb1a43SJosef Bacik 		btrfs_info(fs_info, fmt, ##args);			\
1422eddb1a43SJosef Bacik } while (0)
1423eddb1a43SJosef Bacik 
btrfs_emit_options(struct btrfs_fs_info * info,struct btrfs_fs_context * old)1424eddb1a43SJosef Bacik static void btrfs_emit_options(struct btrfs_fs_info *info,
1425eddb1a43SJosef Bacik 			       struct btrfs_fs_context *old)
1426eddb1a43SJosef Bacik {
1427eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, NODATASUM, "setting nodatasum");
1428eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, DEGRADED, "allowing degraded mounts");
1429eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, NODATASUM, "setting nodatasum");
1430eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, SSD, "enabling ssd optimizations");
1431eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, SSD_SPREAD, "using spread ssd allocation scheme");
1432eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, NOBARRIER, "turning off barriers");
1433eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, NOTREELOG, "disabling tree log");
1434eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, NOLOGREPLAY, "disabling log replay at mount time");
1435eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, FLUSHONCOMMIT, "turning on flush-on-commit");
1436eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, DISCARD_SYNC, "turning on sync discard");
1437eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, DISCARD_ASYNC, "turning on async discard");
1438eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, FREE_SPACE_TREE, "enabling free space tree");
1439eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, SPACE_CACHE, "enabling disk space caching");
1440eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, CLEAR_CACHE, "force clearing of disk cache");
1441eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, AUTO_DEFRAG, "enabling auto defrag");
1442eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, FRAGMENT_DATA, "fragmenting data");
1443eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, FRAGMENT_METADATA, "fragmenting metadata");
1444eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, REF_VERIFY, "doing ref verification");
1445eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, USEBACKUPROOT, "trying to use backup root at mount time");
1446eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, IGNOREBADROOTS, "ignoring bad roots");
1447eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, IGNOREDATACSUMS, "ignoring data csums");
1448169aaaf2SQu Wenruo 	btrfs_info_if_set(info, old, IGNOREMETACSUMS, "ignoring meta csums");
144932e62165SQu Wenruo 	btrfs_info_if_set(info, old, IGNORESUPERFLAGS, "ignoring unknown super block flags");
1450eddb1a43SJosef Bacik 
1451eddb1a43SJosef Bacik 	btrfs_info_if_unset(info, old, NODATACOW, "setting datacow");
1452eddb1a43SJosef Bacik 	btrfs_info_if_unset(info, old, SSD, "not using ssd optimizations");
1453eddb1a43SJosef Bacik 	btrfs_info_if_unset(info, old, SSD_SPREAD, "not using spread ssd allocation scheme");
1454eddb1a43SJosef Bacik 	btrfs_info_if_unset(info, old, NOBARRIER, "turning off barriers");
1455eddb1a43SJosef Bacik 	btrfs_info_if_unset(info, old, NOTREELOG, "enabling tree log");
1456eddb1a43SJosef Bacik 	btrfs_info_if_unset(info, old, SPACE_CACHE, "disabling disk space caching");
1457eddb1a43SJosef Bacik 	btrfs_info_if_unset(info, old, FREE_SPACE_TREE, "disabling free space tree");
1458eddb1a43SJosef Bacik 	btrfs_info_if_unset(info, old, AUTO_DEFRAG, "disabling auto defrag");
1459eddb1a43SJosef Bacik 	btrfs_info_if_unset(info, old, COMPRESS, "use no compression");
1460eddb1a43SJosef Bacik 
1461eddb1a43SJosef Bacik 	/* Did the compression settings change? */
1462eddb1a43SJosef Bacik 	if (btrfs_test_opt(info, COMPRESS) &&
1463eddb1a43SJosef Bacik 	    (!old ||
1464eddb1a43SJosef Bacik 	     old->compress_type != info->compress_type ||
1465eddb1a43SJosef Bacik 	     old->compress_level != info->compress_level ||
1466eddb1a43SJosef Bacik 	     (!btrfs_raw_test_opt(old->mount_opt, FORCE_COMPRESS) &&
1467eddb1a43SJosef Bacik 	      btrfs_raw_test_opt(info->mount_opt, FORCE_COMPRESS)))) {
1468eddb1a43SJosef Bacik 		const char *compress_type = btrfs_compress_type2str(info->compress_type);
1469eddb1a43SJosef Bacik 
1470eddb1a43SJosef Bacik 		btrfs_info(info, "%s %s compression, level %d",
1471eddb1a43SJosef Bacik 			   btrfs_test_opt(info, FORCE_COMPRESS) ? "force" : "use",
1472eddb1a43SJosef Bacik 			   compress_type, info->compress_level);
1473eddb1a43SJosef Bacik 	}
1474eddb1a43SJosef Bacik 
1475eddb1a43SJosef Bacik 	if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
1476eddb1a43SJosef Bacik 		btrfs_info(info, "max_inline set to %llu", info->max_inline);
1477eddb1a43SJosef Bacik }
1478eddb1a43SJosef Bacik 
btrfs_reconfigure(struct fs_context * fc)1479eddb1a43SJosef Bacik static int btrfs_reconfigure(struct fs_context *fc)
1480eddb1a43SJosef Bacik {
1481eddb1a43SJosef Bacik 	struct super_block *sb = fc->root->d_sb;
1482eddb1a43SJosef Bacik 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1483eddb1a43SJosef Bacik 	struct btrfs_fs_context *ctx = fc->fs_private;
1484eddb1a43SJosef Bacik 	struct btrfs_fs_context old_ctx;
1485eddb1a43SJosef Bacik 	int ret = 0;
1486f044b318SJosef Bacik 	bool mount_reconfigure = (fc->s_fs_info != NULL);
1487eddb1a43SJosef Bacik 
1488eddb1a43SJosef Bacik 	btrfs_info_to_ctx(fs_info, &old_ctx);
1489eddb1a43SJosef Bacik 
14902018ef1dSJosef Bacik 	/*
14912018ef1dSJosef Bacik 	 * This is our "bind mount" trick, we don't want to allow the user to do
14922018ef1dSJosef Bacik 	 * anything other than mount a different ro/rw and a different subvol,
14932018ef1dSJosef Bacik 	 * all of the mount options should be maintained.
14942018ef1dSJosef Bacik 	 */
14952018ef1dSJosef Bacik 	if (mount_reconfigure)
14962018ef1dSJosef Bacik 		ctx->mount_opt = old_ctx.mount_opt;
14972018ef1dSJosef Bacik 
149802b9984dSTheodore Ts'o 	sync_filesystem(sb);
149988c4703fSJohannes Thumshirn 	set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1500dc81cdc5SMiao Xie 
1501f044b318SJosef Bacik 	if (!mount_reconfigure &&
1502ad21f15bSJosef Bacik 	    !btrfs_check_options(fs_info, &ctx->mount_opt, fc->sb_flags))
1503eddb1a43SJosef Bacik 		return -EINVAL;
1504f667aef6SQu Wenruo 
1505eddb1a43SJosef Bacik 	ret = btrfs_check_features(fs_info, !(fc->sb_flags & SB_RDONLY));
1506d7f67ac9SQu Wenruo 	if (ret < 0)
1507eddb1a43SJosef Bacik 		return ret;
1508d7f67ac9SQu Wenruo 
1509eddb1a43SJosef Bacik 	btrfs_ctx_to_info(fs_info, ctx);
1510eddb1a43SJosef Bacik 	btrfs_remount_begin(fs_info, old_ctx.mount_opt, fc->sb_flags);
1511eddb1a43SJosef Bacik 	btrfs_resize_thread_pool(fs_info, fs_info->thread_pool_size,
1512eddb1a43SJosef Bacik 				 old_ctx.thread_pool_size);
15130d2450abSSergei Trofimovich 
1514c55a4319SBoris Burkov 	if ((bool)btrfs_test_opt(fs_info, FREE_SPACE_TREE) !=
1515c55a4319SBoris Burkov 	    (bool)btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
1516eddb1a43SJosef Bacik 	    (!sb_rdonly(sb) || (fc->sb_flags & SB_RDONLY))) {
15172838d255SBoris Burkov 		btrfs_warn(fs_info,
1518eddb1a43SJosef Bacik 		"remount supports changing free space tree only from RO to RW");
1519eddb1a43SJosef Bacik 		/* Make sure free space cache options match the state on disk. */
15202838d255SBoris Burkov 		if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
15212838d255SBoris Burkov 			btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
15222838d255SBoris Burkov 			btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
15232838d255SBoris Burkov 		}
15242838d255SBoris Burkov 		if (btrfs_free_space_cache_v1_active(fs_info)) {
15252838d255SBoris Burkov 			btrfs_clear_opt(fs_info->mount_opt, FREE_SPACE_TREE);
15262838d255SBoris Burkov 			btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE);
15272838d255SBoris Burkov 		}
15282838d255SBoris Burkov 	}
15292838d255SBoris Burkov 
1530eddb1a43SJosef Bacik 	ret = 0;
1531eddb1a43SJosef Bacik 	if (!sb_rdonly(sb) && (fc->sb_flags & SB_RDONLY))
1532eddb1a43SJosef Bacik 		ret = btrfs_remount_ro(fs_info);
1533eddb1a43SJosef Bacik 	else if (sb_rdonly(sb) && !(fc->sb_flags & SB_RDONLY))
1534eddb1a43SJosef Bacik 		ret = btrfs_remount_rw(fs_info);
153549b25e05SJeff Mahoney 	if (ret)
153649b25e05SJeff Mahoney 		goto restore;
1537c146afadSYan Zheng 
1538981a37baSChris Mason 	/*
1539eddb1a43SJosef Bacik 	 * If we set the mask during the parameter parsing VFS would reject the
1540eddb1a43SJosef Bacik 	 * remount.  Here we can set the mask and the value will be updated
1541eddb1a43SJosef Bacik 	 * appropriately.
1542981a37baSChris Mason 	 */
1543eddb1a43SJosef Bacik 	if ((fc->sb_flags & SB_POSIXACL) != (sb->s_flags & SB_POSIXACL))
1544eddb1a43SJosef Bacik 		fc->sb_flags_mask |= SB_POSIXACL;
1545faa00889SJosef Bacik 
1546eddb1a43SJosef Bacik 	btrfs_emit_options(fs_info, &old_ctx);
15472c6a92b0SJustin Maggard 	wake_up_process(fs_info->transaction_kthread);
1548eddb1a43SJosef Bacik 	btrfs_remount_cleanup(fs_info, old_ctx.mount_opt);
15498cd29088SBoris Burkov 	btrfs_clear_oneshot_options(fs_info);
155088c4703fSJohannes Thumshirn 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
155188c4703fSJohannes Thumshirn 
1552c146afadSYan Zheng 	return 0;
155349b25e05SJeff Mahoney restore:
1554eddb1a43SJosef Bacik 	btrfs_ctx_to_info(fs_info, &old_ctx);
1555eddb1a43SJosef Bacik 	btrfs_remount_cleanup(fs_info, old_ctx.mount_opt);
155688c4703fSJohannes Thumshirn 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
155749b25e05SJeff Mahoney 	return ret;
1558c146afadSYan Zheng }
1559c146afadSYan Zheng 
1560bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */
btrfs_cmp_device_free_bytes(const void * a,const void * b)1561214cc184SDavid Sterba static int btrfs_cmp_device_free_bytes(const void *a, const void *b)
1562bcd53741SArne Jansen {
1563214cc184SDavid Sterba 	const struct btrfs_device_info *dev_info1 = a;
1564214cc184SDavid Sterba 	const struct btrfs_device_info *dev_info2 = b;
1565214cc184SDavid Sterba 
1566214cc184SDavid Sterba 	if (dev_info1->max_avail > dev_info2->max_avail)
1567bcd53741SArne Jansen 		return -1;
1568214cc184SDavid Sterba 	else if (dev_info1->max_avail < dev_info2->max_avail)
1569bcd53741SArne Jansen 		return 1;
1570bcd53741SArne Jansen 	return 0;
1571bcd53741SArne Jansen }
1572bcd53741SArne Jansen 
1573bcd53741SArne Jansen /*
1574bcd53741SArne Jansen  * sort the devices by max_avail, in which max free extent size of each device
1575bcd53741SArne Jansen  * is stored.(Descending Sort)
1576bcd53741SArne Jansen  */
btrfs_descending_sort_devices(struct btrfs_device_info * devices,size_t nr_devices)1577bcd53741SArne Jansen static inline void btrfs_descending_sort_devices(
1578bcd53741SArne Jansen 					struct btrfs_device_info *devices,
1579bcd53741SArne Jansen 					size_t nr_devices)
1580bcd53741SArne Jansen {
1581bcd53741SArne Jansen 	sort(devices, nr_devices, sizeof(struct btrfs_device_info),
1582bcd53741SArne Jansen 	     btrfs_cmp_device_free_bytes, NULL);
1583bcd53741SArne Jansen }
1584bcd53741SArne Jansen 
15856d07bcecSMiao Xie /*
15866d07bcecSMiao Xie  * The helper to calc the free space on the devices that can be used to store
15876d07bcecSMiao Xie  * file data.
15886d07bcecSMiao Xie  */
btrfs_calc_avail_data_space(struct btrfs_fs_info * fs_info,u64 * free_bytes)15897e17916bSArnd Bergmann static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
15906bccf3abSJeff Mahoney 					      u64 *free_bytes)
15916d07bcecSMiao Xie {
15926d07bcecSMiao Xie 	struct btrfs_device_info *devices_info;
15936d07bcecSMiao Xie 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
15946d07bcecSMiao Xie 	struct btrfs_device *device;
15956d07bcecSMiao Xie 	u64 type;
15966d07bcecSMiao Xie 	u64 avail_space;
15976d07bcecSMiao Xie 	u64 min_stripe_size;
1598559ca6eaSNikolay Borisov 	int num_stripes = 1;
15996d07bcecSMiao Xie 	int i = 0, nr_devices;
16004f080f57SDavid Sterba 	const struct btrfs_raid_attr *rattr;
16016d07bcecSMiao Xie 
16027e33fd99SJosef Bacik 	/*
160301327610SNicholas D Steeves 	 * We aren't under the device list lock, so this is racy-ish, but good
16047e33fd99SJosef Bacik 	 * enough for our purposes.
16057e33fd99SJosef Bacik 	 */
1606b772a86eSLi Zefan 	nr_devices = fs_info->fs_devices->open_devices;
16077e33fd99SJosef Bacik 	if (!nr_devices) {
16087e33fd99SJosef Bacik 		smp_mb();
16097e33fd99SJosef Bacik 		nr_devices = fs_info->fs_devices->open_devices;
16107e33fd99SJosef Bacik 		ASSERT(nr_devices);
16117e33fd99SJosef Bacik 		if (!nr_devices) {
16127e33fd99SJosef Bacik 			*free_bytes = 0;
16137e33fd99SJosef Bacik 			return 0;
16147e33fd99SJosef Bacik 		}
16157e33fd99SJosef Bacik 	}
16166d07bcecSMiao Xie 
1617d9b0d9baSDulshani Gunawardhana 	devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
16186a44517dSDavid Sterba 			       GFP_KERNEL);
16196d07bcecSMiao Xie 	if (!devices_info)
16206d07bcecSMiao Xie 		return -ENOMEM;
16216d07bcecSMiao Xie 
162201327610SNicholas D Steeves 	/* calc min stripe number for data space allocation */
16231b86826dSJeff Mahoney 	type = btrfs_data_alloc_profile(fs_info);
16244f080f57SDavid Sterba 	rattr = &btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)];
16254f080f57SDavid Sterba 
1626e1ea2beeSDavid Sterba 	if (type & BTRFS_BLOCK_GROUP_RAID0)
162739fb26c3SMiao Xie 		num_stripes = nr_devices;
1628d09cb9e1SDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID1_MASK)
1629d09cb9e1SDavid Sterba 		num_stripes = rattr->ncopies;
1630e1ea2beeSDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID10)
163139fb26c3SMiao Xie 		num_stripes = 4;
16326d07bcecSMiao Xie 
16334f080f57SDavid Sterba 	/* Adjust for more than 1 stripe per device */
16344f080f57SDavid Sterba 	min_stripe_size = rattr->dev_stripes * BTRFS_STRIPE_LEN;
16356d07bcecSMiao Xie 
16367e33fd99SJosef Bacik 	rcu_read_lock();
16377e33fd99SJosef Bacik 	list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
1638e12c9621SAnand Jain 		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
1639e12c9621SAnand Jain 						&device->dev_state) ||
1640401e29c1SAnand Jain 		    !device->bdev ||
1641401e29c1SAnand Jain 		    test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
16426d07bcecSMiao Xie 			continue;
16436d07bcecSMiao Xie 
16447e33fd99SJosef Bacik 		if (i >= nr_devices)
16457e33fd99SJosef Bacik 			break;
16467e33fd99SJosef Bacik 
16476d07bcecSMiao Xie 		avail_space = device->total_bytes - device->bytes_used;
16486d07bcecSMiao Xie 
16496d07bcecSMiao Xie 		/* align with stripe_len */
1650559ca6eaSNikolay Borisov 		avail_space = rounddown(avail_space, BTRFS_STRIPE_LEN);
16516d07bcecSMiao Xie 
16526d07bcecSMiao Xie 		/*
165337f85ec3SQu Wenruo 		 * Ensure we have at least min_stripe_size on top of the
165437f85ec3SQu Wenruo 		 * reserved space on the device.
16556d07bcecSMiao Xie 		 */
165637f85ec3SQu Wenruo 		if (avail_space <= BTRFS_DEVICE_RANGE_RESERVED + min_stripe_size)
16576d07bcecSMiao Xie 			continue;
16586d07bcecSMiao Xie 
165937f85ec3SQu Wenruo 		avail_space -= BTRFS_DEVICE_RANGE_RESERVED;
1660559ca6eaSNikolay Borisov 
16616d07bcecSMiao Xie 		devices_info[i].dev = device;
16626d07bcecSMiao Xie 		devices_info[i].max_avail = avail_space;
16636d07bcecSMiao Xie 
16646d07bcecSMiao Xie 		i++;
16656d07bcecSMiao Xie 	}
16667e33fd99SJosef Bacik 	rcu_read_unlock();
16676d07bcecSMiao Xie 
16686d07bcecSMiao Xie 	nr_devices = i;
16696d07bcecSMiao Xie 
16706d07bcecSMiao Xie 	btrfs_descending_sort_devices(devices_info, nr_devices);
16716d07bcecSMiao Xie 
16726d07bcecSMiao Xie 	i = nr_devices - 1;
16736d07bcecSMiao Xie 	avail_space = 0;
1674559ca6eaSNikolay Borisov 	while (nr_devices >= rattr->devs_min) {
1675559ca6eaSNikolay Borisov 		num_stripes = min(num_stripes, nr_devices);
167639fb26c3SMiao Xie 
16776d07bcecSMiao Xie 		if (devices_info[i].max_avail >= min_stripe_size) {
16786d07bcecSMiao Xie 			int j;
16796d07bcecSMiao Xie 			u64 alloc_size;
16806d07bcecSMiao Xie 
168139fb26c3SMiao Xie 			avail_space += devices_info[i].max_avail * num_stripes;
16826d07bcecSMiao Xie 			alloc_size = devices_info[i].max_avail;
168339fb26c3SMiao Xie 			for (j = i + 1 - num_stripes; j <= i; j++)
16846d07bcecSMiao Xie 				devices_info[j].max_avail -= alloc_size;
16856d07bcecSMiao Xie 		}
16866d07bcecSMiao Xie 		i--;
16876d07bcecSMiao Xie 		nr_devices--;
16886d07bcecSMiao Xie 	}
16896d07bcecSMiao Xie 
16906d07bcecSMiao Xie 	kfree(devices_info);
16916d07bcecSMiao Xie 	*free_bytes = avail_space;
16926d07bcecSMiao Xie 	return 0;
16936d07bcecSMiao Xie }
16946d07bcecSMiao Xie 
1695ba7b6e62SDavid Sterba /*
1696ba7b6e62SDavid Sterba  * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
1697ba7b6e62SDavid Sterba  *
1698ba7b6e62SDavid Sterba  * If there's a redundant raid level at DATA block groups, use the respective
1699ba7b6e62SDavid Sterba  * multiplier to scale the sizes.
1700ba7b6e62SDavid Sterba  *
1701ba7b6e62SDavid Sterba  * Unused device space usage is based on simulating the chunk allocator
17020d0c71b3SDavid Sterba  * algorithm that respects the device sizes and order of allocations.  This is
17030d0c71b3SDavid Sterba  * a close approximation of the actual use but there are other factors that may
17040d0c71b3SDavid Sterba  * change the result (like a new metadata chunk).
1705ba7b6e62SDavid Sterba  *
1706ca8a51b3SDavid Sterba  * If metadata is exhausted, f_bavail will be 0.
1707ba7b6e62SDavid Sterba  */
btrfs_statfs(struct dentry * dentry,struct kstatfs * buf)17088fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
17098fd17795SChris Mason {
1710815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
1711815745cfSAl Viro 	struct btrfs_super_block *disk_super = fs_info->super_copy;
1712bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
1713bd4d1088SJosef Bacik 	u64 total_used = 0;
17146d07bcecSMiao Xie 	u64 total_free_data = 0;
1715ca8a51b3SDavid Sterba 	u64 total_free_meta = 0;
1716265fdfa6SDavid Sterba 	u32 bits = fs_info->sectorsize_bits;
1717de37aa51SNikolay Borisov 	__be32 *fsid = (__be32 *)fs_info->fs_devices->fsid;
1718ba7b6e62SDavid Sterba 	unsigned factor = 1;
1719ba7b6e62SDavid Sterba 	struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
17206d07bcecSMiao Xie 	int ret;
1721ca8a51b3SDavid Sterba 	u64 thresh = 0;
1722ae02d1bdSLuis de Bethencourt 	int mixed = 0;
17238fd17795SChris Mason 
172472804905SJosef Bacik 	list_for_each_entry(found, &fs_info->space_info, list) {
17256d07bcecSMiao Xie 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
1726ba7b6e62SDavid Sterba 			int i;
1727ba7b6e62SDavid Sterba 
17286d07bcecSMiao Xie 			total_free_data += found->disk_total - found->disk_used;
17296d07bcecSMiao Xie 			total_free_data -=
17306d07bcecSMiao Xie 				btrfs_account_ro_block_groups_free_space(found);
1731ba7b6e62SDavid Sterba 
1732ba7b6e62SDavid Sterba 			for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
173346df06b8SDavid Sterba 				if (!list_empty(&found->block_groups[i]))
173446df06b8SDavid Sterba 					factor = btrfs_bg_type_to_factor(
173546df06b8SDavid Sterba 						btrfs_raid_array[i].bg_flag);
1736ba7b6e62SDavid Sterba 			}
17376d07bcecSMiao Xie 		}
1738ae02d1bdSLuis de Bethencourt 
1739ae02d1bdSLuis de Bethencourt 		/*
174067da05b3SColin Ian King 		 * Metadata in mixed block group profiles are accounted in data
1741ae02d1bdSLuis de Bethencourt 		 */
1742ae02d1bdSLuis de Bethencourt 		if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) {
1743ae02d1bdSLuis de Bethencourt 			if (found->flags & BTRFS_BLOCK_GROUP_DATA)
1744ae02d1bdSLuis de Bethencourt 				mixed = 1;
1745ae02d1bdSLuis de Bethencourt 			else
1746ae02d1bdSLuis de Bethencourt 				total_free_meta += found->disk_total -
1747ae02d1bdSLuis de Bethencourt 					found->disk_used;
1748ae02d1bdSLuis de Bethencourt 		}
17496d07bcecSMiao Xie 
1750b742bb82SYan, Zheng 		total_used += found->disk_used;
175189a55897SJosef Bacik 	}
1752ba7b6e62SDavid Sterba 
1753ba7b6e62SDavid Sterba 	buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
1754ba7b6e62SDavid Sterba 	buf->f_blocks >>= bits;
1755ba7b6e62SDavid Sterba 	buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
1756ba7b6e62SDavid Sterba 
1757ba7b6e62SDavid Sterba 	/* Account global block reserve as used, it's in logical size already */
1758ba7b6e62SDavid Sterba 	spin_lock(&block_rsv->lock);
175941b34accSLuis de Bethencourt 	/* Mixed block groups accounting is not byte-accurate, avoid overflow */
176041b34accSLuis de Bethencourt 	if (buf->f_bfree >= block_rsv->size >> bits)
1761ba7b6e62SDavid Sterba 		buf->f_bfree -= block_rsv->size >> bits;
176241b34accSLuis de Bethencourt 	else
176341b34accSLuis de Bethencourt 		buf->f_bfree = 0;
1764ba7b6e62SDavid Sterba 	spin_unlock(&block_rsv->lock);
1765ba7b6e62SDavid Sterba 
17660d95c1beSDavid Sterba 	buf->f_bavail = div_u64(total_free_data, factor);
17676bccf3abSJeff Mahoney 	ret = btrfs_calc_avail_data_space(fs_info, &total_free_data);
17687e33fd99SJosef Bacik 	if (ret)
17696d07bcecSMiao Xie 		return ret;
1770ba7b6e62SDavid Sterba 	buf->f_bavail += div_u64(total_free_data, factor);
17716d07bcecSMiao Xie 	buf->f_bavail = buf->f_bavail >> bits;
1772d397712bSChris Mason 
1773ca8a51b3SDavid Sterba 	/*
1774ca8a51b3SDavid Sterba 	 * We calculate the remaining metadata space minus global reserve. If
1775ca8a51b3SDavid Sterba 	 * this is (supposedly) smaller than zero, there's no space. But this
1776ca8a51b3SDavid Sterba 	 * does not hold in practice, the exhausted state happens where's still
1777ca8a51b3SDavid Sterba 	 * some positive delta. So we apply some guesswork and compare the
1778ca8a51b3SDavid Sterba 	 * delta to a 4M threshold.  (Practically observed delta was ~2M.)
1779ca8a51b3SDavid Sterba 	 *
1780ca8a51b3SDavid Sterba 	 * We probably cannot calculate the exact threshold value because this
1781ca8a51b3SDavid Sterba 	 * depends on the internal reservations requested by various
1782ca8a51b3SDavid Sterba 	 * operations, so some operations that consume a few metadata will
1783ca8a51b3SDavid Sterba 	 * succeed even if the Avail is zero. But this is better than the other
1784ca8a51b3SDavid Sterba 	 * way around.
1785ca8a51b3SDavid Sterba 	 */
1786d4417e22SNikolay Borisov 	thresh = SZ_4M;
1787ca8a51b3SDavid Sterba 
1788d55966c4SJosef Bacik 	/*
1789d55966c4SJosef Bacik 	 * We only want to claim there's no available space if we can no longer
1790d55966c4SJosef Bacik 	 * allocate chunks for our metadata profile and our global reserve will
1791d55966c4SJosef Bacik 	 * not fit in the free metadata space.  If we aren't ->full then we
1792d55966c4SJosef Bacik 	 * still can allocate chunks and thus are fine using the currently
1793d55966c4SJosef Bacik 	 * calculated f_bavail.
1794d55966c4SJosef Bacik 	 */
1795d55966c4SJosef Bacik 	if (!mixed && block_rsv->space_info->full &&
179658bfe2ccSJosef Bacik 	    (total_free_meta < thresh || total_free_meta - thresh < block_rsv->size))
1797ca8a51b3SDavid Sterba 		buf->f_bavail = 0;
1798ca8a51b3SDavid Sterba 
1799ba7b6e62SDavid Sterba 	buf->f_type = BTRFS_SUPER_MAGIC;
18004e00422eSDavid Sterba 	buf->f_bsize = fs_info->sectorsize;
1801ba7b6e62SDavid Sterba 	buf->f_namelen = BTRFS_NAME_LEN;
1802ba7b6e62SDavid Sterba 
18039d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
18049d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
18059d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
18069d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
18079d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
180832d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
1809e094f480SJosef Bacik 	buf->f_fsid.val[0] ^= btrfs_root_id(BTRFS_I(d_inode(dentry))->root) >> 32;
1810e094f480SJosef Bacik 	buf->f_fsid.val[1] ^= btrfs_root_id(BTRFS_I(d_inode(dentry))->root);
181132d48fa1SDavid Woodhouse 
18128fd17795SChris Mason 	return 0;
18138fd17795SChris Mason }
1814b5133862SChris Mason 
btrfs_fc_test_super(struct super_block * sb,struct fs_context * fc)18153bb17a25SJosef Bacik static int btrfs_fc_test_super(struct super_block *sb, struct fs_context *fc)
18163bb17a25SJosef Bacik {
18173bb17a25SJosef Bacik 	struct btrfs_fs_info *p = fc->s_fs_info;
18183bb17a25SJosef Bacik 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
18193bb17a25SJosef Bacik 
18203bb17a25SJosef Bacik 	return fs_info->fs_devices == p->fs_devices;
18213bb17a25SJosef Bacik }
18223bb17a25SJosef Bacik 
btrfs_get_tree_super(struct fs_context * fc)18233bb17a25SJosef Bacik static int btrfs_get_tree_super(struct fs_context *fc)
18243bb17a25SJosef Bacik {
18253bb17a25SJosef Bacik 	struct btrfs_fs_info *fs_info = fc->s_fs_info;
18263bb17a25SJosef Bacik 	struct btrfs_fs_context *ctx = fc->fs_private;
18273bb17a25SJosef Bacik 	struct btrfs_fs_devices *fs_devices = NULL;
18283bb17a25SJosef Bacik 	struct block_device *bdev;
18293bb17a25SJosef Bacik 	struct btrfs_device *device;
18303bb17a25SJosef Bacik 	struct super_block *sb;
1831affc5af3SLinus Torvalds 	blk_mode_t mode = btrfs_open_mode(fc);
18323bb17a25SJosef Bacik 	int ret;
18333bb17a25SJosef Bacik 
18343bb17a25SJosef Bacik 	btrfs_ctx_to_info(fs_info, ctx);
18353bb17a25SJosef Bacik 	mutex_lock(&uuid_mutex);
18363bb17a25SJosef Bacik 
18373bb17a25SJosef Bacik 	/*
18383bb17a25SJosef Bacik 	 * With 'true' passed to btrfs_scan_one_device() (mount time) we expect
18393bb17a25SJosef Bacik 	 * either a valid device or an error.
18403bb17a25SJosef Bacik 	 */
18413bb17a25SJosef Bacik 	device = btrfs_scan_one_device(fc->source, mode, true);
18423bb17a25SJosef Bacik 	ASSERT(device != NULL);
18433bb17a25SJosef Bacik 	if (IS_ERR(device)) {
18443bb17a25SJosef Bacik 		mutex_unlock(&uuid_mutex);
18453bb17a25SJosef Bacik 		return PTR_ERR(device);
18463bb17a25SJosef Bacik 	}
18473bb17a25SJosef Bacik 
18483bb17a25SJosef Bacik 	fs_devices = device->fs_devices;
18493bb17a25SJosef Bacik 	fs_info->fs_devices = fs_devices;
18503bb17a25SJosef Bacik 
18513bb17a25SJosef Bacik 	ret = btrfs_open_devices(fs_devices, mode, &btrfs_fs_type);
18523bb17a25SJosef Bacik 	mutex_unlock(&uuid_mutex);
18533bb17a25SJosef Bacik 	if (ret)
18543bb17a25SJosef Bacik 		return ret;
18553bb17a25SJosef Bacik 
18563bb17a25SJosef Bacik 	if (!(fc->sb_flags & SB_RDONLY) && fs_devices->rw_devices == 0) {
18573bb17a25SJosef Bacik 		ret = -EACCES;
18583bb17a25SJosef Bacik 		goto error;
18593bb17a25SJosef Bacik 	}
18603bb17a25SJosef Bacik 
18613bb17a25SJosef Bacik 	bdev = fs_devices->latest_dev->bdev;
18623bb17a25SJosef Bacik 
18633bb17a25SJosef Bacik 	/*
18643bb17a25SJosef Bacik 	 * From now on the error handling is not straightforward.
18653bb17a25SJosef Bacik 	 *
18663bb17a25SJosef Bacik 	 * If successful, this will transfer the fs_info into the super block,
18673bb17a25SJosef Bacik 	 * and fc->s_fs_info will be NULL.  However if there's an existing
18683bb17a25SJosef Bacik 	 * super, we'll still have fc->s_fs_info populated.  If we error
18693bb17a25SJosef Bacik 	 * completely out it'll be cleaned up when we drop the fs_context,
18703bb17a25SJosef Bacik 	 * otherwise it's tied to the lifetime of the super_block.
18713bb17a25SJosef Bacik 	 */
18723bb17a25SJosef Bacik 	sb = sget_fc(fc, btrfs_fc_test_super, set_anon_super_fc);
18733bb17a25SJosef Bacik 	if (IS_ERR(sb)) {
18743bb17a25SJosef Bacik 		ret = PTR_ERR(sb);
18753bb17a25SJosef Bacik 		goto error;
18763bb17a25SJosef Bacik 	}
18773bb17a25SJosef Bacik 
187841d46b29SJosef Bacik 	set_device_specific_options(fs_info);
187941d46b29SJosef Bacik 
18803bb17a25SJosef Bacik 	if (sb->s_root) {
18813bb17a25SJosef Bacik 		btrfs_close_devices(fs_devices);
18823bb17a25SJosef Bacik 		if ((fc->sb_flags ^ sb->s_flags) & SB_RDONLY)
18833bb17a25SJosef Bacik 			ret = -EBUSY;
18843bb17a25SJosef Bacik 	} else {
18853bb17a25SJosef Bacik 		snprintf(sb->s_id, sizeof(sb->s_id), "%pg", bdev);
18863bb17a25SJosef Bacik 		shrinker_debugfs_rename(sb->s_shrink, "sb-btrfs:%s", sb->s_id);
18873bb17a25SJosef Bacik 		btrfs_sb(sb)->bdev_holder = &btrfs_fs_type;
18883bb17a25SJosef Bacik 		ret = btrfs_fill_super(sb, fs_devices, NULL);
18893bb17a25SJosef Bacik 	}
18903bb17a25SJosef Bacik 
18913bb17a25SJosef Bacik 	if (ret) {
18923bb17a25SJosef Bacik 		deactivate_locked_super(sb);
18933bb17a25SJosef Bacik 		return ret;
18943bb17a25SJosef Bacik 	}
18953bb17a25SJosef Bacik 
189683e3a40aSJosef Bacik 	btrfs_clear_oneshot_options(fs_info);
189783e3a40aSJosef Bacik 
18983bb17a25SJosef Bacik 	fc->root = dget(sb->s_root);
18993bb17a25SJosef Bacik 	return 0;
19003bb17a25SJosef Bacik 
19013bb17a25SJosef Bacik error:
19023bb17a25SJosef Bacik 	btrfs_close_devices(fs_devices);
19033bb17a25SJosef Bacik 	return ret;
19043bb17a25SJosef Bacik }
19053bb17a25SJosef Bacik 
1906f044b318SJosef Bacik /*
1907f044b318SJosef Bacik  * Ever since commit 0723a0473fb4 ("btrfs: allow mounting btrfs subvolumes
1908f044b318SJosef Bacik  * with different ro/rw options") the following works:
1909f044b318SJosef Bacik  *
1910f044b318SJosef Bacik  *        (i) mount /dev/sda3 -o subvol=foo,ro /mnt/foo
1911f044b318SJosef Bacik  *       (ii) mount /dev/sda3 -o subvol=bar,rw /mnt/bar
1912f044b318SJosef Bacik  *
1913f044b318SJosef Bacik  * which looks nice and innocent but is actually pretty intricate and deserves
1914f044b318SJosef Bacik  * a long comment.
1915f044b318SJosef Bacik  *
1916f044b318SJosef Bacik  * On another filesystem a subvolume mount is close to something like:
1917f044b318SJosef Bacik  *
1918f044b318SJosef Bacik  *	(iii) # create rw superblock + initial mount
1919f044b318SJosef Bacik  *	      mount -t xfs /dev/sdb /opt/
1920f044b318SJosef Bacik  *
1921f044b318SJosef Bacik  *	      # create ro bind mount
1922f044b318SJosef Bacik  *	      mount --bind -o ro /opt/foo /mnt/foo
1923f044b318SJosef Bacik  *
1924f044b318SJosef Bacik  *	      # unmount initial mount
1925f044b318SJosef Bacik  *	      umount /opt
1926f044b318SJosef Bacik  *
1927f044b318SJosef Bacik  * Of course, there's some special subvolume sauce and there's the fact that the
1928f044b318SJosef Bacik  * sb->s_root dentry is really swapped after mount_subtree(). But conceptually
1929f044b318SJosef Bacik  * it's very close and will help us understand the issue.
1930f044b318SJosef Bacik  *
1931f044b318SJosef Bacik  * The old mount API didn't cleanly distinguish between a mount being made ro
1932f044b318SJosef Bacik  * and a superblock being made ro.  The only way to change the ro state of
1933f044b318SJosef Bacik  * either object was by passing ms_rdonly. If a new mount was created via
1934f044b318SJosef Bacik  * mount(2) such as:
1935f044b318SJosef Bacik  *
1936f044b318SJosef Bacik  *      mount("/dev/sdb", "/mnt", "xfs", ms_rdonly, null);
1937f044b318SJosef Bacik  *
1938f044b318SJosef Bacik  * the MS_RDONLY flag being specified had two effects:
1939f044b318SJosef Bacik  *
1940f044b318SJosef Bacik  * (1) MNT_READONLY was raised -> the resulting mount got
1941f044b318SJosef Bacik  *     @mnt->mnt_flags |= MNT_READONLY raised.
1942f044b318SJosef Bacik  *
1943f044b318SJosef Bacik  * (2) MS_RDONLY was passed to the filesystem's mount method and the filesystems
1944f044b318SJosef Bacik  *     made the superblock ro. Note, how SB_RDONLY has the same value as
1945f044b318SJosef Bacik  *     ms_rdonly and is raised whenever MS_RDONLY is passed through mount(2).
1946f044b318SJosef Bacik  *
1947f044b318SJosef Bacik  * Creating a subtree mount via (iii) ends up leaving a rw superblock with a
1948f044b318SJosef Bacik  * subtree mounted ro.
1949f044b318SJosef Bacik  *
1950f044b318SJosef Bacik  * But consider the effect on the old mount API on btrfs subvolume mounting
1951f044b318SJosef Bacik  * which combines the distinct step in (iii) into a single step.
1952f044b318SJosef Bacik  *
1953f044b318SJosef Bacik  * By issuing (i) both the mount and the superblock are turned ro. Now when (ii)
1954f044b318SJosef Bacik  * is issued the superblock is ro and thus even if the mount created for (ii) is
1955f044b318SJosef Bacik  * rw it wouldn't help. Hence, btrfs needed to transition the superblock from ro
1956f044b318SJosef Bacik  * to rw for (ii) which it did using an internal remount call.
1957f044b318SJosef Bacik  *
1958f044b318SJosef Bacik  * IOW, subvolume mounting was inherently complicated due to the ambiguity of
1959f044b318SJosef Bacik  * MS_RDONLY in mount(2). Note, this ambiguity has mount(8) always translate
1960f044b318SJosef Bacik  * "ro" to MS_RDONLY. IOW, in both (i) and (ii) "ro" becomes MS_RDONLY when
1961f044b318SJosef Bacik  * passed by mount(8) to mount(2).
1962f044b318SJosef Bacik  *
1963f044b318SJosef Bacik  * Enter the new mount API. The new mount API disambiguates making a mount ro
1964f044b318SJosef Bacik  * and making a superblock ro.
1965f044b318SJosef Bacik  *
1966f044b318SJosef Bacik  * (3) To turn a mount ro the MOUNT_ATTR_ONLY flag can be used with either
1967f044b318SJosef Bacik  *     fsmount() or mount_setattr() this is a pure VFS level change for a
1968f044b318SJosef Bacik  *     specific mount or mount tree that is never seen by the filesystem itself.
1969f044b318SJosef Bacik  *
1970f044b318SJosef Bacik  * (4) To turn a superblock ro the "ro" flag must be used with
1971f044b318SJosef Bacik  *     fsconfig(FSCONFIG_SET_FLAG, "ro"). This option is seen by the filesystem
1972f044b318SJosef Bacik  *     in fc->sb_flags.
1973f044b318SJosef Bacik  *
1974f044b318SJosef Bacik  * This disambiguation has rather positive consequences.  Mounting a subvolume
1975f044b318SJosef Bacik  * ro will not also turn the superblock ro. Only the mount for the subvolume
1976f044b318SJosef Bacik  * will become ro.
1977f044b318SJosef Bacik  *
1978f044b318SJosef Bacik  * So, if the superblock creation request comes from the new mount API the
1979f044b318SJosef Bacik  * caller must have explicitly done:
1980f044b318SJosef Bacik  *
1981f044b318SJosef Bacik  *      fsconfig(FSCONFIG_SET_FLAG, "ro")
1982f044b318SJosef Bacik  *      fsmount/mount_setattr(MOUNT_ATTR_RDONLY)
1983f044b318SJosef Bacik  *
1984f044b318SJosef Bacik  * IOW, at some point the caller must have explicitly turned the whole
1985f044b318SJosef Bacik  * superblock ro and we shouldn't just undo it like we did for the old mount
1986f044b318SJosef Bacik  * API. In any case, it lets us avoid the hack in the new mount API.
1987f044b318SJosef Bacik  *
1988f044b318SJosef Bacik  * Consequently, the remounting hack must only be used for requests originating
1989f044b318SJosef Bacik  * from the old mount API and should be marked for full deprecation so it can be
1990f044b318SJosef Bacik  * turned off in a couple of years.
1991f044b318SJosef Bacik  *
1992f044b318SJosef Bacik  * The new mount API has no reason to support this hack.
1993f044b318SJosef Bacik  */
btrfs_reconfigure_for_mount(struct fs_context * fc)1994f044b318SJosef Bacik static struct vfsmount *btrfs_reconfigure_for_mount(struct fs_context *fc)
1995f044b318SJosef Bacik {
1996f044b318SJosef Bacik 	struct vfsmount *mnt;
1997f044b318SJosef Bacik 	int ret;
1998f044b318SJosef Bacik 	const bool ro2rw = !(fc->sb_flags & SB_RDONLY);
1999f044b318SJosef Bacik 
2000f044b318SJosef Bacik 	/*
2001f044b318SJosef Bacik 	 * We got an EBUSY because our SB_RDONLY flag didn't match the existing
2002f044b318SJosef Bacik 	 * super block, so invert our setting here and retry the mount so we
2003f044b318SJosef Bacik 	 * can get our vfsmount.
2004f044b318SJosef Bacik 	 */
2005f044b318SJosef Bacik 	if (ro2rw)
2006f044b318SJosef Bacik 		fc->sb_flags |= SB_RDONLY;
2007f044b318SJosef Bacik 	else
2008f044b318SJosef Bacik 		fc->sb_flags &= ~SB_RDONLY;
2009f044b318SJosef Bacik 
2010f044b318SJosef Bacik 	mnt = fc_mount(fc);
2011f044b318SJosef Bacik 	if (IS_ERR(mnt))
2012f044b318SJosef Bacik 		return mnt;
2013f044b318SJosef Bacik 
2014f044b318SJosef Bacik 	if (!fc->oldapi || !ro2rw)
2015f044b318SJosef Bacik 		return mnt;
2016f044b318SJosef Bacik 
2017f044b318SJosef Bacik 	/* We need to convert to rw, call reconfigure. */
2018f044b318SJosef Bacik 	fc->sb_flags &= ~SB_RDONLY;
2019f044b318SJosef Bacik 	down_write(&mnt->mnt_sb->s_umount);
2020f044b318SJosef Bacik 	ret = btrfs_reconfigure(fc);
2021f044b318SJosef Bacik 	up_write(&mnt->mnt_sb->s_umount);
2022f044b318SJosef Bacik 	if (ret) {
2023f044b318SJosef Bacik 		mntput(mnt);
2024f044b318SJosef Bacik 		return ERR_PTR(ret);
2025f044b318SJosef Bacik 	}
2026f044b318SJosef Bacik 	return mnt;
2027f044b318SJosef Bacik }
2028f044b318SJosef Bacik 
btrfs_get_tree_subvol(struct fs_context * fc)20293bb17a25SJosef Bacik static int btrfs_get_tree_subvol(struct fs_context *fc)
20303bb17a25SJosef Bacik {
20313bb17a25SJosef Bacik 	struct btrfs_fs_info *fs_info = NULL;
20323bb17a25SJosef Bacik 	struct btrfs_fs_context *ctx = fc->fs_private;
20333bb17a25SJosef Bacik 	struct fs_context *dup_fc;
20343bb17a25SJosef Bacik 	struct dentry *dentry;
20353bb17a25SJosef Bacik 	struct vfsmount *mnt;
20363bb17a25SJosef Bacik 
20373bb17a25SJosef Bacik 	/*
20383bb17a25SJosef Bacik 	 * Setup a dummy root and fs_info for test/set super.  This is because
20393bb17a25SJosef Bacik 	 * we don't actually fill this stuff out until open_ctree, but we need
20403bb17a25SJosef Bacik 	 * then open_ctree will properly initialize the file system specific
20413bb17a25SJosef Bacik 	 * settings later.  btrfs_init_fs_info initializes the static elements
20423bb17a25SJosef Bacik 	 * of the fs_info (locks and such) to make cleanup easier if we find a
20433bb17a25SJosef Bacik 	 * superblock with our given fs_devices later on at sget() time.
20443bb17a25SJosef Bacik 	 */
20453bb17a25SJosef Bacik 	fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL);
20463bb17a25SJosef Bacik 	if (!fs_info)
20473bb17a25SJosef Bacik 		return -ENOMEM;
20483bb17a25SJosef Bacik 
20493bb17a25SJosef Bacik 	fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
20503bb17a25SJosef Bacik 	fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
20513bb17a25SJosef Bacik 	if (!fs_info->super_copy || !fs_info->super_for_commit) {
20523bb17a25SJosef Bacik 		btrfs_free_fs_info(fs_info);
20533bb17a25SJosef Bacik 		return -ENOMEM;
20543bb17a25SJosef Bacik 	}
20553bb17a25SJosef Bacik 	btrfs_init_fs_info(fs_info);
20563bb17a25SJosef Bacik 
20573bb17a25SJosef Bacik 	dup_fc = vfs_dup_fs_context(fc);
20583bb17a25SJosef Bacik 	if (IS_ERR(dup_fc)) {
20593bb17a25SJosef Bacik 		btrfs_free_fs_info(fs_info);
20603bb17a25SJosef Bacik 		return PTR_ERR(dup_fc);
20613bb17a25SJosef Bacik 	}
20623bb17a25SJosef Bacik 
20633bb17a25SJosef Bacik 	/*
20643bb17a25SJosef Bacik 	 * When we do the sget_fc this gets transferred to the sb, so we only
20653bb17a25SJosef Bacik 	 * need to set it on the dup_fc as this is what creates the super block.
20663bb17a25SJosef Bacik 	 */
20673bb17a25SJosef Bacik 	dup_fc->s_fs_info = fs_info;
20683bb17a25SJosef Bacik 
20693bb17a25SJosef Bacik 	/*
20703bb17a25SJosef Bacik 	 * We'll do the security settings in our btrfs_get_tree_super() mount
20713bb17a25SJosef Bacik 	 * loop, they were duplicated into dup_fc, we can drop the originals
20723bb17a25SJosef Bacik 	 * here.
20733bb17a25SJosef Bacik 	 */
20743bb17a25SJosef Bacik 	security_free_mnt_opts(&fc->security);
20753bb17a25SJosef Bacik 	fc->security = NULL;
20763bb17a25SJosef Bacik 
20773bb17a25SJosef Bacik 	mnt = fc_mount(dup_fc);
2078f044b318SJosef Bacik 	if (PTR_ERR_OR_ZERO(mnt) == -EBUSY)
2079f044b318SJosef Bacik 		mnt = btrfs_reconfigure_for_mount(dup_fc);
20803bb17a25SJosef Bacik 	put_fs_context(dup_fc);
20813bb17a25SJosef Bacik 	if (IS_ERR(mnt))
20823bb17a25SJosef Bacik 		return PTR_ERR(mnt);
20833bb17a25SJosef Bacik 
20843bb17a25SJosef Bacik 	/*
20853bb17a25SJosef Bacik 	 * This free's ->subvol_name, because if it isn't set we have to
20863bb17a25SJosef Bacik 	 * allocate a buffer to hold the subvol_name, so we just drop our
20873bb17a25SJosef Bacik 	 * reference to it here.
20883bb17a25SJosef Bacik 	 */
20893bb17a25SJosef Bacik 	dentry = mount_subvol(ctx->subvol_name, ctx->subvol_objectid, mnt);
20903bb17a25SJosef Bacik 	ctx->subvol_name = NULL;
20913bb17a25SJosef Bacik 	if (IS_ERR(dentry))
20923bb17a25SJosef Bacik 		return PTR_ERR(dentry);
20933bb17a25SJosef Bacik 
20943bb17a25SJosef Bacik 	fc->root = dentry;
20953bb17a25SJosef Bacik 	return 0;
20963bb17a25SJosef Bacik }
20973bb17a25SJosef Bacik 
btrfs_get_tree(struct fs_context * fc)20983bb17a25SJosef Bacik static int btrfs_get_tree(struct fs_context *fc)
20993bb17a25SJosef Bacik {
21003bb17a25SJosef Bacik 	/*
21013bb17a25SJosef Bacik 	 * Since we use mount_subtree to mount the default/specified subvol, we
21023bb17a25SJosef Bacik 	 * have to do mounts in two steps.
21033bb17a25SJosef Bacik 	 *
21043bb17a25SJosef Bacik 	 * First pass through we call btrfs_get_tree_subvol(), this is just a
21053bb17a25SJosef Bacik 	 * wrapper around fc_mount() to call back into here again, and this time
21063bb17a25SJosef Bacik 	 * we'll call btrfs_get_tree_super().  This will do the open_ctree() and
21073bb17a25SJosef Bacik 	 * everything to open the devices and file system.  Then we return back
21083bb17a25SJosef Bacik 	 * with a fully constructed vfsmount in btrfs_get_tree_subvol(), and
21093bb17a25SJosef Bacik 	 * from there we can do our mount_subvol() call, which will lookup
21103bb17a25SJosef Bacik 	 * whichever subvol we're mounting and setup this fc with the
21113bb17a25SJosef Bacik 	 * appropriate dentry for the subvol.
21123bb17a25SJosef Bacik 	 */
21133bb17a25SJosef Bacik 	if (fc->s_fs_info)
21143bb17a25SJosef Bacik 		return btrfs_get_tree_super(fc);
21153bb17a25SJosef Bacik 	return btrfs_get_tree_subvol(fc);
21163bb17a25SJosef Bacik }
21173bb17a25SJosef Bacik 
btrfs_kill_super(struct super_block * sb)2118aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb)
2119aea52e19SAl Viro {
2120815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2121aea52e19SAl Viro 	kill_anon_super(sb);
21220d4b0463SJosef Bacik 	btrfs_free_fs_info(fs_info);
2123aea52e19SAl Viro }
2124aea52e19SAl Viro 
btrfs_free_fs_context(struct fs_context * fc)21250f85e244SJosef Bacik static void btrfs_free_fs_context(struct fs_context *fc)
21260f85e244SJosef Bacik {
21270f85e244SJosef Bacik 	struct btrfs_fs_context *ctx = fc->fs_private;
21283bb17a25SJosef Bacik 	struct btrfs_fs_info *fs_info = fc->s_fs_info;
21290f85e244SJosef Bacik 
21303bb17a25SJosef Bacik 	if (fs_info)
21313bb17a25SJosef Bacik 		btrfs_free_fs_info(fs_info);
21320f85e244SJosef Bacik 
21333bb17a25SJosef Bacik 	if (ctx && refcount_dec_and_test(&ctx->refs)) {
21340f85e244SJosef Bacik 		kfree(ctx->subvol_name);
21350f85e244SJosef Bacik 		kfree(ctx);
21360f85e244SJosef Bacik 	}
21373bb17a25SJosef Bacik }
21383bb17a25SJosef Bacik 
btrfs_dup_fs_context(struct fs_context * fc,struct fs_context * src_fc)21393bb17a25SJosef Bacik static int btrfs_dup_fs_context(struct fs_context *fc, struct fs_context *src_fc)
21403bb17a25SJosef Bacik {
21413bb17a25SJosef Bacik 	struct btrfs_fs_context *ctx = src_fc->fs_private;
21423bb17a25SJosef Bacik 
21433bb17a25SJosef Bacik 	/*
21443bb17a25SJosef Bacik 	 * Give a ref to our ctx to this dup, as we want to keep it around for
21453bb17a25SJosef Bacik 	 * our original fc so we can have the subvolume name or objectid.
21463bb17a25SJosef Bacik 	 *
21473bb17a25SJosef Bacik 	 * We unset ->source in the original fc because the dup needs it for
21483bb17a25SJosef Bacik 	 * mounting, and then once we free the dup it'll free ->source, so we
21493bb17a25SJosef Bacik 	 * need to make sure we're only pointing to it in one fc.
21503bb17a25SJosef Bacik 	 */
21513bb17a25SJosef Bacik 	refcount_inc(&ctx->refs);
21523bb17a25SJosef Bacik 	fc->fs_private = ctx;
21533bb17a25SJosef Bacik 	fc->source = src_fc->source;
21543bb17a25SJosef Bacik 	src_fc->source = NULL;
21553bb17a25SJosef Bacik 	return 0;
21563bb17a25SJosef Bacik }
21570f85e244SJosef Bacik 
21580f85e244SJosef Bacik static const struct fs_context_operations btrfs_fs_context_ops = {
215917b36120SJosef Bacik 	.parse_param	= btrfs_parse_param,
2160eddb1a43SJosef Bacik 	.reconfigure	= btrfs_reconfigure,
21613bb17a25SJosef Bacik 	.get_tree	= btrfs_get_tree,
21623bb17a25SJosef Bacik 	.dup		= btrfs_dup_fs_context,
21630f85e244SJosef Bacik 	.free		= btrfs_free_fs_context,
216417b36120SJosef Bacik };
216517b36120SJosef Bacik 
btrfs_init_fs_context(struct fs_context * fc)2166ad21f15bSJosef Bacik static int btrfs_init_fs_context(struct fs_context *fc)
21670f85e244SJosef Bacik {
21680f85e244SJosef Bacik 	struct btrfs_fs_context *ctx;
21690f85e244SJosef Bacik 
21700f85e244SJosef Bacik 	ctx = kzalloc(sizeof(struct btrfs_fs_context), GFP_KERNEL);
21710f85e244SJosef Bacik 	if (!ctx)
21720f85e244SJosef Bacik 		return -ENOMEM;
21730f85e244SJosef Bacik 
21743bb17a25SJosef Bacik 	refcount_set(&ctx->refs, 1);
21750f85e244SJosef Bacik 	fc->fs_private = ctx;
21760f85e244SJosef Bacik 	fc->ops = &btrfs_fs_context_ops;
21770f85e244SJosef Bacik 
2178eddb1a43SJosef Bacik 	if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
2179eddb1a43SJosef Bacik 		btrfs_info_to_ctx(btrfs_sb(fc->root->d_sb), ctx);
2180eddb1a43SJosef Bacik 	} else {
2181eddb1a43SJosef Bacik 		ctx->thread_pool_size =
2182eddb1a43SJosef Bacik 			min_t(unsigned long, num_online_cpus() + 2, 8);
2183eddb1a43SJosef Bacik 		ctx->max_inline = BTRFS_DEFAULT_MAX_INLINE;
2184eddb1a43SJosef Bacik 		ctx->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
2185eddb1a43SJosef Bacik 	}
2186eddb1a43SJosef Bacik 
2187ad21f15bSJosef Bacik #ifdef CONFIG_BTRFS_FS_POSIX_ACL
2188ad21f15bSJosef Bacik 	fc->sb_flags |= SB_POSIXACL;
2189ad21f15bSJosef Bacik #endif
2190ad21f15bSJosef Bacik 	fc->sb_flags |= SB_I_VERSION;
2191ad21f15bSJosef Bacik 
21920f85e244SJosef Bacik 	return 0;
21930f85e244SJosef Bacik }
21940f85e244SJosef Bacik 
21952e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
21962e635a27SChris Mason 	.owner			= THIS_MODULE,
21972e635a27SChris Mason 	.name			= "btrfs",
2198ad21f15bSJosef Bacik 	.init_fs_context	= btrfs_init_fs_context,
2199ad21f15bSJosef Bacik 	.parameters		= btrfs_fs_parameters,
220072fa39f5SMisono, Tomohiro 	.kill_sb		= btrfs_kill_super,
22015b9b26f5SChristian Brauner 	.fs_flags		= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA | FS_ALLOW_IDMAP,
220272fa39f5SMisono, Tomohiro  };
220372fa39f5SMisono, Tomohiro 
22047f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs");
2205a9218f6bSChris Mason 
btrfs_control_open(struct inode * inode,struct file * file)2206d8620958STom Van Braeckel static int btrfs_control_open(struct inode *inode, struct file *file)
2207d8620958STom Van Braeckel {
2208d8620958STom Van Braeckel 	/*
2209d8620958STom Van Braeckel 	 * The control file's private_data is used to hold the
2210d8620958STom Van Braeckel 	 * transaction when it is started and is used to keep
2211d8620958STom Van Braeckel 	 * track of whether a transaction is already in progress.
2212d8620958STom Van Braeckel 	 */
2213d8620958STom Van Braeckel 	file->private_data = NULL;
2214d8620958STom Van Braeckel 	return 0;
2215d8620958STom Van Braeckel }
2216d8620958STom Van Braeckel 
2217d352ac68SChris Mason /*
2218cfe953c8SSu Yue  * Used by /dev/btrfs-control for devices ioctls.
2219d352ac68SChris Mason  */
btrfs_control_ioctl(struct file * file,unsigned int cmd,unsigned long arg)22208a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
22218a4b83ccSChris Mason 				unsigned long arg)
22228a4b83ccSChris Mason {
22238a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
222436350e95SGu Jinxiang 	struct btrfs_device *device = NULL;
222516cab91aSAnand Jain 	dev_t devt = 0;
2226c071fcfdSChris Mason 	int ret = -ENOTTY;
22278a4b83ccSChris Mason 
2228e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2229e441d54dSChris Mason 		return -EPERM;
2230e441d54dSChris Mason 
2231dae7b665SLi Zefan 	vol = memdup_user((void __user *)arg, sizeof(*vol));
2232dae7b665SLi Zefan 	if (IS_ERR(vol))
2233dae7b665SLi Zefan 		return PTR_ERR(vol);
22345ab2b180SDavid Sterba 	ret = btrfs_check_ioctl_vol_args_path(vol);
22355ab2b180SDavid Sterba 	if (ret < 0)
22365ab2b180SDavid Sterba 		goto out;
2237c071fcfdSChris Mason 
22388a4b83ccSChris Mason 	switch (cmd) {
22398a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
2240899f9307SDavid Sterba 		mutex_lock(&uuid_mutex);
2241bc27d6f0SAnand Jain 		/*
2242bc27d6f0SAnand Jain 		 * Scanning outside of mount can return NULL which would turn
2243bc27d6f0SAnand Jain 		 * into 0 error code.
2244bc27d6f0SAnand Jain 		 */
2245bc27d6f0SAnand Jain 		device = btrfs_scan_one_device(vol->name, BLK_OPEN_READ, false);
224636350e95SGu Jinxiang 		ret = PTR_ERR_OR_ZERO(device);
2247899f9307SDavid Sterba 		mutex_unlock(&uuid_mutex);
22488a4b83ccSChris Mason 		break;
2249228a73abSAnand Jain 	case BTRFS_IOC_FORGET_DEV:
225016cab91aSAnand Jain 		if (vol->name[0] != 0) {
225116cab91aSAnand Jain 			ret = lookup_bdev(vol->name, &devt);
225216cab91aSAnand Jain 			if (ret)
225316cab91aSAnand Jain 				break;
225416cab91aSAnand Jain 		}
225516cab91aSAnand Jain 		ret = btrfs_forget_devices(devt);
2256228a73abSAnand Jain 		break;
225702db0844SJosef Bacik 	case BTRFS_IOC_DEVICES_READY:
2258899f9307SDavid Sterba 		mutex_lock(&uuid_mutex);
2259bc27d6f0SAnand Jain 		/*
2260bc27d6f0SAnand Jain 		 * Scanning outside of mount can return NULL which would turn
2261bc27d6f0SAnand Jain 		 * into 0 error code.
2262bc27d6f0SAnand Jain 		 */
2263bc27d6f0SAnand Jain 		device = btrfs_scan_one_device(vol->name, BLK_OPEN_READ, false);
2264bc27d6f0SAnand Jain 		if (IS_ERR_OR_NULL(device)) {
2265899f9307SDavid Sterba 			mutex_unlock(&uuid_mutex);
226636350e95SGu Jinxiang 			ret = PTR_ERR(device);
226702db0844SJosef Bacik 			break;
2268899f9307SDavid Sterba 		}
226936350e95SGu Jinxiang 		ret = !(device->fs_devices->num_devices ==
227036350e95SGu Jinxiang 			device->fs_devices->total_devices);
2271899f9307SDavid Sterba 		mutex_unlock(&uuid_mutex);
227202db0844SJosef Bacik 		break;
2273c5868f83SDavid Sterba 	case BTRFS_IOC_GET_SUPPORTED_FEATURES:
2274d5131b65SDavid Sterba 		ret = btrfs_ioctl_get_supported_features((void __user*)arg);
2275c5868f83SDavid Sterba 		break;
22768a4b83ccSChris Mason 	}
2277dae7b665SLi Zefan 
22785ab2b180SDavid Sterba out:
22798a4b83ccSChris Mason 	kfree(vol);
2280f819d837SLinda Knippers 	return ret;
22818a4b83ccSChris Mason }
22828a4b83ccSChris Mason 
btrfs_freeze(struct super_block * sb)22830176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
2284ed0dab6bSYan {
22850b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2286354aa0fbSMiao Xie 
2287fac03c8dSDavid Sterba 	set_bit(BTRFS_FS_FROZEN, &fs_info->flags);
22889e7cc91aSWang Xiaoguang 	/*
22899e7cc91aSWang Xiaoguang 	 * We don't need a barrier here, we'll wait for any transaction that
22909e7cc91aSWang Xiaoguang 	 * could be in progress on other threads (and do delayed iputs that
22919e7cc91aSWang Xiaoguang 	 * we want to avoid on a frozen filesystem), or do the commit
22929e7cc91aSWang Xiaoguang 	 * ourselves.
22939e7cc91aSWang Xiaoguang 	 */
2294ded980ebSFilipe Manana 	return btrfs_commit_current_transaction(fs_info->tree_root);
2295ed0dab6bSYan }
2296ed0dab6bSYan 
check_dev_super(struct btrfs_device * dev)2297a05d3c91SQu Wenruo static int check_dev_super(struct btrfs_device *dev)
2298a05d3c91SQu Wenruo {
2299a05d3c91SQu Wenruo 	struct btrfs_fs_info *fs_info = dev->fs_info;
2300a05d3c91SQu Wenruo 	struct btrfs_super_block *sb;
23010124855fSFilipe Manana 	u64 last_trans;
23023d17adeaSQu Wenruo 	u16 csum_type;
2303a05d3c91SQu Wenruo 	int ret = 0;
2304a05d3c91SQu Wenruo 
2305a05d3c91SQu Wenruo 	/* This should be called with fs still frozen. */
2306a05d3c91SQu Wenruo 	ASSERT(test_bit(BTRFS_FS_FROZEN, &fs_info->flags));
2307a05d3c91SQu Wenruo 
2308a05d3c91SQu Wenruo 	/* Missing dev, no need to check. */
2309a05d3c91SQu Wenruo 	if (!dev->bdev)
2310a05d3c91SQu Wenruo 		return 0;
2311a05d3c91SQu Wenruo 
2312a05d3c91SQu Wenruo 	/* Only need to check the primary super block. */
2313a05d3c91SQu Wenruo 	sb = btrfs_read_dev_one_super(dev->bdev, 0, true);
2314a05d3c91SQu Wenruo 	if (IS_ERR(sb))
2315a05d3c91SQu Wenruo 		return PTR_ERR(sb);
2316a05d3c91SQu Wenruo 
23173d17adeaSQu Wenruo 	/* Verify the checksum. */
23183d17adeaSQu Wenruo 	csum_type = btrfs_super_csum_type(sb);
23193d17adeaSQu Wenruo 	if (csum_type != btrfs_super_csum_type(fs_info->super_copy)) {
23203d17adeaSQu Wenruo 		btrfs_err(fs_info, "csum type changed, has %u expect %u",
23213d17adeaSQu Wenruo 			  csum_type, btrfs_super_csum_type(fs_info->super_copy));
23223d17adeaSQu Wenruo 		ret = -EUCLEAN;
23233d17adeaSQu Wenruo 		goto out;
23243d17adeaSQu Wenruo 	}
23253d17adeaSQu Wenruo 
23263d17adeaSQu Wenruo 	if (btrfs_check_super_csum(fs_info, sb)) {
23273d17adeaSQu Wenruo 		btrfs_err(fs_info, "csum for on-disk super block no longer matches");
23283d17adeaSQu Wenruo 		ret = -EUCLEAN;
23293d17adeaSQu Wenruo 		goto out;
23303d17adeaSQu Wenruo 	}
23313d17adeaSQu Wenruo 
2332a05d3c91SQu Wenruo 	/* Btrfs_validate_super() includes fsid check against super->fsid. */
2333a05d3c91SQu Wenruo 	ret = btrfs_validate_super(fs_info, sb, 0);
2334a05d3c91SQu Wenruo 	if (ret < 0)
2335a05d3c91SQu Wenruo 		goto out;
2336a05d3c91SQu Wenruo 
23370124855fSFilipe Manana 	last_trans = btrfs_get_last_trans_committed(fs_info);
23380124855fSFilipe Manana 	if (btrfs_super_generation(sb) != last_trans) {
2339a05d3c91SQu Wenruo 		btrfs_err(fs_info, "transid mismatch, has %llu expect %llu",
23400124855fSFilipe Manana 			  btrfs_super_generation(sb), last_trans);
2341a05d3c91SQu Wenruo 		ret = -EUCLEAN;
2342a05d3c91SQu Wenruo 		goto out;
2343a05d3c91SQu Wenruo 	}
2344a05d3c91SQu Wenruo out:
2345a05d3c91SQu Wenruo 	btrfs_release_disk_super(sb);
2346a05d3c91SQu Wenruo 	return ret;
2347a05d3c91SQu Wenruo }
2348a05d3c91SQu Wenruo 
btrfs_unfreeze(struct super_block * sb)23499e7cc91aSWang Xiaoguang static int btrfs_unfreeze(struct super_block *sb)
23509e7cc91aSWang Xiaoguang {
2351fac03c8dSDavid Sterba 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2352a05d3c91SQu Wenruo 	struct btrfs_device *device;
2353a05d3c91SQu Wenruo 	int ret = 0;
2354fac03c8dSDavid Sterba 
2355a05d3c91SQu Wenruo 	/*
2356a05d3c91SQu Wenruo 	 * Make sure the fs is not changed by accident (like hibernation then
2357a05d3c91SQu Wenruo 	 * modified by other OS).
2358a05d3c91SQu Wenruo 	 * If we found anything wrong, we mark the fs error immediately.
2359a05d3c91SQu Wenruo 	 *
2360a05d3c91SQu Wenruo 	 * And since the fs is frozen, no one can modify the fs yet, thus
2361a05d3c91SQu Wenruo 	 * we don't need to hold device_list_mutex.
2362a05d3c91SQu Wenruo 	 */
2363a05d3c91SQu Wenruo 	list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
2364a05d3c91SQu Wenruo 		ret = check_dev_super(device);
2365a05d3c91SQu Wenruo 		if (ret < 0) {
2366a05d3c91SQu Wenruo 			btrfs_handle_fs_error(fs_info, ret,
2367a05d3c91SQu Wenruo 				"super block on devid %llu got modified unexpectedly",
2368a05d3c91SQu Wenruo 				device->devid);
2369a05d3c91SQu Wenruo 			break;
2370a05d3c91SQu Wenruo 		}
2371a05d3c91SQu Wenruo 	}
2372fac03c8dSDavid Sterba 	clear_bit(BTRFS_FS_FROZEN, &fs_info->flags);
2373a05d3c91SQu Wenruo 
2374a05d3c91SQu Wenruo 	/*
2375a05d3c91SQu Wenruo 	 * We still return 0, to allow VFS layer to unfreeze the fs even the
2376a05d3c91SQu Wenruo 	 * above checks failed. Since the fs is either fine or read-only, we're
2377a05d3c91SQu Wenruo 	 * safe to continue, without causing further damage.
2378a05d3c91SQu Wenruo 	 */
23799e7cc91aSWang Xiaoguang 	return 0;
23809e7cc91aSWang Xiaoguang }
23819e7cc91aSWang Xiaoguang 
btrfs_show_devname(struct seq_file * m,struct dentry * root)23829c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
23839c5085c1SJosef Bacik {
23849c5085c1SJosef Bacik 	struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
23859c5085c1SJosef Bacik 
238688c14590SDavid Sterba 	/*
23876605fd2fSAnand Jain 	 * There should be always a valid pointer in latest_dev, it may be stale
23886605fd2fSAnand Jain 	 * for a short moment in case it's being deleted but still valid until
23896605fd2fSAnand Jain 	 * the end of RCU grace period.
239088c14590SDavid Sterba 	 */
239188c14590SDavid Sterba 	rcu_read_lock();
2392cb3e217bSQu Wenruo 	seq_escape(m, btrfs_dev_name(fs_info->fs_devices->latest_dev), " \t\n\\");
239388c14590SDavid Sterba 	rcu_read_unlock();
23946605fd2fSAnand Jain 
23959c5085c1SJosef Bacik 	return 0;
23969c5085c1SJosef Bacik }
23979c5085c1SJosef Bacik 
btrfs_nr_cached_objects(struct super_block * sb,struct shrink_control * sc)2398956a17d9SFilipe Manana static long btrfs_nr_cached_objects(struct super_block *sb, struct shrink_control *sc)
2399956a17d9SFilipe Manana {
2400956a17d9SFilipe Manana 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
24010d89a15eSFilipe Manana 	const s64 nr = percpu_counter_sum_positive(&fs_info->evictable_extent_maps);
2402956a17d9SFilipe Manana 
24030d89a15eSFilipe Manana 	trace_btrfs_extent_map_shrinker_count(fs_info, nr);
24040d89a15eSFilipe Manana 
2405*534f7effSQu Wenruo 	/*
2406*534f7effSQu Wenruo 	 * Only report the real number for DEBUG builds, as there are reports of
2407*534f7effSQu Wenruo 	 * serious performance degradation caused by too frequent shrinks.
2408*534f7effSQu Wenruo 	 */
2409*534f7effSQu Wenruo 	if (IS_ENABLED(CONFIG_BTRFS_DEBUG))
24100d89a15eSFilipe Manana 		return nr;
2411*534f7effSQu Wenruo 	return 0;
2412956a17d9SFilipe Manana }
2413956a17d9SFilipe Manana 
btrfs_free_cached_objects(struct super_block * sb,struct shrink_control * sc)2414956a17d9SFilipe Manana static long btrfs_free_cached_objects(struct super_block *sb, struct shrink_control *sc)
2415956a17d9SFilipe Manana {
2416956a17d9SFilipe Manana 	const long nr_to_scan = min_t(unsigned long, LONG_MAX, sc->nr_to_scan);
2417956a17d9SFilipe Manana 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2418956a17d9SFilipe Manana 
2419ae1e766fSFilipe Manana 	/*
2420ae1e766fSFilipe Manana 	 * We may be called from any task trying to allocate memory and we don't
2421ae1e766fSFilipe Manana 	 * want to slow it down with scanning and dropping extent maps. It would
2422ae1e766fSFilipe Manana 	 * also cause heavy lock contention if many tasks concurrently enter
2423ae1e766fSFilipe Manana 	 * here. Therefore only allow kswapd tasks to scan and drop extent maps.
2424ae1e766fSFilipe Manana 	 */
2425ae1e766fSFilipe Manana 	if (!current_is_kswapd())
2426ae1e766fSFilipe Manana 		return 0;
2427ae1e766fSFilipe Manana 
2428956a17d9SFilipe Manana 	return btrfs_free_extent_maps(fs_info, nr_to_scan);
2429956a17d9SFilipe Manana }
2430956a17d9SFilipe Manana 
2431b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
243276dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
2433bd555975SAl Viro 	.evict_inode	= btrfs_evict_inode,
2434e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
2435d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
2436a9572a15SEric Paris 	.show_options	= btrfs_show_options,
24379c5085c1SJosef Bacik 	.show_devname	= btrfs_show_devname,
24382c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
24392c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
244026602cabSAl Viro 	.free_inode	= btrfs_free_inode,
24418fd17795SChris Mason 	.statfs		= btrfs_statfs,
24420176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
24439e7cc91aSWang Xiaoguang 	.unfreeze_fs	= btrfs_unfreeze,
2444956a17d9SFilipe Manana 	.nr_cached_objects = btrfs_nr_cached_objects,
2445956a17d9SFilipe Manana 	.free_cached_objects = btrfs_free_cached_objects,
2446e20d96d6SChris Mason };
2447a9218f6bSChris Mason 
2448a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
2449d8620958STom Van Braeckel 	.open = btrfs_control_open,
2450a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
24511832f2d8SArnd Bergmann 	.compat_ioctl = compat_ptr_ioctl,
2452a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
24536038f373SArnd Bergmann 	.llseek = noop_llseek,
2454a9218f6bSChris Mason };
2455a9218f6bSChris Mason 
2456a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
2457578454ffSKay Sievers 	.minor		= BTRFS_MINOR,
2458a9218f6bSChris Mason 	.name		= "btrfs-control",
2459a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
2460a9218f6bSChris Mason };
2461a9218f6bSChris Mason 
2462578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
2463578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control");
2464578454ffSKay Sievers 
btrfs_interface_init(void)2465f5c29bd9SLiu Bo static int __init btrfs_interface_init(void)
2466a9218f6bSChris Mason {
2467a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
2468a9218f6bSChris Mason }
2469a9218f6bSChris Mason 
btrfs_interface_exit(void)2470e67c718bSDavid Sterba static __cold void btrfs_interface_exit(void)
2471a9218f6bSChris Mason {
2472f368ed60SGreg Kroah-Hartman 	misc_deregister(&btrfs_misc);
2473a9218f6bSChris Mason }
2474a9218f6bSChris Mason 
btrfs_print_mod_info(void)24755565b8e0SQu Wenruo static int __init btrfs_print_mod_info(void)
247685965600SDavid Sterba {
2477edf57cbfSBart Van Assche 	static const char options[] = ""
247885965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG
247985965600SDavid Sterba 			", debug=on"
248085965600SDavid Sterba #endif
248179556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT
248279556c3dSStefan Behrens 			", assert=on"
248379556c3dSStefan Behrens #endif
2484fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
2485fb592373SJosef Bacik 			", ref-verify=on"
2486fb592373SJosef Bacik #endif
24875b316468SNaohiro Aota #ifdef CONFIG_BLK_DEV_ZONED
24885b316468SNaohiro Aota 			", zoned=yes"
24895b316468SNaohiro Aota #else
24905b316468SNaohiro Aota 			", zoned=no"
24915b316468SNaohiro Aota #endif
2492ea3dc7d2SDavid Sterba #ifdef CONFIG_FS_VERITY
2493ea3dc7d2SDavid Sterba 			", fsverity=yes"
2494ea3dc7d2SDavid Sterba #else
2495ea3dc7d2SDavid Sterba 			", fsverity=no"
2496ea3dc7d2SDavid Sterba #endif
2497edf57cbfSBart Van Assche 			;
24986e7a367eSChristoph Hellwig 	pr_info("Btrfs loaded%s\n", options);
24995565b8e0SQu Wenruo 	return 0;
25005565b8e0SQu Wenruo }
25015565b8e0SQu Wenruo 
register_btrfs(void)25025565b8e0SQu Wenruo static int register_btrfs(void)
25035565b8e0SQu Wenruo {
25045565b8e0SQu Wenruo 	return register_filesystem(&btrfs_fs_type);
25055565b8e0SQu Wenruo }
25065565b8e0SQu Wenruo 
unregister_btrfs(void)25075565b8e0SQu Wenruo static void unregister_btrfs(void)
25085565b8e0SQu Wenruo {
25095565b8e0SQu Wenruo 	unregister_filesystem(&btrfs_fs_type);
25105565b8e0SQu Wenruo }
25115565b8e0SQu Wenruo 
25125565b8e0SQu Wenruo /* Helper structure for long init/exit functions. */
25135565b8e0SQu Wenruo struct init_sequence {
25145565b8e0SQu Wenruo 	int (*init_func)(void);
25155565b8e0SQu Wenruo 	/* Can be NULL if the init_func doesn't need cleanup. */
25165565b8e0SQu Wenruo 	void (*exit_func)(void);
25175565b8e0SQu Wenruo };
25185565b8e0SQu Wenruo 
25195565b8e0SQu Wenruo static const struct init_sequence mod_init_seq[] = {
25205565b8e0SQu Wenruo 	{
25215565b8e0SQu Wenruo 		.init_func = btrfs_props_init,
25225565b8e0SQu Wenruo 		.exit_func = NULL,
25235565b8e0SQu Wenruo 	}, {
25245565b8e0SQu Wenruo 		.init_func = btrfs_init_sysfs,
25255565b8e0SQu Wenruo 		.exit_func = btrfs_exit_sysfs,
25265565b8e0SQu Wenruo 	}, {
25275565b8e0SQu Wenruo 		.init_func = btrfs_init_compress,
25285565b8e0SQu Wenruo 		.exit_func = btrfs_exit_compress,
25295565b8e0SQu Wenruo 	}, {
25305565b8e0SQu Wenruo 		.init_func = btrfs_init_cachep,
25315565b8e0SQu Wenruo 		.exit_func = btrfs_destroy_cachep,
25325565b8e0SQu Wenruo 	}, {
25339aa29a20SFilipe Manana 		.init_func = btrfs_init_dio,
25349aa29a20SFilipe Manana 		.exit_func = btrfs_destroy_dio,
25359aa29a20SFilipe Manana 	}, {
25365565b8e0SQu Wenruo 		.init_func = btrfs_transaction_init,
25375565b8e0SQu Wenruo 		.exit_func = btrfs_transaction_exit,
25385565b8e0SQu Wenruo 	}, {
25395565b8e0SQu Wenruo 		.init_func = btrfs_ctree_init,
25405565b8e0SQu Wenruo 		.exit_func = btrfs_ctree_exit,
25415565b8e0SQu Wenruo 	}, {
25425565b8e0SQu Wenruo 		.init_func = btrfs_free_space_init,
25435565b8e0SQu Wenruo 		.exit_func = btrfs_free_space_exit,
25445565b8e0SQu Wenruo 	}, {
25455565b8e0SQu Wenruo 		.init_func = extent_state_init_cachep,
25465565b8e0SQu Wenruo 		.exit_func = extent_state_free_cachep,
25475565b8e0SQu Wenruo 	}, {
25485565b8e0SQu Wenruo 		.init_func = extent_buffer_init_cachep,
25495565b8e0SQu Wenruo 		.exit_func = extent_buffer_free_cachep,
25505565b8e0SQu Wenruo 	}, {
25515565b8e0SQu Wenruo 		.init_func = btrfs_bioset_init,
25525565b8e0SQu Wenruo 		.exit_func = btrfs_bioset_exit,
25535565b8e0SQu Wenruo 	}, {
25545565b8e0SQu Wenruo 		.init_func = extent_map_init,
25555565b8e0SQu Wenruo 		.exit_func = extent_map_exit,
25565565b8e0SQu Wenruo 	}, {
25575565b8e0SQu Wenruo 		.init_func = ordered_data_init,
25585565b8e0SQu Wenruo 		.exit_func = ordered_data_exit,
25595565b8e0SQu Wenruo 	}, {
25605565b8e0SQu Wenruo 		.init_func = btrfs_delayed_inode_init,
25615565b8e0SQu Wenruo 		.exit_func = btrfs_delayed_inode_exit,
25625565b8e0SQu Wenruo 	}, {
25635565b8e0SQu Wenruo 		.init_func = btrfs_auto_defrag_init,
25645565b8e0SQu Wenruo 		.exit_func = btrfs_auto_defrag_exit,
25655565b8e0SQu Wenruo 	}, {
25665565b8e0SQu Wenruo 		.init_func = btrfs_delayed_ref_init,
25675565b8e0SQu Wenruo 		.exit_func = btrfs_delayed_ref_exit,
25685565b8e0SQu Wenruo 	}, {
25695565b8e0SQu Wenruo 		.init_func = btrfs_prelim_ref_init,
25705565b8e0SQu Wenruo 		.exit_func = btrfs_prelim_ref_exit,
25715565b8e0SQu Wenruo 	}, {
25725565b8e0SQu Wenruo 		.init_func = btrfs_interface_init,
25735565b8e0SQu Wenruo 		.exit_func = btrfs_interface_exit,
25745565b8e0SQu Wenruo 	}, {
25755565b8e0SQu Wenruo 		.init_func = btrfs_print_mod_info,
25765565b8e0SQu Wenruo 		.exit_func = NULL,
25775565b8e0SQu Wenruo 	}, {
25785565b8e0SQu Wenruo 		.init_func = btrfs_run_sanity_tests,
25795565b8e0SQu Wenruo 		.exit_func = NULL,
25805565b8e0SQu Wenruo 	}, {
25815565b8e0SQu Wenruo 		.init_func = register_btrfs,
25825565b8e0SQu Wenruo 		.exit_func = unregister_btrfs,
25835565b8e0SQu Wenruo 	}
25845565b8e0SQu Wenruo };
25855565b8e0SQu Wenruo 
25865565b8e0SQu Wenruo static bool mod_init_result[ARRAY_SIZE(mod_init_seq)];
25875565b8e0SQu Wenruo 
btrfs_exit_btrfs_fs(void)258882c0efd3SAnand Jain static __always_inline void btrfs_exit_btrfs_fs(void)
25895565b8e0SQu Wenruo {
25905565b8e0SQu Wenruo 	int i;
25915565b8e0SQu Wenruo 
25925565b8e0SQu Wenruo 	for (i = ARRAY_SIZE(mod_init_seq) - 1; i >= 0; i--) {
25935565b8e0SQu Wenruo 		if (!mod_init_result[i])
25945565b8e0SQu Wenruo 			continue;
25955565b8e0SQu Wenruo 		if (mod_init_seq[i].exit_func)
25965565b8e0SQu Wenruo 			mod_init_seq[i].exit_func();
25975565b8e0SQu Wenruo 		mod_init_result[i] = false;
25985565b8e0SQu Wenruo 	}
259985965600SDavid Sterba }
260085965600SDavid Sterba 
exit_btrfs_fs(void)260182c0efd3SAnand Jain static void __exit exit_btrfs_fs(void)
260282c0efd3SAnand Jain {
260382c0efd3SAnand Jain 	btrfs_exit_btrfs_fs();
2604c68f7290SFilipe Manana 	btrfs_cleanup_fs_uuids();
260582c0efd3SAnand Jain }
260682c0efd3SAnand Jain 
init_btrfs_fs(void)26072e635a27SChris Mason static int __init init_btrfs_fs(void)
26082e635a27SChris Mason {
26095565b8e0SQu Wenruo 	int ret;
26105565b8e0SQu Wenruo 	int i;
261158176a96SJosef Bacik 
26125565b8e0SQu Wenruo 	for (i = 0; i < ARRAY_SIZE(mod_init_seq); i++) {
26135565b8e0SQu Wenruo 		ASSERT(!mod_init_result[i]);
26145565b8e0SQu Wenruo 		ret = mod_init_seq[i].init_func();
261582c0efd3SAnand Jain 		if (ret < 0) {
261682c0efd3SAnand Jain 			btrfs_exit_btrfs_fs();
261782c0efd3SAnand Jain 			return ret;
261882c0efd3SAnand Jain 		}
26195565b8e0SQu Wenruo 		mod_init_result[i] = true;
26205565b8e0SQu Wenruo 	}
26212f4cbe64SWyatt Banks 	return 0;
26222e635a27SChris Mason }
26232e635a27SChris Mason 
262460efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs);
26252e635a27SChris Mason module_exit(exit_btrfs_fs)
26262e635a27SChris Mason 
262795359f63SJeff Johnson MODULE_DESCRIPTION("B-Tree File System (BTRFS)");
26282e635a27SChris Mason MODULE_LICENSE("GPL");
2629d5178578SJohannes Thumshirn MODULE_SOFTDEP("pre: crc32c");
26303951e7f0SJohannes Thumshirn MODULE_SOFTDEP("pre: xxhash64");
26313831bf00SJohannes Thumshirn MODULE_SOFTDEP("pre: sha256");
2632352ae07bSDavid Sterba MODULE_SOFTDEP("pre: blake2b-256");
2633