xref: /linux/fs/btrfs/super.c (revision 830c4adbd04a79f806d4fa579546f36a71b727c1)
16cbd5570SChris Mason /*
26cbd5570SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
36cbd5570SChris Mason  *
46cbd5570SChris Mason  * This program is free software; you can redistribute it and/or
56cbd5570SChris Mason  * modify it under the terms of the GNU General Public
66cbd5570SChris Mason  * License v2 as published by the Free Software Foundation.
76cbd5570SChris Mason  *
86cbd5570SChris Mason  * This program is distributed in the hope that it will be useful,
96cbd5570SChris Mason  * but WITHOUT ANY WARRANTY; without even the implied warranty of
106cbd5570SChris Mason  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116cbd5570SChris Mason  * General Public License for more details.
126cbd5570SChris Mason  *
136cbd5570SChris Mason  * You should have received a copy of the GNU General Public
146cbd5570SChris Mason  * License along with this program; if not, write to the
156cbd5570SChris Mason  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
166cbd5570SChris Mason  * Boston, MA 021110-1307, USA.
176cbd5570SChris Mason  */
186cbd5570SChris Mason 
194b82d6e4SYan #include <linux/blkdev.h>
202e635a27SChris Mason #include <linux/module.h>
21e20d96d6SChris Mason #include <linux/buffer_head.h>
222e635a27SChris Mason #include <linux/fs.h>
232e635a27SChris Mason #include <linux/pagemap.h>
242e635a27SChris Mason #include <linux/highmem.h>
252e635a27SChris Mason #include <linux/time.h>
262e635a27SChris Mason #include <linux/init.h>
27a9572a15SEric Paris #include <linux/seq_file.h>
282e635a27SChris Mason #include <linux/string.h>
292e635a27SChris Mason #include <linux/backing-dev.h>
304b82d6e4SYan #include <linux/mount.h>
31dee26a9fSChris Mason #include <linux/mpage.h>
3275dfe396SChris Mason #include <linux/swap.h>
3375dfe396SChris Mason #include <linux/writeback.h>
348fd17795SChris Mason #include <linux/statfs.h>
3508607c1bSChris Mason #include <linux/compat.h>
3695e05289SChris Mason #include <linux/parser.h>
37c59f8951SChris Mason #include <linux/ctype.h>
386da6abaeSChris Mason #include <linux/namei.h>
39a9218f6bSChris Mason #include <linux/miscdevice.h>
401bcbf313SQinghuang Feng #include <linux/magic.h>
415a0e3ad6STejun Heo #include <linux/slab.h>
4290a887c9SDan Magenheimer #include <linux/cleancache.h>
43*830c4adbSJosef Bacik #include <linux/mnt_namespace.h>
444b4e25f2SChris Mason #include "compat.h"
4516cdcec7SMiao Xie #include "delayed-inode.h"
462e635a27SChris Mason #include "ctree.h"
47e20d96d6SChris Mason #include "disk-io.h"
48d5719762SChris Mason #include "transaction.h"
492c90e5d6SChris Mason #include "btrfs_inode.h"
50c5739bbaSChris Mason #include "ioctl.h"
513a686375SChris Mason #include "print-tree.h"
525103e947SJosef Bacik #include "xattr.h"
538a4b83ccSChris Mason #include "volumes.h"
54b3c3da71SChris Mason #include "version.h"
55be6e8dc0SBalaji Rao #include "export.h"
56c8b97818SChris Mason #include "compression.h"
572e635a27SChris Mason 
581abe9b8aSliubo #define CREATE_TRACE_POINTS
591abe9b8aSliubo #include <trace/events/btrfs.h>
601abe9b8aSliubo 
61b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops;
62*830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type;
63e20d96d6SChris Mason 
64acce952bSliubo static const char *btrfs_decode_error(struct btrfs_fs_info *fs_info, int errno,
65acce952bSliubo 				      char nbuf[16])
66acce952bSliubo {
67acce952bSliubo 	char *errstr = NULL;
68acce952bSliubo 
69acce952bSliubo 	switch (errno) {
70acce952bSliubo 	case -EIO:
71acce952bSliubo 		errstr = "IO failure";
72acce952bSliubo 		break;
73acce952bSliubo 	case -ENOMEM:
74acce952bSliubo 		errstr = "Out of memory";
75acce952bSliubo 		break;
76acce952bSliubo 	case -EROFS:
77acce952bSliubo 		errstr = "Readonly filesystem";
78acce952bSliubo 		break;
79acce952bSliubo 	default:
80acce952bSliubo 		if (nbuf) {
81acce952bSliubo 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
82acce952bSliubo 				errstr = nbuf;
83acce952bSliubo 		}
84acce952bSliubo 		break;
85acce952bSliubo 	}
86acce952bSliubo 
87acce952bSliubo 	return errstr;
88acce952bSliubo }
89acce952bSliubo 
90acce952bSliubo static void __save_error_info(struct btrfs_fs_info *fs_info)
91acce952bSliubo {
92acce952bSliubo 	/*
93acce952bSliubo 	 * today we only save the error info into ram.  Long term we'll
94acce952bSliubo 	 * also send it down to the disk
95acce952bSliubo 	 */
96acce952bSliubo 	fs_info->fs_state = BTRFS_SUPER_FLAG_ERROR;
97acce952bSliubo }
98acce952bSliubo 
99acce952bSliubo /* NOTE:
100acce952bSliubo  *	We move write_super stuff at umount in order to avoid deadlock
101acce952bSliubo  *	for umount hold all lock.
102acce952bSliubo  */
103acce952bSliubo static void save_error_info(struct btrfs_fs_info *fs_info)
104acce952bSliubo {
105acce952bSliubo 	__save_error_info(fs_info);
106acce952bSliubo }
107acce952bSliubo 
108acce952bSliubo /* btrfs handle error by forcing the filesystem readonly */
109acce952bSliubo static void btrfs_handle_error(struct btrfs_fs_info *fs_info)
110acce952bSliubo {
111acce952bSliubo 	struct super_block *sb = fs_info->sb;
112acce952bSliubo 
113acce952bSliubo 	if (sb->s_flags & MS_RDONLY)
114acce952bSliubo 		return;
115acce952bSliubo 
116acce952bSliubo 	if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
117acce952bSliubo 		sb->s_flags |= MS_RDONLY;
118acce952bSliubo 		printk(KERN_INFO "btrfs is forced readonly\n");
119acce952bSliubo 	}
120acce952bSliubo }
121acce952bSliubo 
122acce952bSliubo /*
123acce952bSliubo  * __btrfs_std_error decodes expected errors from the caller and
124acce952bSliubo  * invokes the approciate error response.
125acce952bSliubo  */
126acce952bSliubo void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
127acce952bSliubo 		     unsigned int line, int errno)
128acce952bSliubo {
129acce952bSliubo 	struct super_block *sb = fs_info->sb;
130acce952bSliubo 	char nbuf[16];
131acce952bSliubo 	const char *errstr;
132acce952bSliubo 
133acce952bSliubo 	/*
134acce952bSliubo 	 * Special case: if the error is EROFS, and we're already
135acce952bSliubo 	 * under MS_RDONLY, then it is safe here.
136acce952bSliubo 	 */
137acce952bSliubo 	if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
138acce952bSliubo 		return;
139acce952bSliubo 
140acce952bSliubo 	errstr = btrfs_decode_error(fs_info, errno, nbuf);
141acce952bSliubo 	printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: %s\n",
142acce952bSliubo 		sb->s_id, function, line, errstr);
143acce952bSliubo 	save_error_info(fs_info);
144acce952bSliubo 
145acce952bSliubo 	btrfs_handle_error(fs_info);
146acce952bSliubo }
147acce952bSliubo 
148e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb)
149e20d96d6SChris Mason {
150e20d96d6SChris Mason 	struct btrfs_root *root = btrfs_sb(sb);
151e20d96d6SChris Mason 	int ret;
152e20d96d6SChris Mason 
153e20d96d6SChris Mason 	ret = close_ctree(root);
154e20d96d6SChris Mason 	sb->s_fs_info = NULL;
155559af821SAndi Kleen 
156559af821SAndi Kleen 	(void)ret; /* FIXME: need to fix VFS to return error? */
157e20d96d6SChris Mason }
1582e635a27SChris Mason 
15995e05289SChris Mason enum {
16073f73415SJosef Bacik 	Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
161287a0ab9SJosef Bacik 	Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
162287a0ab9SJosef Bacik 	Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
163261507a0SLi Zefan 	Opt_compress_type, Opt_compress_force, Opt_compress_force_type,
164261507a0SLi Zefan 	Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard,
16591435650SChris Mason 	Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed,
1664b9465cbSChris Mason 	Opt_enospc_debug, Opt_subvolrootid, Opt_defrag,
1674b9465cbSChris Mason 	Opt_inode_cache, Opt_err,
16895e05289SChris Mason };
16995e05289SChris Mason 
17095e05289SChris Mason static match_table_t tokens = {
171dfe25020SChris Mason 	{Opt_degraded, "degraded"},
17295e05289SChris Mason 	{Opt_subvol, "subvol=%s"},
17373f73415SJosef Bacik 	{Opt_subvolid, "subvolid=%d"},
17443e570b0SChristoph Hellwig 	{Opt_device, "device=%s"},
175b6cda9bcSChris Mason 	{Opt_nodatasum, "nodatasum"},
176be20aa9dSChris Mason 	{Opt_nodatacow, "nodatacow"},
17721ad10cfSChris Mason 	{Opt_nobarrier, "nobarrier"},
1786f568d35SChris Mason 	{Opt_max_inline, "max_inline=%s"},
1798f662a76SChris Mason 	{Opt_alloc_start, "alloc_start=%s"},
1804543df7eSChris Mason 	{Opt_thread_pool, "thread_pool=%d"},
181c8b97818SChris Mason 	{Opt_compress, "compress"},
182261507a0SLi Zefan 	{Opt_compress_type, "compress=%s"},
183a555f810SChris Mason 	{Opt_compress_force, "compress-force"},
184261507a0SLi Zefan 	{Opt_compress_force_type, "compress-force=%s"},
185e18e4809SChris Mason 	{Opt_ssd, "ssd"},
186451d7585SChris Mason 	{Opt_ssd_spread, "ssd_spread"},
1873b30c22fSChris Mason 	{Opt_nossd, "nossd"},
18833268eafSJosef Bacik 	{Opt_noacl, "noacl"},
1893a5e1404SSage Weil 	{Opt_notreelog, "notreelog"},
190dccae999SSage Weil 	{Opt_flushoncommit, "flushoncommit"},
19197e728d4SJosef Bacik 	{Opt_ratio, "metadata_ratio=%d"},
192e244a0aeSChristoph Hellwig 	{Opt_discard, "discard"},
1930af3d00bSJosef Bacik 	{Opt_space_cache, "space_cache"},
19488c2ba3bSJosef Bacik 	{Opt_clear_cache, "clear_cache"},
1954260f7c7SSage Weil 	{Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
19691435650SChris Mason 	{Opt_enospc_debug, "enospc_debug"},
197e15d0542SXin Zhong 	{Opt_subvolrootid, "subvolrootid=%d"},
1984cb5300bSChris Mason 	{Opt_defrag, "autodefrag"},
1994b9465cbSChris Mason 	{Opt_inode_cache, "inode_cache"},
20033268eafSJosef Bacik 	{Opt_err, NULL},
20195e05289SChris Mason };
20295e05289SChris Mason 
203edf24abeSChristoph Hellwig /*
204edf24abeSChristoph Hellwig  * Regular mount options parser.  Everything that is needed only when
205edf24abeSChristoph Hellwig  * reading in a new superblock is parsed here.
206edf24abeSChristoph Hellwig  */
207edf24abeSChristoph Hellwig int btrfs_parse_options(struct btrfs_root *root, char *options)
20895e05289SChris Mason {
209edf24abeSChristoph Hellwig 	struct btrfs_fs_info *info = root->fs_info;
21095e05289SChris Mason 	substring_t args[MAX_OPT_ARGS];
211da495eccSJosef Bacik 	char *p, *num, *orig;
2124543df7eSChris Mason 	int intarg;
213a7a3f7caSSage Weil 	int ret = 0;
214261507a0SLi Zefan 	char *compress_type;
215261507a0SLi Zefan 	bool compress_force = false;
216b6cda9bcSChris Mason 
21795e05289SChris Mason 	if (!options)
218edf24abeSChristoph Hellwig 		return 0;
21995e05289SChris Mason 
220be20aa9dSChris Mason 	/*
221be20aa9dSChris Mason 	 * strsep changes the string, duplicate it because parse_options
222be20aa9dSChris Mason 	 * gets called twice
223be20aa9dSChris Mason 	 */
224be20aa9dSChris Mason 	options = kstrdup(options, GFP_NOFS);
225be20aa9dSChris Mason 	if (!options)
226be20aa9dSChris Mason 		return -ENOMEM;
227be20aa9dSChris Mason 
228da495eccSJosef Bacik 	orig = options;
229be20aa9dSChris Mason 
23095e05289SChris Mason 	while ((p = strsep(&options, ",")) != NULL) {
23195e05289SChris Mason 		int token;
23295e05289SChris Mason 		if (!*p)
23395e05289SChris Mason 			continue;
23495e05289SChris Mason 
23595e05289SChris Mason 		token = match_token(p, tokens, args);
23695e05289SChris Mason 		switch (token) {
237dfe25020SChris Mason 		case Opt_degraded:
238edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: allowing degraded mounts\n");
239dfe25020SChris Mason 			btrfs_set_opt(info->mount_opt, DEGRADED);
240dfe25020SChris Mason 			break;
24195e05289SChris Mason 		case Opt_subvol:
24273f73415SJosef Bacik 		case Opt_subvolid:
243e15d0542SXin Zhong 		case Opt_subvolrootid:
24443e570b0SChristoph Hellwig 		case Opt_device:
245edf24abeSChristoph Hellwig 			/*
24643e570b0SChristoph Hellwig 			 * These are parsed by btrfs_parse_early_options
247edf24abeSChristoph Hellwig 			 * and can be happily ignored here.
248edf24abeSChristoph Hellwig 			 */
24995e05289SChris Mason 			break;
250b6cda9bcSChris Mason 		case Opt_nodatasum:
251067c28adSChris Mason 			printk(KERN_INFO "btrfs: setting nodatasum\n");
252b6cda9bcSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
253be20aa9dSChris Mason 			break;
254be20aa9dSChris Mason 		case Opt_nodatacow:
255edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: setting nodatacow\n");
256be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATACOW);
257be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
258b6cda9bcSChris Mason 			break;
259a555f810SChris Mason 		case Opt_compress_force:
260261507a0SLi Zefan 		case Opt_compress_force_type:
261261507a0SLi Zefan 			compress_force = true;
262261507a0SLi Zefan 		case Opt_compress:
263261507a0SLi Zefan 		case Opt_compress_type:
264261507a0SLi Zefan 			if (token == Opt_compress ||
265261507a0SLi Zefan 			    token == Opt_compress_force ||
266261507a0SLi Zefan 			    strcmp(args[0].from, "zlib") == 0) {
267261507a0SLi Zefan 				compress_type = "zlib";
268261507a0SLi Zefan 				info->compress_type = BTRFS_COMPRESS_ZLIB;
269a6fa6faeSLi Zefan 			} else if (strcmp(args[0].from, "lzo") == 0) {
270a6fa6faeSLi Zefan 				compress_type = "lzo";
271a6fa6faeSLi Zefan 				info->compress_type = BTRFS_COMPRESS_LZO;
272261507a0SLi Zefan 			} else {
273261507a0SLi Zefan 				ret = -EINVAL;
274261507a0SLi Zefan 				goto out;
275261507a0SLi Zefan 			}
276261507a0SLi Zefan 
277a555f810SChris Mason 			btrfs_set_opt(info->mount_opt, COMPRESS);
278261507a0SLi Zefan 			if (compress_force) {
279261507a0SLi Zefan 				btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
280261507a0SLi Zefan 				pr_info("btrfs: force %s compression\n",
281261507a0SLi Zefan 					compress_type);
282261507a0SLi Zefan 			} else
283261507a0SLi Zefan 				pr_info("btrfs: use %s compression\n",
284261507a0SLi Zefan 					compress_type);
285a555f810SChris Mason 			break;
286e18e4809SChris Mason 		case Opt_ssd:
287edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
288e18e4809SChris Mason 			btrfs_set_opt(info->mount_opt, SSD);
289e18e4809SChris Mason 			break;
290451d7585SChris Mason 		case Opt_ssd_spread:
291451d7585SChris Mason 			printk(KERN_INFO "btrfs: use spread ssd "
292451d7585SChris Mason 			       "allocation scheme\n");
293451d7585SChris Mason 			btrfs_set_opt(info->mount_opt, SSD);
294451d7585SChris Mason 			btrfs_set_opt(info->mount_opt, SSD_SPREAD);
295451d7585SChris Mason 			break;
2963b30c22fSChris Mason 		case Opt_nossd:
297451d7585SChris Mason 			printk(KERN_INFO "btrfs: not using ssd allocation "
298451d7585SChris Mason 			       "scheme\n");
299c289811cSChris Mason 			btrfs_set_opt(info->mount_opt, NOSSD);
3003b30c22fSChris Mason 			btrfs_clear_opt(info->mount_opt, SSD);
301451d7585SChris Mason 			btrfs_clear_opt(info->mount_opt, SSD_SPREAD);
3023b30c22fSChris Mason 			break;
30321ad10cfSChris Mason 		case Opt_nobarrier:
304edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: turning off barriers\n");
30521ad10cfSChris Mason 			btrfs_set_opt(info->mount_opt, NOBARRIER);
30621ad10cfSChris Mason 			break;
3074543df7eSChris Mason 		case Opt_thread_pool:
3084543df7eSChris Mason 			intarg = 0;
3094543df7eSChris Mason 			match_int(&args[0], &intarg);
3104543df7eSChris Mason 			if (intarg) {
3114543df7eSChris Mason 				info->thread_pool_size = intarg;
3124543df7eSChris Mason 				printk(KERN_INFO "btrfs: thread pool %d\n",
3134543df7eSChris Mason 				       info->thread_pool_size);
3144543df7eSChris Mason 			}
3154543df7eSChris Mason 			break;
3166f568d35SChris Mason 		case Opt_max_inline:
317edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
3186f568d35SChris Mason 			if (num) {
31991748467SAkinobu Mita 				info->max_inline = memparse(num, NULL);
3206f568d35SChris Mason 				kfree(num);
3216f568d35SChris Mason 
32215ada040SChris Mason 				if (info->max_inline) {
3236f568d35SChris Mason 					info->max_inline = max_t(u64,
32415ada040SChris Mason 						info->max_inline,
32515ada040SChris Mason 						root->sectorsize);
32615ada040SChris Mason 				}
327edf24abeSChristoph Hellwig 				printk(KERN_INFO "btrfs: max_inline at %llu\n",
32821380931SJoel Becker 					(unsigned long long)info->max_inline);
3296f568d35SChris Mason 			}
3306f568d35SChris Mason 			break;
3318f662a76SChris Mason 		case Opt_alloc_start:
332edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
3338f662a76SChris Mason 			if (num) {
33491748467SAkinobu Mita 				info->alloc_start = memparse(num, NULL);
3358f662a76SChris Mason 				kfree(num);
336edf24abeSChristoph Hellwig 				printk(KERN_INFO
337edf24abeSChristoph Hellwig 					"btrfs: allocations start at %llu\n",
33821380931SJoel Becker 					(unsigned long long)info->alloc_start);
3398f662a76SChris Mason 			}
3408f662a76SChris Mason 			break;
34133268eafSJosef Bacik 		case Opt_noacl:
34233268eafSJosef Bacik 			root->fs_info->sb->s_flags &= ~MS_POSIXACL;
34333268eafSJosef Bacik 			break;
3443a5e1404SSage Weil 		case Opt_notreelog:
3453a5e1404SSage Weil 			printk(KERN_INFO "btrfs: disabling tree log\n");
3463a5e1404SSage Weil 			btrfs_set_opt(info->mount_opt, NOTREELOG);
3473a5e1404SSage Weil 			break;
348dccae999SSage Weil 		case Opt_flushoncommit:
349dccae999SSage Weil 			printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
350dccae999SSage Weil 			btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
351dccae999SSage Weil 			break;
35297e728d4SJosef Bacik 		case Opt_ratio:
35397e728d4SJosef Bacik 			intarg = 0;
35497e728d4SJosef Bacik 			match_int(&args[0], &intarg);
35597e728d4SJosef Bacik 			if (intarg) {
35697e728d4SJosef Bacik 				info->metadata_ratio = intarg;
35797e728d4SJosef Bacik 				printk(KERN_INFO "btrfs: metadata ratio %d\n",
35897e728d4SJosef Bacik 				       info->metadata_ratio);
35997e728d4SJosef Bacik 			}
36097e728d4SJosef Bacik 			break;
361e244a0aeSChristoph Hellwig 		case Opt_discard:
362e244a0aeSChristoph Hellwig 			btrfs_set_opt(info->mount_opt, DISCARD);
363e244a0aeSChristoph Hellwig 			break;
3640af3d00bSJosef Bacik 		case Opt_space_cache:
3650af3d00bSJosef Bacik 			printk(KERN_INFO "btrfs: enabling disk space caching\n");
3660af3d00bSJosef Bacik 			btrfs_set_opt(info->mount_opt, SPACE_CACHE);
3670de90876SJosef Bacik 			break;
3684b9465cbSChris Mason 		case Opt_inode_cache:
3694b9465cbSChris Mason 			printk(KERN_INFO "btrfs: enabling inode map caching\n");
3704b9465cbSChris Mason 			btrfs_set_opt(info->mount_opt, INODE_MAP_CACHE);
3714b9465cbSChris Mason 			break;
37288c2ba3bSJosef Bacik 		case Opt_clear_cache:
37388c2ba3bSJosef Bacik 			printk(KERN_INFO "btrfs: force clearing of disk cache\n");
37488c2ba3bSJosef Bacik 			btrfs_set_opt(info->mount_opt, CLEAR_CACHE);
3750af3d00bSJosef Bacik 			break;
3764260f7c7SSage Weil 		case Opt_user_subvol_rm_allowed:
3774260f7c7SSage Weil 			btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
3784260f7c7SSage Weil 			break;
37991435650SChris Mason 		case Opt_enospc_debug:
38091435650SChris Mason 			btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
38191435650SChris Mason 			break;
3824cb5300bSChris Mason 		case Opt_defrag:
3834cb5300bSChris Mason 			printk(KERN_INFO "btrfs: enabling auto defrag");
3844cb5300bSChris Mason 			btrfs_set_opt(info->mount_opt, AUTO_DEFRAG);
3854cb5300bSChris Mason 			break;
386a7a3f7caSSage Weil 		case Opt_err:
387a7a3f7caSSage Weil 			printk(KERN_INFO "btrfs: unrecognized mount option "
388a7a3f7caSSage Weil 			       "'%s'\n", p);
389a7a3f7caSSage Weil 			ret = -EINVAL;
390a7a3f7caSSage Weil 			goto out;
39195e05289SChris Mason 		default:
392be20aa9dSChris Mason 			break;
39395e05289SChris Mason 		}
39495e05289SChris Mason 	}
395a7a3f7caSSage Weil out:
396da495eccSJosef Bacik 	kfree(orig);
397a7a3f7caSSage Weil 	return ret;
398edf24abeSChristoph Hellwig }
399edf24abeSChristoph Hellwig 
400edf24abeSChristoph Hellwig /*
401edf24abeSChristoph Hellwig  * Parse mount options that are required early in the mount process.
402edf24abeSChristoph Hellwig  *
403edf24abeSChristoph Hellwig  * All other options will be parsed on much later in the mount process and
404edf24abeSChristoph Hellwig  * only when we need to allocate a new super block.
405edf24abeSChristoph Hellwig  */
40697288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags,
40773f73415SJosef Bacik 		void *holder, char **subvol_name, u64 *subvol_objectid,
408e15d0542SXin Zhong 		u64 *subvol_rootid, struct btrfs_fs_devices **fs_devices)
409edf24abeSChristoph Hellwig {
410edf24abeSChristoph Hellwig 	substring_t args[MAX_OPT_ARGS];
4113f3d0bc0STero Roponen 	char *opts, *orig, *p;
412edf24abeSChristoph Hellwig 	int error = 0;
41373f73415SJosef Bacik 	int intarg;
414edf24abeSChristoph Hellwig 
415edf24abeSChristoph Hellwig 	if (!options)
416*830c4adbSJosef Bacik 		return 0;
417edf24abeSChristoph Hellwig 
418edf24abeSChristoph Hellwig 	/*
419edf24abeSChristoph Hellwig 	 * strsep changes the string, duplicate it because parse_options
420edf24abeSChristoph Hellwig 	 * gets called twice
421edf24abeSChristoph Hellwig 	 */
422edf24abeSChristoph Hellwig 	opts = kstrdup(options, GFP_KERNEL);
423edf24abeSChristoph Hellwig 	if (!opts)
424edf24abeSChristoph Hellwig 		return -ENOMEM;
4253f3d0bc0STero Roponen 	orig = opts;
426edf24abeSChristoph Hellwig 
427edf24abeSChristoph Hellwig 	while ((p = strsep(&opts, ",")) != NULL) {
428edf24abeSChristoph Hellwig 		int token;
429edf24abeSChristoph Hellwig 		if (!*p)
430edf24abeSChristoph Hellwig 			continue;
431edf24abeSChristoph Hellwig 
432edf24abeSChristoph Hellwig 		token = match_token(p, tokens, args);
433edf24abeSChristoph Hellwig 		switch (token) {
434edf24abeSChristoph Hellwig 		case Opt_subvol:
435edf24abeSChristoph Hellwig 			*subvol_name = match_strdup(&args[0]);
436edf24abeSChristoph Hellwig 			break;
43773f73415SJosef Bacik 		case Opt_subvolid:
43873f73415SJosef Bacik 			intarg = 0;
4394849f01dSJosef Bacik 			error = match_int(&args[0], &intarg);
4404849f01dSJosef Bacik 			if (!error) {
4414849f01dSJosef Bacik 				/* we want the original fs_tree */
4424849f01dSJosef Bacik 				if (!intarg)
4434849f01dSJosef Bacik 					*subvol_objectid =
4444849f01dSJosef Bacik 						BTRFS_FS_TREE_OBJECTID;
4454849f01dSJosef Bacik 				else
44673f73415SJosef Bacik 					*subvol_objectid = intarg;
4474849f01dSJosef Bacik 			}
44873f73415SJosef Bacik 			break;
449e15d0542SXin Zhong 		case Opt_subvolrootid:
450e15d0542SXin Zhong 			intarg = 0;
451e15d0542SXin Zhong 			error = match_int(&args[0], &intarg);
452e15d0542SXin Zhong 			if (!error) {
453e15d0542SXin Zhong 				/* we want the original fs_tree */
454e15d0542SXin Zhong 				if (!intarg)
455e15d0542SXin Zhong 					*subvol_rootid =
456e15d0542SXin Zhong 						BTRFS_FS_TREE_OBJECTID;
457e15d0542SXin Zhong 				else
458e15d0542SXin Zhong 					*subvol_rootid = intarg;
459e15d0542SXin Zhong 			}
460e15d0542SXin Zhong 			break;
46143e570b0SChristoph Hellwig 		case Opt_device:
46243e570b0SChristoph Hellwig 			error = btrfs_scan_one_device(match_strdup(&args[0]),
46343e570b0SChristoph Hellwig 					flags, holder, fs_devices);
46443e570b0SChristoph Hellwig 			if (error)
465*830c4adbSJosef Bacik 				goto out;
46643e570b0SChristoph Hellwig 			break;
467edf24abeSChristoph Hellwig 		default:
468edf24abeSChristoph Hellwig 			break;
469edf24abeSChristoph Hellwig 		}
470edf24abeSChristoph Hellwig 	}
471edf24abeSChristoph Hellwig 
472edf24abeSChristoph Hellwig out:
473*830c4adbSJosef Bacik 	kfree(orig);
474edf24abeSChristoph Hellwig 	return error;
47595e05289SChris Mason }
47695e05289SChris Mason 
47773f73415SJosef Bacik static struct dentry *get_default_root(struct super_block *sb,
47873f73415SJosef Bacik 				       u64 subvol_objectid)
47973f73415SJosef Bacik {
48073f73415SJosef Bacik 	struct btrfs_root *root = sb->s_fs_info;
48173f73415SJosef Bacik 	struct btrfs_root *new_root;
48273f73415SJosef Bacik 	struct btrfs_dir_item *di;
48373f73415SJosef Bacik 	struct btrfs_path *path;
48473f73415SJosef Bacik 	struct btrfs_key location;
48573f73415SJosef Bacik 	struct inode *inode;
48673f73415SJosef Bacik 	u64 dir_id;
48773f73415SJosef Bacik 	int new = 0;
48873f73415SJosef Bacik 
48973f73415SJosef Bacik 	/*
49073f73415SJosef Bacik 	 * We have a specific subvol we want to mount, just setup location and
49173f73415SJosef Bacik 	 * go look up the root.
49273f73415SJosef Bacik 	 */
49373f73415SJosef Bacik 	if (subvol_objectid) {
49473f73415SJosef Bacik 		location.objectid = subvol_objectid;
49573f73415SJosef Bacik 		location.type = BTRFS_ROOT_ITEM_KEY;
49673f73415SJosef Bacik 		location.offset = (u64)-1;
49773f73415SJosef Bacik 		goto find_root;
49873f73415SJosef Bacik 	}
49973f73415SJosef Bacik 
50073f73415SJosef Bacik 	path = btrfs_alloc_path();
50173f73415SJosef Bacik 	if (!path)
50273f73415SJosef Bacik 		return ERR_PTR(-ENOMEM);
50373f73415SJosef Bacik 	path->leave_spinning = 1;
50473f73415SJosef Bacik 
50573f73415SJosef Bacik 	/*
50673f73415SJosef Bacik 	 * Find the "default" dir item which points to the root item that we
50773f73415SJosef Bacik 	 * will mount by default if we haven't been given a specific subvolume
50873f73415SJosef Bacik 	 * to mount.
50973f73415SJosef Bacik 	 */
51073f73415SJosef Bacik 	dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
51173f73415SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
512b0839166SJulia Lawall 	if (IS_ERR(di)) {
513b0839166SJulia Lawall 		btrfs_free_path(path);
514fb4f6f91SDan Carpenter 		return ERR_CAST(di);
515b0839166SJulia Lawall 	}
51673f73415SJosef Bacik 	if (!di) {
51773f73415SJosef Bacik 		/*
51873f73415SJosef Bacik 		 * Ok the default dir item isn't there.  This is weird since
51973f73415SJosef Bacik 		 * it's always been there, but don't freak out, just try and
52073f73415SJosef Bacik 		 * mount to root most subvolume.
52173f73415SJosef Bacik 		 */
52273f73415SJosef Bacik 		btrfs_free_path(path);
52373f73415SJosef Bacik 		dir_id = BTRFS_FIRST_FREE_OBJECTID;
52473f73415SJosef Bacik 		new_root = root->fs_info->fs_root;
52573f73415SJosef Bacik 		goto setup_root;
52673f73415SJosef Bacik 	}
52773f73415SJosef Bacik 
52873f73415SJosef Bacik 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
52973f73415SJosef Bacik 	btrfs_free_path(path);
53073f73415SJosef Bacik 
53173f73415SJosef Bacik find_root:
53273f73415SJosef Bacik 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
53373f73415SJosef Bacik 	if (IS_ERR(new_root))
534d0b678cbSJulia Lawall 		return ERR_CAST(new_root);
53573f73415SJosef Bacik 
53673f73415SJosef Bacik 	if (btrfs_root_refs(&new_root->root_item) == 0)
53773f73415SJosef Bacik 		return ERR_PTR(-ENOENT);
53873f73415SJosef Bacik 
53973f73415SJosef Bacik 	dir_id = btrfs_root_dirid(&new_root->root_item);
54073f73415SJosef Bacik setup_root:
54173f73415SJosef Bacik 	location.objectid = dir_id;
54273f73415SJosef Bacik 	location.type = BTRFS_INODE_ITEM_KEY;
54373f73415SJosef Bacik 	location.offset = 0;
54473f73415SJosef Bacik 
54573f73415SJosef Bacik 	inode = btrfs_iget(sb, &location, new_root, &new);
5464cbd1149SDan Carpenter 	if (IS_ERR(inode))
5474cbd1149SDan Carpenter 		return ERR_CAST(inode);
54873f73415SJosef Bacik 
54973f73415SJosef Bacik 	/*
55073f73415SJosef Bacik 	 * If we're just mounting the root most subvol put the inode and return
55173f73415SJosef Bacik 	 * a reference to the dentry.  We will have already gotten a reference
55273f73415SJosef Bacik 	 * to the inode in btrfs_fill_super so we're good to go.
55373f73415SJosef Bacik 	 */
55473f73415SJosef Bacik 	if (!new && sb->s_root->d_inode == inode) {
55573f73415SJosef Bacik 		iput(inode);
55673f73415SJosef Bacik 		return dget(sb->s_root);
55773f73415SJosef Bacik 	}
55873f73415SJosef Bacik 
559ba5b8958SJosef Bacik 	return d_obtain_alias(inode);
56073f73415SJosef Bacik }
56173f73415SJosef Bacik 
5628a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb,
5638a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
5648a4b83ccSChris Mason 			    void *data, int silent)
5652e635a27SChris Mason {
5662e635a27SChris Mason 	struct inode *inode;
567e20d96d6SChris Mason 	struct dentry *root_dentry;
5680f7d52f4SChris Mason 	struct btrfs_root *tree_root;
5695d4f98a2SYan Zheng 	struct btrfs_key key;
57039279cc3SChris Mason 	int err;
5712e635a27SChris Mason 
5722e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
5732e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
574e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
575af53d29aSAl Viro 	sb->s_d_op = &btrfs_dentry_operations;
576be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
5775103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
5782e635a27SChris Mason 	sb->s_time_gran = 1;
5790eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL
58033268eafSJosef Bacik 	sb->s_flags |= MS_POSIXACL;
58149cf6f45SChris Ball #endif
582e20d96d6SChris Mason 
583dfe25020SChris Mason 	tree_root = open_ctree(sb, fs_devices, (char *)data);
584d98237b3SChris Mason 
585e58ca020SYan 	if (IS_ERR(tree_root)) {
586e20d96d6SChris Mason 		printk("btrfs: open_ctree failed\n");
587e58ca020SYan 		return PTR_ERR(tree_root);
588e20d96d6SChris Mason 	}
5890f7d52f4SChris Mason 	sb->s_fs_info = tree_root;
590b888db2bSChris Mason 
5915d4f98a2SYan Zheng 	key.objectid = BTRFS_FIRST_FREE_OBJECTID;
5925d4f98a2SYan Zheng 	key.type = BTRFS_INODE_ITEM_KEY;
5935d4f98a2SYan Zheng 	key.offset = 0;
59473f73415SJosef Bacik 	inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root, NULL);
5955d4f98a2SYan Zheng 	if (IS_ERR(inode)) {
5965d4f98a2SYan Zheng 		err = PTR_ERR(inode);
59739279cc3SChris Mason 		goto fail_close;
59839279cc3SChris Mason 	}
5992e635a27SChris Mason 
600e20d96d6SChris Mason 	root_dentry = d_alloc_root(inode);
601e20d96d6SChris Mason 	if (!root_dentry) {
6022e635a27SChris Mason 		iput(inode);
60339279cc3SChris Mason 		err = -ENOMEM;
60439279cc3SChris Mason 		goto fail_close;
6052e635a27SChris Mason 	}
60658176a96SJosef Bacik 
607e20d96d6SChris Mason 	sb->s_root = root_dentry;
6086885f308SChris Mason 
6096885f308SChris Mason 	save_mount_options(sb, data);
61090a887c9SDan Magenheimer 	cleancache_init_fs(sb);
6112e635a27SChris Mason 	return 0;
6122e635a27SChris Mason 
61339279cc3SChris Mason fail_close:
61439279cc3SChris Mason 	close_ctree(tree_root);
615d5719762SChris Mason 	return err;
616d5719762SChris Mason }
617d5719762SChris Mason 
6186bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
619d5719762SChris Mason {
620d5719762SChris Mason 	struct btrfs_trans_handle *trans;
621dccae999SSage Weil 	struct btrfs_root *root = btrfs_sb(sb);
622d5719762SChris Mason 	int ret;
623df2ce34cSChris Mason 
6241abe9b8aSliubo 	trace_btrfs_sync_fs(wait);
6251abe9b8aSliubo 
626d561c025SChris Mason 	if (!wait) {
6277cfcc17eSChris Mason 		filemap_flush(root->fs_info->btree_inode->i_mapping);
628df2ce34cSChris Mason 		return 0;
629d561c025SChris Mason 	}
630771ed689SChris Mason 
63124bbcf04SYan, Zheng 	btrfs_start_delalloc_inodes(root, 0);
63224bbcf04SYan, Zheng 	btrfs_wait_ordered_extents(root, 0, 0);
633771ed689SChris Mason 
634a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 0);
63598d5dc13STsutomu Itoh 	if (IS_ERR(trans))
63698d5dc13STsutomu Itoh 		return PTR_ERR(trans);
637d5719762SChris Mason 	ret = btrfs_commit_transaction(trans, root);
63854aa1f4dSChris Mason 	return ret;
639d5719762SChris Mason }
640d5719762SChris Mason 
641a9572a15SEric Paris static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
642a9572a15SEric Paris {
643a9572a15SEric Paris 	struct btrfs_root *root = btrfs_sb(vfs->mnt_sb);
644a9572a15SEric Paris 	struct btrfs_fs_info *info = root->fs_info;
645200da64eSTsutomu Itoh 	char *compress_type;
646a9572a15SEric Paris 
647a9572a15SEric Paris 	if (btrfs_test_opt(root, DEGRADED))
648a9572a15SEric Paris 		seq_puts(seq, ",degraded");
649a9572a15SEric Paris 	if (btrfs_test_opt(root, NODATASUM))
650a9572a15SEric Paris 		seq_puts(seq, ",nodatasum");
651a9572a15SEric Paris 	if (btrfs_test_opt(root, NODATACOW))
652a9572a15SEric Paris 		seq_puts(seq, ",nodatacow");
653a9572a15SEric Paris 	if (btrfs_test_opt(root, NOBARRIER))
654a9572a15SEric Paris 		seq_puts(seq, ",nobarrier");
655a9572a15SEric Paris 	if (info->max_inline != 8192 * 1024)
65621380931SJoel Becker 		seq_printf(seq, ",max_inline=%llu",
65721380931SJoel Becker 			   (unsigned long long)info->max_inline);
658a9572a15SEric Paris 	if (info->alloc_start != 0)
65921380931SJoel Becker 		seq_printf(seq, ",alloc_start=%llu",
66021380931SJoel Becker 			   (unsigned long long)info->alloc_start);
661a9572a15SEric Paris 	if (info->thread_pool_size !=  min_t(unsigned long,
662a9572a15SEric Paris 					     num_online_cpus() + 2, 8))
663a9572a15SEric Paris 		seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
664200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, COMPRESS)) {
665200da64eSTsutomu Itoh 		if (info->compress_type == BTRFS_COMPRESS_ZLIB)
666200da64eSTsutomu Itoh 			compress_type = "zlib";
667200da64eSTsutomu Itoh 		else
668200da64eSTsutomu Itoh 			compress_type = "lzo";
669200da64eSTsutomu Itoh 		if (btrfs_test_opt(root, FORCE_COMPRESS))
670200da64eSTsutomu Itoh 			seq_printf(seq, ",compress-force=%s", compress_type);
671200da64eSTsutomu Itoh 		else
672200da64eSTsutomu Itoh 			seq_printf(seq, ",compress=%s", compress_type);
673200da64eSTsutomu Itoh 	}
674c289811cSChris Mason 	if (btrfs_test_opt(root, NOSSD))
675c289811cSChris Mason 		seq_puts(seq, ",nossd");
676451d7585SChris Mason 	if (btrfs_test_opt(root, SSD_SPREAD))
677451d7585SChris Mason 		seq_puts(seq, ",ssd_spread");
678451d7585SChris Mason 	else if (btrfs_test_opt(root, SSD))
679a9572a15SEric Paris 		seq_puts(seq, ",ssd");
6803a5e1404SSage Weil 	if (btrfs_test_opt(root, NOTREELOG))
6816b65c5c6SSage Weil 		seq_puts(seq, ",notreelog");
682dccae999SSage Weil 	if (btrfs_test_opt(root, FLUSHONCOMMIT))
6836b65c5c6SSage Weil 		seq_puts(seq, ",flushoncommit");
68420a5239aSMatthew Wilcox 	if (btrfs_test_opt(root, DISCARD))
68520a5239aSMatthew Wilcox 		seq_puts(seq, ",discard");
686a9572a15SEric Paris 	if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
687a9572a15SEric Paris 		seq_puts(seq, ",noacl");
688200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, SPACE_CACHE))
689200da64eSTsutomu Itoh 		seq_puts(seq, ",space_cache");
690200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, CLEAR_CACHE))
691200da64eSTsutomu Itoh 		seq_puts(seq, ",clear_cache");
692200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
693200da64eSTsutomu Itoh 		seq_puts(seq, ",user_subvol_rm_allowed");
6940942caa3SDavid Sterba 	if (btrfs_test_opt(root, ENOSPC_DEBUG))
6950942caa3SDavid Sterba 		seq_puts(seq, ",enospc_debug");
6960942caa3SDavid Sterba 	if (btrfs_test_opt(root, AUTO_DEFRAG))
6970942caa3SDavid Sterba 		seq_puts(seq, ",autodefrag");
6980942caa3SDavid Sterba 	if (btrfs_test_opt(root, INODE_MAP_CACHE))
6990942caa3SDavid Sterba 		seq_puts(seq, ",inode_cache");
700a9572a15SEric Paris 	return 0;
701a9572a15SEric Paris }
702a9572a15SEric Paris 
703a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data)
7042e635a27SChris Mason {
705450ba0eaSJosef Bacik 	struct btrfs_root *test_root = data;
706a061fc8dSChris Mason 	struct btrfs_root *root = btrfs_sb(s);
7074b82d6e4SYan 
708619c8c76SIan Kent 	/*
709619c8c76SIan Kent 	 * If this super block is going away, return false as it
710619c8c76SIan Kent 	 * can't match as an existing super block.
711619c8c76SIan Kent 	 */
712619c8c76SIan Kent 	if (!atomic_read(&s->s_active))
713619c8c76SIan Kent 		return 0;
714450ba0eaSJosef Bacik 	return root->fs_info->fs_devices == test_root->fs_info->fs_devices;
7154b82d6e4SYan }
7164b82d6e4SYan 
717450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data)
718450ba0eaSJosef Bacik {
719450ba0eaSJosef Bacik 	s->s_fs_info = data;
720450ba0eaSJosef Bacik 
721450ba0eaSJosef Bacik 	return set_anon_super(s, data);
722450ba0eaSJosef Bacik }
723450ba0eaSJosef Bacik 
724*830c4adbSJosef Bacik /*
725*830c4adbSJosef Bacik  * This will strip out the subvol=%s argument for an argument string and add
726*830c4adbSJosef Bacik  * subvolid=0 to make sure we get the actual tree root for path walking to the
727*830c4adbSJosef Bacik  * subvol we want.
728*830c4adbSJosef Bacik  */
729*830c4adbSJosef Bacik static char *setup_root_args(char *args)
730*830c4adbSJosef Bacik {
731*830c4adbSJosef Bacik 	unsigned copied = 0;
732*830c4adbSJosef Bacik 	unsigned len = strlen(args) + 2;
733*830c4adbSJosef Bacik 	char *pos;
734*830c4adbSJosef Bacik 	char *ret;
735*830c4adbSJosef Bacik 
736*830c4adbSJosef Bacik 	/*
737*830c4adbSJosef Bacik 	 * We need the same args as before, but minus
738*830c4adbSJosef Bacik 	 *
739*830c4adbSJosef Bacik 	 * subvol=a
740*830c4adbSJosef Bacik 	 *
741*830c4adbSJosef Bacik 	 * and add
742*830c4adbSJosef Bacik 	 *
743*830c4adbSJosef Bacik 	 * subvolid=0
744*830c4adbSJosef Bacik 	 *
745*830c4adbSJosef Bacik 	 * which is a difference of 2 characters, so we allocate strlen(args) +
746*830c4adbSJosef Bacik 	 * 2 characters.
747*830c4adbSJosef Bacik 	 */
748*830c4adbSJosef Bacik 	ret = kzalloc(len * sizeof(char), GFP_NOFS);
749*830c4adbSJosef Bacik 	if (!ret)
750*830c4adbSJosef Bacik 		return NULL;
751*830c4adbSJosef Bacik 	pos = strstr(args, "subvol=");
752*830c4adbSJosef Bacik 
753*830c4adbSJosef Bacik 	/* This shouldn't happen, but just in case.. */
754*830c4adbSJosef Bacik 	if (!pos) {
755*830c4adbSJosef Bacik 		kfree(ret);
756*830c4adbSJosef Bacik 		return NULL;
757*830c4adbSJosef Bacik 	}
758*830c4adbSJosef Bacik 
759*830c4adbSJosef Bacik 	/*
760*830c4adbSJosef Bacik 	 * The subvol=<> arg is not at the front of the string, copy everybody
761*830c4adbSJosef Bacik 	 * up to that into ret.
762*830c4adbSJosef Bacik 	 */
763*830c4adbSJosef Bacik 	if (pos != args) {
764*830c4adbSJosef Bacik 		*pos = '\0';
765*830c4adbSJosef Bacik 		strcpy(ret, args);
766*830c4adbSJosef Bacik 		copied += strlen(args);
767*830c4adbSJosef Bacik 		pos++;
768*830c4adbSJosef Bacik 	}
769*830c4adbSJosef Bacik 
770*830c4adbSJosef Bacik 	strncpy(ret + copied, "subvolid=0", len - copied);
771*830c4adbSJosef Bacik 
772*830c4adbSJosef Bacik 	/* Length of subvolid=0 */
773*830c4adbSJosef Bacik 	copied += 10;
774*830c4adbSJosef Bacik 
775*830c4adbSJosef Bacik 	/*
776*830c4adbSJosef Bacik 	 * If there is no , after the subvol= option then we know there's no
777*830c4adbSJosef Bacik 	 * other options and we can just return.
778*830c4adbSJosef Bacik 	 */
779*830c4adbSJosef Bacik 	pos = strchr(pos, ',');
780*830c4adbSJosef Bacik 	if (!pos)
781*830c4adbSJosef Bacik 		return ret;
782*830c4adbSJosef Bacik 
783*830c4adbSJosef Bacik 	/* Copy the rest of the arguments into our buffer */
784*830c4adbSJosef Bacik 	strncpy(ret + copied, pos, len - copied);
785*830c4adbSJosef Bacik 	copied += strlen(pos);
786*830c4adbSJosef Bacik 
787*830c4adbSJosef Bacik 	return ret;
788*830c4adbSJosef Bacik }
789*830c4adbSJosef Bacik 
790*830c4adbSJosef Bacik static struct dentry *mount_subvol(const char *subvol_name, int flags,
791*830c4adbSJosef Bacik 				   const char *device_name, char *data)
792*830c4adbSJosef Bacik {
793*830c4adbSJosef Bacik 	struct super_block *s;
794*830c4adbSJosef Bacik 	struct dentry *root;
795*830c4adbSJosef Bacik 	struct vfsmount *mnt;
796*830c4adbSJosef Bacik 	struct mnt_namespace *ns_private;
797*830c4adbSJosef Bacik 	char *newargs;
798*830c4adbSJosef Bacik 	struct path path;
799*830c4adbSJosef Bacik 	int error;
800*830c4adbSJosef Bacik 
801*830c4adbSJosef Bacik 	newargs = setup_root_args(data);
802*830c4adbSJosef Bacik 	if (!newargs)
803*830c4adbSJosef Bacik 		return ERR_PTR(-ENOMEM);
804*830c4adbSJosef Bacik 	mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name,
805*830c4adbSJosef Bacik 			     newargs);
806*830c4adbSJosef Bacik 	kfree(newargs);
807*830c4adbSJosef Bacik 	if (IS_ERR(mnt))
808*830c4adbSJosef Bacik 		return ERR_CAST(mnt);
809*830c4adbSJosef Bacik 
810*830c4adbSJosef Bacik 	ns_private = create_mnt_ns(mnt);
811*830c4adbSJosef Bacik 	if (IS_ERR(ns_private)) {
812*830c4adbSJosef Bacik 		mntput(mnt);
813*830c4adbSJosef Bacik 		return ERR_CAST(ns_private);
814*830c4adbSJosef Bacik 	}
815*830c4adbSJosef Bacik 
816*830c4adbSJosef Bacik 	/*
817*830c4adbSJosef Bacik 	 * This will trigger the automount of the subvol so we can just
818*830c4adbSJosef Bacik 	 * drop the mnt we have here and return the dentry that we
819*830c4adbSJosef Bacik 	 * found.
820*830c4adbSJosef Bacik 	 */
821*830c4adbSJosef Bacik 	error = vfs_path_lookup(mnt->mnt_root, mnt, subvol_name,
822*830c4adbSJosef Bacik 				LOOKUP_FOLLOW, &path);
823*830c4adbSJosef Bacik 	put_mnt_ns(ns_private);
824*830c4adbSJosef Bacik 	if (error)
825*830c4adbSJosef Bacik 		return ERR_PTR(error);
826*830c4adbSJosef Bacik 
827*830c4adbSJosef Bacik 	/* Get a ref to the sb and the dentry we found and return it */
828*830c4adbSJosef Bacik 	s = path.mnt->mnt_sb;
829*830c4adbSJosef Bacik 	atomic_inc(&s->s_active);
830*830c4adbSJosef Bacik 	root = dget(path.dentry);
831*830c4adbSJosef Bacik 	path_put(&path);
832*830c4adbSJosef Bacik 	down_write(&s->s_umount);
833*830c4adbSJosef Bacik 
834*830c4adbSJosef Bacik 	return root;
835*830c4adbSJosef Bacik }
836450ba0eaSJosef Bacik 
837edf24abeSChristoph Hellwig /*
838edf24abeSChristoph Hellwig  * Find a superblock for the given device / mount point.
839edf24abeSChristoph Hellwig  *
840edf24abeSChristoph Hellwig  * Note:  This is based on get_sb_bdev from fs/super.c with a few additions
841edf24abeSChristoph Hellwig  *	  for multiple device setup.  Make sure to keep it in sync.
842edf24abeSChristoph Hellwig  */
843061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
844306e16ceSDavid Sterba 		const char *device_name, void *data)
8454b82d6e4SYan {
8464b82d6e4SYan 	struct block_device *bdev = NULL;
8474b82d6e4SYan 	struct super_block *s;
8484b82d6e4SYan 	struct dentry *root;
8498a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices = NULL;
850450ba0eaSJosef Bacik 	struct btrfs_root *tree_root = NULL;
851450ba0eaSJosef Bacik 	struct btrfs_fs_info *fs_info = NULL;
85297288f2cSChristoph Hellwig 	fmode_t mode = FMODE_READ;
85373f73415SJosef Bacik 	char *subvol_name = NULL;
85473f73415SJosef Bacik 	u64 subvol_objectid = 0;
855e15d0542SXin Zhong 	u64 subvol_rootid = 0;
8564b82d6e4SYan 	int error = 0;
8574b82d6e4SYan 
85897288f2cSChristoph Hellwig 	if (!(flags & MS_RDONLY))
85997288f2cSChristoph Hellwig 		mode |= FMODE_WRITE;
86097288f2cSChristoph Hellwig 
86197288f2cSChristoph Hellwig 	error = btrfs_parse_early_options(data, mode, fs_type,
86273f73415SJosef Bacik 					  &subvol_name, &subvol_objectid,
863e15d0542SXin Zhong 					  &subvol_rootid, &fs_devices);
864edf24abeSChristoph Hellwig 	if (error)
865061dbc6bSAl Viro 		return ERR_PTR(error);
866edf24abeSChristoph Hellwig 
867*830c4adbSJosef Bacik 	if (subvol_name) {
868*830c4adbSJosef Bacik 		root = mount_subvol(subvol_name, flags, device_name, data);
869*830c4adbSJosef Bacik 		kfree(subvol_name);
870*830c4adbSJosef Bacik 		return root;
871*830c4adbSJosef Bacik 	}
872*830c4adbSJosef Bacik 
873306e16ceSDavid Sterba 	error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
8748a4b83ccSChris Mason 	if (error)
875*830c4adbSJosef Bacik 		return ERR_PTR(error);
8764b82d6e4SYan 
87797288f2cSChristoph Hellwig 	error = btrfs_open_devices(fs_devices, mode, fs_type);
8788a4b83ccSChris Mason 	if (error)
879*830c4adbSJosef Bacik 		return ERR_PTR(error);
8808a4b83ccSChris Mason 
8812b82032cSYan Zheng 	if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
8822b82032cSYan Zheng 		error = -EACCES;
8832b82032cSYan Zheng 		goto error_close_devices;
8842b82032cSYan Zheng 	}
8852b82032cSYan Zheng 
886450ba0eaSJosef Bacik 	/*
887450ba0eaSJosef Bacik 	 * Setup a dummy root and fs_info for test/set super.  This is because
888450ba0eaSJosef Bacik 	 * we don't actually fill this stuff out until open_ctree, but we need
889450ba0eaSJosef Bacik 	 * it for searching for existing supers, so this lets us do that and
890450ba0eaSJosef Bacik 	 * then open_ctree will properly initialize everything later.
891450ba0eaSJosef Bacik 	 */
892450ba0eaSJosef Bacik 	fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
893450ba0eaSJosef Bacik 	tree_root = kzalloc(sizeof(struct btrfs_root), GFP_NOFS);
894450ba0eaSJosef Bacik 	if (!fs_info || !tree_root) {
895450ba0eaSJosef Bacik 		error = -ENOMEM;
896450ba0eaSJosef Bacik 		goto error_close_devices;
897450ba0eaSJosef Bacik 	}
898450ba0eaSJosef Bacik 	fs_info->tree_root = tree_root;
899450ba0eaSJosef Bacik 	fs_info->fs_devices = fs_devices;
900450ba0eaSJosef Bacik 	tree_root->fs_info = fs_info;
901450ba0eaSJosef Bacik 
902dfe25020SChris Mason 	bdev = fs_devices->latest_bdev;
903450ba0eaSJosef Bacik 	s = sget(fs_type, btrfs_test_super, btrfs_set_super, tree_root);
904*830c4adbSJosef Bacik 	if (IS_ERR(s)) {
905*830c4adbSJosef Bacik 		error = PTR_ERR(s);
906*830c4adbSJosef Bacik 		goto error_close_devices;
907*830c4adbSJosef Bacik 	}
9084b82d6e4SYan 
9094b82d6e4SYan 	if (s->s_root) {
9104b82d6e4SYan 		if ((flags ^ s->s_flags) & MS_RDONLY) {
9116f5bbff9SAl Viro 			deactivate_locked_super(s);
912*830c4adbSJosef Bacik 			return ERR_PTR(-EBUSY);
9134b82d6e4SYan 		}
9144b82d6e4SYan 
9152b82032cSYan Zheng 		btrfs_close_devices(fs_devices);
916bdc924bbSIan Kent 		kfree(fs_info);
917bdc924bbSIan Kent 		kfree(tree_root);
9184b82d6e4SYan 	} else {
9194b82d6e4SYan 		char b[BDEVNAME_SIZE];
9204b82d6e4SYan 
9219e1f1de0SAl Viro 		s->s_flags = flags | MS_NOSEC;
9224b82d6e4SYan 		strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
9238a4b83ccSChris Mason 		error = btrfs_fill_super(s, fs_devices, data,
9248a4b83ccSChris Mason 					 flags & MS_SILENT ? 1 : 0);
9254b82d6e4SYan 		if (error) {
9266f5bbff9SAl Viro 			deactivate_locked_super(s);
927*830c4adbSJosef Bacik 			return ERR_PTR(error);
9284b82d6e4SYan 		}
9294b82d6e4SYan 
930788f20ebSChris Mason 		btrfs_sb(s)->fs_info->bdev_holder = fs_type;
9314b82d6e4SYan 		s->s_flags |= MS_ACTIVE;
9324b82d6e4SYan 	}
9334b82d6e4SYan 
934e15d0542SXin Zhong 	root = get_default_root(s, subvol_objectid);
935e15d0542SXin Zhong 	if (IS_ERR(root)) {
936e15d0542SXin Zhong 		deactivate_locked_super(s);
937*830c4adbSJosef Bacik 		return root;
93876fcef19SDavid Woodhouse 	}
9394b82d6e4SYan 
940061dbc6bSAl Viro 	return root;
9414b82d6e4SYan 
942c146afadSYan Zheng error_close_devices:
9438a4b83ccSChris Mason 	btrfs_close_devices(fs_devices);
944450ba0eaSJosef Bacik 	kfree(fs_info);
945450ba0eaSJosef Bacik 	kfree(tree_root);
946061dbc6bSAl Viro 	return ERR_PTR(error);
9474b82d6e4SYan }
9482e635a27SChris Mason 
949c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
950c146afadSYan Zheng {
951c146afadSYan Zheng 	struct btrfs_root *root = btrfs_sb(sb);
952c146afadSYan Zheng 	int ret;
953c146afadSYan Zheng 
954b288052eSChris Mason 	ret = btrfs_parse_options(root, data);
955b288052eSChris Mason 	if (ret)
956b288052eSChris Mason 		return -EINVAL;
957b288052eSChris Mason 
958c146afadSYan Zheng 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
959c146afadSYan Zheng 		return 0;
960c146afadSYan Zheng 
961c146afadSYan Zheng 	if (*flags & MS_RDONLY) {
962c146afadSYan Zheng 		sb->s_flags |= MS_RDONLY;
963c146afadSYan Zheng 
964c146afadSYan Zheng 		ret =  btrfs_commit_super(root);
965c146afadSYan Zheng 		WARN_ON(ret);
966c146afadSYan Zheng 	} else {
9672b82032cSYan Zheng 		if (root->fs_info->fs_devices->rw_devices == 0)
9682b82032cSYan Zheng 			return -EACCES;
9692b82032cSYan Zheng 
970c146afadSYan Zheng 		if (btrfs_super_log_root(&root->fs_info->super_copy) != 0)
971c146afadSYan Zheng 			return -EINVAL;
972c146afadSYan Zheng 
973d68fc57bSYan, Zheng 		ret = btrfs_cleanup_fs_roots(root->fs_info);
974c146afadSYan Zheng 		WARN_ON(ret);
975c146afadSYan Zheng 
976d68fc57bSYan, Zheng 		/* recover relocation */
977d68fc57bSYan, Zheng 		ret = btrfs_recover_relocation(root);
978c146afadSYan Zheng 		WARN_ON(ret);
979c146afadSYan Zheng 
980c146afadSYan Zheng 		sb->s_flags &= ~MS_RDONLY;
981c146afadSYan Zheng 	}
982c146afadSYan Zheng 
983c146afadSYan Zheng 	return 0;
984c146afadSYan Zheng }
985c146afadSYan Zheng 
986bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */
987bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1,
988bcd53741SArne Jansen 				       const void *dev_info2)
989bcd53741SArne Jansen {
990bcd53741SArne Jansen 	if (((struct btrfs_device_info *)dev_info1)->max_avail >
991bcd53741SArne Jansen 	    ((struct btrfs_device_info *)dev_info2)->max_avail)
992bcd53741SArne Jansen 		return -1;
993bcd53741SArne Jansen 	else if (((struct btrfs_device_info *)dev_info1)->max_avail <
994bcd53741SArne Jansen 		 ((struct btrfs_device_info *)dev_info2)->max_avail)
995bcd53741SArne Jansen 		return 1;
996bcd53741SArne Jansen 	else
997bcd53741SArne Jansen 	return 0;
998bcd53741SArne Jansen }
999bcd53741SArne Jansen 
1000bcd53741SArne Jansen /*
1001bcd53741SArne Jansen  * sort the devices by max_avail, in which max free extent size of each device
1002bcd53741SArne Jansen  * is stored.(Descending Sort)
1003bcd53741SArne Jansen  */
1004bcd53741SArne Jansen static inline void btrfs_descending_sort_devices(
1005bcd53741SArne Jansen 					struct btrfs_device_info *devices,
1006bcd53741SArne Jansen 					size_t nr_devices)
1007bcd53741SArne Jansen {
1008bcd53741SArne Jansen 	sort(devices, nr_devices, sizeof(struct btrfs_device_info),
1009bcd53741SArne Jansen 	     btrfs_cmp_device_free_bytes, NULL);
1010bcd53741SArne Jansen }
1011bcd53741SArne Jansen 
10126d07bcecSMiao Xie /*
10136d07bcecSMiao Xie  * The helper to calc the free space on the devices that can be used to store
10146d07bcecSMiao Xie  * file data.
10156d07bcecSMiao Xie  */
10166d07bcecSMiao Xie static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
10176d07bcecSMiao Xie {
10186d07bcecSMiao Xie 	struct btrfs_fs_info *fs_info = root->fs_info;
10196d07bcecSMiao Xie 	struct btrfs_device_info *devices_info;
10206d07bcecSMiao Xie 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
10216d07bcecSMiao Xie 	struct btrfs_device *device;
10226d07bcecSMiao Xie 	u64 skip_space;
10236d07bcecSMiao Xie 	u64 type;
10246d07bcecSMiao Xie 	u64 avail_space;
10256d07bcecSMiao Xie 	u64 used_space;
10266d07bcecSMiao Xie 	u64 min_stripe_size;
10276d07bcecSMiao Xie 	int min_stripes = 1;
10286d07bcecSMiao Xie 	int i = 0, nr_devices;
10296d07bcecSMiao Xie 	int ret;
10306d07bcecSMiao Xie 
10316d07bcecSMiao Xie 	nr_devices = fs_info->fs_devices->rw_devices;
10326d07bcecSMiao Xie 	BUG_ON(!nr_devices);
10336d07bcecSMiao Xie 
10346d07bcecSMiao Xie 	devices_info = kmalloc(sizeof(*devices_info) * nr_devices,
10356d07bcecSMiao Xie 			       GFP_NOFS);
10366d07bcecSMiao Xie 	if (!devices_info)
10376d07bcecSMiao Xie 		return -ENOMEM;
10386d07bcecSMiao Xie 
10396d07bcecSMiao Xie 	/* calc min stripe number for data space alloction */
10406d07bcecSMiao Xie 	type = btrfs_get_alloc_profile(root, 1);
10416d07bcecSMiao Xie 	if (type & BTRFS_BLOCK_GROUP_RAID0)
10426d07bcecSMiao Xie 		min_stripes = 2;
10436d07bcecSMiao Xie 	else if (type & BTRFS_BLOCK_GROUP_RAID1)
10446d07bcecSMiao Xie 		min_stripes = 2;
10456d07bcecSMiao Xie 	else if (type & BTRFS_BLOCK_GROUP_RAID10)
10466d07bcecSMiao Xie 		min_stripes = 4;
10476d07bcecSMiao Xie 
10486d07bcecSMiao Xie 	if (type & BTRFS_BLOCK_GROUP_DUP)
10496d07bcecSMiao Xie 		min_stripe_size = 2 * BTRFS_STRIPE_LEN;
10506d07bcecSMiao Xie 	else
10516d07bcecSMiao Xie 		min_stripe_size = BTRFS_STRIPE_LEN;
10526d07bcecSMiao Xie 
10536d07bcecSMiao Xie 	list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
10546d07bcecSMiao Xie 		if (!device->in_fs_metadata)
10556d07bcecSMiao Xie 			continue;
10566d07bcecSMiao Xie 
10576d07bcecSMiao Xie 		avail_space = device->total_bytes - device->bytes_used;
10586d07bcecSMiao Xie 
10596d07bcecSMiao Xie 		/* align with stripe_len */
10606d07bcecSMiao Xie 		do_div(avail_space, BTRFS_STRIPE_LEN);
10616d07bcecSMiao Xie 		avail_space *= BTRFS_STRIPE_LEN;
10626d07bcecSMiao Xie 
10636d07bcecSMiao Xie 		/*
10646d07bcecSMiao Xie 		 * In order to avoid overwritting the superblock on the drive,
10656d07bcecSMiao Xie 		 * btrfs starts at an offset of at least 1MB when doing chunk
10666d07bcecSMiao Xie 		 * allocation.
10676d07bcecSMiao Xie 		 */
10686d07bcecSMiao Xie 		skip_space = 1024 * 1024;
10696d07bcecSMiao Xie 
10706d07bcecSMiao Xie 		/* user can set the offset in fs_info->alloc_start. */
10716d07bcecSMiao Xie 		if (fs_info->alloc_start + BTRFS_STRIPE_LEN <=
10726d07bcecSMiao Xie 		    device->total_bytes)
10736d07bcecSMiao Xie 			skip_space = max(fs_info->alloc_start, skip_space);
10746d07bcecSMiao Xie 
10756d07bcecSMiao Xie 		/*
10766d07bcecSMiao Xie 		 * btrfs can not use the free space in [0, skip_space - 1],
10776d07bcecSMiao Xie 		 * we must subtract it from the total. In order to implement
10786d07bcecSMiao Xie 		 * it, we account the used space in this range first.
10796d07bcecSMiao Xie 		 */
10806d07bcecSMiao Xie 		ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1,
10816d07bcecSMiao Xie 						     &used_space);
10826d07bcecSMiao Xie 		if (ret) {
10836d07bcecSMiao Xie 			kfree(devices_info);
10846d07bcecSMiao Xie 			return ret;
10856d07bcecSMiao Xie 		}
10866d07bcecSMiao Xie 
10876d07bcecSMiao Xie 		/* calc the free space in [0, skip_space - 1] */
10886d07bcecSMiao Xie 		skip_space -= used_space;
10896d07bcecSMiao Xie 
10906d07bcecSMiao Xie 		/*
10916d07bcecSMiao Xie 		 * we can use the free space in [0, skip_space - 1], subtract
10926d07bcecSMiao Xie 		 * it from the total.
10936d07bcecSMiao Xie 		 */
10946d07bcecSMiao Xie 		if (avail_space && avail_space >= skip_space)
10956d07bcecSMiao Xie 			avail_space -= skip_space;
10966d07bcecSMiao Xie 		else
10976d07bcecSMiao Xie 			avail_space = 0;
10986d07bcecSMiao Xie 
10996d07bcecSMiao Xie 		if (avail_space < min_stripe_size)
11006d07bcecSMiao Xie 			continue;
11016d07bcecSMiao Xie 
11026d07bcecSMiao Xie 		devices_info[i].dev = device;
11036d07bcecSMiao Xie 		devices_info[i].max_avail = avail_space;
11046d07bcecSMiao Xie 
11056d07bcecSMiao Xie 		i++;
11066d07bcecSMiao Xie 	}
11076d07bcecSMiao Xie 
11086d07bcecSMiao Xie 	nr_devices = i;
11096d07bcecSMiao Xie 
11106d07bcecSMiao Xie 	btrfs_descending_sort_devices(devices_info, nr_devices);
11116d07bcecSMiao Xie 
11126d07bcecSMiao Xie 	i = nr_devices - 1;
11136d07bcecSMiao Xie 	avail_space = 0;
11146d07bcecSMiao Xie 	while (nr_devices >= min_stripes) {
11156d07bcecSMiao Xie 		if (devices_info[i].max_avail >= min_stripe_size) {
11166d07bcecSMiao Xie 			int j;
11176d07bcecSMiao Xie 			u64 alloc_size;
11186d07bcecSMiao Xie 
11196d07bcecSMiao Xie 			avail_space += devices_info[i].max_avail * min_stripes;
11206d07bcecSMiao Xie 			alloc_size = devices_info[i].max_avail;
11216d07bcecSMiao Xie 			for (j = i + 1 - min_stripes; j <= i; j++)
11226d07bcecSMiao Xie 				devices_info[j].max_avail -= alloc_size;
11236d07bcecSMiao Xie 		}
11246d07bcecSMiao Xie 		i--;
11256d07bcecSMiao Xie 		nr_devices--;
11266d07bcecSMiao Xie 	}
11276d07bcecSMiao Xie 
11286d07bcecSMiao Xie 	kfree(devices_info);
11296d07bcecSMiao Xie 	*free_bytes = avail_space;
11306d07bcecSMiao Xie 	return 0;
11316d07bcecSMiao Xie }
11326d07bcecSMiao Xie 
11338fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
11348fd17795SChris Mason {
11358fd17795SChris Mason 	struct btrfs_root *root = btrfs_sb(dentry->d_sb);
11364b52dff6SChris Mason 	struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1137bd4d1088SJosef Bacik 	struct list_head *head = &root->fs_info->space_info;
1138bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
1139bd4d1088SJosef Bacik 	u64 total_used = 0;
11406d07bcecSMiao Xie 	u64 total_free_data = 0;
1141db94535dSChris Mason 	int bits = dentry->d_sb->s_blocksize_bits;
11429d03632eSDavid Woodhouse 	__be32 *fsid = (__be32 *)root->fs_info->fsid;
11436d07bcecSMiao Xie 	int ret;
11448fd17795SChris Mason 
11456d07bcecSMiao Xie 	/* holding chunk_muext to avoid allocating new chunks */
11466d07bcecSMiao Xie 	mutex_lock(&root->fs_info->chunk_mutex);
1147bd4d1088SJosef Bacik 	rcu_read_lock();
114889a55897SJosef Bacik 	list_for_each_entry_rcu(found, head, list) {
11496d07bcecSMiao Xie 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
11506d07bcecSMiao Xie 			total_free_data += found->disk_total - found->disk_used;
11516d07bcecSMiao Xie 			total_free_data -=
11526d07bcecSMiao Xie 				btrfs_account_ro_block_groups_free_space(found);
11536d07bcecSMiao Xie 		}
11546d07bcecSMiao Xie 
1155b742bb82SYan, Zheng 		total_used += found->disk_used;
115689a55897SJosef Bacik 	}
1157bd4d1088SJosef Bacik 	rcu_read_unlock();
1158bd4d1088SJosef Bacik 
11598fd17795SChris Mason 	buf->f_namelen = BTRFS_NAME_LEN;
1160db94535dSChris Mason 	buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
1161bd4d1088SJosef Bacik 	buf->f_bfree = buf->f_blocks - (total_used >> bits);
11628fd17795SChris Mason 	buf->f_bsize = dentry->d_sb->s_blocksize;
11638fd17795SChris Mason 	buf->f_type = BTRFS_SUPER_MAGIC;
11646d07bcecSMiao Xie 	buf->f_bavail = total_free_data;
11656d07bcecSMiao Xie 	ret = btrfs_calc_avail_data_space(root, &total_free_data);
11666d07bcecSMiao Xie 	if (ret) {
11676d07bcecSMiao Xie 		mutex_unlock(&root->fs_info->chunk_mutex);
11686d07bcecSMiao Xie 		return ret;
11696d07bcecSMiao Xie 	}
11706d07bcecSMiao Xie 	buf->f_bavail += total_free_data;
11716d07bcecSMiao Xie 	buf->f_bavail = buf->f_bavail >> bits;
11726d07bcecSMiao Xie 	mutex_unlock(&root->fs_info->chunk_mutex);
1173d397712bSChris Mason 
11749d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
11759d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
11769d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
11779d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
11789d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
117932d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
118032d48fa1SDavid Woodhouse 	buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
118132d48fa1SDavid Woodhouse 	buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
118232d48fa1SDavid Woodhouse 
11838fd17795SChris Mason 	return 0;
11848fd17795SChris Mason }
1185b5133862SChris Mason 
11862e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
11872e635a27SChris Mason 	.owner		= THIS_MODULE,
11882e635a27SChris Mason 	.name		= "btrfs",
1189061dbc6bSAl Viro 	.mount		= btrfs_mount,
1190a061fc8dSChris Mason 	.kill_sb	= kill_anon_super,
11912e635a27SChris Mason 	.fs_flags	= FS_REQUIRES_DEV,
11922e635a27SChris Mason };
1193a9218f6bSChris Mason 
1194d352ac68SChris Mason /*
1195d352ac68SChris Mason  * used by btrfsctl to scan devices when no FS is mounted
1196d352ac68SChris Mason  */
11978a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
11988a4b83ccSChris Mason 				unsigned long arg)
11998a4b83ccSChris Mason {
12008a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
12018a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices;
1202c071fcfdSChris Mason 	int ret = -ENOTTY;
12038a4b83ccSChris Mason 
1204e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1205e441d54dSChris Mason 		return -EPERM;
1206e441d54dSChris Mason 
1207dae7b665SLi Zefan 	vol = memdup_user((void __user *)arg, sizeof(*vol));
1208dae7b665SLi Zefan 	if (IS_ERR(vol))
1209dae7b665SLi Zefan 		return PTR_ERR(vol);
1210c071fcfdSChris Mason 
12118a4b83ccSChris Mason 	switch (cmd) {
12128a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
121397288f2cSChristoph Hellwig 		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
12148a4b83ccSChris Mason 					    &btrfs_fs_type, &fs_devices);
12158a4b83ccSChris Mason 		break;
12168a4b83ccSChris Mason 	}
1217dae7b665SLi Zefan 
12188a4b83ccSChris Mason 	kfree(vol);
1219f819d837SLinda Knippers 	return ret;
12208a4b83ccSChris Mason }
12218a4b83ccSChris Mason 
12220176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
1223ed0dab6bSYan {
1224ed0dab6bSYan 	struct btrfs_root *root = btrfs_sb(sb);
1225a74a4b97SChris Mason 	mutex_lock(&root->fs_info->transaction_kthread_mutex);
1226a74a4b97SChris Mason 	mutex_lock(&root->fs_info->cleaner_mutex);
12270176260fSLinus Torvalds 	return 0;
1228ed0dab6bSYan }
1229ed0dab6bSYan 
12300176260fSLinus Torvalds static int btrfs_unfreeze(struct super_block *sb)
1231ed0dab6bSYan {
1232ed0dab6bSYan 	struct btrfs_root *root = btrfs_sb(sb);
1233a74a4b97SChris Mason 	mutex_unlock(&root->fs_info->cleaner_mutex);
1234a74a4b97SChris Mason 	mutex_unlock(&root->fs_info->transaction_kthread_mutex);
12350176260fSLinus Torvalds 	return 0;
1236ed0dab6bSYan }
12372e635a27SChris Mason 
1238b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
123976dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
1240bd555975SAl Viro 	.evict_inode	= btrfs_evict_inode,
1241e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
1242d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
1243a9572a15SEric Paris 	.show_options	= btrfs_show_options,
12444730a4bcSChris Mason 	.write_inode	= btrfs_write_inode,
1245b5133862SChris Mason 	.dirty_inode	= btrfs_dirty_inode,
12462c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
12472c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
12488fd17795SChris Mason 	.statfs		= btrfs_statfs,
1249c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
12500176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
12510176260fSLinus Torvalds 	.unfreeze_fs	= btrfs_unfreeze,
1252e20d96d6SChris Mason };
1253a9218f6bSChris Mason 
1254a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
1255a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
1256a9218f6bSChris Mason 	.compat_ioctl = btrfs_control_ioctl,
1257a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
12586038f373SArnd Bergmann 	.llseek = noop_llseek,
1259a9218f6bSChris Mason };
1260a9218f6bSChris Mason 
1261a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
1262578454ffSKay Sievers 	.minor		= BTRFS_MINOR,
1263a9218f6bSChris Mason 	.name		= "btrfs-control",
1264a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
1265a9218f6bSChris Mason };
1266a9218f6bSChris Mason 
1267578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
1268578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control");
1269578454ffSKay Sievers 
1270a9218f6bSChris Mason static int btrfs_interface_init(void)
1271a9218f6bSChris Mason {
1272a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
1273a9218f6bSChris Mason }
1274a9218f6bSChris Mason 
1275b2950863SChristoph Hellwig static void btrfs_interface_exit(void)
1276a9218f6bSChris Mason {
1277a9218f6bSChris Mason 	if (misc_deregister(&btrfs_misc) < 0)
1278d397712bSChris Mason 		printk(KERN_INFO "misc_deregister failed for control device");
1279a9218f6bSChris Mason }
1280a9218f6bSChris Mason 
12812e635a27SChris Mason static int __init init_btrfs_fs(void)
12822e635a27SChris Mason {
12832c90e5d6SChris Mason 	int err;
128458176a96SJosef Bacik 
128558176a96SJosef Bacik 	err = btrfs_init_sysfs();
128658176a96SJosef Bacik 	if (err)
128758176a96SJosef Bacik 		return err;
128858176a96SJosef Bacik 
1289261507a0SLi Zefan 	err = btrfs_init_compress();
12902c90e5d6SChris Mason 	if (err)
1291a74a4b97SChris Mason 		goto free_sysfs;
1292d1310b2eSChris Mason 
1293261507a0SLi Zefan 	err = btrfs_init_cachep();
1294261507a0SLi Zefan 	if (err)
1295261507a0SLi Zefan 		goto free_compress;
1296261507a0SLi Zefan 
1297d1310b2eSChris Mason 	err = extent_io_init();
12982f4cbe64SWyatt Banks 	if (err)
12992f4cbe64SWyatt Banks 		goto free_cachep;
13002f4cbe64SWyatt Banks 
1301d1310b2eSChris Mason 	err = extent_map_init();
1302d1310b2eSChris Mason 	if (err)
1303d1310b2eSChris Mason 		goto free_extent_io;
1304d1310b2eSChris Mason 
130516cdcec7SMiao Xie 	err = btrfs_delayed_inode_init();
13062f4cbe64SWyatt Banks 	if (err)
13072f4cbe64SWyatt Banks 		goto free_extent_map;
1308c8b97818SChris Mason 
130916cdcec7SMiao Xie 	err = btrfs_interface_init();
131016cdcec7SMiao Xie 	if (err)
131116cdcec7SMiao Xie 		goto free_delayed_inode;
131216cdcec7SMiao Xie 
1313a9218f6bSChris Mason 	err = register_filesystem(&btrfs_fs_type);
1314a9218f6bSChris Mason 	if (err)
1315a9218f6bSChris Mason 		goto unregister_ioctl;
1316b3c3da71SChris Mason 
1317b3c3da71SChris Mason 	printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
13182f4cbe64SWyatt Banks 	return 0;
13192f4cbe64SWyatt Banks 
1320a9218f6bSChris Mason unregister_ioctl:
1321a9218f6bSChris Mason 	btrfs_interface_exit();
132216cdcec7SMiao Xie free_delayed_inode:
132316cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
13242f4cbe64SWyatt Banks free_extent_map:
13252f4cbe64SWyatt Banks 	extent_map_exit();
1326d1310b2eSChris Mason free_extent_io:
1327d1310b2eSChris Mason 	extent_io_exit();
13282f4cbe64SWyatt Banks free_cachep:
13292f4cbe64SWyatt Banks 	btrfs_destroy_cachep();
1330261507a0SLi Zefan free_compress:
1331261507a0SLi Zefan 	btrfs_exit_compress();
1332a74a4b97SChris Mason free_sysfs:
13332f4cbe64SWyatt Banks 	btrfs_exit_sysfs();
13342c90e5d6SChris Mason 	return err;
13352e635a27SChris Mason }
13362e635a27SChris Mason 
13372e635a27SChris Mason static void __exit exit_btrfs_fs(void)
13382e635a27SChris Mason {
133939279cc3SChris Mason 	btrfs_destroy_cachep();
134016cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
1341a52d9a80SChris Mason 	extent_map_exit();
1342d1310b2eSChris Mason 	extent_io_exit();
1343a9218f6bSChris Mason 	btrfs_interface_exit();
13442e635a27SChris Mason 	unregister_filesystem(&btrfs_fs_type);
134558176a96SJosef Bacik 	btrfs_exit_sysfs();
13468a4b83ccSChris Mason 	btrfs_cleanup_fs_uuids();
1347261507a0SLi Zefan 	btrfs_exit_compress();
13482e635a27SChris Mason }
13492e635a27SChris Mason 
13502e635a27SChris Mason module_init(init_btrfs_fs)
13512e635a27SChris Mason module_exit(exit_btrfs_fs)
13522e635a27SChris Mason 
13532e635a27SChris Mason MODULE_LICENSE("GPL");
1354