xref: /linux/fs/btrfs/super.c (revision e33e17ee1098d8d751552ac11c111e1c1a3db014)
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;
68830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type;
69e20d96d6SChris Mason 
700723a047SHarald Hoyer static int btrfs_remount(struct super_block *sb, int *flags, char *data);
710723a047SHarald Hoyer 
72*e33e17eeSJeff Mahoney const char *btrfs_decode_error(int errno)
73acce952bSliubo {
7408748810SDavid Sterba 	char *errstr = "unknown";
75acce952bSliubo 
76acce952bSliubo 	switch (errno) {
77acce952bSliubo 	case -EIO:
78acce952bSliubo 		errstr = "IO failure";
79acce952bSliubo 		break;
80acce952bSliubo 	case -ENOMEM:
81acce952bSliubo 		errstr = "Out of memory";
82acce952bSliubo 		break;
83acce952bSliubo 	case -EROFS:
84acce952bSliubo 		errstr = "Readonly filesystem";
85acce952bSliubo 		break;
868c342930SJeff Mahoney 	case -EEXIST:
878c342930SJeff Mahoney 		errstr = "Object already exists";
888c342930SJeff Mahoney 		break;
8994ef7280SDavid Sterba 	case -ENOSPC:
9094ef7280SDavid Sterba 		errstr = "No space left";
9194ef7280SDavid Sterba 		break;
9294ef7280SDavid Sterba 	case -ENOENT:
9394ef7280SDavid Sterba 		errstr = "No such entry";
9494ef7280SDavid Sterba 		break;
95acce952bSliubo 	}
96acce952bSliubo 
97acce952bSliubo 	return errstr;
98acce952bSliubo }
99acce952bSliubo 
100bbece8a3SDavid Sterba static void save_error_info(struct btrfs_fs_info *fs_info)
101acce952bSliubo {
102acce952bSliubo 	/*
103acce952bSliubo 	 * today we only save the error info into ram.  Long term we'll
104acce952bSliubo 	 * also send it down to the disk
105acce952bSliubo 	 */
10687533c47SMiao Xie 	set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
107acce952bSliubo }
108acce952bSliubo 
109acce952bSliubo /* btrfs handle error by forcing the filesystem readonly */
110acce952bSliubo static void btrfs_handle_error(struct btrfs_fs_info *fs_info)
111acce952bSliubo {
112acce952bSliubo 	struct super_block *sb = fs_info->sb;
113acce952bSliubo 
114acce952bSliubo 	if (sb->s_flags & MS_RDONLY)
115acce952bSliubo 		return;
116acce952bSliubo 
11787533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
118acce952bSliubo 		sb->s_flags |= MS_RDONLY;
119c2cf52ebSSimon Kirby 		btrfs_info(fs_info, "forced readonly");
1201acd6831SStefan Behrens 		/*
1211acd6831SStefan Behrens 		 * Note that a running device replace operation is not
1221acd6831SStefan Behrens 		 * canceled here although there is no way to update
1231acd6831SStefan Behrens 		 * the progress. It would add the risk of a deadlock,
1241acd6831SStefan Behrens 		 * therefore the canceling is ommited. The only penalty
1251acd6831SStefan Behrens 		 * is that some I/O remains active until the procedure
1261acd6831SStefan Behrens 		 * completes. The next time when the filesystem is
1271acd6831SStefan Behrens 		 * mounted writeable again, the device replace
1281acd6831SStefan Behrens 		 * operation continues.
1291acd6831SStefan Behrens 		 */
130acce952bSliubo 	}
131acce952bSliubo }
132acce952bSliubo 
133533574c6SJoe Perches #ifdef CONFIG_PRINTK
134acce952bSliubo /*
135acce952bSliubo  * __btrfs_std_error decodes expected errors from the caller and
136acce952bSliubo  * invokes the approciate error response.
137acce952bSliubo  */
138c0d19e2bSDavid Sterba __cold
139acce952bSliubo void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
1404da35113SJeff Mahoney 		       unsigned int line, int errno, const char *fmt, ...)
141acce952bSliubo {
142acce952bSliubo 	struct super_block *sb = fs_info->sb;
143acce952bSliubo 	const char *errstr;
144acce952bSliubo 
145acce952bSliubo 	/*
146acce952bSliubo 	 * Special case: if the error is EROFS, and we're already
147acce952bSliubo 	 * under MS_RDONLY, then it is safe here.
148acce952bSliubo 	 */
149acce952bSliubo 	if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
150acce952bSliubo   		return;
151acce952bSliubo 
15208748810SDavid Sterba 	errstr = btrfs_decode_error(errno);
1534da35113SJeff Mahoney 	if (fmt) {
15437252a66SEric Sandeen 		struct va_format vaf;
15537252a66SEric Sandeen 		va_list args;
15637252a66SEric Sandeen 
15737252a66SEric Sandeen 		va_start(args, fmt);
15837252a66SEric Sandeen 		vaf.fmt = fmt;
15937252a66SEric Sandeen 		vaf.va = &args;
1604da35113SJeff Mahoney 
161efe120a0SFrank Holton 		printk(KERN_CRIT
162efe120a0SFrank Holton 			"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 {
166efe120a0SFrank Holton 		printk(KERN_CRIT "BTRFS: error (device %s) in %s:%d: errno=%d %s\n",
16708748810SDavid Sterba 			sb->s_id, function, line, errno, errstr);
1684da35113SJeff Mahoney 	}
169acce952bSliubo 
1704da35113SJeff Mahoney 	/* Don't go through full error handling during mount */
1714da35113SJeff Mahoney 	save_error_info(fs_info);
172cf79ffb5SJosef Bacik 	if (sb->s_flags & MS_BORN)
173acce952bSliubo 		btrfs_handle_error(fs_info);
174acce952bSliubo }
1754da35113SJeff Mahoney 
176533574c6SJoe Perches static const char * const logtypes[] = {
1774da35113SJeff Mahoney 	"emergency",
1784da35113SJeff Mahoney 	"alert",
1794da35113SJeff Mahoney 	"critical",
1804da35113SJeff Mahoney 	"error",
1814da35113SJeff Mahoney 	"warning",
1824da35113SJeff Mahoney 	"notice",
1834da35113SJeff Mahoney 	"info",
1844da35113SJeff Mahoney 	"debug",
1854da35113SJeff Mahoney };
1864da35113SJeff Mahoney 
187c2cf52ebSSimon Kirby void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
1884da35113SJeff Mahoney {
1894da35113SJeff Mahoney 	struct super_block *sb = fs_info->sb;
1904da35113SJeff Mahoney 	char lvl[4];
1914da35113SJeff Mahoney 	struct va_format vaf;
1924da35113SJeff Mahoney 	va_list args;
1934da35113SJeff Mahoney 	const char *type = logtypes[4];
194533574c6SJoe Perches 	int kern_level;
1954da35113SJeff Mahoney 
1964da35113SJeff Mahoney 	va_start(args, fmt);
1974da35113SJeff Mahoney 
198533574c6SJoe Perches 	kern_level = printk_get_level(fmt);
199533574c6SJoe Perches 	if (kern_level) {
200533574c6SJoe Perches 		size_t size = printk_skip_level(fmt) - fmt;
201533574c6SJoe Perches 		memcpy(lvl, fmt,  size);
202533574c6SJoe Perches 		lvl[size] = '\0';
203533574c6SJoe Perches 		fmt += size;
204533574c6SJoe Perches 		type = logtypes[kern_level - '0'];
2054da35113SJeff Mahoney 	} else
2064da35113SJeff Mahoney 		*lvl = '\0';
2074da35113SJeff Mahoney 
2084da35113SJeff Mahoney 	vaf.fmt = fmt;
2094da35113SJeff Mahoney 	vaf.va = &args;
210533574c6SJoe Perches 
211c2cf52ebSSimon Kirby 	printk("%sBTRFS %s (device %s): %pV\n", lvl, type, sb->s_id, &vaf);
212533574c6SJoe Perches 
213533574c6SJoe Perches 	va_end(args);
2144da35113SJeff Mahoney }
215acce952bSliubo 
216533574c6SJoe Perches #else
217533574c6SJoe Perches 
218533574c6SJoe Perches void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
219533574c6SJoe Perches 		       unsigned int line, int errno, const char *fmt, ...)
220533574c6SJoe Perches {
221533574c6SJoe Perches 	struct super_block *sb = fs_info->sb;
222533574c6SJoe Perches 
223533574c6SJoe Perches 	/*
224533574c6SJoe Perches 	 * Special case: if the error is EROFS, and we're already
225533574c6SJoe Perches 	 * under MS_RDONLY, then it is safe here.
226533574c6SJoe Perches 	 */
227533574c6SJoe Perches 	if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
228533574c6SJoe Perches 		return;
229533574c6SJoe Perches 
230533574c6SJoe Perches 	/* Don't go through full error handling during mount */
231533574c6SJoe Perches 	if (sb->s_flags & MS_BORN) {
232533574c6SJoe Perches 		save_error_info(fs_info);
233533574c6SJoe Perches 		btrfs_handle_error(fs_info);
234533574c6SJoe Perches 	}
235533574c6SJoe Perches }
236533574c6SJoe Perches #endif
237533574c6SJoe Perches 
2388c342930SJeff Mahoney /*
23949b25e05SJeff Mahoney  * We only mark the transaction aborted and then set the file system read-only.
24049b25e05SJeff Mahoney  * This will prevent new transactions from starting or trying to join this
24149b25e05SJeff Mahoney  * one.
24249b25e05SJeff Mahoney  *
24349b25e05SJeff Mahoney  * This means that error recovery at the call site is limited to freeing
24449b25e05SJeff Mahoney  * any local memory allocations and passing the error code up without
24549b25e05SJeff Mahoney  * further cleanup. The transaction should complete as it normally would
24649b25e05SJeff Mahoney  * in the call path but will return -EIO.
24749b25e05SJeff Mahoney  *
24849b25e05SJeff Mahoney  * We'll complete the cleanup in btrfs_end_transaction and
24949b25e05SJeff Mahoney  * btrfs_commit_transaction.
25049b25e05SJeff Mahoney  */
251c0d19e2bSDavid Sterba __cold
25249b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
25349b25e05SJeff Mahoney 			       struct btrfs_root *root, const char *function,
25449b25e05SJeff Mahoney 			       unsigned int line, int errno)
25549b25e05SJeff Mahoney {
25649b25e05SJeff Mahoney 	trans->aborted = errno;
25749b25e05SJeff Mahoney 	/* Nothing used. The other threads that have joined this
25849b25e05SJeff Mahoney 	 * transaction may be able to continue. */
259c92f6be3SFilipe Manana 	if (!trans->blocks_used && list_empty(&trans->new_bgs)) {
26069ce977aSMiao Xie 		const char *errstr;
26169ce977aSMiao Xie 
26208748810SDavid Sterba 		errstr = btrfs_decode_error(errno);
263c2cf52ebSSimon Kirby 		btrfs_warn(root->fs_info,
264c2cf52ebSSimon Kirby 		           "%s:%d: Aborting unused transaction(%s).",
26569ce977aSMiao Xie 		           function, line, errstr);
26649b25e05SJeff Mahoney 		return;
26749b25e05SJeff Mahoney 	}
2688d25a086SMiao Xie 	ACCESS_ONCE(trans->transaction->aborted) = errno;
269501407aaSJosef Bacik 	/* Wake up anybody who may be waiting on this transaction */
270501407aaSJosef Bacik 	wake_up(&root->fs_info->transaction_wait);
271501407aaSJosef Bacik 	wake_up(&root->fs_info->transaction_blocked_wait);
27249b25e05SJeff Mahoney 	__btrfs_std_error(root->fs_info, function, line, errno, NULL);
27349b25e05SJeff Mahoney }
27449b25e05SJeff Mahoney /*
2758c342930SJeff Mahoney  * __btrfs_panic decodes unexpected, fatal errors from the caller,
2768c342930SJeff Mahoney  * issues an alert, and either panics or BUGs, depending on mount options.
2778c342930SJeff Mahoney  */
278c0d19e2bSDavid Sterba __cold
2798c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
2808c342930SJeff Mahoney 		   unsigned int line, int errno, const char *fmt, ...)
2818c342930SJeff Mahoney {
2828c342930SJeff Mahoney 	char *s_id = "<unknown>";
2838c342930SJeff Mahoney 	const char *errstr;
2848c342930SJeff Mahoney 	struct va_format vaf = { .fmt = fmt };
2858c342930SJeff Mahoney 	va_list args;
2868c342930SJeff Mahoney 
2878c342930SJeff Mahoney 	if (fs_info)
2888c342930SJeff Mahoney 		s_id = fs_info->sb->s_id;
2898c342930SJeff Mahoney 
2908c342930SJeff Mahoney 	va_start(args, fmt);
2918c342930SJeff Mahoney 	vaf.va = &args;
2928c342930SJeff Mahoney 
29308748810SDavid Sterba 	errstr = btrfs_decode_error(errno);
294aa43a17cSEric Sandeen 	if (fs_info && (fs_info->mount_opt & BTRFS_MOUNT_PANIC_ON_FATAL_ERROR))
29508748810SDavid Sterba 		panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
29608748810SDavid Sterba 			s_id, function, line, &vaf, errno, errstr);
2978c342930SJeff Mahoney 
298efe120a0SFrank Holton 	btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)",
299efe120a0SFrank Holton 		   function, line, &vaf, errno, errstr);
3008c342930SJeff Mahoney 	va_end(args);
3018c342930SJeff Mahoney 	/* Caller calls BUG() */
3028c342930SJeff Mahoney }
303e20d96d6SChris Mason 
304e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb)
305e20d96d6SChris Mason {
3063abdbd78SDavid Sterba 	close_ctree(btrfs_sb(sb)->tree_root);
307e20d96d6SChris Mason }
3082e635a27SChris Mason 
30995e05289SChris Mason enum {
31073f73415SJosef Bacik 	Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
311287a0ab9SJosef Bacik 	Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
312287a0ab9SJosef Bacik 	Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
313261507a0SLi Zefan 	Opt_compress_type, Opt_compress_force, Opt_compress_force_type,
314261507a0SLi Zefan 	Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard,
31591435650SChris Mason 	Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed,
3169555c6c1SIlya Dryomov 	Opt_enospc_debug, Opt_subvolrootid, Opt_defrag, Opt_inode_cache,
3179555c6c1SIlya Dryomov 	Opt_no_space_cache, Opt_recovery, Opt_skip_balance,
31821adbd5cSStefan Behrens 	Opt_check_integrity, Opt_check_integrity_including_extent_data,
319f420ee1eSStefan Behrens 	Opt_check_integrity_print_mask, Opt_fatal_errors, Opt_rescan_uuid_tree,
320e07a2adeSQu Wenruo 	Opt_commit_interval, Opt_barrier, Opt_nodefrag, Opt_nodiscard,
321a258af7aSQu Wenruo 	Opt_noenospc_debug, Opt_noflushoncommit, Opt_acl, Opt_datacow,
3223818aea2SQu Wenruo 	Opt_datasum, Opt_treelog, Opt_noinode_cache,
3239555c6c1SIlya Dryomov 	Opt_err,
32495e05289SChris Mason };
32595e05289SChris Mason 
32695e05289SChris Mason static match_table_t tokens = {
327dfe25020SChris Mason 	{Opt_degraded, "degraded"},
32895e05289SChris Mason 	{Opt_subvol, "subvol=%s"},
3291493381fSWang Shilong 	{Opt_subvolid, "subvolid=%s"},
33043e570b0SChristoph Hellwig 	{Opt_device, "device=%s"},
331b6cda9bcSChris Mason 	{Opt_nodatasum, "nodatasum"},
332d399167dSQu Wenruo 	{Opt_datasum, "datasum"},
333be20aa9dSChris Mason 	{Opt_nodatacow, "nodatacow"},
334a258af7aSQu Wenruo 	{Opt_datacow, "datacow"},
33521ad10cfSChris Mason 	{Opt_nobarrier, "nobarrier"},
336842bef58SQu Wenruo 	{Opt_barrier, "barrier"},
3376f568d35SChris Mason 	{Opt_max_inline, "max_inline=%s"},
3388f662a76SChris Mason 	{Opt_alloc_start, "alloc_start=%s"},
3394543df7eSChris Mason 	{Opt_thread_pool, "thread_pool=%d"},
340c8b97818SChris Mason 	{Opt_compress, "compress"},
341261507a0SLi Zefan 	{Opt_compress_type, "compress=%s"},
342a555f810SChris Mason 	{Opt_compress_force, "compress-force"},
343261507a0SLi Zefan 	{Opt_compress_force_type, "compress-force=%s"},
344e18e4809SChris Mason 	{Opt_ssd, "ssd"},
345451d7585SChris Mason 	{Opt_ssd_spread, "ssd_spread"},
3463b30c22fSChris Mason 	{Opt_nossd, "nossd"},
347bd0330adSQu Wenruo 	{Opt_acl, "acl"},
34833268eafSJosef Bacik 	{Opt_noacl, "noacl"},
3493a5e1404SSage Weil 	{Opt_notreelog, "notreelog"},
350a88998f2SQu Wenruo 	{Opt_treelog, "treelog"},
351dccae999SSage Weil 	{Opt_flushoncommit, "flushoncommit"},
3522c9ee856SQu Wenruo 	{Opt_noflushoncommit, "noflushoncommit"},
35397e728d4SJosef Bacik 	{Opt_ratio, "metadata_ratio=%d"},
354e244a0aeSChristoph Hellwig 	{Opt_discard, "discard"},
355e07a2adeSQu Wenruo 	{Opt_nodiscard, "nodiscard"},
3560af3d00bSJosef Bacik 	{Opt_space_cache, "space_cache"},
35788c2ba3bSJosef Bacik 	{Opt_clear_cache, "clear_cache"},
3584260f7c7SSage Weil 	{Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
35991435650SChris Mason 	{Opt_enospc_debug, "enospc_debug"},
36053036293SQu Wenruo 	{Opt_noenospc_debug, "noenospc_debug"},
361e15d0542SXin Zhong 	{Opt_subvolrootid, "subvolrootid=%d"},
3624cb5300bSChris Mason 	{Opt_defrag, "autodefrag"},
363fc0ca9afSQu Wenruo 	{Opt_nodefrag, "noautodefrag"},
3644b9465cbSChris Mason 	{Opt_inode_cache, "inode_cache"},
3653818aea2SQu Wenruo 	{Opt_noinode_cache, "noinode_cache"},
3668965593eSDavid Sterba 	{Opt_no_space_cache, "nospace_cache"},
367af31f5e5SChris Mason 	{Opt_recovery, "recovery"},
3689555c6c1SIlya Dryomov 	{Opt_skip_balance, "skip_balance"},
36921adbd5cSStefan Behrens 	{Opt_check_integrity, "check_int"},
37021adbd5cSStefan Behrens 	{Opt_check_integrity_including_extent_data, "check_int_data"},
37121adbd5cSStefan Behrens 	{Opt_check_integrity_print_mask, "check_int_print_mask=%d"},
372f420ee1eSStefan Behrens 	{Opt_rescan_uuid_tree, "rescan_uuid_tree"},
3738c342930SJeff Mahoney 	{Opt_fatal_errors, "fatal_errors=%s"},
3748b87dc17SDavid Sterba 	{Opt_commit_interval, "commit=%d"},
37533268eafSJosef Bacik 	{Opt_err, NULL},
37695e05289SChris Mason };
37795e05289SChris Mason 
378edf24abeSChristoph Hellwig /*
379edf24abeSChristoph Hellwig  * Regular mount options parser.  Everything that is needed only when
380edf24abeSChristoph Hellwig  * reading in a new superblock is parsed here.
38149b25e05SJeff Mahoney  * XXX JDM: This needs to be cleaned up for remount.
382edf24abeSChristoph Hellwig  */
383edf24abeSChristoph Hellwig int btrfs_parse_options(struct btrfs_root *root, char *options)
38495e05289SChris Mason {
385edf24abeSChristoph Hellwig 	struct btrfs_fs_info *info = root->fs_info;
38695e05289SChris Mason 	substring_t args[MAX_OPT_ARGS];
38773bc1876SJosef Bacik 	char *p, *num, *orig = NULL;
38873bc1876SJosef Bacik 	u64 cache_gen;
3894543df7eSChris Mason 	int intarg;
390a7a3f7caSSage Weil 	int ret = 0;
391261507a0SLi Zefan 	char *compress_type;
392261507a0SLi Zefan 	bool compress_force = false;
393b6cda9bcSChris Mason 
3946c41761fSDavid Sterba 	cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
39573bc1876SJosef Bacik 	if (cache_gen)
39673bc1876SJosef Bacik 		btrfs_set_opt(info->mount_opt, SPACE_CACHE);
39773bc1876SJosef Bacik 
39895e05289SChris Mason 	if (!options)
39973bc1876SJosef Bacik 		goto out;
40095e05289SChris Mason 
401be20aa9dSChris Mason 	/*
402be20aa9dSChris Mason 	 * strsep changes the string, duplicate it because parse_options
403be20aa9dSChris Mason 	 * gets called twice
404be20aa9dSChris Mason 	 */
405be20aa9dSChris Mason 	options = kstrdup(options, GFP_NOFS);
406be20aa9dSChris Mason 	if (!options)
407be20aa9dSChris Mason 		return -ENOMEM;
408be20aa9dSChris Mason 
409da495eccSJosef Bacik 	orig = options;
410be20aa9dSChris Mason 
41195e05289SChris Mason 	while ((p = strsep(&options, ",")) != NULL) {
41295e05289SChris Mason 		int token;
41395e05289SChris Mason 		if (!*p)
41495e05289SChris Mason 			continue;
41595e05289SChris Mason 
41695e05289SChris Mason 		token = match_token(p, tokens, args);
41795e05289SChris Mason 		switch (token) {
418dfe25020SChris Mason 		case Opt_degraded:
419efe120a0SFrank Holton 			btrfs_info(root->fs_info, "allowing degraded mounts");
420dfe25020SChris Mason 			btrfs_set_opt(info->mount_opt, DEGRADED);
421dfe25020SChris Mason 			break;
42295e05289SChris Mason 		case Opt_subvol:
42373f73415SJosef Bacik 		case Opt_subvolid:
424e15d0542SXin Zhong 		case Opt_subvolrootid:
42543e570b0SChristoph Hellwig 		case Opt_device:
426edf24abeSChristoph Hellwig 			/*
42743e570b0SChristoph Hellwig 			 * These are parsed by btrfs_parse_early_options
428edf24abeSChristoph Hellwig 			 * and can be happily ignored here.
429edf24abeSChristoph Hellwig 			 */
43095e05289SChris Mason 			break;
431b6cda9bcSChris Mason 		case Opt_nodatasum:
43207802534SQu Wenruo 			btrfs_set_and_info(root, NODATASUM,
43307802534SQu Wenruo 					   "setting nodatasum");
434be20aa9dSChris Mason 			break;
435d399167dSQu Wenruo 		case Opt_datasum:
43607802534SQu Wenruo 			if (btrfs_test_opt(root, NODATASUM)) {
437d399167dSQu Wenruo 				if (btrfs_test_opt(root, NODATACOW))
438d399167dSQu Wenruo 					btrfs_info(root->fs_info, "setting datasum, datacow enabled");
439d399167dSQu Wenruo 				else
440d399167dSQu Wenruo 					btrfs_info(root->fs_info, "setting datasum");
44107802534SQu Wenruo 			}
442d399167dSQu Wenruo 			btrfs_clear_opt(info->mount_opt, NODATACOW);
443d399167dSQu Wenruo 			btrfs_clear_opt(info->mount_opt, NODATASUM);
444d399167dSQu Wenruo 			break;
445be20aa9dSChris Mason 		case Opt_nodatacow:
44607802534SQu Wenruo 			if (!btrfs_test_opt(root, NODATACOW)) {
447bedb2ccaSAndrei Popa 				if (!btrfs_test_opt(root, COMPRESS) ||
448bedb2ccaSAndrei Popa 				    !btrfs_test_opt(root, FORCE_COMPRESS)) {
449efe120a0SFrank Holton 					btrfs_info(root->fs_info,
450efe120a0SFrank Holton 						   "setting nodatacow, compression disabled");
451bedb2ccaSAndrei Popa 				} else {
452efe120a0SFrank Holton 					btrfs_info(root->fs_info, "setting nodatacow");
453bedb2ccaSAndrei Popa 				}
45407802534SQu Wenruo 			}
455bedb2ccaSAndrei Popa 			btrfs_clear_opt(info->mount_opt, COMPRESS);
456bedb2ccaSAndrei Popa 			btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
457be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATACOW);
458be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
459b6cda9bcSChris Mason 			break;
460a258af7aSQu Wenruo 		case Opt_datacow:
46107802534SQu Wenruo 			btrfs_clear_and_info(root, NODATACOW,
46207802534SQu Wenruo 					     "setting datacow");
463a258af7aSQu Wenruo 			break;
464a555f810SChris Mason 		case Opt_compress_force:
465261507a0SLi Zefan 		case Opt_compress_force_type:
466261507a0SLi Zefan 			compress_force = true;
4671c697d4aSEric Sandeen 			/* Fallthrough */
468261507a0SLi Zefan 		case Opt_compress:
469261507a0SLi Zefan 		case Opt_compress_type:
470261507a0SLi Zefan 			if (token == Opt_compress ||
471261507a0SLi Zefan 			    token == Opt_compress_force ||
472261507a0SLi Zefan 			    strcmp(args[0].from, "zlib") == 0) {
473261507a0SLi Zefan 				compress_type = "zlib";
474261507a0SLi Zefan 				info->compress_type = BTRFS_COMPRESS_ZLIB;
475063849eaSArnd Hannemann 				btrfs_set_opt(info->mount_opt, COMPRESS);
476bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATACOW);
477bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATASUM);
478a6fa6faeSLi Zefan 			} else if (strcmp(args[0].from, "lzo") == 0) {
479a6fa6faeSLi Zefan 				compress_type = "lzo";
480a6fa6faeSLi Zefan 				info->compress_type = BTRFS_COMPRESS_LZO;
481063849eaSArnd Hannemann 				btrfs_set_opt(info->mount_opt, COMPRESS);
482bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATACOW);
483bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATASUM);
4842b0ce2c2SMitch Harder 				btrfs_set_fs_incompat(info, COMPRESS_LZO);
485063849eaSArnd Hannemann 			} else if (strncmp(args[0].from, "no", 2) == 0) {
486063849eaSArnd Hannemann 				compress_type = "no";
487063849eaSArnd Hannemann 				btrfs_clear_opt(info->mount_opt, COMPRESS);
488063849eaSArnd Hannemann 				btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
489063849eaSArnd Hannemann 				compress_force = false;
490261507a0SLi Zefan 			} else {
491261507a0SLi Zefan 				ret = -EINVAL;
492261507a0SLi Zefan 				goto out;
493261507a0SLi Zefan 			}
494261507a0SLi Zefan 
495261507a0SLi Zefan 			if (compress_force) {
49607802534SQu Wenruo 				btrfs_set_and_info(root, FORCE_COMPRESS,
49707802534SQu Wenruo 						   "force %s compression",
498261507a0SLi Zefan 						   compress_type);
499143f3636SDavid Sterba 			} else {
50007802534SQu Wenruo 				if (!btrfs_test_opt(root, COMPRESS))
50107802534SQu Wenruo 					btrfs_info(root->fs_info,
502351fd353SDavid Sterba 						   "btrfs: use %s compression",
503261507a0SLi Zefan 						   compress_type);
5044027e0f4SWang Shilong 				/*
5054027e0f4SWang Shilong 				 * If we remount from compress-force=xxx to
5064027e0f4SWang Shilong 				 * compress=xxx, we need clear FORCE_COMPRESS
5074027e0f4SWang Shilong 				 * flag, otherwise, there is no way for users
5084027e0f4SWang Shilong 				 * to disable forcible compression separately.
5094027e0f4SWang Shilong 				 */
5104027e0f4SWang Shilong 				btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
511a7e252afSMiao Xie 			}
512a555f810SChris Mason 			break;
513e18e4809SChris Mason 		case Opt_ssd:
51407802534SQu Wenruo 			btrfs_set_and_info(root, SSD,
51507802534SQu Wenruo 					   "use ssd allocation scheme");
516e18e4809SChris Mason 			break;
517451d7585SChris Mason 		case Opt_ssd_spread:
51807802534SQu Wenruo 			btrfs_set_and_info(root, SSD_SPREAD,
51907802534SQu Wenruo 					   "use spread ssd allocation scheme");
5202aa06a35SEric Sandeen 			btrfs_set_opt(info->mount_opt, SSD);
521451d7585SChris Mason 			break;
5223b30c22fSChris Mason 		case Opt_nossd:
5232aa06a35SEric Sandeen 			btrfs_set_and_info(root, NOSSD,
52407802534SQu Wenruo 					     "not using ssd allocation scheme");
5253b30c22fSChris Mason 			btrfs_clear_opt(info->mount_opt, SSD);
5263b30c22fSChris Mason 			break;
527842bef58SQu Wenruo 		case Opt_barrier:
52807802534SQu Wenruo 			btrfs_clear_and_info(root, NOBARRIER,
52907802534SQu Wenruo 					     "turning on barriers");
530842bef58SQu Wenruo 			break;
53121ad10cfSChris Mason 		case Opt_nobarrier:
53207802534SQu Wenruo 			btrfs_set_and_info(root, NOBARRIER,
53307802534SQu Wenruo 					   "turning off barriers");
53421ad10cfSChris Mason 			break;
5354543df7eSChris Mason 		case Opt_thread_pool:
5362c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
5372c334e87SWang Shilong 			if (ret) {
5382c334e87SWang Shilong 				goto out;
5392c334e87SWang Shilong 			} else if (intarg > 0) {
5404543df7eSChris Mason 				info->thread_pool_size = intarg;
5412c334e87SWang Shilong 			} else {
5422c334e87SWang Shilong 				ret = -EINVAL;
5432c334e87SWang Shilong 				goto out;
5442c334e87SWang Shilong 			}
5454543df7eSChris Mason 			break;
5466f568d35SChris Mason 		case Opt_max_inline:
547edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
5486f568d35SChris Mason 			if (num) {
54991748467SAkinobu Mita 				info->max_inline = memparse(num, NULL);
5506f568d35SChris Mason 				kfree(num);
5516f568d35SChris Mason 
55215ada040SChris Mason 				if (info->max_inline) {
553feb5f965SMitch Harder 					info->max_inline = min_t(u64,
55415ada040SChris Mason 						info->max_inline,
55515ada040SChris Mason 						root->sectorsize);
55615ada040SChris Mason 				}
557efe120a0SFrank Holton 				btrfs_info(root->fs_info, "max_inline at %llu",
558c1c9ff7cSGeert Uytterhoeven 					info->max_inline);
5592c334e87SWang Shilong 			} else {
5602c334e87SWang Shilong 				ret = -ENOMEM;
5612c334e87SWang Shilong 				goto out;
5626f568d35SChris Mason 			}
5636f568d35SChris Mason 			break;
5648f662a76SChris Mason 		case Opt_alloc_start:
565edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
5668f662a76SChris Mason 			if (num) {
567c018daecSMiao Xie 				mutex_lock(&info->chunk_mutex);
56891748467SAkinobu Mita 				info->alloc_start = memparse(num, NULL);
569c018daecSMiao Xie 				mutex_unlock(&info->chunk_mutex);
5708f662a76SChris Mason 				kfree(num);
571efe120a0SFrank Holton 				btrfs_info(root->fs_info, "allocations start at %llu",
572c1c9ff7cSGeert Uytterhoeven 					info->alloc_start);
5732c334e87SWang Shilong 			} else {
5742c334e87SWang Shilong 				ret = -ENOMEM;
5752c334e87SWang Shilong 				goto out;
5768f662a76SChris Mason 			}
5778f662a76SChris Mason 			break;
578bd0330adSQu Wenruo 		case Opt_acl:
57945ff35d6SGuangliang Zhao #ifdef CONFIG_BTRFS_FS_POSIX_ACL
580bd0330adSQu Wenruo 			root->fs_info->sb->s_flags |= MS_POSIXACL;
581bd0330adSQu Wenruo 			break;
58245ff35d6SGuangliang Zhao #else
58345ff35d6SGuangliang Zhao 			btrfs_err(root->fs_info,
58445ff35d6SGuangliang Zhao 				"support for ACL not compiled in!");
58545ff35d6SGuangliang Zhao 			ret = -EINVAL;
58645ff35d6SGuangliang Zhao 			goto out;
58745ff35d6SGuangliang Zhao #endif
58833268eafSJosef Bacik 		case Opt_noacl:
58933268eafSJosef Bacik 			root->fs_info->sb->s_flags &= ~MS_POSIXACL;
59033268eafSJosef Bacik 			break;
5913a5e1404SSage Weil 		case Opt_notreelog:
59207802534SQu Wenruo 			btrfs_set_and_info(root, NOTREELOG,
59307802534SQu Wenruo 					   "disabling tree log");
5943a5e1404SSage Weil 			break;
595a88998f2SQu Wenruo 		case Opt_treelog:
59607802534SQu Wenruo 			btrfs_clear_and_info(root, NOTREELOG,
59707802534SQu Wenruo 					     "enabling tree log");
598a88998f2SQu Wenruo 			break;
599dccae999SSage Weil 		case Opt_flushoncommit:
60007802534SQu Wenruo 			btrfs_set_and_info(root, FLUSHONCOMMIT,
60107802534SQu Wenruo 					   "turning on flush-on-commit");
602dccae999SSage Weil 			break;
6032c9ee856SQu Wenruo 		case Opt_noflushoncommit:
60407802534SQu Wenruo 			btrfs_clear_and_info(root, FLUSHONCOMMIT,
60507802534SQu Wenruo 					     "turning off flush-on-commit");
6062c9ee856SQu Wenruo 			break;
60797e728d4SJosef Bacik 		case Opt_ratio:
6082c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
6092c334e87SWang Shilong 			if (ret) {
6102c334e87SWang Shilong 				goto out;
6112c334e87SWang Shilong 			} else if (intarg >= 0) {
61297e728d4SJosef Bacik 				info->metadata_ratio = intarg;
613efe120a0SFrank Holton 				btrfs_info(root->fs_info, "metadata ratio %d",
61497e728d4SJosef Bacik 				       info->metadata_ratio);
6152c334e87SWang Shilong 			} else {
6162c334e87SWang Shilong 				ret = -EINVAL;
6172c334e87SWang Shilong 				goto out;
61897e728d4SJosef Bacik 			}
61997e728d4SJosef Bacik 			break;
620e244a0aeSChristoph Hellwig 		case Opt_discard:
62107802534SQu Wenruo 			btrfs_set_and_info(root, DISCARD,
62207802534SQu Wenruo 					   "turning on discard");
623e244a0aeSChristoph Hellwig 			break;
624e07a2adeSQu Wenruo 		case Opt_nodiscard:
62507802534SQu Wenruo 			btrfs_clear_and_info(root, DISCARD,
62607802534SQu Wenruo 					     "turning off discard");
627e07a2adeSQu Wenruo 			break;
6280af3d00bSJosef Bacik 		case Opt_space_cache:
62907802534SQu Wenruo 			btrfs_set_and_info(root, SPACE_CACHE,
63007802534SQu Wenruo 					   "enabling disk space caching");
6310de90876SJosef Bacik 			break;
632f420ee1eSStefan Behrens 		case Opt_rescan_uuid_tree:
633f420ee1eSStefan Behrens 			btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
634f420ee1eSStefan Behrens 			break;
63573bc1876SJosef Bacik 		case Opt_no_space_cache:
63607802534SQu Wenruo 			btrfs_clear_and_info(root, SPACE_CACHE,
63707802534SQu Wenruo 					     "disabling disk space caching");
63873bc1876SJosef Bacik 			break;
6394b9465cbSChris Mason 		case Opt_inode_cache:
6407e1876acSDavid Sterba 			btrfs_set_pending_and_info(info, INODE_MAP_CACHE,
64107802534SQu Wenruo 					   "enabling inode map caching");
6423818aea2SQu Wenruo 			break;
6433818aea2SQu Wenruo 		case Opt_noinode_cache:
6447e1876acSDavid Sterba 			btrfs_clear_pending_and_info(info, INODE_MAP_CACHE,
64507802534SQu Wenruo 					     "disabling inode map caching");
6464b9465cbSChris Mason 			break;
64788c2ba3bSJosef Bacik 		case Opt_clear_cache:
64807802534SQu Wenruo 			btrfs_set_and_info(root, CLEAR_CACHE,
64907802534SQu Wenruo 					   "force clearing of disk cache");
6500af3d00bSJosef Bacik 			break;
6514260f7c7SSage Weil 		case Opt_user_subvol_rm_allowed:
6524260f7c7SSage Weil 			btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
6534260f7c7SSage Weil 			break;
65491435650SChris Mason 		case Opt_enospc_debug:
65591435650SChris Mason 			btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
65691435650SChris Mason 			break;
65753036293SQu Wenruo 		case Opt_noenospc_debug:
65853036293SQu Wenruo 			btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG);
65953036293SQu Wenruo 			break;
6604cb5300bSChris Mason 		case Opt_defrag:
66107802534SQu Wenruo 			btrfs_set_and_info(root, AUTO_DEFRAG,
66207802534SQu Wenruo 					   "enabling auto defrag");
6634cb5300bSChris Mason 			break;
664fc0ca9afSQu Wenruo 		case Opt_nodefrag:
66507802534SQu Wenruo 			btrfs_clear_and_info(root, AUTO_DEFRAG,
66607802534SQu Wenruo 					     "disabling auto defrag");
667fc0ca9afSQu Wenruo 			break;
668af31f5e5SChris Mason 		case Opt_recovery:
669efe120a0SFrank Holton 			btrfs_info(root->fs_info, "enabling auto recovery");
670af31f5e5SChris Mason 			btrfs_set_opt(info->mount_opt, RECOVERY);
671af31f5e5SChris Mason 			break;
6729555c6c1SIlya Dryomov 		case Opt_skip_balance:
6739555c6c1SIlya Dryomov 			btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
6749555c6c1SIlya Dryomov 			break;
67521adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
67621adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
677efe120a0SFrank Holton 			btrfs_info(root->fs_info,
678efe120a0SFrank Holton 				   "enabling check integrity including extent data");
67921adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt,
68021adbd5cSStefan Behrens 				      CHECK_INTEGRITY_INCLUDING_EXTENT_DATA);
68121adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
68221adbd5cSStefan Behrens 			break;
68321adbd5cSStefan Behrens 		case Opt_check_integrity:
684efe120a0SFrank Holton 			btrfs_info(root->fs_info, "enabling check integrity");
68521adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
68621adbd5cSStefan Behrens 			break;
68721adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
6882c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
6892c334e87SWang Shilong 			if (ret) {
6902c334e87SWang Shilong 				goto out;
6912c334e87SWang Shilong 			} else if (intarg >= 0) {
69221adbd5cSStefan Behrens 				info->check_integrity_print_mask = intarg;
693efe120a0SFrank Holton 				btrfs_info(root->fs_info, "check_integrity_print_mask 0x%x",
69421adbd5cSStefan Behrens 				       info->check_integrity_print_mask);
6952c334e87SWang Shilong 			} else {
6962c334e87SWang Shilong 				ret = -EINVAL;
6972c334e87SWang Shilong 				goto out;
69821adbd5cSStefan Behrens 			}
69921adbd5cSStefan Behrens 			break;
70021adbd5cSStefan Behrens #else
70121adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
70221adbd5cSStefan Behrens 		case Opt_check_integrity:
70321adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
704efe120a0SFrank Holton 			btrfs_err(root->fs_info,
705efe120a0SFrank Holton 				"support for check_integrity* not compiled in!");
70621adbd5cSStefan Behrens 			ret = -EINVAL;
70721adbd5cSStefan Behrens 			goto out;
70821adbd5cSStefan Behrens #endif
7098c342930SJeff Mahoney 		case Opt_fatal_errors:
7108c342930SJeff Mahoney 			if (strcmp(args[0].from, "panic") == 0)
7118c342930SJeff Mahoney 				btrfs_set_opt(info->mount_opt,
7128c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
7138c342930SJeff Mahoney 			else if (strcmp(args[0].from, "bug") == 0)
7148c342930SJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
7158c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
7168c342930SJeff Mahoney 			else {
7178c342930SJeff Mahoney 				ret = -EINVAL;
7188c342930SJeff Mahoney 				goto out;
7198c342930SJeff Mahoney 			}
7208c342930SJeff Mahoney 			break;
7218b87dc17SDavid Sterba 		case Opt_commit_interval:
7228b87dc17SDavid Sterba 			intarg = 0;
7238b87dc17SDavid Sterba 			ret = match_int(&args[0], &intarg);
7248b87dc17SDavid Sterba 			if (ret < 0) {
725efe120a0SFrank Holton 				btrfs_err(root->fs_info, "invalid commit interval");
7268b87dc17SDavid Sterba 				ret = -EINVAL;
7278b87dc17SDavid Sterba 				goto out;
7288b87dc17SDavid Sterba 			}
7298b87dc17SDavid Sterba 			if (intarg > 0) {
7308b87dc17SDavid Sterba 				if (intarg > 300) {
731efe120a0SFrank Holton 					btrfs_warn(root->fs_info, "excessive commit interval %d",
7328b87dc17SDavid Sterba 							intarg);
7338b87dc17SDavid Sterba 				}
7348b87dc17SDavid Sterba 				info->commit_interval = intarg;
7358b87dc17SDavid Sterba 			} else {
736efe120a0SFrank Holton 				btrfs_info(root->fs_info, "using default commit interval %ds",
7378b87dc17SDavid Sterba 				    BTRFS_DEFAULT_COMMIT_INTERVAL);
7388b87dc17SDavid Sterba 				info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
7398b87dc17SDavid Sterba 			}
7408b87dc17SDavid Sterba 			break;
741a7a3f7caSSage Weil 		case Opt_err:
742efe120a0SFrank Holton 			btrfs_info(root->fs_info, "unrecognized mount option '%s'", p);
743a7a3f7caSSage Weil 			ret = -EINVAL;
744a7a3f7caSSage Weil 			goto out;
74595e05289SChris Mason 		default:
746be20aa9dSChris Mason 			break;
74795e05289SChris Mason 		}
74895e05289SChris Mason 	}
749a7a3f7caSSage Weil out:
75073bc1876SJosef Bacik 	if (!ret && btrfs_test_opt(root, SPACE_CACHE))
751efe120a0SFrank Holton 		btrfs_info(root->fs_info, "disk space caching is enabled");
752da495eccSJosef Bacik 	kfree(orig);
753a7a3f7caSSage Weil 	return ret;
754edf24abeSChristoph Hellwig }
755edf24abeSChristoph Hellwig 
756edf24abeSChristoph Hellwig /*
757edf24abeSChristoph Hellwig  * Parse mount options that are required early in the mount process.
758edf24abeSChristoph Hellwig  *
759edf24abeSChristoph Hellwig  * All other options will be parsed on much later in the mount process and
760edf24abeSChristoph Hellwig  * only when we need to allocate a new super block.
761edf24abeSChristoph Hellwig  */
76297288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags,
76373f73415SJosef Bacik 		void *holder, char **subvol_name, u64 *subvol_objectid,
7645e2a4b25SDavid Sterba 		struct btrfs_fs_devices **fs_devices)
765edf24abeSChristoph Hellwig {
766edf24abeSChristoph Hellwig 	substring_t args[MAX_OPT_ARGS];
76783c8c9bdSJeff Liu 	char *device_name, *opts, *orig, *p;
7681493381fSWang Shilong 	char *num = NULL;
769edf24abeSChristoph Hellwig 	int error = 0;
770edf24abeSChristoph Hellwig 
771edf24abeSChristoph Hellwig 	if (!options)
772830c4adbSJosef Bacik 		return 0;
773edf24abeSChristoph Hellwig 
774edf24abeSChristoph Hellwig 	/*
775edf24abeSChristoph Hellwig 	 * strsep changes the string, duplicate it because parse_options
776edf24abeSChristoph Hellwig 	 * gets called twice
777edf24abeSChristoph Hellwig 	 */
778edf24abeSChristoph Hellwig 	opts = kstrdup(options, GFP_KERNEL);
779edf24abeSChristoph Hellwig 	if (!opts)
780edf24abeSChristoph Hellwig 		return -ENOMEM;
7813f3d0bc0STero Roponen 	orig = opts;
782edf24abeSChristoph Hellwig 
783edf24abeSChristoph Hellwig 	while ((p = strsep(&opts, ",")) != NULL) {
784edf24abeSChristoph Hellwig 		int token;
785edf24abeSChristoph Hellwig 		if (!*p)
786edf24abeSChristoph Hellwig 			continue;
787edf24abeSChristoph Hellwig 
788edf24abeSChristoph Hellwig 		token = match_token(p, tokens, args);
789edf24abeSChristoph Hellwig 		switch (token) {
790edf24abeSChristoph Hellwig 		case Opt_subvol:
791a90e8b6fSIlya Dryomov 			kfree(*subvol_name);
792edf24abeSChristoph Hellwig 			*subvol_name = match_strdup(&args[0]);
7932c334e87SWang Shilong 			if (!*subvol_name) {
7942c334e87SWang Shilong 				error = -ENOMEM;
7952c334e87SWang Shilong 				goto out;
7962c334e87SWang Shilong 			}
797edf24abeSChristoph Hellwig 			break;
79873f73415SJosef Bacik 		case Opt_subvolid:
7991493381fSWang Shilong 			num = match_strdup(&args[0]);
8001493381fSWang Shilong 			if (num) {
8011493381fSWang Shilong 				*subvol_objectid = memparse(num, NULL);
8021493381fSWang Shilong 				kfree(num);
8034849f01dSJosef Bacik 				/* we want the original fs_tree */
8041493381fSWang Shilong 				if (!*subvol_objectid)
8054849f01dSJosef Bacik 					*subvol_objectid =
8064849f01dSJosef Bacik 						BTRFS_FS_TREE_OBJECTID;
8072c334e87SWang Shilong 			} else {
8082c334e87SWang Shilong 				error = -EINVAL;
8092c334e87SWang Shilong 				goto out;
8104849f01dSJosef Bacik 			}
81173f73415SJosef Bacik 			break;
812e15d0542SXin Zhong 		case Opt_subvolrootid:
8135e2a4b25SDavid Sterba 			printk(KERN_WARNING
814efe120a0SFrank Holton 				"BTRFS: 'subvolrootid' mount option is deprecated and has "
815efe120a0SFrank Holton 				"no effect\n");
816e15d0542SXin Zhong 			break;
81743e570b0SChristoph Hellwig 		case Opt_device:
81883c8c9bdSJeff Liu 			device_name = match_strdup(&args[0]);
81983c8c9bdSJeff Liu 			if (!device_name) {
82083c8c9bdSJeff Liu 				error = -ENOMEM;
82183c8c9bdSJeff Liu 				goto out;
82283c8c9bdSJeff Liu 			}
82383c8c9bdSJeff Liu 			error = btrfs_scan_one_device(device_name,
82443e570b0SChristoph Hellwig 					flags, holder, fs_devices);
82583c8c9bdSJeff Liu 			kfree(device_name);
82643e570b0SChristoph Hellwig 			if (error)
827830c4adbSJosef Bacik 				goto out;
82843e570b0SChristoph Hellwig 			break;
829edf24abeSChristoph Hellwig 		default:
830edf24abeSChristoph Hellwig 			break;
831edf24abeSChristoph Hellwig 		}
832edf24abeSChristoph Hellwig 	}
833edf24abeSChristoph Hellwig 
834edf24abeSChristoph Hellwig out:
835830c4adbSJosef Bacik 	kfree(orig);
836edf24abeSChristoph Hellwig 	return error;
83795e05289SChris Mason }
83895e05289SChris Mason 
83905dbe683SOmar Sandoval static char *get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
84073f73415SJosef Bacik 					   u64 subvol_objectid)
84173f73415SJosef Bacik {
842815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
84305dbe683SOmar Sandoval 	struct btrfs_root *fs_root;
84405dbe683SOmar Sandoval 	struct btrfs_root_ref *root_ref;
84505dbe683SOmar Sandoval 	struct btrfs_inode_ref *inode_ref;
84605dbe683SOmar Sandoval 	struct btrfs_key key;
84705dbe683SOmar Sandoval 	struct btrfs_path *path = NULL;
84805dbe683SOmar Sandoval 	char *name = NULL, *ptr;
84905dbe683SOmar Sandoval 	u64 dirid;
85005dbe683SOmar Sandoval 	int len;
85105dbe683SOmar Sandoval 	int ret;
85205dbe683SOmar Sandoval 
85305dbe683SOmar Sandoval 	path = btrfs_alloc_path();
85405dbe683SOmar Sandoval 	if (!path) {
85505dbe683SOmar Sandoval 		ret = -ENOMEM;
85605dbe683SOmar Sandoval 		goto err;
85705dbe683SOmar Sandoval 	}
85805dbe683SOmar Sandoval 	path->leave_spinning = 1;
85905dbe683SOmar Sandoval 
86005dbe683SOmar Sandoval 	name = kmalloc(PATH_MAX, GFP_NOFS);
86105dbe683SOmar Sandoval 	if (!name) {
86205dbe683SOmar Sandoval 		ret = -ENOMEM;
86305dbe683SOmar Sandoval 		goto err;
86405dbe683SOmar Sandoval 	}
86505dbe683SOmar Sandoval 	ptr = name + PATH_MAX - 1;
86605dbe683SOmar Sandoval 	ptr[0] = '\0';
86705dbe683SOmar Sandoval 
86805dbe683SOmar Sandoval 	/*
86905dbe683SOmar Sandoval 	 * Walk up the subvolume trees in the tree of tree roots by root
87005dbe683SOmar Sandoval 	 * backrefs until we hit the top-level subvolume.
87105dbe683SOmar Sandoval 	 */
87205dbe683SOmar Sandoval 	while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
87305dbe683SOmar Sandoval 		key.objectid = subvol_objectid;
87405dbe683SOmar Sandoval 		key.type = BTRFS_ROOT_BACKREF_KEY;
87505dbe683SOmar Sandoval 		key.offset = (u64)-1;
87605dbe683SOmar Sandoval 
87705dbe683SOmar Sandoval 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
87805dbe683SOmar Sandoval 		if (ret < 0) {
87905dbe683SOmar Sandoval 			goto err;
88005dbe683SOmar Sandoval 		} else if (ret > 0) {
88105dbe683SOmar Sandoval 			ret = btrfs_previous_item(root, path, subvol_objectid,
88205dbe683SOmar Sandoval 						  BTRFS_ROOT_BACKREF_KEY);
88305dbe683SOmar Sandoval 			if (ret < 0) {
88405dbe683SOmar Sandoval 				goto err;
88505dbe683SOmar Sandoval 			} else if (ret > 0) {
88605dbe683SOmar Sandoval 				ret = -ENOENT;
88705dbe683SOmar Sandoval 				goto err;
88805dbe683SOmar Sandoval 			}
88905dbe683SOmar Sandoval 		}
89005dbe683SOmar Sandoval 
89105dbe683SOmar Sandoval 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
89205dbe683SOmar Sandoval 		subvol_objectid = key.offset;
89305dbe683SOmar Sandoval 
89405dbe683SOmar Sandoval 		root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
89505dbe683SOmar Sandoval 					  struct btrfs_root_ref);
89605dbe683SOmar Sandoval 		len = btrfs_root_ref_name_len(path->nodes[0], root_ref);
89705dbe683SOmar Sandoval 		ptr -= len + 1;
89805dbe683SOmar Sandoval 		if (ptr < name) {
89905dbe683SOmar Sandoval 			ret = -ENAMETOOLONG;
90005dbe683SOmar Sandoval 			goto err;
90105dbe683SOmar Sandoval 		}
90205dbe683SOmar Sandoval 		read_extent_buffer(path->nodes[0], ptr + 1,
90305dbe683SOmar Sandoval 				   (unsigned long)(root_ref + 1), len);
90405dbe683SOmar Sandoval 		ptr[0] = '/';
90505dbe683SOmar Sandoval 		dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);
90605dbe683SOmar Sandoval 		btrfs_release_path(path);
90705dbe683SOmar Sandoval 
90805dbe683SOmar Sandoval 		key.objectid = subvol_objectid;
90905dbe683SOmar Sandoval 		key.type = BTRFS_ROOT_ITEM_KEY;
91005dbe683SOmar Sandoval 		key.offset = (u64)-1;
91105dbe683SOmar Sandoval 		fs_root = btrfs_read_fs_root_no_name(fs_info, &key);
91205dbe683SOmar Sandoval 		if (IS_ERR(fs_root)) {
91305dbe683SOmar Sandoval 			ret = PTR_ERR(fs_root);
91405dbe683SOmar Sandoval 			goto err;
91505dbe683SOmar Sandoval 		}
91605dbe683SOmar Sandoval 
91705dbe683SOmar Sandoval 		/*
91805dbe683SOmar Sandoval 		 * Walk up the filesystem tree by inode refs until we hit the
91905dbe683SOmar Sandoval 		 * root directory.
92005dbe683SOmar Sandoval 		 */
92105dbe683SOmar Sandoval 		while (dirid != BTRFS_FIRST_FREE_OBJECTID) {
92205dbe683SOmar Sandoval 			key.objectid = dirid;
92305dbe683SOmar Sandoval 			key.type = BTRFS_INODE_REF_KEY;
92405dbe683SOmar Sandoval 			key.offset = (u64)-1;
92505dbe683SOmar Sandoval 
92605dbe683SOmar Sandoval 			ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
92705dbe683SOmar Sandoval 			if (ret < 0) {
92805dbe683SOmar Sandoval 				goto err;
92905dbe683SOmar Sandoval 			} else if (ret > 0) {
93005dbe683SOmar Sandoval 				ret = btrfs_previous_item(fs_root, path, dirid,
93105dbe683SOmar Sandoval 							  BTRFS_INODE_REF_KEY);
93205dbe683SOmar Sandoval 				if (ret < 0) {
93305dbe683SOmar Sandoval 					goto err;
93405dbe683SOmar Sandoval 				} else if (ret > 0) {
93505dbe683SOmar Sandoval 					ret = -ENOENT;
93605dbe683SOmar Sandoval 					goto err;
93705dbe683SOmar Sandoval 				}
93805dbe683SOmar Sandoval 			}
93905dbe683SOmar Sandoval 
94005dbe683SOmar Sandoval 			btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
94105dbe683SOmar Sandoval 			dirid = key.offset;
94205dbe683SOmar Sandoval 
94305dbe683SOmar Sandoval 			inode_ref = btrfs_item_ptr(path->nodes[0],
94405dbe683SOmar Sandoval 						   path->slots[0],
94505dbe683SOmar Sandoval 						   struct btrfs_inode_ref);
94605dbe683SOmar Sandoval 			len = btrfs_inode_ref_name_len(path->nodes[0],
94705dbe683SOmar Sandoval 						       inode_ref);
94805dbe683SOmar Sandoval 			ptr -= len + 1;
94905dbe683SOmar Sandoval 			if (ptr < name) {
95005dbe683SOmar Sandoval 				ret = -ENAMETOOLONG;
95105dbe683SOmar Sandoval 				goto err;
95205dbe683SOmar Sandoval 			}
95305dbe683SOmar Sandoval 			read_extent_buffer(path->nodes[0], ptr + 1,
95405dbe683SOmar Sandoval 					   (unsigned long)(inode_ref + 1), len);
95505dbe683SOmar Sandoval 			ptr[0] = '/';
95605dbe683SOmar Sandoval 			btrfs_release_path(path);
95705dbe683SOmar Sandoval 		}
95805dbe683SOmar Sandoval 	}
95905dbe683SOmar Sandoval 
96005dbe683SOmar Sandoval 	btrfs_free_path(path);
96105dbe683SOmar Sandoval 	if (ptr == name + PATH_MAX - 1) {
96205dbe683SOmar Sandoval 		name[0] = '/';
96305dbe683SOmar Sandoval 		name[1] = '\0';
96405dbe683SOmar Sandoval 	} else {
96505dbe683SOmar Sandoval 		memmove(name, ptr, name + PATH_MAX - ptr);
96605dbe683SOmar Sandoval 	}
96705dbe683SOmar Sandoval 	return name;
96805dbe683SOmar Sandoval 
96905dbe683SOmar Sandoval err:
97005dbe683SOmar Sandoval 	btrfs_free_path(path);
97105dbe683SOmar Sandoval 	kfree(name);
97205dbe683SOmar Sandoval 	return ERR_PTR(ret);
97305dbe683SOmar Sandoval }
97405dbe683SOmar Sandoval 
97505dbe683SOmar Sandoval static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid)
97605dbe683SOmar Sandoval {
97705dbe683SOmar Sandoval 	struct btrfs_root *root = fs_info->tree_root;
97873f73415SJosef Bacik 	struct btrfs_dir_item *di;
97973f73415SJosef Bacik 	struct btrfs_path *path;
98073f73415SJosef Bacik 	struct btrfs_key location;
98173f73415SJosef Bacik 	u64 dir_id;
98273f73415SJosef Bacik 
98373f73415SJosef Bacik 	path = btrfs_alloc_path();
98473f73415SJosef Bacik 	if (!path)
98505dbe683SOmar Sandoval 		return -ENOMEM;
98673f73415SJosef Bacik 	path->leave_spinning = 1;
98773f73415SJosef Bacik 
98873f73415SJosef Bacik 	/*
98973f73415SJosef Bacik 	 * Find the "default" dir item which points to the root item that we
99073f73415SJosef Bacik 	 * will mount by default if we haven't been given a specific subvolume
99173f73415SJosef Bacik 	 * to mount.
99273f73415SJosef Bacik 	 */
993815745cfSAl Viro 	dir_id = btrfs_super_root_dir(fs_info->super_copy);
99473f73415SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
995b0839166SJulia Lawall 	if (IS_ERR(di)) {
996b0839166SJulia Lawall 		btrfs_free_path(path);
99705dbe683SOmar Sandoval 		return PTR_ERR(di);
998b0839166SJulia Lawall 	}
99973f73415SJosef Bacik 	if (!di) {
100073f73415SJosef Bacik 		/*
100173f73415SJosef Bacik 		 * Ok the default dir item isn't there.  This is weird since
100273f73415SJosef Bacik 		 * it's always been there, but don't freak out, just try and
100305dbe683SOmar Sandoval 		 * mount the top-level subvolume.
100473f73415SJosef Bacik 		 */
100573f73415SJosef Bacik 		btrfs_free_path(path);
100605dbe683SOmar Sandoval 		*objectid = BTRFS_FS_TREE_OBJECTID;
100705dbe683SOmar Sandoval 		return 0;
100873f73415SJosef Bacik 	}
100973f73415SJosef Bacik 
101073f73415SJosef Bacik 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
101173f73415SJosef Bacik 	btrfs_free_path(path);
101205dbe683SOmar Sandoval 	*objectid = location.objectid;
101305dbe683SOmar Sandoval 	return 0;
101473f73415SJosef Bacik }
101573f73415SJosef Bacik 
10168a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb,
10178a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
10188a4b83ccSChris Mason 			    void *data, int silent)
10192e635a27SChris Mason {
10202e635a27SChris Mason 	struct inode *inode;
1021815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
10225d4f98a2SYan Zheng 	struct btrfs_key key;
102339279cc3SChris Mason 	int err;
10242e635a27SChris Mason 
10252e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
10262e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
1027e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
1028af53d29aSAl Viro 	sb->s_d_op = &btrfs_dentry_operations;
1029be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
10305103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
10312e635a27SChris Mason 	sb->s_time_gran = 1;
10320eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL
103333268eafSJosef Bacik 	sb->s_flags |= MS_POSIXACL;
103449cf6f45SChris Ball #endif
10350c4d2d95SJosef Bacik 	sb->s_flags |= MS_I_VERSION;
1036ad2b2c80SAl Viro 	err = open_ctree(sb, fs_devices, (char *)data);
1037ad2b2c80SAl Viro 	if (err) {
1038efe120a0SFrank Holton 		printk(KERN_ERR "BTRFS: open_ctree failed\n");
1039ad2b2c80SAl Viro 		return err;
1040e20d96d6SChris Mason 	}
1041b888db2bSChris Mason 
10425d4f98a2SYan Zheng 	key.objectid = BTRFS_FIRST_FREE_OBJECTID;
10435d4f98a2SYan Zheng 	key.type = BTRFS_INODE_ITEM_KEY;
10445d4f98a2SYan Zheng 	key.offset = 0;
104598c7089cSAl Viro 	inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL);
10465d4f98a2SYan Zheng 	if (IS_ERR(inode)) {
10475d4f98a2SYan Zheng 		err = PTR_ERR(inode);
104839279cc3SChris Mason 		goto fail_close;
104939279cc3SChris Mason 	}
10502e635a27SChris Mason 
105148fde701SAl Viro 	sb->s_root = d_make_root(inode);
105248fde701SAl Viro 	if (!sb->s_root) {
105339279cc3SChris Mason 		err = -ENOMEM;
105439279cc3SChris Mason 		goto fail_close;
10552e635a27SChris Mason 	}
105658176a96SJosef Bacik 
10576885f308SChris Mason 	save_mount_options(sb, data);
105890a887c9SDan Magenheimer 	cleancache_init_fs(sb);
105959553edfSAl Viro 	sb->s_flags |= MS_ACTIVE;
10602e635a27SChris Mason 	return 0;
10612e635a27SChris Mason 
106239279cc3SChris Mason fail_close:
1063815745cfSAl Viro 	close_ctree(fs_info->tree_root);
1064d5719762SChris Mason 	return err;
1065d5719762SChris Mason }
1066d5719762SChris Mason 
10676bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
1068d5719762SChris Mason {
1069d5719762SChris Mason 	struct btrfs_trans_handle *trans;
1070815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1071815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
1072df2ce34cSChris Mason 
10731abe9b8aSliubo 	trace_btrfs_sync_fs(wait);
10741abe9b8aSliubo 
1075d561c025SChris Mason 	if (!wait) {
1076815745cfSAl Viro 		filemap_flush(fs_info->btree_inode->i_mapping);
1077df2ce34cSChris Mason 		return 0;
1078d561c025SChris Mason 	}
1079771ed689SChris Mason 
1080b0244199SMiao Xie 	btrfs_wait_ordered_roots(fs_info, -1);
1081771ed689SChris Mason 
1082d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
108360376ce4SJosef Bacik 	if (IS_ERR(trans)) {
1084354aa0fbSMiao Xie 		/* no transaction, don't bother */
10856b5fe46dSDavid Sterba 		if (PTR_ERR(trans) == -ENOENT) {
10866b5fe46dSDavid Sterba 			/*
10876b5fe46dSDavid Sterba 			 * Exit unless we have some pending changes
10886b5fe46dSDavid Sterba 			 * that need to go through commit
10896b5fe46dSDavid Sterba 			 */
10906b5fe46dSDavid Sterba 			if (fs_info->pending_changes == 0)
1091bd7de2c9SJosef Bacik 				return 0;
1092a53f4f8eSQu Wenruo 			/*
1093a53f4f8eSQu Wenruo 			 * A non-blocking test if the fs is frozen. We must not
1094a53f4f8eSQu Wenruo 			 * start a new transaction here otherwise a deadlock
1095a53f4f8eSQu Wenruo 			 * happens. The pending operations are delayed to the
1096a53f4f8eSQu Wenruo 			 * next commit after thawing.
1097a53f4f8eSQu Wenruo 			 */
1098a53f4f8eSQu Wenruo 			if (__sb_start_write(sb, SB_FREEZE_WRITE, false))
1099a53f4f8eSQu Wenruo 				__sb_end_write(sb, SB_FREEZE_WRITE);
1100a53f4f8eSQu Wenruo 			else
1101a53f4f8eSQu Wenruo 				return 0;
11026b5fe46dSDavid Sterba 			trans = btrfs_start_transaction(root, 0);
110360376ce4SJosef Bacik 		}
110498bd5c54SDavid Sterba 		if (IS_ERR(trans))
110598bd5c54SDavid Sterba 			return PTR_ERR(trans);
11066b5fe46dSDavid Sterba 	}
1107bd7de2c9SJosef Bacik 	return btrfs_commit_transaction(trans, root);
1108d5719762SChris Mason }
1109d5719762SChris Mason 
111034c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
1111a9572a15SEric Paris {
1112815745cfSAl Viro 	struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
1113815745cfSAl Viro 	struct btrfs_root *root = info->tree_root;
1114200da64eSTsutomu Itoh 	char *compress_type;
1115a9572a15SEric Paris 
1116a9572a15SEric Paris 	if (btrfs_test_opt(root, DEGRADED))
1117a9572a15SEric Paris 		seq_puts(seq, ",degraded");
1118a9572a15SEric Paris 	if (btrfs_test_opt(root, NODATASUM))
1119a9572a15SEric Paris 		seq_puts(seq, ",nodatasum");
1120a9572a15SEric Paris 	if (btrfs_test_opt(root, NODATACOW))
1121a9572a15SEric Paris 		seq_puts(seq, ",nodatacow");
1122a9572a15SEric Paris 	if (btrfs_test_opt(root, NOBARRIER))
1123a9572a15SEric Paris 		seq_puts(seq, ",nobarrier");
112495ac567aSFilipe David Borba Manana 	if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
1125c1c9ff7cSGeert Uytterhoeven 		seq_printf(seq, ",max_inline=%llu", info->max_inline);
1126a9572a15SEric Paris 	if (info->alloc_start != 0)
1127c1c9ff7cSGeert Uytterhoeven 		seq_printf(seq, ",alloc_start=%llu", info->alloc_start);
1128a9572a15SEric Paris 	if (info->thread_pool_size !=  min_t(unsigned long,
1129a9572a15SEric Paris 					     num_online_cpus() + 2, 8))
1130a9572a15SEric Paris 		seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
1131200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, COMPRESS)) {
1132200da64eSTsutomu Itoh 		if (info->compress_type == BTRFS_COMPRESS_ZLIB)
1133200da64eSTsutomu Itoh 			compress_type = "zlib";
1134200da64eSTsutomu Itoh 		else
1135200da64eSTsutomu Itoh 			compress_type = "lzo";
1136200da64eSTsutomu Itoh 		if (btrfs_test_opt(root, FORCE_COMPRESS))
1137200da64eSTsutomu Itoh 			seq_printf(seq, ",compress-force=%s", compress_type);
1138200da64eSTsutomu Itoh 		else
1139200da64eSTsutomu Itoh 			seq_printf(seq, ",compress=%s", compress_type);
1140200da64eSTsutomu Itoh 	}
1141c289811cSChris Mason 	if (btrfs_test_opt(root, NOSSD))
1142c289811cSChris Mason 		seq_puts(seq, ",nossd");
1143451d7585SChris Mason 	if (btrfs_test_opt(root, SSD_SPREAD))
1144451d7585SChris Mason 		seq_puts(seq, ",ssd_spread");
1145451d7585SChris Mason 	else if (btrfs_test_opt(root, SSD))
1146a9572a15SEric Paris 		seq_puts(seq, ",ssd");
11473a5e1404SSage Weil 	if (btrfs_test_opt(root, NOTREELOG))
11486b65c5c6SSage Weil 		seq_puts(seq, ",notreelog");
1149dccae999SSage Weil 	if (btrfs_test_opt(root, FLUSHONCOMMIT))
11506b65c5c6SSage Weil 		seq_puts(seq, ",flushoncommit");
115120a5239aSMatthew Wilcox 	if (btrfs_test_opt(root, DISCARD))
115220a5239aSMatthew Wilcox 		seq_puts(seq, ",discard");
1153a9572a15SEric Paris 	if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
1154a9572a15SEric Paris 		seq_puts(seq, ",noacl");
1155200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, SPACE_CACHE))
1156200da64eSTsutomu Itoh 		seq_puts(seq, ",space_cache");
115773bc1876SJosef Bacik 	else
11588965593eSDavid Sterba 		seq_puts(seq, ",nospace_cache");
1159f420ee1eSStefan Behrens 	if (btrfs_test_opt(root, RESCAN_UUID_TREE))
1160f420ee1eSStefan Behrens 		seq_puts(seq, ",rescan_uuid_tree");
1161200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, CLEAR_CACHE))
1162200da64eSTsutomu Itoh 		seq_puts(seq, ",clear_cache");
1163200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1164200da64eSTsutomu Itoh 		seq_puts(seq, ",user_subvol_rm_allowed");
11650942caa3SDavid Sterba 	if (btrfs_test_opt(root, ENOSPC_DEBUG))
11660942caa3SDavid Sterba 		seq_puts(seq, ",enospc_debug");
11670942caa3SDavid Sterba 	if (btrfs_test_opt(root, AUTO_DEFRAG))
11680942caa3SDavid Sterba 		seq_puts(seq, ",autodefrag");
11690942caa3SDavid Sterba 	if (btrfs_test_opt(root, INODE_MAP_CACHE))
11700942caa3SDavid Sterba 		seq_puts(seq, ",inode_cache");
11719555c6c1SIlya Dryomov 	if (btrfs_test_opt(root, SKIP_BALANCE))
11729555c6c1SIlya Dryomov 		seq_puts(seq, ",skip_balance");
11738507d216SWang Shilong 	if (btrfs_test_opt(root, RECOVERY))
11748507d216SWang Shilong 		seq_puts(seq, ",recovery");
11758507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
11768507d216SWang Shilong 	if (btrfs_test_opt(root, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA))
11778507d216SWang Shilong 		seq_puts(seq, ",check_int_data");
11788507d216SWang Shilong 	else if (btrfs_test_opt(root, CHECK_INTEGRITY))
11798507d216SWang Shilong 		seq_puts(seq, ",check_int");
11808507d216SWang Shilong 	if (info->check_integrity_print_mask)
11818507d216SWang Shilong 		seq_printf(seq, ",check_int_print_mask=%d",
11828507d216SWang Shilong 				info->check_integrity_print_mask);
11838507d216SWang Shilong #endif
11848507d216SWang Shilong 	if (info->metadata_ratio)
11858507d216SWang Shilong 		seq_printf(seq, ",metadata_ratio=%d",
11868507d216SWang Shilong 				info->metadata_ratio);
11878c342930SJeff Mahoney 	if (btrfs_test_opt(root, PANIC_ON_FATAL_ERROR))
11888c342930SJeff Mahoney 		seq_puts(seq, ",fatal_errors=panic");
11898b87dc17SDavid Sterba 	if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
11908b87dc17SDavid Sterba 		seq_printf(seq, ",commit=%d", info->commit_interval);
1191c8d3fe02SOmar Sandoval 	seq_printf(seq, ",subvolid=%llu",
1192c8d3fe02SOmar Sandoval 		  BTRFS_I(d_inode(dentry))->root->root_key.objectid);
1193c8d3fe02SOmar Sandoval 	seq_puts(seq, ",subvol=");
1194c8d3fe02SOmar Sandoval 	seq_dentry(seq, dentry, " \t\n\\");
1195a9572a15SEric Paris 	return 0;
1196a9572a15SEric Paris }
1197a9572a15SEric Paris 
1198a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data)
11992e635a27SChris Mason {
1200815745cfSAl Viro 	struct btrfs_fs_info *p = data;
1201815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(s);
12024b82d6e4SYan 
1203815745cfSAl Viro 	return fs_info->fs_devices == p->fs_devices;
12044b82d6e4SYan }
12054b82d6e4SYan 
1206450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data)
1207450ba0eaSJosef Bacik {
12086de1d09dSAl Viro 	int err = set_anon_super(s, data);
12096de1d09dSAl Viro 	if (!err)
1210450ba0eaSJosef Bacik 		s->s_fs_info = data;
12116de1d09dSAl Viro 	return err;
1212450ba0eaSJosef Bacik }
1213450ba0eaSJosef Bacik 
1214830c4adbSJosef Bacik /*
1215f9d9ef62SDavid Sterba  * subvolumes are identified by ino 256
1216f9d9ef62SDavid Sterba  */
1217f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode)
1218f9d9ef62SDavid Sterba {
1219f9d9ef62SDavid Sterba 	if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
1220f9d9ef62SDavid Sterba 		return 1;
1221f9d9ef62SDavid Sterba 	return 0;
1222f9d9ef62SDavid Sterba }
1223f9d9ef62SDavid Sterba 
1224f9d9ef62SDavid Sterba /*
1225e6e4dbe8SOmar Sandoval  * This will add subvolid=0 to the argument string while removing any subvol=
1226e6e4dbe8SOmar Sandoval  * and subvolid= arguments to make sure we get the top-level root for path
1227e6e4dbe8SOmar Sandoval  * walking to the subvol we want.
1228830c4adbSJosef Bacik  */
1229830c4adbSJosef Bacik static char *setup_root_args(char *args)
1230830c4adbSJosef Bacik {
1231e6e4dbe8SOmar Sandoval 	char *buf, *dst, *sep;
1232830c4adbSJosef Bacik 
1233e6e4dbe8SOmar Sandoval 	if (!args)
1234e6e4dbe8SOmar Sandoval 		return kstrdup("subvolid=0", GFP_NOFS);
1235830c4adbSJosef Bacik 
1236e6e4dbe8SOmar Sandoval 	/* The worst case is that we add ",subvolid=0" to the end. */
1237e6e4dbe8SOmar Sandoval 	buf = dst = kmalloc(strlen(args) + strlen(",subvolid=0") + 1, GFP_NOFS);
1238f60d16a8SJim Meyering 	if (!buf)
1239f60d16a8SJim Meyering 		return NULL;
1240830c4adbSJosef Bacik 
1241e6e4dbe8SOmar Sandoval 	while (1) {
1242e6e4dbe8SOmar Sandoval 		sep = strchrnul(args, ',');
1243e6e4dbe8SOmar Sandoval 		if (!strstarts(args, "subvol=") &&
1244e6e4dbe8SOmar Sandoval 		    !strstarts(args, "subvolid=")) {
1245e6e4dbe8SOmar Sandoval 			memcpy(dst, args, sep - args);
1246e6e4dbe8SOmar Sandoval 			dst += sep - args;
1247e6e4dbe8SOmar Sandoval 			*dst++ = ',';
1248830c4adbSJosef Bacik 		}
1249e6e4dbe8SOmar Sandoval 		if (*sep)
1250e6e4dbe8SOmar Sandoval 			args = sep + 1;
1251e6e4dbe8SOmar Sandoval 		else
1252e6e4dbe8SOmar Sandoval 			break;
1253e6e4dbe8SOmar Sandoval 	}
1254f60d16a8SJim Meyering 	strcpy(dst, "subvolid=0");
1255830c4adbSJosef Bacik 
1256f60d16a8SJim Meyering 	return buf;
1257830c4adbSJosef Bacik }
1258830c4adbSJosef Bacik 
1259bb289b7bSOmar Sandoval static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
1260bb289b7bSOmar Sandoval 				   int flags, const char *device_name,
1261bb289b7bSOmar Sandoval 				   char *data)
1262830c4adbSJosef Bacik {
1263830c4adbSJosef Bacik 	struct dentry *root;
1264fa330659SOmar Sandoval 	struct vfsmount *mnt = NULL;
1265830c4adbSJosef Bacik 	char *newargs;
1266fa330659SOmar Sandoval 	int ret;
1267830c4adbSJosef Bacik 
1268830c4adbSJosef Bacik 	newargs = setup_root_args(data);
1269fa330659SOmar Sandoval 	if (!newargs) {
1270fa330659SOmar Sandoval 		root = ERR_PTR(-ENOMEM);
1271fa330659SOmar Sandoval 		goto out;
1272fa330659SOmar Sandoval 	}
12730723a047SHarald Hoyer 
1274fa330659SOmar Sandoval 	mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name, newargs);
1275fa330659SOmar Sandoval 	if (PTR_ERR_OR_ZERO(mnt) == -EBUSY) {
12760723a047SHarald Hoyer 		if (flags & MS_RDONLY) {
1277fa330659SOmar Sandoval 			mnt = vfs_kern_mount(&btrfs_fs_type, flags & ~MS_RDONLY,
1278fa330659SOmar Sandoval 					     device_name, newargs);
12790723a047SHarald Hoyer 		} else {
1280fa330659SOmar Sandoval 			mnt = vfs_kern_mount(&btrfs_fs_type, flags | MS_RDONLY,
1281fa330659SOmar Sandoval 					     device_name, newargs);
12820040e606SChristoph Jaeger 			if (IS_ERR(mnt)) {
1283fa330659SOmar Sandoval 				root = ERR_CAST(mnt);
1284fa330659SOmar Sandoval 				mnt = NULL;
1285fa330659SOmar Sandoval 				goto out;
12860040e606SChristoph Jaeger 			}
12870723a047SHarald Hoyer 
1288773cd04eSOmar Sandoval 			down_write(&mnt->mnt_sb->s_umount);
1289fa330659SOmar Sandoval 			ret = btrfs_remount(mnt->mnt_sb, &flags, NULL);
1290773cd04eSOmar Sandoval 			up_write(&mnt->mnt_sb->s_umount);
1291fa330659SOmar Sandoval 			if (ret < 0) {
1292fa330659SOmar Sandoval 				root = ERR_PTR(ret);
1293fa330659SOmar Sandoval 				goto out;
12940723a047SHarald Hoyer 			}
12950723a047SHarald Hoyer 		}
12960723a047SHarald Hoyer 	}
1297fa330659SOmar Sandoval 	if (IS_ERR(mnt)) {
1298fa330659SOmar Sandoval 		root = ERR_CAST(mnt);
1299fa330659SOmar Sandoval 		mnt = NULL;
1300fa330659SOmar Sandoval 		goto out;
1301fa330659SOmar Sandoval 	}
1302830c4adbSJosef Bacik 
130305dbe683SOmar Sandoval 	if (!subvol_name) {
130405dbe683SOmar Sandoval 		if (!subvol_objectid) {
130505dbe683SOmar Sandoval 			ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
130605dbe683SOmar Sandoval 							  &subvol_objectid);
130705dbe683SOmar Sandoval 			if (ret) {
130805dbe683SOmar Sandoval 				root = ERR_PTR(ret);
130905dbe683SOmar Sandoval 				goto out;
131005dbe683SOmar Sandoval 			}
131105dbe683SOmar Sandoval 		}
131205dbe683SOmar Sandoval 		subvol_name = get_subvol_name_from_objectid(btrfs_sb(mnt->mnt_sb),
131305dbe683SOmar Sandoval 							    subvol_objectid);
131405dbe683SOmar Sandoval 		if (IS_ERR(subvol_name)) {
131505dbe683SOmar Sandoval 			root = ERR_CAST(subvol_name);
131605dbe683SOmar Sandoval 			subvol_name = NULL;
131705dbe683SOmar Sandoval 			goto out;
131805dbe683SOmar Sandoval 		}
131905dbe683SOmar Sandoval 
132005dbe683SOmar Sandoval 	}
132105dbe683SOmar Sandoval 
1322ea441d11SAl Viro 	root = mount_subtree(mnt, subvol_name);
1323fa330659SOmar Sandoval 	/* mount_subtree() drops our reference on the vfsmount. */
1324fa330659SOmar Sandoval 	mnt = NULL;
1325830c4adbSJosef Bacik 
1326bb289b7bSOmar Sandoval 	if (!IS_ERR(root)) {
1327ea441d11SAl Viro 		struct super_block *s = root->d_sb;
1328bb289b7bSOmar Sandoval 		struct inode *root_inode = d_inode(root);
1329bb289b7bSOmar Sandoval 		u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid;
1330bb289b7bSOmar Sandoval 
1331bb289b7bSOmar Sandoval 		ret = 0;
1332bb289b7bSOmar Sandoval 		if (!is_subvolume_inode(root_inode)) {
1333bb289b7bSOmar Sandoval 			pr_err("BTRFS: '%s' is not a valid subvolume\n",
1334bb289b7bSOmar Sandoval 			       subvol_name);
1335bb289b7bSOmar Sandoval 			ret = -EINVAL;
1336bb289b7bSOmar Sandoval 		}
1337bb289b7bSOmar Sandoval 		if (subvol_objectid && root_objectid != subvol_objectid) {
133805dbe683SOmar Sandoval 			/*
133905dbe683SOmar Sandoval 			 * This will also catch a race condition where a
134005dbe683SOmar Sandoval 			 * subvolume which was passed by ID is renamed and
134105dbe683SOmar Sandoval 			 * another subvolume is renamed over the old location.
134205dbe683SOmar Sandoval 			 */
1343bb289b7bSOmar Sandoval 			pr_err("BTRFS: subvol '%s' does not match subvolid %llu\n",
1344bb289b7bSOmar Sandoval 			       subvol_name, subvol_objectid);
1345bb289b7bSOmar Sandoval 			ret = -EINVAL;
1346bb289b7bSOmar Sandoval 		}
1347bb289b7bSOmar Sandoval 		if (ret) {
1348ea441d11SAl Viro 			dput(root);
1349bb289b7bSOmar Sandoval 			root = ERR_PTR(ret);
1350ea441d11SAl Viro 			deactivate_locked_super(s);
1351bb289b7bSOmar Sandoval 		}
1352f9d9ef62SDavid Sterba 	}
1353f9d9ef62SDavid Sterba 
1354fa330659SOmar Sandoval out:
1355fa330659SOmar Sandoval 	mntput(mnt);
1356fa330659SOmar Sandoval 	kfree(newargs);
1357fa330659SOmar Sandoval 	kfree(subvol_name);
1358830c4adbSJosef Bacik 	return root;
1359830c4adbSJosef Bacik }
1360450ba0eaSJosef Bacik 
1361f667aef6SQu Wenruo static int parse_security_options(char *orig_opts,
1362f667aef6SQu Wenruo 				  struct security_mnt_opts *sec_opts)
1363f667aef6SQu Wenruo {
1364f667aef6SQu Wenruo 	char *secdata = NULL;
1365f667aef6SQu Wenruo 	int ret = 0;
1366f667aef6SQu Wenruo 
1367f667aef6SQu Wenruo 	secdata = alloc_secdata();
1368f667aef6SQu Wenruo 	if (!secdata)
1369f667aef6SQu Wenruo 		return -ENOMEM;
1370f667aef6SQu Wenruo 	ret = security_sb_copy_data(orig_opts, secdata);
1371f667aef6SQu Wenruo 	if (ret) {
1372f667aef6SQu Wenruo 		free_secdata(secdata);
1373f667aef6SQu Wenruo 		return ret;
1374f667aef6SQu Wenruo 	}
1375f667aef6SQu Wenruo 	ret = security_sb_parse_opts_str(secdata, sec_opts);
1376f667aef6SQu Wenruo 	free_secdata(secdata);
1377f667aef6SQu Wenruo 	return ret;
1378f667aef6SQu Wenruo }
1379f667aef6SQu Wenruo 
1380f667aef6SQu Wenruo static int setup_security_options(struct btrfs_fs_info *fs_info,
1381f667aef6SQu Wenruo 				  struct super_block *sb,
1382f667aef6SQu Wenruo 				  struct security_mnt_opts *sec_opts)
1383f667aef6SQu Wenruo {
1384f667aef6SQu Wenruo 	int ret = 0;
1385f667aef6SQu Wenruo 
1386f667aef6SQu Wenruo 	/*
1387f667aef6SQu Wenruo 	 * Call security_sb_set_mnt_opts() to check whether new sec_opts
1388f667aef6SQu Wenruo 	 * is valid.
1389f667aef6SQu Wenruo 	 */
1390f667aef6SQu Wenruo 	ret = security_sb_set_mnt_opts(sb, sec_opts, 0, NULL);
1391f667aef6SQu Wenruo 	if (ret)
1392f667aef6SQu Wenruo 		return ret;
1393f667aef6SQu Wenruo 
1394a43bb39bSQu Wenruo #ifdef CONFIG_SECURITY
1395f667aef6SQu Wenruo 	if (!fs_info->security_opts.num_mnt_opts) {
1396f667aef6SQu Wenruo 		/* first time security setup, copy sec_opts to fs_info */
1397f667aef6SQu Wenruo 		memcpy(&fs_info->security_opts, sec_opts, sizeof(*sec_opts));
1398f667aef6SQu Wenruo 	} else {
1399f667aef6SQu Wenruo 		/*
1400f667aef6SQu Wenruo 		 * Since SELinux(the only one supports security_mnt_opts) does
1401f667aef6SQu Wenruo 		 * NOT support changing context during remount/mount same sb,
1402f667aef6SQu Wenruo 		 * This must be the same or part of the same security options,
1403f667aef6SQu Wenruo 		 * just free it.
1404f667aef6SQu Wenruo 		 */
1405f667aef6SQu Wenruo 		security_free_mnt_opts(sec_opts);
1406f667aef6SQu Wenruo 	}
1407a43bb39bSQu Wenruo #endif
1408f667aef6SQu Wenruo 	return ret;
1409f667aef6SQu Wenruo }
1410f667aef6SQu Wenruo 
1411edf24abeSChristoph Hellwig /*
1412edf24abeSChristoph Hellwig  * Find a superblock for the given device / mount point.
1413edf24abeSChristoph Hellwig  *
1414edf24abeSChristoph Hellwig  * Note:  This is based on get_sb_bdev from fs/super.c with a few additions
1415edf24abeSChristoph Hellwig  *	  for multiple device setup.  Make sure to keep it in sync.
1416edf24abeSChristoph Hellwig  */
1417061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
1418306e16ceSDavid Sterba 		const char *device_name, void *data)
14194b82d6e4SYan {
14204b82d6e4SYan 	struct block_device *bdev = NULL;
14214b82d6e4SYan 	struct super_block *s;
14228a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices = NULL;
1423450ba0eaSJosef Bacik 	struct btrfs_fs_info *fs_info = NULL;
1424f667aef6SQu Wenruo 	struct security_mnt_opts new_sec_opts;
142597288f2cSChristoph Hellwig 	fmode_t mode = FMODE_READ;
142673f73415SJosef Bacik 	char *subvol_name = NULL;
142773f73415SJosef Bacik 	u64 subvol_objectid = 0;
14284b82d6e4SYan 	int error = 0;
14294b82d6e4SYan 
143097288f2cSChristoph Hellwig 	if (!(flags & MS_RDONLY))
143197288f2cSChristoph Hellwig 		mode |= FMODE_WRITE;
143297288f2cSChristoph Hellwig 
143397288f2cSChristoph Hellwig 	error = btrfs_parse_early_options(data, mode, fs_type,
143473f73415SJosef Bacik 					  &subvol_name, &subvol_objectid,
14355e2a4b25SDavid Sterba 					  &fs_devices);
1436f23c8af8SIlya Dryomov 	if (error) {
1437f23c8af8SIlya Dryomov 		kfree(subvol_name);
1438061dbc6bSAl Viro 		return ERR_PTR(error);
1439f23c8af8SIlya Dryomov 	}
1440edf24abeSChristoph Hellwig 
144105dbe683SOmar Sandoval 	if (subvol_name || subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
1442fa330659SOmar Sandoval 		/* mount_subvol() will free subvol_name. */
1443bb289b7bSOmar Sandoval 		return mount_subvol(subvol_name, subvol_objectid, flags,
1444bb289b7bSOmar Sandoval 				    device_name, data);
1445830c4adbSJosef Bacik 	}
1446830c4adbSJosef Bacik 
1447f667aef6SQu Wenruo 	security_init_mnt_opts(&new_sec_opts);
1448f667aef6SQu Wenruo 	if (data) {
1449f667aef6SQu Wenruo 		error = parse_security_options(data, &new_sec_opts);
14508a4b83ccSChris Mason 		if (error)
1451830c4adbSJosef Bacik 			return ERR_PTR(error);
1452f667aef6SQu Wenruo 	}
1453f667aef6SQu Wenruo 
1454f667aef6SQu Wenruo 	error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
1455f667aef6SQu Wenruo 	if (error)
1456f667aef6SQu Wenruo 		goto error_sec_opts;
14574b82d6e4SYan 
1458450ba0eaSJosef Bacik 	/*
1459450ba0eaSJosef Bacik 	 * Setup a dummy root and fs_info for test/set super.  This is because
1460450ba0eaSJosef Bacik 	 * we don't actually fill this stuff out until open_ctree, but we need
1461450ba0eaSJosef Bacik 	 * it for searching for existing supers, so this lets us do that and
1462450ba0eaSJosef Bacik 	 * then open_ctree will properly initialize everything later.
1463450ba0eaSJosef Bacik 	 */
1464450ba0eaSJosef Bacik 	fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
1465f667aef6SQu Wenruo 	if (!fs_info) {
1466f667aef6SQu Wenruo 		error = -ENOMEM;
1467f667aef6SQu Wenruo 		goto error_sec_opts;
1468f667aef6SQu Wenruo 	}
146904d21a24SIlya Dryomov 
1470450ba0eaSJosef Bacik 	fs_info->fs_devices = fs_devices;
1471450ba0eaSJosef Bacik 
14726c41761fSDavid Sterba 	fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
14736c41761fSDavid Sterba 	fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
1474f667aef6SQu Wenruo 	security_init_mnt_opts(&fs_info->security_opts);
14756c41761fSDavid Sterba 	if (!fs_info->super_copy || !fs_info->super_for_commit) {
14766c41761fSDavid Sterba 		error = -ENOMEM;
147704d21a24SIlya Dryomov 		goto error_fs_info;
147804d21a24SIlya Dryomov 	}
147904d21a24SIlya Dryomov 
148004d21a24SIlya Dryomov 	error = btrfs_open_devices(fs_devices, mode, fs_type);
148104d21a24SIlya Dryomov 	if (error)
148204d21a24SIlya Dryomov 		goto error_fs_info;
148304d21a24SIlya Dryomov 
148404d21a24SIlya Dryomov 	if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
148504d21a24SIlya Dryomov 		error = -EACCES;
14866c41761fSDavid Sterba 		goto error_close_devices;
14876c41761fSDavid Sterba 	}
14886c41761fSDavid Sterba 
1489dfe25020SChris Mason 	bdev = fs_devices->latest_bdev;
14909249e17fSDavid Howells 	s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC,
14919249e17fSDavid Howells 		 fs_info);
1492830c4adbSJosef Bacik 	if (IS_ERR(s)) {
1493830c4adbSJosef Bacik 		error = PTR_ERR(s);
1494830c4adbSJosef Bacik 		goto error_close_devices;
1495830c4adbSJosef Bacik 	}
14964b82d6e4SYan 
14974b82d6e4SYan 	if (s->s_root) {
14982b82032cSYan Zheng 		btrfs_close_devices(fs_devices);
14996c41761fSDavid Sterba 		free_fs_info(fs_info);
150059553edfSAl Viro 		if ((flags ^ s->s_flags) & MS_RDONLY)
150159553edfSAl Viro 			error = -EBUSY;
15024b82d6e4SYan 	} else {
15034b82d6e4SYan 		char b[BDEVNAME_SIZE];
15044b82d6e4SYan 
15054b82d6e4SYan 		strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
1506815745cfSAl Viro 		btrfs_sb(s)->bdev_holder = fs_type;
15078a4b83ccSChris Mason 		error = btrfs_fill_super(s, fs_devices, data,
15088a4b83ccSChris Mason 					 flags & MS_SILENT ? 1 : 0);
15094b82d6e4SYan 	}
151005dbe683SOmar Sandoval 	if (error) {
1511e15d0542SXin Zhong 		deactivate_locked_super(s);
1512f667aef6SQu Wenruo 		goto error_sec_opts;
1513f667aef6SQu Wenruo 	}
1514f667aef6SQu Wenruo 
1515f667aef6SQu Wenruo 	fs_info = btrfs_sb(s);
1516f667aef6SQu Wenruo 	error = setup_security_options(fs_info, s, &new_sec_opts);
1517f667aef6SQu Wenruo 	if (error) {
1518f667aef6SQu Wenruo 		deactivate_locked_super(s);
1519f667aef6SQu Wenruo 		goto error_sec_opts;
1520f667aef6SQu Wenruo 	}
15214b82d6e4SYan 
152205dbe683SOmar Sandoval 	return dget(s->s_root);
15234b82d6e4SYan 
1524c146afadSYan Zheng error_close_devices:
15258a4b83ccSChris Mason 	btrfs_close_devices(fs_devices);
152604d21a24SIlya Dryomov error_fs_info:
15276c41761fSDavid Sterba 	free_fs_info(fs_info);
1528f667aef6SQu Wenruo error_sec_opts:
1529f667aef6SQu Wenruo 	security_free_mnt_opts(&new_sec_opts);
1530061dbc6bSAl Viro 	return ERR_PTR(error);
15314b82d6e4SYan }
15322e635a27SChris Mason 
15330d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
15340d2450abSSergei Trofimovich 				     int new_pool_size, int old_pool_size)
15350d2450abSSergei Trofimovich {
15360d2450abSSergei Trofimovich 	if (new_pool_size == old_pool_size)
15370d2450abSSergei Trofimovich 		return;
15380d2450abSSergei Trofimovich 
15390d2450abSSergei Trofimovich 	fs_info->thread_pool_size = new_pool_size;
15400d2450abSSergei Trofimovich 
1541efe120a0SFrank Holton 	btrfs_info(fs_info, "resize thread pool %d -> %d",
15420d2450abSSergei Trofimovich 	       old_pool_size, new_pool_size);
15430d2450abSSergei Trofimovich 
15445cdc7ad3SQu Wenruo 	btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
1545afe3d242SQu Wenruo 	btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
1546a8c93d4eSQu Wenruo 	btrfs_workqueue_set_max(fs_info->submit_workers, new_pool_size);
1547e66f0bb1SQu Wenruo 	btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
1548fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size);
1549fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size);
1550fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_meta_write_workers,
1551fccb5d86SQu Wenruo 				new_pool_size);
1552fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
1553fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
15545b3bc44eSQu Wenruo 	btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
1555736cfa15SQu Wenruo 	btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size);
15560339ef2fSQu Wenruo 	btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers,
1557ff023aacSStefan Behrens 				new_pool_size);
15580d2450abSSergei Trofimovich }
15590d2450abSSergei Trofimovich 
1560f42a34b2SMiao Xie static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info)
1561dc81cdc5SMiao Xie {
1562dc81cdc5SMiao Xie 	set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1563f42a34b2SMiao Xie }
1564dc81cdc5SMiao Xie 
1565f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1566f42a34b2SMiao Xie 				       unsigned long old_opts, int flags)
1567f42a34b2SMiao Xie {
1568dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1569dc81cdc5SMiao Xie 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
1570dc81cdc5SMiao Xie 	     (flags & MS_RDONLY))) {
1571dc81cdc5SMiao Xie 		/* wait for any defraggers to finish */
1572dc81cdc5SMiao Xie 		wait_event(fs_info->transaction_wait,
1573dc81cdc5SMiao Xie 			   (atomic_read(&fs_info->defrag_running) == 0));
1574dc81cdc5SMiao Xie 		if (flags & MS_RDONLY)
1575dc81cdc5SMiao Xie 			sync_filesystem(fs_info->sb);
1576dc81cdc5SMiao Xie 	}
1577dc81cdc5SMiao Xie }
1578dc81cdc5SMiao Xie 
1579dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1580dc81cdc5SMiao Xie 					 unsigned long old_opts)
1581dc81cdc5SMiao Xie {
1582dc81cdc5SMiao Xie 	/*
1583dc81cdc5SMiao Xie 	 * We need cleanup all defragable inodes if the autodefragment is
1584dc81cdc5SMiao Xie 	 * close or the fs is R/O.
1585dc81cdc5SMiao Xie 	 */
1586dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1587dc81cdc5SMiao Xie 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
1588dc81cdc5SMiao Xie 	     (fs_info->sb->s_flags & MS_RDONLY))) {
1589dc81cdc5SMiao Xie 		btrfs_cleanup_defrag_inodes(fs_info);
1590dc81cdc5SMiao Xie 	}
1591dc81cdc5SMiao Xie 
1592dc81cdc5SMiao Xie 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1593dc81cdc5SMiao Xie }
1594dc81cdc5SMiao Xie 
1595c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1596c146afadSYan Zheng {
1597815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1598815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
159949b25e05SJeff Mahoney 	unsigned old_flags = sb->s_flags;
160049b25e05SJeff Mahoney 	unsigned long old_opts = fs_info->mount_opt;
160149b25e05SJeff Mahoney 	unsigned long old_compress_type = fs_info->compress_type;
160249b25e05SJeff Mahoney 	u64 old_max_inline = fs_info->max_inline;
160349b25e05SJeff Mahoney 	u64 old_alloc_start = fs_info->alloc_start;
160449b25e05SJeff Mahoney 	int old_thread_pool_size = fs_info->thread_pool_size;
160549b25e05SJeff Mahoney 	unsigned int old_metadata_ratio = fs_info->metadata_ratio;
1606c146afadSYan Zheng 	int ret;
1607c146afadSYan Zheng 
160802b9984dSTheodore Ts'o 	sync_filesystem(sb);
1609f42a34b2SMiao Xie 	btrfs_remount_prepare(fs_info);
1610dc81cdc5SMiao Xie 
1611f667aef6SQu Wenruo 	if (data) {
1612f667aef6SQu Wenruo 		struct security_mnt_opts new_sec_opts;
1613f667aef6SQu Wenruo 
1614f667aef6SQu Wenruo 		security_init_mnt_opts(&new_sec_opts);
1615f667aef6SQu Wenruo 		ret = parse_security_options(data, &new_sec_opts);
1616f667aef6SQu Wenruo 		if (ret)
1617f667aef6SQu Wenruo 			goto restore;
1618f667aef6SQu Wenruo 		ret = setup_security_options(fs_info, sb,
1619f667aef6SQu Wenruo 					     &new_sec_opts);
1620f667aef6SQu Wenruo 		if (ret) {
1621f667aef6SQu Wenruo 			security_free_mnt_opts(&new_sec_opts);
1622f667aef6SQu Wenruo 			goto restore;
1623f667aef6SQu Wenruo 		}
1624f667aef6SQu Wenruo 	}
1625f667aef6SQu Wenruo 
1626b288052eSChris Mason 	ret = btrfs_parse_options(root, data);
162749b25e05SJeff Mahoney 	if (ret) {
162849b25e05SJeff Mahoney 		ret = -EINVAL;
162949b25e05SJeff Mahoney 		goto restore;
163049b25e05SJeff Mahoney 	}
1631b288052eSChris Mason 
1632f42a34b2SMiao Xie 	btrfs_remount_begin(fs_info, old_opts, *flags);
16330d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
16340d2450abSSergei Trofimovich 		fs_info->thread_pool_size, old_thread_pool_size);
16350d2450abSSergei Trofimovich 
1636c146afadSYan Zheng 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
1637dc81cdc5SMiao Xie 		goto out;
1638c146afadSYan Zheng 
1639c146afadSYan Zheng 	if (*flags & MS_RDONLY) {
16408dabb742SStefan Behrens 		/*
16418dabb742SStefan Behrens 		 * this also happens on 'umount -rf' or on shutdown, when
16428dabb742SStefan Behrens 		 * the filesystem is busy.
16438dabb742SStefan Behrens 		 */
164421c7e756SMiao Xie 		cancel_work_sync(&fs_info->async_reclaim_work);
1645361c093dSStefan Behrens 
1646361c093dSStefan Behrens 		/* wait for the uuid_scan task to finish */
1647361c093dSStefan Behrens 		down(&fs_info->uuid_tree_rescan_sem);
1648361c093dSStefan Behrens 		/* avoid complains from lockdep et al. */
1649361c093dSStefan Behrens 		up(&fs_info->uuid_tree_rescan_sem);
1650361c093dSStefan Behrens 
1651c146afadSYan Zheng 		sb->s_flags |= MS_RDONLY;
1652c146afadSYan Zheng 
1653e44163e1SJeff Mahoney 		/*
1654e44163e1SJeff Mahoney 		 * Setting MS_RDONLY will put the cleaner thread to
1655e44163e1SJeff Mahoney 		 * sleep at the next loop if it's already active.
1656e44163e1SJeff Mahoney 		 * If it's already asleep, we'll leave unused block
1657e44163e1SJeff Mahoney 		 * groups on disk until we're mounted read-write again
1658e44163e1SJeff Mahoney 		 * unless we clean them up here.
1659e44163e1SJeff Mahoney 		 */
1660e44163e1SJeff Mahoney 		mutex_lock(&root->fs_info->cleaner_mutex);
1661e44163e1SJeff Mahoney 		btrfs_delete_unused_bgs(fs_info);
1662e44163e1SJeff Mahoney 		mutex_unlock(&root->fs_info->cleaner_mutex);
1663e44163e1SJeff Mahoney 
16648dabb742SStefan Behrens 		btrfs_dev_replace_suspend_for_unmount(fs_info);
16658dabb742SStefan Behrens 		btrfs_scrub_cancel(fs_info);
1666061594efSMiao Xie 		btrfs_pause_balance(fs_info);
16678dabb742SStefan Behrens 
1668c146afadSYan Zheng 		ret = btrfs_commit_super(root);
166949b25e05SJeff Mahoney 		if (ret)
167049b25e05SJeff Mahoney 			goto restore;
1671c146afadSYan Zheng 	} else {
16726ef3de9cSDavid Sterba 		if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) {
16736ef3de9cSDavid Sterba 			btrfs_err(fs_info,
1674efe120a0SFrank Holton 				"Remounting read-write after error is not allowed");
16756ef3de9cSDavid Sterba 			ret = -EINVAL;
16766ef3de9cSDavid Sterba 			goto restore;
16776ef3de9cSDavid Sterba 		}
16788a3db184SSergei Trofimovich 		if (fs_info->fs_devices->rw_devices == 0) {
167949b25e05SJeff Mahoney 			ret = -EACCES;
168049b25e05SJeff Mahoney 			goto restore;
16818a3db184SSergei Trofimovich 		}
16822b82032cSYan Zheng 
1683292fd7fcSStefan Behrens 		if (fs_info->fs_devices->missing_devices >
1684292fd7fcSStefan Behrens 		     fs_info->num_tolerated_disk_barrier_failures &&
1685292fd7fcSStefan Behrens 		    !(*flags & MS_RDONLY)) {
1686efe120a0SFrank Holton 			btrfs_warn(fs_info,
1687efe120a0SFrank Holton 				"too many missing devices, writeable remount is not allowed");
1688292fd7fcSStefan Behrens 			ret = -EACCES;
1689292fd7fcSStefan Behrens 			goto restore;
1690292fd7fcSStefan Behrens 		}
1691292fd7fcSStefan Behrens 
16928a3db184SSergei Trofimovich 		if (btrfs_super_log_root(fs_info->super_copy) != 0) {
169349b25e05SJeff Mahoney 			ret = -EINVAL;
169449b25e05SJeff Mahoney 			goto restore;
16958a3db184SSergei Trofimovich 		}
1696c146afadSYan Zheng 
1697815745cfSAl Viro 		ret = btrfs_cleanup_fs_roots(fs_info);
169849b25e05SJeff Mahoney 		if (ret)
169949b25e05SJeff Mahoney 			goto restore;
1700c146afadSYan Zheng 
1701d68fc57bSYan, Zheng 		/* recover relocation */
17025f316481SWang Shilong 		mutex_lock(&fs_info->cleaner_mutex);
1703d68fc57bSYan, Zheng 		ret = btrfs_recover_relocation(root);
17045f316481SWang Shilong 		mutex_unlock(&fs_info->cleaner_mutex);
170549b25e05SJeff Mahoney 		if (ret)
170649b25e05SJeff Mahoney 			goto restore;
1707c146afadSYan Zheng 
17082b6ba629SIlya Dryomov 		ret = btrfs_resume_balance_async(fs_info);
17092b6ba629SIlya Dryomov 		if (ret)
17102b6ba629SIlya Dryomov 			goto restore;
17112b6ba629SIlya Dryomov 
17128dabb742SStefan Behrens 		ret = btrfs_resume_dev_replace_async(fs_info);
17138dabb742SStefan Behrens 		if (ret) {
1714efe120a0SFrank Holton 			btrfs_warn(fs_info, "failed to resume dev_replace");
17158dabb742SStefan Behrens 			goto restore;
17168dabb742SStefan Behrens 		}
171794aebfb2SJosef Bacik 
171894aebfb2SJosef Bacik 		if (!fs_info->uuid_root) {
1719efe120a0SFrank Holton 			btrfs_info(fs_info, "creating UUID tree");
172094aebfb2SJosef Bacik 			ret = btrfs_create_uuid_tree(fs_info);
172194aebfb2SJosef Bacik 			if (ret) {
1722efe120a0SFrank Holton 				btrfs_warn(fs_info, "failed to create the UUID tree %d", ret);
172394aebfb2SJosef Bacik 				goto restore;
172494aebfb2SJosef Bacik 			}
172594aebfb2SJosef Bacik 		}
1726c146afadSYan Zheng 		sb->s_flags &= ~MS_RDONLY;
1727c146afadSYan Zheng 	}
1728dc81cdc5SMiao Xie out:
17292c6a92b0SJustin Maggard 	wake_up_process(fs_info->transaction_kthread);
1730dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
1731c146afadSYan Zheng 	return 0;
173249b25e05SJeff Mahoney 
173349b25e05SJeff Mahoney restore:
173449b25e05SJeff Mahoney 	/* We've hit an error - don't reset MS_RDONLY */
173549b25e05SJeff Mahoney 	if (sb->s_flags & MS_RDONLY)
173649b25e05SJeff Mahoney 		old_flags |= MS_RDONLY;
173749b25e05SJeff Mahoney 	sb->s_flags = old_flags;
173849b25e05SJeff Mahoney 	fs_info->mount_opt = old_opts;
173949b25e05SJeff Mahoney 	fs_info->compress_type = old_compress_type;
174049b25e05SJeff Mahoney 	fs_info->max_inline = old_max_inline;
1741c018daecSMiao Xie 	mutex_lock(&fs_info->chunk_mutex);
174249b25e05SJeff Mahoney 	fs_info->alloc_start = old_alloc_start;
1743c018daecSMiao Xie 	mutex_unlock(&fs_info->chunk_mutex);
17440d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
17450d2450abSSergei Trofimovich 		old_thread_pool_size, fs_info->thread_pool_size);
174649b25e05SJeff Mahoney 	fs_info->metadata_ratio = old_metadata_ratio;
1747dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
174849b25e05SJeff Mahoney 	return ret;
1749c146afadSYan Zheng }
1750c146afadSYan Zheng 
1751bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */
1752bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1,
1753bcd53741SArne Jansen 				       const void *dev_info2)
1754bcd53741SArne Jansen {
1755bcd53741SArne Jansen 	if (((struct btrfs_device_info *)dev_info1)->max_avail >
1756bcd53741SArne Jansen 	    ((struct btrfs_device_info *)dev_info2)->max_avail)
1757bcd53741SArne Jansen 		return -1;
1758bcd53741SArne Jansen 	else if (((struct btrfs_device_info *)dev_info1)->max_avail <
1759bcd53741SArne Jansen 		 ((struct btrfs_device_info *)dev_info2)->max_avail)
1760bcd53741SArne Jansen 		return 1;
1761bcd53741SArne Jansen 	else
1762bcd53741SArne Jansen 	return 0;
1763bcd53741SArne Jansen }
1764bcd53741SArne Jansen 
1765bcd53741SArne Jansen /*
1766bcd53741SArne Jansen  * sort the devices by max_avail, in which max free extent size of each device
1767bcd53741SArne Jansen  * is stored.(Descending Sort)
1768bcd53741SArne Jansen  */
1769bcd53741SArne Jansen static inline void btrfs_descending_sort_devices(
1770bcd53741SArne Jansen 					struct btrfs_device_info *devices,
1771bcd53741SArne Jansen 					size_t nr_devices)
1772bcd53741SArne Jansen {
1773bcd53741SArne Jansen 	sort(devices, nr_devices, sizeof(struct btrfs_device_info),
1774bcd53741SArne Jansen 	     btrfs_cmp_device_free_bytes, NULL);
1775bcd53741SArne Jansen }
1776bcd53741SArne Jansen 
17776d07bcecSMiao Xie /*
17786d07bcecSMiao Xie  * The helper to calc the free space on the devices that can be used to store
17796d07bcecSMiao Xie  * file data.
17806d07bcecSMiao Xie  */
17816d07bcecSMiao Xie static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
17826d07bcecSMiao Xie {
17836d07bcecSMiao Xie 	struct btrfs_fs_info *fs_info = root->fs_info;
17846d07bcecSMiao Xie 	struct btrfs_device_info *devices_info;
17856d07bcecSMiao Xie 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
17866d07bcecSMiao Xie 	struct btrfs_device *device;
17876d07bcecSMiao Xie 	u64 skip_space;
17886d07bcecSMiao Xie 	u64 type;
17896d07bcecSMiao Xie 	u64 avail_space;
17906d07bcecSMiao Xie 	u64 used_space;
17916d07bcecSMiao Xie 	u64 min_stripe_size;
179239fb26c3SMiao Xie 	int min_stripes = 1, num_stripes = 1;
17936d07bcecSMiao Xie 	int i = 0, nr_devices;
17946d07bcecSMiao Xie 	int ret;
17956d07bcecSMiao Xie 
17967e33fd99SJosef Bacik 	/*
17977e33fd99SJosef Bacik 	 * We aren't under the device list lock, so this is racey-ish, but good
17987e33fd99SJosef Bacik 	 * enough for our purposes.
17997e33fd99SJosef Bacik 	 */
1800b772a86eSLi Zefan 	nr_devices = fs_info->fs_devices->open_devices;
18017e33fd99SJosef Bacik 	if (!nr_devices) {
18027e33fd99SJosef Bacik 		smp_mb();
18037e33fd99SJosef Bacik 		nr_devices = fs_info->fs_devices->open_devices;
18047e33fd99SJosef Bacik 		ASSERT(nr_devices);
18057e33fd99SJosef Bacik 		if (!nr_devices) {
18067e33fd99SJosef Bacik 			*free_bytes = 0;
18077e33fd99SJosef Bacik 			return 0;
18087e33fd99SJosef Bacik 		}
18097e33fd99SJosef Bacik 	}
18106d07bcecSMiao Xie 
1811d9b0d9baSDulshani Gunawardhana 	devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
18126d07bcecSMiao Xie 			       GFP_NOFS);
18136d07bcecSMiao Xie 	if (!devices_info)
18146d07bcecSMiao Xie 		return -ENOMEM;
18156d07bcecSMiao Xie 
18166d07bcecSMiao Xie 	/* calc min stripe number for data space alloction */
18176d07bcecSMiao Xie 	type = btrfs_get_alloc_profile(root, 1);
181839fb26c3SMiao Xie 	if (type & BTRFS_BLOCK_GROUP_RAID0) {
18196d07bcecSMiao Xie 		min_stripes = 2;
182039fb26c3SMiao Xie 		num_stripes = nr_devices;
182139fb26c3SMiao Xie 	} else if (type & BTRFS_BLOCK_GROUP_RAID1) {
18226d07bcecSMiao Xie 		min_stripes = 2;
182339fb26c3SMiao Xie 		num_stripes = 2;
182439fb26c3SMiao Xie 	} else if (type & BTRFS_BLOCK_GROUP_RAID10) {
18256d07bcecSMiao Xie 		min_stripes = 4;
182639fb26c3SMiao Xie 		num_stripes = 4;
182739fb26c3SMiao Xie 	}
18286d07bcecSMiao Xie 
18296d07bcecSMiao Xie 	if (type & BTRFS_BLOCK_GROUP_DUP)
18306d07bcecSMiao Xie 		min_stripe_size = 2 * BTRFS_STRIPE_LEN;
18316d07bcecSMiao Xie 	else
18326d07bcecSMiao Xie 		min_stripe_size = BTRFS_STRIPE_LEN;
18336d07bcecSMiao Xie 
18347e33fd99SJosef Bacik 	if (fs_info->alloc_start)
18357e33fd99SJosef Bacik 		mutex_lock(&fs_devices->device_list_mutex);
18367e33fd99SJosef Bacik 	rcu_read_lock();
18377e33fd99SJosef Bacik 	list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
183863a212abSStefan Behrens 		if (!device->in_fs_metadata || !device->bdev ||
183963a212abSStefan Behrens 		    device->is_tgtdev_for_dev_replace)
18406d07bcecSMiao Xie 			continue;
18416d07bcecSMiao Xie 
18427e33fd99SJosef Bacik 		if (i >= nr_devices)
18437e33fd99SJosef Bacik 			break;
18447e33fd99SJosef Bacik 
18456d07bcecSMiao Xie 		avail_space = device->total_bytes - device->bytes_used;
18466d07bcecSMiao Xie 
18476d07bcecSMiao Xie 		/* align with stripe_len */
1848f8c269d7SDavid Sterba 		avail_space = div_u64(avail_space, BTRFS_STRIPE_LEN);
18496d07bcecSMiao Xie 		avail_space *= BTRFS_STRIPE_LEN;
18506d07bcecSMiao Xie 
18516d07bcecSMiao Xie 		/*
18526d07bcecSMiao Xie 		 * In order to avoid overwritting the superblock on the drive,
18536d07bcecSMiao Xie 		 * btrfs starts at an offset of at least 1MB when doing chunk
18546d07bcecSMiao Xie 		 * allocation.
18556d07bcecSMiao Xie 		 */
18566d07bcecSMiao Xie 		skip_space = 1024 * 1024;
18576d07bcecSMiao Xie 
18586d07bcecSMiao Xie 		/* user can set the offset in fs_info->alloc_start. */
18597e33fd99SJosef Bacik 		if (fs_info->alloc_start &&
18607e33fd99SJosef Bacik 		    fs_info->alloc_start + BTRFS_STRIPE_LEN <=
18617e33fd99SJosef Bacik 		    device->total_bytes) {
18627e33fd99SJosef Bacik 			rcu_read_unlock();
18636d07bcecSMiao Xie 			skip_space = max(fs_info->alloc_start, skip_space);
18646d07bcecSMiao Xie 
18656d07bcecSMiao Xie 			/*
18667e33fd99SJosef Bacik 			 * btrfs can not use the free space in
18677e33fd99SJosef Bacik 			 * [0, skip_space - 1], we must subtract it from the
18687e33fd99SJosef Bacik 			 * total. In order to implement it, we account the used
18697e33fd99SJosef Bacik 			 * space in this range first.
18706d07bcecSMiao Xie 			 */
18717e33fd99SJosef Bacik 			ret = btrfs_account_dev_extents_size(device, 0,
18727e33fd99SJosef Bacik 							     skip_space - 1,
18736d07bcecSMiao Xie 							     &used_space);
18746d07bcecSMiao Xie 			if (ret) {
18756d07bcecSMiao Xie 				kfree(devices_info);
18767e33fd99SJosef Bacik 				mutex_unlock(&fs_devices->device_list_mutex);
18776d07bcecSMiao Xie 				return ret;
18786d07bcecSMiao Xie 			}
18796d07bcecSMiao Xie 
18807e33fd99SJosef Bacik 			rcu_read_lock();
18817e33fd99SJosef Bacik 
18826d07bcecSMiao Xie 			/* calc the free space in [0, skip_space - 1] */
18836d07bcecSMiao Xie 			skip_space -= used_space;
18847e33fd99SJosef Bacik 		}
18856d07bcecSMiao Xie 
18866d07bcecSMiao Xie 		/*
18876d07bcecSMiao Xie 		 * we can use the free space in [0, skip_space - 1], subtract
18886d07bcecSMiao Xie 		 * it from the total.
18896d07bcecSMiao Xie 		 */
18906d07bcecSMiao Xie 		if (avail_space && avail_space >= skip_space)
18916d07bcecSMiao Xie 			avail_space -= skip_space;
18926d07bcecSMiao Xie 		else
18936d07bcecSMiao Xie 			avail_space = 0;
18946d07bcecSMiao Xie 
18956d07bcecSMiao Xie 		if (avail_space < min_stripe_size)
18966d07bcecSMiao Xie 			continue;
18976d07bcecSMiao Xie 
18986d07bcecSMiao Xie 		devices_info[i].dev = device;
18996d07bcecSMiao Xie 		devices_info[i].max_avail = avail_space;
19006d07bcecSMiao Xie 
19016d07bcecSMiao Xie 		i++;
19026d07bcecSMiao Xie 	}
19037e33fd99SJosef Bacik 	rcu_read_unlock();
19047e33fd99SJosef Bacik 	if (fs_info->alloc_start)
19057e33fd99SJosef Bacik 		mutex_unlock(&fs_devices->device_list_mutex);
19066d07bcecSMiao Xie 
19076d07bcecSMiao Xie 	nr_devices = i;
19086d07bcecSMiao Xie 
19096d07bcecSMiao Xie 	btrfs_descending_sort_devices(devices_info, nr_devices);
19106d07bcecSMiao Xie 
19116d07bcecSMiao Xie 	i = nr_devices - 1;
19126d07bcecSMiao Xie 	avail_space = 0;
19136d07bcecSMiao Xie 	while (nr_devices >= min_stripes) {
191439fb26c3SMiao Xie 		if (num_stripes > nr_devices)
191539fb26c3SMiao Xie 			num_stripes = nr_devices;
191639fb26c3SMiao Xie 
19176d07bcecSMiao Xie 		if (devices_info[i].max_avail >= min_stripe_size) {
19186d07bcecSMiao Xie 			int j;
19196d07bcecSMiao Xie 			u64 alloc_size;
19206d07bcecSMiao Xie 
192139fb26c3SMiao Xie 			avail_space += devices_info[i].max_avail * num_stripes;
19226d07bcecSMiao Xie 			alloc_size = devices_info[i].max_avail;
192339fb26c3SMiao Xie 			for (j = i + 1 - num_stripes; j <= i; j++)
19246d07bcecSMiao Xie 				devices_info[j].max_avail -= alloc_size;
19256d07bcecSMiao Xie 		}
19266d07bcecSMiao Xie 		i--;
19276d07bcecSMiao Xie 		nr_devices--;
19286d07bcecSMiao Xie 	}
19296d07bcecSMiao Xie 
19306d07bcecSMiao Xie 	kfree(devices_info);
19316d07bcecSMiao Xie 	*free_bytes = avail_space;
19326d07bcecSMiao Xie 	return 0;
19336d07bcecSMiao Xie }
19346d07bcecSMiao Xie 
1935ba7b6e62SDavid Sterba /*
1936ba7b6e62SDavid Sterba  * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
1937ba7b6e62SDavid Sterba  *
1938ba7b6e62SDavid Sterba  * If there's a redundant raid level at DATA block groups, use the respective
1939ba7b6e62SDavid Sterba  * multiplier to scale the sizes.
1940ba7b6e62SDavid Sterba  *
1941ba7b6e62SDavid Sterba  * Unused device space usage is based on simulating the chunk allocator
1942ba7b6e62SDavid Sterba  * algorithm that respects the device sizes, order of allocations and the
1943ba7b6e62SDavid Sterba  * 'alloc_start' value, this is a close approximation of the actual use but
1944ba7b6e62SDavid Sterba  * there are other factors that may change the result (like a new metadata
1945ba7b6e62SDavid Sterba  * chunk).
1946ba7b6e62SDavid Sterba  *
1947ba7b6e62SDavid Sterba  * FIXME: not accurate for mixed block groups, total and free/used are ok,
1948ba7b6e62SDavid Sterba  * available appears slightly larger.
1949ba7b6e62SDavid Sterba  */
19508fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
19518fd17795SChris Mason {
1952815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
1953815745cfSAl Viro 	struct btrfs_super_block *disk_super = fs_info->super_copy;
1954815745cfSAl Viro 	struct list_head *head = &fs_info->space_info;
1955bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
1956bd4d1088SJosef Bacik 	u64 total_used = 0;
19576d07bcecSMiao Xie 	u64 total_free_data = 0;
1958db94535dSChris Mason 	int bits = dentry->d_sb->s_blocksize_bits;
1959815745cfSAl Viro 	__be32 *fsid = (__be32 *)fs_info->fsid;
1960ba7b6e62SDavid Sterba 	unsigned factor = 1;
1961ba7b6e62SDavid Sterba 	struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
19626d07bcecSMiao Xie 	int ret;
19638fd17795SChris Mason 
196415484377SMiao Xie 	/*
196515484377SMiao Xie 	 * holding chunk_muext to avoid allocating new chunks, holding
196615484377SMiao Xie 	 * device_list_mutex to avoid the device being removed
196715484377SMiao Xie 	 */
1968bd4d1088SJosef Bacik 	rcu_read_lock();
196989a55897SJosef Bacik 	list_for_each_entry_rcu(found, head, list) {
19706d07bcecSMiao Xie 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
1971ba7b6e62SDavid Sterba 			int i;
1972ba7b6e62SDavid Sterba 
19736d07bcecSMiao Xie 			total_free_data += found->disk_total - found->disk_used;
19746d07bcecSMiao Xie 			total_free_data -=
19756d07bcecSMiao Xie 				btrfs_account_ro_block_groups_free_space(found);
1976ba7b6e62SDavid Sterba 
1977ba7b6e62SDavid Sterba 			for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1978ba7b6e62SDavid Sterba 				if (!list_empty(&found->block_groups[i])) {
1979ba7b6e62SDavid Sterba 					switch (i) {
1980ba7b6e62SDavid Sterba 					case BTRFS_RAID_DUP:
1981ba7b6e62SDavid Sterba 					case BTRFS_RAID_RAID1:
1982ba7b6e62SDavid Sterba 					case BTRFS_RAID_RAID10:
1983ba7b6e62SDavid Sterba 						factor = 2;
1984ba7b6e62SDavid Sterba 					}
1985ba7b6e62SDavid Sterba 				}
1986ba7b6e62SDavid Sterba 			}
19876d07bcecSMiao Xie 		}
19886d07bcecSMiao Xie 
1989b742bb82SYan, Zheng 		total_used += found->disk_used;
199089a55897SJosef Bacik 	}
1991ba7b6e62SDavid Sterba 
1992bd4d1088SJosef Bacik 	rcu_read_unlock();
1993bd4d1088SJosef Bacik 
1994ba7b6e62SDavid Sterba 	buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
1995ba7b6e62SDavid Sterba 	buf->f_blocks >>= bits;
1996ba7b6e62SDavid Sterba 	buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
1997ba7b6e62SDavid Sterba 
1998ba7b6e62SDavid Sterba 	/* Account global block reserve as used, it's in logical size already */
1999ba7b6e62SDavid Sterba 	spin_lock(&block_rsv->lock);
2000ba7b6e62SDavid Sterba 	buf->f_bfree -= block_rsv->size >> bits;
2001ba7b6e62SDavid Sterba 	spin_unlock(&block_rsv->lock);
2002ba7b6e62SDavid Sterba 
20030d95c1beSDavid Sterba 	buf->f_bavail = div_u64(total_free_data, factor);
2004815745cfSAl Viro 	ret = btrfs_calc_avail_data_space(fs_info->tree_root, &total_free_data);
20057e33fd99SJosef Bacik 	if (ret)
20066d07bcecSMiao Xie 		return ret;
2007ba7b6e62SDavid Sterba 	buf->f_bavail += div_u64(total_free_data, factor);
20086d07bcecSMiao Xie 	buf->f_bavail = buf->f_bavail >> bits;
2009d397712bSChris Mason 
2010ba7b6e62SDavid Sterba 	buf->f_type = BTRFS_SUPER_MAGIC;
2011ba7b6e62SDavid Sterba 	buf->f_bsize = dentry->d_sb->s_blocksize;
2012ba7b6e62SDavid Sterba 	buf->f_namelen = BTRFS_NAME_LEN;
2013ba7b6e62SDavid Sterba 
20149d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
20159d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
20169d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
20179d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
20189d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
201932d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
20202b0143b5SDavid Howells 	buf->f_fsid.val[0] ^= BTRFS_I(d_inode(dentry))->root->objectid >> 32;
20212b0143b5SDavid Howells 	buf->f_fsid.val[1] ^= BTRFS_I(d_inode(dentry))->root->objectid;
202232d48fa1SDavid Woodhouse 
20238fd17795SChris Mason 	return 0;
20248fd17795SChris Mason }
2025b5133862SChris Mason 
2026aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb)
2027aea52e19SAl Viro {
2028815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2029aea52e19SAl Viro 	kill_anon_super(sb);
2030aea52e19SAl Viro 	free_fs_info(fs_info);
2031aea52e19SAl Viro }
2032aea52e19SAl Viro 
20332e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
20342e635a27SChris Mason 	.owner		= THIS_MODULE,
20352e635a27SChris Mason 	.name		= "btrfs",
2036061dbc6bSAl Viro 	.mount		= btrfs_mount,
2037aea52e19SAl Viro 	.kill_sb	= btrfs_kill_super,
2038f667aef6SQu Wenruo 	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
20392e635a27SChris Mason };
20407f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs");
2041a9218f6bSChris Mason 
2042d8620958STom Van Braeckel static int btrfs_control_open(struct inode *inode, struct file *file)
2043d8620958STom Van Braeckel {
2044d8620958STom Van Braeckel 	/*
2045d8620958STom Van Braeckel 	 * The control file's private_data is used to hold the
2046d8620958STom Van Braeckel 	 * transaction when it is started and is used to keep
2047d8620958STom Van Braeckel 	 * track of whether a transaction is already in progress.
2048d8620958STom Van Braeckel 	 */
2049d8620958STom Van Braeckel 	file->private_data = NULL;
2050d8620958STom Van Braeckel 	return 0;
2051d8620958STom Van Braeckel }
2052d8620958STom Van Braeckel 
2053d352ac68SChris Mason /*
2054d352ac68SChris Mason  * used by btrfsctl to scan devices when no FS is mounted
2055d352ac68SChris Mason  */
20568a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
20578a4b83ccSChris Mason 				unsigned long arg)
20588a4b83ccSChris Mason {
20598a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
20608a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices;
2061c071fcfdSChris Mason 	int ret = -ENOTTY;
20628a4b83ccSChris Mason 
2063e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2064e441d54dSChris Mason 		return -EPERM;
2065e441d54dSChris Mason 
2066dae7b665SLi Zefan 	vol = memdup_user((void __user *)arg, sizeof(*vol));
2067dae7b665SLi Zefan 	if (IS_ERR(vol))
2068dae7b665SLi Zefan 		return PTR_ERR(vol);
2069c071fcfdSChris Mason 
20708a4b83ccSChris Mason 	switch (cmd) {
20718a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
207297288f2cSChristoph Hellwig 		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
20738a4b83ccSChris Mason 					    &btrfs_fs_type, &fs_devices);
20748a4b83ccSChris Mason 		break;
207502db0844SJosef Bacik 	case BTRFS_IOC_DEVICES_READY:
207602db0844SJosef Bacik 		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
207702db0844SJosef Bacik 					    &btrfs_fs_type, &fs_devices);
207802db0844SJosef Bacik 		if (ret)
207902db0844SJosef Bacik 			break;
208002db0844SJosef Bacik 		ret = !(fs_devices->num_devices == fs_devices->total_devices);
208102db0844SJosef Bacik 		break;
20828a4b83ccSChris Mason 	}
2083dae7b665SLi Zefan 
20848a4b83ccSChris Mason 	kfree(vol);
2085f819d837SLinda Knippers 	return ret;
20868a4b83ccSChris Mason }
20878a4b83ccSChris Mason 
20880176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
2089ed0dab6bSYan {
2090354aa0fbSMiao Xie 	struct btrfs_trans_handle *trans;
2091354aa0fbSMiao Xie 	struct btrfs_root *root = btrfs_sb(sb)->tree_root;
2092354aa0fbSMiao Xie 
2093d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
2094354aa0fbSMiao Xie 	if (IS_ERR(trans)) {
2095354aa0fbSMiao Xie 		/* no transaction, don't bother */
2096354aa0fbSMiao Xie 		if (PTR_ERR(trans) == -ENOENT)
20970176260fSLinus Torvalds 			return 0;
2098354aa0fbSMiao Xie 		return PTR_ERR(trans);
2099354aa0fbSMiao Xie 	}
2100354aa0fbSMiao Xie 	return btrfs_commit_transaction(trans, root);
2101ed0dab6bSYan }
2102ed0dab6bSYan 
21039c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
21049c5085c1SJosef Bacik {
21059c5085c1SJosef Bacik 	struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
21069c5085c1SJosef Bacik 	struct btrfs_fs_devices *cur_devices;
21079c5085c1SJosef Bacik 	struct btrfs_device *dev, *first_dev = NULL;
21089c5085c1SJosef Bacik 	struct list_head *head;
21099c5085c1SJosef Bacik 	struct rcu_string *name;
21109c5085c1SJosef Bacik 
21119c5085c1SJosef Bacik 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
21129c5085c1SJosef Bacik 	cur_devices = fs_info->fs_devices;
21139c5085c1SJosef Bacik 	while (cur_devices) {
21149c5085c1SJosef Bacik 		head = &cur_devices->devices;
21159c5085c1SJosef Bacik 		list_for_each_entry(dev, head, dev_list) {
2116aa9ddcd4SJosef Bacik 			if (dev->missing)
2117aa9ddcd4SJosef Bacik 				continue;
21180aeb8a6eSAnand Jain 			if (!dev->name)
21190aeb8a6eSAnand Jain 				continue;
21209c5085c1SJosef Bacik 			if (!first_dev || dev->devid < first_dev->devid)
21219c5085c1SJosef Bacik 				first_dev = dev;
21229c5085c1SJosef Bacik 		}
21239c5085c1SJosef Bacik 		cur_devices = cur_devices->seed;
21249c5085c1SJosef Bacik 	}
21259c5085c1SJosef Bacik 
21269c5085c1SJosef Bacik 	if (first_dev) {
21279c5085c1SJosef Bacik 		rcu_read_lock();
21289c5085c1SJosef Bacik 		name = rcu_dereference(first_dev->name);
21299c5085c1SJosef Bacik 		seq_escape(m, name->str, " \t\n\\");
21309c5085c1SJosef Bacik 		rcu_read_unlock();
21319c5085c1SJosef Bacik 	} else {
21329c5085c1SJosef Bacik 		WARN_ON(1);
21339c5085c1SJosef Bacik 	}
21349c5085c1SJosef Bacik 	mutex_unlock(&fs_info->fs_devices->device_list_mutex);
21359c5085c1SJosef Bacik 	return 0;
21369c5085c1SJosef Bacik }
21379c5085c1SJosef Bacik 
2138b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
213976dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
2140bd555975SAl Viro 	.evict_inode	= btrfs_evict_inode,
2141e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
2142d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
2143a9572a15SEric Paris 	.show_options	= btrfs_show_options,
21449c5085c1SJosef Bacik 	.show_devname	= btrfs_show_devname,
21454730a4bcSChris Mason 	.write_inode	= btrfs_write_inode,
21462c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
21472c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
21488fd17795SChris Mason 	.statfs		= btrfs_statfs,
2149c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
21500176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
2151e20d96d6SChris Mason };
2152a9218f6bSChris Mason 
2153a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
2154d8620958STom Van Braeckel 	.open = btrfs_control_open,
2155a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
2156a9218f6bSChris Mason 	.compat_ioctl = btrfs_control_ioctl,
2157a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
21586038f373SArnd Bergmann 	.llseek = noop_llseek,
2159a9218f6bSChris Mason };
2160a9218f6bSChris Mason 
2161a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
2162578454ffSKay Sievers 	.minor		= BTRFS_MINOR,
2163a9218f6bSChris Mason 	.name		= "btrfs-control",
2164a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
2165a9218f6bSChris Mason };
2166a9218f6bSChris Mason 
2167578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
2168578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control");
2169578454ffSKay Sievers 
2170a9218f6bSChris Mason static int btrfs_interface_init(void)
2171a9218f6bSChris Mason {
2172a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
2173a9218f6bSChris Mason }
2174a9218f6bSChris Mason 
2175b2950863SChristoph Hellwig static void btrfs_interface_exit(void)
2176a9218f6bSChris Mason {
2177a9218f6bSChris Mason 	if (misc_deregister(&btrfs_misc) < 0)
2178efe120a0SFrank Holton 		printk(KERN_INFO "BTRFS: misc_deregister failed for control device\n");
2179a9218f6bSChris Mason }
2180a9218f6bSChris Mason 
218185965600SDavid Sterba static void btrfs_print_info(void)
218285965600SDavid Sterba {
218385965600SDavid Sterba 	printk(KERN_INFO "Btrfs loaded"
218485965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG
218585965600SDavid Sterba 			", debug=on"
218685965600SDavid Sterba #endif
218779556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT
218879556c3dSStefan Behrens 			", assert=on"
218979556c3dSStefan Behrens #endif
219085965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
219185965600SDavid Sterba 			", integrity-checker=on"
219285965600SDavid Sterba #endif
219385965600SDavid Sterba 			"\n");
219485965600SDavid Sterba }
219585965600SDavid Sterba 
2196dc11dd5dSJosef Bacik static int btrfs_run_sanity_tests(void)
2197dc11dd5dSJosef Bacik {
219806ea65a3SJosef Bacik 	int ret;
219906ea65a3SJosef Bacik 
2200294e30feSJosef Bacik 	ret = btrfs_init_test_fs();
220106ea65a3SJosef Bacik 	if (ret)
220206ea65a3SJosef Bacik 		return ret;
2203294e30feSJosef Bacik 
2204294e30feSJosef Bacik 	ret = btrfs_test_free_space_cache();
2205294e30feSJosef Bacik 	if (ret)
2206294e30feSJosef Bacik 		goto out;
2207294e30feSJosef Bacik 	ret = btrfs_test_extent_buffer_operations();
2208294e30feSJosef Bacik 	if (ret)
2209294e30feSJosef Bacik 		goto out;
2210294e30feSJosef Bacik 	ret = btrfs_test_extent_io();
2211aaedb55bSJosef Bacik 	if (ret)
2212aaedb55bSJosef Bacik 		goto out;
2213aaedb55bSJosef Bacik 	ret = btrfs_test_inodes();
2214faa2dbf0SJosef Bacik 	if (ret)
2215faa2dbf0SJosef Bacik 		goto out;
2216faa2dbf0SJosef Bacik 	ret = btrfs_test_qgroups();
2217294e30feSJosef Bacik out:
2218294e30feSJosef Bacik 	btrfs_destroy_test_fs();
2219294e30feSJosef Bacik 	return ret;
2220dc11dd5dSJosef Bacik }
2221dc11dd5dSJosef Bacik 
22222e635a27SChris Mason static int __init init_btrfs_fs(void)
22232e635a27SChris Mason {
22242c90e5d6SChris Mason 	int err;
222558176a96SJosef Bacik 
222614a958e6SFilipe David Borba Manana 	err = btrfs_hash_init();
222714a958e6SFilipe David Borba Manana 	if (err)
222814a958e6SFilipe David Borba Manana 		return err;
222914a958e6SFilipe David Borba Manana 
223063541927SFilipe David Borba Manana 	btrfs_props_init();
223163541927SFilipe David Borba Manana 
223258176a96SJosef Bacik 	err = btrfs_init_sysfs();
223358176a96SJosef Bacik 	if (err)
223414a958e6SFilipe David Borba Manana 		goto free_hash;
223558176a96SJosef Bacik 
2236143bede5SJeff Mahoney 	btrfs_init_compress();
2237d1310b2eSChris Mason 
2238261507a0SLi Zefan 	err = btrfs_init_cachep();
2239261507a0SLi Zefan 	if (err)
2240261507a0SLi Zefan 		goto free_compress;
2241261507a0SLi Zefan 
2242d1310b2eSChris Mason 	err = extent_io_init();
22432f4cbe64SWyatt Banks 	if (err)
22442f4cbe64SWyatt Banks 		goto free_cachep;
22452f4cbe64SWyatt Banks 
2246d1310b2eSChris Mason 	err = extent_map_init();
2247d1310b2eSChris Mason 	if (err)
2248d1310b2eSChris Mason 		goto free_extent_io;
2249d1310b2eSChris Mason 
22506352b91dSMiao Xie 	err = ordered_data_init();
22512f4cbe64SWyatt Banks 	if (err)
22522f4cbe64SWyatt Banks 		goto free_extent_map;
2253c8b97818SChris Mason 
22546352b91dSMiao Xie 	err = btrfs_delayed_inode_init();
22556352b91dSMiao Xie 	if (err)
22566352b91dSMiao Xie 		goto free_ordered_data;
22576352b91dSMiao Xie 
22589247f317SMiao Xie 	err = btrfs_auto_defrag_init();
225916cdcec7SMiao Xie 	if (err)
226016cdcec7SMiao Xie 		goto free_delayed_inode;
226116cdcec7SMiao Xie 
226278a6184aSMiao Xie 	err = btrfs_delayed_ref_init();
22639247f317SMiao Xie 	if (err)
22649247f317SMiao Xie 		goto free_auto_defrag;
22659247f317SMiao Xie 
2266b9e9a6cbSWang Shilong 	err = btrfs_prelim_ref_init();
2267b9e9a6cbSWang Shilong 	if (err)
2268af13b492SDavid Sterba 		goto free_delayed_ref;
2269b9e9a6cbSWang Shilong 
227097eb6b69SDavid Sterba 	err = btrfs_end_io_wq_init();
227178a6184aSMiao Xie 	if (err)
2272af13b492SDavid Sterba 		goto free_prelim_ref;
227378a6184aSMiao Xie 
227497eb6b69SDavid Sterba 	err = btrfs_interface_init();
227597eb6b69SDavid Sterba 	if (err)
227697eb6b69SDavid Sterba 		goto free_end_io_wq;
227797eb6b69SDavid Sterba 
2278e565d4b9SJan Schmidt 	btrfs_init_lockdep();
2279e565d4b9SJan Schmidt 
228085965600SDavid Sterba 	btrfs_print_info();
2281dc11dd5dSJosef Bacik 
2282dc11dd5dSJosef Bacik 	err = btrfs_run_sanity_tests();
2283dc11dd5dSJosef Bacik 	if (err)
2284dc11dd5dSJosef Bacik 		goto unregister_ioctl;
2285dc11dd5dSJosef Bacik 
2286dc11dd5dSJosef Bacik 	err = register_filesystem(&btrfs_fs_type);
2287dc11dd5dSJosef Bacik 	if (err)
2288dc11dd5dSJosef Bacik 		goto unregister_ioctl;
228974255aa0SJosef Bacik 
22902f4cbe64SWyatt Banks 	return 0;
22912f4cbe64SWyatt Banks 
2292a9218f6bSChris Mason unregister_ioctl:
2293a9218f6bSChris Mason 	btrfs_interface_exit();
229497eb6b69SDavid Sterba free_end_io_wq:
229597eb6b69SDavid Sterba 	btrfs_end_io_wq_exit();
2296b9e9a6cbSWang Shilong free_prelim_ref:
2297b9e9a6cbSWang Shilong 	btrfs_prelim_ref_exit();
229878a6184aSMiao Xie free_delayed_ref:
229978a6184aSMiao Xie 	btrfs_delayed_ref_exit();
23009247f317SMiao Xie free_auto_defrag:
23019247f317SMiao Xie 	btrfs_auto_defrag_exit();
230216cdcec7SMiao Xie free_delayed_inode:
230316cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
23046352b91dSMiao Xie free_ordered_data:
23056352b91dSMiao Xie 	ordered_data_exit();
23062f4cbe64SWyatt Banks free_extent_map:
23072f4cbe64SWyatt Banks 	extent_map_exit();
2308d1310b2eSChris Mason free_extent_io:
2309d1310b2eSChris Mason 	extent_io_exit();
23102f4cbe64SWyatt Banks free_cachep:
23112f4cbe64SWyatt Banks 	btrfs_destroy_cachep();
2312261507a0SLi Zefan free_compress:
2313261507a0SLi Zefan 	btrfs_exit_compress();
23142f4cbe64SWyatt Banks 	btrfs_exit_sysfs();
231514a958e6SFilipe David Borba Manana free_hash:
231614a958e6SFilipe David Borba Manana 	btrfs_hash_exit();
23172c90e5d6SChris Mason 	return err;
23182e635a27SChris Mason }
23192e635a27SChris Mason 
23202e635a27SChris Mason static void __exit exit_btrfs_fs(void)
23212e635a27SChris Mason {
232239279cc3SChris Mason 	btrfs_destroy_cachep();
232378a6184aSMiao Xie 	btrfs_delayed_ref_exit();
23249247f317SMiao Xie 	btrfs_auto_defrag_exit();
232516cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
2326b9e9a6cbSWang Shilong 	btrfs_prelim_ref_exit();
23276352b91dSMiao Xie 	ordered_data_exit();
2328a52d9a80SChris Mason 	extent_map_exit();
2329d1310b2eSChris Mason 	extent_io_exit();
2330a9218f6bSChris Mason 	btrfs_interface_exit();
23315ed5f588SJosef Bacik 	btrfs_end_io_wq_exit();
23322e635a27SChris Mason 	unregister_filesystem(&btrfs_fs_type);
233358176a96SJosef Bacik 	btrfs_exit_sysfs();
23348a4b83ccSChris Mason 	btrfs_cleanup_fs_uuids();
2335261507a0SLi Zefan 	btrfs_exit_compress();
233614a958e6SFilipe David Borba Manana 	btrfs_hash_exit();
23372e635a27SChris Mason }
23382e635a27SChris Mason 
233960efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs);
23402e635a27SChris Mason module_exit(exit_btrfs_fs)
23412e635a27SChris Mason 
23422e635a27SChris Mason MODULE_LICENSE("GPL");
2343