xref: /openbmc/linux/fs/btrfs/super.c (revision a7e252af)
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"
515103e947SJosef Bacik #include "xattr.h"
528a4b83ccSChris Mason #include "volumes.h"
53be6e8dc0SBalaji Rao #include "export.h"
54c8b97818SChris Mason #include "compression.h"
559c5085c1SJosef Bacik #include "rcu-string.h"
568dabb742SStefan Behrens #include "dev-replace.h"
5774255aa0SJosef Bacik #include "free-space-cache.h"
58b9e9a6cbSWang Shilong #include "backref.h"
59dc11dd5dSJosef Bacik #include "tests/btrfs-tests.h"
602e635a27SChris Mason 
611abe9b8aSliubo #define CREATE_TRACE_POINTS
621abe9b8aSliubo #include <trace/events/btrfs.h>
631abe9b8aSliubo 
64b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops;
65830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type;
66e20d96d6SChris Mason 
6708748810SDavid Sterba static const char *btrfs_decode_error(int errno)
68acce952bSliubo {
6908748810SDavid Sterba 	char *errstr = "unknown";
70acce952bSliubo 
71acce952bSliubo 	switch (errno) {
72acce952bSliubo 	case -EIO:
73acce952bSliubo 		errstr = "IO failure";
74acce952bSliubo 		break;
75acce952bSliubo 	case -ENOMEM:
76acce952bSliubo 		errstr = "Out of memory";
77acce952bSliubo 		break;
78acce952bSliubo 	case -EROFS:
79acce952bSliubo 		errstr = "Readonly filesystem";
80acce952bSliubo 		break;
818c342930SJeff Mahoney 	case -EEXIST:
828c342930SJeff Mahoney 		errstr = "Object already exists";
838c342930SJeff Mahoney 		break;
8494ef7280SDavid Sterba 	case -ENOSPC:
8594ef7280SDavid Sterba 		errstr = "No space left";
8694ef7280SDavid Sterba 		break;
8794ef7280SDavid Sterba 	case -ENOENT:
8894ef7280SDavid Sterba 		errstr = "No such entry";
8994ef7280SDavid Sterba 		break;
90acce952bSliubo 	}
91acce952bSliubo 
92acce952bSliubo 	return errstr;
93acce952bSliubo }
94acce952bSliubo 
95bbece8a3SDavid Sterba static void save_error_info(struct btrfs_fs_info *fs_info)
96acce952bSliubo {
97acce952bSliubo 	/*
98acce952bSliubo 	 * today we only save the error info into ram.  Long term we'll
99acce952bSliubo 	 * also send it down to the disk
100acce952bSliubo 	 */
10187533c47SMiao Xie 	set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
102acce952bSliubo }
103acce952bSliubo 
104acce952bSliubo /* btrfs handle error by forcing the filesystem readonly */
105acce952bSliubo static void btrfs_handle_error(struct btrfs_fs_info *fs_info)
106acce952bSliubo {
107acce952bSliubo 	struct super_block *sb = fs_info->sb;
108acce952bSliubo 
109acce952bSliubo 	if (sb->s_flags & MS_RDONLY)
110acce952bSliubo 		return;
111acce952bSliubo 
11287533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
113acce952bSliubo 		sb->s_flags |= MS_RDONLY;
114c2cf52ebSSimon Kirby 		btrfs_info(fs_info, "forced readonly");
1151acd6831SStefan Behrens 		/*
1161acd6831SStefan Behrens 		 * Note that a running device replace operation is not
1171acd6831SStefan Behrens 		 * canceled here although there is no way to update
1181acd6831SStefan Behrens 		 * the progress. It would add the risk of a deadlock,
1191acd6831SStefan Behrens 		 * therefore the canceling is ommited. The only penalty
1201acd6831SStefan Behrens 		 * is that some I/O remains active until the procedure
1211acd6831SStefan Behrens 		 * completes. The next time when the filesystem is
1221acd6831SStefan Behrens 		 * mounted writeable again, the device replace
1231acd6831SStefan Behrens 		 * operation continues.
1241acd6831SStefan Behrens 		 */
125acce952bSliubo 	}
126acce952bSliubo }
127acce952bSliubo 
128533574c6SJoe Perches #ifdef CONFIG_PRINTK
129acce952bSliubo /*
130acce952bSliubo  * __btrfs_std_error decodes expected errors from the caller and
131acce952bSliubo  * invokes the approciate error response.
132acce952bSliubo  */
133acce952bSliubo void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
1344da35113SJeff Mahoney 		       unsigned int line, int errno, const char *fmt, ...)
135acce952bSliubo {
136acce952bSliubo 	struct super_block *sb = fs_info->sb;
137acce952bSliubo 	const char *errstr;
138acce952bSliubo 
139acce952bSliubo 	/*
140acce952bSliubo 	 * Special case: if the error is EROFS, and we're already
141acce952bSliubo 	 * under MS_RDONLY, then it is safe here.
142acce952bSliubo 	 */
143acce952bSliubo 	if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
144acce952bSliubo   		return;
145acce952bSliubo 
14608748810SDavid Sterba 	errstr = btrfs_decode_error(errno);
1474da35113SJeff Mahoney 	if (fmt) {
14837252a66SEric Sandeen 		struct va_format vaf;
14937252a66SEric Sandeen 		va_list args;
15037252a66SEric Sandeen 
15137252a66SEric Sandeen 		va_start(args, fmt);
15237252a66SEric Sandeen 		vaf.fmt = fmt;
15337252a66SEric Sandeen 		vaf.va = &args;
1544da35113SJeff Mahoney 
15508748810SDavid Sterba 		printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: errno=%d %s (%pV)\n",
15608748810SDavid Sterba 			sb->s_id, function, line, errno, errstr, &vaf);
15737252a66SEric Sandeen 		va_end(args);
1584da35113SJeff Mahoney 	} else {
15908748810SDavid Sterba 		printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: errno=%d %s\n",
16008748810SDavid Sterba 			sb->s_id, function, line, errno, errstr);
1614da35113SJeff Mahoney 	}
162acce952bSliubo 
1634da35113SJeff Mahoney 	/* Don't go through full error handling during mount */
1644da35113SJeff Mahoney 	save_error_info(fs_info);
165cf79ffb5SJosef Bacik 	if (sb->s_flags & MS_BORN)
166acce952bSliubo 		btrfs_handle_error(fs_info);
167acce952bSliubo }
1684da35113SJeff Mahoney 
169533574c6SJoe Perches static const char * const logtypes[] = {
1704da35113SJeff Mahoney 	"emergency",
1714da35113SJeff Mahoney 	"alert",
1724da35113SJeff Mahoney 	"critical",
1734da35113SJeff Mahoney 	"error",
1744da35113SJeff Mahoney 	"warning",
1754da35113SJeff Mahoney 	"notice",
1764da35113SJeff Mahoney 	"info",
1774da35113SJeff Mahoney 	"debug",
1784da35113SJeff Mahoney };
1794da35113SJeff Mahoney 
180c2cf52ebSSimon Kirby void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
1814da35113SJeff Mahoney {
1824da35113SJeff Mahoney 	struct super_block *sb = fs_info->sb;
1834da35113SJeff Mahoney 	char lvl[4];
1844da35113SJeff Mahoney 	struct va_format vaf;
1854da35113SJeff Mahoney 	va_list args;
1864da35113SJeff Mahoney 	const char *type = logtypes[4];
187533574c6SJoe Perches 	int kern_level;
1884da35113SJeff Mahoney 
1894da35113SJeff Mahoney 	va_start(args, fmt);
1904da35113SJeff Mahoney 
191533574c6SJoe Perches 	kern_level = printk_get_level(fmt);
192533574c6SJoe Perches 	if (kern_level) {
193533574c6SJoe Perches 		size_t size = printk_skip_level(fmt) - fmt;
194533574c6SJoe Perches 		memcpy(lvl, fmt,  size);
195533574c6SJoe Perches 		lvl[size] = '\0';
196533574c6SJoe Perches 		fmt += size;
197533574c6SJoe Perches 		type = logtypes[kern_level - '0'];
1984da35113SJeff Mahoney 	} else
1994da35113SJeff Mahoney 		*lvl = '\0';
2004da35113SJeff Mahoney 
2014da35113SJeff Mahoney 	vaf.fmt = fmt;
2024da35113SJeff Mahoney 	vaf.va = &args;
203533574c6SJoe Perches 
204c2cf52ebSSimon Kirby 	printk("%sBTRFS %s (device %s): %pV\n", lvl, type, sb->s_id, &vaf);
205533574c6SJoe Perches 
206533574c6SJoe Perches 	va_end(args);
2074da35113SJeff Mahoney }
208acce952bSliubo 
209533574c6SJoe Perches #else
210533574c6SJoe Perches 
211533574c6SJoe Perches void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
212533574c6SJoe Perches 		       unsigned int line, int errno, const char *fmt, ...)
213533574c6SJoe Perches {
214533574c6SJoe Perches 	struct super_block *sb = fs_info->sb;
215533574c6SJoe Perches 
216533574c6SJoe Perches 	/*
217533574c6SJoe Perches 	 * Special case: if the error is EROFS, and we're already
218533574c6SJoe Perches 	 * under MS_RDONLY, then it is safe here.
219533574c6SJoe Perches 	 */
220533574c6SJoe Perches 	if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
221533574c6SJoe Perches 		return;
222533574c6SJoe Perches 
223533574c6SJoe Perches 	/* Don't go through full error handling during mount */
224533574c6SJoe Perches 	if (sb->s_flags & MS_BORN) {
225533574c6SJoe Perches 		save_error_info(fs_info);
226533574c6SJoe Perches 		btrfs_handle_error(fs_info);
227533574c6SJoe Perches 	}
228533574c6SJoe Perches }
229533574c6SJoe Perches #endif
230533574c6SJoe Perches 
2318c342930SJeff Mahoney /*
23249b25e05SJeff Mahoney  * We only mark the transaction aborted and then set the file system read-only.
23349b25e05SJeff Mahoney  * This will prevent new transactions from starting or trying to join this
23449b25e05SJeff Mahoney  * one.
23549b25e05SJeff Mahoney  *
23649b25e05SJeff Mahoney  * This means that error recovery at the call site is limited to freeing
23749b25e05SJeff Mahoney  * any local memory allocations and passing the error code up without
23849b25e05SJeff Mahoney  * further cleanup. The transaction should complete as it normally would
23949b25e05SJeff Mahoney  * in the call path but will return -EIO.
24049b25e05SJeff Mahoney  *
24149b25e05SJeff Mahoney  * We'll complete the cleanup in btrfs_end_transaction and
24249b25e05SJeff Mahoney  * btrfs_commit_transaction.
24349b25e05SJeff Mahoney  */
24449b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
24549b25e05SJeff Mahoney 			       struct btrfs_root *root, const char *function,
24649b25e05SJeff Mahoney 			       unsigned int line, int errno)
24749b25e05SJeff Mahoney {
24808748810SDavid Sterba 	/*
24908748810SDavid Sterba 	 * Report first abort since mount
25008748810SDavid Sterba 	 */
25108748810SDavid Sterba 	if (!test_and_set_bit(BTRFS_FS_STATE_TRANS_ABORTED,
25208748810SDavid Sterba 				&root->fs_info->fs_state)) {
25308748810SDavid Sterba 		WARN(1, KERN_DEBUG "btrfs: Transaction aborted (error %d)\n",
25408748810SDavid Sterba 				errno);
25508748810SDavid Sterba 	}
25649b25e05SJeff Mahoney 	trans->aborted = errno;
25749b25e05SJeff Mahoney 	/* Nothing used. The other threads that have joined this
25849b25e05SJeff Mahoney 	 * transaction may be able to continue. */
25949b25e05SJeff Mahoney 	if (!trans->blocks_used) {
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  */
2788c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
2798c342930SJeff Mahoney 		   unsigned int line, int errno, const char *fmt, ...)
2808c342930SJeff Mahoney {
2818c342930SJeff Mahoney 	char *s_id = "<unknown>";
2828c342930SJeff Mahoney 	const char *errstr;
2838c342930SJeff Mahoney 	struct va_format vaf = { .fmt = fmt };
2848c342930SJeff Mahoney 	va_list args;
2858c342930SJeff Mahoney 
2868c342930SJeff Mahoney 	if (fs_info)
2878c342930SJeff Mahoney 		s_id = fs_info->sb->s_id;
2888c342930SJeff Mahoney 
2898c342930SJeff Mahoney 	va_start(args, fmt);
2908c342930SJeff Mahoney 	vaf.va = &args;
2918c342930SJeff Mahoney 
29208748810SDavid Sterba 	errstr = btrfs_decode_error(errno);
293aa43a17cSEric Sandeen 	if (fs_info && (fs_info->mount_opt & BTRFS_MOUNT_PANIC_ON_FATAL_ERROR))
29408748810SDavid Sterba 		panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
29508748810SDavid Sterba 			s_id, function, line, &vaf, errno, errstr);
2968c342930SJeff Mahoney 
29708748810SDavid Sterba 	printk(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
29808748810SDavid Sterba 	       s_id, function, line, &vaf, errno, errstr);
2998c342930SJeff Mahoney 	va_end(args);
3008c342930SJeff Mahoney 	/* Caller calls BUG() */
3018c342930SJeff Mahoney }
302e20d96d6SChris Mason 
303e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb)
304e20d96d6SChris Mason {
305815745cfSAl Viro 	(void)close_ctree(btrfs_sb(sb)->tree_root);
306aea52e19SAl Viro 	/* FIXME: need to fix VFS to return error? */
307aea52e19SAl Viro 	/* AV: return it _where_?  ->put_super() can be triggered by any number
308aea52e19SAl Viro 	 * of async events, up to and including delivery of SIGKILL to the
309aea52e19SAl Viro 	 * last process that kept it busy.  Or segfault in the aforementioned
310aea52e19SAl Viro 	 * process...  Whom would you report that to?
311aea52e19SAl Viro 	 */
312e20d96d6SChris Mason }
3132e635a27SChris Mason 
31495e05289SChris Mason enum {
31573f73415SJosef Bacik 	Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
316287a0ab9SJosef Bacik 	Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
317287a0ab9SJosef Bacik 	Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
318261507a0SLi Zefan 	Opt_compress_type, Opt_compress_force, Opt_compress_force_type,
319261507a0SLi Zefan 	Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard,
32091435650SChris Mason 	Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed,
3219555c6c1SIlya Dryomov 	Opt_enospc_debug, Opt_subvolrootid, Opt_defrag, Opt_inode_cache,
3229555c6c1SIlya Dryomov 	Opt_no_space_cache, Opt_recovery, Opt_skip_balance,
32321adbd5cSStefan Behrens 	Opt_check_integrity, Opt_check_integrity_including_extent_data,
324f420ee1eSStefan Behrens 	Opt_check_integrity_print_mask, Opt_fatal_errors, Opt_rescan_uuid_tree,
3258b87dc17SDavid Sterba 	Opt_commit_interval,
3269555c6c1SIlya Dryomov 	Opt_err,
32795e05289SChris Mason };
32895e05289SChris Mason 
32995e05289SChris Mason static match_table_t tokens = {
330dfe25020SChris Mason 	{Opt_degraded, "degraded"},
33195e05289SChris Mason 	{Opt_subvol, "subvol=%s"},
3321493381fSWang Shilong 	{Opt_subvolid, "subvolid=%s"},
33343e570b0SChristoph Hellwig 	{Opt_device, "device=%s"},
334b6cda9bcSChris Mason 	{Opt_nodatasum, "nodatasum"},
335be20aa9dSChris Mason 	{Opt_nodatacow, "nodatacow"},
33621ad10cfSChris Mason 	{Opt_nobarrier, "nobarrier"},
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"},
34733268eafSJosef Bacik 	{Opt_noacl, "noacl"},
3483a5e1404SSage Weil 	{Opt_notreelog, "notreelog"},
349dccae999SSage Weil 	{Opt_flushoncommit, "flushoncommit"},
35097e728d4SJosef Bacik 	{Opt_ratio, "metadata_ratio=%d"},
351e244a0aeSChristoph Hellwig 	{Opt_discard, "discard"},
3520af3d00bSJosef Bacik 	{Opt_space_cache, "space_cache"},
35388c2ba3bSJosef Bacik 	{Opt_clear_cache, "clear_cache"},
3544260f7c7SSage Weil 	{Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
35591435650SChris Mason 	{Opt_enospc_debug, "enospc_debug"},
356e15d0542SXin Zhong 	{Opt_subvolrootid, "subvolrootid=%d"},
3574cb5300bSChris Mason 	{Opt_defrag, "autodefrag"},
3584b9465cbSChris Mason 	{Opt_inode_cache, "inode_cache"},
3598965593eSDavid Sterba 	{Opt_no_space_cache, "nospace_cache"},
360af31f5e5SChris Mason 	{Opt_recovery, "recovery"},
3619555c6c1SIlya Dryomov 	{Opt_skip_balance, "skip_balance"},
36221adbd5cSStefan Behrens 	{Opt_check_integrity, "check_int"},
36321adbd5cSStefan Behrens 	{Opt_check_integrity_including_extent_data, "check_int_data"},
36421adbd5cSStefan Behrens 	{Opt_check_integrity_print_mask, "check_int_print_mask=%d"},
365f420ee1eSStefan Behrens 	{Opt_rescan_uuid_tree, "rescan_uuid_tree"},
3668c342930SJeff Mahoney 	{Opt_fatal_errors, "fatal_errors=%s"},
3678b87dc17SDavid Sterba 	{Opt_commit_interval, "commit=%d"},
36833268eafSJosef Bacik 	{Opt_err, NULL},
36995e05289SChris Mason };
37095e05289SChris Mason 
371edf24abeSChristoph Hellwig /*
372edf24abeSChristoph Hellwig  * Regular mount options parser.  Everything that is needed only when
373edf24abeSChristoph Hellwig  * reading in a new superblock is parsed here.
37449b25e05SJeff Mahoney  * XXX JDM: This needs to be cleaned up for remount.
375edf24abeSChristoph Hellwig  */
376edf24abeSChristoph Hellwig int btrfs_parse_options(struct btrfs_root *root, char *options)
37795e05289SChris Mason {
378edf24abeSChristoph Hellwig 	struct btrfs_fs_info *info = root->fs_info;
37995e05289SChris Mason 	substring_t args[MAX_OPT_ARGS];
38073bc1876SJosef Bacik 	char *p, *num, *orig = NULL;
38173bc1876SJosef Bacik 	u64 cache_gen;
3824543df7eSChris Mason 	int intarg;
383a7a3f7caSSage Weil 	int ret = 0;
384261507a0SLi Zefan 	char *compress_type;
385261507a0SLi Zefan 	bool compress_force = false;
386b6cda9bcSChris Mason 
3876c41761fSDavid Sterba 	cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
38873bc1876SJosef Bacik 	if (cache_gen)
38973bc1876SJosef Bacik 		btrfs_set_opt(info->mount_opt, SPACE_CACHE);
39073bc1876SJosef Bacik 
39195e05289SChris Mason 	if (!options)
39273bc1876SJosef Bacik 		goto out;
39395e05289SChris Mason 
394be20aa9dSChris Mason 	/*
395be20aa9dSChris Mason 	 * strsep changes the string, duplicate it because parse_options
396be20aa9dSChris Mason 	 * gets called twice
397be20aa9dSChris Mason 	 */
398be20aa9dSChris Mason 	options = kstrdup(options, GFP_NOFS);
399be20aa9dSChris Mason 	if (!options)
400be20aa9dSChris Mason 		return -ENOMEM;
401be20aa9dSChris Mason 
402da495eccSJosef Bacik 	orig = options;
403be20aa9dSChris Mason 
40495e05289SChris Mason 	while ((p = strsep(&options, ",")) != NULL) {
40595e05289SChris Mason 		int token;
40695e05289SChris Mason 		if (!*p)
40795e05289SChris Mason 			continue;
40895e05289SChris Mason 
40995e05289SChris Mason 		token = match_token(p, tokens, args);
41095e05289SChris Mason 		switch (token) {
411dfe25020SChris Mason 		case Opt_degraded:
412edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: allowing degraded mounts\n");
413dfe25020SChris Mason 			btrfs_set_opt(info->mount_opt, DEGRADED);
414dfe25020SChris Mason 			break;
41595e05289SChris Mason 		case Opt_subvol:
41673f73415SJosef Bacik 		case Opt_subvolid:
417e15d0542SXin Zhong 		case Opt_subvolrootid:
41843e570b0SChristoph Hellwig 		case Opt_device:
419edf24abeSChristoph Hellwig 			/*
42043e570b0SChristoph Hellwig 			 * These are parsed by btrfs_parse_early_options
421edf24abeSChristoph Hellwig 			 * and can be happily ignored here.
422edf24abeSChristoph Hellwig 			 */
42395e05289SChris Mason 			break;
424b6cda9bcSChris Mason 		case Opt_nodatasum:
425067c28adSChris Mason 			printk(KERN_INFO "btrfs: setting nodatasum\n");
426b6cda9bcSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
427be20aa9dSChris Mason 			break;
428be20aa9dSChris Mason 		case Opt_nodatacow:
429bedb2ccaSAndrei Popa 			if (!btrfs_test_opt(root, COMPRESS) ||
430bedb2ccaSAndrei Popa 				!btrfs_test_opt(root, FORCE_COMPRESS)) {
431bedb2ccaSAndrei Popa 					printk(KERN_INFO "btrfs: setting nodatacow, compression disabled\n");
432bedb2ccaSAndrei Popa 			} else {
433edf24abeSChristoph Hellwig 				printk(KERN_INFO "btrfs: setting nodatacow\n");
434bedb2ccaSAndrei Popa 			}
435bedb2ccaSAndrei Popa 			btrfs_clear_opt(info->mount_opt, COMPRESS);
436bedb2ccaSAndrei Popa 			btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
437be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATACOW);
438be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
439b6cda9bcSChris Mason 			break;
440a555f810SChris Mason 		case Opt_compress_force:
441261507a0SLi Zefan 		case Opt_compress_force_type:
442261507a0SLi Zefan 			compress_force = true;
4431c697d4aSEric Sandeen 			/* Fallthrough */
444261507a0SLi Zefan 		case Opt_compress:
445261507a0SLi Zefan 		case Opt_compress_type:
446261507a0SLi Zefan 			if (token == Opt_compress ||
447261507a0SLi Zefan 			    token == Opt_compress_force ||
448261507a0SLi Zefan 			    strcmp(args[0].from, "zlib") == 0) {
449261507a0SLi Zefan 				compress_type = "zlib";
450261507a0SLi Zefan 				info->compress_type = BTRFS_COMPRESS_ZLIB;
451063849eaSArnd Hannemann 				btrfs_set_opt(info->mount_opt, COMPRESS);
452bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATACOW);
453bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATASUM);
454a6fa6faeSLi Zefan 			} else if (strcmp(args[0].from, "lzo") == 0) {
455a6fa6faeSLi Zefan 				compress_type = "lzo";
456a6fa6faeSLi Zefan 				info->compress_type = BTRFS_COMPRESS_LZO;
457063849eaSArnd Hannemann 				btrfs_set_opt(info->mount_opt, COMPRESS);
458bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATACOW);
459bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATASUM);
4602b0ce2c2SMitch Harder 				btrfs_set_fs_incompat(info, COMPRESS_LZO);
461063849eaSArnd Hannemann 			} else if (strncmp(args[0].from, "no", 2) == 0) {
462063849eaSArnd Hannemann 				compress_type = "no";
463063849eaSArnd Hannemann 				btrfs_clear_opt(info->mount_opt, COMPRESS);
464063849eaSArnd Hannemann 				btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
465063849eaSArnd Hannemann 				compress_force = false;
466261507a0SLi Zefan 			} else {
467261507a0SLi Zefan 				ret = -EINVAL;
468261507a0SLi Zefan 				goto out;
469261507a0SLi Zefan 			}
470261507a0SLi Zefan 
471261507a0SLi Zefan 			if (compress_force) {
472261507a0SLi Zefan 				btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
473261507a0SLi Zefan 				pr_info("btrfs: force %s compression\n",
474261507a0SLi Zefan 					compress_type);
475*a7e252afSMiao Xie 			} else if (btrfs_test_opt(root, COMPRESS)) {
476261507a0SLi Zefan 				pr_info("btrfs: use %s compression\n",
477261507a0SLi Zefan 					compress_type);
478*a7e252afSMiao Xie 			}
479a555f810SChris Mason 			break;
480e18e4809SChris Mason 		case Opt_ssd:
481edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
482e18e4809SChris Mason 			btrfs_set_opt(info->mount_opt, SSD);
483e18e4809SChris Mason 			break;
484451d7585SChris Mason 		case Opt_ssd_spread:
485451d7585SChris Mason 			printk(KERN_INFO "btrfs: use spread ssd "
486451d7585SChris Mason 			       "allocation scheme\n");
487451d7585SChris Mason 			btrfs_set_opt(info->mount_opt, SSD);
488451d7585SChris Mason 			btrfs_set_opt(info->mount_opt, SSD_SPREAD);
489451d7585SChris Mason 			break;
4903b30c22fSChris Mason 		case Opt_nossd:
491451d7585SChris Mason 			printk(KERN_INFO "btrfs: not using ssd allocation "
492451d7585SChris Mason 			       "scheme\n");
493c289811cSChris Mason 			btrfs_set_opt(info->mount_opt, NOSSD);
4943b30c22fSChris Mason 			btrfs_clear_opt(info->mount_opt, SSD);
495451d7585SChris Mason 			btrfs_clear_opt(info->mount_opt, SSD_SPREAD);
4963b30c22fSChris Mason 			break;
49721ad10cfSChris Mason 		case Opt_nobarrier:
498edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: turning off barriers\n");
49921ad10cfSChris Mason 			btrfs_set_opt(info->mount_opt, NOBARRIER);
50021ad10cfSChris Mason 			break;
5014543df7eSChris Mason 		case Opt_thread_pool:
5022c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
5032c334e87SWang Shilong 			if (ret) {
5042c334e87SWang Shilong 				goto out;
5052c334e87SWang Shilong 			} else if (intarg > 0) {
5064543df7eSChris Mason 				info->thread_pool_size = intarg;
5072c334e87SWang Shilong 			} else {
5082c334e87SWang Shilong 				ret = -EINVAL;
5092c334e87SWang Shilong 				goto out;
5102c334e87SWang Shilong 			}
5114543df7eSChris Mason 			break;
5126f568d35SChris Mason 		case Opt_max_inline:
513edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
5146f568d35SChris Mason 			if (num) {
51591748467SAkinobu Mita 				info->max_inline = memparse(num, NULL);
5166f568d35SChris Mason 				kfree(num);
5176f568d35SChris Mason 
51815ada040SChris Mason 				if (info->max_inline) {
5196f568d35SChris Mason 					info->max_inline = max_t(u64,
52015ada040SChris Mason 						info->max_inline,
52115ada040SChris Mason 						root->sectorsize);
52215ada040SChris Mason 				}
523edf24abeSChristoph Hellwig 				printk(KERN_INFO "btrfs: max_inline at %llu\n",
524c1c9ff7cSGeert Uytterhoeven 					info->max_inline);
5252c334e87SWang Shilong 			} else {
5262c334e87SWang Shilong 				ret = -ENOMEM;
5272c334e87SWang Shilong 				goto out;
5286f568d35SChris Mason 			}
5296f568d35SChris Mason 			break;
5308f662a76SChris Mason 		case Opt_alloc_start:
531edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
5328f662a76SChris Mason 			if (num) {
533c018daecSMiao Xie 				mutex_lock(&info->chunk_mutex);
53491748467SAkinobu Mita 				info->alloc_start = memparse(num, NULL);
535c018daecSMiao Xie 				mutex_unlock(&info->chunk_mutex);
5368f662a76SChris Mason 				kfree(num);
537edf24abeSChristoph Hellwig 				printk(KERN_INFO
538edf24abeSChristoph Hellwig 					"btrfs: allocations start at %llu\n",
539c1c9ff7cSGeert Uytterhoeven 					info->alloc_start);
5402c334e87SWang Shilong 			} else {
5412c334e87SWang Shilong 				ret = -ENOMEM;
5422c334e87SWang Shilong 				goto out;
5438f662a76SChris Mason 			}
5448f662a76SChris Mason 			break;
54533268eafSJosef Bacik 		case Opt_noacl:
54633268eafSJosef Bacik 			root->fs_info->sb->s_flags &= ~MS_POSIXACL;
54733268eafSJosef Bacik 			break;
5483a5e1404SSage Weil 		case Opt_notreelog:
5493a5e1404SSage Weil 			printk(KERN_INFO "btrfs: disabling tree log\n");
5503a5e1404SSage Weil 			btrfs_set_opt(info->mount_opt, NOTREELOG);
5513a5e1404SSage Weil 			break;
552dccae999SSage Weil 		case Opt_flushoncommit:
553dccae999SSage Weil 			printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
554dccae999SSage Weil 			btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
555dccae999SSage Weil 			break;
55697e728d4SJosef Bacik 		case Opt_ratio:
5572c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
5582c334e87SWang Shilong 			if (ret) {
5592c334e87SWang Shilong 				goto out;
5602c334e87SWang Shilong 			} else if (intarg >= 0) {
56197e728d4SJosef Bacik 				info->metadata_ratio = intarg;
56297e728d4SJosef Bacik 				printk(KERN_INFO "btrfs: metadata ratio %d\n",
56397e728d4SJosef Bacik 				       info->metadata_ratio);
5642c334e87SWang Shilong 			} else {
5652c334e87SWang Shilong 				ret = -EINVAL;
5662c334e87SWang Shilong 				goto out;
56797e728d4SJosef Bacik 			}
56897e728d4SJosef Bacik 			break;
569e244a0aeSChristoph Hellwig 		case Opt_discard:
570e244a0aeSChristoph Hellwig 			btrfs_set_opt(info->mount_opt, DISCARD);
571e244a0aeSChristoph Hellwig 			break;
5720af3d00bSJosef Bacik 		case Opt_space_cache:
5730af3d00bSJosef Bacik 			btrfs_set_opt(info->mount_opt, SPACE_CACHE);
5740de90876SJosef Bacik 			break;
575f420ee1eSStefan Behrens 		case Opt_rescan_uuid_tree:
576f420ee1eSStefan Behrens 			btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
577f420ee1eSStefan Behrens 			break;
57873bc1876SJosef Bacik 		case Opt_no_space_cache:
57973bc1876SJosef Bacik 			printk(KERN_INFO "btrfs: disabling disk space caching\n");
58073bc1876SJosef Bacik 			btrfs_clear_opt(info->mount_opt, SPACE_CACHE);
58173bc1876SJosef Bacik 			break;
5824b9465cbSChris Mason 		case Opt_inode_cache:
5834b9465cbSChris Mason 			printk(KERN_INFO "btrfs: enabling inode map caching\n");
5844b9465cbSChris Mason 			btrfs_set_opt(info->mount_opt, INODE_MAP_CACHE);
5854b9465cbSChris Mason 			break;
58688c2ba3bSJosef Bacik 		case Opt_clear_cache:
58788c2ba3bSJosef Bacik 			printk(KERN_INFO "btrfs: force clearing of disk cache\n");
58888c2ba3bSJosef Bacik 			btrfs_set_opt(info->mount_opt, CLEAR_CACHE);
5890af3d00bSJosef Bacik 			break;
5904260f7c7SSage Weil 		case Opt_user_subvol_rm_allowed:
5914260f7c7SSage Weil 			btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
5924260f7c7SSage Weil 			break;
59391435650SChris Mason 		case Opt_enospc_debug:
59491435650SChris Mason 			btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
59591435650SChris Mason 			break;
5964cb5300bSChris Mason 		case Opt_defrag:
59748940662SDaniel J Blueman 			printk(KERN_INFO "btrfs: enabling auto defrag\n");
5984cb5300bSChris Mason 			btrfs_set_opt(info->mount_opt, AUTO_DEFRAG);
5994cb5300bSChris Mason 			break;
600af31f5e5SChris Mason 		case Opt_recovery:
60148940662SDaniel J Blueman 			printk(KERN_INFO "btrfs: enabling auto recovery\n");
602af31f5e5SChris Mason 			btrfs_set_opt(info->mount_opt, RECOVERY);
603af31f5e5SChris Mason 			break;
6049555c6c1SIlya Dryomov 		case Opt_skip_balance:
6059555c6c1SIlya Dryomov 			btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
6069555c6c1SIlya Dryomov 			break;
60721adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
60821adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
60921adbd5cSStefan Behrens 			printk(KERN_INFO "btrfs: enabling check integrity"
61021adbd5cSStefan Behrens 			       " including extent data\n");
61121adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt,
61221adbd5cSStefan Behrens 				      CHECK_INTEGRITY_INCLUDING_EXTENT_DATA);
61321adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
61421adbd5cSStefan Behrens 			break;
61521adbd5cSStefan Behrens 		case Opt_check_integrity:
61621adbd5cSStefan Behrens 			printk(KERN_INFO "btrfs: enabling check integrity\n");
61721adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
61821adbd5cSStefan Behrens 			break;
61921adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
6202c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
6212c334e87SWang Shilong 			if (ret) {
6222c334e87SWang Shilong 				goto out;
6232c334e87SWang Shilong 			} else if (intarg >= 0) {
62421adbd5cSStefan Behrens 				info->check_integrity_print_mask = intarg;
62521adbd5cSStefan Behrens 				printk(KERN_INFO "btrfs:"
62621adbd5cSStefan Behrens 				       " check_integrity_print_mask 0x%x\n",
62721adbd5cSStefan Behrens 				       info->check_integrity_print_mask);
6282c334e87SWang Shilong 			} else {
6292c334e87SWang Shilong 				ret = -EINVAL;
6302c334e87SWang Shilong 				goto out;
63121adbd5cSStefan Behrens 			}
63221adbd5cSStefan Behrens 			break;
63321adbd5cSStefan Behrens #else
63421adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
63521adbd5cSStefan Behrens 		case Opt_check_integrity:
63621adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
63721adbd5cSStefan Behrens 			printk(KERN_ERR "btrfs: support for check_integrity*"
63821adbd5cSStefan Behrens 			       " not compiled in!\n");
63921adbd5cSStefan Behrens 			ret = -EINVAL;
64021adbd5cSStefan Behrens 			goto out;
64121adbd5cSStefan Behrens #endif
6428c342930SJeff Mahoney 		case Opt_fatal_errors:
6438c342930SJeff Mahoney 			if (strcmp(args[0].from, "panic") == 0)
6448c342930SJeff Mahoney 				btrfs_set_opt(info->mount_opt,
6458c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
6468c342930SJeff Mahoney 			else if (strcmp(args[0].from, "bug") == 0)
6478c342930SJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
6488c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
6498c342930SJeff Mahoney 			else {
6508c342930SJeff Mahoney 				ret = -EINVAL;
6518c342930SJeff Mahoney 				goto out;
6528c342930SJeff Mahoney 			}
6538c342930SJeff Mahoney 			break;
6548b87dc17SDavid Sterba 		case Opt_commit_interval:
6558b87dc17SDavid Sterba 			intarg = 0;
6568b87dc17SDavid Sterba 			ret = match_int(&args[0], &intarg);
6578b87dc17SDavid Sterba 			if (ret < 0) {
6588b87dc17SDavid Sterba 				printk(KERN_ERR
6598b87dc17SDavid Sterba 					"btrfs: invalid commit interval\n");
6608b87dc17SDavid Sterba 				ret = -EINVAL;
6618b87dc17SDavid Sterba 				goto out;
6628b87dc17SDavid Sterba 			}
6638b87dc17SDavid Sterba 			if (intarg > 0) {
6648b87dc17SDavid Sterba 				if (intarg > 300) {
6658b87dc17SDavid Sterba 					printk(KERN_WARNING
6668b87dc17SDavid Sterba 					    "btrfs: excessive commit interval %d\n",
6678b87dc17SDavid Sterba 							intarg);
6688b87dc17SDavid Sterba 				}
6698b87dc17SDavid Sterba 				info->commit_interval = intarg;
6708b87dc17SDavid Sterba 			} else {
6718b87dc17SDavid Sterba 				printk(KERN_INFO
6728b87dc17SDavid Sterba 				    "btrfs: using default commit interval %ds\n",
6738b87dc17SDavid Sterba 				    BTRFS_DEFAULT_COMMIT_INTERVAL);
6748b87dc17SDavid Sterba 				info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
6758b87dc17SDavid Sterba 			}
6768b87dc17SDavid Sterba 			break;
677a7a3f7caSSage Weil 		case Opt_err:
678a7a3f7caSSage Weil 			printk(KERN_INFO "btrfs: unrecognized mount option "
679a7a3f7caSSage Weil 			       "'%s'\n", p);
680a7a3f7caSSage Weil 			ret = -EINVAL;
681a7a3f7caSSage Weil 			goto out;
68295e05289SChris Mason 		default:
683be20aa9dSChris Mason 			break;
68495e05289SChris Mason 		}
68595e05289SChris Mason 	}
686a7a3f7caSSage Weil out:
68773bc1876SJosef Bacik 	if (!ret && btrfs_test_opt(root, SPACE_CACHE))
68873bc1876SJosef Bacik 		printk(KERN_INFO "btrfs: disk space caching is enabled\n");
689da495eccSJosef Bacik 	kfree(orig);
690a7a3f7caSSage Weil 	return ret;
691edf24abeSChristoph Hellwig }
692edf24abeSChristoph Hellwig 
693edf24abeSChristoph Hellwig /*
694edf24abeSChristoph Hellwig  * Parse mount options that are required early in the mount process.
695edf24abeSChristoph Hellwig  *
696edf24abeSChristoph Hellwig  * All other options will be parsed on much later in the mount process and
697edf24abeSChristoph Hellwig  * only when we need to allocate a new super block.
698edf24abeSChristoph Hellwig  */
69997288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags,
70073f73415SJosef Bacik 		void *holder, char **subvol_name, u64 *subvol_objectid,
7015e2a4b25SDavid Sterba 		struct btrfs_fs_devices **fs_devices)
702edf24abeSChristoph Hellwig {
703edf24abeSChristoph Hellwig 	substring_t args[MAX_OPT_ARGS];
70483c8c9bdSJeff Liu 	char *device_name, *opts, *orig, *p;
7051493381fSWang Shilong 	char *num = NULL;
706edf24abeSChristoph Hellwig 	int error = 0;
707edf24abeSChristoph Hellwig 
708edf24abeSChristoph Hellwig 	if (!options)
709830c4adbSJosef Bacik 		return 0;
710edf24abeSChristoph Hellwig 
711edf24abeSChristoph Hellwig 	/*
712edf24abeSChristoph Hellwig 	 * strsep changes the string, duplicate it because parse_options
713edf24abeSChristoph Hellwig 	 * gets called twice
714edf24abeSChristoph Hellwig 	 */
715edf24abeSChristoph Hellwig 	opts = kstrdup(options, GFP_KERNEL);
716edf24abeSChristoph Hellwig 	if (!opts)
717edf24abeSChristoph Hellwig 		return -ENOMEM;
7183f3d0bc0STero Roponen 	orig = opts;
719edf24abeSChristoph Hellwig 
720edf24abeSChristoph Hellwig 	while ((p = strsep(&opts, ",")) != NULL) {
721edf24abeSChristoph Hellwig 		int token;
722edf24abeSChristoph Hellwig 		if (!*p)
723edf24abeSChristoph Hellwig 			continue;
724edf24abeSChristoph Hellwig 
725edf24abeSChristoph Hellwig 		token = match_token(p, tokens, args);
726edf24abeSChristoph Hellwig 		switch (token) {
727edf24abeSChristoph Hellwig 		case Opt_subvol:
728a90e8b6fSIlya Dryomov 			kfree(*subvol_name);
729edf24abeSChristoph Hellwig 			*subvol_name = match_strdup(&args[0]);
7302c334e87SWang Shilong 			if (!*subvol_name) {
7312c334e87SWang Shilong 				error = -ENOMEM;
7322c334e87SWang Shilong 				goto out;
7332c334e87SWang Shilong 			}
734edf24abeSChristoph Hellwig 			break;
73573f73415SJosef Bacik 		case Opt_subvolid:
7361493381fSWang Shilong 			num = match_strdup(&args[0]);
7371493381fSWang Shilong 			if (num) {
7381493381fSWang Shilong 				*subvol_objectid = memparse(num, NULL);
7391493381fSWang Shilong 				kfree(num);
7404849f01dSJosef Bacik 				/* we want the original fs_tree */
7411493381fSWang Shilong 				if (!*subvol_objectid)
7424849f01dSJosef Bacik 					*subvol_objectid =
7434849f01dSJosef Bacik 						BTRFS_FS_TREE_OBJECTID;
7442c334e87SWang Shilong 			} else {
7452c334e87SWang Shilong 				error = -EINVAL;
7462c334e87SWang Shilong 				goto out;
7474849f01dSJosef Bacik 			}
74873f73415SJosef Bacik 			break;
749e15d0542SXin Zhong 		case Opt_subvolrootid:
7505e2a4b25SDavid Sterba 			printk(KERN_WARNING
7515e2a4b25SDavid Sterba 				"btrfs: 'subvolrootid' mount option is deprecated and has no effect\n");
752e15d0542SXin Zhong 			break;
75343e570b0SChristoph Hellwig 		case Opt_device:
75483c8c9bdSJeff Liu 			device_name = match_strdup(&args[0]);
75583c8c9bdSJeff Liu 			if (!device_name) {
75683c8c9bdSJeff Liu 				error = -ENOMEM;
75783c8c9bdSJeff Liu 				goto out;
75883c8c9bdSJeff Liu 			}
75983c8c9bdSJeff Liu 			error = btrfs_scan_one_device(device_name,
76043e570b0SChristoph Hellwig 					flags, holder, fs_devices);
76183c8c9bdSJeff Liu 			kfree(device_name);
76243e570b0SChristoph Hellwig 			if (error)
763830c4adbSJosef Bacik 				goto out;
76443e570b0SChristoph Hellwig 			break;
765edf24abeSChristoph Hellwig 		default:
766edf24abeSChristoph Hellwig 			break;
767edf24abeSChristoph Hellwig 		}
768edf24abeSChristoph Hellwig 	}
769edf24abeSChristoph Hellwig 
770edf24abeSChristoph Hellwig out:
771830c4adbSJosef Bacik 	kfree(orig);
772edf24abeSChristoph Hellwig 	return error;
77395e05289SChris Mason }
77495e05289SChris Mason 
77573f73415SJosef Bacik static struct dentry *get_default_root(struct super_block *sb,
77673f73415SJosef Bacik 				       u64 subvol_objectid)
77773f73415SJosef Bacik {
778815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
779815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
78073f73415SJosef Bacik 	struct btrfs_root *new_root;
78173f73415SJosef Bacik 	struct btrfs_dir_item *di;
78273f73415SJosef Bacik 	struct btrfs_path *path;
78373f73415SJosef Bacik 	struct btrfs_key location;
78473f73415SJosef Bacik 	struct inode *inode;
78573f73415SJosef Bacik 	u64 dir_id;
78673f73415SJosef Bacik 	int new = 0;
78773f73415SJosef Bacik 
78873f73415SJosef Bacik 	/*
78973f73415SJosef Bacik 	 * We have a specific subvol we want to mount, just setup location and
79073f73415SJosef Bacik 	 * go look up the root.
79173f73415SJosef Bacik 	 */
79273f73415SJosef Bacik 	if (subvol_objectid) {
79373f73415SJosef Bacik 		location.objectid = subvol_objectid;
79473f73415SJosef Bacik 		location.type = BTRFS_ROOT_ITEM_KEY;
79573f73415SJosef Bacik 		location.offset = (u64)-1;
79673f73415SJosef Bacik 		goto find_root;
79773f73415SJosef Bacik 	}
79873f73415SJosef Bacik 
79973f73415SJosef Bacik 	path = btrfs_alloc_path();
80073f73415SJosef Bacik 	if (!path)
80173f73415SJosef Bacik 		return ERR_PTR(-ENOMEM);
80273f73415SJosef Bacik 	path->leave_spinning = 1;
80373f73415SJosef Bacik 
80473f73415SJosef Bacik 	/*
80573f73415SJosef Bacik 	 * Find the "default" dir item which points to the root item that we
80673f73415SJosef Bacik 	 * will mount by default if we haven't been given a specific subvolume
80773f73415SJosef Bacik 	 * to mount.
80873f73415SJosef Bacik 	 */
809815745cfSAl Viro 	dir_id = btrfs_super_root_dir(fs_info->super_copy);
81073f73415SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
811b0839166SJulia Lawall 	if (IS_ERR(di)) {
812b0839166SJulia Lawall 		btrfs_free_path(path);
813fb4f6f91SDan Carpenter 		return ERR_CAST(di);
814b0839166SJulia Lawall 	}
81573f73415SJosef Bacik 	if (!di) {
81673f73415SJosef Bacik 		/*
81773f73415SJosef Bacik 		 * Ok the default dir item isn't there.  This is weird since
81873f73415SJosef Bacik 		 * it's always been there, but don't freak out, just try and
81973f73415SJosef Bacik 		 * mount to root most subvolume.
82073f73415SJosef Bacik 		 */
82173f73415SJosef Bacik 		btrfs_free_path(path);
82273f73415SJosef Bacik 		dir_id = BTRFS_FIRST_FREE_OBJECTID;
823815745cfSAl Viro 		new_root = fs_info->fs_root;
82473f73415SJosef Bacik 		goto setup_root;
82573f73415SJosef Bacik 	}
82673f73415SJosef Bacik 
82773f73415SJosef Bacik 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
82873f73415SJosef Bacik 	btrfs_free_path(path);
82973f73415SJosef Bacik 
83073f73415SJosef Bacik find_root:
831815745cfSAl Viro 	new_root = btrfs_read_fs_root_no_name(fs_info, &location);
83273f73415SJosef Bacik 	if (IS_ERR(new_root))
833d0b678cbSJulia Lawall 		return ERR_CAST(new_root);
83473f73415SJosef Bacik 
83573f73415SJosef Bacik 	dir_id = btrfs_root_dirid(&new_root->root_item);
83673f73415SJosef Bacik setup_root:
83773f73415SJosef Bacik 	location.objectid = dir_id;
83873f73415SJosef Bacik 	location.type = BTRFS_INODE_ITEM_KEY;
83973f73415SJosef Bacik 	location.offset = 0;
84073f73415SJosef Bacik 
84173f73415SJosef Bacik 	inode = btrfs_iget(sb, &location, new_root, &new);
8424cbd1149SDan Carpenter 	if (IS_ERR(inode))
8434cbd1149SDan Carpenter 		return ERR_CAST(inode);
84473f73415SJosef Bacik 
84573f73415SJosef Bacik 	/*
84673f73415SJosef Bacik 	 * If we're just mounting the root most subvol put the inode and return
84773f73415SJosef Bacik 	 * a reference to the dentry.  We will have already gotten a reference
84873f73415SJosef Bacik 	 * to the inode in btrfs_fill_super so we're good to go.
84973f73415SJosef Bacik 	 */
85073f73415SJosef Bacik 	if (!new && sb->s_root->d_inode == inode) {
85173f73415SJosef Bacik 		iput(inode);
85273f73415SJosef Bacik 		return dget(sb->s_root);
85373f73415SJosef Bacik 	}
85473f73415SJosef Bacik 
855ba5b8958SJosef Bacik 	return d_obtain_alias(inode);
85673f73415SJosef Bacik }
85773f73415SJosef Bacik 
8588a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb,
8598a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
8608a4b83ccSChris Mason 			    void *data, int silent)
8612e635a27SChris Mason {
8622e635a27SChris Mason 	struct inode *inode;
863815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
8645d4f98a2SYan Zheng 	struct btrfs_key key;
86539279cc3SChris Mason 	int err;
8662e635a27SChris Mason 
8672e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
8682e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
869e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
870af53d29aSAl Viro 	sb->s_d_op = &btrfs_dentry_operations;
871be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
8725103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
8732e635a27SChris Mason 	sb->s_time_gran = 1;
8740eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL
87533268eafSJosef Bacik 	sb->s_flags |= MS_POSIXACL;
87649cf6f45SChris Ball #endif
8770c4d2d95SJosef Bacik 	sb->s_flags |= MS_I_VERSION;
878ad2b2c80SAl Viro 	err = open_ctree(sb, fs_devices, (char *)data);
879ad2b2c80SAl Viro 	if (err) {
880e20d96d6SChris Mason 		printk("btrfs: open_ctree failed\n");
881ad2b2c80SAl Viro 		return err;
882e20d96d6SChris Mason 	}
883b888db2bSChris Mason 
8845d4f98a2SYan Zheng 	key.objectid = BTRFS_FIRST_FREE_OBJECTID;
8855d4f98a2SYan Zheng 	key.type = BTRFS_INODE_ITEM_KEY;
8865d4f98a2SYan Zheng 	key.offset = 0;
88798c7089cSAl Viro 	inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL);
8885d4f98a2SYan Zheng 	if (IS_ERR(inode)) {
8895d4f98a2SYan Zheng 		err = PTR_ERR(inode);
89039279cc3SChris Mason 		goto fail_close;
89139279cc3SChris Mason 	}
8922e635a27SChris Mason 
89348fde701SAl Viro 	sb->s_root = d_make_root(inode);
89448fde701SAl Viro 	if (!sb->s_root) {
89539279cc3SChris Mason 		err = -ENOMEM;
89639279cc3SChris Mason 		goto fail_close;
8972e635a27SChris Mason 	}
89858176a96SJosef Bacik 
8996885f308SChris Mason 	save_mount_options(sb, data);
90090a887c9SDan Magenheimer 	cleancache_init_fs(sb);
90159553edfSAl Viro 	sb->s_flags |= MS_ACTIVE;
9022e635a27SChris Mason 	return 0;
9032e635a27SChris Mason 
90439279cc3SChris Mason fail_close:
905815745cfSAl Viro 	close_ctree(fs_info->tree_root);
906d5719762SChris Mason 	return err;
907d5719762SChris Mason }
908d5719762SChris Mason 
9096bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
910d5719762SChris Mason {
911d5719762SChris Mason 	struct btrfs_trans_handle *trans;
912815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
913815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
914df2ce34cSChris Mason 
9151abe9b8aSliubo 	trace_btrfs_sync_fs(wait);
9161abe9b8aSliubo 
917d561c025SChris Mason 	if (!wait) {
918815745cfSAl Viro 		filemap_flush(fs_info->btree_inode->i_mapping);
919df2ce34cSChris Mason 		return 0;
920d561c025SChris Mason 	}
921771ed689SChris Mason 
922b0244199SMiao Xie 	btrfs_wait_ordered_roots(fs_info, -1);
923771ed689SChris Mason 
924d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
92560376ce4SJosef Bacik 	if (IS_ERR(trans)) {
926354aa0fbSMiao Xie 		/* no transaction, don't bother */
927354aa0fbSMiao Xie 		if (PTR_ERR(trans) == -ENOENT)
928bd7de2c9SJosef Bacik 			return 0;
92998d5dc13STsutomu Itoh 		return PTR_ERR(trans);
93060376ce4SJosef Bacik 	}
931bd7de2c9SJosef Bacik 	return btrfs_commit_transaction(trans, root);
932d5719762SChris Mason }
933d5719762SChris Mason 
93434c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
935a9572a15SEric Paris {
936815745cfSAl Viro 	struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
937815745cfSAl Viro 	struct btrfs_root *root = info->tree_root;
938200da64eSTsutomu Itoh 	char *compress_type;
939a9572a15SEric Paris 
940a9572a15SEric Paris 	if (btrfs_test_opt(root, DEGRADED))
941a9572a15SEric Paris 		seq_puts(seq, ",degraded");
942a9572a15SEric Paris 	if (btrfs_test_opt(root, NODATASUM))
943a9572a15SEric Paris 		seq_puts(seq, ",nodatasum");
944a9572a15SEric Paris 	if (btrfs_test_opt(root, NODATACOW))
945a9572a15SEric Paris 		seq_puts(seq, ",nodatacow");
946a9572a15SEric Paris 	if (btrfs_test_opt(root, NOBARRIER))
947a9572a15SEric Paris 		seq_puts(seq, ",nobarrier");
948a9572a15SEric Paris 	if (info->max_inline != 8192 * 1024)
949c1c9ff7cSGeert Uytterhoeven 		seq_printf(seq, ",max_inline=%llu", info->max_inline);
950a9572a15SEric Paris 	if (info->alloc_start != 0)
951c1c9ff7cSGeert Uytterhoeven 		seq_printf(seq, ",alloc_start=%llu", info->alloc_start);
952a9572a15SEric Paris 	if (info->thread_pool_size !=  min_t(unsigned long,
953a9572a15SEric Paris 					     num_online_cpus() + 2, 8))
954a9572a15SEric Paris 		seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
955200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, COMPRESS)) {
956200da64eSTsutomu Itoh 		if (info->compress_type == BTRFS_COMPRESS_ZLIB)
957200da64eSTsutomu Itoh 			compress_type = "zlib";
958200da64eSTsutomu Itoh 		else
959200da64eSTsutomu Itoh 			compress_type = "lzo";
960200da64eSTsutomu Itoh 		if (btrfs_test_opt(root, FORCE_COMPRESS))
961200da64eSTsutomu Itoh 			seq_printf(seq, ",compress-force=%s", compress_type);
962200da64eSTsutomu Itoh 		else
963200da64eSTsutomu Itoh 			seq_printf(seq, ",compress=%s", compress_type);
964200da64eSTsutomu Itoh 	}
965c289811cSChris Mason 	if (btrfs_test_opt(root, NOSSD))
966c289811cSChris Mason 		seq_puts(seq, ",nossd");
967451d7585SChris Mason 	if (btrfs_test_opt(root, SSD_SPREAD))
968451d7585SChris Mason 		seq_puts(seq, ",ssd_spread");
969451d7585SChris Mason 	else if (btrfs_test_opt(root, SSD))
970a9572a15SEric Paris 		seq_puts(seq, ",ssd");
9713a5e1404SSage Weil 	if (btrfs_test_opt(root, NOTREELOG))
9726b65c5c6SSage Weil 		seq_puts(seq, ",notreelog");
973dccae999SSage Weil 	if (btrfs_test_opt(root, FLUSHONCOMMIT))
9746b65c5c6SSage Weil 		seq_puts(seq, ",flushoncommit");
97520a5239aSMatthew Wilcox 	if (btrfs_test_opt(root, DISCARD))
97620a5239aSMatthew Wilcox 		seq_puts(seq, ",discard");
977a9572a15SEric Paris 	if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
978a9572a15SEric Paris 		seq_puts(seq, ",noacl");
979200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, SPACE_CACHE))
980200da64eSTsutomu Itoh 		seq_puts(seq, ",space_cache");
98173bc1876SJosef Bacik 	else
9828965593eSDavid Sterba 		seq_puts(seq, ",nospace_cache");
983f420ee1eSStefan Behrens 	if (btrfs_test_opt(root, RESCAN_UUID_TREE))
984f420ee1eSStefan Behrens 		seq_puts(seq, ",rescan_uuid_tree");
985200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, CLEAR_CACHE))
986200da64eSTsutomu Itoh 		seq_puts(seq, ",clear_cache");
987200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
988200da64eSTsutomu Itoh 		seq_puts(seq, ",user_subvol_rm_allowed");
9890942caa3SDavid Sterba 	if (btrfs_test_opt(root, ENOSPC_DEBUG))
9900942caa3SDavid Sterba 		seq_puts(seq, ",enospc_debug");
9910942caa3SDavid Sterba 	if (btrfs_test_opt(root, AUTO_DEFRAG))
9920942caa3SDavid Sterba 		seq_puts(seq, ",autodefrag");
9930942caa3SDavid Sterba 	if (btrfs_test_opt(root, INODE_MAP_CACHE))
9940942caa3SDavid Sterba 		seq_puts(seq, ",inode_cache");
9959555c6c1SIlya Dryomov 	if (btrfs_test_opt(root, SKIP_BALANCE))
9969555c6c1SIlya Dryomov 		seq_puts(seq, ",skip_balance");
9978507d216SWang Shilong 	if (btrfs_test_opt(root, RECOVERY))
9988507d216SWang Shilong 		seq_puts(seq, ",recovery");
9998507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
10008507d216SWang Shilong 	if (btrfs_test_opt(root, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA))
10018507d216SWang Shilong 		seq_puts(seq, ",check_int_data");
10028507d216SWang Shilong 	else if (btrfs_test_opt(root, CHECK_INTEGRITY))
10038507d216SWang Shilong 		seq_puts(seq, ",check_int");
10048507d216SWang Shilong 	if (info->check_integrity_print_mask)
10058507d216SWang Shilong 		seq_printf(seq, ",check_int_print_mask=%d",
10068507d216SWang Shilong 				info->check_integrity_print_mask);
10078507d216SWang Shilong #endif
10088507d216SWang Shilong 	if (info->metadata_ratio)
10098507d216SWang Shilong 		seq_printf(seq, ",metadata_ratio=%d",
10108507d216SWang Shilong 				info->metadata_ratio);
10118c342930SJeff Mahoney 	if (btrfs_test_opt(root, PANIC_ON_FATAL_ERROR))
10128c342930SJeff Mahoney 		seq_puts(seq, ",fatal_errors=panic");
10138b87dc17SDavid Sterba 	if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
10148b87dc17SDavid Sterba 		seq_printf(seq, ",commit=%d", info->commit_interval);
1015a9572a15SEric Paris 	return 0;
1016a9572a15SEric Paris }
1017a9572a15SEric Paris 
1018a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data)
10192e635a27SChris Mason {
1020815745cfSAl Viro 	struct btrfs_fs_info *p = data;
1021815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(s);
10224b82d6e4SYan 
1023815745cfSAl Viro 	return fs_info->fs_devices == p->fs_devices;
10244b82d6e4SYan }
10254b82d6e4SYan 
1026450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data)
1027450ba0eaSJosef Bacik {
10286de1d09dSAl Viro 	int err = set_anon_super(s, data);
10296de1d09dSAl Viro 	if (!err)
1030450ba0eaSJosef Bacik 		s->s_fs_info = data;
10316de1d09dSAl Viro 	return err;
1032450ba0eaSJosef Bacik }
1033450ba0eaSJosef Bacik 
1034830c4adbSJosef Bacik /*
1035f9d9ef62SDavid Sterba  * subvolumes are identified by ino 256
1036f9d9ef62SDavid Sterba  */
1037f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode)
1038f9d9ef62SDavid Sterba {
1039f9d9ef62SDavid Sterba 	if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
1040f9d9ef62SDavid Sterba 		return 1;
1041f9d9ef62SDavid Sterba 	return 0;
1042f9d9ef62SDavid Sterba }
1043f9d9ef62SDavid Sterba 
1044f9d9ef62SDavid Sterba /*
1045830c4adbSJosef Bacik  * This will strip out the subvol=%s argument for an argument string and add
1046830c4adbSJosef Bacik  * subvolid=0 to make sure we get the actual tree root for path walking to the
1047830c4adbSJosef Bacik  * subvol we want.
1048830c4adbSJosef Bacik  */
1049830c4adbSJosef Bacik static char *setup_root_args(char *args)
1050830c4adbSJosef Bacik {
1051f60d16a8SJim Meyering 	unsigned len = strlen(args) + 2 + 1;
1052f60d16a8SJim Meyering 	char *src, *dst, *buf;
1053830c4adbSJosef Bacik 
1054830c4adbSJosef Bacik 	/*
1055f60d16a8SJim Meyering 	 * We need the same args as before, but with this substitution:
1056f60d16a8SJim Meyering 	 * s!subvol=[^,]+!subvolid=0!
1057830c4adbSJosef Bacik 	 *
1058f60d16a8SJim Meyering 	 * Since the replacement string is up to 2 bytes longer than the
1059f60d16a8SJim Meyering 	 * original, allocate strlen(args) + 2 + 1 bytes.
1060830c4adbSJosef Bacik 	 */
1061830c4adbSJosef Bacik 
1062f60d16a8SJim Meyering 	src = strstr(args, "subvol=");
1063830c4adbSJosef Bacik 	/* This shouldn't happen, but just in case.. */
1064f60d16a8SJim Meyering 	if (!src)
1065830c4adbSJosef Bacik 		return NULL;
1066f60d16a8SJim Meyering 
1067f60d16a8SJim Meyering 	buf = dst = kmalloc(len, GFP_NOFS);
1068f60d16a8SJim Meyering 	if (!buf)
1069f60d16a8SJim Meyering 		return NULL;
1070830c4adbSJosef Bacik 
1071830c4adbSJosef Bacik 	/*
1072f60d16a8SJim Meyering 	 * If the subvol= arg is not at the start of the string,
1073f60d16a8SJim Meyering 	 * copy whatever precedes it into buf.
1074830c4adbSJosef Bacik 	 */
1075f60d16a8SJim Meyering 	if (src != args) {
1076f60d16a8SJim Meyering 		*src++ = '\0';
1077f60d16a8SJim Meyering 		strcpy(buf, args);
1078f60d16a8SJim Meyering 		dst += strlen(args);
1079830c4adbSJosef Bacik 	}
1080830c4adbSJosef Bacik 
1081f60d16a8SJim Meyering 	strcpy(dst, "subvolid=0");
1082f60d16a8SJim Meyering 	dst += strlen("subvolid=0");
1083830c4adbSJosef Bacik 
1084830c4adbSJosef Bacik 	/*
1085f60d16a8SJim Meyering 	 * If there is a "," after the original subvol=... string,
1086f60d16a8SJim Meyering 	 * copy that suffix into our buffer.  Otherwise, we're done.
1087830c4adbSJosef Bacik 	 */
1088f60d16a8SJim Meyering 	src = strchr(src, ',');
1089f60d16a8SJim Meyering 	if (src)
1090f60d16a8SJim Meyering 		strcpy(dst, src);
1091830c4adbSJosef Bacik 
1092f60d16a8SJim Meyering 	return buf;
1093830c4adbSJosef Bacik }
1094830c4adbSJosef Bacik 
1095830c4adbSJosef Bacik static struct dentry *mount_subvol(const char *subvol_name, int flags,
1096830c4adbSJosef Bacik 				   const char *device_name, char *data)
1097830c4adbSJosef Bacik {
1098830c4adbSJosef Bacik 	struct dentry *root;
1099830c4adbSJosef Bacik 	struct vfsmount *mnt;
1100830c4adbSJosef Bacik 	char *newargs;
1101830c4adbSJosef Bacik 
1102830c4adbSJosef Bacik 	newargs = setup_root_args(data);
1103830c4adbSJosef Bacik 	if (!newargs)
1104830c4adbSJosef Bacik 		return ERR_PTR(-ENOMEM);
1105830c4adbSJosef Bacik 	mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name,
1106830c4adbSJosef Bacik 			     newargs);
1107830c4adbSJosef Bacik 	kfree(newargs);
1108830c4adbSJosef Bacik 	if (IS_ERR(mnt))
1109830c4adbSJosef Bacik 		return ERR_CAST(mnt);
1110830c4adbSJosef Bacik 
1111ea441d11SAl Viro 	root = mount_subtree(mnt, subvol_name);
1112830c4adbSJosef Bacik 
1113ea441d11SAl Viro 	if (!IS_ERR(root) && !is_subvolume_inode(root->d_inode)) {
1114ea441d11SAl Viro 		struct super_block *s = root->d_sb;
1115ea441d11SAl Viro 		dput(root);
1116ea441d11SAl Viro 		root = ERR_PTR(-EINVAL);
1117ea441d11SAl Viro 		deactivate_locked_super(s);
1118f9d9ef62SDavid Sterba 		printk(KERN_ERR "btrfs: '%s' is not a valid subvolume\n",
1119f9d9ef62SDavid Sterba 				subvol_name);
1120f9d9ef62SDavid Sterba 	}
1121f9d9ef62SDavid Sterba 
1122830c4adbSJosef Bacik 	return root;
1123830c4adbSJosef Bacik }
1124450ba0eaSJosef Bacik 
1125edf24abeSChristoph Hellwig /*
1126edf24abeSChristoph Hellwig  * Find a superblock for the given device / mount point.
1127edf24abeSChristoph Hellwig  *
1128edf24abeSChristoph Hellwig  * Note:  This is based on get_sb_bdev from fs/super.c with a few additions
1129edf24abeSChristoph Hellwig  *	  for multiple device setup.  Make sure to keep it in sync.
1130edf24abeSChristoph Hellwig  */
1131061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
1132306e16ceSDavid Sterba 		const char *device_name, void *data)
11334b82d6e4SYan {
11344b82d6e4SYan 	struct block_device *bdev = NULL;
11354b82d6e4SYan 	struct super_block *s;
11364b82d6e4SYan 	struct dentry *root;
11378a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices = NULL;
1138450ba0eaSJosef Bacik 	struct btrfs_fs_info *fs_info = NULL;
113997288f2cSChristoph Hellwig 	fmode_t mode = FMODE_READ;
114073f73415SJosef Bacik 	char *subvol_name = NULL;
114173f73415SJosef Bacik 	u64 subvol_objectid = 0;
11424b82d6e4SYan 	int error = 0;
11434b82d6e4SYan 
114497288f2cSChristoph Hellwig 	if (!(flags & MS_RDONLY))
114597288f2cSChristoph Hellwig 		mode |= FMODE_WRITE;
114697288f2cSChristoph Hellwig 
114797288f2cSChristoph Hellwig 	error = btrfs_parse_early_options(data, mode, fs_type,
114873f73415SJosef Bacik 					  &subvol_name, &subvol_objectid,
11495e2a4b25SDavid Sterba 					  &fs_devices);
1150f23c8af8SIlya Dryomov 	if (error) {
1151f23c8af8SIlya Dryomov 		kfree(subvol_name);
1152061dbc6bSAl Viro 		return ERR_PTR(error);
1153f23c8af8SIlya Dryomov 	}
1154edf24abeSChristoph Hellwig 
1155830c4adbSJosef Bacik 	if (subvol_name) {
1156830c4adbSJosef Bacik 		root = mount_subvol(subvol_name, flags, device_name, data);
1157830c4adbSJosef Bacik 		kfree(subvol_name);
1158830c4adbSJosef Bacik 		return root;
1159830c4adbSJosef Bacik 	}
1160830c4adbSJosef Bacik 
1161306e16ceSDavid Sterba 	error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
11628a4b83ccSChris Mason 	if (error)
1163830c4adbSJosef Bacik 		return ERR_PTR(error);
11644b82d6e4SYan 
1165450ba0eaSJosef Bacik 	/*
1166450ba0eaSJosef Bacik 	 * Setup a dummy root and fs_info for test/set super.  This is because
1167450ba0eaSJosef Bacik 	 * we don't actually fill this stuff out until open_ctree, but we need
1168450ba0eaSJosef Bacik 	 * it for searching for existing supers, so this lets us do that and
1169450ba0eaSJosef Bacik 	 * then open_ctree will properly initialize everything later.
1170450ba0eaSJosef Bacik 	 */
1171450ba0eaSJosef Bacik 	fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
117204d21a24SIlya Dryomov 	if (!fs_info)
117304d21a24SIlya Dryomov 		return ERR_PTR(-ENOMEM);
117404d21a24SIlya Dryomov 
1175450ba0eaSJosef Bacik 	fs_info->fs_devices = fs_devices;
1176450ba0eaSJosef Bacik 
11776c41761fSDavid Sterba 	fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
11786c41761fSDavid Sterba 	fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
11796c41761fSDavid Sterba 	if (!fs_info->super_copy || !fs_info->super_for_commit) {
11806c41761fSDavid Sterba 		error = -ENOMEM;
118104d21a24SIlya Dryomov 		goto error_fs_info;
118204d21a24SIlya Dryomov 	}
118304d21a24SIlya Dryomov 
118404d21a24SIlya Dryomov 	error = btrfs_open_devices(fs_devices, mode, fs_type);
118504d21a24SIlya Dryomov 	if (error)
118604d21a24SIlya Dryomov 		goto error_fs_info;
118704d21a24SIlya Dryomov 
118804d21a24SIlya Dryomov 	if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
118904d21a24SIlya Dryomov 		error = -EACCES;
11906c41761fSDavid Sterba 		goto error_close_devices;
11916c41761fSDavid Sterba 	}
11926c41761fSDavid Sterba 
1193dfe25020SChris Mason 	bdev = fs_devices->latest_bdev;
11949249e17fSDavid Howells 	s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC,
11959249e17fSDavid Howells 		 fs_info);
1196830c4adbSJosef Bacik 	if (IS_ERR(s)) {
1197830c4adbSJosef Bacik 		error = PTR_ERR(s);
1198830c4adbSJosef Bacik 		goto error_close_devices;
1199830c4adbSJosef Bacik 	}
12004b82d6e4SYan 
12014b82d6e4SYan 	if (s->s_root) {
12022b82032cSYan Zheng 		btrfs_close_devices(fs_devices);
12036c41761fSDavid Sterba 		free_fs_info(fs_info);
120459553edfSAl Viro 		if ((flags ^ s->s_flags) & MS_RDONLY)
120559553edfSAl Viro 			error = -EBUSY;
12064b82d6e4SYan 	} else {
12074b82d6e4SYan 		char b[BDEVNAME_SIZE];
12084b82d6e4SYan 
12094b82d6e4SYan 		strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
1210815745cfSAl Viro 		btrfs_sb(s)->bdev_holder = fs_type;
12118a4b83ccSChris Mason 		error = btrfs_fill_super(s, fs_devices, data,
12128a4b83ccSChris Mason 					 flags & MS_SILENT ? 1 : 0);
12134b82d6e4SYan 	}
12144b82d6e4SYan 
121559553edfSAl Viro 	root = !error ? get_default_root(s, subvol_objectid) : ERR_PTR(error);
121659553edfSAl Viro 	if (IS_ERR(root))
1217e15d0542SXin Zhong 		deactivate_locked_super(s);
12184b82d6e4SYan 
1219061dbc6bSAl Viro 	return root;
12204b82d6e4SYan 
1221c146afadSYan Zheng error_close_devices:
12228a4b83ccSChris Mason 	btrfs_close_devices(fs_devices);
122304d21a24SIlya Dryomov error_fs_info:
12246c41761fSDavid Sterba 	free_fs_info(fs_info);
1225061dbc6bSAl Viro 	return ERR_PTR(error);
12264b82d6e4SYan }
12272e635a27SChris Mason 
12280d2450abSSergei Trofimovich static void btrfs_set_max_workers(struct btrfs_workers *workers, int new_limit)
12290d2450abSSergei Trofimovich {
12300d2450abSSergei Trofimovich 	spin_lock_irq(&workers->lock);
12310d2450abSSergei Trofimovich 	workers->max_workers = new_limit;
12320d2450abSSergei Trofimovich 	spin_unlock_irq(&workers->lock);
12330d2450abSSergei Trofimovich }
12340d2450abSSergei Trofimovich 
12350d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
12360d2450abSSergei Trofimovich 				     int new_pool_size, int old_pool_size)
12370d2450abSSergei Trofimovich {
12380d2450abSSergei Trofimovich 	if (new_pool_size == old_pool_size)
12390d2450abSSergei Trofimovich 		return;
12400d2450abSSergei Trofimovich 
12410d2450abSSergei Trofimovich 	fs_info->thread_pool_size = new_pool_size;
12420d2450abSSergei Trofimovich 
12430d2450abSSergei Trofimovich 	printk(KERN_INFO "btrfs: resize thread pool %d -> %d\n",
12440d2450abSSergei Trofimovich 	       old_pool_size, new_pool_size);
12450d2450abSSergei Trofimovich 
12460d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->generic_worker, new_pool_size);
12470d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->workers, new_pool_size);
12480d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->delalloc_workers, new_pool_size);
12490d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->submit_workers, new_pool_size);
12500d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->caching_workers, new_pool_size);
12510d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->fixup_workers, new_pool_size);
12520d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->endio_workers, new_pool_size);
12530d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->endio_meta_workers, new_pool_size);
12540d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->endio_meta_write_workers, new_pool_size);
12550d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->endio_write_workers, new_pool_size);
12560d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->endio_freespace_worker, new_pool_size);
12570d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->delayed_workers, new_pool_size);
12580d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->readahead_workers, new_pool_size);
1259ff023aacSStefan Behrens 	btrfs_set_max_workers(&fs_info->scrub_wr_completion_workers,
1260ff023aacSStefan Behrens 			      new_pool_size);
12610d2450abSSergei Trofimovich }
12620d2450abSSergei Trofimovich 
1263f42a34b2SMiao Xie static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info)
1264dc81cdc5SMiao Xie {
1265dc81cdc5SMiao Xie 	set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1266f42a34b2SMiao Xie }
1267dc81cdc5SMiao Xie 
1268f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1269f42a34b2SMiao Xie 				       unsigned long old_opts, int flags)
1270f42a34b2SMiao Xie {
1271dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1272dc81cdc5SMiao Xie 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
1273dc81cdc5SMiao Xie 	     (flags & MS_RDONLY))) {
1274dc81cdc5SMiao Xie 		/* wait for any defraggers to finish */
1275dc81cdc5SMiao Xie 		wait_event(fs_info->transaction_wait,
1276dc81cdc5SMiao Xie 			   (atomic_read(&fs_info->defrag_running) == 0));
1277dc81cdc5SMiao Xie 		if (flags & MS_RDONLY)
1278dc81cdc5SMiao Xie 			sync_filesystem(fs_info->sb);
1279dc81cdc5SMiao Xie 	}
1280dc81cdc5SMiao Xie }
1281dc81cdc5SMiao Xie 
1282dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1283dc81cdc5SMiao Xie 					 unsigned long old_opts)
1284dc81cdc5SMiao Xie {
1285dc81cdc5SMiao Xie 	/*
1286dc81cdc5SMiao Xie 	 * We need cleanup all defragable inodes if the autodefragment is
1287dc81cdc5SMiao Xie 	 * close or the fs is R/O.
1288dc81cdc5SMiao Xie 	 */
1289dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1290dc81cdc5SMiao Xie 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
1291dc81cdc5SMiao Xie 	     (fs_info->sb->s_flags & MS_RDONLY))) {
1292dc81cdc5SMiao Xie 		btrfs_cleanup_defrag_inodes(fs_info);
1293dc81cdc5SMiao Xie 	}
1294dc81cdc5SMiao Xie 
1295dc81cdc5SMiao Xie 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1296dc81cdc5SMiao Xie }
1297dc81cdc5SMiao Xie 
1298c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1299c146afadSYan Zheng {
1300815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1301815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
130249b25e05SJeff Mahoney 	unsigned old_flags = sb->s_flags;
130349b25e05SJeff Mahoney 	unsigned long old_opts = fs_info->mount_opt;
130449b25e05SJeff Mahoney 	unsigned long old_compress_type = fs_info->compress_type;
130549b25e05SJeff Mahoney 	u64 old_max_inline = fs_info->max_inline;
130649b25e05SJeff Mahoney 	u64 old_alloc_start = fs_info->alloc_start;
130749b25e05SJeff Mahoney 	int old_thread_pool_size = fs_info->thread_pool_size;
130849b25e05SJeff Mahoney 	unsigned int old_metadata_ratio = fs_info->metadata_ratio;
1309c146afadSYan Zheng 	int ret;
1310c146afadSYan Zheng 
1311f42a34b2SMiao Xie 	btrfs_remount_prepare(fs_info);
1312dc81cdc5SMiao Xie 
1313b288052eSChris Mason 	ret = btrfs_parse_options(root, data);
131449b25e05SJeff Mahoney 	if (ret) {
131549b25e05SJeff Mahoney 		ret = -EINVAL;
131649b25e05SJeff Mahoney 		goto restore;
131749b25e05SJeff Mahoney 	}
1318b288052eSChris Mason 
1319f42a34b2SMiao Xie 	btrfs_remount_begin(fs_info, old_opts, *flags);
13200d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
13210d2450abSSergei Trofimovich 		fs_info->thread_pool_size, old_thread_pool_size);
13220d2450abSSergei Trofimovich 
1323c146afadSYan Zheng 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
1324dc81cdc5SMiao Xie 		goto out;
1325c146afadSYan Zheng 
1326c146afadSYan Zheng 	if (*flags & MS_RDONLY) {
13278dabb742SStefan Behrens 		/*
13288dabb742SStefan Behrens 		 * this also happens on 'umount -rf' or on shutdown, when
13298dabb742SStefan Behrens 		 * the filesystem is busy.
13308dabb742SStefan Behrens 		 */
1331361c093dSStefan Behrens 
1332361c093dSStefan Behrens 		/* wait for the uuid_scan task to finish */
1333361c093dSStefan Behrens 		down(&fs_info->uuid_tree_rescan_sem);
1334361c093dSStefan Behrens 		/* avoid complains from lockdep et al. */
1335361c093dSStefan Behrens 		up(&fs_info->uuid_tree_rescan_sem);
1336361c093dSStefan Behrens 
1337c146afadSYan Zheng 		sb->s_flags |= MS_RDONLY;
1338c146afadSYan Zheng 
13398dabb742SStefan Behrens 		btrfs_dev_replace_suspend_for_unmount(fs_info);
13408dabb742SStefan Behrens 		btrfs_scrub_cancel(fs_info);
1341061594efSMiao Xie 		btrfs_pause_balance(fs_info);
13428dabb742SStefan Behrens 
1343c146afadSYan Zheng 		ret = btrfs_commit_super(root);
134449b25e05SJeff Mahoney 		if (ret)
134549b25e05SJeff Mahoney 			goto restore;
1346c146afadSYan Zheng 	} else {
13476ef3de9cSDavid Sterba 		if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) {
13486ef3de9cSDavid Sterba 			btrfs_err(fs_info,
13496ef3de9cSDavid Sterba 				"Remounting read-write after error is not allowed\n");
13506ef3de9cSDavid Sterba 			ret = -EINVAL;
13516ef3de9cSDavid Sterba 			goto restore;
13526ef3de9cSDavid Sterba 		}
13538a3db184SSergei Trofimovich 		if (fs_info->fs_devices->rw_devices == 0) {
135449b25e05SJeff Mahoney 			ret = -EACCES;
135549b25e05SJeff Mahoney 			goto restore;
13568a3db184SSergei Trofimovich 		}
13572b82032cSYan Zheng 
1358292fd7fcSStefan Behrens 		if (fs_info->fs_devices->missing_devices >
1359292fd7fcSStefan Behrens 		     fs_info->num_tolerated_disk_barrier_failures &&
1360292fd7fcSStefan Behrens 		    !(*flags & MS_RDONLY)) {
1361292fd7fcSStefan Behrens 			printk(KERN_WARNING
1362292fd7fcSStefan Behrens 			       "Btrfs: too many missing devices, writeable remount is not allowed\n");
1363292fd7fcSStefan Behrens 			ret = -EACCES;
1364292fd7fcSStefan Behrens 			goto restore;
1365292fd7fcSStefan Behrens 		}
1366292fd7fcSStefan Behrens 
13678a3db184SSergei Trofimovich 		if (btrfs_super_log_root(fs_info->super_copy) != 0) {
136849b25e05SJeff Mahoney 			ret = -EINVAL;
136949b25e05SJeff Mahoney 			goto restore;
13708a3db184SSergei Trofimovich 		}
1371c146afadSYan Zheng 
1372815745cfSAl Viro 		ret = btrfs_cleanup_fs_roots(fs_info);
137349b25e05SJeff Mahoney 		if (ret)
137449b25e05SJeff Mahoney 			goto restore;
1375c146afadSYan Zheng 
1376d68fc57bSYan, Zheng 		/* recover relocation */
1377d68fc57bSYan, Zheng 		ret = btrfs_recover_relocation(root);
137849b25e05SJeff Mahoney 		if (ret)
137949b25e05SJeff Mahoney 			goto restore;
1380c146afadSYan Zheng 
13812b6ba629SIlya Dryomov 		ret = btrfs_resume_balance_async(fs_info);
13822b6ba629SIlya Dryomov 		if (ret)
13832b6ba629SIlya Dryomov 			goto restore;
13842b6ba629SIlya Dryomov 
13858dabb742SStefan Behrens 		ret = btrfs_resume_dev_replace_async(fs_info);
13868dabb742SStefan Behrens 		if (ret) {
13878dabb742SStefan Behrens 			pr_warn("btrfs: failed to resume dev_replace\n");
13888dabb742SStefan Behrens 			goto restore;
13898dabb742SStefan Behrens 		}
139094aebfb2SJosef Bacik 
139194aebfb2SJosef Bacik 		if (!fs_info->uuid_root) {
139294aebfb2SJosef Bacik 			pr_info("btrfs: creating UUID tree\n");
139394aebfb2SJosef Bacik 			ret = btrfs_create_uuid_tree(fs_info);
139494aebfb2SJosef Bacik 			if (ret) {
139594aebfb2SJosef Bacik 				pr_warn("btrfs: failed to create the uuid tree"
139694aebfb2SJosef Bacik 					"%d\n", ret);
139794aebfb2SJosef Bacik 				goto restore;
139894aebfb2SJosef Bacik 			}
139994aebfb2SJosef Bacik 		}
1400c146afadSYan Zheng 		sb->s_flags &= ~MS_RDONLY;
1401c146afadSYan Zheng 	}
1402dc81cdc5SMiao Xie out:
1403dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
1404c146afadSYan Zheng 	return 0;
140549b25e05SJeff Mahoney 
140649b25e05SJeff Mahoney restore:
140749b25e05SJeff Mahoney 	/* We've hit an error - don't reset MS_RDONLY */
140849b25e05SJeff Mahoney 	if (sb->s_flags & MS_RDONLY)
140949b25e05SJeff Mahoney 		old_flags |= MS_RDONLY;
141049b25e05SJeff Mahoney 	sb->s_flags = old_flags;
141149b25e05SJeff Mahoney 	fs_info->mount_opt = old_opts;
141249b25e05SJeff Mahoney 	fs_info->compress_type = old_compress_type;
141349b25e05SJeff Mahoney 	fs_info->max_inline = old_max_inline;
1414c018daecSMiao Xie 	mutex_lock(&fs_info->chunk_mutex);
141549b25e05SJeff Mahoney 	fs_info->alloc_start = old_alloc_start;
1416c018daecSMiao Xie 	mutex_unlock(&fs_info->chunk_mutex);
14170d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
14180d2450abSSergei Trofimovich 		old_thread_pool_size, fs_info->thread_pool_size);
141949b25e05SJeff Mahoney 	fs_info->metadata_ratio = old_metadata_ratio;
1420dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
142149b25e05SJeff Mahoney 	return ret;
1422c146afadSYan Zheng }
1423c146afadSYan Zheng 
1424bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */
1425bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1,
1426bcd53741SArne Jansen 				       const void *dev_info2)
1427bcd53741SArne Jansen {
1428bcd53741SArne Jansen 	if (((struct btrfs_device_info *)dev_info1)->max_avail >
1429bcd53741SArne Jansen 	    ((struct btrfs_device_info *)dev_info2)->max_avail)
1430bcd53741SArne Jansen 		return -1;
1431bcd53741SArne Jansen 	else if (((struct btrfs_device_info *)dev_info1)->max_avail <
1432bcd53741SArne Jansen 		 ((struct btrfs_device_info *)dev_info2)->max_avail)
1433bcd53741SArne Jansen 		return 1;
1434bcd53741SArne Jansen 	else
1435bcd53741SArne Jansen 	return 0;
1436bcd53741SArne Jansen }
1437bcd53741SArne Jansen 
1438bcd53741SArne Jansen /*
1439bcd53741SArne Jansen  * sort the devices by max_avail, in which max free extent size of each device
1440bcd53741SArne Jansen  * is stored.(Descending Sort)
1441bcd53741SArne Jansen  */
1442bcd53741SArne Jansen static inline void btrfs_descending_sort_devices(
1443bcd53741SArne Jansen 					struct btrfs_device_info *devices,
1444bcd53741SArne Jansen 					size_t nr_devices)
1445bcd53741SArne Jansen {
1446bcd53741SArne Jansen 	sort(devices, nr_devices, sizeof(struct btrfs_device_info),
1447bcd53741SArne Jansen 	     btrfs_cmp_device_free_bytes, NULL);
1448bcd53741SArne Jansen }
1449bcd53741SArne Jansen 
14506d07bcecSMiao Xie /*
14516d07bcecSMiao Xie  * The helper to calc the free space on the devices that can be used to store
14526d07bcecSMiao Xie  * file data.
14536d07bcecSMiao Xie  */
14546d07bcecSMiao Xie static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
14556d07bcecSMiao Xie {
14566d07bcecSMiao Xie 	struct btrfs_fs_info *fs_info = root->fs_info;
14576d07bcecSMiao Xie 	struct btrfs_device_info *devices_info;
14586d07bcecSMiao Xie 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
14596d07bcecSMiao Xie 	struct btrfs_device *device;
14606d07bcecSMiao Xie 	u64 skip_space;
14616d07bcecSMiao Xie 	u64 type;
14626d07bcecSMiao Xie 	u64 avail_space;
14636d07bcecSMiao Xie 	u64 used_space;
14646d07bcecSMiao Xie 	u64 min_stripe_size;
146539fb26c3SMiao Xie 	int min_stripes = 1, num_stripes = 1;
14666d07bcecSMiao Xie 	int i = 0, nr_devices;
14676d07bcecSMiao Xie 	int ret;
14686d07bcecSMiao Xie 
1469b772a86eSLi Zefan 	nr_devices = fs_info->fs_devices->open_devices;
14706d07bcecSMiao Xie 	BUG_ON(!nr_devices);
14716d07bcecSMiao Xie 
1472d9b0d9baSDulshani Gunawardhana 	devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
14736d07bcecSMiao Xie 			       GFP_NOFS);
14746d07bcecSMiao Xie 	if (!devices_info)
14756d07bcecSMiao Xie 		return -ENOMEM;
14766d07bcecSMiao Xie 
14776d07bcecSMiao Xie 	/* calc min stripe number for data space alloction */
14786d07bcecSMiao Xie 	type = btrfs_get_alloc_profile(root, 1);
147939fb26c3SMiao Xie 	if (type & BTRFS_BLOCK_GROUP_RAID0) {
14806d07bcecSMiao Xie 		min_stripes = 2;
148139fb26c3SMiao Xie 		num_stripes = nr_devices;
148239fb26c3SMiao Xie 	} else if (type & BTRFS_BLOCK_GROUP_RAID1) {
14836d07bcecSMiao Xie 		min_stripes = 2;
148439fb26c3SMiao Xie 		num_stripes = 2;
148539fb26c3SMiao Xie 	} else if (type & BTRFS_BLOCK_GROUP_RAID10) {
14866d07bcecSMiao Xie 		min_stripes = 4;
148739fb26c3SMiao Xie 		num_stripes = 4;
148839fb26c3SMiao Xie 	}
14896d07bcecSMiao Xie 
14906d07bcecSMiao Xie 	if (type & BTRFS_BLOCK_GROUP_DUP)
14916d07bcecSMiao Xie 		min_stripe_size = 2 * BTRFS_STRIPE_LEN;
14926d07bcecSMiao Xie 	else
14936d07bcecSMiao Xie 		min_stripe_size = BTRFS_STRIPE_LEN;
14946d07bcecSMiao Xie 
1495b772a86eSLi Zefan 	list_for_each_entry(device, &fs_devices->devices, dev_list) {
149663a212abSStefan Behrens 		if (!device->in_fs_metadata || !device->bdev ||
149763a212abSStefan Behrens 		    device->is_tgtdev_for_dev_replace)
14986d07bcecSMiao Xie 			continue;
14996d07bcecSMiao Xie 
15006d07bcecSMiao Xie 		avail_space = device->total_bytes - device->bytes_used;
15016d07bcecSMiao Xie 
15026d07bcecSMiao Xie 		/* align with stripe_len */
15036d07bcecSMiao Xie 		do_div(avail_space, BTRFS_STRIPE_LEN);
15046d07bcecSMiao Xie 		avail_space *= BTRFS_STRIPE_LEN;
15056d07bcecSMiao Xie 
15066d07bcecSMiao Xie 		/*
15076d07bcecSMiao Xie 		 * In order to avoid overwritting the superblock on the drive,
15086d07bcecSMiao Xie 		 * btrfs starts at an offset of at least 1MB when doing chunk
15096d07bcecSMiao Xie 		 * allocation.
15106d07bcecSMiao Xie 		 */
15116d07bcecSMiao Xie 		skip_space = 1024 * 1024;
15126d07bcecSMiao Xie 
15136d07bcecSMiao Xie 		/* user can set the offset in fs_info->alloc_start. */
15146d07bcecSMiao Xie 		if (fs_info->alloc_start + BTRFS_STRIPE_LEN <=
15156d07bcecSMiao Xie 		    device->total_bytes)
15166d07bcecSMiao Xie 			skip_space = max(fs_info->alloc_start, skip_space);
15176d07bcecSMiao Xie 
15186d07bcecSMiao Xie 		/*
15196d07bcecSMiao Xie 		 * btrfs can not use the free space in [0, skip_space - 1],
15206d07bcecSMiao Xie 		 * we must subtract it from the total. In order to implement
15216d07bcecSMiao Xie 		 * it, we account the used space in this range first.
15226d07bcecSMiao Xie 		 */
15236d07bcecSMiao Xie 		ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1,
15246d07bcecSMiao Xie 						     &used_space);
15256d07bcecSMiao Xie 		if (ret) {
15266d07bcecSMiao Xie 			kfree(devices_info);
15276d07bcecSMiao Xie 			return ret;
15286d07bcecSMiao Xie 		}
15296d07bcecSMiao Xie 
15306d07bcecSMiao Xie 		/* calc the free space in [0, skip_space - 1] */
15316d07bcecSMiao Xie 		skip_space -= used_space;
15326d07bcecSMiao Xie 
15336d07bcecSMiao Xie 		/*
15346d07bcecSMiao Xie 		 * we can use the free space in [0, skip_space - 1], subtract
15356d07bcecSMiao Xie 		 * it from the total.
15366d07bcecSMiao Xie 		 */
15376d07bcecSMiao Xie 		if (avail_space && avail_space >= skip_space)
15386d07bcecSMiao Xie 			avail_space -= skip_space;
15396d07bcecSMiao Xie 		else
15406d07bcecSMiao Xie 			avail_space = 0;
15416d07bcecSMiao Xie 
15426d07bcecSMiao Xie 		if (avail_space < min_stripe_size)
15436d07bcecSMiao Xie 			continue;
15446d07bcecSMiao Xie 
15456d07bcecSMiao Xie 		devices_info[i].dev = device;
15466d07bcecSMiao Xie 		devices_info[i].max_avail = avail_space;
15476d07bcecSMiao Xie 
15486d07bcecSMiao Xie 		i++;
15496d07bcecSMiao Xie 	}
15506d07bcecSMiao Xie 
15516d07bcecSMiao Xie 	nr_devices = i;
15526d07bcecSMiao Xie 
15536d07bcecSMiao Xie 	btrfs_descending_sort_devices(devices_info, nr_devices);
15546d07bcecSMiao Xie 
15556d07bcecSMiao Xie 	i = nr_devices - 1;
15566d07bcecSMiao Xie 	avail_space = 0;
15576d07bcecSMiao Xie 	while (nr_devices >= min_stripes) {
155839fb26c3SMiao Xie 		if (num_stripes > nr_devices)
155939fb26c3SMiao Xie 			num_stripes = nr_devices;
156039fb26c3SMiao Xie 
15616d07bcecSMiao Xie 		if (devices_info[i].max_avail >= min_stripe_size) {
15626d07bcecSMiao Xie 			int j;
15636d07bcecSMiao Xie 			u64 alloc_size;
15646d07bcecSMiao Xie 
156539fb26c3SMiao Xie 			avail_space += devices_info[i].max_avail * num_stripes;
15666d07bcecSMiao Xie 			alloc_size = devices_info[i].max_avail;
156739fb26c3SMiao Xie 			for (j = i + 1 - num_stripes; j <= i; j++)
15686d07bcecSMiao Xie 				devices_info[j].max_avail -= alloc_size;
15696d07bcecSMiao Xie 		}
15706d07bcecSMiao Xie 		i--;
15716d07bcecSMiao Xie 		nr_devices--;
15726d07bcecSMiao Xie 	}
15736d07bcecSMiao Xie 
15746d07bcecSMiao Xie 	kfree(devices_info);
15756d07bcecSMiao Xie 	*free_bytes = avail_space;
15766d07bcecSMiao Xie 	return 0;
15776d07bcecSMiao Xie }
15786d07bcecSMiao Xie 
15798fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
15808fd17795SChris Mason {
1581815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
1582815745cfSAl Viro 	struct btrfs_super_block *disk_super = fs_info->super_copy;
1583815745cfSAl Viro 	struct list_head *head = &fs_info->space_info;
1584bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
1585bd4d1088SJosef Bacik 	u64 total_used = 0;
15866d07bcecSMiao Xie 	u64 total_free_data = 0;
1587db94535dSChris Mason 	int bits = dentry->d_sb->s_blocksize_bits;
1588815745cfSAl Viro 	__be32 *fsid = (__be32 *)fs_info->fsid;
15896d07bcecSMiao Xie 	int ret;
15908fd17795SChris Mason 
15916d07bcecSMiao Xie 	/* holding chunk_muext to avoid allocating new chunks */
1592815745cfSAl Viro 	mutex_lock(&fs_info->chunk_mutex);
1593bd4d1088SJosef Bacik 	rcu_read_lock();
159489a55897SJosef Bacik 	list_for_each_entry_rcu(found, head, list) {
15956d07bcecSMiao Xie 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
15966d07bcecSMiao Xie 			total_free_data += found->disk_total - found->disk_used;
15976d07bcecSMiao Xie 			total_free_data -=
15986d07bcecSMiao Xie 				btrfs_account_ro_block_groups_free_space(found);
15996d07bcecSMiao Xie 		}
16006d07bcecSMiao Xie 
1601b742bb82SYan, Zheng 		total_used += found->disk_used;
160289a55897SJosef Bacik 	}
1603bd4d1088SJosef Bacik 	rcu_read_unlock();
1604bd4d1088SJosef Bacik 
16058fd17795SChris Mason 	buf->f_namelen = BTRFS_NAME_LEN;
1606db94535dSChris Mason 	buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
1607bd4d1088SJosef Bacik 	buf->f_bfree = buf->f_blocks - (total_used >> bits);
16088fd17795SChris Mason 	buf->f_bsize = dentry->d_sb->s_blocksize;
16098fd17795SChris Mason 	buf->f_type = BTRFS_SUPER_MAGIC;
16106d07bcecSMiao Xie 	buf->f_bavail = total_free_data;
1611815745cfSAl Viro 	ret = btrfs_calc_avail_data_space(fs_info->tree_root, &total_free_data);
16126d07bcecSMiao Xie 	if (ret) {
1613815745cfSAl Viro 		mutex_unlock(&fs_info->chunk_mutex);
16146d07bcecSMiao Xie 		return ret;
16156d07bcecSMiao Xie 	}
16166d07bcecSMiao Xie 	buf->f_bavail += total_free_data;
16176d07bcecSMiao Xie 	buf->f_bavail = buf->f_bavail >> bits;
1618815745cfSAl Viro 	mutex_unlock(&fs_info->chunk_mutex);
1619d397712bSChris Mason 
16209d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
16219d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
16229d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
16239d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
16249d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
162532d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
162632d48fa1SDavid Woodhouse 	buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
162732d48fa1SDavid Woodhouse 	buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
162832d48fa1SDavid Woodhouse 
16298fd17795SChris Mason 	return 0;
16308fd17795SChris Mason }
1631b5133862SChris Mason 
1632aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb)
1633aea52e19SAl Viro {
1634815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1635aea52e19SAl Viro 	kill_anon_super(sb);
1636aea52e19SAl Viro 	free_fs_info(fs_info);
1637aea52e19SAl Viro }
1638aea52e19SAl Viro 
16392e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
16402e635a27SChris Mason 	.owner		= THIS_MODULE,
16412e635a27SChris Mason 	.name		= "btrfs",
1642061dbc6bSAl Viro 	.mount		= btrfs_mount,
1643aea52e19SAl Viro 	.kill_sb	= btrfs_kill_super,
16442e635a27SChris Mason 	.fs_flags	= FS_REQUIRES_DEV,
16452e635a27SChris Mason };
16467f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs");
1647a9218f6bSChris Mason 
1648d352ac68SChris Mason /*
1649d352ac68SChris Mason  * used by btrfsctl to scan devices when no FS is mounted
1650d352ac68SChris Mason  */
16518a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
16528a4b83ccSChris Mason 				unsigned long arg)
16538a4b83ccSChris Mason {
16548a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
16558a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices;
1656c071fcfdSChris Mason 	int ret = -ENOTTY;
16578a4b83ccSChris Mason 
1658e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1659e441d54dSChris Mason 		return -EPERM;
1660e441d54dSChris Mason 
1661dae7b665SLi Zefan 	vol = memdup_user((void __user *)arg, sizeof(*vol));
1662dae7b665SLi Zefan 	if (IS_ERR(vol))
1663dae7b665SLi Zefan 		return PTR_ERR(vol);
1664c071fcfdSChris Mason 
16658a4b83ccSChris Mason 	switch (cmd) {
16668a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
166797288f2cSChristoph Hellwig 		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
16688a4b83ccSChris Mason 					    &btrfs_fs_type, &fs_devices);
16698a4b83ccSChris Mason 		break;
167002db0844SJosef Bacik 	case BTRFS_IOC_DEVICES_READY:
167102db0844SJosef Bacik 		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
167202db0844SJosef Bacik 					    &btrfs_fs_type, &fs_devices);
167302db0844SJosef Bacik 		if (ret)
167402db0844SJosef Bacik 			break;
167502db0844SJosef Bacik 		ret = !(fs_devices->num_devices == fs_devices->total_devices);
167602db0844SJosef Bacik 		break;
16778a4b83ccSChris Mason 	}
1678dae7b665SLi Zefan 
16798a4b83ccSChris Mason 	kfree(vol);
1680f819d837SLinda Knippers 	return ret;
16818a4b83ccSChris Mason }
16828a4b83ccSChris Mason 
16830176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
1684ed0dab6bSYan {
1685354aa0fbSMiao Xie 	struct btrfs_trans_handle *trans;
1686354aa0fbSMiao Xie 	struct btrfs_root *root = btrfs_sb(sb)->tree_root;
1687354aa0fbSMiao Xie 
1688d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
1689354aa0fbSMiao Xie 	if (IS_ERR(trans)) {
1690354aa0fbSMiao Xie 		/* no transaction, don't bother */
1691354aa0fbSMiao Xie 		if (PTR_ERR(trans) == -ENOENT)
16920176260fSLinus Torvalds 			return 0;
1693354aa0fbSMiao Xie 		return PTR_ERR(trans);
1694354aa0fbSMiao Xie 	}
1695354aa0fbSMiao Xie 	return btrfs_commit_transaction(trans, root);
1696ed0dab6bSYan }
1697ed0dab6bSYan 
16980176260fSLinus Torvalds static int btrfs_unfreeze(struct super_block *sb)
1699ed0dab6bSYan {
17000176260fSLinus Torvalds 	return 0;
1701ed0dab6bSYan }
17022e635a27SChris Mason 
17039c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
17049c5085c1SJosef Bacik {
17059c5085c1SJosef Bacik 	struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
17069c5085c1SJosef Bacik 	struct btrfs_fs_devices *cur_devices;
17079c5085c1SJosef Bacik 	struct btrfs_device *dev, *first_dev = NULL;
17089c5085c1SJosef Bacik 	struct list_head *head;
17099c5085c1SJosef Bacik 	struct rcu_string *name;
17109c5085c1SJosef Bacik 
17119c5085c1SJosef Bacik 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
17129c5085c1SJosef Bacik 	cur_devices = fs_info->fs_devices;
17139c5085c1SJosef Bacik 	while (cur_devices) {
17149c5085c1SJosef Bacik 		head = &cur_devices->devices;
17159c5085c1SJosef Bacik 		list_for_each_entry(dev, head, dev_list) {
1716aa9ddcd4SJosef Bacik 			if (dev->missing)
1717aa9ddcd4SJosef Bacik 				continue;
17189c5085c1SJosef Bacik 			if (!first_dev || dev->devid < first_dev->devid)
17199c5085c1SJosef Bacik 				first_dev = dev;
17209c5085c1SJosef Bacik 		}
17219c5085c1SJosef Bacik 		cur_devices = cur_devices->seed;
17229c5085c1SJosef Bacik 	}
17239c5085c1SJosef Bacik 
17249c5085c1SJosef Bacik 	if (first_dev) {
17259c5085c1SJosef Bacik 		rcu_read_lock();
17269c5085c1SJosef Bacik 		name = rcu_dereference(first_dev->name);
17279c5085c1SJosef Bacik 		seq_escape(m, name->str, " \t\n\\");
17289c5085c1SJosef Bacik 		rcu_read_unlock();
17299c5085c1SJosef Bacik 	} else {
17309c5085c1SJosef Bacik 		WARN_ON(1);
17319c5085c1SJosef Bacik 	}
17329c5085c1SJosef Bacik 	mutex_unlock(&fs_info->fs_devices->device_list_mutex);
17339c5085c1SJosef Bacik 	return 0;
17349c5085c1SJosef Bacik }
17359c5085c1SJosef Bacik 
1736b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
173776dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
1738bd555975SAl Viro 	.evict_inode	= btrfs_evict_inode,
1739e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
1740d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
1741a9572a15SEric Paris 	.show_options	= btrfs_show_options,
17429c5085c1SJosef Bacik 	.show_devname	= btrfs_show_devname,
17434730a4bcSChris Mason 	.write_inode	= btrfs_write_inode,
17442c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
17452c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
17468fd17795SChris Mason 	.statfs		= btrfs_statfs,
1747c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
17480176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
17490176260fSLinus Torvalds 	.unfreeze_fs	= btrfs_unfreeze,
1750e20d96d6SChris Mason };
1751a9218f6bSChris Mason 
1752a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
1753a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
1754a9218f6bSChris Mason 	.compat_ioctl = btrfs_control_ioctl,
1755a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
17566038f373SArnd Bergmann 	.llseek = noop_llseek,
1757a9218f6bSChris Mason };
1758a9218f6bSChris Mason 
1759a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
1760578454ffSKay Sievers 	.minor		= BTRFS_MINOR,
1761a9218f6bSChris Mason 	.name		= "btrfs-control",
1762a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
1763a9218f6bSChris Mason };
1764a9218f6bSChris Mason 
1765578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
1766578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control");
1767578454ffSKay Sievers 
1768a9218f6bSChris Mason static int btrfs_interface_init(void)
1769a9218f6bSChris Mason {
1770a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
1771a9218f6bSChris Mason }
1772a9218f6bSChris Mason 
1773b2950863SChristoph Hellwig static void btrfs_interface_exit(void)
1774a9218f6bSChris Mason {
1775a9218f6bSChris Mason 	if (misc_deregister(&btrfs_misc) < 0)
177648940662SDaniel J Blueman 		printk(KERN_INFO "btrfs: misc_deregister failed for control device\n");
1777a9218f6bSChris Mason }
1778a9218f6bSChris Mason 
177985965600SDavid Sterba static void btrfs_print_info(void)
178085965600SDavid Sterba {
178185965600SDavid Sterba 	printk(KERN_INFO "Btrfs loaded"
178285965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG
178385965600SDavid Sterba 			", debug=on"
178485965600SDavid Sterba #endif
178579556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT
178679556c3dSStefan Behrens 			", assert=on"
178779556c3dSStefan Behrens #endif
178885965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
178985965600SDavid Sterba 			", integrity-checker=on"
179085965600SDavid Sterba #endif
179185965600SDavid Sterba 			"\n");
179285965600SDavid Sterba }
179385965600SDavid Sterba 
1794dc11dd5dSJosef Bacik static int btrfs_run_sanity_tests(void)
1795dc11dd5dSJosef Bacik {
179606ea65a3SJosef Bacik 	int ret;
179706ea65a3SJosef Bacik 
1798294e30feSJosef Bacik 	ret = btrfs_init_test_fs();
179906ea65a3SJosef Bacik 	if (ret)
180006ea65a3SJosef Bacik 		return ret;
1801294e30feSJosef Bacik 
1802294e30feSJosef Bacik 	ret = btrfs_test_free_space_cache();
1803294e30feSJosef Bacik 	if (ret)
1804294e30feSJosef Bacik 		goto out;
1805294e30feSJosef Bacik 	ret = btrfs_test_extent_buffer_operations();
1806294e30feSJosef Bacik 	if (ret)
1807294e30feSJosef Bacik 		goto out;
1808294e30feSJosef Bacik 	ret = btrfs_test_extent_io();
1809aaedb55bSJosef Bacik 	if (ret)
1810aaedb55bSJosef Bacik 		goto out;
1811aaedb55bSJosef Bacik 	ret = btrfs_test_inodes();
1812294e30feSJosef Bacik out:
1813294e30feSJosef Bacik 	btrfs_destroy_test_fs();
1814294e30feSJosef Bacik 	return ret;
1815dc11dd5dSJosef Bacik }
1816dc11dd5dSJosef Bacik 
18172e635a27SChris Mason static int __init init_btrfs_fs(void)
18182e635a27SChris Mason {
18192c90e5d6SChris Mason 	int err;
182058176a96SJosef Bacik 
182158176a96SJosef Bacik 	err = btrfs_init_sysfs();
182258176a96SJosef Bacik 	if (err)
182358176a96SJosef Bacik 		return err;
182458176a96SJosef Bacik 
1825143bede5SJeff Mahoney 	btrfs_init_compress();
1826d1310b2eSChris Mason 
1827261507a0SLi Zefan 	err = btrfs_init_cachep();
1828261507a0SLi Zefan 	if (err)
1829261507a0SLi Zefan 		goto free_compress;
1830261507a0SLi Zefan 
1831d1310b2eSChris Mason 	err = extent_io_init();
18322f4cbe64SWyatt Banks 	if (err)
18332f4cbe64SWyatt Banks 		goto free_cachep;
18342f4cbe64SWyatt Banks 
1835d1310b2eSChris Mason 	err = extent_map_init();
1836d1310b2eSChris Mason 	if (err)
1837d1310b2eSChris Mason 		goto free_extent_io;
1838d1310b2eSChris Mason 
18396352b91dSMiao Xie 	err = ordered_data_init();
18402f4cbe64SWyatt Banks 	if (err)
18412f4cbe64SWyatt Banks 		goto free_extent_map;
1842c8b97818SChris Mason 
18436352b91dSMiao Xie 	err = btrfs_delayed_inode_init();
18446352b91dSMiao Xie 	if (err)
18456352b91dSMiao Xie 		goto free_ordered_data;
18466352b91dSMiao Xie 
18479247f317SMiao Xie 	err = btrfs_auto_defrag_init();
184816cdcec7SMiao Xie 	if (err)
184916cdcec7SMiao Xie 		goto free_delayed_inode;
185016cdcec7SMiao Xie 
185178a6184aSMiao Xie 	err = btrfs_delayed_ref_init();
18529247f317SMiao Xie 	if (err)
18539247f317SMiao Xie 		goto free_auto_defrag;
18549247f317SMiao Xie 
1855b9e9a6cbSWang Shilong 	err = btrfs_prelim_ref_init();
1856b9e9a6cbSWang Shilong 	if (err)
1857b9e9a6cbSWang Shilong 		goto free_prelim_ref;
1858b9e9a6cbSWang Shilong 
185978a6184aSMiao Xie 	err = btrfs_interface_init();
186078a6184aSMiao Xie 	if (err)
186178a6184aSMiao Xie 		goto free_delayed_ref;
186278a6184aSMiao Xie 
1863e565d4b9SJan Schmidt 	btrfs_init_lockdep();
1864e565d4b9SJan Schmidt 
186585965600SDavid Sterba 	btrfs_print_info();
1866dc11dd5dSJosef Bacik 
1867dc11dd5dSJosef Bacik 	err = btrfs_run_sanity_tests();
1868dc11dd5dSJosef Bacik 	if (err)
1869dc11dd5dSJosef Bacik 		goto unregister_ioctl;
1870dc11dd5dSJosef Bacik 
1871dc11dd5dSJosef Bacik 	err = register_filesystem(&btrfs_fs_type);
1872dc11dd5dSJosef Bacik 	if (err)
1873dc11dd5dSJosef Bacik 		goto unregister_ioctl;
187474255aa0SJosef Bacik 
18752f4cbe64SWyatt Banks 	return 0;
18762f4cbe64SWyatt Banks 
1877a9218f6bSChris Mason unregister_ioctl:
1878a9218f6bSChris Mason 	btrfs_interface_exit();
1879b9e9a6cbSWang Shilong free_prelim_ref:
1880b9e9a6cbSWang Shilong 	btrfs_prelim_ref_exit();
188178a6184aSMiao Xie free_delayed_ref:
188278a6184aSMiao Xie 	btrfs_delayed_ref_exit();
18839247f317SMiao Xie free_auto_defrag:
18849247f317SMiao Xie 	btrfs_auto_defrag_exit();
188516cdcec7SMiao Xie free_delayed_inode:
188616cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
18876352b91dSMiao Xie free_ordered_data:
18886352b91dSMiao Xie 	ordered_data_exit();
18892f4cbe64SWyatt Banks free_extent_map:
18902f4cbe64SWyatt Banks 	extent_map_exit();
1891d1310b2eSChris Mason free_extent_io:
1892d1310b2eSChris Mason 	extent_io_exit();
18932f4cbe64SWyatt Banks free_cachep:
18942f4cbe64SWyatt Banks 	btrfs_destroy_cachep();
1895261507a0SLi Zefan free_compress:
1896261507a0SLi Zefan 	btrfs_exit_compress();
18972f4cbe64SWyatt Banks 	btrfs_exit_sysfs();
18982c90e5d6SChris Mason 	return err;
18992e635a27SChris Mason }
19002e635a27SChris Mason 
19012e635a27SChris Mason static void __exit exit_btrfs_fs(void)
19022e635a27SChris Mason {
190339279cc3SChris Mason 	btrfs_destroy_cachep();
190478a6184aSMiao Xie 	btrfs_delayed_ref_exit();
19059247f317SMiao Xie 	btrfs_auto_defrag_exit();
190616cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
1907b9e9a6cbSWang Shilong 	btrfs_prelim_ref_exit();
19086352b91dSMiao Xie 	ordered_data_exit();
1909a52d9a80SChris Mason 	extent_map_exit();
1910d1310b2eSChris Mason 	extent_io_exit();
1911a9218f6bSChris Mason 	btrfs_interface_exit();
19122e635a27SChris Mason 	unregister_filesystem(&btrfs_fs_type);
191358176a96SJosef Bacik 	btrfs_exit_sysfs();
19148a4b83ccSChris Mason 	btrfs_cleanup_fs_uuids();
1915261507a0SLi Zefan 	btrfs_exit_compress();
19162e635a27SChris Mason }
19172e635a27SChris Mason 
19182e635a27SChris Mason module_init(init_btrfs_fs)
19192e635a27SChris Mason module_exit(exit_btrfs_fs)
19202e635a27SChris Mason 
19212e635a27SChris Mason MODULE_LICENSE("GPL");
1922