xref: /openbmc/linux/fs/btrfs/super.c (revision 8cd29088)
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>
2690a887c9SDan Magenheimer #include <linux/cleancache.h>
2722c44fe6SJosef Bacik #include <linux/ratelimit.h>
289678c543SNikolay Borisov #include <linux/crc32c.h>
2955e301fdSFilipe Brandenburger #include <linux/btrfs.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"
388a4b83ccSChris Mason #include "volumes.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"
512e635a27SChris Mason 
52d3982100SMark Fasheh #include "qgroup.h"
531abe9b8aSliubo #define CREATE_TRACE_POINTS
541abe9b8aSliubo #include <trace/events/btrfs.h>
551abe9b8aSliubo 
56b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops;
5772fa39f5SMisono, Tomohiro 
5872fa39f5SMisono, Tomohiro /*
5972fa39f5SMisono, Tomohiro  * Types for mounting the default subvolume and a subvolume explicitly
6072fa39f5SMisono, Tomohiro  * requested by subvol=/path. That way the callchain is straightforward and we
6172fa39f5SMisono, Tomohiro  * don't have to play tricks with the mount options and recursive calls to
6272fa39f5SMisono, Tomohiro  * btrfs_mount.
63312c89fbSMisono, Tomohiro  *
64312c89fbSMisono, Tomohiro  * The new btrfs_root_fs_type also servers as a tag for the bdev_holder.
6572fa39f5SMisono, Tomohiro  */
66830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type;
6772fa39f5SMisono, Tomohiro static struct file_system_type btrfs_root_fs_type;
68e20d96d6SChris Mason 
690723a047SHarald Hoyer static int btrfs_remount(struct super_block *sb, int *flags, char *data);
700723a047SHarald Hoyer 
7159131393SJosef Bacik /*
7259131393SJosef Bacik  * Generally the error codes correspond to their respective errors, but there
7359131393SJosef Bacik  * are a few special cases.
7459131393SJosef Bacik  *
7559131393SJosef Bacik  * EUCLEAN: Any sort of corruption that we encounter.  The tree-checker for
7659131393SJosef Bacik  *          instance will return EUCLEAN if any of the blocks are corrupted in
7759131393SJosef Bacik  *          a way that is problematic.  We want to reserve EUCLEAN for these
7859131393SJosef Bacik  *          sort of corruptions.
7959131393SJosef Bacik  *
8059131393SJosef Bacik  * EROFS: If we check BTRFS_FS_STATE_ERROR and fail out with a return error, we
8159131393SJosef Bacik  *        need to use EROFS for this case.  We will have no idea of the
8259131393SJosef Bacik  *        original failure, that will have been reported at the time we tripped
8359131393SJosef Bacik  *        over the error.  Each subsequent error that doesn't have any context
8459131393SJosef Bacik  *        of the original error should use EROFS when handling BTRFS_FS_STATE_ERROR.
8559131393SJosef Bacik  */
864143cb8bSDavid Sterba const char * __attribute_const__ btrfs_decode_error(int errno)
87acce952bSliubo {
8808748810SDavid Sterba 	char *errstr = "unknown";
89acce952bSliubo 
90acce952bSliubo 	switch (errno) {
91d54f8144SDavid Sterba 	case -ENOENT:		/* -2 */
92d54f8144SDavid Sterba 		errstr = "No such entry";
93d54f8144SDavid Sterba 		break;
94d54f8144SDavid Sterba 	case -EIO:		/* -5 */
95acce952bSliubo 		errstr = "IO failure";
96acce952bSliubo 		break;
97d54f8144SDavid Sterba 	case -ENOMEM:		/* -12*/
98acce952bSliubo 		errstr = "Out of memory";
99acce952bSliubo 		break;
100d54f8144SDavid Sterba 	case -EEXIST:		/* -17 */
1018c342930SJeff Mahoney 		errstr = "Object already exists";
1028c342930SJeff Mahoney 		break;
103d54f8144SDavid Sterba 	case -ENOSPC:		/* -28 */
10494ef7280SDavid Sterba 		errstr = "No space left";
10594ef7280SDavid Sterba 		break;
106d54f8144SDavid Sterba 	case -EROFS:		/* -30 */
107d54f8144SDavid Sterba 		errstr = "Readonly filesystem";
10894ef7280SDavid Sterba 		break;
109fb8521caSDavid Sterba 	case -EOPNOTSUPP:	/* -95 */
110fb8521caSDavid Sterba 		errstr = "Operation not supported";
111fb8521caSDavid Sterba 		break;
112fb8521caSDavid Sterba 	case -EUCLEAN:		/* -117 */
113fb8521caSDavid Sterba 		errstr = "Filesystem corrupted";
114fb8521caSDavid Sterba 		break;
115fb8521caSDavid Sterba 	case -EDQUOT:		/* -122 */
116fb8521caSDavid Sterba 		errstr = "Quota exceeded";
117fb8521caSDavid Sterba 		break;
118acce952bSliubo 	}
119acce952bSliubo 
120acce952bSliubo 	return errstr;
121acce952bSliubo }
122acce952bSliubo 
123acce952bSliubo /*
12434d97007SAnand Jain  * __btrfs_handle_fs_error decodes expected errors from the caller and
12552042d8eSAndrea Gelmini  * invokes the appropriate error response.
126acce952bSliubo  */
127c0d19e2bSDavid Sterba __cold
12834d97007SAnand Jain void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
1294da35113SJeff Mahoney 		       unsigned int line, int errno, const char *fmt, ...)
130acce952bSliubo {
131acce952bSliubo 	struct super_block *sb = fs_info->sb;
13257d816a1SAnand Jain #ifdef CONFIG_PRINTK
133acce952bSliubo 	const char *errstr;
13457d816a1SAnand Jain #endif
135acce952bSliubo 
136acce952bSliubo 	/*
137acce952bSliubo 	 * Special case: if the error is EROFS, and we're already
1381751e8a6SLinus Torvalds 	 * under SB_RDONLY, then it is safe here.
139acce952bSliubo 	 */
140bc98a42cSDavid Howells 	if (errno == -EROFS && sb_rdonly(sb))
141acce952bSliubo   		return;
142acce952bSliubo 
14357d816a1SAnand Jain #ifdef CONFIG_PRINTK
14408748810SDavid Sterba 	errstr = btrfs_decode_error(errno);
1454da35113SJeff Mahoney 	if (fmt) {
14637252a66SEric Sandeen 		struct va_format vaf;
14737252a66SEric Sandeen 		va_list args;
14837252a66SEric Sandeen 
14937252a66SEric Sandeen 		va_start(args, fmt);
15037252a66SEric Sandeen 		vaf.fmt = fmt;
15137252a66SEric Sandeen 		vaf.va = &args;
1524da35113SJeff Mahoney 
15362e85577SJeff Mahoney 		pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s (%pV)\n",
15408748810SDavid Sterba 			sb->s_id, function, line, errno, errstr, &vaf);
15537252a66SEric Sandeen 		va_end(args);
1564da35113SJeff Mahoney 	} else {
15762e85577SJeff Mahoney 		pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s\n",
15808748810SDavid Sterba 			sb->s_id, function, line, errno, errstr);
1594da35113SJeff Mahoney 	}
16057d816a1SAnand Jain #endif
161acce952bSliubo 
1620713d90cSAnand Jain 	/*
1630713d90cSAnand Jain 	 * Today we only save the error info to memory.  Long term we'll
1640713d90cSAnand Jain 	 * also send it down to the disk
1650713d90cSAnand Jain 	 */
1660713d90cSAnand Jain 	set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
1670713d90cSAnand Jain 
1684da35113SJeff Mahoney 	/* Don't go through full error handling during mount */
169922ea899SAnand Jain 	if (!(sb->s_flags & SB_BORN))
170922ea899SAnand Jain 		return;
171922ea899SAnand Jain 
172922ea899SAnand Jain 	if (sb_rdonly(sb))
173922ea899SAnand Jain 		return;
174922ea899SAnand Jain 
175b0643e59SDennis Zhou 	btrfs_discard_stop(fs_info);
176b0643e59SDennis Zhou 
177922ea899SAnand Jain 	/* btrfs handle error by forcing the filesystem readonly */
178922ea899SAnand Jain 	sb->s_flags |= SB_RDONLY;
179922ea899SAnand Jain 	btrfs_info(fs_info, "forced readonly");
180922ea899SAnand Jain 	/*
181922ea899SAnand Jain 	 * Note that a running device replace operation is not canceled here
182922ea899SAnand Jain 	 * although there is no way to update the progress. It would add the
183922ea899SAnand Jain 	 * risk of a deadlock, therefore the canceling is omitted. The only
184922ea899SAnand Jain 	 * penalty is that some I/O remains active until the procedure
18552042d8eSAndrea Gelmini 	 * completes. The next time when the filesystem is mounted writable
186922ea899SAnand Jain 	 * again, the device replace operation continues.
187922ea899SAnand Jain 	 */
188acce952bSliubo }
1894da35113SJeff Mahoney 
19057d816a1SAnand Jain #ifdef CONFIG_PRINTK
191533574c6SJoe Perches static const char * const logtypes[] = {
1924da35113SJeff Mahoney 	"emergency",
1934da35113SJeff Mahoney 	"alert",
1944da35113SJeff Mahoney 	"critical",
1954da35113SJeff Mahoney 	"error",
1964da35113SJeff Mahoney 	"warning",
1974da35113SJeff Mahoney 	"notice",
1984da35113SJeff Mahoney 	"info",
1994da35113SJeff Mahoney 	"debug",
2004da35113SJeff Mahoney };
2014da35113SJeff Mahoney 
20235f4e5e6SNikolay Borisov 
20335f4e5e6SNikolay Borisov /*
20435f4e5e6SNikolay Borisov  * Use one ratelimit state per log level so that a flood of less important
20535f4e5e6SNikolay Borisov  * messages doesn't cause more important ones to be dropped.
20635f4e5e6SNikolay Borisov  */
20735f4e5e6SNikolay Borisov static struct ratelimit_state printk_limits[] = {
20835f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[0], DEFAULT_RATELIMIT_INTERVAL, 100),
20935f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[1], DEFAULT_RATELIMIT_INTERVAL, 100),
21035f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[2], DEFAULT_RATELIMIT_INTERVAL, 100),
21135f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[3], DEFAULT_RATELIMIT_INTERVAL, 100),
21235f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[4], DEFAULT_RATELIMIT_INTERVAL, 100),
21335f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[5], DEFAULT_RATELIMIT_INTERVAL, 100),
21435f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[6], DEFAULT_RATELIMIT_INTERVAL, 100),
21535f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[7], DEFAULT_RATELIMIT_INTERVAL, 100),
21635f4e5e6SNikolay Borisov };
21735f4e5e6SNikolay Borisov 
218b105e927SDavid Sterba void __cold btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
2194da35113SJeff Mahoney {
22040f7828bSPetr Mladek 	char lvl[PRINTK_MAX_SINGLE_HEADER_LEN + 1] = "\0";
2214da35113SJeff Mahoney 	struct va_format vaf;
2224da35113SJeff Mahoney 	va_list args;
223533574c6SJoe Perches 	int kern_level;
22440f7828bSPetr Mladek 	const char *type = logtypes[4];
22540f7828bSPetr Mladek 	struct ratelimit_state *ratelimit = &printk_limits[4];
2264da35113SJeff Mahoney 
2274da35113SJeff Mahoney 	va_start(args, fmt);
2284da35113SJeff Mahoney 
229262c5e86SPetr Mladek 	while ((kern_level = printk_get_level(fmt)) != 0) {
230533574c6SJoe Perches 		size_t size = printk_skip_level(fmt) - fmt;
231262c5e86SPetr Mladek 
232262c5e86SPetr Mladek 		if (kern_level >= '0' && kern_level <= '7') {
233533574c6SJoe Perches 			memcpy(lvl, fmt,  size);
234533574c6SJoe Perches 			lvl[size] = '\0';
235533574c6SJoe Perches 			type = logtypes[kern_level - '0'];
23635f4e5e6SNikolay Borisov 			ratelimit = &printk_limits[kern_level - '0'];
237262c5e86SPetr Mladek 		}
238262c5e86SPetr Mladek 		fmt += size;
239262c5e86SPetr Mladek 	}
240262c5e86SPetr Mladek 
2414da35113SJeff Mahoney 	vaf.fmt = fmt;
2424da35113SJeff Mahoney 	vaf.va = &args;
243533574c6SJoe Perches 
244a0f6d924SDavid Sterba 	if (__ratelimit(ratelimit)) {
245a0f6d924SDavid Sterba 		if (fs_info)
2463993b112SColin Ian King 			printk("%sBTRFS %s (device %s): %pV\n", lvl, type,
247a0f6d924SDavid Sterba 				fs_info->sb->s_id, &vaf);
248a0f6d924SDavid Sterba 		else
249a0f6d924SDavid Sterba 			printk("%sBTRFS %s: %pV\n", lvl, type, &vaf);
250a0f6d924SDavid Sterba 	}
251533574c6SJoe Perches 
252533574c6SJoe Perches 	va_end(args);
2534da35113SJeff Mahoney }
254533574c6SJoe Perches #endif
255533574c6SJoe Perches 
2568c342930SJeff Mahoney /*
25749b25e05SJeff Mahoney  * We only mark the transaction aborted and then set the file system read-only.
25849b25e05SJeff Mahoney  * This will prevent new transactions from starting or trying to join this
25949b25e05SJeff Mahoney  * one.
26049b25e05SJeff Mahoney  *
26149b25e05SJeff Mahoney  * This means that error recovery at the call site is limited to freeing
26249b25e05SJeff Mahoney  * any local memory allocations and passing the error code up without
26349b25e05SJeff Mahoney  * further cleanup. The transaction should complete as it normally would
26449b25e05SJeff Mahoney  * in the call path but will return -EIO.
26549b25e05SJeff Mahoney  *
26649b25e05SJeff Mahoney  * We'll complete the cleanup in btrfs_end_transaction and
26749b25e05SJeff Mahoney  * btrfs_commit_transaction.
26849b25e05SJeff Mahoney  */
269c0d19e2bSDavid Sterba __cold
27049b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
27166642832SJeff Mahoney 			       const char *function,
27249b25e05SJeff Mahoney 			       unsigned int line, int errno)
27349b25e05SJeff Mahoney {
27466642832SJeff Mahoney 	struct btrfs_fs_info *fs_info = trans->fs_info;
27566642832SJeff Mahoney 
276bf31f87fSDavid Sterba 	WRITE_ONCE(trans->aborted, errno);
27749b25e05SJeff Mahoney 	/* Nothing used. The other threads that have joined this
27849b25e05SJeff Mahoney 	 * transaction may be able to continue. */
27964c12921SJeff Mahoney 	if (!trans->dirty && list_empty(&trans->new_bgs)) {
28069ce977aSMiao Xie 		const char *errstr;
28169ce977aSMiao Xie 
28208748810SDavid Sterba 		errstr = btrfs_decode_error(errno);
28366642832SJeff Mahoney 		btrfs_warn(fs_info,
284c2cf52ebSSimon Kirby 		           "%s:%d: Aborting unused transaction(%s).",
28569ce977aSMiao Xie 		           function, line, errstr);
28649b25e05SJeff Mahoney 		return;
28749b25e05SJeff Mahoney 	}
28820c7bcecSSeraphime Kirkovski 	WRITE_ONCE(trans->transaction->aborted, errno);
289501407aaSJosef Bacik 	/* Wake up anybody who may be waiting on this transaction */
29066642832SJeff Mahoney 	wake_up(&fs_info->transaction_wait);
29166642832SJeff Mahoney 	wake_up(&fs_info->transaction_blocked_wait);
29266642832SJeff Mahoney 	__btrfs_handle_fs_error(fs_info, function, line, errno, NULL);
29349b25e05SJeff Mahoney }
29449b25e05SJeff Mahoney /*
2958c342930SJeff Mahoney  * __btrfs_panic decodes unexpected, fatal errors from the caller,
2968c342930SJeff Mahoney  * issues an alert, and either panics or BUGs, depending on mount options.
2978c342930SJeff Mahoney  */
298c0d19e2bSDavid Sterba __cold
2998c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
3008c342930SJeff Mahoney 		   unsigned int line, int errno, const char *fmt, ...)
3018c342930SJeff Mahoney {
3028c342930SJeff Mahoney 	char *s_id = "<unknown>";
3038c342930SJeff Mahoney 	const char *errstr;
3048c342930SJeff Mahoney 	struct va_format vaf = { .fmt = fmt };
3058c342930SJeff Mahoney 	va_list args;
3068c342930SJeff Mahoney 
3078c342930SJeff Mahoney 	if (fs_info)
3088c342930SJeff Mahoney 		s_id = fs_info->sb->s_id;
3098c342930SJeff Mahoney 
3108c342930SJeff Mahoney 	va_start(args, fmt);
3118c342930SJeff Mahoney 	vaf.va = &args;
3128c342930SJeff Mahoney 
31308748810SDavid Sterba 	errstr = btrfs_decode_error(errno);
314d8953d69SSatoru Takeuchi 	if (fs_info && (btrfs_test_opt(fs_info, PANIC_ON_FATAL_ERROR)))
31508748810SDavid Sterba 		panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
31608748810SDavid Sterba 			s_id, function, line, &vaf, errno, errstr);
3178c342930SJeff Mahoney 
318efe120a0SFrank Holton 	btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)",
319efe120a0SFrank Holton 		   function, line, &vaf, errno, errstr);
3208c342930SJeff Mahoney 	va_end(args);
3218c342930SJeff Mahoney 	/* Caller calls BUG() */
3228c342930SJeff Mahoney }
323acce952bSliubo 
324e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb)
325e20d96d6SChris Mason {
3266bccf3abSJeff Mahoney 	close_ctree(btrfs_sb(sb));
327e20d96d6SChris Mason }
3282e635a27SChris Mason 
32995e05289SChris Mason enum {
330416a7202SDavid Sterba 	Opt_acl, Opt_noacl,
331416a7202SDavid Sterba 	Opt_clear_cache,
332416a7202SDavid Sterba 	Opt_commit_interval,
333416a7202SDavid Sterba 	Opt_compress,
334416a7202SDavid Sterba 	Opt_compress_force,
335416a7202SDavid Sterba 	Opt_compress_force_type,
336416a7202SDavid Sterba 	Opt_compress_type,
337416a7202SDavid Sterba 	Opt_degraded,
338416a7202SDavid Sterba 	Opt_device,
339416a7202SDavid Sterba 	Opt_fatal_errors,
340416a7202SDavid Sterba 	Opt_flushoncommit, Opt_noflushoncommit,
341416a7202SDavid Sterba 	Opt_max_inline,
342416a7202SDavid Sterba 	Opt_barrier, Opt_nobarrier,
343416a7202SDavid Sterba 	Opt_datacow, Opt_nodatacow,
344416a7202SDavid Sterba 	Opt_datasum, Opt_nodatasum,
345416a7202SDavid Sterba 	Opt_defrag, Opt_nodefrag,
346416a7202SDavid Sterba 	Opt_discard, Opt_nodiscard,
347b0643e59SDennis Zhou 	Opt_discard_mode,
348416a7202SDavid Sterba 	Opt_norecovery,
349416a7202SDavid Sterba 	Opt_ratio,
350416a7202SDavid Sterba 	Opt_rescan_uuid_tree,
351416a7202SDavid Sterba 	Opt_skip_balance,
352416a7202SDavid Sterba 	Opt_space_cache, Opt_no_space_cache,
353416a7202SDavid Sterba 	Opt_space_cache_version,
354416a7202SDavid Sterba 	Opt_ssd, Opt_nossd,
355416a7202SDavid Sterba 	Opt_ssd_spread, Opt_nossd_spread,
356416a7202SDavid Sterba 	Opt_subvol,
35737becec9SOmar Sandoval 	Opt_subvol_empty,
358416a7202SDavid Sterba 	Opt_subvolid,
359416a7202SDavid Sterba 	Opt_thread_pool,
360416a7202SDavid Sterba 	Opt_treelog, Opt_notreelog,
361416a7202SDavid Sterba 	Opt_user_subvol_rm_allowed,
362416a7202SDavid Sterba 
36374ef0018SQu Wenruo 	/* Rescue options */
36474ef0018SQu Wenruo 	Opt_rescue,
36574ef0018SQu Wenruo 	Opt_usebackuproot,
36674ef0018SQu Wenruo 	Opt_nologreplay,
36742437a63SJosef Bacik 	Opt_ignorebadroots,
368882dbe0cSJosef Bacik 	Opt_ignoredatacsums,
3699037d3cbSJosef Bacik 	Opt_rescue_all,
37074ef0018SQu Wenruo 
371416a7202SDavid Sterba 	/* Deprecated options */
372416a7202SDavid Sterba 	Opt_recovery,
3735297199aSNikolay Borisov 	Opt_inode_cache, Opt_noinode_cache,
374416a7202SDavid Sterba 
375416a7202SDavid Sterba 	/* Debugging options */
376416a7202SDavid Sterba 	Opt_check_integrity,
37770f6d82eSOmar Sandoval 	Opt_check_integrity_including_extent_data,
378416a7202SDavid Sterba 	Opt_check_integrity_print_mask,
379416a7202SDavid Sterba 	Opt_enospc_debug, Opt_noenospc_debug,
380d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
381d0bd4560SJosef Bacik 	Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
382d0bd4560SJosef Bacik #endif
383fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
384fb592373SJosef Bacik 	Opt_ref_verify,
385fb592373SJosef Bacik #endif
3869555c6c1SIlya Dryomov 	Opt_err,
38795e05289SChris Mason };
38895e05289SChris Mason 
3894d4ab6d6SDavid Sterba static const match_table_t tokens = {
390416a7202SDavid Sterba 	{Opt_acl, "acl"},
391416a7202SDavid Sterba 	{Opt_noacl, "noacl"},
392416a7202SDavid Sterba 	{Opt_clear_cache, "clear_cache"},
393416a7202SDavid Sterba 	{Opt_commit_interval, "commit=%u"},
394c8b97818SChris Mason 	{Opt_compress, "compress"},
395261507a0SLi Zefan 	{Opt_compress_type, "compress=%s"},
396a555f810SChris Mason 	{Opt_compress_force, "compress-force"},
397261507a0SLi Zefan 	{Opt_compress_force_type, "compress-force=%s"},
398416a7202SDavid Sterba 	{Opt_degraded, "degraded"},
399416a7202SDavid Sterba 	{Opt_device, "device=%s"},
400416a7202SDavid Sterba 	{Opt_fatal_errors, "fatal_errors=%s"},
401dccae999SSage Weil 	{Opt_flushoncommit, "flushoncommit"},
4022c9ee856SQu Wenruo 	{Opt_noflushoncommit, "noflushoncommit"},
4034b9465cbSChris Mason 	{Opt_inode_cache, "inode_cache"},
4043818aea2SQu Wenruo 	{Opt_noinode_cache, "noinode_cache"},
405416a7202SDavid Sterba 	{Opt_max_inline, "max_inline=%s"},
406416a7202SDavid Sterba 	{Opt_barrier, "barrier"},
407416a7202SDavid Sterba 	{Opt_nobarrier, "nobarrier"},
408416a7202SDavid Sterba 	{Opt_datacow, "datacow"},
409416a7202SDavid Sterba 	{Opt_nodatacow, "nodatacow"},
410416a7202SDavid Sterba 	{Opt_datasum, "datasum"},
411416a7202SDavid Sterba 	{Opt_nodatasum, "nodatasum"},
412416a7202SDavid Sterba 	{Opt_defrag, "autodefrag"},
413416a7202SDavid Sterba 	{Opt_nodefrag, "noautodefrag"},
414416a7202SDavid Sterba 	{Opt_discard, "discard"},
415b0643e59SDennis Zhou 	{Opt_discard_mode, "discard=%s"},
416416a7202SDavid Sterba 	{Opt_nodiscard, "nodiscard"},
417416a7202SDavid Sterba 	{Opt_norecovery, "norecovery"},
418416a7202SDavid Sterba 	{Opt_ratio, "metadata_ratio=%u"},
419416a7202SDavid Sterba 	{Opt_rescan_uuid_tree, "rescan_uuid_tree"},
4209555c6c1SIlya Dryomov 	{Opt_skip_balance, "skip_balance"},
421416a7202SDavid Sterba 	{Opt_space_cache, "space_cache"},
422416a7202SDavid Sterba 	{Opt_no_space_cache, "nospace_cache"},
423416a7202SDavid Sterba 	{Opt_space_cache_version, "space_cache=%s"},
424416a7202SDavid Sterba 	{Opt_ssd, "ssd"},
425416a7202SDavid Sterba 	{Opt_nossd, "nossd"},
426416a7202SDavid Sterba 	{Opt_ssd_spread, "ssd_spread"},
427416a7202SDavid Sterba 	{Opt_nossd_spread, "nossd_spread"},
428416a7202SDavid Sterba 	{Opt_subvol, "subvol=%s"},
42937becec9SOmar Sandoval 	{Opt_subvol_empty, "subvol="},
430416a7202SDavid Sterba 	{Opt_subvolid, "subvolid=%s"},
431416a7202SDavid Sterba 	{Opt_thread_pool, "thread_pool=%u"},
432416a7202SDavid Sterba 	{Opt_treelog, "treelog"},
433416a7202SDavid Sterba 	{Opt_notreelog, "notreelog"},
434416a7202SDavid Sterba 	{Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
435416a7202SDavid Sterba 
43674ef0018SQu Wenruo 	/* Rescue options */
43774ef0018SQu Wenruo 	{Opt_rescue, "rescue=%s"},
43874ef0018SQu Wenruo 	/* Deprecated, with alias rescue=nologreplay */
43974ef0018SQu Wenruo 	{Opt_nologreplay, "nologreplay"},
44074ef0018SQu Wenruo 	/* Deprecated, with alias rescue=usebackuproot */
44174ef0018SQu Wenruo 	{Opt_usebackuproot, "usebackuproot"},
44274ef0018SQu Wenruo 
443416a7202SDavid Sterba 	/* Deprecated options */
444416a7202SDavid Sterba 	{Opt_recovery, "recovery"},
445416a7202SDavid Sterba 
446416a7202SDavid Sterba 	/* Debugging options */
44721adbd5cSStefan Behrens 	{Opt_check_integrity, "check_int"},
44821adbd5cSStefan Behrens 	{Opt_check_integrity_including_extent_data, "check_int_data"},
44902453bdeSAnand Jain 	{Opt_check_integrity_print_mask, "check_int_print_mask=%u"},
450416a7202SDavid Sterba 	{Opt_enospc_debug, "enospc_debug"},
451416a7202SDavid Sterba 	{Opt_noenospc_debug, "noenospc_debug"},
452d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
453d0bd4560SJosef Bacik 	{Opt_fragment_data, "fragment=data"},
454d0bd4560SJosef Bacik 	{Opt_fragment_metadata, "fragment=metadata"},
455d0bd4560SJosef Bacik 	{Opt_fragment_all, "fragment=all"},
456d0bd4560SJosef Bacik #endif
457fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
458fb592373SJosef Bacik 	{Opt_ref_verify, "ref_verify"},
459fb592373SJosef Bacik #endif
46033268eafSJosef Bacik 	{Opt_err, NULL},
46195e05289SChris Mason };
46295e05289SChris Mason 
46374ef0018SQu Wenruo static const match_table_t rescue_tokens = {
46474ef0018SQu Wenruo 	{Opt_usebackuproot, "usebackuproot"},
46574ef0018SQu Wenruo 	{Opt_nologreplay, "nologreplay"},
46642437a63SJosef Bacik 	{Opt_ignorebadroots, "ignorebadroots"},
46742437a63SJosef Bacik 	{Opt_ignorebadroots, "ibadroots"},
468882dbe0cSJosef Bacik 	{Opt_ignoredatacsums, "ignoredatacsums"},
469882dbe0cSJosef Bacik 	{Opt_ignoredatacsums, "idatacsums"},
4709037d3cbSJosef Bacik 	{Opt_rescue_all, "all"},
47174ef0018SQu Wenruo 	{Opt_err, NULL},
47274ef0018SQu Wenruo };
47374ef0018SQu Wenruo 
474d70bf748SJosef Bacik static bool check_ro_option(struct btrfs_fs_info *fs_info, unsigned long opt,
475d70bf748SJosef Bacik 			    const char *opt_name)
476d70bf748SJosef Bacik {
477d70bf748SJosef Bacik 	if (fs_info->mount_opt & opt) {
478d70bf748SJosef Bacik 		btrfs_err(fs_info, "%s must be used with ro mount option",
479d70bf748SJosef Bacik 			  opt_name);
480d70bf748SJosef Bacik 		return true;
481d70bf748SJosef Bacik 	}
482d70bf748SJosef Bacik 	return false;
483d70bf748SJosef Bacik }
484d70bf748SJosef Bacik 
48574ef0018SQu Wenruo static int parse_rescue_options(struct btrfs_fs_info *info, const char *options)
48674ef0018SQu Wenruo {
48774ef0018SQu Wenruo 	char *opts;
48874ef0018SQu Wenruo 	char *orig;
48974ef0018SQu Wenruo 	char *p;
49074ef0018SQu Wenruo 	substring_t args[MAX_OPT_ARGS];
49174ef0018SQu Wenruo 	int ret = 0;
49274ef0018SQu Wenruo 
49374ef0018SQu Wenruo 	opts = kstrdup(options, GFP_KERNEL);
49474ef0018SQu Wenruo 	if (!opts)
49574ef0018SQu Wenruo 		return -ENOMEM;
49674ef0018SQu Wenruo 	orig = opts;
49774ef0018SQu Wenruo 
49874ef0018SQu Wenruo 	while ((p = strsep(&opts, ":")) != NULL) {
49974ef0018SQu Wenruo 		int token;
50074ef0018SQu Wenruo 
50174ef0018SQu Wenruo 		if (!*p)
50274ef0018SQu Wenruo 			continue;
50374ef0018SQu Wenruo 		token = match_token(p, rescue_tokens, args);
50474ef0018SQu Wenruo 		switch (token){
50574ef0018SQu Wenruo 		case Opt_usebackuproot:
50674ef0018SQu Wenruo 			btrfs_info(info,
50774ef0018SQu Wenruo 				   "trying to use backup root at mount time");
50874ef0018SQu Wenruo 			btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
50974ef0018SQu Wenruo 			break;
51074ef0018SQu Wenruo 		case Opt_nologreplay:
51174ef0018SQu Wenruo 			btrfs_set_and_info(info, NOLOGREPLAY,
51274ef0018SQu Wenruo 					   "disabling log replay at mount time");
51374ef0018SQu Wenruo 			break;
51442437a63SJosef Bacik 		case Opt_ignorebadroots:
51542437a63SJosef Bacik 			btrfs_set_and_info(info, IGNOREBADROOTS,
51642437a63SJosef Bacik 					   "ignoring bad roots");
51742437a63SJosef Bacik 			break;
518882dbe0cSJosef Bacik 		case Opt_ignoredatacsums:
519882dbe0cSJosef Bacik 			btrfs_set_and_info(info, IGNOREDATACSUMS,
520882dbe0cSJosef Bacik 					   "ignoring data csums");
521882dbe0cSJosef Bacik 			break;
5229037d3cbSJosef Bacik 		case Opt_rescue_all:
5239037d3cbSJosef Bacik 			btrfs_info(info, "enabling all of the rescue options");
5249037d3cbSJosef Bacik 			btrfs_set_and_info(info, IGNOREDATACSUMS,
5259037d3cbSJosef Bacik 					   "ignoring data csums");
5269037d3cbSJosef Bacik 			btrfs_set_and_info(info, IGNOREBADROOTS,
5279037d3cbSJosef Bacik 					   "ignoring bad roots");
5289037d3cbSJosef Bacik 			btrfs_set_and_info(info, NOLOGREPLAY,
5299037d3cbSJosef Bacik 					   "disabling log replay at mount time");
5309037d3cbSJosef Bacik 			break;
53174ef0018SQu Wenruo 		case Opt_err:
53274ef0018SQu Wenruo 			btrfs_info(info, "unrecognized rescue option '%s'", p);
53374ef0018SQu Wenruo 			ret = -EINVAL;
53474ef0018SQu Wenruo 			goto out;
53574ef0018SQu Wenruo 		default:
53674ef0018SQu Wenruo 			break;
53774ef0018SQu Wenruo 		}
53874ef0018SQu Wenruo 
53974ef0018SQu Wenruo 	}
54074ef0018SQu Wenruo out:
54174ef0018SQu Wenruo 	kfree(orig);
54274ef0018SQu Wenruo 	return ret;
54374ef0018SQu Wenruo }
54474ef0018SQu Wenruo 
545edf24abeSChristoph Hellwig /*
546edf24abeSChristoph Hellwig  * Regular mount options parser.  Everything that is needed only when
547edf24abeSChristoph Hellwig  * reading in a new superblock is parsed here.
54849b25e05SJeff Mahoney  * XXX JDM: This needs to be cleaned up for remount.
549edf24abeSChristoph Hellwig  */
5502ff7e61eSJeff Mahoney int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
55196da0919SQu Wenruo 			unsigned long new_flags)
55295e05289SChris Mason {
55395e05289SChris Mason 	substring_t args[MAX_OPT_ARGS];
554e215772cSMisono, Tomohiro 	char *p, *num;
55573bc1876SJosef Bacik 	u64 cache_gen;
5564543df7eSChris Mason 	int intarg;
557a7a3f7caSSage Weil 	int ret = 0;
558261507a0SLi Zefan 	char *compress_type;
559261507a0SLi Zefan 	bool compress_force = false;
560b7c47bbbSTsutomu Itoh 	enum btrfs_compression_type saved_compress_type;
56127942c99SDavid Sterba 	int saved_compress_level;
562b7c47bbbSTsutomu Itoh 	bool saved_compress_force;
563b7c47bbbSTsutomu Itoh 	int no_compress = 0;
564b6cda9bcSChris Mason 
5650b246afaSJeff Mahoney 	cache_gen = btrfs_super_cache_generation(info->super_copy);
5660b246afaSJeff Mahoney 	if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
56770f6d82eSOmar Sandoval 		btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE);
5685d1ab66cSNaohiro Aota 	else if (cache_gen) {
5695d1ab66cSNaohiro Aota 		if (btrfs_is_zoned(info)) {
5705d1ab66cSNaohiro Aota 			btrfs_info(info,
5715d1ab66cSNaohiro Aota 			"zoned: clearing existing space cache");
5725d1ab66cSNaohiro Aota 			btrfs_set_super_cache_generation(info->super_copy, 0);
5735d1ab66cSNaohiro Aota 		} else {
57473bc1876SJosef Bacik 			btrfs_set_opt(info->mount_opt, SPACE_CACHE);
5755d1ab66cSNaohiro Aota 		}
5765d1ab66cSNaohiro Aota 	}
57773bc1876SJosef Bacik 
57896da0919SQu Wenruo 	/*
57996da0919SQu Wenruo 	 * Even the options are empty, we still need to do extra check
58096da0919SQu Wenruo 	 * against new flags
58196da0919SQu Wenruo 	 */
58295e05289SChris Mason 	if (!options)
58396da0919SQu Wenruo 		goto check;
58495e05289SChris Mason 
58595e05289SChris Mason 	while ((p = strsep(&options, ",")) != NULL) {
58695e05289SChris Mason 		int token;
58795e05289SChris Mason 		if (!*p)
58895e05289SChris Mason 			continue;
58995e05289SChris Mason 
59095e05289SChris Mason 		token = match_token(p, tokens, args);
59195e05289SChris Mason 		switch (token) {
592dfe25020SChris Mason 		case Opt_degraded:
5930b246afaSJeff Mahoney 			btrfs_info(info, "allowing degraded mounts");
594dfe25020SChris Mason 			btrfs_set_opt(info->mount_opt, DEGRADED);
595dfe25020SChris Mason 			break;
59695e05289SChris Mason 		case Opt_subvol:
59737becec9SOmar Sandoval 		case Opt_subvol_empty:
59873f73415SJosef Bacik 		case Opt_subvolid:
59943e570b0SChristoph Hellwig 		case Opt_device:
600edf24abeSChristoph Hellwig 			/*
601fa59f27cSAnand Jain 			 * These are parsed by btrfs_parse_subvol_options or
602fa59f27cSAnand Jain 			 * btrfs_parse_device_options and can be ignored here.
603edf24abeSChristoph Hellwig 			 */
60495e05289SChris Mason 			break;
605b6cda9bcSChris Mason 		case Opt_nodatasum:
6063cdde224SJeff Mahoney 			btrfs_set_and_info(info, NODATASUM,
60707802534SQu Wenruo 					   "setting nodatasum");
608be20aa9dSChris Mason 			break;
609d399167dSQu Wenruo 		case Opt_datasum:
6103cdde224SJeff Mahoney 			if (btrfs_test_opt(info, NODATASUM)) {
6113cdde224SJeff Mahoney 				if (btrfs_test_opt(info, NODATACOW))
6120b246afaSJeff Mahoney 					btrfs_info(info,
6135d163e0eSJeff Mahoney 						   "setting datasum, datacow enabled");
614d399167dSQu Wenruo 				else
6150b246afaSJeff Mahoney 					btrfs_info(info, "setting datasum");
61607802534SQu Wenruo 			}
617d399167dSQu Wenruo 			btrfs_clear_opt(info->mount_opt, NODATACOW);
618d399167dSQu Wenruo 			btrfs_clear_opt(info->mount_opt, NODATASUM);
619d399167dSQu Wenruo 			break;
620be20aa9dSChris Mason 		case Opt_nodatacow:
6213cdde224SJeff Mahoney 			if (!btrfs_test_opt(info, NODATACOW)) {
6223cdde224SJeff Mahoney 				if (!btrfs_test_opt(info, COMPRESS) ||
6233cdde224SJeff Mahoney 				    !btrfs_test_opt(info, FORCE_COMPRESS)) {
6240b246afaSJeff Mahoney 					btrfs_info(info,
625efe120a0SFrank Holton 						   "setting nodatacow, compression disabled");
626bedb2ccaSAndrei Popa 				} else {
6270b246afaSJeff Mahoney 					btrfs_info(info, "setting nodatacow");
628bedb2ccaSAndrei Popa 				}
62907802534SQu Wenruo 			}
630bedb2ccaSAndrei Popa 			btrfs_clear_opt(info->mount_opt, COMPRESS);
631bedb2ccaSAndrei Popa 			btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
632be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATACOW);
633be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
634b6cda9bcSChris Mason 			break;
635a258af7aSQu Wenruo 		case Opt_datacow:
6363cdde224SJeff Mahoney 			btrfs_clear_and_info(info, NODATACOW,
63707802534SQu Wenruo 					     "setting datacow");
638a258af7aSQu Wenruo 			break;
639a555f810SChris Mason 		case Opt_compress_force:
640261507a0SLi Zefan 		case Opt_compress_force_type:
641261507a0SLi Zefan 			compress_force = true;
642c730ae0cSMarcos Paulo de Souza 			fallthrough;
643261507a0SLi Zefan 		case Opt_compress:
644261507a0SLi Zefan 		case Opt_compress_type:
6453cdde224SJeff Mahoney 			saved_compress_type = btrfs_test_opt(info,
6463cdde224SJeff Mahoney 							     COMPRESS) ?
647b7c47bbbSTsutomu Itoh 				info->compress_type : BTRFS_COMPRESS_NONE;
648b7c47bbbSTsutomu Itoh 			saved_compress_force =
6493cdde224SJeff Mahoney 				btrfs_test_opt(info, FORCE_COMPRESS);
65027942c99SDavid Sterba 			saved_compress_level = info->compress_level;
651261507a0SLi Zefan 			if (token == Opt_compress ||
652261507a0SLi Zefan 			    token == Opt_compress_force ||
653a7164fa4SDavid Sterba 			    strncmp(args[0].from, "zlib", 4) == 0) {
654261507a0SLi Zefan 				compress_type = "zlib";
655eae8d825SQu Wenruo 
656261507a0SLi Zefan 				info->compress_type = BTRFS_COMPRESS_ZLIB;
657eae8d825SQu Wenruo 				info->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
658eae8d825SQu Wenruo 				/*
659eae8d825SQu Wenruo 				 * args[0] contains uninitialized data since
660eae8d825SQu Wenruo 				 * for these tokens we don't expect any
661eae8d825SQu Wenruo 				 * parameter.
662eae8d825SQu Wenruo 				 */
663eae8d825SQu Wenruo 				if (token != Opt_compress &&
664eae8d825SQu Wenruo 				    token != Opt_compress_force)
665f51d2b59SDavid Sterba 					info->compress_level =
666d0ab62ceSDennis Zhou 					  btrfs_compress_str2level(
667d0ab62ceSDennis Zhou 							BTRFS_COMPRESS_ZLIB,
668d0ab62ceSDennis Zhou 							args[0].from + 4);
669063849eaSArnd Hannemann 				btrfs_set_opt(info->mount_opt, COMPRESS);
670bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATACOW);
671bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATASUM);
672b7c47bbbSTsutomu Itoh 				no_compress = 0;
673a7164fa4SDavid Sterba 			} else if (strncmp(args[0].from, "lzo", 3) == 0) {
674a6fa6faeSLi Zefan 				compress_type = "lzo";
675a6fa6faeSLi Zefan 				info->compress_type = BTRFS_COMPRESS_LZO;
676282dd7d7SMarcos Paulo de Souza 				info->compress_level = 0;
677063849eaSArnd Hannemann 				btrfs_set_opt(info->mount_opt, COMPRESS);
678bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATACOW);
679bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATASUM);
6802b0ce2c2SMitch Harder 				btrfs_set_fs_incompat(info, COMPRESS_LZO);
681b7c47bbbSTsutomu Itoh 				no_compress = 0;
6823f93aef5SDennis Zhou 			} else if (strncmp(args[0].from, "zstd", 4) == 0) {
6835c1aab1dSNick Terrell 				compress_type = "zstd";
6845c1aab1dSNick Terrell 				info->compress_type = BTRFS_COMPRESS_ZSTD;
6853f93aef5SDennis Zhou 				info->compress_level =
6863f93aef5SDennis Zhou 					btrfs_compress_str2level(
6873f93aef5SDennis Zhou 							 BTRFS_COMPRESS_ZSTD,
6883f93aef5SDennis Zhou 							 args[0].from + 4);
6895c1aab1dSNick Terrell 				btrfs_set_opt(info->mount_opt, COMPRESS);
6905c1aab1dSNick Terrell 				btrfs_clear_opt(info->mount_opt, NODATACOW);
6915c1aab1dSNick Terrell 				btrfs_clear_opt(info->mount_opt, NODATASUM);
6925c1aab1dSNick Terrell 				btrfs_set_fs_incompat(info, COMPRESS_ZSTD);
6935c1aab1dSNick Terrell 				no_compress = 0;
694063849eaSArnd Hannemann 			} else if (strncmp(args[0].from, "no", 2) == 0) {
695063849eaSArnd Hannemann 				compress_type = "no";
69627942c99SDavid Sterba 				info->compress_level = 0;
69727942c99SDavid Sterba 				info->compress_type = 0;
698063849eaSArnd Hannemann 				btrfs_clear_opt(info->mount_opt, COMPRESS);
699063849eaSArnd Hannemann 				btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
700063849eaSArnd Hannemann 				compress_force = false;
701b7c47bbbSTsutomu Itoh 				no_compress++;
702261507a0SLi Zefan 			} else {
703261507a0SLi Zefan 				ret = -EINVAL;
704261507a0SLi Zefan 				goto out;
705261507a0SLi Zefan 			}
706261507a0SLi Zefan 
707261507a0SLi Zefan 			if (compress_force) {
708b7c47bbbSTsutomu Itoh 				btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
709143f3636SDavid Sterba 			} else {
7104027e0f4SWang Shilong 				/*
7114027e0f4SWang Shilong 				 * If we remount from compress-force=xxx to
7124027e0f4SWang Shilong 				 * compress=xxx, we need clear FORCE_COMPRESS
7134027e0f4SWang Shilong 				 * flag, otherwise, there is no way for users
7144027e0f4SWang Shilong 				 * to disable forcible compression separately.
7154027e0f4SWang Shilong 				 */
7164027e0f4SWang Shilong 				btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
717a7e252afSMiao Xie 			}
71827942c99SDavid Sterba 			if (no_compress == 1) {
71927942c99SDavid Sterba 				btrfs_info(info, "use no compression");
72027942c99SDavid Sterba 			} else if ((info->compress_type != saved_compress_type) ||
72127942c99SDavid Sterba 				   (compress_force != saved_compress_force) ||
72227942c99SDavid Sterba 				   (info->compress_level != saved_compress_level)) {
723f51d2b59SDavid Sterba 				btrfs_info(info, "%s %s compression, level %d",
724b7c47bbbSTsutomu Itoh 					   (compress_force) ? "force" : "use",
725f51d2b59SDavid Sterba 					   compress_type, info->compress_level);
726b7c47bbbSTsutomu Itoh 			}
727b7c47bbbSTsutomu Itoh 			compress_force = false;
728a555f810SChris Mason 			break;
729e18e4809SChris Mason 		case Opt_ssd:
7303cdde224SJeff Mahoney 			btrfs_set_and_info(info, SSD,
731583b7231SHans van Kranenburg 					   "enabling ssd optimizations");
732951e7966SAdam Borowski 			btrfs_clear_opt(info->mount_opt, NOSSD);
733e18e4809SChris Mason 			break;
734451d7585SChris Mason 		case Opt_ssd_spread:
735583b7231SHans van Kranenburg 			btrfs_set_and_info(info, SSD,
736583b7231SHans van Kranenburg 					   "enabling ssd optimizations");
7373cdde224SJeff Mahoney 			btrfs_set_and_info(info, SSD_SPREAD,
738583b7231SHans van Kranenburg 					   "using spread ssd allocation scheme");
739951e7966SAdam Borowski 			btrfs_clear_opt(info->mount_opt, NOSSD);
740451d7585SChris Mason 			break;
7413b30c22fSChris Mason 		case Opt_nossd:
742583b7231SHans van Kranenburg 			btrfs_set_opt(info->mount_opt, NOSSD);
743583b7231SHans van Kranenburg 			btrfs_clear_and_info(info, SSD,
744583b7231SHans van Kranenburg 					     "not using ssd optimizations");
745c730ae0cSMarcos Paulo de Souza 			fallthrough;
74662b8e077SHoward McLauchlan 		case Opt_nossd_spread:
747583b7231SHans van Kranenburg 			btrfs_clear_and_info(info, SSD_SPREAD,
748583b7231SHans van Kranenburg 					     "not using spread ssd allocation scheme");
7493b30c22fSChris Mason 			break;
750842bef58SQu Wenruo 		case Opt_barrier:
7513cdde224SJeff Mahoney 			btrfs_clear_and_info(info, NOBARRIER,
75207802534SQu Wenruo 					     "turning on barriers");
753842bef58SQu Wenruo 			break;
75421ad10cfSChris Mason 		case Opt_nobarrier:
7553cdde224SJeff Mahoney 			btrfs_set_and_info(info, NOBARRIER,
75607802534SQu Wenruo 					   "turning off barriers");
75721ad10cfSChris Mason 			break;
7584543df7eSChris Mason 		case Opt_thread_pool:
7592c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
7602c334e87SWang Shilong 			if (ret) {
7612c334e87SWang Shilong 				goto out;
762f7b885beSAnand Jain 			} else if (intarg == 0) {
7632c334e87SWang Shilong 				ret = -EINVAL;
7642c334e87SWang Shilong 				goto out;
7652c334e87SWang Shilong 			}
766f7b885beSAnand Jain 			info->thread_pool_size = intarg;
7674543df7eSChris Mason 			break;
7686f568d35SChris Mason 		case Opt_max_inline:
769edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
7706f568d35SChris Mason 			if (num) {
77191748467SAkinobu Mita 				info->max_inline = memparse(num, NULL);
7726f568d35SChris Mason 				kfree(num);
7736f568d35SChris Mason 
77415ada040SChris Mason 				if (info->max_inline) {
775feb5f965SMitch Harder 					info->max_inline = min_t(u64,
77615ada040SChris Mason 						info->max_inline,
7770b246afaSJeff Mahoney 						info->sectorsize);
77815ada040SChris Mason 				}
7790b246afaSJeff Mahoney 				btrfs_info(info, "max_inline at %llu",
780c1c9ff7cSGeert Uytterhoeven 					   info->max_inline);
7812c334e87SWang Shilong 			} else {
7822c334e87SWang Shilong 				ret = -ENOMEM;
7832c334e87SWang Shilong 				goto out;
7846f568d35SChris Mason 			}
7856f568d35SChris Mason 			break;
786bd0330adSQu Wenruo 		case Opt_acl:
78745ff35d6SGuangliang Zhao #ifdef CONFIG_BTRFS_FS_POSIX_ACL
7881751e8a6SLinus Torvalds 			info->sb->s_flags |= SB_POSIXACL;
789bd0330adSQu Wenruo 			break;
79045ff35d6SGuangliang Zhao #else
7910b246afaSJeff Mahoney 			btrfs_err(info, "support for ACL not compiled in!");
79245ff35d6SGuangliang Zhao 			ret = -EINVAL;
79345ff35d6SGuangliang Zhao 			goto out;
79445ff35d6SGuangliang Zhao #endif
79533268eafSJosef Bacik 		case Opt_noacl:
7961751e8a6SLinus Torvalds 			info->sb->s_flags &= ~SB_POSIXACL;
79733268eafSJosef Bacik 			break;
7983a5e1404SSage Weil 		case Opt_notreelog:
7993cdde224SJeff Mahoney 			btrfs_set_and_info(info, NOTREELOG,
80007802534SQu Wenruo 					   "disabling tree log");
8013a5e1404SSage Weil 			break;
802a88998f2SQu Wenruo 		case Opt_treelog:
8033cdde224SJeff Mahoney 			btrfs_clear_and_info(info, NOTREELOG,
80407802534SQu Wenruo 					     "enabling tree log");
805a88998f2SQu Wenruo 			break;
806fed8f166SQu Wenruo 		case Opt_norecovery:
80796da0919SQu Wenruo 		case Opt_nologreplay:
80874ef0018SQu Wenruo 			btrfs_warn(info,
80974ef0018SQu Wenruo 		"'nologreplay' is deprecated, use 'rescue=nologreplay' instead");
8103cdde224SJeff Mahoney 			btrfs_set_and_info(info, NOLOGREPLAY,
81196da0919SQu Wenruo 					   "disabling log replay at mount time");
81296da0919SQu Wenruo 			break;
813dccae999SSage Weil 		case Opt_flushoncommit:
8143cdde224SJeff Mahoney 			btrfs_set_and_info(info, FLUSHONCOMMIT,
81507802534SQu Wenruo 					   "turning on flush-on-commit");
816dccae999SSage Weil 			break;
8172c9ee856SQu Wenruo 		case Opt_noflushoncommit:
8183cdde224SJeff Mahoney 			btrfs_clear_and_info(info, FLUSHONCOMMIT,
81907802534SQu Wenruo 					     "turning off flush-on-commit");
8202c9ee856SQu Wenruo 			break;
82197e728d4SJosef Bacik 		case Opt_ratio:
8222c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
823764cb8b4SAnand Jain 			if (ret)
8242c334e87SWang Shilong 				goto out;
82597e728d4SJosef Bacik 			info->metadata_ratio = intarg;
826764cb8b4SAnand Jain 			btrfs_info(info, "metadata ratio %u",
82797e728d4SJosef Bacik 				   info->metadata_ratio);
82897e728d4SJosef Bacik 			break;
829e244a0aeSChristoph Hellwig 		case Opt_discard:
830b0643e59SDennis Zhou 		case Opt_discard_mode:
831b0643e59SDennis Zhou 			if (token == Opt_discard ||
832b0643e59SDennis Zhou 			    strcmp(args[0].from, "sync") == 0) {
833b0643e59SDennis Zhou 				btrfs_clear_opt(info->mount_opt, DISCARD_ASYNC);
83446b27f50SDennis Zhou 				btrfs_set_and_info(info, DISCARD_SYNC,
83546b27f50SDennis Zhou 						   "turning on sync discard");
836b0643e59SDennis Zhou 			} else if (strcmp(args[0].from, "async") == 0) {
837b0643e59SDennis Zhou 				btrfs_clear_opt(info->mount_opt, DISCARD_SYNC);
838b0643e59SDennis Zhou 				btrfs_set_and_info(info, DISCARD_ASYNC,
839b0643e59SDennis Zhou 						   "turning on async discard");
840b0643e59SDennis Zhou 			} else {
841b0643e59SDennis Zhou 				ret = -EINVAL;
842b0643e59SDennis Zhou 				goto out;
843b0643e59SDennis Zhou 			}
844e244a0aeSChristoph Hellwig 			break;
845e07a2adeSQu Wenruo 		case Opt_nodiscard:
84646b27f50SDennis Zhou 			btrfs_clear_and_info(info, DISCARD_SYNC,
84707802534SQu Wenruo 					     "turning off discard");
848b0643e59SDennis Zhou 			btrfs_clear_and_info(info, DISCARD_ASYNC,
849b0643e59SDennis Zhou 					     "turning off async discard");
850e07a2adeSQu Wenruo 			break;
8510af3d00bSJosef Bacik 		case Opt_space_cache:
85270f6d82eSOmar Sandoval 		case Opt_space_cache_version:
85370f6d82eSOmar Sandoval 			if (token == Opt_space_cache ||
85470f6d82eSOmar Sandoval 			    strcmp(args[0].from, "v1") == 0) {
8550b246afaSJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
85670f6d82eSOmar Sandoval 						FREE_SPACE_TREE);
8573cdde224SJeff Mahoney 				btrfs_set_and_info(info, SPACE_CACHE,
85807802534SQu Wenruo 					   "enabling disk space caching");
85970f6d82eSOmar Sandoval 			} else if (strcmp(args[0].from, "v2") == 0) {
8600b246afaSJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
86170f6d82eSOmar Sandoval 						SPACE_CACHE);
8620b246afaSJeff Mahoney 				btrfs_set_and_info(info, FREE_SPACE_TREE,
86370f6d82eSOmar Sandoval 						   "enabling free space tree");
86470f6d82eSOmar Sandoval 			} else {
86570f6d82eSOmar Sandoval 				ret = -EINVAL;
86670f6d82eSOmar Sandoval 				goto out;
86770f6d82eSOmar Sandoval 			}
8680de90876SJosef Bacik 			break;
869f420ee1eSStefan Behrens 		case Opt_rescan_uuid_tree:
870f420ee1eSStefan Behrens 			btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
871f420ee1eSStefan Behrens 			break;
87273bc1876SJosef Bacik 		case Opt_no_space_cache:
8733cdde224SJeff Mahoney 			if (btrfs_test_opt(info, SPACE_CACHE)) {
8740b246afaSJeff Mahoney 				btrfs_clear_and_info(info, SPACE_CACHE,
87507802534SQu Wenruo 					     "disabling disk space caching");
87670f6d82eSOmar Sandoval 			}
8773cdde224SJeff Mahoney 			if (btrfs_test_opt(info, FREE_SPACE_TREE)) {
8780b246afaSJeff Mahoney 				btrfs_clear_and_info(info, FREE_SPACE_TREE,
87970f6d82eSOmar Sandoval 					     "disabling free space tree");
88070f6d82eSOmar Sandoval 			}
88173bc1876SJosef Bacik 			break;
8824b9465cbSChris Mason 		case Opt_inode_cache:
8833818aea2SQu Wenruo 		case Opt_noinode_cache:
8845297199aSNikolay Borisov 			btrfs_warn(info,
8855297199aSNikolay Borisov 	"the 'inode_cache' option is deprecated and has no effect since 5.11");
8864b9465cbSChris Mason 			break;
88788c2ba3bSJosef Bacik 		case Opt_clear_cache:
8883cdde224SJeff Mahoney 			btrfs_set_and_info(info, CLEAR_CACHE,
88907802534SQu Wenruo 					   "force clearing of disk cache");
8900af3d00bSJosef Bacik 			break;
8914260f7c7SSage Weil 		case Opt_user_subvol_rm_allowed:
8924260f7c7SSage Weil 			btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
8934260f7c7SSage Weil 			break;
89491435650SChris Mason 		case Opt_enospc_debug:
89591435650SChris Mason 			btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
89691435650SChris Mason 			break;
89753036293SQu Wenruo 		case Opt_noenospc_debug:
89853036293SQu Wenruo 			btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG);
89953036293SQu Wenruo 			break;
9004cb5300bSChris Mason 		case Opt_defrag:
9013cdde224SJeff Mahoney 			btrfs_set_and_info(info, AUTO_DEFRAG,
90207802534SQu Wenruo 					   "enabling auto defrag");
9034cb5300bSChris Mason 			break;
904fc0ca9afSQu Wenruo 		case Opt_nodefrag:
9053cdde224SJeff Mahoney 			btrfs_clear_and_info(info, AUTO_DEFRAG,
90607802534SQu Wenruo 					     "disabling auto defrag");
907fc0ca9afSQu Wenruo 			break;
908af31f5e5SChris Mason 		case Opt_recovery:
9098dcddfa0SQu Wenruo 		case Opt_usebackuproot:
91074ef0018SQu Wenruo 			btrfs_warn(info,
91174ef0018SQu Wenruo 			"'%s' is deprecated, use 'rescue=usebackuproot' instead",
91274ef0018SQu Wenruo 				   token == Opt_recovery ? "recovery" :
91374ef0018SQu Wenruo 				   "usebackuproot");
9140b246afaSJeff Mahoney 			btrfs_info(info,
9158dcddfa0SQu Wenruo 				   "trying to use backup root at mount time");
9168dcddfa0SQu Wenruo 			btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
917af31f5e5SChris Mason 			break;
9189555c6c1SIlya Dryomov 		case Opt_skip_balance:
9199555c6c1SIlya Dryomov 			btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
9209555c6c1SIlya Dryomov 			break;
92121adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
92221adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
9230b246afaSJeff Mahoney 			btrfs_info(info,
924efe120a0SFrank Holton 				   "enabling check integrity including extent data");
92521adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt,
92621adbd5cSStefan Behrens 				      CHECK_INTEGRITY_INCLUDING_EXTENT_DATA);
92721adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
92821adbd5cSStefan Behrens 			break;
92921adbd5cSStefan Behrens 		case Opt_check_integrity:
9300b246afaSJeff Mahoney 			btrfs_info(info, "enabling check integrity");
93121adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
93221adbd5cSStefan Behrens 			break;
93321adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
9342c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
93502453bdeSAnand Jain 			if (ret)
9362c334e87SWang Shilong 				goto out;
93721adbd5cSStefan Behrens 			info->check_integrity_print_mask = intarg;
93802453bdeSAnand Jain 			btrfs_info(info, "check_integrity_print_mask 0x%x",
93921adbd5cSStefan Behrens 				   info->check_integrity_print_mask);
94021adbd5cSStefan Behrens 			break;
94121adbd5cSStefan Behrens #else
94221adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
94321adbd5cSStefan Behrens 		case Opt_check_integrity:
94421adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
9450b246afaSJeff Mahoney 			btrfs_err(info,
946efe120a0SFrank Holton 				  "support for check_integrity* not compiled in!");
94721adbd5cSStefan Behrens 			ret = -EINVAL;
94821adbd5cSStefan Behrens 			goto out;
94921adbd5cSStefan Behrens #endif
9508c342930SJeff Mahoney 		case Opt_fatal_errors:
9518c342930SJeff Mahoney 			if (strcmp(args[0].from, "panic") == 0)
9528c342930SJeff Mahoney 				btrfs_set_opt(info->mount_opt,
9538c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
9548c342930SJeff Mahoney 			else if (strcmp(args[0].from, "bug") == 0)
9558c342930SJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
9568c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
9578c342930SJeff Mahoney 			else {
9588c342930SJeff Mahoney 				ret = -EINVAL;
9598c342930SJeff Mahoney 				goto out;
9608c342930SJeff Mahoney 			}
9618c342930SJeff Mahoney 			break;
9628b87dc17SDavid Sterba 		case Opt_commit_interval:
9638b87dc17SDavid Sterba 			intarg = 0;
9648b87dc17SDavid Sterba 			ret = match_int(&args[0], &intarg);
965d3740608SAnand Jain 			if (ret)
9668b87dc17SDavid Sterba 				goto out;
967d3740608SAnand Jain 			if (intarg == 0) {
968d3740608SAnand Jain 				btrfs_info(info,
969d3740608SAnand Jain 					   "using default commit interval %us",
970d3740608SAnand Jain 					   BTRFS_DEFAULT_COMMIT_INTERVAL);
971d3740608SAnand Jain 				intarg = BTRFS_DEFAULT_COMMIT_INTERVAL;
972d3740608SAnand Jain 			} else if (intarg > 300) {
973d3740608SAnand Jain 				btrfs_warn(info, "excessive commit interval %d",
9748b87dc17SDavid Sterba 					   intarg);
9758b87dc17SDavid Sterba 			}
9768b87dc17SDavid Sterba 			info->commit_interval = intarg;
9778b87dc17SDavid Sterba 			break;
97874ef0018SQu Wenruo 		case Opt_rescue:
97974ef0018SQu Wenruo 			ret = parse_rescue_options(info, args[0].from);
98074ef0018SQu Wenruo 			if (ret < 0)
98174ef0018SQu Wenruo 				goto out;
98274ef0018SQu Wenruo 			break;
983d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
984d0bd4560SJosef Bacik 		case Opt_fragment_all:
9850b246afaSJeff Mahoney 			btrfs_info(info, "fragmenting all space");
986d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
987d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA);
988d0bd4560SJosef Bacik 			break;
989d0bd4560SJosef Bacik 		case Opt_fragment_metadata:
9900b246afaSJeff Mahoney 			btrfs_info(info, "fragmenting metadata");
991d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt,
992d0bd4560SJosef Bacik 				      FRAGMENT_METADATA);
993d0bd4560SJosef Bacik 			break;
994d0bd4560SJosef Bacik 		case Opt_fragment_data:
9950b246afaSJeff Mahoney 			btrfs_info(info, "fragmenting data");
996d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
997d0bd4560SJosef Bacik 			break;
998d0bd4560SJosef Bacik #endif
999fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
1000fb592373SJosef Bacik 		case Opt_ref_verify:
1001fb592373SJosef Bacik 			btrfs_info(info, "doing ref verification");
1002fb592373SJosef Bacik 			btrfs_set_opt(info->mount_opt, REF_VERIFY);
1003fb592373SJosef Bacik 			break;
1004fb592373SJosef Bacik #endif
1005a7a3f7caSSage Weil 		case Opt_err:
10067e8f19e5SDavid Sterba 			btrfs_err(info, "unrecognized mount option '%s'", p);
1007a7a3f7caSSage Weil 			ret = -EINVAL;
1008a7a3f7caSSage Weil 			goto out;
100995e05289SChris Mason 		default:
1010be20aa9dSChris Mason 			break;
101195e05289SChris Mason 		}
101295e05289SChris Mason 	}
101396da0919SQu Wenruo check:
1014d70bf748SJosef Bacik 	/* We're read-only, don't have to check. */
1015d70bf748SJosef Bacik 	if (new_flags & SB_RDONLY)
1016d70bf748SJosef Bacik 		goto out;
1017d70bf748SJosef Bacik 
101842437a63SJosef Bacik 	if (check_ro_option(info, BTRFS_MOUNT_NOLOGREPLAY, "nologreplay") ||
1019882dbe0cSJosef Bacik 	    check_ro_option(info, BTRFS_MOUNT_IGNOREBADROOTS, "ignorebadroots") ||
1020882dbe0cSJosef Bacik 	    check_ro_option(info, BTRFS_MOUNT_IGNOREDATACSUMS, "ignoredatacsums"))
102196da0919SQu Wenruo 		ret = -EINVAL;
1022a7a3f7caSSage Weil out:
10230b246afaSJeff Mahoney 	if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) &&
10243cdde224SJeff Mahoney 	    !btrfs_test_opt(info, FREE_SPACE_TREE) &&
10253cdde224SJeff Mahoney 	    !btrfs_test_opt(info, CLEAR_CACHE)) {
10260b246afaSJeff Mahoney 		btrfs_err(info, "cannot disable free space tree");
102770f6d82eSOmar Sandoval 		ret = -EINVAL;
102870f6d82eSOmar Sandoval 
102970f6d82eSOmar Sandoval 	}
10305d1ab66cSNaohiro Aota 	if (!ret)
10315d1ab66cSNaohiro Aota 		ret = btrfs_check_mountopts_zoned(info);
10323cdde224SJeff Mahoney 	if (!ret && btrfs_test_opt(info, SPACE_CACHE))
10330b246afaSJeff Mahoney 		btrfs_info(info, "disk space caching is enabled");
10343cdde224SJeff Mahoney 	if (!ret && btrfs_test_opt(info, FREE_SPACE_TREE))
10350b246afaSJeff Mahoney 		btrfs_info(info, "using free space tree");
1036a7a3f7caSSage Weil 	return ret;
1037edf24abeSChristoph Hellwig }
1038edf24abeSChristoph Hellwig 
1039edf24abeSChristoph Hellwig /*
1040edf24abeSChristoph Hellwig  * Parse mount options that are required early in the mount process.
1041edf24abeSChristoph Hellwig  *
1042edf24abeSChristoph Hellwig  * All other options will be parsed on much later in the mount process and
1043edf24abeSChristoph Hellwig  * only when we need to allocate a new super block.
1044edf24abeSChristoph Hellwig  */
1045fa59f27cSAnand Jain static int btrfs_parse_device_options(const char *options, fmode_t flags,
1046d64dcbd1SGu Jinxiang 				      void *holder)
1047edf24abeSChristoph Hellwig {
1048edf24abeSChristoph Hellwig 	substring_t args[MAX_OPT_ARGS];
104983c8c9bdSJeff Liu 	char *device_name, *opts, *orig, *p;
105036350e95SGu Jinxiang 	struct btrfs_device *device = NULL;
1051d7407606SMisono, Tomohiro 	int error = 0;
1052d7407606SMisono, Tomohiro 
10535139cff5SDavid Sterba 	lockdep_assert_held(&uuid_mutex);
10545139cff5SDavid Sterba 
1055d7407606SMisono, Tomohiro 	if (!options)
1056d7407606SMisono, Tomohiro 		return 0;
1057d7407606SMisono, Tomohiro 
1058d7407606SMisono, Tomohiro 	/*
1059d7407606SMisono, Tomohiro 	 * strsep changes the string, duplicate it because btrfs_parse_options
1060d7407606SMisono, Tomohiro 	 * gets called later
1061d7407606SMisono, Tomohiro 	 */
1062d7407606SMisono, Tomohiro 	opts = kstrdup(options, GFP_KERNEL);
1063d7407606SMisono, Tomohiro 	if (!opts)
1064d7407606SMisono, Tomohiro 		return -ENOMEM;
1065d7407606SMisono, Tomohiro 	orig = opts;
1066d7407606SMisono, Tomohiro 
1067d7407606SMisono, Tomohiro 	while ((p = strsep(&opts, ",")) != NULL) {
1068d7407606SMisono, Tomohiro 		int token;
1069d7407606SMisono, Tomohiro 
1070d7407606SMisono, Tomohiro 		if (!*p)
1071d7407606SMisono, Tomohiro 			continue;
1072d7407606SMisono, Tomohiro 
1073d7407606SMisono, Tomohiro 		token = match_token(p, tokens, args);
1074d7407606SMisono, Tomohiro 		if (token == Opt_device) {
1075d7407606SMisono, Tomohiro 			device_name = match_strdup(&args[0]);
1076d7407606SMisono, Tomohiro 			if (!device_name) {
1077d7407606SMisono, Tomohiro 				error = -ENOMEM;
1078d7407606SMisono, Tomohiro 				goto out;
1079d7407606SMisono, Tomohiro 			}
108036350e95SGu Jinxiang 			device = btrfs_scan_one_device(device_name, flags,
108136350e95SGu Jinxiang 					holder);
1082d7407606SMisono, Tomohiro 			kfree(device_name);
108336350e95SGu Jinxiang 			if (IS_ERR(device)) {
108436350e95SGu Jinxiang 				error = PTR_ERR(device);
1085d7407606SMisono, Tomohiro 				goto out;
1086d7407606SMisono, Tomohiro 			}
1087d7407606SMisono, Tomohiro 		}
108836350e95SGu Jinxiang 	}
1089d7407606SMisono, Tomohiro 
1090d7407606SMisono, Tomohiro out:
1091d7407606SMisono, Tomohiro 	kfree(orig);
1092d7407606SMisono, Tomohiro 	return error;
1093d7407606SMisono, Tomohiro }
1094d7407606SMisono, Tomohiro 
1095d7407606SMisono, Tomohiro /*
1096d7407606SMisono, Tomohiro  * Parse mount options that are related to subvolume id
1097d7407606SMisono, Tomohiro  *
1098d7407606SMisono, Tomohiro  * The value is later passed to mount_subvol()
1099d7407606SMisono, Tomohiro  */
110093b9bcdfSGu Jinxiang static int btrfs_parse_subvol_options(const char *options, char **subvol_name,
110193b9bcdfSGu Jinxiang 		u64 *subvol_objectid)
1102d7407606SMisono, Tomohiro {
1103d7407606SMisono, Tomohiro 	substring_t args[MAX_OPT_ARGS];
1104d7407606SMisono, Tomohiro 	char *opts, *orig, *p;
1105edf24abeSChristoph Hellwig 	int error = 0;
1106ccb0e7d1SAnand Jain 	u64 subvolid;
1107edf24abeSChristoph Hellwig 
1108edf24abeSChristoph Hellwig 	if (!options)
1109830c4adbSJosef Bacik 		return 0;
1110edf24abeSChristoph Hellwig 
1111edf24abeSChristoph Hellwig 	/*
1112d7407606SMisono, Tomohiro 	 * strsep changes the string, duplicate it because
1113fa59f27cSAnand Jain 	 * btrfs_parse_device_options gets called later
1114edf24abeSChristoph Hellwig 	 */
1115edf24abeSChristoph Hellwig 	opts = kstrdup(options, GFP_KERNEL);
1116edf24abeSChristoph Hellwig 	if (!opts)
1117edf24abeSChristoph Hellwig 		return -ENOMEM;
11183f3d0bc0STero Roponen 	orig = opts;
1119edf24abeSChristoph Hellwig 
1120edf24abeSChristoph Hellwig 	while ((p = strsep(&opts, ",")) != NULL) {
1121edf24abeSChristoph Hellwig 		int token;
1122edf24abeSChristoph Hellwig 		if (!*p)
1123edf24abeSChristoph Hellwig 			continue;
1124edf24abeSChristoph Hellwig 
1125edf24abeSChristoph Hellwig 		token = match_token(p, tokens, args);
1126edf24abeSChristoph Hellwig 		switch (token) {
1127edf24abeSChristoph Hellwig 		case Opt_subvol:
1128a90e8b6fSIlya Dryomov 			kfree(*subvol_name);
1129edf24abeSChristoph Hellwig 			*subvol_name = match_strdup(&args[0]);
11302c334e87SWang Shilong 			if (!*subvol_name) {
11312c334e87SWang Shilong 				error = -ENOMEM;
11322c334e87SWang Shilong 				goto out;
11332c334e87SWang Shilong 			}
1134edf24abeSChristoph Hellwig 			break;
113573f73415SJosef Bacik 		case Opt_subvolid:
1136ccb0e7d1SAnand Jain 			error = match_u64(&args[0], &subvolid);
1137ccb0e7d1SAnand Jain 			if (error)
11382c334e87SWang Shilong 				goto out;
1139ccb0e7d1SAnand Jain 
1140ccb0e7d1SAnand Jain 			/* we want the original fs_tree */
1141ccb0e7d1SAnand Jain 			if (subvolid == 0)
1142ccb0e7d1SAnand Jain 				subvolid = BTRFS_FS_TREE_OBJECTID;
1143ccb0e7d1SAnand Jain 
1144ccb0e7d1SAnand Jain 			*subvol_objectid = subvolid;
114573f73415SJosef Bacik 			break;
1146edf24abeSChristoph Hellwig 		default:
1147edf24abeSChristoph Hellwig 			break;
1148edf24abeSChristoph Hellwig 		}
1149edf24abeSChristoph Hellwig 	}
1150edf24abeSChristoph Hellwig 
1151edf24abeSChristoph Hellwig out:
1152830c4adbSJosef Bacik 	kfree(orig);
1153edf24abeSChristoph Hellwig 	return error;
115495e05289SChris Mason }
115595e05289SChris Mason 
1156c0c907a4SMarcos Paulo de Souza char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
115773f73415SJosef Bacik 					  u64 subvol_objectid)
115873f73415SJosef Bacik {
1159815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
11605168489aSJosef Bacik 	struct btrfs_root *fs_root = NULL;
116105dbe683SOmar Sandoval 	struct btrfs_root_ref *root_ref;
116205dbe683SOmar Sandoval 	struct btrfs_inode_ref *inode_ref;
116305dbe683SOmar Sandoval 	struct btrfs_key key;
116405dbe683SOmar Sandoval 	struct btrfs_path *path = NULL;
116505dbe683SOmar Sandoval 	char *name = NULL, *ptr;
116605dbe683SOmar Sandoval 	u64 dirid;
116705dbe683SOmar Sandoval 	int len;
116805dbe683SOmar Sandoval 	int ret;
116905dbe683SOmar Sandoval 
117005dbe683SOmar Sandoval 	path = btrfs_alloc_path();
117105dbe683SOmar Sandoval 	if (!path) {
117205dbe683SOmar Sandoval 		ret = -ENOMEM;
117305dbe683SOmar Sandoval 		goto err;
117405dbe683SOmar Sandoval 	}
117505dbe683SOmar Sandoval 
11763ec83621SDavid Sterba 	name = kmalloc(PATH_MAX, GFP_KERNEL);
117705dbe683SOmar Sandoval 	if (!name) {
117805dbe683SOmar Sandoval 		ret = -ENOMEM;
117905dbe683SOmar Sandoval 		goto err;
118005dbe683SOmar Sandoval 	}
118105dbe683SOmar Sandoval 	ptr = name + PATH_MAX - 1;
118205dbe683SOmar Sandoval 	ptr[0] = '\0';
118305dbe683SOmar Sandoval 
118405dbe683SOmar Sandoval 	/*
118505dbe683SOmar Sandoval 	 * Walk up the subvolume trees in the tree of tree roots by root
118605dbe683SOmar Sandoval 	 * backrefs until we hit the top-level subvolume.
118705dbe683SOmar Sandoval 	 */
118805dbe683SOmar Sandoval 	while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
118905dbe683SOmar Sandoval 		key.objectid = subvol_objectid;
119005dbe683SOmar Sandoval 		key.type = BTRFS_ROOT_BACKREF_KEY;
119105dbe683SOmar Sandoval 		key.offset = (u64)-1;
119205dbe683SOmar Sandoval 
119305dbe683SOmar Sandoval 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
119405dbe683SOmar Sandoval 		if (ret < 0) {
119505dbe683SOmar Sandoval 			goto err;
119605dbe683SOmar Sandoval 		} else if (ret > 0) {
119705dbe683SOmar Sandoval 			ret = btrfs_previous_item(root, path, subvol_objectid,
119805dbe683SOmar Sandoval 						  BTRFS_ROOT_BACKREF_KEY);
119905dbe683SOmar Sandoval 			if (ret < 0) {
120005dbe683SOmar Sandoval 				goto err;
120105dbe683SOmar Sandoval 			} else if (ret > 0) {
120205dbe683SOmar Sandoval 				ret = -ENOENT;
120305dbe683SOmar Sandoval 				goto err;
120405dbe683SOmar Sandoval 			}
120505dbe683SOmar Sandoval 		}
120605dbe683SOmar Sandoval 
120705dbe683SOmar Sandoval 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
120805dbe683SOmar Sandoval 		subvol_objectid = key.offset;
120905dbe683SOmar Sandoval 
121005dbe683SOmar Sandoval 		root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
121105dbe683SOmar Sandoval 					  struct btrfs_root_ref);
121205dbe683SOmar Sandoval 		len = btrfs_root_ref_name_len(path->nodes[0], root_ref);
121305dbe683SOmar Sandoval 		ptr -= len + 1;
121405dbe683SOmar Sandoval 		if (ptr < name) {
121505dbe683SOmar Sandoval 			ret = -ENAMETOOLONG;
121605dbe683SOmar Sandoval 			goto err;
121705dbe683SOmar Sandoval 		}
121805dbe683SOmar Sandoval 		read_extent_buffer(path->nodes[0], ptr + 1,
121905dbe683SOmar Sandoval 				   (unsigned long)(root_ref + 1), len);
122005dbe683SOmar Sandoval 		ptr[0] = '/';
122105dbe683SOmar Sandoval 		dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);
122205dbe683SOmar Sandoval 		btrfs_release_path(path);
122305dbe683SOmar Sandoval 
122456e9357aSDavid Sterba 		fs_root = btrfs_get_fs_root(fs_info, subvol_objectid, true);
122505dbe683SOmar Sandoval 		if (IS_ERR(fs_root)) {
122605dbe683SOmar Sandoval 			ret = PTR_ERR(fs_root);
12275168489aSJosef Bacik 			fs_root = NULL;
12285168489aSJosef Bacik 			goto err;
12295168489aSJosef Bacik 		}
123005dbe683SOmar Sandoval 
123105dbe683SOmar Sandoval 		/*
123205dbe683SOmar Sandoval 		 * Walk up the filesystem tree by inode refs until we hit the
123305dbe683SOmar Sandoval 		 * root directory.
123405dbe683SOmar Sandoval 		 */
123505dbe683SOmar Sandoval 		while (dirid != BTRFS_FIRST_FREE_OBJECTID) {
123605dbe683SOmar Sandoval 			key.objectid = dirid;
123705dbe683SOmar Sandoval 			key.type = BTRFS_INODE_REF_KEY;
123805dbe683SOmar Sandoval 			key.offset = (u64)-1;
123905dbe683SOmar Sandoval 
124005dbe683SOmar Sandoval 			ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
124105dbe683SOmar Sandoval 			if (ret < 0) {
124205dbe683SOmar Sandoval 				goto err;
124305dbe683SOmar Sandoval 			} else if (ret > 0) {
124405dbe683SOmar Sandoval 				ret = btrfs_previous_item(fs_root, path, dirid,
124505dbe683SOmar Sandoval 							  BTRFS_INODE_REF_KEY);
124605dbe683SOmar Sandoval 				if (ret < 0) {
124705dbe683SOmar Sandoval 					goto err;
124805dbe683SOmar Sandoval 				} else if (ret > 0) {
124905dbe683SOmar Sandoval 					ret = -ENOENT;
125005dbe683SOmar Sandoval 					goto err;
125105dbe683SOmar Sandoval 				}
125205dbe683SOmar Sandoval 			}
125305dbe683SOmar Sandoval 
125405dbe683SOmar Sandoval 			btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
125505dbe683SOmar Sandoval 			dirid = key.offset;
125605dbe683SOmar Sandoval 
125705dbe683SOmar Sandoval 			inode_ref = btrfs_item_ptr(path->nodes[0],
125805dbe683SOmar Sandoval 						   path->slots[0],
125905dbe683SOmar Sandoval 						   struct btrfs_inode_ref);
126005dbe683SOmar Sandoval 			len = btrfs_inode_ref_name_len(path->nodes[0],
126105dbe683SOmar Sandoval 						       inode_ref);
126205dbe683SOmar Sandoval 			ptr -= len + 1;
126305dbe683SOmar Sandoval 			if (ptr < name) {
126405dbe683SOmar Sandoval 				ret = -ENAMETOOLONG;
126505dbe683SOmar Sandoval 				goto err;
126605dbe683SOmar Sandoval 			}
126705dbe683SOmar Sandoval 			read_extent_buffer(path->nodes[0], ptr + 1,
126805dbe683SOmar Sandoval 					   (unsigned long)(inode_ref + 1), len);
126905dbe683SOmar Sandoval 			ptr[0] = '/';
127005dbe683SOmar Sandoval 			btrfs_release_path(path);
127105dbe683SOmar Sandoval 		}
127200246528SJosef Bacik 		btrfs_put_root(fs_root);
12735168489aSJosef Bacik 		fs_root = NULL;
127405dbe683SOmar Sandoval 	}
127505dbe683SOmar Sandoval 
127605dbe683SOmar Sandoval 	btrfs_free_path(path);
127705dbe683SOmar Sandoval 	if (ptr == name + PATH_MAX - 1) {
127805dbe683SOmar Sandoval 		name[0] = '/';
127905dbe683SOmar Sandoval 		name[1] = '\0';
128005dbe683SOmar Sandoval 	} else {
128105dbe683SOmar Sandoval 		memmove(name, ptr, name + PATH_MAX - ptr);
128205dbe683SOmar Sandoval 	}
128305dbe683SOmar Sandoval 	return name;
128405dbe683SOmar Sandoval 
128505dbe683SOmar Sandoval err:
128600246528SJosef Bacik 	btrfs_put_root(fs_root);
128705dbe683SOmar Sandoval 	btrfs_free_path(path);
128805dbe683SOmar Sandoval 	kfree(name);
128905dbe683SOmar Sandoval 	return ERR_PTR(ret);
129005dbe683SOmar Sandoval }
129105dbe683SOmar Sandoval 
129205dbe683SOmar Sandoval static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid)
129305dbe683SOmar Sandoval {
129405dbe683SOmar Sandoval 	struct btrfs_root *root = fs_info->tree_root;
129573f73415SJosef Bacik 	struct btrfs_dir_item *di;
129673f73415SJosef Bacik 	struct btrfs_path *path;
129773f73415SJosef Bacik 	struct btrfs_key location;
129873f73415SJosef Bacik 	u64 dir_id;
129973f73415SJosef Bacik 
130073f73415SJosef Bacik 	path = btrfs_alloc_path();
130173f73415SJosef Bacik 	if (!path)
130205dbe683SOmar Sandoval 		return -ENOMEM;
130373f73415SJosef Bacik 
130473f73415SJosef Bacik 	/*
130573f73415SJosef Bacik 	 * Find the "default" dir item which points to the root item that we
130673f73415SJosef Bacik 	 * will mount by default if we haven't been given a specific subvolume
130773f73415SJosef Bacik 	 * to mount.
130873f73415SJosef Bacik 	 */
1309815745cfSAl Viro 	dir_id = btrfs_super_root_dir(fs_info->super_copy);
131073f73415SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
1311b0839166SJulia Lawall 	if (IS_ERR(di)) {
1312b0839166SJulia Lawall 		btrfs_free_path(path);
131305dbe683SOmar Sandoval 		return PTR_ERR(di);
1314b0839166SJulia Lawall 	}
131573f73415SJosef Bacik 	if (!di) {
131673f73415SJosef Bacik 		/*
131773f73415SJosef Bacik 		 * Ok the default dir item isn't there.  This is weird since
131873f73415SJosef Bacik 		 * it's always been there, but don't freak out, just try and
131905dbe683SOmar Sandoval 		 * mount the top-level subvolume.
132073f73415SJosef Bacik 		 */
132173f73415SJosef Bacik 		btrfs_free_path(path);
132205dbe683SOmar Sandoval 		*objectid = BTRFS_FS_TREE_OBJECTID;
132305dbe683SOmar Sandoval 		return 0;
132473f73415SJosef Bacik 	}
132573f73415SJosef Bacik 
132673f73415SJosef Bacik 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
132773f73415SJosef Bacik 	btrfs_free_path(path);
132805dbe683SOmar Sandoval 	*objectid = location.objectid;
132905dbe683SOmar Sandoval 	return 0;
133073f73415SJosef Bacik }
133173f73415SJosef Bacik 
13328a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb,
13338a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
133456e033a7SDavid Sterba 			    void *data)
13352e635a27SChris Mason {
13362e635a27SChris Mason 	struct inode *inode;
1337815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
133839279cc3SChris Mason 	int err;
13392e635a27SChris Mason 
13402e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
13412e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
1342e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
1343af53d29aSAl Viro 	sb->s_d_op = &btrfs_dentry_operations;
1344be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
13455103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
13462e635a27SChris Mason 	sb->s_time_gran = 1;
13470eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL
13481751e8a6SLinus Torvalds 	sb->s_flags |= SB_POSIXACL;
134949cf6f45SChris Ball #endif
1350357fdad0SMatthew Garrett 	sb->s_flags |= SB_I_VERSION;
1351da2f0f74SChris Mason 	sb->s_iflags |= SB_I_CGROUPWB;
13529e11ceeeSJan Kara 
13539e11ceeeSJan Kara 	err = super_setup_bdi(sb);
13549e11ceeeSJan Kara 	if (err) {
13559e11ceeeSJan Kara 		btrfs_err(fs_info, "super_setup_bdi failed");
13569e11ceeeSJan Kara 		return err;
13579e11ceeeSJan Kara 	}
13589e11ceeeSJan Kara 
1359ad2b2c80SAl Viro 	err = open_ctree(sb, fs_devices, (char *)data);
1360ad2b2c80SAl Viro 	if (err) {
1361ab8d0fc4SJeff Mahoney 		btrfs_err(fs_info, "open_ctree failed");
1362ad2b2c80SAl Viro 		return err;
1363e20d96d6SChris Mason 	}
1364b888db2bSChris Mason 
13650202e83fSDavid Sterba 	inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, fs_info->fs_root);
13665d4f98a2SYan Zheng 	if (IS_ERR(inode)) {
13675d4f98a2SYan Zheng 		err = PTR_ERR(inode);
136839279cc3SChris Mason 		goto fail_close;
136939279cc3SChris Mason 	}
13702e635a27SChris Mason 
137148fde701SAl Viro 	sb->s_root = d_make_root(inode);
137248fde701SAl Viro 	if (!sb->s_root) {
137339279cc3SChris Mason 		err = -ENOMEM;
137439279cc3SChris Mason 		goto fail_close;
13752e635a27SChris Mason 	}
137658176a96SJosef Bacik 
137790a887c9SDan Magenheimer 	cleancache_init_fs(sb);
13781751e8a6SLinus Torvalds 	sb->s_flags |= SB_ACTIVE;
13792e635a27SChris Mason 	return 0;
13802e635a27SChris Mason 
138139279cc3SChris Mason fail_close:
13826bccf3abSJeff Mahoney 	close_ctree(fs_info);
1383d5719762SChris Mason 	return err;
1384d5719762SChris Mason }
1385d5719762SChris Mason 
13866bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
1387d5719762SChris Mason {
1388d5719762SChris Mason 	struct btrfs_trans_handle *trans;
1389815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1390815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
1391df2ce34cSChris Mason 
1392bc074524SJeff Mahoney 	trace_btrfs_sync_fs(fs_info, wait);
13931abe9b8aSliubo 
1394d561c025SChris Mason 	if (!wait) {
1395815745cfSAl Viro 		filemap_flush(fs_info->btree_inode->i_mapping);
1396df2ce34cSChris Mason 		return 0;
1397d561c025SChris Mason 	}
1398771ed689SChris Mason 
13996374e57aSChris Mason 	btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
1400771ed689SChris Mason 
1401d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
140260376ce4SJosef Bacik 	if (IS_ERR(trans)) {
1403354aa0fbSMiao Xie 		/* no transaction, don't bother */
14046b5fe46dSDavid Sterba 		if (PTR_ERR(trans) == -ENOENT) {
14056b5fe46dSDavid Sterba 			/*
14066b5fe46dSDavid Sterba 			 * Exit unless we have some pending changes
14076b5fe46dSDavid Sterba 			 * that need to go through commit
14086b5fe46dSDavid Sterba 			 */
14096b5fe46dSDavid Sterba 			if (fs_info->pending_changes == 0)
1410bd7de2c9SJosef Bacik 				return 0;
1411a53f4f8eSQu Wenruo 			/*
1412a53f4f8eSQu Wenruo 			 * A non-blocking test if the fs is frozen. We must not
1413a53f4f8eSQu Wenruo 			 * start a new transaction here otherwise a deadlock
1414a53f4f8eSQu Wenruo 			 * happens. The pending operations are delayed to the
1415a53f4f8eSQu Wenruo 			 * next commit after thawing.
1416a53f4f8eSQu Wenruo 			 */
1417a7e3c5f2SRakesh Pandit 			if (sb_start_write_trylock(sb))
1418a7e3c5f2SRakesh Pandit 				sb_end_write(sb);
1419a53f4f8eSQu Wenruo 			else
1420a53f4f8eSQu Wenruo 				return 0;
14216b5fe46dSDavid Sterba 			trans = btrfs_start_transaction(root, 0);
142260376ce4SJosef Bacik 		}
142398bd5c54SDavid Sterba 		if (IS_ERR(trans))
142498bd5c54SDavid Sterba 			return PTR_ERR(trans);
14256b5fe46dSDavid Sterba 	}
14263a45bb20SJeff Mahoney 	return btrfs_commit_transaction(trans);
1427d5719762SChris Mason }
1428d5719762SChris Mason 
1429ab0b4a3eSJosef Bacik static void print_rescue_option(struct seq_file *seq, const char *s, bool *printed)
1430ab0b4a3eSJosef Bacik {
1431ab0b4a3eSJosef Bacik 	seq_printf(seq, "%s%s", (*printed) ? ":" : ",rescue=", s);
1432ab0b4a3eSJosef Bacik 	*printed = true;
1433ab0b4a3eSJosef Bacik }
1434ab0b4a3eSJosef Bacik 
143534c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
1436a9572a15SEric Paris {
1437815745cfSAl Viro 	struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
14380f628c63SDavid Sterba 	const char *compress_type;
14393ef3959bSJosef Bacik 	const char *subvol_name;
1440ab0b4a3eSJosef Bacik 	bool printed = false;
1441a9572a15SEric Paris 
14423cdde224SJeff Mahoney 	if (btrfs_test_opt(info, DEGRADED))
1443a9572a15SEric Paris 		seq_puts(seq, ",degraded");
14443cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NODATASUM))
1445a9572a15SEric Paris 		seq_puts(seq, ",nodatasum");
14463cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NODATACOW))
1447a9572a15SEric Paris 		seq_puts(seq, ",nodatacow");
14483cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOBARRIER))
1449a9572a15SEric Paris 		seq_puts(seq, ",nobarrier");
145095ac567aSFilipe David Borba Manana 	if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
1451c1c9ff7cSGeert Uytterhoeven 		seq_printf(seq, ",max_inline=%llu", info->max_inline);
1452a9572a15SEric Paris 	if (info->thread_pool_size !=  min_t(unsigned long,
1453a9572a15SEric Paris 					     num_online_cpus() + 2, 8))
1454f7b885beSAnand Jain 		seq_printf(seq, ",thread_pool=%u", info->thread_pool_size);
14553cdde224SJeff Mahoney 	if (btrfs_test_opt(info, COMPRESS)) {
14560f628c63SDavid Sterba 		compress_type = btrfs_compress_type2str(info->compress_type);
14573cdde224SJeff Mahoney 		if (btrfs_test_opt(info, FORCE_COMPRESS))
1458200da64eSTsutomu Itoh 			seq_printf(seq, ",compress-force=%s", compress_type);
1459200da64eSTsutomu Itoh 		else
1460200da64eSTsutomu Itoh 			seq_printf(seq, ",compress=%s", compress_type);
1461f51d2b59SDavid Sterba 		if (info->compress_level)
1462fa4d885aSAdam Borowski 			seq_printf(seq, ":%d", info->compress_level);
1463200da64eSTsutomu Itoh 	}
14643cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOSSD))
1465c289811cSChris Mason 		seq_puts(seq, ",nossd");
14663cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SSD_SPREAD))
1467451d7585SChris Mason 		seq_puts(seq, ",ssd_spread");
14683cdde224SJeff Mahoney 	else if (btrfs_test_opt(info, SSD))
1469a9572a15SEric Paris 		seq_puts(seq, ",ssd");
14703cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOTREELOG))
14716b65c5c6SSage Weil 		seq_puts(seq, ",notreelog");
14723cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOLOGREPLAY))
1473ab0b4a3eSJosef Bacik 		print_rescue_option(seq, "nologreplay", &printed);
147468319c18SJosef Bacik 	if (btrfs_test_opt(info, USEBACKUPROOT))
147568319c18SJosef Bacik 		print_rescue_option(seq, "usebackuproot", &printed);
147642437a63SJosef Bacik 	if (btrfs_test_opt(info, IGNOREBADROOTS))
147742437a63SJosef Bacik 		print_rescue_option(seq, "ignorebadroots", &printed);
1478882dbe0cSJosef Bacik 	if (btrfs_test_opt(info, IGNOREDATACSUMS))
1479882dbe0cSJosef Bacik 		print_rescue_option(seq, "ignoredatacsums", &printed);
14803cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FLUSHONCOMMIT))
14816b65c5c6SSage Weil 		seq_puts(seq, ",flushoncommit");
148246b27f50SDennis Zhou 	if (btrfs_test_opt(info, DISCARD_SYNC))
148320a5239aSMatthew Wilcox 		seq_puts(seq, ",discard");
1484b0643e59SDennis Zhou 	if (btrfs_test_opt(info, DISCARD_ASYNC))
1485b0643e59SDennis Zhou 		seq_puts(seq, ",discard=async");
14861751e8a6SLinus Torvalds 	if (!(info->sb->s_flags & SB_POSIXACL))
1487a9572a15SEric Paris 		seq_puts(seq, ",noacl");
14883cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SPACE_CACHE))
1489200da64eSTsutomu Itoh 		seq_puts(seq, ",space_cache");
14903cdde224SJeff Mahoney 	else if (btrfs_test_opt(info, FREE_SPACE_TREE))
149170f6d82eSOmar Sandoval 		seq_puts(seq, ",space_cache=v2");
149273bc1876SJosef Bacik 	else
14938965593eSDavid Sterba 		seq_puts(seq, ",nospace_cache");
14943cdde224SJeff Mahoney 	if (btrfs_test_opt(info, RESCAN_UUID_TREE))
1495f420ee1eSStefan Behrens 		seq_puts(seq, ",rescan_uuid_tree");
14963cdde224SJeff Mahoney 	if (btrfs_test_opt(info, CLEAR_CACHE))
1497200da64eSTsutomu Itoh 		seq_puts(seq, ",clear_cache");
14983cdde224SJeff Mahoney 	if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED))
1499200da64eSTsutomu Itoh 		seq_puts(seq, ",user_subvol_rm_allowed");
15003cdde224SJeff Mahoney 	if (btrfs_test_opt(info, ENOSPC_DEBUG))
15010942caa3SDavid Sterba 		seq_puts(seq, ",enospc_debug");
15023cdde224SJeff Mahoney 	if (btrfs_test_opt(info, AUTO_DEFRAG))
15030942caa3SDavid Sterba 		seq_puts(seq, ",autodefrag");
15043cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SKIP_BALANCE))
15059555c6c1SIlya Dryomov 		seq_puts(seq, ",skip_balance");
15068507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
15073cdde224SJeff Mahoney 	if (btrfs_test_opt(info, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA))
15088507d216SWang Shilong 		seq_puts(seq, ",check_int_data");
15093cdde224SJeff Mahoney 	else if (btrfs_test_opt(info, CHECK_INTEGRITY))
15108507d216SWang Shilong 		seq_puts(seq, ",check_int");
15118507d216SWang Shilong 	if (info->check_integrity_print_mask)
15128507d216SWang Shilong 		seq_printf(seq, ",check_int_print_mask=%d",
15138507d216SWang Shilong 				info->check_integrity_print_mask);
15148507d216SWang Shilong #endif
15158507d216SWang Shilong 	if (info->metadata_ratio)
1516764cb8b4SAnand Jain 		seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio);
15173cdde224SJeff Mahoney 	if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR))
15188c342930SJeff Mahoney 		seq_puts(seq, ",fatal_errors=panic");
15198b87dc17SDavid Sterba 	if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
1520d3740608SAnand Jain 		seq_printf(seq, ",commit=%u", info->commit_interval);
1521d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
15223cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FRAGMENT_DATA))
1523d0bd4560SJosef Bacik 		seq_puts(seq, ",fragment=data");
15243cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FRAGMENT_METADATA))
1525d0bd4560SJosef Bacik 		seq_puts(seq, ",fragment=metadata");
1526d0bd4560SJosef Bacik #endif
1527fb592373SJosef Bacik 	if (btrfs_test_opt(info, REF_VERIFY))
1528fb592373SJosef Bacik 		seq_puts(seq, ",ref_verify");
1529c8d3fe02SOmar Sandoval 	seq_printf(seq, ",subvolid=%llu",
1530c8d3fe02SOmar Sandoval 		  BTRFS_I(d_inode(dentry))->root->root_key.objectid);
15313ef3959bSJosef Bacik 	subvol_name = btrfs_get_subvol_name_from_objectid(info,
15323ef3959bSJosef Bacik 			BTRFS_I(d_inode(dentry))->root->root_key.objectid);
15333ef3959bSJosef Bacik 	if (!IS_ERR(subvol_name)) {
1534c8d3fe02SOmar Sandoval 		seq_puts(seq, ",subvol=");
15353ef3959bSJosef Bacik 		seq_escape(seq, subvol_name, " \t\n\\");
15363ef3959bSJosef Bacik 		kfree(subvol_name);
15373ef3959bSJosef Bacik 	}
1538a9572a15SEric Paris 	return 0;
1539a9572a15SEric Paris }
1540a9572a15SEric Paris 
1541a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data)
15422e635a27SChris Mason {
1543815745cfSAl Viro 	struct btrfs_fs_info *p = data;
1544815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(s);
15454b82d6e4SYan 
1546815745cfSAl Viro 	return fs_info->fs_devices == p->fs_devices;
15474b82d6e4SYan }
15484b82d6e4SYan 
1549450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data)
1550450ba0eaSJosef Bacik {
15516de1d09dSAl Viro 	int err = set_anon_super(s, data);
15526de1d09dSAl Viro 	if (!err)
1553450ba0eaSJosef Bacik 		s->s_fs_info = data;
15546de1d09dSAl Viro 	return err;
1555450ba0eaSJosef Bacik }
1556450ba0eaSJosef Bacik 
1557830c4adbSJosef Bacik /*
1558f9d9ef62SDavid Sterba  * subvolumes are identified by ino 256
1559f9d9ef62SDavid Sterba  */
1560f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode)
1561f9d9ef62SDavid Sterba {
1562f9d9ef62SDavid Sterba 	if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
1563f9d9ef62SDavid Sterba 		return 1;
1564f9d9ef62SDavid Sterba 	return 0;
1565f9d9ef62SDavid Sterba }
1566f9d9ef62SDavid Sterba 
1567bb289b7bSOmar Sandoval static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
1568ae0bc863SAnand Jain 				   struct vfsmount *mnt)
1569830c4adbSJosef Bacik {
1570830c4adbSJosef Bacik 	struct dentry *root;
1571fa330659SOmar Sandoval 	int ret;
1572830c4adbSJosef Bacik 
157305dbe683SOmar Sandoval 	if (!subvol_name) {
157405dbe683SOmar Sandoval 		if (!subvol_objectid) {
157505dbe683SOmar Sandoval 			ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
157605dbe683SOmar Sandoval 							  &subvol_objectid);
157705dbe683SOmar Sandoval 			if (ret) {
157805dbe683SOmar Sandoval 				root = ERR_PTR(ret);
157905dbe683SOmar Sandoval 				goto out;
158005dbe683SOmar Sandoval 			}
158105dbe683SOmar Sandoval 		}
1582c0c907a4SMarcos Paulo de Souza 		subvol_name = btrfs_get_subvol_name_from_objectid(
1583c0c907a4SMarcos Paulo de Souza 					btrfs_sb(mnt->mnt_sb), subvol_objectid);
158405dbe683SOmar Sandoval 		if (IS_ERR(subvol_name)) {
158505dbe683SOmar Sandoval 			root = ERR_CAST(subvol_name);
158605dbe683SOmar Sandoval 			subvol_name = NULL;
158705dbe683SOmar Sandoval 			goto out;
158805dbe683SOmar Sandoval 		}
158905dbe683SOmar Sandoval 
159005dbe683SOmar Sandoval 	}
159105dbe683SOmar Sandoval 
1592ea441d11SAl Viro 	root = mount_subtree(mnt, subvol_name);
1593fa330659SOmar Sandoval 	/* mount_subtree() drops our reference on the vfsmount. */
1594fa330659SOmar Sandoval 	mnt = NULL;
1595830c4adbSJosef Bacik 
1596bb289b7bSOmar Sandoval 	if (!IS_ERR(root)) {
1597ea441d11SAl Viro 		struct super_block *s = root->d_sb;
1598ab8d0fc4SJeff Mahoney 		struct btrfs_fs_info *fs_info = btrfs_sb(s);
1599bb289b7bSOmar Sandoval 		struct inode *root_inode = d_inode(root);
1600bb289b7bSOmar Sandoval 		u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid;
1601bb289b7bSOmar Sandoval 
1602bb289b7bSOmar Sandoval 		ret = 0;
1603bb289b7bSOmar Sandoval 		if (!is_subvolume_inode(root_inode)) {
1604ab8d0fc4SJeff Mahoney 			btrfs_err(fs_info, "'%s' is not a valid subvolume",
1605bb289b7bSOmar Sandoval 			       subvol_name);
1606bb289b7bSOmar Sandoval 			ret = -EINVAL;
1607bb289b7bSOmar Sandoval 		}
1608bb289b7bSOmar Sandoval 		if (subvol_objectid && root_objectid != subvol_objectid) {
160905dbe683SOmar Sandoval 			/*
161005dbe683SOmar Sandoval 			 * This will also catch a race condition where a
161105dbe683SOmar Sandoval 			 * subvolume which was passed by ID is renamed and
161205dbe683SOmar Sandoval 			 * another subvolume is renamed over the old location.
161305dbe683SOmar Sandoval 			 */
1614ab8d0fc4SJeff Mahoney 			btrfs_err(fs_info,
1615ab8d0fc4SJeff Mahoney 				  "subvol '%s' does not match subvolid %llu",
1616bb289b7bSOmar Sandoval 				  subvol_name, subvol_objectid);
1617bb289b7bSOmar Sandoval 			ret = -EINVAL;
1618bb289b7bSOmar Sandoval 		}
1619bb289b7bSOmar Sandoval 		if (ret) {
1620ea441d11SAl Viro 			dput(root);
1621bb289b7bSOmar Sandoval 			root = ERR_PTR(ret);
1622ea441d11SAl Viro 			deactivate_locked_super(s);
1623bb289b7bSOmar Sandoval 		}
1624f9d9ef62SDavid Sterba 	}
1625f9d9ef62SDavid Sterba 
1626fa330659SOmar Sandoval out:
1627fa330659SOmar Sandoval 	mntput(mnt);
1628fa330659SOmar Sandoval 	kfree(subvol_name);
1629830c4adbSJosef Bacik 	return root;
1630830c4adbSJosef Bacik }
1631450ba0eaSJosef Bacik 
1632312c89fbSMisono, Tomohiro /*
1633312c89fbSMisono, Tomohiro  * Find a superblock for the given device / mount point.
1634312c89fbSMisono, Tomohiro  *
1635312c89fbSMisono, Tomohiro  * Note: This is based on mount_bdev from fs/super.c with a few additions
1636312c89fbSMisono, Tomohiro  *       for multiple device setup.  Make sure to keep it in sync.
1637312c89fbSMisono, Tomohiro  */
163872fa39f5SMisono, Tomohiro static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,
163972fa39f5SMisono, Tomohiro 		int flags, const char *device_name, void *data)
164072fa39f5SMisono, Tomohiro {
164172fa39f5SMisono, Tomohiro 	struct block_device *bdev = NULL;
164272fa39f5SMisono, Tomohiro 	struct super_block *s;
164336350e95SGu Jinxiang 	struct btrfs_device *device = NULL;
164472fa39f5SMisono, Tomohiro 	struct btrfs_fs_devices *fs_devices = NULL;
164572fa39f5SMisono, Tomohiro 	struct btrfs_fs_info *fs_info = NULL;
1646204cc0ccSAl Viro 	void *new_sec_opts = NULL;
164772fa39f5SMisono, Tomohiro 	fmode_t mode = FMODE_READ;
164872fa39f5SMisono, Tomohiro 	int error = 0;
164972fa39f5SMisono, Tomohiro 
165072fa39f5SMisono, Tomohiro 	if (!(flags & SB_RDONLY))
165172fa39f5SMisono, Tomohiro 		mode |= FMODE_WRITE;
165272fa39f5SMisono, Tomohiro 
165372fa39f5SMisono, Tomohiro 	if (data) {
1654a65001e8SAl Viro 		error = security_sb_eat_lsm_opts(data, &new_sec_opts);
165572fa39f5SMisono, Tomohiro 		if (error)
165672fa39f5SMisono, Tomohiro 			return ERR_PTR(error);
165772fa39f5SMisono, Tomohiro 	}
165872fa39f5SMisono, Tomohiro 
165972fa39f5SMisono, Tomohiro 	/*
166072fa39f5SMisono, Tomohiro 	 * Setup a dummy root and fs_info for test/set super.  This is because
166172fa39f5SMisono, Tomohiro 	 * we don't actually fill this stuff out until open_ctree, but we need
16628260edbaSJosef Bacik 	 * then open_ctree will properly initialize the file system specific
16638260edbaSJosef Bacik 	 * settings later.  btrfs_init_fs_info initializes the static elements
16648260edbaSJosef Bacik 	 * of the fs_info (locks and such) to make cleanup easier if we find a
16658260edbaSJosef Bacik 	 * superblock with our given fs_devices later on at sget() time.
166672fa39f5SMisono, Tomohiro 	 */
1667a8fd1f71SJeff Mahoney 	fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL);
166872fa39f5SMisono, Tomohiro 	if (!fs_info) {
166972fa39f5SMisono, Tomohiro 		error = -ENOMEM;
167072fa39f5SMisono, Tomohiro 		goto error_sec_opts;
167172fa39f5SMisono, Tomohiro 	}
16728260edbaSJosef Bacik 	btrfs_init_fs_info(fs_info);
167372fa39f5SMisono, Tomohiro 
167472fa39f5SMisono, Tomohiro 	fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
167572fa39f5SMisono, Tomohiro 	fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
167672fa39f5SMisono, Tomohiro 	if (!fs_info->super_copy || !fs_info->super_for_commit) {
167772fa39f5SMisono, Tomohiro 		error = -ENOMEM;
167872fa39f5SMisono, Tomohiro 		goto error_fs_info;
167972fa39f5SMisono, Tomohiro 	}
168072fa39f5SMisono, Tomohiro 
1681f5194e34SDavid Sterba 	mutex_lock(&uuid_mutex);
1682fa59f27cSAnand Jain 	error = btrfs_parse_device_options(data, mode, fs_type);
168381ffd56bSDavid Sterba 	if (error) {
1684399f7f4cSDavid Sterba 		mutex_unlock(&uuid_mutex);
1685399f7f4cSDavid Sterba 		goto error_fs_info;
168681ffd56bSDavid Sterba 	}
1687399f7f4cSDavid Sterba 
168836350e95SGu Jinxiang 	device = btrfs_scan_one_device(device_name, mode, fs_type);
168936350e95SGu Jinxiang 	if (IS_ERR(device)) {
1690399f7f4cSDavid Sterba 		mutex_unlock(&uuid_mutex);
169136350e95SGu Jinxiang 		error = PTR_ERR(device);
1692399f7f4cSDavid Sterba 		goto error_fs_info;
169381ffd56bSDavid Sterba 	}
1694399f7f4cSDavid Sterba 
169536350e95SGu Jinxiang 	fs_devices = device->fs_devices;
1696399f7f4cSDavid Sterba 	fs_info->fs_devices = fs_devices;
1697399f7f4cSDavid Sterba 
169872fa39f5SMisono, Tomohiro 	error = btrfs_open_devices(fs_devices, mode, fs_type);
1699f5194e34SDavid Sterba 	mutex_unlock(&uuid_mutex);
170072fa39f5SMisono, Tomohiro 	if (error)
170172fa39f5SMisono, Tomohiro 		goto error_fs_info;
170272fa39f5SMisono, Tomohiro 
170372fa39f5SMisono, Tomohiro 	if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) {
170472fa39f5SMisono, Tomohiro 		error = -EACCES;
170572fa39f5SMisono, Tomohiro 		goto error_close_devices;
170672fa39f5SMisono, Tomohiro 	}
170772fa39f5SMisono, Tomohiro 
170872fa39f5SMisono, Tomohiro 	bdev = fs_devices->latest_bdev;
170972fa39f5SMisono, Tomohiro 	s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC,
171072fa39f5SMisono, Tomohiro 		 fs_info);
171172fa39f5SMisono, Tomohiro 	if (IS_ERR(s)) {
171272fa39f5SMisono, Tomohiro 		error = PTR_ERR(s);
171372fa39f5SMisono, Tomohiro 		goto error_close_devices;
171472fa39f5SMisono, Tomohiro 	}
171572fa39f5SMisono, Tomohiro 
171672fa39f5SMisono, Tomohiro 	if (s->s_root) {
171772fa39f5SMisono, Tomohiro 		btrfs_close_devices(fs_devices);
17180d4b0463SJosef Bacik 		btrfs_free_fs_info(fs_info);
171972fa39f5SMisono, Tomohiro 		if ((flags ^ s->s_flags) & SB_RDONLY)
172072fa39f5SMisono, Tomohiro 			error = -EBUSY;
172172fa39f5SMisono, Tomohiro 	} else {
172272fa39f5SMisono, Tomohiro 		snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
172372fa39f5SMisono, Tomohiro 		btrfs_sb(s)->bdev_holder = fs_type;
17249b4e675aSDavid Sterba 		if (!strstr(crc32c_impl(), "generic"))
17259b4e675aSDavid Sterba 			set_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags);
172672fa39f5SMisono, Tomohiro 		error = btrfs_fill_super(s, fs_devices, data);
172772fa39f5SMisono, Tomohiro 	}
1728a65001e8SAl Viro 	if (!error)
1729204cc0ccSAl Viro 		error = security_sb_set_mnt_opts(s, new_sec_opts, 0, NULL);
1730a65001e8SAl Viro 	security_free_mnt_opts(&new_sec_opts);
173172fa39f5SMisono, Tomohiro 	if (error) {
173272fa39f5SMisono, Tomohiro 		deactivate_locked_super(s);
1733a65001e8SAl Viro 		return ERR_PTR(error);
173472fa39f5SMisono, Tomohiro 	}
173572fa39f5SMisono, Tomohiro 
173672fa39f5SMisono, Tomohiro 	return dget(s->s_root);
173772fa39f5SMisono, Tomohiro 
173872fa39f5SMisono, Tomohiro error_close_devices:
173972fa39f5SMisono, Tomohiro 	btrfs_close_devices(fs_devices);
174072fa39f5SMisono, Tomohiro error_fs_info:
17410d4b0463SJosef Bacik 	btrfs_free_fs_info(fs_info);
174272fa39f5SMisono, Tomohiro error_sec_opts:
174372fa39f5SMisono, Tomohiro 	security_free_mnt_opts(&new_sec_opts);
174472fa39f5SMisono, Tomohiro 	return ERR_PTR(error);
174572fa39f5SMisono, Tomohiro }
1746312c89fbSMisono, Tomohiro 
1747edf24abeSChristoph Hellwig /*
1748312c89fbSMisono, Tomohiro  * Mount function which is called by VFS layer.
1749edf24abeSChristoph Hellwig  *
1750312c89fbSMisono, Tomohiro  * In order to allow mounting a subvolume directly, btrfs uses mount_subtree()
1751312c89fbSMisono, Tomohiro  * which needs vfsmount* of device's root (/).  This means device's root has to
1752312c89fbSMisono, Tomohiro  * be mounted internally in any case.
1753312c89fbSMisono, Tomohiro  *
1754312c89fbSMisono, Tomohiro  * Operation flow:
1755312c89fbSMisono, Tomohiro  *   1. Parse subvol id related options for later use in mount_subvol().
1756312c89fbSMisono, Tomohiro  *
1757312c89fbSMisono, Tomohiro  *   2. Mount device's root (/) by calling vfs_kern_mount().
1758312c89fbSMisono, Tomohiro  *
1759312c89fbSMisono, Tomohiro  *      NOTE: vfs_kern_mount() is used by VFS to call btrfs_mount() in the
1760312c89fbSMisono, Tomohiro  *      first place. In order to avoid calling btrfs_mount() again, we use
1761312c89fbSMisono, Tomohiro  *      different file_system_type which is not registered to VFS by
1762312c89fbSMisono, Tomohiro  *      register_filesystem() (btrfs_root_fs_type). As a result,
1763312c89fbSMisono, Tomohiro  *      btrfs_mount_root() is called. The return value will be used by
1764312c89fbSMisono, Tomohiro  *      mount_subtree() in mount_subvol().
1765312c89fbSMisono, Tomohiro  *
1766312c89fbSMisono, Tomohiro  *   3. Call mount_subvol() to get the dentry of subvolume. Since there is
1767312c89fbSMisono, Tomohiro  *      "btrfs subvolume set-default", mount_subvol() is called always.
1768edf24abeSChristoph Hellwig  */
1769061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
1770306e16ceSDavid Sterba 		const char *device_name, void *data)
17714b82d6e4SYan {
1772312c89fbSMisono, Tomohiro 	struct vfsmount *mnt_root;
1773312c89fbSMisono, Tomohiro 	struct dentry *root;
177473f73415SJosef Bacik 	char *subvol_name = NULL;
177573f73415SJosef Bacik 	u64 subvol_objectid = 0;
17764b82d6e4SYan 	int error = 0;
17774b82d6e4SYan 
177893b9bcdfSGu Jinxiang 	error = btrfs_parse_subvol_options(data, &subvol_name,
177993b9bcdfSGu Jinxiang 					&subvol_objectid);
1780f23c8af8SIlya Dryomov 	if (error) {
1781f23c8af8SIlya Dryomov 		kfree(subvol_name);
1782061dbc6bSAl Viro 		return ERR_PTR(error);
1783f23c8af8SIlya Dryomov 	}
1784edf24abeSChristoph Hellwig 
1785312c89fbSMisono, Tomohiro 	/* mount device's root (/) */
1786312c89fbSMisono, Tomohiro 	mnt_root = vfs_kern_mount(&btrfs_root_fs_type, flags, device_name, data);
1787312c89fbSMisono, Tomohiro 	if (PTR_ERR_OR_ZERO(mnt_root) == -EBUSY) {
1788312c89fbSMisono, Tomohiro 		if (flags & SB_RDONLY) {
1789312c89fbSMisono, Tomohiro 			mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1790312c89fbSMisono, Tomohiro 				flags & ~SB_RDONLY, device_name, data);
17914b82d6e4SYan 		} else {
1792312c89fbSMisono, Tomohiro 			mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1793312c89fbSMisono, Tomohiro 				flags | SB_RDONLY, device_name, data);
1794312c89fbSMisono, Tomohiro 			if (IS_ERR(mnt_root)) {
1795312c89fbSMisono, Tomohiro 				root = ERR_CAST(mnt_root);
1796532b618bSEric W. Biederman 				kfree(subvol_name);
1797312c89fbSMisono, Tomohiro 				goto out;
1798f667aef6SQu Wenruo 			}
1799f667aef6SQu Wenruo 
1800312c89fbSMisono, Tomohiro 			down_write(&mnt_root->mnt_sb->s_umount);
1801312c89fbSMisono, Tomohiro 			error = btrfs_remount(mnt_root->mnt_sb, &flags, NULL);
1802312c89fbSMisono, Tomohiro 			up_write(&mnt_root->mnt_sb->s_umount);
1803312c89fbSMisono, Tomohiro 			if (error < 0) {
1804312c89fbSMisono, Tomohiro 				root = ERR_PTR(error);
1805312c89fbSMisono, Tomohiro 				mntput(mnt_root);
1806532b618bSEric W. Biederman 				kfree(subvol_name);
1807312c89fbSMisono, Tomohiro 				goto out;
1808312c89fbSMisono, Tomohiro 			}
1809312c89fbSMisono, Tomohiro 		}
1810312c89fbSMisono, Tomohiro 	}
1811312c89fbSMisono, Tomohiro 	if (IS_ERR(mnt_root)) {
1812312c89fbSMisono, Tomohiro 		root = ERR_CAST(mnt_root);
1813532b618bSEric W. Biederman 		kfree(subvol_name);
1814312c89fbSMisono, Tomohiro 		goto out;
1815f667aef6SQu Wenruo 	}
18164b82d6e4SYan 
1817312c89fbSMisono, Tomohiro 	/* mount_subvol() will free subvol_name and mnt_root */
1818ae0bc863SAnand Jain 	root = mount_subvol(subvol_name, subvol_objectid, mnt_root);
18194b82d6e4SYan 
1820312c89fbSMisono, Tomohiro out:
1821312c89fbSMisono, Tomohiro 	return root;
18224b82d6e4SYan }
18232e635a27SChris Mason 
18240d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
1825f7b885beSAnand Jain 				     u32 new_pool_size, u32 old_pool_size)
18260d2450abSSergei Trofimovich {
18270d2450abSSergei Trofimovich 	if (new_pool_size == old_pool_size)
18280d2450abSSergei Trofimovich 		return;
18290d2450abSSergei Trofimovich 
18300d2450abSSergei Trofimovich 	fs_info->thread_pool_size = new_pool_size;
18310d2450abSSergei Trofimovich 
1832efe120a0SFrank Holton 	btrfs_info(fs_info, "resize thread pool %d -> %d",
18330d2450abSSergei Trofimovich 	       old_pool_size, new_pool_size);
18340d2450abSSergei Trofimovich 
18355cdc7ad3SQu Wenruo 	btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
1836afe3d242SQu Wenruo 	btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
1837e66f0bb1SQu Wenruo 	btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
1838fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size);
1839fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size);
1840fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_meta_write_workers,
1841fccb5d86SQu Wenruo 				new_pool_size);
1842fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
1843fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
18445b3bc44eSQu Wenruo 	btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
1845736cfa15SQu Wenruo 	btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size);
18460339ef2fSQu Wenruo 	btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers,
1847ff023aacSStefan Behrens 				new_pool_size);
18480d2450abSSergei Trofimovich }
18490d2450abSSergei Trofimovich 
1850f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1851f42a34b2SMiao Xie 				       unsigned long old_opts, int flags)
1852f42a34b2SMiao Xie {
1853dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1854dc81cdc5SMiao Xie 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
18551751e8a6SLinus Torvalds 	     (flags & SB_RDONLY))) {
1856dc81cdc5SMiao Xie 		/* wait for any defraggers to finish */
1857dc81cdc5SMiao Xie 		wait_event(fs_info->transaction_wait,
1858dc81cdc5SMiao Xie 			   (atomic_read(&fs_info->defrag_running) == 0));
18591751e8a6SLinus Torvalds 		if (flags & SB_RDONLY)
1860dc81cdc5SMiao Xie 			sync_filesystem(fs_info->sb);
1861dc81cdc5SMiao Xie 	}
1862dc81cdc5SMiao Xie }
1863dc81cdc5SMiao Xie 
1864dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1865dc81cdc5SMiao Xie 					 unsigned long old_opts)
1866dc81cdc5SMiao Xie {
1867dc81cdc5SMiao Xie 	/*
1868180e4d47SLuis de Bethencourt 	 * We need to cleanup all defragable inodes if the autodefragment is
1869180e4d47SLuis de Bethencourt 	 * close or the filesystem is read only.
1870dc81cdc5SMiao Xie 	 */
1871dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1872bc98a42cSDavid Howells 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) {
1873dc81cdc5SMiao Xie 		btrfs_cleanup_defrag_inodes(fs_info);
1874dc81cdc5SMiao Xie 	}
1875dc81cdc5SMiao Xie 
1876b0643e59SDennis Zhou 	/* If we toggled discard async */
1877b0643e59SDennis Zhou 	if (!btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1878b0643e59SDennis Zhou 	    btrfs_test_opt(fs_info, DISCARD_ASYNC))
1879b0643e59SDennis Zhou 		btrfs_discard_resume(fs_info);
1880b0643e59SDennis Zhou 	else if (btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1881b0643e59SDennis Zhou 		 !btrfs_test_opt(fs_info, DISCARD_ASYNC))
1882b0643e59SDennis Zhou 		btrfs_discard_cleanup(fs_info);
1883dc81cdc5SMiao Xie }
1884dc81cdc5SMiao Xie 
1885c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1886c146afadSYan Zheng {
1887815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
188849b25e05SJeff Mahoney 	unsigned old_flags = sb->s_flags;
188949b25e05SJeff Mahoney 	unsigned long old_opts = fs_info->mount_opt;
189049b25e05SJeff Mahoney 	unsigned long old_compress_type = fs_info->compress_type;
189149b25e05SJeff Mahoney 	u64 old_max_inline = fs_info->max_inline;
1892f7b885beSAnand Jain 	u32 old_thread_pool_size = fs_info->thread_pool_size;
1893d612ac59SAnand Jain 	u32 old_metadata_ratio = fs_info->metadata_ratio;
1894c146afadSYan Zheng 	int ret;
1895c146afadSYan Zheng 
189602b9984dSTheodore Ts'o 	sync_filesystem(sb);
189788c4703fSJohannes Thumshirn 	set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1898dc81cdc5SMiao Xie 
1899f667aef6SQu Wenruo 	if (data) {
1900204cc0ccSAl Viro 		void *new_sec_opts = NULL;
1901f667aef6SQu Wenruo 
1902a65001e8SAl Viro 		ret = security_sb_eat_lsm_opts(data, &new_sec_opts);
1903a65001e8SAl Viro 		if (!ret)
1904204cc0ccSAl Viro 			ret = security_sb_remount(sb, new_sec_opts);
1905a65001e8SAl Viro 		security_free_mnt_opts(&new_sec_opts);
1906f667aef6SQu Wenruo 		if (ret)
1907f667aef6SQu Wenruo 			goto restore;
1908f667aef6SQu Wenruo 	}
1909f667aef6SQu Wenruo 
19102ff7e61eSJeff Mahoney 	ret = btrfs_parse_options(fs_info, data, *flags);
1911891f41cbSChengguang Xu 	if (ret)
191249b25e05SJeff Mahoney 		goto restore;
1913b288052eSChris Mason 
1914f42a34b2SMiao Xie 	btrfs_remount_begin(fs_info, old_opts, *flags);
19150d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
19160d2450abSSergei Trofimovich 		fs_info->thread_pool_size, old_thread_pool_size);
19170d2450abSSergei Trofimovich 
19181751e8a6SLinus Torvalds 	if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
1919dc81cdc5SMiao Xie 		goto out;
1920c146afadSYan Zheng 
19211751e8a6SLinus Torvalds 	if (*flags & SB_RDONLY) {
19228dabb742SStefan Behrens 		/*
19238dabb742SStefan Behrens 		 * this also happens on 'umount -rf' or on shutdown, when
19248dabb742SStefan Behrens 		 * the filesystem is busy.
19258dabb742SStefan Behrens 		 */
192621c7e756SMiao Xie 		cancel_work_sync(&fs_info->async_reclaim_work);
192757056740SJosef Bacik 		cancel_work_sync(&fs_info->async_data_reclaim_work);
1928361c093dSStefan Behrens 
1929b0643e59SDennis Zhou 		btrfs_discard_cleanup(fs_info);
1930b0643e59SDennis Zhou 
1931361c093dSStefan Behrens 		/* wait for the uuid_scan task to finish */
1932361c093dSStefan Behrens 		down(&fs_info->uuid_tree_rescan_sem);
1933361c093dSStefan Behrens 		/* avoid complains from lockdep et al. */
1934361c093dSStefan Behrens 		up(&fs_info->uuid_tree_rescan_sem);
1935361c093dSStefan Behrens 
19361751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
1937c146afadSYan Zheng 
1938e44163e1SJeff Mahoney 		/*
19391751e8a6SLinus Torvalds 		 * Setting SB_RDONLY will put the cleaner thread to
1940e44163e1SJeff Mahoney 		 * sleep at the next loop if it's already active.
1941e44163e1SJeff Mahoney 		 * If it's already asleep, we'll leave unused block
1942e44163e1SJeff Mahoney 		 * groups on disk until we're mounted read-write again
1943e44163e1SJeff Mahoney 		 * unless we clean them up here.
1944e44163e1SJeff Mahoney 		 */
1945e44163e1SJeff Mahoney 		btrfs_delete_unused_bgs(fs_info);
1946e44163e1SJeff Mahoney 
19478dabb742SStefan Behrens 		btrfs_dev_replace_suspend_for_unmount(fs_info);
19488dabb742SStefan Behrens 		btrfs_scrub_cancel(fs_info);
1949061594efSMiao Xie 		btrfs_pause_balance(fs_info);
19508dabb742SStefan Behrens 
19516bccf3abSJeff Mahoney 		ret = btrfs_commit_super(fs_info);
195249b25e05SJeff Mahoney 		if (ret)
195349b25e05SJeff Mahoney 			goto restore;
1954c146afadSYan Zheng 	} else {
19550b246afaSJeff Mahoney 		if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
19566ef3de9cSDavid Sterba 			btrfs_err(fs_info,
1957efe120a0SFrank Holton 				"Remounting read-write after error is not allowed");
19586ef3de9cSDavid Sterba 			ret = -EINVAL;
19596ef3de9cSDavid Sterba 			goto restore;
19606ef3de9cSDavid Sterba 		}
19618a3db184SSergei Trofimovich 		if (fs_info->fs_devices->rw_devices == 0) {
196249b25e05SJeff Mahoney 			ret = -EACCES;
196349b25e05SJeff Mahoney 			goto restore;
19648a3db184SSergei Trofimovich 		}
19652b82032cSYan Zheng 
19666528b99dSAnand Jain 		if (!btrfs_check_rw_degradable(fs_info, NULL)) {
1967efe120a0SFrank Holton 			btrfs_warn(fs_info,
196852042d8eSAndrea Gelmini 		"too many missing devices, writable remount is not allowed");
1969292fd7fcSStefan Behrens 			ret = -EACCES;
1970292fd7fcSStefan Behrens 			goto restore;
1971292fd7fcSStefan Behrens 		}
1972292fd7fcSStefan Behrens 
19738a3db184SSergei Trofimovich 		if (btrfs_super_log_root(fs_info->super_copy) != 0) {
197410a3a3edSDavid Sterba 			btrfs_warn(fs_info,
197510a3a3edSDavid Sterba 		"mount required to replay tree-log, cannot remount read-write");
197649b25e05SJeff Mahoney 			ret = -EINVAL;
197749b25e05SJeff Mahoney 			goto restore;
19788a3db184SSergei Trofimovich 		}
1979c146afadSYan Zheng 
198044c0ca21SBoris Burkov 		/*
198144c0ca21SBoris Burkov 		 * NOTE: when remounting with a change that does writes, don't
198244c0ca21SBoris Burkov 		 * put it anywhere above this point, as we are not sure to be
198344c0ca21SBoris Burkov 		 * safe to write until we pass the above checks.
198444c0ca21SBoris Burkov 		 */
198544c0ca21SBoris Burkov 		ret = btrfs_start_pre_rw_mount(fs_info);
198649b25e05SJeff Mahoney 		if (ret)
198749b25e05SJeff Mahoney 			goto restore;
1988c146afadSYan Zheng 
19891751e8a6SLinus Torvalds 		sb->s_flags &= ~SB_RDONLY;
199090c711abSZygo Blaxell 
1991afcdd129SJosef Bacik 		set_bit(BTRFS_FS_OPEN, &fs_info->flags);
1992c146afadSYan Zheng 	}
1993dc81cdc5SMiao Xie out:
1994faa00889SJosef Bacik 	/*
1995faa00889SJosef Bacik 	 * We need to set SB_I_VERSION here otherwise it'll get cleared by VFS,
1996faa00889SJosef Bacik 	 * since the absence of the flag means it can be toggled off by remount.
1997faa00889SJosef Bacik 	 */
1998faa00889SJosef Bacik 	*flags |= SB_I_VERSION;
1999faa00889SJosef Bacik 
20002c6a92b0SJustin Maggard 	wake_up_process(fs_info->transaction_kthread);
2001dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
2002*8cd29088SBoris Burkov 	btrfs_clear_oneshot_options(fs_info);
200388c4703fSJohannes Thumshirn 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
200488c4703fSJohannes Thumshirn 
2005c146afadSYan Zheng 	return 0;
200649b25e05SJeff Mahoney 
200749b25e05SJeff Mahoney restore:
20081751e8a6SLinus Torvalds 	/* We've hit an error - don't reset SB_RDONLY */
2009bc98a42cSDavid Howells 	if (sb_rdonly(sb))
20101751e8a6SLinus Torvalds 		old_flags |= SB_RDONLY;
201149b25e05SJeff Mahoney 	sb->s_flags = old_flags;
201249b25e05SJeff Mahoney 	fs_info->mount_opt = old_opts;
201349b25e05SJeff Mahoney 	fs_info->compress_type = old_compress_type;
201449b25e05SJeff Mahoney 	fs_info->max_inline = old_max_inline;
20150d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
20160d2450abSSergei Trofimovich 		old_thread_pool_size, fs_info->thread_pool_size);
201749b25e05SJeff Mahoney 	fs_info->metadata_ratio = old_metadata_ratio;
2018dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
201988c4703fSJohannes Thumshirn 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
202088c4703fSJohannes Thumshirn 
202149b25e05SJeff Mahoney 	return ret;
2022c146afadSYan Zheng }
2023c146afadSYan Zheng 
2024bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */
20257e17916bSArnd Bergmann static inline int btrfs_cmp_device_free_bytes(const void *dev_info1,
2026bcd53741SArne Jansen 				       const void *dev_info2)
2027bcd53741SArne Jansen {
2028bcd53741SArne Jansen 	if (((struct btrfs_device_info *)dev_info1)->max_avail >
2029bcd53741SArne Jansen 	    ((struct btrfs_device_info *)dev_info2)->max_avail)
2030bcd53741SArne Jansen 		return -1;
2031bcd53741SArne Jansen 	else if (((struct btrfs_device_info *)dev_info1)->max_avail <
2032bcd53741SArne Jansen 		 ((struct btrfs_device_info *)dev_info2)->max_avail)
2033bcd53741SArne Jansen 		return 1;
2034bcd53741SArne Jansen 	else
2035bcd53741SArne Jansen 	return 0;
2036bcd53741SArne Jansen }
2037bcd53741SArne Jansen 
2038bcd53741SArne Jansen /*
2039bcd53741SArne Jansen  * sort the devices by max_avail, in which max free extent size of each device
2040bcd53741SArne Jansen  * is stored.(Descending Sort)
2041bcd53741SArne Jansen  */
2042bcd53741SArne Jansen static inline void btrfs_descending_sort_devices(
2043bcd53741SArne Jansen 					struct btrfs_device_info *devices,
2044bcd53741SArne Jansen 					size_t nr_devices)
2045bcd53741SArne Jansen {
2046bcd53741SArne Jansen 	sort(devices, nr_devices, sizeof(struct btrfs_device_info),
2047bcd53741SArne Jansen 	     btrfs_cmp_device_free_bytes, NULL);
2048bcd53741SArne Jansen }
2049bcd53741SArne Jansen 
20506d07bcecSMiao Xie /*
20516d07bcecSMiao Xie  * The helper to calc the free space on the devices that can be used to store
20526d07bcecSMiao Xie  * file data.
20536d07bcecSMiao Xie  */
20547e17916bSArnd Bergmann static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
20556bccf3abSJeff Mahoney 					      u64 *free_bytes)
20566d07bcecSMiao Xie {
20576d07bcecSMiao Xie 	struct btrfs_device_info *devices_info;
20586d07bcecSMiao Xie 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
20596d07bcecSMiao Xie 	struct btrfs_device *device;
20606d07bcecSMiao Xie 	u64 type;
20616d07bcecSMiao Xie 	u64 avail_space;
20626d07bcecSMiao Xie 	u64 min_stripe_size;
2063559ca6eaSNikolay Borisov 	int num_stripes = 1;
20646d07bcecSMiao Xie 	int i = 0, nr_devices;
20654f080f57SDavid Sterba 	const struct btrfs_raid_attr *rattr;
20666d07bcecSMiao Xie 
20677e33fd99SJosef Bacik 	/*
206801327610SNicholas D Steeves 	 * We aren't under the device list lock, so this is racy-ish, but good
20697e33fd99SJosef Bacik 	 * enough for our purposes.
20707e33fd99SJosef Bacik 	 */
2071b772a86eSLi Zefan 	nr_devices = fs_info->fs_devices->open_devices;
20727e33fd99SJosef Bacik 	if (!nr_devices) {
20737e33fd99SJosef Bacik 		smp_mb();
20747e33fd99SJosef Bacik 		nr_devices = fs_info->fs_devices->open_devices;
20757e33fd99SJosef Bacik 		ASSERT(nr_devices);
20767e33fd99SJosef Bacik 		if (!nr_devices) {
20777e33fd99SJosef Bacik 			*free_bytes = 0;
20787e33fd99SJosef Bacik 			return 0;
20797e33fd99SJosef Bacik 		}
20807e33fd99SJosef Bacik 	}
20816d07bcecSMiao Xie 
2082d9b0d9baSDulshani Gunawardhana 	devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
20836a44517dSDavid Sterba 			       GFP_KERNEL);
20846d07bcecSMiao Xie 	if (!devices_info)
20856d07bcecSMiao Xie 		return -ENOMEM;
20866d07bcecSMiao Xie 
208701327610SNicholas D Steeves 	/* calc min stripe number for data space allocation */
20881b86826dSJeff Mahoney 	type = btrfs_data_alloc_profile(fs_info);
20894f080f57SDavid Sterba 	rattr = &btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)];
20904f080f57SDavid Sterba 
2091e1ea2beeSDavid Sterba 	if (type & BTRFS_BLOCK_GROUP_RAID0)
209239fb26c3SMiao Xie 		num_stripes = nr_devices;
2093e1ea2beeSDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID1)
209439fb26c3SMiao Xie 		num_stripes = 2;
209547e6f742SDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID1C3)
209647e6f742SDavid Sterba 		num_stripes = 3;
20978d6fac00SDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID1C4)
20988d6fac00SDavid Sterba 		num_stripes = 4;
2099e1ea2beeSDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID10)
210039fb26c3SMiao Xie 		num_stripes = 4;
21016d07bcecSMiao Xie 
21024f080f57SDavid Sterba 	/* Adjust for more than 1 stripe per device */
21034f080f57SDavid Sterba 	min_stripe_size = rattr->dev_stripes * BTRFS_STRIPE_LEN;
21046d07bcecSMiao Xie 
21057e33fd99SJosef Bacik 	rcu_read_lock();
21067e33fd99SJosef Bacik 	list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
2107e12c9621SAnand Jain 		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2108e12c9621SAnand Jain 						&device->dev_state) ||
2109401e29c1SAnand Jain 		    !device->bdev ||
2110401e29c1SAnand Jain 		    test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
21116d07bcecSMiao Xie 			continue;
21126d07bcecSMiao Xie 
21137e33fd99SJosef Bacik 		if (i >= nr_devices)
21147e33fd99SJosef Bacik 			break;
21157e33fd99SJosef Bacik 
21166d07bcecSMiao Xie 		avail_space = device->total_bytes - device->bytes_used;
21176d07bcecSMiao Xie 
21186d07bcecSMiao Xie 		/* align with stripe_len */
2119559ca6eaSNikolay Borisov 		avail_space = rounddown(avail_space, BTRFS_STRIPE_LEN);
21206d07bcecSMiao Xie 
21216d07bcecSMiao Xie 		/*
212201327610SNicholas D Steeves 		 * In order to avoid overwriting the superblock on the drive,
21236d07bcecSMiao Xie 		 * btrfs starts at an offset of at least 1MB when doing chunk
21246d07bcecSMiao Xie 		 * allocation.
2125559ca6eaSNikolay Borisov 		 *
2126559ca6eaSNikolay Borisov 		 * This ensures we have at least min_stripe_size free space
2127559ca6eaSNikolay Borisov 		 * after excluding 1MB.
21286d07bcecSMiao Xie 		 */
2129559ca6eaSNikolay Borisov 		if (avail_space <= SZ_1M + min_stripe_size)
21306d07bcecSMiao Xie 			continue;
21316d07bcecSMiao Xie 
2132559ca6eaSNikolay Borisov 		avail_space -= SZ_1M;
2133559ca6eaSNikolay Borisov 
21346d07bcecSMiao Xie 		devices_info[i].dev = device;
21356d07bcecSMiao Xie 		devices_info[i].max_avail = avail_space;
21366d07bcecSMiao Xie 
21376d07bcecSMiao Xie 		i++;
21386d07bcecSMiao Xie 	}
21397e33fd99SJosef Bacik 	rcu_read_unlock();
21406d07bcecSMiao Xie 
21416d07bcecSMiao Xie 	nr_devices = i;
21426d07bcecSMiao Xie 
21436d07bcecSMiao Xie 	btrfs_descending_sort_devices(devices_info, nr_devices);
21446d07bcecSMiao Xie 
21456d07bcecSMiao Xie 	i = nr_devices - 1;
21466d07bcecSMiao Xie 	avail_space = 0;
2147559ca6eaSNikolay Borisov 	while (nr_devices >= rattr->devs_min) {
2148559ca6eaSNikolay Borisov 		num_stripes = min(num_stripes, nr_devices);
214939fb26c3SMiao Xie 
21506d07bcecSMiao Xie 		if (devices_info[i].max_avail >= min_stripe_size) {
21516d07bcecSMiao Xie 			int j;
21526d07bcecSMiao Xie 			u64 alloc_size;
21536d07bcecSMiao Xie 
215439fb26c3SMiao Xie 			avail_space += devices_info[i].max_avail * num_stripes;
21556d07bcecSMiao Xie 			alloc_size = devices_info[i].max_avail;
215639fb26c3SMiao Xie 			for (j = i + 1 - num_stripes; j <= i; j++)
21576d07bcecSMiao Xie 				devices_info[j].max_avail -= alloc_size;
21586d07bcecSMiao Xie 		}
21596d07bcecSMiao Xie 		i--;
21606d07bcecSMiao Xie 		nr_devices--;
21616d07bcecSMiao Xie 	}
21626d07bcecSMiao Xie 
21636d07bcecSMiao Xie 	kfree(devices_info);
21646d07bcecSMiao Xie 	*free_bytes = avail_space;
21656d07bcecSMiao Xie 	return 0;
21666d07bcecSMiao Xie }
21676d07bcecSMiao Xie 
2168ba7b6e62SDavid Sterba /*
2169ba7b6e62SDavid Sterba  * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
2170ba7b6e62SDavid Sterba  *
2171ba7b6e62SDavid Sterba  * If there's a redundant raid level at DATA block groups, use the respective
2172ba7b6e62SDavid Sterba  * multiplier to scale the sizes.
2173ba7b6e62SDavid Sterba  *
2174ba7b6e62SDavid Sterba  * Unused device space usage is based on simulating the chunk allocator
21750d0c71b3SDavid Sterba  * algorithm that respects the device sizes and order of allocations.  This is
21760d0c71b3SDavid Sterba  * a close approximation of the actual use but there are other factors that may
21770d0c71b3SDavid Sterba  * change the result (like a new metadata chunk).
2178ba7b6e62SDavid Sterba  *
2179ca8a51b3SDavid Sterba  * If metadata is exhausted, f_bavail will be 0.
2180ba7b6e62SDavid Sterba  */
21818fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
21828fd17795SChris Mason {
2183815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
2184815745cfSAl Viro 	struct btrfs_super_block *disk_super = fs_info->super_copy;
2185bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
2186bd4d1088SJosef Bacik 	u64 total_used = 0;
21876d07bcecSMiao Xie 	u64 total_free_data = 0;
2188ca8a51b3SDavid Sterba 	u64 total_free_meta = 0;
2189265fdfa6SDavid Sterba 	u32 bits = fs_info->sectorsize_bits;
2190de37aa51SNikolay Borisov 	__be32 *fsid = (__be32 *)fs_info->fs_devices->fsid;
2191ba7b6e62SDavid Sterba 	unsigned factor = 1;
2192ba7b6e62SDavid Sterba 	struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
21936d07bcecSMiao Xie 	int ret;
2194ca8a51b3SDavid Sterba 	u64 thresh = 0;
2195ae02d1bdSLuis de Bethencourt 	int mixed = 0;
21968fd17795SChris Mason 
219772804905SJosef Bacik 	list_for_each_entry(found, &fs_info->space_info, list) {
21986d07bcecSMiao Xie 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
2199ba7b6e62SDavid Sterba 			int i;
2200ba7b6e62SDavid Sterba 
22016d07bcecSMiao Xie 			total_free_data += found->disk_total - found->disk_used;
22026d07bcecSMiao Xie 			total_free_data -=
22036d07bcecSMiao Xie 				btrfs_account_ro_block_groups_free_space(found);
2204ba7b6e62SDavid Sterba 
2205ba7b6e62SDavid Sterba 			for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
220646df06b8SDavid Sterba 				if (!list_empty(&found->block_groups[i]))
220746df06b8SDavid Sterba 					factor = btrfs_bg_type_to_factor(
220846df06b8SDavid Sterba 						btrfs_raid_array[i].bg_flag);
2209ba7b6e62SDavid Sterba 			}
22106d07bcecSMiao Xie 		}
2211ae02d1bdSLuis de Bethencourt 
2212ae02d1bdSLuis de Bethencourt 		/*
2213ae02d1bdSLuis de Bethencourt 		 * Metadata in mixed block goup profiles are accounted in data
2214ae02d1bdSLuis de Bethencourt 		 */
2215ae02d1bdSLuis de Bethencourt 		if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) {
2216ae02d1bdSLuis de Bethencourt 			if (found->flags & BTRFS_BLOCK_GROUP_DATA)
2217ae02d1bdSLuis de Bethencourt 				mixed = 1;
2218ae02d1bdSLuis de Bethencourt 			else
2219ae02d1bdSLuis de Bethencourt 				total_free_meta += found->disk_total -
2220ae02d1bdSLuis de Bethencourt 					found->disk_used;
2221ae02d1bdSLuis de Bethencourt 		}
22226d07bcecSMiao Xie 
2223b742bb82SYan, Zheng 		total_used += found->disk_used;
222489a55897SJosef Bacik 	}
2225ba7b6e62SDavid Sterba 
2226ba7b6e62SDavid Sterba 	buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
2227ba7b6e62SDavid Sterba 	buf->f_blocks >>= bits;
2228ba7b6e62SDavid Sterba 	buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
2229ba7b6e62SDavid Sterba 
2230ba7b6e62SDavid Sterba 	/* Account global block reserve as used, it's in logical size already */
2231ba7b6e62SDavid Sterba 	spin_lock(&block_rsv->lock);
223241b34accSLuis de Bethencourt 	/* Mixed block groups accounting is not byte-accurate, avoid overflow */
223341b34accSLuis de Bethencourt 	if (buf->f_bfree >= block_rsv->size >> bits)
2234ba7b6e62SDavid Sterba 		buf->f_bfree -= block_rsv->size >> bits;
223541b34accSLuis de Bethencourt 	else
223641b34accSLuis de Bethencourt 		buf->f_bfree = 0;
2237ba7b6e62SDavid Sterba 	spin_unlock(&block_rsv->lock);
2238ba7b6e62SDavid Sterba 
22390d95c1beSDavid Sterba 	buf->f_bavail = div_u64(total_free_data, factor);
22406bccf3abSJeff Mahoney 	ret = btrfs_calc_avail_data_space(fs_info, &total_free_data);
22417e33fd99SJosef Bacik 	if (ret)
22426d07bcecSMiao Xie 		return ret;
2243ba7b6e62SDavid Sterba 	buf->f_bavail += div_u64(total_free_data, factor);
22446d07bcecSMiao Xie 	buf->f_bavail = buf->f_bavail >> bits;
2245d397712bSChris Mason 
2246ca8a51b3SDavid Sterba 	/*
2247ca8a51b3SDavid Sterba 	 * We calculate the remaining metadata space minus global reserve. If
2248ca8a51b3SDavid Sterba 	 * this is (supposedly) smaller than zero, there's no space. But this
2249ca8a51b3SDavid Sterba 	 * does not hold in practice, the exhausted state happens where's still
2250ca8a51b3SDavid Sterba 	 * some positive delta. So we apply some guesswork and compare the
2251ca8a51b3SDavid Sterba 	 * delta to a 4M threshold.  (Practically observed delta was ~2M.)
2252ca8a51b3SDavid Sterba 	 *
2253ca8a51b3SDavid Sterba 	 * We probably cannot calculate the exact threshold value because this
2254ca8a51b3SDavid Sterba 	 * depends on the internal reservations requested by various
2255ca8a51b3SDavid Sterba 	 * operations, so some operations that consume a few metadata will
2256ca8a51b3SDavid Sterba 	 * succeed even if the Avail is zero. But this is better than the other
2257ca8a51b3SDavid Sterba 	 * way around.
2258ca8a51b3SDavid Sterba 	 */
2259d4417e22SNikolay Borisov 	thresh = SZ_4M;
2260ca8a51b3SDavid Sterba 
2261d55966c4SJosef Bacik 	/*
2262d55966c4SJosef Bacik 	 * We only want to claim there's no available space if we can no longer
2263d55966c4SJosef Bacik 	 * allocate chunks for our metadata profile and our global reserve will
2264d55966c4SJosef Bacik 	 * not fit in the free metadata space.  If we aren't ->full then we
2265d55966c4SJosef Bacik 	 * still can allocate chunks and thus are fine using the currently
2266d55966c4SJosef Bacik 	 * calculated f_bavail.
2267d55966c4SJosef Bacik 	 */
2268d55966c4SJosef Bacik 	if (!mixed && block_rsv->space_info->full &&
2269d55966c4SJosef Bacik 	    total_free_meta - thresh < block_rsv->size)
2270ca8a51b3SDavid Sterba 		buf->f_bavail = 0;
2271ca8a51b3SDavid Sterba 
2272ba7b6e62SDavid Sterba 	buf->f_type = BTRFS_SUPER_MAGIC;
2273ba7b6e62SDavid Sterba 	buf->f_bsize = dentry->d_sb->s_blocksize;
2274ba7b6e62SDavid Sterba 	buf->f_namelen = BTRFS_NAME_LEN;
2275ba7b6e62SDavid Sterba 
22769d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
22779d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
22789d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
22799d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
22809d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
228132d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
22824fd786e6SMisono Tomohiro 	buf->f_fsid.val[0] ^=
22834fd786e6SMisono Tomohiro 		BTRFS_I(d_inode(dentry))->root->root_key.objectid >> 32;
22844fd786e6SMisono Tomohiro 	buf->f_fsid.val[1] ^=
22854fd786e6SMisono Tomohiro 		BTRFS_I(d_inode(dentry))->root->root_key.objectid;
228632d48fa1SDavid Woodhouse 
22878fd17795SChris Mason 	return 0;
22888fd17795SChris Mason }
2289b5133862SChris Mason 
2290aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb)
2291aea52e19SAl Viro {
2292815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2293aea52e19SAl Viro 	kill_anon_super(sb);
22940d4b0463SJosef Bacik 	btrfs_free_fs_info(fs_info);
2295aea52e19SAl Viro }
2296aea52e19SAl Viro 
22972e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
22982e635a27SChris Mason 	.owner		= THIS_MODULE,
22992e635a27SChris Mason 	.name		= "btrfs",
2300061dbc6bSAl Viro 	.mount		= btrfs_mount,
2301aea52e19SAl Viro 	.kill_sb	= btrfs_kill_super,
2302f667aef6SQu Wenruo 	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
23032e635a27SChris Mason };
230472fa39f5SMisono, Tomohiro 
230572fa39f5SMisono, Tomohiro static struct file_system_type btrfs_root_fs_type = {
230672fa39f5SMisono, Tomohiro 	.owner		= THIS_MODULE,
230772fa39f5SMisono, Tomohiro 	.name		= "btrfs",
230872fa39f5SMisono, Tomohiro 	.mount		= btrfs_mount_root,
230972fa39f5SMisono, Tomohiro 	.kill_sb	= btrfs_kill_super,
231072fa39f5SMisono, Tomohiro 	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
231172fa39f5SMisono, Tomohiro };
231272fa39f5SMisono, Tomohiro 
23137f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs");
2314a9218f6bSChris Mason 
2315d8620958STom Van Braeckel static int btrfs_control_open(struct inode *inode, struct file *file)
2316d8620958STom Van Braeckel {
2317d8620958STom Van Braeckel 	/*
2318d8620958STom Van Braeckel 	 * The control file's private_data is used to hold the
2319d8620958STom Van Braeckel 	 * transaction when it is started and is used to keep
2320d8620958STom Van Braeckel 	 * track of whether a transaction is already in progress.
2321d8620958STom Van Braeckel 	 */
2322d8620958STom Van Braeckel 	file->private_data = NULL;
2323d8620958STom Van Braeckel 	return 0;
2324d8620958STom Van Braeckel }
2325d8620958STom Van Braeckel 
2326d352ac68SChris Mason /*
2327cfe953c8SSu Yue  * Used by /dev/btrfs-control for devices ioctls.
2328d352ac68SChris Mason  */
23298a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
23308a4b83ccSChris Mason 				unsigned long arg)
23318a4b83ccSChris Mason {
23328a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
233336350e95SGu Jinxiang 	struct btrfs_device *device = NULL;
2334c071fcfdSChris Mason 	int ret = -ENOTTY;
23358a4b83ccSChris Mason 
2336e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2337e441d54dSChris Mason 		return -EPERM;
2338e441d54dSChris Mason 
2339dae7b665SLi Zefan 	vol = memdup_user((void __user *)arg, sizeof(*vol));
2340dae7b665SLi Zefan 	if (IS_ERR(vol))
2341dae7b665SLi Zefan 		return PTR_ERR(vol);
2342f505754fSFilipe Manana 	vol->name[BTRFS_PATH_NAME_MAX] = '\0';
2343c071fcfdSChris Mason 
23448a4b83ccSChris Mason 	switch (cmd) {
23458a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
2346899f9307SDavid Sterba 		mutex_lock(&uuid_mutex);
234736350e95SGu Jinxiang 		device = btrfs_scan_one_device(vol->name, FMODE_READ,
234836350e95SGu Jinxiang 					       &btrfs_root_fs_type);
234936350e95SGu Jinxiang 		ret = PTR_ERR_OR_ZERO(device);
2350899f9307SDavid Sterba 		mutex_unlock(&uuid_mutex);
23518a4b83ccSChris Mason 		break;
2352228a73abSAnand Jain 	case BTRFS_IOC_FORGET_DEV:
2353228a73abSAnand Jain 		ret = btrfs_forget_devices(vol->name);
2354228a73abSAnand Jain 		break;
235502db0844SJosef Bacik 	case BTRFS_IOC_DEVICES_READY:
2356899f9307SDavid Sterba 		mutex_lock(&uuid_mutex);
235736350e95SGu Jinxiang 		device = btrfs_scan_one_device(vol->name, FMODE_READ,
235836350e95SGu Jinxiang 					       &btrfs_root_fs_type);
235936350e95SGu Jinxiang 		if (IS_ERR(device)) {
2360899f9307SDavid Sterba 			mutex_unlock(&uuid_mutex);
236136350e95SGu Jinxiang 			ret = PTR_ERR(device);
236202db0844SJosef Bacik 			break;
2363899f9307SDavid Sterba 		}
236436350e95SGu Jinxiang 		ret = !(device->fs_devices->num_devices ==
236536350e95SGu Jinxiang 			device->fs_devices->total_devices);
2366899f9307SDavid Sterba 		mutex_unlock(&uuid_mutex);
236702db0844SJosef Bacik 		break;
2368c5868f83SDavid Sterba 	case BTRFS_IOC_GET_SUPPORTED_FEATURES:
2369d5131b65SDavid Sterba 		ret = btrfs_ioctl_get_supported_features((void __user*)arg);
2370c5868f83SDavid Sterba 		break;
23718a4b83ccSChris Mason 	}
2372dae7b665SLi Zefan 
23738a4b83ccSChris Mason 	kfree(vol);
2374f819d837SLinda Knippers 	return ret;
23758a4b83ccSChris Mason }
23768a4b83ccSChris Mason 
23770176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
2378ed0dab6bSYan {
2379354aa0fbSMiao Xie 	struct btrfs_trans_handle *trans;
23800b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
23810b246afaSJeff Mahoney 	struct btrfs_root *root = fs_info->tree_root;
2382354aa0fbSMiao Xie 
2383fac03c8dSDavid Sterba 	set_bit(BTRFS_FS_FROZEN, &fs_info->flags);
23849e7cc91aSWang Xiaoguang 	/*
23859e7cc91aSWang Xiaoguang 	 * We don't need a barrier here, we'll wait for any transaction that
23869e7cc91aSWang Xiaoguang 	 * could be in progress on other threads (and do delayed iputs that
23879e7cc91aSWang Xiaoguang 	 * we want to avoid on a frozen filesystem), or do the commit
23889e7cc91aSWang Xiaoguang 	 * ourselves.
23899e7cc91aSWang Xiaoguang 	 */
2390d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
2391354aa0fbSMiao Xie 	if (IS_ERR(trans)) {
2392354aa0fbSMiao Xie 		/* no transaction, don't bother */
2393354aa0fbSMiao Xie 		if (PTR_ERR(trans) == -ENOENT)
23940176260fSLinus Torvalds 			return 0;
2395354aa0fbSMiao Xie 		return PTR_ERR(trans);
2396354aa0fbSMiao Xie 	}
23973a45bb20SJeff Mahoney 	return btrfs_commit_transaction(trans);
2398ed0dab6bSYan }
2399ed0dab6bSYan 
24009e7cc91aSWang Xiaoguang static int btrfs_unfreeze(struct super_block *sb)
24019e7cc91aSWang Xiaoguang {
2402fac03c8dSDavid Sterba 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2403fac03c8dSDavid Sterba 
2404fac03c8dSDavid Sterba 	clear_bit(BTRFS_FS_FROZEN, &fs_info->flags);
24059e7cc91aSWang Xiaoguang 	return 0;
24069e7cc91aSWang Xiaoguang }
24079e7cc91aSWang Xiaoguang 
24089c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
24099c5085c1SJosef Bacik {
24109c5085c1SJosef Bacik 	struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
24119c5085c1SJosef Bacik 	struct btrfs_device *dev, *first_dev = NULL;
24129c5085c1SJosef Bacik 
241388c14590SDavid Sterba 	/*
241488c14590SDavid Sterba 	 * Lightweight locking of the devices. We should not need
241588c14590SDavid Sterba 	 * device_list_mutex here as we only read the device data and the list
241688c14590SDavid Sterba 	 * is protected by RCU.  Even if a device is deleted during the list
241788c14590SDavid Sterba 	 * traversals, we'll get valid data, the freeing callback will wait at
241852042d8eSAndrea Gelmini 	 * least until the rcu_read_unlock.
241988c14590SDavid Sterba 	 */
242088c14590SDavid Sterba 	rcu_read_lock();
24214faf55b0SAnand Jain 	list_for_each_entry_rcu(dev, &fs_info->fs_devices->devices, dev_list) {
2422e6e674bdSAnand Jain 		if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
2423aa9ddcd4SJosef Bacik 			continue;
24240aeb8a6eSAnand Jain 		if (!dev->name)
24250aeb8a6eSAnand Jain 			continue;
24269c5085c1SJosef Bacik 		if (!first_dev || dev->devid < first_dev->devid)
24279c5085c1SJosef Bacik 			first_dev = dev;
24289c5085c1SJosef Bacik 	}
24299c5085c1SJosef Bacik 
2430672d5990SMisono Tomohiro 	if (first_dev)
2431672d5990SMisono Tomohiro 		seq_escape(m, rcu_str_deref(first_dev->name), " \t\n\\");
2432672d5990SMisono Tomohiro 	else
24339c5085c1SJosef Bacik 		WARN_ON(1);
243488c14590SDavid Sterba 	rcu_read_unlock();
24359c5085c1SJosef Bacik 	return 0;
24369c5085c1SJosef Bacik }
24379c5085c1SJosef Bacik 
2438b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
243976dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
2440bd555975SAl Viro 	.evict_inode	= btrfs_evict_inode,
2441e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
2442d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
2443a9572a15SEric Paris 	.show_options	= btrfs_show_options,
24449c5085c1SJosef Bacik 	.show_devname	= btrfs_show_devname,
24452c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
24462c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
244726602cabSAl Viro 	.free_inode	= btrfs_free_inode,
24488fd17795SChris Mason 	.statfs		= btrfs_statfs,
2449c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
24500176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
24519e7cc91aSWang Xiaoguang 	.unfreeze_fs	= btrfs_unfreeze,
2452e20d96d6SChris Mason };
2453a9218f6bSChris Mason 
2454a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
2455d8620958STom Van Braeckel 	.open = btrfs_control_open,
2456a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
24571832f2d8SArnd Bergmann 	.compat_ioctl = compat_ptr_ioctl,
2458a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
24596038f373SArnd Bergmann 	.llseek = noop_llseek,
2460a9218f6bSChris Mason };
2461a9218f6bSChris Mason 
2462a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
2463578454ffSKay Sievers 	.minor		= BTRFS_MINOR,
2464a9218f6bSChris Mason 	.name		= "btrfs-control",
2465a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
2466a9218f6bSChris Mason };
2467a9218f6bSChris Mason 
2468578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
2469578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control");
2470578454ffSKay Sievers 
2471f5c29bd9SLiu Bo static int __init btrfs_interface_init(void)
2472a9218f6bSChris Mason {
2473a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
2474a9218f6bSChris Mason }
2475a9218f6bSChris Mason 
2476e67c718bSDavid Sterba static __cold void btrfs_interface_exit(void)
2477a9218f6bSChris Mason {
2478f368ed60SGreg Kroah-Hartman 	misc_deregister(&btrfs_misc);
2479a9218f6bSChris Mason }
2480a9218f6bSChris Mason 
2481f5c29bd9SLiu Bo static void __init btrfs_print_mod_info(void)
248285965600SDavid Sterba {
2483edf57cbfSBart Van Assche 	static const char options[] = ""
248485965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG
248585965600SDavid Sterba 			", debug=on"
248685965600SDavid Sterba #endif
248779556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT
248879556c3dSStefan Behrens 			", assert=on"
248979556c3dSStefan Behrens #endif
249085965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
249185965600SDavid Sterba 			", integrity-checker=on"
249285965600SDavid Sterba #endif
2493fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
2494fb592373SJosef Bacik 			", ref-verify=on"
2495fb592373SJosef Bacik #endif
24965b316468SNaohiro Aota #ifdef CONFIG_BLK_DEV_ZONED
24975b316468SNaohiro Aota 			", zoned=yes"
24985b316468SNaohiro Aota #else
24995b316468SNaohiro Aota 			", zoned=no"
25005b316468SNaohiro Aota #endif
2501edf57cbfSBart Van Assche 			;
2502edf57cbfSBart Van Assche 	pr_info("Btrfs loaded, crc32c=%s%s\n", crc32c_impl(), options);
250385965600SDavid Sterba }
250485965600SDavid Sterba 
25052e635a27SChris Mason static int __init init_btrfs_fs(void)
25062e635a27SChris Mason {
25072c90e5d6SChris Mason 	int err;
250858176a96SJosef Bacik 
250963541927SFilipe David Borba Manana 	btrfs_props_init();
251063541927SFilipe David Borba Manana 
251158176a96SJosef Bacik 	err = btrfs_init_sysfs();
251258176a96SJosef Bacik 	if (err)
25139678c543SNikolay Borisov 		return err;
251458176a96SJosef Bacik 
2515143bede5SJeff Mahoney 	btrfs_init_compress();
2516d1310b2eSChris Mason 
2517261507a0SLi Zefan 	err = btrfs_init_cachep();
2518261507a0SLi Zefan 	if (err)
2519261507a0SLi Zefan 		goto free_compress;
2520261507a0SLi Zefan 
2521d1310b2eSChris Mason 	err = extent_io_init();
25222f4cbe64SWyatt Banks 	if (err)
25232f4cbe64SWyatt Banks 		goto free_cachep;
25242f4cbe64SWyatt Banks 
25256f0d04f8SJosef Bacik 	err = extent_state_cache_init();
2526d1310b2eSChris Mason 	if (err)
2527d1310b2eSChris Mason 		goto free_extent_io;
2528d1310b2eSChris Mason 
25296f0d04f8SJosef Bacik 	err = extent_map_init();
25306f0d04f8SJosef Bacik 	if (err)
25316f0d04f8SJosef Bacik 		goto free_extent_state_cache;
25326f0d04f8SJosef Bacik 
25336352b91dSMiao Xie 	err = ordered_data_init();
25342f4cbe64SWyatt Banks 	if (err)
25352f4cbe64SWyatt Banks 		goto free_extent_map;
2536c8b97818SChris Mason 
25376352b91dSMiao Xie 	err = btrfs_delayed_inode_init();
25386352b91dSMiao Xie 	if (err)
25396352b91dSMiao Xie 		goto free_ordered_data;
25406352b91dSMiao Xie 
25419247f317SMiao Xie 	err = btrfs_auto_defrag_init();
254216cdcec7SMiao Xie 	if (err)
254316cdcec7SMiao Xie 		goto free_delayed_inode;
254416cdcec7SMiao Xie 
254578a6184aSMiao Xie 	err = btrfs_delayed_ref_init();
25469247f317SMiao Xie 	if (err)
25479247f317SMiao Xie 		goto free_auto_defrag;
25489247f317SMiao Xie 
2549b9e9a6cbSWang Shilong 	err = btrfs_prelim_ref_init();
2550b9e9a6cbSWang Shilong 	if (err)
2551af13b492SDavid Sterba 		goto free_delayed_ref;
2552b9e9a6cbSWang Shilong 
255397eb6b69SDavid Sterba 	err = btrfs_end_io_wq_init();
255478a6184aSMiao Xie 	if (err)
2555af13b492SDavid Sterba 		goto free_prelim_ref;
255678a6184aSMiao Xie 
255797eb6b69SDavid Sterba 	err = btrfs_interface_init();
255897eb6b69SDavid Sterba 	if (err)
255997eb6b69SDavid Sterba 		goto free_end_io_wq;
256097eb6b69SDavid Sterba 
25618ae1af3cSAnand Jain 	btrfs_print_mod_info();
2562dc11dd5dSJosef Bacik 
2563dc11dd5dSJosef Bacik 	err = btrfs_run_sanity_tests();
2564dc11dd5dSJosef Bacik 	if (err)
2565dc11dd5dSJosef Bacik 		goto unregister_ioctl;
2566dc11dd5dSJosef Bacik 
2567dc11dd5dSJosef Bacik 	err = register_filesystem(&btrfs_fs_type);
2568dc11dd5dSJosef Bacik 	if (err)
2569dc11dd5dSJosef Bacik 		goto unregister_ioctl;
257074255aa0SJosef Bacik 
25712f4cbe64SWyatt Banks 	return 0;
25722f4cbe64SWyatt Banks 
2573a9218f6bSChris Mason unregister_ioctl:
2574a9218f6bSChris Mason 	btrfs_interface_exit();
257597eb6b69SDavid Sterba free_end_io_wq:
257697eb6b69SDavid Sterba 	btrfs_end_io_wq_exit();
2577b9e9a6cbSWang Shilong free_prelim_ref:
2578b9e9a6cbSWang Shilong 	btrfs_prelim_ref_exit();
257978a6184aSMiao Xie free_delayed_ref:
258078a6184aSMiao Xie 	btrfs_delayed_ref_exit();
25819247f317SMiao Xie free_auto_defrag:
25829247f317SMiao Xie 	btrfs_auto_defrag_exit();
258316cdcec7SMiao Xie free_delayed_inode:
258416cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
25856352b91dSMiao Xie free_ordered_data:
25866352b91dSMiao Xie 	ordered_data_exit();
25872f4cbe64SWyatt Banks free_extent_map:
25882f4cbe64SWyatt Banks 	extent_map_exit();
25896f0d04f8SJosef Bacik free_extent_state_cache:
25906f0d04f8SJosef Bacik 	extent_state_cache_exit();
2591d1310b2eSChris Mason free_extent_io:
2592d1310b2eSChris Mason 	extent_io_exit();
25932f4cbe64SWyatt Banks free_cachep:
25942f4cbe64SWyatt Banks 	btrfs_destroy_cachep();
2595261507a0SLi Zefan free_compress:
2596261507a0SLi Zefan 	btrfs_exit_compress();
25972f4cbe64SWyatt Banks 	btrfs_exit_sysfs();
25989678c543SNikolay Borisov 
25992c90e5d6SChris Mason 	return err;
26002e635a27SChris Mason }
26012e635a27SChris Mason 
26022e635a27SChris Mason static void __exit exit_btrfs_fs(void)
26032e635a27SChris Mason {
260439279cc3SChris Mason 	btrfs_destroy_cachep();
260578a6184aSMiao Xie 	btrfs_delayed_ref_exit();
26069247f317SMiao Xie 	btrfs_auto_defrag_exit();
260716cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
2608b9e9a6cbSWang Shilong 	btrfs_prelim_ref_exit();
26096352b91dSMiao Xie 	ordered_data_exit();
2610a52d9a80SChris Mason 	extent_map_exit();
26116f0d04f8SJosef Bacik 	extent_state_cache_exit();
2612d1310b2eSChris Mason 	extent_io_exit();
2613a9218f6bSChris Mason 	btrfs_interface_exit();
26145ed5f588SJosef Bacik 	btrfs_end_io_wq_exit();
26152e635a27SChris Mason 	unregister_filesystem(&btrfs_fs_type);
261658176a96SJosef Bacik 	btrfs_exit_sysfs();
26178a4b83ccSChris Mason 	btrfs_cleanup_fs_uuids();
2618261507a0SLi Zefan 	btrfs_exit_compress();
26192e635a27SChris Mason }
26202e635a27SChris Mason 
262160efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs);
26222e635a27SChris Mason module_exit(exit_btrfs_fs)
26232e635a27SChris Mason 
26242e635a27SChris Mason MODULE_LICENSE("GPL");
2625d5178578SJohannes Thumshirn MODULE_SOFTDEP("pre: crc32c");
26263951e7f0SJohannes Thumshirn MODULE_SOFTDEP("pre: xxhash64");
26273831bf00SJohannes Thumshirn MODULE_SOFTDEP("pre: sha256");
2628352ae07bSDavid Sterba MODULE_SOFTDEP("pre: blake2b-256");
2629