xref: /linux/fs/btrfs/super.c (revision 19d00cc196a3a66fd074f62b39d219f743b92338)
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>
272e635a27SChris Mason #include <linux/string.h>
282e635a27SChris Mason #include <linux/smp_lock.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>
404b4e25f2SChris Mason #include <linux/version.h>
414b4e25f2SChris Mason #include "compat.h"
422e635a27SChris Mason #include "ctree.h"
43e20d96d6SChris Mason #include "disk-io.h"
44d5719762SChris Mason #include "transaction.h"
452c90e5d6SChris Mason #include "btrfs_inode.h"
46c5739bbaSChris Mason #include "ioctl.h"
473a686375SChris Mason #include "print-tree.h"
485103e947SJosef Bacik #include "xattr.h"
498a4b83ccSChris Mason #include "volumes.h"
50b3c3da71SChris Mason #include "version.h"
51be6e8dc0SBalaji Rao #include "export.h"
52c8b97818SChris Mason #include "compression.h"
532e635a27SChris Mason 
545f39d397SChris Mason #define BTRFS_SUPER_MAGIC 0x9123683E
55e20d96d6SChris Mason 
56e20d96d6SChris Mason static struct super_operations btrfs_super_ops;
57e20d96d6SChris Mason 
58e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb)
59e20d96d6SChris Mason {
60e20d96d6SChris Mason 	struct btrfs_root *root = btrfs_sb(sb);
61e20d96d6SChris Mason 	int ret;
62e20d96d6SChris Mason 
63e20d96d6SChris Mason 	ret = close_ctree(root);
64e20d96d6SChris Mason 	sb->s_fs_info = NULL;
65e20d96d6SChris Mason }
662e635a27SChris Mason 
6795e05289SChris Mason enum {
6843e570b0SChristoph Hellwig 	Opt_degraded, Opt_subvol, Opt_device, Opt_nodatasum, Opt_nodatacow,
69dfe25020SChris Mason 	Opt_max_extent, Opt_max_inline, Opt_alloc_start, Opt_nobarrier,
70c8b97818SChris Mason 	Opt_ssd, Opt_thread_pool, Opt_noacl,  Opt_compress, Opt_err,
7195e05289SChris Mason };
7295e05289SChris Mason 
7395e05289SChris Mason static match_table_t tokens = {
74dfe25020SChris Mason 	{Opt_degraded, "degraded"},
7595e05289SChris Mason 	{Opt_subvol, "subvol=%s"},
7643e570b0SChristoph Hellwig 	{Opt_device, "device=%s"},
77b6cda9bcSChris Mason 	{Opt_nodatasum, "nodatasum"},
78be20aa9dSChris Mason 	{Opt_nodatacow, "nodatacow"},
7921ad10cfSChris Mason 	{Opt_nobarrier, "nobarrier"},
80c59f8951SChris Mason 	{Opt_max_extent, "max_extent=%s"},
816f568d35SChris Mason 	{Opt_max_inline, "max_inline=%s"},
828f662a76SChris Mason 	{Opt_alloc_start, "alloc_start=%s"},
834543df7eSChris Mason 	{Opt_thread_pool, "thread_pool=%d"},
84c8b97818SChris Mason 	{Opt_compress, "compress"},
85e18e4809SChris Mason 	{Opt_ssd, "ssd"},
8633268eafSJosef Bacik 	{Opt_noacl, "noacl"},
8733268eafSJosef Bacik 	{Opt_err, NULL},
8895e05289SChris Mason };
8995e05289SChris Mason 
90edbd8d4eSChris Mason u64 btrfs_parse_size(char *str)
91c59f8951SChris Mason {
92edbd8d4eSChris Mason 	u64 res;
93c59f8951SChris Mason 	int mult = 1;
94c59f8951SChris Mason 	char *end;
95c59f8951SChris Mason 	char last;
96c59f8951SChris Mason 
97c59f8951SChris Mason 	res = simple_strtoul(str, &end, 10);
98c59f8951SChris Mason 
99c59f8951SChris Mason 	last = end[0];
100c59f8951SChris Mason 	if (isalpha(last)) {
101c59f8951SChris Mason 		last = tolower(last);
102c59f8951SChris Mason 		switch (last) {
103c59f8951SChris Mason 		case 'g':
104c59f8951SChris Mason 			mult *= 1024;
105c59f8951SChris Mason 		case 'm':
106c59f8951SChris Mason 			mult *= 1024;
107c59f8951SChris Mason 		case 'k':
108c59f8951SChris Mason 			mult *= 1024;
109c59f8951SChris Mason 		}
110c59f8951SChris Mason 		res = res * mult;
111c59f8951SChris Mason 	}
112c59f8951SChris Mason 	return res;
113c59f8951SChris Mason }
114c59f8951SChris Mason 
115edf24abeSChristoph Hellwig /*
116edf24abeSChristoph Hellwig  * Regular mount options parser.  Everything that is needed only when
117edf24abeSChristoph Hellwig  * reading in a new superblock is parsed here.
118edf24abeSChristoph Hellwig  */
119edf24abeSChristoph Hellwig int btrfs_parse_options(struct btrfs_root *root, char *options)
12095e05289SChris Mason {
121edf24abeSChristoph Hellwig 	struct btrfs_fs_info *info = root->fs_info;
12295e05289SChris Mason 	substring_t args[MAX_OPT_ARGS];
123edf24abeSChristoph Hellwig 	char *p, *num;
1244543df7eSChris Mason 	int intarg;
125b6cda9bcSChris Mason 
12695e05289SChris Mason 	if (!options)
127edf24abeSChristoph Hellwig 		return 0;
12895e05289SChris Mason 
129be20aa9dSChris Mason 	/*
130be20aa9dSChris Mason 	 * strsep changes the string, duplicate it because parse_options
131be20aa9dSChris Mason 	 * gets called twice
132be20aa9dSChris Mason 	 */
133be20aa9dSChris Mason 	options = kstrdup(options, GFP_NOFS);
134be20aa9dSChris Mason 	if (!options)
135be20aa9dSChris Mason 		return -ENOMEM;
136be20aa9dSChris Mason 
137be20aa9dSChris Mason 
13895e05289SChris Mason 	while ((p = strsep(&options, ",")) != NULL) {
13995e05289SChris Mason 		int token;
14095e05289SChris Mason 		if (!*p)
14195e05289SChris Mason 			continue;
14295e05289SChris Mason 
14395e05289SChris Mason 		token = match_token(p, tokens, args);
14495e05289SChris Mason 		switch (token) {
145dfe25020SChris Mason 		case Opt_degraded:
146edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: allowing degraded mounts\n");
147dfe25020SChris Mason 			btrfs_set_opt(info->mount_opt, DEGRADED);
148dfe25020SChris Mason 			break;
14995e05289SChris Mason 		case Opt_subvol:
15043e570b0SChristoph Hellwig 		case Opt_device:
151edf24abeSChristoph Hellwig 			/*
15243e570b0SChristoph Hellwig 			 * These are parsed by btrfs_parse_early_options
153edf24abeSChristoph Hellwig 			 * and can be happily ignored here.
154edf24abeSChristoph Hellwig 			 */
15595e05289SChris Mason 			break;
156b6cda9bcSChris Mason 		case Opt_nodatasum:
157edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: setting nodatacsum\n");
158b6cda9bcSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
159be20aa9dSChris Mason 			break;
160be20aa9dSChris Mason 		case Opt_nodatacow:
161edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: setting nodatacow\n");
162be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATACOW);
163be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
164b6cda9bcSChris Mason 			break;
165c8b97818SChris Mason 		case Opt_compress:
166c8b97818SChris Mason 			printk(KERN_INFO "btrfs: use compression\n");
167c8b97818SChris Mason 			btrfs_set_opt(info->mount_opt, COMPRESS);
168c8b97818SChris Mason 			break;
169e18e4809SChris Mason 		case Opt_ssd:
170edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
171e18e4809SChris Mason 			btrfs_set_opt(info->mount_opt, SSD);
172e18e4809SChris Mason 			break;
17321ad10cfSChris Mason 		case Opt_nobarrier:
174edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: turning off barriers\n");
17521ad10cfSChris Mason 			btrfs_set_opt(info->mount_opt, NOBARRIER);
17621ad10cfSChris Mason 			break;
1774543df7eSChris Mason 		case Opt_thread_pool:
1784543df7eSChris Mason 			intarg = 0;
1794543df7eSChris Mason 			match_int(&args[0], &intarg);
1804543df7eSChris Mason 			if (intarg) {
1814543df7eSChris Mason 				info->thread_pool_size = intarg;
1824543df7eSChris Mason 				printk(KERN_INFO "btrfs: thread pool %d\n",
1834543df7eSChris Mason 				       info->thread_pool_size);
1844543df7eSChris Mason 			}
1854543df7eSChris Mason 			break;
186c59f8951SChris Mason 		case Opt_max_extent:
187edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
188c59f8951SChris Mason 			if (num) {
189edf24abeSChristoph Hellwig 				info->max_extent = btrfs_parse_size(num);
190c59f8951SChris Mason 				kfree(num);
191c59f8951SChris Mason 
192c59f8951SChris Mason 				info->max_extent = max_t(u64,
193edf24abeSChristoph Hellwig 					info->max_extent, root->sectorsize);
194edf24abeSChristoph Hellwig 				printk(KERN_INFO "btrfs: max_extent at %llu\n",
195c59f8951SChris Mason 				       info->max_extent);
196c59f8951SChris Mason 			}
197c59f8951SChris Mason 			break;
1986f568d35SChris Mason 		case Opt_max_inline:
199edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
2006f568d35SChris Mason 			if (num) {
201edf24abeSChristoph Hellwig 				info->max_inline = btrfs_parse_size(num);
2026f568d35SChris Mason 				kfree(num);
2036f568d35SChris Mason 
20415ada040SChris Mason 				if (info->max_inline) {
2056f568d35SChris Mason 					info->max_inline = max_t(u64,
20615ada040SChris Mason 						info->max_inline,
20715ada040SChris Mason 						root->sectorsize);
20815ada040SChris Mason 				}
209edf24abeSChristoph Hellwig 				printk(KERN_INFO "btrfs: max_inline at %llu\n",
2106f568d35SChris Mason 					info->max_inline);
2116f568d35SChris Mason 			}
2126f568d35SChris Mason 			break;
2138f662a76SChris Mason 		case Opt_alloc_start:
214edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
2158f662a76SChris Mason 			if (num) {
216edf24abeSChristoph Hellwig 				info->alloc_start = btrfs_parse_size(num);
2178f662a76SChris Mason 				kfree(num);
218edf24abeSChristoph Hellwig 				printk(KERN_INFO
219edf24abeSChristoph Hellwig 					"btrfs: allocations start at %llu\n",
220edf24abeSChristoph Hellwig 					info->alloc_start);
2218f662a76SChris Mason 			}
2228f662a76SChris Mason 			break;
22333268eafSJosef Bacik 		case Opt_noacl:
22433268eafSJosef Bacik 			root->fs_info->sb->s_flags &= ~MS_POSIXACL;
22533268eafSJosef Bacik 			break;
22695e05289SChris Mason 		default:
227be20aa9dSChris Mason 			break;
22895e05289SChris Mason 		}
22995e05289SChris Mason 	}
230be20aa9dSChris Mason 	kfree(options);
231edf24abeSChristoph Hellwig 	return 0;
232edf24abeSChristoph Hellwig }
233edf24abeSChristoph Hellwig 
234edf24abeSChristoph Hellwig /*
235edf24abeSChristoph Hellwig  * Parse mount options that are required early in the mount process.
236edf24abeSChristoph Hellwig  *
237edf24abeSChristoph Hellwig  * All other options will be parsed on much later in the mount process and
238edf24abeSChristoph Hellwig  * only when we need to allocate a new super block.
239edf24abeSChristoph Hellwig  */
24097288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags,
24143e570b0SChristoph Hellwig 		void *holder, char **subvol_name,
24243e570b0SChristoph Hellwig 		struct btrfs_fs_devices **fs_devices)
243edf24abeSChristoph Hellwig {
244edf24abeSChristoph Hellwig 	substring_t args[MAX_OPT_ARGS];
245edf24abeSChristoph Hellwig 	char *opts, *p;
246edf24abeSChristoph Hellwig 	int error = 0;
247edf24abeSChristoph Hellwig 
248edf24abeSChristoph Hellwig 	if (!options)
249edf24abeSChristoph Hellwig 		goto out;
250edf24abeSChristoph Hellwig 
251edf24abeSChristoph Hellwig 	/*
252edf24abeSChristoph Hellwig 	 * strsep changes the string, duplicate it because parse_options
253edf24abeSChristoph Hellwig 	 * gets called twice
254edf24abeSChristoph Hellwig 	 */
255edf24abeSChristoph Hellwig 	opts = kstrdup(options, GFP_KERNEL);
256edf24abeSChristoph Hellwig 	if (!opts)
257edf24abeSChristoph Hellwig 		return -ENOMEM;
258edf24abeSChristoph Hellwig 
259edf24abeSChristoph Hellwig 	while ((p = strsep(&opts, ",")) != NULL) {
260edf24abeSChristoph Hellwig 		int token;
261edf24abeSChristoph Hellwig 		if (!*p)
262edf24abeSChristoph Hellwig 			continue;
263edf24abeSChristoph Hellwig 
264edf24abeSChristoph Hellwig 		token = match_token(p, tokens, args);
265edf24abeSChristoph Hellwig 		switch (token) {
266edf24abeSChristoph Hellwig 		case Opt_subvol:
267edf24abeSChristoph Hellwig 			*subvol_name = match_strdup(&args[0]);
268edf24abeSChristoph Hellwig 			break;
26943e570b0SChristoph Hellwig 		case Opt_device:
27043e570b0SChristoph Hellwig 			error = btrfs_scan_one_device(match_strdup(&args[0]),
27143e570b0SChristoph Hellwig 					flags, holder, fs_devices);
27243e570b0SChristoph Hellwig 			if (error)
27343e570b0SChristoph Hellwig 				goto out_free_opts;
27443e570b0SChristoph Hellwig 			break;
275edf24abeSChristoph Hellwig 		default:
276edf24abeSChristoph Hellwig 			break;
277edf24abeSChristoph Hellwig 		}
278edf24abeSChristoph Hellwig 	}
279edf24abeSChristoph Hellwig 
28043e570b0SChristoph Hellwig  out_free_opts:
281edf24abeSChristoph Hellwig 	kfree(opts);
282edf24abeSChristoph Hellwig  out:
283edf24abeSChristoph Hellwig 	/*
284edf24abeSChristoph Hellwig 	 * If no subvolume name is specified we use the default one.  Allocate
2853de4586cSChris Mason 	 * a copy of the string "." here so that code later in the
286edf24abeSChristoph Hellwig 	 * mount path doesn't care if it's the default volume or another one.
287edf24abeSChristoph Hellwig 	 */
288edf24abeSChristoph Hellwig 	if (!*subvol_name) {
2893de4586cSChris Mason 		*subvol_name = kstrdup(".", GFP_KERNEL);
290edf24abeSChristoph Hellwig 		if (!*subvol_name)
291edf24abeSChristoph Hellwig 			return -ENOMEM;
292edf24abeSChristoph Hellwig 	}
293edf24abeSChristoph Hellwig 	return error;
29495e05289SChris Mason }
29595e05289SChris Mason 
2968a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb,
2978a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
2988a4b83ccSChris Mason 			    void *data, int silent)
2992e635a27SChris Mason {
3002e635a27SChris Mason 	struct inode *inode;
301e20d96d6SChris Mason 	struct dentry *root_dentry;
302e20d96d6SChris Mason 	struct btrfs_super_block *disk_super;
3030f7d52f4SChris Mason 	struct btrfs_root *tree_root;
304d6e4a428SChris Mason 	struct btrfs_inode *bi;
30539279cc3SChris Mason 	int err;
3062e635a27SChris Mason 
3072e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
3082e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
309e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
310be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
3115103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
3122e635a27SChris Mason 	sb->s_time_gran = 1;
31333268eafSJosef Bacik 	sb->s_flags |= MS_POSIXACL;
314e20d96d6SChris Mason 
315dfe25020SChris Mason 	tree_root = open_ctree(sb, fs_devices, (char *)data);
316d98237b3SChris Mason 
317e58ca020SYan 	if (IS_ERR(tree_root)) {
318e20d96d6SChris Mason 		printk("btrfs: open_ctree failed\n");
319e58ca020SYan 		return PTR_ERR(tree_root);
320e20d96d6SChris Mason 	}
3210f7d52f4SChris Mason 	sb->s_fs_info = tree_root;
3225f39d397SChris Mason 	disk_super = &tree_root->fs_info->super_copy;
3233de4586cSChris Mason 	inode = btrfs_iget_locked(sb, BTRFS_FIRST_FREE_OBJECTID,
3243de4586cSChris Mason 				  tree_root->fs_info->fs_root);
325d6e4a428SChris Mason 	bi = BTRFS_I(inode);
326d6e4a428SChris Mason 	bi->location.objectid = inode->i_ino;
327d6e4a428SChris Mason 	bi->location.offset = 0;
3283de4586cSChris Mason 	bi->root = tree_root->fs_info->fs_root;
329b888db2bSChris Mason 
330d6e4a428SChris Mason 	btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
331d6e4a428SChris Mason 
33239279cc3SChris Mason 	if (!inode) {
33339279cc3SChris Mason 		err = -ENOMEM;
33439279cc3SChris Mason 		goto fail_close;
33539279cc3SChris Mason 	}
336e20d96d6SChris Mason 	if (inode->i_state & I_NEW) {
337e20d96d6SChris Mason 		btrfs_read_locked_inode(inode);
338e20d96d6SChris Mason 		unlock_new_inode(inode);
339e20d96d6SChris Mason 	}
3402e635a27SChris Mason 
341e20d96d6SChris Mason 	root_dentry = d_alloc_root(inode);
342e20d96d6SChris Mason 	if (!root_dentry) {
3432e635a27SChris Mason 		iput(inode);
34439279cc3SChris Mason 		err = -ENOMEM;
34539279cc3SChris Mason 		goto fail_close;
3462e635a27SChris Mason 	}
347e4404d6eSYan Zheng #if 0
34858176a96SJosef Bacik 	/* this does the super kobj at the same time */
34958176a96SJosef Bacik 	err = btrfs_sysfs_add_super(tree_root->fs_info);
35058176a96SJosef Bacik 	if (err)
35158176a96SJosef Bacik 		goto fail_close;
352e4404d6eSYan Zheng #endif
35358176a96SJosef Bacik 
354e20d96d6SChris Mason 	sb->s_root = root_dentry;
3556885f308SChris Mason 
3566885f308SChris Mason 	save_mount_options(sb, data);
3572e635a27SChris Mason 	return 0;
3582e635a27SChris Mason 
35939279cc3SChris Mason fail_close:
36039279cc3SChris Mason 	close_ctree(tree_root);
361d5719762SChris Mason 	return err;
362d5719762SChris Mason }
363d5719762SChris Mason 
3646bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
365d5719762SChris Mason {
366d5719762SChris Mason 	struct btrfs_trans_handle *trans;
367d5719762SChris Mason 	struct btrfs_root *root;
368d5719762SChris Mason 	int ret;
369d98237b3SChris Mason 	root = btrfs_sb(sb);
370df2ce34cSChris Mason 
371c146afadSYan Zheng 	if (sb->s_flags & MS_RDONLY)
372c146afadSYan Zheng 		return 0;
373c146afadSYan Zheng 
374d5719762SChris Mason 	sb->s_dirt = 0;
375d561c025SChris Mason 	if (!wait) {
3767cfcc17eSChris Mason 		filemap_flush(root->fs_info->btree_inode->i_mapping);
377df2ce34cSChris Mason 		return 0;
378d561c025SChris Mason 	}
379771ed689SChris Mason 
380771ed689SChris Mason 	btrfs_start_delalloc_inodes(root);
381771ed689SChris Mason 	btrfs_wait_ordered_extents(root, 0);
382771ed689SChris Mason 
383e9d0b13bSChris Mason 	btrfs_clean_old_snapshots(root);
384d5719762SChris Mason 	trans = btrfs_start_transaction(root, 1);
385d5719762SChris Mason 	ret = btrfs_commit_transaction(trans, root);
386d5719762SChris Mason 	sb->s_dirt = 0;
38754aa1f4dSChris Mason 	return ret;
388d5719762SChris Mason }
389d5719762SChris Mason 
390d561c025SChris Mason static void btrfs_write_super(struct super_block *sb)
391d561c025SChris Mason {
39208607c1bSChris Mason 	sb->s_dirt = 0;
393d561c025SChris Mason }
394d561c025SChris Mason 
395a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data)
3962e635a27SChris Mason {
397a061fc8dSChris Mason 	struct btrfs_fs_devices *test_fs_devices = data;
398a061fc8dSChris Mason 	struct btrfs_root *root = btrfs_sb(s);
3994b82d6e4SYan 
400a061fc8dSChris Mason 	return root->fs_info->fs_devices == test_fs_devices;
4014b82d6e4SYan }
4024b82d6e4SYan 
403edf24abeSChristoph Hellwig /*
404edf24abeSChristoph Hellwig  * Find a superblock for the given device / mount point.
405edf24abeSChristoph Hellwig  *
406edf24abeSChristoph Hellwig  * Note:  This is based on get_sb_bdev from fs/super.c with a few additions
407edf24abeSChristoph Hellwig  *	  for multiple device setup.  Make sure to keep it in sync.
408edf24abeSChristoph Hellwig  */
409edf24abeSChristoph Hellwig static int btrfs_get_sb(struct file_system_type *fs_type, int flags,
410edf24abeSChristoph Hellwig 		const char *dev_name, void *data, struct vfsmount *mnt)
4114b82d6e4SYan {
412edf24abeSChristoph Hellwig 	char *subvol_name = NULL;
4134b82d6e4SYan 	struct block_device *bdev = NULL;
4144b82d6e4SYan 	struct super_block *s;
4154b82d6e4SYan 	struct dentry *root;
4168a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices = NULL;
41797288f2cSChristoph Hellwig 	fmode_t mode = FMODE_READ;
4184b82d6e4SYan 	int error = 0;
4194b82d6e4SYan 
42097288f2cSChristoph Hellwig 	if (!(flags & MS_RDONLY))
42197288f2cSChristoph Hellwig 		mode |= FMODE_WRITE;
42297288f2cSChristoph Hellwig 
42397288f2cSChristoph Hellwig 	error = btrfs_parse_early_options(data, mode, fs_type,
42443e570b0SChristoph Hellwig 					  &subvol_name, &fs_devices);
425edf24abeSChristoph Hellwig 	if (error)
4261f483660SShen Feng 		return error;
427edf24abeSChristoph Hellwig 
42897288f2cSChristoph Hellwig 	error = btrfs_scan_one_device(dev_name, mode, fs_type, &fs_devices);
4298a4b83ccSChris Mason 	if (error)
430edf24abeSChristoph Hellwig 		goto error_free_subvol_name;
4314b82d6e4SYan 
43297288f2cSChristoph Hellwig 	error = btrfs_open_devices(fs_devices, mode, fs_type);
4338a4b83ccSChris Mason 	if (error)
434edf24abeSChristoph Hellwig 		goto error_free_subvol_name;
4358a4b83ccSChris Mason 
4362b82032cSYan Zheng 	if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
4372b82032cSYan Zheng 		error = -EACCES;
4382b82032cSYan Zheng 		goto error_close_devices;
4392b82032cSYan Zheng 	}
4402b82032cSYan Zheng 
441dfe25020SChris Mason 	bdev = fs_devices->latest_bdev;
442a061fc8dSChris Mason 	s = sget(fs_type, btrfs_test_super, set_anon_super, fs_devices);
4434b82d6e4SYan 	if (IS_ERR(s))
4444b82d6e4SYan 		goto error_s;
4454b82d6e4SYan 
4464b82d6e4SYan 	if (s->s_root) {
4474b82d6e4SYan 		if ((flags ^ s->s_flags) & MS_RDONLY) {
4484b82d6e4SYan 			up_write(&s->s_umount);
4494b82d6e4SYan 			deactivate_super(s);
4504b82d6e4SYan 			error = -EBUSY;
451c146afadSYan Zheng 			goto error_close_devices;
4524b82d6e4SYan 		}
4534b82d6e4SYan 
4542b82032cSYan Zheng 		btrfs_close_devices(fs_devices);
4554b82d6e4SYan 	} else {
4564b82d6e4SYan 		char b[BDEVNAME_SIZE];
4574b82d6e4SYan 
4584b82d6e4SYan 		s->s_flags = flags;
4594b82d6e4SYan 		strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
4608a4b83ccSChris Mason 		error = btrfs_fill_super(s, fs_devices, data,
4618a4b83ccSChris Mason 					 flags & MS_SILENT ? 1 : 0);
4624b82d6e4SYan 		if (error) {
4634b82d6e4SYan 			up_write(&s->s_umount);
4644b82d6e4SYan 			deactivate_super(s);
4651f483660SShen Feng 			goto error_free_subvol_name;
4664b82d6e4SYan 		}
4674b82d6e4SYan 
468788f20ebSChris Mason 		btrfs_sb(s)->fs_info->bdev_holder = fs_type;
4694b82d6e4SYan 		s->s_flags |= MS_ACTIVE;
4704b82d6e4SYan 	}
4714b82d6e4SYan 
47276fcef19SDavid Woodhouse 	if (!strcmp(subvol_name, "."))
47376fcef19SDavid Woodhouse 		root = dget(s->s_root);
47476fcef19SDavid Woodhouse 	else {
475b48652c1SYan Zheng 		mutex_lock(&s->s_root->d_inode->i_mutex);
476d397712bSChris Mason 		root = lookup_one_len(subvol_name, s->s_root,
477d397712bSChris Mason 				      strlen(subvol_name));
478b48652c1SYan Zheng 		mutex_unlock(&s->s_root->d_inode->i_mutex);
479d397712bSChris Mason 
4804b82d6e4SYan 		if (IS_ERR(root)) {
4814b82d6e4SYan 			up_write(&s->s_umount);
4824b82d6e4SYan 			deactivate_super(s);
4834b82d6e4SYan 			error = PTR_ERR(root);
4841f483660SShen Feng 			goto error_free_subvol_name;
4854b82d6e4SYan 		}
4864b82d6e4SYan 		if (!root->d_inode) {
4874b82d6e4SYan 			dput(root);
4884b82d6e4SYan 			up_write(&s->s_umount);
4894b82d6e4SYan 			deactivate_super(s);
4904b82d6e4SYan 			error = -ENXIO;
4911f483660SShen Feng 			goto error_free_subvol_name;
4924b82d6e4SYan 		}
49376fcef19SDavid Woodhouse 	}
4944b82d6e4SYan 
4954b82d6e4SYan 	mnt->mnt_sb = s;
4964b82d6e4SYan 	mnt->mnt_root = root;
497edf24abeSChristoph Hellwig 
498edf24abeSChristoph Hellwig 	kfree(subvol_name);
4994b82d6e4SYan 	return 0;
5004b82d6e4SYan 
5014b82d6e4SYan error_s:
5024b82d6e4SYan 	error = PTR_ERR(s);
503c146afadSYan Zheng error_close_devices:
5048a4b83ccSChris Mason 	btrfs_close_devices(fs_devices);
505edf24abeSChristoph Hellwig error_free_subvol_name:
506edf24abeSChristoph Hellwig 	kfree(subvol_name);
5074b82d6e4SYan 	return error;
5084b82d6e4SYan }
5092e635a27SChris Mason 
510c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
511c146afadSYan Zheng {
512c146afadSYan Zheng 	struct btrfs_root *root = btrfs_sb(sb);
513c146afadSYan Zheng 	int ret;
514c146afadSYan Zheng 
515c146afadSYan Zheng 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
516c146afadSYan Zheng 		return 0;
517c146afadSYan Zheng 
518c146afadSYan Zheng 	if (*flags & MS_RDONLY) {
519c146afadSYan Zheng 		sb->s_flags |= MS_RDONLY;
520c146afadSYan Zheng 
521c146afadSYan Zheng 		ret =  btrfs_commit_super(root);
522c146afadSYan Zheng 		WARN_ON(ret);
523c146afadSYan Zheng 	} else {
5242b82032cSYan Zheng 		if (root->fs_info->fs_devices->rw_devices == 0)
5252b82032cSYan Zheng 			return -EACCES;
5262b82032cSYan Zheng 
527c146afadSYan Zheng 		if (btrfs_super_log_root(&root->fs_info->super_copy) != 0)
528c146afadSYan Zheng 			return -EINVAL;
529c146afadSYan Zheng 
530c146afadSYan Zheng 		ret = btrfs_cleanup_reloc_trees(root);
531c146afadSYan Zheng 		WARN_ON(ret);
532c146afadSYan Zheng 
533c146afadSYan Zheng 		ret = btrfs_cleanup_fs_roots(root->fs_info);
534c146afadSYan Zheng 		WARN_ON(ret);
535c146afadSYan Zheng 
536c146afadSYan Zheng 		sb->s_flags &= ~MS_RDONLY;
537c146afadSYan Zheng 	}
538c146afadSYan Zheng 
539c146afadSYan Zheng 	return 0;
540c146afadSYan Zheng }
541c146afadSYan Zheng 
5428fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
5438fd17795SChris Mason {
5448fd17795SChris Mason 	struct btrfs_root *root = btrfs_sb(dentry->d_sb);
5454b52dff6SChris Mason 	struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
546db94535dSChris Mason 	int bits = dentry->d_sb->s_blocksize_bits;
5479d03632eSDavid Woodhouse 	__be32 *fsid = (__be32 *)root->fs_info->fsid;
5488fd17795SChris Mason 
5498fd17795SChris Mason 	buf->f_namelen = BTRFS_NAME_LEN;
550db94535dSChris Mason 	buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
551db94535dSChris Mason 	buf->f_bfree = buf->f_blocks -
552db94535dSChris Mason 		(btrfs_super_bytes_used(disk_super) >> bits);
5538fd17795SChris Mason 	buf->f_bavail = buf->f_bfree;
5548fd17795SChris Mason 	buf->f_bsize = dentry->d_sb->s_blocksize;
5558fd17795SChris Mason 	buf->f_type = BTRFS_SUPER_MAGIC;
556d397712bSChris Mason 
5579d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
5589d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
5599d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
5609d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
5619d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
56232d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
56332d48fa1SDavid Woodhouse 	buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
56432d48fa1SDavid Woodhouse 	buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
56532d48fa1SDavid Woodhouse 
5668fd17795SChris Mason 	return 0;
5678fd17795SChris Mason }
568b5133862SChris Mason 
5692e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
5702e635a27SChris Mason 	.owner		= THIS_MODULE,
5712e635a27SChris Mason 	.name		= "btrfs",
5722e635a27SChris Mason 	.get_sb		= btrfs_get_sb,
573a061fc8dSChris Mason 	.kill_sb	= kill_anon_super,
5742e635a27SChris Mason 	.fs_flags	= FS_REQUIRES_DEV,
5752e635a27SChris Mason };
576a9218f6bSChris Mason 
577d352ac68SChris Mason /*
578d352ac68SChris Mason  * used by btrfsctl to scan devices when no FS is mounted
579d352ac68SChris Mason  */
5808a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
5818a4b83ccSChris Mason 				unsigned long arg)
5828a4b83ccSChris Mason {
5838a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
5848a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices;
585c071fcfdSChris Mason 	int ret = -ENOTTY;
5868a4b83ccSChris Mason 
587e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
588e441d54dSChris Mason 		return -EPERM;
589e441d54dSChris Mason 
5908a4b83ccSChris Mason 	vol = kmalloc(sizeof(*vol), GFP_KERNEL);
591*19d00cc1SWang Cong 	if (!vol)
592*19d00cc1SWang Cong 		return -ENOMEM;
593*19d00cc1SWang Cong 
5948a4b83ccSChris Mason 	if (copy_from_user(vol, (void __user *)arg, sizeof(*vol))) {
5958a4b83ccSChris Mason 		ret = -EFAULT;
5968a4b83ccSChris Mason 		goto out;
5978a4b83ccSChris Mason 	}
598c071fcfdSChris Mason 
5998a4b83ccSChris Mason 	switch (cmd) {
6008a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
60197288f2cSChristoph Hellwig 		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
6028a4b83ccSChris Mason 					    &btrfs_fs_type, &fs_devices);
6038a4b83ccSChris Mason 		break;
6048a4b83ccSChris Mason 	}
6058a4b83ccSChris Mason out:
6068a4b83ccSChris Mason 	kfree(vol);
607f819d837SLinda Knippers 	return ret;
6088a4b83ccSChris Mason }
6098a4b83ccSChris Mason 
610ed0dab6bSYan static void btrfs_write_super_lockfs(struct super_block *sb)
611ed0dab6bSYan {
612ed0dab6bSYan 	struct btrfs_root *root = btrfs_sb(sb);
613a74a4b97SChris Mason 	mutex_lock(&root->fs_info->transaction_kthread_mutex);
614a74a4b97SChris Mason 	mutex_lock(&root->fs_info->cleaner_mutex);
615ed0dab6bSYan }
616ed0dab6bSYan 
617ed0dab6bSYan static void btrfs_unlockfs(struct super_block *sb)
618ed0dab6bSYan {
619ed0dab6bSYan 	struct btrfs_root *root = btrfs_sb(sb);
620a74a4b97SChris Mason 	mutex_unlock(&root->fs_info->cleaner_mutex);
621a74a4b97SChris Mason 	mutex_unlock(&root->fs_info->transaction_kthread_mutex);
622ed0dab6bSYan }
6232e635a27SChris Mason 
624e20d96d6SChris Mason static struct super_operations btrfs_super_ops = {
625134e9731SChris Mason 	.delete_inode	= btrfs_delete_inode,
626e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
627d5719762SChris Mason 	.write_super	= btrfs_write_super,
628d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
6296885f308SChris Mason 	.show_options	= generic_show_options,
6304730a4bcSChris Mason 	.write_inode	= btrfs_write_inode,
631b5133862SChris Mason 	.dirty_inode	= btrfs_dirty_inode,
6322c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
6332c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
6348fd17795SChris Mason 	.statfs		= btrfs_statfs,
635c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
636ed0dab6bSYan 	.write_super_lockfs = btrfs_write_super_lockfs,
637ed0dab6bSYan 	.unlockfs	= btrfs_unlockfs,
638e20d96d6SChris Mason };
639a9218f6bSChris Mason 
640a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
641a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
642a9218f6bSChris Mason 	.compat_ioctl = btrfs_control_ioctl,
643a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
644a9218f6bSChris Mason };
645a9218f6bSChris Mason 
646a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
647a9218f6bSChris Mason 	.minor		= MISC_DYNAMIC_MINOR,
648a9218f6bSChris Mason 	.name		= "btrfs-control",
649a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
650a9218f6bSChris Mason };
651a9218f6bSChris Mason 
652a9218f6bSChris Mason static int btrfs_interface_init(void)
653a9218f6bSChris Mason {
654a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
655a9218f6bSChris Mason }
656a9218f6bSChris Mason 
657b2950863SChristoph Hellwig static void btrfs_interface_exit(void)
658a9218f6bSChris Mason {
659a9218f6bSChris Mason 	if (misc_deregister(&btrfs_misc) < 0)
660d397712bSChris Mason 		printk(KERN_INFO "misc_deregister failed for control device");
661a9218f6bSChris Mason }
662a9218f6bSChris Mason 
6632e635a27SChris Mason static int __init init_btrfs_fs(void)
6642e635a27SChris Mason {
6652c90e5d6SChris Mason 	int err;
66658176a96SJosef Bacik 
66758176a96SJosef Bacik 	err = btrfs_init_sysfs();
66858176a96SJosef Bacik 	if (err)
66958176a96SJosef Bacik 		return err;
67058176a96SJosef Bacik 
67139279cc3SChris Mason 	err = btrfs_init_cachep();
6722c90e5d6SChris Mason 	if (err)
673a74a4b97SChris Mason 		goto free_sysfs;
674d1310b2eSChris Mason 
675d1310b2eSChris Mason 	err = extent_io_init();
6762f4cbe64SWyatt Banks 	if (err)
6772f4cbe64SWyatt Banks 		goto free_cachep;
6782f4cbe64SWyatt Banks 
679d1310b2eSChris Mason 	err = extent_map_init();
680d1310b2eSChris Mason 	if (err)
681d1310b2eSChris Mason 		goto free_extent_io;
682d1310b2eSChris Mason 
683a9218f6bSChris Mason 	err = btrfs_interface_init();
6842f4cbe64SWyatt Banks 	if (err)
6852f4cbe64SWyatt Banks 		goto free_extent_map;
686c8b97818SChris Mason 
687a9218f6bSChris Mason 	err = register_filesystem(&btrfs_fs_type);
688a9218f6bSChris Mason 	if (err)
689a9218f6bSChris Mason 		goto unregister_ioctl;
690b3c3da71SChris Mason 
691b3c3da71SChris Mason 	printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
6922f4cbe64SWyatt Banks 	return 0;
6932f4cbe64SWyatt Banks 
694a9218f6bSChris Mason unregister_ioctl:
695a9218f6bSChris Mason 	btrfs_interface_exit();
6962f4cbe64SWyatt Banks free_extent_map:
6972f4cbe64SWyatt Banks 	extent_map_exit();
698d1310b2eSChris Mason free_extent_io:
699d1310b2eSChris Mason 	extent_io_exit();
7002f4cbe64SWyatt Banks free_cachep:
7012f4cbe64SWyatt Banks 	btrfs_destroy_cachep();
702a74a4b97SChris Mason free_sysfs:
7032f4cbe64SWyatt Banks 	btrfs_exit_sysfs();
7042c90e5d6SChris Mason 	return err;
7052e635a27SChris Mason }
7062e635a27SChris Mason 
7072e635a27SChris Mason static void __exit exit_btrfs_fs(void)
7082e635a27SChris Mason {
70939279cc3SChris Mason 	btrfs_destroy_cachep();
710a52d9a80SChris Mason 	extent_map_exit();
711d1310b2eSChris Mason 	extent_io_exit();
712a9218f6bSChris Mason 	btrfs_interface_exit();
7132e635a27SChris Mason 	unregister_filesystem(&btrfs_fs_type);
71458176a96SJosef Bacik 	btrfs_exit_sysfs();
7158a4b83ccSChris Mason 	btrfs_cleanup_fs_uuids();
716c8b97818SChris Mason 	btrfs_zlib_exit();
7172e635a27SChris Mason }
7182e635a27SChris Mason 
7192e635a27SChris Mason module_init(init_btrfs_fs)
7202e635a27SChris Mason module_exit(exit_btrfs_fs)
7212e635a27SChris Mason 
7222e635a27SChris Mason MODULE_LICENSE("GPL");
723