xref: /linux/fs/btrfs/super.c (revision 0c4d2d95d06e920e0c61707e62c7fffc9c57f63a)
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>
444b4e25f2SChris Mason #include "compat.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"
50c5739bbaSChris Mason #include "ioctl.h"
513a686375SChris Mason #include "print-tree.h"
525103e947SJosef Bacik #include "xattr.h"
538a4b83ccSChris Mason #include "volumes.h"
54b3c3da71SChris Mason #include "version.h"
55be6e8dc0SBalaji Rao #include "export.h"
56c8b97818SChris Mason #include "compression.h"
572e635a27SChris Mason 
581abe9b8aSliubo #define CREATE_TRACE_POINTS
591abe9b8aSliubo #include <trace/events/btrfs.h>
601abe9b8aSliubo 
61b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops;
62830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type;
63e20d96d6SChris Mason 
64acce952bSliubo static const char *btrfs_decode_error(struct btrfs_fs_info *fs_info, int errno,
65acce952bSliubo 				      char nbuf[16])
66acce952bSliubo {
67acce952bSliubo 	char *errstr = NULL;
68acce952bSliubo 
69acce952bSliubo 	switch (errno) {
70acce952bSliubo 	case -EIO:
71acce952bSliubo 		errstr = "IO failure";
72acce952bSliubo 		break;
73acce952bSliubo 	case -ENOMEM:
74acce952bSliubo 		errstr = "Out of memory";
75acce952bSliubo 		break;
76acce952bSliubo 	case -EROFS:
77acce952bSliubo 		errstr = "Readonly filesystem";
78acce952bSliubo 		break;
798c342930SJeff Mahoney 	case -EEXIST:
808c342930SJeff Mahoney 		errstr = "Object already exists";
818c342930SJeff Mahoney 		break;
82acce952bSliubo 	default:
83acce952bSliubo 		if (nbuf) {
84acce952bSliubo 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
85acce952bSliubo 				errstr = nbuf;
86acce952bSliubo 		}
87acce952bSliubo 		break;
88acce952bSliubo 	}
89acce952bSliubo 
90acce952bSliubo 	return errstr;
91acce952bSliubo }
92acce952bSliubo 
93acce952bSliubo static void __save_error_info(struct btrfs_fs_info *fs_info)
94acce952bSliubo {
95acce952bSliubo 	/*
96acce952bSliubo 	 * today we only save the error info into ram.  Long term we'll
97acce952bSliubo 	 * also send it down to the disk
98acce952bSliubo 	 */
99acce952bSliubo 	fs_info->fs_state = BTRFS_SUPER_FLAG_ERROR;
100acce952bSliubo }
101acce952bSliubo 
102acce952bSliubo /* NOTE:
103acce952bSliubo  *	We move write_super stuff at umount in order to avoid deadlock
104acce952bSliubo  *	for umount hold all lock.
105acce952bSliubo  */
106acce952bSliubo static void save_error_info(struct btrfs_fs_info *fs_info)
107acce952bSliubo {
108acce952bSliubo 	__save_error_info(fs_info);
109acce952bSliubo }
110acce952bSliubo 
111acce952bSliubo /* btrfs handle error by forcing the filesystem readonly */
112acce952bSliubo static void btrfs_handle_error(struct btrfs_fs_info *fs_info)
113acce952bSliubo {
114acce952bSliubo 	struct super_block *sb = fs_info->sb;
115acce952bSliubo 
116acce952bSliubo 	if (sb->s_flags & MS_RDONLY)
117acce952bSliubo 		return;
118acce952bSliubo 
119acce952bSliubo 	if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
120acce952bSliubo 		sb->s_flags |= MS_RDONLY;
121acce952bSliubo 		printk(KERN_INFO "btrfs is forced readonly\n");
12249b25e05SJeff Mahoney 		__btrfs_scrub_cancel(fs_info);
12349b25e05SJeff Mahoney //		WARN_ON(1);
124acce952bSliubo 	}
125acce952bSliubo }
126acce952bSliubo 
127acce952bSliubo /*
128acce952bSliubo  * __btrfs_std_error decodes expected errors from the caller and
129acce952bSliubo  * invokes the approciate error response.
130acce952bSliubo  */
131acce952bSliubo void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
1324da35113SJeff Mahoney 		       unsigned int line, int errno, const char *fmt, ...)
133acce952bSliubo {
134acce952bSliubo 	struct super_block *sb = fs_info->sb;
135acce952bSliubo 	char nbuf[16];
136acce952bSliubo 	const char *errstr;
1374da35113SJeff Mahoney 	va_list args;
1384da35113SJeff Mahoney 	va_start(args, fmt);
139acce952bSliubo 
140acce952bSliubo 	/*
141acce952bSliubo 	 * Special case: if the error is EROFS, and we're already
142acce952bSliubo 	 * under MS_RDONLY, then it is safe here.
143acce952bSliubo 	 */
144acce952bSliubo 	if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
145acce952bSliubo   		return;
146acce952bSliubo 
147acce952bSliubo   	errstr = btrfs_decode_error(fs_info, errno, nbuf);
1484da35113SJeff Mahoney 	if (fmt) {
1494da35113SJeff Mahoney 		struct va_format vaf = {
1504da35113SJeff Mahoney 			.fmt = fmt,
1514da35113SJeff Mahoney 			.va = &args,
1524da35113SJeff Mahoney 		};
1534da35113SJeff Mahoney 
1544da35113SJeff Mahoney 		printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: %s (%pV)\n",
1554da35113SJeff Mahoney 			sb->s_id, function, line, errstr, &vaf);
1564da35113SJeff Mahoney 	} else {
157acce952bSliubo 		printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: %s\n",
158acce952bSliubo 			sb->s_id, function, line, errstr);
1594da35113SJeff Mahoney 	}
160acce952bSliubo 
1614da35113SJeff Mahoney 	/* Don't go through full error handling during mount */
1624da35113SJeff Mahoney 	if (sb->s_flags & MS_BORN) {
1634da35113SJeff Mahoney 		save_error_info(fs_info);
164acce952bSliubo 		btrfs_handle_error(fs_info);
165acce952bSliubo 	}
1664da35113SJeff Mahoney 	va_end(args);
1674da35113SJeff Mahoney }
1684da35113SJeff Mahoney 
1694da35113SJeff Mahoney const char *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 
1804da35113SJeff Mahoney void btrfs_printk(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];
1874da35113SJeff Mahoney 
1884da35113SJeff Mahoney 	va_start(args, fmt);
1894da35113SJeff Mahoney 
1904da35113SJeff Mahoney 	if (fmt[0] == '<' && isdigit(fmt[1]) && fmt[2] == '>') {
1914da35113SJeff Mahoney 		strncpy(lvl, fmt, 3);
1924da35113SJeff Mahoney 		fmt += 3;
1934da35113SJeff Mahoney 		type = logtypes[fmt[1] - '0'];
1944da35113SJeff Mahoney 	} else
1954da35113SJeff Mahoney 		*lvl = '\0';
1964da35113SJeff Mahoney 
1974da35113SJeff Mahoney 	vaf.fmt = fmt;
1984da35113SJeff Mahoney 	vaf.va = &args;
1994da35113SJeff Mahoney 	printk("%sBTRFS %s (device %s): %pV", lvl, type, sb->s_id, &vaf);
2004da35113SJeff Mahoney }
201acce952bSliubo 
2028c342930SJeff Mahoney /*
20349b25e05SJeff Mahoney  * We only mark the transaction aborted and then set the file system read-only.
20449b25e05SJeff Mahoney  * This will prevent new transactions from starting or trying to join this
20549b25e05SJeff Mahoney  * one.
20649b25e05SJeff Mahoney  *
20749b25e05SJeff Mahoney  * This means that error recovery at the call site is limited to freeing
20849b25e05SJeff Mahoney  * any local memory allocations and passing the error code up without
20949b25e05SJeff Mahoney  * further cleanup. The transaction should complete as it normally would
21049b25e05SJeff Mahoney  * in the call path but will return -EIO.
21149b25e05SJeff Mahoney  *
21249b25e05SJeff Mahoney  * We'll complete the cleanup in btrfs_end_transaction and
21349b25e05SJeff Mahoney  * btrfs_commit_transaction.
21449b25e05SJeff Mahoney  */
21549b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
21649b25e05SJeff Mahoney 			       struct btrfs_root *root, const char *function,
21749b25e05SJeff Mahoney 			       unsigned int line, int errno)
21849b25e05SJeff Mahoney {
21979787eaaSJeff Mahoney 	WARN_ONCE(1, KERN_DEBUG "btrfs: Transaction aborted");
22049b25e05SJeff Mahoney 	trans->aborted = errno;
22149b25e05SJeff Mahoney 	/* Nothing used. The other threads that have joined this
22249b25e05SJeff Mahoney 	 * transaction may be able to continue. */
22349b25e05SJeff Mahoney 	if (!trans->blocks_used) {
22449b25e05SJeff Mahoney 		btrfs_printk(root->fs_info, "Aborting unused transaction.\n");
22549b25e05SJeff Mahoney 		return;
22649b25e05SJeff Mahoney 	}
22749b25e05SJeff Mahoney 	trans->transaction->aborted = errno;
22849b25e05SJeff Mahoney 	__btrfs_std_error(root->fs_info, function, line, errno, NULL);
22949b25e05SJeff Mahoney }
23049b25e05SJeff Mahoney /*
2318c342930SJeff Mahoney  * __btrfs_panic decodes unexpected, fatal errors from the caller,
2328c342930SJeff Mahoney  * issues an alert, and either panics or BUGs, depending on mount options.
2338c342930SJeff Mahoney  */
2348c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
2358c342930SJeff Mahoney 		   unsigned int line, int errno, const char *fmt, ...)
2368c342930SJeff Mahoney {
2378c342930SJeff Mahoney 	char nbuf[16];
2388c342930SJeff Mahoney 	char *s_id = "<unknown>";
2398c342930SJeff Mahoney 	const char *errstr;
2408c342930SJeff Mahoney 	struct va_format vaf = { .fmt = fmt };
2418c342930SJeff Mahoney 	va_list args;
2428c342930SJeff Mahoney 
2438c342930SJeff Mahoney 	if (fs_info)
2448c342930SJeff Mahoney 		s_id = fs_info->sb->s_id;
2458c342930SJeff Mahoney 
2468c342930SJeff Mahoney 	va_start(args, fmt);
2478c342930SJeff Mahoney 	vaf.va = &args;
2488c342930SJeff Mahoney 
2498c342930SJeff Mahoney 	errstr = btrfs_decode_error(fs_info, errno, nbuf);
2508c342930SJeff Mahoney 	if (fs_info->mount_opt & BTRFS_MOUNT_PANIC_ON_FATAL_ERROR)
2518c342930SJeff Mahoney 		panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (%s)\n",
2528c342930SJeff Mahoney 			s_id, function, line, &vaf, errstr);
2538c342930SJeff Mahoney 
2548c342930SJeff Mahoney 	printk(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (%s)\n",
2558c342930SJeff Mahoney 	       s_id, function, line, &vaf, errstr);
2568c342930SJeff Mahoney 	va_end(args);
2578c342930SJeff Mahoney 	/* Caller calls BUG() */
2588c342930SJeff Mahoney }
259e20d96d6SChris Mason 
260e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb)
261e20d96d6SChris Mason {
262815745cfSAl Viro 	(void)close_ctree(btrfs_sb(sb)->tree_root);
263aea52e19SAl Viro 	/* FIXME: need to fix VFS to return error? */
264aea52e19SAl Viro 	/* AV: return it _where_?  ->put_super() can be triggered by any number
265aea52e19SAl Viro 	 * of async events, up to and including delivery of SIGKILL to the
266aea52e19SAl Viro 	 * last process that kept it busy.  Or segfault in the aforementioned
267aea52e19SAl Viro 	 * process...  Whom would you report that to?
268aea52e19SAl Viro 	 */
269e20d96d6SChris Mason }
2702e635a27SChris Mason 
27195e05289SChris Mason enum {
27273f73415SJosef Bacik 	Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
273287a0ab9SJosef Bacik 	Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
274287a0ab9SJosef Bacik 	Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
275261507a0SLi Zefan 	Opt_compress_type, Opt_compress_force, Opt_compress_force_type,
276261507a0SLi Zefan 	Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard,
27791435650SChris Mason 	Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed,
2789555c6c1SIlya Dryomov 	Opt_enospc_debug, Opt_subvolrootid, Opt_defrag, Opt_inode_cache,
2799555c6c1SIlya Dryomov 	Opt_no_space_cache, Opt_recovery, Opt_skip_balance,
28021adbd5cSStefan Behrens 	Opt_check_integrity, Opt_check_integrity_including_extent_data,
2818c342930SJeff Mahoney 	Opt_check_integrity_print_mask, Opt_fatal_errors,
2829555c6c1SIlya Dryomov 	Opt_err,
28395e05289SChris Mason };
28495e05289SChris Mason 
28595e05289SChris Mason static match_table_t tokens = {
286dfe25020SChris Mason 	{Opt_degraded, "degraded"},
28795e05289SChris Mason 	{Opt_subvol, "subvol=%s"},
28873f73415SJosef Bacik 	{Opt_subvolid, "subvolid=%d"},
28943e570b0SChristoph Hellwig 	{Opt_device, "device=%s"},
290b6cda9bcSChris Mason 	{Opt_nodatasum, "nodatasum"},
291be20aa9dSChris Mason 	{Opt_nodatacow, "nodatacow"},
29221ad10cfSChris Mason 	{Opt_nobarrier, "nobarrier"},
2936f568d35SChris Mason 	{Opt_max_inline, "max_inline=%s"},
2948f662a76SChris Mason 	{Opt_alloc_start, "alloc_start=%s"},
2954543df7eSChris Mason 	{Opt_thread_pool, "thread_pool=%d"},
296c8b97818SChris Mason 	{Opt_compress, "compress"},
297261507a0SLi Zefan 	{Opt_compress_type, "compress=%s"},
298a555f810SChris Mason 	{Opt_compress_force, "compress-force"},
299261507a0SLi Zefan 	{Opt_compress_force_type, "compress-force=%s"},
300e18e4809SChris Mason 	{Opt_ssd, "ssd"},
301451d7585SChris Mason 	{Opt_ssd_spread, "ssd_spread"},
3023b30c22fSChris Mason 	{Opt_nossd, "nossd"},
30333268eafSJosef Bacik 	{Opt_noacl, "noacl"},
3043a5e1404SSage Weil 	{Opt_notreelog, "notreelog"},
305dccae999SSage Weil 	{Opt_flushoncommit, "flushoncommit"},
30697e728d4SJosef Bacik 	{Opt_ratio, "metadata_ratio=%d"},
307e244a0aeSChristoph Hellwig 	{Opt_discard, "discard"},
3080af3d00bSJosef Bacik 	{Opt_space_cache, "space_cache"},
30988c2ba3bSJosef Bacik 	{Opt_clear_cache, "clear_cache"},
3104260f7c7SSage Weil 	{Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
31191435650SChris Mason 	{Opt_enospc_debug, "enospc_debug"},
312e15d0542SXin Zhong 	{Opt_subvolrootid, "subvolrootid=%d"},
3134cb5300bSChris Mason 	{Opt_defrag, "autodefrag"},
3144b9465cbSChris Mason 	{Opt_inode_cache, "inode_cache"},
3158965593eSDavid Sterba 	{Opt_no_space_cache, "nospace_cache"},
316af31f5e5SChris Mason 	{Opt_recovery, "recovery"},
3179555c6c1SIlya Dryomov 	{Opt_skip_balance, "skip_balance"},
31821adbd5cSStefan Behrens 	{Opt_check_integrity, "check_int"},
31921adbd5cSStefan Behrens 	{Opt_check_integrity_including_extent_data, "check_int_data"},
32021adbd5cSStefan Behrens 	{Opt_check_integrity_print_mask, "check_int_print_mask=%d"},
3218c342930SJeff Mahoney 	{Opt_fatal_errors, "fatal_errors=%s"},
32233268eafSJosef Bacik 	{Opt_err, NULL},
32395e05289SChris Mason };
32495e05289SChris Mason 
325edf24abeSChristoph Hellwig /*
326edf24abeSChristoph Hellwig  * Regular mount options parser.  Everything that is needed only when
327edf24abeSChristoph Hellwig  * reading in a new superblock is parsed here.
32849b25e05SJeff Mahoney  * XXX JDM: This needs to be cleaned up for remount.
329edf24abeSChristoph Hellwig  */
330edf24abeSChristoph Hellwig int btrfs_parse_options(struct btrfs_root *root, char *options)
33195e05289SChris Mason {
332edf24abeSChristoph Hellwig 	struct btrfs_fs_info *info = root->fs_info;
33395e05289SChris Mason 	substring_t args[MAX_OPT_ARGS];
33473bc1876SJosef Bacik 	char *p, *num, *orig = NULL;
33573bc1876SJosef Bacik 	u64 cache_gen;
3364543df7eSChris Mason 	int intarg;
337a7a3f7caSSage Weil 	int ret = 0;
338261507a0SLi Zefan 	char *compress_type;
339261507a0SLi Zefan 	bool compress_force = false;
340b6cda9bcSChris Mason 
3416c41761fSDavid Sterba 	cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
34273bc1876SJosef Bacik 	if (cache_gen)
34373bc1876SJosef Bacik 		btrfs_set_opt(info->mount_opt, SPACE_CACHE);
34473bc1876SJosef Bacik 
34595e05289SChris Mason 	if (!options)
34673bc1876SJosef Bacik 		goto out;
34795e05289SChris Mason 
348be20aa9dSChris Mason 	/*
349be20aa9dSChris Mason 	 * strsep changes the string, duplicate it because parse_options
350be20aa9dSChris Mason 	 * gets called twice
351be20aa9dSChris Mason 	 */
352be20aa9dSChris Mason 	options = kstrdup(options, GFP_NOFS);
353be20aa9dSChris Mason 	if (!options)
354be20aa9dSChris Mason 		return -ENOMEM;
355be20aa9dSChris Mason 
356da495eccSJosef Bacik 	orig = options;
357be20aa9dSChris Mason 
35895e05289SChris Mason 	while ((p = strsep(&options, ",")) != NULL) {
35995e05289SChris Mason 		int token;
36095e05289SChris Mason 		if (!*p)
36195e05289SChris Mason 			continue;
36295e05289SChris Mason 
36395e05289SChris Mason 		token = match_token(p, tokens, args);
36495e05289SChris Mason 		switch (token) {
365dfe25020SChris Mason 		case Opt_degraded:
366edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: allowing degraded mounts\n");
367dfe25020SChris Mason 			btrfs_set_opt(info->mount_opt, DEGRADED);
368dfe25020SChris Mason 			break;
36995e05289SChris Mason 		case Opt_subvol:
37073f73415SJosef Bacik 		case Opt_subvolid:
371e15d0542SXin Zhong 		case Opt_subvolrootid:
37243e570b0SChristoph Hellwig 		case Opt_device:
373edf24abeSChristoph Hellwig 			/*
37443e570b0SChristoph Hellwig 			 * These are parsed by btrfs_parse_early_options
375edf24abeSChristoph Hellwig 			 * and can be happily ignored here.
376edf24abeSChristoph Hellwig 			 */
37795e05289SChris Mason 			break;
378b6cda9bcSChris Mason 		case Opt_nodatasum:
379067c28adSChris Mason 			printk(KERN_INFO "btrfs: setting nodatasum\n");
380b6cda9bcSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
381be20aa9dSChris Mason 			break;
382be20aa9dSChris Mason 		case Opt_nodatacow:
383edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: setting nodatacow\n");
384be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATACOW);
385be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
386b6cda9bcSChris Mason 			break;
387a555f810SChris Mason 		case Opt_compress_force:
388261507a0SLi Zefan 		case Opt_compress_force_type:
389261507a0SLi Zefan 			compress_force = true;
390261507a0SLi Zefan 		case Opt_compress:
391261507a0SLi Zefan 		case Opt_compress_type:
392261507a0SLi Zefan 			if (token == Opt_compress ||
393261507a0SLi Zefan 			    token == Opt_compress_force ||
394261507a0SLi Zefan 			    strcmp(args[0].from, "zlib") == 0) {
395261507a0SLi Zefan 				compress_type = "zlib";
396261507a0SLi Zefan 				info->compress_type = BTRFS_COMPRESS_ZLIB;
397a6fa6faeSLi Zefan 			} else if (strcmp(args[0].from, "lzo") == 0) {
398a6fa6faeSLi Zefan 				compress_type = "lzo";
399a6fa6faeSLi Zefan 				info->compress_type = BTRFS_COMPRESS_LZO;
400261507a0SLi Zefan 			} else {
401261507a0SLi Zefan 				ret = -EINVAL;
402261507a0SLi Zefan 				goto out;
403261507a0SLi Zefan 			}
404261507a0SLi Zefan 
405a555f810SChris Mason 			btrfs_set_opt(info->mount_opt, COMPRESS);
406261507a0SLi Zefan 			if (compress_force) {
407261507a0SLi Zefan 				btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
408261507a0SLi Zefan 				pr_info("btrfs: force %s compression\n",
409261507a0SLi Zefan 					compress_type);
410261507a0SLi Zefan 			} else
411261507a0SLi Zefan 				pr_info("btrfs: use %s compression\n",
412261507a0SLi Zefan 					compress_type);
413a555f810SChris Mason 			break;
414e18e4809SChris Mason 		case Opt_ssd:
415edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
416e18e4809SChris Mason 			btrfs_set_opt(info->mount_opt, SSD);
417e18e4809SChris Mason 			break;
418451d7585SChris Mason 		case Opt_ssd_spread:
419451d7585SChris Mason 			printk(KERN_INFO "btrfs: use spread ssd "
420451d7585SChris Mason 			       "allocation scheme\n");
421451d7585SChris Mason 			btrfs_set_opt(info->mount_opt, SSD);
422451d7585SChris Mason 			btrfs_set_opt(info->mount_opt, SSD_SPREAD);
423451d7585SChris Mason 			break;
4243b30c22fSChris Mason 		case Opt_nossd:
425451d7585SChris Mason 			printk(KERN_INFO "btrfs: not using ssd allocation "
426451d7585SChris Mason 			       "scheme\n");
427c289811cSChris Mason 			btrfs_set_opt(info->mount_opt, NOSSD);
4283b30c22fSChris Mason 			btrfs_clear_opt(info->mount_opt, SSD);
429451d7585SChris Mason 			btrfs_clear_opt(info->mount_opt, SSD_SPREAD);
4303b30c22fSChris Mason 			break;
43121ad10cfSChris Mason 		case Opt_nobarrier:
432edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: turning off barriers\n");
43321ad10cfSChris Mason 			btrfs_set_opt(info->mount_opt, NOBARRIER);
43421ad10cfSChris Mason 			break;
4354543df7eSChris Mason 		case Opt_thread_pool:
4364543df7eSChris Mason 			intarg = 0;
4374543df7eSChris Mason 			match_int(&args[0], &intarg);
4384543df7eSChris Mason 			if (intarg) {
4394543df7eSChris Mason 				info->thread_pool_size = intarg;
4404543df7eSChris Mason 				printk(KERN_INFO "btrfs: thread pool %d\n",
4414543df7eSChris Mason 				       info->thread_pool_size);
4424543df7eSChris Mason 			}
4434543df7eSChris Mason 			break;
4446f568d35SChris Mason 		case Opt_max_inline:
445edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
4466f568d35SChris Mason 			if (num) {
44791748467SAkinobu Mita 				info->max_inline = memparse(num, NULL);
4486f568d35SChris Mason 				kfree(num);
4496f568d35SChris Mason 
45015ada040SChris Mason 				if (info->max_inline) {
4516f568d35SChris Mason 					info->max_inline = max_t(u64,
45215ada040SChris Mason 						info->max_inline,
45315ada040SChris Mason 						root->sectorsize);
45415ada040SChris Mason 				}
455edf24abeSChristoph Hellwig 				printk(KERN_INFO "btrfs: max_inline at %llu\n",
45621380931SJoel Becker 					(unsigned long long)info->max_inline);
4576f568d35SChris Mason 			}
4586f568d35SChris Mason 			break;
4598f662a76SChris Mason 		case Opt_alloc_start:
460edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
4618f662a76SChris Mason 			if (num) {
46291748467SAkinobu Mita 				info->alloc_start = memparse(num, NULL);
4638f662a76SChris Mason 				kfree(num);
464edf24abeSChristoph Hellwig 				printk(KERN_INFO
465edf24abeSChristoph Hellwig 					"btrfs: allocations start at %llu\n",
46621380931SJoel Becker 					(unsigned long long)info->alloc_start);
4678f662a76SChris Mason 			}
4688f662a76SChris Mason 			break;
46933268eafSJosef Bacik 		case Opt_noacl:
47033268eafSJosef Bacik 			root->fs_info->sb->s_flags &= ~MS_POSIXACL;
47133268eafSJosef Bacik 			break;
4723a5e1404SSage Weil 		case Opt_notreelog:
4733a5e1404SSage Weil 			printk(KERN_INFO "btrfs: disabling tree log\n");
4743a5e1404SSage Weil 			btrfs_set_opt(info->mount_opt, NOTREELOG);
4753a5e1404SSage Weil 			break;
476dccae999SSage Weil 		case Opt_flushoncommit:
477dccae999SSage Weil 			printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
478dccae999SSage Weil 			btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
479dccae999SSage Weil 			break;
48097e728d4SJosef Bacik 		case Opt_ratio:
48197e728d4SJosef Bacik 			intarg = 0;
48297e728d4SJosef Bacik 			match_int(&args[0], &intarg);
48397e728d4SJosef Bacik 			if (intarg) {
48497e728d4SJosef Bacik 				info->metadata_ratio = intarg;
48597e728d4SJosef Bacik 				printk(KERN_INFO "btrfs: metadata ratio %d\n",
48697e728d4SJosef Bacik 				       info->metadata_ratio);
48797e728d4SJosef Bacik 			}
48897e728d4SJosef Bacik 			break;
489e244a0aeSChristoph Hellwig 		case Opt_discard:
490e244a0aeSChristoph Hellwig 			btrfs_set_opt(info->mount_opt, DISCARD);
491e244a0aeSChristoph Hellwig 			break;
4920af3d00bSJosef Bacik 		case Opt_space_cache:
4930af3d00bSJosef Bacik 			btrfs_set_opt(info->mount_opt, SPACE_CACHE);
4940de90876SJosef Bacik 			break;
49573bc1876SJosef Bacik 		case Opt_no_space_cache:
49673bc1876SJosef Bacik 			printk(KERN_INFO "btrfs: disabling disk space caching\n");
49773bc1876SJosef Bacik 			btrfs_clear_opt(info->mount_opt, SPACE_CACHE);
49873bc1876SJosef Bacik 			break;
4994b9465cbSChris Mason 		case Opt_inode_cache:
5004b9465cbSChris Mason 			printk(KERN_INFO "btrfs: enabling inode map caching\n");
5014b9465cbSChris Mason 			btrfs_set_opt(info->mount_opt, INODE_MAP_CACHE);
5024b9465cbSChris Mason 			break;
50388c2ba3bSJosef Bacik 		case Opt_clear_cache:
50488c2ba3bSJosef Bacik 			printk(KERN_INFO "btrfs: force clearing of disk cache\n");
50588c2ba3bSJosef Bacik 			btrfs_set_opt(info->mount_opt, CLEAR_CACHE);
5060af3d00bSJosef Bacik 			break;
5074260f7c7SSage Weil 		case Opt_user_subvol_rm_allowed:
5084260f7c7SSage Weil 			btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
5094260f7c7SSage Weil 			break;
51091435650SChris Mason 		case Opt_enospc_debug:
51191435650SChris Mason 			btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
51291435650SChris Mason 			break;
5134cb5300bSChris Mason 		case Opt_defrag:
5144cb5300bSChris Mason 			printk(KERN_INFO "btrfs: enabling auto defrag");
5154cb5300bSChris Mason 			btrfs_set_opt(info->mount_opt, AUTO_DEFRAG);
5164cb5300bSChris Mason 			break;
517af31f5e5SChris Mason 		case Opt_recovery:
518af31f5e5SChris Mason 			printk(KERN_INFO "btrfs: enabling auto recovery");
519af31f5e5SChris Mason 			btrfs_set_opt(info->mount_opt, RECOVERY);
520af31f5e5SChris Mason 			break;
5219555c6c1SIlya Dryomov 		case Opt_skip_balance:
5229555c6c1SIlya Dryomov 			btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
5239555c6c1SIlya Dryomov 			break;
52421adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
52521adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
52621adbd5cSStefan Behrens 			printk(KERN_INFO "btrfs: enabling check integrity"
52721adbd5cSStefan Behrens 			       " including extent data\n");
52821adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt,
52921adbd5cSStefan Behrens 				      CHECK_INTEGRITY_INCLUDING_EXTENT_DATA);
53021adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
53121adbd5cSStefan Behrens 			break;
53221adbd5cSStefan Behrens 		case Opt_check_integrity:
53321adbd5cSStefan Behrens 			printk(KERN_INFO "btrfs: enabling check integrity\n");
53421adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
53521adbd5cSStefan Behrens 			break;
53621adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
53721adbd5cSStefan Behrens 			intarg = 0;
53821adbd5cSStefan Behrens 			match_int(&args[0], &intarg);
53921adbd5cSStefan Behrens 			if (intarg) {
54021adbd5cSStefan Behrens 				info->check_integrity_print_mask = intarg;
54121adbd5cSStefan Behrens 				printk(KERN_INFO "btrfs:"
54221adbd5cSStefan Behrens 				       " check_integrity_print_mask 0x%x\n",
54321adbd5cSStefan Behrens 				       info->check_integrity_print_mask);
54421adbd5cSStefan Behrens 			}
54521adbd5cSStefan Behrens 			break;
54621adbd5cSStefan Behrens #else
54721adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
54821adbd5cSStefan Behrens 		case Opt_check_integrity:
54921adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
55021adbd5cSStefan Behrens 			printk(KERN_ERR "btrfs: support for check_integrity*"
55121adbd5cSStefan Behrens 			       " not compiled in!\n");
55221adbd5cSStefan Behrens 			ret = -EINVAL;
55321adbd5cSStefan Behrens 			goto out;
55421adbd5cSStefan Behrens #endif
5558c342930SJeff Mahoney 		case Opt_fatal_errors:
5568c342930SJeff Mahoney 			if (strcmp(args[0].from, "panic") == 0)
5578c342930SJeff Mahoney 				btrfs_set_opt(info->mount_opt,
5588c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
5598c342930SJeff Mahoney 			else if (strcmp(args[0].from, "bug") == 0)
5608c342930SJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
5618c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
5628c342930SJeff Mahoney 			else {
5638c342930SJeff Mahoney 				ret = -EINVAL;
5648c342930SJeff Mahoney 				goto out;
5658c342930SJeff Mahoney 			}
5668c342930SJeff Mahoney 			break;
567a7a3f7caSSage Weil 		case Opt_err:
568a7a3f7caSSage Weil 			printk(KERN_INFO "btrfs: unrecognized mount option "
569a7a3f7caSSage Weil 			       "'%s'\n", p);
570a7a3f7caSSage Weil 			ret = -EINVAL;
571a7a3f7caSSage Weil 			goto out;
57295e05289SChris Mason 		default:
573be20aa9dSChris Mason 			break;
57495e05289SChris Mason 		}
57595e05289SChris Mason 	}
576a7a3f7caSSage Weil out:
57773bc1876SJosef Bacik 	if (!ret && btrfs_test_opt(root, SPACE_CACHE))
57873bc1876SJosef Bacik 		printk(KERN_INFO "btrfs: disk space caching is enabled\n");
579da495eccSJosef Bacik 	kfree(orig);
580a7a3f7caSSage Weil 	return ret;
581edf24abeSChristoph Hellwig }
582edf24abeSChristoph Hellwig 
583edf24abeSChristoph Hellwig /*
584edf24abeSChristoph Hellwig  * Parse mount options that are required early in the mount process.
585edf24abeSChristoph Hellwig  *
586edf24abeSChristoph Hellwig  * All other options will be parsed on much later in the mount process and
587edf24abeSChristoph Hellwig  * only when we need to allocate a new super block.
588edf24abeSChristoph Hellwig  */
58997288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags,
59073f73415SJosef Bacik 		void *holder, char **subvol_name, u64 *subvol_objectid,
591e15d0542SXin Zhong 		u64 *subvol_rootid, struct btrfs_fs_devices **fs_devices)
592edf24abeSChristoph Hellwig {
593edf24abeSChristoph Hellwig 	substring_t args[MAX_OPT_ARGS];
59483c8c9bdSJeff Liu 	char *device_name, *opts, *orig, *p;
595edf24abeSChristoph Hellwig 	int error = 0;
59673f73415SJosef Bacik 	int intarg;
597edf24abeSChristoph Hellwig 
598edf24abeSChristoph Hellwig 	if (!options)
599830c4adbSJosef Bacik 		return 0;
600edf24abeSChristoph Hellwig 
601edf24abeSChristoph Hellwig 	/*
602edf24abeSChristoph Hellwig 	 * strsep changes the string, duplicate it because parse_options
603edf24abeSChristoph Hellwig 	 * gets called twice
604edf24abeSChristoph Hellwig 	 */
605edf24abeSChristoph Hellwig 	opts = kstrdup(options, GFP_KERNEL);
606edf24abeSChristoph Hellwig 	if (!opts)
607edf24abeSChristoph Hellwig 		return -ENOMEM;
6083f3d0bc0STero Roponen 	orig = opts;
609edf24abeSChristoph Hellwig 
610edf24abeSChristoph Hellwig 	while ((p = strsep(&opts, ",")) != NULL) {
611edf24abeSChristoph Hellwig 		int token;
612edf24abeSChristoph Hellwig 		if (!*p)
613edf24abeSChristoph Hellwig 			continue;
614edf24abeSChristoph Hellwig 
615edf24abeSChristoph Hellwig 		token = match_token(p, tokens, args);
616edf24abeSChristoph Hellwig 		switch (token) {
617edf24abeSChristoph Hellwig 		case Opt_subvol:
618a90e8b6fSIlya Dryomov 			kfree(*subvol_name);
619edf24abeSChristoph Hellwig 			*subvol_name = match_strdup(&args[0]);
620edf24abeSChristoph Hellwig 			break;
62173f73415SJosef Bacik 		case Opt_subvolid:
62273f73415SJosef Bacik 			intarg = 0;
6234849f01dSJosef Bacik 			error = match_int(&args[0], &intarg);
6244849f01dSJosef Bacik 			if (!error) {
6254849f01dSJosef Bacik 				/* we want the original fs_tree */
6264849f01dSJosef Bacik 				if (!intarg)
6274849f01dSJosef Bacik 					*subvol_objectid =
6284849f01dSJosef Bacik 						BTRFS_FS_TREE_OBJECTID;
6294849f01dSJosef Bacik 				else
63073f73415SJosef Bacik 					*subvol_objectid = intarg;
6314849f01dSJosef Bacik 			}
63273f73415SJosef Bacik 			break;
633e15d0542SXin Zhong 		case Opt_subvolrootid:
634e15d0542SXin Zhong 			intarg = 0;
635e15d0542SXin Zhong 			error = match_int(&args[0], &intarg);
636e15d0542SXin Zhong 			if (!error) {
637e15d0542SXin Zhong 				/* we want the original fs_tree */
638e15d0542SXin Zhong 				if (!intarg)
639e15d0542SXin Zhong 					*subvol_rootid =
640e15d0542SXin Zhong 						BTRFS_FS_TREE_OBJECTID;
641e15d0542SXin Zhong 				else
642e15d0542SXin Zhong 					*subvol_rootid = intarg;
643e15d0542SXin Zhong 			}
644e15d0542SXin Zhong 			break;
64543e570b0SChristoph Hellwig 		case Opt_device:
64683c8c9bdSJeff Liu 			device_name = match_strdup(&args[0]);
64783c8c9bdSJeff Liu 			if (!device_name) {
64883c8c9bdSJeff Liu 				error = -ENOMEM;
64983c8c9bdSJeff Liu 				goto out;
65083c8c9bdSJeff Liu 			}
65183c8c9bdSJeff Liu 			error = btrfs_scan_one_device(device_name,
65243e570b0SChristoph Hellwig 					flags, holder, fs_devices);
65383c8c9bdSJeff Liu 			kfree(device_name);
65443e570b0SChristoph Hellwig 			if (error)
655830c4adbSJosef Bacik 				goto out;
65643e570b0SChristoph Hellwig 			break;
657edf24abeSChristoph Hellwig 		default:
658edf24abeSChristoph Hellwig 			break;
659edf24abeSChristoph Hellwig 		}
660edf24abeSChristoph Hellwig 	}
661edf24abeSChristoph Hellwig 
662edf24abeSChristoph Hellwig out:
663830c4adbSJosef Bacik 	kfree(orig);
664edf24abeSChristoph Hellwig 	return error;
66595e05289SChris Mason }
66695e05289SChris Mason 
66773f73415SJosef Bacik static struct dentry *get_default_root(struct super_block *sb,
66873f73415SJosef Bacik 				       u64 subvol_objectid)
66973f73415SJosef Bacik {
670815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
671815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
67273f73415SJosef Bacik 	struct btrfs_root *new_root;
67373f73415SJosef Bacik 	struct btrfs_dir_item *di;
67473f73415SJosef Bacik 	struct btrfs_path *path;
67573f73415SJosef Bacik 	struct btrfs_key location;
67673f73415SJosef Bacik 	struct inode *inode;
67773f73415SJosef Bacik 	u64 dir_id;
67873f73415SJosef Bacik 	int new = 0;
67973f73415SJosef Bacik 
68073f73415SJosef Bacik 	/*
68173f73415SJosef Bacik 	 * We have a specific subvol we want to mount, just setup location and
68273f73415SJosef Bacik 	 * go look up the root.
68373f73415SJosef Bacik 	 */
68473f73415SJosef Bacik 	if (subvol_objectid) {
68573f73415SJosef Bacik 		location.objectid = subvol_objectid;
68673f73415SJosef Bacik 		location.type = BTRFS_ROOT_ITEM_KEY;
68773f73415SJosef Bacik 		location.offset = (u64)-1;
68873f73415SJosef Bacik 		goto find_root;
68973f73415SJosef Bacik 	}
69073f73415SJosef Bacik 
69173f73415SJosef Bacik 	path = btrfs_alloc_path();
69273f73415SJosef Bacik 	if (!path)
69373f73415SJosef Bacik 		return ERR_PTR(-ENOMEM);
69473f73415SJosef Bacik 	path->leave_spinning = 1;
69573f73415SJosef Bacik 
69673f73415SJosef Bacik 	/*
69773f73415SJosef Bacik 	 * Find the "default" dir item which points to the root item that we
69873f73415SJosef Bacik 	 * will mount by default if we haven't been given a specific subvolume
69973f73415SJosef Bacik 	 * to mount.
70073f73415SJosef Bacik 	 */
701815745cfSAl Viro 	dir_id = btrfs_super_root_dir(fs_info->super_copy);
70273f73415SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
703b0839166SJulia Lawall 	if (IS_ERR(di)) {
704b0839166SJulia Lawall 		btrfs_free_path(path);
705fb4f6f91SDan Carpenter 		return ERR_CAST(di);
706b0839166SJulia Lawall 	}
70773f73415SJosef Bacik 	if (!di) {
70873f73415SJosef Bacik 		/*
70973f73415SJosef Bacik 		 * Ok the default dir item isn't there.  This is weird since
71073f73415SJosef Bacik 		 * it's always been there, but don't freak out, just try and
71173f73415SJosef Bacik 		 * mount to root most subvolume.
71273f73415SJosef Bacik 		 */
71373f73415SJosef Bacik 		btrfs_free_path(path);
71473f73415SJosef Bacik 		dir_id = BTRFS_FIRST_FREE_OBJECTID;
715815745cfSAl Viro 		new_root = fs_info->fs_root;
71673f73415SJosef Bacik 		goto setup_root;
71773f73415SJosef Bacik 	}
71873f73415SJosef Bacik 
71973f73415SJosef Bacik 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
72073f73415SJosef Bacik 	btrfs_free_path(path);
72173f73415SJosef Bacik 
72273f73415SJosef Bacik find_root:
723815745cfSAl Viro 	new_root = btrfs_read_fs_root_no_name(fs_info, &location);
72473f73415SJosef Bacik 	if (IS_ERR(new_root))
725d0b678cbSJulia Lawall 		return ERR_CAST(new_root);
72673f73415SJosef Bacik 
72773f73415SJosef Bacik 	if (btrfs_root_refs(&new_root->root_item) == 0)
72873f73415SJosef Bacik 		return ERR_PTR(-ENOENT);
72973f73415SJosef Bacik 
73073f73415SJosef Bacik 	dir_id = btrfs_root_dirid(&new_root->root_item);
73173f73415SJosef Bacik setup_root:
73273f73415SJosef Bacik 	location.objectid = dir_id;
73373f73415SJosef Bacik 	location.type = BTRFS_INODE_ITEM_KEY;
73473f73415SJosef Bacik 	location.offset = 0;
73573f73415SJosef Bacik 
73673f73415SJosef Bacik 	inode = btrfs_iget(sb, &location, new_root, &new);
7374cbd1149SDan Carpenter 	if (IS_ERR(inode))
7384cbd1149SDan Carpenter 		return ERR_CAST(inode);
73973f73415SJosef Bacik 
74073f73415SJosef Bacik 	/*
74173f73415SJosef Bacik 	 * If we're just mounting the root most subvol put the inode and return
74273f73415SJosef Bacik 	 * a reference to the dentry.  We will have already gotten a reference
74373f73415SJosef Bacik 	 * to the inode in btrfs_fill_super so we're good to go.
74473f73415SJosef Bacik 	 */
74573f73415SJosef Bacik 	if (!new && sb->s_root->d_inode == inode) {
74673f73415SJosef Bacik 		iput(inode);
74773f73415SJosef Bacik 		return dget(sb->s_root);
74873f73415SJosef Bacik 	}
74973f73415SJosef Bacik 
750ba5b8958SJosef Bacik 	return d_obtain_alias(inode);
75173f73415SJosef Bacik }
75273f73415SJosef Bacik 
7538a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb,
7548a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
7558a4b83ccSChris Mason 			    void *data, int silent)
7562e635a27SChris Mason {
7572e635a27SChris Mason 	struct inode *inode;
758815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
7595d4f98a2SYan Zheng 	struct btrfs_key key;
76039279cc3SChris Mason 	int err;
7612e635a27SChris Mason 
7622e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
7632e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
764e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
765af53d29aSAl Viro 	sb->s_d_op = &btrfs_dentry_operations;
766be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
7675103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
7682e635a27SChris Mason 	sb->s_time_gran = 1;
7690eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL
77033268eafSJosef Bacik 	sb->s_flags |= MS_POSIXACL;
77149cf6f45SChris Ball #endif
772*0c4d2d95SJosef Bacik 	sb->s_flags |= MS_I_VERSION;
773ad2b2c80SAl Viro 	err = open_ctree(sb, fs_devices, (char *)data);
774ad2b2c80SAl Viro 	if (err) {
775e20d96d6SChris Mason 		printk("btrfs: open_ctree failed\n");
776ad2b2c80SAl Viro 		return err;
777e20d96d6SChris Mason 	}
778b888db2bSChris Mason 
7795d4f98a2SYan Zheng 	key.objectid = BTRFS_FIRST_FREE_OBJECTID;
7805d4f98a2SYan Zheng 	key.type = BTRFS_INODE_ITEM_KEY;
7815d4f98a2SYan Zheng 	key.offset = 0;
78298c7089cSAl Viro 	inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL);
7835d4f98a2SYan Zheng 	if (IS_ERR(inode)) {
7845d4f98a2SYan Zheng 		err = PTR_ERR(inode);
78539279cc3SChris Mason 		goto fail_close;
78639279cc3SChris Mason 	}
7872e635a27SChris Mason 
78848fde701SAl Viro 	sb->s_root = d_make_root(inode);
78948fde701SAl Viro 	if (!sb->s_root) {
79039279cc3SChris Mason 		err = -ENOMEM;
79139279cc3SChris Mason 		goto fail_close;
7922e635a27SChris Mason 	}
79358176a96SJosef Bacik 
7946885f308SChris Mason 	save_mount_options(sb, data);
79590a887c9SDan Magenheimer 	cleancache_init_fs(sb);
79659553edfSAl Viro 	sb->s_flags |= MS_ACTIVE;
7972e635a27SChris Mason 	return 0;
7982e635a27SChris Mason 
79939279cc3SChris Mason fail_close:
800815745cfSAl Viro 	close_ctree(fs_info->tree_root);
801d5719762SChris Mason 	return err;
802d5719762SChris Mason }
803d5719762SChris Mason 
8046bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
805d5719762SChris Mason {
806d5719762SChris Mason 	struct btrfs_trans_handle *trans;
807815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
808815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
809d5719762SChris Mason 	int ret;
810df2ce34cSChris Mason 
8111abe9b8aSliubo 	trace_btrfs_sync_fs(wait);
8121abe9b8aSliubo 
813d561c025SChris Mason 	if (!wait) {
814815745cfSAl Viro 		filemap_flush(fs_info->btree_inode->i_mapping);
815df2ce34cSChris Mason 		return 0;
816d561c025SChris Mason 	}
817771ed689SChris Mason 
81824bbcf04SYan, Zheng 	btrfs_wait_ordered_extents(root, 0, 0);
819771ed689SChris Mason 
820a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 0);
82198d5dc13STsutomu Itoh 	if (IS_ERR(trans))
82298d5dc13STsutomu Itoh 		return PTR_ERR(trans);
823d5719762SChris Mason 	ret = btrfs_commit_transaction(trans, root);
82454aa1f4dSChris Mason 	return ret;
825d5719762SChris Mason }
826d5719762SChris Mason 
82734c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
828a9572a15SEric Paris {
829815745cfSAl Viro 	struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
830815745cfSAl Viro 	struct btrfs_root *root = info->tree_root;
831200da64eSTsutomu Itoh 	char *compress_type;
832a9572a15SEric Paris 
833a9572a15SEric Paris 	if (btrfs_test_opt(root, DEGRADED))
834a9572a15SEric Paris 		seq_puts(seq, ",degraded");
835a9572a15SEric Paris 	if (btrfs_test_opt(root, NODATASUM))
836a9572a15SEric Paris 		seq_puts(seq, ",nodatasum");
837a9572a15SEric Paris 	if (btrfs_test_opt(root, NODATACOW))
838a9572a15SEric Paris 		seq_puts(seq, ",nodatacow");
839a9572a15SEric Paris 	if (btrfs_test_opt(root, NOBARRIER))
840a9572a15SEric Paris 		seq_puts(seq, ",nobarrier");
841a9572a15SEric Paris 	if (info->max_inline != 8192 * 1024)
84221380931SJoel Becker 		seq_printf(seq, ",max_inline=%llu",
84321380931SJoel Becker 			   (unsigned long long)info->max_inline);
844a9572a15SEric Paris 	if (info->alloc_start != 0)
84521380931SJoel Becker 		seq_printf(seq, ",alloc_start=%llu",
84621380931SJoel Becker 			   (unsigned long long)info->alloc_start);
847a9572a15SEric Paris 	if (info->thread_pool_size !=  min_t(unsigned long,
848a9572a15SEric Paris 					     num_online_cpus() + 2, 8))
849a9572a15SEric Paris 		seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
850200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, COMPRESS)) {
851200da64eSTsutomu Itoh 		if (info->compress_type == BTRFS_COMPRESS_ZLIB)
852200da64eSTsutomu Itoh 			compress_type = "zlib";
853200da64eSTsutomu Itoh 		else
854200da64eSTsutomu Itoh 			compress_type = "lzo";
855200da64eSTsutomu Itoh 		if (btrfs_test_opt(root, FORCE_COMPRESS))
856200da64eSTsutomu Itoh 			seq_printf(seq, ",compress-force=%s", compress_type);
857200da64eSTsutomu Itoh 		else
858200da64eSTsutomu Itoh 			seq_printf(seq, ",compress=%s", compress_type);
859200da64eSTsutomu Itoh 	}
860c289811cSChris Mason 	if (btrfs_test_opt(root, NOSSD))
861c289811cSChris Mason 		seq_puts(seq, ",nossd");
862451d7585SChris Mason 	if (btrfs_test_opt(root, SSD_SPREAD))
863451d7585SChris Mason 		seq_puts(seq, ",ssd_spread");
864451d7585SChris Mason 	else if (btrfs_test_opt(root, SSD))
865a9572a15SEric Paris 		seq_puts(seq, ",ssd");
8663a5e1404SSage Weil 	if (btrfs_test_opt(root, NOTREELOG))
8676b65c5c6SSage Weil 		seq_puts(seq, ",notreelog");
868dccae999SSage Weil 	if (btrfs_test_opt(root, FLUSHONCOMMIT))
8696b65c5c6SSage Weil 		seq_puts(seq, ",flushoncommit");
87020a5239aSMatthew Wilcox 	if (btrfs_test_opt(root, DISCARD))
87120a5239aSMatthew Wilcox 		seq_puts(seq, ",discard");
872a9572a15SEric Paris 	if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
873a9572a15SEric Paris 		seq_puts(seq, ",noacl");
874200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, SPACE_CACHE))
875200da64eSTsutomu Itoh 		seq_puts(seq, ",space_cache");
87673bc1876SJosef Bacik 	else
8778965593eSDavid Sterba 		seq_puts(seq, ",nospace_cache");
878200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, CLEAR_CACHE))
879200da64eSTsutomu Itoh 		seq_puts(seq, ",clear_cache");
880200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
881200da64eSTsutomu Itoh 		seq_puts(seq, ",user_subvol_rm_allowed");
8820942caa3SDavid Sterba 	if (btrfs_test_opt(root, ENOSPC_DEBUG))
8830942caa3SDavid Sterba 		seq_puts(seq, ",enospc_debug");
8840942caa3SDavid Sterba 	if (btrfs_test_opt(root, AUTO_DEFRAG))
8850942caa3SDavid Sterba 		seq_puts(seq, ",autodefrag");
8860942caa3SDavid Sterba 	if (btrfs_test_opt(root, INODE_MAP_CACHE))
8870942caa3SDavid Sterba 		seq_puts(seq, ",inode_cache");
8889555c6c1SIlya Dryomov 	if (btrfs_test_opt(root, SKIP_BALANCE))
8899555c6c1SIlya Dryomov 		seq_puts(seq, ",skip_balance");
8908c342930SJeff Mahoney 	if (btrfs_test_opt(root, PANIC_ON_FATAL_ERROR))
8918c342930SJeff Mahoney 		seq_puts(seq, ",fatal_errors=panic");
892a9572a15SEric Paris 	return 0;
893a9572a15SEric Paris }
894a9572a15SEric Paris 
895a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data)
8962e635a27SChris Mason {
897815745cfSAl Viro 	struct btrfs_fs_info *p = data;
898815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(s);
8994b82d6e4SYan 
900815745cfSAl Viro 	return fs_info->fs_devices == p->fs_devices;
9014b82d6e4SYan }
9024b82d6e4SYan 
903450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data)
904450ba0eaSJosef Bacik {
9056de1d09dSAl Viro 	int err = set_anon_super(s, data);
9066de1d09dSAl Viro 	if (!err)
907450ba0eaSJosef Bacik 		s->s_fs_info = data;
9086de1d09dSAl Viro 	return err;
909450ba0eaSJosef Bacik }
910450ba0eaSJosef Bacik 
911830c4adbSJosef Bacik /*
912f9d9ef62SDavid Sterba  * subvolumes are identified by ino 256
913f9d9ef62SDavid Sterba  */
914f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode)
915f9d9ef62SDavid Sterba {
916f9d9ef62SDavid Sterba 	if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
917f9d9ef62SDavid Sterba 		return 1;
918f9d9ef62SDavid Sterba 	return 0;
919f9d9ef62SDavid Sterba }
920f9d9ef62SDavid Sterba 
921f9d9ef62SDavid Sterba /*
922830c4adbSJosef Bacik  * This will strip out the subvol=%s argument for an argument string and add
923830c4adbSJosef Bacik  * subvolid=0 to make sure we get the actual tree root for path walking to the
924830c4adbSJosef Bacik  * subvol we want.
925830c4adbSJosef Bacik  */
926830c4adbSJosef Bacik static char *setup_root_args(char *args)
927830c4adbSJosef Bacik {
928830c4adbSJosef Bacik 	unsigned copied = 0;
929830c4adbSJosef Bacik 	unsigned len = strlen(args) + 2;
930830c4adbSJosef Bacik 	char *pos;
931830c4adbSJosef Bacik 	char *ret;
932830c4adbSJosef Bacik 
933830c4adbSJosef Bacik 	/*
934830c4adbSJosef Bacik 	 * We need the same args as before, but minus
935830c4adbSJosef Bacik 	 *
936830c4adbSJosef Bacik 	 * subvol=a
937830c4adbSJosef Bacik 	 *
938830c4adbSJosef Bacik 	 * and add
939830c4adbSJosef Bacik 	 *
940830c4adbSJosef Bacik 	 * subvolid=0
941830c4adbSJosef Bacik 	 *
942830c4adbSJosef Bacik 	 * which is a difference of 2 characters, so we allocate strlen(args) +
943830c4adbSJosef Bacik 	 * 2 characters.
944830c4adbSJosef Bacik 	 */
945830c4adbSJosef Bacik 	ret = kzalloc(len * sizeof(char), GFP_NOFS);
946830c4adbSJosef Bacik 	if (!ret)
947830c4adbSJosef Bacik 		return NULL;
948830c4adbSJosef Bacik 	pos = strstr(args, "subvol=");
949830c4adbSJosef Bacik 
950830c4adbSJosef Bacik 	/* This shouldn't happen, but just in case.. */
951830c4adbSJosef Bacik 	if (!pos) {
952830c4adbSJosef Bacik 		kfree(ret);
953830c4adbSJosef Bacik 		return NULL;
954830c4adbSJosef Bacik 	}
955830c4adbSJosef Bacik 
956830c4adbSJosef Bacik 	/*
957830c4adbSJosef Bacik 	 * The subvol=<> arg is not at the front of the string, copy everybody
958830c4adbSJosef Bacik 	 * up to that into ret.
959830c4adbSJosef Bacik 	 */
960830c4adbSJosef Bacik 	if (pos != args) {
961830c4adbSJosef Bacik 		*pos = '\0';
962830c4adbSJosef Bacik 		strcpy(ret, args);
963830c4adbSJosef Bacik 		copied += strlen(args);
964830c4adbSJosef Bacik 		pos++;
965830c4adbSJosef Bacik 	}
966830c4adbSJosef Bacik 
967830c4adbSJosef Bacik 	strncpy(ret + copied, "subvolid=0", len - copied);
968830c4adbSJosef Bacik 
969830c4adbSJosef Bacik 	/* Length of subvolid=0 */
970830c4adbSJosef Bacik 	copied += 10;
971830c4adbSJosef Bacik 
972830c4adbSJosef Bacik 	/*
973830c4adbSJosef Bacik 	 * If there is no , after the subvol= option then we know there's no
974830c4adbSJosef Bacik 	 * other options and we can just return.
975830c4adbSJosef Bacik 	 */
976830c4adbSJosef Bacik 	pos = strchr(pos, ',');
977830c4adbSJosef Bacik 	if (!pos)
978830c4adbSJosef Bacik 		return ret;
979830c4adbSJosef Bacik 
980830c4adbSJosef Bacik 	/* Copy the rest of the arguments into our buffer */
981830c4adbSJosef Bacik 	strncpy(ret + copied, pos, len - copied);
982830c4adbSJosef Bacik 	copied += strlen(pos);
983830c4adbSJosef Bacik 
984830c4adbSJosef Bacik 	return ret;
985830c4adbSJosef Bacik }
986830c4adbSJosef Bacik 
987830c4adbSJosef Bacik static struct dentry *mount_subvol(const char *subvol_name, int flags,
988830c4adbSJosef Bacik 				   const char *device_name, char *data)
989830c4adbSJosef Bacik {
990830c4adbSJosef Bacik 	struct dentry *root;
991830c4adbSJosef Bacik 	struct vfsmount *mnt;
992830c4adbSJosef Bacik 	char *newargs;
993830c4adbSJosef Bacik 
994830c4adbSJosef Bacik 	newargs = setup_root_args(data);
995830c4adbSJosef Bacik 	if (!newargs)
996830c4adbSJosef Bacik 		return ERR_PTR(-ENOMEM);
997830c4adbSJosef Bacik 	mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name,
998830c4adbSJosef Bacik 			     newargs);
999830c4adbSJosef Bacik 	kfree(newargs);
1000830c4adbSJosef Bacik 	if (IS_ERR(mnt))
1001830c4adbSJosef Bacik 		return ERR_CAST(mnt);
1002830c4adbSJosef Bacik 
1003ea441d11SAl Viro 	root = mount_subtree(mnt, subvol_name);
1004830c4adbSJosef Bacik 
1005ea441d11SAl Viro 	if (!IS_ERR(root) && !is_subvolume_inode(root->d_inode)) {
1006ea441d11SAl Viro 		struct super_block *s = root->d_sb;
1007ea441d11SAl Viro 		dput(root);
1008ea441d11SAl Viro 		root = ERR_PTR(-EINVAL);
1009ea441d11SAl Viro 		deactivate_locked_super(s);
1010f9d9ef62SDavid Sterba 		printk(KERN_ERR "btrfs: '%s' is not a valid subvolume\n",
1011f9d9ef62SDavid Sterba 				subvol_name);
1012f9d9ef62SDavid Sterba 	}
1013f9d9ef62SDavid Sterba 
1014830c4adbSJosef Bacik 	return root;
1015830c4adbSJosef Bacik }
1016450ba0eaSJosef Bacik 
1017edf24abeSChristoph Hellwig /*
1018edf24abeSChristoph Hellwig  * Find a superblock for the given device / mount point.
1019edf24abeSChristoph Hellwig  *
1020edf24abeSChristoph Hellwig  * Note:  This is based on get_sb_bdev from fs/super.c with a few additions
1021edf24abeSChristoph Hellwig  *	  for multiple device setup.  Make sure to keep it in sync.
1022edf24abeSChristoph Hellwig  */
1023061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
1024306e16ceSDavid Sterba 		const char *device_name, void *data)
10254b82d6e4SYan {
10264b82d6e4SYan 	struct block_device *bdev = NULL;
10274b82d6e4SYan 	struct super_block *s;
10284b82d6e4SYan 	struct dentry *root;
10298a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices = NULL;
1030450ba0eaSJosef Bacik 	struct btrfs_fs_info *fs_info = NULL;
103197288f2cSChristoph Hellwig 	fmode_t mode = FMODE_READ;
103273f73415SJosef Bacik 	char *subvol_name = NULL;
103373f73415SJosef Bacik 	u64 subvol_objectid = 0;
1034e15d0542SXin Zhong 	u64 subvol_rootid = 0;
10354b82d6e4SYan 	int error = 0;
10364b82d6e4SYan 
103797288f2cSChristoph Hellwig 	if (!(flags & MS_RDONLY))
103897288f2cSChristoph Hellwig 		mode |= FMODE_WRITE;
103997288f2cSChristoph Hellwig 
104097288f2cSChristoph Hellwig 	error = btrfs_parse_early_options(data, mode, fs_type,
104173f73415SJosef Bacik 					  &subvol_name, &subvol_objectid,
1042e15d0542SXin Zhong 					  &subvol_rootid, &fs_devices);
1043f23c8af8SIlya Dryomov 	if (error) {
1044f23c8af8SIlya Dryomov 		kfree(subvol_name);
1045061dbc6bSAl Viro 		return ERR_PTR(error);
1046f23c8af8SIlya Dryomov 	}
1047edf24abeSChristoph Hellwig 
1048830c4adbSJosef Bacik 	if (subvol_name) {
1049830c4adbSJosef Bacik 		root = mount_subvol(subvol_name, flags, device_name, data);
1050830c4adbSJosef Bacik 		kfree(subvol_name);
1051830c4adbSJosef Bacik 		return root;
1052830c4adbSJosef Bacik 	}
1053830c4adbSJosef Bacik 
1054306e16ceSDavid Sterba 	error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
10558a4b83ccSChris Mason 	if (error)
1056830c4adbSJosef Bacik 		return ERR_PTR(error);
10574b82d6e4SYan 
1058450ba0eaSJosef Bacik 	/*
1059450ba0eaSJosef Bacik 	 * Setup a dummy root and fs_info for test/set super.  This is because
1060450ba0eaSJosef Bacik 	 * we don't actually fill this stuff out until open_ctree, but we need
1061450ba0eaSJosef Bacik 	 * it for searching for existing supers, so this lets us do that and
1062450ba0eaSJosef Bacik 	 * then open_ctree will properly initialize everything later.
1063450ba0eaSJosef Bacik 	 */
1064450ba0eaSJosef Bacik 	fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
106504d21a24SIlya Dryomov 	if (!fs_info)
106604d21a24SIlya Dryomov 		return ERR_PTR(-ENOMEM);
106704d21a24SIlya Dryomov 
1068450ba0eaSJosef Bacik 	fs_info->fs_devices = fs_devices;
1069450ba0eaSJosef Bacik 
10706c41761fSDavid Sterba 	fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
10716c41761fSDavid Sterba 	fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
10726c41761fSDavid Sterba 	if (!fs_info->super_copy || !fs_info->super_for_commit) {
10736c41761fSDavid Sterba 		error = -ENOMEM;
107404d21a24SIlya Dryomov 		goto error_fs_info;
107504d21a24SIlya Dryomov 	}
107604d21a24SIlya Dryomov 
107704d21a24SIlya Dryomov 	error = btrfs_open_devices(fs_devices, mode, fs_type);
107804d21a24SIlya Dryomov 	if (error)
107904d21a24SIlya Dryomov 		goto error_fs_info;
108004d21a24SIlya Dryomov 
108104d21a24SIlya Dryomov 	if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
108204d21a24SIlya Dryomov 		error = -EACCES;
10836c41761fSDavid Sterba 		goto error_close_devices;
10846c41761fSDavid Sterba 	}
10856c41761fSDavid Sterba 
1086dfe25020SChris Mason 	bdev = fs_devices->latest_bdev;
1087815745cfSAl Viro 	s = sget(fs_type, btrfs_test_super, btrfs_set_super, fs_info);
1088830c4adbSJosef Bacik 	if (IS_ERR(s)) {
1089830c4adbSJosef Bacik 		error = PTR_ERR(s);
1090830c4adbSJosef Bacik 		goto error_close_devices;
1091830c4adbSJosef Bacik 	}
10924b82d6e4SYan 
10934b82d6e4SYan 	if (s->s_root) {
10942b82032cSYan Zheng 		btrfs_close_devices(fs_devices);
10956c41761fSDavid Sterba 		free_fs_info(fs_info);
109659553edfSAl Viro 		if ((flags ^ s->s_flags) & MS_RDONLY)
109759553edfSAl Viro 			error = -EBUSY;
10984b82d6e4SYan 	} else {
10994b82d6e4SYan 		char b[BDEVNAME_SIZE];
11004b82d6e4SYan 
11019e1f1de0SAl Viro 		s->s_flags = flags | MS_NOSEC;
11024b82d6e4SYan 		strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
1103815745cfSAl Viro 		btrfs_sb(s)->bdev_holder = fs_type;
11048a4b83ccSChris Mason 		error = btrfs_fill_super(s, fs_devices, data,
11058a4b83ccSChris Mason 					 flags & MS_SILENT ? 1 : 0);
11064b82d6e4SYan 	}
11074b82d6e4SYan 
110859553edfSAl Viro 	root = !error ? get_default_root(s, subvol_objectid) : ERR_PTR(error);
110959553edfSAl Viro 	if (IS_ERR(root))
1110e15d0542SXin Zhong 		deactivate_locked_super(s);
11114b82d6e4SYan 
1112061dbc6bSAl Viro 	return root;
11134b82d6e4SYan 
1114c146afadSYan Zheng error_close_devices:
11158a4b83ccSChris Mason 	btrfs_close_devices(fs_devices);
111604d21a24SIlya Dryomov error_fs_info:
11176c41761fSDavid Sterba 	free_fs_info(fs_info);
1118061dbc6bSAl Viro 	return ERR_PTR(error);
11194b82d6e4SYan }
11202e635a27SChris Mason 
1121c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1122c146afadSYan Zheng {
1123815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1124815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
112549b25e05SJeff Mahoney 	unsigned old_flags = sb->s_flags;
112649b25e05SJeff Mahoney 	unsigned long old_opts = fs_info->mount_opt;
112749b25e05SJeff Mahoney 	unsigned long old_compress_type = fs_info->compress_type;
112849b25e05SJeff Mahoney 	u64 old_max_inline = fs_info->max_inline;
112949b25e05SJeff Mahoney 	u64 old_alloc_start = fs_info->alloc_start;
113049b25e05SJeff Mahoney 	int old_thread_pool_size = fs_info->thread_pool_size;
113149b25e05SJeff Mahoney 	unsigned int old_metadata_ratio = fs_info->metadata_ratio;
1132c146afadSYan Zheng 	int ret;
1133c146afadSYan Zheng 
1134b288052eSChris Mason 	ret = btrfs_parse_options(root, data);
113549b25e05SJeff Mahoney 	if (ret) {
113649b25e05SJeff Mahoney 		ret = -EINVAL;
113749b25e05SJeff Mahoney 		goto restore;
113849b25e05SJeff Mahoney 	}
1139b288052eSChris Mason 
1140c146afadSYan Zheng 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
1141c146afadSYan Zheng 		return 0;
1142c146afadSYan Zheng 
1143c146afadSYan Zheng 	if (*flags & MS_RDONLY) {
1144c146afadSYan Zheng 		sb->s_flags |= MS_RDONLY;
1145c146afadSYan Zheng 
1146c146afadSYan Zheng 		ret = btrfs_commit_super(root);
114749b25e05SJeff Mahoney 		if (ret)
114849b25e05SJeff Mahoney 			goto restore;
1149c146afadSYan Zheng 	} else {
11508a3db184SSergei Trofimovich 		if (fs_info->fs_devices->rw_devices == 0) {
115149b25e05SJeff Mahoney 			ret = -EACCES;
115249b25e05SJeff Mahoney 			goto restore;
11538a3db184SSergei Trofimovich 		}
11542b82032cSYan Zheng 
11558a3db184SSergei Trofimovich 		if (btrfs_super_log_root(fs_info->super_copy) != 0) {
115649b25e05SJeff Mahoney 			ret = -EINVAL;
115749b25e05SJeff Mahoney 			goto restore;
11588a3db184SSergei Trofimovich 		}
1159c146afadSYan Zheng 
1160815745cfSAl Viro 		ret = btrfs_cleanup_fs_roots(fs_info);
116149b25e05SJeff Mahoney 		if (ret)
116249b25e05SJeff Mahoney 			goto restore;
1163c146afadSYan Zheng 
1164d68fc57bSYan, Zheng 		/* recover relocation */
1165d68fc57bSYan, Zheng 		ret = btrfs_recover_relocation(root);
116649b25e05SJeff Mahoney 		if (ret)
116749b25e05SJeff Mahoney 			goto restore;
1168c146afadSYan Zheng 
1169c146afadSYan Zheng 		sb->s_flags &= ~MS_RDONLY;
1170c146afadSYan Zheng 	}
1171c146afadSYan Zheng 
1172c146afadSYan Zheng 	return 0;
117349b25e05SJeff Mahoney 
117449b25e05SJeff Mahoney restore:
117549b25e05SJeff Mahoney 	/* We've hit an error - don't reset MS_RDONLY */
117649b25e05SJeff Mahoney 	if (sb->s_flags & MS_RDONLY)
117749b25e05SJeff Mahoney 		old_flags |= MS_RDONLY;
117849b25e05SJeff Mahoney 	sb->s_flags = old_flags;
117949b25e05SJeff Mahoney 	fs_info->mount_opt = old_opts;
118049b25e05SJeff Mahoney 	fs_info->compress_type = old_compress_type;
118149b25e05SJeff Mahoney 	fs_info->max_inline = old_max_inline;
118249b25e05SJeff Mahoney 	fs_info->alloc_start = old_alloc_start;
118349b25e05SJeff Mahoney 	fs_info->thread_pool_size = old_thread_pool_size;
118449b25e05SJeff Mahoney 	fs_info->metadata_ratio = old_metadata_ratio;
118549b25e05SJeff Mahoney 	return ret;
1186c146afadSYan Zheng }
1187c146afadSYan Zheng 
1188bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */
1189bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1,
1190bcd53741SArne Jansen 				       const void *dev_info2)
1191bcd53741SArne Jansen {
1192bcd53741SArne Jansen 	if (((struct btrfs_device_info *)dev_info1)->max_avail >
1193bcd53741SArne Jansen 	    ((struct btrfs_device_info *)dev_info2)->max_avail)
1194bcd53741SArne Jansen 		return -1;
1195bcd53741SArne Jansen 	else if (((struct btrfs_device_info *)dev_info1)->max_avail <
1196bcd53741SArne Jansen 		 ((struct btrfs_device_info *)dev_info2)->max_avail)
1197bcd53741SArne Jansen 		return 1;
1198bcd53741SArne Jansen 	else
1199bcd53741SArne Jansen 	return 0;
1200bcd53741SArne Jansen }
1201bcd53741SArne Jansen 
1202bcd53741SArne Jansen /*
1203bcd53741SArne Jansen  * sort the devices by max_avail, in which max free extent size of each device
1204bcd53741SArne Jansen  * is stored.(Descending Sort)
1205bcd53741SArne Jansen  */
1206bcd53741SArne Jansen static inline void btrfs_descending_sort_devices(
1207bcd53741SArne Jansen 					struct btrfs_device_info *devices,
1208bcd53741SArne Jansen 					size_t nr_devices)
1209bcd53741SArne Jansen {
1210bcd53741SArne Jansen 	sort(devices, nr_devices, sizeof(struct btrfs_device_info),
1211bcd53741SArne Jansen 	     btrfs_cmp_device_free_bytes, NULL);
1212bcd53741SArne Jansen }
1213bcd53741SArne Jansen 
12146d07bcecSMiao Xie /*
12156d07bcecSMiao Xie  * The helper to calc the free space on the devices that can be used to store
12166d07bcecSMiao Xie  * file data.
12176d07bcecSMiao Xie  */
12186d07bcecSMiao Xie static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
12196d07bcecSMiao Xie {
12206d07bcecSMiao Xie 	struct btrfs_fs_info *fs_info = root->fs_info;
12216d07bcecSMiao Xie 	struct btrfs_device_info *devices_info;
12226d07bcecSMiao Xie 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
12236d07bcecSMiao Xie 	struct btrfs_device *device;
12246d07bcecSMiao Xie 	u64 skip_space;
12256d07bcecSMiao Xie 	u64 type;
12266d07bcecSMiao Xie 	u64 avail_space;
12276d07bcecSMiao Xie 	u64 used_space;
12286d07bcecSMiao Xie 	u64 min_stripe_size;
122939fb26c3SMiao Xie 	int min_stripes = 1, num_stripes = 1;
12306d07bcecSMiao Xie 	int i = 0, nr_devices;
12316d07bcecSMiao Xie 	int ret;
12326d07bcecSMiao Xie 
1233b772a86eSLi Zefan 	nr_devices = fs_info->fs_devices->open_devices;
12346d07bcecSMiao Xie 	BUG_ON(!nr_devices);
12356d07bcecSMiao Xie 
12366d07bcecSMiao Xie 	devices_info = kmalloc(sizeof(*devices_info) * nr_devices,
12376d07bcecSMiao Xie 			       GFP_NOFS);
12386d07bcecSMiao Xie 	if (!devices_info)
12396d07bcecSMiao Xie 		return -ENOMEM;
12406d07bcecSMiao Xie 
12416d07bcecSMiao Xie 	/* calc min stripe number for data space alloction */
12426d07bcecSMiao Xie 	type = btrfs_get_alloc_profile(root, 1);
124339fb26c3SMiao Xie 	if (type & BTRFS_BLOCK_GROUP_RAID0) {
12446d07bcecSMiao Xie 		min_stripes = 2;
124539fb26c3SMiao Xie 		num_stripes = nr_devices;
124639fb26c3SMiao Xie 	} else if (type & BTRFS_BLOCK_GROUP_RAID1) {
12476d07bcecSMiao Xie 		min_stripes = 2;
124839fb26c3SMiao Xie 		num_stripes = 2;
124939fb26c3SMiao Xie 	} else if (type & BTRFS_BLOCK_GROUP_RAID10) {
12506d07bcecSMiao Xie 		min_stripes = 4;
125139fb26c3SMiao Xie 		num_stripes = 4;
125239fb26c3SMiao Xie 	}
12536d07bcecSMiao Xie 
12546d07bcecSMiao Xie 	if (type & BTRFS_BLOCK_GROUP_DUP)
12556d07bcecSMiao Xie 		min_stripe_size = 2 * BTRFS_STRIPE_LEN;
12566d07bcecSMiao Xie 	else
12576d07bcecSMiao Xie 		min_stripe_size = BTRFS_STRIPE_LEN;
12586d07bcecSMiao Xie 
1259b772a86eSLi Zefan 	list_for_each_entry(device, &fs_devices->devices, dev_list) {
1260b772a86eSLi Zefan 		if (!device->in_fs_metadata || !device->bdev)
12616d07bcecSMiao Xie 			continue;
12626d07bcecSMiao Xie 
12636d07bcecSMiao Xie 		avail_space = device->total_bytes - device->bytes_used;
12646d07bcecSMiao Xie 
12656d07bcecSMiao Xie 		/* align with stripe_len */
12666d07bcecSMiao Xie 		do_div(avail_space, BTRFS_STRIPE_LEN);
12676d07bcecSMiao Xie 		avail_space *= BTRFS_STRIPE_LEN;
12686d07bcecSMiao Xie 
12696d07bcecSMiao Xie 		/*
12706d07bcecSMiao Xie 		 * In order to avoid overwritting the superblock on the drive,
12716d07bcecSMiao Xie 		 * btrfs starts at an offset of at least 1MB when doing chunk
12726d07bcecSMiao Xie 		 * allocation.
12736d07bcecSMiao Xie 		 */
12746d07bcecSMiao Xie 		skip_space = 1024 * 1024;
12756d07bcecSMiao Xie 
12766d07bcecSMiao Xie 		/* user can set the offset in fs_info->alloc_start. */
12776d07bcecSMiao Xie 		if (fs_info->alloc_start + BTRFS_STRIPE_LEN <=
12786d07bcecSMiao Xie 		    device->total_bytes)
12796d07bcecSMiao Xie 			skip_space = max(fs_info->alloc_start, skip_space);
12806d07bcecSMiao Xie 
12816d07bcecSMiao Xie 		/*
12826d07bcecSMiao Xie 		 * btrfs can not use the free space in [0, skip_space - 1],
12836d07bcecSMiao Xie 		 * we must subtract it from the total. In order to implement
12846d07bcecSMiao Xie 		 * it, we account the used space in this range first.
12856d07bcecSMiao Xie 		 */
12866d07bcecSMiao Xie 		ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1,
12876d07bcecSMiao Xie 						     &used_space);
12886d07bcecSMiao Xie 		if (ret) {
12896d07bcecSMiao Xie 			kfree(devices_info);
12906d07bcecSMiao Xie 			return ret;
12916d07bcecSMiao Xie 		}
12926d07bcecSMiao Xie 
12936d07bcecSMiao Xie 		/* calc the free space in [0, skip_space - 1] */
12946d07bcecSMiao Xie 		skip_space -= used_space;
12956d07bcecSMiao Xie 
12966d07bcecSMiao Xie 		/*
12976d07bcecSMiao Xie 		 * we can use the free space in [0, skip_space - 1], subtract
12986d07bcecSMiao Xie 		 * it from the total.
12996d07bcecSMiao Xie 		 */
13006d07bcecSMiao Xie 		if (avail_space && avail_space >= skip_space)
13016d07bcecSMiao Xie 			avail_space -= skip_space;
13026d07bcecSMiao Xie 		else
13036d07bcecSMiao Xie 			avail_space = 0;
13046d07bcecSMiao Xie 
13056d07bcecSMiao Xie 		if (avail_space < min_stripe_size)
13066d07bcecSMiao Xie 			continue;
13076d07bcecSMiao Xie 
13086d07bcecSMiao Xie 		devices_info[i].dev = device;
13096d07bcecSMiao Xie 		devices_info[i].max_avail = avail_space;
13106d07bcecSMiao Xie 
13116d07bcecSMiao Xie 		i++;
13126d07bcecSMiao Xie 	}
13136d07bcecSMiao Xie 
13146d07bcecSMiao Xie 	nr_devices = i;
13156d07bcecSMiao Xie 
13166d07bcecSMiao Xie 	btrfs_descending_sort_devices(devices_info, nr_devices);
13176d07bcecSMiao Xie 
13186d07bcecSMiao Xie 	i = nr_devices - 1;
13196d07bcecSMiao Xie 	avail_space = 0;
13206d07bcecSMiao Xie 	while (nr_devices >= min_stripes) {
132139fb26c3SMiao Xie 		if (num_stripes > nr_devices)
132239fb26c3SMiao Xie 			num_stripes = nr_devices;
132339fb26c3SMiao Xie 
13246d07bcecSMiao Xie 		if (devices_info[i].max_avail >= min_stripe_size) {
13256d07bcecSMiao Xie 			int j;
13266d07bcecSMiao Xie 			u64 alloc_size;
13276d07bcecSMiao Xie 
132839fb26c3SMiao Xie 			avail_space += devices_info[i].max_avail * num_stripes;
13296d07bcecSMiao Xie 			alloc_size = devices_info[i].max_avail;
133039fb26c3SMiao Xie 			for (j = i + 1 - num_stripes; j <= i; j++)
13316d07bcecSMiao Xie 				devices_info[j].max_avail -= alloc_size;
13326d07bcecSMiao Xie 		}
13336d07bcecSMiao Xie 		i--;
13346d07bcecSMiao Xie 		nr_devices--;
13356d07bcecSMiao Xie 	}
13366d07bcecSMiao Xie 
13376d07bcecSMiao Xie 	kfree(devices_info);
13386d07bcecSMiao Xie 	*free_bytes = avail_space;
13396d07bcecSMiao Xie 	return 0;
13406d07bcecSMiao Xie }
13416d07bcecSMiao Xie 
13428fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
13438fd17795SChris Mason {
1344815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
1345815745cfSAl Viro 	struct btrfs_super_block *disk_super = fs_info->super_copy;
1346815745cfSAl Viro 	struct list_head *head = &fs_info->space_info;
1347bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
1348bd4d1088SJosef Bacik 	u64 total_used = 0;
13496d07bcecSMiao Xie 	u64 total_free_data = 0;
1350db94535dSChris Mason 	int bits = dentry->d_sb->s_blocksize_bits;
1351815745cfSAl Viro 	__be32 *fsid = (__be32 *)fs_info->fsid;
13526d07bcecSMiao Xie 	int ret;
13538fd17795SChris Mason 
13546d07bcecSMiao Xie 	/* holding chunk_muext to avoid allocating new chunks */
1355815745cfSAl Viro 	mutex_lock(&fs_info->chunk_mutex);
1356bd4d1088SJosef Bacik 	rcu_read_lock();
135789a55897SJosef Bacik 	list_for_each_entry_rcu(found, head, list) {
13586d07bcecSMiao Xie 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
13596d07bcecSMiao Xie 			total_free_data += found->disk_total - found->disk_used;
13606d07bcecSMiao Xie 			total_free_data -=
13616d07bcecSMiao Xie 				btrfs_account_ro_block_groups_free_space(found);
13626d07bcecSMiao Xie 		}
13636d07bcecSMiao Xie 
1364b742bb82SYan, Zheng 		total_used += found->disk_used;
136589a55897SJosef Bacik 	}
1366bd4d1088SJosef Bacik 	rcu_read_unlock();
1367bd4d1088SJosef Bacik 
13688fd17795SChris Mason 	buf->f_namelen = BTRFS_NAME_LEN;
1369db94535dSChris Mason 	buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
1370bd4d1088SJosef Bacik 	buf->f_bfree = buf->f_blocks - (total_used >> bits);
13718fd17795SChris Mason 	buf->f_bsize = dentry->d_sb->s_blocksize;
13728fd17795SChris Mason 	buf->f_type = BTRFS_SUPER_MAGIC;
13736d07bcecSMiao Xie 	buf->f_bavail = total_free_data;
1374815745cfSAl Viro 	ret = btrfs_calc_avail_data_space(fs_info->tree_root, &total_free_data);
13756d07bcecSMiao Xie 	if (ret) {
1376815745cfSAl Viro 		mutex_unlock(&fs_info->chunk_mutex);
13776d07bcecSMiao Xie 		return ret;
13786d07bcecSMiao Xie 	}
13796d07bcecSMiao Xie 	buf->f_bavail += total_free_data;
13806d07bcecSMiao Xie 	buf->f_bavail = buf->f_bavail >> bits;
1381815745cfSAl Viro 	mutex_unlock(&fs_info->chunk_mutex);
1382d397712bSChris Mason 
13839d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
13849d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
13859d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
13869d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
13879d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
138832d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
138932d48fa1SDavid Woodhouse 	buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
139032d48fa1SDavid Woodhouse 	buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
139132d48fa1SDavid Woodhouse 
13928fd17795SChris Mason 	return 0;
13938fd17795SChris Mason }
1394b5133862SChris Mason 
1395aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb)
1396aea52e19SAl Viro {
1397815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1398aea52e19SAl Viro 	kill_anon_super(sb);
1399aea52e19SAl Viro 	free_fs_info(fs_info);
1400aea52e19SAl Viro }
1401aea52e19SAl Viro 
14022e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
14032e635a27SChris Mason 	.owner		= THIS_MODULE,
14042e635a27SChris Mason 	.name		= "btrfs",
1405061dbc6bSAl Viro 	.mount		= btrfs_mount,
1406aea52e19SAl Viro 	.kill_sb	= btrfs_kill_super,
14072e635a27SChris Mason 	.fs_flags	= FS_REQUIRES_DEV,
14082e635a27SChris Mason };
1409a9218f6bSChris Mason 
1410d352ac68SChris Mason /*
1411d352ac68SChris Mason  * used by btrfsctl to scan devices when no FS is mounted
1412d352ac68SChris Mason  */
14138a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
14148a4b83ccSChris Mason 				unsigned long arg)
14158a4b83ccSChris Mason {
14168a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
14178a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices;
1418c071fcfdSChris Mason 	int ret = -ENOTTY;
14198a4b83ccSChris Mason 
1420e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1421e441d54dSChris Mason 		return -EPERM;
1422e441d54dSChris Mason 
1423dae7b665SLi Zefan 	vol = memdup_user((void __user *)arg, sizeof(*vol));
1424dae7b665SLi Zefan 	if (IS_ERR(vol))
1425dae7b665SLi Zefan 		return PTR_ERR(vol);
1426c071fcfdSChris Mason 
14278a4b83ccSChris Mason 	switch (cmd) {
14288a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
142997288f2cSChristoph Hellwig 		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
14308a4b83ccSChris Mason 					    &btrfs_fs_type, &fs_devices);
14318a4b83ccSChris Mason 		break;
14328a4b83ccSChris Mason 	}
1433dae7b665SLi Zefan 
14348a4b83ccSChris Mason 	kfree(vol);
1435f819d837SLinda Knippers 	return ret;
14368a4b83ccSChris Mason }
14378a4b83ccSChris Mason 
14380176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
1439ed0dab6bSYan {
1440815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1441815745cfSAl Viro 	mutex_lock(&fs_info->transaction_kthread_mutex);
1442815745cfSAl Viro 	mutex_lock(&fs_info->cleaner_mutex);
14430176260fSLinus Torvalds 	return 0;
1444ed0dab6bSYan }
1445ed0dab6bSYan 
14460176260fSLinus Torvalds static int btrfs_unfreeze(struct super_block *sb)
1447ed0dab6bSYan {
1448815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1449815745cfSAl Viro 	mutex_unlock(&fs_info->cleaner_mutex);
1450815745cfSAl Viro 	mutex_unlock(&fs_info->transaction_kthread_mutex);
14510176260fSLinus Torvalds 	return 0;
1452ed0dab6bSYan }
14532e635a27SChris Mason 
145422c44fe6SJosef Bacik static void btrfs_fs_dirty_inode(struct inode *inode, int flags)
145522c44fe6SJosef Bacik {
145622c44fe6SJosef Bacik 	int ret;
145722c44fe6SJosef Bacik 
145822c44fe6SJosef Bacik 	ret = btrfs_dirty_inode(inode);
145922c44fe6SJosef Bacik 	if (ret)
146022c44fe6SJosef Bacik 		printk_ratelimited(KERN_ERR "btrfs: fail to dirty inode %Lu "
146122c44fe6SJosef Bacik 				   "error %d\n", btrfs_ino(inode), ret);
146222c44fe6SJosef Bacik }
146322c44fe6SJosef Bacik 
1464b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
146576dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
1466bd555975SAl Viro 	.evict_inode	= btrfs_evict_inode,
1467e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
1468d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
1469a9572a15SEric Paris 	.show_options	= btrfs_show_options,
14704730a4bcSChris Mason 	.write_inode	= btrfs_write_inode,
147122c44fe6SJosef Bacik 	.dirty_inode	= btrfs_fs_dirty_inode,
14722c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
14732c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
14748fd17795SChris Mason 	.statfs		= btrfs_statfs,
1475c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
14760176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
14770176260fSLinus Torvalds 	.unfreeze_fs	= btrfs_unfreeze,
1478e20d96d6SChris Mason };
1479a9218f6bSChris Mason 
1480a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
1481a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
1482a9218f6bSChris Mason 	.compat_ioctl = btrfs_control_ioctl,
1483a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
14846038f373SArnd Bergmann 	.llseek = noop_llseek,
1485a9218f6bSChris Mason };
1486a9218f6bSChris Mason 
1487a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
1488578454ffSKay Sievers 	.minor		= BTRFS_MINOR,
1489a9218f6bSChris Mason 	.name		= "btrfs-control",
1490a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
1491a9218f6bSChris Mason };
1492a9218f6bSChris Mason 
1493578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
1494578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control");
1495578454ffSKay Sievers 
1496a9218f6bSChris Mason static int btrfs_interface_init(void)
1497a9218f6bSChris Mason {
1498a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
1499a9218f6bSChris Mason }
1500a9218f6bSChris Mason 
1501b2950863SChristoph Hellwig static void btrfs_interface_exit(void)
1502a9218f6bSChris Mason {
1503a9218f6bSChris Mason 	if (misc_deregister(&btrfs_misc) < 0)
1504d397712bSChris Mason 		printk(KERN_INFO "misc_deregister failed for control device");
1505a9218f6bSChris Mason }
1506a9218f6bSChris Mason 
15072e635a27SChris Mason static int __init init_btrfs_fs(void)
15082e635a27SChris Mason {
15092c90e5d6SChris Mason 	int err;
151058176a96SJosef Bacik 
151158176a96SJosef Bacik 	err = btrfs_init_sysfs();
151258176a96SJosef Bacik 	if (err)
151358176a96SJosef Bacik 		return err;
151458176a96SJosef Bacik 
1515143bede5SJeff Mahoney 	btrfs_init_compress();
1516d1310b2eSChris Mason 
1517261507a0SLi Zefan 	err = btrfs_init_cachep();
1518261507a0SLi Zefan 	if (err)
1519261507a0SLi Zefan 		goto free_compress;
1520261507a0SLi Zefan 
1521d1310b2eSChris Mason 	err = extent_io_init();
15222f4cbe64SWyatt Banks 	if (err)
15232f4cbe64SWyatt Banks 		goto free_cachep;
15242f4cbe64SWyatt Banks 
1525d1310b2eSChris Mason 	err = extent_map_init();
1526d1310b2eSChris Mason 	if (err)
1527d1310b2eSChris Mason 		goto free_extent_io;
1528d1310b2eSChris Mason 
152916cdcec7SMiao Xie 	err = btrfs_delayed_inode_init();
15302f4cbe64SWyatt Banks 	if (err)
15312f4cbe64SWyatt Banks 		goto free_extent_map;
1532c8b97818SChris Mason 
153316cdcec7SMiao Xie 	err = btrfs_interface_init();
153416cdcec7SMiao Xie 	if (err)
153516cdcec7SMiao Xie 		goto free_delayed_inode;
153616cdcec7SMiao Xie 
1537a9218f6bSChris Mason 	err = register_filesystem(&btrfs_fs_type);
1538a9218f6bSChris Mason 	if (err)
1539a9218f6bSChris Mason 		goto unregister_ioctl;
1540b3c3da71SChris Mason 
1541e565d4b9SJan Schmidt 	btrfs_init_lockdep();
1542e565d4b9SJan Schmidt 
1543b3c3da71SChris Mason 	printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
15442f4cbe64SWyatt Banks 	return 0;
15452f4cbe64SWyatt Banks 
1546a9218f6bSChris Mason unregister_ioctl:
1547a9218f6bSChris Mason 	btrfs_interface_exit();
154816cdcec7SMiao Xie free_delayed_inode:
154916cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
15502f4cbe64SWyatt Banks free_extent_map:
15512f4cbe64SWyatt Banks 	extent_map_exit();
1552d1310b2eSChris Mason free_extent_io:
1553d1310b2eSChris Mason 	extent_io_exit();
15542f4cbe64SWyatt Banks free_cachep:
15552f4cbe64SWyatt Banks 	btrfs_destroy_cachep();
1556261507a0SLi Zefan free_compress:
1557261507a0SLi Zefan 	btrfs_exit_compress();
15582f4cbe64SWyatt Banks 	btrfs_exit_sysfs();
15592c90e5d6SChris Mason 	return err;
15602e635a27SChris Mason }
15612e635a27SChris Mason 
15622e635a27SChris Mason static void __exit exit_btrfs_fs(void)
15632e635a27SChris Mason {
156439279cc3SChris Mason 	btrfs_destroy_cachep();
156516cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
1566a52d9a80SChris Mason 	extent_map_exit();
1567d1310b2eSChris Mason 	extent_io_exit();
1568a9218f6bSChris Mason 	btrfs_interface_exit();
15692e635a27SChris Mason 	unregister_filesystem(&btrfs_fs_type);
157058176a96SJosef Bacik 	btrfs_exit_sysfs();
15718a4b83ccSChris Mason 	btrfs_cleanup_fs_uuids();
1572261507a0SLi Zefan 	btrfs_exit_compress();
15732e635a27SChris Mason }
15742e635a27SChris Mason 
15752e635a27SChris Mason module_init(init_btrfs_fs)
15762e635a27SChris Mason module_exit(exit_btrfs_fs)
15772e635a27SChris Mason 
15782e635a27SChris Mason MODULE_LICENSE("GPL");
1579