xref: /openbmc/linux/fs/btrfs/super.c (revision 05dbe683)
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 
7208748810SDavid Sterba static 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 
839*05dbe683SOmar 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;
843*05dbe683SOmar Sandoval 	struct btrfs_root *fs_root;
844*05dbe683SOmar Sandoval 	struct btrfs_root_ref *root_ref;
845*05dbe683SOmar Sandoval 	struct btrfs_inode_ref *inode_ref;
846*05dbe683SOmar Sandoval 	struct btrfs_key key;
847*05dbe683SOmar Sandoval 	struct btrfs_path *path = NULL;
848*05dbe683SOmar Sandoval 	char *name = NULL, *ptr;
849*05dbe683SOmar Sandoval 	u64 dirid;
850*05dbe683SOmar Sandoval 	int len;
851*05dbe683SOmar Sandoval 	int ret;
852*05dbe683SOmar Sandoval 
853*05dbe683SOmar Sandoval 	path = btrfs_alloc_path();
854*05dbe683SOmar Sandoval 	if (!path) {
855*05dbe683SOmar Sandoval 		ret = -ENOMEM;
856*05dbe683SOmar Sandoval 		goto err;
857*05dbe683SOmar Sandoval 	}
858*05dbe683SOmar Sandoval 	path->leave_spinning = 1;
859*05dbe683SOmar Sandoval 
860*05dbe683SOmar Sandoval 	name = kmalloc(PATH_MAX, GFP_NOFS);
861*05dbe683SOmar Sandoval 	if (!name) {
862*05dbe683SOmar Sandoval 		ret = -ENOMEM;
863*05dbe683SOmar Sandoval 		goto err;
864*05dbe683SOmar Sandoval 	}
865*05dbe683SOmar Sandoval 	ptr = name + PATH_MAX - 1;
866*05dbe683SOmar Sandoval 	ptr[0] = '\0';
867*05dbe683SOmar Sandoval 
868*05dbe683SOmar Sandoval 	/*
869*05dbe683SOmar Sandoval 	 * Walk up the subvolume trees in the tree of tree roots by root
870*05dbe683SOmar Sandoval 	 * backrefs until we hit the top-level subvolume.
871*05dbe683SOmar Sandoval 	 */
872*05dbe683SOmar Sandoval 	while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
873*05dbe683SOmar Sandoval 		key.objectid = subvol_objectid;
874*05dbe683SOmar Sandoval 		key.type = BTRFS_ROOT_BACKREF_KEY;
875*05dbe683SOmar Sandoval 		key.offset = (u64)-1;
876*05dbe683SOmar Sandoval 
877*05dbe683SOmar Sandoval 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
878*05dbe683SOmar Sandoval 		if (ret < 0) {
879*05dbe683SOmar Sandoval 			goto err;
880*05dbe683SOmar Sandoval 		} else if (ret > 0) {
881*05dbe683SOmar Sandoval 			ret = btrfs_previous_item(root, path, subvol_objectid,
882*05dbe683SOmar Sandoval 						  BTRFS_ROOT_BACKREF_KEY);
883*05dbe683SOmar Sandoval 			if (ret < 0) {
884*05dbe683SOmar Sandoval 				goto err;
885*05dbe683SOmar Sandoval 			} else if (ret > 0) {
886*05dbe683SOmar Sandoval 				ret = -ENOENT;
887*05dbe683SOmar Sandoval 				goto err;
888*05dbe683SOmar Sandoval 			}
889*05dbe683SOmar Sandoval 		}
890*05dbe683SOmar Sandoval 
891*05dbe683SOmar Sandoval 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
892*05dbe683SOmar Sandoval 		subvol_objectid = key.offset;
893*05dbe683SOmar Sandoval 
894*05dbe683SOmar Sandoval 		root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
895*05dbe683SOmar Sandoval 					  struct btrfs_root_ref);
896*05dbe683SOmar Sandoval 		len = btrfs_root_ref_name_len(path->nodes[0], root_ref);
897*05dbe683SOmar Sandoval 		ptr -= len + 1;
898*05dbe683SOmar Sandoval 		if (ptr < name) {
899*05dbe683SOmar Sandoval 			ret = -ENAMETOOLONG;
900*05dbe683SOmar Sandoval 			goto err;
901*05dbe683SOmar Sandoval 		}
902*05dbe683SOmar Sandoval 		read_extent_buffer(path->nodes[0], ptr + 1,
903*05dbe683SOmar Sandoval 				   (unsigned long)(root_ref + 1), len);
904*05dbe683SOmar Sandoval 		ptr[0] = '/';
905*05dbe683SOmar Sandoval 		dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);
906*05dbe683SOmar Sandoval 		btrfs_release_path(path);
907*05dbe683SOmar Sandoval 
908*05dbe683SOmar Sandoval 		key.objectid = subvol_objectid;
909*05dbe683SOmar Sandoval 		key.type = BTRFS_ROOT_ITEM_KEY;
910*05dbe683SOmar Sandoval 		key.offset = (u64)-1;
911*05dbe683SOmar Sandoval 		fs_root = btrfs_read_fs_root_no_name(fs_info, &key);
912*05dbe683SOmar Sandoval 		if (IS_ERR(fs_root)) {
913*05dbe683SOmar Sandoval 			ret = PTR_ERR(fs_root);
914*05dbe683SOmar Sandoval 			goto err;
915*05dbe683SOmar Sandoval 		}
916*05dbe683SOmar Sandoval 
917*05dbe683SOmar Sandoval 		/*
918*05dbe683SOmar Sandoval 		 * Walk up the filesystem tree by inode refs until we hit the
919*05dbe683SOmar Sandoval 		 * root directory.
920*05dbe683SOmar Sandoval 		 */
921*05dbe683SOmar Sandoval 		while (dirid != BTRFS_FIRST_FREE_OBJECTID) {
922*05dbe683SOmar Sandoval 			key.objectid = dirid;
923*05dbe683SOmar Sandoval 			key.type = BTRFS_INODE_REF_KEY;
924*05dbe683SOmar Sandoval 			key.offset = (u64)-1;
925*05dbe683SOmar Sandoval 
926*05dbe683SOmar Sandoval 			ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
927*05dbe683SOmar Sandoval 			if (ret < 0) {
928*05dbe683SOmar Sandoval 				goto err;
929*05dbe683SOmar Sandoval 			} else if (ret > 0) {
930*05dbe683SOmar Sandoval 				ret = btrfs_previous_item(fs_root, path, dirid,
931*05dbe683SOmar Sandoval 							  BTRFS_INODE_REF_KEY);
932*05dbe683SOmar Sandoval 				if (ret < 0) {
933*05dbe683SOmar Sandoval 					goto err;
934*05dbe683SOmar Sandoval 				} else if (ret > 0) {
935*05dbe683SOmar Sandoval 					ret = -ENOENT;
936*05dbe683SOmar Sandoval 					goto err;
937*05dbe683SOmar Sandoval 				}
938*05dbe683SOmar Sandoval 			}
939*05dbe683SOmar Sandoval 
940*05dbe683SOmar Sandoval 			btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
941*05dbe683SOmar Sandoval 			dirid = key.offset;
942*05dbe683SOmar Sandoval 
943*05dbe683SOmar Sandoval 			inode_ref = btrfs_item_ptr(path->nodes[0],
944*05dbe683SOmar Sandoval 						   path->slots[0],
945*05dbe683SOmar Sandoval 						   struct btrfs_inode_ref);
946*05dbe683SOmar Sandoval 			len = btrfs_inode_ref_name_len(path->nodes[0],
947*05dbe683SOmar Sandoval 						       inode_ref);
948*05dbe683SOmar Sandoval 			ptr -= len + 1;
949*05dbe683SOmar Sandoval 			if (ptr < name) {
950*05dbe683SOmar Sandoval 				ret = -ENAMETOOLONG;
951*05dbe683SOmar Sandoval 				goto err;
952*05dbe683SOmar Sandoval 			}
953*05dbe683SOmar Sandoval 			read_extent_buffer(path->nodes[0], ptr + 1,
954*05dbe683SOmar Sandoval 					   (unsigned long)(inode_ref + 1), len);
955*05dbe683SOmar Sandoval 			ptr[0] = '/';
956*05dbe683SOmar Sandoval 			btrfs_release_path(path);
957*05dbe683SOmar Sandoval 		}
958*05dbe683SOmar Sandoval 	}
959*05dbe683SOmar Sandoval 
960*05dbe683SOmar Sandoval 	btrfs_free_path(path);
961*05dbe683SOmar Sandoval 	if (ptr == name + PATH_MAX - 1) {
962*05dbe683SOmar Sandoval 		name[0] = '/';
963*05dbe683SOmar Sandoval 		name[1] = '\0';
964*05dbe683SOmar Sandoval 	} else {
965*05dbe683SOmar Sandoval 		memmove(name, ptr, name + PATH_MAX - ptr);
966*05dbe683SOmar Sandoval 	}
967*05dbe683SOmar Sandoval 	return name;
968*05dbe683SOmar Sandoval 
969*05dbe683SOmar Sandoval err:
970*05dbe683SOmar Sandoval 	btrfs_free_path(path);
971*05dbe683SOmar Sandoval 	kfree(name);
972*05dbe683SOmar Sandoval 	return ERR_PTR(ret);
973*05dbe683SOmar Sandoval }
974*05dbe683SOmar Sandoval 
975*05dbe683SOmar Sandoval static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid)
976*05dbe683SOmar Sandoval {
977*05dbe683SOmar 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)
985*05dbe683SOmar 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);
997*05dbe683SOmar 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
1003*05dbe683SOmar Sandoval 		 * mount the top-level subvolume.
100473f73415SJosef Bacik 		 */
100573f73415SJosef Bacik 		btrfs_free_path(path);
1006*05dbe683SOmar Sandoval 		*objectid = BTRFS_FS_TREE_OBJECTID;
1007*05dbe683SOmar 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);
1012*05dbe683SOmar Sandoval 	*objectid = location.objectid;
1013*05dbe683SOmar 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);
1191a9572a15SEric Paris 	return 0;
1192a9572a15SEric Paris }
1193a9572a15SEric Paris 
1194a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data)
11952e635a27SChris Mason {
1196815745cfSAl Viro 	struct btrfs_fs_info *p = data;
1197815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(s);
11984b82d6e4SYan 
1199815745cfSAl Viro 	return fs_info->fs_devices == p->fs_devices;
12004b82d6e4SYan }
12014b82d6e4SYan 
1202450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data)
1203450ba0eaSJosef Bacik {
12046de1d09dSAl Viro 	int err = set_anon_super(s, data);
12056de1d09dSAl Viro 	if (!err)
1206450ba0eaSJosef Bacik 		s->s_fs_info = data;
12076de1d09dSAl Viro 	return err;
1208450ba0eaSJosef Bacik }
1209450ba0eaSJosef Bacik 
1210830c4adbSJosef Bacik /*
1211f9d9ef62SDavid Sterba  * subvolumes are identified by ino 256
1212f9d9ef62SDavid Sterba  */
1213f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode)
1214f9d9ef62SDavid Sterba {
1215f9d9ef62SDavid Sterba 	if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
1216f9d9ef62SDavid Sterba 		return 1;
1217f9d9ef62SDavid Sterba 	return 0;
1218f9d9ef62SDavid Sterba }
1219f9d9ef62SDavid Sterba 
1220f9d9ef62SDavid Sterba /*
1221e6e4dbe8SOmar Sandoval  * This will add subvolid=0 to the argument string while removing any subvol=
1222e6e4dbe8SOmar Sandoval  * and subvolid= arguments to make sure we get the top-level root for path
1223e6e4dbe8SOmar Sandoval  * walking to the subvol we want.
1224830c4adbSJosef Bacik  */
1225830c4adbSJosef Bacik static char *setup_root_args(char *args)
1226830c4adbSJosef Bacik {
1227e6e4dbe8SOmar Sandoval 	char *buf, *dst, *sep;
1228830c4adbSJosef Bacik 
1229e6e4dbe8SOmar Sandoval 	if (!args)
1230e6e4dbe8SOmar Sandoval 		return kstrdup("subvolid=0", GFP_NOFS);
1231830c4adbSJosef Bacik 
1232e6e4dbe8SOmar Sandoval 	/* The worst case is that we add ",subvolid=0" to the end. */
1233e6e4dbe8SOmar Sandoval 	buf = dst = kmalloc(strlen(args) + strlen(",subvolid=0") + 1, GFP_NOFS);
1234f60d16a8SJim Meyering 	if (!buf)
1235f60d16a8SJim Meyering 		return NULL;
1236830c4adbSJosef Bacik 
1237e6e4dbe8SOmar Sandoval 	while (1) {
1238e6e4dbe8SOmar Sandoval 		sep = strchrnul(args, ',');
1239e6e4dbe8SOmar Sandoval 		if (!strstarts(args, "subvol=") &&
1240e6e4dbe8SOmar Sandoval 		    !strstarts(args, "subvolid=")) {
1241e6e4dbe8SOmar Sandoval 			memcpy(dst, args, sep - args);
1242e6e4dbe8SOmar Sandoval 			dst += sep - args;
1243e6e4dbe8SOmar Sandoval 			*dst++ = ',';
1244830c4adbSJosef Bacik 		}
1245e6e4dbe8SOmar Sandoval 		if (*sep)
1246e6e4dbe8SOmar Sandoval 			args = sep + 1;
1247e6e4dbe8SOmar Sandoval 		else
1248e6e4dbe8SOmar Sandoval 			break;
1249e6e4dbe8SOmar Sandoval 	}
1250f60d16a8SJim Meyering 	strcpy(dst, "subvolid=0");
1251830c4adbSJosef Bacik 
1252f60d16a8SJim Meyering 	return buf;
1253830c4adbSJosef Bacik }
1254830c4adbSJosef Bacik 
1255bb289b7bSOmar Sandoval static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
1256bb289b7bSOmar Sandoval 				   int flags, const char *device_name,
1257bb289b7bSOmar Sandoval 				   char *data)
1258830c4adbSJosef Bacik {
1259830c4adbSJosef Bacik 	struct dentry *root;
1260fa330659SOmar Sandoval 	struct vfsmount *mnt = NULL;
1261830c4adbSJosef Bacik 	char *newargs;
1262fa330659SOmar Sandoval 	int ret;
1263830c4adbSJosef Bacik 
1264830c4adbSJosef Bacik 	newargs = setup_root_args(data);
1265fa330659SOmar Sandoval 	if (!newargs) {
1266fa330659SOmar Sandoval 		root = ERR_PTR(-ENOMEM);
1267fa330659SOmar Sandoval 		goto out;
1268fa330659SOmar Sandoval 	}
12690723a047SHarald Hoyer 
1270fa330659SOmar Sandoval 	mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name, newargs);
1271fa330659SOmar Sandoval 	if (PTR_ERR_OR_ZERO(mnt) == -EBUSY) {
12720723a047SHarald Hoyer 		if (flags & MS_RDONLY) {
1273fa330659SOmar Sandoval 			mnt = vfs_kern_mount(&btrfs_fs_type, flags & ~MS_RDONLY,
1274fa330659SOmar Sandoval 					     device_name, newargs);
12750723a047SHarald Hoyer 		} else {
1276fa330659SOmar Sandoval 			mnt = vfs_kern_mount(&btrfs_fs_type, flags | MS_RDONLY,
1277fa330659SOmar Sandoval 					     device_name, newargs);
12780040e606SChristoph Jaeger 			if (IS_ERR(mnt)) {
1279fa330659SOmar Sandoval 				root = ERR_CAST(mnt);
1280fa330659SOmar Sandoval 				mnt = NULL;
1281fa330659SOmar Sandoval 				goto out;
12820040e606SChristoph Jaeger 			}
12830723a047SHarald Hoyer 
1284773cd04eSOmar Sandoval 			down_write(&mnt->mnt_sb->s_umount);
1285fa330659SOmar Sandoval 			ret = btrfs_remount(mnt->mnt_sb, &flags, NULL);
1286773cd04eSOmar Sandoval 			up_write(&mnt->mnt_sb->s_umount);
1287fa330659SOmar Sandoval 			if (ret < 0) {
1288fa330659SOmar Sandoval 				root = ERR_PTR(ret);
1289fa330659SOmar Sandoval 				goto out;
12900723a047SHarald Hoyer 			}
12910723a047SHarald Hoyer 		}
12920723a047SHarald Hoyer 	}
1293fa330659SOmar Sandoval 	if (IS_ERR(mnt)) {
1294fa330659SOmar Sandoval 		root = ERR_CAST(mnt);
1295fa330659SOmar Sandoval 		mnt = NULL;
1296fa330659SOmar Sandoval 		goto out;
1297fa330659SOmar Sandoval 	}
1298830c4adbSJosef Bacik 
1299*05dbe683SOmar Sandoval 	if (!subvol_name) {
1300*05dbe683SOmar Sandoval 		if (!subvol_objectid) {
1301*05dbe683SOmar Sandoval 			ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
1302*05dbe683SOmar Sandoval 							  &subvol_objectid);
1303*05dbe683SOmar Sandoval 			if (ret) {
1304*05dbe683SOmar Sandoval 				root = ERR_PTR(ret);
1305*05dbe683SOmar Sandoval 				goto out;
1306*05dbe683SOmar Sandoval 			}
1307*05dbe683SOmar Sandoval 		}
1308*05dbe683SOmar Sandoval 		subvol_name = get_subvol_name_from_objectid(btrfs_sb(mnt->mnt_sb),
1309*05dbe683SOmar Sandoval 							    subvol_objectid);
1310*05dbe683SOmar Sandoval 		if (IS_ERR(subvol_name)) {
1311*05dbe683SOmar Sandoval 			root = ERR_CAST(subvol_name);
1312*05dbe683SOmar Sandoval 			subvol_name = NULL;
1313*05dbe683SOmar Sandoval 			goto out;
1314*05dbe683SOmar Sandoval 		}
1315*05dbe683SOmar Sandoval 
1316*05dbe683SOmar Sandoval 	}
1317*05dbe683SOmar Sandoval 
1318ea441d11SAl Viro 	root = mount_subtree(mnt, subvol_name);
1319fa330659SOmar Sandoval 	/* mount_subtree() drops our reference on the vfsmount. */
1320fa330659SOmar Sandoval 	mnt = NULL;
1321830c4adbSJosef Bacik 
1322bb289b7bSOmar Sandoval 	if (!IS_ERR(root)) {
1323ea441d11SAl Viro 		struct super_block *s = root->d_sb;
1324bb289b7bSOmar Sandoval 		struct inode *root_inode = d_inode(root);
1325bb289b7bSOmar Sandoval 		u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid;
1326bb289b7bSOmar Sandoval 
1327bb289b7bSOmar Sandoval 		ret = 0;
1328bb289b7bSOmar Sandoval 		if (!is_subvolume_inode(root_inode)) {
1329bb289b7bSOmar Sandoval 			pr_err("BTRFS: '%s' is not a valid subvolume\n",
1330bb289b7bSOmar Sandoval 			       subvol_name);
1331bb289b7bSOmar Sandoval 			ret = -EINVAL;
1332bb289b7bSOmar Sandoval 		}
1333bb289b7bSOmar Sandoval 		if (subvol_objectid && root_objectid != subvol_objectid) {
1334*05dbe683SOmar Sandoval 			/*
1335*05dbe683SOmar Sandoval 			 * This will also catch a race condition where a
1336*05dbe683SOmar Sandoval 			 * subvolume which was passed by ID is renamed and
1337*05dbe683SOmar Sandoval 			 * another subvolume is renamed over the old location.
1338*05dbe683SOmar Sandoval 			 */
1339bb289b7bSOmar Sandoval 			pr_err("BTRFS: subvol '%s' does not match subvolid %llu\n",
1340bb289b7bSOmar Sandoval 			       subvol_name, subvol_objectid);
1341bb289b7bSOmar Sandoval 			ret = -EINVAL;
1342bb289b7bSOmar Sandoval 		}
1343bb289b7bSOmar Sandoval 		if (ret) {
1344ea441d11SAl Viro 			dput(root);
1345bb289b7bSOmar Sandoval 			root = ERR_PTR(ret);
1346ea441d11SAl Viro 			deactivate_locked_super(s);
1347bb289b7bSOmar Sandoval 		}
1348f9d9ef62SDavid Sterba 	}
1349f9d9ef62SDavid Sterba 
1350fa330659SOmar Sandoval out:
1351fa330659SOmar Sandoval 	mntput(mnt);
1352fa330659SOmar Sandoval 	kfree(newargs);
1353fa330659SOmar Sandoval 	kfree(subvol_name);
1354830c4adbSJosef Bacik 	return root;
1355830c4adbSJosef Bacik }
1356450ba0eaSJosef Bacik 
1357f667aef6SQu Wenruo static int parse_security_options(char *orig_opts,
1358f667aef6SQu Wenruo 				  struct security_mnt_opts *sec_opts)
1359f667aef6SQu Wenruo {
1360f667aef6SQu Wenruo 	char *secdata = NULL;
1361f667aef6SQu Wenruo 	int ret = 0;
1362f667aef6SQu Wenruo 
1363f667aef6SQu Wenruo 	secdata = alloc_secdata();
1364f667aef6SQu Wenruo 	if (!secdata)
1365f667aef6SQu Wenruo 		return -ENOMEM;
1366f667aef6SQu Wenruo 	ret = security_sb_copy_data(orig_opts, secdata);
1367f667aef6SQu Wenruo 	if (ret) {
1368f667aef6SQu Wenruo 		free_secdata(secdata);
1369f667aef6SQu Wenruo 		return ret;
1370f667aef6SQu Wenruo 	}
1371f667aef6SQu Wenruo 	ret = security_sb_parse_opts_str(secdata, sec_opts);
1372f667aef6SQu Wenruo 	free_secdata(secdata);
1373f667aef6SQu Wenruo 	return ret;
1374f667aef6SQu Wenruo }
1375f667aef6SQu Wenruo 
1376f667aef6SQu Wenruo static int setup_security_options(struct btrfs_fs_info *fs_info,
1377f667aef6SQu Wenruo 				  struct super_block *sb,
1378f667aef6SQu Wenruo 				  struct security_mnt_opts *sec_opts)
1379f667aef6SQu Wenruo {
1380f667aef6SQu Wenruo 	int ret = 0;
1381f667aef6SQu Wenruo 
1382f667aef6SQu Wenruo 	/*
1383f667aef6SQu Wenruo 	 * Call security_sb_set_mnt_opts() to check whether new sec_opts
1384f667aef6SQu Wenruo 	 * is valid.
1385f667aef6SQu Wenruo 	 */
1386f667aef6SQu Wenruo 	ret = security_sb_set_mnt_opts(sb, sec_opts, 0, NULL);
1387f667aef6SQu Wenruo 	if (ret)
1388f667aef6SQu Wenruo 		return ret;
1389f667aef6SQu Wenruo 
1390a43bb39bSQu Wenruo #ifdef CONFIG_SECURITY
1391f667aef6SQu Wenruo 	if (!fs_info->security_opts.num_mnt_opts) {
1392f667aef6SQu Wenruo 		/* first time security setup, copy sec_opts to fs_info */
1393f667aef6SQu Wenruo 		memcpy(&fs_info->security_opts, sec_opts, sizeof(*sec_opts));
1394f667aef6SQu Wenruo 	} else {
1395f667aef6SQu Wenruo 		/*
1396f667aef6SQu Wenruo 		 * Since SELinux(the only one supports security_mnt_opts) does
1397f667aef6SQu Wenruo 		 * NOT support changing context during remount/mount same sb,
1398f667aef6SQu Wenruo 		 * This must be the same or part of the same security options,
1399f667aef6SQu Wenruo 		 * just free it.
1400f667aef6SQu Wenruo 		 */
1401f667aef6SQu Wenruo 		security_free_mnt_opts(sec_opts);
1402f667aef6SQu Wenruo 	}
1403a43bb39bSQu Wenruo #endif
1404f667aef6SQu Wenruo 	return ret;
1405f667aef6SQu Wenruo }
1406f667aef6SQu Wenruo 
1407edf24abeSChristoph Hellwig /*
1408edf24abeSChristoph Hellwig  * Find a superblock for the given device / mount point.
1409edf24abeSChristoph Hellwig  *
1410edf24abeSChristoph Hellwig  * Note:  This is based on get_sb_bdev from fs/super.c with a few additions
1411edf24abeSChristoph Hellwig  *	  for multiple device setup.  Make sure to keep it in sync.
1412edf24abeSChristoph Hellwig  */
1413061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
1414306e16ceSDavid Sterba 		const char *device_name, void *data)
14154b82d6e4SYan {
14164b82d6e4SYan 	struct block_device *bdev = NULL;
14174b82d6e4SYan 	struct super_block *s;
14188a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices = NULL;
1419450ba0eaSJosef Bacik 	struct btrfs_fs_info *fs_info = NULL;
1420f667aef6SQu Wenruo 	struct security_mnt_opts new_sec_opts;
142197288f2cSChristoph Hellwig 	fmode_t mode = FMODE_READ;
142273f73415SJosef Bacik 	char *subvol_name = NULL;
142373f73415SJosef Bacik 	u64 subvol_objectid = 0;
14244b82d6e4SYan 	int error = 0;
14254b82d6e4SYan 
142697288f2cSChristoph Hellwig 	if (!(flags & MS_RDONLY))
142797288f2cSChristoph Hellwig 		mode |= FMODE_WRITE;
142897288f2cSChristoph Hellwig 
142997288f2cSChristoph Hellwig 	error = btrfs_parse_early_options(data, mode, fs_type,
143073f73415SJosef Bacik 					  &subvol_name, &subvol_objectid,
14315e2a4b25SDavid Sterba 					  &fs_devices);
1432f23c8af8SIlya Dryomov 	if (error) {
1433f23c8af8SIlya Dryomov 		kfree(subvol_name);
1434061dbc6bSAl Viro 		return ERR_PTR(error);
1435f23c8af8SIlya Dryomov 	}
1436edf24abeSChristoph Hellwig 
1437*05dbe683SOmar Sandoval 	if (subvol_name || subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
1438fa330659SOmar Sandoval 		/* mount_subvol() will free subvol_name. */
1439bb289b7bSOmar Sandoval 		return mount_subvol(subvol_name, subvol_objectid, flags,
1440bb289b7bSOmar Sandoval 				    device_name, data);
1441830c4adbSJosef Bacik 	}
1442830c4adbSJosef Bacik 
1443f667aef6SQu Wenruo 	security_init_mnt_opts(&new_sec_opts);
1444f667aef6SQu Wenruo 	if (data) {
1445f667aef6SQu Wenruo 		error = parse_security_options(data, &new_sec_opts);
14468a4b83ccSChris Mason 		if (error)
1447830c4adbSJosef Bacik 			return ERR_PTR(error);
1448f667aef6SQu Wenruo 	}
1449f667aef6SQu Wenruo 
1450f667aef6SQu Wenruo 	error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
1451f667aef6SQu Wenruo 	if (error)
1452f667aef6SQu Wenruo 		goto error_sec_opts;
14534b82d6e4SYan 
1454450ba0eaSJosef Bacik 	/*
1455450ba0eaSJosef Bacik 	 * Setup a dummy root and fs_info for test/set super.  This is because
1456450ba0eaSJosef Bacik 	 * we don't actually fill this stuff out until open_ctree, but we need
1457450ba0eaSJosef Bacik 	 * it for searching for existing supers, so this lets us do that and
1458450ba0eaSJosef Bacik 	 * then open_ctree will properly initialize everything later.
1459450ba0eaSJosef Bacik 	 */
1460450ba0eaSJosef Bacik 	fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
1461f667aef6SQu Wenruo 	if (!fs_info) {
1462f667aef6SQu Wenruo 		error = -ENOMEM;
1463f667aef6SQu Wenruo 		goto error_sec_opts;
1464f667aef6SQu Wenruo 	}
146504d21a24SIlya Dryomov 
1466450ba0eaSJosef Bacik 	fs_info->fs_devices = fs_devices;
1467450ba0eaSJosef Bacik 
14686c41761fSDavid Sterba 	fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
14696c41761fSDavid Sterba 	fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
1470f667aef6SQu Wenruo 	security_init_mnt_opts(&fs_info->security_opts);
14716c41761fSDavid Sterba 	if (!fs_info->super_copy || !fs_info->super_for_commit) {
14726c41761fSDavid Sterba 		error = -ENOMEM;
147304d21a24SIlya Dryomov 		goto error_fs_info;
147404d21a24SIlya Dryomov 	}
147504d21a24SIlya Dryomov 
147604d21a24SIlya Dryomov 	error = btrfs_open_devices(fs_devices, mode, fs_type);
147704d21a24SIlya Dryomov 	if (error)
147804d21a24SIlya Dryomov 		goto error_fs_info;
147904d21a24SIlya Dryomov 
148004d21a24SIlya Dryomov 	if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
148104d21a24SIlya Dryomov 		error = -EACCES;
14826c41761fSDavid Sterba 		goto error_close_devices;
14836c41761fSDavid Sterba 	}
14846c41761fSDavid Sterba 
1485dfe25020SChris Mason 	bdev = fs_devices->latest_bdev;
14869249e17fSDavid Howells 	s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC,
14879249e17fSDavid Howells 		 fs_info);
1488830c4adbSJosef Bacik 	if (IS_ERR(s)) {
1489830c4adbSJosef Bacik 		error = PTR_ERR(s);
1490830c4adbSJosef Bacik 		goto error_close_devices;
1491830c4adbSJosef Bacik 	}
14924b82d6e4SYan 
14934b82d6e4SYan 	if (s->s_root) {
14942b82032cSYan Zheng 		btrfs_close_devices(fs_devices);
14956c41761fSDavid Sterba 		free_fs_info(fs_info);
149659553edfSAl Viro 		if ((flags ^ s->s_flags) & MS_RDONLY)
149759553edfSAl Viro 			error = -EBUSY;
14984b82d6e4SYan 	} else {
14994b82d6e4SYan 		char b[BDEVNAME_SIZE];
15004b82d6e4SYan 
15014b82d6e4SYan 		strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
1502815745cfSAl Viro 		btrfs_sb(s)->bdev_holder = fs_type;
15038a4b83ccSChris Mason 		error = btrfs_fill_super(s, fs_devices, data,
15048a4b83ccSChris Mason 					 flags & MS_SILENT ? 1 : 0);
15054b82d6e4SYan 	}
1506*05dbe683SOmar Sandoval 	if (error) {
1507e15d0542SXin Zhong 		deactivate_locked_super(s);
1508f667aef6SQu Wenruo 		goto error_sec_opts;
1509f667aef6SQu Wenruo 	}
1510f667aef6SQu Wenruo 
1511f667aef6SQu Wenruo 	fs_info = btrfs_sb(s);
1512f667aef6SQu Wenruo 	error = setup_security_options(fs_info, s, &new_sec_opts);
1513f667aef6SQu Wenruo 	if (error) {
1514f667aef6SQu Wenruo 		deactivate_locked_super(s);
1515f667aef6SQu Wenruo 		goto error_sec_opts;
1516f667aef6SQu Wenruo 	}
15174b82d6e4SYan 
1518*05dbe683SOmar Sandoval 	return dget(s->s_root);
15194b82d6e4SYan 
1520c146afadSYan Zheng error_close_devices:
15218a4b83ccSChris Mason 	btrfs_close_devices(fs_devices);
152204d21a24SIlya Dryomov error_fs_info:
15236c41761fSDavid Sterba 	free_fs_info(fs_info);
1524f667aef6SQu Wenruo error_sec_opts:
1525f667aef6SQu Wenruo 	security_free_mnt_opts(&new_sec_opts);
1526061dbc6bSAl Viro 	return ERR_PTR(error);
15274b82d6e4SYan }
15282e635a27SChris Mason 
15290d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
15300d2450abSSergei Trofimovich 				     int new_pool_size, int old_pool_size)
15310d2450abSSergei Trofimovich {
15320d2450abSSergei Trofimovich 	if (new_pool_size == old_pool_size)
15330d2450abSSergei Trofimovich 		return;
15340d2450abSSergei Trofimovich 
15350d2450abSSergei Trofimovich 	fs_info->thread_pool_size = new_pool_size;
15360d2450abSSergei Trofimovich 
1537efe120a0SFrank Holton 	btrfs_info(fs_info, "resize thread pool %d -> %d",
15380d2450abSSergei Trofimovich 	       old_pool_size, new_pool_size);
15390d2450abSSergei Trofimovich 
15405cdc7ad3SQu Wenruo 	btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
1541afe3d242SQu Wenruo 	btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
1542a8c93d4eSQu Wenruo 	btrfs_workqueue_set_max(fs_info->submit_workers, new_pool_size);
1543e66f0bb1SQu Wenruo 	btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
1544fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size);
1545fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size);
1546fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_meta_write_workers,
1547fccb5d86SQu Wenruo 				new_pool_size);
1548fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
1549fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
15505b3bc44eSQu Wenruo 	btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
1551736cfa15SQu Wenruo 	btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size);
15520339ef2fSQu Wenruo 	btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers,
1553ff023aacSStefan Behrens 				new_pool_size);
15540d2450abSSergei Trofimovich }
15550d2450abSSergei Trofimovich 
1556f42a34b2SMiao Xie static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info)
1557dc81cdc5SMiao Xie {
1558dc81cdc5SMiao Xie 	set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1559f42a34b2SMiao Xie }
1560dc81cdc5SMiao Xie 
1561f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1562f42a34b2SMiao Xie 				       unsigned long old_opts, int flags)
1563f42a34b2SMiao Xie {
1564dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1565dc81cdc5SMiao Xie 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
1566dc81cdc5SMiao Xie 	     (flags & MS_RDONLY))) {
1567dc81cdc5SMiao Xie 		/* wait for any defraggers to finish */
1568dc81cdc5SMiao Xie 		wait_event(fs_info->transaction_wait,
1569dc81cdc5SMiao Xie 			   (atomic_read(&fs_info->defrag_running) == 0));
1570dc81cdc5SMiao Xie 		if (flags & MS_RDONLY)
1571dc81cdc5SMiao Xie 			sync_filesystem(fs_info->sb);
1572dc81cdc5SMiao Xie 	}
1573dc81cdc5SMiao Xie }
1574dc81cdc5SMiao Xie 
1575dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1576dc81cdc5SMiao Xie 					 unsigned long old_opts)
1577dc81cdc5SMiao Xie {
1578dc81cdc5SMiao Xie 	/*
1579dc81cdc5SMiao Xie 	 * We need cleanup all defragable inodes if the autodefragment is
1580dc81cdc5SMiao Xie 	 * close or the fs is R/O.
1581dc81cdc5SMiao Xie 	 */
1582dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1583dc81cdc5SMiao Xie 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
1584dc81cdc5SMiao Xie 	     (fs_info->sb->s_flags & MS_RDONLY))) {
1585dc81cdc5SMiao Xie 		btrfs_cleanup_defrag_inodes(fs_info);
1586dc81cdc5SMiao Xie 	}
1587dc81cdc5SMiao Xie 
1588dc81cdc5SMiao Xie 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1589dc81cdc5SMiao Xie }
1590dc81cdc5SMiao Xie 
1591c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1592c146afadSYan Zheng {
1593815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1594815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
159549b25e05SJeff Mahoney 	unsigned old_flags = sb->s_flags;
159649b25e05SJeff Mahoney 	unsigned long old_opts = fs_info->mount_opt;
159749b25e05SJeff Mahoney 	unsigned long old_compress_type = fs_info->compress_type;
159849b25e05SJeff Mahoney 	u64 old_max_inline = fs_info->max_inline;
159949b25e05SJeff Mahoney 	u64 old_alloc_start = fs_info->alloc_start;
160049b25e05SJeff Mahoney 	int old_thread_pool_size = fs_info->thread_pool_size;
160149b25e05SJeff Mahoney 	unsigned int old_metadata_ratio = fs_info->metadata_ratio;
1602c146afadSYan Zheng 	int ret;
1603c146afadSYan Zheng 
160402b9984dSTheodore Ts'o 	sync_filesystem(sb);
1605f42a34b2SMiao Xie 	btrfs_remount_prepare(fs_info);
1606dc81cdc5SMiao Xie 
1607f667aef6SQu Wenruo 	if (data) {
1608f667aef6SQu Wenruo 		struct security_mnt_opts new_sec_opts;
1609f667aef6SQu Wenruo 
1610f667aef6SQu Wenruo 		security_init_mnt_opts(&new_sec_opts);
1611f667aef6SQu Wenruo 		ret = parse_security_options(data, &new_sec_opts);
1612f667aef6SQu Wenruo 		if (ret)
1613f667aef6SQu Wenruo 			goto restore;
1614f667aef6SQu Wenruo 		ret = setup_security_options(fs_info, sb,
1615f667aef6SQu Wenruo 					     &new_sec_opts);
1616f667aef6SQu Wenruo 		if (ret) {
1617f667aef6SQu Wenruo 			security_free_mnt_opts(&new_sec_opts);
1618f667aef6SQu Wenruo 			goto restore;
1619f667aef6SQu Wenruo 		}
1620f667aef6SQu Wenruo 	}
1621f667aef6SQu Wenruo 
1622b288052eSChris Mason 	ret = btrfs_parse_options(root, data);
162349b25e05SJeff Mahoney 	if (ret) {
162449b25e05SJeff Mahoney 		ret = -EINVAL;
162549b25e05SJeff Mahoney 		goto restore;
162649b25e05SJeff Mahoney 	}
1627b288052eSChris Mason 
1628f42a34b2SMiao Xie 	btrfs_remount_begin(fs_info, old_opts, *flags);
16290d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
16300d2450abSSergei Trofimovich 		fs_info->thread_pool_size, old_thread_pool_size);
16310d2450abSSergei Trofimovich 
1632c146afadSYan Zheng 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
1633dc81cdc5SMiao Xie 		goto out;
1634c146afadSYan Zheng 
1635c146afadSYan Zheng 	if (*flags & MS_RDONLY) {
16368dabb742SStefan Behrens 		/*
16378dabb742SStefan Behrens 		 * this also happens on 'umount -rf' or on shutdown, when
16388dabb742SStefan Behrens 		 * the filesystem is busy.
16398dabb742SStefan Behrens 		 */
164021c7e756SMiao Xie 		cancel_work_sync(&fs_info->async_reclaim_work);
1641361c093dSStefan Behrens 
1642361c093dSStefan Behrens 		/* wait for the uuid_scan task to finish */
1643361c093dSStefan Behrens 		down(&fs_info->uuid_tree_rescan_sem);
1644361c093dSStefan Behrens 		/* avoid complains from lockdep et al. */
1645361c093dSStefan Behrens 		up(&fs_info->uuid_tree_rescan_sem);
1646361c093dSStefan Behrens 
1647c146afadSYan Zheng 		sb->s_flags |= MS_RDONLY;
1648c146afadSYan Zheng 
16498dabb742SStefan Behrens 		btrfs_dev_replace_suspend_for_unmount(fs_info);
16508dabb742SStefan Behrens 		btrfs_scrub_cancel(fs_info);
1651061594efSMiao Xie 		btrfs_pause_balance(fs_info);
16528dabb742SStefan Behrens 
1653c146afadSYan Zheng 		ret = btrfs_commit_super(root);
165449b25e05SJeff Mahoney 		if (ret)
165549b25e05SJeff Mahoney 			goto restore;
1656c146afadSYan Zheng 	} else {
16576ef3de9cSDavid Sterba 		if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) {
16586ef3de9cSDavid Sterba 			btrfs_err(fs_info,
1659efe120a0SFrank Holton 				"Remounting read-write after error is not allowed");
16606ef3de9cSDavid Sterba 			ret = -EINVAL;
16616ef3de9cSDavid Sterba 			goto restore;
16626ef3de9cSDavid Sterba 		}
16638a3db184SSergei Trofimovich 		if (fs_info->fs_devices->rw_devices == 0) {
166449b25e05SJeff Mahoney 			ret = -EACCES;
166549b25e05SJeff Mahoney 			goto restore;
16668a3db184SSergei Trofimovich 		}
16672b82032cSYan Zheng 
1668292fd7fcSStefan Behrens 		if (fs_info->fs_devices->missing_devices >
1669292fd7fcSStefan Behrens 		     fs_info->num_tolerated_disk_barrier_failures &&
1670292fd7fcSStefan Behrens 		    !(*flags & MS_RDONLY)) {
1671efe120a0SFrank Holton 			btrfs_warn(fs_info,
1672efe120a0SFrank Holton 				"too many missing devices, writeable remount is not allowed");
1673292fd7fcSStefan Behrens 			ret = -EACCES;
1674292fd7fcSStefan Behrens 			goto restore;
1675292fd7fcSStefan Behrens 		}
1676292fd7fcSStefan Behrens 
16778a3db184SSergei Trofimovich 		if (btrfs_super_log_root(fs_info->super_copy) != 0) {
167849b25e05SJeff Mahoney 			ret = -EINVAL;
167949b25e05SJeff Mahoney 			goto restore;
16808a3db184SSergei Trofimovich 		}
1681c146afadSYan Zheng 
1682815745cfSAl Viro 		ret = btrfs_cleanup_fs_roots(fs_info);
168349b25e05SJeff Mahoney 		if (ret)
168449b25e05SJeff Mahoney 			goto restore;
1685c146afadSYan Zheng 
1686d68fc57bSYan, Zheng 		/* recover relocation */
16875f316481SWang Shilong 		mutex_lock(&fs_info->cleaner_mutex);
1688d68fc57bSYan, Zheng 		ret = btrfs_recover_relocation(root);
16895f316481SWang Shilong 		mutex_unlock(&fs_info->cleaner_mutex);
169049b25e05SJeff Mahoney 		if (ret)
169149b25e05SJeff Mahoney 			goto restore;
1692c146afadSYan Zheng 
16932b6ba629SIlya Dryomov 		ret = btrfs_resume_balance_async(fs_info);
16942b6ba629SIlya Dryomov 		if (ret)
16952b6ba629SIlya Dryomov 			goto restore;
16962b6ba629SIlya Dryomov 
16978dabb742SStefan Behrens 		ret = btrfs_resume_dev_replace_async(fs_info);
16988dabb742SStefan Behrens 		if (ret) {
1699efe120a0SFrank Holton 			btrfs_warn(fs_info, "failed to resume dev_replace");
17008dabb742SStefan Behrens 			goto restore;
17018dabb742SStefan Behrens 		}
170294aebfb2SJosef Bacik 
170394aebfb2SJosef Bacik 		if (!fs_info->uuid_root) {
1704efe120a0SFrank Holton 			btrfs_info(fs_info, "creating UUID tree");
170594aebfb2SJosef Bacik 			ret = btrfs_create_uuid_tree(fs_info);
170694aebfb2SJosef Bacik 			if (ret) {
1707efe120a0SFrank Holton 				btrfs_warn(fs_info, "failed to create the UUID tree %d", ret);
170894aebfb2SJosef Bacik 				goto restore;
170994aebfb2SJosef Bacik 			}
171094aebfb2SJosef Bacik 		}
1711c146afadSYan Zheng 		sb->s_flags &= ~MS_RDONLY;
1712c146afadSYan Zheng 	}
1713dc81cdc5SMiao Xie out:
17142c6a92b0SJustin Maggard 	wake_up_process(fs_info->transaction_kthread);
1715dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
1716c146afadSYan Zheng 	return 0;
171749b25e05SJeff Mahoney 
171849b25e05SJeff Mahoney restore:
171949b25e05SJeff Mahoney 	/* We've hit an error - don't reset MS_RDONLY */
172049b25e05SJeff Mahoney 	if (sb->s_flags & MS_RDONLY)
172149b25e05SJeff Mahoney 		old_flags |= MS_RDONLY;
172249b25e05SJeff Mahoney 	sb->s_flags = old_flags;
172349b25e05SJeff Mahoney 	fs_info->mount_opt = old_opts;
172449b25e05SJeff Mahoney 	fs_info->compress_type = old_compress_type;
172549b25e05SJeff Mahoney 	fs_info->max_inline = old_max_inline;
1726c018daecSMiao Xie 	mutex_lock(&fs_info->chunk_mutex);
172749b25e05SJeff Mahoney 	fs_info->alloc_start = old_alloc_start;
1728c018daecSMiao Xie 	mutex_unlock(&fs_info->chunk_mutex);
17290d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
17300d2450abSSergei Trofimovich 		old_thread_pool_size, fs_info->thread_pool_size);
173149b25e05SJeff Mahoney 	fs_info->metadata_ratio = old_metadata_ratio;
1732dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
173349b25e05SJeff Mahoney 	return ret;
1734c146afadSYan Zheng }
1735c146afadSYan Zheng 
1736bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */
1737bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1,
1738bcd53741SArne Jansen 				       const void *dev_info2)
1739bcd53741SArne Jansen {
1740bcd53741SArne Jansen 	if (((struct btrfs_device_info *)dev_info1)->max_avail >
1741bcd53741SArne Jansen 	    ((struct btrfs_device_info *)dev_info2)->max_avail)
1742bcd53741SArne Jansen 		return -1;
1743bcd53741SArne Jansen 	else if (((struct btrfs_device_info *)dev_info1)->max_avail <
1744bcd53741SArne Jansen 		 ((struct btrfs_device_info *)dev_info2)->max_avail)
1745bcd53741SArne Jansen 		return 1;
1746bcd53741SArne Jansen 	else
1747bcd53741SArne Jansen 	return 0;
1748bcd53741SArne Jansen }
1749bcd53741SArne Jansen 
1750bcd53741SArne Jansen /*
1751bcd53741SArne Jansen  * sort the devices by max_avail, in which max free extent size of each device
1752bcd53741SArne Jansen  * is stored.(Descending Sort)
1753bcd53741SArne Jansen  */
1754bcd53741SArne Jansen static inline void btrfs_descending_sort_devices(
1755bcd53741SArne Jansen 					struct btrfs_device_info *devices,
1756bcd53741SArne Jansen 					size_t nr_devices)
1757bcd53741SArne Jansen {
1758bcd53741SArne Jansen 	sort(devices, nr_devices, sizeof(struct btrfs_device_info),
1759bcd53741SArne Jansen 	     btrfs_cmp_device_free_bytes, NULL);
1760bcd53741SArne Jansen }
1761bcd53741SArne Jansen 
17626d07bcecSMiao Xie /*
17636d07bcecSMiao Xie  * The helper to calc the free space on the devices that can be used to store
17646d07bcecSMiao Xie  * file data.
17656d07bcecSMiao Xie  */
17666d07bcecSMiao Xie static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
17676d07bcecSMiao Xie {
17686d07bcecSMiao Xie 	struct btrfs_fs_info *fs_info = root->fs_info;
17696d07bcecSMiao Xie 	struct btrfs_device_info *devices_info;
17706d07bcecSMiao Xie 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
17716d07bcecSMiao Xie 	struct btrfs_device *device;
17726d07bcecSMiao Xie 	u64 skip_space;
17736d07bcecSMiao Xie 	u64 type;
17746d07bcecSMiao Xie 	u64 avail_space;
17756d07bcecSMiao Xie 	u64 used_space;
17766d07bcecSMiao Xie 	u64 min_stripe_size;
177739fb26c3SMiao Xie 	int min_stripes = 1, num_stripes = 1;
17786d07bcecSMiao Xie 	int i = 0, nr_devices;
17796d07bcecSMiao Xie 	int ret;
17806d07bcecSMiao Xie 
17817e33fd99SJosef Bacik 	/*
17827e33fd99SJosef Bacik 	 * We aren't under the device list lock, so this is racey-ish, but good
17837e33fd99SJosef Bacik 	 * enough for our purposes.
17847e33fd99SJosef Bacik 	 */
1785b772a86eSLi Zefan 	nr_devices = fs_info->fs_devices->open_devices;
17867e33fd99SJosef Bacik 	if (!nr_devices) {
17877e33fd99SJosef Bacik 		smp_mb();
17887e33fd99SJosef Bacik 		nr_devices = fs_info->fs_devices->open_devices;
17897e33fd99SJosef Bacik 		ASSERT(nr_devices);
17907e33fd99SJosef Bacik 		if (!nr_devices) {
17917e33fd99SJosef Bacik 			*free_bytes = 0;
17927e33fd99SJosef Bacik 			return 0;
17937e33fd99SJosef Bacik 		}
17947e33fd99SJosef Bacik 	}
17956d07bcecSMiao Xie 
1796d9b0d9baSDulshani Gunawardhana 	devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
17976d07bcecSMiao Xie 			       GFP_NOFS);
17986d07bcecSMiao Xie 	if (!devices_info)
17996d07bcecSMiao Xie 		return -ENOMEM;
18006d07bcecSMiao Xie 
18016d07bcecSMiao Xie 	/* calc min stripe number for data space alloction */
18026d07bcecSMiao Xie 	type = btrfs_get_alloc_profile(root, 1);
180339fb26c3SMiao Xie 	if (type & BTRFS_BLOCK_GROUP_RAID0) {
18046d07bcecSMiao Xie 		min_stripes = 2;
180539fb26c3SMiao Xie 		num_stripes = nr_devices;
180639fb26c3SMiao Xie 	} else if (type & BTRFS_BLOCK_GROUP_RAID1) {
18076d07bcecSMiao Xie 		min_stripes = 2;
180839fb26c3SMiao Xie 		num_stripes = 2;
180939fb26c3SMiao Xie 	} else if (type & BTRFS_BLOCK_GROUP_RAID10) {
18106d07bcecSMiao Xie 		min_stripes = 4;
181139fb26c3SMiao Xie 		num_stripes = 4;
181239fb26c3SMiao Xie 	}
18136d07bcecSMiao Xie 
18146d07bcecSMiao Xie 	if (type & BTRFS_BLOCK_GROUP_DUP)
18156d07bcecSMiao Xie 		min_stripe_size = 2 * BTRFS_STRIPE_LEN;
18166d07bcecSMiao Xie 	else
18176d07bcecSMiao Xie 		min_stripe_size = BTRFS_STRIPE_LEN;
18186d07bcecSMiao Xie 
18197e33fd99SJosef Bacik 	if (fs_info->alloc_start)
18207e33fd99SJosef Bacik 		mutex_lock(&fs_devices->device_list_mutex);
18217e33fd99SJosef Bacik 	rcu_read_lock();
18227e33fd99SJosef Bacik 	list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
182363a212abSStefan Behrens 		if (!device->in_fs_metadata || !device->bdev ||
182463a212abSStefan Behrens 		    device->is_tgtdev_for_dev_replace)
18256d07bcecSMiao Xie 			continue;
18266d07bcecSMiao Xie 
18277e33fd99SJosef Bacik 		if (i >= nr_devices)
18287e33fd99SJosef Bacik 			break;
18297e33fd99SJosef Bacik 
18306d07bcecSMiao Xie 		avail_space = device->total_bytes - device->bytes_used;
18316d07bcecSMiao Xie 
18326d07bcecSMiao Xie 		/* align with stripe_len */
1833f8c269d7SDavid Sterba 		avail_space = div_u64(avail_space, BTRFS_STRIPE_LEN);
18346d07bcecSMiao Xie 		avail_space *= BTRFS_STRIPE_LEN;
18356d07bcecSMiao Xie 
18366d07bcecSMiao Xie 		/*
18376d07bcecSMiao Xie 		 * In order to avoid overwritting the superblock on the drive,
18386d07bcecSMiao Xie 		 * btrfs starts at an offset of at least 1MB when doing chunk
18396d07bcecSMiao Xie 		 * allocation.
18406d07bcecSMiao Xie 		 */
18416d07bcecSMiao Xie 		skip_space = 1024 * 1024;
18426d07bcecSMiao Xie 
18436d07bcecSMiao Xie 		/* user can set the offset in fs_info->alloc_start. */
18447e33fd99SJosef Bacik 		if (fs_info->alloc_start &&
18457e33fd99SJosef Bacik 		    fs_info->alloc_start + BTRFS_STRIPE_LEN <=
18467e33fd99SJosef Bacik 		    device->total_bytes) {
18477e33fd99SJosef Bacik 			rcu_read_unlock();
18486d07bcecSMiao Xie 			skip_space = max(fs_info->alloc_start, skip_space);
18496d07bcecSMiao Xie 
18506d07bcecSMiao Xie 			/*
18517e33fd99SJosef Bacik 			 * btrfs can not use the free space in
18527e33fd99SJosef Bacik 			 * [0, skip_space - 1], we must subtract it from the
18537e33fd99SJosef Bacik 			 * total. In order to implement it, we account the used
18547e33fd99SJosef Bacik 			 * space in this range first.
18556d07bcecSMiao Xie 			 */
18567e33fd99SJosef Bacik 			ret = btrfs_account_dev_extents_size(device, 0,
18577e33fd99SJosef Bacik 							     skip_space - 1,
18586d07bcecSMiao Xie 							     &used_space);
18596d07bcecSMiao Xie 			if (ret) {
18606d07bcecSMiao Xie 				kfree(devices_info);
18617e33fd99SJosef Bacik 				mutex_unlock(&fs_devices->device_list_mutex);
18626d07bcecSMiao Xie 				return ret;
18636d07bcecSMiao Xie 			}
18646d07bcecSMiao Xie 
18657e33fd99SJosef Bacik 			rcu_read_lock();
18667e33fd99SJosef Bacik 
18676d07bcecSMiao Xie 			/* calc the free space in [0, skip_space - 1] */
18686d07bcecSMiao Xie 			skip_space -= used_space;
18697e33fd99SJosef Bacik 		}
18706d07bcecSMiao Xie 
18716d07bcecSMiao Xie 		/*
18726d07bcecSMiao Xie 		 * we can use the free space in [0, skip_space - 1], subtract
18736d07bcecSMiao Xie 		 * it from the total.
18746d07bcecSMiao Xie 		 */
18756d07bcecSMiao Xie 		if (avail_space && avail_space >= skip_space)
18766d07bcecSMiao Xie 			avail_space -= skip_space;
18776d07bcecSMiao Xie 		else
18786d07bcecSMiao Xie 			avail_space = 0;
18796d07bcecSMiao Xie 
18806d07bcecSMiao Xie 		if (avail_space < min_stripe_size)
18816d07bcecSMiao Xie 			continue;
18826d07bcecSMiao Xie 
18836d07bcecSMiao Xie 		devices_info[i].dev = device;
18846d07bcecSMiao Xie 		devices_info[i].max_avail = avail_space;
18856d07bcecSMiao Xie 
18866d07bcecSMiao Xie 		i++;
18876d07bcecSMiao Xie 	}
18887e33fd99SJosef Bacik 	rcu_read_unlock();
18897e33fd99SJosef Bacik 	if (fs_info->alloc_start)
18907e33fd99SJosef Bacik 		mutex_unlock(&fs_devices->device_list_mutex);
18916d07bcecSMiao Xie 
18926d07bcecSMiao Xie 	nr_devices = i;
18936d07bcecSMiao Xie 
18946d07bcecSMiao Xie 	btrfs_descending_sort_devices(devices_info, nr_devices);
18956d07bcecSMiao Xie 
18966d07bcecSMiao Xie 	i = nr_devices - 1;
18976d07bcecSMiao Xie 	avail_space = 0;
18986d07bcecSMiao Xie 	while (nr_devices >= min_stripes) {
189939fb26c3SMiao Xie 		if (num_stripes > nr_devices)
190039fb26c3SMiao Xie 			num_stripes = nr_devices;
190139fb26c3SMiao Xie 
19026d07bcecSMiao Xie 		if (devices_info[i].max_avail >= min_stripe_size) {
19036d07bcecSMiao Xie 			int j;
19046d07bcecSMiao Xie 			u64 alloc_size;
19056d07bcecSMiao Xie 
190639fb26c3SMiao Xie 			avail_space += devices_info[i].max_avail * num_stripes;
19076d07bcecSMiao Xie 			alloc_size = devices_info[i].max_avail;
190839fb26c3SMiao Xie 			for (j = i + 1 - num_stripes; j <= i; j++)
19096d07bcecSMiao Xie 				devices_info[j].max_avail -= alloc_size;
19106d07bcecSMiao Xie 		}
19116d07bcecSMiao Xie 		i--;
19126d07bcecSMiao Xie 		nr_devices--;
19136d07bcecSMiao Xie 	}
19146d07bcecSMiao Xie 
19156d07bcecSMiao Xie 	kfree(devices_info);
19166d07bcecSMiao Xie 	*free_bytes = avail_space;
19176d07bcecSMiao Xie 	return 0;
19186d07bcecSMiao Xie }
19196d07bcecSMiao Xie 
1920ba7b6e62SDavid Sterba /*
1921ba7b6e62SDavid Sterba  * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
1922ba7b6e62SDavid Sterba  *
1923ba7b6e62SDavid Sterba  * If there's a redundant raid level at DATA block groups, use the respective
1924ba7b6e62SDavid Sterba  * multiplier to scale the sizes.
1925ba7b6e62SDavid Sterba  *
1926ba7b6e62SDavid Sterba  * Unused device space usage is based on simulating the chunk allocator
1927ba7b6e62SDavid Sterba  * algorithm that respects the device sizes, order of allocations and the
1928ba7b6e62SDavid Sterba  * 'alloc_start' value, this is a close approximation of the actual use but
1929ba7b6e62SDavid Sterba  * there are other factors that may change the result (like a new metadata
1930ba7b6e62SDavid Sterba  * chunk).
1931ba7b6e62SDavid Sterba  *
1932ba7b6e62SDavid Sterba  * FIXME: not accurate for mixed block groups, total and free/used are ok,
1933ba7b6e62SDavid Sterba  * available appears slightly larger.
1934ba7b6e62SDavid Sterba  */
19358fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
19368fd17795SChris Mason {
1937815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
1938815745cfSAl Viro 	struct btrfs_super_block *disk_super = fs_info->super_copy;
1939815745cfSAl Viro 	struct list_head *head = &fs_info->space_info;
1940bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
1941bd4d1088SJosef Bacik 	u64 total_used = 0;
19426d07bcecSMiao Xie 	u64 total_free_data = 0;
1943db94535dSChris Mason 	int bits = dentry->d_sb->s_blocksize_bits;
1944815745cfSAl Viro 	__be32 *fsid = (__be32 *)fs_info->fsid;
1945ba7b6e62SDavid Sterba 	unsigned factor = 1;
1946ba7b6e62SDavid Sterba 	struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
19476d07bcecSMiao Xie 	int ret;
19488fd17795SChris Mason 
194915484377SMiao Xie 	/*
195015484377SMiao Xie 	 * holding chunk_muext to avoid allocating new chunks, holding
195115484377SMiao Xie 	 * device_list_mutex to avoid the device being removed
195215484377SMiao Xie 	 */
1953bd4d1088SJosef Bacik 	rcu_read_lock();
195489a55897SJosef Bacik 	list_for_each_entry_rcu(found, head, list) {
19556d07bcecSMiao Xie 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
1956ba7b6e62SDavid Sterba 			int i;
1957ba7b6e62SDavid Sterba 
19586d07bcecSMiao Xie 			total_free_data += found->disk_total - found->disk_used;
19596d07bcecSMiao Xie 			total_free_data -=
19606d07bcecSMiao Xie 				btrfs_account_ro_block_groups_free_space(found);
1961ba7b6e62SDavid Sterba 
1962ba7b6e62SDavid Sterba 			for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1963ba7b6e62SDavid Sterba 				if (!list_empty(&found->block_groups[i])) {
1964ba7b6e62SDavid Sterba 					switch (i) {
1965ba7b6e62SDavid Sterba 					case BTRFS_RAID_DUP:
1966ba7b6e62SDavid Sterba 					case BTRFS_RAID_RAID1:
1967ba7b6e62SDavid Sterba 					case BTRFS_RAID_RAID10:
1968ba7b6e62SDavid Sterba 						factor = 2;
1969ba7b6e62SDavid Sterba 					}
1970ba7b6e62SDavid Sterba 				}
1971ba7b6e62SDavid Sterba 			}
19726d07bcecSMiao Xie 		}
19736d07bcecSMiao Xie 
1974b742bb82SYan, Zheng 		total_used += found->disk_used;
197589a55897SJosef Bacik 	}
1976ba7b6e62SDavid Sterba 
1977bd4d1088SJosef Bacik 	rcu_read_unlock();
1978bd4d1088SJosef Bacik 
1979ba7b6e62SDavid Sterba 	buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
1980ba7b6e62SDavid Sterba 	buf->f_blocks >>= bits;
1981ba7b6e62SDavid Sterba 	buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
1982ba7b6e62SDavid Sterba 
1983ba7b6e62SDavid Sterba 	/* Account global block reserve as used, it's in logical size already */
1984ba7b6e62SDavid Sterba 	spin_lock(&block_rsv->lock);
1985ba7b6e62SDavid Sterba 	buf->f_bfree -= block_rsv->size >> bits;
1986ba7b6e62SDavid Sterba 	spin_unlock(&block_rsv->lock);
1987ba7b6e62SDavid Sterba 
19880d95c1beSDavid Sterba 	buf->f_bavail = div_u64(total_free_data, factor);
1989815745cfSAl Viro 	ret = btrfs_calc_avail_data_space(fs_info->tree_root, &total_free_data);
19907e33fd99SJosef Bacik 	if (ret)
19916d07bcecSMiao Xie 		return ret;
1992ba7b6e62SDavid Sterba 	buf->f_bavail += div_u64(total_free_data, factor);
19936d07bcecSMiao Xie 	buf->f_bavail = buf->f_bavail >> bits;
1994d397712bSChris Mason 
1995ba7b6e62SDavid Sterba 	buf->f_type = BTRFS_SUPER_MAGIC;
1996ba7b6e62SDavid Sterba 	buf->f_bsize = dentry->d_sb->s_blocksize;
1997ba7b6e62SDavid Sterba 	buf->f_namelen = BTRFS_NAME_LEN;
1998ba7b6e62SDavid Sterba 
19999d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
20009d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
20019d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
20029d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
20039d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
200432d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
20052b0143b5SDavid Howells 	buf->f_fsid.val[0] ^= BTRFS_I(d_inode(dentry))->root->objectid >> 32;
20062b0143b5SDavid Howells 	buf->f_fsid.val[1] ^= BTRFS_I(d_inode(dentry))->root->objectid;
200732d48fa1SDavid Woodhouse 
20088fd17795SChris Mason 	return 0;
20098fd17795SChris Mason }
2010b5133862SChris Mason 
2011aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb)
2012aea52e19SAl Viro {
2013815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2014aea52e19SAl Viro 	kill_anon_super(sb);
2015aea52e19SAl Viro 	free_fs_info(fs_info);
2016aea52e19SAl Viro }
2017aea52e19SAl Viro 
20182e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
20192e635a27SChris Mason 	.owner		= THIS_MODULE,
20202e635a27SChris Mason 	.name		= "btrfs",
2021061dbc6bSAl Viro 	.mount		= btrfs_mount,
2022aea52e19SAl Viro 	.kill_sb	= btrfs_kill_super,
2023f667aef6SQu Wenruo 	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
20242e635a27SChris Mason };
20257f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs");
2026a9218f6bSChris Mason 
2027d8620958STom Van Braeckel static int btrfs_control_open(struct inode *inode, struct file *file)
2028d8620958STom Van Braeckel {
2029d8620958STom Van Braeckel 	/*
2030d8620958STom Van Braeckel 	 * The control file's private_data is used to hold the
2031d8620958STom Van Braeckel 	 * transaction when it is started and is used to keep
2032d8620958STom Van Braeckel 	 * track of whether a transaction is already in progress.
2033d8620958STom Van Braeckel 	 */
2034d8620958STom Van Braeckel 	file->private_data = NULL;
2035d8620958STom Van Braeckel 	return 0;
2036d8620958STom Van Braeckel }
2037d8620958STom Van Braeckel 
2038d352ac68SChris Mason /*
2039d352ac68SChris Mason  * used by btrfsctl to scan devices when no FS is mounted
2040d352ac68SChris Mason  */
20418a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
20428a4b83ccSChris Mason 				unsigned long arg)
20438a4b83ccSChris Mason {
20448a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
20458a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices;
2046c071fcfdSChris Mason 	int ret = -ENOTTY;
20478a4b83ccSChris Mason 
2048e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2049e441d54dSChris Mason 		return -EPERM;
2050e441d54dSChris Mason 
2051dae7b665SLi Zefan 	vol = memdup_user((void __user *)arg, sizeof(*vol));
2052dae7b665SLi Zefan 	if (IS_ERR(vol))
2053dae7b665SLi Zefan 		return PTR_ERR(vol);
2054c071fcfdSChris Mason 
20558a4b83ccSChris Mason 	switch (cmd) {
20568a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
205797288f2cSChristoph Hellwig 		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
20588a4b83ccSChris Mason 					    &btrfs_fs_type, &fs_devices);
20598a4b83ccSChris Mason 		break;
206002db0844SJosef Bacik 	case BTRFS_IOC_DEVICES_READY:
206102db0844SJosef Bacik 		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
206202db0844SJosef Bacik 					    &btrfs_fs_type, &fs_devices);
206302db0844SJosef Bacik 		if (ret)
206402db0844SJosef Bacik 			break;
206502db0844SJosef Bacik 		ret = !(fs_devices->num_devices == fs_devices->total_devices);
206602db0844SJosef Bacik 		break;
20678a4b83ccSChris Mason 	}
2068dae7b665SLi Zefan 
20698a4b83ccSChris Mason 	kfree(vol);
2070f819d837SLinda Knippers 	return ret;
20718a4b83ccSChris Mason }
20728a4b83ccSChris Mason 
20730176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
2074ed0dab6bSYan {
2075354aa0fbSMiao Xie 	struct btrfs_trans_handle *trans;
2076354aa0fbSMiao Xie 	struct btrfs_root *root = btrfs_sb(sb)->tree_root;
2077354aa0fbSMiao Xie 
2078d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
2079354aa0fbSMiao Xie 	if (IS_ERR(trans)) {
2080354aa0fbSMiao Xie 		/* no transaction, don't bother */
2081354aa0fbSMiao Xie 		if (PTR_ERR(trans) == -ENOENT)
20820176260fSLinus Torvalds 			return 0;
2083354aa0fbSMiao Xie 		return PTR_ERR(trans);
2084354aa0fbSMiao Xie 	}
2085354aa0fbSMiao Xie 	return btrfs_commit_transaction(trans, root);
2086ed0dab6bSYan }
2087ed0dab6bSYan 
20889c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
20899c5085c1SJosef Bacik {
20909c5085c1SJosef Bacik 	struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
20919c5085c1SJosef Bacik 	struct btrfs_fs_devices *cur_devices;
20929c5085c1SJosef Bacik 	struct btrfs_device *dev, *first_dev = NULL;
20939c5085c1SJosef Bacik 	struct list_head *head;
20949c5085c1SJosef Bacik 	struct rcu_string *name;
20959c5085c1SJosef Bacik 
20969c5085c1SJosef Bacik 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
20979c5085c1SJosef Bacik 	cur_devices = fs_info->fs_devices;
20989c5085c1SJosef Bacik 	while (cur_devices) {
20999c5085c1SJosef Bacik 		head = &cur_devices->devices;
21009c5085c1SJosef Bacik 		list_for_each_entry(dev, head, dev_list) {
2101aa9ddcd4SJosef Bacik 			if (dev->missing)
2102aa9ddcd4SJosef Bacik 				continue;
21030aeb8a6eSAnand Jain 			if (!dev->name)
21040aeb8a6eSAnand Jain 				continue;
21059c5085c1SJosef Bacik 			if (!first_dev || dev->devid < first_dev->devid)
21069c5085c1SJosef Bacik 				first_dev = dev;
21079c5085c1SJosef Bacik 		}
21089c5085c1SJosef Bacik 		cur_devices = cur_devices->seed;
21099c5085c1SJosef Bacik 	}
21109c5085c1SJosef Bacik 
21119c5085c1SJosef Bacik 	if (first_dev) {
21129c5085c1SJosef Bacik 		rcu_read_lock();
21139c5085c1SJosef Bacik 		name = rcu_dereference(first_dev->name);
21149c5085c1SJosef Bacik 		seq_escape(m, name->str, " \t\n\\");
21159c5085c1SJosef Bacik 		rcu_read_unlock();
21169c5085c1SJosef Bacik 	} else {
21179c5085c1SJosef Bacik 		WARN_ON(1);
21189c5085c1SJosef Bacik 	}
21199c5085c1SJosef Bacik 	mutex_unlock(&fs_info->fs_devices->device_list_mutex);
21209c5085c1SJosef Bacik 	return 0;
21219c5085c1SJosef Bacik }
21229c5085c1SJosef Bacik 
2123b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
212476dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
2125bd555975SAl Viro 	.evict_inode	= btrfs_evict_inode,
2126e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
2127d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
2128a9572a15SEric Paris 	.show_options	= btrfs_show_options,
21299c5085c1SJosef Bacik 	.show_devname	= btrfs_show_devname,
21304730a4bcSChris Mason 	.write_inode	= btrfs_write_inode,
21312c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
21322c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
21338fd17795SChris Mason 	.statfs		= btrfs_statfs,
2134c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
21350176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
2136e20d96d6SChris Mason };
2137a9218f6bSChris Mason 
2138a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
2139d8620958STom Van Braeckel 	.open = btrfs_control_open,
2140a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
2141a9218f6bSChris Mason 	.compat_ioctl = btrfs_control_ioctl,
2142a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
21436038f373SArnd Bergmann 	.llseek = noop_llseek,
2144a9218f6bSChris Mason };
2145a9218f6bSChris Mason 
2146a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
2147578454ffSKay Sievers 	.minor		= BTRFS_MINOR,
2148a9218f6bSChris Mason 	.name		= "btrfs-control",
2149a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
2150a9218f6bSChris Mason };
2151a9218f6bSChris Mason 
2152578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
2153578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control");
2154578454ffSKay Sievers 
2155a9218f6bSChris Mason static int btrfs_interface_init(void)
2156a9218f6bSChris Mason {
2157a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
2158a9218f6bSChris Mason }
2159a9218f6bSChris Mason 
2160b2950863SChristoph Hellwig static void btrfs_interface_exit(void)
2161a9218f6bSChris Mason {
2162a9218f6bSChris Mason 	if (misc_deregister(&btrfs_misc) < 0)
2163efe120a0SFrank Holton 		printk(KERN_INFO "BTRFS: misc_deregister failed for control device\n");
2164a9218f6bSChris Mason }
2165a9218f6bSChris Mason 
216685965600SDavid Sterba static void btrfs_print_info(void)
216785965600SDavid Sterba {
216885965600SDavid Sterba 	printk(KERN_INFO "Btrfs loaded"
216985965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG
217085965600SDavid Sterba 			", debug=on"
217185965600SDavid Sterba #endif
217279556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT
217379556c3dSStefan Behrens 			", assert=on"
217479556c3dSStefan Behrens #endif
217585965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
217685965600SDavid Sterba 			", integrity-checker=on"
217785965600SDavid Sterba #endif
217885965600SDavid Sterba 			"\n");
217985965600SDavid Sterba }
218085965600SDavid Sterba 
2181dc11dd5dSJosef Bacik static int btrfs_run_sanity_tests(void)
2182dc11dd5dSJosef Bacik {
218306ea65a3SJosef Bacik 	int ret;
218406ea65a3SJosef Bacik 
2185294e30feSJosef Bacik 	ret = btrfs_init_test_fs();
218606ea65a3SJosef Bacik 	if (ret)
218706ea65a3SJosef Bacik 		return ret;
2188294e30feSJosef Bacik 
2189294e30feSJosef Bacik 	ret = btrfs_test_free_space_cache();
2190294e30feSJosef Bacik 	if (ret)
2191294e30feSJosef Bacik 		goto out;
2192294e30feSJosef Bacik 	ret = btrfs_test_extent_buffer_operations();
2193294e30feSJosef Bacik 	if (ret)
2194294e30feSJosef Bacik 		goto out;
2195294e30feSJosef Bacik 	ret = btrfs_test_extent_io();
2196aaedb55bSJosef Bacik 	if (ret)
2197aaedb55bSJosef Bacik 		goto out;
2198aaedb55bSJosef Bacik 	ret = btrfs_test_inodes();
2199faa2dbf0SJosef Bacik 	if (ret)
2200faa2dbf0SJosef Bacik 		goto out;
2201faa2dbf0SJosef Bacik 	ret = btrfs_test_qgroups();
2202294e30feSJosef Bacik out:
2203294e30feSJosef Bacik 	btrfs_destroy_test_fs();
2204294e30feSJosef Bacik 	return ret;
2205dc11dd5dSJosef Bacik }
2206dc11dd5dSJosef Bacik 
22072e635a27SChris Mason static int __init init_btrfs_fs(void)
22082e635a27SChris Mason {
22092c90e5d6SChris Mason 	int err;
221058176a96SJosef Bacik 
221114a958e6SFilipe David Borba Manana 	err = btrfs_hash_init();
221214a958e6SFilipe David Borba Manana 	if (err)
221314a958e6SFilipe David Borba Manana 		return err;
221414a958e6SFilipe David Borba Manana 
221563541927SFilipe David Borba Manana 	btrfs_props_init();
221663541927SFilipe David Borba Manana 
221758176a96SJosef Bacik 	err = btrfs_init_sysfs();
221858176a96SJosef Bacik 	if (err)
221914a958e6SFilipe David Borba Manana 		goto free_hash;
222058176a96SJosef Bacik 
2221143bede5SJeff Mahoney 	btrfs_init_compress();
2222d1310b2eSChris Mason 
2223261507a0SLi Zefan 	err = btrfs_init_cachep();
2224261507a0SLi Zefan 	if (err)
2225261507a0SLi Zefan 		goto free_compress;
2226261507a0SLi Zefan 
2227d1310b2eSChris Mason 	err = extent_io_init();
22282f4cbe64SWyatt Banks 	if (err)
22292f4cbe64SWyatt Banks 		goto free_cachep;
22302f4cbe64SWyatt Banks 
2231d1310b2eSChris Mason 	err = extent_map_init();
2232d1310b2eSChris Mason 	if (err)
2233d1310b2eSChris Mason 		goto free_extent_io;
2234d1310b2eSChris Mason 
22356352b91dSMiao Xie 	err = ordered_data_init();
22362f4cbe64SWyatt Banks 	if (err)
22372f4cbe64SWyatt Banks 		goto free_extent_map;
2238c8b97818SChris Mason 
22396352b91dSMiao Xie 	err = btrfs_delayed_inode_init();
22406352b91dSMiao Xie 	if (err)
22416352b91dSMiao Xie 		goto free_ordered_data;
22426352b91dSMiao Xie 
22439247f317SMiao Xie 	err = btrfs_auto_defrag_init();
224416cdcec7SMiao Xie 	if (err)
224516cdcec7SMiao Xie 		goto free_delayed_inode;
224616cdcec7SMiao Xie 
224778a6184aSMiao Xie 	err = btrfs_delayed_ref_init();
22489247f317SMiao Xie 	if (err)
22499247f317SMiao Xie 		goto free_auto_defrag;
22509247f317SMiao Xie 
2251b9e9a6cbSWang Shilong 	err = btrfs_prelim_ref_init();
2252b9e9a6cbSWang Shilong 	if (err)
2253af13b492SDavid Sterba 		goto free_delayed_ref;
2254b9e9a6cbSWang Shilong 
225597eb6b69SDavid Sterba 	err = btrfs_end_io_wq_init();
225678a6184aSMiao Xie 	if (err)
2257af13b492SDavid Sterba 		goto free_prelim_ref;
225878a6184aSMiao Xie 
225997eb6b69SDavid Sterba 	err = btrfs_interface_init();
226097eb6b69SDavid Sterba 	if (err)
226197eb6b69SDavid Sterba 		goto free_end_io_wq;
226297eb6b69SDavid Sterba 
2263e565d4b9SJan Schmidt 	btrfs_init_lockdep();
2264e565d4b9SJan Schmidt 
226585965600SDavid Sterba 	btrfs_print_info();
2266dc11dd5dSJosef Bacik 
2267dc11dd5dSJosef Bacik 	err = btrfs_run_sanity_tests();
2268dc11dd5dSJosef Bacik 	if (err)
2269dc11dd5dSJosef Bacik 		goto unregister_ioctl;
2270dc11dd5dSJosef Bacik 
2271dc11dd5dSJosef Bacik 	err = register_filesystem(&btrfs_fs_type);
2272dc11dd5dSJosef Bacik 	if (err)
2273dc11dd5dSJosef Bacik 		goto unregister_ioctl;
227474255aa0SJosef Bacik 
22752f4cbe64SWyatt Banks 	return 0;
22762f4cbe64SWyatt Banks 
2277a9218f6bSChris Mason unregister_ioctl:
2278a9218f6bSChris Mason 	btrfs_interface_exit();
227997eb6b69SDavid Sterba free_end_io_wq:
228097eb6b69SDavid Sterba 	btrfs_end_io_wq_exit();
2281b9e9a6cbSWang Shilong free_prelim_ref:
2282b9e9a6cbSWang Shilong 	btrfs_prelim_ref_exit();
228378a6184aSMiao Xie free_delayed_ref:
228478a6184aSMiao Xie 	btrfs_delayed_ref_exit();
22859247f317SMiao Xie free_auto_defrag:
22869247f317SMiao Xie 	btrfs_auto_defrag_exit();
228716cdcec7SMiao Xie free_delayed_inode:
228816cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
22896352b91dSMiao Xie free_ordered_data:
22906352b91dSMiao Xie 	ordered_data_exit();
22912f4cbe64SWyatt Banks free_extent_map:
22922f4cbe64SWyatt Banks 	extent_map_exit();
2293d1310b2eSChris Mason free_extent_io:
2294d1310b2eSChris Mason 	extent_io_exit();
22952f4cbe64SWyatt Banks free_cachep:
22962f4cbe64SWyatt Banks 	btrfs_destroy_cachep();
2297261507a0SLi Zefan free_compress:
2298261507a0SLi Zefan 	btrfs_exit_compress();
22992f4cbe64SWyatt Banks 	btrfs_exit_sysfs();
230014a958e6SFilipe David Borba Manana free_hash:
230114a958e6SFilipe David Borba Manana 	btrfs_hash_exit();
23022c90e5d6SChris Mason 	return err;
23032e635a27SChris Mason }
23042e635a27SChris Mason 
23052e635a27SChris Mason static void __exit exit_btrfs_fs(void)
23062e635a27SChris Mason {
230739279cc3SChris Mason 	btrfs_destroy_cachep();
230878a6184aSMiao Xie 	btrfs_delayed_ref_exit();
23099247f317SMiao Xie 	btrfs_auto_defrag_exit();
231016cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
2311b9e9a6cbSWang Shilong 	btrfs_prelim_ref_exit();
23126352b91dSMiao Xie 	ordered_data_exit();
2313a52d9a80SChris Mason 	extent_map_exit();
2314d1310b2eSChris Mason 	extent_io_exit();
2315a9218f6bSChris Mason 	btrfs_interface_exit();
23165ed5f588SJosef Bacik 	btrfs_end_io_wq_exit();
23172e635a27SChris Mason 	unregister_filesystem(&btrfs_fs_type);
231858176a96SJosef Bacik 	btrfs_exit_sysfs();
23198a4b83ccSChris Mason 	btrfs_cleanup_fs_uuids();
2320261507a0SLi Zefan 	btrfs_exit_compress();
232114a958e6SFilipe David Borba Manana 	btrfs_hash_exit();
23222e635a27SChris Mason }
23232e635a27SChris Mason 
232460efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs);
23252e635a27SChris Mason module_exit(exit_btrfs_fs)
23262e635a27SChris Mason 
23272e635a27SChris Mason MODULE_LICENSE("GPL");
2328