xref: /linux/fs/btrfs/super.c (revision b083916638eee513be501f53b42a4be0b9851db0)
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 
551abe9b8aSliubo #define CREATE_TRACE_POINTS
561abe9b8aSliubo #include <trace/events/btrfs.h>
571abe9b8aSliubo 
58b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops;
59e20d96d6SChris Mason 
60acce952bSliubo static const char *btrfs_decode_error(struct btrfs_fs_info *fs_info, int errno,
61acce952bSliubo 				      char nbuf[16])
62acce952bSliubo {
63acce952bSliubo 	char *errstr = NULL;
64acce952bSliubo 
65acce952bSliubo 	switch (errno) {
66acce952bSliubo 	case -EIO:
67acce952bSliubo 		errstr = "IO failure";
68acce952bSliubo 		break;
69acce952bSliubo 	case -ENOMEM:
70acce952bSliubo 		errstr = "Out of memory";
71acce952bSliubo 		break;
72acce952bSliubo 	case -EROFS:
73acce952bSliubo 		errstr = "Readonly filesystem";
74acce952bSliubo 		break;
75acce952bSliubo 	default:
76acce952bSliubo 		if (nbuf) {
77acce952bSliubo 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
78acce952bSliubo 				errstr = nbuf;
79acce952bSliubo 		}
80acce952bSliubo 		break;
81acce952bSliubo 	}
82acce952bSliubo 
83acce952bSliubo 	return errstr;
84acce952bSliubo }
85acce952bSliubo 
86acce952bSliubo static void __save_error_info(struct btrfs_fs_info *fs_info)
87acce952bSliubo {
88acce952bSliubo 	/*
89acce952bSliubo 	 * today we only save the error info into ram.  Long term we'll
90acce952bSliubo 	 * also send it down to the disk
91acce952bSliubo 	 */
92acce952bSliubo 	fs_info->fs_state = BTRFS_SUPER_FLAG_ERROR;
93acce952bSliubo }
94acce952bSliubo 
95acce952bSliubo /* NOTE:
96acce952bSliubo  *	We move write_super stuff at umount in order to avoid deadlock
97acce952bSliubo  *	for umount hold all lock.
98acce952bSliubo  */
99acce952bSliubo static void save_error_info(struct btrfs_fs_info *fs_info)
100acce952bSliubo {
101acce952bSliubo 	__save_error_info(fs_info);
102acce952bSliubo }
103acce952bSliubo 
104acce952bSliubo /* btrfs handle error by forcing the filesystem readonly */
105acce952bSliubo static void btrfs_handle_error(struct btrfs_fs_info *fs_info)
106acce952bSliubo {
107acce952bSliubo 	struct super_block *sb = fs_info->sb;
108acce952bSliubo 
109acce952bSliubo 	if (sb->s_flags & MS_RDONLY)
110acce952bSliubo 		return;
111acce952bSliubo 
112acce952bSliubo 	if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
113acce952bSliubo 		sb->s_flags |= MS_RDONLY;
114acce952bSliubo 		printk(KERN_INFO "btrfs is forced readonly\n");
115acce952bSliubo 	}
116acce952bSliubo }
117acce952bSliubo 
118acce952bSliubo /*
119acce952bSliubo  * __btrfs_std_error decodes expected errors from the caller and
120acce952bSliubo  * invokes the approciate error response.
121acce952bSliubo  */
122acce952bSliubo void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
123acce952bSliubo 		     unsigned int line, int errno)
124acce952bSliubo {
125acce952bSliubo 	struct super_block *sb = fs_info->sb;
126acce952bSliubo 	char nbuf[16];
127acce952bSliubo 	const char *errstr;
128acce952bSliubo 
129acce952bSliubo 	/*
130acce952bSliubo 	 * Special case: if the error is EROFS, and we're already
131acce952bSliubo 	 * under MS_RDONLY, then it is safe here.
132acce952bSliubo 	 */
133acce952bSliubo 	if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
134acce952bSliubo 		return;
135acce952bSliubo 
136acce952bSliubo 	errstr = btrfs_decode_error(fs_info, errno, nbuf);
137acce952bSliubo 	printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: %s\n",
138acce952bSliubo 		sb->s_id, function, line, errstr);
139acce952bSliubo 	save_error_info(fs_info);
140acce952bSliubo 
141acce952bSliubo 	btrfs_handle_error(fs_info);
142acce952bSliubo }
143acce952bSliubo 
144e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb)
145e20d96d6SChris Mason {
146e20d96d6SChris Mason 	struct btrfs_root *root = btrfs_sb(sb);
147e20d96d6SChris Mason 	int ret;
148e20d96d6SChris Mason 
149e20d96d6SChris Mason 	ret = close_ctree(root);
150e20d96d6SChris Mason 	sb->s_fs_info = NULL;
151559af821SAndi Kleen 
152559af821SAndi Kleen 	(void)ret; /* FIXME: need to fix VFS to return error? */
153e20d96d6SChris Mason }
1542e635a27SChris Mason 
15595e05289SChris Mason enum {
15673f73415SJosef Bacik 	Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
157287a0ab9SJosef Bacik 	Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
158287a0ab9SJosef Bacik 	Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
159261507a0SLi Zefan 	Opt_compress_type, Opt_compress_force, Opt_compress_force_type,
160261507a0SLi Zefan 	Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard,
16191435650SChris Mason 	Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed,
162e15d0542SXin Zhong 	Opt_enospc_debug, Opt_subvolrootid, Opt_err,
16395e05289SChris Mason };
16495e05289SChris Mason 
16595e05289SChris Mason static match_table_t tokens = {
166dfe25020SChris Mason 	{Opt_degraded, "degraded"},
16795e05289SChris Mason 	{Opt_subvol, "subvol=%s"},
16873f73415SJosef Bacik 	{Opt_subvolid, "subvolid=%d"},
16943e570b0SChristoph Hellwig 	{Opt_device, "device=%s"},
170b6cda9bcSChris Mason 	{Opt_nodatasum, "nodatasum"},
171be20aa9dSChris Mason 	{Opt_nodatacow, "nodatacow"},
17221ad10cfSChris Mason 	{Opt_nobarrier, "nobarrier"},
1736f568d35SChris Mason 	{Opt_max_inline, "max_inline=%s"},
1748f662a76SChris Mason 	{Opt_alloc_start, "alloc_start=%s"},
1754543df7eSChris Mason 	{Opt_thread_pool, "thread_pool=%d"},
176c8b97818SChris Mason 	{Opt_compress, "compress"},
177261507a0SLi Zefan 	{Opt_compress_type, "compress=%s"},
178a555f810SChris Mason 	{Opt_compress_force, "compress-force"},
179261507a0SLi Zefan 	{Opt_compress_force_type, "compress-force=%s"},
180e18e4809SChris Mason 	{Opt_ssd, "ssd"},
181451d7585SChris Mason 	{Opt_ssd_spread, "ssd_spread"},
1823b30c22fSChris Mason 	{Opt_nossd, "nossd"},
18333268eafSJosef Bacik 	{Opt_noacl, "noacl"},
1843a5e1404SSage Weil 	{Opt_notreelog, "notreelog"},
185dccae999SSage Weil 	{Opt_flushoncommit, "flushoncommit"},
18697e728d4SJosef Bacik 	{Opt_ratio, "metadata_ratio=%d"},
187e244a0aeSChristoph Hellwig 	{Opt_discard, "discard"},
1880af3d00bSJosef Bacik 	{Opt_space_cache, "space_cache"},
18988c2ba3bSJosef Bacik 	{Opt_clear_cache, "clear_cache"},
1904260f7c7SSage Weil 	{Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
19191435650SChris Mason 	{Opt_enospc_debug, "enospc_debug"},
192e15d0542SXin Zhong 	{Opt_subvolrootid, "subvolrootid=%d"},
19333268eafSJosef Bacik 	{Opt_err, NULL},
19495e05289SChris Mason };
19595e05289SChris Mason 
196edf24abeSChristoph Hellwig /*
197edf24abeSChristoph Hellwig  * Regular mount options parser.  Everything that is needed only when
198edf24abeSChristoph Hellwig  * reading in a new superblock is parsed here.
199edf24abeSChristoph Hellwig  */
200edf24abeSChristoph Hellwig int btrfs_parse_options(struct btrfs_root *root, char *options)
20195e05289SChris Mason {
202edf24abeSChristoph Hellwig 	struct btrfs_fs_info *info = root->fs_info;
20395e05289SChris Mason 	substring_t args[MAX_OPT_ARGS];
204da495eccSJosef Bacik 	char *p, *num, *orig;
2054543df7eSChris Mason 	int intarg;
206a7a3f7caSSage Weil 	int ret = 0;
207261507a0SLi Zefan 	char *compress_type;
208261507a0SLi Zefan 	bool compress_force = false;
209b6cda9bcSChris Mason 
21095e05289SChris Mason 	if (!options)
211edf24abeSChristoph Hellwig 		return 0;
21295e05289SChris Mason 
213be20aa9dSChris Mason 	/*
214be20aa9dSChris Mason 	 * strsep changes the string, duplicate it because parse_options
215be20aa9dSChris Mason 	 * gets called twice
216be20aa9dSChris Mason 	 */
217be20aa9dSChris Mason 	options = kstrdup(options, GFP_NOFS);
218be20aa9dSChris Mason 	if (!options)
219be20aa9dSChris Mason 		return -ENOMEM;
220be20aa9dSChris Mason 
221da495eccSJosef Bacik 	orig = options;
222be20aa9dSChris Mason 
22395e05289SChris Mason 	while ((p = strsep(&options, ",")) != NULL) {
22495e05289SChris Mason 		int token;
22595e05289SChris Mason 		if (!*p)
22695e05289SChris Mason 			continue;
22795e05289SChris Mason 
22895e05289SChris Mason 		token = match_token(p, tokens, args);
22995e05289SChris Mason 		switch (token) {
230dfe25020SChris Mason 		case Opt_degraded:
231edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: allowing degraded mounts\n");
232dfe25020SChris Mason 			btrfs_set_opt(info->mount_opt, DEGRADED);
233dfe25020SChris Mason 			break;
23495e05289SChris Mason 		case Opt_subvol:
23573f73415SJosef Bacik 		case Opt_subvolid:
236e15d0542SXin Zhong 		case Opt_subvolrootid:
23743e570b0SChristoph Hellwig 		case Opt_device:
238edf24abeSChristoph Hellwig 			/*
23943e570b0SChristoph Hellwig 			 * These are parsed by btrfs_parse_early_options
240edf24abeSChristoph Hellwig 			 * and can be happily ignored here.
241edf24abeSChristoph Hellwig 			 */
24295e05289SChris Mason 			break;
243b6cda9bcSChris Mason 		case Opt_nodatasum:
244067c28adSChris Mason 			printk(KERN_INFO "btrfs: setting nodatasum\n");
245b6cda9bcSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
246be20aa9dSChris Mason 			break;
247be20aa9dSChris Mason 		case Opt_nodatacow:
248edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: setting nodatacow\n");
249be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATACOW);
250be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
251b6cda9bcSChris Mason 			break;
252a555f810SChris Mason 		case Opt_compress_force:
253261507a0SLi Zefan 		case Opt_compress_force_type:
254261507a0SLi Zefan 			compress_force = true;
255261507a0SLi Zefan 		case Opt_compress:
256261507a0SLi Zefan 		case Opt_compress_type:
257261507a0SLi Zefan 			if (token == Opt_compress ||
258261507a0SLi Zefan 			    token == Opt_compress_force ||
259261507a0SLi Zefan 			    strcmp(args[0].from, "zlib") == 0) {
260261507a0SLi Zefan 				compress_type = "zlib";
261261507a0SLi Zefan 				info->compress_type = BTRFS_COMPRESS_ZLIB;
262a6fa6faeSLi Zefan 			} else if (strcmp(args[0].from, "lzo") == 0) {
263a6fa6faeSLi Zefan 				compress_type = "lzo";
264a6fa6faeSLi Zefan 				info->compress_type = BTRFS_COMPRESS_LZO;
265261507a0SLi Zefan 			} else {
266261507a0SLi Zefan 				ret = -EINVAL;
267261507a0SLi Zefan 				goto out;
268261507a0SLi Zefan 			}
269261507a0SLi Zefan 
270a555f810SChris Mason 			btrfs_set_opt(info->mount_opt, COMPRESS);
271261507a0SLi Zefan 			if (compress_force) {
272261507a0SLi Zefan 				btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
273261507a0SLi Zefan 				pr_info("btrfs: force %s compression\n",
274261507a0SLi Zefan 					compress_type);
275261507a0SLi Zefan 			} else
276261507a0SLi Zefan 				pr_info("btrfs: use %s compression\n",
277261507a0SLi Zefan 					compress_type);
278a555f810SChris Mason 			break;
279e18e4809SChris Mason 		case Opt_ssd:
280edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
281e18e4809SChris Mason 			btrfs_set_opt(info->mount_opt, SSD);
282e18e4809SChris Mason 			break;
283451d7585SChris Mason 		case Opt_ssd_spread:
284451d7585SChris Mason 			printk(KERN_INFO "btrfs: use spread ssd "
285451d7585SChris Mason 			       "allocation scheme\n");
286451d7585SChris Mason 			btrfs_set_opt(info->mount_opt, SSD);
287451d7585SChris Mason 			btrfs_set_opt(info->mount_opt, SSD_SPREAD);
288451d7585SChris Mason 			break;
2893b30c22fSChris Mason 		case Opt_nossd:
290451d7585SChris Mason 			printk(KERN_INFO "btrfs: not using ssd allocation "
291451d7585SChris Mason 			       "scheme\n");
292c289811cSChris Mason 			btrfs_set_opt(info->mount_opt, NOSSD);
2933b30c22fSChris Mason 			btrfs_clear_opt(info->mount_opt, SSD);
294451d7585SChris Mason 			btrfs_clear_opt(info->mount_opt, SSD_SPREAD);
2953b30c22fSChris Mason 			break;
29621ad10cfSChris Mason 		case Opt_nobarrier:
297edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: turning off barriers\n");
29821ad10cfSChris Mason 			btrfs_set_opt(info->mount_opt, NOBARRIER);
29921ad10cfSChris Mason 			break;
3004543df7eSChris Mason 		case Opt_thread_pool:
3014543df7eSChris Mason 			intarg = 0;
3024543df7eSChris Mason 			match_int(&args[0], &intarg);
3034543df7eSChris Mason 			if (intarg) {
3044543df7eSChris Mason 				info->thread_pool_size = intarg;
3054543df7eSChris Mason 				printk(KERN_INFO "btrfs: thread pool %d\n",
3064543df7eSChris Mason 				       info->thread_pool_size);
3074543df7eSChris Mason 			}
3084543df7eSChris Mason 			break;
3096f568d35SChris Mason 		case Opt_max_inline:
310edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
3116f568d35SChris Mason 			if (num) {
31291748467SAkinobu Mita 				info->max_inline = memparse(num, NULL);
3136f568d35SChris Mason 				kfree(num);
3146f568d35SChris Mason 
31515ada040SChris Mason 				if (info->max_inline) {
3166f568d35SChris Mason 					info->max_inline = max_t(u64,
31715ada040SChris Mason 						info->max_inline,
31815ada040SChris Mason 						root->sectorsize);
31915ada040SChris Mason 				}
320edf24abeSChristoph Hellwig 				printk(KERN_INFO "btrfs: max_inline at %llu\n",
32121380931SJoel Becker 					(unsigned long long)info->max_inline);
3226f568d35SChris Mason 			}
3236f568d35SChris Mason 			break;
3248f662a76SChris Mason 		case Opt_alloc_start:
325edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
3268f662a76SChris Mason 			if (num) {
32791748467SAkinobu Mita 				info->alloc_start = memparse(num, NULL);
3288f662a76SChris Mason 				kfree(num);
329edf24abeSChristoph Hellwig 				printk(KERN_INFO
330edf24abeSChristoph Hellwig 					"btrfs: allocations start at %llu\n",
33121380931SJoel Becker 					(unsigned long long)info->alloc_start);
3328f662a76SChris Mason 			}
3338f662a76SChris Mason 			break;
33433268eafSJosef Bacik 		case Opt_noacl:
33533268eafSJosef Bacik 			root->fs_info->sb->s_flags &= ~MS_POSIXACL;
33633268eafSJosef Bacik 			break;
3373a5e1404SSage Weil 		case Opt_notreelog:
3383a5e1404SSage Weil 			printk(KERN_INFO "btrfs: disabling tree log\n");
3393a5e1404SSage Weil 			btrfs_set_opt(info->mount_opt, NOTREELOG);
3403a5e1404SSage Weil 			break;
341dccae999SSage Weil 		case Opt_flushoncommit:
342dccae999SSage Weil 			printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
343dccae999SSage Weil 			btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
344dccae999SSage Weil 			break;
34597e728d4SJosef Bacik 		case Opt_ratio:
34697e728d4SJosef Bacik 			intarg = 0;
34797e728d4SJosef Bacik 			match_int(&args[0], &intarg);
34897e728d4SJosef Bacik 			if (intarg) {
34997e728d4SJosef Bacik 				info->metadata_ratio = intarg;
35097e728d4SJosef Bacik 				printk(KERN_INFO "btrfs: metadata ratio %d\n",
35197e728d4SJosef Bacik 				       info->metadata_ratio);
35297e728d4SJosef Bacik 			}
35397e728d4SJosef Bacik 			break;
354e244a0aeSChristoph Hellwig 		case Opt_discard:
355e244a0aeSChristoph Hellwig 			btrfs_set_opt(info->mount_opt, DISCARD);
356e244a0aeSChristoph Hellwig 			break;
3570af3d00bSJosef Bacik 		case Opt_space_cache:
3580af3d00bSJosef Bacik 			printk(KERN_INFO "btrfs: enabling disk space caching\n");
3590af3d00bSJosef Bacik 			btrfs_set_opt(info->mount_opt, SPACE_CACHE);
3600de90876SJosef Bacik 			break;
36188c2ba3bSJosef Bacik 		case Opt_clear_cache:
36288c2ba3bSJosef Bacik 			printk(KERN_INFO "btrfs: force clearing of disk cache\n");
36388c2ba3bSJosef Bacik 			btrfs_set_opt(info->mount_opt, CLEAR_CACHE);
3640af3d00bSJosef Bacik 			break;
3654260f7c7SSage Weil 		case Opt_user_subvol_rm_allowed:
3664260f7c7SSage Weil 			btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
3674260f7c7SSage Weil 			break;
36891435650SChris Mason 		case Opt_enospc_debug:
36991435650SChris Mason 			btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
37091435650SChris Mason 			break;
371a7a3f7caSSage Weil 		case Opt_err:
372a7a3f7caSSage Weil 			printk(KERN_INFO "btrfs: unrecognized mount option "
373a7a3f7caSSage Weil 			       "'%s'\n", p);
374a7a3f7caSSage Weil 			ret = -EINVAL;
375a7a3f7caSSage Weil 			goto out;
37695e05289SChris Mason 		default:
377be20aa9dSChris Mason 			break;
37895e05289SChris Mason 		}
37995e05289SChris Mason 	}
380a7a3f7caSSage Weil out:
381da495eccSJosef Bacik 	kfree(orig);
382a7a3f7caSSage Weil 	return ret;
383edf24abeSChristoph Hellwig }
384edf24abeSChristoph Hellwig 
385edf24abeSChristoph Hellwig /*
386edf24abeSChristoph Hellwig  * Parse mount options that are required early in the mount process.
387edf24abeSChristoph Hellwig  *
388edf24abeSChristoph Hellwig  * All other options will be parsed on much later in the mount process and
389edf24abeSChristoph Hellwig  * only when we need to allocate a new super block.
390edf24abeSChristoph Hellwig  */
39197288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags,
39273f73415SJosef Bacik 		void *holder, char **subvol_name, u64 *subvol_objectid,
393e15d0542SXin Zhong 		u64 *subvol_rootid, struct btrfs_fs_devices **fs_devices)
394edf24abeSChristoph Hellwig {
395edf24abeSChristoph Hellwig 	substring_t args[MAX_OPT_ARGS];
3963f3d0bc0STero Roponen 	char *opts, *orig, *p;
397edf24abeSChristoph Hellwig 	int error = 0;
39873f73415SJosef Bacik 	int intarg;
399edf24abeSChristoph Hellwig 
400edf24abeSChristoph Hellwig 	if (!options)
401edf24abeSChristoph Hellwig 		goto out;
402edf24abeSChristoph Hellwig 
403edf24abeSChristoph Hellwig 	/*
404edf24abeSChristoph Hellwig 	 * strsep changes the string, duplicate it because parse_options
405edf24abeSChristoph Hellwig 	 * gets called twice
406edf24abeSChristoph Hellwig 	 */
407edf24abeSChristoph Hellwig 	opts = kstrdup(options, GFP_KERNEL);
408edf24abeSChristoph Hellwig 	if (!opts)
409edf24abeSChristoph Hellwig 		return -ENOMEM;
4103f3d0bc0STero Roponen 	orig = opts;
411edf24abeSChristoph Hellwig 
412edf24abeSChristoph Hellwig 	while ((p = strsep(&opts, ",")) != NULL) {
413edf24abeSChristoph Hellwig 		int token;
414edf24abeSChristoph Hellwig 		if (!*p)
415edf24abeSChristoph Hellwig 			continue;
416edf24abeSChristoph Hellwig 
417edf24abeSChristoph Hellwig 		token = match_token(p, tokens, args);
418edf24abeSChristoph Hellwig 		switch (token) {
419edf24abeSChristoph Hellwig 		case Opt_subvol:
420edf24abeSChristoph Hellwig 			*subvol_name = match_strdup(&args[0]);
421edf24abeSChristoph Hellwig 			break;
42273f73415SJosef Bacik 		case Opt_subvolid:
42373f73415SJosef Bacik 			intarg = 0;
4244849f01dSJosef Bacik 			error = match_int(&args[0], &intarg);
4254849f01dSJosef Bacik 			if (!error) {
4264849f01dSJosef Bacik 				/* we want the original fs_tree */
4274849f01dSJosef Bacik 				if (!intarg)
4284849f01dSJosef Bacik 					*subvol_objectid =
4294849f01dSJosef Bacik 						BTRFS_FS_TREE_OBJECTID;
4304849f01dSJosef Bacik 				else
43173f73415SJosef Bacik 					*subvol_objectid = intarg;
4324849f01dSJosef Bacik 			}
43373f73415SJosef Bacik 			break;
434e15d0542SXin Zhong 		case Opt_subvolrootid:
435e15d0542SXin Zhong 			intarg = 0;
436e15d0542SXin Zhong 			error = match_int(&args[0], &intarg);
437e15d0542SXin Zhong 			if (!error) {
438e15d0542SXin Zhong 				/* we want the original fs_tree */
439e15d0542SXin Zhong 				if (!intarg)
440e15d0542SXin Zhong 					*subvol_rootid =
441e15d0542SXin Zhong 						BTRFS_FS_TREE_OBJECTID;
442e15d0542SXin Zhong 				else
443e15d0542SXin Zhong 					*subvol_rootid = intarg;
444e15d0542SXin Zhong 			}
445e15d0542SXin Zhong 			break;
44643e570b0SChristoph Hellwig 		case Opt_device:
44743e570b0SChristoph Hellwig 			error = btrfs_scan_one_device(match_strdup(&args[0]),
44843e570b0SChristoph Hellwig 					flags, holder, fs_devices);
44943e570b0SChristoph Hellwig 			if (error)
45043e570b0SChristoph Hellwig 				goto out_free_opts;
45143e570b0SChristoph Hellwig 			break;
452edf24abeSChristoph Hellwig 		default:
453edf24abeSChristoph Hellwig 			break;
454edf24abeSChristoph Hellwig 		}
455edf24abeSChristoph Hellwig 	}
456edf24abeSChristoph Hellwig 
45743e570b0SChristoph Hellwig  out_free_opts:
4583f3d0bc0STero Roponen 	kfree(orig);
459edf24abeSChristoph Hellwig  out:
460edf24abeSChristoph Hellwig 	/*
461edf24abeSChristoph Hellwig 	 * If no subvolume name is specified we use the default one.  Allocate
4623de4586cSChris Mason 	 * a copy of the string "." here so that code later in the
463edf24abeSChristoph Hellwig 	 * mount path doesn't care if it's the default volume or another one.
464edf24abeSChristoph Hellwig 	 */
465edf24abeSChristoph Hellwig 	if (!*subvol_name) {
4663de4586cSChris Mason 		*subvol_name = kstrdup(".", GFP_KERNEL);
467edf24abeSChristoph Hellwig 		if (!*subvol_name)
468edf24abeSChristoph Hellwig 			return -ENOMEM;
469edf24abeSChristoph Hellwig 	}
470edf24abeSChristoph Hellwig 	return error;
47195e05289SChris Mason }
47295e05289SChris Mason 
47373f73415SJosef Bacik static struct dentry *get_default_root(struct super_block *sb,
47473f73415SJosef Bacik 				       u64 subvol_objectid)
47573f73415SJosef Bacik {
47673f73415SJosef Bacik 	struct btrfs_root *root = sb->s_fs_info;
47773f73415SJosef Bacik 	struct btrfs_root *new_root;
47873f73415SJosef Bacik 	struct btrfs_dir_item *di;
47973f73415SJosef Bacik 	struct btrfs_path *path;
48073f73415SJosef Bacik 	struct btrfs_key location;
48173f73415SJosef Bacik 	struct inode *inode;
48273f73415SJosef Bacik 	struct dentry *dentry;
48373f73415SJosef Bacik 	u64 dir_id;
48473f73415SJosef Bacik 	int new = 0;
48573f73415SJosef Bacik 
48673f73415SJosef Bacik 	/*
48773f73415SJosef Bacik 	 * We have a specific subvol we want to mount, just setup location and
48873f73415SJosef Bacik 	 * go look up the root.
48973f73415SJosef Bacik 	 */
49073f73415SJosef Bacik 	if (subvol_objectid) {
49173f73415SJosef Bacik 		location.objectid = subvol_objectid;
49273f73415SJosef Bacik 		location.type = BTRFS_ROOT_ITEM_KEY;
49373f73415SJosef Bacik 		location.offset = (u64)-1;
49473f73415SJosef Bacik 		goto find_root;
49573f73415SJosef Bacik 	}
49673f73415SJosef Bacik 
49773f73415SJosef Bacik 	path = btrfs_alloc_path();
49873f73415SJosef Bacik 	if (!path)
49973f73415SJosef Bacik 		return ERR_PTR(-ENOMEM);
50073f73415SJosef Bacik 	path->leave_spinning = 1;
50173f73415SJosef Bacik 
50273f73415SJosef Bacik 	/*
50373f73415SJosef Bacik 	 * Find the "default" dir item which points to the root item that we
50473f73415SJosef Bacik 	 * will mount by default if we haven't been given a specific subvolume
50573f73415SJosef Bacik 	 * to mount.
50673f73415SJosef Bacik 	 */
50773f73415SJosef Bacik 	dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
50873f73415SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
509*b0839166SJulia Lawall 	if (IS_ERR(di)) {
510*b0839166SJulia Lawall 		btrfs_free_path(path);
511fb4f6f91SDan Carpenter 		return ERR_CAST(di);
512*b0839166SJulia Lawall 	}
51373f73415SJosef Bacik 	if (!di) {
51473f73415SJosef Bacik 		/*
51573f73415SJosef Bacik 		 * Ok the default dir item isn't there.  This is weird since
51673f73415SJosef Bacik 		 * it's always been there, but don't freak out, just try and
51773f73415SJosef Bacik 		 * mount to root most subvolume.
51873f73415SJosef Bacik 		 */
51973f73415SJosef Bacik 		btrfs_free_path(path);
52073f73415SJosef Bacik 		dir_id = BTRFS_FIRST_FREE_OBJECTID;
52173f73415SJosef Bacik 		new_root = root->fs_info->fs_root;
52273f73415SJosef Bacik 		goto setup_root;
52373f73415SJosef Bacik 	}
52473f73415SJosef Bacik 
52573f73415SJosef Bacik 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
52673f73415SJosef Bacik 	btrfs_free_path(path);
52773f73415SJosef Bacik 
52873f73415SJosef Bacik find_root:
52973f73415SJosef Bacik 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
53073f73415SJosef Bacik 	if (IS_ERR(new_root))
531d0b678cbSJulia Lawall 		return ERR_CAST(new_root);
53273f73415SJosef Bacik 
53373f73415SJosef Bacik 	if (btrfs_root_refs(&new_root->root_item) == 0)
53473f73415SJosef Bacik 		return ERR_PTR(-ENOENT);
53573f73415SJosef Bacik 
53673f73415SJosef Bacik 	dir_id = btrfs_root_dirid(&new_root->root_item);
53773f73415SJosef Bacik setup_root:
53873f73415SJosef Bacik 	location.objectid = dir_id;
53973f73415SJosef Bacik 	location.type = BTRFS_INODE_ITEM_KEY;
54073f73415SJosef Bacik 	location.offset = 0;
54173f73415SJosef Bacik 
54273f73415SJosef Bacik 	inode = btrfs_iget(sb, &location, new_root, &new);
5434cbd1149SDan Carpenter 	if (IS_ERR(inode))
5444cbd1149SDan Carpenter 		return ERR_CAST(inode);
54573f73415SJosef Bacik 
54673f73415SJosef Bacik 	/*
54773f73415SJosef Bacik 	 * If we're just mounting the root most subvol put the inode and return
54873f73415SJosef Bacik 	 * a reference to the dentry.  We will have already gotten a reference
54973f73415SJosef Bacik 	 * to the inode in btrfs_fill_super so we're good to go.
55073f73415SJosef Bacik 	 */
55173f73415SJosef Bacik 	if (!new && sb->s_root->d_inode == inode) {
55273f73415SJosef Bacik 		iput(inode);
55373f73415SJosef Bacik 		return dget(sb->s_root);
55473f73415SJosef Bacik 	}
55573f73415SJosef Bacik 
55673f73415SJosef Bacik 	if (new) {
55773f73415SJosef Bacik 		const struct qstr name = { .name = "/", .len = 1 };
55873f73415SJosef Bacik 
55973f73415SJosef Bacik 		/*
56073f73415SJosef Bacik 		 * New inode, we need to make the dentry a sibling of s_root so
56173f73415SJosef Bacik 		 * everything gets cleaned up properly on unmount.
56273f73415SJosef Bacik 		 */
56373f73415SJosef Bacik 		dentry = d_alloc(sb->s_root, &name);
56473f73415SJosef Bacik 		if (!dentry) {
56573f73415SJosef Bacik 			iput(inode);
56673f73415SJosef Bacik 			return ERR_PTR(-ENOMEM);
56773f73415SJosef Bacik 		}
56873f73415SJosef Bacik 		d_splice_alias(inode, dentry);
56973f73415SJosef Bacik 	} else {
57073f73415SJosef Bacik 		/*
57173f73415SJosef Bacik 		 * We found the inode in cache, just find a dentry for it and
57273f73415SJosef Bacik 		 * put the reference to the inode we just got.
57373f73415SJosef Bacik 		 */
57473f73415SJosef Bacik 		dentry = d_find_alias(inode);
57573f73415SJosef Bacik 		iput(inode);
57673f73415SJosef Bacik 	}
57773f73415SJosef Bacik 
57873f73415SJosef Bacik 	return dentry;
57973f73415SJosef Bacik }
58073f73415SJosef Bacik 
5818a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb,
5828a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
5838a4b83ccSChris Mason 			    void *data, int silent)
5842e635a27SChris Mason {
5852e635a27SChris Mason 	struct inode *inode;
586e20d96d6SChris Mason 	struct dentry *root_dentry;
5870f7d52f4SChris Mason 	struct btrfs_root *tree_root;
5885d4f98a2SYan Zheng 	struct btrfs_key key;
58939279cc3SChris Mason 	int err;
5902e635a27SChris Mason 
5912e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
5922e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
593e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
594af53d29aSAl Viro 	sb->s_d_op = &btrfs_dentry_operations;
595be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
5965103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
5972e635a27SChris Mason 	sb->s_time_gran = 1;
5980eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL
59933268eafSJosef Bacik 	sb->s_flags |= MS_POSIXACL;
60049cf6f45SChris Ball #endif
601e20d96d6SChris Mason 
602dfe25020SChris Mason 	tree_root = open_ctree(sb, fs_devices, (char *)data);
603d98237b3SChris Mason 
604e58ca020SYan 	if (IS_ERR(tree_root)) {
605e20d96d6SChris Mason 		printk("btrfs: open_ctree failed\n");
606e58ca020SYan 		return PTR_ERR(tree_root);
607e20d96d6SChris Mason 	}
6080f7d52f4SChris Mason 	sb->s_fs_info = tree_root;
609b888db2bSChris Mason 
6105d4f98a2SYan Zheng 	key.objectid = BTRFS_FIRST_FREE_OBJECTID;
6115d4f98a2SYan Zheng 	key.type = BTRFS_INODE_ITEM_KEY;
6125d4f98a2SYan Zheng 	key.offset = 0;
61373f73415SJosef Bacik 	inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root, NULL);
6145d4f98a2SYan Zheng 	if (IS_ERR(inode)) {
6155d4f98a2SYan Zheng 		err = PTR_ERR(inode);
61639279cc3SChris Mason 		goto fail_close;
61739279cc3SChris Mason 	}
6182e635a27SChris Mason 
619e20d96d6SChris Mason 	root_dentry = d_alloc_root(inode);
620e20d96d6SChris Mason 	if (!root_dentry) {
6212e635a27SChris Mason 		iput(inode);
62239279cc3SChris Mason 		err = -ENOMEM;
62339279cc3SChris Mason 		goto fail_close;
6242e635a27SChris Mason 	}
62558176a96SJosef Bacik 
626e20d96d6SChris Mason 	sb->s_root = root_dentry;
6276885f308SChris Mason 
6286885f308SChris Mason 	save_mount_options(sb, data);
6292e635a27SChris Mason 	return 0;
6302e635a27SChris Mason 
63139279cc3SChris Mason fail_close:
63239279cc3SChris Mason 	close_ctree(tree_root);
633d5719762SChris Mason 	return err;
634d5719762SChris Mason }
635d5719762SChris Mason 
6366bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
637d5719762SChris Mason {
638d5719762SChris Mason 	struct btrfs_trans_handle *trans;
639dccae999SSage Weil 	struct btrfs_root *root = btrfs_sb(sb);
640d5719762SChris Mason 	int ret;
641df2ce34cSChris Mason 
6421abe9b8aSliubo 	trace_btrfs_sync_fs(wait);
6431abe9b8aSliubo 
644d561c025SChris Mason 	if (!wait) {
6457cfcc17eSChris Mason 		filemap_flush(root->fs_info->btree_inode->i_mapping);
646df2ce34cSChris Mason 		return 0;
647d561c025SChris Mason 	}
648771ed689SChris Mason 
64924bbcf04SYan, Zheng 	btrfs_start_delalloc_inodes(root, 0);
65024bbcf04SYan, Zheng 	btrfs_wait_ordered_extents(root, 0, 0);
651771ed689SChris Mason 
652a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 0);
65398d5dc13STsutomu Itoh 	if (IS_ERR(trans))
65498d5dc13STsutomu Itoh 		return PTR_ERR(trans);
655d5719762SChris Mason 	ret = btrfs_commit_transaction(trans, root);
65654aa1f4dSChris Mason 	return ret;
657d5719762SChris Mason }
658d5719762SChris Mason 
659a9572a15SEric Paris static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
660a9572a15SEric Paris {
661a9572a15SEric Paris 	struct btrfs_root *root = btrfs_sb(vfs->mnt_sb);
662a9572a15SEric Paris 	struct btrfs_fs_info *info = root->fs_info;
663200da64eSTsutomu Itoh 	char *compress_type;
664a9572a15SEric Paris 
665a9572a15SEric Paris 	if (btrfs_test_opt(root, DEGRADED))
666a9572a15SEric Paris 		seq_puts(seq, ",degraded");
667a9572a15SEric Paris 	if (btrfs_test_opt(root, NODATASUM))
668a9572a15SEric Paris 		seq_puts(seq, ",nodatasum");
669a9572a15SEric Paris 	if (btrfs_test_opt(root, NODATACOW))
670a9572a15SEric Paris 		seq_puts(seq, ",nodatacow");
671a9572a15SEric Paris 	if (btrfs_test_opt(root, NOBARRIER))
672a9572a15SEric Paris 		seq_puts(seq, ",nobarrier");
673a9572a15SEric Paris 	if (info->max_inline != 8192 * 1024)
67421380931SJoel Becker 		seq_printf(seq, ",max_inline=%llu",
67521380931SJoel Becker 			   (unsigned long long)info->max_inline);
676a9572a15SEric Paris 	if (info->alloc_start != 0)
67721380931SJoel Becker 		seq_printf(seq, ",alloc_start=%llu",
67821380931SJoel Becker 			   (unsigned long long)info->alloc_start);
679a9572a15SEric Paris 	if (info->thread_pool_size !=  min_t(unsigned long,
680a9572a15SEric Paris 					     num_online_cpus() + 2, 8))
681a9572a15SEric Paris 		seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
682200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, COMPRESS)) {
683200da64eSTsutomu Itoh 		if (info->compress_type == BTRFS_COMPRESS_ZLIB)
684200da64eSTsutomu Itoh 			compress_type = "zlib";
685200da64eSTsutomu Itoh 		else
686200da64eSTsutomu Itoh 			compress_type = "lzo";
687200da64eSTsutomu Itoh 		if (btrfs_test_opt(root, FORCE_COMPRESS))
688200da64eSTsutomu Itoh 			seq_printf(seq, ",compress-force=%s", compress_type);
689200da64eSTsutomu Itoh 		else
690200da64eSTsutomu Itoh 			seq_printf(seq, ",compress=%s", compress_type);
691200da64eSTsutomu Itoh 	}
692c289811cSChris Mason 	if (btrfs_test_opt(root, NOSSD))
693c289811cSChris Mason 		seq_puts(seq, ",nossd");
694451d7585SChris Mason 	if (btrfs_test_opt(root, SSD_SPREAD))
695451d7585SChris Mason 		seq_puts(seq, ",ssd_spread");
696451d7585SChris Mason 	else if (btrfs_test_opt(root, SSD))
697a9572a15SEric Paris 		seq_puts(seq, ",ssd");
6983a5e1404SSage Weil 	if (btrfs_test_opt(root, NOTREELOG))
6996b65c5c6SSage Weil 		seq_puts(seq, ",notreelog");
700dccae999SSage Weil 	if (btrfs_test_opt(root, FLUSHONCOMMIT))
7016b65c5c6SSage Weil 		seq_puts(seq, ",flushoncommit");
70220a5239aSMatthew Wilcox 	if (btrfs_test_opt(root, DISCARD))
70320a5239aSMatthew Wilcox 		seq_puts(seq, ",discard");
704a9572a15SEric Paris 	if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
705a9572a15SEric Paris 		seq_puts(seq, ",noacl");
706200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, SPACE_CACHE))
707200da64eSTsutomu Itoh 		seq_puts(seq, ",space_cache");
708200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, CLEAR_CACHE))
709200da64eSTsutomu Itoh 		seq_puts(seq, ",clear_cache");
710200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
711200da64eSTsutomu Itoh 		seq_puts(seq, ",user_subvol_rm_allowed");
712a9572a15SEric Paris 	return 0;
713a9572a15SEric Paris }
714a9572a15SEric Paris 
715a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data)
7162e635a27SChris Mason {
717450ba0eaSJosef Bacik 	struct btrfs_root *test_root = data;
718a061fc8dSChris Mason 	struct btrfs_root *root = btrfs_sb(s);
7194b82d6e4SYan 
720619c8c76SIan Kent 	/*
721619c8c76SIan Kent 	 * If this super block is going away, return false as it
722619c8c76SIan Kent 	 * can't match as an existing super block.
723619c8c76SIan Kent 	 */
724619c8c76SIan Kent 	if (!atomic_read(&s->s_active))
725619c8c76SIan Kent 		return 0;
726450ba0eaSJosef Bacik 	return root->fs_info->fs_devices == test_root->fs_info->fs_devices;
7274b82d6e4SYan }
7284b82d6e4SYan 
729450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data)
730450ba0eaSJosef Bacik {
731450ba0eaSJosef Bacik 	s->s_fs_info = data;
732450ba0eaSJosef Bacik 
733450ba0eaSJosef Bacik 	return set_anon_super(s, data);
734450ba0eaSJosef Bacik }
735450ba0eaSJosef Bacik 
736450ba0eaSJosef Bacik 
737edf24abeSChristoph Hellwig /*
738edf24abeSChristoph Hellwig  * Find a superblock for the given device / mount point.
739edf24abeSChristoph Hellwig  *
740edf24abeSChristoph Hellwig  * Note:  This is based on get_sb_bdev from fs/super.c with a few additions
741edf24abeSChristoph Hellwig  *	  for multiple device setup.  Make sure to keep it in sync.
742edf24abeSChristoph Hellwig  */
743061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
744061dbc6bSAl Viro 		const char *dev_name, void *data)
7454b82d6e4SYan {
7464b82d6e4SYan 	struct block_device *bdev = NULL;
7474b82d6e4SYan 	struct super_block *s;
7484b82d6e4SYan 	struct dentry *root;
7498a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices = NULL;
750450ba0eaSJosef Bacik 	struct btrfs_root *tree_root = NULL;
751450ba0eaSJosef Bacik 	struct btrfs_fs_info *fs_info = NULL;
75297288f2cSChristoph Hellwig 	fmode_t mode = FMODE_READ;
75373f73415SJosef Bacik 	char *subvol_name = NULL;
75473f73415SJosef Bacik 	u64 subvol_objectid = 0;
755e15d0542SXin Zhong 	u64 subvol_rootid = 0;
7564b82d6e4SYan 	int error = 0;
7574b82d6e4SYan 
75897288f2cSChristoph Hellwig 	if (!(flags & MS_RDONLY))
75997288f2cSChristoph Hellwig 		mode |= FMODE_WRITE;
76097288f2cSChristoph Hellwig 
76197288f2cSChristoph Hellwig 	error = btrfs_parse_early_options(data, mode, fs_type,
76273f73415SJosef Bacik 					  &subvol_name, &subvol_objectid,
763e15d0542SXin Zhong 					  &subvol_rootid, &fs_devices);
764edf24abeSChristoph Hellwig 	if (error)
765061dbc6bSAl Viro 		return ERR_PTR(error);
766edf24abeSChristoph Hellwig 
76797288f2cSChristoph Hellwig 	error = btrfs_scan_one_device(dev_name, mode, fs_type, &fs_devices);
7688a4b83ccSChris Mason 	if (error)
769edf24abeSChristoph Hellwig 		goto error_free_subvol_name;
7704b82d6e4SYan 
77197288f2cSChristoph Hellwig 	error = btrfs_open_devices(fs_devices, mode, fs_type);
7728a4b83ccSChris Mason 	if (error)
773edf24abeSChristoph Hellwig 		goto error_free_subvol_name;
7748a4b83ccSChris Mason 
7752b82032cSYan Zheng 	if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
7762b82032cSYan Zheng 		error = -EACCES;
7772b82032cSYan Zheng 		goto error_close_devices;
7782b82032cSYan Zheng 	}
7792b82032cSYan Zheng 
780450ba0eaSJosef Bacik 	/*
781450ba0eaSJosef Bacik 	 * Setup a dummy root and fs_info for test/set super.  This is because
782450ba0eaSJosef Bacik 	 * we don't actually fill this stuff out until open_ctree, but we need
783450ba0eaSJosef Bacik 	 * it for searching for existing supers, so this lets us do that and
784450ba0eaSJosef Bacik 	 * then open_ctree will properly initialize everything later.
785450ba0eaSJosef Bacik 	 */
786450ba0eaSJosef Bacik 	fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
787450ba0eaSJosef Bacik 	tree_root = kzalloc(sizeof(struct btrfs_root), GFP_NOFS);
788450ba0eaSJosef Bacik 	if (!fs_info || !tree_root) {
789450ba0eaSJosef Bacik 		error = -ENOMEM;
790450ba0eaSJosef Bacik 		goto error_close_devices;
791450ba0eaSJosef Bacik 	}
792450ba0eaSJosef Bacik 	fs_info->tree_root = tree_root;
793450ba0eaSJosef Bacik 	fs_info->fs_devices = fs_devices;
794450ba0eaSJosef Bacik 	tree_root->fs_info = fs_info;
795450ba0eaSJosef Bacik 
796dfe25020SChris Mason 	bdev = fs_devices->latest_bdev;
797450ba0eaSJosef Bacik 	s = sget(fs_type, btrfs_test_super, btrfs_set_super, tree_root);
7984b82d6e4SYan 	if (IS_ERR(s))
7994b82d6e4SYan 		goto error_s;
8004b82d6e4SYan 
8014b82d6e4SYan 	if (s->s_root) {
8024b82d6e4SYan 		if ((flags ^ s->s_flags) & MS_RDONLY) {
8036f5bbff9SAl Viro 			deactivate_locked_super(s);
8044b82d6e4SYan 			error = -EBUSY;
805c146afadSYan Zheng 			goto error_close_devices;
8064b82d6e4SYan 		}
8074b82d6e4SYan 
8082b82032cSYan Zheng 		btrfs_close_devices(fs_devices);
809bdc924bbSIan Kent 		kfree(fs_info);
810bdc924bbSIan Kent 		kfree(tree_root);
8114b82d6e4SYan 	} else {
8124b82d6e4SYan 		char b[BDEVNAME_SIZE];
8134b82d6e4SYan 
8144b82d6e4SYan 		s->s_flags = flags;
8154b82d6e4SYan 		strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
8168a4b83ccSChris Mason 		error = btrfs_fill_super(s, fs_devices, data,
8178a4b83ccSChris Mason 					 flags & MS_SILENT ? 1 : 0);
8184b82d6e4SYan 		if (error) {
8196f5bbff9SAl Viro 			deactivate_locked_super(s);
8201f483660SShen Feng 			goto error_free_subvol_name;
8214b82d6e4SYan 		}
8224b82d6e4SYan 
823788f20ebSChris Mason 		btrfs_sb(s)->fs_info->bdev_holder = fs_type;
8244b82d6e4SYan 		s->s_flags |= MS_ACTIVE;
8254b82d6e4SYan 	}
8264b82d6e4SYan 
827e15d0542SXin Zhong 	/* if they gave us a subvolume name bind mount into that */
828e15d0542SXin Zhong 	if (strcmp(subvol_name, ".")) {
829e15d0542SXin Zhong 		struct dentry *new_root;
830e15d0542SXin Zhong 
831e15d0542SXin Zhong 		root = get_default_root(s, subvol_rootid);
8324b82d6e4SYan 		if (IS_ERR(root)) {
8334b82d6e4SYan 			error = PTR_ERR(root);
83473f73415SJosef Bacik 			deactivate_locked_super(s);
8350e78340fSJosef Bacik 			goto error_free_subvol_name;
8364b82d6e4SYan 		}
837e15d0542SXin Zhong 
83873f73415SJosef Bacik 		mutex_lock(&root->d_inode->i_mutex);
83973f73415SJosef Bacik 		new_root = lookup_one_len(subvol_name, root,
84073f73415SJosef Bacik 				      strlen(subvol_name));
84173f73415SJosef Bacik 		mutex_unlock(&root->d_inode->i_mutex);
84273f73415SJosef Bacik 
84373f73415SJosef Bacik 		if (IS_ERR(new_root)) {
844f106e82cSLi Zefan 			dput(root);
84573f73415SJosef Bacik 			deactivate_locked_super(s);
84673f73415SJosef Bacik 			error = PTR_ERR(new_root);
8470e78340fSJosef Bacik 			goto error_free_subvol_name;
84873f73415SJosef Bacik 		}
84973f73415SJosef Bacik 		if (!new_root->d_inode) {
85073f73415SJosef Bacik 			dput(root);
85173f73415SJosef Bacik 			dput(new_root);
8526f5bbff9SAl Viro 			deactivate_locked_super(s);
8534b82d6e4SYan 			error = -ENXIO;
8540e78340fSJosef Bacik 			goto error_free_subvol_name;
8554b82d6e4SYan 		}
85673f73415SJosef Bacik 		dput(root);
85773f73415SJosef Bacik 		root = new_root;
858e15d0542SXin Zhong 	} else {
859e15d0542SXin Zhong 		root = get_default_root(s, subvol_objectid);
860e15d0542SXin Zhong 		if (IS_ERR(root)) {
861e15d0542SXin Zhong 			error = PTR_ERR(root);
862e15d0542SXin Zhong 			deactivate_locked_super(s);
863e15d0542SXin Zhong 			goto error_free_subvol_name;
864e15d0542SXin Zhong 		}
86576fcef19SDavid Woodhouse 	}
8664b82d6e4SYan 
867edf24abeSChristoph Hellwig 	kfree(subvol_name);
868061dbc6bSAl Viro 	return root;
8694b82d6e4SYan 
8704b82d6e4SYan error_s:
8714b82d6e4SYan 	error = PTR_ERR(s);
872c146afadSYan Zheng error_close_devices:
8738a4b83ccSChris Mason 	btrfs_close_devices(fs_devices);
874450ba0eaSJosef Bacik 	kfree(fs_info);
875450ba0eaSJosef Bacik 	kfree(tree_root);
876edf24abeSChristoph Hellwig error_free_subvol_name:
877edf24abeSChristoph Hellwig 	kfree(subvol_name);
878061dbc6bSAl Viro 	return ERR_PTR(error);
8794b82d6e4SYan }
8802e635a27SChris Mason 
881c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
882c146afadSYan Zheng {
883c146afadSYan Zheng 	struct btrfs_root *root = btrfs_sb(sb);
884c146afadSYan Zheng 	int ret;
885c146afadSYan Zheng 
886b288052eSChris Mason 	ret = btrfs_parse_options(root, data);
887b288052eSChris Mason 	if (ret)
888b288052eSChris Mason 		return -EINVAL;
889b288052eSChris Mason 
890c146afadSYan Zheng 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
891c146afadSYan Zheng 		return 0;
892c146afadSYan Zheng 
893c146afadSYan Zheng 	if (*flags & MS_RDONLY) {
894c146afadSYan Zheng 		sb->s_flags |= MS_RDONLY;
895c146afadSYan Zheng 
896c146afadSYan Zheng 		ret =  btrfs_commit_super(root);
897c146afadSYan Zheng 		WARN_ON(ret);
898c146afadSYan Zheng 	} else {
8992b82032cSYan Zheng 		if (root->fs_info->fs_devices->rw_devices == 0)
9002b82032cSYan Zheng 			return -EACCES;
9012b82032cSYan Zheng 
902c146afadSYan Zheng 		if (btrfs_super_log_root(&root->fs_info->super_copy) != 0)
903c146afadSYan Zheng 			return -EINVAL;
904c146afadSYan Zheng 
905d68fc57bSYan, Zheng 		ret = btrfs_cleanup_fs_roots(root->fs_info);
906c146afadSYan Zheng 		WARN_ON(ret);
907c146afadSYan Zheng 
908d68fc57bSYan, Zheng 		/* recover relocation */
909d68fc57bSYan, Zheng 		ret = btrfs_recover_relocation(root);
910c146afadSYan Zheng 		WARN_ON(ret);
911c146afadSYan Zheng 
912c146afadSYan Zheng 		sb->s_flags &= ~MS_RDONLY;
913c146afadSYan Zheng 	}
914c146afadSYan Zheng 
915c146afadSYan Zheng 	return 0;
916c146afadSYan Zheng }
917c146afadSYan Zheng 
9186d07bcecSMiao Xie /*
9196d07bcecSMiao Xie  * The helper to calc the free space on the devices that can be used to store
9206d07bcecSMiao Xie  * file data.
9216d07bcecSMiao Xie  */
9226d07bcecSMiao Xie static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
9236d07bcecSMiao Xie {
9246d07bcecSMiao Xie 	struct btrfs_fs_info *fs_info = root->fs_info;
9256d07bcecSMiao Xie 	struct btrfs_device_info *devices_info;
9266d07bcecSMiao Xie 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
9276d07bcecSMiao Xie 	struct btrfs_device *device;
9286d07bcecSMiao Xie 	u64 skip_space;
9296d07bcecSMiao Xie 	u64 type;
9306d07bcecSMiao Xie 	u64 avail_space;
9316d07bcecSMiao Xie 	u64 used_space;
9326d07bcecSMiao Xie 	u64 min_stripe_size;
9336d07bcecSMiao Xie 	int min_stripes = 1;
9346d07bcecSMiao Xie 	int i = 0, nr_devices;
9356d07bcecSMiao Xie 	int ret;
9366d07bcecSMiao Xie 
9376d07bcecSMiao Xie 	nr_devices = fs_info->fs_devices->rw_devices;
9386d07bcecSMiao Xie 	BUG_ON(!nr_devices);
9396d07bcecSMiao Xie 
9406d07bcecSMiao Xie 	devices_info = kmalloc(sizeof(*devices_info) * nr_devices,
9416d07bcecSMiao Xie 			       GFP_NOFS);
9426d07bcecSMiao Xie 	if (!devices_info)
9436d07bcecSMiao Xie 		return -ENOMEM;
9446d07bcecSMiao Xie 
9456d07bcecSMiao Xie 	/* calc min stripe number for data space alloction */
9466d07bcecSMiao Xie 	type = btrfs_get_alloc_profile(root, 1);
9476d07bcecSMiao Xie 	if (type & BTRFS_BLOCK_GROUP_RAID0)
9486d07bcecSMiao Xie 		min_stripes = 2;
9496d07bcecSMiao Xie 	else if (type & BTRFS_BLOCK_GROUP_RAID1)
9506d07bcecSMiao Xie 		min_stripes = 2;
9516d07bcecSMiao Xie 	else if (type & BTRFS_BLOCK_GROUP_RAID10)
9526d07bcecSMiao Xie 		min_stripes = 4;
9536d07bcecSMiao Xie 
9546d07bcecSMiao Xie 	if (type & BTRFS_BLOCK_GROUP_DUP)
9556d07bcecSMiao Xie 		min_stripe_size = 2 * BTRFS_STRIPE_LEN;
9566d07bcecSMiao Xie 	else
9576d07bcecSMiao Xie 		min_stripe_size = BTRFS_STRIPE_LEN;
9586d07bcecSMiao Xie 
9596d07bcecSMiao Xie 	list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
9606d07bcecSMiao Xie 		if (!device->in_fs_metadata)
9616d07bcecSMiao Xie 			continue;
9626d07bcecSMiao Xie 
9636d07bcecSMiao Xie 		avail_space = device->total_bytes - device->bytes_used;
9646d07bcecSMiao Xie 
9656d07bcecSMiao Xie 		/* align with stripe_len */
9666d07bcecSMiao Xie 		do_div(avail_space, BTRFS_STRIPE_LEN);
9676d07bcecSMiao Xie 		avail_space *= BTRFS_STRIPE_LEN;
9686d07bcecSMiao Xie 
9696d07bcecSMiao Xie 		/*
9706d07bcecSMiao Xie 		 * In order to avoid overwritting the superblock on the drive,
9716d07bcecSMiao Xie 		 * btrfs starts at an offset of at least 1MB when doing chunk
9726d07bcecSMiao Xie 		 * allocation.
9736d07bcecSMiao Xie 		 */
9746d07bcecSMiao Xie 		skip_space = 1024 * 1024;
9756d07bcecSMiao Xie 
9766d07bcecSMiao Xie 		/* user can set the offset in fs_info->alloc_start. */
9776d07bcecSMiao Xie 		if (fs_info->alloc_start + BTRFS_STRIPE_LEN <=
9786d07bcecSMiao Xie 		    device->total_bytes)
9796d07bcecSMiao Xie 			skip_space = max(fs_info->alloc_start, skip_space);
9806d07bcecSMiao Xie 
9816d07bcecSMiao Xie 		/*
9826d07bcecSMiao Xie 		 * btrfs can not use the free space in [0, skip_space - 1],
9836d07bcecSMiao Xie 		 * we must subtract it from the total. In order to implement
9846d07bcecSMiao Xie 		 * it, we account the used space in this range first.
9856d07bcecSMiao Xie 		 */
9866d07bcecSMiao Xie 		ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1,
9876d07bcecSMiao Xie 						     &used_space);
9886d07bcecSMiao Xie 		if (ret) {
9896d07bcecSMiao Xie 			kfree(devices_info);
9906d07bcecSMiao Xie 			return ret;
9916d07bcecSMiao Xie 		}
9926d07bcecSMiao Xie 
9936d07bcecSMiao Xie 		/* calc the free space in [0, skip_space - 1] */
9946d07bcecSMiao Xie 		skip_space -= used_space;
9956d07bcecSMiao Xie 
9966d07bcecSMiao Xie 		/*
9976d07bcecSMiao Xie 		 * we can use the free space in [0, skip_space - 1], subtract
9986d07bcecSMiao Xie 		 * it from the total.
9996d07bcecSMiao Xie 		 */
10006d07bcecSMiao Xie 		if (avail_space && avail_space >= skip_space)
10016d07bcecSMiao Xie 			avail_space -= skip_space;
10026d07bcecSMiao Xie 		else
10036d07bcecSMiao Xie 			avail_space = 0;
10046d07bcecSMiao Xie 
10056d07bcecSMiao Xie 		if (avail_space < min_stripe_size)
10066d07bcecSMiao Xie 			continue;
10076d07bcecSMiao Xie 
10086d07bcecSMiao Xie 		devices_info[i].dev = device;
10096d07bcecSMiao Xie 		devices_info[i].max_avail = avail_space;
10106d07bcecSMiao Xie 
10116d07bcecSMiao Xie 		i++;
10126d07bcecSMiao Xie 	}
10136d07bcecSMiao Xie 
10146d07bcecSMiao Xie 	nr_devices = i;
10156d07bcecSMiao Xie 
10166d07bcecSMiao Xie 	btrfs_descending_sort_devices(devices_info, nr_devices);
10176d07bcecSMiao Xie 
10186d07bcecSMiao Xie 	i = nr_devices - 1;
10196d07bcecSMiao Xie 	avail_space = 0;
10206d07bcecSMiao Xie 	while (nr_devices >= min_stripes) {
10216d07bcecSMiao Xie 		if (devices_info[i].max_avail >= min_stripe_size) {
10226d07bcecSMiao Xie 			int j;
10236d07bcecSMiao Xie 			u64 alloc_size;
10246d07bcecSMiao Xie 
10256d07bcecSMiao Xie 			avail_space += devices_info[i].max_avail * min_stripes;
10266d07bcecSMiao Xie 			alloc_size = devices_info[i].max_avail;
10276d07bcecSMiao Xie 			for (j = i + 1 - min_stripes; j <= i; j++)
10286d07bcecSMiao Xie 				devices_info[j].max_avail -= alloc_size;
10296d07bcecSMiao Xie 		}
10306d07bcecSMiao Xie 		i--;
10316d07bcecSMiao Xie 		nr_devices--;
10326d07bcecSMiao Xie 	}
10336d07bcecSMiao Xie 
10346d07bcecSMiao Xie 	kfree(devices_info);
10356d07bcecSMiao Xie 	*free_bytes = avail_space;
10366d07bcecSMiao Xie 	return 0;
10376d07bcecSMiao Xie }
10386d07bcecSMiao Xie 
10398fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
10408fd17795SChris Mason {
10418fd17795SChris Mason 	struct btrfs_root *root = btrfs_sb(dentry->d_sb);
10424b52dff6SChris Mason 	struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1043bd4d1088SJosef Bacik 	struct list_head *head = &root->fs_info->space_info;
1044bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
1045bd4d1088SJosef Bacik 	u64 total_used = 0;
10466d07bcecSMiao Xie 	u64 total_free_data = 0;
1047db94535dSChris Mason 	int bits = dentry->d_sb->s_blocksize_bits;
10489d03632eSDavid Woodhouse 	__be32 *fsid = (__be32 *)root->fs_info->fsid;
10496d07bcecSMiao Xie 	int ret;
10508fd17795SChris Mason 
10516d07bcecSMiao Xie 	/* holding chunk_muext to avoid allocating new chunks */
10526d07bcecSMiao Xie 	mutex_lock(&root->fs_info->chunk_mutex);
1053bd4d1088SJosef Bacik 	rcu_read_lock();
105489a55897SJosef Bacik 	list_for_each_entry_rcu(found, head, list) {
10556d07bcecSMiao Xie 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
10566d07bcecSMiao Xie 			total_free_data += found->disk_total - found->disk_used;
10576d07bcecSMiao Xie 			total_free_data -=
10586d07bcecSMiao Xie 				btrfs_account_ro_block_groups_free_space(found);
10596d07bcecSMiao Xie 		}
10606d07bcecSMiao Xie 
1061b742bb82SYan, Zheng 		total_used += found->disk_used;
106289a55897SJosef Bacik 	}
1063bd4d1088SJosef Bacik 	rcu_read_unlock();
1064bd4d1088SJosef Bacik 
10658fd17795SChris Mason 	buf->f_namelen = BTRFS_NAME_LEN;
1066db94535dSChris Mason 	buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
1067bd4d1088SJosef Bacik 	buf->f_bfree = buf->f_blocks - (total_used >> bits);
10688fd17795SChris Mason 	buf->f_bsize = dentry->d_sb->s_blocksize;
10698fd17795SChris Mason 	buf->f_type = BTRFS_SUPER_MAGIC;
10706d07bcecSMiao Xie 	buf->f_bavail = total_free_data;
10716d07bcecSMiao Xie 	ret = btrfs_calc_avail_data_space(root, &total_free_data);
10726d07bcecSMiao Xie 	if (ret) {
10736d07bcecSMiao Xie 		mutex_unlock(&root->fs_info->chunk_mutex);
10746d07bcecSMiao Xie 		return ret;
10756d07bcecSMiao Xie 	}
10766d07bcecSMiao Xie 	buf->f_bavail += total_free_data;
10776d07bcecSMiao Xie 	buf->f_bavail = buf->f_bavail >> bits;
10786d07bcecSMiao Xie 	mutex_unlock(&root->fs_info->chunk_mutex);
1079d397712bSChris Mason 
10809d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
10819d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
10829d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
10839d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
10849d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
108532d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
108632d48fa1SDavid Woodhouse 	buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
108732d48fa1SDavid Woodhouse 	buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
108832d48fa1SDavid Woodhouse 
10898fd17795SChris Mason 	return 0;
10908fd17795SChris Mason }
1091b5133862SChris Mason 
10922e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
10932e635a27SChris Mason 	.owner		= THIS_MODULE,
10942e635a27SChris Mason 	.name		= "btrfs",
1095061dbc6bSAl Viro 	.mount		= btrfs_mount,
1096a061fc8dSChris Mason 	.kill_sb	= kill_anon_super,
10972e635a27SChris Mason 	.fs_flags	= FS_REQUIRES_DEV,
10982e635a27SChris Mason };
1099a9218f6bSChris Mason 
1100d352ac68SChris Mason /*
1101d352ac68SChris Mason  * used by btrfsctl to scan devices when no FS is mounted
1102d352ac68SChris Mason  */
11038a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
11048a4b83ccSChris Mason 				unsigned long arg)
11058a4b83ccSChris Mason {
11068a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
11078a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices;
1108c071fcfdSChris Mason 	int ret = -ENOTTY;
11098a4b83ccSChris Mason 
1110e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1111e441d54dSChris Mason 		return -EPERM;
1112e441d54dSChris Mason 
1113dae7b665SLi Zefan 	vol = memdup_user((void __user *)arg, sizeof(*vol));
1114dae7b665SLi Zefan 	if (IS_ERR(vol))
1115dae7b665SLi Zefan 		return PTR_ERR(vol);
1116c071fcfdSChris Mason 
11178a4b83ccSChris Mason 	switch (cmd) {
11188a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
111997288f2cSChristoph Hellwig 		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
11208a4b83ccSChris Mason 					    &btrfs_fs_type, &fs_devices);
11218a4b83ccSChris Mason 		break;
11228a4b83ccSChris Mason 	}
1123dae7b665SLi Zefan 
11248a4b83ccSChris Mason 	kfree(vol);
1125f819d837SLinda Knippers 	return ret;
11268a4b83ccSChris Mason }
11278a4b83ccSChris Mason 
11280176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
1129ed0dab6bSYan {
1130ed0dab6bSYan 	struct btrfs_root *root = btrfs_sb(sb);
1131a74a4b97SChris Mason 	mutex_lock(&root->fs_info->transaction_kthread_mutex);
1132a74a4b97SChris Mason 	mutex_lock(&root->fs_info->cleaner_mutex);
11330176260fSLinus Torvalds 	return 0;
1134ed0dab6bSYan }
1135ed0dab6bSYan 
11360176260fSLinus Torvalds static int btrfs_unfreeze(struct super_block *sb)
1137ed0dab6bSYan {
1138ed0dab6bSYan 	struct btrfs_root *root = btrfs_sb(sb);
1139a74a4b97SChris Mason 	mutex_unlock(&root->fs_info->cleaner_mutex);
1140a74a4b97SChris Mason 	mutex_unlock(&root->fs_info->transaction_kthread_mutex);
11410176260fSLinus Torvalds 	return 0;
1142ed0dab6bSYan }
11432e635a27SChris Mason 
1144b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
114576dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
1146bd555975SAl Viro 	.evict_inode	= btrfs_evict_inode,
1147e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
1148d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
1149a9572a15SEric Paris 	.show_options	= btrfs_show_options,
11504730a4bcSChris Mason 	.write_inode	= btrfs_write_inode,
1151b5133862SChris Mason 	.dirty_inode	= btrfs_dirty_inode,
11522c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
11532c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
11548fd17795SChris Mason 	.statfs		= btrfs_statfs,
1155c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
11560176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
11570176260fSLinus Torvalds 	.unfreeze_fs	= btrfs_unfreeze,
1158e20d96d6SChris Mason };
1159a9218f6bSChris Mason 
1160a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
1161a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
1162a9218f6bSChris Mason 	.compat_ioctl = btrfs_control_ioctl,
1163a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
11646038f373SArnd Bergmann 	.llseek = noop_llseek,
1165a9218f6bSChris Mason };
1166a9218f6bSChris Mason 
1167a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
1168578454ffSKay Sievers 	.minor		= BTRFS_MINOR,
1169a9218f6bSChris Mason 	.name		= "btrfs-control",
1170a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
1171a9218f6bSChris Mason };
1172a9218f6bSChris Mason 
1173578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
1174578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control");
1175578454ffSKay Sievers 
1176a9218f6bSChris Mason static int btrfs_interface_init(void)
1177a9218f6bSChris Mason {
1178a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
1179a9218f6bSChris Mason }
1180a9218f6bSChris Mason 
1181b2950863SChristoph Hellwig static void btrfs_interface_exit(void)
1182a9218f6bSChris Mason {
1183a9218f6bSChris Mason 	if (misc_deregister(&btrfs_misc) < 0)
1184d397712bSChris Mason 		printk(KERN_INFO "misc_deregister failed for control device");
1185a9218f6bSChris Mason }
1186a9218f6bSChris Mason 
11872e635a27SChris Mason static int __init init_btrfs_fs(void)
11882e635a27SChris Mason {
11892c90e5d6SChris Mason 	int err;
119058176a96SJosef Bacik 
119158176a96SJosef Bacik 	err = btrfs_init_sysfs();
119258176a96SJosef Bacik 	if (err)
119358176a96SJosef Bacik 		return err;
119458176a96SJosef Bacik 
1195261507a0SLi Zefan 	err = btrfs_init_compress();
11962c90e5d6SChris Mason 	if (err)
1197a74a4b97SChris Mason 		goto free_sysfs;
1198d1310b2eSChris Mason 
1199261507a0SLi Zefan 	err = btrfs_init_cachep();
1200261507a0SLi Zefan 	if (err)
1201261507a0SLi Zefan 		goto free_compress;
1202261507a0SLi Zefan 
1203d1310b2eSChris Mason 	err = extent_io_init();
12042f4cbe64SWyatt Banks 	if (err)
12052f4cbe64SWyatt Banks 		goto free_cachep;
12062f4cbe64SWyatt Banks 
1207d1310b2eSChris Mason 	err = extent_map_init();
1208d1310b2eSChris Mason 	if (err)
1209d1310b2eSChris Mason 		goto free_extent_io;
1210d1310b2eSChris Mason 
1211a9218f6bSChris Mason 	err = btrfs_interface_init();
12122f4cbe64SWyatt Banks 	if (err)
12132f4cbe64SWyatt Banks 		goto free_extent_map;
1214c8b97818SChris Mason 
1215a9218f6bSChris Mason 	err = register_filesystem(&btrfs_fs_type);
1216a9218f6bSChris Mason 	if (err)
1217a9218f6bSChris Mason 		goto unregister_ioctl;
1218b3c3da71SChris Mason 
1219b3c3da71SChris Mason 	printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
12202f4cbe64SWyatt Banks 	return 0;
12212f4cbe64SWyatt Banks 
1222a9218f6bSChris Mason unregister_ioctl:
1223a9218f6bSChris Mason 	btrfs_interface_exit();
12242f4cbe64SWyatt Banks free_extent_map:
12252f4cbe64SWyatt Banks 	extent_map_exit();
1226d1310b2eSChris Mason free_extent_io:
1227d1310b2eSChris Mason 	extent_io_exit();
12282f4cbe64SWyatt Banks free_cachep:
12292f4cbe64SWyatt Banks 	btrfs_destroy_cachep();
1230261507a0SLi Zefan free_compress:
1231261507a0SLi Zefan 	btrfs_exit_compress();
1232a74a4b97SChris Mason free_sysfs:
12332f4cbe64SWyatt Banks 	btrfs_exit_sysfs();
12342c90e5d6SChris Mason 	return err;
12352e635a27SChris Mason }
12362e635a27SChris Mason 
12372e635a27SChris Mason static void __exit exit_btrfs_fs(void)
12382e635a27SChris Mason {
123939279cc3SChris Mason 	btrfs_destroy_cachep();
1240a52d9a80SChris Mason 	extent_map_exit();
1241d1310b2eSChris Mason 	extent_io_exit();
1242a9218f6bSChris Mason 	btrfs_interface_exit();
12432e635a27SChris Mason 	unregister_filesystem(&btrfs_fs_type);
124458176a96SJosef Bacik 	btrfs_exit_sysfs();
12458a4b83ccSChris Mason 	btrfs_cleanup_fs_uuids();
1246261507a0SLi Zefan 	btrfs_exit_compress();
12472e635a27SChris Mason }
12482e635a27SChris Mason 
12492e635a27SChris Mason module_init(init_btrfs_fs)
12502e635a27SChris Mason module_exit(exit_btrfs_fs)
12512e635a27SChris Mason 
12522e635a27SChris Mason MODULE_LICENSE("GPL");
1253