xref: /linux/fs/btrfs/super.c (revision 72fa39f5c7a1c9d95c24ddf6605581ea05d6081c)
16cbd5570SChris Mason /*
26cbd5570SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
36cbd5570SChris Mason  *
46cbd5570SChris Mason  * This program is free software; you can redistribute it and/or
56cbd5570SChris Mason  * modify it under the terms of the GNU General Public
66cbd5570SChris Mason  * License v2 as published by the Free Software Foundation.
76cbd5570SChris Mason  *
86cbd5570SChris Mason  * This program is distributed in the hope that it will be useful,
96cbd5570SChris Mason  * but WITHOUT ANY WARRANTY; without even the implied warranty of
106cbd5570SChris Mason  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116cbd5570SChris Mason  * General Public License for more details.
126cbd5570SChris Mason  *
136cbd5570SChris Mason  * You should have received a copy of the GNU General Public
146cbd5570SChris Mason  * License along with this program; if not, write to the
156cbd5570SChris Mason  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
166cbd5570SChris Mason  * Boston, MA 021110-1307, USA.
176cbd5570SChris Mason  */
186cbd5570SChris Mason 
194b82d6e4SYan #include <linux/blkdev.h>
202e635a27SChris Mason #include <linux/module.h>
21e20d96d6SChris Mason #include <linux/buffer_head.h>
222e635a27SChris Mason #include <linux/fs.h>
232e635a27SChris Mason #include <linux/pagemap.h>
242e635a27SChris Mason #include <linux/highmem.h>
252e635a27SChris Mason #include <linux/time.h>
262e635a27SChris Mason #include <linux/init.h>
27a9572a15SEric Paris #include <linux/seq_file.h>
282e635a27SChris Mason #include <linux/string.h>
292e635a27SChris Mason #include <linux/backing-dev.h>
304b82d6e4SYan #include <linux/mount.h>
31dee26a9fSChris Mason #include <linux/mpage.h>
3275dfe396SChris Mason #include <linux/swap.h>
3375dfe396SChris Mason #include <linux/writeback.h>
348fd17795SChris Mason #include <linux/statfs.h>
3508607c1bSChris Mason #include <linux/compat.h>
3695e05289SChris Mason #include <linux/parser.h>
37c59f8951SChris Mason #include <linux/ctype.h>
386da6abaeSChris Mason #include <linux/namei.h>
39a9218f6bSChris Mason #include <linux/miscdevice.h>
401bcbf313SQinghuang Feng #include <linux/magic.h>
415a0e3ad6STejun Heo #include <linux/slab.h>
4290a887c9SDan Magenheimer #include <linux/cleancache.h>
4322c44fe6SJosef Bacik #include <linux/ratelimit.h>
4455e301fdSFilipe Brandenburger #include <linux/btrfs.h>
4516cdcec7SMiao Xie #include "delayed-inode.h"
462e635a27SChris Mason #include "ctree.h"
47e20d96d6SChris Mason #include "disk-io.h"
48d5719762SChris Mason #include "transaction.h"
492c90e5d6SChris Mason #include "btrfs_inode.h"
503a686375SChris Mason #include "print-tree.h"
5114a958e6SFilipe David Borba Manana #include "hash.h"
5263541927SFilipe David Borba Manana #include "props.h"
535103e947SJosef Bacik #include "xattr.h"
548a4b83ccSChris Mason #include "volumes.h"
55be6e8dc0SBalaji Rao #include "export.h"
56c8b97818SChris Mason #include "compression.h"
579c5085c1SJosef Bacik #include "rcu-string.h"
588dabb742SStefan Behrens #include "dev-replace.h"
5974255aa0SJosef Bacik #include "free-space-cache.h"
60b9e9a6cbSWang Shilong #include "backref.h"
61dc11dd5dSJosef Bacik #include "tests/btrfs-tests.h"
622e635a27SChris Mason 
63d3982100SMark Fasheh #include "qgroup.h"
641abe9b8aSliubo #define CREATE_TRACE_POINTS
651abe9b8aSliubo #include <trace/events/btrfs.h>
661abe9b8aSliubo 
67b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops;
68*72fa39f5SMisono, Tomohiro 
69*72fa39f5SMisono, Tomohiro /*
70*72fa39f5SMisono, Tomohiro  * Types for mounting the default subvolume and a subvolume explicitly
71*72fa39f5SMisono, Tomohiro  * requested by subvol=/path. That way the callchain is straightforward and we
72*72fa39f5SMisono, Tomohiro  * don't have to play tricks with the mount options and recursive calls to
73*72fa39f5SMisono, Tomohiro  * btrfs_mount.
74*72fa39f5SMisono, Tomohiro  */
75830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type;
76*72fa39f5SMisono, Tomohiro static struct file_system_type btrfs_root_fs_type;
77e20d96d6SChris Mason 
780723a047SHarald Hoyer static int btrfs_remount(struct super_block *sb, int *flags, char *data);
790723a047SHarald Hoyer 
80e33e17eeSJeff Mahoney const char *btrfs_decode_error(int errno)
81acce952bSliubo {
8208748810SDavid Sterba 	char *errstr = "unknown";
83acce952bSliubo 
84acce952bSliubo 	switch (errno) {
85acce952bSliubo 	case -EIO:
86acce952bSliubo 		errstr = "IO failure";
87acce952bSliubo 		break;
88acce952bSliubo 	case -ENOMEM:
89acce952bSliubo 		errstr = "Out of memory";
90acce952bSliubo 		break;
91acce952bSliubo 	case -EROFS:
92acce952bSliubo 		errstr = "Readonly filesystem";
93acce952bSliubo 		break;
948c342930SJeff Mahoney 	case -EEXIST:
958c342930SJeff Mahoney 		errstr = "Object already exists";
968c342930SJeff Mahoney 		break;
9794ef7280SDavid Sterba 	case -ENOSPC:
9894ef7280SDavid Sterba 		errstr = "No space left";
9994ef7280SDavid Sterba 		break;
10094ef7280SDavid Sterba 	case -ENOENT:
10194ef7280SDavid Sterba 		errstr = "No such entry";
10294ef7280SDavid Sterba 		break;
103acce952bSliubo 	}
104acce952bSliubo 
105acce952bSliubo 	return errstr;
106acce952bSliubo }
107acce952bSliubo 
108acce952bSliubo /* btrfs handle error by forcing the filesystem readonly */
109acce952bSliubo static void btrfs_handle_error(struct btrfs_fs_info *fs_info)
110acce952bSliubo {
111acce952bSliubo 	struct super_block *sb = fs_info->sb;
112acce952bSliubo 
113bc98a42cSDavid Howells 	if (sb_rdonly(sb))
114acce952bSliubo 		return;
115acce952bSliubo 
11687533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
1171751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
118c2cf52ebSSimon Kirby 		btrfs_info(fs_info, "forced readonly");
1191acd6831SStefan Behrens 		/*
1201acd6831SStefan Behrens 		 * Note that a running device replace operation is not
1211acd6831SStefan Behrens 		 * canceled here although there is no way to update
1221acd6831SStefan Behrens 		 * the progress. It would add the risk of a deadlock,
12301327610SNicholas D Steeves 		 * therefore the canceling is omitted. The only penalty
1241acd6831SStefan Behrens 		 * is that some I/O remains active until the procedure
1251acd6831SStefan Behrens 		 * completes. The next time when the filesystem is
1261acd6831SStefan Behrens 		 * mounted writeable again, the device replace
1271acd6831SStefan Behrens 		 * operation continues.
1281acd6831SStefan Behrens 		 */
129acce952bSliubo 	}
130acce952bSliubo }
131acce952bSliubo 
132acce952bSliubo /*
13334d97007SAnand Jain  * __btrfs_handle_fs_error decodes expected errors from the caller and
134acce952bSliubo  * invokes the approciate error response.
135acce952bSliubo  */
136c0d19e2bSDavid Sterba __cold
13734d97007SAnand Jain void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
1384da35113SJeff Mahoney 		       unsigned int line, int errno, const char *fmt, ...)
139acce952bSliubo {
140acce952bSliubo 	struct super_block *sb = fs_info->sb;
14157d816a1SAnand Jain #ifdef CONFIG_PRINTK
142acce952bSliubo 	const char *errstr;
14357d816a1SAnand Jain #endif
144acce952bSliubo 
145acce952bSliubo 	/*
146acce952bSliubo 	 * Special case: if the error is EROFS, and we're already
1471751e8a6SLinus Torvalds 	 * under SB_RDONLY, then it is safe here.
148acce952bSliubo 	 */
149bc98a42cSDavid Howells 	if (errno == -EROFS && sb_rdonly(sb))
150acce952bSliubo   		return;
151acce952bSliubo 
15257d816a1SAnand Jain #ifdef CONFIG_PRINTK
15308748810SDavid Sterba 	errstr = btrfs_decode_error(errno);
1544da35113SJeff Mahoney 	if (fmt) {
15537252a66SEric Sandeen 		struct va_format vaf;
15637252a66SEric Sandeen 		va_list args;
15737252a66SEric Sandeen 
15837252a66SEric Sandeen 		va_start(args, fmt);
15937252a66SEric Sandeen 		vaf.fmt = fmt;
16037252a66SEric Sandeen 		vaf.va = &args;
1614da35113SJeff Mahoney 
16262e85577SJeff Mahoney 		pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s (%pV)\n",
16308748810SDavid Sterba 			sb->s_id, function, line, errno, errstr, &vaf);
16437252a66SEric Sandeen 		va_end(args);
1654da35113SJeff Mahoney 	} else {
16662e85577SJeff Mahoney 		pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s\n",
16708748810SDavid Sterba 			sb->s_id, function, line, errno, errstr);
1684da35113SJeff Mahoney 	}
16957d816a1SAnand Jain #endif
170acce952bSliubo 
1710713d90cSAnand Jain 	/*
1720713d90cSAnand Jain 	 * Today we only save the error info to memory.  Long term we'll
1730713d90cSAnand Jain 	 * also send it down to the disk
1740713d90cSAnand Jain 	 */
1750713d90cSAnand Jain 	set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
1760713d90cSAnand Jain 
1774da35113SJeff Mahoney 	/* Don't go through full error handling during mount */
1781751e8a6SLinus Torvalds 	if (sb->s_flags & SB_BORN)
179acce952bSliubo 		btrfs_handle_error(fs_info);
180acce952bSliubo }
1814da35113SJeff Mahoney 
18257d816a1SAnand Jain #ifdef CONFIG_PRINTK
183533574c6SJoe Perches static const char * const logtypes[] = {
1844da35113SJeff Mahoney 	"emergency",
1854da35113SJeff Mahoney 	"alert",
1864da35113SJeff Mahoney 	"critical",
1874da35113SJeff Mahoney 	"error",
1884da35113SJeff Mahoney 	"warning",
1894da35113SJeff Mahoney 	"notice",
1904da35113SJeff Mahoney 	"info",
1914da35113SJeff Mahoney 	"debug",
1924da35113SJeff Mahoney };
1934da35113SJeff Mahoney 
19435f4e5e6SNikolay Borisov 
19535f4e5e6SNikolay Borisov /*
19635f4e5e6SNikolay Borisov  * Use one ratelimit state per log level so that a flood of less important
19735f4e5e6SNikolay Borisov  * messages doesn't cause more important ones to be dropped.
19835f4e5e6SNikolay Borisov  */
19935f4e5e6SNikolay Borisov static struct ratelimit_state printk_limits[] = {
20035f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[0], DEFAULT_RATELIMIT_INTERVAL, 100),
20135f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[1], DEFAULT_RATELIMIT_INTERVAL, 100),
20235f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[2], DEFAULT_RATELIMIT_INTERVAL, 100),
20335f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[3], DEFAULT_RATELIMIT_INTERVAL, 100),
20435f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[4], DEFAULT_RATELIMIT_INTERVAL, 100),
20535f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[5], DEFAULT_RATELIMIT_INTERVAL, 100),
20635f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[6], DEFAULT_RATELIMIT_INTERVAL, 100),
20735f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[7], DEFAULT_RATELIMIT_INTERVAL, 100),
20835f4e5e6SNikolay Borisov };
20935f4e5e6SNikolay Borisov 
210c2cf52ebSSimon Kirby void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
2114da35113SJeff Mahoney {
21240f7828bSPetr Mladek 	char lvl[PRINTK_MAX_SINGLE_HEADER_LEN + 1] = "\0";
2134da35113SJeff Mahoney 	struct va_format vaf;
2144da35113SJeff Mahoney 	va_list args;
215533574c6SJoe Perches 	int kern_level;
21640f7828bSPetr Mladek 	const char *type = logtypes[4];
21740f7828bSPetr Mladek 	struct ratelimit_state *ratelimit = &printk_limits[4];
2184da35113SJeff Mahoney 
2194da35113SJeff Mahoney 	va_start(args, fmt);
2204da35113SJeff Mahoney 
221262c5e86SPetr Mladek 	while ((kern_level = printk_get_level(fmt)) != 0) {
222533574c6SJoe Perches 		size_t size = printk_skip_level(fmt) - fmt;
223262c5e86SPetr Mladek 
224262c5e86SPetr Mladek 		if (kern_level >= '0' && kern_level <= '7') {
225533574c6SJoe Perches 			memcpy(lvl, fmt,  size);
226533574c6SJoe Perches 			lvl[size] = '\0';
227533574c6SJoe Perches 			type = logtypes[kern_level - '0'];
22835f4e5e6SNikolay Borisov 			ratelimit = &printk_limits[kern_level - '0'];
229262c5e86SPetr Mladek 		}
230262c5e86SPetr Mladek 		fmt += size;
231262c5e86SPetr Mladek 	}
232262c5e86SPetr Mladek 
2334da35113SJeff Mahoney 	vaf.fmt = fmt;
2344da35113SJeff Mahoney 	vaf.va = &args;
235533574c6SJoe Perches 
23635f4e5e6SNikolay Borisov 	if (__ratelimit(ratelimit))
2373993b112SColin Ian King 		printk("%sBTRFS %s (device %s): %pV\n", lvl, type,
2383993b112SColin Ian King 			fs_info ? fs_info->sb->s_id : "<unknown>", &vaf);
239533574c6SJoe Perches 
240533574c6SJoe Perches 	va_end(args);
2414da35113SJeff Mahoney }
242533574c6SJoe Perches #endif
243533574c6SJoe Perches 
2448c342930SJeff Mahoney /*
24549b25e05SJeff Mahoney  * We only mark the transaction aborted and then set the file system read-only.
24649b25e05SJeff Mahoney  * This will prevent new transactions from starting or trying to join this
24749b25e05SJeff Mahoney  * one.
24849b25e05SJeff Mahoney  *
24949b25e05SJeff Mahoney  * This means that error recovery at the call site is limited to freeing
25049b25e05SJeff Mahoney  * any local memory allocations and passing the error code up without
25149b25e05SJeff Mahoney  * further cleanup. The transaction should complete as it normally would
25249b25e05SJeff Mahoney  * in the call path but will return -EIO.
25349b25e05SJeff Mahoney  *
25449b25e05SJeff Mahoney  * We'll complete the cleanup in btrfs_end_transaction and
25549b25e05SJeff Mahoney  * btrfs_commit_transaction.
25649b25e05SJeff Mahoney  */
257c0d19e2bSDavid Sterba __cold
25849b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
25966642832SJeff Mahoney 			       const char *function,
26049b25e05SJeff Mahoney 			       unsigned int line, int errno)
26149b25e05SJeff Mahoney {
26266642832SJeff Mahoney 	struct btrfs_fs_info *fs_info = trans->fs_info;
26366642832SJeff Mahoney 
26449b25e05SJeff Mahoney 	trans->aborted = errno;
26549b25e05SJeff Mahoney 	/* Nothing used. The other threads that have joined this
26649b25e05SJeff Mahoney 	 * transaction may be able to continue. */
26764c12921SJeff Mahoney 	if (!trans->dirty && list_empty(&trans->new_bgs)) {
26869ce977aSMiao Xie 		const char *errstr;
26969ce977aSMiao Xie 
27008748810SDavid Sterba 		errstr = btrfs_decode_error(errno);
27166642832SJeff Mahoney 		btrfs_warn(fs_info,
272c2cf52ebSSimon Kirby 		           "%s:%d: Aborting unused transaction(%s).",
27369ce977aSMiao Xie 		           function, line, errstr);
27449b25e05SJeff Mahoney 		return;
27549b25e05SJeff Mahoney 	}
27620c7bcecSSeraphime Kirkovski 	WRITE_ONCE(trans->transaction->aborted, errno);
277501407aaSJosef Bacik 	/* Wake up anybody who may be waiting on this transaction */
27866642832SJeff Mahoney 	wake_up(&fs_info->transaction_wait);
27966642832SJeff Mahoney 	wake_up(&fs_info->transaction_blocked_wait);
28066642832SJeff Mahoney 	__btrfs_handle_fs_error(fs_info, function, line, errno, NULL);
28149b25e05SJeff Mahoney }
28249b25e05SJeff Mahoney /*
2838c342930SJeff Mahoney  * __btrfs_panic decodes unexpected, fatal errors from the caller,
2848c342930SJeff Mahoney  * issues an alert, and either panics or BUGs, depending on mount options.
2858c342930SJeff Mahoney  */
286c0d19e2bSDavid Sterba __cold
2878c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
2888c342930SJeff Mahoney 		   unsigned int line, int errno, const char *fmt, ...)
2898c342930SJeff Mahoney {
2908c342930SJeff Mahoney 	char *s_id = "<unknown>";
2918c342930SJeff Mahoney 	const char *errstr;
2928c342930SJeff Mahoney 	struct va_format vaf = { .fmt = fmt };
2938c342930SJeff Mahoney 	va_list args;
2948c342930SJeff Mahoney 
2958c342930SJeff Mahoney 	if (fs_info)
2968c342930SJeff Mahoney 		s_id = fs_info->sb->s_id;
2978c342930SJeff Mahoney 
2988c342930SJeff Mahoney 	va_start(args, fmt);
2998c342930SJeff Mahoney 	vaf.va = &args;
3008c342930SJeff Mahoney 
30108748810SDavid Sterba 	errstr = btrfs_decode_error(errno);
302d8953d69SSatoru Takeuchi 	if (fs_info && (btrfs_test_opt(fs_info, PANIC_ON_FATAL_ERROR)))
30308748810SDavid Sterba 		panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
30408748810SDavid Sterba 			s_id, function, line, &vaf, errno, errstr);
3058c342930SJeff Mahoney 
306efe120a0SFrank Holton 	btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)",
307efe120a0SFrank Holton 		   function, line, &vaf, errno, errstr);
3088c342930SJeff Mahoney 	va_end(args);
3098c342930SJeff Mahoney 	/* Caller calls BUG() */
3108c342930SJeff Mahoney }
311acce952bSliubo 
312e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb)
313e20d96d6SChris Mason {
3146bccf3abSJeff Mahoney 	close_ctree(btrfs_sb(sb));
315e20d96d6SChris Mason }
3162e635a27SChris Mason 
31795e05289SChris Mason enum {
31873f73415SJosef Bacik 	Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
319287a0ab9SJosef Bacik 	Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
320287a0ab9SJosef Bacik 	Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
321261507a0SLi Zefan 	Opt_compress_type, Opt_compress_force, Opt_compress_force_type,
322261507a0SLi Zefan 	Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard,
32370f6d82eSOmar Sandoval 	Opt_space_cache, Opt_space_cache_version, Opt_clear_cache,
32470f6d82eSOmar Sandoval 	Opt_user_subvol_rm_allowed, Opt_enospc_debug, Opt_subvolrootid,
32570f6d82eSOmar Sandoval 	Opt_defrag, Opt_inode_cache, Opt_no_space_cache, Opt_recovery,
32670f6d82eSOmar Sandoval 	Opt_skip_balance, Opt_check_integrity,
32770f6d82eSOmar Sandoval 	Opt_check_integrity_including_extent_data,
328f420ee1eSStefan Behrens 	Opt_check_integrity_print_mask, Opt_fatal_errors, Opt_rescan_uuid_tree,
329e07a2adeSQu Wenruo 	Opt_commit_interval, Opt_barrier, Opt_nodefrag, Opt_nodiscard,
330a258af7aSQu Wenruo 	Opt_noenospc_debug, Opt_noflushoncommit, Opt_acl, Opt_datacow,
3318dcddfa0SQu Wenruo 	Opt_datasum, Opt_treelog, Opt_noinode_cache, Opt_usebackuproot,
332fed8f166SQu Wenruo 	Opt_nologreplay, Opt_norecovery,
333d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
334d0bd4560SJosef Bacik 	Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
335d0bd4560SJosef Bacik #endif
336fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
337fb592373SJosef Bacik 	Opt_ref_verify,
338fb592373SJosef Bacik #endif
3399555c6c1SIlya Dryomov 	Opt_err,
34095e05289SChris Mason };
34195e05289SChris Mason 
3424d4ab6d6SDavid Sterba static const match_table_t tokens = {
343dfe25020SChris Mason 	{Opt_degraded, "degraded"},
34495e05289SChris Mason 	{Opt_subvol, "subvol=%s"},
3451493381fSWang Shilong 	{Opt_subvolid, "subvolid=%s"},
34643e570b0SChristoph Hellwig 	{Opt_device, "device=%s"},
347b6cda9bcSChris Mason 	{Opt_nodatasum, "nodatasum"},
348d399167dSQu Wenruo 	{Opt_datasum, "datasum"},
349be20aa9dSChris Mason 	{Opt_nodatacow, "nodatacow"},
350a258af7aSQu Wenruo 	{Opt_datacow, "datacow"},
35121ad10cfSChris Mason 	{Opt_nobarrier, "nobarrier"},
352842bef58SQu Wenruo 	{Opt_barrier, "barrier"},
3536f568d35SChris Mason 	{Opt_max_inline, "max_inline=%s"},
3548f662a76SChris Mason 	{Opt_alloc_start, "alloc_start=%s"},
3554543df7eSChris Mason 	{Opt_thread_pool, "thread_pool=%d"},
356c8b97818SChris Mason 	{Opt_compress, "compress"},
357261507a0SLi Zefan 	{Opt_compress_type, "compress=%s"},
358a555f810SChris Mason 	{Opt_compress_force, "compress-force"},
359261507a0SLi Zefan 	{Opt_compress_force_type, "compress-force=%s"},
360e18e4809SChris Mason 	{Opt_ssd, "ssd"},
361451d7585SChris Mason 	{Opt_ssd_spread, "ssd_spread"},
3623b30c22fSChris Mason 	{Opt_nossd, "nossd"},
363bd0330adSQu Wenruo 	{Opt_acl, "acl"},
36433268eafSJosef Bacik 	{Opt_noacl, "noacl"},
3653a5e1404SSage Weil 	{Opt_notreelog, "notreelog"},
366a88998f2SQu Wenruo 	{Opt_treelog, "treelog"},
36796da0919SQu Wenruo 	{Opt_nologreplay, "nologreplay"},
368fed8f166SQu Wenruo 	{Opt_norecovery, "norecovery"},
369dccae999SSage Weil 	{Opt_flushoncommit, "flushoncommit"},
3702c9ee856SQu Wenruo 	{Opt_noflushoncommit, "noflushoncommit"},
37197e728d4SJosef Bacik 	{Opt_ratio, "metadata_ratio=%d"},
372e244a0aeSChristoph Hellwig 	{Opt_discard, "discard"},
373e07a2adeSQu Wenruo 	{Opt_nodiscard, "nodiscard"},
3740af3d00bSJosef Bacik 	{Opt_space_cache, "space_cache"},
37570f6d82eSOmar Sandoval 	{Opt_space_cache_version, "space_cache=%s"},
37688c2ba3bSJosef Bacik 	{Opt_clear_cache, "clear_cache"},
3774260f7c7SSage Weil 	{Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
37891435650SChris Mason 	{Opt_enospc_debug, "enospc_debug"},
37953036293SQu Wenruo 	{Opt_noenospc_debug, "noenospc_debug"},
380e15d0542SXin Zhong 	{Opt_subvolrootid, "subvolrootid=%d"},
3814cb5300bSChris Mason 	{Opt_defrag, "autodefrag"},
382fc0ca9afSQu Wenruo 	{Opt_nodefrag, "noautodefrag"},
3834b9465cbSChris Mason 	{Opt_inode_cache, "inode_cache"},
3843818aea2SQu Wenruo 	{Opt_noinode_cache, "noinode_cache"},
3858965593eSDavid Sterba 	{Opt_no_space_cache, "nospace_cache"},
3868dcddfa0SQu Wenruo 	{Opt_recovery, "recovery"}, /* deprecated */
3878dcddfa0SQu Wenruo 	{Opt_usebackuproot, "usebackuproot"},
3889555c6c1SIlya Dryomov 	{Opt_skip_balance, "skip_balance"},
38921adbd5cSStefan Behrens 	{Opt_check_integrity, "check_int"},
39021adbd5cSStefan Behrens 	{Opt_check_integrity_including_extent_data, "check_int_data"},
39121adbd5cSStefan Behrens 	{Opt_check_integrity_print_mask, "check_int_print_mask=%d"},
392f420ee1eSStefan Behrens 	{Opt_rescan_uuid_tree, "rescan_uuid_tree"},
3938c342930SJeff Mahoney 	{Opt_fatal_errors, "fatal_errors=%s"},
3948b87dc17SDavid Sterba 	{Opt_commit_interval, "commit=%d"},
395d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
396d0bd4560SJosef Bacik 	{Opt_fragment_data, "fragment=data"},
397d0bd4560SJosef Bacik 	{Opt_fragment_metadata, "fragment=metadata"},
398d0bd4560SJosef Bacik 	{Opt_fragment_all, "fragment=all"},
399d0bd4560SJosef Bacik #endif
400fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
401fb592373SJosef Bacik 	{Opt_ref_verify, "ref_verify"},
402fb592373SJosef Bacik #endif
40333268eafSJosef Bacik 	{Opt_err, NULL},
40495e05289SChris Mason };
40595e05289SChris Mason 
406edf24abeSChristoph Hellwig /*
407edf24abeSChristoph Hellwig  * Regular mount options parser.  Everything that is needed only when
408edf24abeSChristoph Hellwig  * reading in a new superblock is parsed here.
40949b25e05SJeff Mahoney  * XXX JDM: This needs to be cleaned up for remount.
410edf24abeSChristoph Hellwig  */
4112ff7e61eSJeff Mahoney int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
41296da0919SQu Wenruo 			unsigned long new_flags)
41395e05289SChris Mason {
41495e05289SChris Mason 	substring_t args[MAX_OPT_ARGS];
41573bc1876SJosef Bacik 	char *p, *num, *orig = NULL;
41673bc1876SJosef Bacik 	u64 cache_gen;
4174543df7eSChris Mason 	int intarg;
418a7a3f7caSSage Weil 	int ret = 0;
419261507a0SLi Zefan 	char *compress_type;
420261507a0SLi Zefan 	bool compress_force = false;
421b7c47bbbSTsutomu Itoh 	enum btrfs_compression_type saved_compress_type;
422b7c47bbbSTsutomu Itoh 	bool saved_compress_force;
423b7c47bbbSTsutomu Itoh 	int no_compress = 0;
424b6cda9bcSChris Mason 
4250b246afaSJeff Mahoney 	cache_gen = btrfs_super_cache_generation(info->super_copy);
4260b246afaSJeff Mahoney 	if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
42770f6d82eSOmar Sandoval 		btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE);
42870f6d82eSOmar Sandoval 	else if (cache_gen)
42973bc1876SJosef Bacik 		btrfs_set_opt(info->mount_opt, SPACE_CACHE);
43073bc1876SJosef Bacik 
43196da0919SQu Wenruo 	/*
43296da0919SQu Wenruo 	 * Even the options are empty, we still need to do extra check
43396da0919SQu Wenruo 	 * against new flags
43496da0919SQu Wenruo 	 */
43595e05289SChris Mason 	if (!options)
43696da0919SQu Wenruo 		goto check;
43795e05289SChris Mason 
438be20aa9dSChris Mason 	/*
439be20aa9dSChris Mason 	 * strsep changes the string, duplicate it because parse_options
440be20aa9dSChris Mason 	 * gets called twice
441be20aa9dSChris Mason 	 */
4423ec83621SDavid Sterba 	options = kstrdup(options, GFP_KERNEL);
443be20aa9dSChris Mason 	if (!options)
444be20aa9dSChris Mason 		return -ENOMEM;
445be20aa9dSChris Mason 
446da495eccSJosef Bacik 	orig = options;
447be20aa9dSChris Mason 
44895e05289SChris Mason 	while ((p = strsep(&options, ",")) != NULL) {
44995e05289SChris Mason 		int token;
45095e05289SChris Mason 		if (!*p)
45195e05289SChris Mason 			continue;
45295e05289SChris Mason 
45395e05289SChris Mason 		token = match_token(p, tokens, args);
45495e05289SChris Mason 		switch (token) {
455dfe25020SChris Mason 		case Opt_degraded:
4560b246afaSJeff Mahoney 			btrfs_info(info, "allowing degraded mounts");
457dfe25020SChris Mason 			btrfs_set_opt(info->mount_opt, DEGRADED);
458dfe25020SChris Mason 			break;
45995e05289SChris Mason 		case Opt_subvol:
46073f73415SJosef Bacik 		case Opt_subvolid:
461e15d0542SXin Zhong 		case Opt_subvolrootid:
46243e570b0SChristoph Hellwig 		case Opt_device:
463edf24abeSChristoph Hellwig 			/*
46443e570b0SChristoph Hellwig 			 * These are parsed by btrfs_parse_early_options
465edf24abeSChristoph Hellwig 			 * and can be happily ignored here.
466edf24abeSChristoph Hellwig 			 */
46795e05289SChris Mason 			break;
468b6cda9bcSChris Mason 		case Opt_nodatasum:
4693cdde224SJeff Mahoney 			btrfs_set_and_info(info, NODATASUM,
47007802534SQu Wenruo 					   "setting nodatasum");
471be20aa9dSChris Mason 			break;
472d399167dSQu Wenruo 		case Opt_datasum:
4733cdde224SJeff Mahoney 			if (btrfs_test_opt(info, NODATASUM)) {
4743cdde224SJeff Mahoney 				if (btrfs_test_opt(info, NODATACOW))
4750b246afaSJeff Mahoney 					btrfs_info(info,
4765d163e0eSJeff Mahoney 						   "setting datasum, datacow enabled");
477d399167dSQu Wenruo 				else
4780b246afaSJeff Mahoney 					btrfs_info(info, "setting datasum");
47907802534SQu Wenruo 			}
480d399167dSQu Wenruo 			btrfs_clear_opt(info->mount_opt, NODATACOW);
481d399167dSQu Wenruo 			btrfs_clear_opt(info->mount_opt, NODATASUM);
482d399167dSQu Wenruo 			break;
483be20aa9dSChris Mason 		case Opt_nodatacow:
4843cdde224SJeff Mahoney 			if (!btrfs_test_opt(info, NODATACOW)) {
4853cdde224SJeff Mahoney 				if (!btrfs_test_opt(info, COMPRESS) ||
4863cdde224SJeff Mahoney 				    !btrfs_test_opt(info, FORCE_COMPRESS)) {
4870b246afaSJeff Mahoney 					btrfs_info(info,
488efe120a0SFrank Holton 						   "setting nodatacow, compression disabled");
489bedb2ccaSAndrei Popa 				} else {
4900b246afaSJeff Mahoney 					btrfs_info(info, "setting nodatacow");
491bedb2ccaSAndrei Popa 				}
49207802534SQu Wenruo 			}
493bedb2ccaSAndrei Popa 			btrfs_clear_opt(info->mount_opt, COMPRESS);
494bedb2ccaSAndrei Popa 			btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
495be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATACOW);
496be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
497b6cda9bcSChris Mason 			break;
498a258af7aSQu Wenruo 		case Opt_datacow:
4993cdde224SJeff Mahoney 			btrfs_clear_and_info(info, NODATACOW,
50007802534SQu Wenruo 					     "setting datacow");
501a258af7aSQu Wenruo 			break;
502a555f810SChris Mason 		case Opt_compress_force:
503261507a0SLi Zefan 		case Opt_compress_force_type:
504261507a0SLi Zefan 			compress_force = true;
5051c697d4aSEric Sandeen 			/* Fallthrough */
506261507a0SLi Zefan 		case Opt_compress:
507261507a0SLi Zefan 		case Opt_compress_type:
5083cdde224SJeff Mahoney 			saved_compress_type = btrfs_test_opt(info,
5093cdde224SJeff Mahoney 							     COMPRESS) ?
510b7c47bbbSTsutomu Itoh 				info->compress_type : BTRFS_COMPRESS_NONE;
511b7c47bbbSTsutomu Itoh 			saved_compress_force =
5123cdde224SJeff Mahoney 				btrfs_test_opt(info, FORCE_COMPRESS);
513261507a0SLi Zefan 			if (token == Opt_compress ||
514261507a0SLi Zefan 			    token == Opt_compress_force ||
515a7164fa4SDavid Sterba 			    strncmp(args[0].from, "zlib", 4) == 0) {
516261507a0SLi Zefan 				compress_type = "zlib";
517eae8d825SQu Wenruo 
518261507a0SLi Zefan 				info->compress_type = BTRFS_COMPRESS_ZLIB;
519eae8d825SQu Wenruo 				info->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
520eae8d825SQu Wenruo 				/*
521eae8d825SQu Wenruo 				 * args[0] contains uninitialized data since
522eae8d825SQu Wenruo 				 * for these tokens we don't expect any
523eae8d825SQu Wenruo 				 * parameter.
524eae8d825SQu Wenruo 				 */
525eae8d825SQu Wenruo 				if (token != Opt_compress &&
526eae8d825SQu Wenruo 				    token != Opt_compress_force)
527f51d2b59SDavid Sterba 					info->compress_level =
528f51d2b59SDavid Sterba 					  btrfs_compress_str2level(args[0].from);
529063849eaSArnd Hannemann 				btrfs_set_opt(info->mount_opt, COMPRESS);
530bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATACOW);
531bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATASUM);
532b7c47bbbSTsutomu Itoh 				no_compress = 0;
533a7164fa4SDavid Sterba 			} else if (strncmp(args[0].from, "lzo", 3) == 0) {
534a6fa6faeSLi Zefan 				compress_type = "lzo";
535a6fa6faeSLi Zefan 				info->compress_type = BTRFS_COMPRESS_LZO;
536063849eaSArnd Hannemann 				btrfs_set_opt(info->mount_opt, COMPRESS);
537bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATACOW);
538bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATASUM);
5392b0ce2c2SMitch Harder 				btrfs_set_fs_incompat(info, COMPRESS_LZO);
540b7c47bbbSTsutomu Itoh 				no_compress = 0;
5415c1aab1dSNick Terrell 			} else if (strcmp(args[0].from, "zstd") == 0) {
5425c1aab1dSNick Terrell 				compress_type = "zstd";
5435c1aab1dSNick Terrell 				info->compress_type = BTRFS_COMPRESS_ZSTD;
5445c1aab1dSNick Terrell 				btrfs_set_opt(info->mount_opt, COMPRESS);
5455c1aab1dSNick Terrell 				btrfs_clear_opt(info->mount_opt, NODATACOW);
5465c1aab1dSNick Terrell 				btrfs_clear_opt(info->mount_opt, NODATASUM);
5475c1aab1dSNick Terrell 				btrfs_set_fs_incompat(info, COMPRESS_ZSTD);
5485c1aab1dSNick Terrell 				no_compress = 0;
549063849eaSArnd Hannemann 			} else if (strncmp(args[0].from, "no", 2) == 0) {
550063849eaSArnd Hannemann 				compress_type = "no";
551063849eaSArnd Hannemann 				btrfs_clear_opt(info->mount_opt, COMPRESS);
552063849eaSArnd Hannemann 				btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
553063849eaSArnd Hannemann 				compress_force = false;
554b7c47bbbSTsutomu Itoh 				no_compress++;
555261507a0SLi Zefan 			} else {
556261507a0SLi Zefan 				ret = -EINVAL;
557261507a0SLi Zefan 				goto out;
558261507a0SLi Zefan 			}
559261507a0SLi Zefan 
560261507a0SLi Zefan 			if (compress_force) {
561b7c47bbbSTsutomu Itoh 				btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
562143f3636SDavid Sterba 			} else {
5634027e0f4SWang Shilong 				/*
5644027e0f4SWang Shilong 				 * If we remount from compress-force=xxx to
5654027e0f4SWang Shilong 				 * compress=xxx, we need clear FORCE_COMPRESS
5664027e0f4SWang Shilong 				 * flag, otherwise, there is no way for users
5674027e0f4SWang Shilong 				 * to disable forcible compression separately.
5684027e0f4SWang Shilong 				 */
5694027e0f4SWang Shilong 				btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
570a7e252afSMiao Xie 			}
5713cdde224SJeff Mahoney 			if ((btrfs_test_opt(info, COMPRESS) &&
572b7c47bbbSTsutomu Itoh 			     (info->compress_type != saved_compress_type ||
573b7c47bbbSTsutomu Itoh 			      compress_force != saved_compress_force)) ||
5743cdde224SJeff Mahoney 			    (!btrfs_test_opt(info, COMPRESS) &&
575b7c47bbbSTsutomu Itoh 			     no_compress == 1)) {
576f51d2b59SDavid Sterba 				btrfs_info(info, "%s %s compression, level %d",
577b7c47bbbSTsutomu Itoh 					   (compress_force) ? "force" : "use",
578f51d2b59SDavid Sterba 					   compress_type, info->compress_level);
579b7c47bbbSTsutomu Itoh 			}
580b7c47bbbSTsutomu Itoh 			compress_force = false;
581a555f810SChris Mason 			break;
582e18e4809SChris Mason 		case Opt_ssd:
5833cdde224SJeff Mahoney 			btrfs_set_and_info(info, SSD,
584583b7231SHans van Kranenburg 					   "enabling ssd optimizations");
585951e7966SAdam Borowski 			btrfs_clear_opt(info->mount_opt, NOSSD);
586e18e4809SChris Mason 			break;
587451d7585SChris Mason 		case Opt_ssd_spread:
588583b7231SHans van Kranenburg 			btrfs_set_and_info(info, SSD,
589583b7231SHans van Kranenburg 					   "enabling ssd optimizations");
5903cdde224SJeff Mahoney 			btrfs_set_and_info(info, SSD_SPREAD,
591583b7231SHans van Kranenburg 					   "using spread ssd allocation scheme");
592951e7966SAdam Borowski 			btrfs_clear_opt(info->mount_opt, NOSSD);
593451d7585SChris Mason 			break;
5943b30c22fSChris Mason 		case Opt_nossd:
595583b7231SHans van Kranenburg 			btrfs_set_opt(info->mount_opt, NOSSD);
596583b7231SHans van Kranenburg 			btrfs_clear_and_info(info, SSD,
597583b7231SHans van Kranenburg 					     "not using ssd optimizations");
598583b7231SHans van Kranenburg 			btrfs_clear_and_info(info, SSD_SPREAD,
599583b7231SHans van Kranenburg 					     "not using spread ssd allocation scheme");
6003b30c22fSChris Mason 			break;
601842bef58SQu Wenruo 		case Opt_barrier:
6023cdde224SJeff Mahoney 			btrfs_clear_and_info(info, NOBARRIER,
60307802534SQu Wenruo 					     "turning on barriers");
604842bef58SQu Wenruo 			break;
60521ad10cfSChris Mason 		case Opt_nobarrier:
6063cdde224SJeff Mahoney 			btrfs_set_and_info(info, NOBARRIER,
60707802534SQu Wenruo 					   "turning off barriers");
60821ad10cfSChris Mason 			break;
6094543df7eSChris Mason 		case Opt_thread_pool:
6102c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
6112c334e87SWang Shilong 			if (ret) {
6122c334e87SWang Shilong 				goto out;
6132c334e87SWang Shilong 			} else if (intarg > 0) {
6144543df7eSChris Mason 				info->thread_pool_size = intarg;
6152c334e87SWang Shilong 			} else {
6162c334e87SWang Shilong 				ret = -EINVAL;
6172c334e87SWang Shilong 				goto out;
6182c334e87SWang Shilong 			}
6194543df7eSChris Mason 			break;
6206f568d35SChris Mason 		case Opt_max_inline:
621edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
6226f568d35SChris Mason 			if (num) {
62391748467SAkinobu Mita 				info->max_inline = memparse(num, NULL);
6246f568d35SChris Mason 				kfree(num);
6256f568d35SChris Mason 
62615ada040SChris Mason 				if (info->max_inline) {
627feb5f965SMitch Harder 					info->max_inline = min_t(u64,
62815ada040SChris Mason 						info->max_inline,
6290b246afaSJeff Mahoney 						info->sectorsize);
63015ada040SChris Mason 				}
6310b246afaSJeff Mahoney 				btrfs_info(info, "max_inline at %llu",
632c1c9ff7cSGeert Uytterhoeven 					   info->max_inline);
6332c334e87SWang Shilong 			} else {
6342c334e87SWang Shilong 				ret = -ENOMEM;
6352c334e87SWang Shilong 				goto out;
6366f568d35SChris Mason 			}
6376f568d35SChris Mason 			break;
6388f662a76SChris Mason 		case Opt_alloc_start:
6390d0c71b3SDavid Sterba 			btrfs_info(info,
6400d0c71b3SDavid Sterba 				"option alloc_start is obsolete, ignored");
6418f662a76SChris Mason 			break;
642bd0330adSQu Wenruo 		case Opt_acl:
64345ff35d6SGuangliang Zhao #ifdef CONFIG_BTRFS_FS_POSIX_ACL
6441751e8a6SLinus Torvalds 			info->sb->s_flags |= SB_POSIXACL;
645bd0330adSQu Wenruo 			break;
64645ff35d6SGuangliang Zhao #else
6470b246afaSJeff Mahoney 			btrfs_err(info, "support for ACL not compiled in!");
64845ff35d6SGuangliang Zhao 			ret = -EINVAL;
64945ff35d6SGuangliang Zhao 			goto out;
65045ff35d6SGuangliang Zhao #endif
65133268eafSJosef Bacik 		case Opt_noacl:
6521751e8a6SLinus Torvalds 			info->sb->s_flags &= ~SB_POSIXACL;
65333268eafSJosef Bacik 			break;
6543a5e1404SSage Weil 		case Opt_notreelog:
6553cdde224SJeff Mahoney 			btrfs_set_and_info(info, NOTREELOG,
65607802534SQu Wenruo 					   "disabling tree log");
6573a5e1404SSage Weil 			break;
658a88998f2SQu Wenruo 		case Opt_treelog:
6593cdde224SJeff Mahoney 			btrfs_clear_and_info(info, NOTREELOG,
66007802534SQu Wenruo 					     "enabling tree log");
661a88998f2SQu Wenruo 			break;
662fed8f166SQu Wenruo 		case Opt_norecovery:
66396da0919SQu Wenruo 		case Opt_nologreplay:
6643cdde224SJeff Mahoney 			btrfs_set_and_info(info, NOLOGREPLAY,
66596da0919SQu Wenruo 					   "disabling log replay at mount time");
66696da0919SQu Wenruo 			break;
667dccae999SSage Weil 		case Opt_flushoncommit:
6683cdde224SJeff Mahoney 			btrfs_set_and_info(info, FLUSHONCOMMIT,
66907802534SQu Wenruo 					   "turning on flush-on-commit");
670dccae999SSage Weil 			break;
6712c9ee856SQu Wenruo 		case Opt_noflushoncommit:
6723cdde224SJeff Mahoney 			btrfs_clear_and_info(info, FLUSHONCOMMIT,
67307802534SQu Wenruo 					     "turning off flush-on-commit");
6742c9ee856SQu Wenruo 			break;
67597e728d4SJosef Bacik 		case Opt_ratio:
6762c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
6772c334e87SWang Shilong 			if (ret) {
6782c334e87SWang Shilong 				goto out;
6792c334e87SWang Shilong 			} else if (intarg >= 0) {
68097e728d4SJosef Bacik 				info->metadata_ratio = intarg;
6810b246afaSJeff Mahoney 				btrfs_info(info, "metadata ratio %d",
68297e728d4SJosef Bacik 					   info->metadata_ratio);
6832c334e87SWang Shilong 			} else {
6842c334e87SWang Shilong 				ret = -EINVAL;
6852c334e87SWang Shilong 				goto out;
68697e728d4SJosef Bacik 			}
68797e728d4SJosef Bacik 			break;
688e244a0aeSChristoph Hellwig 		case Opt_discard:
6893cdde224SJeff Mahoney 			btrfs_set_and_info(info, DISCARD,
69007802534SQu Wenruo 					   "turning on discard");
691e244a0aeSChristoph Hellwig 			break;
692e07a2adeSQu Wenruo 		case Opt_nodiscard:
6933cdde224SJeff Mahoney 			btrfs_clear_and_info(info, DISCARD,
69407802534SQu Wenruo 					     "turning off discard");
695e07a2adeSQu Wenruo 			break;
6960af3d00bSJosef Bacik 		case Opt_space_cache:
69770f6d82eSOmar Sandoval 		case Opt_space_cache_version:
69870f6d82eSOmar Sandoval 			if (token == Opt_space_cache ||
69970f6d82eSOmar Sandoval 			    strcmp(args[0].from, "v1") == 0) {
7000b246afaSJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
70170f6d82eSOmar Sandoval 						FREE_SPACE_TREE);
7023cdde224SJeff Mahoney 				btrfs_set_and_info(info, SPACE_CACHE,
70307802534SQu Wenruo 					   "enabling disk space caching");
70470f6d82eSOmar Sandoval 			} else if (strcmp(args[0].from, "v2") == 0) {
7050b246afaSJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
70670f6d82eSOmar Sandoval 						SPACE_CACHE);
7070b246afaSJeff Mahoney 				btrfs_set_and_info(info, FREE_SPACE_TREE,
70870f6d82eSOmar Sandoval 						   "enabling free space tree");
70970f6d82eSOmar Sandoval 			} else {
71070f6d82eSOmar Sandoval 				ret = -EINVAL;
71170f6d82eSOmar Sandoval 				goto out;
71270f6d82eSOmar Sandoval 			}
7130de90876SJosef Bacik 			break;
714f420ee1eSStefan Behrens 		case Opt_rescan_uuid_tree:
715f420ee1eSStefan Behrens 			btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
716f420ee1eSStefan Behrens 			break;
71773bc1876SJosef Bacik 		case Opt_no_space_cache:
7183cdde224SJeff Mahoney 			if (btrfs_test_opt(info, SPACE_CACHE)) {
7190b246afaSJeff Mahoney 				btrfs_clear_and_info(info, SPACE_CACHE,
72007802534SQu Wenruo 					     "disabling disk space caching");
72170f6d82eSOmar Sandoval 			}
7223cdde224SJeff Mahoney 			if (btrfs_test_opt(info, FREE_SPACE_TREE)) {
7230b246afaSJeff Mahoney 				btrfs_clear_and_info(info, FREE_SPACE_TREE,
72470f6d82eSOmar Sandoval 					     "disabling free space tree");
72570f6d82eSOmar Sandoval 			}
72673bc1876SJosef Bacik 			break;
7274b9465cbSChris Mason 		case Opt_inode_cache:
7287e1876acSDavid Sterba 			btrfs_set_pending_and_info(info, INODE_MAP_CACHE,
72907802534SQu Wenruo 					   "enabling inode map caching");
7303818aea2SQu Wenruo 			break;
7313818aea2SQu Wenruo 		case Opt_noinode_cache:
7327e1876acSDavid Sterba 			btrfs_clear_pending_and_info(info, INODE_MAP_CACHE,
73307802534SQu Wenruo 					     "disabling inode map caching");
7344b9465cbSChris Mason 			break;
73588c2ba3bSJosef Bacik 		case Opt_clear_cache:
7363cdde224SJeff Mahoney 			btrfs_set_and_info(info, CLEAR_CACHE,
73707802534SQu Wenruo 					   "force clearing of disk cache");
7380af3d00bSJosef Bacik 			break;
7394260f7c7SSage Weil 		case Opt_user_subvol_rm_allowed:
7404260f7c7SSage Weil 			btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
7414260f7c7SSage Weil 			break;
74291435650SChris Mason 		case Opt_enospc_debug:
74391435650SChris Mason 			btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
74491435650SChris Mason 			break;
74553036293SQu Wenruo 		case Opt_noenospc_debug:
74653036293SQu Wenruo 			btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG);
74753036293SQu Wenruo 			break;
7484cb5300bSChris Mason 		case Opt_defrag:
7493cdde224SJeff Mahoney 			btrfs_set_and_info(info, AUTO_DEFRAG,
75007802534SQu Wenruo 					   "enabling auto defrag");
7514cb5300bSChris Mason 			break;
752fc0ca9afSQu Wenruo 		case Opt_nodefrag:
7533cdde224SJeff Mahoney 			btrfs_clear_and_info(info, AUTO_DEFRAG,
75407802534SQu Wenruo 					     "disabling auto defrag");
755fc0ca9afSQu Wenruo 			break;
756af31f5e5SChris Mason 		case Opt_recovery:
7570b246afaSJeff Mahoney 			btrfs_warn(info,
7588dcddfa0SQu Wenruo 				   "'recovery' is deprecated, use 'usebackuproot' instead");
7598dcddfa0SQu Wenruo 		case Opt_usebackuproot:
7600b246afaSJeff Mahoney 			btrfs_info(info,
7618dcddfa0SQu Wenruo 				   "trying to use backup root at mount time");
7628dcddfa0SQu Wenruo 			btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
763af31f5e5SChris Mason 			break;
7649555c6c1SIlya Dryomov 		case Opt_skip_balance:
7659555c6c1SIlya Dryomov 			btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
7669555c6c1SIlya Dryomov 			break;
76721adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
76821adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
7690b246afaSJeff Mahoney 			btrfs_info(info,
770efe120a0SFrank Holton 				   "enabling check integrity including extent data");
77121adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt,
77221adbd5cSStefan Behrens 				      CHECK_INTEGRITY_INCLUDING_EXTENT_DATA);
77321adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
77421adbd5cSStefan Behrens 			break;
77521adbd5cSStefan Behrens 		case Opt_check_integrity:
7760b246afaSJeff Mahoney 			btrfs_info(info, "enabling check integrity");
77721adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
77821adbd5cSStefan Behrens 			break;
77921adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
7802c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
7812c334e87SWang Shilong 			if (ret) {
7822c334e87SWang Shilong 				goto out;
7832c334e87SWang Shilong 			} else if (intarg >= 0) {
78421adbd5cSStefan Behrens 				info->check_integrity_print_mask = intarg;
7850b246afaSJeff Mahoney 				btrfs_info(info,
7865d163e0eSJeff Mahoney 					   "check_integrity_print_mask 0x%x",
78721adbd5cSStefan Behrens 					   info->check_integrity_print_mask);
7882c334e87SWang Shilong 			} else {
7892c334e87SWang Shilong 				ret = -EINVAL;
7902c334e87SWang Shilong 				goto out;
79121adbd5cSStefan Behrens 			}
79221adbd5cSStefan Behrens 			break;
79321adbd5cSStefan Behrens #else
79421adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
79521adbd5cSStefan Behrens 		case Opt_check_integrity:
79621adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
7970b246afaSJeff Mahoney 			btrfs_err(info,
798efe120a0SFrank Holton 				  "support for check_integrity* not compiled in!");
79921adbd5cSStefan Behrens 			ret = -EINVAL;
80021adbd5cSStefan Behrens 			goto out;
80121adbd5cSStefan Behrens #endif
8028c342930SJeff Mahoney 		case Opt_fatal_errors:
8038c342930SJeff Mahoney 			if (strcmp(args[0].from, "panic") == 0)
8048c342930SJeff Mahoney 				btrfs_set_opt(info->mount_opt,
8058c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
8068c342930SJeff Mahoney 			else if (strcmp(args[0].from, "bug") == 0)
8078c342930SJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
8088c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
8098c342930SJeff Mahoney 			else {
8108c342930SJeff Mahoney 				ret = -EINVAL;
8118c342930SJeff Mahoney 				goto out;
8128c342930SJeff Mahoney 			}
8138c342930SJeff Mahoney 			break;
8148b87dc17SDavid Sterba 		case Opt_commit_interval:
8158b87dc17SDavid Sterba 			intarg = 0;
8168b87dc17SDavid Sterba 			ret = match_int(&args[0], &intarg);
8178b87dc17SDavid Sterba 			if (ret < 0) {
8180b246afaSJeff Mahoney 				btrfs_err(info, "invalid commit interval");
8198b87dc17SDavid Sterba 				ret = -EINVAL;
8208b87dc17SDavid Sterba 				goto out;
8218b87dc17SDavid Sterba 			}
8228b87dc17SDavid Sterba 			if (intarg > 0) {
8238b87dc17SDavid Sterba 				if (intarg > 300) {
8240b246afaSJeff Mahoney 					btrfs_warn(info,
8255d163e0eSJeff Mahoney 						"excessive commit interval %d",
8268b87dc17SDavid Sterba 						intarg);
8278b87dc17SDavid Sterba 				}
8288b87dc17SDavid Sterba 				info->commit_interval = intarg;
8298b87dc17SDavid Sterba 			} else {
8300b246afaSJeff Mahoney 				btrfs_info(info,
8315d163e0eSJeff Mahoney 					   "using default commit interval %ds",
8328b87dc17SDavid Sterba 					   BTRFS_DEFAULT_COMMIT_INTERVAL);
8338b87dc17SDavid Sterba 				info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
8348b87dc17SDavid Sterba 			}
8358b87dc17SDavid Sterba 			break;
836d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
837d0bd4560SJosef Bacik 		case Opt_fragment_all:
8380b246afaSJeff Mahoney 			btrfs_info(info, "fragmenting all space");
839d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
840d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA);
841d0bd4560SJosef Bacik 			break;
842d0bd4560SJosef Bacik 		case Opt_fragment_metadata:
8430b246afaSJeff Mahoney 			btrfs_info(info, "fragmenting metadata");
844d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt,
845d0bd4560SJosef Bacik 				      FRAGMENT_METADATA);
846d0bd4560SJosef Bacik 			break;
847d0bd4560SJosef Bacik 		case Opt_fragment_data:
8480b246afaSJeff Mahoney 			btrfs_info(info, "fragmenting data");
849d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
850d0bd4560SJosef Bacik 			break;
851d0bd4560SJosef Bacik #endif
852fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
853fb592373SJosef Bacik 		case Opt_ref_verify:
854fb592373SJosef Bacik 			btrfs_info(info, "doing ref verification");
855fb592373SJosef Bacik 			btrfs_set_opt(info->mount_opt, REF_VERIFY);
856fb592373SJosef Bacik 			break;
857fb592373SJosef Bacik #endif
858a7a3f7caSSage Weil 		case Opt_err:
8590b246afaSJeff Mahoney 			btrfs_info(info, "unrecognized mount option '%s'", p);
860a7a3f7caSSage Weil 			ret = -EINVAL;
861a7a3f7caSSage Weil 			goto out;
86295e05289SChris Mason 		default:
863be20aa9dSChris Mason 			break;
86495e05289SChris Mason 		}
86595e05289SChris Mason 	}
86696da0919SQu Wenruo check:
86796da0919SQu Wenruo 	/*
86896da0919SQu Wenruo 	 * Extra check for current option against current flag
86996da0919SQu Wenruo 	 */
8701751e8a6SLinus Torvalds 	if (btrfs_test_opt(info, NOLOGREPLAY) && !(new_flags & SB_RDONLY)) {
8710b246afaSJeff Mahoney 		btrfs_err(info,
87296da0919SQu Wenruo 			  "nologreplay must be used with ro mount option");
87396da0919SQu Wenruo 		ret = -EINVAL;
87496da0919SQu Wenruo 	}
875a7a3f7caSSage Weil out:
8760b246afaSJeff Mahoney 	if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) &&
8773cdde224SJeff Mahoney 	    !btrfs_test_opt(info, FREE_SPACE_TREE) &&
8783cdde224SJeff Mahoney 	    !btrfs_test_opt(info, CLEAR_CACHE)) {
8790b246afaSJeff Mahoney 		btrfs_err(info, "cannot disable free space tree");
88070f6d82eSOmar Sandoval 		ret = -EINVAL;
88170f6d82eSOmar Sandoval 
88270f6d82eSOmar Sandoval 	}
8833cdde224SJeff Mahoney 	if (!ret && btrfs_test_opt(info, SPACE_CACHE))
8840b246afaSJeff Mahoney 		btrfs_info(info, "disk space caching is enabled");
8853cdde224SJeff Mahoney 	if (!ret && btrfs_test_opt(info, FREE_SPACE_TREE))
8860b246afaSJeff Mahoney 		btrfs_info(info, "using free space tree");
887da495eccSJosef Bacik 	kfree(orig);
888a7a3f7caSSage Weil 	return ret;
889edf24abeSChristoph Hellwig }
890edf24abeSChristoph Hellwig 
891edf24abeSChristoph Hellwig /*
892edf24abeSChristoph Hellwig  * Parse mount options that are required early in the mount process.
893edf24abeSChristoph Hellwig  *
894edf24abeSChristoph Hellwig  * All other options will be parsed on much later in the mount process and
895edf24abeSChristoph Hellwig  * only when we need to allocate a new super block.
896edf24abeSChristoph Hellwig  */
89797288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags,
89873f73415SJosef Bacik 		void *holder, char **subvol_name, u64 *subvol_objectid,
8995e2a4b25SDavid Sterba 		struct btrfs_fs_devices **fs_devices)
900edf24abeSChristoph Hellwig {
901edf24abeSChristoph Hellwig 	substring_t args[MAX_OPT_ARGS];
90283c8c9bdSJeff Liu 	char *device_name, *opts, *orig, *p;
9031493381fSWang Shilong 	char *num = NULL;
904edf24abeSChristoph Hellwig 	int error = 0;
905edf24abeSChristoph Hellwig 
906edf24abeSChristoph Hellwig 	if (!options)
907830c4adbSJosef Bacik 		return 0;
908edf24abeSChristoph Hellwig 
909edf24abeSChristoph Hellwig 	/*
910edf24abeSChristoph Hellwig 	 * strsep changes the string, duplicate it because parse_options
911edf24abeSChristoph Hellwig 	 * gets called twice
912edf24abeSChristoph Hellwig 	 */
913edf24abeSChristoph Hellwig 	opts = kstrdup(options, GFP_KERNEL);
914edf24abeSChristoph Hellwig 	if (!opts)
915edf24abeSChristoph Hellwig 		return -ENOMEM;
9163f3d0bc0STero Roponen 	orig = opts;
917edf24abeSChristoph Hellwig 
918edf24abeSChristoph Hellwig 	while ((p = strsep(&opts, ",")) != NULL) {
919edf24abeSChristoph Hellwig 		int token;
920edf24abeSChristoph Hellwig 		if (!*p)
921edf24abeSChristoph Hellwig 			continue;
922edf24abeSChristoph Hellwig 
923edf24abeSChristoph Hellwig 		token = match_token(p, tokens, args);
924edf24abeSChristoph Hellwig 		switch (token) {
925edf24abeSChristoph Hellwig 		case Opt_subvol:
926a90e8b6fSIlya Dryomov 			kfree(*subvol_name);
927edf24abeSChristoph Hellwig 			*subvol_name = match_strdup(&args[0]);
9282c334e87SWang Shilong 			if (!*subvol_name) {
9292c334e87SWang Shilong 				error = -ENOMEM;
9302c334e87SWang Shilong 				goto out;
9312c334e87SWang Shilong 			}
932edf24abeSChristoph Hellwig 			break;
93373f73415SJosef Bacik 		case Opt_subvolid:
9341493381fSWang Shilong 			num = match_strdup(&args[0]);
9351493381fSWang Shilong 			if (num) {
9361493381fSWang Shilong 				*subvol_objectid = memparse(num, NULL);
9371493381fSWang Shilong 				kfree(num);
9384849f01dSJosef Bacik 				/* we want the original fs_tree */
9391493381fSWang Shilong 				if (!*subvol_objectid)
9404849f01dSJosef Bacik 					*subvol_objectid =
9414849f01dSJosef Bacik 						BTRFS_FS_TREE_OBJECTID;
9422c334e87SWang Shilong 			} else {
9432c334e87SWang Shilong 				error = -EINVAL;
9442c334e87SWang Shilong 				goto out;
9454849f01dSJosef Bacik 			}
94673f73415SJosef Bacik 			break;
947e15d0542SXin Zhong 		case Opt_subvolrootid:
94862e85577SJeff Mahoney 			pr_warn("BTRFS: 'subvolrootid' mount option is deprecated and has no effect\n");
949e15d0542SXin Zhong 			break;
95043e570b0SChristoph Hellwig 		case Opt_device:
95183c8c9bdSJeff Liu 			device_name = match_strdup(&args[0]);
95283c8c9bdSJeff Liu 			if (!device_name) {
95383c8c9bdSJeff Liu 				error = -ENOMEM;
95483c8c9bdSJeff Liu 				goto out;
95583c8c9bdSJeff Liu 			}
95683c8c9bdSJeff Liu 			error = btrfs_scan_one_device(device_name,
95743e570b0SChristoph Hellwig 					flags, holder, fs_devices);
95883c8c9bdSJeff Liu 			kfree(device_name);
95943e570b0SChristoph Hellwig 			if (error)
960830c4adbSJosef Bacik 				goto out;
96143e570b0SChristoph Hellwig 			break;
962edf24abeSChristoph Hellwig 		default:
963edf24abeSChristoph Hellwig 			break;
964edf24abeSChristoph Hellwig 		}
965edf24abeSChristoph Hellwig 	}
966edf24abeSChristoph Hellwig 
967edf24abeSChristoph Hellwig out:
968830c4adbSJosef Bacik 	kfree(orig);
969edf24abeSChristoph Hellwig 	return error;
97095e05289SChris Mason }
97195e05289SChris Mason 
97205dbe683SOmar Sandoval static char *get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
97373f73415SJosef Bacik 					   u64 subvol_objectid)
97473f73415SJosef Bacik {
975815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
97605dbe683SOmar Sandoval 	struct btrfs_root *fs_root;
97705dbe683SOmar Sandoval 	struct btrfs_root_ref *root_ref;
97805dbe683SOmar Sandoval 	struct btrfs_inode_ref *inode_ref;
97905dbe683SOmar Sandoval 	struct btrfs_key key;
98005dbe683SOmar Sandoval 	struct btrfs_path *path = NULL;
98105dbe683SOmar Sandoval 	char *name = NULL, *ptr;
98205dbe683SOmar Sandoval 	u64 dirid;
98305dbe683SOmar Sandoval 	int len;
98405dbe683SOmar Sandoval 	int ret;
98505dbe683SOmar Sandoval 
98605dbe683SOmar Sandoval 	path = btrfs_alloc_path();
98705dbe683SOmar Sandoval 	if (!path) {
98805dbe683SOmar Sandoval 		ret = -ENOMEM;
98905dbe683SOmar Sandoval 		goto err;
99005dbe683SOmar Sandoval 	}
99105dbe683SOmar Sandoval 	path->leave_spinning = 1;
99205dbe683SOmar Sandoval 
9933ec83621SDavid Sterba 	name = kmalloc(PATH_MAX, GFP_KERNEL);
99405dbe683SOmar Sandoval 	if (!name) {
99505dbe683SOmar Sandoval 		ret = -ENOMEM;
99605dbe683SOmar Sandoval 		goto err;
99705dbe683SOmar Sandoval 	}
99805dbe683SOmar Sandoval 	ptr = name + PATH_MAX - 1;
99905dbe683SOmar Sandoval 	ptr[0] = '\0';
100005dbe683SOmar Sandoval 
100105dbe683SOmar Sandoval 	/*
100205dbe683SOmar Sandoval 	 * Walk up the subvolume trees in the tree of tree roots by root
100305dbe683SOmar Sandoval 	 * backrefs until we hit the top-level subvolume.
100405dbe683SOmar Sandoval 	 */
100505dbe683SOmar Sandoval 	while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
100605dbe683SOmar Sandoval 		key.objectid = subvol_objectid;
100705dbe683SOmar Sandoval 		key.type = BTRFS_ROOT_BACKREF_KEY;
100805dbe683SOmar Sandoval 		key.offset = (u64)-1;
100905dbe683SOmar Sandoval 
101005dbe683SOmar Sandoval 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
101105dbe683SOmar Sandoval 		if (ret < 0) {
101205dbe683SOmar Sandoval 			goto err;
101305dbe683SOmar Sandoval 		} else if (ret > 0) {
101405dbe683SOmar Sandoval 			ret = btrfs_previous_item(root, path, subvol_objectid,
101505dbe683SOmar Sandoval 						  BTRFS_ROOT_BACKREF_KEY);
101605dbe683SOmar Sandoval 			if (ret < 0) {
101705dbe683SOmar Sandoval 				goto err;
101805dbe683SOmar Sandoval 			} else if (ret > 0) {
101905dbe683SOmar Sandoval 				ret = -ENOENT;
102005dbe683SOmar Sandoval 				goto err;
102105dbe683SOmar Sandoval 			}
102205dbe683SOmar Sandoval 		}
102305dbe683SOmar Sandoval 
102405dbe683SOmar Sandoval 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
102505dbe683SOmar Sandoval 		subvol_objectid = key.offset;
102605dbe683SOmar Sandoval 
102705dbe683SOmar Sandoval 		root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
102805dbe683SOmar Sandoval 					  struct btrfs_root_ref);
102905dbe683SOmar Sandoval 		len = btrfs_root_ref_name_len(path->nodes[0], root_ref);
103005dbe683SOmar Sandoval 		ptr -= len + 1;
103105dbe683SOmar Sandoval 		if (ptr < name) {
103205dbe683SOmar Sandoval 			ret = -ENAMETOOLONG;
103305dbe683SOmar Sandoval 			goto err;
103405dbe683SOmar Sandoval 		}
103505dbe683SOmar Sandoval 		read_extent_buffer(path->nodes[0], ptr + 1,
103605dbe683SOmar Sandoval 				   (unsigned long)(root_ref + 1), len);
103705dbe683SOmar Sandoval 		ptr[0] = '/';
103805dbe683SOmar Sandoval 		dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);
103905dbe683SOmar Sandoval 		btrfs_release_path(path);
104005dbe683SOmar Sandoval 
104105dbe683SOmar Sandoval 		key.objectid = subvol_objectid;
104205dbe683SOmar Sandoval 		key.type = BTRFS_ROOT_ITEM_KEY;
104305dbe683SOmar Sandoval 		key.offset = (u64)-1;
104405dbe683SOmar Sandoval 		fs_root = btrfs_read_fs_root_no_name(fs_info, &key);
104505dbe683SOmar Sandoval 		if (IS_ERR(fs_root)) {
104605dbe683SOmar Sandoval 			ret = PTR_ERR(fs_root);
104705dbe683SOmar Sandoval 			goto err;
104805dbe683SOmar Sandoval 		}
104905dbe683SOmar Sandoval 
105005dbe683SOmar Sandoval 		/*
105105dbe683SOmar Sandoval 		 * Walk up the filesystem tree by inode refs until we hit the
105205dbe683SOmar Sandoval 		 * root directory.
105305dbe683SOmar Sandoval 		 */
105405dbe683SOmar Sandoval 		while (dirid != BTRFS_FIRST_FREE_OBJECTID) {
105505dbe683SOmar Sandoval 			key.objectid = dirid;
105605dbe683SOmar Sandoval 			key.type = BTRFS_INODE_REF_KEY;
105705dbe683SOmar Sandoval 			key.offset = (u64)-1;
105805dbe683SOmar Sandoval 
105905dbe683SOmar Sandoval 			ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
106005dbe683SOmar Sandoval 			if (ret < 0) {
106105dbe683SOmar Sandoval 				goto err;
106205dbe683SOmar Sandoval 			} else if (ret > 0) {
106305dbe683SOmar Sandoval 				ret = btrfs_previous_item(fs_root, path, dirid,
106405dbe683SOmar Sandoval 							  BTRFS_INODE_REF_KEY);
106505dbe683SOmar Sandoval 				if (ret < 0) {
106605dbe683SOmar Sandoval 					goto err;
106705dbe683SOmar Sandoval 				} else if (ret > 0) {
106805dbe683SOmar Sandoval 					ret = -ENOENT;
106905dbe683SOmar Sandoval 					goto err;
107005dbe683SOmar Sandoval 				}
107105dbe683SOmar Sandoval 			}
107205dbe683SOmar Sandoval 
107305dbe683SOmar Sandoval 			btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
107405dbe683SOmar Sandoval 			dirid = key.offset;
107505dbe683SOmar Sandoval 
107605dbe683SOmar Sandoval 			inode_ref = btrfs_item_ptr(path->nodes[0],
107705dbe683SOmar Sandoval 						   path->slots[0],
107805dbe683SOmar Sandoval 						   struct btrfs_inode_ref);
107905dbe683SOmar Sandoval 			len = btrfs_inode_ref_name_len(path->nodes[0],
108005dbe683SOmar Sandoval 						       inode_ref);
108105dbe683SOmar Sandoval 			ptr -= len + 1;
108205dbe683SOmar Sandoval 			if (ptr < name) {
108305dbe683SOmar Sandoval 				ret = -ENAMETOOLONG;
108405dbe683SOmar Sandoval 				goto err;
108505dbe683SOmar Sandoval 			}
108605dbe683SOmar Sandoval 			read_extent_buffer(path->nodes[0], ptr + 1,
108705dbe683SOmar Sandoval 					   (unsigned long)(inode_ref + 1), len);
108805dbe683SOmar Sandoval 			ptr[0] = '/';
108905dbe683SOmar Sandoval 			btrfs_release_path(path);
109005dbe683SOmar Sandoval 		}
109105dbe683SOmar Sandoval 	}
109205dbe683SOmar Sandoval 
109305dbe683SOmar Sandoval 	btrfs_free_path(path);
109405dbe683SOmar Sandoval 	if (ptr == name + PATH_MAX - 1) {
109505dbe683SOmar Sandoval 		name[0] = '/';
109605dbe683SOmar Sandoval 		name[1] = '\0';
109705dbe683SOmar Sandoval 	} else {
109805dbe683SOmar Sandoval 		memmove(name, ptr, name + PATH_MAX - ptr);
109905dbe683SOmar Sandoval 	}
110005dbe683SOmar Sandoval 	return name;
110105dbe683SOmar Sandoval 
110205dbe683SOmar Sandoval err:
110305dbe683SOmar Sandoval 	btrfs_free_path(path);
110405dbe683SOmar Sandoval 	kfree(name);
110505dbe683SOmar Sandoval 	return ERR_PTR(ret);
110605dbe683SOmar Sandoval }
110705dbe683SOmar Sandoval 
110805dbe683SOmar Sandoval static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid)
110905dbe683SOmar Sandoval {
111005dbe683SOmar Sandoval 	struct btrfs_root *root = fs_info->tree_root;
111173f73415SJosef Bacik 	struct btrfs_dir_item *di;
111273f73415SJosef Bacik 	struct btrfs_path *path;
111373f73415SJosef Bacik 	struct btrfs_key location;
111473f73415SJosef Bacik 	u64 dir_id;
111573f73415SJosef Bacik 
111673f73415SJosef Bacik 	path = btrfs_alloc_path();
111773f73415SJosef Bacik 	if (!path)
111805dbe683SOmar Sandoval 		return -ENOMEM;
111973f73415SJosef Bacik 	path->leave_spinning = 1;
112073f73415SJosef Bacik 
112173f73415SJosef Bacik 	/*
112273f73415SJosef Bacik 	 * Find the "default" dir item which points to the root item that we
112373f73415SJosef Bacik 	 * will mount by default if we haven't been given a specific subvolume
112473f73415SJosef Bacik 	 * to mount.
112573f73415SJosef Bacik 	 */
1126815745cfSAl Viro 	dir_id = btrfs_super_root_dir(fs_info->super_copy);
112773f73415SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
1128b0839166SJulia Lawall 	if (IS_ERR(di)) {
1129b0839166SJulia Lawall 		btrfs_free_path(path);
113005dbe683SOmar Sandoval 		return PTR_ERR(di);
1131b0839166SJulia Lawall 	}
113273f73415SJosef Bacik 	if (!di) {
113373f73415SJosef Bacik 		/*
113473f73415SJosef Bacik 		 * Ok the default dir item isn't there.  This is weird since
113573f73415SJosef Bacik 		 * it's always been there, but don't freak out, just try and
113605dbe683SOmar Sandoval 		 * mount the top-level subvolume.
113773f73415SJosef Bacik 		 */
113873f73415SJosef Bacik 		btrfs_free_path(path);
113905dbe683SOmar Sandoval 		*objectid = BTRFS_FS_TREE_OBJECTID;
114005dbe683SOmar Sandoval 		return 0;
114173f73415SJosef Bacik 	}
114273f73415SJosef Bacik 
114373f73415SJosef Bacik 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
114473f73415SJosef Bacik 	btrfs_free_path(path);
114505dbe683SOmar Sandoval 	*objectid = location.objectid;
114605dbe683SOmar Sandoval 	return 0;
114773f73415SJosef Bacik }
114873f73415SJosef Bacik 
11498a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb,
11508a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
115156e033a7SDavid Sterba 			    void *data)
11522e635a27SChris Mason {
11532e635a27SChris Mason 	struct inode *inode;
1154815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
11555d4f98a2SYan Zheng 	struct btrfs_key key;
115639279cc3SChris Mason 	int err;
11572e635a27SChris Mason 
11582e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
11592e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
1160e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
1161af53d29aSAl Viro 	sb->s_d_op = &btrfs_dentry_operations;
1162be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
11635103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
11642e635a27SChris Mason 	sb->s_time_gran = 1;
11650eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL
11661751e8a6SLinus Torvalds 	sb->s_flags |= SB_POSIXACL;
116749cf6f45SChris Ball #endif
1168357fdad0SMatthew Garrett 	sb->s_flags |= SB_I_VERSION;
1169da2f0f74SChris Mason 	sb->s_iflags |= SB_I_CGROUPWB;
11709e11ceeeSJan Kara 
11719e11ceeeSJan Kara 	err = super_setup_bdi(sb);
11729e11ceeeSJan Kara 	if (err) {
11739e11ceeeSJan Kara 		btrfs_err(fs_info, "super_setup_bdi failed");
11749e11ceeeSJan Kara 		return err;
11759e11ceeeSJan Kara 	}
11769e11ceeeSJan Kara 
1177ad2b2c80SAl Viro 	err = open_ctree(sb, fs_devices, (char *)data);
1178ad2b2c80SAl Viro 	if (err) {
1179ab8d0fc4SJeff Mahoney 		btrfs_err(fs_info, "open_ctree failed");
1180ad2b2c80SAl Viro 		return err;
1181e20d96d6SChris Mason 	}
1182b888db2bSChris Mason 
11835d4f98a2SYan Zheng 	key.objectid = BTRFS_FIRST_FREE_OBJECTID;
11845d4f98a2SYan Zheng 	key.type = BTRFS_INODE_ITEM_KEY;
11855d4f98a2SYan Zheng 	key.offset = 0;
118698c7089cSAl Viro 	inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL);
11875d4f98a2SYan Zheng 	if (IS_ERR(inode)) {
11885d4f98a2SYan Zheng 		err = PTR_ERR(inode);
118939279cc3SChris Mason 		goto fail_close;
119039279cc3SChris Mason 	}
11912e635a27SChris Mason 
119248fde701SAl Viro 	sb->s_root = d_make_root(inode);
119348fde701SAl Viro 	if (!sb->s_root) {
119439279cc3SChris Mason 		err = -ENOMEM;
119539279cc3SChris Mason 		goto fail_close;
11962e635a27SChris Mason 	}
119758176a96SJosef Bacik 
119890a887c9SDan Magenheimer 	cleancache_init_fs(sb);
11991751e8a6SLinus Torvalds 	sb->s_flags |= SB_ACTIVE;
12002e635a27SChris Mason 	return 0;
12012e635a27SChris Mason 
120239279cc3SChris Mason fail_close:
12036bccf3abSJeff Mahoney 	close_ctree(fs_info);
1204d5719762SChris Mason 	return err;
1205d5719762SChris Mason }
1206d5719762SChris Mason 
12076bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
1208d5719762SChris Mason {
1209d5719762SChris Mason 	struct btrfs_trans_handle *trans;
1210815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1211815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
1212df2ce34cSChris Mason 
1213bc074524SJeff Mahoney 	trace_btrfs_sync_fs(fs_info, wait);
12141abe9b8aSliubo 
1215d561c025SChris Mason 	if (!wait) {
1216815745cfSAl Viro 		filemap_flush(fs_info->btree_inode->i_mapping);
1217df2ce34cSChris Mason 		return 0;
1218d561c025SChris Mason 	}
1219771ed689SChris Mason 
12206374e57aSChris Mason 	btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
1221771ed689SChris Mason 
1222d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
122360376ce4SJosef Bacik 	if (IS_ERR(trans)) {
1224354aa0fbSMiao Xie 		/* no transaction, don't bother */
12256b5fe46dSDavid Sterba 		if (PTR_ERR(trans) == -ENOENT) {
12266b5fe46dSDavid Sterba 			/*
12276b5fe46dSDavid Sterba 			 * Exit unless we have some pending changes
12286b5fe46dSDavid Sterba 			 * that need to go through commit
12296b5fe46dSDavid Sterba 			 */
12306b5fe46dSDavid Sterba 			if (fs_info->pending_changes == 0)
1231bd7de2c9SJosef Bacik 				return 0;
1232a53f4f8eSQu Wenruo 			/*
1233a53f4f8eSQu Wenruo 			 * A non-blocking test if the fs is frozen. We must not
1234a53f4f8eSQu Wenruo 			 * start a new transaction here otherwise a deadlock
1235a53f4f8eSQu Wenruo 			 * happens. The pending operations are delayed to the
1236a53f4f8eSQu Wenruo 			 * next commit after thawing.
1237a53f4f8eSQu Wenruo 			 */
1238a7e3c5f2SRakesh Pandit 			if (sb_start_write_trylock(sb))
1239a7e3c5f2SRakesh Pandit 				sb_end_write(sb);
1240a53f4f8eSQu Wenruo 			else
1241a53f4f8eSQu Wenruo 				return 0;
12426b5fe46dSDavid Sterba 			trans = btrfs_start_transaction(root, 0);
124360376ce4SJosef Bacik 		}
124498bd5c54SDavid Sterba 		if (IS_ERR(trans))
124598bd5c54SDavid Sterba 			return PTR_ERR(trans);
12466b5fe46dSDavid Sterba 	}
12473a45bb20SJeff Mahoney 	return btrfs_commit_transaction(trans);
1248d5719762SChris Mason }
1249d5719762SChris Mason 
125034c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
1251a9572a15SEric Paris {
1252815745cfSAl Viro 	struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
12530f628c63SDavid Sterba 	const char *compress_type;
1254a9572a15SEric Paris 
12553cdde224SJeff Mahoney 	if (btrfs_test_opt(info, DEGRADED))
1256a9572a15SEric Paris 		seq_puts(seq, ",degraded");
12573cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NODATASUM))
1258a9572a15SEric Paris 		seq_puts(seq, ",nodatasum");
12593cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NODATACOW))
1260a9572a15SEric Paris 		seq_puts(seq, ",nodatacow");
12613cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOBARRIER))
1262a9572a15SEric Paris 		seq_puts(seq, ",nobarrier");
126395ac567aSFilipe David Borba Manana 	if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
1264c1c9ff7cSGeert Uytterhoeven 		seq_printf(seq, ",max_inline=%llu", info->max_inline);
1265a9572a15SEric Paris 	if (info->thread_pool_size !=  min_t(unsigned long,
1266a9572a15SEric Paris 					     num_online_cpus() + 2, 8))
1267a9572a15SEric Paris 		seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
12683cdde224SJeff Mahoney 	if (btrfs_test_opt(info, COMPRESS)) {
12690f628c63SDavid Sterba 		compress_type = btrfs_compress_type2str(info->compress_type);
12703cdde224SJeff Mahoney 		if (btrfs_test_opt(info, FORCE_COMPRESS))
1271200da64eSTsutomu Itoh 			seq_printf(seq, ",compress-force=%s", compress_type);
1272200da64eSTsutomu Itoh 		else
1273200da64eSTsutomu Itoh 			seq_printf(seq, ",compress=%s", compress_type);
1274f51d2b59SDavid Sterba 		if (info->compress_level)
1275fa4d885aSAdam Borowski 			seq_printf(seq, ":%d", info->compress_level);
1276200da64eSTsutomu Itoh 	}
12773cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOSSD))
1278c289811cSChris Mason 		seq_puts(seq, ",nossd");
12793cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SSD_SPREAD))
1280451d7585SChris Mason 		seq_puts(seq, ",ssd_spread");
12813cdde224SJeff Mahoney 	else if (btrfs_test_opt(info, SSD))
1282a9572a15SEric Paris 		seq_puts(seq, ",ssd");
12833cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOTREELOG))
12846b65c5c6SSage Weil 		seq_puts(seq, ",notreelog");
12853cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOLOGREPLAY))
128696da0919SQu Wenruo 		seq_puts(seq, ",nologreplay");
12873cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FLUSHONCOMMIT))
12886b65c5c6SSage Weil 		seq_puts(seq, ",flushoncommit");
12893cdde224SJeff Mahoney 	if (btrfs_test_opt(info, DISCARD))
129020a5239aSMatthew Wilcox 		seq_puts(seq, ",discard");
12911751e8a6SLinus Torvalds 	if (!(info->sb->s_flags & SB_POSIXACL))
1292a9572a15SEric Paris 		seq_puts(seq, ",noacl");
12933cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SPACE_CACHE))
1294200da64eSTsutomu Itoh 		seq_puts(seq, ",space_cache");
12953cdde224SJeff Mahoney 	else if (btrfs_test_opt(info, FREE_SPACE_TREE))
129670f6d82eSOmar Sandoval 		seq_puts(seq, ",space_cache=v2");
129773bc1876SJosef Bacik 	else
12988965593eSDavid Sterba 		seq_puts(seq, ",nospace_cache");
12993cdde224SJeff Mahoney 	if (btrfs_test_opt(info, RESCAN_UUID_TREE))
1300f420ee1eSStefan Behrens 		seq_puts(seq, ",rescan_uuid_tree");
13013cdde224SJeff Mahoney 	if (btrfs_test_opt(info, CLEAR_CACHE))
1302200da64eSTsutomu Itoh 		seq_puts(seq, ",clear_cache");
13033cdde224SJeff Mahoney 	if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED))
1304200da64eSTsutomu Itoh 		seq_puts(seq, ",user_subvol_rm_allowed");
13053cdde224SJeff Mahoney 	if (btrfs_test_opt(info, ENOSPC_DEBUG))
13060942caa3SDavid Sterba 		seq_puts(seq, ",enospc_debug");
13073cdde224SJeff Mahoney 	if (btrfs_test_opt(info, AUTO_DEFRAG))
13080942caa3SDavid Sterba 		seq_puts(seq, ",autodefrag");
13093cdde224SJeff Mahoney 	if (btrfs_test_opt(info, INODE_MAP_CACHE))
13100942caa3SDavid Sterba 		seq_puts(seq, ",inode_cache");
13113cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SKIP_BALANCE))
13129555c6c1SIlya Dryomov 		seq_puts(seq, ",skip_balance");
13138507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
13143cdde224SJeff Mahoney 	if (btrfs_test_opt(info, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA))
13158507d216SWang Shilong 		seq_puts(seq, ",check_int_data");
13163cdde224SJeff Mahoney 	else if (btrfs_test_opt(info, CHECK_INTEGRITY))
13178507d216SWang Shilong 		seq_puts(seq, ",check_int");
13188507d216SWang Shilong 	if (info->check_integrity_print_mask)
13198507d216SWang Shilong 		seq_printf(seq, ",check_int_print_mask=%d",
13208507d216SWang Shilong 				info->check_integrity_print_mask);
13218507d216SWang Shilong #endif
13228507d216SWang Shilong 	if (info->metadata_ratio)
13238507d216SWang Shilong 		seq_printf(seq, ",metadata_ratio=%d",
13248507d216SWang Shilong 				info->metadata_ratio);
13253cdde224SJeff Mahoney 	if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR))
13268c342930SJeff Mahoney 		seq_puts(seq, ",fatal_errors=panic");
13278b87dc17SDavid Sterba 	if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
13288b87dc17SDavid Sterba 		seq_printf(seq, ",commit=%d", info->commit_interval);
1329d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
13303cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FRAGMENT_DATA))
1331d0bd4560SJosef Bacik 		seq_puts(seq, ",fragment=data");
13323cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FRAGMENT_METADATA))
1333d0bd4560SJosef Bacik 		seq_puts(seq, ",fragment=metadata");
1334d0bd4560SJosef Bacik #endif
1335fb592373SJosef Bacik 	if (btrfs_test_opt(info, REF_VERIFY))
1336fb592373SJosef Bacik 		seq_puts(seq, ",ref_verify");
1337c8d3fe02SOmar Sandoval 	seq_printf(seq, ",subvolid=%llu",
1338c8d3fe02SOmar Sandoval 		  BTRFS_I(d_inode(dentry))->root->root_key.objectid);
1339c8d3fe02SOmar Sandoval 	seq_puts(seq, ",subvol=");
1340c8d3fe02SOmar Sandoval 	seq_dentry(seq, dentry, " \t\n\\");
1341a9572a15SEric Paris 	return 0;
1342a9572a15SEric Paris }
1343a9572a15SEric Paris 
1344a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data)
13452e635a27SChris Mason {
1346815745cfSAl Viro 	struct btrfs_fs_info *p = data;
1347815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(s);
13484b82d6e4SYan 
1349815745cfSAl Viro 	return fs_info->fs_devices == p->fs_devices;
13504b82d6e4SYan }
13514b82d6e4SYan 
1352450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data)
1353450ba0eaSJosef Bacik {
13546de1d09dSAl Viro 	int err = set_anon_super(s, data);
13556de1d09dSAl Viro 	if (!err)
1356450ba0eaSJosef Bacik 		s->s_fs_info = data;
13576de1d09dSAl Viro 	return err;
1358450ba0eaSJosef Bacik }
1359450ba0eaSJosef Bacik 
1360830c4adbSJosef Bacik /*
1361f9d9ef62SDavid Sterba  * subvolumes are identified by ino 256
1362f9d9ef62SDavid Sterba  */
1363f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode)
1364f9d9ef62SDavid Sterba {
1365f9d9ef62SDavid Sterba 	if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
1366f9d9ef62SDavid Sterba 		return 1;
1367f9d9ef62SDavid Sterba 	return 0;
1368f9d9ef62SDavid Sterba }
1369f9d9ef62SDavid Sterba 
1370f9d9ef62SDavid Sterba /*
1371e6e4dbe8SOmar Sandoval  * This will add subvolid=0 to the argument string while removing any subvol=
1372e6e4dbe8SOmar Sandoval  * and subvolid= arguments to make sure we get the top-level root for path
1373e6e4dbe8SOmar Sandoval  * walking to the subvol we want.
1374830c4adbSJosef Bacik  */
1375830c4adbSJosef Bacik static char *setup_root_args(char *args)
1376830c4adbSJosef Bacik {
1377e6e4dbe8SOmar Sandoval 	char *buf, *dst, *sep;
1378830c4adbSJosef Bacik 
1379e6e4dbe8SOmar Sandoval 	if (!args)
13803ec83621SDavid Sterba 		return kstrdup("subvolid=0", GFP_KERNEL);
1381830c4adbSJosef Bacik 
1382e6e4dbe8SOmar Sandoval 	/* The worst case is that we add ",subvolid=0" to the end. */
13833ec83621SDavid Sterba 	buf = dst = kmalloc(strlen(args) + strlen(",subvolid=0") + 1,
13843ec83621SDavid Sterba 			GFP_KERNEL);
1385f60d16a8SJim Meyering 	if (!buf)
1386f60d16a8SJim Meyering 		return NULL;
1387830c4adbSJosef Bacik 
1388e6e4dbe8SOmar Sandoval 	while (1) {
1389e6e4dbe8SOmar Sandoval 		sep = strchrnul(args, ',');
1390e6e4dbe8SOmar Sandoval 		if (!strstarts(args, "subvol=") &&
1391e6e4dbe8SOmar Sandoval 		    !strstarts(args, "subvolid=")) {
1392e6e4dbe8SOmar Sandoval 			memcpy(dst, args, sep - args);
1393e6e4dbe8SOmar Sandoval 			dst += sep - args;
1394e6e4dbe8SOmar Sandoval 			*dst++ = ',';
1395830c4adbSJosef Bacik 		}
1396e6e4dbe8SOmar Sandoval 		if (*sep)
1397e6e4dbe8SOmar Sandoval 			args = sep + 1;
1398e6e4dbe8SOmar Sandoval 		else
1399e6e4dbe8SOmar Sandoval 			break;
1400e6e4dbe8SOmar Sandoval 	}
1401f60d16a8SJim Meyering 	strcpy(dst, "subvolid=0");
1402830c4adbSJosef Bacik 
1403f60d16a8SJim Meyering 	return buf;
1404830c4adbSJosef Bacik }
1405830c4adbSJosef Bacik 
1406bb289b7bSOmar Sandoval static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
1407bb289b7bSOmar Sandoval 				   int flags, const char *device_name,
1408bb289b7bSOmar Sandoval 				   char *data)
1409830c4adbSJosef Bacik {
1410830c4adbSJosef Bacik 	struct dentry *root;
1411fa330659SOmar Sandoval 	struct vfsmount *mnt = NULL;
1412830c4adbSJosef Bacik 	char *newargs;
1413fa330659SOmar Sandoval 	int ret;
1414830c4adbSJosef Bacik 
1415830c4adbSJosef Bacik 	newargs = setup_root_args(data);
1416fa330659SOmar Sandoval 	if (!newargs) {
1417fa330659SOmar Sandoval 		root = ERR_PTR(-ENOMEM);
1418fa330659SOmar Sandoval 		goto out;
1419fa330659SOmar Sandoval 	}
14200723a047SHarald Hoyer 
1421fa330659SOmar Sandoval 	mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name, newargs);
1422fa330659SOmar Sandoval 	if (PTR_ERR_OR_ZERO(mnt) == -EBUSY) {
14231751e8a6SLinus Torvalds 		if (flags & SB_RDONLY) {
14241751e8a6SLinus Torvalds 			mnt = vfs_kern_mount(&btrfs_fs_type, flags & ~SB_RDONLY,
1425fa330659SOmar Sandoval 					     device_name, newargs);
14260723a047SHarald Hoyer 		} else {
14271751e8a6SLinus Torvalds 			mnt = vfs_kern_mount(&btrfs_fs_type, flags | SB_RDONLY,
1428fa330659SOmar Sandoval 					     device_name, newargs);
14290040e606SChristoph Jaeger 			if (IS_ERR(mnt)) {
1430fa330659SOmar Sandoval 				root = ERR_CAST(mnt);
1431fa330659SOmar Sandoval 				mnt = NULL;
1432fa330659SOmar Sandoval 				goto out;
14330040e606SChristoph Jaeger 			}
14340723a047SHarald Hoyer 
1435773cd04eSOmar Sandoval 			down_write(&mnt->mnt_sb->s_umount);
1436fa330659SOmar Sandoval 			ret = btrfs_remount(mnt->mnt_sb, &flags, NULL);
1437773cd04eSOmar Sandoval 			up_write(&mnt->mnt_sb->s_umount);
1438fa330659SOmar Sandoval 			if (ret < 0) {
1439fa330659SOmar Sandoval 				root = ERR_PTR(ret);
1440fa330659SOmar Sandoval 				goto out;
14410723a047SHarald Hoyer 			}
14420723a047SHarald Hoyer 		}
14430723a047SHarald Hoyer 	}
1444fa330659SOmar Sandoval 	if (IS_ERR(mnt)) {
1445fa330659SOmar Sandoval 		root = ERR_CAST(mnt);
1446fa330659SOmar Sandoval 		mnt = NULL;
1447fa330659SOmar Sandoval 		goto out;
1448fa330659SOmar Sandoval 	}
1449830c4adbSJosef Bacik 
145005dbe683SOmar Sandoval 	if (!subvol_name) {
145105dbe683SOmar Sandoval 		if (!subvol_objectid) {
145205dbe683SOmar Sandoval 			ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
145305dbe683SOmar Sandoval 							  &subvol_objectid);
145405dbe683SOmar Sandoval 			if (ret) {
145505dbe683SOmar Sandoval 				root = ERR_PTR(ret);
145605dbe683SOmar Sandoval 				goto out;
145705dbe683SOmar Sandoval 			}
145805dbe683SOmar Sandoval 		}
145905dbe683SOmar Sandoval 		subvol_name = get_subvol_name_from_objectid(btrfs_sb(mnt->mnt_sb),
146005dbe683SOmar Sandoval 							    subvol_objectid);
146105dbe683SOmar Sandoval 		if (IS_ERR(subvol_name)) {
146205dbe683SOmar Sandoval 			root = ERR_CAST(subvol_name);
146305dbe683SOmar Sandoval 			subvol_name = NULL;
146405dbe683SOmar Sandoval 			goto out;
146505dbe683SOmar Sandoval 		}
146605dbe683SOmar Sandoval 
146705dbe683SOmar Sandoval 	}
146805dbe683SOmar Sandoval 
1469ea441d11SAl Viro 	root = mount_subtree(mnt, subvol_name);
1470fa330659SOmar Sandoval 	/* mount_subtree() drops our reference on the vfsmount. */
1471fa330659SOmar Sandoval 	mnt = NULL;
1472830c4adbSJosef Bacik 
1473bb289b7bSOmar Sandoval 	if (!IS_ERR(root)) {
1474ea441d11SAl Viro 		struct super_block *s = root->d_sb;
1475ab8d0fc4SJeff Mahoney 		struct btrfs_fs_info *fs_info = btrfs_sb(s);
1476bb289b7bSOmar Sandoval 		struct inode *root_inode = d_inode(root);
1477bb289b7bSOmar Sandoval 		u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid;
1478bb289b7bSOmar Sandoval 
1479bb289b7bSOmar Sandoval 		ret = 0;
1480bb289b7bSOmar Sandoval 		if (!is_subvolume_inode(root_inode)) {
1481ab8d0fc4SJeff Mahoney 			btrfs_err(fs_info, "'%s' is not a valid subvolume",
1482bb289b7bSOmar Sandoval 			       subvol_name);
1483bb289b7bSOmar Sandoval 			ret = -EINVAL;
1484bb289b7bSOmar Sandoval 		}
1485bb289b7bSOmar Sandoval 		if (subvol_objectid && root_objectid != subvol_objectid) {
148605dbe683SOmar Sandoval 			/*
148705dbe683SOmar Sandoval 			 * This will also catch a race condition where a
148805dbe683SOmar Sandoval 			 * subvolume which was passed by ID is renamed and
148905dbe683SOmar Sandoval 			 * another subvolume is renamed over the old location.
149005dbe683SOmar Sandoval 			 */
1491ab8d0fc4SJeff Mahoney 			btrfs_err(fs_info,
1492ab8d0fc4SJeff Mahoney 				  "subvol '%s' does not match subvolid %llu",
1493bb289b7bSOmar Sandoval 				  subvol_name, subvol_objectid);
1494bb289b7bSOmar Sandoval 			ret = -EINVAL;
1495bb289b7bSOmar Sandoval 		}
1496bb289b7bSOmar Sandoval 		if (ret) {
1497ea441d11SAl Viro 			dput(root);
1498bb289b7bSOmar Sandoval 			root = ERR_PTR(ret);
1499ea441d11SAl Viro 			deactivate_locked_super(s);
1500bb289b7bSOmar Sandoval 		}
1501f9d9ef62SDavid Sterba 	}
1502f9d9ef62SDavid Sterba 
1503fa330659SOmar Sandoval out:
1504fa330659SOmar Sandoval 	mntput(mnt);
1505fa330659SOmar Sandoval 	kfree(newargs);
1506fa330659SOmar Sandoval 	kfree(subvol_name);
1507830c4adbSJosef Bacik 	return root;
1508830c4adbSJosef Bacik }
1509450ba0eaSJosef Bacik 
1510f667aef6SQu Wenruo static int parse_security_options(char *orig_opts,
1511f667aef6SQu Wenruo 				  struct security_mnt_opts *sec_opts)
1512f667aef6SQu Wenruo {
1513f667aef6SQu Wenruo 	char *secdata = NULL;
1514f667aef6SQu Wenruo 	int ret = 0;
1515f667aef6SQu Wenruo 
1516f667aef6SQu Wenruo 	secdata = alloc_secdata();
1517f667aef6SQu Wenruo 	if (!secdata)
1518f667aef6SQu Wenruo 		return -ENOMEM;
1519f667aef6SQu Wenruo 	ret = security_sb_copy_data(orig_opts, secdata);
1520f667aef6SQu Wenruo 	if (ret) {
1521f667aef6SQu Wenruo 		free_secdata(secdata);
1522f667aef6SQu Wenruo 		return ret;
1523f667aef6SQu Wenruo 	}
1524f667aef6SQu Wenruo 	ret = security_sb_parse_opts_str(secdata, sec_opts);
1525f667aef6SQu Wenruo 	free_secdata(secdata);
1526f667aef6SQu Wenruo 	return ret;
1527f667aef6SQu Wenruo }
1528f667aef6SQu Wenruo 
1529f667aef6SQu Wenruo static int setup_security_options(struct btrfs_fs_info *fs_info,
1530f667aef6SQu Wenruo 				  struct super_block *sb,
1531f667aef6SQu Wenruo 				  struct security_mnt_opts *sec_opts)
1532f667aef6SQu Wenruo {
1533f667aef6SQu Wenruo 	int ret = 0;
1534f667aef6SQu Wenruo 
1535f667aef6SQu Wenruo 	/*
1536f667aef6SQu Wenruo 	 * Call security_sb_set_mnt_opts() to check whether new sec_opts
1537f667aef6SQu Wenruo 	 * is valid.
1538f667aef6SQu Wenruo 	 */
1539f667aef6SQu Wenruo 	ret = security_sb_set_mnt_opts(sb, sec_opts, 0, NULL);
1540f667aef6SQu Wenruo 	if (ret)
1541f667aef6SQu Wenruo 		return ret;
1542f667aef6SQu Wenruo 
1543a43bb39bSQu Wenruo #ifdef CONFIG_SECURITY
1544f667aef6SQu Wenruo 	if (!fs_info->security_opts.num_mnt_opts) {
1545f667aef6SQu Wenruo 		/* first time security setup, copy sec_opts to fs_info */
1546f667aef6SQu Wenruo 		memcpy(&fs_info->security_opts, sec_opts, sizeof(*sec_opts));
1547f667aef6SQu Wenruo 	} else {
1548f667aef6SQu Wenruo 		/*
1549180e4d47SLuis de Bethencourt 		 * Since SELinux (the only one supporting security_mnt_opts)
1550180e4d47SLuis de Bethencourt 		 * does NOT support changing context during remount/mount of
1551180e4d47SLuis de Bethencourt 		 * the same sb, this must be the same or part of the same
1552180e4d47SLuis de Bethencourt 		 * security options, just free it.
1553f667aef6SQu Wenruo 		 */
1554f667aef6SQu Wenruo 		security_free_mnt_opts(sec_opts);
1555f667aef6SQu Wenruo 	}
1556a43bb39bSQu Wenruo #endif
1557f667aef6SQu Wenruo 	return ret;
1558f667aef6SQu Wenruo }
1559f667aef6SQu Wenruo 
1560*72fa39f5SMisono, Tomohiro static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,
1561*72fa39f5SMisono, Tomohiro 		int flags, const char *device_name, void *data)
1562*72fa39f5SMisono, Tomohiro {
1563*72fa39f5SMisono, Tomohiro 	struct block_device *bdev = NULL;
1564*72fa39f5SMisono, Tomohiro 	struct super_block *s;
1565*72fa39f5SMisono, Tomohiro 	struct btrfs_fs_devices *fs_devices = NULL;
1566*72fa39f5SMisono, Tomohiro 	struct btrfs_fs_info *fs_info = NULL;
1567*72fa39f5SMisono, Tomohiro 	struct security_mnt_opts new_sec_opts;
1568*72fa39f5SMisono, Tomohiro 	fmode_t mode = FMODE_READ;
1569*72fa39f5SMisono, Tomohiro 	char *subvol_name = NULL;
1570*72fa39f5SMisono, Tomohiro 	u64 subvol_objectid = 0;
1571*72fa39f5SMisono, Tomohiro 	int error = 0;
1572*72fa39f5SMisono, Tomohiro 
1573*72fa39f5SMisono, Tomohiro 	if (!(flags & SB_RDONLY))
1574*72fa39f5SMisono, Tomohiro 		mode |= FMODE_WRITE;
1575*72fa39f5SMisono, Tomohiro 
1576*72fa39f5SMisono, Tomohiro 	error = btrfs_parse_early_options(data, mode, fs_type,
1577*72fa39f5SMisono, Tomohiro 					  &subvol_name, &subvol_objectid,
1578*72fa39f5SMisono, Tomohiro 					  &fs_devices);
1579*72fa39f5SMisono, Tomohiro 	if (error) {
1580*72fa39f5SMisono, Tomohiro 		kfree(subvol_name);
1581*72fa39f5SMisono, Tomohiro 		return ERR_PTR(error);
1582*72fa39f5SMisono, Tomohiro 	}
1583*72fa39f5SMisono, Tomohiro 
1584*72fa39f5SMisono, Tomohiro 	security_init_mnt_opts(&new_sec_opts);
1585*72fa39f5SMisono, Tomohiro 	if (data) {
1586*72fa39f5SMisono, Tomohiro 		error = parse_security_options(data, &new_sec_opts);
1587*72fa39f5SMisono, Tomohiro 		if (error)
1588*72fa39f5SMisono, Tomohiro 			return ERR_PTR(error);
1589*72fa39f5SMisono, Tomohiro 	}
1590*72fa39f5SMisono, Tomohiro 
1591*72fa39f5SMisono, Tomohiro 	error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
1592*72fa39f5SMisono, Tomohiro 	if (error)
1593*72fa39f5SMisono, Tomohiro 		goto error_sec_opts;
1594*72fa39f5SMisono, Tomohiro 
1595*72fa39f5SMisono, Tomohiro 	/*
1596*72fa39f5SMisono, Tomohiro 	 * Setup a dummy root and fs_info for test/set super.  This is because
1597*72fa39f5SMisono, Tomohiro 	 * we don't actually fill this stuff out until open_ctree, but we need
1598*72fa39f5SMisono, Tomohiro 	 * it for searching for existing supers, so this lets us do that and
1599*72fa39f5SMisono, Tomohiro 	 * then open_ctree will properly initialize everything later.
1600*72fa39f5SMisono, Tomohiro 	 */
1601*72fa39f5SMisono, Tomohiro 	fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL);
1602*72fa39f5SMisono, Tomohiro 	if (!fs_info) {
1603*72fa39f5SMisono, Tomohiro 		error = -ENOMEM;
1604*72fa39f5SMisono, Tomohiro 		goto error_sec_opts;
1605*72fa39f5SMisono, Tomohiro 	}
1606*72fa39f5SMisono, Tomohiro 
1607*72fa39f5SMisono, Tomohiro 	fs_info->fs_devices = fs_devices;
1608*72fa39f5SMisono, Tomohiro 
1609*72fa39f5SMisono, Tomohiro 	fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
1610*72fa39f5SMisono, Tomohiro 	fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
1611*72fa39f5SMisono, Tomohiro 	security_init_mnt_opts(&fs_info->security_opts);
1612*72fa39f5SMisono, Tomohiro 	if (!fs_info->super_copy || !fs_info->super_for_commit) {
1613*72fa39f5SMisono, Tomohiro 		error = -ENOMEM;
1614*72fa39f5SMisono, Tomohiro 		goto error_fs_info;
1615*72fa39f5SMisono, Tomohiro 	}
1616*72fa39f5SMisono, Tomohiro 
1617*72fa39f5SMisono, Tomohiro 	error = btrfs_open_devices(fs_devices, mode, fs_type);
1618*72fa39f5SMisono, Tomohiro 	if (error)
1619*72fa39f5SMisono, Tomohiro 		goto error_fs_info;
1620*72fa39f5SMisono, Tomohiro 
1621*72fa39f5SMisono, Tomohiro 	if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) {
1622*72fa39f5SMisono, Tomohiro 		error = -EACCES;
1623*72fa39f5SMisono, Tomohiro 		goto error_close_devices;
1624*72fa39f5SMisono, Tomohiro 	}
1625*72fa39f5SMisono, Tomohiro 
1626*72fa39f5SMisono, Tomohiro 	bdev = fs_devices->latest_bdev;
1627*72fa39f5SMisono, Tomohiro 	s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC,
1628*72fa39f5SMisono, Tomohiro 		 fs_info);
1629*72fa39f5SMisono, Tomohiro 	if (IS_ERR(s)) {
1630*72fa39f5SMisono, Tomohiro 		error = PTR_ERR(s);
1631*72fa39f5SMisono, Tomohiro 		goto error_close_devices;
1632*72fa39f5SMisono, Tomohiro 	}
1633*72fa39f5SMisono, Tomohiro 
1634*72fa39f5SMisono, Tomohiro 	if (s->s_root) {
1635*72fa39f5SMisono, Tomohiro 		btrfs_close_devices(fs_devices);
1636*72fa39f5SMisono, Tomohiro 		free_fs_info(fs_info);
1637*72fa39f5SMisono, Tomohiro 		if ((flags ^ s->s_flags) & SB_RDONLY)
1638*72fa39f5SMisono, Tomohiro 			error = -EBUSY;
1639*72fa39f5SMisono, Tomohiro 	} else {
1640*72fa39f5SMisono, Tomohiro 		snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
1641*72fa39f5SMisono, Tomohiro 		btrfs_sb(s)->bdev_holder = fs_type;
1642*72fa39f5SMisono, Tomohiro 		error = btrfs_fill_super(s, fs_devices, data);
1643*72fa39f5SMisono, Tomohiro 	}
1644*72fa39f5SMisono, Tomohiro 	if (error) {
1645*72fa39f5SMisono, Tomohiro 		deactivate_locked_super(s);
1646*72fa39f5SMisono, Tomohiro 		goto error_sec_opts;
1647*72fa39f5SMisono, Tomohiro 	}
1648*72fa39f5SMisono, Tomohiro 
1649*72fa39f5SMisono, Tomohiro 	fs_info = btrfs_sb(s);
1650*72fa39f5SMisono, Tomohiro 	error = setup_security_options(fs_info, s, &new_sec_opts);
1651*72fa39f5SMisono, Tomohiro 	if (error) {
1652*72fa39f5SMisono, Tomohiro 		deactivate_locked_super(s);
1653*72fa39f5SMisono, Tomohiro 		goto error_sec_opts;
1654*72fa39f5SMisono, Tomohiro 	}
1655*72fa39f5SMisono, Tomohiro 
1656*72fa39f5SMisono, Tomohiro 	return dget(s->s_root);
1657*72fa39f5SMisono, Tomohiro 
1658*72fa39f5SMisono, Tomohiro error_close_devices:
1659*72fa39f5SMisono, Tomohiro 	btrfs_close_devices(fs_devices);
1660*72fa39f5SMisono, Tomohiro error_fs_info:
1661*72fa39f5SMisono, Tomohiro 	free_fs_info(fs_info);
1662*72fa39f5SMisono, Tomohiro error_sec_opts:
1663*72fa39f5SMisono, Tomohiro 	security_free_mnt_opts(&new_sec_opts);
1664*72fa39f5SMisono, Tomohiro 	return ERR_PTR(error);
1665*72fa39f5SMisono, Tomohiro }
1666edf24abeSChristoph Hellwig /*
1667edf24abeSChristoph Hellwig  * Find a superblock for the given device / mount point.
1668edf24abeSChristoph Hellwig  *
1669edf24abeSChristoph Hellwig  * Note:  This is based on get_sb_bdev from fs/super.c with a few additions
1670edf24abeSChristoph Hellwig  *	  for multiple device setup.  Make sure to keep it in sync.
1671edf24abeSChristoph Hellwig  */
1672061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
1673306e16ceSDavid Sterba 		const char *device_name, void *data)
16744b82d6e4SYan {
16754b82d6e4SYan 	struct block_device *bdev = NULL;
16764b82d6e4SYan 	struct super_block *s;
16778a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices = NULL;
1678450ba0eaSJosef Bacik 	struct btrfs_fs_info *fs_info = NULL;
1679f667aef6SQu Wenruo 	struct security_mnt_opts new_sec_opts;
168097288f2cSChristoph Hellwig 	fmode_t mode = FMODE_READ;
168173f73415SJosef Bacik 	char *subvol_name = NULL;
168273f73415SJosef Bacik 	u64 subvol_objectid = 0;
16834b82d6e4SYan 	int error = 0;
16844b82d6e4SYan 
16851751e8a6SLinus Torvalds 	if (!(flags & SB_RDONLY))
168697288f2cSChristoph Hellwig 		mode |= FMODE_WRITE;
168797288f2cSChristoph Hellwig 
168897288f2cSChristoph Hellwig 	error = btrfs_parse_early_options(data, mode, fs_type,
168973f73415SJosef Bacik 					  &subvol_name, &subvol_objectid,
16905e2a4b25SDavid Sterba 					  &fs_devices);
1691f23c8af8SIlya Dryomov 	if (error) {
1692f23c8af8SIlya Dryomov 		kfree(subvol_name);
1693061dbc6bSAl Viro 		return ERR_PTR(error);
1694f23c8af8SIlya Dryomov 	}
1695edf24abeSChristoph Hellwig 
169605dbe683SOmar Sandoval 	if (subvol_name || subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
1697fa330659SOmar Sandoval 		/* mount_subvol() will free subvol_name. */
1698bb289b7bSOmar Sandoval 		return mount_subvol(subvol_name, subvol_objectid, flags,
1699bb289b7bSOmar Sandoval 				    device_name, data);
1700830c4adbSJosef Bacik 	}
1701830c4adbSJosef Bacik 
1702f667aef6SQu Wenruo 	security_init_mnt_opts(&new_sec_opts);
1703f667aef6SQu Wenruo 	if (data) {
1704f667aef6SQu Wenruo 		error = parse_security_options(data, &new_sec_opts);
17058a4b83ccSChris Mason 		if (error)
1706830c4adbSJosef Bacik 			return ERR_PTR(error);
1707f667aef6SQu Wenruo 	}
1708f667aef6SQu Wenruo 
1709f667aef6SQu Wenruo 	error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
1710f667aef6SQu Wenruo 	if (error)
1711f667aef6SQu Wenruo 		goto error_sec_opts;
17124b82d6e4SYan 
1713450ba0eaSJosef Bacik 	/*
1714450ba0eaSJosef Bacik 	 * Setup a dummy root and fs_info for test/set super.  This is because
1715450ba0eaSJosef Bacik 	 * we don't actually fill this stuff out until open_ctree, but we need
1716450ba0eaSJosef Bacik 	 * it for searching for existing supers, so this lets us do that and
1717450ba0eaSJosef Bacik 	 * then open_ctree will properly initialize everything later.
1718450ba0eaSJosef Bacik 	 */
17193ec83621SDavid Sterba 	fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL);
1720f667aef6SQu Wenruo 	if (!fs_info) {
1721f667aef6SQu Wenruo 		error = -ENOMEM;
1722f667aef6SQu Wenruo 		goto error_sec_opts;
1723f667aef6SQu Wenruo 	}
172404d21a24SIlya Dryomov 
1725450ba0eaSJosef Bacik 	fs_info->fs_devices = fs_devices;
1726450ba0eaSJosef Bacik 
17273ec83621SDavid Sterba 	fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
17283ec83621SDavid Sterba 	fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
1729f667aef6SQu Wenruo 	security_init_mnt_opts(&fs_info->security_opts);
17306c41761fSDavid Sterba 	if (!fs_info->super_copy || !fs_info->super_for_commit) {
17316c41761fSDavid Sterba 		error = -ENOMEM;
173204d21a24SIlya Dryomov 		goto error_fs_info;
173304d21a24SIlya Dryomov 	}
173404d21a24SIlya Dryomov 
173504d21a24SIlya Dryomov 	error = btrfs_open_devices(fs_devices, mode, fs_type);
173604d21a24SIlya Dryomov 	if (error)
173704d21a24SIlya Dryomov 		goto error_fs_info;
173804d21a24SIlya Dryomov 
17391751e8a6SLinus Torvalds 	if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) {
174004d21a24SIlya Dryomov 		error = -EACCES;
17416c41761fSDavid Sterba 		goto error_close_devices;
17426c41761fSDavid Sterba 	}
17436c41761fSDavid Sterba 
1744dfe25020SChris Mason 	bdev = fs_devices->latest_bdev;
17451751e8a6SLinus Torvalds 	s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC,
17469249e17fSDavid Howells 		 fs_info);
1747830c4adbSJosef Bacik 	if (IS_ERR(s)) {
1748830c4adbSJosef Bacik 		error = PTR_ERR(s);
1749830c4adbSJosef Bacik 		goto error_close_devices;
1750830c4adbSJosef Bacik 	}
17514b82d6e4SYan 
17524b82d6e4SYan 	if (s->s_root) {
17532b82032cSYan Zheng 		btrfs_close_devices(fs_devices);
17546c41761fSDavid Sterba 		free_fs_info(fs_info);
17551751e8a6SLinus Torvalds 		if ((flags ^ s->s_flags) & SB_RDONLY)
175659553edfSAl Viro 			error = -EBUSY;
17574b82d6e4SYan 	} else {
1758a1c6f057SDmitry Monakhov 		snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
1759815745cfSAl Viro 		btrfs_sb(s)->bdev_holder = fs_type;
176056e033a7SDavid Sterba 		error = btrfs_fill_super(s, fs_devices, data);
17614b82d6e4SYan 	}
176205dbe683SOmar Sandoval 	if (error) {
1763e15d0542SXin Zhong 		deactivate_locked_super(s);
1764f667aef6SQu Wenruo 		goto error_sec_opts;
1765f667aef6SQu Wenruo 	}
1766f667aef6SQu Wenruo 
1767f667aef6SQu Wenruo 	fs_info = btrfs_sb(s);
1768f667aef6SQu Wenruo 	error = setup_security_options(fs_info, s, &new_sec_opts);
1769f667aef6SQu Wenruo 	if (error) {
1770f667aef6SQu Wenruo 		deactivate_locked_super(s);
1771f667aef6SQu Wenruo 		goto error_sec_opts;
1772f667aef6SQu Wenruo 	}
17734b82d6e4SYan 
177405dbe683SOmar Sandoval 	return dget(s->s_root);
17754b82d6e4SYan 
1776c146afadSYan Zheng error_close_devices:
17778a4b83ccSChris Mason 	btrfs_close_devices(fs_devices);
177804d21a24SIlya Dryomov error_fs_info:
17796c41761fSDavid Sterba 	free_fs_info(fs_info);
1780f667aef6SQu Wenruo error_sec_opts:
1781f667aef6SQu Wenruo 	security_free_mnt_opts(&new_sec_opts);
1782061dbc6bSAl Viro 	return ERR_PTR(error);
17834b82d6e4SYan }
17842e635a27SChris Mason 
17850d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
17860d2450abSSergei Trofimovich 				     int new_pool_size, int old_pool_size)
17870d2450abSSergei Trofimovich {
17880d2450abSSergei Trofimovich 	if (new_pool_size == old_pool_size)
17890d2450abSSergei Trofimovich 		return;
17900d2450abSSergei Trofimovich 
17910d2450abSSergei Trofimovich 	fs_info->thread_pool_size = new_pool_size;
17920d2450abSSergei Trofimovich 
1793efe120a0SFrank Holton 	btrfs_info(fs_info, "resize thread pool %d -> %d",
17940d2450abSSergei Trofimovich 	       old_pool_size, new_pool_size);
17950d2450abSSergei Trofimovich 
17965cdc7ad3SQu Wenruo 	btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
1797afe3d242SQu Wenruo 	btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
1798a8c93d4eSQu Wenruo 	btrfs_workqueue_set_max(fs_info->submit_workers, new_pool_size);
1799e66f0bb1SQu Wenruo 	btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
1800fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size);
1801fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size);
1802fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_meta_write_workers,
1803fccb5d86SQu Wenruo 				new_pool_size);
1804fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
1805fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
18065b3bc44eSQu Wenruo 	btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
1807736cfa15SQu Wenruo 	btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size);
18080339ef2fSQu Wenruo 	btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers,
1809ff023aacSStefan Behrens 				new_pool_size);
18100d2450abSSergei Trofimovich }
18110d2450abSSergei Trofimovich 
1812f42a34b2SMiao Xie static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info)
1813dc81cdc5SMiao Xie {
1814dc81cdc5SMiao Xie 	set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1815f42a34b2SMiao Xie }
1816dc81cdc5SMiao Xie 
1817f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1818f42a34b2SMiao Xie 				       unsigned long old_opts, int flags)
1819f42a34b2SMiao Xie {
1820dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1821dc81cdc5SMiao Xie 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
18221751e8a6SLinus Torvalds 	     (flags & SB_RDONLY))) {
1823dc81cdc5SMiao Xie 		/* wait for any defraggers to finish */
1824dc81cdc5SMiao Xie 		wait_event(fs_info->transaction_wait,
1825dc81cdc5SMiao Xie 			   (atomic_read(&fs_info->defrag_running) == 0));
18261751e8a6SLinus Torvalds 		if (flags & SB_RDONLY)
1827dc81cdc5SMiao Xie 			sync_filesystem(fs_info->sb);
1828dc81cdc5SMiao Xie 	}
1829dc81cdc5SMiao Xie }
1830dc81cdc5SMiao Xie 
1831dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1832dc81cdc5SMiao Xie 					 unsigned long old_opts)
1833dc81cdc5SMiao Xie {
1834dc81cdc5SMiao Xie 	/*
1835180e4d47SLuis de Bethencourt 	 * We need to cleanup all defragable inodes if the autodefragment is
1836180e4d47SLuis de Bethencourt 	 * close or the filesystem is read only.
1837dc81cdc5SMiao Xie 	 */
1838dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1839bc98a42cSDavid Howells 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) {
1840dc81cdc5SMiao Xie 		btrfs_cleanup_defrag_inodes(fs_info);
1841dc81cdc5SMiao Xie 	}
1842dc81cdc5SMiao Xie 
1843dc81cdc5SMiao Xie 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1844dc81cdc5SMiao Xie }
1845dc81cdc5SMiao Xie 
1846c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1847c146afadSYan Zheng {
1848815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1849815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
185049b25e05SJeff Mahoney 	unsigned old_flags = sb->s_flags;
185149b25e05SJeff Mahoney 	unsigned long old_opts = fs_info->mount_opt;
185249b25e05SJeff Mahoney 	unsigned long old_compress_type = fs_info->compress_type;
185349b25e05SJeff Mahoney 	u64 old_max_inline = fs_info->max_inline;
185449b25e05SJeff Mahoney 	int old_thread_pool_size = fs_info->thread_pool_size;
185549b25e05SJeff Mahoney 	unsigned int old_metadata_ratio = fs_info->metadata_ratio;
1856c146afadSYan Zheng 	int ret;
1857c146afadSYan Zheng 
185802b9984dSTheodore Ts'o 	sync_filesystem(sb);
1859f42a34b2SMiao Xie 	btrfs_remount_prepare(fs_info);
1860dc81cdc5SMiao Xie 
1861f667aef6SQu Wenruo 	if (data) {
1862f667aef6SQu Wenruo 		struct security_mnt_opts new_sec_opts;
1863f667aef6SQu Wenruo 
1864f667aef6SQu Wenruo 		security_init_mnt_opts(&new_sec_opts);
1865f667aef6SQu Wenruo 		ret = parse_security_options(data, &new_sec_opts);
1866f667aef6SQu Wenruo 		if (ret)
1867f667aef6SQu Wenruo 			goto restore;
1868f667aef6SQu Wenruo 		ret = setup_security_options(fs_info, sb,
1869f667aef6SQu Wenruo 					     &new_sec_opts);
1870f667aef6SQu Wenruo 		if (ret) {
1871f667aef6SQu Wenruo 			security_free_mnt_opts(&new_sec_opts);
1872f667aef6SQu Wenruo 			goto restore;
1873f667aef6SQu Wenruo 		}
1874f667aef6SQu Wenruo 	}
1875f667aef6SQu Wenruo 
18762ff7e61eSJeff Mahoney 	ret = btrfs_parse_options(fs_info, data, *flags);
187749b25e05SJeff Mahoney 	if (ret) {
187849b25e05SJeff Mahoney 		ret = -EINVAL;
187949b25e05SJeff Mahoney 		goto restore;
188049b25e05SJeff Mahoney 	}
1881b288052eSChris Mason 
1882f42a34b2SMiao Xie 	btrfs_remount_begin(fs_info, old_opts, *flags);
18830d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
18840d2450abSSergei Trofimovich 		fs_info->thread_pool_size, old_thread_pool_size);
18850d2450abSSergei Trofimovich 
18861751e8a6SLinus Torvalds 	if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
1887dc81cdc5SMiao Xie 		goto out;
1888c146afadSYan Zheng 
18891751e8a6SLinus Torvalds 	if (*flags & SB_RDONLY) {
18908dabb742SStefan Behrens 		/*
18918dabb742SStefan Behrens 		 * this also happens on 'umount -rf' or on shutdown, when
18928dabb742SStefan Behrens 		 * the filesystem is busy.
18938dabb742SStefan Behrens 		 */
189421c7e756SMiao Xie 		cancel_work_sync(&fs_info->async_reclaim_work);
1895361c093dSStefan Behrens 
1896361c093dSStefan Behrens 		/* wait for the uuid_scan task to finish */
1897361c093dSStefan Behrens 		down(&fs_info->uuid_tree_rescan_sem);
1898361c093dSStefan Behrens 		/* avoid complains from lockdep et al. */
1899361c093dSStefan Behrens 		up(&fs_info->uuid_tree_rescan_sem);
1900361c093dSStefan Behrens 
19011751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
1902c146afadSYan Zheng 
1903e44163e1SJeff Mahoney 		/*
19041751e8a6SLinus Torvalds 		 * Setting SB_RDONLY will put the cleaner thread to
1905e44163e1SJeff Mahoney 		 * sleep at the next loop if it's already active.
1906e44163e1SJeff Mahoney 		 * If it's already asleep, we'll leave unused block
1907e44163e1SJeff Mahoney 		 * groups on disk until we're mounted read-write again
1908e44163e1SJeff Mahoney 		 * unless we clean them up here.
1909e44163e1SJeff Mahoney 		 */
1910e44163e1SJeff Mahoney 		btrfs_delete_unused_bgs(fs_info);
1911e44163e1SJeff Mahoney 
19128dabb742SStefan Behrens 		btrfs_dev_replace_suspend_for_unmount(fs_info);
19138dabb742SStefan Behrens 		btrfs_scrub_cancel(fs_info);
1914061594efSMiao Xie 		btrfs_pause_balance(fs_info);
19158dabb742SStefan Behrens 
19166bccf3abSJeff Mahoney 		ret = btrfs_commit_super(fs_info);
191749b25e05SJeff Mahoney 		if (ret)
191849b25e05SJeff Mahoney 			goto restore;
1919c146afadSYan Zheng 	} else {
19200b246afaSJeff Mahoney 		if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
19216ef3de9cSDavid Sterba 			btrfs_err(fs_info,
1922efe120a0SFrank Holton 				"Remounting read-write after error is not allowed");
19236ef3de9cSDavid Sterba 			ret = -EINVAL;
19246ef3de9cSDavid Sterba 			goto restore;
19256ef3de9cSDavid Sterba 		}
19268a3db184SSergei Trofimovich 		if (fs_info->fs_devices->rw_devices == 0) {
192749b25e05SJeff Mahoney 			ret = -EACCES;
192849b25e05SJeff Mahoney 			goto restore;
19298a3db184SSergei Trofimovich 		}
19302b82032cSYan Zheng 
1931b382cfe8SQu Wenruo 		if (!btrfs_check_rw_degradable(fs_info)) {
1932efe120a0SFrank Holton 			btrfs_warn(fs_info,
1933efe120a0SFrank Holton 				"too many missing devices, writeable remount is not allowed");
1934292fd7fcSStefan Behrens 			ret = -EACCES;
1935292fd7fcSStefan Behrens 			goto restore;
1936292fd7fcSStefan Behrens 		}
1937292fd7fcSStefan Behrens 
19388a3db184SSergei Trofimovich 		if (btrfs_super_log_root(fs_info->super_copy) != 0) {
193949b25e05SJeff Mahoney 			ret = -EINVAL;
194049b25e05SJeff Mahoney 			goto restore;
19418a3db184SSergei Trofimovich 		}
1942c146afadSYan Zheng 
1943815745cfSAl Viro 		ret = btrfs_cleanup_fs_roots(fs_info);
194449b25e05SJeff Mahoney 		if (ret)
194549b25e05SJeff Mahoney 			goto restore;
1946c146afadSYan Zheng 
1947d68fc57bSYan, Zheng 		/* recover relocation */
19485f316481SWang Shilong 		mutex_lock(&fs_info->cleaner_mutex);
1949d68fc57bSYan, Zheng 		ret = btrfs_recover_relocation(root);
19505f316481SWang Shilong 		mutex_unlock(&fs_info->cleaner_mutex);
195149b25e05SJeff Mahoney 		if (ret)
195249b25e05SJeff Mahoney 			goto restore;
1953c146afadSYan Zheng 
19542b6ba629SIlya Dryomov 		ret = btrfs_resume_balance_async(fs_info);
19552b6ba629SIlya Dryomov 		if (ret)
19562b6ba629SIlya Dryomov 			goto restore;
19572b6ba629SIlya Dryomov 
19588dabb742SStefan Behrens 		ret = btrfs_resume_dev_replace_async(fs_info);
19598dabb742SStefan Behrens 		if (ret) {
1960efe120a0SFrank Holton 			btrfs_warn(fs_info, "failed to resume dev_replace");
19618dabb742SStefan Behrens 			goto restore;
19628dabb742SStefan Behrens 		}
196394aebfb2SJosef Bacik 
19646c6b5a39SAleksa Sarai 		btrfs_qgroup_rescan_resume(fs_info);
19656c6b5a39SAleksa Sarai 
196694aebfb2SJosef Bacik 		if (!fs_info->uuid_root) {
1967efe120a0SFrank Holton 			btrfs_info(fs_info, "creating UUID tree");
196894aebfb2SJosef Bacik 			ret = btrfs_create_uuid_tree(fs_info);
196994aebfb2SJosef Bacik 			if (ret) {
19705d163e0eSJeff Mahoney 				btrfs_warn(fs_info,
19715d163e0eSJeff Mahoney 					   "failed to create the UUID tree %d",
19725d163e0eSJeff Mahoney 					   ret);
197394aebfb2SJosef Bacik 				goto restore;
197494aebfb2SJosef Bacik 			}
197594aebfb2SJosef Bacik 		}
19761751e8a6SLinus Torvalds 		sb->s_flags &= ~SB_RDONLY;
197790c711abSZygo Blaxell 
1978afcdd129SJosef Bacik 		set_bit(BTRFS_FS_OPEN, &fs_info->flags);
1979c146afadSYan Zheng 	}
1980dc81cdc5SMiao Xie out:
19812c6a92b0SJustin Maggard 	wake_up_process(fs_info->transaction_kthread);
1982dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
1983c146afadSYan Zheng 	return 0;
198449b25e05SJeff Mahoney 
198549b25e05SJeff Mahoney restore:
19861751e8a6SLinus Torvalds 	/* We've hit an error - don't reset SB_RDONLY */
1987bc98a42cSDavid Howells 	if (sb_rdonly(sb))
19881751e8a6SLinus Torvalds 		old_flags |= SB_RDONLY;
198949b25e05SJeff Mahoney 	sb->s_flags = old_flags;
199049b25e05SJeff Mahoney 	fs_info->mount_opt = old_opts;
199149b25e05SJeff Mahoney 	fs_info->compress_type = old_compress_type;
199249b25e05SJeff Mahoney 	fs_info->max_inline = old_max_inline;
19930d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
19940d2450abSSergei Trofimovich 		old_thread_pool_size, fs_info->thread_pool_size);
199549b25e05SJeff Mahoney 	fs_info->metadata_ratio = old_metadata_ratio;
1996dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
199749b25e05SJeff Mahoney 	return ret;
1998c146afadSYan Zheng }
1999c146afadSYan Zheng 
2000bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */
2001bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1,
2002bcd53741SArne Jansen 				       const void *dev_info2)
2003bcd53741SArne Jansen {
2004bcd53741SArne Jansen 	if (((struct btrfs_device_info *)dev_info1)->max_avail >
2005bcd53741SArne Jansen 	    ((struct btrfs_device_info *)dev_info2)->max_avail)
2006bcd53741SArne Jansen 		return -1;
2007bcd53741SArne Jansen 	else if (((struct btrfs_device_info *)dev_info1)->max_avail <
2008bcd53741SArne Jansen 		 ((struct btrfs_device_info *)dev_info2)->max_avail)
2009bcd53741SArne Jansen 		return 1;
2010bcd53741SArne Jansen 	else
2011bcd53741SArne Jansen 	return 0;
2012bcd53741SArne Jansen }
2013bcd53741SArne Jansen 
2014bcd53741SArne Jansen /*
2015bcd53741SArne Jansen  * sort the devices by max_avail, in which max free extent size of each device
2016bcd53741SArne Jansen  * is stored.(Descending Sort)
2017bcd53741SArne Jansen  */
2018bcd53741SArne Jansen static inline void btrfs_descending_sort_devices(
2019bcd53741SArne Jansen 					struct btrfs_device_info *devices,
2020bcd53741SArne Jansen 					size_t nr_devices)
2021bcd53741SArne Jansen {
2022bcd53741SArne Jansen 	sort(devices, nr_devices, sizeof(struct btrfs_device_info),
2023bcd53741SArne Jansen 	     btrfs_cmp_device_free_bytes, NULL);
2024bcd53741SArne Jansen }
2025bcd53741SArne Jansen 
20266d07bcecSMiao Xie /*
20276d07bcecSMiao Xie  * The helper to calc the free space on the devices that can be used to store
20286d07bcecSMiao Xie  * file data.
20296d07bcecSMiao Xie  */
20306bccf3abSJeff Mahoney static int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
20316bccf3abSJeff Mahoney 				       u64 *free_bytes)
20326d07bcecSMiao Xie {
20336d07bcecSMiao Xie 	struct btrfs_device_info *devices_info;
20346d07bcecSMiao Xie 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
20356d07bcecSMiao Xie 	struct btrfs_device *device;
20366d07bcecSMiao Xie 	u64 skip_space;
20376d07bcecSMiao Xie 	u64 type;
20386d07bcecSMiao Xie 	u64 avail_space;
20396d07bcecSMiao Xie 	u64 min_stripe_size;
204039fb26c3SMiao Xie 	int min_stripes = 1, num_stripes = 1;
20416d07bcecSMiao Xie 	int i = 0, nr_devices;
20426d07bcecSMiao Xie 
20437e33fd99SJosef Bacik 	/*
204401327610SNicholas D Steeves 	 * We aren't under the device list lock, so this is racy-ish, but good
20457e33fd99SJosef Bacik 	 * enough for our purposes.
20467e33fd99SJosef Bacik 	 */
2047b772a86eSLi Zefan 	nr_devices = fs_info->fs_devices->open_devices;
20487e33fd99SJosef Bacik 	if (!nr_devices) {
20497e33fd99SJosef Bacik 		smp_mb();
20507e33fd99SJosef Bacik 		nr_devices = fs_info->fs_devices->open_devices;
20517e33fd99SJosef Bacik 		ASSERT(nr_devices);
20527e33fd99SJosef Bacik 		if (!nr_devices) {
20537e33fd99SJosef Bacik 			*free_bytes = 0;
20547e33fd99SJosef Bacik 			return 0;
20557e33fd99SJosef Bacik 		}
20567e33fd99SJosef Bacik 	}
20576d07bcecSMiao Xie 
2058d9b0d9baSDulshani Gunawardhana 	devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
20596a44517dSDavid Sterba 			       GFP_KERNEL);
20606d07bcecSMiao Xie 	if (!devices_info)
20616d07bcecSMiao Xie 		return -ENOMEM;
20626d07bcecSMiao Xie 
206301327610SNicholas D Steeves 	/* calc min stripe number for data space allocation */
20641b86826dSJeff Mahoney 	type = btrfs_data_alloc_profile(fs_info);
206539fb26c3SMiao Xie 	if (type & BTRFS_BLOCK_GROUP_RAID0) {
20666d07bcecSMiao Xie 		min_stripes = 2;
206739fb26c3SMiao Xie 		num_stripes = nr_devices;
206839fb26c3SMiao Xie 	} else if (type & BTRFS_BLOCK_GROUP_RAID1) {
20696d07bcecSMiao Xie 		min_stripes = 2;
207039fb26c3SMiao Xie 		num_stripes = 2;
207139fb26c3SMiao Xie 	} else if (type & BTRFS_BLOCK_GROUP_RAID10) {
20726d07bcecSMiao Xie 		min_stripes = 4;
207339fb26c3SMiao Xie 		num_stripes = 4;
207439fb26c3SMiao Xie 	}
20756d07bcecSMiao Xie 
20766d07bcecSMiao Xie 	if (type & BTRFS_BLOCK_GROUP_DUP)
20776d07bcecSMiao Xie 		min_stripe_size = 2 * BTRFS_STRIPE_LEN;
20786d07bcecSMiao Xie 	else
20796d07bcecSMiao Xie 		min_stripe_size = BTRFS_STRIPE_LEN;
20806d07bcecSMiao Xie 
20817e33fd99SJosef Bacik 	rcu_read_lock();
20827e33fd99SJosef Bacik 	list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
2083e12c9621SAnand Jain 		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2084e12c9621SAnand Jain 						&device->dev_state) ||
2085401e29c1SAnand Jain 		    !device->bdev ||
2086401e29c1SAnand Jain 		    test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
20876d07bcecSMiao Xie 			continue;
20886d07bcecSMiao Xie 
20897e33fd99SJosef Bacik 		if (i >= nr_devices)
20907e33fd99SJosef Bacik 			break;
20917e33fd99SJosef Bacik 
20926d07bcecSMiao Xie 		avail_space = device->total_bytes - device->bytes_used;
20936d07bcecSMiao Xie 
20946d07bcecSMiao Xie 		/* align with stripe_len */
2095f8c269d7SDavid Sterba 		avail_space = div_u64(avail_space, BTRFS_STRIPE_LEN);
20966d07bcecSMiao Xie 		avail_space *= BTRFS_STRIPE_LEN;
20976d07bcecSMiao Xie 
20986d07bcecSMiao Xie 		/*
209901327610SNicholas D Steeves 		 * In order to avoid overwriting the superblock on the drive,
21006d07bcecSMiao Xie 		 * btrfs starts at an offset of at least 1MB when doing chunk
21016d07bcecSMiao Xie 		 * allocation.
21026d07bcecSMiao Xie 		 */
2103ee22184bSByongho Lee 		skip_space = SZ_1M;
21046d07bcecSMiao Xie 
21056d07bcecSMiao Xie 		/*
21066d07bcecSMiao Xie 		 * we can use the free space in [0, skip_space - 1], subtract
21076d07bcecSMiao Xie 		 * it from the total.
21086d07bcecSMiao Xie 		 */
21096d07bcecSMiao Xie 		if (avail_space && avail_space >= skip_space)
21106d07bcecSMiao Xie 			avail_space -= skip_space;
21116d07bcecSMiao Xie 		else
21126d07bcecSMiao Xie 			avail_space = 0;
21136d07bcecSMiao Xie 
21146d07bcecSMiao Xie 		if (avail_space < min_stripe_size)
21156d07bcecSMiao Xie 			continue;
21166d07bcecSMiao Xie 
21176d07bcecSMiao Xie 		devices_info[i].dev = device;
21186d07bcecSMiao Xie 		devices_info[i].max_avail = avail_space;
21196d07bcecSMiao Xie 
21206d07bcecSMiao Xie 		i++;
21216d07bcecSMiao Xie 	}
21227e33fd99SJosef Bacik 	rcu_read_unlock();
21236d07bcecSMiao Xie 
21246d07bcecSMiao Xie 	nr_devices = i;
21256d07bcecSMiao Xie 
21266d07bcecSMiao Xie 	btrfs_descending_sort_devices(devices_info, nr_devices);
21276d07bcecSMiao Xie 
21286d07bcecSMiao Xie 	i = nr_devices - 1;
21296d07bcecSMiao Xie 	avail_space = 0;
21306d07bcecSMiao Xie 	while (nr_devices >= min_stripes) {
213139fb26c3SMiao Xie 		if (num_stripes > nr_devices)
213239fb26c3SMiao Xie 			num_stripes = nr_devices;
213339fb26c3SMiao Xie 
21346d07bcecSMiao Xie 		if (devices_info[i].max_avail >= min_stripe_size) {
21356d07bcecSMiao Xie 			int j;
21366d07bcecSMiao Xie 			u64 alloc_size;
21376d07bcecSMiao Xie 
213839fb26c3SMiao Xie 			avail_space += devices_info[i].max_avail * num_stripes;
21396d07bcecSMiao Xie 			alloc_size = devices_info[i].max_avail;
214039fb26c3SMiao Xie 			for (j = i + 1 - num_stripes; j <= i; j++)
21416d07bcecSMiao Xie 				devices_info[j].max_avail -= alloc_size;
21426d07bcecSMiao Xie 		}
21436d07bcecSMiao Xie 		i--;
21446d07bcecSMiao Xie 		nr_devices--;
21456d07bcecSMiao Xie 	}
21466d07bcecSMiao Xie 
21476d07bcecSMiao Xie 	kfree(devices_info);
21486d07bcecSMiao Xie 	*free_bytes = avail_space;
21496d07bcecSMiao Xie 	return 0;
21506d07bcecSMiao Xie }
21516d07bcecSMiao Xie 
2152ba7b6e62SDavid Sterba /*
2153ba7b6e62SDavid Sterba  * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
2154ba7b6e62SDavid Sterba  *
2155ba7b6e62SDavid Sterba  * If there's a redundant raid level at DATA block groups, use the respective
2156ba7b6e62SDavid Sterba  * multiplier to scale the sizes.
2157ba7b6e62SDavid Sterba  *
2158ba7b6e62SDavid Sterba  * Unused device space usage is based on simulating the chunk allocator
21590d0c71b3SDavid Sterba  * algorithm that respects the device sizes and order of allocations.  This is
21600d0c71b3SDavid Sterba  * a close approximation of the actual use but there are other factors that may
21610d0c71b3SDavid Sterba  * change the result (like a new metadata chunk).
2162ba7b6e62SDavid Sterba  *
2163ca8a51b3SDavid Sterba  * If metadata is exhausted, f_bavail will be 0.
2164ba7b6e62SDavid Sterba  */
21658fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
21668fd17795SChris Mason {
2167815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
2168815745cfSAl Viro 	struct btrfs_super_block *disk_super = fs_info->super_copy;
2169815745cfSAl Viro 	struct list_head *head = &fs_info->space_info;
2170bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
2171bd4d1088SJosef Bacik 	u64 total_used = 0;
21726d07bcecSMiao Xie 	u64 total_free_data = 0;
2173ca8a51b3SDavid Sterba 	u64 total_free_meta = 0;
2174db94535dSChris Mason 	int bits = dentry->d_sb->s_blocksize_bits;
2175815745cfSAl Viro 	__be32 *fsid = (__be32 *)fs_info->fsid;
2176ba7b6e62SDavid Sterba 	unsigned factor = 1;
2177ba7b6e62SDavid Sterba 	struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
21786d07bcecSMiao Xie 	int ret;
2179ca8a51b3SDavid Sterba 	u64 thresh = 0;
2180ae02d1bdSLuis de Bethencourt 	int mixed = 0;
21818fd17795SChris Mason 
2182bd4d1088SJosef Bacik 	rcu_read_lock();
218389a55897SJosef Bacik 	list_for_each_entry_rcu(found, head, list) {
21846d07bcecSMiao Xie 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
2185ba7b6e62SDavid Sterba 			int i;
2186ba7b6e62SDavid Sterba 
21876d07bcecSMiao Xie 			total_free_data += found->disk_total - found->disk_used;
21886d07bcecSMiao Xie 			total_free_data -=
21896d07bcecSMiao Xie 				btrfs_account_ro_block_groups_free_space(found);
2190ba7b6e62SDavid Sterba 
2191ba7b6e62SDavid Sterba 			for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
2192ba7b6e62SDavid Sterba 				if (!list_empty(&found->block_groups[i])) {
2193ba7b6e62SDavid Sterba 					switch (i) {
2194ba7b6e62SDavid Sterba 					case BTRFS_RAID_DUP:
2195ba7b6e62SDavid Sterba 					case BTRFS_RAID_RAID1:
2196ba7b6e62SDavid Sterba 					case BTRFS_RAID_RAID10:
2197ba7b6e62SDavid Sterba 						factor = 2;
2198ba7b6e62SDavid Sterba 					}
2199ba7b6e62SDavid Sterba 				}
2200ba7b6e62SDavid Sterba 			}
22016d07bcecSMiao Xie 		}
2202ae02d1bdSLuis de Bethencourt 
2203ae02d1bdSLuis de Bethencourt 		/*
2204ae02d1bdSLuis de Bethencourt 		 * Metadata in mixed block goup profiles are accounted in data
2205ae02d1bdSLuis de Bethencourt 		 */
2206ae02d1bdSLuis de Bethencourt 		if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) {
2207ae02d1bdSLuis de Bethencourt 			if (found->flags & BTRFS_BLOCK_GROUP_DATA)
2208ae02d1bdSLuis de Bethencourt 				mixed = 1;
2209ae02d1bdSLuis de Bethencourt 			else
2210ae02d1bdSLuis de Bethencourt 				total_free_meta += found->disk_total -
2211ae02d1bdSLuis de Bethencourt 					found->disk_used;
2212ae02d1bdSLuis de Bethencourt 		}
22136d07bcecSMiao Xie 
2214b742bb82SYan, Zheng 		total_used += found->disk_used;
221589a55897SJosef Bacik 	}
2216ba7b6e62SDavid Sterba 
2217bd4d1088SJosef Bacik 	rcu_read_unlock();
2218bd4d1088SJosef Bacik 
2219ba7b6e62SDavid Sterba 	buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
2220ba7b6e62SDavid Sterba 	buf->f_blocks >>= bits;
2221ba7b6e62SDavid Sterba 	buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
2222ba7b6e62SDavid Sterba 
2223ba7b6e62SDavid Sterba 	/* Account global block reserve as used, it's in logical size already */
2224ba7b6e62SDavid Sterba 	spin_lock(&block_rsv->lock);
222541b34accSLuis de Bethencourt 	/* Mixed block groups accounting is not byte-accurate, avoid overflow */
222641b34accSLuis de Bethencourt 	if (buf->f_bfree >= block_rsv->size >> bits)
2227ba7b6e62SDavid Sterba 		buf->f_bfree -= block_rsv->size >> bits;
222841b34accSLuis de Bethencourt 	else
222941b34accSLuis de Bethencourt 		buf->f_bfree = 0;
2230ba7b6e62SDavid Sterba 	spin_unlock(&block_rsv->lock);
2231ba7b6e62SDavid Sterba 
22320d95c1beSDavid Sterba 	buf->f_bavail = div_u64(total_free_data, factor);
22336bccf3abSJeff Mahoney 	ret = btrfs_calc_avail_data_space(fs_info, &total_free_data);
22347e33fd99SJosef Bacik 	if (ret)
22356d07bcecSMiao Xie 		return ret;
2236ba7b6e62SDavid Sterba 	buf->f_bavail += div_u64(total_free_data, factor);
22376d07bcecSMiao Xie 	buf->f_bavail = buf->f_bavail >> bits;
2238d397712bSChris Mason 
2239ca8a51b3SDavid Sterba 	/*
2240ca8a51b3SDavid Sterba 	 * We calculate the remaining metadata space minus global reserve. If
2241ca8a51b3SDavid Sterba 	 * this is (supposedly) smaller than zero, there's no space. But this
2242ca8a51b3SDavid Sterba 	 * does not hold in practice, the exhausted state happens where's still
2243ca8a51b3SDavid Sterba 	 * some positive delta. So we apply some guesswork and compare the
2244ca8a51b3SDavid Sterba 	 * delta to a 4M threshold.  (Practically observed delta was ~2M.)
2245ca8a51b3SDavid Sterba 	 *
2246ca8a51b3SDavid Sterba 	 * We probably cannot calculate the exact threshold value because this
2247ca8a51b3SDavid Sterba 	 * depends on the internal reservations requested by various
2248ca8a51b3SDavid Sterba 	 * operations, so some operations that consume a few metadata will
2249ca8a51b3SDavid Sterba 	 * succeed even if the Avail is zero. But this is better than the other
2250ca8a51b3SDavid Sterba 	 * way around.
2251ca8a51b3SDavid Sterba 	 */
2252d4417e22SNikolay Borisov 	thresh = SZ_4M;
2253ca8a51b3SDavid Sterba 
2254ae02d1bdSLuis de Bethencourt 	if (!mixed && total_free_meta - thresh < block_rsv->size)
2255ca8a51b3SDavid Sterba 		buf->f_bavail = 0;
2256ca8a51b3SDavid Sterba 
2257ba7b6e62SDavid Sterba 	buf->f_type = BTRFS_SUPER_MAGIC;
2258ba7b6e62SDavid Sterba 	buf->f_bsize = dentry->d_sb->s_blocksize;
2259ba7b6e62SDavid Sterba 	buf->f_namelen = BTRFS_NAME_LEN;
2260ba7b6e62SDavid Sterba 
22619d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
22629d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
22639d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
22649d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
22659d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
226632d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
22672b0143b5SDavid Howells 	buf->f_fsid.val[0] ^= BTRFS_I(d_inode(dentry))->root->objectid >> 32;
22682b0143b5SDavid Howells 	buf->f_fsid.val[1] ^= BTRFS_I(d_inode(dentry))->root->objectid;
226932d48fa1SDavid Woodhouse 
22708fd17795SChris Mason 	return 0;
22718fd17795SChris Mason }
2272b5133862SChris Mason 
2273aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb)
2274aea52e19SAl Viro {
2275815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2276aea52e19SAl Viro 	kill_anon_super(sb);
2277aea52e19SAl Viro 	free_fs_info(fs_info);
2278aea52e19SAl Viro }
2279aea52e19SAl Viro 
22802e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
22812e635a27SChris Mason 	.owner		= THIS_MODULE,
22822e635a27SChris Mason 	.name		= "btrfs",
2283061dbc6bSAl Viro 	.mount		= btrfs_mount,
2284aea52e19SAl Viro 	.kill_sb	= btrfs_kill_super,
2285f667aef6SQu Wenruo 	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
22862e635a27SChris Mason };
2287*72fa39f5SMisono, Tomohiro 
2288*72fa39f5SMisono, Tomohiro static struct file_system_type btrfs_root_fs_type = {
2289*72fa39f5SMisono, Tomohiro 	.owner		= THIS_MODULE,
2290*72fa39f5SMisono, Tomohiro 	.name		= "btrfs",
2291*72fa39f5SMisono, Tomohiro 	.mount		= btrfs_mount_root,
2292*72fa39f5SMisono, Tomohiro 	.kill_sb	= btrfs_kill_super,
2293*72fa39f5SMisono, Tomohiro 	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
2294*72fa39f5SMisono, Tomohiro };
2295*72fa39f5SMisono, Tomohiro 
22967f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs");
2297a9218f6bSChris Mason 
2298d8620958STom Van Braeckel static int btrfs_control_open(struct inode *inode, struct file *file)
2299d8620958STom Van Braeckel {
2300d8620958STom Van Braeckel 	/*
2301d8620958STom Van Braeckel 	 * The control file's private_data is used to hold the
2302d8620958STom Van Braeckel 	 * transaction when it is started and is used to keep
2303d8620958STom Van Braeckel 	 * track of whether a transaction is already in progress.
2304d8620958STom Van Braeckel 	 */
2305d8620958STom Van Braeckel 	file->private_data = NULL;
2306d8620958STom Van Braeckel 	return 0;
2307d8620958STom Van Braeckel }
2308d8620958STom Van Braeckel 
2309d352ac68SChris Mason /*
2310d352ac68SChris Mason  * used by btrfsctl to scan devices when no FS is mounted
2311d352ac68SChris Mason  */
23128a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
23138a4b83ccSChris Mason 				unsigned long arg)
23148a4b83ccSChris Mason {
23158a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
23168a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices;
2317c071fcfdSChris Mason 	int ret = -ENOTTY;
23188a4b83ccSChris Mason 
2319e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2320e441d54dSChris Mason 		return -EPERM;
2321e441d54dSChris Mason 
2322dae7b665SLi Zefan 	vol = memdup_user((void __user *)arg, sizeof(*vol));
2323dae7b665SLi Zefan 	if (IS_ERR(vol))
2324dae7b665SLi Zefan 		return PTR_ERR(vol);
2325c071fcfdSChris Mason 
23268a4b83ccSChris Mason 	switch (cmd) {
23278a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
232897288f2cSChristoph Hellwig 		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
23298a4b83ccSChris Mason 					    &btrfs_fs_type, &fs_devices);
23308a4b83ccSChris Mason 		break;
233102db0844SJosef Bacik 	case BTRFS_IOC_DEVICES_READY:
233202db0844SJosef Bacik 		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
233302db0844SJosef Bacik 					    &btrfs_fs_type, &fs_devices);
233402db0844SJosef Bacik 		if (ret)
233502db0844SJosef Bacik 			break;
233602db0844SJosef Bacik 		ret = !(fs_devices->num_devices == fs_devices->total_devices);
233702db0844SJosef Bacik 		break;
2338c5868f83SDavid Sterba 	case BTRFS_IOC_GET_SUPPORTED_FEATURES:
2339d5131b65SDavid Sterba 		ret = btrfs_ioctl_get_supported_features((void __user*)arg);
2340c5868f83SDavid Sterba 		break;
23418a4b83ccSChris Mason 	}
2342dae7b665SLi Zefan 
23438a4b83ccSChris Mason 	kfree(vol);
2344f819d837SLinda Knippers 	return ret;
23458a4b83ccSChris Mason }
23468a4b83ccSChris Mason 
23470176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
2348ed0dab6bSYan {
2349354aa0fbSMiao Xie 	struct btrfs_trans_handle *trans;
23500b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
23510b246afaSJeff Mahoney 	struct btrfs_root *root = fs_info->tree_root;
2352354aa0fbSMiao Xie 
2353fac03c8dSDavid Sterba 	set_bit(BTRFS_FS_FROZEN, &fs_info->flags);
23549e7cc91aSWang Xiaoguang 	/*
23559e7cc91aSWang Xiaoguang 	 * We don't need a barrier here, we'll wait for any transaction that
23569e7cc91aSWang Xiaoguang 	 * could be in progress on other threads (and do delayed iputs that
23579e7cc91aSWang Xiaoguang 	 * we want to avoid on a frozen filesystem), or do the commit
23589e7cc91aSWang Xiaoguang 	 * ourselves.
23599e7cc91aSWang Xiaoguang 	 */
2360d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
2361354aa0fbSMiao Xie 	if (IS_ERR(trans)) {
2362354aa0fbSMiao Xie 		/* no transaction, don't bother */
2363354aa0fbSMiao Xie 		if (PTR_ERR(trans) == -ENOENT)
23640176260fSLinus Torvalds 			return 0;
2365354aa0fbSMiao Xie 		return PTR_ERR(trans);
2366354aa0fbSMiao Xie 	}
23673a45bb20SJeff Mahoney 	return btrfs_commit_transaction(trans);
2368ed0dab6bSYan }
2369ed0dab6bSYan 
23709e7cc91aSWang Xiaoguang static int btrfs_unfreeze(struct super_block *sb)
23719e7cc91aSWang Xiaoguang {
2372fac03c8dSDavid Sterba 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2373fac03c8dSDavid Sterba 
2374fac03c8dSDavid Sterba 	clear_bit(BTRFS_FS_FROZEN, &fs_info->flags);
23759e7cc91aSWang Xiaoguang 	return 0;
23769e7cc91aSWang Xiaoguang }
23779e7cc91aSWang Xiaoguang 
23789c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
23799c5085c1SJosef Bacik {
23809c5085c1SJosef Bacik 	struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
23819c5085c1SJosef Bacik 	struct btrfs_fs_devices *cur_devices;
23829c5085c1SJosef Bacik 	struct btrfs_device *dev, *first_dev = NULL;
23839c5085c1SJosef Bacik 	struct list_head *head;
23849c5085c1SJosef Bacik 	struct rcu_string *name;
23859c5085c1SJosef Bacik 
23869c5085c1SJosef Bacik 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
23879c5085c1SJosef Bacik 	cur_devices = fs_info->fs_devices;
23889c5085c1SJosef Bacik 	while (cur_devices) {
23899c5085c1SJosef Bacik 		head = &cur_devices->devices;
23909c5085c1SJosef Bacik 		list_for_each_entry(dev, head, dev_list) {
2391e6e674bdSAnand Jain 			if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
2392aa9ddcd4SJosef Bacik 				continue;
23930aeb8a6eSAnand Jain 			if (!dev->name)
23940aeb8a6eSAnand Jain 				continue;
23959c5085c1SJosef Bacik 			if (!first_dev || dev->devid < first_dev->devid)
23969c5085c1SJosef Bacik 				first_dev = dev;
23979c5085c1SJosef Bacik 		}
23989c5085c1SJosef Bacik 		cur_devices = cur_devices->seed;
23999c5085c1SJosef Bacik 	}
24009c5085c1SJosef Bacik 
24019c5085c1SJosef Bacik 	if (first_dev) {
24029c5085c1SJosef Bacik 		rcu_read_lock();
24039c5085c1SJosef Bacik 		name = rcu_dereference(first_dev->name);
24049c5085c1SJosef Bacik 		seq_escape(m, name->str, " \t\n\\");
24059c5085c1SJosef Bacik 		rcu_read_unlock();
24069c5085c1SJosef Bacik 	} else {
24079c5085c1SJosef Bacik 		WARN_ON(1);
24089c5085c1SJosef Bacik 	}
24099c5085c1SJosef Bacik 	mutex_unlock(&fs_info->fs_devices->device_list_mutex);
24109c5085c1SJosef Bacik 	return 0;
24119c5085c1SJosef Bacik }
24129c5085c1SJosef Bacik 
2413b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
241476dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
2415bd555975SAl Viro 	.evict_inode	= btrfs_evict_inode,
2416e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
2417d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
2418a9572a15SEric Paris 	.show_options	= btrfs_show_options,
24199c5085c1SJosef Bacik 	.show_devname	= btrfs_show_devname,
24204730a4bcSChris Mason 	.write_inode	= btrfs_write_inode,
24212c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
24222c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
24238fd17795SChris Mason 	.statfs		= btrfs_statfs,
2424c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
24250176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
24269e7cc91aSWang Xiaoguang 	.unfreeze_fs	= btrfs_unfreeze,
2427e20d96d6SChris Mason };
2428a9218f6bSChris Mason 
2429a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
2430d8620958STom Van Braeckel 	.open = btrfs_control_open,
2431a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
2432a9218f6bSChris Mason 	.compat_ioctl = btrfs_control_ioctl,
2433a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
24346038f373SArnd Bergmann 	.llseek = noop_llseek,
2435a9218f6bSChris Mason };
2436a9218f6bSChris Mason 
2437a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
2438578454ffSKay Sievers 	.minor		= BTRFS_MINOR,
2439a9218f6bSChris Mason 	.name		= "btrfs-control",
2440a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
2441a9218f6bSChris Mason };
2442a9218f6bSChris Mason 
2443578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
2444578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control");
2445578454ffSKay Sievers 
2446f5c29bd9SLiu Bo static int __init btrfs_interface_init(void)
2447a9218f6bSChris Mason {
2448a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
2449a9218f6bSChris Mason }
2450a9218f6bSChris Mason 
2451b2950863SChristoph Hellwig static void btrfs_interface_exit(void)
2452a9218f6bSChris Mason {
2453f368ed60SGreg Kroah-Hartman 	misc_deregister(&btrfs_misc);
2454a9218f6bSChris Mason }
2455a9218f6bSChris Mason 
2456f5c29bd9SLiu Bo static void __init btrfs_print_mod_info(void)
245785965600SDavid Sterba {
245862e85577SJeff Mahoney 	pr_info("Btrfs loaded, crc32c=%s"
245985965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG
246085965600SDavid Sterba 			", debug=on"
246185965600SDavid Sterba #endif
246279556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT
246379556c3dSStefan Behrens 			", assert=on"
246479556c3dSStefan Behrens #endif
246585965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
246685965600SDavid Sterba 			", integrity-checker=on"
246785965600SDavid Sterba #endif
2468fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
2469fb592373SJosef Bacik 			", ref-verify=on"
2470fb592373SJosef Bacik #endif
24715f9e1059SJeff Mahoney 			"\n",
24725f9e1059SJeff Mahoney 			btrfs_crc32c_impl());
247385965600SDavid Sterba }
247485965600SDavid Sterba 
24752e635a27SChris Mason static int __init init_btrfs_fs(void)
24762e635a27SChris Mason {
24772c90e5d6SChris Mason 	int err;
247858176a96SJosef Bacik 
247914a958e6SFilipe David Borba Manana 	err = btrfs_hash_init();
248014a958e6SFilipe David Borba Manana 	if (err)
248114a958e6SFilipe David Borba Manana 		return err;
248214a958e6SFilipe David Borba Manana 
248363541927SFilipe David Borba Manana 	btrfs_props_init();
248463541927SFilipe David Borba Manana 
248558176a96SJosef Bacik 	err = btrfs_init_sysfs();
248658176a96SJosef Bacik 	if (err)
248714a958e6SFilipe David Borba Manana 		goto free_hash;
248858176a96SJosef Bacik 
2489143bede5SJeff Mahoney 	btrfs_init_compress();
2490d1310b2eSChris Mason 
2491261507a0SLi Zefan 	err = btrfs_init_cachep();
2492261507a0SLi Zefan 	if (err)
2493261507a0SLi Zefan 		goto free_compress;
2494261507a0SLi Zefan 
2495d1310b2eSChris Mason 	err = extent_io_init();
24962f4cbe64SWyatt Banks 	if (err)
24972f4cbe64SWyatt Banks 		goto free_cachep;
24982f4cbe64SWyatt Banks 
2499d1310b2eSChris Mason 	err = extent_map_init();
2500d1310b2eSChris Mason 	if (err)
2501d1310b2eSChris Mason 		goto free_extent_io;
2502d1310b2eSChris Mason 
25036352b91dSMiao Xie 	err = ordered_data_init();
25042f4cbe64SWyatt Banks 	if (err)
25052f4cbe64SWyatt Banks 		goto free_extent_map;
2506c8b97818SChris Mason 
25076352b91dSMiao Xie 	err = btrfs_delayed_inode_init();
25086352b91dSMiao Xie 	if (err)
25096352b91dSMiao Xie 		goto free_ordered_data;
25106352b91dSMiao Xie 
25119247f317SMiao Xie 	err = btrfs_auto_defrag_init();
251216cdcec7SMiao Xie 	if (err)
251316cdcec7SMiao Xie 		goto free_delayed_inode;
251416cdcec7SMiao Xie 
251578a6184aSMiao Xie 	err = btrfs_delayed_ref_init();
25169247f317SMiao Xie 	if (err)
25179247f317SMiao Xie 		goto free_auto_defrag;
25189247f317SMiao Xie 
2519b9e9a6cbSWang Shilong 	err = btrfs_prelim_ref_init();
2520b9e9a6cbSWang Shilong 	if (err)
2521af13b492SDavid Sterba 		goto free_delayed_ref;
2522b9e9a6cbSWang Shilong 
252397eb6b69SDavid Sterba 	err = btrfs_end_io_wq_init();
252478a6184aSMiao Xie 	if (err)
2525af13b492SDavid Sterba 		goto free_prelim_ref;
252678a6184aSMiao Xie 
252797eb6b69SDavid Sterba 	err = btrfs_interface_init();
252897eb6b69SDavid Sterba 	if (err)
252997eb6b69SDavid Sterba 		goto free_end_io_wq;
253097eb6b69SDavid Sterba 
2531e565d4b9SJan Schmidt 	btrfs_init_lockdep();
2532e565d4b9SJan Schmidt 
25338ae1af3cSAnand Jain 	btrfs_print_mod_info();
2534dc11dd5dSJosef Bacik 
2535dc11dd5dSJosef Bacik 	err = btrfs_run_sanity_tests();
2536dc11dd5dSJosef Bacik 	if (err)
2537dc11dd5dSJosef Bacik 		goto unregister_ioctl;
2538dc11dd5dSJosef Bacik 
2539dc11dd5dSJosef Bacik 	err = register_filesystem(&btrfs_fs_type);
2540dc11dd5dSJosef Bacik 	if (err)
2541dc11dd5dSJosef Bacik 		goto unregister_ioctl;
254274255aa0SJosef Bacik 
25432f4cbe64SWyatt Banks 	return 0;
25442f4cbe64SWyatt Banks 
2545a9218f6bSChris Mason unregister_ioctl:
2546a9218f6bSChris Mason 	btrfs_interface_exit();
254797eb6b69SDavid Sterba free_end_io_wq:
254897eb6b69SDavid Sterba 	btrfs_end_io_wq_exit();
2549b9e9a6cbSWang Shilong free_prelim_ref:
2550b9e9a6cbSWang Shilong 	btrfs_prelim_ref_exit();
255178a6184aSMiao Xie free_delayed_ref:
255278a6184aSMiao Xie 	btrfs_delayed_ref_exit();
25539247f317SMiao Xie free_auto_defrag:
25549247f317SMiao Xie 	btrfs_auto_defrag_exit();
255516cdcec7SMiao Xie free_delayed_inode:
255616cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
25576352b91dSMiao Xie free_ordered_data:
25586352b91dSMiao Xie 	ordered_data_exit();
25592f4cbe64SWyatt Banks free_extent_map:
25602f4cbe64SWyatt Banks 	extent_map_exit();
2561d1310b2eSChris Mason free_extent_io:
2562d1310b2eSChris Mason 	extent_io_exit();
25632f4cbe64SWyatt Banks free_cachep:
25642f4cbe64SWyatt Banks 	btrfs_destroy_cachep();
2565261507a0SLi Zefan free_compress:
2566261507a0SLi Zefan 	btrfs_exit_compress();
25672f4cbe64SWyatt Banks 	btrfs_exit_sysfs();
256814a958e6SFilipe David Borba Manana free_hash:
256914a958e6SFilipe David Borba Manana 	btrfs_hash_exit();
25702c90e5d6SChris Mason 	return err;
25712e635a27SChris Mason }
25722e635a27SChris Mason 
25732e635a27SChris Mason static void __exit exit_btrfs_fs(void)
25742e635a27SChris Mason {
257539279cc3SChris Mason 	btrfs_destroy_cachep();
257678a6184aSMiao Xie 	btrfs_delayed_ref_exit();
25779247f317SMiao Xie 	btrfs_auto_defrag_exit();
257816cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
2579b9e9a6cbSWang Shilong 	btrfs_prelim_ref_exit();
25806352b91dSMiao Xie 	ordered_data_exit();
2581a52d9a80SChris Mason 	extent_map_exit();
2582d1310b2eSChris Mason 	extent_io_exit();
2583a9218f6bSChris Mason 	btrfs_interface_exit();
25845ed5f588SJosef Bacik 	btrfs_end_io_wq_exit();
25852e635a27SChris Mason 	unregister_filesystem(&btrfs_fs_type);
258658176a96SJosef Bacik 	btrfs_exit_sysfs();
25878a4b83ccSChris Mason 	btrfs_cleanup_fs_uuids();
2588261507a0SLi Zefan 	btrfs_exit_compress();
258914a958e6SFilipe David Borba Manana 	btrfs_hash_exit();
25902e635a27SChris Mason }
25912e635a27SChris Mason 
259260efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs);
25932e635a27SChris Mason module_exit(exit_btrfs_fs)
25942e635a27SChris Mason 
25952e635a27SChris Mason MODULE_LICENSE("GPL");
2596