xref: /linux/fs/btrfs/super.c (revision 4cbd1149fbcc351bdf08ab749867d157905d0d35)
16cbd5570SChris Mason /*
26cbd5570SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
36cbd5570SChris Mason  *
46cbd5570SChris Mason  * This program is free software; you can redistribute it and/or
56cbd5570SChris Mason  * modify it under the terms of the GNU General Public
66cbd5570SChris Mason  * License v2 as published by the Free Software Foundation.
76cbd5570SChris Mason  *
86cbd5570SChris Mason  * This program is distributed in the hope that it will be useful,
96cbd5570SChris Mason  * but WITHOUT ANY WARRANTY; without even the implied warranty of
106cbd5570SChris Mason  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116cbd5570SChris Mason  * General Public License for more details.
126cbd5570SChris Mason  *
136cbd5570SChris Mason  * You should have received a copy of the GNU General Public
146cbd5570SChris Mason  * License along with this program; if not, write to the
156cbd5570SChris Mason  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
166cbd5570SChris Mason  * Boston, MA 021110-1307, USA.
176cbd5570SChris Mason  */
186cbd5570SChris Mason 
194b82d6e4SYan #include <linux/blkdev.h>
202e635a27SChris Mason #include <linux/module.h>
21e20d96d6SChris Mason #include <linux/buffer_head.h>
222e635a27SChris Mason #include <linux/fs.h>
232e635a27SChris Mason #include <linux/pagemap.h>
242e635a27SChris Mason #include <linux/highmem.h>
252e635a27SChris Mason #include <linux/time.h>
262e635a27SChris Mason #include <linux/init.h>
27a9572a15SEric Paris #include <linux/seq_file.h>
282e635a27SChris Mason #include <linux/string.h>
292e635a27SChris Mason #include <linux/backing-dev.h>
304b82d6e4SYan #include <linux/mount.h>
31dee26a9fSChris Mason #include <linux/mpage.h>
3275dfe396SChris Mason #include <linux/swap.h>
3375dfe396SChris Mason #include <linux/writeback.h>
348fd17795SChris Mason #include <linux/statfs.h>
3508607c1bSChris Mason #include <linux/compat.h>
3695e05289SChris Mason #include <linux/parser.h>
37c59f8951SChris Mason #include <linux/ctype.h>
386da6abaeSChris Mason #include <linux/namei.h>
39a9218f6bSChris Mason #include <linux/miscdevice.h>
401bcbf313SQinghuang Feng #include <linux/magic.h>
415a0e3ad6STejun Heo #include <linux/slab.h>
424b4e25f2SChris Mason #include "compat.h"
432e635a27SChris Mason #include "ctree.h"
44e20d96d6SChris Mason #include "disk-io.h"
45d5719762SChris Mason #include "transaction.h"
462c90e5d6SChris Mason #include "btrfs_inode.h"
47c5739bbaSChris Mason #include "ioctl.h"
483a686375SChris Mason #include "print-tree.h"
495103e947SJosef Bacik #include "xattr.h"
508a4b83ccSChris Mason #include "volumes.h"
51b3c3da71SChris Mason #include "version.h"
52be6e8dc0SBalaji Rao #include "export.h"
53c8b97818SChris Mason #include "compression.h"
542e635a27SChris Mason 
55b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops;
56e20d96d6SChris Mason 
57e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb)
58e20d96d6SChris Mason {
59e20d96d6SChris Mason 	struct btrfs_root *root = btrfs_sb(sb);
60e20d96d6SChris Mason 	int ret;
61e20d96d6SChris Mason 
62e20d96d6SChris Mason 	ret = close_ctree(root);
63e20d96d6SChris Mason 	sb->s_fs_info = NULL;
64e20d96d6SChris Mason }
652e635a27SChris Mason 
6695e05289SChris Mason enum {
6773f73415SJosef Bacik 	Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
68287a0ab9SJosef Bacik 	Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
69287a0ab9SJosef Bacik 	Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
70287a0ab9SJosef Bacik 	Opt_compress_force, Opt_notreelog, Opt_ratio, Opt_flushoncommit,
71e244a0aeSChristoph Hellwig 	Opt_discard, Opt_err,
7295e05289SChris Mason };
7395e05289SChris Mason 
7495e05289SChris Mason static match_table_t tokens = {
75dfe25020SChris Mason 	{Opt_degraded, "degraded"},
7695e05289SChris Mason 	{Opt_subvol, "subvol=%s"},
7773f73415SJosef Bacik 	{Opt_subvolid, "subvolid=%d"},
7843e570b0SChristoph Hellwig 	{Opt_device, "device=%s"},
79b6cda9bcSChris Mason 	{Opt_nodatasum, "nodatasum"},
80be20aa9dSChris Mason 	{Opt_nodatacow, "nodatacow"},
8121ad10cfSChris Mason 	{Opt_nobarrier, "nobarrier"},
826f568d35SChris Mason 	{Opt_max_inline, "max_inline=%s"},
838f662a76SChris Mason 	{Opt_alloc_start, "alloc_start=%s"},
844543df7eSChris Mason 	{Opt_thread_pool, "thread_pool=%d"},
85c8b97818SChris Mason 	{Opt_compress, "compress"},
86a555f810SChris Mason 	{Opt_compress_force, "compress-force"},
87e18e4809SChris Mason 	{Opt_ssd, "ssd"},
88451d7585SChris Mason 	{Opt_ssd_spread, "ssd_spread"},
893b30c22fSChris Mason 	{Opt_nossd, "nossd"},
9033268eafSJosef Bacik 	{Opt_noacl, "noacl"},
913a5e1404SSage Weil 	{Opt_notreelog, "notreelog"},
92dccae999SSage Weil 	{Opt_flushoncommit, "flushoncommit"},
9397e728d4SJosef Bacik 	{Opt_ratio, "metadata_ratio=%d"},
94e244a0aeSChristoph Hellwig 	{Opt_discard, "discard"},
9533268eafSJosef Bacik 	{Opt_err, NULL},
9695e05289SChris Mason };
9795e05289SChris Mason 
98edf24abeSChristoph Hellwig /*
99edf24abeSChristoph Hellwig  * Regular mount options parser.  Everything that is needed only when
100edf24abeSChristoph Hellwig  * reading in a new superblock is parsed here.
101edf24abeSChristoph Hellwig  */
102edf24abeSChristoph Hellwig int btrfs_parse_options(struct btrfs_root *root, char *options)
10395e05289SChris Mason {
104edf24abeSChristoph Hellwig 	struct btrfs_fs_info *info = root->fs_info;
10595e05289SChris Mason 	substring_t args[MAX_OPT_ARGS];
106da495eccSJosef Bacik 	char *p, *num, *orig;
1074543df7eSChris Mason 	int intarg;
108a7a3f7caSSage Weil 	int ret = 0;
109b6cda9bcSChris Mason 
11095e05289SChris Mason 	if (!options)
111edf24abeSChristoph Hellwig 		return 0;
11295e05289SChris Mason 
113be20aa9dSChris Mason 	/*
114be20aa9dSChris Mason 	 * strsep changes the string, duplicate it because parse_options
115be20aa9dSChris Mason 	 * gets called twice
116be20aa9dSChris Mason 	 */
117be20aa9dSChris Mason 	options = kstrdup(options, GFP_NOFS);
118be20aa9dSChris Mason 	if (!options)
119be20aa9dSChris Mason 		return -ENOMEM;
120be20aa9dSChris Mason 
121da495eccSJosef Bacik 	orig = options;
122be20aa9dSChris Mason 
12395e05289SChris Mason 	while ((p = strsep(&options, ",")) != NULL) {
12495e05289SChris Mason 		int token;
12595e05289SChris Mason 		if (!*p)
12695e05289SChris Mason 			continue;
12795e05289SChris Mason 
12895e05289SChris Mason 		token = match_token(p, tokens, args);
12995e05289SChris Mason 		switch (token) {
130dfe25020SChris Mason 		case Opt_degraded:
131edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: allowing degraded mounts\n");
132dfe25020SChris Mason 			btrfs_set_opt(info->mount_opt, DEGRADED);
133dfe25020SChris Mason 			break;
13495e05289SChris Mason 		case Opt_subvol:
13573f73415SJosef Bacik 		case Opt_subvolid:
13643e570b0SChristoph Hellwig 		case Opt_device:
137edf24abeSChristoph Hellwig 			/*
13843e570b0SChristoph Hellwig 			 * These are parsed by btrfs_parse_early_options
139edf24abeSChristoph Hellwig 			 * and can be happily ignored here.
140edf24abeSChristoph Hellwig 			 */
14195e05289SChris Mason 			break;
142b6cda9bcSChris Mason 		case Opt_nodatasum:
143067c28adSChris Mason 			printk(KERN_INFO "btrfs: setting nodatasum\n");
144b6cda9bcSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
145be20aa9dSChris Mason 			break;
146be20aa9dSChris Mason 		case Opt_nodatacow:
147edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: setting nodatacow\n");
148be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATACOW);
149be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
150b6cda9bcSChris Mason 			break;
151c8b97818SChris Mason 		case Opt_compress:
152c8b97818SChris Mason 			printk(KERN_INFO "btrfs: use compression\n");
153c8b97818SChris Mason 			btrfs_set_opt(info->mount_opt, COMPRESS);
154c8b97818SChris Mason 			break;
155a555f810SChris Mason 		case Opt_compress_force:
156a555f810SChris Mason 			printk(KERN_INFO "btrfs: forcing compression\n");
157a555f810SChris Mason 			btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
158a555f810SChris Mason 			btrfs_set_opt(info->mount_opt, COMPRESS);
159a555f810SChris Mason 			break;
160e18e4809SChris Mason 		case Opt_ssd:
161edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
162e18e4809SChris Mason 			btrfs_set_opt(info->mount_opt, SSD);
163e18e4809SChris Mason 			break;
164451d7585SChris Mason 		case Opt_ssd_spread:
165451d7585SChris Mason 			printk(KERN_INFO "btrfs: use spread ssd "
166451d7585SChris Mason 			       "allocation scheme\n");
167451d7585SChris Mason 			btrfs_set_opt(info->mount_opt, SSD);
168451d7585SChris Mason 			btrfs_set_opt(info->mount_opt, SSD_SPREAD);
169451d7585SChris Mason 			break;
1703b30c22fSChris Mason 		case Opt_nossd:
171451d7585SChris Mason 			printk(KERN_INFO "btrfs: not using ssd allocation "
172451d7585SChris Mason 			       "scheme\n");
173c289811cSChris Mason 			btrfs_set_opt(info->mount_opt, NOSSD);
1743b30c22fSChris Mason 			btrfs_clear_opt(info->mount_opt, SSD);
175451d7585SChris Mason 			btrfs_clear_opt(info->mount_opt, SSD_SPREAD);
1763b30c22fSChris Mason 			break;
17721ad10cfSChris Mason 		case Opt_nobarrier:
178edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: turning off barriers\n");
17921ad10cfSChris Mason 			btrfs_set_opt(info->mount_opt, NOBARRIER);
18021ad10cfSChris Mason 			break;
1814543df7eSChris Mason 		case Opt_thread_pool:
1824543df7eSChris Mason 			intarg = 0;
1834543df7eSChris Mason 			match_int(&args[0], &intarg);
1844543df7eSChris Mason 			if (intarg) {
1854543df7eSChris Mason 				info->thread_pool_size = intarg;
1864543df7eSChris Mason 				printk(KERN_INFO "btrfs: thread pool %d\n",
1874543df7eSChris Mason 				       info->thread_pool_size);
1884543df7eSChris Mason 			}
1894543df7eSChris Mason 			break;
1906f568d35SChris Mason 		case Opt_max_inline:
191edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
1926f568d35SChris Mason 			if (num) {
19391748467SAkinobu Mita 				info->max_inline = memparse(num, NULL);
1946f568d35SChris Mason 				kfree(num);
1956f568d35SChris Mason 
19615ada040SChris Mason 				if (info->max_inline) {
1976f568d35SChris Mason 					info->max_inline = max_t(u64,
19815ada040SChris Mason 						info->max_inline,
19915ada040SChris Mason 						root->sectorsize);
20015ada040SChris Mason 				}
201edf24abeSChristoph Hellwig 				printk(KERN_INFO "btrfs: max_inline at %llu\n",
20221380931SJoel Becker 					(unsigned long long)info->max_inline);
2036f568d35SChris Mason 			}
2046f568d35SChris Mason 			break;
2058f662a76SChris Mason 		case Opt_alloc_start:
206edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
2078f662a76SChris Mason 			if (num) {
20891748467SAkinobu Mita 				info->alloc_start = memparse(num, NULL);
2098f662a76SChris Mason 				kfree(num);
210edf24abeSChristoph Hellwig 				printk(KERN_INFO
211edf24abeSChristoph Hellwig 					"btrfs: allocations start at %llu\n",
21221380931SJoel Becker 					(unsigned long long)info->alloc_start);
2138f662a76SChris Mason 			}
2148f662a76SChris Mason 			break;
21533268eafSJosef Bacik 		case Opt_noacl:
21633268eafSJosef Bacik 			root->fs_info->sb->s_flags &= ~MS_POSIXACL;
21733268eafSJosef Bacik 			break;
2183a5e1404SSage Weil 		case Opt_notreelog:
2193a5e1404SSage Weil 			printk(KERN_INFO "btrfs: disabling tree log\n");
2203a5e1404SSage Weil 			btrfs_set_opt(info->mount_opt, NOTREELOG);
2213a5e1404SSage Weil 			break;
222dccae999SSage Weil 		case Opt_flushoncommit:
223dccae999SSage Weil 			printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
224dccae999SSage Weil 			btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
225dccae999SSage Weil 			break;
22697e728d4SJosef Bacik 		case Opt_ratio:
22797e728d4SJosef Bacik 			intarg = 0;
22897e728d4SJosef Bacik 			match_int(&args[0], &intarg);
22997e728d4SJosef Bacik 			if (intarg) {
23097e728d4SJosef Bacik 				info->metadata_ratio = intarg;
23197e728d4SJosef Bacik 				printk(KERN_INFO "btrfs: metadata ratio %d\n",
23297e728d4SJosef Bacik 				       info->metadata_ratio);
23397e728d4SJosef Bacik 			}
23497e728d4SJosef Bacik 			break;
235e244a0aeSChristoph Hellwig 		case Opt_discard:
236e244a0aeSChristoph Hellwig 			btrfs_set_opt(info->mount_opt, DISCARD);
237e244a0aeSChristoph Hellwig 			break;
238a7a3f7caSSage Weil 		case Opt_err:
239a7a3f7caSSage Weil 			printk(KERN_INFO "btrfs: unrecognized mount option "
240a7a3f7caSSage Weil 			       "'%s'\n", p);
241a7a3f7caSSage Weil 			ret = -EINVAL;
242a7a3f7caSSage Weil 			goto out;
24395e05289SChris Mason 		default:
244be20aa9dSChris Mason 			break;
24595e05289SChris Mason 		}
24695e05289SChris Mason 	}
247a7a3f7caSSage Weil out:
248da495eccSJosef Bacik 	kfree(orig);
249a7a3f7caSSage Weil 	return ret;
250edf24abeSChristoph Hellwig }
251edf24abeSChristoph Hellwig 
252edf24abeSChristoph Hellwig /*
253edf24abeSChristoph Hellwig  * Parse mount options that are required early in the mount process.
254edf24abeSChristoph Hellwig  *
255edf24abeSChristoph Hellwig  * All other options will be parsed on much later in the mount process and
256edf24abeSChristoph Hellwig  * only when we need to allocate a new super block.
257edf24abeSChristoph Hellwig  */
25897288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags,
25973f73415SJosef Bacik 		void *holder, char **subvol_name, u64 *subvol_objectid,
26043e570b0SChristoph Hellwig 		struct btrfs_fs_devices **fs_devices)
261edf24abeSChristoph Hellwig {
262edf24abeSChristoph Hellwig 	substring_t args[MAX_OPT_ARGS];
263edf24abeSChristoph Hellwig 	char *opts, *p;
264edf24abeSChristoph Hellwig 	int error = 0;
26573f73415SJosef Bacik 	int intarg;
266edf24abeSChristoph Hellwig 
267edf24abeSChristoph Hellwig 	if (!options)
268edf24abeSChristoph Hellwig 		goto out;
269edf24abeSChristoph Hellwig 
270edf24abeSChristoph Hellwig 	/*
271edf24abeSChristoph Hellwig 	 * strsep changes the string, duplicate it because parse_options
272edf24abeSChristoph Hellwig 	 * gets called twice
273edf24abeSChristoph Hellwig 	 */
274edf24abeSChristoph Hellwig 	opts = kstrdup(options, GFP_KERNEL);
275edf24abeSChristoph Hellwig 	if (!opts)
276edf24abeSChristoph Hellwig 		return -ENOMEM;
277edf24abeSChristoph Hellwig 
278edf24abeSChristoph Hellwig 	while ((p = strsep(&opts, ",")) != NULL) {
279edf24abeSChristoph Hellwig 		int token;
280edf24abeSChristoph Hellwig 		if (!*p)
281edf24abeSChristoph Hellwig 			continue;
282edf24abeSChristoph Hellwig 
283edf24abeSChristoph Hellwig 		token = match_token(p, tokens, args);
284edf24abeSChristoph Hellwig 		switch (token) {
285edf24abeSChristoph Hellwig 		case Opt_subvol:
286edf24abeSChristoph Hellwig 			*subvol_name = match_strdup(&args[0]);
287edf24abeSChristoph Hellwig 			break;
28873f73415SJosef Bacik 		case Opt_subvolid:
28973f73415SJosef Bacik 			intarg = 0;
2904849f01dSJosef Bacik 			error = match_int(&args[0], &intarg);
2914849f01dSJosef Bacik 			if (!error) {
2924849f01dSJosef Bacik 				/* we want the original fs_tree */
2934849f01dSJosef Bacik 				if (!intarg)
2944849f01dSJosef Bacik 					*subvol_objectid =
2954849f01dSJosef Bacik 						BTRFS_FS_TREE_OBJECTID;
2964849f01dSJosef Bacik 				else
29773f73415SJosef Bacik 					*subvol_objectid = intarg;
2984849f01dSJosef Bacik 			}
29973f73415SJosef Bacik 			break;
30043e570b0SChristoph Hellwig 		case Opt_device:
30143e570b0SChristoph Hellwig 			error = btrfs_scan_one_device(match_strdup(&args[0]),
30243e570b0SChristoph Hellwig 					flags, holder, fs_devices);
30343e570b0SChristoph Hellwig 			if (error)
30443e570b0SChristoph Hellwig 				goto out_free_opts;
30543e570b0SChristoph Hellwig 			break;
306edf24abeSChristoph Hellwig 		default:
307edf24abeSChristoph Hellwig 			break;
308edf24abeSChristoph Hellwig 		}
309edf24abeSChristoph Hellwig 	}
310edf24abeSChristoph Hellwig 
31143e570b0SChristoph Hellwig  out_free_opts:
312edf24abeSChristoph Hellwig 	kfree(opts);
313edf24abeSChristoph Hellwig  out:
314edf24abeSChristoph Hellwig 	/*
315edf24abeSChristoph Hellwig 	 * If no subvolume name is specified we use the default one.  Allocate
3163de4586cSChris Mason 	 * a copy of the string "." here so that code later in the
317edf24abeSChristoph Hellwig 	 * mount path doesn't care if it's the default volume or another one.
318edf24abeSChristoph Hellwig 	 */
319edf24abeSChristoph Hellwig 	if (!*subvol_name) {
3203de4586cSChris Mason 		*subvol_name = kstrdup(".", GFP_KERNEL);
321edf24abeSChristoph Hellwig 		if (!*subvol_name)
322edf24abeSChristoph Hellwig 			return -ENOMEM;
323edf24abeSChristoph Hellwig 	}
324edf24abeSChristoph Hellwig 	return error;
32595e05289SChris Mason }
32695e05289SChris Mason 
32773f73415SJosef Bacik static struct dentry *get_default_root(struct super_block *sb,
32873f73415SJosef Bacik 				       u64 subvol_objectid)
32973f73415SJosef Bacik {
33073f73415SJosef Bacik 	struct btrfs_root *root = sb->s_fs_info;
33173f73415SJosef Bacik 	struct btrfs_root *new_root;
33273f73415SJosef Bacik 	struct btrfs_dir_item *di;
33373f73415SJosef Bacik 	struct btrfs_path *path;
33473f73415SJosef Bacik 	struct btrfs_key location;
33573f73415SJosef Bacik 	struct inode *inode;
33673f73415SJosef Bacik 	struct dentry *dentry;
33773f73415SJosef Bacik 	u64 dir_id;
33873f73415SJosef Bacik 	int new = 0;
33973f73415SJosef Bacik 
34073f73415SJosef Bacik 	/*
34173f73415SJosef Bacik 	 * We have a specific subvol we want to mount, just setup location and
34273f73415SJosef Bacik 	 * go look up the root.
34373f73415SJosef Bacik 	 */
34473f73415SJosef Bacik 	if (subvol_objectid) {
34573f73415SJosef Bacik 		location.objectid = subvol_objectid;
34673f73415SJosef Bacik 		location.type = BTRFS_ROOT_ITEM_KEY;
34773f73415SJosef Bacik 		location.offset = (u64)-1;
34873f73415SJosef Bacik 		goto find_root;
34973f73415SJosef Bacik 	}
35073f73415SJosef Bacik 
35173f73415SJosef Bacik 	path = btrfs_alloc_path();
35273f73415SJosef Bacik 	if (!path)
35373f73415SJosef Bacik 		return ERR_PTR(-ENOMEM);
35473f73415SJosef Bacik 	path->leave_spinning = 1;
35573f73415SJosef Bacik 
35673f73415SJosef Bacik 	/*
35773f73415SJosef Bacik 	 * Find the "default" dir item which points to the root item that we
35873f73415SJosef Bacik 	 * will mount by default if we haven't been given a specific subvolume
35973f73415SJosef Bacik 	 * to mount.
36073f73415SJosef Bacik 	 */
36173f73415SJosef Bacik 	dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
36273f73415SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
363fb4f6f91SDan Carpenter 	if (IS_ERR(di))
364fb4f6f91SDan Carpenter 		return ERR_CAST(di);
36573f73415SJosef Bacik 	if (!di) {
36673f73415SJosef Bacik 		/*
36773f73415SJosef Bacik 		 * Ok the default dir item isn't there.  This is weird since
36873f73415SJosef Bacik 		 * it's always been there, but don't freak out, just try and
36973f73415SJosef Bacik 		 * mount to root most subvolume.
37073f73415SJosef Bacik 		 */
37173f73415SJosef Bacik 		btrfs_free_path(path);
37273f73415SJosef Bacik 		dir_id = BTRFS_FIRST_FREE_OBJECTID;
37373f73415SJosef Bacik 		new_root = root->fs_info->fs_root;
37473f73415SJosef Bacik 		goto setup_root;
37573f73415SJosef Bacik 	}
37673f73415SJosef Bacik 
37773f73415SJosef Bacik 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
37873f73415SJosef Bacik 	btrfs_free_path(path);
37973f73415SJosef Bacik 
38073f73415SJosef Bacik find_root:
38173f73415SJosef Bacik 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
38273f73415SJosef Bacik 	if (IS_ERR(new_root))
38373f73415SJosef Bacik 		return ERR_PTR(PTR_ERR(new_root));
38473f73415SJosef Bacik 
38573f73415SJosef Bacik 	if (btrfs_root_refs(&new_root->root_item) == 0)
38673f73415SJosef Bacik 		return ERR_PTR(-ENOENT);
38773f73415SJosef Bacik 
38873f73415SJosef Bacik 	dir_id = btrfs_root_dirid(&new_root->root_item);
38973f73415SJosef Bacik setup_root:
39073f73415SJosef Bacik 	location.objectid = dir_id;
39173f73415SJosef Bacik 	location.type = BTRFS_INODE_ITEM_KEY;
39273f73415SJosef Bacik 	location.offset = 0;
39373f73415SJosef Bacik 
39473f73415SJosef Bacik 	inode = btrfs_iget(sb, &location, new_root, &new);
395*4cbd1149SDan Carpenter 	if (IS_ERR(inode))
396*4cbd1149SDan Carpenter 		return ERR_CAST(inode);
39773f73415SJosef Bacik 
39873f73415SJosef Bacik 	/*
39973f73415SJosef Bacik 	 * If we're just mounting the root most subvol put the inode and return
40073f73415SJosef Bacik 	 * a reference to the dentry.  We will have already gotten a reference
40173f73415SJosef Bacik 	 * to the inode in btrfs_fill_super so we're good to go.
40273f73415SJosef Bacik 	 */
40373f73415SJosef Bacik 	if (!new && sb->s_root->d_inode == inode) {
40473f73415SJosef Bacik 		iput(inode);
40573f73415SJosef Bacik 		return dget(sb->s_root);
40673f73415SJosef Bacik 	}
40773f73415SJosef Bacik 
40873f73415SJosef Bacik 	if (new) {
40973f73415SJosef Bacik 		const struct qstr name = { .name = "/", .len = 1 };
41073f73415SJosef Bacik 
41173f73415SJosef Bacik 		/*
41273f73415SJosef Bacik 		 * New inode, we need to make the dentry a sibling of s_root so
41373f73415SJosef Bacik 		 * everything gets cleaned up properly on unmount.
41473f73415SJosef Bacik 		 */
41573f73415SJosef Bacik 		dentry = d_alloc(sb->s_root, &name);
41673f73415SJosef Bacik 		if (!dentry) {
41773f73415SJosef Bacik 			iput(inode);
41873f73415SJosef Bacik 			return ERR_PTR(-ENOMEM);
41973f73415SJosef Bacik 		}
42073f73415SJosef Bacik 		d_splice_alias(inode, dentry);
42173f73415SJosef Bacik 	} else {
42273f73415SJosef Bacik 		/*
42373f73415SJosef Bacik 		 * We found the inode in cache, just find a dentry for it and
42473f73415SJosef Bacik 		 * put the reference to the inode we just got.
42573f73415SJosef Bacik 		 */
42673f73415SJosef Bacik 		dentry = d_find_alias(inode);
42773f73415SJosef Bacik 		iput(inode);
42873f73415SJosef Bacik 	}
42973f73415SJosef Bacik 
43073f73415SJosef Bacik 	return dentry;
43173f73415SJosef Bacik }
43273f73415SJosef Bacik 
4338a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb,
4348a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
4358a4b83ccSChris Mason 			    void *data, int silent)
4362e635a27SChris Mason {
4372e635a27SChris Mason 	struct inode *inode;
438e20d96d6SChris Mason 	struct dentry *root_dentry;
439e20d96d6SChris Mason 	struct btrfs_super_block *disk_super;
4400f7d52f4SChris Mason 	struct btrfs_root *tree_root;
4415d4f98a2SYan Zheng 	struct btrfs_key key;
44239279cc3SChris Mason 	int err;
4432e635a27SChris Mason 
4442e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
4452e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
446e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
447be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
4485103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
4492e635a27SChris Mason 	sb->s_time_gran = 1;
4500eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL
45133268eafSJosef Bacik 	sb->s_flags |= MS_POSIXACL;
45249cf6f45SChris Ball #endif
453e20d96d6SChris Mason 
454dfe25020SChris Mason 	tree_root = open_ctree(sb, fs_devices, (char *)data);
455d98237b3SChris Mason 
456e58ca020SYan 	if (IS_ERR(tree_root)) {
457e20d96d6SChris Mason 		printk("btrfs: open_ctree failed\n");
458e58ca020SYan 		return PTR_ERR(tree_root);
459e20d96d6SChris Mason 	}
4600f7d52f4SChris Mason 	sb->s_fs_info = tree_root;
4615f39d397SChris Mason 	disk_super = &tree_root->fs_info->super_copy;
462b888db2bSChris Mason 
4635d4f98a2SYan Zheng 	key.objectid = BTRFS_FIRST_FREE_OBJECTID;
4645d4f98a2SYan Zheng 	key.type = BTRFS_INODE_ITEM_KEY;
4655d4f98a2SYan Zheng 	key.offset = 0;
46673f73415SJosef Bacik 	inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root, NULL);
4675d4f98a2SYan Zheng 	if (IS_ERR(inode)) {
4685d4f98a2SYan Zheng 		err = PTR_ERR(inode);
46939279cc3SChris Mason 		goto fail_close;
47039279cc3SChris Mason 	}
4712e635a27SChris Mason 
472e20d96d6SChris Mason 	root_dentry = d_alloc_root(inode);
473e20d96d6SChris Mason 	if (!root_dentry) {
4742e635a27SChris Mason 		iput(inode);
47539279cc3SChris Mason 		err = -ENOMEM;
47639279cc3SChris Mason 		goto fail_close;
4772e635a27SChris Mason 	}
47858176a96SJosef Bacik 
479e20d96d6SChris Mason 	sb->s_root = root_dentry;
4806885f308SChris Mason 
4816885f308SChris Mason 	save_mount_options(sb, data);
4822e635a27SChris Mason 	return 0;
4832e635a27SChris Mason 
48439279cc3SChris Mason fail_close:
48539279cc3SChris Mason 	close_ctree(tree_root);
486d5719762SChris Mason 	return err;
487d5719762SChris Mason }
488d5719762SChris Mason 
4896bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
490d5719762SChris Mason {
491d5719762SChris Mason 	struct btrfs_trans_handle *trans;
492dccae999SSage Weil 	struct btrfs_root *root = btrfs_sb(sb);
493d5719762SChris Mason 	int ret;
494df2ce34cSChris Mason 
495d561c025SChris Mason 	if (!wait) {
4967cfcc17eSChris Mason 		filemap_flush(root->fs_info->btree_inode->i_mapping);
497df2ce34cSChris Mason 		return 0;
498d561c025SChris Mason 	}
499771ed689SChris Mason 
50024bbcf04SYan, Zheng 	btrfs_start_delalloc_inodes(root, 0);
50124bbcf04SYan, Zheng 	btrfs_wait_ordered_extents(root, 0, 0);
502771ed689SChris Mason 
503a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 0);
504d5719762SChris Mason 	ret = btrfs_commit_transaction(trans, root);
50554aa1f4dSChris Mason 	return ret;
506d5719762SChris Mason }
507d5719762SChris Mason 
508a9572a15SEric Paris static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
509a9572a15SEric Paris {
510a9572a15SEric Paris 	struct btrfs_root *root = btrfs_sb(vfs->mnt_sb);
511a9572a15SEric Paris 	struct btrfs_fs_info *info = root->fs_info;
512a9572a15SEric Paris 
513a9572a15SEric Paris 	if (btrfs_test_opt(root, DEGRADED))
514a9572a15SEric Paris 		seq_puts(seq, ",degraded");
515a9572a15SEric Paris 	if (btrfs_test_opt(root, NODATASUM))
516a9572a15SEric Paris 		seq_puts(seq, ",nodatasum");
517a9572a15SEric Paris 	if (btrfs_test_opt(root, NODATACOW))
518a9572a15SEric Paris 		seq_puts(seq, ",nodatacow");
519a9572a15SEric Paris 	if (btrfs_test_opt(root, NOBARRIER))
520a9572a15SEric Paris 		seq_puts(seq, ",nobarrier");
521a9572a15SEric Paris 	if (info->max_inline != 8192 * 1024)
52221380931SJoel Becker 		seq_printf(seq, ",max_inline=%llu",
52321380931SJoel Becker 			   (unsigned long long)info->max_inline);
524a9572a15SEric Paris 	if (info->alloc_start != 0)
52521380931SJoel Becker 		seq_printf(seq, ",alloc_start=%llu",
52621380931SJoel Becker 			   (unsigned long long)info->alloc_start);
527a9572a15SEric Paris 	if (info->thread_pool_size !=  min_t(unsigned long,
528a9572a15SEric Paris 					     num_online_cpus() + 2, 8))
529a9572a15SEric Paris 		seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
530a9572a15SEric Paris 	if (btrfs_test_opt(root, COMPRESS))
531a9572a15SEric Paris 		seq_puts(seq, ",compress");
532c289811cSChris Mason 	if (btrfs_test_opt(root, NOSSD))
533c289811cSChris Mason 		seq_puts(seq, ",nossd");
534451d7585SChris Mason 	if (btrfs_test_opt(root, SSD_SPREAD))
535451d7585SChris Mason 		seq_puts(seq, ",ssd_spread");
536451d7585SChris Mason 	else if (btrfs_test_opt(root, SSD))
537a9572a15SEric Paris 		seq_puts(seq, ",ssd");
5383a5e1404SSage Weil 	if (btrfs_test_opt(root, NOTREELOG))
5396b65c5c6SSage Weil 		seq_puts(seq, ",notreelog");
540dccae999SSage Weil 	if (btrfs_test_opt(root, FLUSHONCOMMIT))
5416b65c5c6SSage Weil 		seq_puts(seq, ",flushoncommit");
54220a5239aSMatthew Wilcox 	if (btrfs_test_opt(root, DISCARD))
54320a5239aSMatthew Wilcox 		seq_puts(seq, ",discard");
544a9572a15SEric Paris 	if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
545a9572a15SEric Paris 		seq_puts(seq, ",noacl");
546a9572a15SEric Paris 	return 0;
547a9572a15SEric Paris }
548a9572a15SEric Paris 
549a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data)
5502e635a27SChris Mason {
551a061fc8dSChris Mason 	struct btrfs_fs_devices *test_fs_devices = data;
552a061fc8dSChris Mason 	struct btrfs_root *root = btrfs_sb(s);
5534b82d6e4SYan 
554a061fc8dSChris Mason 	return root->fs_info->fs_devices == test_fs_devices;
5554b82d6e4SYan }
5564b82d6e4SYan 
557edf24abeSChristoph Hellwig /*
558edf24abeSChristoph Hellwig  * Find a superblock for the given device / mount point.
559edf24abeSChristoph Hellwig  *
560edf24abeSChristoph Hellwig  * Note:  This is based on get_sb_bdev from fs/super.c with a few additions
561edf24abeSChristoph Hellwig  *	  for multiple device setup.  Make sure to keep it in sync.
562edf24abeSChristoph Hellwig  */
563edf24abeSChristoph Hellwig static int btrfs_get_sb(struct file_system_type *fs_type, int flags,
564edf24abeSChristoph Hellwig 		const char *dev_name, void *data, struct vfsmount *mnt)
5654b82d6e4SYan {
5664b82d6e4SYan 	struct block_device *bdev = NULL;
5674b82d6e4SYan 	struct super_block *s;
5684b82d6e4SYan 	struct dentry *root;
5698a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices = NULL;
57097288f2cSChristoph Hellwig 	fmode_t mode = FMODE_READ;
57173f73415SJosef Bacik 	char *subvol_name = NULL;
57273f73415SJosef Bacik 	u64 subvol_objectid = 0;
5734b82d6e4SYan 	int error = 0;
57473f73415SJosef Bacik 	int found = 0;
5754b82d6e4SYan 
57697288f2cSChristoph Hellwig 	if (!(flags & MS_RDONLY))
57797288f2cSChristoph Hellwig 		mode |= FMODE_WRITE;
57897288f2cSChristoph Hellwig 
57997288f2cSChristoph Hellwig 	error = btrfs_parse_early_options(data, mode, fs_type,
58073f73415SJosef Bacik 					  &subvol_name, &subvol_objectid,
58173f73415SJosef Bacik 					  &fs_devices);
582edf24abeSChristoph Hellwig 	if (error)
5831f483660SShen Feng 		return error;
584edf24abeSChristoph Hellwig 
58597288f2cSChristoph Hellwig 	error = btrfs_scan_one_device(dev_name, mode, fs_type, &fs_devices);
5868a4b83ccSChris Mason 	if (error)
587edf24abeSChristoph Hellwig 		goto error_free_subvol_name;
5884b82d6e4SYan 
58997288f2cSChristoph Hellwig 	error = btrfs_open_devices(fs_devices, mode, fs_type);
5908a4b83ccSChris Mason 	if (error)
591edf24abeSChristoph Hellwig 		goto error_free_subvol_name;
5928a4b83ccSChris Mason 
5932b82032cSYan Zheng 	if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
5942b82032cSYan Zheng 		error = -EACCES;
5952b82032cSYan Zheng 		goto error_close_devices;
5962b82032cSYan Zheng 	}
5972b82032cSYan Zheng 
598dfe25020SChris Mason 	bdev = fs_devices->latest_bdev;
599a061fc8dSChris Mason 	s = sget(fs_type, btrfs_test_super, set_anon_super, fs_devices);
6004b82d6e4SYan 	if (IS_ERR(s))
6014b82d6e4SYan 		goto error_s;
6024b82d6e4SYan 
6034b82d6e4SYan 	if (s->s_root) {
6044b82d6e4SYan 		if ((flags ^ s->s_flags) & MS_RDONLY) {
6056f5bbff9SAl Viro 			deactivate_locked_super(s);
6064b82d6e4SYan 			error = -EBUSY;
607c146afadSYan Zheng 			goto error_close_devices;
6084b82d6e4SYan 		}
6094b82d6e4SYan 
61073f73415SJosef Bacik 		found = 1;
6112b82032cSYan Zheng 		btrfs_close_devices(fs_devices);
6124b82d6e4SYan 	} else {
6134b82d6e4SYan 		char b[BDEVNAME_SIZE];
6144b82d6e4SYan 
6154b82d6e4SYan 		s->s_flags = flags;
6164b82d6e4SYan 		strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
6178a4b83ccSChris Mason 		error = btrfs_fill_super(s, fs_devices, data,
6188a4b83ccSChris Mason 					 flags & MS_SILENT ? 1 : 0);
6194b82d6e4SYan 		if (error) {
6206f5bbff9SAl Viro 			deactivate_locked_super(s);
6211f483660SShen Feng 			goto error_free_subvol_name;
6224b82d6e4SYan 		}
6234b82d6e4SYan 
624788f20ebSChris Mason 		btrfs_sb(s)->fs_info->bdev_holder = fs_type;
6254b82d6e4SYan 		s->s_flags |= MS_ACTIVE;
6264b82d6e4SYan 	}
6274b82d6e4SYan 
62873f73415SJosef Bacik 	root = get_default_root(s, subvol_objectid);
6294b82d6e4SYan 	if (IS_ERR(root)) {
6304b82d6e4SYan 		error = PTR_ERR(root);
63173f73415SJosef Bacik 		deactivate_locked_super(s);
63273f73415SJosef Bacik 		goto error;
6334b82d6e4SYan 	}
63473f73415SJosef Bacik 	/* if they gave us a subvolume name bind mount into that */
63573f73415SJosef Bacik 	if (strcmp(subvol_name, ".")) {
63673f73415SJosef Bacik 		struct dentry *new_root;
63773f73415SJosef Bacik 		mutex_lock(&root->d_inode->i_mutex);
63873f73415SJosef Bacik 		new_root = lookup_one_len(subvol_name, root,
63973f73415SJosef Bacik 				      strlen(subvol_name));
64073f73415SJosef Bacik 		mutex_unlock(&root->d_inode->i_mutex);
64173f73415SJosef Bacik 
64273f73415SJosef Bacik 		if (IS_ERR(new_root)) {
64373f73415SJosef Bacik 			deactivate_locked_super(s);
64473f73415SJosef Bacik 			error = PTR_ERR(new_root);
6454b82d6e4SYan 			dput(root);
64673f73415SJosef Bacik 			goto error_close_devices;
64773f73415SJosef Bacik 		}
64873f73415SJosef Bacik 		if (!new_root->d_inode) {
64973f73415SJosef Bacik 			dput(root);
65073f73415SJosef Bacik 			dput(new_root);
6516f5bbff9SAl Viro 			deactivate_locked_super(s);
6524b82d6e4SYan 			error = -ENXIO;
65373f73415SJosef Bacik 			goto error_close_devices;
6544b82d6e4SYan 		}
65573f73415SJosef Bacik 		dput(root);
65673f73415SJosef Bacik 		root = new_root;
65776fcef19SDavid Woodhouse 	}
6584b82d6e4SYan 
6594b82d6e4SYan 	mnt->mnt_sb = s;
6604b82d6e4SYan 	mnt->mnt_root = root;
661edf24abeSChristoph Hellwig 
662edf24abeSChristoph Hellwig 	kfree(subvol_name);
6634b82d6e4SYan 	return 0;
6644b82d6e4SYan 
6654b82d6e4SYan error_s:
6664b82d6e4SYan 	error = PTR_ERR(s);
667c146afadSYan Zheng error_close_devices:
6688a4b83ccSChris Mason 	btrfs_close_devices(fs_devices);
669edf24abeSChristoph Hellwig error_free_subvol_name:
670edf24abeSChristoph Hellwig 	kfree(subvol_name);
67173f73415SJosef Bacik error:
6724b82d6e4SYan 	return error;
6734b82d6e4SYan }
6742e635a27SChris Mason 
675c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
676c146afadSYan Zheng {
677c146afadSYan Zheng 	struct btrfs_root *root = btrfs_sb(sb);
678c146afadSYan Zheng 	int ret;
679c146afadSYan Zheng 
680b288052eSChris Mason 	ret = btrfs_parse_options(root, data);
681b288052eSChris Mason 	if (ret)
682b288052eSChris Mason 		return -EINVAL;
683b288052eSChris Mason 
684c146afadSYan Zheng 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
685c146afadSYan Zheng 		return 0;
686c146afadSYan Zheng 
687c146afadSYan Zheng 	if (*flags & MS_RDONLY) {
688c146afadSYan Zheng 		sb->s_flags |= MS_RDONLY;
689c146afadSYan Zheng 
690c146afadSYan Zheng 		ret =  btrfs_commit_super(root);
691c146afadSYan Zheng 		WARN_ON(ret);
692c146afadSYan Zheng 	} else {
6932b82032cSYan Zheng 		if (root->fs_info->fs_devices->rw_devices == 0)
6942b82032cSYan Zheng 			return -EACCES;
6952b82032cSYan Zheng 
696c146afadSYan Zheng 		if (btrfs_super_log_root(&root->fs_info->super_copy) != 0)
697c146afadSYan Zheng 			return -EINVAL;
698c146afadSYan Zheng 
699d68fc57bSYan, Zheng 		ret = btrfs_cleanup_fs_roots(root->fs_info);
700c146afadSYan Zheng 		WARN_ON(ret);
701c146afadSYan Zheng 
702d68fc57bSYan, Zheng 		/* recover relocation */
703d68fc57bSYan, Zheng 		ret = btrfs_recover_relocation(root);
704c146afadSYan Zheng 		WARN_ON(ret);
705c146afadSYan Zheng 
706c146afadSYan Zheng 		sb->s_flags &= ~MS_RDONLY;
707c146afadSYan Zheng 	}
708c146afadSYan Zheng 
709c146afadSYan Zheng 	return 0;
710c146afadSYan Zheng }
711c146afadSYan Zheng 
7128fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
7138fd17795SChris Mason {
7148fd17795SChris Mason 	struct btrfs_root *root = btrfs_sb(dentry->d_sb);
7154b52dff6SChris Mason 	struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
716bd4d1088SJosef Bacik 	struct list_head *head = &root->fs_info->space_info;
717bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
718bd4d1088SJosef Bacik 	u64 total_used = 0;
719db94535dSChris Mason 	int bits = dentry->d_sb->s_blocksize_bits;
7209d03632eSDavid Woodhouse 	__be32 *fsid = (__be32 *)root->fs_info->fsid;
7218fd17795SChris Mason 
722bd4d1088SJosef Bacik 	rcu_read_lock();
723b742bb82SYan, Zheng 	list_for_each_entry_rcu(found, head, list)
724b742bb82SYan, Zheng 		total_used += found->disk_used;
725bd4d1088SJosef Bacik 	rcu_read_unlock();
726bd4d1088SJosef Bacik 
7278fd17795SChris Mason 	buf->f_namelen = BTRFS_NAME_LEN;
728db94535dSChris Mason 	buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
729bd4d1088SJosef Bacik 	buf->f_bfree = buf->f_blocks - (total_used >> bits);
730b742bb82SYan, Zheng 	buf->f_bavail = buf->f_bfree;
7318fd17795SChris Mason 	buf->f_bsize = dentry->d_sb->s_blocksize;
7328fd17795SChris Mason 	buf->f_type = BTRFS_SUPER_MAGIC;
733d397712bSChris Mason 
7349d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
7359d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
7369d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
7379d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
7389d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
73932d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
74032d48fa1SDavid Woodhouse 	buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
74132d48fa1SDavid Woodhouse 	buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
74232d48fa1SDavid Woodhouse 
7438fd17795SChris Mason 	return 0;
7448fd17795SChris Mason }
745b5133862SChris Mason 
7462e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
7472e635a27SChris Mason 	.owner		= THIS_MODULE,
7482e635a27SChris Mason 	.name		= "btrfs",
7492e635a27SChris Mason 	.get_sb		= btrfs_get_sb,
750a061fc8dSChris Mason 	.kill_sb	= kill_anon_super,
7512e635a27SChris Mason 	.fs_flags	= FS_REQUIRES_DEV,
7522e635a27SChris Mason };
753a9218f6bSChris Mason 
754d352ac68SChris Mason /*
755d352ac68SChris Mason  * used by btrfsctl to scan devices when no FS is mounted
756d352ac68SChris Mason  */
7578a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
7588a4b83ccSChris Mason 				unsigned long arg)
7598a4b83ccSChris Mason {
7608a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
7618a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices;
762c071fcfdSChris Mason 	int ret = -ENOTTY;
7638a4b83ccSChris Mason 
764e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
765e441d54dSChris Mason 		return -EPERM;
766e441d54dSChris Mason 
767dae7b665SLi Zefan 	vol = memdup_user((void __user *)arg, sizeof(*vol));
768dae7b665SLi Zefan 	if (IS_ERR(vol))
769dae7b665SLi Zefan 		return PTR_ERR(vol);
770c071fcfdSChris Mason 
7718a4b83ccSChris Mason 	switch (cmd) {
7728a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
77397288f2cSChristoph Hellwig 		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
7748a4b83ccSChris Mason 					    &btrfs_fs_type, &fs_devices);
7758a4b83ccSChris Mason 		break;
7768a4b83ccSChris Mason 	}
777dae7b665SLi Zefan 
7788a4b83ccSChris Mason 	kfree(vol);
779f819d837SLinda Knippers 	return ret;
7808a4b83ccSChris Mason }
7818a4b83ccSChris Mason 
7820176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
783ed0dab6bSYan {
784ed0dab6bSYan 	struct btrfs_root *root = btrfs_sb(sb);
785a74a4b97SChris Mason 	mutex_lock(&root->fs_info->transaction_kthread_mutex);
786a74a4b97SChris Mason 	mutex_lock(&root->fs_info->cleaner_mutex);
7870176260fSLinus Torvalds 	return 0;
788ed0dab6bSYan }
789ed0dab6bSYan 
7900176260fSLinus Torvalds static int btrfs_unfreeze(struct super_block *sb)
791ed0dab6bSYan {
792ed0dab6bSYan 	struct btrfs_root *root = btrfs_sb(sb);
793a74a4b97SChris Mason 	mutex_unlock(&root->fs_info->cleaner_mutex);
794a74a4b97SChris Mason 	mutex_unlock(&root->fs_info->transaction_kthread_mutex);
7950176260fSLinus Torvalds 	return 0;
796ed0dab6bSYan }
7972e635a27SChris Mason 
798b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
79976dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
800134e9731SChris Mason 	.delete_inode	= btrfs_delete_inode,
801e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
802d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
803a9572a15SEric Paris 	.show_options	= btrfs_show_options,
8044730a4bcSChris Mason 	.write_inode	= btrfs_write_inode,
805b5133862SChris Mason 	.dirty_inode	= btrfs_dirty_inode,
8062c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
8072c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
8088fd17795SChris Mason 	.statfs		= btrfs_statfs,
809c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
8100176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
8110176260fSLinus Torvalds 	.unfreeze_fs	= btrfs_unfreeze,
812e20d96d6SChris Mason };
813a9218f6bSChris Mason 
814a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
815a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
816a9218f6bSChris Mason 	.compat_ioctl = btrfs_control_ioctl,
817a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
818a9218f6bSChris Mason };
819a9218f6bSChris Mason 
820a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
821a9218f6bSChris Mason 	.minor		= MISC_DYNAMIC_MINOR,
822a9218f6bSChris Mason 	.name		= "btrfs-control",
823a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
824a9218f6bSChris Mason };
825a9218f6bSChris Mason 
826a9218f6bSChris Mason static int btrfs_interface_init(void)
827a9218f6bSChris Mason {
828a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
829a9218f6bSChris Mason }
830a9218f6bSChris Mason 
831b2950863SChristoph Hellwig static void btrfs_interface_exit(void)
832a9218f6bSChris Mason {
833a9218f6bSChris Mason 	if (misc_deregister(&btrfs_misc) < 0)
834d397712bSChris Mason 		printk(KERN_INFO "misc_deregister failed for control device");
835a9218f6bSChris Mason }
836a9218f6bSChris Mason 
8372e635a27SChris Mason static int __init init_btrfs_fs(void)
8382e635a27SChris Mason {
8392c90e5d6SChris Mason 	int err;
84058176a96SJosef Bacik 
84158176a96SJosef Bacik 	err = btrfs_init_sysfs();
84258176a96SJosef Bacik 	if (err)
84358176a96SJosef Bacik 		return err;
84458176a96SJosef Bacik 
84539279cc3SChris Mason 	err = btrfs_init_cachep();
8462c90e5d6SChris Mason 	if (err)
847a74a4b97SChris Mason 		goto free_sysfs;
848d1310b2eSChris Mason 
849d1310b2eSChris Mason 	err = extent_io_init();
8502f4cbe64SWyatt Banks 	if (err)
8512f4cbe64SWyatt Banks 		goto free_cachep;
8522f4cbe64SWyatt Banks 
853d1310b2eSChris Mason 	err = extent_map_init();
854d1310b2eSChris Mason 	if (err)
855d1310b2eSChris Mason 		goto free_extent_io;
856d1310b2eSChris Mason 
857a9218f6bSChris Mason 	err = btrfs_interface_init();
8582f4cbe64SWyatt Banks 	if (err)
8592f4cbe64SWyatt Banks 		goto free_extent_map;
860c8b97818SChris Mason 
861a9218f6bSChris Mason 	err = register_filesystem(&btrfs_fs_type);
862a9218f6bSChris Mason 	if (err)
863a9218f6bSChris Mason 		goto unregister_ioctl;
864b3c3da71SChris Mason 
865b3c3da71SChris Mason 	printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
8662f4cbe64SWyatt Banks 	return 0;
8672f4cbe64SWyatt Banks 
868a9218f6bSChris Mason unregister_ioctl:
869a9218f6bSChris Mason 	btrfs_interface_exit();
8702f4cbe64SWyatt Banks free_extent_map:
8712f4cbe64SWyatt Banks 	extent_map_exit();
872d1310b2eSChris Mason free_extent_io:
873d1310b2eSChris Mason 	extent_io_exit();
8742f4cbe64SWyatt Banks free_cachep:
8752f4cbe64SWyatt Banks 	btrfs_destroy_cachep();
876a74a4b97SChris Mason free_sysfs:
8772f4cbe64SWyatt Banks 	btrfs_exit_sysfs();
8782c90e5d6SChris Mason 	return err;
8792e635a27SChris Mason }
8802e635a27SChris Mason 
8812e635a27SChris Mason static void __exit exit_btrfs_fs(void)
8822e635a27SChris Mason {
88339279cc3SChris Mason 	btrfs_destroy_cachep();
884a52d9a80SChris Mason 	extent_map_exit();
885d1310b2eSChris Mason 	extent_io_exit();
886a9218f6bSChris Mason 	btrfs_interface_exit();
8872e635a27SChris Mason 	unregister_filesystem(&btrfs_fs_type);
88858176a96SJosef Bacik 	btrfs_exit_sysfs();
8898a4b83ccSChris Mason 	btrfs_cleanup_fs_uuids();
890c8b97818SChris Mason 	btrfs_zlib_exit();
8912e635a27SChris Mason }
8922e635a27SChris Mason 
8932e635a27SChris Mason module_init(init_btrfs_fs)
8942e635a27SChris Mason module_exit(exit_btrfs_fs)
8952e635a27SChris Mason 
8962e635a27SChris Mason MODULE_LICENSE("GPL");
897