xref: /linux/fs/btrfs/super.c (revision 98d5dc13e7e74b77ca3b4c3cbded9f48d2dbbbb7)
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>
424b4e25f2SChris Mason #include "compat.h"
432e635a27SChris Mason #include "ctree.h"
44e20d96d6SChris Mason #include "disk-io.h"
45d5719762SChris Mason #include "transaction.h"
462c90e5d6SChris Mason #include "btrfs_inode.h"
47c5739bbaSChris Mason #include "ioctl.h"
483a686375SChris Mason #include "print-tree.h"
495103e947SJosef Bacik #include "xattr.h"
508a4b83ccSChris Mason #include "volumes.h"
51b3c3da71SChris Mason #include "version.h"
52be6e8dc0SBalaji Rao #include "export.h"
53c8b97818SChris Mason #include "compression.h"
542e635a27SChris Mason 
55b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops;
56e20d96d6SChris Mason 
57acce952bSliubo static const char *btrfs_decode_error(struct btrfs_fs_info *fs_info, int errno,
58acce952bSliubo 				      char nbuf[16])
59acce952bSliubo {
60acce952bSliubo 	char *errstr = NULL;
61acce952bSliubo 
62acce952bSliubo 	switch (errno) {
63acce952bSliubo 	case -EIO:
64acce952bSliubo 		errstr = "IO failure";
65acce952bSliubo 		break;
66acce952bSliubo 	case -ENOMEM:
67acce952bSliubo 		errstr = "Out of memory";
68acce952bSliubo 		break;
69acce952bSliubo 	case -EROFS:
70acce952bSliubo 		errstr = "Readonly filesystem";
71acce952bSliubo 		break;
72acce952bSliubo 	default:
73acce952bSliubo 		if (nbuf) {
74acce952bSliubo 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
75acce952bSliubo 				errstr = nbuf;
76acce952bSliubo 		}
77acce952bSliubo 		break;
78acce952bSliubo 	}
79acce952bSliubo 
80acce952bSliubo 	return errstr;
81acce952bSliubo }
82acce952bSliubo 
83acce952bSliubo static void __save_error_info(struct btrfs_fs_info *fs_info)
84acce952bSliubo {
85acce952bSliubo 	/*
86acce952bSliubo 	 * today we only save the error info into ram.  Long term we'll
87acce952bSliubo 	 * also send it down to the disk
88acce952bSliubo 	 */
89acce952bSliubo 	fs_info->fs_state = BTRFS_SUPER_FLAG_ERROR;
90acce952bSliubo }
91acce952bSliubo 
92acce952bSliubo /* NOTE:
93acce952bSliubo  *	We move write_super stuff at umount in order to avoid deadlock
94acce952bSliubo  *	for umount hold all lock.
95acce952bSliubo  */
96acce952bSliubo static void save_error_info(struct btrfs_fs_info *fs_info)
97acce952bSliubo {
98acce952bSliubo 	__save_error_info(fs_info);
99acce952bSliubo }
100acce952bSliubo 
101acce952bSliubo /* btrfs handle error by forcing the filesystem readonly */
102acce952bSliubo static void btrfs_handle_error(struct btrfs_fs_info *fs_info)
103acce952bSliubo {
104acce952bSliubo 	struct super_block *sb = fs_info->sb;
105acce952bSliubo 
106acce952bSliubo 	if (sb->s_flags & MS_RDONLY)
107acce952bSliubo 		return;
108acce952bSliubo 
109acce952bSliubo 	if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
110acce952bSliubo 		sb->s_flags |= MS_RDONLY;
111acce952bSliubo 		printk(KERN_INFO "btrfs is forced readonly\n");
112acce952bSliubo 	}
113acce952bSliubo }
114acce952bSliubo 
115acce952bSliubo /*
116acce952bSliubo  * __btrfs_std_error decodes expected errors from the caller and
117acce952bSliubo  * invokes the approciate error response.
118acce952bSliubo  */
119acce952bSliubo void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
120acce952bSliubo 		     unsigned int line, int errno)
121acce952bSliubo {
122acce952bSliubo 	struct super_block *sb = fs_info->sb;
123acce952bSliubo 	char nbuf[16];
124acce952bSliubo 	const char *errstr;
125acce952bSliubo 
126acce952bSliubo 	/*
127acce952bSliubo 	 * Special case: if the error is EROFS, and we're already
128acce952bSliubo 	 * under MS_RDONLY, then it is safe here.
129acce952bSliubo 	 */
130acce952bSliubo 	if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
131acce952bSliubo 		return;
132acce952bSliubo 
133acce952bSliubo 	errstr = btrfs_decode_error(fs_info, errno, nbuf);
134acce952bSliubo 	printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: %s\n",
135acce952bSliubo 		sb->s_id, function, line, errstr);
136acce952bSliubo 	save_error_info(fs_info);
137acce952bSliubo 
138acce952bSliubo 	btrfs_handle_error(fs_info);
139acce952bSliubo }
140acce952bSliubo 
141e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb)
142e20d96d6SChris Mason {
143e20d96d6SChris Mason 	struct btrfs_root *root = btrfs_sb(sb);
144e20d96d6SChris Mason 	int ret;
145e20d96d6SChris Mason 
146e20d96d6SChris Mason 	ret = close_ctree(root);
147e20d96d6SChris Mason 	sb->s_fs_info = NULL;
148559af821SAndi Kleen 
149559af821SAndi Kleen 	(void)ret; /* FIXME: need to fix VFS to return error? */
150e20d96d6SChris Mason }
1512e635a27SChris Mason 
15295e05289SChris Mason enum {
15373f73415SJosef Bacik 	Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
154287a0ab9SJosef Bacik 	Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
155287a0ab9SJosef Bacik 	Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
156261507a0SLi Zefan 	Opt_compress_type, Opt_compress_force, Opt_compress_force_type,
157261507a0SLi Zefan 	Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard,
158261507a0SLi Zefan 	Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed, Opt_err,
15995e05289SChris Mason };
16095e05289SChris Mason 
16195e05289SChris Mason static match_table_t tokens = {
162dfe25020SChris Mason 	{Opt_degraded, "degraded"},
16395e05289SChris Mason 	{Opt_subvol, "subvol=%s"},
16473f73415SJosef Bacik 	{Opt_subvolid, "subvolid=%d"},
16543e570b0SChristoph Hellwig 	{Opt_device, "device=%s"},
166b6cda9bcSChris Mason 	{Opt_nodatasum, "nodatasum"},
167be20aa9dSChris Mason 	{Opt_nodatacow, "nodatacow"},
16821ad10cfSChris Mason 	{Opt_nobarrier, "nobarrier"},
1696f568d35SChris Mason 	{Opt_max_inline, "max_inline=%s"},
1708f662a76SChris Mason 	{Opt_alloc_start, "alloc_start=%s"},
1714543df7eSChris Mason 	{Opt_thread_pool, "thread_pool=%d"},
172c8b97818SChris Mason 	{Opt_compress, "compress"},
173261507a0SLi Zefan 	{Opt_compress_type, "compress=%s"},
174a555f810SChris Mason 	{Opt_compress_force, "compress-force"},
175261507a0SLi Zefan 	{Opt_compress_force_type, "compress-force=%s"},
176e18e4809SChris Mason 	{Opt_ssd, "ssd"},
177451d7585SChris Mason 	{Opt_ssd_spread, "ssd_spread"},
1783b30c22fSChris Mason 	{Opt_nossd, "nossd"},
17933268eafSJosef Bacik 	{Opt_noacl, "noacl"},
1803a5e1404SSage Weil 	{Opt_notreelog, "notreelog"},
181dccae999SSage Weil 	{Opt_flushoncommit, "flushoncommit"},
18297e728d4SJosef Bacik 	{Opt_ratio, "metadata_ratio=%d"},
183e244a0aeSChristoph Hellwig 	{Opt_discard, "discard"},
1840af3d00bSJosef Bacik 	{Opt_space_cache, "space_cache"},
18588c2ba3bSJosef Bacik 	{Opt_clear_cache, "clear_cache"},
1864260f7c7SSage Weil 	{Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
18733268eafSJosef Bacik 	{Opt_err, NULL},
18895e05289SChris Mason };
18995e05289SChris Mason 
190edf24abeSChristoph Hellwig /*
191edf24abeSChristoph Hellwig  * Regular mount options parser.  Everything that is needed only when
192edf24abeSChristoph Hellwig  * reading in a new superblock is parsed here.
193edf24abeSChristoph Hellwig  */
194edf24abeSChristoph Hellwig int btrfs_parse_options(struct btrfs_root *root, char *options)
19595e05289SChris Mason {
196edf24abeSChristoph Hellwig 	struct btrfs_fs_info *info = root->fs_info;
19795e05289SChris Mason 	substring_t args[MAX_OPT_ARGS];
198da495eccSJosef Bacik 	char *p, *num, *orig;
1994543df7eSChris Mason 	int intarg;
200a7a3f7caSSage Weil 	int ret = 0;
201261507a0SLi Zefan 	char *compress_type;
202261507a0SLi Zefan 	bool compress_force = false;
203b6cda9bcSChris Mason 
20495e05289SChris Mason 	if (!options)
205edf24abeSChristoph Hellwig 		return 0;
20695e05289SChris Mason 
207be20aa9dSChris Mason 	/*
208be20aa9dSChris Mason 	 * strsep changes the string, duplicate it because parse_options
209be20aa9dSChris Mason 	 * gets called twice
210be20aa9dSChris Mason 	 */
211be20aa9dSChris Mason 	options = kstrdup(options, GFP_NOFS);
212be20aa9dSChris Mason 	if (!options)
213be20aa9dSChris Mason 		return -ENOMEM;
214be20aa9dSChris Mason 
215da495eccSJosef Bacik 	orig = options;
216be20aa9dSChris Mason 
21795e05289SChris Mason 	while ((p = strsep(&options, ",")) != NULL) {
21895e05289SChris Mason 		int token;
21995e05289SChris Mason 		if (!*p)
22095e05289SChris Mason 			continue;
22195e05289SChris Mason 
22295e05289SChris Mason 		token = match_token(p, tokens, args);
22395e05289SChris Mason 		switch (token) {
224dfe25020SChris Mason 		case Opt_degraded:
225edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: allowing degraded mounts\n");
226dfe25020SChris Mason 			btrfs_set_opt(info->mount_opt, DEGRADED);
227dfe25020SChris Mason 			break;
22895e05289SChris Mason 		case Opt_subvol:
22973f73415SJosef Bacik 		case Opt_subvolid:
23043e570b0SChristoph Hellwig 		case Opt_device:
231edf24abeSChristoph Hellwig 			/*
23243e570b0SChristoph Hellwig 			 * These are parsed by btrfs_parse_early_options
233edf24abeSChristoph Hellwig 			 * and can be happily ignored here.
234edf24abeSChristoph Hellwig 			 */
23595e05289SChris Mason 			break;
236b6cda9bcSChris Mason 		case Opt_nodatasum:
237067c28adSChris Mason 			printk(KERN_INFO "btrfs: setting nodatasum\n");
238b6cda9bcSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
239be20aa9dSChris Mason 			break;
240be20aa9dSChris Mason 		case Opt_nodatacow:
241edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: setting nodatacow\n");
242be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATACOW);
243be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
244b6cda9bcSChris Mason 			break;
245a555f810SChris Mason 		case Opt_compress_force:
246261507a0SLi Zefan 		case Opt_compress_force_type:
247261507a0SLi Zefan 			compress_force = true;
248261507a0SLi Zefan 		case Opt_compress:
249261507a0SLi Zefan 		case Opt_compress_type:
250261507a0SLi Zefan 			if (token == Opt_compress ||
251261507a0SLi Zefan 			    token == Opt_compress_force ||
252261507a0SLi Zefan 			    strcmp(args[0].from, "zlib") == 0) {
253261507a0SLi Zefan 				compress_type = "zlib";
254261507a0SLi Zefan 				info->compress_type = BTRFS_COMPRESS_ZLIB;
255a6fa6faeSLi Zefan 			} else if (strcmp(args[0].from, "lzo") == 0) {
256a6fa6faeSLi Zefan 				compress_type = "lzo";
257a6fa6faeSLi Zefan 				info->compress_type = BTRFS_COMPRESS_LZO;
258261507a0SLi Zefan 			} else {
259261507a0SLi Zefan 				ret = -EINVAL;
260261507a0SLi Zefan 				goto out;
261261507a0SLi Zefan 			}
262261507a0SLi Zefan 
263a555f810SChris Mason 			btrfs_set_opt(info->mount_opt, COMPRESS);
264261507a0SLi Zefan 			if (compress_force) {
265261507a0SLi Zefan 				btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
266261507a0SLi Zefan 				pr_info("btrfs: force %s compression\n",
267261507a0SLi Zefan 					compress_type);
268261507a0SLi Zefan 			} else
269261507a0SLi Zefan 				pr_info("btrfs: use %s compression\n",
270261507a0SLi Zefan 					compress_type);
271a555f810SChris Mason 			break;
272e18e4809SChris Mason 		case Opt_ssd:
273edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
274e18e4809SChris Mason 			btrfs_set_opt(info->mount_opt, SSD);
275e18e4809SChris Mason 			break;
276451d7585SChris Mason 		case Opt_ssd_spread:
277451d7585SChris Mason 			printk(KERN_INFO "btrfs: use spread ssd "
278451d7585SChris Mason 			       "allocation scheme\n");
279451d7585SChris Mason 			btrfs_set_opt(info->mount_opt, SSD);
280451d7585SChris Mason 			btrfs_set_opt(info->mount_opt, SSD_SPREAD);
281451d7585SChris Mason 			break;
2823b30c22fSChris Mason 		case Opt_nossd:
283451d7585SChris Mason 			printk(KERN_INFO "btrfs: not using ssd allocation "
284451d7585SChris Mason 			       "scheme\n");
285c289811cSChris Mason 			btrfs_set_opt(info->mount_opt, NOSSD);
2863b30c22fSChris Mason 			btrfs_clear_opt(info->mount_opt, SSD);
287451d7585SChris Mason 			btrfs_clear_opt(info->mount_opt, SSD_SPREAD);
2883b30c22fSChris Mason 			break;
28921ad10cfSChris Mason 		case Opt_nobarrier:
290edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: turning off barriers\n");
29121ad10cfSChris Mason 			btrfs_set_opt(info->mount_opt, NOBARRIER);
29221ad10cfSChris Mason 			break;
2934543df7eSChris Mason 		case Opt_thread_pool:
2944543df7eSChris Mason 			intarg = 0;
2954543df7eSChris Mason 			match_int(&args[0], &intarg);
2964543df7eSChris Mason 			if (intarg) {
2974543df7eSChris Mason 				info->thread_pool_size = intarg;
2984543df7eSChris Mason 				printk(KERN_INFO "btrfs: thread pool %d\n",
2994543df7eSChris Mason 				       info->thread_pool_size);
3004543df7eSChris Mason 			}
3014543df7eSChris Mason 			break;
3026f568d35SChris Mason 		case Opt_max_inline:
303edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
3046f568d35SChris Mason 			if (num) {
30591748467SAkinobu Mita 				info->max_inline = memparse(num, NULL);
3066f568d35SChris Mason 				kfree(num);
3076f568d35SChris Mason 
30815ada040SChris Mason 				if (info->max_inline) {
3096f568d35SChris Mason 					info->max_inline = max_t(u64,
31015ada040SChris Mason 						info->max_inline,
31115ada040SChris Mason 						root->sectorsize);
31215ada040SChris Mason 				}
313edf24abeSChristoph Hellwig 				printk(KERN_INFO "btrfs: max_inline at %llu\n",
31421380931SJoel Becker 					(unsigned long long)info->max_inline);
3156f568d35SChris Mason 			}
3166f568d35SChris Mason 			break;
3178f662a76SChris Mason 		case Opt_alloc_start:
318edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
3198f662a76SChris Mason 			if (num) {
32091748467SAkinobu Mita 				info->alloc_start = memparse(num, NULL);
3218f662a76SChris Mason 				kfree(num);
322edf24abeSChristoph Hellwig 				printk(KERN_INFO
323edf24abeSChristoph Hellwig 					"btrfs: allocations start at %llu\n",
32421380931SJoel Becker 					(unsigned long long)info->alloc_start);
3258f662a76SChris Mason 			}
3268f662a76SChris Mason 			break;
32733268eafSJosef Bacik 		case Opt_noacl:
32833268eafSJosef Bacik 			root->fs_info->sb->s_flags &= ~MS_POSIXACL;
32933268eafSJosef Bacik 			break;
3303a5e1404SSage Weil 		case Opt_notreelog:
3313a5e1404SSage Weil 			printk(KERN_INFO "btrfs: disabling tree log\n");
3323a5e1404SSage Weil 			btrfs_set_opt(info->mount_opt, NOTREELOG);
3333a5e1404SSage Weil 			break;
334dccae999SSage Weil 		case Opt_flushoncommit:
335dccae999SSage Weil 			printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
336dccae999SSage Weil 			btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
337dccae999SSage Weil 			break;
33897e728d4SJosef Bacik 		case Opt_ratio:
33997e728d4SJosef Bacik 			intarg = 0;
34097e728d4SJosef Bacik 			match_int(&args[0], &intarg);
34197e728d4SJosef Bacik 			if (intarg) {
34297e728d4SJosef Bacik 				info->metadata_ratio = intarg;
34397e728d4SJosef Bacik 				printk(KERN_INFO "btrfs: metadata ratio %d\n",
34497e728d4SJosef Bacik 				       info->metadata_ratio);
34597e728d4SJosef Bacik 			}
34697e728d4SJosef Bacik 			break;
347e244a0aeSChristoph Hellwig 		case Opt_discard:
348e244a0aeSChristoph Hellwig 			btrfs_set_opt(info->mount_opt, DISCARD);
349e244a0aeSChristoph Hellwig 			break;
3500af3d00bSJosef Bacik 		case Opt_space_cache:
3510af3d00bSJosef Bacik 			printk(KERN_INFO "btrfs: enabling disk space caching\n");
3520af3d00bSJosef Bacik 			btrfs_set_opt(info->mount_opt, SPACE_CACHE);
3530de90876SJosef Bacik 			break;
35488c2ba3bSJosef Bacik 		case Opt_clear_cache:
35588c2ba3bSJosef Bacik 			printk(KERN_INFO "btrfs: force clearing of disk cache\n");
35688c2ba3bSJosef Bacik 			btrfs_set_opt(info->mount_opt, CLEAR_CACHE);
3570af3d00bSJosef Bacik 			break;
3584260f7c7SSage Weil 		case Opt_user_subvol_rm_allowed:
3594260f7c7SSage Weil 			btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
3604260f7c7SSage Weil 			break;
361a7a3f7caSSage Weil 		case Opt_err:
362a7a3f7caSSage Weil 			printk(KERN_INFO "btrfs: unrecognized mount option "
363a7a3f7caSSage Weil 			       "'%s'\n", p);
364a7a3f7caSSage Weil 			ret = -EINVAL;
365a7a3f7caSSage Weil 			goto out;
36695e05289SChris Mason 		default:
367be20aa9dSChris Mason 			break;
36895e05289SChris Mason 		}
36995e05289SChris Mason 	}
370a7a3f7caSSage Weil out:
371da495eccSJosef Bacik 	kfree(orig);
372a7a3f7caSSage Weil 	return ret;
373edf24abeSChristoph Hellwig }
374edf24abeSChristoph Hellwig 
375edf24abeSChristoph Hellwig /*
376edf24abeSChristoph Hellwig  * Parse mount options that are required early in the mount process.
377edf24abeSChristoph Hellwig  *
378edf24abeSChristoph Hellwig  * All other options will be parsed on much later in the mount process and
379edf24abeSChristoph Hellwig  * only when we need to allocate a new super block.
380edf24abeSChristoph Hellwig  */
38197288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags,
38273f73415SJosef Bacik 		void *holder, char **subvol_name, u64 *subvol_objectid,
38343e570b0SChristoph Hellwig 		struct btrfs_fs_devices **fs_devices)
384edf24abeSChristoph Hellwig {
385edf24abeSChristoph Hellwig 	substring_t args[MAX_OPT_ARGS];
3863f3d0bc0STero Roponen 	char *opts, *orig, *p;
387edf24abeSChristoph Hellwig 	int error = 0;
38873f73415SJosef Bacik 	int intarg;
389edf24abeSChristoph Hellwig 
390edf24abeSChristoph Hellwig 	if (!options)
391edf24abeSChristoph Hellwig 		goto out;
392edf24abeSChristoph Hellwig 
393edf24abeSChristoph Hellwig 	/*
394edf24abeSChristoph Hellwig 	 * strsep changes the string, duplicate it because parse_options
395edf24abeSChristoph Hellwig 	 * gets called twice
396edf24abeSChristoph Hellwig 	 */
397edf24abeSChristoph Hellwig 	opts = kstrdup(options, GFP_KERNEL);
398edf24abeSChristoph Hellwig 	if (!opts)
399edf24abeSChristoph Hellwig 		return -ENOMEM;
4003f3d0bc0STero Roponen 	orig = opts;
401edf24abeSChristoph Hellwig 
402edf24abeSChristoph Hellwig 	while ((p = strsep(&opts, ",")) != NULL) {
403edf24abeSChristoph Hellwig 		int token;
404edf24abeSChristoph Hellwig 		if (!*p)
405edf24abeSChristoph Hellwig 			continue;
406edf24abeSChristoph Hellwig 
407edf24abeSChristoph Hellwig 		token = match_token(p, tokens, args);
408edf24abeSChristoph Hellwig 		switch (token) {
409edf24abeSChristoph Hellwig 		case Opt_subvol:
410edf24abeSChristoph Hellwig 			*subvol_name = match_strdup(&args[0]);
411edf24abeSChristoph Hellwig 			break;
41273f73415SJosef Bacik 		case Opt_subvolid:
41373f73415SJosef Bacik 			intarg = 0;
4144849f01dSJosef Bacik 			error = match_int(&args[0], &intarg);
4154849f01dSJosef Bacik 			if (!error) {
4164849f01dSJosef Bacik 				/* we want the original fs_tree */
4174849f01dSJosef Bacik 				if (!intarg)
4184849f01dSJosef Bacik 					*subvol_objectid =
4194849f01dSJosef Bacik 						BTRFS_FS_TREE_OBJECTID;
4204849f01dSJosef Bacik 				else
42173f73415SJosef Bacik 					*subvol_objectid = intarg;
4224849f01dSJosef Bacik 			}
42373f73415SJosef Bacik 			break;
42443e570b0SChristoph Hellwig 		case Opt_device:
42543e570b0SChristoph Hellwig 			error = btrfs_scan_one_device(match_strdup(&args[0]),
42643e570b0SChristoph Hellwig 					flags, holder, fs_devices);
42743e570b0SChristoph Hellwig 			if (error)
42843e570b0SChristoph Hellwig 				goto out_free_opts;
42943e570b0SChristoph Hellwig 			break;
430edf24abeSChristoph Hellwig 		default:
431edf24abeSChristoph Hellwig 			break;
432edf24abeSChristoph Hellwig 		}
433edf24abeSChristoph Hellwig 	}
434edf24abeSChristoph Hellwig 
43543e570b0SChristoph Hellwig  out_free_opts:
4363f3d0bc0STero Roponen 	kfree(orig);
437edf24abeSChristoph Hellwig  out:
438edf24abeSChristoph Hellwig 	/*
439edf24abeSChristoph Hellwig 	 * If no subvolume name is specified we use the default one.  Allocate
4403de4586cSChris Mason 	 * a copy of the string "." here so that code later in the
441edf24abeSChristoph Hellwig 	 * mount path doesn't care if it's the default volume or another one.
442edf24abeSChristoph Hellwig 	 */
443edf24abeSChristoph Hellwig 	if (!*subvol_name) {
4443de4586cSChris Mason 		*subvol_name = kstrdup(".", GFP_KERNEL);
445edf24abeSChristoph Hellwig 		if (!*subvol_name)
446edf24abeSChristoph Hellwig 			return -ENOMEM;
447edf24abeSChristoph Hellwig 	}
448edf24abeSChristoph Hellwig 	return error;
44995e05289SChris Mason }
45095e05289SChris Mason 
45173f73415SJosef Bacik static struct dentry *get_default_root(struct super_block *sb,
45273f73415SJosef Bacik 				       u64 subvol_objectid)
45373f73415SJosef Bacik {
45473f73415SJosef Bacik 	struct btrfs_root *root = sb->s_fs_info;
45573f73415SJosef Bacik 	struct btrfs_root *new_root;
45673f73415SJosef Bacik 	struct btrfs_dir_item *di;
45773f73415SJosef Bacik 	struct btrfs_path *path;
45873f73415SJosef Bacik 	struct btrfs_key location;
45973f73415SJosef Bacik 	struct inode *inode;
46073f73415SJosef Bacik 	struct dentry *dentry;
46173f73415SJosef Bacik 	u64 dir_id;
46273f73415SJosef Bacik 	int new = 0;
46373f73415SJosef Bacik 
46473f73415SJosef Bacik 	/*
46573f73415SJosef Bacik 	 * We have a specific subvol we want to mount, just setup location and
46673f73415SJosef Bacik 	 * go look up the root.
46773f73415SJosef Bacik 	 */
46873f73415SJosef Bacik 	if (subvol_objectid) {
46973f73415SJosef Bacik 		location.objectid = subvol_objectid;
47073f73415SJosef Bacik 		location.type = BTRFS_ROOT_ITEM_KEY;
47173f73415SJosef Bacik 		location.offset = (u64)-1;
47273f73415SJosef Bacik 		goto find_root;
47373f73415SJosef Bacik 	}
47473f73415SJosef Bacik 
47573f73415SJosef Bacik 	path = btrfs_alloc_path();
47673f73415SJosef Bacik 	if (!path)
47773f73415SJosef Bacik 		return ERR_PTR(-ENOMEM);
47873f73415SJosef Bacik 	path->leave_spinning = 1;
47973f73415SJosef Bacik 
48073f73415SJosef Bacik 	/*
48173f73415SJosef Bacik 	 * Find the "default" dir item which points to the root item that we
48273f73415SJosef Bacik 	 * will mount by default if we haven't been given a specific subvolume
48373f73415SJosef Bacik 	 * to mount.
48473f73415SJosef Bacik 	 */
48573f73415SJosef Bacik 	dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
48673f73415SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
487fb4f6f91SDan Carpenter 	if (IS_ERR(di))
488fb4f6f91SDan Carpenter 		return ERR_CAST(di);
48973f73415SJosef Bacik 	if (!di) {
49073f73415SJosef Bacik 		/*
49173f73415SJosef Bacik 		 * Ok the default dir item isn't there.  This is weird since
49273f73415SJosef Bacik 		 * it's always been there, but don't freak out, just try and
49373f73415SJosef Bacik 		 * mount to root most subvolume.
49473f73415SJosef Bacik 		 */
49573f73415SJosef Bacik 		btrfs_free_path(path);
49673f73415SJosef Bacik 		dir_id = BTRFS_FIRST_FREE_OBJECTID;
49773f73415SJosef Bacik 		new_root = root->fs_info->fs_root;
49873f73415SJosef Bacik 		goto setup_root;
49973f73415SJosef Bacik 	}
50073f73415SJosef Bacik 
50173f73415SJosef Bacik 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
50273f73415SJosef Bacik 	btrfs_free_path(path);
50373f73415SJosef Bacik 
50473f73415SJosef Bacik find_root:
50573f73415SJosef Bacik 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
50673f73415SJosef Bacik 	if (IS_ERR(new_root))
507d0b678cbSJulia Lawall 		return ERR_CAST(new_root);
50873f73415SJosef Bacik 
50973f73415SJosef Bacik 	if (btrfs_root_refs(&new_root->root_item) == 0)
51073f73415SJosef Bacik 		return ERR_PTR(-ENOENT);
51173f73415SJosef Bacik 
51273f73415SJosef Bacik 	dir_id = btrfs_root_dirid(&new_root->root_item);
51373f73415SJosef Bacik setup_root:
51473f73415SJosef Bacik 	location.objectid = dir_id;
51573f73415SJosef Bacik 	location.type = BTRFS_INODE_ITEM_KEY;
51673f73415SJosef Bacik 	location.offset = 0;
51773f73415SJosef Bacik 
51873f73415SJosef Bacik 	inode = btrfs_iget(sb, &location, new_root, &new);
5194cbd1149SDan Carpenter 	if (IS_ERR(inode))
5204cbd1149SDan Carpenter 		return ERR_CAST(inode);
52173f73415SJosef Bacik 
52273f73415SJosef Bacik 	/*
52373f73415SJosef Bacik 	 * If we're just mounting the root most subvol put the inode and return
52473f73415SJosef Bacik 	 * a reference to the dentry.  We will have already gotten a reference
52573f73415SJosef Bacik 	 * to the inode in btrfs_fill_super so we're good to go.
52673f73415SJosef Bacik 	 */
52773f73415SJosef Bacik 	if (!new && sb->s_root->d_inode == inode) {
52873f73415SJosef Bacik 		iput(inode);
52973f73415SJosef Bacik 		return dget(sb->s_root);
53073f73415SJosef Bacik 	}
53173f73415SJosef Bacik 
53273f73415SJosef Bacik 	if (new) {
53373f73415SJosef Bacik 		const struct qstr name = { .name = "/", .len = 1 };
53473f73415SJosef Bacik 
53573f73415SJosef Bacik 		/*
53673f73415SJosef Bacik 		 * New inode, we need to make the dentry a sibling of s_root so
53773f73415SJosef Bacik 		 * everything gets cleaned up properly on unmount.
53873f73415SJosef Bacik 		 */
53973f73415SJosef Bacik 		dentry = d_alloc(sb->s_root, &name);
54073f73415SJosef Bacik 		if (!dentry) {
54173f73415SJosef Bacik 			iput(inode);
54273f73415SJosef Bacik 			return ERR_PTR(-ENOMEM);
54373f73415SJosef Bacik 		}
54473f73415SJosef Bacik 		d_splice_alias(inode, dentry);
54573f73415SJosef Bacik 	} else {
54673f73415SJosef Bacik 		/*
54773f73415SJosef Bacik 		 * We found the inode in cache, just find a dentry for it and
54873f73415SJosef Bacik 		 * put the reference to the inode we just got.
54973f73415SJosef Bacik 		 */
55073f73415SJosef Bacik 		dentry = d_find_alias(inode);
55173f73415SJosef Bacik 		iput(inode);
55273f73415SJosef Bacik 	}
55373f73415SJosef Bacik 
55473f73415SJosef Bacik 	return dentry;
55573f73415SJosef Bacik }
55673f73415SJosef Bacik 
5578a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb,
5588a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
5598a4b83ccSChris Mason 			    void *data, int silent)
5602e635a27SChris Mason {
5612e635a27SChris Mason 	struct inode *inode;
562e20d96d6SChris Mason 	struct dentry *root_dentry;
5630f7d52f4SChris Mason 	struct btrfs_root *tree_root;
5645d4f98a2SYan Zheng 	struct btrfs_key key;
56539279cc3SChris Mason 	int err;
5662e635a27SChris Mason 
5672e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
5682e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
569e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
570be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
5715103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
5722e635a27SChris Mason 	sb->s_time_gran = 1;
5730eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL
57433268eafSJosef Bacik 	sb->s_flags |= MS_POSIXACL;
57549cf6f45SChris Ball #endif
576e20d96d6SChris Mason 
577dfe25020SChris Mason 	tree_root = open_ctree(sb, fs_devices, (char *)data);
578d98237b3SChris Mason 
579e58ca020SYan 	if (IS_ERR(tree_root)) {
580e20d96d6SChris Mason 		printk("btrfs: open_ctree failed\n");
581e58ca020SYan 		return PTR_ERR(tree_root);
582e20d96d6SChris Mason 	}
5830f7d52f4SChris Mason 	sb->s_fs_info = tree_root;
584b888db2bSChris Mason 
5855d4f98a2SYan Zheng 	key.objectid = BTRFS_FIRST_FREE_OBJECTID;
5865d4f98a2SYan Zheng 	key.type = BTRFS_INODE_ITEM_KEY;
5875d4f98a2SYan Zheng 	key.offset = 0;
58873f73415SJosef Bacik 	inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root, NULL);
5895d4f98a2SYan Zheng 	if (IS_ERR(inode)) {
5905d4f98a2SYan Zheng 		err = PTR_ERR(inode);
59139279cc3SChris Mason 		goto fail_close;
59239279cc3SChris Mason 	}
5932e635a27SChris Mason 
594e20d96d6SChris Mason 	root_dentry = d_alloc_root(inode);
595e20d96d6SChris Mason 	if (!root_dentry) {
5962e635a27SChris Mason 		iput(inode);
59739279cc3SChris Mason 		err = -ENOMEM;
59839279cc3SChris Mason 		goto fail_close;
5992e635a27SChris Mason 	}
60058176a96SJosef Bacik 
601e20d96d6SChris Mason 	sb->s_root = root_dentry;
6026885f308SChris Mason 
6036885f308SChris Mason 	save_mount_options(sb, data);
6042e635a27SChris Mason 	return 0;
6052e635a27SChris Mason 
60639279cc3SChris Mason fail_close:
60739279cc3SChris Mason 	close_ctree(tree_root);
608d5719762SChris Mason 	return err;
609d5719762SChris Mason }
610d5719762SChris Mason 
6116bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
612d5719762SChris Mason {
613d5719762SChris Mason 	struct btrfs_trans_handle *trans;
614dccae999SSage Weil 	struct btrfs_root *root = btrfs_sb(sb);
615d5719762SChris Mason 	int ret;
616df2ce34cSChris Mason 
617d561c025SChris Mason 	if (!wait) {
6187cfcc17eSChris Mason 		filemap_flush(root->fs_info->btree_inode->i_mapping);
619df2ce34cSChris Mason 		return 0;
620d561c025SChris Mason 	}
621771ed689SChris Mason 
62224bbcf04SYan, Zheng 	btrfs_start_delalloc_inodes(root, 0);
62324bbcf04SYan, Zheng 	btrfs_wait_ordered_extents(root, 0, 0);
624771ed689SChris Mason 
625a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 0);
626*98d5dc13STsutomu Itoh 	if (IS_ERR(trans))
627*98d5dc13STsutomu Itoh 		return PTR_ERR(trans);
628d5719762SChris Mason 	ret = btrfs_commit_transaction(trans, root);
62954aa1f4dSChris Mason 	return ret;
630d5719762SChris Mason }
631d5719762SChris Mason 
632a9572a15SEric Paris static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
633a9572a15SEric Paris {
634a9572a15SEric Paris 	struct btrfs_root *root = btrfs_sb(vfs->mnt_sb);
635a9572a15SEric Paris 	struct btrfs_fs_info *info = root->fs_info;
636a9572a15SEric Paris 
637a9572a15SEric Paris 	if (btrfs_test_opt(root, DEGRADED))
638a9572a15SEric Paris 		seq_puts(seq, ",degraded");
639a9572a15SEric Paris 	if (btrfs_test_opt(root, NODATASUM))
640a9572a15SEric Paris 		seq_puts(seq, ",nodatasum");
641a9572a15SEric Paris 	if (btrfs_test_opt(root, NODATACOW))
642a9572a15SEric Paris 		seq_puts(seq, ",nodatacow");
643a9572a15SEric Paris 	if (btrfs_test_opt(root, NOBARRIER))
644a9572a15SEric Paris 		seq_puts(seq, ",nobarrier");
645a9572a15SEric Paris 	if (info->max_inline != 8192 * 1024)
64621380931SJoel Becker 		seq_printf(seq, ",max_inline=%llu",
64721380931SJoel Becker 			   (unsigned long long)info->max_inline);
648a9572a15SEric Paris 	if (info->alloc_start != 0)
64921380931SJoel Becker 		seq_printf(seq, ",alloc_start=%llu",
65021380931SJoel Becker 			   (unsigned long long)info->alloc_start);
651a9572a15SEric Paris 	if (info->thread_pool_size !=  min_t(unsigned long,
652a9572a15SEric Paris 					     num_online_cpus() + 2, 8))
653a9572a15SEric Paris 		seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
654a9572a15SEric Paris 	if (btrfs_test_opt(root, COMPRESS))
655a9572a15SEric Paris 		seq_puts(seq, ",compress");
656c289811cSChris Mason 	if (btrfs_test_opt(root, NOSSD))
657c289811cSChris Mason 		seq_puts(seq, ",nossd");
658451d7585SChris Mason 	if (btrfs_test_opt(root, SSD_SPREAD))
659451d7585SChris Mason 		seq_puts(seq, ",ssd_spread");
660451d7585SChris Mason 	else if (btrfs_test_opt(root, SSD))
661a9572a15SEric Paris 		seq_puts(seq, ",ssd");
6623a5e1404SSage Weil 	if (btrfs_test_opt(root, NOTREELOG))
6636b65c5c6SSage Weil 		seq_puts(seq, ",notreelog");
664dccae999SSage Weil 	if (btrfs_test_opt(root, FLUSHONCOMMIT))
6656b65c5c6SSage Weil 		seq_puts(seq, ",flushoncommit");
66620a5239aSMatthew Wilcox 	if (btrfs_test_opt(root, DISCARD))
66720a5239aSMatthew Wilcox 		seq_puts(seq, ",discard");
668a9572a15SEric Paris 	if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
669a9572a15SEric Paris 		seq_puts(seq, ",noacl");
670a9572a15SEric Paris 	return 0;
671a9572a15SEric Paris }
672a9572a15SEric Paris 
673a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data)
6742e635a27SChris Mason {
675450ba0eaSJosef Bacik 	struct btrfs_root *test_root = data;
676a061fc8dSChris Mason 	struct btrfs_root *root = btrfs_sb(s);
6774b82d6e4SYan 
678619c8c76SIan Kent 	/*
679619c8c76SIan Kent 	 * If this super block is going away, return false as it
680619c8c76SIan Kent 	 * can't match as an existing super block.
681619c8c76SIan Kent 	 */
682619c8c76SIan Kent 	if (!atomic_read(&s->s_active))
683619c8c76SIan Kent 		return 0;
684450ba0eaSJosef Bacik 	return root->fs_info->fs_devices == test_root->fs_info->fs_devices;
6854b82d6e4SYan }
6864b82d6e4SYan 
687450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data)
688450ba0eaSJosef Bacik {
689450ba0eaSJosef Bacik 	s->s_fs_info = data;
690450ba0eaSJosef Bacik 
691450ba0eaSJosef Bacik 	return set_anon_super(s, data);
692450ba0eaSJosef Bacik }
693450ba0eaSJosef Bacik 
694450ba0eaSJosef Bacik 
695edf24abeSChristoph Hellwig /*
696edf24abeSChristoph Hellwig  * Find a superblock for the given device / mount point.
697edf24abeSChristoph Hellwig  *
698edf24abeSChristoph Hellwig  * Note:  This is based on get_sb_bdev from fs/super.c with a few additions
699edf24abeSChristoph Hellwig  *	  for multiple device setup.  Make sure to keep it in sync.
700edf24abeSChristoph Hellwig  */
701edf24abeSChristoph Hellwig static int btrfs_get_sb(struct file_system_type *fs_type, int flags,
702edf24abeSChristoph Hellwig 		const char *dev_name, void *data, struct vfsmount *mnt)
7034b82d6e4SYan {
7044b82d6e4SYan 	struct block_device *bdev = NULL;
7054b82d6e4SYan 	struct super_block *s;
7064b82d6e4SYan 	struct dentry *root;
7078a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices = NULL;
708450ba0eaSJosef Bacik 	struct btrfs_root *tree_root = NULL;
709450ba0eaSJosef Bacik 	struct btrfs_fs_info *fs_info = NULL;
71097288f2cSChristoph Hellwig 	fmode_t mode = FMODE_READ;
71173f73415SJosef Bacik 	char *subvol_name = NULL;
71273f73415SJosef Bacik 	u64 subvol_objectid = 0;
7134b82d6e4SYan 	int error = 0;
7144b82d6e4SYan 
71597288f2cSChristoph Hellwig 	if (!(flags & MS_RDONLY))
71697288f2cSChristoph Hellwig 		mode |= FMODE_WRITE;
71797288f2cSChristoph Hellwig 
71897288f2cSChristoph Hellwig 	error = btrfs_parse_early_options(data, mode, fs_type,
71973f73415SJosef Bacik 					  &subvol_name, &subvol_objectid,
72073f73415SJosef Bacik 					  &fs_devices);
721edf24abeSChristoph Hellwig 	if (error)
7221f483660SShen Feng 		return error;
723edf24abeSChristoph Hellwig 
72497288f2cSChristoph Hellwig 	error = btrfs_scan_one_device(dev_name, mode, fs_type, &fs_devices);
7258a4b83ccSChris Mason 	if (error)
726edf24abeSChristoph Hellwig 		goto error_free_subvol_name;
7274b82d6e4SYan 
72897288f2cSChristoph Hellwig 	error = btrfs_open_devices(fs_devices, mode, fs_type);
7298a4b83ccSChris Mason 	if (error)
730edf24abeSChristoph Hellwig 		goto error_free_subvol_name;
7318a4b83ccSChris Mason 
7322b82032cSYan Zheng 	if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
7332b82032cSYan Zheng 		error = -EACCES;
7342b82032cSYan Zheng 		goto error_close_devices;
7352b82032cSYan Zheng 	}
7362b82032cSYan Zheng 
737450ba0eaSJosef Bacik 	/*
738450ba0eaSJosef Bacik 	 * Setup a dummy root and fs_info for test/set super.  This is because
739450ba0eaSJosef Bacik 	 * we don't actually fill this stuff out until open_ctree, but we need
740450ba0eaSJosef Bacik 	 * it for searching for existing supers, so this lets us do that and
741450ba0eaSJosef Bacik 	 * then open_ctree will properly initialize everything later.
742450ba0eaSJosef Bacik 	 */
743450ba0eaSJosef Bacik 	fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
744450ba0eaSJosef Bacik 	tree_root = kzalloc(sizeof(struct btrfs_root), GFP_NOFS);
745450ba0eaSJosef Bacik 	if (!fs_info || !tree_root) {
746450ba0eaSJosef Bacik 		error = -ENOMEM;
747450ba0eaSJosef Bacik 		goto error_close_devices;
748450ba0eaSJosef Bacik 	}
749450ba0eaSJosef Bacik 	fs_info->tree_root = tree_root;
750450ba0eaSJosef Bacik 	fs_info->fs_devices = fs_devices;
751450ba0eaSJosef Bacik 	tree_root->fs_info = fs_info;
752450ba0eaSJosef Bacik 
753dfe25020SChris Mason 	bdev = fs_devices->latest_bdev;
754450ba0eaSJosef Bacik 	s = sget(fs_type, btrfs_test_super, btrfs_set_super, tree_root);
7554b82d6e4SYan 	if (IS_ERR(s))
7564b82d6e4SYan 		goto error_s;
7574b82d6e4SYan 
7584b82d6e4SYan 	if (s->s_root) {
7594b82d6e4SYan 		if ((flags ^ s->s_flags) & MS_RDONLY) {
7606f5bbff9SAl Viro 			deactivate_locked_super(s);
7614b82d6e4SYan 			error = -EBUSY;
762c146afadSYan Zheng 			goto error_close_devices;
7634b82d6e4SYan 		}
7644b82d6e4SYan 
7652b82032cSYan Zheng 		btrfs_close_devices(fs_devices);
766bdc924bbSIan Kent 		kfree(fs_info);
767bdc924bbSIan Kent 		kfree(tree_root);
7684b82d6e4SYan 	} else {
7694b82d6e4SYan 		char b[BDEVNAME_SIZE];
7704b82d6e4SYan 
7714b82d6e4SYan 		s->s_flags = flags;
7724b82d6e4SYan 		strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
7738a4b83ccSChris Mason 		error = btrfs_fill_super(s, fs_devices, data,
7748a4b83ccSChris Mason 					 flags & MS_SILENT ? 1 : 0);
7754b82d6e4SYan 		if (error) {
7766f5bbff9SAl Viro 			deactivate_locked_super(s);
7771f483660SShen Feng 			goto error_free_subvol_name;
7784b82d6e4SYan 		}
7794b82d6e4SYan 
780788f20ebSChris Mason 		btrfs_sb(s)->fs_info->bdev_holder = fs_type;
7814b82d6e4SYan 		s->s_flags |= MS_ACTIVE;
7824b82d6e4SYan 	}
7834b82d6e4SYan 
78473f73415SJosef Bacik 	root = get_default_root(s, subvol_objectid);
7854b82d6e4SYan 	if (IS_ERR(root)) {
7864b82d6e4SYan 		error = PTR_ERR(root);
78773f73415SJosef Bacik 		deactivate_locked_super(s);
7880e78340fSJosef Bacik 		goto error_free_subvol_name;
7894b82d6e4SYan 	}
79073f73415SJosef Bacik 	/* if they gave us a subvolume name bind mount into that */
79173f73415SJosef Bacik 	if (strcmp(subvol_name, ".")) {
79273f73415SJosef Bacik 		struct dentry *new_root;
79373f73415SJosef Bacik 		mutex_lock(&root->d_inode->i_mutex);
79473f73415SJosef Bacik 		new_root = lookup_one_len(subvol_name, root,
79573f73415SJosef Bacik 				      strlen(subvol_name));
79673f73415SJosef Bacik 		mutex_unlock(&root->d_inode->i_mutex);
79773f73415SJosef Bacik 
79873f73415SJosef Bacik 		if (IS_ERR(new_root)) {
799f106e82cSLi Zefan 			dput(root);
80073f73415SJosef Bacik 			deactivate_locked_super(s);
80173f73415SJosef Bacik 			error = PTR_ERR(new_root);
8020e78340fSJosef Bacik 			goto error_free_subvol_name;
80373f73415SJosef Bacik 		}
80473f73415SJosef Bacik 		if (!new_root->d_inode) {
80573f73415SJosef Bacik 			dput(root);
80673f73415SJosef Bacik 			dput(new_root);
8076f5bbff9SAl Viro 			deactivate_locked_super(s);
8084b82d6e4SYan 			error = -ENXIO;
8090e78340fSJosef Bacik 			goto error_free_subvol_name;
8104b82d6e4SYan 		}
81173f73415SJosef Bacik 		dput(root);
81273f73415SJosef Bacik 		root = new_root;
81376fcef19SDavid Woodhouse 	}
8144b82d6e4SYan 
8154b82d6e4SYan 	mnt->mnt_sb = s;
8164b82d6e4SYan 	mnt->mnt_root = root;
817edf24abeSChristoph Hellwig 
818edf24abeSChristoph Hellwig 	kfree(subvol_name);
8194b82d6e4SYan 	return 0;
8204b82d6e4SYan 
8214b82d6e4SYan error_s:
8224b82d6e4SYan 	error = PTR_ERR(s);
823c146afadSYan Zheng error_close_devices:
8248a4b83ccSChris Mason 	btrfs_close_devices(fs_devices);
825450ba0eaSJosef Bacik 	kfree(fs_info);
826450ba0eaSJosef Bacik 	kfree(tree_root);
827edf24abeSChristoph Hellwig error_free_subvol_name:
828edf24abeSChristoph Hellwig 	kfree(subvol_name);
8294b82d6e4SYan 	return error;
8304b82d6e4SYan }
8312e635a27SChris Mason 
832c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
833c146afadSYan Zheng {
834c146afadSYan Zheng 	struct btrfs_root *root = btrfs_sb(sb);
835c146afadSYan Zheng 	int ret;
836c146afadSYan Zheng 
837b288052eSChris Mason 	ret = btrfs_parse_options(root, data);
838b288052eSChris Mason 	if (ret)
839b288052eSChris Mason 		return -EINVAL;
840b288052eSChris Mason 
841c146afadSYan Zheng 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
842c146afadSYan Zheng 		return 0;
843c146afadSYan Zheng 
844c146afadSYan Zheng 	if (*flags & MS_RDONLY) {
845c146afadSYan Zheng 		sb->s_flags |= MS_RDONLY;
846c146afadSYan Zheng 
847c146afadSYan Zheng 		ret =  btrfs_commit_super(root);
848c146afadSYan Zheng 		WARN_ON(ret);
849c146afadSYan Zheng 	} else {
8502b82032cSYan Zheng 		if (root->fs_info->fs_devices->rw_devices == 0)
8512b82032cSYan Zheng 			return -EACCES;
8522b82032cSYan Zheng 
853c146afadSYan Zheng 		if (btrfs_super_log_root(&root->fs_info->super_copy) != 0)
854c146afadSYan Zheng 			return -EINVAL;
855c146afadSYan Zheng 
856d68fc57bSYan, Zheng 		ret = btrfs_cleanup_fs_roots(root->fs_info);
857c146afadSYan Zheng 		WARN_ON(ret);
858c146afadSYan Zheng 
859d68fc57bSYan, Zheng 		/* recover relocation */
860d68fc57bSYan, Zheng 		ret = btrfs_recover_relocation(root);
861c146afadSYan Zheng 		WARN_ON(ret);
862c146afadSYan Zheng 
863c146afadSYan Zheng 		sb->s_flags &= ~MS_RDONLY;
864c146afadSYan Zheng 	}
865c146afadSYan Zheng 
866c146afadSYan Zheng 	return 0;
867c146afadSYan Zheng }
868c146afadSYan Zheng 
8696d07bcecSMiao Xie /*
8706d07bcecSMiao Xie  * The helper to calc the free space on the devices that can be used to store
8716d07bcecSMiao Xie  * file data.
8726d07bcecSMiao Xie  */
8736d07bcecSMiao Xie static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
8746d07bcecSMiao Xie {
8756d07bcecSMiao Xie 	struct btrfs_fs_info *fs_info = root->fs_info;
8766d07bcecSMiao Xie 	struct btrfs_device_info *devices_info;
8776d07bcecSMiao Xie 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
8786d07bcecSMiao Xie 	struct btrfs_device *device;
8796d07bcecSMiao Xie 	u64 skip_space;
8806d07bcecSMiao Xie 	u64 type;
8816d07bcecSMiao Xie 	u64 avail_space;
8826d07bcecSMiao Xie 	u64 used_space;
8836d07bcecSMiao Xie 	u64 min_stripe_size;
8846d07bcecSMiao Xie 	int min_stripes = 1;
8856d07bcecSMiao Xie 	int i = 0, nr_devices;
8866d07bcecSMiao Xie 	int ret;
8876d07bcecSMiao Xie 
8886d07bcecSMiao Xie 	nr_devices = fs_info->fs_devices->rw_devices;
8896d07bcecSMiao Xie 	BUG_ON(!nr_devices);
8906d07bcecSMiao Xie 
8916d07bcecSMiao Xie 	devices_info = kmalloc(sizeof(*devices_info) * nr_devices,
8926d07bcecSMiao Xie 			       GFP_NOFS);
8936d07bcecSMiao Xie 	if (!devices_info)
8946d07bcecSMiao Xie 		return -ENOMEM;
8956d07bcecSMiao Xie 
8966d07bcecSMiao Xie 	/* calc min stripe number for data space alloction */
8976d07bcecSMiao Xie 	type = btrfs_get_alloc_profile(root, 1);
8986d07bcecSMiao Xie 	if (type & BTRFS_BLOCK_GROUP_RAID0)
8996d07bcecSMiao Xie 		min_stripes = 2;
9006d07bcecSMiao Xie 	else if (type & BTRFS_BLOCK_GROUP_RAID1)
9016d07bcecSMiao Xie 		min_stripes = 2;
9026d07bcecSMiao Xie 	else if (type & BTRFS_BLOCK_GROUP_RAID10)
9036d07bcecSMiao Xie 		min_stripes = 4;
9046d07bcecSMiao Xie 
9056d07bcecSMiao Xie 	if (type & BTRFS_BLOCK_GROUP_DUP)
9066d07bcecSMiao Xie 		min_stripe_size = 2 * BTRFS_STRIPE_LEN;
9076d07bcecSMiao Xie 	else
9086d07bcecSMiao Xie 		min_stripe_size = BTRFS_STRIPE_LEN;
9096d07bcecSMiao Xie 
9106d07bcecSMiao Xie 	list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
9116d07bcecSMiao Xie 		if (!device->in_fs_metadata)
9126d07bcecSMiao Xie 			continue;
9136d07bcecSMiao Xie 
9146d07bcecSMiao Xie 		avail_space = device->total_bytes - device->bytes_used;
9156d07bcecSMiao Xie 
9166d07bcecSMiao Xie 		/* align with stripe_len */
9176d07bcecSMiao Xie 		do_div(avail_space, BTRFS_STRIPE_LEN);
9186d07bcecSMiao Xie 		avail_space *= BTRFS_STRIPE_LEN;
9196d07bcecSMiao Xie 
9206d07bcecSMiao Xie 		/*
9216d07bcecSMiao Xie 		 * In order to avoid overwritting the superblock on the drive,
9226d07bcecSMiao Xie 		 * btrfs starts at an offset of at least 1MB when doing chunk
9236d07bcecSMiao Xie 		 * allocation.
9246d07bcecSMiao Xie 		 */
9256d07bcecSMiao Xie 		skip_space = 1024 * 1024;
9266d07bcecSMiao Xie 
9276d07bcecSMiao Xie 		/* user can set the offset in fs_info->alloc_start. */
9286d07bcecSMiao Xie 		if (fs_info->alloc_start + BTRFS_STRIPE_LEN <=
9296d07bcecSMiao Xie 		    device->total_bytes)
9306d07bcecSMiao Xie 			skip_space = max(fs_info->alloc_start, skip_space);
9316d07bcecSMiao Xie 
9326d07bcecSMiao Xie 		/*
9336d07bcecSMiao Xie 		 * btrfs can not use the free space in [0, skip_space - 1],
9346d07bcecSMiao Xie 		 * we must subtract it from the total. In order to implement
9356d07bcecSMiao Xie 		 * it, we account the used space in this range first.
9366d07bcecSMiao Xie 		 */
9376d07bcecSMiao Xie 		ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1,
9386d07bcecSMiao Xie 						     &used_space);
9396d07bcecSMiao Xie 		if (ret) {
9406d07bcecSMiao Xie 			kfree(devices_info);
9416d07bcecSMiao Xie 			return ret;
9426d07bcecSMiao Xie 		}
9436d07bcecSMiao Xie 
9446d07bcecSMiao Xie 		/* calc the free space in [0, skip_space - 1] */
9456d07bcecSMiao Xie 		skip_space -= used_space;
9466d07bcecSMiao Xie 
9476d07bcecSMiao Xie 		/*
9486d07bcecSMiao Xie 		 * we can use the free space in [0, skip_space - 1], subtract
9496d07bcecSMiao Xie 		 * it from the total.
9506d07bcecSMiao Xie 		 */
9516d07bcecSMiao Xie 		if (avail_space && avail_space >= skip_space)
9526d07bcecSMiao Xie 			avail_space -= skip_space;
9536d07bcecSMiao Xie 		else
9546d07bcecSMiao Xie 			avail_space = 0;
9556d07bcecSMiao Xie 
9566d07bcecSMiao Xie 		if (avail_space < min_stripe_size)
9576d07bcecSMiao Xie 			continue;
9586d07bcecSMiao Xie 
9596d07bcecSMiao Xie 		devices_info[i].dev = device;
9606d07bcecSMiao Xie 		devices_info[i].max_avail = avail_space;
9616d07bcecSMiao Xie 
9626d07bcecSMiao Xie 		i++;
9636d07bcecSMiao Xie 	}
9646d07bcecSMiao Xie 
9656d07bcecSMiao Xie 	nr_devices = i;
9666d07bcecSMiao Xie 
9676d07bcecSMiao Xie 	btrfs_descending_sort_devices(devices_info, nr_devices);
9686d07bcecSMiao Xie 
9696d07bcecSMiao Xie 	i = nr_devices - 1;
9706d07bcecSMiao Xie 	avail_space = 0;
9716d07bcecSMiao Xie 	while (nr_devices >= min_stripes) {
9726d07bcecSMiao Xie 		if (devices_info[i].max_avail >= min_stripe_size) {
9736d07bcecSMiao Xie 			int j;
9746d07bcecSMiao Xie 			u64 alloc_size;
9756d07bcecSMiao Xie 
9766d07bcecSMiao Xie 			avail_space += devices_info[i].max_avail * min_stripes;
9776d07bcecSMiao Xie 			alloc_size = devices_info[i].max_avail;
9786d07bcecSMiao Xie 			for (j = i + 1 - min_stripes; j <= i; j++)
9796d07bcecSMiao Xie 				devices_info[j].max_avail -= alloc_size;
9806d07bcecSMiao Xie 		}
9816d07bcecSMiao Xie 		i--;
9826d07bcecSMiao Xie 		nr_devices--;
9836d07bcecSMiao Xie 	}
9846d07bcecSMiao Xie 
9856d07bcecSMiao Xie 	kfree(devices_info);
9866d07bcecSMiao Xie 	*free_bytes = avail_space;
9876d07bcecSMiao Xie 	return 0;
9886d07bcecSMiao Xie }
9896d07bcecSMiao Xie 
9908fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
9918fd17795SChris Mason {
9928fd17795SChris Mason 	struct btrfs_root *root = btrfs_sb(dentry->d_sb);
9934b52dff6SChris Mason 	struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
994bd4d1088SJosef Bacik 	struct list_head *head = &root->fs_info->space_info;
995bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
996bd4d1088SJosef Bacik 	u64 total_used = 0;
9976d07bcecSMiao Xie 	u64 total_free_data = 0;
998db94535dSChris Mason 	int bits = dentry->d_sb->s_blocksize_bits;
9999d03632eSDavid Woodhouse 	__be32 *fsid = (__be32 *)root->fs_info->fsid;
10006d07bcecSMiao Xie 	int ret;
10018fd17795SChris Mason 
10026d07bcecSMiao Xie 	/* holding chunk_muext to avoid allocating new chunks */
10036d07bcecSMiao Xie 	mutex_lock(&root->fs_info->chunk_mutex);
1004bd4d1088SJosef Bacik 	rcu_read_lock();
100589a55897SJosef Bacik 	list_for_each_entry_rcu(found, head, list) {
10066d07bcecSMiao Xie 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
10076d07bcecSMiao Xie 			total_free_data += found->disk_total - found->disk_used;
10086d07bcecSMiao Xie 			total_free_data -=
10096d07bcecSMiao Xie 				btrfs_account_ro_block_groups_free_space(found);
10106d07bcecSMiao Xie 		}
10116d07bcecSMiao Xie 
1012b742bb82SYan, Zheng 		total_used += found->disk_used;
101389a55897SJosef Bacik 	}
1014bd4d1088SJosef Bacik 	rcu_read_unlock();
1015bd4d1088SJosef Bacik 
10168fd17795SChris Mason 	buf->f_namelen = BTRFS_NAME_LEN;
1017db94535dSChris Mason 	buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
1018bd4d1088SJosef Bacik 	buf->f_bfree = buf->f_blocks - (total_used >> bits);
10198fd17795SChris Mason 	buf->f_bsize = dentry->d_sb->s_blocksize;
10208fd17795SChris Mason 	buf->f_type = BTRFS_SUPER_MAGIC;
10216d07bcecSMiao Xie 	buf->f_bavail = total_free_data;
10226d07bcecSMiao Xie 	ret = btrfs_calc_avail_data_space(root, &total_free_data);
10236d07bcecSMiao Xie 	if (ret) {
10246d07bcecSMiao Xie 		mutex_unlock(&root->fs_info->chunk_mutex);
10256d07bcecSMiao Xie 		return ret;
10266d07bcecSMiao Xie 	}
10276d07bcecSMiao Xie 	buf->f_bavail += total_free_data;
10286d07bcecSMiao Xie 	buf->f_bavail = buf->f_bavail >> bits;
10296d07bcecSMiao Xie 	mutex_unlock(&root->fs_info->chunk_mutex);
1030d397712bSChris Mason 
10319d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
10329d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
10339d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
10349d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
10359d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
103632d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
103732d48fa1SDavid Woodhouse 	buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
103832d48fa1SDavid Woodhouse 	buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
103932d48fa1SDavid Woodhouse 
10408fd17795SChris Mason 	return 0;
10418fd17795SChris Mason }
1042b5133862SChris Mason 
10432e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
10442e635a27SChris Mason 	.owner		= THIS_MODULE,
10452e635a27SChris Mason 	.name		= "btrfs",
10462e635a27SChris Mason 	.get_sb		= btrfs_get_sb,
1047a061fc8dSChris Mason 	.kill_sb	= kill_anon_super,
10482e635a27SChris Mason 	.fs_flags	= FS_REQUIRES_DEV,
10492e635a27SChris Mason };
1050a9218f6bSChris Mason 
1051d352ac68SChris Mason /*
1052d352ac68SChris Mason  * used by btrfsctl to scan devices when no FS is mounted
1053d352ac68SChris Mason  */
10548a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
10558a4b83ccSChris Mason 				unsigned long arg)
10568a4b83ccSChris Mason {
10578a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
10588a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices;
1059c071fcfdSChris Mason 	int ret = -ENOTTY;
10608a4b83ccSChris Mason 
1061e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1062e441d54dSChris Mason 		return -EPERM;
1063e441d54dSChris Mason 
1064dae7b665SLi Zefan 	vol = memdup_user((void __user *)arg, sizeof(*vol));
1065dae7b665SLi Zefan 	if (IS_ERR(vol))
1066dae7b665SLi Zefan 		return PTR_ERR(vol);
1067c071fcfdSChris Mason 
10688a4b83ccSChris Mason 	switch (cmd) {
10698a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
107097288f2cSChristoph Hellwig 		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
10718a4b83ccSChris Mason 					    &btrfs_fs_type, &fs_devices);
10728a4b83ccSChris Mason 		break;
10738a4b83ccSChris Mason 	}
1074dae7b665SLi Zefan 
10758a4b83ccSChris Mason 	kfree(vol);
1076f819d837SLinda Knippers 	return ret;
10778a4b83ccSChris Mason }
10788a4b83ccSChris Mason 
10790176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
1080ed0dab6bSYan {
1081ed0dab6bSYan 	struct btrfs_root *root = btrfs_sb(sb);
1082a74a4b97SChris Mason 	mutex_lock(&root->fs_info->transaction_kthread_mutex);
1083a74a4b97SChris Mason 	mutex_lock(&root->fs_info->cleaner_mutex);
10840176260fSLinus Torvalds 	return 0;
1085ed0dab6bSYan }
1086ed0dab6bSYan 
10870176260fSLinus Torvalds static int btrfs_unfreeze(struct super_block *sb)
1088ed0dab6bSYan {
1089ed0dab6bSYan 	struct btrfs_root *root = btrfs_sb(sb);
1090a74a4b97SChris Mason 	mutex_unlock(&root->fs_info->cleaner_mutex);
1091a74a4b97SChris Mason 	mutex_unlock(&root->fs_info->transaction_kthread_mutex);
10920176260fSLinus Torvalds 	return 0;
1093ed0dab6bSYan }
10942e635a27SChris Mason 
1095b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
109676dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
1097bd555975SAl Viro 	.evict_inode	= btrfs_evict_inode,
1098e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
1099d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
1100a9572a15SEric Paris 	.show_options	= btrfs_show_options,
11014730a4bcSChris Mason 	.write_inode	= btrfs_write_inode,
1102b5133862SChris Mason 	.dirty_inode	= btrfs_dirty_inode,
11032c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
11042c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
11058fd17795SChris Mason 	.statfs		= btrfs_statfs,
1106c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
11070176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
11080176260fSLinus Torvalds 	.unfreeze_fs	= btrfs_unfreeze,
1109e20d96d6SChris Mason };
1110a9218f6bSChris Mason 
1111a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
1112a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
1113a9218f6bSChris Mason 	.compat_ioctl = btrfs_control_ioctl,
1114a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
1115a9218f6bSChris Mason };
1116a9218f6bSChris Mason 
1117a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
1118578454ffSKay Sievers 	.minor		= BTRFS_MINOR,
1119a9218f6bSChris Mason 	.name		= "btrfs-control",
1120a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
1121a9218f6bSChris Mason };
1122a9218f6bSChris Mason 
1123578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
1124578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control");
1125578454ffSKay Sievers 
1126a9218f6bSChris Mason static int btrfs_interface_init(void)
1127a9218f6bSChris Mason {
1128a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
1129a9218f6bSChris Mason }
1130a9218f6bSChris Mason 
1131b2950863SChristoph Hellwig static void btrfs_interface_exit(void)
1132a9218f6bSChris Mason {
1133a9218f6bSChris Mason 	if (misc_deregister(&btrfs_misc) < 0)
1134d397712bSChris Mason 		printk(KERN_INFO "misc_deregister failed for control device");
1135a9218f6bSChris Mason }
1136a9218f6bSChris Mason 
11372e635a27SChris Mason static int __init init_btrfs_fs(void)
11382e635a27SChris Mason {
11392c90e5d6SChris Mason 	int err;
114058176a96SJosef Bacik 
114158176a96SJosef Bacik 	err = btrfs_init_sysfs();
114258176a96SJosef Bacik 	if (err)
114358176a96SJosef Bacik 		return err;
114458176a96SJosef Bacik 
1145261507a0SLi Zefan 	err = btrfs_init_compress();
11462c90e5d6SChris Mason 	if (err)
1147a74a4b97SChris Mason 		goto free_sysfs;
1148d1310b2eSChris Mason 
1149261507a0SLi Zefan 	err = btrfs_init_cachep();
1150261507a0SLi Zefan 	if (err)
1151261507a0SLi Zefan 		goto free_compress;
1152261507a0SLi Zefan 
1153d1310b2eSChris Mason 	err = extent_io_init();
11542f4cbe64SWyatt Banks 	if (err)
11552f4cbe64SWyatt Banks 		goto free_cachep;
11562f4cbe64SWyatt Banks 
1157d1310b2eSChris Mason 	err = extent_map_init();
1158d1310b2eSChris Mason 	if (err)
1159d1310b2eSChris Mason 		goto free_extent_io;
1160d1310b2eSChris Mason 
1161a9218f6bSChris Mason 	err = btrfs_interface_init();
11622f4cbe64SWyatt Banks 	if (err)
11632f4cbe64SWyatt Banks 		goto free_extent_map;
1164c8b97818SChris Mason 
1165a9218f6bSChris Mason 	err = register_filesystem(&btrfs_fs_type);
1166a9218f6bSChris Mason 	if (err)
1167a9218f6bSChris Mason 		goto unregister_ioctl;
1168b3c3da71SChris Mason 
1169b3c3da71SChris Mason 	printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
11702f4cbe64SWyatt Banks 	return 0;
11712f4cbe64SWyatt Banks 
1172a9218f6bSChris Mason unregister_ioctl:
1173a9218f6bSChris Mason 	btrfs_interface_exit();
11742f4cbe64SWyatt Banks free_extent_map:
11752f4cbe64SWyatt Banks 	extent_map_exit();
1176d1310b2eSChris Mason free_extent_io:
1177d1310b2eSChris Mason 	extent_io_exit();
11782f4cbe64SWyatt Banks free_cachep:
11792f4cbe64SWyatt Banks 	btrfs_destroy_cachep();
1180261507a0SLi Zefan free_compress:
1181261507a0SLi Zefan 	btrfs_exit_compress();
1182a74a4b97SChris Mason free_sysfs:
11832f4cbe64SWyatt Banks 	btrfs_exit_sysfs();
11842c90e5d6SChris Mason 	return err;
11852e635a27SChris Mason }
11862e635a27SChris Mason 
11872e635a27SChris Mason static void __exit exit_btrfs_fs(void)
11882e635a27SChris Mason {
118939279cc3SChris Mason 	btrfs_destroy_cachep();
1190a52d9a80SChris Mason 	extent_map_exit();
1191d1310b2eSChris Mason 	extent_io_exit();
1192a9218f6bSChris Mason 	btrfs_interface_exit();
11932e635a27SChris Mason 	unregister_filesystem(&btrfs_fs_type);
119458176a96SJosef Bacik 	btrfs_exit_sysfs();
11958a4b83ccSChris Mason 	btrfs_cleanup_fs_uuids();
1196261507a0SLi Zefan 	btrfs_exit_compress();
11972e635a27SChris Mason }
11982e635a27SChris Mason 
11992e635a27SChris Mason module_init(init_btrfs_fs)
12002e635a27SChris Mason module_exit(exit_btrfs_fs)
12012e635a27SChris Mason 
12022e635a27SChris Mason MODULE_LICENSE("GPL");
1203