xref: /linux/fs/btrfs/super.c (revision 4e00422ee62663e31e611d7de4d2c4aa3f8555f2)
1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0
26cbd5570SChris Mason /*
36cbd5570SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
46cbd5570SChris Mason  */
56cbd5570SChris Mason 
64b82d6e4SYan #include <linux/blkdev.h>
72e635a27SChris Mason #include <linux/module.h>
82e635a27SChris Mason #include <linux/fs.h>
92e635a27SChris Mason #include <linux/pagemap.h>
102e635a27SChris Mason #include <linux/highmem.h>
112e635a27SChris Mason #include <linux/time.h>
122e635a27SChris Mason #include <linux/init.h>
13a9572a15SEric Paris #include <linux/seq_file.h>
142e635a27SChris Mason #include <linux/string.h>
152e635a27SChris Mason #include <linux/backing-dev.h>
164b82d6e4SYan #include <linux/mount.h>
1775dfe396SChris Mason #include <linux/writeback.h>
188fd17795SChris Mason #include <linux/statfs.h>
1908607c1bSChris Mason #include <linux/compat.h>
2095e05289SChris Mason #include <linux/parser.h>
21c59f8951SChris Mason #include <linux/ctype.h>
226da6abaeSChris Mason #include <linux/namei.h>
23a9218f6bSChris Mason #include <linux/miscdevice.h>
241bcbf313SQinghuang Feng #include <linux/magic.h>
255a0e3ad6STejun Heo #include <linux/slab.h>
2622c44fe6SJosef Bacik #include <linux/ratelimit.h>
279678c543SNikolay Borisov #include <linux/crc32c.h>
2855e301fdSFilipe Brandenburger #include <linux/btrfs.h>
29c60a2880SJosef Bacik #include <linux/security.h>
3015ddcdd3SJosef Bacik #include <linux/fs_parser.h>
319b569ea0SJosef Bacik #include "messages.h"
3216cdcec7SMiao Xie #include "delayed-inode.h"
332e635a27SChris Mason #include "ctree.h"
34e20d96d6SChris Mason #include "disk-io.h"
35d5719762SChris Mason #include "transaction.h"
362c90e5d6SChris Mason #include "btrfs_inode.h"
373a686375SChris Mason #include "print-tree.h"
3863541927SFilipe David Borba Manana #include "props.h"
395103e947SJosef Bacik #include "xattr.h"
40103c1972SChristoph Hellwig #include "bio.h"
41be6e8dc0SBalaji Rao #include "export.h"
42c8b97818SChris Mason #include "compression.h"
439c5085c1SJosef Bacik #include "rcu-string.h"
448dabb742SStefan Behrens #include "dev-replace.h"
4574255aa0SJosef Bacik #include "free-space-cache.h"
46b9e9a6cbSWang Shilong #include "backref.h"
478719aaaeSJosef Bacik #include "space-info.h"
4889439109SDavid Sterba #include "sysfs.h"
49b70f5097SNaohiro Aota #include "zoned.h"
50dc11dd5dSJosef Bacik #include "tests/btrfs-tests.h"
51aac0023cSJosef Bacik #include "block-group.h"
52b0643e59SDennis Zhou #include "discard.h"
53d3982100SMark Fasheh #include "qgroup.h"
54b8bea09aSQu Wenruo #include "raid56.h"
55c7f13d42SJosef Bacik #include "fs.h"
5607e81dc9SJosef Bacik #include "accessors.h"
5759b818e0SJosef Bacik #include "defrag.h"
58f2b39277SJosef Bacik #include "dir-item.h"
597572dec8SJosef Bacik #include "ioctl.h"
602fc6822cSJosef Bacik #include "scrub.h"
615c11adccSJosef Bacik #include "verity.h"
62c03b2207SJosef Bacik #include "super.h"
63cfc2de0fSBoris Burkov #include "extent-tree.h"
641abe9b8aSliubo #define CREATE_TRACE_POINTS
651abe9b8aSliubo #include <trace/events/btrfs.h>
661abe9b8aSliubo 
67b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops;
68830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type;
690723a047SHarald Hoyer 
70e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb)
71e20d96d6SChris Mason {
722db31320SQu Wenruo 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
732db31320SQu Wenruo 
742db31320SQu Wenruo 	btrfs_info(fs_info, "last unmount of filesystem %pU", fs_info->fs_devices->fsid);
752db31320SQu Wenruo 	close_ctree(fs_info);
76e20d96d6SChris Mason }
772e635a27SChris Mason 
7817b36120SJosef Bacik /* Store the mount options related information. */
7917b36120SJosef Bacik struct btrfs_fs_context {
8017b36120SJosef Bacik 	char *subvol_name;
8117b36120SJosef Bacik 	u64 subvol_objectid;
8217b36120SJosef Bacik 	u64 max_inline;
8317b36120SJosef Bacik 	u32 commit_interval;
8417b36120SJosef Bacik 	u32 metadata_ratio;
8517b36120SJosef Bacik 	u32 thread_pool_size;
8617b36120SJosef Bacik 	unsigned long mount_opt;
8717b36120SJosef Bacik 	unsigned long compress_type:4;
8817b36120SJosef Bacik 	unsigned int compress_level;
893bb17a25SJosef Bacik 	refcount_t refs;
9017b36120SJosef Bacik };
9117b36120SJosef Bacik 
9295e05289SChris Mason enum {
936941823cSJosef Bacik 	Opt_acl,
94416a7202SDavid Sterba 	Opt_clear_cache,
95416a7202SDavid Sterba 	Opt_commit_interval,
96416a7202SDavid Sterba 	Opt_compress,
97416a7202SDavid Sterba 	Opt_compress_force,
98416a7202SDavid Sterba 	Opt_compress_force_type,
99416a7202SDavid Sterba 	Opt_compress_type,
100416a7202SDavid Sterba 	Opt_degraded,
101416a7202SDavid Sterba 	Opt_device,
102416a7202SDavid Sterba 	Opt_fatal_errors,
1036941823cSJosef Bacik 	Opt_flushoncommit,
104416a7202SDavid Sterba 	Opt_max_inline,
1056941823cSJosef Bacik 	Opt_barrier,
1066941823cSJosef Bacik 	Opt_datacow,
1076941823cSJosef Bacik 	Opt_datasum,
1086941823cSJosef Bacik 	Opt_defrag,
1096941823cSJosef Bacik 	Opt_discard,
110b0643e59SDennis Zhou 	Opt_discard_mode,
111416a7202SDavid Sterba 	Opt_ratio,
112416a7202SDavid Sterba 	Opt_rescan_uuid_tree,
113416a7202SDavid Sterba 	Opt_skip_balance,
1146941823cSJosef Bacik 	Opt_space_cache,
115416a7202SDavid Sterba 	Opt_space_cache_version,
1166941823cSJosef Bacik 	Opt_ssd,
1176941823cSJosef Bacik 	Opt_ssd_spread,
118416a7202SDavid Sterba 	Opt_subvol,
11937becec9SOmar Sandoval 	Opt_subvol_empty,
120416a7202SDavid Sterba 	Opt_subvolid,
121416a7202SDavid Sterba 	Opt_thread_pool,
1226941823cSJosef Bacik 	Opt_treelog,
123416a7202SDavid Sterba 	Opt_user_subvol_rm_allowed,
124416a7202SDavid Sterba 
12574ef0018SQu Wenruo 	/* Rescue options */
12674ef0018SQu Wenruo 	Opt_rescue,
12774ef0018SQu Wenruo 	Opt_usebackuproot,
12874ef0018SQu Wenruo 	Opt_nologreplay,
12942437a63SJosef Bacik 	Opt_ignorebadroots,
130882dbe0cSJosef Bacik 	Opt_ignoredatacsums,
1319037d3cbSJosef Bacik 	Opt_rescue_all,
13274ef0018SQu Wenruo 
133416a7202SDavid Sterba 	/* Debugging options */
1346941823cSJosef Bacik 	Opt_enospc_debug,
135d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
13615ddcdd3SJosef Bacik 	Opt_fragment, Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
137d0bd4560SJosef Bacik #endif
138fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
139fb592373SJosef Bacik 	Opt_ref_verify,
140fb592373SJosef Bacik #endif
1419555c6c1SIlya Dryomov 	Opt_err,
14295e05289SChris Mason };
14395e05289SChris Mason 
14415ddcdd3SJosef Bacik enum {
14515ddcdd3SJosef Bacik 	Opt_fatal_errors_panic,
14615ddcdd3SJosef Bacik 	Opt_fatal_errors_bug,
14715ddcdd3SJosef Bacik };
148416a7202SDavid Sterba 
14915ddcdd3SJosef Bacik static const struct constant_table btrfs_parameter_fatal_errors[] = {
15015ddcdd3SJosef Bacik 	{ "panic", Opt_fatal_errors_panic },
15115ddcdd3SJosef Bacik 	{ "bug", Opt_fatal_errors_bug },
15215ddcdd3SJosef Bacik 	{}
15315ddcdd3SJosef Bacik };
15474ef0018SQu Wenruo 
15515ddcdd3SJosef Bacik enum {
15615ddcdd3SJosef Bacik 	Opt_discard_sync,
15715ddcdd3SJosef Bacik 	Opt_discard_async,
15815ddcdd3SJosef Bacik };
159416a7202SDavid Sterba 
16015ddcdd3SJosef Bacik static const struct constant_table btrfs_parameter_discard[] = {
16115ddcdd3SJosef Bacik 	{ "sync", Opt_discard_sync },
16215ddcdd3SJosef Bacik 	{ "async", Opt_discard_async },
16315ddcdd3SJosef Bacik 	{}
16415ddcdd3SJosef Bacik };
16515ddcdd3SJosef Bacik 
16615ddcdd3SJosef Bacik enum {
16715ddcdd3SJosef Bacik 	Opt_space_cache_v1,
16815ddcdd3SJosef Bacik 	Opt_space_cache_v2,
16915ddcdd3SJosef Bacik };
17015ddcdd3SJosef Bacik 
17115ddcdd3SJosef Bacik static const struct constant_table btrfs_parameter_space_cache[] = {
17215ddcdd3SJosef Bacik 	{ "v1", Opt_space_cache_v1 },
17315ddcdd3SJosef Bacik 	{ "v2", Opt_space_cache_v2 },
17415ddcdd3SJosef Bacik 	{}
17515ddcdd3SJosef Bacik };
17615ddcdd3SJosef Bacik 
17715ddcdd3SJosef Bacik enum {
17815ddcdd3SJosef Bacik 	Opt_rescue_usebackuproot,
17915ddcdd3SJosef Bacik 	Opt_rescue_nologreplay,
18015ddcdd3SJosef Bacik 	Opt_rescue_ignorebadroots,
18115ddcdd3SJosef Bacik 	Opt_rescue_ignoredatacsums,
18215ddcdd3SJosef Bacik 	Opt_rescue_parameter_all,
18315ddcdd3SJosef Bacik };
18415ddcdd3SJosef Bacik 
18515ddcdd3SJosef Bacik static const struct constant_table btrfs_parameter_rescue[] = {
18615ddcdd3SJosef Bacik 	{ "usebackuproot", Opt_rescue_usebackuproot },
18715ddcdd3SJosef Bacik 	{ "nologreplay", Opt_rescue_nologreplay },
18815ddcdd3SJosef Bacik 	{ "ignorebadroots", Opt_rescue_ignorebadroots },
18915ddcdd3SJosef Bacik 	{ "ibadroots", Opt_rescue_ignorebadroots },
19015ddcdd3SJosef Bacik 	{ "ignoredatacsums", Opt_rescue_ignoredatacsums },
19115ddcdd3SJosef Bacik 	{ "idatacsums", Opt_rescue_ignoredatacsums },
19215ddcdd3SJosef Bacik 	{ "all", Opt_rescue_parameter_all },
19315ddcdd3SJosef Bacik 	{}
19415ddcdd3SJosef Bacik };
19515ddcdd3SJosef Bacik 
196d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
19715ddcdd3SJosef Bacik enum {
19815ddcdd3SJosef Bacik 	Opt_fragment_parameter_data,
19915ddcdd3SJosef Bacik 	Opt_fragment_parameter_metadata,
20015ddcdd3SJosef Bacik 	Opt_fragment_parameter_all,
20115ddcdd3SJosef Bacik };
20215ddcdd3SJosef Bacik 
20315ddcdd3SJosef Bacik static const struct constant_table btrfs_parameter_fragment[] = {
20415ddcdd3SJosef Bacik 	{ "data", Opt_fragment_parameter_data },
20515ddcdd3SJosef Bacik 	{ "metadata", Opt_fragment_parameter_metadata },
20615ddcdd3SJosef Bacik 	{ "all", Opt_fragment_parameter_all },
20715ddcdd3SJosef Bacik 	{}
20815ddcdd3SJosef Bacik };
20915ddcdd3SJosef Bacik #endif
21015ddcdd3SJosef Bacik 
211ad21f15bSJosef Bacik static const struct fs_parameter_spec btrfs_fs_parameters[] = {
21215ddcdd3SJosef Bacik 	fsparam_flag_no("acl", Opt_acl),
21315ddcdd3SJosef Bacik 	fsparam_flag_no("autodefrag", Opt_defrag),
21415ddcdd3SJosef Bacik 	fsparam_flag_no("barrier", Opt_barrier),
21515ddcdd3SJosef Bacik 	fsparam_flag("clear_cache", Opt_clear_cache),
21615ddcdd3SJosef Bacik 	fsparam_u32("commit", Opt_commit_interval),
21715ddcdd3SJosef Bacik 	fsparam_flag("compress", Opt_compress),
21815ddcdd3SJosef Bacik 	fsparam_string("compress", Opt_compress_type),
21915ddcdd3SJosef Bacik 	fsparam_flag("compress-force", Opt_compress_force),
22015ddcdd3SJosef Bacik 	fsparam_string("compress-force", Opt_compress_force_type),
22115ddcdd3SJosef Bacik 	fsparam_flag_no("datacow", Opt_datacow),
22215ddcdd3SJosef Bacik 	fsparam_flag_no("datasum", Opt_datasum),
22315ddcdd3SJosef Bacik 	fsparam_flag("degraded", Opt_degraded),
22415ddcdd3SJosef Bacik 	fsparam_string("device", Opt_device),
22515ddcdd3SJosef Bacik 	fsparam_flag_no("discard", Opt_discard),
22615ddcdd3SJosef Bacik 	fsparam_enum("discard", Opt_discard_mode, btrfs_parameter_discard),
22715ddcdd3SJosef Bacik 	fsparam_enum("fatal_errors", Opt_fatal_errors, btrfs_parameter_fatal_errors),
22815ddcdd3SJosef Bacik 	fsparam_flag_no("flushoncommit", Opt_flushoncommit),
22915ddcdd3SJosef Bacik 	fsparam_string("max_inline", Opt_max_inline),
23015ddcdd3SJosef Bacik 	fsparam_u32("metadata_ratio", Opt_ratio),
23115ddcdd3SJosef Bacik 	fsparam_flag("rescan_uuid_tree", Opt_rescan_uuid_tree),
23215ddcdd3SJosef Bacik 	fsparam_flag("skip_balance", Opt_skip_balance),
23315ddcdd3SJosef Bacik 	fsparam_flag_no("space_cache", Opt_space_cache),
23415ddcdd3SJosef Bacik 	fsparam_enum("space_cache", Opt_space_cache_version, btrfs_parameter_space_cache),
23515ddcdd3SJosef Bacik 	fsparam_flag_no("ssd", Opt_ssd),
23615ddcdd3SJosef Bacik 	fsparam_flag_no("ssd_spread", Opt_ssd_spread),
23715ddcdd3SJosef Bacik 	fsparam_string("subvol", Opt_subvol),
23815ddcdd3SJosef Bacik 	fsparam_flag("subvol=", Opt_subvol_empty),
23915ddcdd3SJosef Bacik 	fsparam_u64("subvolid", Opt_subvolid),
24015ddcdd3SJosef Bacik 	fsparam_u32("thread_pool", Opt_thread_pool),
24115ddcdd3SJosef Bacik 	fsparam_flag_no("treelog", Opt_treelog),
24215ddcdd3SJosef Bacik 	fsparam_flag("user_subvol_rm_allowed", Opt_user_subvol_rm_allowed),
24315ddcdd3SJosef Bacik 
24415ddcdd3SJosef Bacik 	/* Rescue options. */
24515ddcdd3SJosef Bacik 	fsparam_enum("rescue", Opt_rescue, btrfs_parameter_rescue),
24615ddcdd3SJosef Bacik 	/* Deprecated, with alias rescue=nologreplay */
24715ddcdd3SJosef Bacik 	__fsparam(NULL, "nologreplay", Opt_nologreplay, fs_param_deprecated, NULL),
24815ddcdd3SJosef Bacik 	/* Deprecated, with alias rescue=usebackuproot */
24915ddcdd3SJosef Bacik 	__fsparam(NULL, "usebackuproot", Opt_usebackuproot, fs_param_deprecated, NULL),
25015ddcdd3SJosef Bacik 
25115ddcdd3SJosef Bacik 	/* Debugging options. */
25215ddcdd3SJosef Bacik 	fsparam_flag_no("enospc_debug", Opt_enospc_debug),
25315ddcdd3SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
25415ddcdd3SJosef Bacik 	fsparam_enum("fragment", Opt_fragment, btrfs_parameter_fragment),
255d0bd4560SJosef Bacik #endif
256fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
25715ddcdd3SJosef Bacik 	fsparam_flag("ref_verify", Opt_ref_verify),
258fb592373SJosef Bacik #endif
25915ddcdd3SJosef Bacik 	{}
26095e05289SChris Mason };
26195e05289SChris Mason 
262affc5af3SLinus Torvalds /* No support for restricting writes to btrfs devices yet... */
263affc5af3SLinus Torvalds static inline blk_mode_t btrfs_open_mode(struct fs_context *fc)
264affc5af3SLinus Torvalds {
265affc5af3SLinus Torvalds 	return sb_open_mode(fc->sb_flags) & ~BLK_OPEN_RESTRICT_WRITES;
266affc5af3SLinus Torvalds }
26774ef0018SQu Wenruo 
26817b36120SJosef Bacik static int btrfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
26917b36120SJosef Bacik {
27017b36120SJosef Bacik 	struct btrfs_fs_context *ctx = fc->fs_private;
27117b36120SJosef Bacik 	struct fs_parse_result result;
27217b36120SJosef Bacik 	int opt;
27317b36120SJosef Bacik 
27417b36120SJosef Bacik 	opt = fs_parse(fc, btrfs_fs_parameters, param, &result);
27517b36120SJosef Bacik 	if (opt < 0)
27617b36120SJosef Bacik 		return opt;
27717b36120SJosef Bacik 
27817b36120SJosef Bacik 	switch (opt) {
27917b36120SJosef Bacik 	case Opt_degraded:
28017b36120SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, DEGRADED);
28117b36120SJosef Bacik 		break;
28217b36120SJosef Bacik 	case Opt_subvol_empty:
28317b36120SJosef Bacik 		/*
28417b36120SJosef Bacik 		 * This exists because we used to allow it on accident, so we're
28517b36120SJosef Bacik 		 * keeping it to maintain ABI.  See 37becec95ac3 ("Btrfs: allow
28617b36120SJosef Bacik 		 * empty subvol= again").
28717b36120SJosef Bacik 		 */
28817b36120SJosef Bacik 		break;
28917b36120SJosef Bacik 	case Opt_subvol:
29017b36120SJosef Bacik 		kfree(ctx->subvol_name);
29117b36120SJosef Bacik 		ctx->subvol_name = kstrdup(param->string, GFP_KERNEL);
29217b36120SJosef Bacik 		if (!ctx->subvol_name)
29317b36120SJosef Bacik 			return -ENOMEM;
29417b36120SJosef Bacik 		break;
29517b36120SJosef Bacik 	case Opt_subvolid:
29617b36120SJosef Bacik 		ctx->subvol_objectid = result.uint_64;
29717b36120SJosef Bacik 
29817b36120SJosef Bacik 		/* subvolid=0 means give me the original fs_tree. */
29917b36120SJosef Bacik 		if (!ctx->subvol_objectid)
30017b36120SJosef Bacik 			ctx->subvol_objectid = BTRFS_FS_TREE_OBJECTID;
30117b36120SJosef Bacik 		break;
30217b36120SJosef Bacik 	case Opt_device: {
30317b36120SJosef Bacik 		struct btrfs_device *device;
304affc5af3SLinus Torvalds 		blk_mode_t mode = btrfs_open_mode(fc);
30517b36120SJosef Bacik 
30617b36120SJosef Bacik 		mutex_lock(&uuid_mutex);
30717b36120SJosef Bacik 		device = btrfs_scan_one_device(param->string, mode, false);
30817b36120SJosef Bacik 		mutex_unlock(&uuid_mutex);
30917b36120SJosef Bacik 		if (IS_ERR(device))
31017b36120SJosef Bacik 			return PTR_ERR(device);
31117b36120SJosef Bacik 		break;
31217b36120SJosef Bacik 	}
31317b36120SJosef Bacik 	case Opt_datasum:
31417b36120SJosef Bacik 		if (result.negated) {
31517b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NODATASUM);
31617b36120SJosef Bacik 		} else {
31717b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATACOW);
31817b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATASUM);
31917b36120SJosef Bacik 		}
32017b36120SJosef Bacik 		break;
32117b36120SJosef Bacik 	case Opt_datacow:
32217b36120SJosef Bacik 		if (result.negated) {
32317b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, COMPRESS);
32417b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
32517b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NODATACOW);
32617b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NODATASUM);
32717b36120SJosef Bacik 		} else {
32817b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATACOW);
32917b36120SJosef Bacik 		}
33017b36120SJosef Bacik 		break;
33117b36120SJosef Bacik 	case Opt_compress_force:
33217b36120SJosef Bacik 	case Opt_compress_force_type:
33317b36120SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, FORCE_COMPRESS);
33417b36120SJosef Bacik 		fallthrough;
33517b36120SJosef Bacik 	case Opt_compress:
33617b36120SJosef Bacik 	case Opt_compress_type:
33717b36120SJosef Bacik 		if (opt == Opt_compress || opt == Opt_compress_force) {
33817b36120SJosef Bacik 			ctx->compress_type = BTRFS_COMPRESS_ZLIB;
33917b36120SJosef Bacik 			ctx->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
34017b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, COMPRESS);
34117b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATACOW);
34217b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATASUM);
34317b36120SJosef Bacik 		} else if (strncmp(param->string, "zlib", 4) == 0) {
34417b36120SJosef Bacik 			ctx->compress_type = BTRFS_COMPRESS_ZLIB;
34517b36120SJosef Bacik 			ctx->compress_level =
34617b36120SJosef Bacik 				btrfs_compress_str2level(BTRFS_COMPRESS_ZLIB,
34717b36120SJosef Bacik 							 param->string + 4);
34817b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, COMPRESS);
34917b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATACOW);
35017b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATASUM);
35117b36120SJosef Bacik 		} else if (strncmp(param->string, "lzo", 3) == 0) {
35217b36120SJosef Bacik 			ctx->compress_type = BTRFS_COMPRESS_LZO;
35317b36120SJosef Bacik 			ctx->compress_level = 0;
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, "zstd", 4) == 0) {
35817b36120SJosef Bacik 			ctx->compress_type = BTRFS_COMPRESS_ZSTD;
35917b36120SJosef Bacik 			ctx->compress_level =
36017b36120SJosef Bacik 				btrfs_compress_str2level(BTRFS_COMPRESS_ZSTD,
36117b36120SJosef Bacik 							 param->string + 4);
36217b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, COMPRESS);
36317b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATACOW);
36417b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NODATASUM);
36517b36120SJosef Bacik 		} else if (strncmp(param->string, "no", 2) == 0) {
36617b36120SJosef Bacik 			ctx->compress_level = 0;
36717b36120SJosef Bacik 			ctx->compress_type = 0;
36817b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, COMPRESS);
36917b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
37017b36120SJosef Bacik 		} else {
37117b36120SJosef Bacik 			btrfs_err(NULL, "unrecognized compression value %s",
37217b36120SJosef Bacik 				  param->string);
37317b36120SJosef Bacik 			return -EINVAL;
37417b36120SJosef Bacik 		}
37517b36120SJosef Bacik 		break;
37617b36120SJosef Bacik 	case Opt_ssd:
37717b36120SJosef Bacik 		if (result.negated) {
37817b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NOSSD);
37917b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, SSD);
38017b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, SSD_SPREAD);
38117b36120SJosef Bacik 		} else {
38217b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, SSD);
38317b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NOSSD);
38417b36120SJosef Bacik 		}
38517b36120SJosef Bacik 		break;
38617b36120SJosef Bacik 	case Opt_ssd_spread:
38717b36120SJosef Bacik 		if (result.negated) {
38817b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, SSD_SPREAD);
38917b36120SJosef Bacik 		} else {
39017b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, SSD);
39117b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, SSD_SPREAD);
39217b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NOSSD);
39317b36120SJosef Bacik 		}
39417b36120SJosef Bacik 		break;
39517b36120SJosef Bacik 	case Opt_barrier:
39617b36120SJosef Bacik 		if (result.negated)
39717b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NOBARRIER);
39817b36120SJosef Bacik 		else
39917b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NOBARRIER);
40017b36120SJosef Bacik 		break;
40117b36120SJosef Bacik 	case Opt_thread_pool:
40217b36120SJosef Bacik 		if (result.uint_32 == 0) {
40317b36120SJosef Bacik 			btrfs_err(NULL, "invalid value 0 for thread_pool");
40417b36120SJosef Bacik 			return -EINVAL;
40517b36120SJosef Bacik 		}
40617b36120SJosef Bacik 		ctx->thread_pool_size = result.uint_32;
40717b36120SJosef Bacik 		break;
40817b36120SJosef Bacik 	case Opt_max_inline:
40917b36120SJosef Bacik 		ctx->max_inline = memparse(param->string, NULL);
41017b36120SJosef Bacik 		break;
41117b36120SJosef Bacik 	case Opt_acl:
41217b36120SJosef Bacik 		if (result.negated) {
41317b36120SJosef Bacik 			fc->sb_flags &= ~SB_POSIXACL;
41417b36120SJosef Bacik 		} else {
41517b36120SJosef Bacik #ifdef CONFIG_BTRFS_FS_POSIX_ACL
41617b36120SJosef Bacik 			fc->sb_flags |= SB_POSIXACL;
41717b36120SJosef Bacik #else
41817b36120SJosef Bacik 			btrfs_err(NULL, "support for ACL not compiled in");
41917b36120SJosef Bacik 			return -EINVAL;
42017b36120SJosef Bacik #endif
42117b36120SJosef Bacik 		}
42217b36120SJosef Bacik 		/*
42317b36120SJosef Bacik 		 * VFS limits the ability to toggle ACL on and off via remount,
42417b36120SJosef Bacik 		 * despite every file system allowing this.  This seems to be
42517b36120SJosef Bacik 		 * an oversight since we all do, but it'll fail if we're
42617b36120SJosef Bacik 		 * remounting.  So don't set the mask here, we'll check it in
42717b36120SJosef Bacik 		 * btrfs_reconfigure and do the toggling ourselves.
42817b36120SJosef Bacik 		 */
42917b36120SJosef Bacik 		if (fc->purpose != FS_CONTEXT_FOR_RECONFIGURE)
43017b36120SJosef Bacik 			fc->sb_flags_mask |= SB_POSIXACL;
43117b36120SJosef Bacik 		break;
43217b36120SJosef Bacik 	case Opt_treelog:
43317b36120SJosef Bacik 		if (result.negated)
43417b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NOTREELOG);
43517b36120SJosef Bacik 		else
43617b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, NOTREELOG);
43717b36120SJosef Bacik 		break;
43817b36120SJosef Bacik 	case Opt_nologreplay:
43917b36120SJosef Bacik 		btrfs_warn(NULL,
44017b36120SJosef Bacik 		"'nologreplay' is deprecated, use 'rescue=nologreplay' instead");
44117b36120SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, NOLOGREPLAY);
44217b36120SJosef Bacik 		break;
44317b36120SJosef Bacik 	case Opt_flushoncommit:
44417b36120SJosef Bacik 		if (result.negated)
44517b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, FLUSHONCOMMIT);
44617b36120SJosef Bacik 		else
44717b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, FLUSHONCOMMIT);
44817b36120SJosef Bacik 		break;
44917b36120SJosef Bacik 	case Opt_ratio:
45017b36120SJosef Bacik 		ctx->metadata_ratio = result.uint_32;
45117b36120SJosef Bacik 		break;
45217b36120SJosef Bacik 	case Opt_discard:
45317b36120SJosef Bacik 		if (result.negated) {
45417b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, DISCARD_SYNC);
45517b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, DISCARD_ASYNC);
45617b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NODISCARD);
45717b36120SJosef Bacik 		} else {
45817b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, DISCARD_SYNC);
45917b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, DISCARD_ASYNC);
46017b36120SJosef Bacik 		}
46117b36120SJosef Bacik 		break;
46217b36120SJosef Bacik 	case Opt_discard_mode:
46317b36120SJosef Bacik 		switch (result.uint_32) {
46417b36120SJosef Bacik 		case Opt_discard_sync:
46517b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, DISCARD_ASYNC);
46617b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, DISCARD_SYNC);
46717b36120SJosef Bacik 			break;
46817b36120SJosef Bacik 		case Opt_discard_async:
46917b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, DISCARD_SYNC);
47017b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, DISCARD_ASYNC);
47117b36120SJosef Bacik 			break;
47217b36120SJosef Bacik 		default:
47317b36120SJosef Bacik 			btrfs_err(NULL, "unrecognized discard mode value %s",
47417b36120SJosef Bacik 				  param->key);
47517b36120SJosef Bacik 			return -EINVAL;
47617b36120SJosef Bacik 		}
47717b36120SJosef Bacik 		btrfs_clear_opt(ctx->mount_opt, NODISCARD);
47817b36120SJosef Bacik 		break;
47917b36120SJosef Bacik 	case Opt_space_cache:
48017b36120SJosef Bacik 		if (result.negated) {
48117b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NOSPACECACHE);
48217b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, SPACE_CACHE);
48317b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, FREE_SPACE_TREE);
48417b36120SJosef Bacik 		} else {
48517b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, FREE_SPACE_TREE);
48617b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, SPACE_CACHE);
48717b36120SJosef Bacik 		}
48817b36120SJosef Bacik 		break;
48917b36120SJosef Bacik 	case Opt_space_cache_version:
49017b36120SJosef Bacik 		switch (result.uint_32) {
49117b36120SJosef Bacik 		case Opt_space_cache_v1:
49217b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, SPACE_CACHE);
49317b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, FREE_SPACE_TREE);
49417b36120SJosef Bacik 			break;
49517b36120SJosef Bacik 		case Opt_space_cache_v2:
49617b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, SPACE_CACHE);
49717b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, FREE_SPACE_TREE);
49817b36120SJosef Bacik 			break;
49917b36120SJosef Bacik 		default:
50017b36120SJosef Bacik 			btrfs_err(NULL, "unrecognized space_cache value %s",
50117b36120SJosef Bacik 				  param->key);
50217b36120SJosef Bacik 			return -EINVAL;
50317b36120SJosef Bacik 		}
50417b36120SJosef Bacik 		break;
50517b36120SJosef Bacik 	case Opt_rescan_uuid_tree:
50617b36120SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, RESCAN_UUID_TREE);
50717b36120SJosef Bacik 		break;
50817b36120SJosef Bacik 	case Opt_clear_cache:
50917b36120SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, CLEAR_CACHE);
51017b36120SJosef Bacik 		break;
51117b36120SJosef Bacik 	case Opt_user_subvol_rm_allowed:
51217b36120SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, USER_SUBVOL_RM_ALLOWED);
51317b36120SJosef Bacik 		break;
51417b36120SJosef Bacik 	case Opt_enospc_debug:
51517b36120SJosef Bacik 		if (result.negated)
51617b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, ENOSPC_DEBUG);
51717b36120SJosef Bacik 		else
51817b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, ENOSPC_DEBUG);
51917b36120SJosef Bacik 		break;
52017b36120SJosef Bacik 	case Opt_defrag:
52117b36120SJosef Bacik 		if (result.negated)
52217b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, AUTO_DEFRAG);
52317b36120SJosef Bacik 		else
52417b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, AUTO_DEFRAG);
52517b36120SJosef Bacik 		break;
52617b36120SJosef Bacik 	case Opt_usebackuproot:
52717b36120SJosef Bacik 		btrfs_warn(NULL,
52817b36120SJosef Bacik 			   "'usebackuproot' is deprecated, use 'rescue=usebackuproot' instead");
52917b36120SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, USEBACKUPROOT);
5309fb3b1a7SJosef Bacik 
5319fb3b1a7SJosef Bacik 		/* If we're loading the backup roots we can't trust the space cache. */
5329fb3b1a7SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, CLEAR_CACHE);
53317b36120SJosef Bacik 		break;
53417b36120SJosef Bacik 	case Opt_skip_balance:
53517b36120SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, SKIP_BALANCE);
53617b36120SJosef Bacik 		break;
53717b36120SJosef Bacik 	case Opt_fatal_errors:
53817b36120SJosef Bacik 		switch (result.uint_32) {
53917b36120SJosef Bacik 		case Opt_fatal_errors_panic:
54017b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, PANIC_ON_FATAL_ERROR);
54117b36120SJosef Bacik 			break;
54217b36120SJosef Bacik 		case Opt_fatal_errors_bug:
54317b36120SJosef Bacik 			btrfs_clear_opt(ctx->mount_opt, PANIC_ON_FATAL_ERROR);
54417b36120SJosef Bacik 			break;
54517b36120SJosef Bacik 		default:
54617b36120SJosef Bacik 			btrfs_err(NULL, "unrecognized fatal_errors value %s",
54717b36120SJosef Bacik 				  param->key);
54817b36120SJosef Bacik 			return -EINVAL;
54917b36120SJosef Bacik 		}
55017b36120SJosef Bacik 		break;
55117b36120SJosef Bacik 	case Opt_commit_interval:
55217b36120SJosef Bacik 		ctx->commit_interval = result.uint_32;
55317b36120SJosef Bacik 		if (ctx->commit_interval == 0)
55417b36120SJosef Bacik 			ctx->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
55517b36120SJosef Bacik 		break;
55617b36120SJosef Bacik 	case Opt_rescue:
55717b36120SJosef Bacik 		switch (result.uint_32) {
55817b36120SJosef Bacik 		case Opt_rescue_usebackuproot:
55917b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, USEBACKUPROOT);
56017b36120SJosef Bacik 			break;
56117b36120SJosef Bacik 		case Opt_rescue_nologreplay:
56217b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NOLOGREPLAY);
56317b36120SJosef Bacik 			break;
56417b36120SJosef Bacik 		case Opt_rescue_ignorebadroots:
56517b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, IGNOREBADROOTS);
56617b36120SJosef Bacik 			break;
56717b36120SJosef Bacik 		case Opt_rescue_ignoredatacsums:
56817b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, IGNOREDATACSUMS);
56917b36120SJosef Bacik 			break;
57017b36120SJosef Bacik 		case Opt_rescue_parameter_all:
57117b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, IGNOREDATACSUMS);
57217b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, IGNOREBADROOTS);
57317b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, NOLOGREPLAY);
57417b36120SJosef Bacik 			break;
57517b36120SJosef Bacik 		default:
57617b36120SJosef Bacik 			btrfs_info(NULL, "unrecognized rescue option '%s'",
57717b36120SJosef Bacik 				   param->key);
57817b36120SJosef Bacik 			return -EINVAL;
57917b36120SJosef Bacik 		}
58017b36120SJosef Bacik 		break;
58117b36120SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
58217b36120SJosef Bacik 	case Opt_fragment:
58317b36120SJosef Bacik 		switch (result.uint_32) {
58417b36120SJosef Bacik 		case Opt_fragment_parameter_all:
58517b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, FRAGMENT_DATA);
58617b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, FRAGMENT_METADATA);
58717b36120SJosef Bacik 			break;
58817b36120SJosef Bacik 		case Opt_fragment_parameter_metadata:
58917b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, FRAGMENT_METADATA);
59017b36120SJosef Bacik 			break;
59117b36120SJosef Bacik 		case Opt_fragment_parameter_data:
59217b36120SJosef Bacik 			btrfs_set_opt(ctx->mount_opt, FRAGMENT_DATA);
59317b36120SJosef Bacik 			break;
59417b36120SJosef Bacik 		default:
59517b36120SJosef Bacik 			btrfs_info(NULL, "unrecognized fragment option '%s'",
59617b36120SJosef Bacik 				   param->key);
59717b36120SJosef Bacik 			return -EINVAL;
59817b36120SJosef Bacik 		}
59917b36120SJosef Bacik 		break;
60017b36120SJosef Bacik #endif
60117b36120SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
60217b36120SJosef Bacik 	case Opt_ref_verify:
60317b36120SJosef Bacik 		btrfs_set_opt(ctx->mount_opt, REF_VERIFY);
60417b36120SJosef Bacik 		break;
60517b36120SJosef Bacik #endif
60617b36120SJosef Bacik 	default:
60717b36120SJosef Bacik 		btrfs_err(NULL, "unrecognized mount option '%s'", param->key);
60817b36120SJosef Bacik 		return -EINVAL;
60917b36120SJosef Bacik 	}
61017b36120SJosef Bacik 
61117b36120SJosef Bacik 	return 0;
61217b36120SJosef Bacik }
61317b36120SJosef Bacik 
61483e3a40aSJosef Bacik /*
61583e3a40aSJosef Bacik  * Some options only have meaning at mount time and shouldn't persist across
61683e3a40aSJosef Bacik  * remounts, or be displayed. Clear these at the end of mount and remount code
61783e3a40aSJosef Bacik  * paths.
61883e3a40aSJosef Bacik  */
61983e3a40aSJosef Bacik static void btrfs_clear_oneshot_options(struct btrfs_fs_info *fs_info)
62083e3a40aSJosef Bacik {
62183e3a40aSJosef Bacik 	btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT);
62283e3a40aSJosef Bacik 	btrfs_clear_opt(fs_info->mount_opt, CLEAR_CACHE);
62383e3a40aSJosef Bacik 	btrfs_clear_opt(fs_info->mount_opt, NOSPACECACHE);
62483e3a40aSJosef Bacik }
62583e3a40aSJosef Bacik 
626eddb1a43SJosef Bacik static bool check_ro_option(struct btrfs_fs_info *fs_info,
627eddb1a43SJosef Bacik 			    unsigned long mount_opt, unsigned long opt,
628d70bf748SJosef Bacik 			    const char *opt_name)
629d70bf748SJosef Bacik {
630eddb1a43SJosef Bacik 	if (mount_opt & opt) {
631d70bf748SJosef Bacik 		btrfs_err(fs_info, "%s must be used with ro mount option",
632d70bf748SJosef Bacik 			  opt_name);
633d70bf748SJosef Bacik 		return true;
634d70bf748SJosef Bacik 	}
635d70bf748SJosef Bacik 	return false;
636d70bf748SJosef Bacik }
637d70bf748SJosef Bacik 
638ad21f15bSJosef Bacik bool btrfs_check_options(struct btrfs_fs_info *info, unsigned long *mount_opt,
639eddb1a43SJosef Bacik 			 unsigned long flags)
64074ef0018SQu Wenruo {
6412b41b19dSJosef Bacik 	bool ret = true;
64274ef0018SQu Wenruo 
6432b41b19dSJosef Bacik 	if (!(flags & SB_RDONLY) &&
644eddb1a43SJosef Bacik 	    (check_ro_option(info, *mount_opt, BTRFS_MOUNT_NOLOGREPLAY, "nologreplay") ||
645eddb1a43SJosef Bacik 	     check_ro_option(info, *mount_opt, BTRFS_MOUNT_IGNOREBADROOTS, "ignorebadroots") ||
646eddb1a43SJosef Bacik 	     check_ro_option(info, *mount_opt, BTRFS_MOUNT_IGNOREDATACSUMS, "ignoredatacsums")))
6472b41b19dSJosef Bacik 		ret = false;
64874ef0018SQu Wenruo 
6490b246afaSJeff Mahoney 	if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) &&
650eddb1a43SJosef Bacik 	    !btrfs_raw_test_opt(*mount_opt, FREE_SPACE_TREE) &&
651eddb1a43SJosef Bacik 	    !btrfs_raw_test_opt(*mount_opt, CLEAR_CACHE)) {
6522b41b19dSJosef Bacik 		btrfs_err(info, "cannot disable free-space-tree");
6532b41b19dSJosef Bacik 		ret = false;
65464b5d5b2SQu Wenruo 	}
65564b5d5b2SQu Wenruo 	if (btrfs_fs_compat_ro(info, BLOCK_GROUP_TREE) &&
656eddb1a43SJosef Bacik 	     !btrfs_raw_test_opt(*mount_opt, FREE_SPACE_TREE)) {
6572b41b19dSJosef Bacik 		btrfs_err(info, "cannot disable free-space-tree with block-group-tree feature");
6582b41b19dSJosef Bacik 		ret = false;
65970f6d82eSOmar Sandoval 	}
6602b41b19dSJosef Bacik 
661eddb1a43SJosef Bacik 	if (btrfs_check_mountopts_zoned(info, mount_opt))
6622b41b19dSJosef Bacik 		ret = false;
6632b41b19dSJosef Bacik 
6642b41b19dSJosef Bacik 	if (!test_bit(BTRFS_FS_STATE_REMOUNTING, &info->fs_state)) {
665eddb1a43SJosef Bacik 		if (btrfs_raw_test_opt(*mount_opt, SPACE_CACHE))
6660b246afaSJeff Mahoney 			btrfs_info(info, "disk space caching is enabled");
667eddb1a43SJosef Bacik 		if (btrfs_raw_test_opt(*mount_opt, FREE_SPACE_TREE))
6682b41b19dSJosef Bacik 			btrfs_info(info, "using free-space-tree");
669dbecac26SMaciej S. Szmigiero 	}
6702b41b19dSJosef Bacik 
671a7a3f7caSSage Weil 	return ret;
672edf24abeSChristoph Hellwig }
673edf24abeSChristoph Hellwig 
674edf24abeSChristoph Hellwig /*
675a6a8f22aSJosef Bacik  * This is subtle, we only call this during open_ctree().  We need to pre-load
676a6a8f22aSJosef Bacik  * the mount options with the on-disk settings.  Before the new mount API took
677a6a8f22aSJosef Bacik  * effect we would do this on mount and remount.  With the new mount API we'll
678a6a8f22aSJosef Bacik  * only do this on the initial mount.
679edf24abeSChristoph Hellwig  *
680a6a8f22aSJosef Bacik  * This isn't a change in behavior, because we're using the current state of the
681a6a8f22aSJosef Bacik  * file system to set the current mount options.  If you mounted with special
682a6a8f22aSJosef Bacik  * options to disable these features and then remounted we wouldn't revert the
683a6a8f22aSJosef Bacik  * settings, because mounting without these features cleared the on-disk
684a6a8f22aSJosef Bacik  * settings, so this being called on re-mount is not needed.
685edf24abeSChristoph Hellwig  */
686a6a8f22aSJosef Bacik void btrfs_set_free_space_cache_settings(struct btrfs_fs_info *fs_info)
687edf24abeSChristoph Hellwig {
688a6a8f22aSJosef Bacik 	if (fs_info->sectorsize < PAGE_SIZE) {
689a6a8f22aSJosef Bacik 		btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
690a6a8f22aSJosef Bacik 		if (!btrfs_test_opt(fs_info, FREE_SPACE_TREE)) {
691a6a8f22aSJosef Bacik 			btrfs_info(fs_info,
692a6a8f22aSJosef Bacik 				   "forcing free space tree for sector size %u with page size %lu",
693a6a8f22aSJosef Bacik 				   fs_info->sectorsize, PAGE_SIZE);
694a6a8f22aSJosef Bacik 			btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
695a6a8f22aSJosef Bacik 		}
696a6a8f22aSJosef Bacik 	}
697d7407606SMisono, Tomohiro 
698d7407606SMisono, Tomohiro 	/*
699ad21f15bSJosef Bacik 	 * At this point our mount options are populated, so we only mess with
700ad21f15bSJosef Bacik 	 * these settings if we don't have any settings already.
701d7407606SMisono, Tomohiro 	 */
702ad21f15bSJosef Bacik 	if (btrfs_test_opt(fs_info, FREE_SPACE_TREE))
703ad21f15bSJosef Bacik 		return;
704d7407606SMisono, Tomohiro 
705ad21f15bSJosef Bacik 	if (btrfs_is_zoned(fs_info) &&
706ad21f15bSJosef Bacik 	    btrfs_free_space_cache_v1_active(fs_info)) {
707ad21f15bSJosef Bacik 		btrfs_info(fs_info, "zoned: clearing existing space cache");
708ad21f15bSJosef Bacik 		btrfs_set_super_cache_generation(fs_info->super_copy, 0);
709ad21f15bSJosef Bacik 		return;
71036350e95SGu Jinxiang 	}
711d7407606SMisono, Tomohiro 
712ad21f15bSJosef Bacik 	if (btrfs_test_opt(fs_info, SPACE_CACHE))
713ad21f15bSJosef Bacik 		return;
714ad21f15bSJosef Bacik 
715ad21f15bSJosef Bacik 	if (btrfs_test_opt(fs_info, NOSPACECACHE))
716ad21f15bSJosef Bacik 		return;
717d7407606SMisono, Tomohiro 
718d7407606SMisono, Tomohiro 	/*
719ad21f15bSJosef Bacik 	 * At this point we don't have explicit options set by the user, set
720ad21f15bSJosef Bacik 	 * them ourselves based on the state of the file system.
721ad21f15bSJosef Bacik 	 */
722ad21f15bSJosef Bacik 	if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
723ad21f15bSJosef Bacik 		btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
724ad21f15bSJosef Bacik 	else if (btrfs_free_space_cache_v1_active(fs_info))
725ad21f15bSJosef Bacik 		btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE);
726a6a8f22aSJosef Bacik }
727a6a8f22aSJosef Bacik 
72841d46b29SJosef Bacik static void set_device_specific_options(struct btrfs_fs_info *fs_info)
72941d46b29SJosef Bacik {
73041d46b29SJosef Bacik 	if (!btrfs_test_opt(fs_info, NOSSD) &&
73141d46b29SJosef Bacik 	    !fs_info->fs_devices->rotating)
73241d46b29SJosef Bacik 		btrfs_set_opt(fs_info->mount_opt, SSD);
73341d46b29SJosef Bacik 
73441d46b29SJosef Bacik 	/*
73541d46b29SJosef Bacik 	 * For devices supporting discard turn on discard=async automatically,
73641d46b29SJosef Bacik 	 * unless it's already set or disabled. This could be turned off by
73741d46b29SJosef Bacik 	 * nodiscard for the same mount.
738d7407606SMisono, Tomohiro 	 *
73941d46b29SJosef Bacik 	 * The zoned mode piggy backs on the discard functionality for
74041d46b29SJosef Bacik 	 * resetting a zone. There is no reason to delay the zone reset as it is
74141d46b29SJosef Bacik 	 * fast enough. So, do not enable async discard for zoned mode.
742d7407606SMisono, Tomohiro 	 */
74341d46b29SJosef Bacik 	if (!(btrfs_test_opt(fs_info, DISCARD_SYNC) ||
74441d46b29SJosef Bacik 	      btrfs_test_opt(fs_info, DISCARD_ASYNC) ||
74541d46b29SJosef Bacik 	      btrfs_test_opt(fs_info, NODISCARD)) &&
74641d46b29SJosef Bacik 	    fs_info->fs_devices->discardable &&
74741d46b29SJosef Bacik 	    !btrfs_is_zoned(fs_info))
74841d46b29SJosef Bacik 		btrfs_set_opt(fs_info->mount_opt, DISCARD_ASYNC);
74995e05289SChris Mason }
75095e05289SChris Mason 
751c0c907a4SMarcos Paulo de Souza char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
75273f73415SJosef Bacik 					  u64 subvol_objectid)
75373f73415SJosef Bacik {
754815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
7555168489aSJosef Bacik 	struct btrfs_root *fs_root = NULL;
75605dbe683SOmar Sandoval 	struct btrfs_root_ref *root_ref;
75705dbe683SOmar Sandoval 	struct btrfs_inode_ref *inode_ref;
75805dbe683SOmar Sandoval 	struct btrfs_key key;
75905dbe683SOmar Sandoval 	struct btrfs_path *path = NULL;
76005dbe683SOmar Sandoval 	char *name = NULL, *ptr;
76105dbe683SOmar Sandoval 	u64 dirid;
76205dbe683SOmar Sandoval 	int len;
76305dbe683SOmar Sandoval 	int ret;
76405dbe683SOmar Sandoval 
76505dbe683SOmar Sandoval 	path = btrfs_alloc_path();
76605dbe683SOmar Sandoval 	if (!path) {
76705dbe683SOmar Sandoval 		ret = -ENOMEM;
76805dbe683SOmar Sandoval 		goto err;
76905dbe683SOmar Sandoval 	}
77005dbe683SOmar Sandoval 
7713ec83621SDavid Sterba 	name = kmalloc(PATH_MAX, GFP_KERNEL);
77205dbe683SOmar Sandoval 	if (!name) {
77305dbe683SOmar Sandoval 		ret = -ENOMEM;
77405dbe683SOmar Sandoval 		goto err;
77505dbe683SOmar Sandoval 	}
77605dbe683SOmar Sandoval 	ptr = name + PATH_MAX - 1;
77705dbe683SOmar Sandoval 	ptr[0] = '\0';
77805dbe683SOmar Sandoval 
77905dbe683SOmar Sandoval 	/*
78005dbe683SOmar Sandoval 	 * Walk up the subvolume trees in the tree of tree roots by root
78105dbe683SOmar Sandoval 	 * backrefs until we hit the top-level subvolume.
78205dbe683SOmar Sandoval 	 */
78305dbe683SOmar Sandoval 	while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
78405dbe683SOmar Sandoval 		key.objectid = subvol_objectid;
78505dbe683SOmar Sandoval 		key.type = BTRFS_ROOT_BACKREF_KEY;
78605dbe683SOmar Sandoval 		key.offset = (u64)-1;
78705dbe683SOmar Sandoval 
7880ff40a91SMarcos Paulo de Souza 		ret = btrfs_search_backwards(root, &key, path);
78905dbe683SOmar Sandoval 		if (ret < 0) {
79005dbe683SOmar Sandoval 			goto err;
79105dbe683SOmar Sandoval 		} else if (ret > 0) {
79205dbe683SOmar Sandoval 			ret = -ENOENT;
79305dbe683SOmar Sandoval 			goto err;
79405dbe683SOmar Sandoval 		}
79505dbe683SOmar Sandoval 
79605dbe683SOmar Sandoval 		subvol_objectid = key.offset;
79705dbe683SOmar Sandoval 
79805dbe683SOmar Sandoval 		root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
79905dbe683SOmar Sandoval 					  struct btrfs_root_ref);
80005dbe683SOmar Sandoval 		len = btrfs_root_ref_name_len(path->nodes[0], root_ref);
80105dbe683SOmar Sandoval 		ptr -= len + 1;
80205dbe683SOmar Sandoval 		if (ptr < name) {
80305dbe683SOmar Sandoval 			ret = -ENAMETOOLONG;
80405dbe683SOmar Sandoval 			goto err;
80505dbe683SOmar Sandoval 		}
80605dbe683SOmar Sandoval 		read_extent_buffer(path->nodes[0], ptr + 1,
80705dbe683SOmar Sandoval 				   (unsigned long)(root_ref + 1), len);
80805dbe683SOmar Sandoval 		ptr[0] = '/';
80905dbe683SOmar Sandoval 		dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);
81005dbe683SOmar Sandoval 		btrfs_release_path(path);
81105dbe683SOmar Sandoval 
81256e9357aSDavid Sterba 		fs_root = btrfs_get_fs_root(fs_info, subvol_objectid, true);
81305dbe683SOmar Sandoval 		if (IS_ERR(fs_root)) {
81405dbe683SOmar Sandoval 			ret = PTR_ERR(fs_root);
8155168489aSJosef Bacik 			fs_root = NULL;
8165168489aSJosef Bacik 			goto err;
8175168489aSJosef Bacik 		}
81805dbe683SOmar Sandoval 
81905dbe683SOmar Sandoval 		/*
82005dbe683SOmar Sandoval 		 * Walk up the filesystem tree by inode refs until we hit the
82105dbe683SOmar Sandoval 		 * root directory.
82205dbe683SOmar Sandoval 		 */
82305dbe683SOmar Sandoval 		while (dirid != BTRFS_FIRST_FREE_OBJECTID) {
82405dbe683SOmar Sandoval 			key.objectid = dirid;
82505dbe683SOmar Sandoval 			key.type = BTRFS_INODE_REF_KEY;
82605dbe683SOmar Sandoval 			key.offset = (u64)-1;
82705dbe683SOmar Sandoval 
8280ff40a91SMarcos Paulo de Souza 			ret = btrfs_search_backwards(fs_root, &key, path);
82905dbe683SOmar Sandoval 			if (ret < 0) {
83005dbe683SOmar Sandoval 				goto err;
83105dbe683SOmar Sandoval 			} else if (ret > 0) {
83205dbe683SOmar Sandoval 				ret = -ENOENT;
83305dbe683SOmar Sandoval 				goto err;
83405dbe683SOmar Sandoval 			}
83505dbe683SOmar Sandoval 
83605dbe683SOmar Sandoval 			dirid = key.offset;
83705dbe683SOmar Sandoval 
83805dbe683SOmar Sandoval 			inode_ref = btrfs_item_ptr(path->nodes[0],
83905dbe683SOmar Sandoval 						   path->slots[0],
84005dbe683SOmar Sandoval 						   struct btrfs_inode_ref);
84105dbe683SOmar Sandoval 			len = btrfs_inode_ref_name_len(path->nodes[0],
84205dbe683SOmar Sandoval 						       inode_ref);
84305dbe683SOmar Sandoval 			ptr -= len + 1;
84405dbe683SOmar Sandoval 			if (ptr < name) {
84505dbe683SOmar Sandoval 				ret = -ENAMETOOLONG;
84605dbe683SOmar Sandoval 				goto err;
84705dbe683SOmar Sandoval 			}
84805dbe683SOmar Sandoval 			read_extent_buffer(path->nodes[0], ptr + 1,
84905dbe683SOmar Sandoval 					   (unsigned long)(inode_ref + 1), len);
85005dbe683SOmar Sandoval 			ptr[0] = '/';
85105dbe683SOmar Sandoval 			btrfs_release_path(path);
85205dbe683SOmar Sandoval 		}
85300246528SJosef Bacik 		btrfs_put_root(fs_root);
8545168489aSJosef Bacik 		fs_root = NULL;
85505dbe683SOmar Sandoval 	}
85605dbe683SOmar Sandoval 
85705dbe683SOmar Sandoval 	btrfs_free_path(path);
85805dbe683SOmar Sandoval 	if (ptr == name + PATH_MAX - 1) {
85905dbe683SOmar Sandoval 		name[0] = '/';
86005dbe683SOmar Sandoval 		name[1] = '\0';
86105dbe683SOmar Sandoval 	} else {
86205dbe683SOmar Sandoval 		memmove(name, ptr, name + PATH_MAX - ptr);
86305dbe683SOmar Sandoval 	}
86405dbe683SOmar Sandoval 	return name;
86505dbe683SOmar Sandoval 
86605dbe683SOmar Sandoval err:
86700246528SJosef Bacik 	btrfs_put_root(fs_root);
86805dbe683SOmar Sandoval 	btrfs_free_path(path);
86905dbe683SOmar Sandoval 	kfree(name);
87005dbe683SOmar Sandoval 	return ERR_PTR(ret);
87105dbe683SOmar Sandoval }
87205dbe683SOmar Sandoval 
87305dbe683SOmar Sandoval static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid)
87405dbe683SOmar Sandoval {
87505dbe683SOmar Sandoval 	struct btrfs_root *root = fs_info->tree_root;
87673f73415SJosef Bacik 	struct btrfs_dir_item *di;
87773f73415SJosef Bacik 	struct btrfs_path *path;
87873f73415SJosef Bacik 	struct btrfs_key location;
8796db75318SSweet Tea Dorminy 	struct fscrypt_str name = FSTR_INIT("default", 7);
88073f73415SJosef Bacik 	u64 dir_id;
88173f73415SJosef Bacik 
88273f73415SJosef Bacik 	path = btrfs_alloc_path();
88373f73415SJosef Bacik 	if (!path)
88405dbe683SOmar Sandoval 		return -ENOMEM;
88573f73415SJosef Bacik 
88673f73415SJosef Bacik 	/*
88773f73415SJosef Bacik 	 * Find the "default" dir item which points to the root item that we
88873f73415SJosef Bacik 	 * will mount by default if we haven't been given a specific subvolume
88973f73415SJosef Bacik 	 * to mount.
89073f73415SJosef Bacik 	 */
891815745cfSAl Viro 	dir_id = btrfs_super_root_dir(fs_info->super_copy);
892e43eec81SSweet Tea Dorminy 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, &name, 0);
893b0839166SJulia Lawall 	if (IS_ERR(di)) {
894b0839166SJulia Lawall 		btrfs_free_path(path);
89505dbe683SOmar Sandoval 		return PTR_ERR(di);
896b0839166SJulia Lawall 	}
89773f73415SJosef Bacik 	if (!di) {
89873f73415SJosef Bacik 		/*
89973f73415SJosef Bacik 		 * Ok the default dir item isn't there.  This is weird since
90073f73415SJosef Bacik 		 * it's always been there, but don't freak out, just try and
90105dbe683SOmar Sandoval 		 * mount the top-level subvolume.
90273f73415SJosef Bacik 		 */
90373f73415SJosef Bacik 		btrfs_free_path(path);
90405dbe683SOmar Sandoval 		*objectid = BTRFS_FS_TREE_OBJECTID;
90505dbe683SOmar Sandoval 		return 0;
90673f73415SJosef Bacik 	}
90773f73415SJosef Bacik 
90873f73415SJosef Bacik 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
90973f73415SJosef Bacik 	btrfs_free_path(path);
91005dbe683SOmar Sandoval 	*objectid = location.objectid;
91105dbe683SOmar Sandoval 	return 0;
91273f73415SJosef Bacik }
91373f73415SJosef Bacik 
9148a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb,
9158a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
91656e033a7SDavid Sterba 			    void *data)
9172e635a27SChris Mason {
9182e635a27SChris Mason 	struct inode *inode;
919815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
92039279cc3SChris Mason 	int err;
9212e635a27SChris Mason 
9222e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
9232e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
924e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
925af53d29aSAl Viro 	sb->s_d_op = &btrfs_dentry_operations;
926be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
92714605409SBoris Burkov #ifdef CONFIG_FS_VERITY
92814605409SBoris Burkov 	sb->s_vop = &btrfs_verityops;
92914605409SBoris Burkov #endif
9305103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
9312e635a27SChris Mason 	sb->s_time_gran = 1;
932da2f0f74SChris Mason 	sb->s_iflags |= SB_I_CGROUPWB;
9339e11ceeeSJan Kara 
9349e11ceeeSJan Kara 	err = super_setup_bdi(sb);
9359e11ceeeSJan Kara 	if (err) {
9369e11ceeeSJan Kara 		btrfs_err(fs_info, "super_setup_bdi failed");
9379e11ceeeSJan Kara 		return err;
9389e11ceeeSJan Kara 	}
9399e11ceeeSJan Kara 
940ad2b2c80SAl Viro 	err = open_ctree(sb, fs_devices, (char *)data);
941ad2b2c80SAl Viro 	if (err) {
942ab8d0fc4SJeff Mahoney 		btrfs_err(fs_info, "open_ctree failed");
943ad2b2c80SAl Viro 		return err;
944e20d96d6SChris Mason 	}
945b888db2bSChris Mason 
9460202e83fSDavid Sterba 	inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, fs_info->fs_root);
9475d4f98a2SYan Zheng 	if (IS_ERR(inode)) {
9485d4f98a2SYan Zheng 		err = PTR_ERR(inode);
94913b98989SJosef Bacik 		btrfs_handle_fs_error(fs_info, err, NULL);
95039279cc3SChris Mason 		goto fail_close;
95139279cc3SChris Mason 	}
9522e635a27SChris Mason 
95348fde701SAl Viro 	sb->s_root = d_make_root(inode);
95448fde701SAl Viro 	if (!sb->s_root) {
95539279cc3SChris Mason 		err = -ENOMEM;
95639279cc3SChris Mason 		goto fail_close;
9572e635a27SChris Mason 	}
95858176a96SJosef Bacik 
9591751e8a6SLinus Torvalds 	sb->s_flags |= SB_ACTIVE;
9602e635a27SChris Mason 	return 0;
9612e635a27SChris Mason 
96239279cc3SChris Mason fail_close:
9636bccf3abSJeff Mahoney 	close_ctree(fs_info);
964d5719762SChris Mason 	return err;
965d5719762SChris Mason }
966d5719762SChris Mason 
9676bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
968d5719762SChris Mason {
969d5719762SChris Mason 	struct btrfs_trans_handle *trans;
970815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
971815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
972df2ce34cSChris Mason 
973bc074524SJeff Mahoney 	trace_btrfs_sync_fs(fs_info, wait);
9741abe9b8aSliubo 
975d561c025SChris Mason 	if (!wait) {
976815745cfSAl Viro 		filemap_flush(fs_info->btree_inode->i_mapping);
977df2ce34cSChris Mason 		return 0;
978d561c025SChris Mason 	}
979771ed689SChris Mason 
9806374e57aSChris Mason 	btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
981771ed689SChris Mason 
982d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
98360376ce4SJosef Bacik 	if (IS_ERR(trans)) {
984354aa0fbSMiao Xie 		/* no transaction, don't bother */
9856b5fe46dSDavid Sterba 		if (PTR_ERR(trans) == -ENOENT) {
9866b5fe46dSDavid Sterba 			/*
9876b5fe46dSDavid Sterba 			 * Exit unless we have some pending changes
9886b5fe46dSDavid Sterba 			 * that need to go through commit
9896b5fe46dSDavid Sterba 			 */
990c52cc7b7SJosef Bacik 			if (!test_bit(BTRFS_FS_NEED_TRANS_COMMIT,
991c52cc7b7SJosef Bacik 				      &fs_info->flags))
992bd7de2c9SJosef Bacik 				return 0;
993a53f4f8eSQu Wenruo 			/*
994a53f4f8eSQu Wenruo 			 * A non-blocking test if the fs is frozen. We must not
995a53f4f8eSQu Wenruo 			 * start a new transaction here otherwise a deadlock
996a53f4f8eSQu Wenruo 			 * happens. The pending operations are delayed to the
997a53f4f8eSQu Wenruo 			 * next commit after thawing.
998a53f4f8eSQu Wenruo 			 */
999a7e3c5f2SRakesh Pandit 			if (sb_start_write_trylock(sb))
1000a7e3c5f2SRakesh Pandit 				sb_end_write(sb);
1001a53f4f8eSQu Wenruo 			else
1002a53f4f8eSQu Wenruo 				return 0;
10036b5fe46dSDavid Sterba 			trans = btrfs_start_transaction(root, 0);
100460376ce4SJosef Bacik 		}
100598bd5c54SDavid Sterba 		if (IS_ERR(trans))
100698bd5c54SDavid Sterba 			return PTR_ERR(trans);
10076b5fe46dSDavid Sterba 	}
10083a45bb20SJeff Mahoney 	return btrfs_commit_transaction(trans);
1009d5719762SChris Mason }
1010d5719762SChris Mason 
1011ab0b4a3eSJosef Bacik static void print_rescue_option(struct seq_file *seq, const char *s, bool *printed)
1012ab0b4a3eSJosef Bacik {
1013ab0b4a3eSJosef Bacik 	seq_printf(seq, "%s%s", (*printed) ? ":" : ",rescue=", s);
1014ab0b4a3eSJosef Bacik 	*printed = true;
1015ab0b4a3eSJosef Bacik }
1016ab0b4a3eSJosef Bacik 
101734c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
1018a9572a15SEric Paris {
1019815745cfSAl Viro 	struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
10200f628c63SDavid Sterba 	const char *compress_type;
10213ef3959bSJosef Bacik 	const char *subvol_name;
1022ab0b4a3eSJosef Bacik 	bool printed = false;
1023a9572a15SEric Paris 
10243cdde224SJeff Mahoney 	if (btrfs_test_opt(info, DEGRADED))
1025a9572a15SEric Paris 		seq_puts(seq, ",degraded");
10263cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NODATASUM))
1027a9572a15SEric Paris 		seq_puts(seq, ",nodatasum");
10283cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NODATACOW))
1029a9572a15SEric Paris 		seq_puts(seq, ",nodatacow");
10303cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOBARRIER))
1031a9572a15SEric Paris 		seq_puts(seq, ",nobarrier");
103295ac567aSFilipe David Borba Manana 	if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
1033c1c9ff7cSGeert Uytterhoeven 		seq_printf(seq, ",max_inline=%llu", info->max_inline);
1034a9572a15SEric Paris 	if (info->thread_pool_size !=  min_t(unsigned long,
1035a9572a15SEric Paris 					     num_online_cpus() + 2, 8))
1036f7b885beSAnand Jain 		seq_printf(seq, ",thread_pool=%u", info->thread_pool_size);
10373cdde224SJeff Mahoney 	if (btrfs_test_opt(info, COMPRESS)) {
10380f628c63SDavid Sterba 		compress_type = btrfs_compress_type2str(info->compress_type);
10393cdde224SJeff Mahoney 		if (btrfs_test_opt(info, FORCE_COMPRESS))
1040200da64eSTsutomu Itoh 			seq_printf(seq, ",compress-force=%s", compress_type);
1041200da64eSTsutomu Itoh 		else
1042200da64eSTsutomu Itoh 			seq_printf(seq, ",compress=%s", compress_type);
1043f51d2b59SDavid Sterba 		if (info->compress_level)
1044fa4d885aSAdam Borowski 			seq_printf(seq, ":%d", info->compress_level);
1045200da64eSTsutomu Itoh 	}
10463cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOSSD))
1047c289811cSChris Mason 		seq_puts(seq, ",nossd");
10483cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SSD_SPREAD))
1049451d7585SChris Mason 		seq_puts(seq, ",ssd_spread");
10503cdde224SJeff Mahoney 	else if (btrfs_test_opt(info, SSD))
1051a9572a15SEric Paris 		seq_puts(seq, ",ssd");
10523cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOTREELOG))
10536b65c5c6SSage Weil 		seq_puts(seq, ",notreelog");
10543cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOLOGREPLAY))
1055ab0b4a3eSJosef Bacik 		print_rescue_option(seq, "nologreplay", &printed);
105668319c18SJosef Bacik 	if (btrfs_test_opt(info, USEBACKUPROOT))
105768319c18SJosef Bacik 		print_rescue_option(seq, "usebackuproot", &printed);
105842437a63SJosef Bacik 	if (btrfs_test_opt(info, IGNOREBADROOTS))
105942437a63SJosef Bacik 		print_rescue_option(seq, "ignorebadroots", &printed);
1060882dbe0cSJosef Bacik 	if (btrfs_test_opt(info, IGNOREDATACSUMS))
1061882dbe0cSJosef Bacik 		print_rescue_option(seq, "ignoredatacsums", &printed);
10623cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FLUSHONCOMMIT))
10636b65c5c6SSage Weil 		seq_puts(seq, ",flushoncommit");
106446b27f50SDennis Zhou 	if (btrfs_test_opt(info, DISCARD_SYNC))
106520a5239aSMatthew Wilcox 		seq_puts(seq, ",discard");
1066b0643e59SDennis Zhou 	if (btrfs_test_opt(info, DISCARD_ASYNC))
1067b0643e59SDennis Zhou 		seq_puts(seq, ",discard=async");
10681751e8a6SLinus Torvalds 	if (!(info->sb->s_flags & SB_POSIXACL))
1069a9572a15SEric Paris 		seq_puts(seq, ",noacl");
107004c41559SBoris Burkov 	if (btrfs_free_space_cache_v1_active(info))
1071200da64eSTsutomu Itoh 		seq_puts(seq, ",space_cache");
107204c41559SBoris Burkov 	else if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
107370f6d82eSOmar Sandoval 		seq_puts(seq, ",space_cache=v2");
107473bc1876SJosef Bacik 	else
10758965593eSDavid Sterba 		seq_puts(seq, ",nospace_cache");
10763cdde224SJeff Mahoney 	if (btrfs_test_opt(info, RESCAN_UUID_TREE))
1077f420ee1eSStefan Behrens 		seq_puts(seq, ",rescan_uuid_tree");
10783cdde224SJeff Mahoney 	if (btrfs_test_opt(info, CLEAR_CACHE))
1079200da64eSTsutomu Itoh 		seq_puts(seq, ",clear_cache");
10803cdde224SJeff Mahoney 	if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED))
1081200da64eSTsutomu Itoh 		seq_puts(seq, ",user_subvol_rm_allowed");
10823cdde224SJeff Mahoney 	if (btrfs_test_opt(info, ENOSPC_DEBUG))
10830942caa3SDavid Sterba 		seq_puts(seq, ",enospc_debug");
10843cdde224SJeff Mahoney 	if (btrfs_test_opt(info, AUTO_DEFRAG))
10850942caa3SDavid Sterba 		seq_puts(seq, ",autodefrag");
10863cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SKIP_BALANCE))
10879555c6c1SIlya Dryomov 		seq_puts(seq, ",skip_balance");
10888507d216SWang Shilong 	if (info->metadata_ratio)
1089764cb8b4SAnand Jain 		seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio);
10903cdde224SJeff Mahoney 	if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR))
10918c342930SJeff Mahoney 		seq_puts(seq, ",fatal_errors=panic");
10928b87dc17SDavid Sterba 	if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
1093d3740608SAnand Jain 		seq_printf(seq, ",commit=%u", info->commit_interval);
1094d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
10953cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FRAGMENT_DATA))
1096d0bd4560SJosef Bacik 		seq_puts(seq, ",fragment=data");
10973cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FRAGMENT_METADATA))
1098d0bd4560SJosef Bacik 		seq_puts(seq, ",fragment=metadata");
1099d0bd4560SJosef Bacik #endif
1100fb592373SJosef Bacik 	if (btrfs_test_opt(info, REF_VERIFY))
1101fb592373SJosef Bacik 		seq_puts(seq, ",ref_verify");
1102c8d3fe02SOmar Sandoval 	seq_printf(seq, ",subvolid=%llu",
1103c8d3fe02SOmar Sandoval 		  BTRFS_I(d_inode(dentry))->root->root_key.objectid);
11043ef3959bSJosef Bacik 	subvol_name = btrfs_get_subvol_name_from_objectid(info,
11053ef3959bSJosef Bacik 			BTRFS_I(d_inode(dentry))->root->root_key.objectid);
11063ef3959bSJosef Bacik 	if (!IS_ERR(subvol_name)) {
1107c8d3fe02SOmar Sandoval 		seq_puts(seq, ",subvol=");
11083ef3959bSJosef Bacik 		seq_escape(seq, subvol_name, " \t\n\\");
11093ef3959bSJosef Bacik 		kfree(subvol_name);
11103ef3959bSJosef Bacik 	}
1111a9572a15SEric Paris 	return 0;
1112a9572a15SEric Paris }
1113a9572a15SEric Paris 
1114830c4adbSJosef Bacik /*
1115f9d9ef62SDavid Sterba  * subvolumes are identified by ino 256
1116f9d9ef62SDavid Sterba  */
1117f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode)
1118f9d9ef62SDavid Sterba {
1119f9d9ef62SDavid Sterba 	if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
1120f9d9ef62SDavid Sterba 		return 1;
1121f9d9ef62SDavid Sterba 	return 0;
1122f9d9ef62SDavid Sterba }
1123f9d9ef62SDavid Sterba 
1124bb289b7bSOmar Sandoval static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
1125ae0bc863SAnand Jain 				   struct vfsmount *mnt)
1126830c4adbSJosef Bacik {
1127830c4adbSJosef Bacik 	struct dentry *root;
1128fa330659SOmar Sandoval 	int ret;
1129830c4adbSJosef Bacik 
113005dbe683SOmar Sandoval 	if (!subvol_name) {
113105dbe683SOmar Sandoval 		if (!subvol_objectid) {
113205dbe683SOmar Sandoval 			ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
113305dbe683SOmar Sandoval 							  &subvol_objectid);
113405dbe683SOmar Sandoval 			if (ret) {
113505dbe683SOmar Sandoval 				root = ERR_PTR(ret);
113605dbe683SOmar Sandoval 				goto out;
113705dbe683SOmar Sandoval 			}
113805dbe683SOmar Sandoval 		}
1139c0c907a4SMarcos Paulo de Souza 		subvol_name = btrfs_get_subvol_name_from_objectid(
1140c0c907a4SMarcos Paulo de Souza 					btrfs_sb(mnt->mnt_sb), subvol_objectid);
114105dbe683SOmar Sandoval 		if (IS_ERR(subvol_name)) {
114205dbe683SOmar Sandoval 			root = ERR_CAST(subvol_name);
114305dbe683SOmar Sandoval 			subvol_name = NULL;
114405dbe683SOmar Sandoval 			goto out;
114505dbe683SOmar Sandoval 		}
114605dbe683SOmar Sandoval 
114705dbe683SOmar Sandoval 	}
114805dbe683SOmar Sandoval 
1149ea441d11SAl Viro 	root = mount_subtree(mnt, subvol_name);
1150fa330659SOmar Sandoval 	/* mount_subtree() drops our reference on the vfsmount. */
1151fa330659SOmar Sandoval 	mnt = NULL;
1152830c4adbSJosef Bacik 
1153bb289b7bSOmar Sandoval 	if (!IS_ERR(root)) {
1154ea441d11SAl Viro 		struct super_block *s = root->d_sb;
1155ab8d0fc4SJeff Mahoney 		struct btrfs_fs_info *fs_info = btrfs_sb(s);
1156bb289b7bSOmar Sandoval 		struct inode *root_inode = d_inode(root);
1157bb289b7bSOmar Sandoval 		u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid;
1158bb289b7bSOmar Sandoval 
1159bb289b7bSOmar Sandoval 		ret = 0;
1160bb289b7bSOmar Sandoval 		if (!is_subvolume_inode(root_inode)) {
1161ab8d0fc4SJeff Mahoney 			btrfs_err(fs_info, "'%s' is not a valid subvolume",
1162bb289b7bSOmar Sandoval 			       subvol_name);
1163bb289b7bSOmar Sandoval 			ret = -EINVAL;
1164bb289b7bSOmar Sandoval 		}
1165bb289b7bSOmar Sandoval 		if (subvol_objectid && root_objectid != subvol_objectid) {
116605dbe683SOmar Sandoval 			/*
116705dbe683SOmar Sandoval 			 * This will also catch a race condition where a
116805dbe683SOmar Sandoval 			 * subvolume which was passed by ID is renamed and
116905dbe683SOmar Sandoval 			 * another subvolume is renamed over the old location.
117005dbe683SOmar Sandoval 			 */
1171ab8d0fc4SJeff Mahoney 			btrfs_err(fs_info,
1172ab8d0fc4SJeff Mahoney 				  "subvol '%s' does not match subvolid %llu",
1173bb289b7bSOmar Sandoval 				  subvol_name, subvol_objectid);
1174bb289b7bSOmar Sandoval 			ret = -EINVAL;
1175bb289b7bSOmar Sandoval 		}
1176bb289b7bSOmar Sandoval 		if (ret) {
1177ea441d11SAl Viro 			dput(root);
1178bb289b7bSOmar Sandoval 			root = ERR_PTR(ret);
1179ea441d11SAl Viro 			deactivate_locked_super(s);
1180bb289b7bSOmar Sandoval 		}
1181f9d9ef62SDavid Sterba 	}
1182f9d9ef62SDavid Sterba 
1183fa330659SOmar Sandoval out:
1184fa330659SOmar Sandoval 	mntput(mnt);
1185fa330659SOmar Sandoval 	kfree(subvol_name);
1186830c4adbSJosef Bacik 	return root;
1187830c4adbSJosef Bacik }
1188450ba0eaSJosef Bacik 
11890d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
1190f7b885beSAnand Jain 				     u32 new_pool_size, u32 old_pool_size)
11910d2450abSSergei Trofimovich {
11920d2450abSSergei Trofimovich 	if (new_pool_size == old_pool_size)
11930d2450abSSergei Trofimovich 		return;
11940d2450abSSergei Trofimovich 
11950d2450abSSergei Trofimovich 	fs_info->thread_pool_size = new_pool_size;
11960d2450abSSergei Trofimovich 
1197efe120a0SFrank Holton 	btrfs_info(fs_info, "resize thread pool %d -> %d",
11980d2450abSSergei Trofimovich 	       old_pool_size, new_pool_size);
11990d2450abSSergei Trofimovich 
12005cdc7ad3SQu Wenruo 	btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
1201afe3d242SQu Wenruo 	btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
1202e66f0bb1SQu Wenruo 	btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
120340fac647SChristoph Hellwig 	workqueue_set_max_active(fs_info->endio_workers, new_pool_size);
120440fac647SChristoph Hellwig 	workqueue_set_max_active(fs_info->endio_meta_workers, new_pool_size);
1205fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
1206fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
12075b3bc44eSQu Wenruo 	btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
12080d2450abSSergei Trofimovich }
12090d2450abSSergei Trofimovich 
1210f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1211f42a34b2SMiao Xie 				       unsigned long old_opts, int flags)
1212f42a34b2SMiao Xie {
1213dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1214dc81cdc5SMiao Xie 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
12151751e8a6SLinus Torvalds 	     (flags & SB_RDONLY))) {
1216dc81cdc5SMiao Xie 		/* wait for any defraggers to finish */
1217dc81cdc5SMiao Xie 		wait_event(fs_info->transaction_wait,
1218dc81cdc5SMiao Xie 			   (atomic_read(&fs_info->defrag_running) == 0));
12191751e8a6SLinus Torvalds 		if (flags & SB_RDONLY)
1220dc81cdc5SMiao Xie 			sync_filesystem(fs_info->sb);
1221dc81cdc5SMiao Xie 	}
1222dc81cdc5SMiao Xie }
1223dc81cdc5SMiao Xie 
1224dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1225dc81cdc5SMiao Xie 					 unsigned long old_opts)
1226dc81cdc5SMiao Xie {
122794846229SBoris Burkov 	const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE);
122894846229SBoris Burkov 
1229dc81cdc5SMiao Xie 	/*
1230180e4d47SLuis de Bethencourt 	 * We need to cleanup all defragable inodes if the autodefragment is
1231180e4d47SLuis de Bethencourt 	 * close or the filesystem is read only.
1232dc81cdc5SMiao Xie 	 */
1233dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1234bc98a42cSDavid Howells 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) {
1235dc81cdc5SMiao Xie 		btrfs_cleanup_defrag_inodes(fs_info);
1236dc81cdc5SMiao Xie 	}
1237dc81cdc5SMiao Xie 
1238b0643e59SDennis Zhou 	/* If we toggled discard async */
1239b0643e59SDennis Zhou 	if (!btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1240b0643e59SDennis Zhou 	    btrfs_test_opt(fs_info, DISCARD_ASYNC))
1241b0643e59SDennis Zhou 		btrfs_discard_resume(fs_info);
1242b0643e59SDennis Zhou 	else if (btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1243b0643e59SDennis Zhou 		 !btrfs_test_opt(fs_info, DISCARD_ASYNC))
1244b0643e59SDennis Zhou 		btrfs_discard_cleanup(fs_info);
124594846229SBoris Burkov 
124694846229SBoris Burkov 	/* If we toggled space cache */
124794846229SBoris Burkov 	if (cache_opt != btrfs_free_space_cache_v1_active(fs_info))
124894846229SBoris Burkov 		btrfs_set_free_space_cache_v1_active(fs_info, cache_opt);
1249dc81cdc5SMiao Xie }
1250dc81cdc5SMiao Xie 
12519ef40c2eSJosef Bacik static int btrfs_remount_rw(struct btrfs_fs_info *fs_info)
1252c146afadSYan Zheng {
1253c146afadSYan Zheng 	int ret;
1254c146afadSYan Zheng 
12559ef40c2eSJosef Bacik 	if (BTRFS_FS_ERROR(fs_info)) {
12569ef40c2eSJosef Bacik 		btrfs_err(fs_info,
12579ef40c2eSJosef Bacik 			  "remounting read-write after error is not allowed");
12589ef40c2eSJosef Bacik 		return -EINVAL;
12599ef40c2eSJosef Bacik 	}
12609ef40c2eSJosef Bacik 
12619ef40c2eSJosef Bacik 	if (fs_info->fs_devices->rw_devices == 0)
12629ef40c2eSJosef Bacik 		return -EACCES;
12639ef40c2eSJosef Bacik 
12649ef40c2eSJosef Bacik 	if (!btrfs_check_rw_degradable(fs_info, NULL)) {
12659ef40c2eSJosef Bacik 		btrfs_warn(fs_info,
12669ef40c2eSJosef Bacik 			   "too many missing devices, writable remount is not allowed");
12679ef40c2eSJosef Bacik 		return -EACCES;
12689ef40c2eSJosef Bacik 	}
12699ef40c2eSJosef Bacik 
12709ef40c2eSJosef Bacik 	if (btrfs_super_log_root(fs_info->super_copy) != 0) {
12719ef40c2eSJosef Bacik 		btrfs_warn(fs_info,
12729ef40c2eSJosef Bacik 			   "mount required to replay tree-log, cannot remount read-write");
12739ef40c2eSJosef Bacik 		return -EINVAL;
12749ef40c2eSJosef Bacik 	}
12759ef40c2eSJosef Bacik 
12769ef40c2eSJosef Bacik 	/*
12779ef40c2eSJosef Bacik 	 * NOTE: when remounting with a change that does writes, don't put it
12789ef40c2eSJosef Bacik 	 * anywhere above this point, as we are not sure to be safe to write
12799ef40c2eSJosef Bacik 	 * until we pass the above checks.
12809ef40c2eSJosef Bacik 	 */
12819ef40c2eSJosef Bacik 	ret = btrfs_start_pre_rw_mount(fs_info);
12829ef40c2eSJosef Bacik 	if (ret)
12839ef40c2eSJosef Bacik 		return ret;
12849ef40c2eSJosef Bacik 
12859ef40c2eSJosef Bacik 	btrfs_clear_sb_rdonly(fs_info->sb);
12869ef40c2eSJosef Bacik 
12879ef40c2eSJosef Bacik 	set_bit(BTRFS_FS_OPEN, &fs_info->flags);
12889ef40c2eSJosef Bacik 
12899ef40c2eSJosef Bacik 	/*
12909ef40c2eSJosef Bacik 	 * If we've gone from readonly -> read-write, we need to get our
12919ef40c2eSJosef Bacik 	 * sync/async discard lists in the right state.
12929ef40c2eSJosef Bacik 	 */
12939ef40c2eSJosef Bacik 	btrfs_discard_resume(fs_info);
12949ef40c2eSJosef Bacik 
12959ef40c2eSJosef Bacik 	return 0;
12969ef40c2eSJosef Bacik }
12979ef40c2eSJosef Bacik 
12989ef40c2eSJosef Bacik static int btrfs_remount_ro(struct btrfs_fs_info *fs_info)
12999ef40c2eSJosef Bacik {
13009ef40c2eSJosef Bacik 	/*
13019ef40c2eSJosef Bacik 	 * This also happens on 'umount -rf' or on shutdown, when the
13029ef40c2eSJosef Bacik 	 * filesystem is busy.
13039ef40c2eSJosef Bacik 	 */
13049ef40c2eSJosef Bacik 	cancel_work_sync(&fs_info->async_reclaim_work);
13059ef40c2eSJosef Bacik 	cancel_work_sync(&fs_info->async_data_reclaim_work);
13069ef40c2eSJosef Bacik 
13079ef40c2eSJosef Bacik 	btrfs_discard_cleanup(fs_info);
13089ef40c2eSJosef Bacik 
13099ef40c2eSJosef Bacik 	/* Wait for the uuid_scan task to finish */
13109ef40c2eSJosef Bacik 	down(&fs_info->uuid_tree_rescan_sem);
13119ef40c2eSJosef Bacik 	/* Avoid complains from lockdep et al. */
13129ef40c2eSJosef Bacik 	up(&fs_info->uuid_tree_rescan_sem);
13139ef40c2eSJosef Bacik 
13149ef40c2eSJosef Bacik 	btrfs_set_sb_rdonly(fs_info->sb);
13159ef40c2eSJosef Bacik 
13169ef40c2eSJosef Bacik 	/*
13179ef40c2eSJosef Bacik 	 * Setting SB_RDONLY will put the cleaner thread to sleep at the next
13189ef40c2eSJosef Bacik 	 * loop if it's already active.  If it's already asleep, we'll leave
13199ef40c2eSJosef Bacik 	 * unused block groups on disk until we're mounted read-write again
13209ef40c2eSJosef Bacik 	 * unless we clean them up here.
13219ef40c2eSJosef Bacik 	 */
13229ef40c2eSJosef Bacik 	btrfs_delete_unused_bgs(fs_info);
13239ef40c2eSJosef Bacik 
13249ef40c2eSJosef Bacik 	/*
13259ef40c2eSJosef Bacik 	 * The cleaner task could be already running before we set the flag
13269ef40c2eSJosef Bacik 	 * BTRFS_FS_STATE_RO (and SB_RDONLY in the superblock).  We must make
13279ef40c2eSJosef Bacik 	 * sure that after we finish the remount, i.e. after we call
13289ef40c2eSJosef Bacik 	 * btrfs_commit_super(), the cleaner can no longer start a transaction
13299ef40c2eSJosef Bacik 	 * - either because it was dropping a dead root, running delayed iputs
13309ef40c2eSJosef Bacik 	 *   or deleting an unused block group (the cleaner picked a block
13319ef40c2eSJosef Bacik 	 *   group from the list of unused block groups before we were able to
13329ef40c2eSJosef Bacik 	 *   in the previous call to btrfs_delete_unused_bgs()).
13339ef40c2eSJosef Bacik 	 */
13349ef40c2eSJosef Bacik 	wait_on_bit(&fs_info->flags, BTRFS_FS_CLEANER_RUNNING, TASK_UNINTERRUPTIBLE);
13359ef40c2eSJosef Bacik 
13369ef40c2eSJosef Bacik 	/*
13379ef40c2eSJosef Bacik 	 * We've set the superblock to RO mode, so we might have made the
13389ef40c2eSJosef Bacik 	 * cleaner task sleep without running all pending delayed iputs. Go
13399ef40c2eSJosef Bacik 	 * through all the delayed iputs here, so that if an unmount happens
13409ef40c2eSJosef Bacik 	 * without remounting RW we don't end up at finishing close_ctree()
13419ef40c2eSJosef Bacik 	 * with a non-empty list of delayed iputs.
13429ef40c2eSJosef Bacik 	 */
13439ef40c2eSJosef Bacik 	btrfs_run_delayed_iputs(fs_info);
13449ef40c2eSJosef Bacik 
13459ef40c2eSJosef Bacik 	btrfs_dev_replace_suspend_for_unmount(fs_info);
13469ef40c2eSJosef Bacik 	btrfs_scrub_cancel(fs_info);
13479ef40c2eSJosef Bacik 	btrfs_pause_balance(fs_info);
13489ef40c2eSJosef Bacik 
13499ef40c2eSJosef Bacik 	/*
13509ef40c2eSJosef Bacik 	 * Pause the qgroup rescan worker if it is running. We don't want it to
13519ef40c2eSJosef Bacik 	 * be still running after we are in RO mode, as after that, by the time
13529ef40c2eSJosef Bacik 	 * we unmount, it might have left a transaction open, so we would leak
13539ef40c2eSJosef Bacik 	 * the transaction and/or crash.
13549ef40c2eSJosef Bacik 	 */
13559ef40c2eSJosef Bacik 	btrfs_qgroup_wait_for_completion(fs_info, false);
13569ef40c2eSJosef Bacik 
13579ef40c2eSJosef Bacik 	return btrfs_commit_super(fs_info);
13589ef40c2eSJosef Bacik }
13599ef40c2eSJosef Bacik 
1360eddb1a43SJosef Bacik static void btrfs_ctx_to_info(struct btrfs_fs_info *fs_info, struct btrfs_fs_context *ctx)
1361eddb1a43SJosef Bacik {
1362eddb1a43SJosef Bacik 	fs_info->max_inline = ctx->max_inline;
1363eddb1a43SJosef Bacik 	fs_info->commit_interval = ctx->commit_interval;
1364eddb1a43SJosef Bacik 	fs_info->metadata_ratio = ctx->metadata_ratio;
1365eddb1a43SJosef Bacik 	fs_info->thread_pool_size = ctx->thread_pool_size;
1366eddb1a43SJosef Bacik 	fs_info->mount_opt = ctx->mount_opt;
1367eddb1a43SJosef Bacik 	fs_info->compress_type = ctx->compress_type;
1368eddb1a43SJosef Bacik 	fs_info->compress_level = ctx->compress_level;
1369eddb1a43SJosef Bacik }
1370eddb1a43SJosef Bacik 
1371eddb1a43SJosef Bacik static void btrfs_info_to_ctx(struct btrfs_fs_info *fs_info, struct btrfs_fs_context *ctx)
1372eddb1a43SJosef Bacik {
1373eddb1a43SJosef Bacik 	ctx->max_inline = fs_info->max_inline;
1374eddb1a43SJosef Bacik 	ctx->commit_interval = fs_info->commit_interval;
1375eddb1a43SJosef Bacik 	ctx->metadata_ratio = fs_info->metadata_ratio;
1376eddb1a43SJosef Bacik 	ctx->thread_pool_size = fs_info->thread_pool_size;
1377eddb1a43SJosef Bacik 	ctx->mount_opt = fs_info->mount_opt;
1378eddb1a43SJosef Bacik 	ctx->compress_type = fs_info->compress_type;
1379eddb1a43SJosef Bacik 	ctx->compress_level = fs_info->compress_level;
1380eddb1a43SJosef Bacik }
1381eddb1a43SJosef Bacik 
1382eddb1a43SJosef Bacik #define btrfs_info_if_set(fs_info, old_ctx, opt, fmt, args...)			\
1383eddb1a43SJosef Bacik do {										\
1384eddb1a43SJosef Bacik 	if ((!old_ctx || !btrfs_raw_test_opt(old_ctx->mount_opt, opt)) &&	\
1385eddb1a43SJosef Bacik 	    btrfs_raw_test_opt(fs_info->mount_opt, opt))			\
1386eddb1a43SJosef Bacik 		btrfs_info(fs_info, fmt, ##args);				\
1387eddb1a43SJosef Bacik } while (0)
1388eddb1a43SJosef Bacik 
1389eddb1a43SJosef Bacik #define btrfs_info_if_unset(fs_info, old_ctx, opt, fmt, args...)	\
1390eddb1a43SJosef Bacik do {									\
1391eddb1a43SJosef Bacik 	if ((old_ctx && btrfs_raw_test_opt(old_ctx->mount_opt, opt)) &&	\
1392eddb1a43SJosef Bacik 	    !btrfs_raw_test_opt(fs_info->mount_opt, opt))		\
1393eddb1a43SJosef Bacik 		btrfs_info(fs_info, fmt, ##args);			\
1394eddb1a43SJosef Bacik } while (0)
1395eddb1a43SJosef Bacik 
1396eddb1a43SJosef Bacik static void btrfs_emit_options(struct btrfs_fs_info *info,
1397eddb1a43SJosef Bacik 			       struct btrfs_fs_context *old)
1398eddb1a43SJosef Bacik {
1399eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, NODATASUM, "setting nodatasum");
1400eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, DEGRADED, "allowing degraded mounts");
1401eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, NODATASUM, "setting nodatasum");
1402eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, SSD, "enabling ssd optimizations");
1403eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, SSD_SPREAD, "using spread ssd allocation scheme");
1404eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, NOBARRIER, "turning off barriers");
1405eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, NOTREELOG, "disabling tree log");
1406eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, NOLOGREPLAY, "disabling log replay at mount time");
1407eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, FLUSHONCOMMIT, "turning on flush-on-commit");
1408eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, DISCARD_SYNC, "turning on sync discard");
1409eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, DISCARD_ASYNC, "turning on async discard");
1410eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, FREE_SPACE_TREE, "enabling free space tree");
1411eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, SPACE_CACHE, "enabling disk space caching");
1412eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, CLEAR_CACHE, "force clearing of disk cache");
1413eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, AUTO_DEFRAG, "enabling auto defrag");
1414eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, FRAGMENT_DATA, "fragmenting data");
1415eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, FRAGMENT_METADATA, "fragmenting metadata");
1416eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, REF_VERIFY, "doing ref verification");
1417eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, USEBACKUPROOT, "trying to use backup root at mount time");
1418eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, IGNOREBADROOTS, "ignoring bad roots");
1419eddb1a43SJosef Bacik 	btrfs_info_if_set(info, old, IGNOREDATACSUMS, "ignoring data csums");
1420eddb1a43SJosef Bacik 
1421eddb1a43SJosef Bacik 	btrfs_info_if_unset(info, old, NODATACOW, "setting datacow");
1422eddb1a43SJosef Bacik 	btrfs_info_if_unset(info, old, SSD, "not using ssd optimizations");
1423eddb1a43SJosef Bacik 	btrfs_info_if_unset(info, old, SSD_SPREAD, "not using spread ssd allocation scheme");
1424eddb1a43SJosef Bacik 	btrfs_info_if_unset(info, old, NOBARRIER, "turning off barriers");
1425eddb1a43SJosef Bacik 	btrfs_info_if_unset(info, old, NOTREELOG, "enabling tree log");
1426eddb1a43SJosef Bacik 	btrfs_info_if_unset(info, old, SPACE_CACHE, "disabling disk space caching");
1427eddb1a43SJosef Bacik 	btrfs_info_if_unset(info, old, FREE_SPACE_TREE, "disabling free space tree");
1428eddb1a43SJosef Bacik 	btrfs_info_if_unset(info, old, AUTO_DEFRAG, "disabling auto defrag");
1429eddb1a43SJosef Bacik 	btrfs_info_if_unset(info, old, COMPRESS, "use no compression");
1430eddb1a43SJosef Bacik 
1431eddb1a43SJosef Bacik 	/* Did the compression settings change? */
1432eddb1a43SJosef Bacik 	if (btrfs_test_opt(info, COMPRESS) &&
1433eddb1a43SJosef Bacik 	    (!old ||
1434eddb1a43SJosef Bacik 	     old->compress_type != info->compress_type ||
1435eddb1a43SJosef Bacik 	     old->compress_level != info->compress_level ||
1436eddb1a43SJosef Bacik 	     (!btrfs_raw_test_opt(old->mount_opt, FORCE_COMPRESS) &&
1437eddb1a43SJosef Bacik 	      btrfs_raw_test_opt(info->mount_opt, FORCE_COMPRESS)))) {
1438eddb1a43SJosef Bacik 		const char *compress_type = btrfs_compress_type2str(info->compress_type);
1439eddb1a43SJosef Bacik 
1440eddb1a43SJosef Bacik 		btrfs_info(info, "%s %s compression, level %d",
1441eddb1a43SJosef Bacik 			   btrfs_test_opt(info, FORCE_COMPRESS) ? "force" : "use",
1442eddb1a43SJosef Bacik 			   compress_type, info->compress_level);
1443eddb1a43SJosef Bacik 	}
1444eddb1a43SJosef Bacik 
1445eddb1a43SJosef Bacik 	if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
1446eddb1a43SJosef Bacik 		btrfs_info(info, "max_inline set to %llu", info->max_inline);
1447eddb1a43SJosef Bacik }
1448eddb1a43SJosef Bacik 
1449eddb1a43SJosef Bacik static int btrfs_reconfigure(struct fs_context *fc)
1450eddb1a43SJosef Bacik {
1451eddb1a43SJosef Bacik 	struct super_block *sb = fc->root->d_sb;
1452eddb1a43SJosef Bacik 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1453eddb1a43SJosef Bacik 	struct btrfs_fs_context *ctx = fc->fs_private;
1454eddb1a43SJosef Bacik 	struct btrfs_fs_context old_ctx;
1455eddb1a43SJosef Bacik 	int ret = 0;
1456f044b318SJosef Bacik 	bool mount_reconfigure = (fc->s_fs_info != NULL);
1457eddb1a43SJosef Bacik 
1458eddb1a43SJosef Bacik 	btrfs_info_to_ctx(fs_info, &old_ctx);
1459eddb1a43SJosef Bacik 
14602018ef1dSJosef Bacik 	/*
14612018ef1dSJosef Bacik 	 * This is our "bind mount" trick, we don't want to allow the user to do
14622018ef1dSJosef Bacik 	 * anything other than mount a different ro/rw and a different subvol,
14632018ef1dSJosef Bacik 	 * all of the mount options should be maintained.
14642018ef1dSJosef Bacik 	 */
14652018ef1dSJosef Bacik 	if (mount_reconfigure)
14662018ef1dSJosef Bacik 		ctx->mount_opt = old_ctx.mount_opt;
14672018ef1dSJosef Bacik 
146802b9984dSTheodore Ts'o 	sync_filesystem(sb);
146988c4703fSJohannes Thumshirn 	set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1470dc81cdc5SMiao Xie 
1471f044b318SJosef Bacik 	if (!mount_reconfigure &&
1472ad21f15bSJosef Bacik 	    !btrfs_check_options(fs_info, &ctx->mount_opt, fc->sb_flags))
1473eddb1a43SJosef Bacik 		return -EINVAL;
1474f667aef6SQu Wenruo 
1475eddb1a43SJosef Bacik 	ret = btrfs_check_features(fs_info, !(fc->sb_flags & SB_RDONLY));
1476d7f67ac9SQu Wenruo 	if (ret < 0)
1477eddb1a43SJosef Bacik 		return ret;
1478d7f67ac9SQu Wenruo 
1479eddb1a43SJosef Bacik 	btrfs_ctx_to_info(fs_info, ctx);
1480eddb1a43SJosef Bacik 	btrfs_remount_begin(fs_info, old_ctx.mount_opt, fc->sb_flags);
1481eddb1a43SJosef Bacik 	btrfs_resize_thread_pool(fs_info, fs_info->thread_pool_size,
1482eddb1a43SJosef Bacik 				 old_ctx.thread_pool_size);
14830d2450abSSergei Trofimovich 
1484c55a4319SBoris Burkov 	if ((bool)btrfs_test_opt(fs_info, FREE_SPACE_TREE) !=
1485c55a4319SBoris Burkov 	    (bool)btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
1486eddb1a43SJosef Bacik 	    (!sb_rdonly(sb) || (fc->sb_flags & SB_RDONLY))) {
14872838d255SBoris Burkov 		btrfs_warn(fs_info,
1488eddb1a43SJosef Bacik 		"remount supports changing free space tree only from RO to RW");
1489eddb1a43SJosef Bacik 		/* Make sure free space cache options match the state on disk. */
14902838d255SBoris Burkov 		if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
14912838d255SBoris Burkov 			btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
14922838d255SBoris Burkov 			btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
14932838d255SBoris Burkov 		}
14942838d255SBoris Burkov 		if (btrfs_free_space_cache_v1_active(fs_info)) {
14952838d255SBoris Burkov 			btrfs_clear_opt(fs_info->mount_opt, FREE_SPACE_TREE);
14962838d255SBoris Burkov 			btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE);
14972838d255SBoris Burkov 		}
14982838d255SBoris Burkov 	}
14992838d255SBoris Burkov 
1500eddb1a43SJosef Bacik 	ret = 0;
1501eddb1a43SJosef Bacik 	if (!sb_rdonly(sb) && (fc->sb_flags & SB_RDONLY))
1502eddb1a43SJosef Bacik 		ret = btrfs_remount_ro(fs_info);
1503eddb1a43SJosef Bacik 	else if (sb_rdonly(sb) && !(fc->sb_flags & SB_RDONLY))
1504eddb1a43SJosef Bacik 		ret = btrfs_remount_rw(fs_info);
150549b25e05SJeff Mahoney 	if (ret)
150649b25e05SJeff Mahoney 		goto restore;
1507c146afadSYan Zheng 
1508981a37baSChris Mason 	/*
1509eddb1a43SJosef Bacik 	 * If we set the mask during the parameter parsing VFS would reject the
1510eddb1a43SJosef Bacik 	 * remount.  Here we can set the mask and the value will be updated
1511eddb1a43SJosef Bacik 	 * appropriately.
1512981a37baSChris Mason 	 */
1513eddb1a43SJosef Bacik 	if ((fc->sb_flags & SB_POSIXACL) != (sb->s_flags & SB_POSIXACL))
1514eddb1a43SJosef Bacik 		fc->sb_flags_mask |= SB_POSIXACL;
1515faa00889SJosef Bacik 
1516eddb1a43SJosef Bacik 	btrfs_emit_options(fs_info, &old_ctx);
15172c6a92b0SJustin Maggard 	wake_up_process(fs_info->transaction_kthread);
1518eddb1a43SJosef Bacik 	btrfs_remount_cleanup(fs_info, old_ctx.mount_opt);
15198cd29088SBoris Burkov 	btrfs_clear_oneshot_options(fs_info);
152088c4703fSJohannes Thumshirn 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
152188c4703fSJohannes Thumshirn 
1522c146afadSYan Zheng 	return 0;
152349b25e05SJeff Mahoney restore:
1524eddb1a43SJosef Bacik 	btrfs_ctx_to_info(fs_info, &old_ctx);
1525eddb1a43SJosef Bacik 	btrfs_remount_cleanup(fs_info, old_ctx.mount_opt);
152688c4703fSJohannes Thumshirn 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
152749b25e05SJeff Mahoney 	return ret;
1528c146afadSYan Zheng }
1529c146afadSYan Zheng 
1530bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */
1531214cc184SDavid Sterba static int btrfs_cmp_device_free_bytes(const void *a, const void *b)
1532bcd53741SArne Jansen {
1533214cc184SDavid Sterba 	const struct btrfs_device_info *dev_info1 = a;
1534214cc184SDavid Sterba 	const struct btrfs_device_info *dev_info2 = b;
1535214cc184SDavid Sterba 
1536214cc184SDavid Sterba 	if (dev_info1->max_avail > dev_info2->max_avail)
1537bcd53741SArne Jansen 		return -1;
1538214cc184SDavid Sterba 	else if (dev_info1->max_avail < dev_info2->max_avail)
1539bcd53741SArne Jansen 		return 1;
1540bcd53741SArne Jansen 	return 0;
1541bcd53741SArne Jansen }
1542bcd53741SArne Jansen 
1543bcd53741SArne Jansen /*
1544bcd53741SArne Jansen  * sort the devices by max_avail, in which max free extent size of each device
1545bcd53741SArne Jansen  * is stored.(Descending Sort)
1546bcd53741SArne Jansen  */
1547bcd53741SArne Jansen static inline void btrfs_descending_sort_devices(
1548bcd53741SArne Jansen 					struct btrfs_device_info *devices,
1549bcd53741SArne Jansen 					size_t nr_devices)
1550bcd53741SArne Jansen {
1551bcd53741SArne Jansen 	sort(devices, nr_devices, sizeof(struct btrfs_device_info),
1552bcd53741SArne Jansen 	     btrfs_cmp_device_free_bytes, NULL);
1553bcd53741SArne Jansen }
1554bcd53741SArne Jansen 
15556d07bcecSMiao Xie /*
15566d07bcecSMiao Xie  * The helper to calc the free space on the devices that can be used to store
15576d07bcecSMiao Xie  * file data.
15586d07bcecSMiao Xie  */
15597e17916bSArnd Bergmann static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
15606bccf3abSJeff Mahoney 					      u64 *free_bytes)
15616d07bcecSMiao Xie {
15626d07bcecSMiao Xie 	struct btrfs_device_info *devices_info;
15636d07bcecSMiao Xie 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
15646d07bcecSMiao Xie 	struct btrfs_device *device;
15656d07bcecSMiao Xie 	u64 type;
15666d07bcecSMiao Xie 	u64 avail_space;
15676d07bcecSMiao Xie 	u64 min_stripe_size;
1568559ca6eaSNikolay Borisov 	int num_stripes = 1;
15696d07bcecSMiao Xie 	int i = 0, nr_devices;
15704f080f57SDavid Sterba 	const struct btrfs_raid_attr *rattr;
15716d07bcecSMiao Xie 
15727e33fd99SJosef Bacik 	/*
157301327610SNicholas D Steeves 	 * We aren't under the device list lock, so this is racy-ish, but good
15747e33fd99SJosef Bacik 	 * enough for our purposes.
15757e33fd99SJosef Bacik 	 */
1576b772a86eSLi Zefan 	nr_devices = fs_info->fs_devices->open_devices;
15777e33fd99SJosef Bacik 	if (!nr_devices) {
15787e33fd99SJosef Bacik 		smp_mb();
15797e33fd99SJosef Bacik 		nr_devices = fs_info->fs_devices->open_devices;
15807e33fd99SJosef Bacik 		ASSERT(nr_devices);
15817e33fd99SJosef Bacik 		if (!nr_devices) {
15827e33fd99SJosef Bacik 			*free_bytes = 0;
15837e33fd99SJosef Bacik 			return 0;
15847e33fd99SJosef Bacik 		}
15857e33fd99SJosef Bacik 	}
15866d07bcecSMiao Xie 
1587d9b0d9baSDulshani Gunawardhana 	devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
15886a44517dSDavid Sterba 			       GFP_KERNEL);
15896d07bcecSMiao Xie 	if (!devices_info)
15906d07bcecSMiao Xie 		return -ENOMEM;
15916d07bcecSMiao Xie 
159201327610SNicholas D Steeves 	/* calc min stripe number for data space allocation */
15931b86826dSJeff Mahoney 	type = btrfs_data_alloc_profile(fs_info);
15944f080f57SDavid Sterba 	rattr = &btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)];
15954f080f57SDavid Sterba 
1596e1ea2beeSDavid Sterba 	if (type & BTRFS_BLOCK_GROUP_RAID0)
159739fb26c3SMiao Xie 		num_stripes = nr_devices;
1598d09cb9e1SDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID1_MASK)
1599d09cb9e1SDavid Sterba 		num_stripes = rattr->ncopies;
1600e1ea2beeSDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID10)
160139fb26c3SMiao Xie 		num_stripes = 4;
16026d07bcecSMiao Xie 
16034f080f57SDavid Sterba 	/* Adjust for more than 1 stripe per device */
16044f080f57SDavid Sterba 	min_stripe_size = rattr->dev_stripes * BTRFS_STRIPE_LEN;
16056d07bcecSMiao Xie 
16067e33fd99SJosef Bacik 	rcu_read_lock();
16077e33fd99SJosef Bacik 	list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
1608e12c9621SAnand Jain 		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
1609e12c9621SAnand Jain 						&device->dev_state) ||
1610401e29c1SAnand Jain 		    !device->bdev ||
1611401e29c1SAnand Jain 		    test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
16126d07bcecSMiao Xie 			continue;
16136d07bcecSMiao Xie 
16147e33fd99SJosef Bacik 		if (i >= nr_devices)
16157e33fd99SJosef Bacik 			break;
16167e33fd99SJosef Bacik 
16176d07bcecSMiao Xie 		avail_space = device->total_bytes - device->bytes_used;
16186d07bcecSMiao Xie 
16196d07bcecSMiao Xie 		/* align with stripe_len */
1620559ca6eaSNikolay Borisov 		avail_space = rounddown(avail_space, BTRFS_STRIPE_LEN);
16216d07bcecSMiao Xie 
16226d07bcecSMiao Xie 		/*
162337f85ec3SQu Wenruo 		 * Ensure we have at least min_stripe_size on top of the
162437f85ec3SQu Wenruo 		 * reserved space on the device.
16256d07bcecSMiao Xie 		 */
162637f85ec3SQu Wenruo 		if (avail_space <= BTRFS_DEVICE_RANGE_RESERVED + min_stripe_size)
16276d07bcecSMiao Xie 			continue;
16286d07bcecSMiao Xie 
162937f85ec3SQu Wenruo 		avail_space -= BTRFS_DEVICE_RANGE_RESERVED;
1630559ca6eaSNikolay Borisov 
16316d07bcecSMiao Xie 		devices_info[i].dev = device;
16326d07bcecSMiao Xie 		devices_info[i].max_avail = avail_space;
16336d07bcecSMiao Xie 
16346d07bcecSMiao Xie 		i++;
16356d07bcecSMiao Xie 	}
16367e33fd99SJosef Bacik 	rcu_read_unlock();
16376d07bcecSMiao Xie 
16386d07bcecSMiao Xie 	nr_devices = i;
16396d07bcecSMiao Xie 
16406d07bcecSMiao Xie 	btrfs_descending_sort_devices(devices_info, nr_devices);
16416d07bcecSMiao Xie 
16426d07bcecSMiao Xie 	i = nr_devices - 1;
16436d07bcecSMiao Xie 	avail_space = 0;
1644559ca6eaSNikolay Borisov 	while (nr_devices >= rattr->devs_min) {
1645559ca6eaSNikolay Borisov 		num_stripes = min(num_stripes, nr_devices);
164639fb26c3SMiao Xie 
16476d07bcecSMiao Xie 		if (devices_info[i].max_avail >= min_stripe_size) {
16486d07bcecSMiao Xie 			int j;
16496d07bcecSMiao Xie 			u64 alloc_size;
16506d07bcecSMiao Xie 
165139fb26c3SMiao Xie 			avail_space += devices_info[i].max_avail * num_stripes;
16526d07bcecSMiao Xie 			alloc_size = devices_info[i].max_avail;
165339fb26c3SMiao Xie 			for (j = i + 1 - num_stripes; j <= i; j++)
16546d07bcecSMiao Xie 				devices_info[j].max_avail -= alloc_size;
16556d07bcecSMiao Xie 		}
16566d07bcecSMiao Xie 		i--;
16576d07bcecSMiao Xie 		nr_devices--;
16586d07bcecSMiao Xie 	}
16596d07bcecSMiao Xie 
16606d07bcecSMiao Xie 	kfree(devices_info);
16616d07bcecSMiao Xie 	*free_bytes = avail_space;
16626d07bcecSMiao Xie 	return 0;
16636d07bcecSMiao Xie }
16646d07bcecSMiao Xie 
1665ba7b6e62SDavid Sterba /*
1666ba7b6e62SDavid Sterba  * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
1667ba7b6e62SDavid Sterba  *
1668ba7b6e62SDavid Sterba  * If there's a redundant raid level at DATA block groups, use the respective
1669ba7b6e62SDavid Sterba  * multiplier to scale the sizes.
1670ba7b6e62SDavid Sterba  *
1671ba7b6e62SDavid Sterba  * Unused device space usage is based on simulating the chunk allocator
16720d0c71b3SDavid Sterba  * algorithm that respects the device sizes and order of allocations.  This is
16730d0c71b3SDavid Sterba  * a close approximation of the actual use but there are other factors that may
16740d0c71b3SDavid Sterba  * change the result (like a new metadata chunk).
1675ba7b6e62SDavid Sterba  *
1676ca8a51b3SDavid Sterba  * If metadata is exhausted, f_bavail will be 0.
1677ba7b6e62SDavid Sterba  */
16788fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
16798fd17795SChris Mason {
1680815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
1681815745cfSAl Viro 	struct btrfs_super_block *disk_super = fs_info->super_copy;
1682bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
1683bd4d1088SJosef Bacik 	u64 total_used = 0;
16846d07bcecSMiao Xie 	u64 total_free_data = 0;
1685ca8a51b3SDavid Sterba 	u64 total_free_meta = 0;
1686265fdfa6SDavid Sterba 	u32 bits = fs_info->sectorsize_bits;
1687de37aa51SNikolay Borisov 	__be32 *fsid = (__be32 *)fs_info->fs_devices->fsid;
1688ba7b6e62SDavid Sterba 	unsigned factor = 1;
1689ba7b6e62SDavid Sterba 	struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
16906d07bcecSMiao Xie 	int ret;
1691ca8a51b3SDavid Sterba 	u64 thresh = 0;
1692ae02d1bdSLuis de Bethencourt 	int mixed = 0;
16938fd17795SChris Mason 
169472804905SJosef Bacik 	list_for_each_entry(found, &fs_info->space_info, list) {
16956d07bcecSMiao Xie 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
1696ba7b6e62SDavid Sterba 			int i;
1697ba7b6e62SDavid Sterba 
16986d07bcecSMiao Xie 			total_free_data += found->disk_total - found->disk_used;
16996d07bcecSMiao Xie 			total_free_data -=
17006d07bcecSMiao Xie 				btrfs_account_ro_block_groups_free_space(found);
1701ba7b6e62SDavid Sterba 
1702ba7b6e62SDavid Sterba 			for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
170346df06b8SDavid Sterba 				if (!list_empty(&found->block_groups[i]))
170446df06b8SDavid Sterba 					factor = btrfs_bg_type_to_factor(
170546df06b8SDavid Sterba 						btrfs_raid_array[i].bg_flag);
1706ba7b6e62SDavid Sterba 			}
17076d07bcecSMiao Xie 		}
1708ae02d1bdSLuis de Bethencourt 
1709ae02d1bdSLuis de Bethencourt 		/*
171067da05b3SColin Ian King 		 * Metadata in mixed block group profiles are accounted in data
1711ae02d1bdSLuis de Bethencourt 		 */
1712ae02d1bdSLuis de Bethencourt 		if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) {
1713ae02d1bdSLuis de Bethencourt 			if (found->flags & BTRFS_BLOCK_GROUP_DATA)
1714ae02d1bdSLuis de Bethencourt 				mixed = 1;
1715ae02d1bdSLuis de Bethencourt 			else
1716ae02d1bdSLuis de Bethencourt 				total_free_meta += found->disk_total -
1717ae02d1bdSLuis de Bethencourt 					found->disk_used;
1718ae02d1bdSLuis de Bethencourt 		}
17196d07bcecSMiao Xie 
1720b742bb82SYan, Zheng 		total_used += found->disk_used;
172189a55897SJosef Bacik 	}
1722ba7b6e62SDavid Sterba 
1723ba7b6e62SDavid Sterba 	buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
1724ba7b6e62SDavid Sterba 	buf->f_blocks >>= bits;
1725ba7b6e62SDavid Sterba 	buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
1726ba7b6e62SDavid Sterba 
1727ba7b6e62SDavid Sterba 	/* Account global block reserve as used, it's in logical size already */
1728ba7b6e62SDavid Sterba 	spin_lock(&block_rsv->lock);
172941b34accSLuis de Bethencourt 	/* Mixed block groups accounting is not byte-accurate, avoid overflow */
173041b34accSLuis de Bethencourt 	if (buf->f_bfree >= block_rsv->size >> bits)
1731ba7b6e62SDavid Sterba 		buf->f_bfree -= block_rsv->size >> bits;
173241b34accSLuis de Bethencourt 	else
173341b34accSLuis de Bethencourt 		buf->f_bfree = 0;
1734ba7b6e62SDavid Sterba 	spin_unlock(&block_rsv->lock);
1735ba7b6e62SDavid Sterba 
17360d95c1beSDavid Sterba 	buf->f_bavail = div_u64(total_free_data, factor);
17376bccf3abSJeff Mahoney 	ret = btrfs_calc_avail_data_space(fs_info, &total_free_data);
17387e33fd99SJosef Bacik 	if (ret)
17396d07bcecSMiao Xie 		return ret;
1740ba7b6e62SDavid Sterba 	buf->f_bavail += div_u64(total_free_data, factor);
17416d07bcecSMiao Xie 	buf->f_bavail = buf->f_bavail >> bits;
1742d397712bSChris Mason 
1743ca8a51b3SDavid Sterba 	/*
1744ca8a51b3SDavid Sterba 	 * We calculate the remaining metadata space minus global reserve. If
1745ca8a51b3SDavid Sterba 	 * this is (supposedly) smaller than zero, there's no space. But this
1746ca8a51b3SDavid Sterba 	 * does not hold in practice, the exhausted state happens where's still
1747ca8a51b3SDavid Sterba 	 * some positive delta. So we apply some guesswork and compare the
1748ca8a51b3SDavid Sterba 	 * delta to a 4M threshold.  (Practically observed delta was ~2M.)
1749ca8a51b3SDavid Sterba 	 *
1750ca8a51b3SDavid Sterba 	 * We probably cannot calculate the exact threshold value because this
1751ca8a51b3SDavid Sterba 	 * depends on the internal reservations requested by various
1752ca8a51b3SDavid Sterba 	 * operations, so some operations that consume a few metadata will
1753ca8a51b3SDavid Sterba 	 * succeed even if the Avail is zero. But this is better than the other
1754ca8a51b3SDavid Sterba 	 * way around.
1755ca8a51b3SDavid Sterba 	 */
1756d4417e22SNikolay Borisov 	thresh = SZ_4M;
1757ca8a51b3SDavid Sterba 
1758d55966c4SJosef Bacik 	/*
1759d55966c4SJosef Bacik 	 * We only want to claim there's no available space if we can no longer
1760d55966c4SJosef Bacik 	 * allocate chunks for our metadata profile and our global reserve will
1761d55966c4SJosef Bacik 	 * not fit in the free metadata space.  If we aren't ->full then we
1762d55966c4SJosef Bacik 	 * still can allocate chunks and thus are fine using the currently
1763d55966c4SJosef Bacik 	 * calculated f_bavail.
1764d55966c4SJosef Bacik 	 */
1765d55966c4SJosef Bacik 	if (!mixed && block_rsv->space_info->full &&
176658bfe2ccSJosef Bacik 	    (total_free_meta < thresh || total_free_meta - thresh < block_rsv->size))
1767ca8a51b3SDavid Sterba 		buf->f_bavail = 0;
1768ca8a51b3SDavid Sterba 
1769ba7b6e62SDavid Sterba 	buf->f_type = BTRFS_SUPER_MAGIC;
1770*4e00422eSDavid Sterba 	buf->f_bsize = fs_info->sectorsize;
1771ba7b6e62SDavid Sterba 	buf->f_namelen = BTRFS_NAME_LEN;
1772ba7b6e62SDavid Sterba 
17739d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
17749d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
17759d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
17769d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
17779d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
177832d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
17794fd786e6SMisono Tomohiro 	buf->f_fsid.val[0] ^=
17804fd786e6SMisono Tomohiro 		BTRFS_I(d_inode(dentry))->root->root_key.objectid >> 32;
17814fd786e6SMisono Tomohiro 	buf->f_fsid.val[1] ^=
17824fd786e6SMisono Tomohiro 		BTRFS_I(d_inode(dentry))->root->root_key.objectid;
178332d48fa1SDavid Woodhouse 
17848fd17795SChris Mason 	return 0;
17858fd17795SChris Mason }
1786b5133862SChris Mason 
17873bb17a25SJosef Bacik static int btrfs_fc_test_super(struct super_block *sb, struct fs_context *fc)
17883bb17a25SJosef Bacik {
17893bb17a25SJosef Bacik 	struct btrfs_fs_info *p = fc->s_fs_info;
17903bb17a25SJosef Bacik 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
17913bb17a25SJosef Bacik 
17923bb17a25SJosef Bacik 	return fs_info->fs_devices == p->fs_devices;
17933bb17a25SJosef Bacik }
17943bb17a25SJosef Bacik 
17953bb17a25SJosef Bacik static int btrfs_get_tree_super(struct fs_context *fc)
17963bb17a25SJosef Bacik {
17973bb17a25SJosef Bacik 	struct btrfs_fs_info *fs_info = fc->s_fs_info;
17983bb17a25SJosef Bacik 	struct btrfs_fs_context *ctx = fc->fs_private;
17993bb17a25SJosef Bacik 	struct btrfs_fs_devices *fs_devices = NULL;
18003bb17a25SJosef Bacik 	struct block_device *bdev;
18013bb17a25SJosef Bacik 	struct btrfs_device *device;
18023bb17a25SJosef Bacik 	struct super_block *sb;
1803affc5af3SLinus Torvalds 	blk_mode_t mode = btrfs_open_mode(fc);
18043bb17a25SJosef Bacik 	int ret;
18053bb17a25SJosef Bacik 
18063bb17a25SJosef Bacik 	btrfs_ctx_to_info(fs_info, ctx);
18073bb17a25SJosef Bacik 	mutex_lock(&uuid_mutex);
18083bb17a25SJosef Bacik 
18093bb17a25SJosef Bacik 	/*
18103bb17a25SJosef Bacik 	 * With 'true' passed to btrfs_scan_one_device() (mount time) we expect
18113bb17a25SJosef Bacik 	 * either a valid device or an error.
18123bb17a25SJosef Bacik 	 */
18133bb17a25SJosef Bacik 	device = btrfs_scan_one_device(fc->source, mode, true);
18143bb17a25SJosef Bacik 	ASSERT(device != NULL);
18153bb17a25SJosef Bacik 	if (IS_ERR(device)) {
18163bb17a25SJosef Bacik 		mutex_unlock(&uuid_mutex);
18173bb17a25SJosef Bacik 		return PTR_ERR(device);
18183bb17a25SJosef Bacik 	}
18193bb17a25SJosef Bacik 
18203bb17a25SJosef Bacik 	fs_devices = device->fs_devices;
18213bb17a25SJosef Bacik 	fs_info->fs_devices = fs_devices;
18223bb17a25SJosef Bacik 
18233bb17a25SJosef Bacik 	ret = btrfs_open_devices(fs_devices, mode, &btrfs_fs_type);
18243bb17a25SJosef Bacik 	mutex_unlock(&uuid_mutex);
18253bb17a25SJosef Bacik 	if (ret)
18263bb17a25SJosef Bacik 		return ret;
18273bb17a25SJosef Bacik 
18283bb17a25SJosef Bacik 	if (!(fc->sb_flags & SB_RDONLY) && fs_devices->rw_devices == 0) {
18293bb17a25SJosef Bacik 		ret = -EACCES;
18303bb17a25SJosef Bacik 		goto error;
18313bb17a25SJosef Bacik 	}
18323bb17a25SJosef Bacik 
18333bb17a25SJosef Bacik 	bdev = fs_devices->latest_dev->bdev;
18343bb17a25SJosef Bacik 
18353bb17a25SJosef Bacik 	/*
18363bb17a25SJosef Bacik 	 * From now on the error handling is not straightforward.
18373bb17a25SJosef Bacik 	 *
18383bb17a25SJosef Bacik 	 * If successful, this will transfer the fs_info into the super block,
18393bb17a25SJosef Bacik 	 * and fc->s_fs_info will be NULL.  However if there's an existing
18403bb17a25SJosef Bacik 	 * super, we'll still have fc->s_fs_info populated.  If we error
18413bb17a25SJosef Bacik 	 * completely out it'll be cleaned up when we drop the fs_context,
18423bb17a25SJosef Bacik 	 * otherwise it's tied to the lifetime of the super_block.
18433bb17a25SJosef Bacik 	 */
18443bb17a25SJosef Bacik 	sb = sget_fc(fc, btrfs_fc_test_super, set_anon_super_fc);
18453bb17a25SJosef Bacik 	if (IS_ERR(sb)) {
18463bb17a25SJosef Bacik 		ret = PTR_ERR(sb);
18473bb17a25SJosef Bacik 		goto error;
18483bb17a25SJosef Bacik 	}
18493bb17a25SJosef Bacik 
185041d46b29SJosef Bacik 	set_device_specific_options(fs_info);
185141d46b29SJosef Bacik 
18523bb17a25SJosef Bacik 	if (sb->s_root) {
18533bb17a25SJosef Bacik 		btrfs_close_devices(fs_devices);
18543bb17a25SJosef Bacik 		if ((fc->sb_flags ^ sb->s_flags) & SB_RDONLY)
18553bb17a25SJosef Bacik 			ret = -EBUSY;
18563bb17a25SJosef Bacik 	} else {
18573bb17a25SJosef Bacik 		snprintf(sb->s_id, sizeof(sb->s_id), "%pg", bdev);
18583bb17a25SJosef Bacik 		shrinker_debugfs_rename(sb->s_shrink, "sb-btrfs:%s", sb->s_id);
18593bb17a25SJosef Bacik 		btrfs_sb(sb)->bdev_holder = &btrfs_fs_type;
18603bb17a25SJosef Bacik 		ret = btrfs_fill_super(sb, fs_devices, NULL);
18613bb17a25SJosef Bacik 	}
18623bb17a25SJosef Bacik 
18633bb17a25SJosef Bacik 	if (ret) {
18643bb17a25SJosef Bacik 		deactivate_locked_super(sb);
18653bb17a25SJosef Bacik 		return ret;
18663bb17a25SJosef Bacik 	}
18673bb17a25SJosef Bacik 
186883e3a40aSJosef Bacik 	btrfs_clear_oneshot_options(fs_info);
186983e3a40aSJosef Bacik 
18703bb17a25SJosef Bacik 	fc->root = dget(sb->s_root);
18713bb17a25SJosef Bacik 	return 0;
18723bb17a25SJosef Bacik 
18733bb17a25SJosef Bacik error:
18743bb17a25SJosef Bacik 	btrfs_close_devices(fs_devices);
18753bb17a25SJosef Bacik 	return ret;
18763bb17a25SJosef Bacik }
18773bb17a25SJosef Bacik 
1878f044b318SJosef Bacik /*
1879f044b318SJosef Bacik  * Ever since commit 0723a0473fb4 ("btrfs: allow mounting btrfs subvolumes
1880f044b318SJosef Bacik  * with different ro/rw options") the following works:
1881f044b318SJosef Bacik  *
1882f044b318SJosef Bacik  *        (i) mount /dev/sda3 -o subvol=foo,ro /mnt/foo
1883f044b318SJosef Bacik  *       (ii) mount /dev/sda3 -o subvol=bar,rw /mnt/bar
1884f044b318SJosef Bacik  *
1885f044b318SJosef Bacik  * which looks nice and innocent but is actually pretty intricate and deserves
1886f044b318SJosef Bacik  * a long comment.
1887f044b318SJosef Bacik  *
1888f044b318SJosef Bacik  * On another filesystem a subvolume mount is close to something like:
1889f044b318SJosef Bacik  *
1890f044b318SJosef Bacik  *	(iii) # create rw superblock + initial mount
1891f044b318SJosef Bacik  *	      mount -t xfs /dev/sdb /opt/
1892f044b318SJosef Bacik  *
1893f044b318SJosef Bacik  *	      # create ro bind mount
1894f044b318SJosef Bacik  *	      mount --bind -o ro /opt/foo /mnt/foo
1895f044b318SJosef Bacik  *
1896f044b318SJosef Bacik  *	      # unmount initial mount
1897f044b318SJosef Bacik  *	      umount /opt
1898f044b318SJosef Bacik  *
1899f044b318SJosef Bacik  * Of course, there's some special subvolume sauce and there's the fact that the
1900f044b318SJosef Bacik  * sb->s_root dentry is really swapped after mount_subtree(). But conceptually
1901f044b318SJosef Bacik  * it's very close and will help us understand the issue.
1902f044b318SJosef Bacik  *
1903f044b318SJosef Bacik  * The old mount API didn't cleanly distinguish between a mount being made ro
1904f044b318SJosef Bacik  * and a superblock being made ro.  The only way to change the ro state of
1905f044b318SJosef Bacik  * either object was by passing ms_rdonly. If a new mount was created via
1906f044b318SJosef Bacik  * mount(2) such as:
1907f044b318SJosef Bacik  *
1908f044b318SJosef Bacik  *      mount("/dev/sdb", "/mnt", "xfs", ms_rdonly, null);
1909f044b318SJosef Bacik  *
1910f044b318SJosef Bacik  * the MS_RDONLY flag being specified had two effects:
1911f044b318SJosef Bacik  *
1912f044b318SJosef Bacik  * (1) MNT_READONLY was raised -> the resulting mount got
1913f044b318SJosef Bacik  *     @mnt->mnt_flags |= MNT_READONLY raised.
1914f044b318SJosef Bacik  *
1915f044b318SJosef Bacik  * (2) MS_RDONLY was passed to the filesystem's mount method and the filesystems
1916f044b318SJosef Bacik  *     made the superblock ro. Note, how SB_RDONLY has the same value as
1917f044b318SJosef Bacik  *     ms_rdonly and is raised whenever MS_RDONLY is passed through mount(2).
1918f044b318SJosef Bacik  *
1919f044b318SJosef Bacik  * Creating a subtree mount via (iii) ends up leaving a rw superblock with a
1920f044b318SJosef Bacik  * subtree mounted ro.
1921f044b318SJosef Bacik  *
1922f044b318SJosef Bacik  * But consider the effect on the old mount API on btrfs subvolume mounting
1923f044b318SJosef Bacik  * which combines the distinct step in (iii) into a single step.
1924f044b318SJosef Bacik  *
1925f044b318SJosef Bacik  * By issuing (i) both the mount and the superblock are turned ro. Now when (ii)
1926f044b318SJosef Bacik  * is issued the superblock is ro and thus even if the mount created for (ii) is
1927f044b318SJosef Bacik  * rw it wouldn't help. Hence, btrfs needed to transition the superblock from ro
1928f044b318SJosef Bacik  * to rw for (ii) which it did using an internal remount call.
1929f044b318SJosef Bacik  *
1930f044b318SJosef Bacik  * IOW, subvolume mounting was inherently complicated due to the ambiguity of
1931f044b318SJosef Bacik  * MS_RDONLY in mount(2). Note, this ambiguity has mount(8) always translate
1932f044b318SJosef Bacik  * "ro" to MS_RDONLY. IOW, in both (i) and (ii) "ro" becomes MS_RDONLY when
1933f044b318SJosef Bacik  * passed by mount(8) to mount(2).
1934f044b318SJosef Bacik  *
1935f044b318SJosef Bacik  * Enter the new mount API. The new mount API disambiguates making a mount ro
1936f044b318SJosef Bacik  * and making a superblock ro.
1937f044b318SJosef Bacik  *
1938f044b318SJosef Bacik  * (3) To turn a mount ro the MOUNT_ATTR_ONLY flag can be used with either
1939f044b318SJosef Bacik  *     fsmount() or mount_setattr() this is a pure VFS level change for a
1940f044b318SJosef Bacik  *     specific mount or mount tree that is never seen by the filesystem itself.
1941f044b318SJosef Bacik  *
1942f044b318SJosef Bacik  * (4) To turn a superblock ro the "ro" flag must be used with
1943f044b318SJosef Bacik  *     fsconfig(FSCONFIG_SET_FLAG, "ro"). This option is seen by the filesystem
1944f044b318SJosef Bacik  *     in fc->sb_flags.
1945f044b318SJosef Bacik  *
1946f044b318SJosef Bacik  * This disambiguation has rather positive consequences.  Mounting a subvolume
1947f044b318SJosef Bacik  * ro will not also turn the superblock ro. Only the mount for the subvolume
1948f044b318SJosef Bacik  * will become ro.
1949f044b318SJosef Bacik  *
1950f044b318SJosef Bacik  * So, if the superblock creation request comes from the new mount API the
1951f044b318SJosef Bacik  * caller must have explicitly done:
1952f044b318SJosef Bacik  *
1953f044b318SJosef Bacik  *      fsconfig(FSCONFIG_SET_FLAG, "ro")
1954f044b318SJosef Bacik  *      fsmount/mount_setattr(MOUNT_ATTR_RDONLY)
1955f044b318SJosef Bacik  *
1956f044b318SJosef Bacik  * IOW, at some point the caller must have explicitly turned the whole
1957f044b318SJosef Bacik  * superblock ro and we shouldn't just undo it like we did for the old mount
1958f044b318SJosef Bacik  * API. In any case, it lets us avoid the hack in the new mount API.
1959f044b318SJosef Bacik  *
1960f044b318SJosef Bacik  * Consequently, the remounting hack must only be used for requests originating
1961f044b318SJosef Bacik  * from the old mount API and should be marked for full deprecation so it can be
1962f044b318SJosef Bacik  * turned off in a couple of years.
1963f044b318SJosef Bacik  *
1964f044b318SJosef Bacik  * The new mount API has no reason to support this hack.
1965f044b318SJosef Bacik  */
1966f044b318SJosef Bacik static struct vfsmount *btrfs_reconfigure_for_mount(struct fs_context *fc)
1967f044b318SJosef Bacik {
1968f044b318SJosef Bacik 	struct vfsmount *mnt;
1969f044b318SJosef Bacik 	int ret;
1970f044b318SJosef Bacik 	const bool ro2rw = !(fc->sb_flags & SB_RDONLY);
1971f044b318SJosef Bacik 
1972f044b318SJosef Bacik 	/*
1973f044b318SJosef Bacik 	 * We got an EBUSY because our SB_RDONLY flag didn't match the existing
1974f044b318SJosef Bacik 	 * super block, so invert our setting here and retry the mount so we
1975f044b318SJosef Bacik 	 * can get our vfsmount.
1976f044b318SJosef Bacik 	 */
1977f044b318SJosef Bacik 	if (ro2rw)
1978f044b318SJosef Bacik 		fc->sb_flags |= SB_RDONLY;
1979f044b318SJosef Bacik 	else
1980f044b318SJosef Bacik 		fc->sb_flags &= ~SB_RDONLY;
1981f044b318SJosef Bacik 
1982f044b318SJosef Bacik 	mnt = fc_mount(fc);
1983f044b318SJosef Bacik 	if (IS_ERR(mnt))
1984f044b318SJosef Bacik 		return mnt;
1985f044b318SJosef Bacik 
1986f044b318SJosef Bacik 	if (!fc->oldapi || !ro2rw)
1987f044b318SJosef Bacik 		return mnt;
1988f044b318SJosef Bacik 
1989f044b318SJosef Bacik 	/* We need to convert to rw, call reconfigure. */
1990f044b318SJosef Bacik 	fc->sb_flags &= ~SB_RDONLY;
1991f044b318SJosef Bacik 	down_write(&mnt->mnt_sb->s_umount);
1992f044b318SJosef Bacik 	ret = btrfs_reconfigure(fc);
1993f044b318SJosef Bacik 	up_write(&mnt->mnt_sb->s_umount);
1994f044b318SJosef Bacik 	if (ret) {
1995f044b318SJosef Bacik 		mntput(mnt);
1996f044b318SJosef Bacik 		return ERR_PTR(ret);
1997f044b318SJosef Bacik 	}
1998f044b318SJosef Bacik 	return mnt;
1999f044b318SJosef Bacik }
2000f044b318SJosef Bacik 
20013bb17a25SJosef Bacik static int btrfs_get_tree_subvol(struct fs_context *fc)
20023bb17a25SJosef Bacik {
20033bb17a25SJosef Bacik 	struct btrfs_fs_info *fs_info = NULL;
20043bb17a25SJosef Bacik 	struct btrfs_fs_context *ctx = fc->fs_private;
20053bb17a25SJosef Bacik 	struct fs_context *dup_fc;
20063bb17a25SJosef Bacik 	struct dentry *dentry;
20073bb17a25SJosef Bacik 	struct vfsmount *mnt;
20083bb17a25SJosef Bacik 
20093bb17a25SJosef Bacik 	/*
20103bb17a25SJosef Bacik 	 * Setup a dummy root and fs_info for test/set super.  This is because
20113bb17a25SJosef Bacik 	 * we don't actually fill this stuff out until open_ctree, but we need
20123bb17a25SJosef Bacik 	 * then open_ctree will properly initialize the file system specific
20133bb17a25SJosef Bacik 	 * settings later.  btrfs_init_fs_info initializes the static elements
20143bb17a25SJosef Bacik 	 * of the fs_info (locks and such) to make cleanup easier if we find a
20153bb17a25SJosef Bacik 	 * superblock with our given fs_devices later on at sget() time.
20163bb17a25SJosef Bacik 	 */
20173bb17a25SJosef Bacik 	fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL);
20183bb17a25SJosef Bacik 	if (!fs_info)
20193bb17a25SJosef Bacik 		return -ENOMEM;
20203bb17a25SJosef Bacik 
20213bb17a25SJosef Bacik 	fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
20223bb17a25SJosef Bacik 	fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
20233bb17a25SJosef Bacik 	if (!fs_info->super_copy || !fs_info->super_for_commit) {
20243bb17a25SJosef Bacik 		btrfs_free_fs_info(fs_info);
20253bb17a25SJosef Bacik 		return -ENOMEM;
20263bb17a25SJosef Bacik 	}
20273bb17a25SJosef Bacik 	btrfs_init_fs_info(fs_info);
20283bb17a25SJosef Bacik 
20293bb17a25SJosef Bacik 	dup_fc = vfs_dup_fs_context(fc);
20303bb17a25SJosef Bacik 	if (IS_ERR(dup_fc)) {
20313bb17a25SJosef Bacik 		btrfs_free_fs_info(fs_info);
20323bb17a25SJosef Bacik 		return PTR_ERR(dup_fc);
20333bb17a25SJosef Bacik 	}
20343bb17a25SJosef Bacik 
20353bb17a25SJosef Bacik 	/*
20363bb17a25SJosef Bacik 	 * When we do the sget_fc this gets transferred to the sb, so we only
20373bb17a25SJosef Bacik 	 * need to set it on the dup_fc as this is what creates the super block.
20383bb17a25SJosef Bacik 	 */
20393bb17a25SJosef Bacik 	dup_fc->s_fs_info = fs_info;
20403bb17a25SJosef Bacik 
20413bb17a25SJosef Bacik 	/*
20423bb17a25SJosef Bacik 	 * We'll do the security settings in our btrfs_get_tree_super() mount
20433bb17a25SJosef Bacik 	 * loop, they were duplicated into dup_fc, we can drop the originals
20443bb17a25SJosef Bacik 	 * here.
20453bb17a25SJosef Bacik 	 */
20463bb17a25SJosef Bacik 	security_free_mnt_opts(&fc->security);
20473bb17a25SJosef Bacik 	fc->security = NULL;
20483bb17a25SJosef Bacik 
20493bb17a25SJosef Bacik 	mnt = fc_mount(dup_fc);
2050f044b318SJosef Bacik 	if (PTR_ERR_OR_ZERO(mnt) == -EBUSY)
2051f044b318SJosef Bacik 		mnt = btrfs_reconfigure_for_mount(dup_fc);
20523bb17a25SJosef Bacik 	put_fs_context(dup_fc);
20533bb17a25SJosef Bacik 	if (IS_ERR(mnt))
20543bb17a25SJosef Bacik 		return PTR_ERR(mnt);
20553bb17a25SJosef Bacik 
20563bb17a25SJosef Bacik 	/*
20573bb17a25SJosef Bacik 	 * This free's ->subvol_name, because if it isn't set we have to
20583bb17a25SJosef Bacik 	 * allocate a buffer to hold the subvol_name, so we just drop our
20593bb17a25SJosef Bacik 	 * reference to it here.
20603bb17a25SJosef Bacik 	 */
20613bb17a25SJosef Bacik 	dentry = mount_subvol(ctx->subvol_name, ctx->subvol_objectid, mnt);
20623bb17a25SJosef Bacik 	ctx->subvol_name = NULL;
20633bb17a25SJosef Bacik 	if (IS_ERR(dentry))
20643bb17a25SJosef Bacik 		return PTR_ERR(dentry);
20653bb17a25SJosef Bacik 
20663bb17a25SJosef Bacik 	fc->root = dentry;
20673bb17a25SJosef Bacik 	return 0;
20683bb17a25SJosef Bacik }
20693bb17a25SJosef Bacik 
20703bb17a25SJosef Bacik static int btrfs_get_tree(struct fs_context *fc)
20713bb17a25SJosef Bacik {
20723bb17a25SJosef Bacik 	/*
20733bb17a25SJosef Bacik 	 * Since we use mount_subtree to mount the default/specified subvol, we
20743bb17a25SJosef Bacik 	 * have to do mounts in two steps.
20753bb17a25SJosef Bacik 	 *
20763bb17a25SJosef Bacik 	 * First pass through we call btrfs_get_tree_subvol(), this is just a
20773bb17a25SJosef Bacik 	 * wrapper around fc_mount() to call back into here again, and this time
20783bb17a25SJosef Bacik 	 * we'll call btrfs_get_tree_super().  This will do the open_ctree() and
20793bb17a25SJosef Bacik 	 * everything to open the devices and file system.  Then we return back
20803bb17a25SJosef Bacik 	 * with a fully constructed vfsmount in btrfs_get_tree_subvol(), and
20813bb17a25SJosef Bacik 	 * from there we can do our mount_subvol() call, which will lookup
20823bb17a25SJosef Bacik 	 * whichever subvol we're mounting and setup this fc with the
20833bb17a25SJosef Bacik 	 * appropriate dentry for the subvol.
20843bb17a25SJosef Bacik 	 */
20853bb17a25SJosef Bacik 	if (fc->s_fs_info)
20863bb17a25SJosef Bacik 		return btrfs_get_tree_super(fc);
20873bb17a25SJosef Bacik 	return btrfs_get_tree_subvol(fc);
20883bb17a25SJosef Bacik }
20893bb17a25SJosef Bacik 
2090aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb)
2091aea52e19SAl Viro {
2092815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2093aea52e19SAl Viro 	kill_anon_super(sb);
20940d4b0463SJosef Bacik 	btrfs_free_fs_info(fs_info);
2095aea52e19SAl Viro }
2096aea52e19SAl Viro 
20970f85e244SJosef Bacik static void btrfs_free_fs_context(struct fs_context *fc)
20980f85e244SJosef Bacik {
20990f85e244SJosef Bacik 	struct btrfs_fs_context *ctx = fc->fs_private;
21003bb17a25SJosef Bacik 	struct btrfs_fs_info *fs_info = fc->s_fs_info;
21010f85e244SJosef Bacik 
21023bb17a25SJosef Bacik 	if (fs_info)
21033bb17a25SJosef Bacik 		btrfs_free_fs_info(fs_info);
21040f85e244SJosef Bacik 
21053bb17a25SJosef Bacik 	if (ctx && refcount_dec_and_test(&ctx->refs)) {
21060f85e244SJosef Bacik 		kfree(ctx->subvol_name);
21070f85e244SJosef Bacik 		kfree(ctx);
21080f85e244SJosef Bacik 	}
21093bb17a25SJosef Bacik }
21103bb17a25SJosef Bacik 
21113bb17a25SJosef Bacik static int btrfs_dup_fs_context(struct fs_context *fc, struct fs_context *src_fc)
21123bb17a25SJosef Bacik {
21133bb17a25SJosef Bacik 	struct btrfs_fs_context *ctx = src_fc->fs_private;
21143bb17a25SJosef Bacik 
21153bb17a25SJosef Bacik 	/*
21163bb17a25SJosef Bacik 	 * Give a ref to our ctx to this dup, as we want to keep it around for
21173bb17a25SJosef Bacik 	 * our original fc so we can have the subvolume name or objectid.
21183bb17a25SJosef Bacik 	 *
21193bb17a25SJosef Bacik 	 * We unset ->source in the original fc because the dup needs it for
21203bb17a25SJosef Bacik 	 * mounting, and then once we free the dup it'll free ->source, so we
21213bb17a25SJosef Bacik 	 * need to make sure we're only pointing to it in one fc.
21223bb17a25SJosef Bacik 	 */
21233bb17a25SJosef Bacik 	refcount_inc(&ctx->refs);
21243bb17a25SJosef Bacik 	fc->fs_private = ctx;
21253bb17a25SJosef Bacik 	fc->source = src_fc->source;
21263bb17a25SJosef Bacik 	src_fc->source = NULL;
21273bb17a25SJosef Bacik 	return 0;
21283bb17a25SJosef Bacik }
21290f85e244SJosef Bacik 
21300f85e244SJosef Bacik static const struct fs_context_operations btrfs_fs_context_ops = {
213117b36120SJosef Bacik 	.parse_param	= btrfs_parse_param,
2132eddb1a43SJosef Bacik 	.reconfigure	= btrfs_reconfigure,
21333bb17a25SJosef Bacik 	.get_tree	= btrfs_get_tree,
21343bb17a25SJosef Bacik 	.dup		= btrfs_dup_fs_context,
21350f85e244SJosef Bacik 	.free		= btrfs_free_fs_context,
213617b36120SJosef Bacik };
213717b36120SJosef Bacik 
2138ad21f15bSJosef Bacik static int btrfs_init_fs_context(struct fs_context *fc)
21390f85e244SJosef Bacik {
21400f85e244SJosef Bacik 	struct btrfs_fs_context *ctx;
21410f85e244SJosef Bacik 
21420f85e244SJosef Bacik 	ctx = kzalloc(sizeof(struct btrfs_fs_context), GFP_KERNEL);
21430f85e244SJosef Bacik 	if (!ctx)
21440f85e244SJosef Bacik 		return -ENOMEM;
21450f85e244SJosef Bacik 
21463bb17a25SJosef Bacik 	refcount_set(&ctx->refs, 1);
21470f85e244SJosef Bacik 	fc->fs_private = ctx;
21480f85e244SJosef Bacik 	fc->ops = &btrfs_fs_context_ops;
21490f85e244SJosef Bacik 
2150eddb1a43SJosef Bacik 	if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
2151eddb1a43SJosef Bacik 		btrfs_info_to_ctx(btrfs_sb(fc->root->d_sb), ctx);
2152eddb1a43SJosef Bacik 	} else {
2153eddb1a43SJosef Bacik 		ctx->thread_pool_size =
2154eddb1a43SJosef Bacik 			min_t(unsigned long, num_online_cpus() + 2, 8);
2155eddb1a43SJosef Bacik 		ctx->max_inline = BTRFS_DEFAULT_MAX_INLINE;
2156eddb1a43SJosef Bacik 		ctx->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
2157eddb1a43SJosef Bacik 	}
2158eddb1a43SJosef Bacik 
2159ad21f15bSJosef Bacik #ifdef CONFIG_BTRFS_FS_POSIX_ACL
2160ad21f15bSJosef Bacik 	fc->sb_flags |= SB_POSIXACL;
2161ad21f15bSJosef Bacik #endif
2162ad21f15bSJosef Bacik 	fc->sb_flags |= SB_I_VERSION;
2163ad21f15bSJosef Bacik 
21640f85e244SJosef Bacik 	return 0;
21650f85e244SJosef Bacik }
21660f85e244SJosef Bacik 
21672e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
21682e635a27SChris Mason 	.owner			= THIS_MODULE,
21692e635a27SChris Mason 	.name			= "btrfs",
2170ad21f15bSJosef Bacik 	.init_fs_context	= btrfs_init_fs_context,
2171ad21f15bSJosef Bacik 	.parameters		= btrfs_fs_parameters,
217272fa39f5SMisono, Tomohiro 	.kill_sb		= btrfs_kill_super,
21735b9b26f5SChristian Brauner 	.fs_flags		= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA | FS_ALLOW_IDMAP,
217472fa39f5SMisono, Tomohiro  };
217572fa39f5SMisono, Tomohiro 
21767f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs");
2177a9218f6bSChris Mason 
2178d8620958STom Van Braeckel static int btrfs_control_open(struct inode *inode, struct file *file)
2179d8620958STom Van Braeckel {
2180d8620958STom Van Braeckel 	/*
2181d8620958STom Van Braeckel 	 * The control file's private_data is used to hold the
2182d8620958STom Van Braeckel 	 * transaction when it is started and is used to keep
2183d8620958STom Van Braeckel 	 * track of whether a transaction is already in progress.
2184d8620958STom Van Braeckel 	 */
2185d8620958STom Van Braeckel 	file->private_data = NULL;
2186d8620958STom Van Braeckel 	return 0;
2187d8620958STom Van Braeckel }
2188d8620958STom Van Braeckel 
2189d352ac68SChris Mason /*
2190cfe953c8SSu Yue  * Used by /dev/btrfs-control for devices ioctls.
2191d352ac68SChris Mason  */
21928a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
21938a4b83ccSChris Mason 				unsigned long arg)
21948a4b83ccSChris Mason {
21958a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
219636350e95SGu Jinxiang 	struct btrfs_device *device = NULL;
219716cab91aSAnand Jain 	dev_t devt = 0;
2198c071fcfdSChris Mason 	int ret = -ENOTTY;
21998a4b83ccSChris Mason 
2200e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2201e441d54dSChris Mason 		return -EPERM;
2202e441d54dSChris Mason 
2203dae7b665SLi Zefan 	vol = memdup_user((void __user *)arg, sizeof(*vol));
2204dae7b665SLi Zefan 	if (IS_ERR(vol))
2205dae7b665SLi Zefan 		return PTR_ERR(vol);
2206f505754fSFilipe Manana 	vol->name[BTRFS_PATH_NAME_MAX] = '\0';
2207c071fcfdSChris Mason 
22088a4b83ccSChris Mason 	switch (cmd) {
22098a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
2210899f9307SDavid Sterba 		mutex_lock(&uuid_mutex);
2211bc27d6f0SAnand Jain 		/*
2212bc27d6f0SAnand Jain 		 * Scanning outside of mount can return NULL which would turn
2213bc27d6f0SAnand Jain 		 * into 0 error code.
2214bc27d6f0SAnand Jain 		 */
2215bc27d6f0SAnand Jain 		device = btrfs_scan_one_device(vol->name, BLK_OPEN_READ, false);
221636350e95SGu Jinxiang 		ret = PTR_ERR_OR_ZERO(device);
2217899f9307SDavid Sterba 		mutex_unlock(&uuid_mutex);
22188a4b83ccSChris Mason 		break;
2219228a73abSAnand Jain 	case BTRFS_IOC_FORGET_DEV:
222016cab91aSAnand Jain 		if (vol->name[0] != 0) {
222116cab91aSAnand Jain 			ret = lookup_bdev(vol->name, &devt);
222216cab91aSAnand Jain 			if (ret)
222316cab91aSAnand Jain 				break;
222416cab91aSAnand Jain 		}
222516cab91aSAnand Jain 		ret = btrfs_forget_devices(devt);
2226228a73abSAnand Jain 		break;
222702db0844SJosef Bacik 	case BTRFS_IOC_DEVICES_READY:
2228899f9307SDavid Sterba 		mutex_lock(&uuid_mutex);
2229bc27d6f0SAnand Jain 		/*
2230bc27d6f0SAnand Jain 		 * Scanning outside of mount can return NULL which would turn
2231bc27d6f0SAnand Jain 		 * into 0 error code.
2232bc27d6f0SAnand Jain 		 */
2233bc27d6f0SAnand Jain 		device = btrfs_scan_one_device(vol->name, BLK_OPEN_READ, false);
2234bc27d6f0SAnand Jain 		if (IS_ERR_OR_NULL(device)) {
2235899f9307SDavid Sterba 			mutex_unlock(&uuid_mutex);
223636350e95SGu Jinxiang 			ret = PTR_ERR(device);
223702db0844SJosef Bacik 			break;
2238899f9307SDavid Sterba 		}
223936350e95SGu Jinxiang 		ret = !(device->fs_devices->num_devices ==
224036350e95SGu Jinxiang 			device->fs_devices->total_devices);
2241899f9307SDavid Sterba 		mutex_unlock(&uuid_mutex);
224202db0844SJosef Bacik 		break;
2243c5868f83SDavid Sterba 	case BTRFS_IOC_GET_SUPPORTED_FEATURES:
2244d5131b65SDavid Sterba 		ret = btrfs_ioctl_get_supported_features((void __user*)arg);
2245c5868f83SDavid Sterba 		break;
22468a4b83ccSChris Mason 	}
2247dae7b665SLi Zefan 
22488a4b83ccSChris Mason 	kfree(vol);
2249f819d837SLinda Knippers 	return ret;
22508a4b83ccSChris Mason }
22518a4b83ccSChris Mason 
22520176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
2253ed0dab6bSYan {
2254354aa0fbSMiao Xie 	struct btrfs_trans_handle *trans;
22550b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
22560b246afaSJeff Mahoney 	struct btrfs_root *root = fs_info->tree_root;
2257354aa0fbSMiao Xie 
2258fac03c8dSDavid Sterba 	set_bit(BTRFS_FS_FROZEN, &fs_info->flags);
22599e7cc91aSWang Xiaoguang 	/*
22609e7cc91aSWang Xiaoguang 	 * We don't need a barrier here, we'll wait for any transaction that
22619e7cc91aSWang Xiaoguang 	 * could be in progress on other threads (and do delayed iputs that
22629e7cc91aSWang Xiaoguang 	 * we want to avoid on a frozen filesystem), or do the commit
22639e7cc91aSWang Xiaoguang 	 * ourselves.
22649e7cc91aSWang Xiaoguang 	 */
2265d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
2266354aa0fbSMiao Xie 	if (IS_ERR(trans)) {
2267354aa0fbSMiao Xie 		/* no transaction, don't bother */
2268354aa0fbSMiao Xie 		if (PTR_ERR(trans) == -ENOENT)
22690176260fSLinus Torvalds 			return 0;
2270354aa0fbSMiao Xie 		return PTR_ERR(trans);
2271354aa0fbSMiao Xie 	}
22723a45bb20SJeff Mahoney 	return btrfs_commit_transaction(trans);
2273ed0dab6bSYan }
2274ed0dab6bSYan 
2275a05d3c91SQu Wenruo static int check_dev_super(struct btrfs_device *dev)
2276a05d3c91SQu Wenruo {
2277a05d3c91SQu Wenruo 	struct btrfs_fs_info *fs_info = dev->fs_info;
2278a05d3c91SQu Wenruo 	struct btrfs_super_block *sb;
22790124855fSFilipe Manana 	u64 last_trans;
22803d17adeaSQu Wenruo 	u16 csum_type;
2281a05d3c91SQu Wenruo 	int ret = 0;
2282a05d3c91SQu Wenruo 
2283a05d3c91SQu Wenruo 	/* This should be called with fs still frozen. */
2284a05d3c91SQu Wenruo 	ASSERT(test_bit(BTRFS_FS_FROZEN, &fs_info->flags));
2285a05d3c91SQu Wenruo 
2286a05d3c91SQu Wenruo 	/* Missing dev, no need to check. */
2287a05d3c91SQu Wenruo 	if (!dev->bdev)
2288a05d3c91SQu Wenruo 		return 0;
2289a05d3c91SQu Wenruo 
2290a05d3c91SQu Wenruo 	/* Only need to check the primary super block. */
2291a05d3c91SQu Wenruo 	sb = btrfs_read_dev_one_super(dev->bdev, 0, true);
2292a05d3c91SQu Wenruo 	if (IS_ERR(sb))
2293a05d3c91SQu Wenruo 		return PTR_ERR(sb);
2294a05d3c91SQu Wenruo 
22953d17adeaSQu Wenruo 	/* Verify the checksum. */
22963d17adeaSQu Wenruo 	csum_type = btrfs_super_csum_type(sb);
22973d17adeaSQu Wenruo 	if (csum_type != btrfs_super_csum_type(fs_info->super_copy)) {
22983d17adeaSQu Wenruo 		btrfs_err(fs_info, "csum type changed, has %u expect %u",
22993d17adeaSQu Wenruo 			  csum_type, btrfs_super_csum_type(fs_info->super_copy));
23003d17adeaSQu Wenruo 		ret = -EUCLEAN;
23013d17adeaSQu Wenruo 		goto out;
23023d17adeaSQu Wenruo 	}
23033d17adeaSQu Wenruo 
23043d17adeaSQu Wenruo 	if (btrfs_check_super_csum(fs_info, sb)) {
23053d17adeaSQu Wenruo 		btrfs_err(fs_info, "csum for on-disk super block no longer matches");
23063d17adeaSQu Wenruo 		ret = -EUCLEAN;
23073d17adeaSQu Wenruo 		goto out;
23083d17adeaSQu Wenruo 	}
23093d17adeaSQu Wenruo 
2310a05d3c91SQu Wenruo 	/* Btrfs_validate_super() includes fsid check against super->fsid. */
2311a05d3c91SQu Wenruo 	ret = btrfs_validate_super(fs_info, sb, 0);
2312a05d3c91SQu Wenruo 	if (ret < 0)
2313a05d3c91SQu Wenruo 		goto out;
2314a05d3c91SQu Wenruo 
23150124855fSFilipe Manana 	last_trans = btrfs_get_last_trans_committed(fs_info);
23160124855fSFilipe Manana 	if (btrfs_super_generation(sb) != last_trans) {
2317a05d3c91SQu Wenruo 		btrfs_err(fs_info, "transid mismatch, has %llu expect %llu",
23180124855fSFilipe Manana 			  btrfs_super_generation(sb), last_trans);
2319a05d3c91SQu Wenruo 		ret = -EUCLEAN;
2320a05d3c91SQu Wenruo 		goto out;
2321a05d3c91SQu Wenruo 	}
2322a05d3c91SQu Wenruo out:
2323a05d3c91SQu Wenruo 	btrfs_release_disk_super(sb);
2324a05d3c91SQu Wenruo 	return ret;
2325a05d3c91SQu Wenruo }
2326a05d3c91SQu Wenruo 
23279e7cc91aSWang Xiaoguang static int btrfs_unfreeze(struct super_block *sb)
23289e7cc91aSWang Xiaoguang {
2329fac03c8dSDavid Sterba 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2330a05d3c91SQu Wenruo 	struct btrfs_device *device;
2331a05d3c91SQu Wenruo 	int ret = 0;
2332fac03c8dSDavid Sterba 
2333a05d3c91SQu Wenruo 	/*
2334a05d3c91SQu Wenruo 	 * Make sure the fs is not changed by accident (like hibernation then
2335a05d3c91SQu Wenruo 	 * modified by other OS).
2336a05d3c91SQu Wenruo 	 * If we found anything wrong, we mark the fs error immediately.
2337a05d3c91SQu Wenruo 	 *
2338a05d3c91SQu Wenruo 	 * And since the fs is frozen, no one can modify the fs yet, thus
2339a05d3c91SQu Wenruo 	 * we don't need to hold device_list_mutex.
2340a05d3c91SQu Wenruo 	 */
2341a05d3c91SQu Wenruo 	list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
2342a05d3c91SQu Wenruo 		ret = check_dev_super(device);
2343a05d3c91SQu Wenruo 		if (ret < 0) {
2344a05d3c91SQu Wenruo 			btrfs_handle_fs_error(fs_info, ret,
2345a05d3c91SQu Wenruo 				"super block on devid %llu got modified unexpectedly",
2346a05d3c91SQu Wenruo 				device->devid);
2347a05d3c91SQu Wenruo 			break;
2348a05d3c91SQu Wenruo 		}
2349a05d3c91SQu Wenruo 	}
2350fac03c8dSDavid Sterba 	clear_bit(BTRFS_FS_FROZEN, &fs_info->flags);
2351a05d3c91SQu Wenruo 
2352a05d3c91SQu Wenruo 	/*
2353a05d3c91SQu Wenruo 	 * We still return 0, to allow VFS layer to unfreeze the fs even the
2354a05d3c91SQu Wenruo 	 * above checks failed. Since the fs is either fine or read-only, we're
2355a05d3c91SQu Wenruo 	 * safe to continue, without causing further damage.
2356a05d3c91SQu Wenruo 	 */
23579e7cc91aSWang Xiaoguang 	return 0;
23589e7cc91aSWang Xiaoguang }
23599e7cc91aSWang Xiaoguang 
23609c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
23619c5085c1SJosef Bacik {
23629c5085c1SJosef Bacik 	struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
23639c5085c1SJosef Bacik 
236488c14590SDavid Sterba 	/*
23656605fd2fSAnand Jain 	 * There should be always a valid pointer in latest_dev, it may be stale
23666605fd2fSAnand Jain 	 * for a short moment in case it's being deleted but still valid until
23676605fd2fSAnand Jain 	 * the end of RCU grace period.
236888c14590SDavid Sterba 	 */
236988c14590SDavid Sterba 	rcu_read_lock();
2370cb3e217bSQu Wenruo 	seq_escape(m, btrfs_dev_name(fs_info->fs_devices->latest_dev), " \t\n\\");
237188c14590SDavid Sterba 	rcu_read_unlock();
23726605fd2fSAnand Jain 
23739c5085c1SJosef Bacik 	return 0;
23749c5085c1SJosef Bacik }
23759c5085c1SJosef Bacik 
2376b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
237776dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
2378bd555975SAl Viro 	.evict_inode	= btrfs_evict_inode,
2379e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
2380d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
2381a9572a15SEric Paris 	.show_options	= btrfs_show_options,
23829c5085c1SJosef Bacik 	.show_devname	= btrfs_show_devname,
23832c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
23842c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
238526602cabSAl Viro 	.free_inode	= btrfs_free_inode,
23868fd17795SChris Mason 	.statfs		= btrfs_statfs,
23870176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
23889e7cc91aSWang Xiaoguang 	.unfreeze_fs	= btrfs_unfreeze,
2389e20d96d6SChris Mason };
2390a9218f6bSChris Mason 
2391a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
2392d8620958STom Van Braeckel 	.open = btrfs_control_open,
2393a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
23941832f2d8SArnd Bergmann 	.compat_ioctl = compat_ptr_ioctl,
2395a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
23966038f373SArnd Bergmann 	.llseek = noop_llseek,
2397a9218f6bSChris Mason };
2398a9218f6bSChris Mason 
2399a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
2400578454ffSKay Sievers 	.minor		= BTRFS_MINOR,
2401a9218f6bSChris Mason 	.name		= "btrfs-control",
2402a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
2403a9218f6bSChris Mason };
2404a9218f6bSChris Mason 
2405578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
2406578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control");
2407578454ffSKay Sievers 
2408f5c29bd9SLiu Bo static int __init btrfs_interface_init(void)
2409a9218f6bSChris Mason {
2410a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
2411a9218f6bSChris Mason }
2412a9218f6bSChris Mason 
2413e67c718bSDavid Sterba static __cold void btrfs_interface_exit(void)
2414a9218f6bSChris Mason {
2415f368ed60SGreg Kroah-Hartman 	misc_deregister(&btrfs_misc);
2416a9218f6bSChris Mason }
2417a9218f6bSChris Mason 
24185565b8e0SQu Wenruo static int __init btrfs_print_mod_info(void)
241985965600SDavid Sterba {
2420edf57cbfSBart Van Assche 	static const char options[] = ""
242185965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG
242285965600SDavid Sterba 			", debug=on"
242385965600SDavid Sterba #endif
242479556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT
242579556c3dSStefan Behrens 			", assert=on"
242679556c3dSStefan Behrens #endif
2427fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
2428fb592373SJosef Bacik 			", ref-verify=on"
2429fb592373SJosef Bacik #endif
24305b316468SNaohiro Aota #ifdef CONFIG_BLK_DEV_ZONED
24315b316468SNaohiro Aota 			", zoned=yes"
24325b316468SNaohiro Aota #else
24335b316468SNaohiro Aota 			", zoned=no"
24345b316468SNaohiro Aota #endif
2435ea3dc7d2SDavid Sterba #ifdef CONFIG_FS_VERITY
2436ea3dc7d2SDavid Sterba 			", fsverity=yes"
2437ea3dc7d2SDavid Sterba #else
2438ea3dc7d2SDavid Sterba 			", fsverity=no"
2439ea3dc7d2SDavid Sterba #endif
2440edf57cbfSBart Van Assche 			;
24416e7a367eSChristoph Hellwig 	pr_info("Btrfs loaded%s\n", options);
24425565b8e0SQu Wenruo 	return 0;
24435565b8e0SQu Wenruo }
24445565b8e0SQu Wenruo 
24455565b8e0SQu Wenruo static int register_btrfs(void)
24465565b8e0SQu Wenruo {
24475565b8e0SQu Wenruo 	return register_filesystem(&btrfs_fs_type);
24485565b8e0SQu Wenruo }
24495565b8e0SQu Wenruo 
24505565b8e0SQu Wenruo static void unregister_btrfs(void)
24515565b8e0SQu Wenruo {
24525565b8e0SQu Wenruo 	unregister_filesystem(&btrfs_fs_type);
24535565b8e0SQu Wenruo }
24545565b8e0SQu Wenruo 
24555565b8e0SQu Wenruo /* Helper structure for long init/exit functions. */
24565565b8e0SQu Wenruo struct init_sequence {
24575565b8e0SQu Wenruo 	int (*init_func)(void);
24585565b8e0SQu Wenruo 	/* Can be NULL if the init_func doesn't need cleanup. */
24595565b8e0SQu Wenruo 	void (*exit_func)(void);
24605565b8e0SQu Wenruo };
24615565b8e0SQu Wenruo 
24625565b8e0SQu Wenruo static const struct init_sequence mod_init_seq[] = {
24635565b8e0SQu Wenruo 	{
24645565b8e0SQu Wenruo 		.init_func = btrfs_props_init,
24655565b8e0SQu Wenruo 		.exit_func = NULL,
24665565b8e0SQu Wenruo 	}, {
24675565b8e0SQu Wenruo 		.init_func = btrfs_init_sysfs,
24685565b8e0SQu Wenruo 		.exit_func = btrfs_exit_sysfs,
24695565b8e0SQu Wenruo 	}, {
24705565b8e0SQu Wenruo 		.init_func = btrfs_init_compress,
24715565b8e0SQu Wenruo 		.exit_func = btrfs_exit_compress,
24725565b8e0SQu Wenruo 	}, {
24735565b8e0SQu Wenruo 		.init_func = btrfs_init_cachep,
24745565b8e0SQu Wenruo 		.exit_func = btrfs_destroy_cachep,
24755565b8e0SQu Wenruo 	}, {
24765565b8e0SQu Wenruo 		.init_func = btrfs_transaction_init,
24775565b8e0SQu Wenruo 		.exit_func = btrfs_transaction_exit,
24785565b8e0SQu Wenruo 	}, {
24795565b8e0SQu Wenruo 		.init_func = btrfs_ctree_init,
24805565b8e0SQu Wenruo 		.exit_func = btrfs_ctree_exit,
24815565b8e0SQu Wenruo 	}, {
24825565b8e0SQu Wenruo 		.init_func = btrfs_free_space_init,
24835565b8e0SQu Wenruo 		.exit_func = btrfs_free_space_exit,
24845565b8e0SQu Wenruo 	}, {
24855565b8e0SQu Wenruo 		.init_func = extent_state_init_cachep,
24865565b8e0SQu Wenruo 		.exit_func = extent_state_free_cachep,
24875565b8e0SQu Wenruo 	}, {
24885565b8e0SQu Wenruo 		.init_func = extent_buffer_init_cachep,
24895565b8e0SQu Wenruo 		.exit_func = extent_buffer_free_cachep,
24905565b8e0SQu Wenruo 	}, {
24915565b8e0SQu Wenruo 		.init_func = btrfs_bioset_init,
24925565b8e0SQu Wenruo 		.exit_func = btrfs_bioset_exit,
24935565b8e0SQu Wenruo 	}, {
24945565b8e0SQu Wenruo 		.init_func = extent_map_init,
24955565b8e0SQu Wenruo 		.exit_func = extent_map_exit,
24965565b8e0SQu Wenruo 	}, {
24975565b8e0SQu Wenruo 		.init_func = ordered_data_init,
24985565b8e0SQu Wenruo 		.exit_func = ordered_data_exit,
24995565b8e0SQu Wenruo 	}, {
25005565b8e0SQu Wenruo 		.init_func = btrfs_delayed_inode_init,
25015565b8e0SQu Wenruo 		.exit_func = btrfs_delayed_inode_exit,
25025565b8e0SQu Wenruo 	}, {
25035565b8e0SQu Wenruo 		.init_func = btrfs_auto_defrag_init,
25045565b8e0SQu Wenruo 		.exit_func = btrfs_auto_defrag_exit,
25055565b8e0SQu Wenruo 	}, {
25065565b8e0SQu Wenruo 		.init_func = btrfs_delayed_ref_init,
25075565b8e0SQu Wenruo 		.exit_func = btrfs_delayed_ref_exit,
25085565b8e0SQu Wenruo 	}, {
25095565b8e0SQu Wenruo 		.init_func = btrfs_prelim_ref_init,
25105565b8e0SQu Wenruo 		.exit_func = btrfs_prelim_ref_exit,
25115565b8e0SQu Wenruo 	}, {
25125565b8e0SQu Wenruo 		.init_func = btrfs_interface_init,
25135565b8e0SQu Wenruo 		.exit_func = btrfs_interface_exit,
25145565b8e0SQu Wenruo 	}, {
25155565b8e0SQu Wenruo 		.init_func = btrfs_print_mod_info,
25165565b8e0SQu Wenruo 		.exit_func = NULL,
25175565b8e0SQu Wenruo 	}, {
25185565b8e0SQu Wenruo 		.init_func = btrfs_run_sanity_tests,
25195565b8e0SQu Wenruo 		.exit_func = NULL,
25205565b8e0SQu Wenruo 	}, {
25215565b8e0SQu Wenruo 		.init_func = register_btrfs,
25225565b8e0SQu Wenruo 		.exit_func = unregister_btrfs,
25235565b8e0SQu Wenruo 	}
25245565b8e0SQu Wenruo };
25255565b8e0SQu Wenruo 
25265565b8e0SQu Wenruo static bool mod_init_result[ARRAY_SIZE(mod_init_seq)];
25275565b8e0SQu Wenruo 
252882c0efd3SAnand Jain static __always_inline void btrfs_exit_btrfs_fs(void)
25295565b8e0SQu Wenruo {
25305565b8e0SQu Wenruo 	int i;
25315565b8e0SQu Wenruo 
25325565b8e0SQu Wenruo 	for (i = ARRAY_SIZE(mod_init_seq) - 1; i >= 0; i--) {
25335565b8e0SQu Wenruo 		if (!mod_init_result[i])
25345565b8e0SQu Wenruo 			continue;
25355565b8e0SQu Wenruo 		if (mod_init_seq[i].exit_func)
25365565b8e0SQu Wenruo 			mod_init_seq[i].exit_func();
25375565b8e0SQu Wenruo 		mod_init_result[i] = false;
25385565b8e0SQu Wenruo 	}
253985965600SDavid Sterba }
254085965600SDavid Sterba 
254182c0efd3SAnand Jain static void __exit exit_btrfs_fs(void)
254282c0efd3SAnand Jain {
254382c0efd3SAnand Jain 	btrfs_exit_btrfs_fs();
2544c68f7290SFilipe Manana 	btrfs_cleanup_fs_uuids();
254582c0efd3SAnand Jain }
254682c0efd3SAnand Jain 
25472e635a27SChris Mason static int __init init_btrfs_fs(void)
25482e635a27SChris Mason {
25495565b8e0SQu Wenruo 	int ret;
25505565b8e0SQu Wenruo 	int i;
255158176a96SJosef Bacik 
25525565b8e0SQu Wenruo 	for (i = 0; i < ARRAY_SIZE(mod_init_seq); i++) {
25535565b8e0SQu Wenruo 		ASSERT(!mod_init_result[i]);
25545565b8e0SQu Wenruo 		ret = mod_init_seq[i].init_func();
255582c0efd3SAnand Jain 		if (ret < 0) {
255682c0efd3SAnand Jain 			btrfs_exit_btrfs_fs();
255782c0efd3SAnand Jain 			return ret;
255882c0efd3SAnand Jain 		}
25595565b8e0SQu Wenruo 		mod_init_result[i] = true;
25605565b8e0SQu Wenruo 	}
25612f4cbe64SWyatt Banks 	return 0;
25622e635a27SChris Mason }
25632e635a27SChris Mason 
256460efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs);
25652e635a27SChris Mason module_exit(exit_btrfs_fs)
25662e635a27SChris Mason 
25672e635a27SChris Mason MODULE_LICENSE("GPL");
2568d5178578SJohannes Thumshirn MODULE_SOFTDEP("pre: crc32c");
25693951e7f0SJohannes Thumshirn MODULE_SOFTDEP("pre: xxhash64");
25703831bf00SJohannes Thumshirn MODULE_SOFTDEP("pre: sha256");
2571352ae07bSDavid Sterba MODULE_SOFTDEP("pre: blake2b-256");
2572