xref: /openbmc/linux/fs/btrfs/super.c (revision 1bca9776)
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>
299b569ea0SJosef Bacik #include "messages.h"
3016cdcec7SMiao Xie #include "delayed-inode.h"
312e635a27SChris Mason #include "ctree.h"
32e20d96d6SChris Mason #include "disk-io.h"
33d5719762SChris Mason #include "transaction.h"
342c90e5d6SChris Mason #include "btrfs_inode.h"
353a686375SChris Mason #include "print-tree.h"
3663541927SFilipe David Borba Manana #include "props.h"
375103e947SJosef Bacik #include "xattr.h"
38103c1972SChristoph Hellwig #include "bio.h"
39be6e8dc0SBalaji Rao #include "export.h"
40c8b97818SChris Mason #include "compression.h"
419c5085c1SJosef Bacik #include "rcu-string.h"
428dabb742SStefan Behrens #include "dev-replace.h"
4374255aa0SJosef Bacik #include "free-space-cache.h"
44b9e9a6cbSWang Shilong #include "backref.h"
458719aaaeSJosef Bacik #include "space-info.h"
4689439109SDavid Sterba #include "sysfs.h"
47b70f5097SNaohiro Aota #include "zoned.h"
48dc11dd5dSJosef Bacik #include "tests/btrfs-tests.h"
49aac0023cSJosef Bacik #include "block-group.h"
50b0643e59SDennis Zhou #include "discard.h"
51d3982100SMark Fasheh #include "qgroup.h"
52b8bea09aSQu Wenruo #include "raid56.h"
53c7f13d42SJosef Bacik #include "fs.h"
5407e81dc9SJosef Bacik #include "accessors.h"
5559b818e0SJosef Bacik #include "defrag.h"
56f2b39277SJosef Bacik #include "dir-item.h"
577572dec8SJosef Bacik #include "ioctl.h"
582fc6822cSJosef Bacik #include "scrub.h"
595c11adccSJosef Bacik #include "verity.h"
60c03b2207SJosef Bacik #include "super.h"
61cfc2de0fSBoris Burkov #include "extent-tree.h"
621abe9b8aSliubo #define CREATE_TRACE_POINTS
631abe9b8aSliubo #include <trace/events/btrfs.h>
641abe9b8aSliubo 
65b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops;
6672fa39f5SMisono, Tomohiro 
6772fa39f5SMisono, Tomohiro /*
6872fa39f5SMisono, Tomohiro  * Types for mounting the default subvolume and a subvolume explicitly
6972fa39f5SMisono, Tomohiro  * requested by subvol=/path. That way the callchain is straightforward and we
7072fa39f5SMisono, Tomohiro  * don't have to play tricks with the mount options and recursive calls to
7172fa39f5SMisono, Tomohiro  * btrfs_mount.
72312c89fbSMisono, Tomohiro  *
73312c89fbSMisono, Tomohiro  * The new btrfs_root_fs_type also servers as a tag for the bdev_holder.
7472fa39f5SMisono, Tomohiro  */
75830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type;
7672fa39f5SMisono, Tomohiro static struct file_system_type btrfs_root_fs_type;
77e20d96d6SChris Mason 
780723a047SHarald Hoyer static int btrfs_remount(struct super_block *sb, int *flags, char *data);
790723a047SHarald Hoyer 
btrfs_put_super(struct super_block * sb)80e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb)
81e20d96d6SChris Mason {
829f7894e2SQu Wenruo 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
839f7894e2SQu Wenruo 
849f7894e2SQu Wenruo 	btrfs_info(fs_info, "last unmount of filesystem %pU", fs_info->fs_devices->fsid);
859f7894e2SQu Wenruo 	close_ctree(fs_info);
86e20d96d6SChris Mason }
872e635a27SChris Mason 
8895e05289SChris Mason enum {
89416a7202SDavid Sterba 	Opt_acl, Opt_noacl,
90416a7202SDavid Sterba 	Opt_clear_cache,
91416a7202SDavid Sterba 	Opt_commit_interval,
92416a7202SDavid Sterba 	Opt_compress,
93416a7202SDavid Sterba 	Opt_compress_force,
94416a7202SDavid Sterba 	Opt_compress_force_type,
95416a7202SDavid Sterba 	Opt_compress_type,
96416a7202SDavid Sterba 	Opt_degraded,
97416a7202SDavid Sterba 	Opt_device,
98416a7202SDavid Sterba 	Opt_fatal_errors,
99416a7202SDavid Sterba 	Opt_flushoncommit, Opt_noflushoncommit,
100416a7202SDavid Sterba 	Opt_max_inline,
101416a7202SDavid Sterba 	Opt_barrier, Opt_nobarrier,
102416a7202SDavid Sterba 	Opt_datacow, Opt_nodatacow,
103416a7202SDavid Sterba 	Opt_datasum, Opt_nodatasum,
104416a7202SDavid Sterba 	Opt_defrag, Opt_nodefrag,
105416a7202SDavid Sterba 	Opt_discard, Opt_nodiscard,
106b0643e59SDennis Zhou 	Opt_discard_mode,
107416a7202SDavid Sterba 	Opt_norecovery,
108416a7202SDavid Sterba 	Opt_ratio,
109416a7202SDavid Sterba 	Opt_rescan_uuid_tree,
110416a7202SDavid Sterba 	Opt_skip_balance,
111416a7202SDavid Sterba 	Opt_space_cache, Opt_no_space_cache,
112416a7202SDavid Sterba 	Opt_space_cache_version,
113416a7202SDavid Sterba 	Opt_ssd, Opt_nossd,
114416a7202SDavid Sterba 	Opt_ssd_spread, Opt_nossd_spread,
115416a7202SDavid Sterba 	Opt_subvol,
11637becec9SOmar Sandoval 	Opt_subvol_empty,
117416a7202SDavid Sterba 	Opt_subvolid,
118416a7202SDavid Sterba 	Opt_thread_pool,
119416a7202SDavid Sterba 	Opt_treelog, Opt_notreelog,
120416a7202SDavid Sterba 	Opt_user_subvol_rm_allowed,
121416a7202SDavid Sterba 
12274ef0018SQu Wenruo 	/* Rescue options */
12374ef0018SQu Wenruo 	Opt_rescue,
12474ef0018SQu Wenruo 	Opt_usebackuproot,
12574ef0018SQu Wenruo 	Opt_nologreplay,
12642437a63SJosef Bacik 	Opt_ignorebadroots,
127882dbe0cSJosef Bacik 	Opt_ignoredatacsums,
1289037d3cbSJosef Bacik 	Opt_rescue_all,
12974ef0018SQu Wenruo 
130416a7202SDavid Sterba 	/* Deprecated options */
131416a7202SDavid Sterba 	Opt_recovery,
1325297199aSNikolay Borisov 	Opt_inode_cache, Opt_noinode_cache,
133416a7202SDavid Sterba 
134416a7202SDavid Sterba 	/* Debugging options */
135416a7202SDavid Sterba 	Opt_check_integrity,
13670f6d82eSOmar Sandoval 	Opt_check_integrity_including_extent_data,
137416a7202SDavid Sterba 	Opt_check_integrity_print_mask,
138416a7202SDavid Sterba 	Opt_enospc_debug, Opt_noenospc_debug,
139d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
140d0bd4560SJosef Bacik 	Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
141d0bd4560SJosef Bacik #endif
142fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
143fb592373SJosef Bacik 	Opt_ref_verify,
144fb592373SJosef Bacik #endif
1459555c6c1SIlya Dryomov 	Opt_err,
14695e05289SChris Mason };
14795e05289SChris Mason 
1484d4ab6d6SDavid Sterba static const match_table_t tokens = {
149416a7202SDavid Sterba 	{Opt_acl, "acl"},
150416a7202SDavid Sterba 	{Opt_noacl, "noacl"},
151416a7202SDavid Sterba 	{Opt_clear_cache, "clear_cache"},
152416a7202SDavid Sterba 	{Opt_commit_interval, "commit=%u"},
153c8b97818SChris Mason 	{Opt_compress, "compress"},
154261507a0SLi Zefan 	{Opt_compress_type, "compress=%s"},
155a555f810SChris Mason 	{Opt_compress_force, "compress-force"},
156261507a0SLi Zefan 	{Opt_compress_force_type, "compress-force=%s"},
157416a7202SDavid Sterba 	{Opt_degraded, "degraded"},
158416a7202SDavid Sterba 	{Opt_device, "device=%s"},
159416a7202SDavid Sterba 	{Opt_fatal_errors, "fatal_errors=%s"},
160dccae999SSage Weil 	{Opt_flushoncommit, "flushoncommit"},
1612c9ee856SQu Wenruo 	{Opt_noflushoncommit, "noflushoncommit"},
1624b9465cbSChris Mason 	{Opt_inode_cache, "inode_cache"},
1633818aea2SQu Wenruo 	{Opt_noinode_cache, "noinode_cache"},
164416a7202SDavid Sterba 	{Opt_max_inline, "max_inline=%s"},
165416a7202SDavid Sterba 	{Opt_barrier, "barrier"},
166416a7202SDavid Sterba 	{Opt_nobarrier, "nobarrier"},
167416a7202SDavid Sterba 	{Opt_datacow, "datacow"},
168416a7202SDavid Sterba 	{Opt_nodatacow, "nodatacow"},
169416a7202SDavid Sterba 	{Opt_datasum, "datasum"},
170416a7202SDavid Sterba 	{Opt_nodatasum, "nodatasum"},
171416a7202SDavid Sterba 	{Opt_defrag, "autodefrag"},
172416a7202SDavid Sterba 	{Opt_nodefrag, "noautodefrag"},
173416a7202SDavid Sterba 	{Opt_discard, "discard"},
174b0643e59SDennis Zhou 	{Opt_discard_mode, "discard=%s"},
175416a7202SDavid Sterba 	{Opt_nodiscard, "nodiscard"},
176416a7202SDavid Sterba 	{Opt_norecovery, "norecovery"},
177416a7202SDavid Sterba 	{Opt_ratio, "metadata_ratio=%u"},
178416a7202SDavid Sterba 	{Opt_rescan_uuid_tree, "rescan_uuid_tree"},
1799555c6c1SIlya Dryomov 	{Opt_skip_balance, "skip_balance"},
180416a7202SDavid Sterba 	{Opt_space_cache, "space_cache"},
181416a7202SDavid Sterba 	{Opt_no_space_cache, "nospace_cache"},
182416a7202SDavid Sterba 	{Opt_space_cache_version, "space_cache=%s"},
183416a7202SDavid Sterba 	{Opt_ssd, "ssd"},
184416a7202SDavid Sterba 	{Opt_nossd, "nossd"},
185416a7202SDavid Sterba 	{Opt_ssd_spread, "ssd_spread"},
186416a7202SDavid Sterba 	{Opt_nossd_spread, "nossd_spread"},
187416a7202SDavid Sterba 	{Opt_subvol, "subvol=%s"},
18837becec9SOmar Sandoval 	{Opt_subvol_empty, "subvol="},
189416a7202SDavid Sterba 	{Opt_subvolid, "subvolid=%s"},
190416a7202SDavid Sterba 	{Opt_thread_pool, "thread_pool=%u"},
191416a7202SDavid Sterba 	{Opt_treelog, "treelog"},
192416a7202SDavid Sterba 	{Opt_notreelog, "notreelog"},
193416a7202SDavid Sterba 	{Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
194416a7202SDavid Sterba 
19574ef0018SQu Wenruo 	/* Rescue options */
19674ef0018SQu Wenruo 	{Opt_rescue, "rescue=%s"},
19774ef0018SQu Wenruo 	/* Deprecated, with alias rescue=nologreplay */
19874ef0018SQu Wenruo 	{Opt_nologreplay, "nologreplay"},
19974ef0018SQu Wenruo 	/* Deprecated, with alias rescue=usebackuproot */
20074ef0018SQu Wenruo 	{Opt_usebackuproot, "usebackuproot"},
20174ef0018SQu Wenruo 
202416a7202SDavid Sterba 	/* Deprecated options */
203416a7202SDavid Sterba 	{Opt_recovery, "recovery"},
204416a7202SDavid Sterba 
205416a7202SDavid Sterba 	/* Debugging options */
20621adbd5cSStefan Behrens 	{Opt_check_integrity, "check_int"},
20721adbd5cSStefan Behrens 	{Opt_check_integrity_including_extent_data, "check_int_data"},
20802453bdeSAnand Jain 	{Opt_check_integrity_print_mask, "check_int_print_mask=%u"},
209416a7202SDavid Sterba 	{Opt_enospc_debug, "enospc_debug"},
210416a7202SDavid Sterba 	{Opt_noenospc_debug, "noenospc_debug"},
211d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
212d0bd4560SJosef Bacik 	{Opt_fragment_data, "fragment=data"},
213d0bd4560SJosef Bacik 	{Opt_fragment_metadata, "fragment=metadata"},
214d0bd4560SJosef Bacik 	{Opt_fragment_all, "fragment=all"},
215d0bd4560SJosef Bacik #endif
216fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
217fb592373SJosef Bacik 	{Opt_ref_verify, "ref_verify"},
218fb592373SJosef Bacik #endif
21933268eafSJosef Bacik 	{Opt_err, NULL},
22095e05289SChris Mason };
22195e05289SChris Mason 
22274ef0018SQu Wenruo static const match_table_t rescue_tokens = {
22374ef0018SQu Wenruo 	{Opt_usebackuproot, "usebackuproot"},
22474ef0018SQu Wenruo 	{Opt_nologreplay, "nologreplay"},
22542437a63SJosef Bacik 	{Opt_ignorebadroots, "ignorebadroots"},
22642437a63SJosef Bacik 	{Opt_ignorebadroots, "ibadroots"},
227882dbe0cSJosef Bacik 	{Opt_ignoredatacsums, "ignoredatacsums"},
228882dbe0cSJosef Bacik 	{Opt_ignoredatacsums, "idatacsums"},
2299037d3cbSJosef Bacik 	{Opt_rescue_all, "all"},
23074ef0018SQu Wenruo 	{Opt_err, NULL},
23174ef0018SQu Wenruo };
23274ef0018SQu Wenruo 
check_ro_option(struct btrfs_fs_info * fs_info,unsigned long opt,const char * opt_name)233d70bf748SJosef Bacik static bool check_ro_option(struct btrfs_fs_info *fs_info, unsigned long opt,
234d70bf748SJosef Bacik 			    const char *opt_name)
235d70bf748SJosef Bacik {
236d70bf748SJosef Bacik 	if (fs_info->mount_opt & opt) {
237d70bf748SJosef Bacik 		btrfs_err(fs_info, "%s must be used with ro mount option",
238d70bf748SJosef Bacik 			  opt_name);
239d70bf748SJosef Bacik 		return true;
240d70bf748SJosef Bacik 	}
241d70bf748SJosef Bacik 	return false;
242d70bf748SJosef Bacik }
243d70bf748SJosef Bacik 
parse_rescue_options(struct btrfs_fs_info * info,const char * options)24474ef0018SQu Wenruo static int parse_rescue_options(struct btrfs_fs_info *info, const char *options)
24574ef0018SQu Wenruo {
24674ef0018SQu Wenruo 	char *opts;
24774ef0018SQu Wenruo 	char *orig;
24874ef0018SQu Wenruo 	char *p;
24974ef0018SQu Wenruo 	substring_t args[MAX_OPT_ARGS];
25074ef0018SQu Wenruo 	int ret = 0;
25174ef0018SQu Wenruo 
25274ef0018SQu Wenruo 	opts = kstrdup(options, GFP_KERNEL);
25374ef0018SQu Wenruo 	if (!opts)
25474ef0018SQu Wenruo 		return -ENOMEM;
25574ef0018SQu Wenruo 	orig = opts;
25674ef0018SQu Wenruo 
25774ef0018SQu Wenruo 	while ((p = strsep(&opts, ":")) != NULL) {
25874ef0018SQu Wenruo 		int token;
25974ef0018SQu Wenruo 
26074ef0018SQu Wenruo 		if (!*p)
26174ef0018SQu Wenruo 			continue;
26274ef0018SQu Wenruo 		token = match_token(p, rescue_tokens, args);
26374ef0018SQu Wenruo 		switch (token){
26474ef0018SQu Wenruo 		case Opt_usebackuproot:
26574ef0018SQu Wenruo 			btrfs_info(info,
26674ef0018SQu Wenruo 				   "trying to use backup root at mount time");
26774ef0018SQu Wenruo 			btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
26874ef0018SQu Wenruo 			break;
26974ef0018SQu Wenruo 		case Opt_nologreplay:
27074ef0018SQu Wenruo 			btrfs_set_and_info(info, NOLOGREPLAY,
27174ef0018SQu Wenruo 					   "disabling log replay at mount time");
27274ef0018SQu Wenruo 			break;
27342437a63SJosef Bacik 		case Opt_ignorebadroots:
27442437a63SJosef Bacik 			btrfs_set_and_info(info, IGNOREBADROOTS,
27542437a63SJosef Bacik 					   "ignoring bad roots");
27642437a63SJosef Bacik 			break;
277882dbe0cSJosef Bacik 		case Opt_ignoredatacsums:
278882dbe0cSJosef Bacik 			btrfs_set_and_info(info, IGNOREDATACSUMS,
279882dbe0cSJosef Bacik 					   "ignoring data csums");
280882dbe0cSJosef Bacik 			break;
2819037d3cbSJosef Bacik 		case Opt_rescue_all:
2829037d3cbSJosef Bacik 			btrfs_info(info, "enabling all of the rescue options");
2839037d3cbSJosef Bacik 			btrfs_set_and_info(info, IGNOREDATACSUMS,
2849037d3cbSJosef Bacik 					   "ignoring data csums");
2859037d3cbSJosef Bacik 			btrfs_set_and_info(info, IGNOREBADROOTS,
2869037d3cbSJosef Bacik 					   "ignoring bad roots");
2879037d3cbSJosef Bacik 			btrfs_set_and_info(info, NOLOGREPLAY,
2889037d3cbSJosef Bacik 					   "disabling log replay at mount time");
2899037d3cbSJosef Bacik 			break;
29074ef0018SQu Wenruo 		case Opt_err:
29174ef0018SQu Wenruo 			btrfs_info(info, "unrecognized rescue option '%s'", p);
29274ef0018SQu Wenruo 			ret = -EINVAL;
29374ef0018SQu Wenruo 			goto out;
29474ef0018SQu Wenruo 		default:
29574ef0018SQu Wenruo 			break;
29674ef0018SQu Wenruo 		}
29774ef0018SQu Wenruo 
29874ef0018SQu Wenruo 	}
29974ef0018SQu Wenruo out:
30074ef0018SQu Wenruo 	kfree(orig);
30174ef0018SQu Wenruo 	return ret;
30274ef0018SQu Wenruo }
30374ef0018SQu Wenruo 
304edf24abeSChristoph Hellwig /*
305edf24abeSChristoph Hellwig  * Regular mount options parser.  Everything that is needed only when
306edf24abeSChristoph Hellwig  * reading in a new superblock is parsed here.
30749b25e05SJeff Mahoney  * XXX JDM: This needs to be cleaned up for remount.
308edf24abeSChristoph Hellwig  */
btrfs_parse_options(struct btrfs_fs_info * info,char * options,unsigned long new_flags)3092ff7e61eSJeff Mahoney int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
31096da0919SQu Wenruo 			unsigned long new_flags)
31195e05289SChris Mason {
31295e05289SChris Mason 	substring_t args[MAX_OPT_ARGS];
313e215772cSMisono, Tomohiro 	char *p, *num;
3144543df7eSChris Mason 	int intarg;
315a7a3f7caSSage Weil 	int ret = 0;
316261507a0SLi Zefan 	char *compress_type;
317261507a0SLi Zefan 	bool compress_force = false;
318b7c47bbbSTsutomu Itoh 	enum btrfs_compression_type saved_compress_type;
31927942c99SDavid Sterba 	int saved_compress_level;
320b7c47bbbSTsutomu Itoh 	bool saved_compress_force;
321b7c47bbbSTsutomu Itoh 	int no_compress = 0;
322dbecac26SMaciej S. Szmigiero 	const bool remounting = test_bit(BTRFS_FS_STATE_REMOUNTING, &info->fs_state);
323b6cda9bcSChris Mason 
3240b246afaSJeff Mahoney 	if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
32570f6d82eSOmar Sandoval 		btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE);
32694846229SBoris Burkov 	else if (btrfs_free_space_cache_v1_active(info)) {
3275d1ab66cSNaohiro Aota 		if (btrfs_is_zoned(info)) {
3285d1ab66cSNaohiro Aota 			btrfs_info(info,
3295d1ab66cSNaohiro Aota 			"zoned: clearing existing space cache");
3305d1ab66cSNaohiro Aota 			btrfs_set_super_cache_generation(info->super_copy, 0);
3315d1ab66cSNaohiro Aota 		} else {
33273bc1876SJosef Bacik 			btrfs_set_opt(info->mount_opt, SPACE_CACHE);
3335d1ab66cSNaohiro Aota 		}
3345d1ab66cSNaohiro Aota 	}
33573bc1876SJosef Bacik 
33696da0919SQu Wenruo 	/*
33796da0919SQu Wenruo 	 * Even the options are empty, we still need to do extra check
33896da0919SQu Wenruo 	 * against new flags
33996da0919SQu Wenruo 	 */
34095e05289SChris Mason 	if (!options)
34196da0919SQu Wenruo 		goto check;
34295e05289SChris Mason 
34395e05289SChris Mason 	while ((p = strsep(&options, ",")) != NULL) {
34495e05289SChris Mason 		int token;
34595e05289SChris Mason 		if (!*p)
34695e05289SChris Mason 			continue;
34795e05289SChris Mason 
34895e05289SChris Mason 		token = match_token(p, tokens, args);
34995e05289SChris Mason 		switch (token) {
350dfe25020SChris Mason 		case Opt_degraded:
3510b246afaSJeff Mahoney 			btrfs_info(info, "allowing degraded mounts");
352dfe25020SChris Mason 			btrfs_set_opt(info->mount_opt, DEGRADED);
353dfe25020SChris Mason 			break;
35495e05289SChris Mason 		case Opt_subvol:
35537becec9SOmar Sandoval 		case Opt_subvol_empty:
35673f73415SJosef Bacik 		case Opt_subvolid:
35743e570b0SChristoph Hellwig 		case Opt_device:
358edf24abeSChristoph Hellwig 			/*
359fa59f27cSAnand Jain 			 * These are parsed by btrfs_parse_subvol_options or
360fa59f27cSAnand Jain 			 * btrfs_parse_device_options and can be ignored here.
361edf24abeSChristoph Hellwig 			 */
36295e05289SChris Mason 			break;
363b6cda9bcSChris Mason 		case Opt_nodatasum:
3643cdde224SJeff Mahoney 			btrfs_set_and_info(info, NODATASUM,
36507802534SQu Wenruo 					   "setting nodatasum");
366be20aa9dSChris Mason 			break;
367d399167dSQu Wenruo 		case Opt_datasum:
3683cdde224SJeff Mahoney 			if (btrfs_test_opt(info, NODATASUM)) {
3693cdde224SJeff Mahoney 				if (btrfs_test_opt(info, NODATACOW))
3700b246afaSJeff Mahoney 					btrfs_info(info,
3715d163e0eSJeff Mahoney 						   "setting datasum, datacow enabled");
372d399167dSQu Wenruo 				else
3730b246afaSJeff Mahoney 					btrfs_info(info, "setting datasum");
37407802534SQu Wenruo 			}
375d399167dSQu Wenruo 			btrfs_clear_opt(info->mount_opt, NODATACOW);
376d399167dSQu Wenruo 			btrfs_clear_opt(info->mount_opt, NODATASUM);
377d399167dSQu Wenruo 			break;
378be20aa9dSChris Mason 		case Opt_nodatacow:
3793cdde224SJeff Mahoney 			if (!btrfs_test_opt(info, NODATACOW)) {
3803cdde224SJeff Mahoney 				if (!btrfs_test_opt(info, COMPRESS) ||
3813cdde224SJeff Mahoney 				    !btrfs_test_opt(info, FORCE_COMPRESS)) {
3820b246afaSJeff Mahoney 					btrfs_info(info,
383efe120a0SFrank Holton 						   "setting nodatacow, compression disabled");
384bedb2ccaSAndrei Popa 				} else {
3850b246afaSJeff Mahoney 					btrfs_info(info, "setting nodatacow");
386bedb2ccaSAndrei Popa 				}
38707802534SQu Wenruo 			}
388bedb2ccaSAndrei Popa 			btrfs_clear_opt(info->mount_opt, COMPRESS);
389bedb2ccaSAndrei Popa 			btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
390be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATACOW);
391be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
392b6cda9bcSChris Mason 			break;
393a258af7aSQu Wenruo 		case Opt_datacow:
3943cdde224SJeff Mahoney 			btrfs_clear_and_info(info, NODATACOW,
39507802534SQu Wenruo 					     "setting datacow");
396a258af7aSQu Wenruo 			break;
397a555f810SChris Mason 		case Opt_compress_force:
398261507a0SLi Zefan 		case Opt_compress_force_type:
399261507a0SLi Zefan 			compress_force = true;
400c730ae0cSMarcos Paulo de Souza 			fallthrough;
401261507a0SLi Zefan 		case Opt_compress:
402261507a0SLi Zefan 		case Opt_compress_type:
4033cdde224SJeff Mahoney 			saved_compress_type = btrfs_test_opt(info,
4043cdde224SJeff Mahoney 							     COMPRESS) ?
405b7c47bbbSTsutomu Itoh 				info->compress_type : BTRFS_COMPRESS_NONE;
406b7c47bbbSTsutomu Itoh 			saved_compress_force =
4073cdde224SJeff Mahoney 				btrfs_test_opt(info, FORCE_COMPRESS);
40827942c99SDavid Sterba 			saved_compress_level = info->compress_level;
409261507a0SLi Zefan 			if (token == Opt_compress ||
410261507a0SLi Zefan 			    token == Opt_compress_force ||
411a7164fa4SDavid Sterba 			    strncmp(args[0].from, "zlib", 4) == 0) {
412261507a0SLi Zefan 				compress_type = "zlib";
413eae8d825SQu Wenruo 
414261507a0SLi Zefan 				info->compress_type = BTRFS_COMPRESS_ZLIB;
415eae8d825SQu Wenruo 				info->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
416eae8d825SQu Wenruo 				/*
417eae8d825SQu Wenruo 				 * args[0] contains uninitialized data since
418eae8d825SQu Wenruo 				 * for these tokens we don't expect any
419eae8d825SQu Wenruo 				 * parameter.
420eae8d825SQu Wenruo 				 */
421eae8d825SQu Wenruo 				if (token != Opt_compress &&
422eae8d825SQu Wenruo 				    token != Opt_compress_force)
423f51d2b59SDavid Sterba 					info->compress_level =
424d0ab62ceSDennis Zhou 					  btrfs_compress_str2level(
425d0ab62ceSDennis Zhou 							BTRFS_COMPRESS_ZLIB,
426d0ab62ceSDennis Zhou 							args[0].from + 4);
427063849eaSArnd Hannemann 				btrfs_set_opt(info->mount_opt, COMPRESS);
428bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATACOW);
429bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATASUM);
430b7c47bbbSTsutomu Itoh 				no_compress = 0;
431a7164fa4SDavid Sterba 			} else if (strncmp(args[0].from, "lzo", 3) == 0) {
432a6fa6faeSLi Zefan 				compress_type = "lzo";
433a6fa6faeSLi Zefan 				info->compress_type = BTRFS_COMPRESS_LZO;
434282dd7d7SMarcos Paulo de Souza 				info->compress_level = 0;
435063849eaSArnd Hannemann 				btrfs_set_opt(info->mount_opt, COMPRESS);
436bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATACOW);
437bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATASUM);
4382b0ce2c2SMitch Harder 				btrfs_set_fs_incompat(info, COMPRESS_LZO);
439b7c47bbbSTsutomu Itoh 				no_compress = 0;
4403f93aef5SDennis Zhou 			} else if (strncmp(args[0].from, "zstd", 4) == 0) {
4415c1aab1dSNick Terrell 				compress_type = "zstd";
4425c1aab1dSNick Terrell 				info->compress_type = BTRFS_COMPRESS_ZSTD;
4433f93aef5SDennis Zhou 				info->compress_level =
4443f93aef5SDennis Zhou 					btrfs_compress_str2level(
4453f93aef5SDennis Zhou 							 BTRFS_COMPRESS_ZSTD,
4463f93aef5SDennis Zhou 							 args[0].from + 4);
4475c1aab1dSNick Terrell 				btrfs_set_opt(info->mount_opt, COMPRESS);
4485c1aab1dSNick Terrell 				btrfs_clear_opt(info->mount_opt, NODATACOW);
4495c1aab1dSNick Terrell 				btrfs_clear_opt(info->mount_opt, NODATASUM);
4505c1aab1dSNick Terrell 				btrfs_set_fs_incompat(info, COMPRESS_ZSTD);
4515c1aab1dSNick Terrell 				no_compress = 0;
452063849eaSArnd Hannemann 			} else if (strncmp(args[0].from, "no", 2) == 0) {
453063849eaSArnd Hannemann 				compress_type = "no";
45427942c99SDavid Sterba 				info->compress_level = 0;
45527942c99SDavid Sterba 				info->compress_type = 0;
456063849eaSArnd Hannemann 				btrfs_clear_opt(info->mount_opt, COMPRESS);
457063849eaSArnd Hannemann 				btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
458063849eaSArnd Hannemann 				compress_force = false;
459b7c47bbbSTsutomu Itoh 				no_compress++;
460261507a0SLi Zefan 			} else {
461e3a4167cSDavid Sterba 				btrfs_err(info, "unrecognized compression value %s",
462e3a4167cSDavid Sterba 					  args[0].from);
463261507a0SLi Zefan 				ret = -EINVAL;
464261507a0SLi Zefan 				goto out;
465261507a0SLi Zefan 			}
466261507a0SLi Zefan 
467261507a0SLi Zefan 			if (compress_force) {
468b7c47bbbSTsutomu Itoh 				btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
469143f3636SDavid Sterba 			} else {
4704027e0f4SWang Shilong 				/*
4714027e0f4SWang Shilong 				 * If we remount from compress-force=xxx to
4724027e0f4SWang Shilong 				 * compress=xxx, we need clear FORCE_COMPRESS
4734027e0f4SWang Shilong 				 * flag, otherwise, there is no way for users
4744027e0f4SWang Shilong 				 * to disable forcible compression separately.
4754027e0f4SWang Shilong 				 */
4764027e0f4SWang Shilong 				btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
477a7e252afSMiao Xie 			}
47827942c99SDavid Sterba 			if (no_compress == 1) {
47927942c99SDavid Sterba 				btrfs_info(info, "use no compression");
48027942c99SDavid Sterba 			} else if ((info->compress_type != saved_compress_type) ||
48127942c99SDavid Sterba 				   (compress_force != saved_compress_force) ||
48227942c99SDavid Sterba 				   (info->compress_level != saved_compress_level)) {
483f51d2b59SDavid Sterba 				btrfs_info(info, "%s %s compression, level %d",
484b7c47bbbSTsutomu Itoh 					   (compress_force) ? "force" : "use",
485f51d2b59SDavid Sterba 					   compress_type, info->compress_level);
486b7c47bbbSTsutomu Itoh 			}
487b7c47bbbSTsutomu Itoh 			compress_force = false;
488a555f810SChris Mason 			break;
489e18e4809SChris Mason 		case Opt_ssd:
4903cdde224SJeff Mahoney 			btrfs_set_and_info(info, SSD,
491583b7231SHans van Kranenburg 					   "enabling ssd optimizations");
492951e7966SAdam Borowski 			btrfs_clear_opt(info->mount_opt, NOSSD);
493e18e4809SChris Mason 			break;
494451d7585SChris Mason 		case Opt_ssd_spread:
495583b7231SHans van Kranenburg 			btrfs_set_and_info(info, SSD,
496583b7231SHans van Kranenburg 					   "enabling ssd optimizations");
4973cdde224SJeff Mahoney 			btrfs_set_and_info(info, SSD_SPREAD,
498583b7231SHans van Kranenburg 					   "using spread ssd allocation scheme");
499951e7966SAdam Borowski 			btrfs_clear_opt(info->mount_opt, NOSSD);
500451d7585SChris Mason 			break;
5013b30c22fSChris Mason 		case Opt_nossd:
502583b7231SHans van Kranenburg 			btrfs_set_opt(info->mount_opt, NOSSD);
503583b7231SHans van Kranenburg 			btrfs_clear_and_info(info, SSD,
504583b7231SHans van Kranenburg 					     "not using ssd optimizations");
505c730ae0cSMarcos Paulo de Souza 			fallthrough;
50662b8e077SHoward McLauchlan 		case Opt_nossd_spread:
507583b7231SHans van Kranenburg 			btrfs_clear_and_info(info, SSD_SPREAD,
508583b7231SHans van Kranenburg 					     "not using spread ssd allocation scheme");
5093b30c22fSChris Mason 			break;
510842bef58SQu Wenruo 		case Opt_barrier:
5113cdde224SJeff Mahoney 			btrfs_clear_and_info(info, NOBARRIER,
51207802534SQu Wenruo 					     "turning on barriers");
513842bef58SQu Wenruo 			break;
51421ad10cfSChris Mason 		case Opt_nobarrier:
5153cdde224SJeff Mahoney 			btrfs_set_and_info(info, NOBARRIER,
51607802534SQu Wenruo 					   "turning off barriers");
51721ad10cfSChris Mason 			break;
5184543df7eSChris Mason 		case Opt_thread_pool:
5192c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
5202c334e87SWang Shilong 			if (ret) {
521e3a4167cSDavid Sterba 				btrfs_err(info, "unrecognized thread_pool value %s",
522e3a4167cSDavid Sterba 					  args[0].from);
5232c334e87SWang Shilong 				goto out;
524f7b885beSAnand Jain 			} else if (intarg == 0) {
525e3a4167cSDavid Sterba 				btrfs_err(info, "invalid value 0 for thread_pool");
5262c334e87SWang Shilong 				ret = -EINVAL;
5272c334e87SWang Shilong 				goto out;
5282c334e87SWang Shilong 			}
529f7b885beSAnand Jain 			info->thread_pool_size = intarg;
5304543df7eSChris Mason 			break;
5316f568d35SChris Mason 		case Opt_max_inline:
532edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
5336f568d35SChris Mason 			if (num) {
53491748467SAkinobu Mita 				info->max_inline = memparse(num, NULL);
5356f568d35SChris Mason 				kfree(num);
5366f568d35SChris Mason 
53715ada040SChris Mason 				if (info->max_inline) {
538feb5f965SMitch Harder 					info->max_inline = min_t(u64,
53915ada040SChris Mason 						info->max_inline,
5400b246afaSJeff Mahoney 						info->sectorsize);
54115ada040SChris Mason 				}
5420b246afaSJeff Mahoney 				btrfs_info(info, "max_inline at %llu",
543c1c9ff7cSGeert Uytterhoeven 					   info->max_inline);
5442c334e87SWang Shilong 			} else {
5452c334e87SWang Shilong 				ret = -ENOMEM;
5462c334e87SWang Shilong 				goto out;
5476f568d35SChris Mason 			}
5486f568d35SChris Mason 			break;
549bd0330adSQu Wenruo 		case Opt_acl:
55045ff35d6SGuangliang Zhao #ifdef CONFIG_BTRFS_FS_POSIX_ACL
5511751e8a6SLinus Torvalds 			info->sb->s_flags |= SB_POSIXACL;
552bd0330adSQu Wenruo 			break;
55345ff35d6SGuangliang Zhao #else
5540b246afaSJeff Mahoney 			btrfs_err(info, "support for ACL not compiled in!");
55545ff35d6SGuangliang Zhao 			ret = -EINVAL;
55645ff35d6SGuangliang Zhao 			goto out;
55745ff35d6SGuangliang Zhao #endif
55833268eafSJosef Bacik 		case Opt_noacl:
5591751e8a6SLinus Torvalds 			info->sb->s_flags &= ~SB_POSIXACL;
56033268eafSJosef Bacik 			break;
5613a5e1404SSage Weil 		case Opt_notreelog:
5623cdde224SJeff Mahoney 			btrfs_set_and_info(info, NOTREELOG,
56307802534SQu Wenruo 					   "disabling tree log");
5643a5e1404SSage Weil 			break;
565a88998f2SQu Wenruo 		case Opt_treelog:
5663cdde224SJeff Mahoney 			btrfs_clear_and_info(info, NOTREELOG,
56707802534SQu Wenruo 					     "enabling tree log");
568a88998f2SQu Wenruo 			break;
569fed8f166SQu Wenruo 		case Opt_norecovery:
57096da0919SQu Wenruo 		case Opt_nologreplay:
57174ef0018SQu Wenruo 			btrfs_warn(info,
57274ef0018SQu Wenruo 		"'nologreplay' is deprecated, use 'rescue=nologreplay' instead");
5733cdde224SJeff Mahoney 			btrfs_set_and_info(info, NOLOGREPLAY,
57496da0919SQu Wenruo 					   "disabling log replay at mount time");
57596da0919SQu Wenruo 			break;
576dccae999SSage Weil 		case Opt_flushoncommit:
5773cdde224SJeff Mahoney 			btrfs_set_and_info(info, FLUSHONCOMMIT,
57807802534SQu Wenruo 					   "turning on flush-on-commit");
579dccae999SSage Weil 			break;
5802c9ee856SQu Wenruo 		case Opt_noflushoncommit:
5813cdde224SJeff Mahoney 			btrfs_clear_and_info(info, FLUSHONCOMMIT,
58207802534SQu Wenruo 					     "turning off flush-on-commit");
5832c9ee856SQu Wenruo 			break;
58497e728d4SJosef Bacik 		case Opt_ratio:
5852c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
586e3a4167cSDavid Sterba 			if (ret) {
587e3a4167cSDavid Sterba 				btrfs_err(info, "unrecognized metadata_ratio value %s",
588e3a4167cSDavid Sterba 					  args[0].from);
5892c334e87SWang Shilong 				goto out;
590e3a4167cSDavid Sterba 			}
59197e728d4SJosef Bacik 			info->metadata_ratio = intarg;
592764cb8b4SAnand Jain 			btrfs_info(info, "metadata ratio %u",
59397e728d4SJosef Bacik 				   info->metadata_ratio);
59497e728d4SJosef Bacik 			break;
595e244a0aeSChristoph Hellwig 		case Opt_discard:
596b0643e59SDennis Zhou 		case Opt_discard_mode:
597b0643e59SDennis Zhou 			if (token == Opt_discard ||
598b0643e59SDennis Zhou 			    strcmp(args[0].from, "sync") == 0) {
599b0643e59SDennis Zhou 				btrfs_clear_opt(info->mount_opt, DISCARD_ASYNC);
60046b27f50SDennis Zhou 				btrfs_set_and_info(info, DISCARD_SYNC,
60146b27f50SDennis Zhou 						   "turning on sync discard");
602b0643e59SDennis Zhou 			} else if (strcmp(args[0].from, "async") == 0) {
603b0643e59SDennis Zhou 				btrfs_clear_opt(info->mount_opt, DISCARD_SYNC);
604b0643e59SDennis Zhou 				btrfs_set_and_info(info, DISCARD_ASYNC,
605b0643e59SDennis Zhou 						   "turning on async discard");
606b0643e59SDennis Zhou 			} else {
607e3a4167cSDavid Sterba 				btrfs_err(info, "unrecognized discard mode value %s",
608e3a4167cSDavid Sterba 					  args[0].from);
609b0643e59SDennis Zhou 				ret = -EINVAL;
610b0643e59SDennis Zhou 				goto out;
611b0643e59SDennis Zhou 			}
61263a7cb13SDavid Sterba 			btrfs_clear_opt(info->mount_opt, NODISCARD);
613e244a0aeSChristoph Hellwig 			break;
614e07a2adeSQu Wenruo 		case Opt_nodiscard:
61546b27f50SDennis Zhou 			btrfs_clear_and_info(info, DISCARD_SYNC,
61607802534SQu Wenruo 					     "turning off discard");
617b0643e59SDennis Zhou 			btrfs_clear_and_info(info, DISCARD_ASYNC,
618b0643e59SDennis Zhou 					     "turning off async discard");
61963a7cb13SDavid Sterba 			btrfs_set_opt(info->mount_opt, NODISCARD);
620e07a2adeSQu Wenruo 			break;
6210af3d00bSJosef Bacik 		case Opt_space_cache:
62270f6d82eSOmar Sandoval 		case Opt_space_cache_version:
62363cd070dSJosef Bacik 			/*
62463cd070dSJosef Bacik 			 * We already set FREE_SPACE_TREE above because we have
62563cd070dSJosef Bacik 			 * compat_ro(FREE_SPACE_TREE) set, and we aren't going
62663cd070dSJosef Bacik 			 * to allow v1 to be set for extent tree v2, simply
62763cd070dSJosef Bacik 			 * ignore this setting if we're extent tree v2.
62863cd070dSJosef Bacik 			 */
62963cd070dSJosef Bacik 			if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
63063cd070dSJosef Bacik 				break;
63170f6d82eSOmar Sandoval 			if (token == Opt_space_cache ||
63270f6d82eSOmar Sandoval 			    strcmp(args[0].from, "v1") == 0) {
6330b246afaSJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
63470f6d82eSOmar Sandoval 						FREE_SPACE_TREE);
6353cdde224SJeff Mahoney 				btrfs_set_and_info(info, SPACE_CACHE,
63607802534SQu Wenruo 					   "enabling disk space caching");
63770f6d82eSOmar Sandoval 			} else if (strcmp(args[0].from, "v2") == 0) {
6380b246afaSJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
63970f6d82eSOmar Sandoval 						SPACE_CACHE);
6400b246afaSJeff Mahoney 				btrfs_set_and_info(info, FREE_SPACE_TREE,
64170f6d82eSOmar Sandoval 						   "enabling free space tree");
64270f6d82eSOmar Sandoval 			} else {
643e3a4167cSDavid Sterba 				btrfs_err(info, "unrecognized space_cache value %s",
644e3a4167cSDavid Sterba 					  args[0].from);
64570f6d82eSOmar Sandoval 				ret = -EINVAL;
64670f6d82eSOmar Sandoval 				goto out;
64770f6d82eSOmar Sandoval 			}
6480de90876SJosef Bacik 			break;
649f420ee1eSStefan Behrens 		case Opt_rescan_uuid_tree:
650f420ee1eSStefan Behrens 			btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
651f420ee1eSStefan Behrens 			break;
65273bc1876SJosef Bacik 		case Opt_no_space_cache:
65363cd070dSJosef Bacik 			/*
65463cd070dSJosef Bacik 			 * We cannot operate without the free space tree with
65563cd070dSJosef Bacik 			 * extent tree v2, ignore this option.
65663cd070dSJosef Bacik 			 */
65763cd070dSJosef Bacik 			if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
65863cd070dSJosef Bacik 				break;
6593cdde224SJeff Mahoney 			if (btrfs_test_opt(info, SPACE_CACHE)) {
6600b246afaSJeff Mahoney 				btrfs_clear_and_info(info, SPACE_CACHE,
66107802534SQu Wenruo 					     "disabling disk space caching");
66270f6d82eSOmar Sandoval 			}
6633cdde224SJeff Mahoney 			if (btrfs_test_opt(info, FREE_SPACE_TREE)) {
6640b246afaSJeff Mahoney 				btrfs_clear_and_info(info, FREE_SPACE_TREE,
66570f6d82eSOmar Sandoval 					     "disabling free space tree");
66670f6d82eSOmar Sandoval 			}
66773bc1876SJosef Bacik 			break;
6684b9465cbSChris Mason 		case Opt_inode_cache:
6693818aea2SQu Wenruo 		case Opt_noinode_cache:
6705297199aSNikolay Borisov 			btrfs_warn(info,
6715297199aSNikolay Borisov 	"the 'inode_cache' option is deprecated and has no effect since 5.11");
6724b9465cbSChris Mason 			break;
67388c2ba3bSJosef Bacik 		case Opt_clear_cache:
67463cd070dSJosef Bacik 			/*
67563cd070dSJosef Bacik 			 * We cannot clear the free space tree with extent tree
67663cd070dSJosef Bacik 			 * v2, ignore this option.
67763cd070dSJosef Bacik 			 */
67863cd070dSJosef Bacik 			if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
67963cd070dSJosef Bacik 				break;
6803cdde224SJeff Mahoney 			btrfs_set_and_info(info, CLEAR_CACHE,
68107802534SQu Wenruo 					   "force clearing of disk cache");
6820af3d00bSJosef Bacik 			break;
6834260f7c7SSage Weil 		case Opt_user_subvol_rm_allowed:
6844260f7c7SSage Weil 			btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
6854260f7c7SSage Weil 			break;
68691435650SChris Mason 		case Opt_enospc_debug:
68791435650SChris Mason 			btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
68891435650SChris Mason 			break;
68953036293SQu Wenruo 		case Opt_noenospc_debug:
69053036293SQu Wenruo 			btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG);
69153036293SQu Wenruo 			break;
6924cb5300bSChris Mason 		case Opt_defrag:
6933cdde224SJeff Mahoney 			btrfs_set_and_info(info, AUTO_DEFRAG,
69407802534SQu Wenruo 					   "enabling auto defrag");
6954cb5300bSChris Mason 			break;
696fc0ca9afSQu Wenruo 		case Opt_nodefrag:
6973cdde224SJeff Mahoney 			btrfs_clear_and_info(info, AUTO_DEFRAG,
69807802534SQu Wenruo 					     "disabling auto defrag");
699fc0ca9afSQu Wenruo 			break;
700af31f5e5SChris Mason 		case Opt_recovery:
7018dcddfa0SQu Wenruo 		case Opt_usebackuproot:
70274ef0018SQu Wenruo 			btrfs_warn(info,
70374ef0018SQu Wenruo 			"'%s' is deprecated, use 'rescue=usebackuproot' instead",
70474ef0018SQu Wenruo 				   token == Opt_recovery ? "recovery" :
70574ef0018SQu Wenruo 				   "usebackuproot");
7060b246afaSJeff Mahoney 			btrfs_info(info,
7078dcddfa0SQu Wenruo 				   "trying to use backup root at mount time");
7088dcddfa0SQu Wenruo 			btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
709af31f5e5SChris Mason 			break;
7109555c6c1SIlya Dryomov 		case Opt_skip_balance:
7119555c6c1SIlya Dryomov 			btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
7129555c6c1SIlya Dryomov 			break;
71321adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
71421adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
71516c3a476SQu Wenruo 			btrfs_warn(info,
71616c3a476SQu Wenruo 	"integrity checker is deprecated and will be removed in 6.7");
7170b246afaSJeff Mahoney 			btrfs_info(info,
718efe120a0SFrank Holton 				   "enabling check integrity including extent data");
719cbeaae4fSDavid Sterba 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY_DATA);
72021adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
72121adbd5cSStefan Behrens 			break;
72221adbd5cSStefan Behrens 		case Opt_check_integrity:
72316c3a476SQu Wenruo 			btrfs_warn(info,
72416c3a476SQu Wenruo 	"integrity checker is deprecated and will be removed in 6.7");
7250b246afaSJeff Mahoney 			btrfs_info(info, "enabling check integrity");
72621adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
72721adbd5cSStefan Behrens 			break;
72821adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
7292c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
730e3a4167cSDavid Sterba 			if (ret) {
731e3a4167cSDavid Sterba 				btrfs_err(info,
732e3a4167cSDavid Sterba 				"unrecognized check_integrity_print_mask value %s",
733e3a4167cSDavid Sterba 					args[0].from);
7342c334e87SWang Shilong 				goto out;
735e3a4167cSDavid Sterba 			}
73621adbd5cSStefan Behrens 			info->check_integrity_print_mask = intarg;
73716c3a476SQu Wenruo 			btrfs_warn(info,
73816c3a476SQu Wenruo 	"integrity checker is deprecated and will be removed in 6.7");
73902453bdeSAnand Jain 			btrfs_info(info, "check_integrity_print_mask 0x%x",
74021adbd5cSStefan Behrens 				   info->check_integrity_print_mask);
74121adbd5cSStefan Behrens 			break;
74221adbd5cSStefan Behrens #else
74321adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
74421adbd5cSStefan Behrens 		case Opt_check_integrity:
74521adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
7460b246afaSJeff Mahoney 			btrfs_err(info,
747efe120a0SFrank Holton 				  "support for check_integrity* not compiled in!");
74821adbd5cSStefan Behrens 			ret = -EINVAL;
74921adbd5cSStefan Behrens 			goto out;
75021adbd5cSStefan Behrens #endif
7518c342930SJeff Mahoney 		case Opt_fatal_errors:
752e3a4167cSDavid Sterba 			if (strcmp(args[0].from, "panic") == 0) {
7538c342930SJeff Mahoney 				btrfs_set_opt(info->mount_opt,
7548c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
755e3a4167cSDavid Sterba 			} else if (strcmp(args[0].from, "bug") == 0) {
7568c342930SJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
7578c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
758e3a4167cSDavid Sterba 			} else {
759e3a4167cSDavid Sterba 				btrfs_err(info, "unrecognized fatal_errors value %s",
760e3a4167cSDavid Sterba 					  args[0].from);
7618c342930SJeff Mahoney 				ret = -EINVAL;
7628c342930SJeff Mahoney 				goto out;
7638c342930SJeff Mahoney 			}
7648c342930SJeff Mahoney 			break;
7658b87dc17SDavid Sterba 		case Opt_commit_interval:
7668b87dc17SDavid Sterba 			intarg = 0;
7678b87dc17SDavid Sterba 			ret = match_int(&args[0], &intarg);
768e3a4167cSDavid Sterba 			if (ret) {
769e3a4167cSDavid Sterba 				btrfs_err(info, "unrecognized commit_interval value %s",
770e3a4167cSDavid Sterba 					  args[0].from);
771e3a4167cSDavid Sterba 				ret = -EINVAL;
7728b87dc17SDavid Sterba 				goto out;
773e3a4167cSDavid Sterba 			}
774d3740608SAnand Jain 			if (intarg == 0) {
775d3740608SAnand Jain 				btrfs_info(info,
776d3740608SAnand Jain 					   "using default commit interval %us",
777d3740608SAnand Jain 					   BTRFS_DEFAULT_COMMIT_INTERVAL);
778d3740608SAnand Jain 				intarg = BTRFS_DEFAULT_COMMIT_INTERVAL;
779d3740608SAnand Jain 			} else if (intarg > 300) {
780d3740608SAnand Jain 				btrfs_warn(info, "excessive commit interval %d",
7818b87dc17SDavid Sterba 					   intarg);
7828b87dc17SDavid Sterba 			}
7838b87dc17SDavid Sterba 			info->commit_interval = intarg;
7848b87dc17SDavid Sterba 			break;
78574ef0018SQu Wenruo 		case Opt_rescue:
78674ef0018SQu Wenruo 			ret = parse_rescue_options(info, args[0].from);
787e3a4167cSDavid Sterba 			if (ret < 0) {
788e3a4167cSDavid Sterba 				btrfs_err(info, "unrecognized rescue value %s",
789e3a4167cSDavid Sterba 					  args[0].from);
79074ef0018SQu Wenruo 				goto out;
791e3a4167cSDavid Sterba 			}
79274ef0018SQu Wenruo 			break;
793d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
794d0bd4560SJosef Bacik 		case Opt_fragment_all:
7950b246afaSJeff Mahoney 			btrfs_info(info, "fragmenting all space");
796d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
797d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA);
798d0bd4560SJosef Bacik 			break;
799d0bd4560SJosef Bacik 		case Opt_fragment_metadata:
8000b246afaSJeff Mahoney 			btrfs_info(info, "fragmenting metadata");
801d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt,
802d0bd4560SJosef Bacik 				      FRAGMENT_METADATA);
803d0bd4560SJosef Bacik 			break;
804d0bd4560SJosef Bacik 		case Opt_fragment_data:
8050b246afaSJeff Mahoney 			btrfs_info(info, "fragmenting data");
806d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
807d0bd4560SJosef Bacik 			break;
808d0bd4560SJosef Bacik #endif
809fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
810fb592373SJosef Bacik 		case Opt_ref_verify:
811fb592373SJosef Bacik 			btrfs_info(info, "doing ref verification");
812fb592373SJosef Bacik 			btrfs_set_opt(info->mount_opt, REF_VERIFY);
813fb592373SJosef Bacik 			break;
814fb592373SJosef Bacik #endif
815a7a3f7caSSage Weil 		case Opt_err:
8167e8f19e5SDavid Sterba 			btrfs_err(info, "unrecognized mount option '%s'", p);
817a7a3f7caSSage Weil 			ret = -EINVAL;
818a7a3f7caSSage Weil 			goto out;
81995e05289SChris Mason 		default:
820be20aa9dSChris Mason 			break;
82195e05289SChris Mason 		}
82295e05289SChris Mason 	}
82396da0919SQu Wenruo check:
824d70bf748SJosef Bacik 	/* We're read-only, don't have to check. */
825d70bf748SJosef Bacik 	if (new_flags & SB_RDONLY)
826d70bf748SJosef Bacik 		goto out;
827d70bf748SJosef Bacik 
82842437a63SJosef Bacik 	if (check_ro_option(info, BTRFS_MOUNT_NOLOGREPLAY, "nologreplay") ||
829882dbe0cSJosef Bacik 	    check_ro_option(info, BTRFS_MOUNT_IGNOREBADROOTS, "ignorebadroots") ||
830882dbe0cSJosef Bacik 	    check_ro_option(info, BTRFS_MOUNT_IGNOREDATACSUMS, "ignoredatacsums"))
83196da0919SQu Wenruo 		ret = -EINVAL;
832a7a3f7caSSage Weil out:
8330b246afaSJeff Mahoney 	if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) &&
8343cdde224SJeff Mahoney 	    !btrfs_test_opt(info, FREE_SPACE_TREE) &&
8353cdde224SJeff Mahoney 	    !btrfs_test_opt(info, CLEAR_CACHE)) {
8360b246afaSJeff Mahoney 		btrfs_err(info, "cannot disable free space tree");
83770f6d82eSOmar Sandoval 		ret = -EINVAL;
83864b5d5b2SQu Wenruo 	}
83964b5d5b2SQu Wenruo 	if (btrfs_fs_compat_ro(info, BLOCK_GROUP_TREE) &&
8401d6a4fc8SQu Wenruo 	     !btrfs_test_opt(info, FREE_SPACE_TREE)) {
84164b5d5b2SQu Wenruo 		btrfs_err(info, "cannot disable free space tree with block-group-tree feature");
84264b5d5b2SQu Wenruo 		ret = -EINVAL;
84370f6d82eSOmar Sandoval 	}
8445d1ab66cSNaohiro Aota 	if (!ret)
8455d1ab66cSNaohiro Aota 		ret = btrfs_check_mountopts_zoned(info);
846dbecac26SMaciej S. Szmigiero 	if (!ret && !remounting) {
847dbecac26SMaciej S. Szmigiero 		if (btrfs_test_opt(info, SPACE_CACHE))
8480b246afaSJeff Mahoney 			btrfs_info(info, "disk space caching is enabled");
849dbecac26SMaciej S. Szmigiero 		if (btrfs_test_opt(info, FREE_SPACE_TREE))
8500b246afaSJeff Mahoney 			btrfs_info(info, "using free space tree");
851dbecac26SMaciej S. Szmigiero 	}
852a7a3f7caSSage Weil 	return ret;
853edf24abeSChristoph Hellwig }
854edf24abeSChristoph Hellwig 
855edf24abeSChristoph Hellwig /*
856edf24abeSChristoph Hellwig  * Parse mount options that are required early in the mount process.
857edf24abeSChristoph Hellwig  *
858edf24abeSChristoph Hellwig  * All other options will be parsed on much later in the mount process and
859edf24abeSChristoph Hellwig  * only when we need to allocate a new super block.
860edf24abeSChristoph Hellwig  */
btrfs_parse_device_options(const char * options,blk_mode_t flags)86105bdb996SChristoph Hellwig static int btrfs_parse_device_options(const char *options, blk_mode_t flags)
862edf24abeSChristoph Hellwig {
863edf24abeSChristoph Hellwig 	substring_t args[MAX_OPT_ARGS];
86483c8c9bdSJeff Liu 	char *device_name, *opts, *orig, *p;
86536350e95SGu Jinxiang 	struct btrfs_device *device = NULL;
866d7407606SMisono, Tomohiro 	int error = 0;
867d7407606SMisono, Tomohiro 
8685139cff5SDavid Sterba 	lockdep_assert_held(&uuid_mutex);
8695139cff5SDavid Sterba 
870d7407606SMisono, Tomohiro 	if (!options)
871d7407606SMisono, Tomohiro 		return 0;
872d7407606SMisono, Tomohiro 
873d7407606SMisono, Tomohiro 	/*
874d7407606SMisono, Tomohiro 	 * strsep changes the string, duplicate it because btrfs_parse_options
875d7407606SMisono, Tomohiro 	 * gets called later
876d7407606SMisono, Tomohiro 	 */
877d7407606SMisono, Tomohiro 	opts = kstrdup(options, GFP_KERNEL);
878d7407606SMisono, Tomohiro 	if (!opts)
879d7407606SMisono, Tomohiro 		return -ENOMEM;
880d7407606SMisono, Tomohiro 	orig = opts;
881d7407606SMisono, Tomohiro 
882d7407606SMisono, Tomohiro 	while ((p = strsep(&opts, ",")) != NULL) {
883d7407606SMisono, Tomohiro 		int token;
884d7407606SMisono, Tomohiro 
885d7407606SMisono, Tomohiro 		if (!*p)
886d7407606SMisono, Tomohiro 			continue;
887d7407606SMisono, Tomohiro 
888d7407606SMisono, Tomohiro 		token = match_token(p, tokens, args);
889d7407606SMisono, Tomohiro 		if (token == Opt_device) {
890d7407606SMisono, Tomohiro 			device_name = match_strdup(&args[0]);
891d7407606SMisono, Tomohiro 			if (!device_name) {
892d7407606SMisono, Tomohiro 				error = -ENOMEM;
893d7407606SMisono, Tomohiro 				goto out;
894d7407606SMisono, Tomohiro 			}
8952ef78928SChristoph Hellwig 			device = btrfs_scan_one_device(device_name, flags);
896d7407606SMisono, Tomohiro 			kfree(device_name);
89736350e95SGu Jinxiang 			if (IS_ERR(device)) {
89836350e95SGu Jinxiang 				error = PTR_ERR(device);
899d7407606SMisono, Tomohiro 				goto out;
900d7407606SMisono, Tomohiro 			}
901d7407606SMisono, Tomohiro 		}
90236350e95SGu Jinxiang 	}
903d7407606SMisono, Tomohiro 
904d7407606SMisono, Tomohiro out:
905d7407606SMisono, Tomohiro 	kfree(orig);
906d7407606SMisono, Tomohiro 	return error;
907d7407606SMisono, Tomohiro }
908d7407606SMisono, Tomohiro 
909d7407606SMisono, Tomohiro /*
910d7407606SMisono, Tomohiro  * Parse mount options that are related to subvolume id
911d7407606SMisono, Tomohiro  *
912d7407606SMisono, Tomohiro  * The value is later passed to mount_subvol()
913d7407606SMisono, Tomohiro  */
btrfs_parse_subvol_options(const char * options,char ** subvol_name,u64 * subvol_objectid)91493b9bcdfSGu Jinxiang static int btrfs_parse_subvol_options(const char *options, char **subvol_name,
91593b9bcdfSGu Jinxiang 		u64 *subvol_objectid)
916d7407606SMisono, Tomohiro {
917d7407606SMisono, Tomohiro 	substring_t args[MAX_OPT_ARGS];
918d7407606SMisono, Tomohiro 	char *opts, *orig, *p;
919edf24abeSChristoph Hellwig 	int error = 0;
920ccb0e7d1SAnand Jain 	u64 subvolid;
921edf24abeSChristoph Hellwig 
922edf24abeSChristoph Hellwig 	if (!options)
923830c4adbSJosef Bacik 		return 0;
924edf24abeSChristoph Hellwig 
925edf24abeSChristoph Hellwig 	/*
926d7407606SMisono, Tomohiro 	 * strsep changes the string, duplicate it because
927fa59f27cSAnand Jain 	 * btrfs_parse_device_options gets called later
928edf24abeSChristoph Hellwig 	 */
929edf24abeSChristoph Hellwig 	opts = kstrdup(options, GFP_KERNEL);
930edf24abeSChristoph Hellwig 	if (!opts)
931edf24abeSChristoph Hellwig 		return -ENOMEM;
9323f3d0bc0STero Roponen 	orig = opts;
933edf24abeSChristoph Hellwig 
934edf24abeSChristoph Hellwig 	while ((p = strsep(&opts, ",")) != NULL) {
935edf24abeSChristoph Hellwig 		int token;
936edf24abeSChristoph Hellwig 		if (!*p)
937edf24abeSChristoph Hellwig 			continue;
938edf24abeSChristoph Hellwig 
939edf24abeSChristoph Hellwig 		token = match_token(p, tokens, args);
940edf24abeSChristoph Hellwig 		switch (token) {
941edf24abeSChristoph Hellwig 		case Opt_subvol:
942a90e8b6fSIlya Dryomov 			kfree(*subvol_name);
943edf24abeSChristoph Hellwig 			*subvol_name = match_strdup(&args[0]);
9442c334e87SWang Shilong 			if (!*subvol_name) {
9452c334e87SWang Shilong 				error = -ENOMEM;
9462c334e87SWang Shilong 				goto out;
9472c334e87SWang Shilong 			}
948edf24abeSChristoph Hellwig 			break;
94973f73415SJosef Bacik 		case Opt_subvolid:
950ccb0e7d1SAnand Jain 			error = match_u64(&args[0], &subvolid);
951ccb0e7d1SAnand Jain 			if (error)
9522c334e87SWang Shilong 				goto out;
953ccb0e7d1SAnand Jain 
954ccb0e7d1SAnand Jain 			/* we want the original fs_tree */
955ccb0e7d1SAnand Jain 			if (subvolid == 0)
956ccb0e7d1SAnand Jain 				subvolid = BTRFS_FS_TREE_OBJECTID;
957ccb0e7d1SAnand Jain 
958ccb0e7d1SAnand Jain 			*subvol_objectid = subvolid;
95973f73415SJosef Bacik 			break;
960edf24abeSChristoph Hellwig 		default:
961edf24abeSChristoph Hellwig 			break;
962edf24abeSChristoph Hellwig 		}
963edf24abeSChristoph Hellwig 	}
964edf24abeSChristoph Hellwig 
965edf24abeSChristoph Hellwig out:
966830c4adbSJosef Bacik 	kfree(orig);
967edf24abeSChristoph Hellwig 	return error;
96895e05289SChris Mason }
96995e05289SChris Mason 
btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info * fs_info,u64 subvol_objectid)970c0c907a4SMarcos Paulo de Souza char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
97173f73415SJosef Bacik 					  u64 subvol_objectid)
97273f73415SJosef Bacik {
973815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
9745168489aSJosef Bacik 	struct btrfs_root *fs_root = NULL;
97505dbe683SOmar Sandoval 	struct btrfs_root_ref *root_ref;
97605dbe683SOmar Sandoval 	struct btrfs_inode_ref *inode_ref;
97705dbe683SOmar Sandoval 	struct btrfs_key key;
97805dbe683SOmar Sandoval 	struct btrfs_path *path = NULL;
97905dbe683SOmar Sandoval 	char *name = NULL, *ptr;
98005dbe683SOmar Sandoval 	u64 dirid;
98105dbe683SOmar Sandoval 	int len;
98205dbe683SOmar Sandoval 	int ret;
98305dbe683SOmar Sandoval 
98405dbe683SOmar Sandoval 	path = btrfs_alloc_path();
98505dbe683SOmar Sandoval 	if (!path) {
98605dbe683SOmar Sandoval 		ret = -ENOMEM;
98705dbe683SOmar Sandoval 		goto err;
98805dbe683SOmar Sandoval 	}
98905dbe683SOmar Sandoval 
9903ec83621SDavid Sterba 	name = kmalloc(PATH_MAX, GFP_KERNEL);
99105dbe683SOmar Sandoval 	if (!name) {
99205dbe683SOmar Sandoval 		ret = -ENOMEM;
99305dbe683SOmar Sandoval 		goto err;
99405dbe683SOmar Sandoval 	}
99505dbe683SOmar Sandoval 	ptr = name + PATH_MAX - 1;
99605dbe683SOmar Sandoval 	ptr[0] = '\0';
99705dbe683SOmar Sandoval 
99805dbe683SOmar Sandoval 	/*
99905dbe683SOmar Sandoval 	 * Walk up the subvolume trees in the tree of tree roots by root
100005dbe683SOmar Sandoval 	 * backrefs until we hit the top-level subvolume.
100105dbe683SOmar Sandoval 	 */
100205dbe683SOmar Sandoval 	while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
100305dbe683SOmar Sandoval 		key.objectid = subvol_objectid;
100405dbe683SOmar Sandoval 		key.type = BTRFS_ROOT_BACKREF_KEY;
100505dbe683SOmar Sandoval 		key.offset = (u64)-1;
100605dbe683SOmar Sandoval 
10070ff40a91SMarcos Paulo de Souza 		ret = btrfs_search_backwards(root, &key, path);
100805dbe683SOmar Sandoval 		if (ret < 0) {
100905dbe683SOmar Sandoval 			goto err;
101005dbe683SOmar Sandoval 		} else if (ret > 0) {
101105dbe683SOmar Sandoval 			ret = -ENOENT;
101205dbe683SOmar Sandoval 			goto err;
101305dbe683SOmar Sandoval 		}
101405dbe683SOmar Sandoval 
101505dbe683SOmar Sandoval 		subvol_objectid = key.offset;
101605dbe683SOmar Sandoval 
101705dbe683SOmar Sandoval 		root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
101805dbe683SOmar Sandoval 					  struct btrfs_root_ref);
101905dbe683SOmar Sandoval 		len = btrfs_root_ref_name_len(path->nodes[0], root_ref);
102005dbe683SOmar Sandoval 		ptr -= len + 1;
102105dbe683SOmar Sandoval 		if (ptr < name) {
102205dbe683SOmar Sandoval 			ret = -ENAMETOOLONG;
102305dbe683SOmar Sandoval 			goto err;
102405dbe683SOmar Sandoval 		}
102505dbe683SOmar Sandoval 		read_extent_buffer(path->nodes[0], ptr + 1,
102605dbe683SOmar Sandoval 				   (unsigned long)(root_ref + 1), len);
102705dbe683SOmar Sandoval 		ptr[0] = '/';
102805dbe683SOmar Sandoval 		dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);
102905dbe683SOmar Sandoval 		btrfs_release_path(path);
103005dbe683SOmar Sandoval 
103156e9357aSDavid Sterba 		fs_root = btrfs_get_fs_root(fs_info, subvol_objectid, true);
103205dbe683SOmar Sandoval 		if (IS_ERR(fs_root)) {
103305dbe683SOmar Sandoval 			ret = PTR_ERR(fs_root);
10345168489aSJosef Bacik 			fs_root = NULL;
10355168489aSJosef Bacik 			goto err;
10365168489aSJosef Bacik 		}
103705dbe683SOmar Sandoval 
103805dbe683SOmar Sandoval 		/*
103905dbe683SOmar Sandoval 		 * Walk up the filesystem tree by inode refs until we hit the
104005dbe683SOmar Sandoval 		 * root directory.
104105dbe683SOmar Sandoval 		 */
104205dbe683SOmar Sandoval 		while (dirid != BTRFS_FIRST_FREE_OBJECTID) {
104305dbe683SOmar Sandoval 			key.objectid = dirid;
104405dbe683SOmar Sandoval 			key.type = BTRFS_INODE_REF_KEY;
104505dbe683SOmar Sandoval 			key.offset = (u64)-1;
104605dbe683SOmar Sandoval 
10470ff40a91SMarcos Paulo de Souza 			ret = btrfs_search_backwards(fs_root, &key, path);
104805dbe683SOmar Sandoval 			if (ret < 0) {
104905dbe683SOmar Sandoval 				goto err;
105005dbe683SOmar Sandoval 			} else if (ret > 0) {
105105dbe683SOmar Sandoval 				ret = -ENOENT;
105205dbe683SOmar Sandoval 				goto err;
105305dbe683SOmar Sandoval 			}
105405dbe683SOmar Sandoval 
105505dbe683SOmar Sandoval 			dirid = key.offset;
105605dbe683SOmar Sandoval 
105705dbe683SOmar Sandoval 			inode_ref = btrfs_item_ptr(path->nodes[0],
105805dbe683SOmar Sandoval 						   path->slots[0],
105905dbe683SOmar Sandoval 						   struct btrfs_inode_ref);
106005dbe683SOmar Sandoval 			len = btrfs_inode_ref_name_len(path->nodes[0],
106105dbe683SOmar Sandoval 						       inode_ref);
106205dbe683SOmar Sandoval 			ptr -= len + 1;
106305dbe683SOmar Sandoval 			if (ptr < name) {
106405dbe683SOmar Sandoval 				ret = -ENAMETOOLONG;
106505dbe683SOmar Sandoval 				goto err;
106605dbe683SOmar Sandoval 			}
106705dbe683SOmar Sandoval 			read_extent_buffer(path->nodes[0], ptr + 1,
106805dbe683SOmar Sandoval 					   (unsigned long)(inode_ref + 1), len);
106905dbe683SOmar Sandoval 			ptr[0] = '/';
107005dbe683SOmar Sandoval 			btrfs_release_path(path);
107105dbe683SOmar Sandoval 		}
107200246528SJosef Bacik 		btrfs_put_root(fs_root);
10735168489aSJosef Bacik 		fs_root = NULL;
107405dbe683SOmar Sandoval 	}
107505dbe683SOmar Sandoval 
107605dbe683SOmar Sandoval 	btrfs_free_path(path);
107705dbe683SOmar Sandoval 	if (ptr == name + PATH_MAX - 1) {
107805dbe683SOmar Sandoval 		name[0] = '/';
107905dbe683SOmar Sandoval 		name[1] = '\0';
108005dbe683SOmar Sandoval 	} else {
108105dbe683SOmar Sandoval 		memmove(name, ptr, name + PATH_MAX - ptr);
108205dbe683SOmar Sandoval 	}
108305dbe683SOmar Sandoval 	return name;
108405dbe683SOmar Sandoval 
108505dbe683SOmar Sandoval err:
108600246528SJosef Bacik 	btrfs_put_root(fs_root);
108705dbe683SOmar Sandoval 	btrfs_free_path(path);
108805dbe683SOmar Sandoval 	kfree(name);
108905dbe683SOmar Sandoval 	return ERR_PTR(ret);
109005dbe683SOmar Sandoval }
109105dbe683SOmar Sandoval 
get_default_subvol_objectid(struct btrfs_fs_info * fs_info,u64 * objectid)109205dbe683SOmar Sandoval static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid)
109305dbe683SOmar Sandoval {
109405dbe683SOmar Sandoval 	struct btrfs_root *root = fs_info->tree_root;
109573f73415SJosef Bacik 	struct btrfs_dir_item *di;
109673f73415SJosef Bacik 	struct btrfs_path *path;
109773f73415SJosef Bacik 	struct btrfs_key location;
10986db75318SSweet Tea Dorminy 	struct fscrypt_str name = FSTR_INIT("default", 7);
109973f73415SJosef Bacik 	u64 dir_id;
110073f73415SJosef Bacik 
110173f73415SJosef Bacik 	path = btrfs_alloc_path();
110273f73415SJosef Bacik 	if (!path)
110305dbe683SOmar Sandoval 		return -ENOMEM;
110473f73415SJosef Bacik 
110573f73415SJosef Bacik 	/*
110673f73415SJosef Bacik 	 * Find the "default" dir item which points to the root item that we
110773f73415SJosef Bacik 	 * will mount by default if we haven't been given a specific subvolume
110873f73415SJosef Bacik 	 * to mount.
110973f73415SJosef Bacik 	 */
1110815745cfSAl Viro 	dir_id = btrfs_super_root_dir(fs_info->super_copy);
1111e43eec81SSweet Tea Dorminy 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, &name, 0);
1112b0839166SJulia Lawall 	if (IS_ERR(di)) {
1113b0839166SJulia Lawall 		btrfs_free_path(path);
111405dbe683SOmar Sandoval 		return PTR_ERR(di);
1115b0839166SJulia Lawall 	}
111673f73415SJosef Bacik 	if (!di) {
111773f73415SJosef Bacik 		/*
111873f73415SJosef Bacik 		 * Ok the default dir item isn't there.  This is weird since
111973f73415SJosef Bacik 		 * it's always been there, but don't freak out, just try and
112005dbe683SOmar Sandoval 		 * mount the top-level subvolume.
112173f73415SJosef Bacik 		 */
112273f73415SJosef Bacik 		btrfs_free_path(path);
112305dbe683SOmar Sandoval 		*objectid = BTRFS_FS_TREE_OBJECTID;
112405dbe683SOmar Sandoval 		return 0;
112573f73415SJosef Bacik 	}
112673f73415SJosef Bacik 
112773f73415SJosef Bacik 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
112873f73415SJosef Bacik 	btrfs_free_path(path);
112905dbe683SOmar Sandoval 	*objectid = location.objectid;
113005dbe683SOmar Sandoval 	return 0;
113173f73415SJosef Bacik }
113273f73415SJosef Bacik 
btrfs_fill_super(struct super_block * sb,struct btrfs_fs_devices * fs_devices,void * data)11338a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb,
11348a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
113556e033a7SDavid Sterba 			    void *data)
11362e635a27SChris Mason {
11372e635a27SChris Mason 	struct inode *inode;
1138815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
113939279cc3SChris Mason 	int err;
11402e635a27SChris Mason 
11412e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
11422e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
1143e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
1144af53d29aSAl Viro 	sb->s_d_op = &btrfs_dentry_operations;
1145be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
114614605409SBoris Burkov #ifdef CONFIG_FS_VERITY
114714605409SBoris Burkov 	sb->s_vop = &btrfs_verityops;
114814605409SBoris Burkov #endif
11495103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
11502e635a27SChris Mason 	sb->s_time_gran = 1;
11510eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL
11521751e8a6SLinus Torvalds 	sb->s_flags |= SB_POSIXACL;
115349cf6f45SChris Ball #endif
1154357fdad0SMatthew Garrett 	sb->s_flags |= SB_I_VERSION;
1155da2f0f74SChris Mason 	sb->s_iflags |= SB_I_CGROUPWB;
11569e11ceeeSJan Kara 
11579e11ceeeSJan Kara 	err = super_setup_bdi(sb);
11589e11ceeeSJan Kara 	if (err) {
11599e11ceeeSJan Kara 		btrfs_err(fs_info, "super_setup_bdi failed");
11609e11ceeeSJan Kara 		return err;
11619e11ceeeSJan Kara 	}
11629e11ceeeSJan Kara 
1163ad2b2c80SAl Viro 	err = open_ctree(sb, fs_devices, (char *)data);
1164ad2b2c80SAl Viro 	if (err) {
1165ab8d0fc4SJeff Mahoney 		btrfs_err(fs_info, "open_ctree failed");
1166ad2b2c80SAl Viro 		return err;
1167e20d96d6SChris Mason 	}
1168b888db2bSChris Mason 
11690202e83fSDavid Sterba 	inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, fs_info->fs_root);
11705d4f98a2SYan Zheng 	if (IS_ERR(inode)) {
11715d4f98a2SYan Zheng 		err = PTR_ERR(inode);
117213b98989SJosef Bacik 		btrfs_handle_fs_error(fs_info, err, NULL);
117339279cc3SChris Mason 		goto fail_close;
117439279cc3SChris Mason 	}
11752e635a27SChris Mason 
117648fde701SAl Viro 	sb->s_root = d_make_root(inode);
117748fde701SAl Viro 	if (!sb->s_root) {
117839279cc3SChris Mason 		err = -ENOMEM;
117939279cc3SChris Mason 		goto fail_close;
11802e635a27SChris Mason 	}
118158176a96SJosef Bacik 
11821751e8a6SLinus Torvalds 	sb->s_flags |= SB_ACTIVE;
11832e635a27SChris Mason 	return 0;
11842e635a27SChris Mason 
118539279cc3SChris Mason fail_close:
11866bccf3abSJeff Mahoney 	close_ctree(fs_info);
1187d5719762SChris Mason 	return err;
1188d5719762SChris Mason }
1189d5719762SChris Mason 
btrfs_sync_fs(struct super_block * sb,int wait)11906bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
1191d5719762SChris Mason {
1192d5719762SChris Mason 	struct btrfs_trans_handle *trans;
1193815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1194815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
1195df2ce34cSChris Mason 
1196bc074524SJeff Mahoney 	trace_btrfs_sync_fs(fs_info, wait);
11971abe9b8aSliubo 
1198d561c025SChris Mason 	if (!wait) {
1199815745cfSAl Viro 		filemap_flush(fs_info->btree_inode->i_mapping);
1200df2ce34cSChris Mason 		return 0;
1201d561c025SChris Mason 	}
1202771ed689SChris Mason 
12036374e57aSChris Mason 	btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
1204771ed689SChris Mason 
1205d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
120660376ce4SJosef Bacik 	if (IS_ERR(trans)) {
1207354aa0fbSMiao Xie 		/* no transaction, don't bother */
12086b5fe46dSDavid Sterba 		if (PTR_ERR(trans) == -ENOENT) {
12096b5fe46dSDavid Sterba 			/*
12106b5fe46dSDavid Sterba 			 * Exit unless we have some pending changes
12116b5fe46dSDavid Sterba 			 * that need to go through commit
12126b5fe46dSDavid Sterba 			 */
1213c52cc7b7SJosef Bacik 			if (!test_bit(BTRFS_FS_NEED_TRANS_COMMIT,
1214c52cc7b7SJosef Bacik 				      &fs_info->flags))
1215bd7de2c9SJosef Bacik 				return 0;
1216a53f4f8eSQu Wenruo 			/*
1217a53f4f8eSQu Wenruo 			 * A non-blocking test if the fs is frozen. We must not
1218a53f4f8eSQu Wenruo 			 * start a new transaction here otherwise a deadlock
1219a53f4f8eSQu Wenruo 			 * happens. The pending operations are delayed to the
1220a53f4f8eSQu Wenruo 			 * next commit after thawing.
1221a53f4f8eSQu Wenruo 			 */
1222a7e3c5f2SRakesh Pandit 			if (sb_start_write_trylock(sb))
1223a7e3c5f2SRakesh Pandit 				sb_end_write(sb);
1224a53f4f8eSQu Wenruo 			else
1225a53f4f8eSQu Wenruo 				return 0;
12266b5fe46dSDavid Sterba 			trans = btrfs_start_transaction(root, 0);
122760376ce4SJosef Bacik 		}
122898bd5c54SDavid Sterba 		if (IS_ERR(trans))
122998bd5c54SDavid Sterba 			return PTR_ERR(trans);
12306b5fe46dSDavid Sterba 	}
12313a45bb20SJeff Mahoney 	return btrfs_commit_transaction(trans);
1232d5719762SChris Mason }
1233d5719762SChris Mason 
print_rescue_option(struct seq_file * seq,const char * s,bool * printed)1234ab0b4a3eSJosef Bacik static void print_rescue_option(struct seq_file *seq, const char *s, bool *printed)
1235ab0b4a3eSJosef Bacik {
1236ab0b4a3eSJosef Bacik 	seq_printf(seq, "%s%s", (*printed) ? ":" : ",rescue=", s);
1237ab0b4a3eSJosef Bacik 	*printed = true;
1238ab0b4a3eSJosef Bacik }
1239ab0b4a3eSJosef Bacik 
btrfs_show_options(struct seq_file * seq,struct dentry * dentry)124034c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
1241a9572a15SEric Paris {
1242815745cfSAl Viro 	struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
12430f628c63SDavid Sterba 	const char *compress_type;
12443ef3959bSJosef Bacik 	const char *subvol_name;
1245ab0b4a3eSJosef Bacik 	bool printed = false;
1246a9572a15SEric Paris 
12473cdde224SJeff Mahoney 	if (btrfs_test_opt(info, DEGRADED))
1248a9572a15SEric Paris 		seq_puts(seq, ",degraded");
12493cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NODATASUM))
1250a9572a15SEric Paris 		seq_puts(seq, ",nodatasum");
12513cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NODATACOW))
1252a9572a15SEric Paris 		seq_puts(seq, ",nodatacow");
12533cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOBARRIER))
1254a9572a15SEric Paris 		seq_puts(seq, ",nobarrier");
125595ac567aSFilipe David Borba Manana 	if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
1256c1c9ff7cSGeert Uytterhoeven 		seq_printf(seq, ",max_inline=%llu", info->max_inline);
1257a9572a15SEric Paris 	if (info->thread_pool_size !=  min_t(unsigned long,
1258a9572a15SEric Paris 					     num_online_cpus() + 2, 8))
1259f7b885beSAnand Jain 		seq_printf(seq, ",thread_pool=%u", info->thread_pool_size);
12603cdde224SJeff Mahoney 	if (btrfs_test_opt(info, COMPRESS)) {
12610f628c63SDavid Sterba 		compress_type = btrfs_compress_type2str(info->compress_type);
12623cdde224SJeff Mahoney 		if (btrfs_test_opt(info, FORCE_COMPRESS))
1263200da64eSTsutomu Itoh 			seq_printf(seq, ",compress-force=%s", compress_type);
1264200da64eSTsutomu Itoh 		else
1265200da64eSTsutomu Itoh 			seq_printf(seq, ",compress=%s", compress_type);
1266f51d2b59SDavid Sterba 		if (info->compress_level)
1267fa4d885aSAdam Borowski 			seq_printf(seq, ":%d", info->compress_level);
1268200da64eSTsutomu Itoh 	}
12693cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOSSD))
1270c289811cSChris Mason 		seq_puts(seq, ",nossd");
12713cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SSD_SPREAD))
1272451d7585SChris Mason 		seq_puts(seq, ",ssd_spread");
12733cdde224SJeff Mahoney 	else if (btrfs_test_opt(info, SSD))
1274a9572a15SEric Paris 		seq_puts(seq, ",ssd");
12753cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOTREELOG))
12766b65c5c6SSage Weil 		seq_puts(seq, ",notreelog");
12773cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOLOGREPLAY))
1278ab0b4a3eSJosef Bacik 		print_rescue_option(seq, "nologreplay", &printed);
127968319c18SJosef Bacik 	if (btrfs_test_opt(info, USEBACKUPROOT))
128068319c18SJosef Bacik 		print_rescue_option(seq, "usebackuproot", &printed);
128142437a63SJosef Bacik 	if (btrfs_test_opt(info, IGNOREBADROOTS))
128242437a63SJosef Bacik 		print_rescue_option(seq, "ignorebadroots", &printed);
1283882dbe0cSJosef Bacik 	if (btrfs_test_opt(info, IGNOREDATACSUMS))
1284882dbe0cSJosef Bacik 		print_rescue_option(seq, "ignoredatacsums", &printed);
12853cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FLUSHONCOMMIT))
12866b65c5c6SSage Weil 		seq_puts(seq, ",flushoncommit");
128746b27f50SDennis Zhou 	if (btrfs_test_opt(info, DISCARD_SYNC))
128820a5239aSMatthew Wilcox 		seq_puts(seq, ",discard");
1289b0643e59SDennis Zhou 	if (btrfs_test_opt(info, DISCARD_ASYNC))
1290b0643e59SDennis Zhou 		seq_puts(seq, ",discard=async");
12911751e8a6SLinus Torvalds 	if (!(info->sb->s_flags & SB_POSIXACL))
1292a9572a15SEric Paris 		seq_puts(seq, ",noacl");
129304c41559SBoris Burkov 	if (btrfs_free_space_cache_v1_active(info))
1294200da64eSTsutomu Itoh 		seq_puts(seq, ",space_cache");
129504c41559SBoris Burkov 	else if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
129670f6d82eSOmar Sandoval 		seq_puts(seq, ",space_cache=v2");
129773bc1876SJosef Bacik 	else
12988965593eSDavid Sterba 		seq_puts(seq, ",nospace_cache");
12993cdde224SJeff Mahoney 	if (btrfs_test_opt(info, RESCAN_UUID_TREE))
1300f420ee1eSStefan Behrens 		seq_puts(seq, ",rescan_uuid_tree");
13013cdde224SJeff Mahoney 	if (btrfs_test_opt(info, CLEAR_CACHE))
1302200da64eSTsutomu Itoh 		seq_puts(seq, ",clear_cache");
13033cdde224SJeff Mahoney 	if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED))
1304200da64eSTsutomu Itoh 		seq_puts(seq, ",user_subvol_rm_allowed");
13053cdde224SJeff Mahoney 	if (btrfs_test_opt(info, ENOSPC_DEBUG))
13060942caa3SDavid Sterba 		seq_puts(seq, ",enospc_debug");
13073cdde224SJeff Mahoney 	if (btrfs_test_opt(info, AUTO_DEFRAG))
13080942caa3SDavid Sterba 		seq_puts(seq, ",autodefrag");
13093cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SKIP_BALANCE))
13109555c6c1SIlya Dryomov 		seq_puts(seq, ",skip_balance");
13118507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
1312cbeaae4fSDavid Sterba 	if (btrfs_test_opt(info, CHECK_INTEGRITY_DATA))
13138507d216SWang Shilong 		seq_puts(seq, ",check_int_data");
13143cdde224SJeff Mahoney 	else if (btrfs_test_opt(info, CHECK_INTEGRITY))
13158507d216SWang Shilong 		seq_puts(seq, ",check_int");
13168507d216SWang Shilong 	if (info->check_integrity_print_mask)
13178507d216SWang Shilong 		seq_printf(seq, ",check_int_print_mask=%d",
13188507d216SWang Shilong 				info->check_integrity_print_mask);
13198507d216SWang Shilong #endif
13208507d216SWang Shilong 	if (info->metadata_ratio)
1321764cb8b4SAnand Jain 		seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio);
13223cdde224SJeff Mahoney 	if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR))
13238c342930SJeff Mahoney 		seq_puts(seq, ",fatal_errors=panic");
13248b87dc17SDavid Sterba 	if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
1325d3740608SAnand Jain 		seq_printf(seq, ",commit=%u", info->commit_interval);
1326d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
13273cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FRAGMENT_DATA))
1328d0bd4560SJosef Bacik 		seq_puts(seq, ",fragment=data");
13293cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FRAGMENT_METADATA))
1330d0bd4560SJosef Bacik 		seq_puts(seq, ",fragment=metadata");
1331d0bd4560SJosef Bacik #endif
1332fb592373SJosef Bacik 	if (btrfs_test_opt(info, REF_VERIFY))
1333fb592373SJosef Bacik 		seq_puts(seq, ",ref_verify");
1334c8d3fe02SOmar Sandoval 	seq_printf(seq, ",subvolid=%llu",
1335c8d3fe02SOmar Sandoval 		  BTRFS_I(d_inode(dentry))->root->root_key.objectid);
13363ef3959bSJosef Bacik 	subvol_name = btrfs_get_subvol_name_from_objectid(info,
13373ef3959bSJosef Bacik 			BTRFS_I(d_inode(dentry))->root->root_key.objectid);
13383ef3959bSJosef Bacik 	if (!IS_ERR(subvol_name)) {
1339c8d3fe02SOmar Sandoval 		seq_puts(seq, ",subvol=");
13403ef3959bSJosef Bacik 		seq_escape(seq, subvol_name, " \t\n\\");
13413ef3959bSJosef Bacik 		kfree(subvol_name);
13423ef3959bSJosef Bacik 	}
1343a9572a15SEric Paris 	return 0;
1344a9572a15SEric Paris }
1345a9572a15SEric Paris 
btrfs_test_super(struct super_block * s,void * data)1346a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data)
13472e635a27SChris Mason {
1348815745cfSAl Viro 	struct btrfs_fs_info *p = data;
1349815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(s);
13504b82d6e4SYan 
1351815745cfSAl Viro 	return fs_info->fs_devices == p->fs_devices;
13524b82d6e4SYan }
13534b82d6e4SYan 
btrfs_set_super(struct super_block * s,void * data)1354450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data)
1355450ba0eaSJosef Bacik {
13566de1d09dSAl Viro 	int err = set_anon_super(s, data);
13576de1d09dSAl Viro 	if (!err)
1358450ba0eaSJosef Bacik 		s->s_fs_info = data;
13596de1d09dSAl Viro 	return err;
1360450ba0eaSJosef Bacik }
1361450ba0eaSJosef Bacik 
1362830c4adbSJosef Bacik /*
1363f9d9ef62SDavid Sterba  * subvolumes are identified by ino 256
1364f9d9ef62SDavid Sterba  */
is_subvolume_inode(struct inode * inode)1365f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode)
1366f9d9ef62SDavid Sterba {
1367f9d9ef62SDavid Sterba 	if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
1368f9d9ef62SDavid Sterba 		return 1;
1369f9d9ef62SDavid Sterba 	return 0;
1370f9d9ef62SDavid Sterba }
1371f9d9ef62SDavid Sterba 
mount_subvol(const char * subvol_name,u64 subvol_objectid,struct vfsmount * mnt)1372bb289b7bSOmar Sandoval static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
1373ae0bc863SAnand Jain 				   struct vfsmount *mnt)
1374830c4adbSJosef Bacik {
1375830c4adbSJosef Bacik 	struct dentry *root;
1376fa330659SOmar Sandoval 	int ret;
1377830c4adbSJosef Bacik 
137805dbe683SOmar Sandoval 	if (!subvol_name) {
137905dbe683SOmar Sandoval 		if (!subvol_objectid) {
138005dbe683SOmar Sandoval 			ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
138105dbe683SOmar Sandoval 							  &subvol_objectid);
138205dbe683SOmar Sandoval 			if (ret) {
138305dbe683SOmar Sandoval 				root = ERR_PTR(ret);
138405dbe683SOmar Sandoval 				goto out;
138505dbe683SOmar Sandoval 			}
138605dbe683SOmar Sandoval 		}
1387c0c907a4SMarcos Paulo de Souza 		subvol_name = btrfs_get_subvol_name_from_objectid(
1388c0c907a4SMarcos Paulo de Souza 					btrfs_sb(mnt->mnt_sb), subvol_objectid);
138905dbe683SOmar Sandoval 		if (IS_ERR(subvol_name)) {
139005dbe683SOmar Sandoval 			root = ERR_CAST(subvol_name);
139105dbe683SOmar Sandoval 			subvol_name = NULL;
139205dbe683SOmar Sandoval 			goto out;
139305dbe683SOmar Sandoval 		}
139405dbe683SOmar Sandoval 
139505dbe683SOmar Sandoval 	}
139605dbe683SOmar Sandoval 
1397ea441d11SAl Viro 	root = mount_subtree(mnt, subvol_name);
1398fa330659SOmar Sandoval 	/* mount_subtree() drops our reference on the vfsmount. */
1399fa330659SOmar Sandoval 	mnt = NULL;
1400830c4adbSJosef Bacik 
1401bb289b7bSOmar Sandoval 	if (!IS_ERR(root)) {
1402ea441d11SAl Viro 		struct super_block *s = root->d_sb;
1403ab8d0fc4SJeff Mahoney 		struct btrfs_fs_info *fs_info = btrfs_sb(s);
1404bb289b7bSOmar Sandoval 		struct inode *root_inode = d_inode(root);
1405bb289b7bSOmar Sandoval 		u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid;
1406bb289b7bSOmar Sandoval 
1407bb289b7bSOmar Sandoval 		ret = 0;
1408bb289b7bSOmar Sandoval 		if (!is_subvolume_inode(root_inode)) {
1409ab8d0fc4SJeff Mahoney 			btrfs_err(fs_info, "'%s' is not a valid subvolume",
1410bb289b7bSOmar Sandoval 			       subvol_name);
1411bb289b7bSOmar Sandoval 			ret = -EINVAL;
1412bb289b7bSOmar Sandoval 		}
1413bb289b7bSOmar Sandoval 		if (subvol_objectid && root_objectid != subvol_objectid) {
141405dbe683SOmar Sandoval 			/*
141505dbe683SOmar Sandoval 			 * This will also catch a race condition where a
141605dbe683SOmar Sandoval 			 * subvolume which was passed by ID is renamed and
141705dbe683SOmar Sandoval 			 * another subvolume is renamed over the old location.
141805dbe683SOmar Sandoval 			 */
1419ab8d0fc4SJeff Mahoney 			btrfs_err(fs_info,
1420ab8d0fc4SJeff Mahoney 				  "subvol '%s' does not match subvolid %llu",
1421bb289b7bSOmar Sandoval 				  subvol_name, subvol_objectid);
1422bb289b7bSOmar Sandoval 			ret = -EINVAL;
1423bb289b7bSOmar Sandoval 		}
1424bb289b7bSOmar Sandoval 		if (ret) {
1425ea441d11SAl Viro 			dput(root);
1426bb289b7bSOmar Sandoval 			root = ERR_PTR(ret);
1427ea441d11SAl Viro 			deactivate_locked_super(s);
1428bb289b7bSOmar Sandoval 		}
1429f9d9ef62SDavid Sterba 	}
1430f9d9ef62SDavid Sterba 
1431fa330659SOmar Sandoval out:
1432fa330659SOmar Sandoval 	mntput(mnt);
1433fa330659SOmar Sandoval 	kfree(subvol_name);
1434830c4adbSJosef Bacik 	return root;
1435830c4adbSJosef Bacik }
1436450ba0eaSJosef Bacik 
1437312c89fbSMisono, Tomohiro /*
1438312c89fbSMisono, Tomohiro  * Find a superblock for the given device / mount point.
1439312c89fbSMisono, Tomohiro  *
1440312c89fbSMisono, Tomohiro  * Note: This is based on mount_bdev from fs/super.c with a few additions
1441312c89fbSMisono, Tomohiro  *       for multiple device setup.  Make sure to keep it in sync.
1442312c89fbSMisono, Tomohiro  */
btrfs_mount_root(struct file_system_type * fs_type,int flags,const char * device_name,void * data)144372fa39f5SMisono, Tomohiro static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,
144472fa39f5SMisono, Tomohiro 		int flags, const char *device_name, void *data)
144572fa39f5SMisono, Tomohiro {
144672fa39f5SMisono, Tomohiro 	struct block_device *bdev = NULL;
144772fa39f5SMisono, Tomohiro 	struct super_block *s;
144836350e95SGu Jinxiang 	struct btrfs_device *device = NULL;
144972fa39f5SMisono, Tomohiro 	struct btrfs_fs_devices *fs_devices = NULL;
145072fa39f5SMisono, Tomohiro 	struct btrfs_fs_info *fs_info = NULL;
1451204cc0ccSAl Viro 	void *new_sec_opts = NULL;
145205bdb996SChristoph Hellwig 	blk_mode_t mode = sb_open_mode(flags);
145372fa39f5SMisono, Tomohiro 	int error = 0;
145472fa39f5SMisono, Tomohiro 
145572fa39f5SMisono, Tomohiro 	if (data) {
1456a65001e8SAl Viro 		error = security_sb_eat_lsm_opts(data, &new_sec_opts);
145772fa39f5SMisono, Tomohiro 		if (error)
145872fa39f5SMisono, Tomohiro 			return ERR_PTR(error);
145972fa39f5SMisono, Tomohiro 	}
146072fa39f5SMisono, Tomohiro 
146172fa39f5SMisono, Tomohiro 	/*
146272fa39f5SMisono, Tomohiro 	 * Setup a dummy root and fs_info for test/set super.  This is because
146372fa39f5SMisono, Tomohiro 	 * we don't actually fill this stuff out until open_ctree, but we need
14648260edbaSJosef Bacik 	 * then open_ctree will properly initialize the file system specific
14658260edbaSJosef Bacik 	 * settings later.  btrfs_init_fs_info initializes the static elements
14668260edbaSJosef Bacik 	 * of the fs_info (locks and such) to make cleanup easier if we find a
14678260edbaSJosef Bacik 	 * superblock with our given fs_devices later on at sget() time.
146872fa39f5SMisono, Tomohiro 	 */
1469a8fd1f71SJeff Mahoney 	fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL);
147072fa39f5SMisono, Tomohiro 	if (!fs_info) {
147172fa39f5SMisono, Tomohiro 		error = -ENOMEM;
147272fa39f5SMisono, Tomohiro 		goto error_sec_opts;
147372fa39f5SMisono, Tomohiro 	}
14748260edbaSJosef Bacik 	btrfs_init_fs_info(fs_info);
147572fa39f5SMisono, Tomohiro 
147672fa39f5SMisono, Tomohiro 	fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
147772fa39f5SMisono, Tomohiro 	fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
147872fa39f5SMisono, Tomohiro 	if (!fs_info->super_copy || !fs_info->super_for_commit) {
147972fa39f5SMisono, Tomohiro 		error = -ENOMEM;
148072fa39f5SMisono, Tomohiro 		goto error_fs_info;
148172fa39f5SMisono, Tomohiro 	}
148272fa39f5SMisono, Tomohiro 
1483f5194e34SDavid Sterba 	mutex_lock(&uuid_mutex);
14842ef78928SChristoph Hellwig 	error = btrfs_parse_device_options(data, mode);
148581ffd56bSDavid Sterba 	if (error) {
1486399f7f4cSDavid Sterba 		mutex_unlock(&uuid_mutex);
1487399f7f4cSDavid Sterba 		goto error_fs_info;
148881ffd56bSDavid Sterba 	}
1489399f7f4cSDavid Sterba 
14902ef78928SChristoph Hellwig 	device = btrfs_scan_one_device(device_name, mode);
149136350e95SGu Jinxiang 	if (IS_ERR(device)) {
1492399f7f4cSDavid Sterba 		mutex_unlock(&uuid_mutex);
149336350e95SGu Jinxiang 		error = PTR_ERR(device);
1494399f7f4cSDavid Sterba 		goto error_fs_info;
149581ffd56bSDavid Sterba 	}
1496399f7f4cSDavid Sterba 
149736350e95SGu Jinxiang 	fs_devices = device->fs_devices;
1498399f7f4cSDavid Sterba 	fs_info->fs_devices = fs_devices;
1499399f7f4cSDavid Sterba 
150072fa39f5SMisono, Tomohiro 	error = btrfs_open_devices(fs_devices, mode, fs_type);
1501f5194e34SDavid Sterba 	mutex_unlock(&uuid_mutex);
150272fa39f5SMisono, Tomohiro 	if (error)
150372fa39f5SMisono, Tomohiro 		goto error_fs_info;
150472fa39f5SMisono, Tomohiro 
150572fa39f5SMisono, Tomohiro 	if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) {
150672fa39f5SMisono, Tomohiro 		error = -EACCES;
150772fa39f5SMisono, Tomohiro 		goto error_close_devices;
150872fa39f5SMisono, Tomohiro 	}
150972fa39f5SMisono, Tomohiro 
1510d24fa5c1SAnand Jain 	bdev = fs_devices->latest_dev->bdev;
151172fa39f5SMisono, Tomohiro 	s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC,
151272fa39f5SMisono, Tomohiro 		 fs_info);
151372fa39f5SMisono, Tomohiro 	if (IS_ERR(s)) {
151472fa39f5SMisono, Tomohiro 		error = PTR_ERR(s);
151572fa39f5SMisono, Tomohiro 		goto error_close_devices;
151672fa39f5SMisono, Tomohiro 	}
151772fa39f5SMisono, Tomohiro 
151872fa39f5SMisono, Tomohiro 	if (s->s_root) {
151972fa39f5SMisono, Tomohiro 		btrfs_close_devices(fs_devices);
15200d4b0463SJosef Bacik 		btrfs_free_fs_info(fs_info);
152172fa39f5SMisono, Tomohiro 		if ((flags ^ s->s_flags) & SB_RDONLY)
152272fa39f5SMisono, Tomohiro 			error = -EBUSY;
152372fa39f5SMisono, Tomohiro 	} else {
152472fa39f5SMisono, Tomohiro 		snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
1525e33c267aSRoman Gushchin 		shrinker_debugfs_rename(&s->s_shrink, "sb-%s:%s", fs_type->name,
1526e33c267aSRoman Gushchin 					s->s_id);
152772fa39f5SMisono, Tomohiro 		btrfs_sb(s)->bdev_holder = fs_type;
152872fa39f5SMisono, Tomohiro 		error = btrfs_fill_super(s, fs_devices, data);
152972fa39f5SMisono, Tomohiro 	}
1530a65001e8SAl Viro 	if (!error)
1531204cc0ccSAl Viro 		error = security_sb_set_mnt_opts(s, new_sec_opts, 0, NULL);
1532a65001e8SAl Viro 	security_free_mnt_opts(&new_sec_opts);
153372fa39f5SMisono, Tomohiro 	if (error) {
153472fa39f5SMisono, Tomohiro 		deactivate_locked_super(s);
1535a65001e8SAl Viro 		return ERR_PTR(error);
153672fa39f5SMisono, Tomohiro 	}
153772fa39f5SMisono, Tomohiro 
153872fa39f5SMisono, Tomohiro 	return dget(s->s_root);
153972fa39f5SMisono, Tomohiro 
154072fa39f5SMisono, Tomohiro error_close_devices:
154172fa39f5SMisono, Tomohiro 	btrfs_close_devices(fs_devices);
154272fa39f5SMisono, Tomohiro error_fs_info:
15430d4b0463SJosef Bacik 	btrfs_free_fs_info(fs_info);
154472fa39f5SMisono, Tomohiro error_sec_opts:
154572fa39f5SMisono, Tomohiro 	security_free_mnt_opts(&new_sec_opts);
154672fa39f5SMisono, Tomohiro 	return ERR_PTR(error);
154772fa39f5SMisono, Tomohiro }
1548312c89fbSMisono, Tomohiro 
1549edf24abeSChristoph Hellwig /*
1550312c89fbSMisono, Tomohiro  * Mount function which is called by VFS layer.
1551edf24abeSChristoph Hellwig  *
1552312c89fbSMisono, Tomohiro  * In order to allow mounting a subvolume directly, btrfs uses mount_subtree()
1553312c89fbSMisono, Tomohiro  * which needs vfsmount* of device's root (/).  This means device's root has to
1554312c89fbSMisono, Tomohiro  * be mounted internally in any case.
1555312c89fbSMisono, Tomohiro  *
1556312c89fbSMisono, Tomohiro  * Operation flow:
1557312c89fbSMisono, Tomohiro  *   1. Parse subvol id related options for later use in mount_subvol().
1558312c89fbSMisono, Tomohiro  *
1559312c89fbSMisono, Tomohiro  *   2. Mount device's root (/) by calling vfs_kern_mount().
1560312c89fbSMisono, Tomohiro  *
1561312c89fbSMisono, Tomohiro  *      NOTE: vfs_kern_mount() is used by VFS to call btrfs_mount() in the
1562312c89fbSMisono, Tomohiro  *      first place. In order to avoid calling btrfs_mount() again, we use
1563312c89fbSMisono, Tomohiro  *      different file_system_type which is not registered to VFS by
1564312c89fbSMisono, Tomohiro  *      register_filesystem() (btrfs_root_fs_type). As a result,
1565312c89fbSMisono, Tomohiro  *      btrfs_mount_root() is called. The return value will be used by
1566312c89fbSMisono, Tomohiro  *      mount_subtree() in mount_subvol().
1567312c89fbSMisono, Tomohiro  *
1568312c89fbSMisono, Tomohiro  *   3. Call mount_subvol() to get the dentry of subvolume. Since there is
1569312c89fbSMisono, Tomohiro  *      "btrfs subvolume set-default", mount_subvol() is called always.
1570edf24abeSChristoph Hellwig  */
btrfs_mount(struct file_system_type * fs_type,int flags,const char * device_name,void * data)1571061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
1572306e16ceSDavid Sterba 		const char *device_name, void *data)
15734b82d6e4SYan {
1574312c89fbSMisono, Tomohiro 	struct vfsmount *mnt_root;
1575312c89fbSMisono, Tomohiro 	struct dentry *root;
157673f73415SJosef Bacik 	char *subvol_name = NULL;
157773f73415SJosef Bacik 	u64 subvol_objectid = 0;
15784b82d6e4SYan 	int error = 0;
15794b82d6e4SYan 
158093b9bcdfSGu Jinxiang 	error = btrfs_parse_subvol_options(data, &subvol_name,
158193b9bcdfSGu Jinxiang 					&subvol_objectid);
1582f23c8af8SIlya Dryomov 	if (error) {
1583f23c8af8SIlya Dryomov 		kfree(subvol_name);
1584061dbc6bSAl Viro 		return ERR_PTR(error);
1585f23c8af8SIlya Dryomov 	}
1586edf24abeSChristoph Hellwig 
1587312c89fbSMisono, Tomohiro 	/* mount device's root (/) */
1588312c89fbSMisono, Tomohiro 	mnt_root = vfs_kern_mount(&btrfs_root_fs_type, flags, device_name, data);
1589312c89fbSMisono, Tomohiro 	if (PTR_ERR_OR_ZERO(mnt_root) == -EBUSY) {
1590312c89fbSMisono, Tomohiro 		if (flags & SB_RDONLY) {
1591312c89fbSMisono, Tomohiro 			mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1592312c89fbSMisono, Tomohiro 				flags & ~SB_RDONLY, device_name, data);
15934b82d6e4SYan 		} else {
1594312c89fbSMisono, Tomohiro 			mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1595312c89fbSMisono, Tomohiro 				flags | SB_RDONLY, device_name, data);
1596312c89fbSMisono, Tomohiro 			if (IS_ERR(mnt_root)) {
1597312c89fbSMisono, Tomohiro 				root = ERR_CAST(mnt_root);
1598532b618bSEric W. Biederman 				kfree(subvol_name);
1599312c89fbSMisono, Tomohiro 				goto out;
1600f667aef6SQu Wenruo 			}
1601f667aef6SQu Wenruo 
1602312c89fbSMisono, Tomohiro 			down_write(&mnt_root->mnt_sb->s_umount);
1603312c89fbSMisono, Tomohiro 			error = btrfs_remount(mnt_root->mnt_sb, &flags, NULL);
1604312c89fbSMisono, Tomohiro 			up_write(&mnt_root->mnt_sb->s_umount);
1605312c89fbSMisono, Tomohiro 			if (error < 0) {
1606312c89fbSMisono, Tomohiro 				root = ERR_PTR(error);
1607312c89fbSMisono, Tomohiro 				mntput(mnt_root);
1608532b618bSEric W. Biederman 				kfree(subvol_name);
1609312c89fbSMisono, Tomohiro 				goto out;
1610312c89fbSMisono, Tomohiro 			}
1611312c89fbSMisono, Tomohiro 		}
1612312c89fbSMisono, Tomohiro 	}
1613312c89fbSMisono, Tomohiro 	if (IS_ERR(mnt_root)) {
1614312c89fbSMisono, Tomohiro 		root = ERR_CAST(mnt_root);
1615532b618bSEric W. Biederman 		kfree(subvol_name);
1616312c89fbSMisono, Tomohiro 		goto out;
1617f667aef6SQu Wenruo 	}
16184b82d6e4SYan 
1619312c89fbSMisono, Tomohiro 	/* mount_subvol() will free subvol_name and mnt_root */
1620ae0bc863SAnand Jain 	root = mount_subvol(subvol_name, subvol_objectid, mnt_root);
16214b82d6e4SYan 
1622312c89fbSMisono, Tomohiro out:
1623312c89fbSMisono, Tomohiro 	return root;
16244b82d6e4SYan }
16252e635a27SChris Mason 
btrfs_resize_thread_pool(struct btrfs_fs_info * fs_info,u32 new_pool_size,u32 old_pool_size)16260d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
1627f7b885beSAnand Jain 				     u32 new_pool_size, u32 old_pool_size)
16280d2450abSSergei Trofimovich {
16290d2450abSSergei Trofimovich 	if (new_pool_size == old_pool_size)
16300d2450abSSergei Trofimovich 		return;
16310d2450abSSergei Trofimovich 
16320d2450abSSergei Trofimovich 	fs_info->thread_pool_size = new_pool_size;
16330d2450abSSergei Trofimovich 
1634efe120a0SFrank Holton 	btrfs_info(fs_info, "resize thread pool %d -> %d",
16350d2450abSSergei Trofimovich 	       old_pool_size, new_pool_size);
16360d2450abSSergei Trofimovich 
16375cdc7ad3SQu Wenruo 	btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
1638afe3d242SQu Wenruo 	btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
1639e66f0bb1SQu Wenruo 	btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
164040fac647SChristoph Hellwig 	workqueue_set_max_active(fs_info->endio_workers, new_pool_size);
164140fac647SChristoph Hellwig 	workqueue_set_max_active(fs_info->endio_meta_workers, new_pool_size);
1642fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
1643fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
16445b3bc44eSQu Wenruo 	btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
16450d2450abSSergei Trofimovich }
16460d2450abSSergei Trofimovich 
btrfs_remount_begin(struct btrfs_fs_info * fs_info,unsigned long old_opts,int flags)1647f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1648f42a34b2SMiao Xie 				       unsigned long old_opts, int flags)
1649f42a34b2SMiao Xie {
1650dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1651dc81cdc5SMiao Xie 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
16521751e8a6SLinus Torvalds 	     (flags & SB_RDONLY))) {
1653dc81cdc5SMiao Xie 		/* wait for any defraggers to finish */
1654dc81cdc5SMiao Xie 		wait_event(fs_info->transaction_wait,
1655dc81cdc5SMiao Xie 			   (atomic_read(&fs_info->defrag_running) == 0));
16561751e8a6SLinus Torvalds 		if (flags & SB_RDONLY)
1657dc81cdc5SMiao Xie 			sync_filesystem(fs_info->sb);
1658dc81cdc5SMiao Xie 	}
1659dc81cdc5SMiao Xie }
1660dc81cdc5SMiao Xie 
btrfs_remount_cleanup(struct btrfs_fs_info * fs_info,unsigned long old_opts)1661dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1662dc81cdc5SMiao Xie 					 unsigned long old_opts)
1663dc81cdc5SMiao Xie {
166494846229SBoris Burkov 	const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE);
166594846229SBoris Burkov 
1666dc81cdc5SMiao Xie 	/*
1667180e4d47SLuis de Bethencourt 	 * We need to cleanup all defragable inodes if the autodefragment is
1668180e4d47SLuis de Bethencourt 	 * close or the filesystem is read only.
1669dc81cdc5SMiao Xie 	 */
1670dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1671bc98a42cSDavid Howells 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) {
1672dc81cdc5SMiao Xie 		btrfs_cleanup_defrag_inodes(fs_info);
1673dc81cdc5SMiao Xie 	}
1674dc81cdc5SMiao Xie 
1675b0643e59SDennis Zhou 	/* If we toggled discard async */
1676b0643e59SDennis Zhou 	if (!btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1677b0643e59SDennis Zhou 	    btrfs_test_opt(fs_info, DISCARD_ASYNC))
1678b0643e59SDennis Zhou 		btrfs_discard_resume(fs_info);
1679b0643e59SDennis Zhou 	else if (btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1680b0643e59SDennis Zhou 		 !btrfs_test_opt(fs_info, DISCARD_ASYNC))
1681b0643e59SDennis Zhou 		btrfs_discard_cleanup(fs_info);
168294846229SBoris Burkov 
168394846229SBoris Burkov 	/* If we toggled space cache */
168494846229SBoris Burkov 	if (cache_opt != btrfs_free_space_cache_v1_active(fs_info))
168594846229SBoris Burkov 		btrfs_set_free_space_cache_v1_active(fs_info, cache_opt);
1686dc81cdc5SMiao Xie }
1687dc81cdc5SMiao Xie 
btrfs_remount(struct super_block * sb,int * flags,char * data)1688c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1689c146afadSYan Zheng {
1690815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
169149b25e05SJeff Mahoney 	unsigned old_flags = sb->s_flags;
169249b25e05SJeff Mahoney 	unsigned long old_opts = fs_info->mount_opt;
169349b25e05SJeff Mahoney 	unsigned long old_compress_type = fs_info->compress_type;
169449b25e05SJeff Mahoney 	u64 old_max_inline = fs_info->max_inline;
1695f7b885beSAnand Jain 	u32 old_thread_pool_size = fs_info->thread_pool_size;
1696d612ac59SAnand Jain 	u32 old_metadata_ratio = fs_info->metadata_ratio;
1697c146afadSYan Zheng 	int ret;
1698c146afadSYan Zheng 
169902b9984dSTheodore Ts'o 	sync_filesystem(sb);
170088c4703fSJohannes Thumshirn 	set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1701dc81cdc5SMiao Xie 
1702f667aef6SQu Wenruo 	if (data) {
1703204cc0ccSAl Viro 		void *new_sec_opts = NULL;
1704f667aef6SQu Wenruo 
1705a65001e8SAl Viro 		ret = security_sb_eat_lsm_opts(data, &new_sec_opts);
1706a65001e8SAl Viro 		if (!ret)
1707204cc0ccSAl Viro 			ret = security_sb_remount(sb, new_sec_opts);
1708a65001e8SAl Viro 		security_free_mnt_opts(&new_sec_opts);
1709f667aef6SQu Wenruo 		if (ret)
1710f667aef6SQu Wenruo 			goto restore;
1711f667aef6SQu Wenruo 	}
1712f667aef6SQu Wenruo 
17132ff7e61eSJeff Mahoney 	ret = btrfs_parse_options(fs_info, data, *flags);
1714891f41cbSChengguang Xu 	if (ret)
171549b25e05SJeff Mahoney 		goto restore;
1716b288052eSChris Mason 
17172ba48b20SQu Wenruo 	ret = btrfs_check_features(fs_info, !(*flags & SB_RDONLY));
1718d7f67ac9SQu Wenruo 	if (ret < 0)
17190591f040SQu Wenruo 		goto restore;
1720d7f67ac9SQu Wenruo 
1721f42a34b2SMiao Xie 	btrfs_remount_begin(fs_info, old_opts, *flags);
17220d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
17230d2450abSSergei Trofimovich 		fs_info->thread_pool_size, old_thread_pool_size);
17240d2450abSSergei Trofimovich 
1725c55a4319SBoris Burkov 	if ((bool)btrfs_test_opt(fs_info, FREE_SPACE_TREE) !=
1726c55a4319SBoris Burkov 	    (bool)btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
17272838d255SBoris Burkov 	    (!sb_rdonly(sb) || (*flags & SB_RDONLY))) {
17282838d255SBoris Burkov 		btrfs_warn(fs_info,
17292838d255SBoris Burkov 		"remount supports changing free space tree only from ro to rw");
17302838d255SBoris Burkov 		/* Make sure free space cache options match the state on disk */
17312838d255SBoris Burkov 		if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
17322838d255SBoris Burkov 			btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
17332838d255SBoris Burkov 			btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
17342838d255SBoris Burkov 		}
17352838d255SBoris Burkov 		if (btrfs_free_space_cache_v1_active(fs_info)) {
17362838d255SBoris Burkov 			btrfs_clear_opt(fs_info->mount_opt, FREE_SPACE_TREE);
17372838d255SBoris Burkov 			btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE);
17382838d255SBoris Burkov 		}
17392838d255SBoris Burkov 	}
17402838d255SBoris Burkov 
17411751e8a6SLinus Torvalds 	if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
1742dc81cdc5SMiao Xie 		goto out;
1743c146afadSYan Zheng 
17441751e8a6SLinus Torvalds 	if (*flags & SB_RDONLY) {
17458dabb742SStefan Behrens 		/*
17468dabb742SStefan Behrens 		 * this also happens on 'umount -rf' or on shutdown, when
17478dabb742SStefan Behrens 		 * the filesystem is busy.
17488dabb742SStefan Behrens 		 */
174921c7e756SMiao Xie 		cancel_work_sync(&fs_info->async_reclaim_work);
175057056740SJosef Bacik 		cancel_work_sync(&fs_info->async_data_reclaim_work);
1751361c093dSStefan Behrens 
1752b0643e59SDennis Zhou 		btrfs_discard_cleanup(fs_info);
1753b0643e59SDennis Zhou 
1754361c093dSStefan Behrens 		/* wait for the uuid_scan task to finish */
1755361c093dSStefan Behrens 		down(&fs_info->uuid_tree_rescan_sem);
1756361c093dSStefan Behrens 		/* avoid complains from lockdep et al. */
1757361c093dSStefan Behrens 		up(&fs_info->uuid_tree_rescan_sem);
1758361c093dSStefan Behrens 
1759a0a1db70SFilipe Manana 		btrfs_set_sb_rdonly(sb);
1760c146afadSYan Zheng 
1761e44163e1SJeff Mahoney 		/*
17621751e8a6SLinus Torvalds 		 * Setting SB_RDONLY will put the cleaner thread to
1763e44163e1SJeff Mahoney 		 * sleep at the next loop if it's already active.
1764e44163e1SJeff Mahoney 		 * If it's already asleep, we'll leave unused block
1765e44163e1SJeff Mahoney 		 * groups on disk until we're mounted read-write again
1766e44163e1SJeff Mahoney 		 * unless we clean them up here.
1767e44163e1SJeff Mahoney 		 */
1768e44163e1SJeff Mahoney 		btrfs_delete_unused_bgs(fs_info);
1769e44163e1SJeff Mahoney 
1770a0a1db70SFilipe Manana 		/*
1771a0a1db70SFilipe Manana 		 * The cleaner task could be already running before we set the
1772a0a1db70SFilipe Manana 		 * flag BTRFS_FS_STATE_RO (and SB_RDONLY in the superblock).
1773a0a1db70SFilipe Manana 		 * We must make sure that after we finish the remount, i.e. after
1774a0a1db70SFilipe Manana 		 * we call btrfs_commit_super(), the cleaner can no longer start
1775a0a1db70SFilipe Manana 		 * a transaction - either because it was dropping a dead root,
1776a0a1db70SFilipe Manana 		 * running delayed iputs or deleting an unused block group (the
1777a0a1db70SFilipe Manana 		 * cleaner picked a block group from the list of unused block
1778a0a1db70SFilipe Manana 		 * groups before we were able to in the previous call to
1779a0a1db70SFilipe Manana 		 * btrfs_delete_unused_bgs()).
1780a0a1db70SFilipe Manana 		 */
1781a0a1db70SFilipe Manana 		wait_on_bit(&fs_info->flags, BTRFS_FS_CLEANER_RUNNING,
1782a0a1db70SFilipe Manana 			    TASK_UNINTERRUPTIBLE);
1783a0a1db70SFilipe Manana 
1784a8cc263eSFilipe Manana 		/*
1785a8cc263eSFilipe Manana 		 * We've set the superblock to RO mode, so we might have made
1786a8cc263eSFilipe Manana 		 * the cleaner task sleep without running all pending delayed
1787a8cc263eSFilipe Manana 		 * iputs. Go through all the delayed iputs here, so that if an
1788a8cc263eSFilipe Manana 		 * unmount happens without remounting RW we don't end up at
1789a8cc263eSFilipe Manana 		 * finishing close_ctree() with a non-empty list of delayed
1790a8cc263eSFilipe Manana 		 * iputs.
1791a8cc263eSFilipe Manana 		 */
1792a8cc263eSFilipe Manana 		btrfs_run_delayed_iputs(fs_info);
1793a8cc263eSFilipe Manana 
17948dabb742SStefan Behrens 		btrfs_dev_replace_suspend_for_unmount(fs_info);
17958dabb742SStefan Behrens 		btrfs_scrub_cancel(fs_info);
1796061594efSMiao Xie 		btrfs_pause_balance(fs_info);
17978dabb742SStefan Behrens 
1798cb13eea3SFilipe Manana 		/*
1799cb13eea3SFilipe Manana 		 * Pause the qgroup rescan worker if it is running. We don't want
1800cb13eea3SFilipe Manana 		 * it to be still running after we are in RO mode, as after that,
1801cb13eea3SFilipe Manana 		 * by the time we unmount, it might have left a transaction open,
1802cb13eea3SFilipe Manana 		 * so we would leak the transaction and/or crash.
1803cb13eea3SFilipe Manana 		 */
1804cb13eea3SFilipe Manana 		btrfs_qgroup_wait_for_completion(fs_info, false);
1805cb13eea3SFilipe Manana 
18066bccf3abSJeff Mahoney 		ret = btrfs_commit_super(fs_info);
180749b25e05SJeff Mahoney 		if (ret)
180849b25e05SJeff Mahoney 			goto restore;
1809c146afadSYan Zheng 	} else {
181084961539SJosef Bacik 		if (BTRFS_FS_ERROR(fs_info)) {
18116ef3de9cSDavid Sterba 			btrfs_err(fs_info,
1812efe120a0SFrank Holton 				"Remounting read-write after error is not allowed");
18136ef3de9cSDavid Sterba 			ret = -EINVAL;
18146ef3de9cSDavid Sterba 			goto restore;
18156ef3de9cSDavid Sterba 		}
18168a3db184SSergei Trofimovich 		if (fs_info->fs_devices->rw_devices == 0) {
181749b25e05SJeff Mahoney 			ret = -EACCES;
181849b25e05SJeff Mahoney 			goto restore;
18198a3db184SSergei Trofimovich 		}
18202b82032cSYan Zheng 
18216528b99dSAnand Jain 		if (!btrfs_check_rw_degradable(fs_info, NULL)) {
1822efe120a0SFrank Holton 			btrfs_warn(fs_info,
182352042d8eSAndrea Gelmini 		"too many missing devices, writable remount is not allowed");
1824292fd7fcSStefan Behrens 			ret = -EACCES;
1825292fd7fcSStefan Behrens 			goto restore;
1826292fd7fcSStefan Behrens 		}
1827292fd7fcSStefan Behrens 
18288a3db184SSergei Trofimovich 		if (btrfs_super_log_root(fs_info->super_copy) != 0) {
182910a3a3edSDavid Sterba 			btrfs_warn(fs_info,
183010a3a3edSDavid Sterba 		"mount required to replay tree-log, cannot remount read-write");
183149b25e05SJeff Mahoney 			ret = -EINVAL;
183249b25e05SJeff Mahoney 			goto restore;
18338a3db184SSergei Trofimovich 		}
1834c146afadSYan Zheng 
183544c0ca21SBoris Burkov 		/*
183644c0ca21SBoris Burkov 		 * NOTE: when remounting with a change that does writes, don't
183744c0ca21SBoris Burkov 		 * put it anywhere above this point, as we are not sure to be
183844c0ca21SBoris Burkov 		 * safe to write until we pass the above checks.
183944c0ca21SBoris Burkov 		 */
184044c0ca21SBoris Burkov 		ret = btrfs_start_pre_rw_mount(fs_info);
184149b25e05SJeff Mahoney 		if (ret)
184249b25e05SJeff Mahoney 			goto restore;
1843c146afadSYan Zheng 
1844a0a1db70SFilipe Manana 		btrfs_clear_sb_rdonly(sb);
184590c711abSZygo Blaxell 
1846afcdd129SJosef Bacik 		set_bit(BTRFS_FS_OPEN, &fs_info->flags);
1847981a37baSChris Mason 
1848981a37baSChris Mason 		/*
1849981a37baSChris Mason 		 * If we've gone from readonly -> read/write, we need to get
1850981a37baSChris Mason 		 * our sync/async discard lists in the right state.
1851981a37baSChris Mason 		 */
1852981a37baSChris Mason 		btrfs_discard_resume(fs_info);
1853c146afadSYan Zheng 	}
1854dc81cdc5SMiao Xie out:
1855faa00889SJosef Bacik 	/*
1856faa00889SJosef Bacik 	 * We need to set SB_I_VERSION here otherwise it'll get cleared by VFS,
1857faa00889SJosef Bacik 	 * since the absence of the flag means it can be toggled off by remount.
1858faa00889SJosef Bacik 	 */
1859faa00889SJosef Bacik 	*flags |= SB_I_VERSION;
1860faa00889SJosef Bacik 
18612c6a92b0SJustin Maggard 	wake_up_process(fs_info->transaction_kthread);
1862dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
18638cd29088SBoris Burkov 	btrfs_clear_oneshot_options(fs_info);
186488c4703fSJohannes Thumshirn 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
186588c4703fSJohannes Thumshirn 
1866c146afadSYan Zheng 	return 0;
186749b25e05SJeff Mahoney 
186849b25e05SJeff Mahoney restore:
18691751e8a6SLinus Torvalds 	/* We've hit an error - don't reset SB_RDONLY */
1870bc98a42cSDavid Howells 	if (sb_rdonly(sb))
18711751e8a6SLinus Torvalds 		old_flags |= SB_RDONLY;
1872a0a1db70SFilipe Manana 	if (!(old_flags & SB_RDONLY))
1873a0a1db70SFilipe Manana 		clear_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state);
187449b25e05SJeff Mahoney 	sb->s_flags = old_flags;
187549b25e05SJeff Mahoney 	fs_info->mount_opt = old_opts;
187649b25e05SJeff Mahoney 	fs_info->compress_type = old_compress_type;
187749b25e05SJeff Mahoney 	fs_info->max_inline = old_max_inline;
18780d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
18790d2450abSSergei Trofimovich 		old_thread_pool_size, fs_info->thread_pool_size);
188049b25e05SJeff Mahoney 	fs_info->metadata_ratio = old_metadata_ratio;
1881dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
188288c4703fSJohannes Thumshirn 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
188388c4703fSJohannes Thumshirn 
188449b25e05SJeff Mahoney 	return ret;
1885c146afadSYan Zheng }
1886c146afadSYan Zheng 
1887bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */
btrfs_cmp_device_free_bytes(const void * a,const void * b)1888214cc184SDavid Sterba static int btrfs_cmp_device_free_bytes(const void *a, const void *b)
1889bcd53741SArne Jansen {
1890214cc184SDavid Sterba 	const struct btrfs_device_info *dev_info1 = a;
1891214cc184SDavid Sterba 	const struct btrfs_device_info *dev_info2 = b;
1892214cc184SDavid Sterba 
1893214cc184SDavid Sterba 	if (dev_info1->max_avail > dev_info2->max_avail)
1894bcd53741SArne Jansen 		return -1;
1895214cc184SDavid Sterba 	else if (dev_info1->max_avail < dev_info2->max_avail)
1896bcd53741SArne Jansen 		return 1;
1897bcd53741SArne Jansen 	return 0;
1898bcd53741SArne Jansen }
1899bcd53741SArne Jansen 
1900bcd53741SArne Jansen /*
1901bcd53741SArne Jansen  * sort the devices by max_avail, in which max free extent size of each device
1902bcd53741SArne Jansen  * is stored.(Descending Sort)
1903bcd53741SArne Jansen  */
btrfs_descending_sort_devices(struct btrfs_device_info * devices,size_t nr_devices)1904bcd53741SArne Jansen static inline void btrfs_descending_sort_devices(
1905bcd53741SArne Jansen 					struct btrfs_device_info *devices,
1906bcd53741SArne Jansen 					size_t nr_devices)
1907bcd53741SArne Jansen {
1908bcd53741SArne Jansen 	sort(devices, nr_devices, sizeof(struct btrfs_device_info),
1909bcd53741SArne Jansen 	     btrfs_cmp_device_free_bytes, NULL);
1910bcd53741SArne Jansen }
1911bcd53741SArne Jansen 
19126d07bcecSMiao Xie /*
19136d07bcecSMiao Xie  * The helper to calc the free space on the devices that can be used to store
19146d07bcecSMiao Xie  * file data.
19156d07bcecSMiao Xie  */
btrfs_calc_avail_data_space(struct btrfs_fs_info * fs_info,u64 * free_bytes)19167e17916bSArnd Bergmann static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
19176bccf3abSJeff Mahoney 					      u64 *free_bytes)
19186d07bcecSMiao Xie {
19196d07bcecSMiao Xie 	struct btrfs_device_info *devices_info;
19206d07bcecSMiao Xie 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
19216d07bcecSMiao Xie 	struct btrfs_device *device;
19226d07bcecSMiao Xie 	u64 type;
19236d07bcecSMiao Xie 	u64 avail_space;
19246d07bcecSMiao Xie 	u64 min_stripe_size;
1925559ca6eaSNikolay Borisov 	int num_stripes = 1;
19266d07bcecSMiao Xie 	int i = 0, nr_devices;
19274f080f57SDavid Sterba 	const struct btrfs_raid_attr *rattr;
19286d07bcecSMiao Xie 
19297e33fd99SJosef Bacik 	/*
193001327610SNicholas D Steeves 	 * We aren't under the device list lock, so this is racy-ish, but good
19317e33fd99SJosef Bacik 	 * enough for our purposes.
19327e33fd99SJosef Bacik 	 */
1933b772a86eSLi Zefan 	nr_devices = fs_info->fs_devices->open_devices;
19347e33fd99SJosef Bacik 	if (!nr_devices) {
19357e33fd99SJosef Bacik 		smp_mb();
19367e33fd99SJosef Bacik 		nr_devices = fs_info->fs_devices->open_devices;
19377e33fd99SJosef Bacik 		ASSERT(nr_devices);
19387e33fd99SJosef Bacik 		if (!nr_devices) {
19397e33fd99SJosef Bacik 			*free_bytes = 0;
19407e33fd99SJosef Bacik 			return 0;
19417e33fd99SJosef Bacik 		}
19427e33fd99SJosef Bacik 	}
19436d07bcecSMiao Xie 
1944d9b0d9baSDulshani Gunawardhana 	devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
19456a44517dSDavid Sterba 			       GFP_KERNEL);
19466d07bcecSMiao Xie 	if (!devices_info)
19476d07bcecSMiao Xie 		return -ENOMEM;
19486d07bcecSMiao Xie 
194901327610SNicholas D Steeves 	/* calc min stripe number for data space allocation */
19501b86826dSJeff Mahoney 	type = btrfs_data_alloc_profile(fs_info);
19514f080f57SDavid Sterba 	rattr = &btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)];
19524f080f57SDavid Sterba 
1953e1ea2beeSDavid Sterba 	if (type & BTRFS_BLOCK_GROUP_RAID0)
195439fb26c3SMiao Xie 		num_stripes = nr_devices;
1955d09cb9e1SDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID1_MASK)
1956d09cb9e1SDavid Sterba 		num_stripes = rattr->ncopies;
1957e1ea2beeSDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID10)
195839fb26c3SMiao Xie 		num_stripes = 4;
19596d07bcecSMiao Xie 
19604f080f57SDavid Sterba 	/* Adjust for more than 1 stripe per device */
19614f080f57SDavid Sterba 	min_stripe_size = rattr->dev_stripes * BTRFS_STRIPE_LEN;
19626d07bcecSMiao Xie 
19637e33fd99SJosef Bacik 	rcu_read_lock();
19647e33fd99SJosef Bacik 	list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
1965e12c9621SAnand Jain 		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
1966e12c9621SAnand Jain 						&device->dev_state) ||
1967401e29c1SAnand Jain 		    !device->bdev ||
1968401e29c1SAnand Jain 		    test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
19696d07bcecSMiao Xie 			continue;
19706d07bcecSMiao Xie 
19717e33fd99SJosef Bacik 		if (i >= nr_devices)
19727e33fd99SJosef Bacik 			break;
19737e33fd99SJosef Bacik 
19746d07bcecSMiao Xie 		avail_space = device->total_bytes - device->bytes_used;
19756d07bcecSMiao Xie 
19766d07bcecSMiao Xie 		/* align with stripe_len */
1977559ca6eaSNikolay Borisov 		avail_space = rounddown(avail_space, BTRFS_STRIPE_LEN);
19786d07bcecSMiao Xie 
19796d07bcecSMiao Xie 		/*
198037f85ec3SQu Wenruo 		 * Ensure we have at least min_stripe_size on top of the
198137f85ec3SQu Wenruo 		 * reserved space on the device.
19826d07bcecSMiao Xie 		 */
198337f85ec3SQu Wenruo 		if (avail_space <= BTRFS_DEVICE_RANGE_RESERVED + min_stripe_size)
19846d07bcecSMiao Xie 			continue;
19856d07bcecSMiao Xie 
198637f85ec3SQu Wenruo 		avail_space -= BTRFS_DEVICE_RANGE_RESERVED;
1987559ca6eaSNikolay Borisov 
19886d07bcecSMiao Xie 		devices_info[i].dev = device;
19896d07bcecSMiao Xie 		devices_info[i].max_avail = avail_space;
19906d07bcecSMiao Xie 
19916d07bcecSMiao Xie 		i++;
19926d07bcecSMiao Xie 	}
19937e33fd99SJosef Bacik 	rcu_read_unlock();
19946d07bcecSMiao Xie 
19956d07bcecSMiao Xie 	nr_devices = i;
19966d07bcecSMiao Xie 
19976d07bcecSMiao Xie 	btrfs_descending_sort_devices(devices_info, nr_devices);
19986d07bcecSMiao Xie 
19996d07bcecSMiao Xie 	i = nr_devices - 1;
20006d07bcecSMiao Xie 	avail_space = 0;
2001559ca6eaSNikolay Borisov 	while (nr_devices >= rattr->devs_min) {
2002559ca6eaSNikolay Borisov 		num_stripes = min(num_stripes, nr_devices);
200339fb26c3SMiao Xie 
20046d07bcecSMiao Xie 		if (devices_info[i].max_avail >= min_stripe_size) {
20056d07bcecSMiao Xie 			int j;
20066d07bcecSMiao Xie 			u64 alloc_size;
20076d07bcecSMiao Xie 
200839fb26c3SMiao Xie 			avail_space += devices_info[i].max_avail * num_stripes;
20096d07bcecSMiao Xie 			alloc_size = devices_info[i].max_avail;
201039fb26c3SMiao Xie 			for (j = i + 1 - num_stripes; j <= i; j++)
20116d07bcecSMiao Xie 				devices_info[j].max_avail -= alloc_size;
20126d07bcecSMiao Xie 		}
20136d07bcecSMiao Xie 		i--;
20146d07bcecSMiao Xie 		nr_devices--;
20156d07bcecSMiao Xie 	}
20166d07bcecSMiao Xie 
20176d07bcecSMiao Xie 	kfree(devices_info);
20186d07bcecSMiao Xie 	*free_bytes = avail_space;
20196d07bcecSMiao Xie 	return 0;
20206d07bcecSMiao Xie }
20216d07bcecSMiao Xie 
2022ba7b6e62SDavid Sterba /*
2023ba7b6e62SDavid Sterba  * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
2024ba7b6e62SDavid Sterba  *
2025ba7b6e62SDavid Sterba  * If there's a redundant raid level at DATA block groups, use the respective
2026ba7b6e62SDavid Sterba  * multiplier to scale the sizes.
2027ba7b6e62SDavid Sterba  *
2028ba7b6e62SDavid Sterba  * Unused device space usage is based on simulating the chunk allocator
20290d0c71b3SDavid Sterba  * algorithm that respects the device sizes and order of allocations.  This is
20300d0c71b3SDavid Sterba  * a close approximation of the actual use but there are other factors that may
20310d0c71b3SDavid Sterba  * change the result (like a new metadata chunk).
2032ba7b6e62SDavid Sterba  *
2033ca8a51b3SDavid Sterba  * If metadata is exhausted, f_bavail will be 0.
2034ba7b6e62SDavid Sterba  */
btrfs_statfs(struct dentry * dentry,struct kstatfs * buf)20358fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
20368fd17795SChris Mason {
2037815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
2038815745cfSAl Viro 	struct btrfs_super_block *disk_super = fs_info->super_copy;
2039bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
2040bd4d1088SJosef Bacik 	u64 total_used = 0;
20416d07bcecSMiao Xie 	u64 total_free_data = 0;
2042ca8a51b3SDavid Sterba 	u64 total_free_meta = 0;
2043265fdfa6SDavid Sterba 	u32 bits = fs_info->sectorsize_bits;
2044de37aa51SNikolay Borisov 	__be32 *fsid = (__be32 *)fs_info->fs_devices->fsid;
2045ba7b6e62SDavid Sterba 	unsigned factor = 1;
2046ba7b6e62SDavid Sterba 	struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
20476d07bcecSMiao Xie 	int ret;
2048ca8a51b3SDavid Sterba 	u64 thresh = 0;
2049ae02d1bdSLuis de Bethencourt 	int mixed = 0;
20508fd17795SChris Mason 
205172804905SJosef Bacik 	list_for_each_entry(found, &fs_info->space_info, list) {
20526d07bcecSMiao Xie 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
2053ba7b6e62SDavid Sterba 			int i;
2054ba7b6e62SDavid Sterba 
20556d07bcecSMiao Xie 			total_free_data += found->disk_total - found->disk_used;
20566d07bcecSMiao Xie 			total_free_data -=
20576d07bcecSMiao Xie 				btrfs_account_ro_block_groups_free_space(found);
2058ba7b6e62SDavid Sterba 
2059ba7b6e62SDavid Sterba 			for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
206046df06b8SDavid Sterba 				if (!list_empty(&found->block_groups[i]))
206146df06b8SDavid Sterba 					factor = btrfs_bg_type_to_factor(
206246df06b8SDavid Sterba 						btrfs_raid_array[i].bg_flag);
2063ba7b6e62SDavid Sterba 			}
20646d07bcecSMiao Xie 		}
2065ae02d1bdSLuis de Bethencourt 
2066ae02d1bdSLuis de Bethencourt 		/*
206767da05b3SColin Ian King 		 * Metadata in mixed block group profiles are accounted in data
2068ae02d1bdSLuis de Bethencourt 		 */
2069ae02d1bdSLuis de Bethencourt 		if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) {
2070ae02d1bdSLuis de Bethencourt 			if (found->flags & BTRFS_BLOCK_GROUP_DATA)
2071ae02d1bdSLuis de Bethencourt 				mixed = 1;
2072ae02d1bdSLuis de Bethencourt 			else
2073ae02d1bdSLuis de Bethencourt 				total_free_meta += found->disk_total -
2074ae02d1bdSLuis de Bethencourt 					found->disk_used;
2075ae02d1bdSLuis de Bethencourt 		}
20766d07bcecSMiao Xie 
2077b742bb82SYan, Zheng 		total_used += found->disk_used;
207889a55897SJosef Bacik 	}
2079ba7b6e62SDavid Sterba 
2080ba7b6e62SDavid Sterba 	buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
2081ba7b6e62SDavid Sterba 	buf->f_blocks >>= bits;
2082ba7b6e62SDavid Sterba 	buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
2083ba7b6e62SDavid Sterba 
2084ba7b6e62SDavid Sterba 	/* Account global block reserve as used, it's in logical size already */
2085ba7b6e62SDavid Sterba 	spin_lock(&block_rsv->lock);
208641b34accSLuis de Bethencourt 	/* Mixed block groups accounting is not byte-accurate, avoid overflow */
208741b34accSLuis de Bethencourt 	if (buf->f_bfree >= block_rsv->size >> bits)
2088ba7b6e62SDavid Sterba 		buf->f_bfree -= block_rsv->size >> bits;
208941b34accSLuis de Bethencourt 	else
209041b34accSLuis de Bethencourt 		buf->f_bfree = 0;
2091ba7b6e62SDavid Sterba 	spin_unlock(&block_rsv->lock);
2092ba7b6e62SDavid Sterba 
20930d95c1beSDavid Sterba 	buf->f_bavail = div_u64(total_free_data, factor);
20946bccf3abSJeff Mahoney 	ret = btrfs_calc_avail_data_space(fs_info, &total_free_data);
20957e33fd99SJosef Bacik 	if (ret)
20966d07bcecSMiao Xie 		return ret;
2097ba7b6e62SDavid Sterba 	buf->f_bavail += div_u64(total_free_data, factor);
20986d07bcecSMiao Xie 	buf->f_bavail = buf->f_bavail >> bits;
2099d397712bSChris Mason 
2100ca8a51b3SDavid Sterba 	/*
2101ca8a51b3SDavid Sterba 	 * We calculate the remaining metadata space minus global reserve. If
2102ca8a51b3SDavid Sterba 	 * this is (supposedly) smaller than zero, there's no space. But this
2103ca8a51b3SDavid Sterba 	 * does not hold in practice, the exhausted state happens where's still
2104ca8a51b3SDavid Sterba 	 * some positive delta. So we apply some guesswork and compare the
2105ca8a51b3SDavid Sterba 	 * delta to a 4M threshold.  (Practically observed delta was ~2M.)
2106ca8a51b3SDavid Sterba 	 *
2107ca8a51b3SDavid Sterba 	 * We probably cannot calculate the exact threshold value because this
2108ca8a51b3SDavid Sterba 	 * depends on the internal reservations requested by various
2109ca8a51b3SDavid Sterba 	 * operations, so some operations that consume a few metadata will
2110ca8a51b3SDavid Sterba 	 * succeed even if the Avail is zero. But this is better than the other
2111ca8a51b3SDavid Sterba 	 * way around.
2112ca8a51b3SDavid Sterba 	 */
2113d4417e22SNikolay Borisov 	thresh = SZ_4M;
2114ca8a51b3SDavid Sterba 
2115d55966c4SJosef Bacik 	/*
2116d55966c4SJosef Bacik 	 * We only want to claim there's no available space if we can no longer
2117d55966c4SJosef Bacik 	 * allocate chunks for our metadata profile and our global reserve will
2118d55966c4SJosef Bacik 	 * not fit in the free metadata space.  If we aren't ->full then we
2119d55966c4SJosef Bacik 	 * still can allocate chunks and thus are fine using the currently
2120d55966c4SJosef Bacik 	 * calculated f_bavail.
2121d55966c4SJosef Bacik 	 */
2122d55966c4SJosef Bacik 	if (!mixed && block_rsv->space_info->full &&
212358bfe2ccSJosef Bacik 	    (total_free_meta < thresh || total_free_meta - thresh < block_rsv->size))
2124ca8a51b3SDavid Sterba 		buf->f_bavail = 0;
2125ca8a51b3SDavid Sterba 
2126ba7b6e62SDavid Sterba 	buf->f_type = BTRFS_SUPER_MAGIC;
2127*1bca9776SDavid Sterba 	buf->f_bsize = fs_info->sectorsize;
2128ba7b6e62SDavid Sterba 	buf->f_namelen = BTRFS_NAME_LEN;
2129ba7b6e62SDavid Sterba 
21309d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
21319d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
21329d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
21339d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
21349d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
213532d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
21364fd786e6SMisono Tomohiro 	buf->f_fsid.val[0] ^=
21374fd786e6SMisono Tomohiro 		BTRFS_I(d_inode(dentry))->root->root_key.objectid >> 32;
21384fd786e6SMisono Tomohiro 	buf->f_fsid.val[1] ^=
21394fd786e6SMisono Tomohiro 		BTRFS_I(d_inode(dentry))->root->root_key.objectid;
214032d48fa1SDavid Woodhouse 
21418fd17795SChris Mason 	return 0;
21428fd17795SChris Mason }
2143b5133862SChris Mason 
btrfs_kill_super(struct super_block * sb)2144aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb)
2145aea52e19SAl Viro {
2146815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2147aea52e19SAl Viro 	kill_anon_super(sb);
21480d4b0463SJosef Bacik 	btrfs_free_fs_info(fs_info);
2149aea52e19SAl Viro }
2150aea52e19SAl Viro 
21512e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
21522e635a27SChris Mason 	.owner		= THIS_MODULE,
21532e635a27SChris Mason 	.name		= "btrfs",
2154061dbc6bSAl Viro 	.mount		= btrfs_mount,
2155aea52e19SAl Viro 	.kill_sb	= btrfs_kill_super,
2156f667aef6SQu Wenruo 	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
21572e635a27SChris Mason };
215872fa39f5SMisono, Tomohiro 
215972fa39f5SMisono, Tomohiro static struct file_system_type btrfs_root_fs_type = {
216072fa39f5SMisono, Tomohiro 	.owner		= THIS_MODULE,
216172fa39f5SMisono, Tomohiro 	.name		= "btrfs",
216272fa39f5SMisono, Tomohiro 	.mount		= btrfs_mount_root,
216372fa39f5SMisono, Tomohiro 	.kill_sb	= btrfs_kill_super,
21645b9b26f5SChristian Brauner 	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA | FS_ALLOW_IDMAP,
216572fa39f5SMisono, Tomohiro };
216672fa39f5SMisono, Tomohiro 
21677f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs");
2168a9218f6bSChris Mason 
btrfs_control_open(struct inode * inode,struct file * file)2169d8620958STom Van Braeckel static int btrfs_control_open(struct inode *inode, struct file *file)
2170d8620958STom Van Braeckel {
2171d8620958STom Van Braeckel 	/*
2172d8620958STom Van Braeckel 	 * The control file's private_data is used to hold the
2173d8620958STom Van Braeckel 	 * transaction when it is started and is used to keep
2174d8620958STom Van Braeckel 	 * track of whether a transaction is already in progress.
2175d8620958STom Van Braeckel 	 */
2176d8620958STom Van Braeckel 	file->private_data = NULL;
2177d8620958STom Van Braeckel 	return 0;
2178d8620958STom Van Braeckel }
2179d8620958STom Van Braeckel 
2180d352ac68SChris Mason /*
2181cfe953c8SSu Yue  * Used by /dev/btrfs-control for devices ioctls.
2182d352ac68SChris Mason  */
btrfs_control_ioctl(struct file * file,unsigned int cmd,unsigned long arg)21838a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
21848a4b83ccSChris Mason 				unsigned long arg)
21858a4b83ccSChris Mason {
21868a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
218736350e95SGu Jinxiang 	struct btrfs_device *device = NULL;
218816cab91aSAnand Jain 	dev_t devt = 0;
2189c071fcfdSChris Mason 	int ret = -ENOTTY;
21908a4b83ccSChris Mason 
2191e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2192e441d54dSChris Mason 		return -EPERM;
2193e441d54dSChris Mason 
2194dae7b665SLi Zefan 	vol = memdup_user((void __user *)arg, sizeof(*vol));
2195dae7b665SLi Zefan 	if (IS_ERR(vol))
2196dae7b665SLi Zefan 		return PTR_ERR(vol);
2197f505754fSFilipe Manana 	vol->name[BTRFS_PATH_NAME_MAX] = '\0';
2198c071fcfdSChris Mason 
21998a4b83ccSChris Mason 	switch (cmd) {
22008a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
2201899f9307SDavid Sterba 		mutex_lock(&uuid_mutex);
220205bdb996SChristoph Hellwig 		device = btrfs_scan_one_device(vol->name, BLK_OPEN_READ);
220336350e95SGu Jinxiang 		ret = PTR_ERR_OR_ZERO(device);
2204899f9307SDavid Sterba 		mutex_unlock(&uuid_mutex);
22058a4b83ccSChris Mason 		break;
2206228a73abSAnand Jain 	case BTRFS_IOC_FORGET_DEV:
220716cab91aSAnand Jain 		if (vol->name[0] != 0) {
220816cab91aSAnand Jain 			ret = lookup_bdev(vol->name, &devt);
220916cab91aSAnand Jain 			if (ret)
221016cab91aSAnand Jain 				break;
221116cab91aSAnand Jain 		}
221216cab91aSAnand Jain 		ret = btrfs_forget_devices(devt);
2213228a73abSAnand Jain 		break;
221402db0844SJosef Bacik 	case BTRFS_IOC_DEVICES_READY:
2215899f9307SDavid Sterba 		mutex_lock(&uuid_mutex);
221605bdb996SChristoph Hellwig 		device = btrfs_scan_one_device(vol->name, BLK_OPEN_READ);
221736350e95SGu Jinxiang 		if (IS_ERR(device)) {
2218899f9307SDavid Sterba 			mutex_unlock(&uuid_mutex);
221936350e95SGu Jinxiang 			ret = PTR_ERR(device);
222002db0844SJosef Bacik 			break;
2221899f9307SDavid Sterba 		}
222236350e95SGu Jinxiang 		ret = !(device->fs_devices->num_devices ==
222336350e95SGu Jinxiang 			device->fs_devices->total_devices);
2224899f9307SDavid Sterba 		mutex_unlock(&uuid_mutex);
222502db0844SJosef Bacik 		break;
2226c5868f83SDavid Sterba 	case BTRFS_IOC_GET_SUPPORTED_FEATURES:
2227d5131b65SDavid Sterba 		ret = btrfs_ioctl_get_supported_features((void __user*)arg);
2228c5868f83SDavid Sterba 		break;
22298a4b83ccSChris Mason 	}
2230dae7b665SLi Zefan 
22318a4b83ccSChris Mason 	kfree(vol);
2232f819d837SLinda Knippers 	return ret;
22338a4b83ccSChris Mason }
22348a4b83ccSChris Mason 
btrfs_freeze(struct super_block * sb)22350176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
2236ed0dab6bSYan {
2237354aa0fbSMiao Xie 	struct btrfs_trans_handle *trans;
22380b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
22390b246afaSJeff Mahoney 	struct btrfs_root *root = fs_info->tree_root;
2240354aa0fbSMiao Xie 
2241fac03c8dSDavid Sterba 	set_bit(BTRFS_FS_FROZEN, &fs_info->flags);
22429e7cc91aSWang Xiaoguang 	/*
22439e7cc91aSWang Xiaoguang 	 * We don't need a barrier here, we'll wait for any transaction that
22449e7cc91aSWang Xiaoguang 	 * could be in progress on other threads (and do delayed iputs that
22459e7cc91aSWang Xiaoguang 	 * we want to avoid on a frozen filesystem), or do the commit
22469e7cc91aSWang Xiaoguang 	 * ourselves.
22479e7cc91aSWang Xiaoguang 	 */
2248d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
2249354aa0fbSMiao Xie 	if (IS_ERR(trans)) {
2250354aa0fbSMiao Xie 		/* no transaction, don't bother */
2251354aa0fbSMiao Xie 		if (PTR_ERR(trans) == -ENOENT)
22520176260fSLinus Torvalds 			return 0;
2253354aa0fbSMiao Xie 		return PTR_ERR(trans);
2254354aa0fbSMiao Xie 	}
22553a45bb20SJeff Mahoney 	return btrfs_commit_transaction(trans);
2256ed0dab6bSYan }
2257ed0dab6bSYan 
check_dev_super(struct btrfs_device * dev)2258a05d3c91SQu Wenruo static int check_dev_super(struct btrfs_device *dev)
2259a05d3c91SQu Wenruo {
2260a05d3c91SQu Wenruo 	struct btrfs_fs_info *fs_info = dev->fs_info;
2261a05d3c91SQu Wenruo 	struct btrfs_super_block *sb;
22623d17adeaSQu Wenruo 	u16 csum_type;
2263a05d3c91SQu Wenruo 	int ret = 0;
2264a05d3c91SQu Wenruo 
2265a05d3c91SQu Wenruo 	/* This should be called with fs still frozen. */
2266a05d3c91SQu Wenruo 	ASSERT(test_bit(BTRFS_FS_FROZEN, &fs_info->flags));
2267a05d3c91SQu Wenruo 
2268a05d3c91SQu Wenruo 	/* Missing dev, no need to check. */
2269a05d3c91SQu Wenruo 	if (!dev->bdev)
2270a05d3c91SQu Wenruo 		return 0;
2271a05d3c91SQu Wenruo 
2272a05d3c91SQu Wenruo 	/* Only need to check the primary super block. */
2273a05d3c91SQu Wenruo 	sb = btrfs_read_dev_one_super(dev->bdev, 0, true);
2274a05d3c91SQu Wenruo 	if (IS_ERR(sb))
2275a05d3c91SQu Wenruo 		return PTR_ERR(sb);
2276a05d3c91SQu Wenruo 
22773d17adeaSQu Wenruo 	/* Verify the checksum. */
22783d17adeaSQu Wenruo 	csum_type = btrfs_super_csum_type(sb);
22793d17adeaSQu Wenruo 	if (csum_type != btrfs_super_csum_type(fs_info->super_copy)) {
22803d17adeaSQu Wenruo 		btrfs_err(fs_info, "csum type changed, has %u expect %u",
22813d17adeaSQu Wenruo 			  csum_type, btrfs_super_csum_type(fs_info->super_copy));
22823d17adeaSQu Wenruo 		ret = -EUCLEAN;
22833d17adeaSQu Wenruo 		goto out;
22843d17adeaSQu Wenruo 	}
22853d17adeaSQu Wenruo 
22863d17adeaSQu Wenruo 	if (btrfs_check_super_csum(fs_info, sb)) {
22873d17adeaSQu Wenruo 		btrfs_err(fs_info, "csum for on-disk super block no longer matches");
22883d17adeaSQu Wenruo 		ret = -EUCLEAN;
22893d17adeaSQu Wenruo 		goto out;
22903d17adeaSQu Wenruo 	}
22913d17adeaSQu Wenruo 
2292a05d3c91SQu Wenruo 	/* Btrfs_validate_super() includes fsid check against super->fsid. */
2293a05d3c91SQu Wenruo 	ret = btrfs_validate_super(fs_info, sb, 0);
2294a05d3c91SQu Wenruo 	if (ret < 0)
2295a05d3c91SQu Wenruo 		goto out;
2296a05d3c91SQu Wenruo 
2297a05d3c91SQu Wenruo 	if (btrfs_super_generation(sb) != fs_info->last_trans_committed) {
2298a05d3c91SQu Wenruo 		btrfs_err(fs_info, "transid mismatch, has %llu expect %llu",
2299a05d3c91SQu Wenruo 			btrfs_super_generation(sb),
2300a05d3c91SQu Wenruo 			fs_info->last_trans_committed);
2301a05d3c91SQu Wenruo 		ret = -EUCLEAN;
2302a05d3c91SQu Wenruo 		goto out;
2303a05d3c91SQu Wenruo 	}
2304a05d3c91SQu Wenruo out:
2305a05d3c91SQu Wenruo 	btrfs_release_disk_super(sb);
2306a05d3c91SQu Wenruo 	return ret;
2307a05d3c91SQu Wenruo }
2308a05d3c91SQu Wenruo 
btrfs_unfreeze(struct super_block * sb)23099e7cc91aSWang Xiaoguang static int btrfs_unfreeze(struct super_block *sb)
23109e7cc91aSWang Xiaoguang {
2311fac03c8dSDavid Sterba 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2312a05d3c91SQu Wenruo 	struct btrfs_device *device;
2313a05d3c91SQu Wenruo 	int ret = 0;
2314fac03c8dSDavid Sterba 
2315a05d3c91SQu Wenruo 	/*
2316a05d3c91SQu Wenruo 	 * Make sure the fs is not changed by accident (like hibernation then
2317a05d3c91SQu Wenruo 	 * modified by other OS).
2318a05d3c91SQu Wenruo 	 * If we found anything wrong, we mark the fs error immediately.
2319a05d3c91SQu Wenruo 	 *
2320a05d3c91SQu Wenruo 	 * And since the fs is frozen, no one can modify the fs yet, thus
2321a05d3c91SQu Wenruo 	 * we don't need to hold device_list_mutex.
2322a05d3c91SQu Wenruo 	 */
2323a05d3c91SQu Wenruo 	list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
2324a05d3c91SQu Wenruo 		ret = check_dev_super(device);
2325a05d3c91SQu Wenruo 		if (ret < 0) {
2326a05d3c91SQu Wenruo 			btrfs_handle_fs_error(fs_info, ret,
2327a05d3c91SQu Wenruo 				"super block on devid %llu got modified unexpectedly",
2328a05d3c91SQu Wenruo 				device->devid);
2329a05d3c91SQu Wenruo 			break;
2330a05d3c91SQu Wenruo 		}
2331a05d3c91SQu Wenruo 	}
2332fac03c8dSDavid Sterba 	clear_bit(BTRFS_FS_FROZEN, &fs_info->flags);
2333a05d3c91SQu Wenruo 
2334a05d3c91SQu Wenruo 	/*
2335a05d3c91SQu Wenruo 	 * We still return 0, to allow VFS layer to unfreeze the fs even the
2336a05d3c91SQu Wenruo 	 * above checks failed. Since the fs is either fine or read-only, we're
2337a05d3c91SQu Wenruo 	 * safe to continue, without causing further damage.
2338a05d3c91SQu Wenruo 	 */
23399e7cc91aSWang Xiaoguang 	return 0;
23409e7cc91aSWang Xiaoguang }
23419e7cc91aSWang Xiaoguang 
btrfs_show_devname(struct seq_file * m,struct dentry * root)23429c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
23439c5085c1SJosef Bacik {
23449c5085c1SJosef Bacik 	struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
23459c5085c1SJosef Bacik 
234688c14590SDavid Sterba 	/*
23476605fd2fSAnand Jain 	 * There should be always a valid pointer in latest_dev, it may be stale
23486605fd2fSAnand Jain 	 * for a short moment in case it's being deleted but still valid until
23496605fd2fSAnand Jain 	 * the end of RCU grace period.
235088c14590SDavid Sterba 	 */
235188c14590SDavid Sterba 	rcu_read_lock();
2352cb3e217bSQu Wenruo 	seq_escape(m, btrfs_dev_name(fs_info->fs_devices->latest_dev), " \t\n\\");
235388c14590SDavid Sterba 	rcu_read_unlock();
23546605fd2fSAnand Jain 
23559c5085c1SJosef Bacik 	return 0;
23569c5085c1SJosef Bacik }
23579c5085c1SJosef Bacik 
2358b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
235976dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
2360bd555975SAl Viro 	.evict_inode	= btrfs_evict_inode,
2361e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
2362d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
2363a9572a15SEric Paris 	.show_options	= btrfs_show_options,
23649c5085c1SJosef Bacik 	.show_devname	= btrfs_show_devname,
23652c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
23662c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
236726602cabSAl Viro 	.free_inode	= btrfs_free_inode,
23688fd17795SChris Mason 	.statfs		= btrfs_statfs,
2369c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
23700176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
23719e7cc91aSWang Xiaoguang 	.unfreeze_fs	= btrfs_unfreeze,
2372e20d96d6SChris Mason };
2373a9218f6bSChris Mason 
2374a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
2375d8620958STom Van Braeckel 	.open = btrfs_control_open,
2376a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
23771832f2d8SArnd Bergmann 	.compat_ioctl = compat_ptr_ioctl,
2378a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
23796038f373SArnd Bergmann 	.llseek = noop_llseek,
2380a9218f6bSChris Mason };
2381a9218f6bSChris Mason 
2382a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
2383578454ffSKay Sievers 	.minor		= BTRFS_MINOR,
2384a9218f6bSChris Mason 	.name		= "btrfs-control",
2385a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
2386a9218f6bSChris Mason };
2387a9218f6bSChris Mason 
2388578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
2389578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control");
2390578454ffSKay Sievers 
btrfs_interface_init(void)2391f5c29bd9SLiu Bo static int __init btrfs_interface_init(void)
2392a9218f6bSChris Mason {
2393a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
2394a9218f6bSChris Mason }
2395a9218f6bSChris Mason 
btrfs_interface_exit(void)2396e67c718bSDavid Sterba static __cold void btrfs_interface_exit(void)
2397a9218f6bSChris Mason {
2398f368ed60SGreg Kroah-Hartman 	misc_deregister(&btrfs_misc);
2399a9218f6bSChris Mason }
2400a9218f6bSChris Mason 
btrfs_print_mod_info(void)24015565b8e0SQu Wenruo static int __init btrfs_print_mod_info(void)
240285965600SDavid Sterba {
2403edf57cbfSBart Van Assche 	static const char options[] = ""
240485965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG
240585965600SDavid Sterba 			", debug=on"
240685965600SDavid Sterba #endif
240779556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT
240879556c3dSStefan Behrens 			", assert=on"
240979556c3dSStefan Behrens #endif
241085965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
241185965600SDavid Sterba 			", integrity-checker=on"
241285965600SDavid Sterba #endif
2413fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
2414fb592373SJosef Bacik 			", ref-verify=on"
2415fb592373SJosef Bacik #endif
24165b316468SNaohiro Aota #ifdef CONFIG_BLK_DEV_ZONED
24175b316468SNaohiro Aota 			", zoned=yes"
24185b316468SNaohiro Aota #else
24195b316468SNaohiro Aota 			", zoned=no"
24205b316468SNaohiro Aota #endif
2421ea3dc7d2SDavid Sterba #ifdef CONFIG_FS_VERITY
2422ea3dc7d2SDavid Sterba 			", fsverity=yes"
2423ea3dc7d2SDavid Sterba #else
2424ea3dc7d2SDavid Sterba 			", fsverity=no"
2425ea3dc7d2SDavid Sterba #endif
2426edf57cbfSBart Van Assche 			;
24276e7a367eSChristoph Hellwig 	pr_info("Btrfs loaded%s\n", options);
24285565b8e0SQu Wenruo 	return 0;
24295565b8e0SQu Wenruo }
24305565b8e0SQu Wenruo 
register_btrfs(void)24315565b8e0SQu Wenruo static int register_btrfs(void)
24325565b8e0SQu Wenruo {
24335565b8e0SQu Wenruo 	return register_filesystem(&btrfs_fs_type);
24345565b8e0SQu Wenruo }
24355565b8e0SQu Wenruo 
unregister_btrfs(void)24365565b8e0SQu Wenruo static void unregister_btrfs(void)
24375565b8e0SQu Wenruo {
24385565b8e0SQu Wenruo 	unregister_filesystem(&btrfs_fs_type);
24395565b8e0SQu Wenruo }
24405565b8e0SQu Wenruo 
24415565b8e0SQu Wenruo /* Helper structure for long init/exit functions. */
24425565b8e0SQu Wenruo struct init_sequence {
24435565b8e0SQu Wenruo 	int (*init_func)(void);
24445565b8e0SQu Wenruo 	/* Can be NULL if the init_func doesn't need cleanup. */
24455565b8e0SQu Wenruo 	void (*exit_func)(void);
24465565b8e0SQu Wenruo };
24475565b8e0SQu Wenruo 
24485565b8e0SQu Wenruo static const struct init_sequence mod_init_seq[] = {
24495565b8e0SQu Wenruo 	{
24505565b8e0SQu Wenruo 		.init_func = btrfs_props_init,
24515565b8e0SQu Wenruo 		.exit_func = NULL,
24525565b8e0SQu Wenruo 	}, {
24535565b8e0SQu Wenruo 		.init_func = btrfs_init_sysfs,
24545565b8e0SQu Wenruo 		.exit_func = btrfs_exit_sysfs,
24555565b8e0SQu Wenruo 	}, {
24565565b8e0SQu Wenruo 		.init_func = btrfs_init_compress,
24575565b8e0SQu Wenruo 		.exit_func = btrfs_exit_compress,
24585565b8e0SQu Wenruo 	}, {
24595565b8e0SQu Wenruo 		.init_func = btrfs_init_cachep,
24605565b8e0SQu Wenruo 		.exit_func = btrfs_destroy_cachep,
24615565b8e0SQu Wenruo 	}, {
24625565b8e0SQu Wenruo 		.init_func = btrfs_transaction_init,
24635565b8e0SQu Wenruo 		.exit_func = btrfs_transaction_exit,
24645565b8e0SQu Wenruo 	}, {
24655565b8e0SQu Wenruo 		.init_func = btrfs_ctree_init,
24665565b8e0SQu Wenruo 		.exit_func = btrfs_ctree_exit,
24675565b8e0SQu Wenruo 	}, {
24685565b8e0SQu Wenruo 		.init_func = btrfs_free_space_init,
24695565b8e0SQu Wenruo 		.exit_func = btrfs_free_space_exit,
24705565b8e0SQu Wenruo 	}, {
24715565b8e0SQu Wenruo 		.init_func = extent_state_init_cachep,
24725565b8e0SQu Wenruo 		.exit_func = extent_state_free_cachep,
24735565b8e0SQu Wenruo 	}, {
24745565b8e0SQu Wenruo 		.init_func = extent_buffer_init_cachep,
24755565b8e0SQu Wenruo 		.exit_func = extent_buffer_free_cachep,
24765565b8e0SQu Wenruo 	}, {
24775565b8e0SQu Wenruo 		.init_func = btrfs_bioset_init,
24785565b8e0SQu Wenruo 		.exit_func = btrfs_bioset_exit,
24795565b8e0SQu Wenruo 	}, {
24805565b8e0SQu Wenruo 		.init_func = extent_map_init,
24815565b8e0SQu Wenruo 		.exit_func = extent_map_exit,
24825565b8e0SQu Wenruo 	}, {
24835565b8e0SQu Wenruo 		.init_func = ordered_data_init,
24845565b8e0SQu Wenruo 		.exit_func = ordered_data_exit,
24855565b8e0SQu Wenruo 	}, {
24865565b8e0SQu Wenruo 		.init_func = btrfs_delayed_inode_init,
24875565b8e0SQu Wenruo 		.exit_func = btrfs_delayed_inode_exit,
24885565b8e0SQu Wenruo 	}, {
24895565b8e0SQu Wenruo 		.init_func = btrfs_auto_defrag_init,
24905565b8e0SQu Wenruo 		.exit_func = btrfs_auto_defrag_exit,
24915565b8e0SQu Wenruo 	}, {
24925565b8e0SQu Wenruo 		.init_func = btrfs_delayed_ref_init,
24935565b8e0SQu Wenruo 		.exit_func = btrfs_delayed_ref_exit,
24945565b8e0SQu Wenruo 	}, {
24955565b8e0SQu Wenruo 		.init_func = btrfs_prelim_ref_init,
24965565b8e0SQu Wenruo 		.exit_func = btrfs_prelim_ref_exit,
24975565b8e0SQu Wenruo 	}, {
24985565b8e0SQu Wenruo 		.init_func = btrfs_interface_init,
24995565b8e0SQu Wenruo 		.exit_func = btrfs_interface_exit,
25005565b8e0SQu Wenruo 	}, {
25015565b8e0SQu Wenruo 		.init_func = btrfs_print_mod_info,
25025565b8e0SQu Wenruo 		.exit_func = NULL,
25035565b8e0SQu Wenruo 	}, {
25045565b8e0SQu Wenruo 		.init_func = btrfs_run_sanity_tests,
25055565b8e0SQu Wenruo 		.exit_func = NULL,
25065565b8e0SQu Wenruo 	}, {
25075565b8e0SQu Wenruo 		.init_func = register_btrfs,
25085565b8e0SQu Wenruo 		.exit_func = unregister_btrfs,
25095565b8e0SQu Wenruo 	}
25105565b8e0SQu Wenruo };
25115565b8e0SQu Wenruo 
25125565b8e0SQu Wenruo static bool mod_init_result[ARRAY_SIZE(mod_init_seq)];
25135565b8e0SQu Wenruo 
btrfs_exit_btrfs_fs(void)251482c0efd3SAnand Jain static __always_inline void btrfs_exit_btrfs_fs(void)
25155565b8e0SQu Wenruo {
25165565b8e0SQu Wenruo 	int i;
25175565b8e0SQu Wenruo 
25185565b8e0SQu Wenruo 	for (i = ARRAY_SIZE(mod_init_seq) - 1; i >= 0; i--) {
25195565b8e0SQu Wenruo 		if (!mod_init_result[i])
25205565b8e0SQu Wenruo 			continue;
25215565b8e0SQu Wenruo 		if (mod_init_seq[i].exit_func)
25225565b8e0SQu Wenruo 			mod_init_seq[i].exit_func();
25235565b8e0SQu Wenruo 		mod_init_result[i] = false;
25245565b8e0SQu Wenruo 	}
252585965600SDavid Sterba }
252685965600SDavid Sterba 
exit_btrfs_fs(void)252782c0efd3SAnand Jain static void __exit exit_btrfs_fs(void)
252882c0efd3SAnand Jain {
252982c0efd3SAnand Jain 	btrfs_exit_btrfs_fs();
2530c68f7290SFilipe Manana 	btrfs_cleanup_fs_uuids();
253182c0efd3SAnand Jain }
253282c0efd3SAnand Jain 
init_btrfs_fs(void)25332e635a27SChris Mason static int __init init_btrfs_fs(void)
25342e635a27SChris Mason {
25355565b8e0SQu Wenruo 	int ret;
25365565b8e0SQu Wenruo 	int i;
253758176a96SJosef Bacik 
25385565b8e0SQu Wenruo 	for (i = 0; i < ARRAY_SIZE(mod_init_seq); i++) {
25395565b8e0SQu Wenruo 		ASSERT(!mod_init_result[i]);
25405565b8e0SQu Wenruo 		ret = mod_init_seq[i].init_func();
254182c0efd3SAnand Jain 		if (ret < 0) {
254282c0efd3SAnand Jain 			btrfs_exit_btrfs_fs();
254382c0efd3SAnand Jain 			return ret;
254482c0efd3SAnand Jain 		}
25455565b8e0SQu Wenruo 		mod_init_result[i] = true;
25465565b8e0SQu Wenruo 	}
25472f4cbe64SWyatt Banks 	return 0;
25482e635a27SChris Mason }
25492e635a27SChris Mason 
255060efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs);
25512e635a27SChris Mason module_exit(exit_btrfs_fs)
25522e635a27SChris Mason 
25532e635a27SChris Mason MODULE_LICENSE("GPL");
2554d5178578SJohannes Thumshirn MODULE_SOFTDEP("pre: crc32c");
25553951e7f0SJohannes Thumshirn MODULE_SOFTDEP("pre: xxhash64");
25563831bf00SJohannes Thumshirn MODULE_SOFTDEP("pre: sha256");
2557352ae07bSDavid Sterba MODULE_SOFTDEP("pre: blake2b-256");
2558