xref: /linux/fs/btrfs/super.c (revision b2950863c61bc24cf0f63bc05947d9d50663c4c0)
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);
6158176a96SJosef Bacik 	struct btrfs_fs_info *fs = root->fs_info;
62e20d96d6SChris Mason 	int ret;
63e20d96d6SChris Mason 
64e20d96d6SChris Mason 	ret = close_ctree(root);
65e20d96d6SChris Mason 	if (ret) {
66e20d96d6SChris Mason 		printk("close ctree returns %d\n", ret);
67e20d96d6SChris Mason 	}
6858176a96SJosef Bacik 	btrfs_sysfs_del_super(fs);
69e20d96d6SChris Mason 	sb->s_fs_info = NULL;
70e20d96d6SChris Mason }
712e635a27SChris Mason 
7295e05289SChris Mason enum {
7343e570b0SChristoph Hellwig 	Opt_degraded, Opt_subvol, Opt_device, Opt_nodatasum, Opt_nodatacow,
74dfe25020SChris Mason 	Opt_max_extent, Opt_max_inline, Opt_alloc_start, Opt_nobarrier,
75c8b97818SChris Mason 	Opt_ssd, Opt_thread_pool, Opt_noacl,  Opt_compress, Opt_err,
7695e05289SChris Mason };
7795e05289SChris Mason 
7895e05289SChris Mason static match_table_t tokens = {
79dfe25020SChris Mason 	{Opt_degraded, "degraded"},
8095e05289SChris Mason 	{Opt_subvol, "subvol=%s"},
8143e570b0SChristoph Hellwig 	{Opt_device, "device=%s"},
82b6cda9bcSChris Mason 	{Opt_nodatasum, "nodatasum"},
83be20aa9dSChris Mason 	{Opt_nodatacow, "nodatacow"},
8421ad10cfSChris Mason 	{Opt_nobarrier, "nobarrier"},
85c59f8951SChris Mason 	{Opt_max_extent, "max_extent=%s"},
866f568d35SChris Mason 	{Opt_max_inline, "max_inline=%s"},
878f662a76SChris Mason 	{Opt_alloc_start, "alloc_start=%s"},
884543df7eSChris Mason 	{Opt_thread_pool, "thread_pool=%d"},
89c8b97818SChris Mason 	{Opt_compress, "compress"},
90e18e4809SChris Mason 	{Opt_ssd, "ssd"},
9133268eafSJosef Bacik 	{Opt_noacl, "noacl"},
9233268eafSJosef Bacik 	{Opt_err, NULL},
9395e05289SChris Mason };
9495e05289SChris Mason 
95edbd8d4eSChris Mason u64 btrfs_parse_size(char *str)
96c59f8951SChris Mason {
97edbd8d4eSChris Mason 	u64 res;
98c59f8951SChris Mason 	int mult = 1;
99c59f8951SChris Mason 	char *end;
100c59f8951SChris Mason 	char last;
101c59f8951SChris Mason 
102c59f8951SChris Mason 	res = simple_strtoul(str, &end, 10);
103c59f8951SChris Mason 
104c59f8951SChris Mason 	last = end[0];
105c59f8951SChris Mason 	if (isalpha(last)) {
106c59f8951SChris Mason 		last = tolower(last);
107c59f8951SChris Mason 		switch (last) {
108c59f8951SChris Mason 		case 'g':
109c59f8951SChris Mason 			mult *= 1024;
110c59f8951SChris Mason 		case 'm':
111c59f8951SChris Mason 			mult *= 1024;
112c59f8951SChris Mason 		case 'k':
113c59f8951SChris Mason 			mult *= 1024;
114c59f8951SChris Mason 		}
115c59f8951SChris Mason 		res = res * mult;
116c59f8951SChris Mason 	}
117c59f8951SChris Mason 	return res;
118c59f8951SChris Mason }
119c59f8951SChris Mason 
120edf24abeSChristoph Hellwig /*
121edf24abeSChristoph Hellwig  * Regular mount options parser.  Everything that is needed only when
122edf24abeSChristoph Hellwig  * reading in a new superblock is parsed here.
123edf24abeSChristoph Hellwig  */
124edf24abeSChristoph Hellwig int btrfs_parse_options(struct btrfs_root *root, char *options)
12595e05289SChris Mason {
126edf24abeSChristoph Hellwig 	struct btrfs_fs_info *info = root->fs_info;
12795e05289SChris Mason 	substring_t args[MAX_OPT_ARGS];
128edf24abeSChristoph Hellwig 	char *p, *num;
1294543df7eSChris Mason 	int intarg;
130b6cda9bcSChris Mason 
13195e05289SChris Mason 	if (!options)
132edf24abeSChristoph Hellwig 		return 0;
13395e05289SChris Mason 
134be20aa9dSChris Mason 	/*
135be20aa9dSChris Mason 	 * strsep changes the string, duplicate it because parse_options
136be20aa9dSChris Mason 	 * gets called twice
137be20aa9dSChris Mason 	 */
138be20aa9dSChris Mason 	options = kstrdup(options, GFP_NOFS);
139be20aa9dSChris Mason 	if (!options)
140be20aa9dSChris Mason 		return -ENOMEM;
141be20aa9dSChris Mason 
142be20aa9dSChris Mason 
14395e05289SChris Mason 	while ((p = strsep(&options, ",")) != NULL) {
14495e05289SChris Mason 		int token;
14595e05289SChris Mason 		if (!*p)
14695e05289SChris Mason 			continue;
14795e05289SChris Mason 
14895e05289SChris Mason 		token = match_token(p, tokens, args);
14995e05289SChris Mason 		switch (token) {
150dfe25020SChris Mason 		case Opt_degraded:
151edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: allowing degraded mounts\n");
152dfe25020SChris Mason 			btrfs_set_opt(info->mount_opt, DEGRADED);
153dfe25020SChris Mason 			break;
15495e05289SChris Mason 		case Opt_subvol:
15543e570b0SChristoph Hellwig 		case Opt_device:
156edf24abeSChristoph Hellwig 			/*
15743e570b0SChristoph Hellwig 			 * These are parsed by btrfs_parse_early_options
158edf24abeSChristoph Hellwig 			 * and can be happily ignored here.
159edf24abeSChristoph Hellwig 			 */
16095e05289SChris Mason 			break;
161b6cda9bcSChris Mason 		case Opt_nodatasum:
162edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: setting nodatacsum\n");
163b6cda9bcSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
164be20aa9dSChris Mason 			break;
165be20aa9dSChris Mason 		case Opt_nodatacow:
166edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: setting nodatacow\n");
167be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATACOW);
168be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
169b6cda9bcSChris Mason 			break;
170c8b97818SChris Mason 		case Opt_compress:
171c8b97818SChris Mason 			printk(KERN_INFO "btrfs: use compression\n");
172c8b97818SChris Mason 			btrfs_set_opt(info->mount_opt, COMPRESS);
173c8b97818SChris Mason 			break;
174e18e4809SChris Mason 		case Opt_ssd:
175edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
176e18e4809SChris Mason 			btrfs_set_opt(info->mount_opt, SSD);
177e18e4809SChris Mason 			break;
17821ad10cfSChris Mason 		case Opt_nobarrier:
179edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: turning off barriers\n");
18021ad10cfSChris Mason 			btrfs_set_opt(info->mount_opt, NOBARRIER);
18121ad10cfSChris Mason 			break;
1824543df7eSChris Mason 		case Opt_thread_pool:
1834543df7eSChris Mason 			intarg = 0;
1844543df7eSChris Mason 			match_int(&args[0], &intarg);
1854543df7eSChris Mason 			if (intarg) {
1864543df7eSChris Mason 				info->thread_pool_size = intarg;
1874543df7eSChris Mason 				printk(KERN_INFO "btrfs: thread pool %d\n",
1884543df7eSChris Mason 				       info->thread_pool_size);
1894543df7eSChris Mason 			}
1904543df7eSChris Mason 			break;
191c59f8951SChris Mason 		case Opt_max_extent:
192edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
193c59f8951SChris Mason 			if (num) {
194edf24abeSChristoph Hellwig 				info->max_extent = btrfs_parse_size(num);
195c59f8951SChris Mason 				kfree(num);
196c59f8951SChris Mason 
197c59f8951SChris Mason 				info->max_extent = max_t(u64,
198edf24abeSChristoph Hellwig 					info->max_extent, root->sectorsize);
199edf24abeSChristoph Hellwig 				printk(KERN_INFO "btrfs: max_extent at %llu\n",
200c59f8951SChris Mason 				       info->max_extent);
201c59f8951SChris Mason 			}
202c59f8951SChris Mason 			break;
2036f568d35SChris Mason 		case Opt_max_inline:
204edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
2056f568d35SChris Mason 			if (num) {
206edf24abeSChristoph Hellwig 				info->max_inline = btrfs_parse_size(num);
2076f568d35SChris Mason 				kfree(num);
2086f568d35SChris Mason 
20915ada040SChris Mason 				if (info->max_inline) {
2106f568d35SChris Mason 					info->max_inline = max_t(u64,
21115ada040SChris Mason 						info->max_inline,
21215ada040SChris Mason 						root->sectorsize);
21315ada040SChris Mason 				}
214edf24abeSChristoph Hellwig 				printk(KERN_INFO "btrfs: max_inline at %llu\n",
2156f568d35SChris Mason 					info->max_inline);
2166f568d35SChris Mason 			}
2176f568d35SChris Mason 			break;
2188f662a76SChris Mason 		case Opt_alloc_start:
219edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
2208f662a76SChris Mason 			if (num) {
221edf24abeSChristoph Hellwig 				info->alloc_start = btrfs_parse_size(num);
2228f662a76SChris Mason 				kfree(num);
223edf24abeSChristoph Hellwig 				printk(KERN_INFO
224edf24abeSChristoph Hellwig 					"btrfs: allocations start at %llu\n",
225edf24abeSChristoph Hellwig 					info->alloc_start);
2268f662a76SChris Mason 			}
2278f662a76SChris Mason 			break;
22833268eafSJosef Bacik 		case Opt_noacl:
22933268eafSJosef Bacik 			root->fs_info->sb->s_flags &= ~MS_POSIXACL;
23033268eafSJosef Bacik 			break;
23195e05289SChris Mason 		default:
232be20aa9dSChris Mason 			break;
23395e05289SChris Mason 		}
23495e05289SChris Mason 	}
235be20aa9dSChris Mason 	kfree(options);
236edf24abeSChristoph Hellwig 	return 0;
237edf24abeSChristoph Hellwig }
238edf24abeSChristoph Hellwig 
239edf24abeSChristoph Hellwig /*
240edf24abeSChristoph Hellwig  * Parse mount options that are required early in the mount process.
241edf24abeSChristoph Hellwig  *
242edf24abeSChristoph Hellwig  * All other options will be parsed on much later in the mount process and
243edf24abeSChristoph Hellwig  * only when we need to allocate a new super block.
244edf24abeSChristoph Hellwig  */
24543e570b0SChristoph Hellwig static int btrfs_parse_early_options(const char *options, int flags,
24643e570b0SChristoph Hellwig 		void *holder, char **subvol_name,
24743e570b0SChristoph Hellwig 		struct btrfs_fs_devices **fs_devices)
248edf24abeSChristoph Hellwig {
249edf24abeSChristoph Hellwig 	substring_t args[MAX_OPT_ARGS];
250edf24abeSChristoph Hellwig 	char *opts, *p;
251edf24abeSChristoph Hellwig 	int error = 0;
252edf24abeSChristoph Hellwig 
253edf24abeSChristoph Hellwig 	if (!options)
254edf24abeSChristoph Hellwig 		goto out;
255edf24abeSChristoph Hellwig 
256edf24abeSChristoph Hellwig 	/*
257edf24abeSChristoph Hellwig 	 * strsep changes the string, duplicate it because parse_options
258edf24abeSChristoph Hellwig 	 * gets called twice
259edf24abeSChristoph Hellwig 	 */
260edf24abeSChristoph Hellwig 	opts = kstrdup(options, GFP_KERNEL);
261edf24abeSChristoph Hellwig 	if (!opts)
262edf24abeSChristoph Hellwig 		return -ENOMEM;
263edf24abeSChristoph Hellwig 
264edf24abeSChristoph Hellwig 	while ((p = strsep(&opts, ",")) != NULL) {
265edf24abeSChristoph Hellwig 		int token;
266edf24abeSChristoph Hellwig 		if (!*p)
267edf24abeSChristoph Hellwig 			continue;
268edf24abeSChristoph Hellwig 
269edf24abeSChristoph Hellwig 		token = match_token(p, tokens, args);
270edf24abeSChristoph Hellwig 		switch (token) {
271edf24abeSChristoph Hellwig 		case Opt_subvol:
272edf24abeSChristoph Hellwig 			*subvol_name = match_strdup(&args[0]);
273edf24abeSChristoph Hellwig 			break;
27443e570b0SChristoph Hellwig 		case Opt_device:
27543e570b0SChristoph Hellwig 			error = btrfs_scan_one_device(match_strdup(&args[0]),
27643e570b0SChristoph Hellwig 					flags, holder, fs_devices);
27743e570b0SChristoph Hellwig 			if (error)
27843e570b0SChristoph Hellwig 				goto out_free_opts;
27943e570b0SChristoph Hellwig 			break;
280edf24abeSChristoph Hellwig 		default:
281edf24abeSChristoph Hellwig 			break;
282edf24abeSChristoph Hellwig 		}
283edf24abeSChristoph Hellwig 	}
284edf24abeSChristoph Hellwig 
28543e570b0SChristoph Hellwig  out_free_opts:
286edf24abeSChristoph Hellwig 	kfree(opts);
287edf24abeSChristoph Hellwig  out:
288edf24abeSChristoph Hellwig 	/*
289edf24abeSChristoph Hellwig 	 * If no subvolume name is specified we use the default one.  Allocate
2903de4586cSChris Mason 	 * a copy of the string "." here so that code later in the
291edf24abeSChristoph Hellwig 	 * mount path doesn't care if it's the default volume or another one.
292edf24abeSChristoph Hellwig 	 */
293edf24abeSChristoph Hellwig 	if (!*subvol_name) {
2943de4586cSChris Mason 		*subvol_name = kstrdup(".", GFP_KERNEL);
295edf24abeSChristoph Hellwig 		if (!*subvol_name)
296edf24abeSChristoph Hellwig 			return -ENOMEM;
297edf24abeSChristoph Hellwig 	}
298edf24abeSChristoph Hellwig 	return error;
29995e05289SChris Mason }
30095e05289SChris Mason 
3018a4b83ccSChris Mason static int btrfs_fill_super(struct super_block * sb,
3028a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
3038a4b83ccSChris Mason 			    void * data, int silent)
3042e635a27SChris Mason {
3052e635a27SChris Mason 	struct inode * inode;
306e20d96d6SChris Mason 	struct dentry * root_dentry;
307e20d96d6SChris Mason 	struct btrfs_super_block *disk_super;
3080f7d52f4SChris Mason 	struct btrfs_root *tree_root;
309d6e4a428SChris Mason 	struct btrfs_inode *bi;
31039279cc3SChris Mason 	int err;
3112e635a27SChris Mason 
3122e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
3132e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
314e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
315be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
3165103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
3172e635a27SChris Mason 	sb->s_time_gran = 1;
31833268eafSJosef Bacik 	sb->s_flags |= MS_POSIXACL;
319e20d96d6SChris Mason 
320dfe25020SChris Mason 	tree_root = open_ctree(sb, fs_devices, (char *)data);
321d98237b3SChris Mason 
322e58ca020SYan 	if (IS_ERR(tree_root)) {
323e20d96d6SChris Mason 		printk("btrfs: open_ctree failed\n");
324e58ca020SYan 		return PTR_ERR(tree_root);
325e20d96d6SChris Mason 	}
3260f7d52f4SChris Mason 	sb->s_fs_info = tree_root;
3275f39d397SChris Mason 	disk_super = &tree_root->fs_info->super_copy;
3283de4586cSChris Mason 	inode = btrfs_iget_locked(sb, BTRFS_FIRST_FREE_OBJECTID,
3293de4586cSChris Mason 				  tree_root->fs_info->fs_root);
330d6e4a428SChris Mason 	bi = BTRFS_I(inode);
331d6e4a428SChris Mason 	bi->location.objectid = inode->i_ino;
332d6e4a428SChris Mason 	bi->location.offset = 0;
3333de4586cSChris Mason 	bi->root = tree_root->fs_info->fs_root;
334b888db2bSChris Mason 
335d6e4a428SChris Mason 	btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
336d6e4a428SChris Mason 
33739279cc3SChris Mason 	if (!inode) {
33839279cc3SChris Mason 		err = -ENOMEM;
33939279cc3SChris Mason 		goto fail_close;
34039279cc3SChris Mason 	}
341e20d96d6SChris Mason 	if (inode->i_state & I_NEW) {
342e20d96d6SChris Mason 		btrfs_read_locked_inode(inode);
343e20d96d6SChris Mason 		unlock_new_inode(inode);
344e20d96d6SChris Mason 	}
3452e635a27SChris Mason 
346e20d96d6SChris Mason 	root_dentry = d_alloc_root(inode);
347e20d96d6SChris Mason 	if (!root_dentry) {
3482e635a27SChris Mason 		iput(inode);
34939279cc3SChris Mason 		err = -ENOMEM;
35039279cc3SChris Mason 		goto fail_close;
3512e635a27SChris Mason 	}
35258176a96SJosef Bacik 
35358176a96SJosef Bacik 	/* this does the super kobj at the same time */
35458176a96SJosef Bacik 	err = btrfs_sysfs_add_super(tree_root->fs_info);
35558176a96SJosef Bacik 	if (err)
35658176a96SJosef Bacik 		goto fail_close;
35758176a96SJosef Bacik 
358e20d96d6SChris Mason 	sb->s_root = root_dentry;
3596885f308SChris Mason 
3606885f308SChris Mason 	save_mount_options(sb, data);
3612e635a27SChris Mason 	return 0;
3622e635a27SChris Mason 
36339279cc3SChris Mason fail_close:
36439279cc3SChris Mason 	close_ctree(tree_root);
365d5719762SChris Mason 	return err;
366d5719762SChris Mason }
367d5719762SChris Mason 
3686bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
369d5719762SChris Mason {
370d5719762SChris Mason 	struct btrfs_trans_handle *trans;
371d5719762SChris Mason 	struct btrfs_root *root;
372d5719762SChris Mason 	int ret;
373d98237b3SChris Mason 	root = btrfs_sb(sb);
374df2ce34cSChris Mason 
375c146afadSYan Zheng 	if (sb->s_flags & MS_RDONLY)
376c146afadSYan Zheng 		return 0;
377c146afadSYan Zheng 
378d5719762SChris Mason 	sb->s_dirt = 0;
379d561c025SChris Mason 	if (!wait) {
3807cfcc17eSChris Mason 		filemap_flush(root->fs_info->btree_inode->i_mapping);
381df2ce34cSChris Mason 		return 0;
382d561c025SChris Mason 	}
383771ed689SChris Mason 
384771ed689SChris Mason 	btrfs_start_delalloc_inodes(root);
385771ed689SChris Mason 	btrfs_wait_ordered_extents(root, 0);
386771ed689SChris Mason 
387e9d0b13bSChris Mason 	btrfs_clean_old_snapshots(root);
388d5719762SChris Mason 	trans = btrfs_start_transaction(root, 1);
389d5719762SChris Mason 	ret = btrfs_commit_transaction(trans, root);
390d5719762SChris Mason 	sb->s_dirt = 0;
39154aa1f4dSChris Mason 	return ret;
392d5719762SChris Mason }
393d5719762SChris Mason 
394d561c025SChris Mason static void btrfs_write_super(struct super_block *sb)
395d561c025SChris Mason {
39608607c1bSChris Mason 	sb->s_dirt = 0;
397d561c025SChris Mason }
398d561c025SChris Mason 
399a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data)
4002e635a27SChris Mason {
401a061fc8dSChris Mason 	struct btrfs_fs_devices *test_fs_devices = data;
402a061fc8dSChris Mason 	struct btrfs_root *root = btrfs_sb(s);
4034b82d6e4SYan 
404a061fc8dSChris Mason 	return root->fs_info->fs_devices == test_fs_devices;
4054b82d6e4SYan }
4064b82d6e4SYan 
407edf24abeSChristoph Hellwig /*
408edf24abeSChristoph Hellwig  * Find a superblock for the given device / mount point.
409edf24abeSChristoph Hellwig  *
410edf24abeSChristoph Hellwig  * Note:  This is based on get_sb_bdev from fs/super.c with a few additions
411edf24abeSChristoph Hellwig  *	  for multiple device setup.  Make sure to keep it in sync.
412edf24abeSChristoph Hellwig  */
413edf24abeSChristoph Hellwig static int btrfs_get_sb(struct file_system_type *fs_type, int flags,
414edf24abeSChristoph Hellwig 		const char *dev_name, void *data, struct vfsmount *mnt)
4154b82d6e4SYan {
416edf24abeSChristoph Hellwig 	char *subvol_name = NULL;
4174b82d6e4SYan 	struct block_device *bdev = NULL;
4184b82d6e4SYan 	struct super_block *s;
4194b82d6e4SYan 	struct dentry *root;
4208a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices = NULL;
4214b82d6e4SYan 	int error = 0;
4224b82d6e4SYan 
42343e570b0SChristoph Hellwig 	error = btrfs_parse_early_options(data, flags, fs_type,
42443e570b0SChristoph Hellwig 					  &subvol_name, &fs_devices);
425edf24abeSChristoph Hellwig 	if (error)
426edf24abeSChristoph Hellwig 		goto error;
427edf24abeSChristoph Hellwig 
4288a4b83ccSChris Mason 	error = btrfs_scan_one_device(dev_name, flags, fs_type, &fs_devices);
4298a4b83ccSChris Mason 	if (error)
430edf24abeSChristoph Hellwig 		goto error_free_subvol_name;
4314b82d6e4SYan 
4328a4b83ccSChris Mason 	error = btrfs_open_devices(fs_devices, flags, 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);
4654b82d6e4SYan 			goto error;
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);
476edf24abeSChristoph Hellwig 		root = lookup_one_len(subvol_name, s->s_root, strlen(subvol_name));
477b48652c1SYan Zheng 		mutex_unlock(&s->s_root->d_inode->i_mutex);
4784b82d6e4SYan 		if (IS_ERR(root)) {
4794b82d6e4SYan 			up_write(&s->s_umount);
4804b82d6e4SYan 			deactivate_super(s);
4814b82d6e4SYan 			error = PTR_ERR(root);
4824b82d6e4SYan 			goto error;
4834b82d6e4SYan 		}
4844b82d6e4SYan 		if (!root->d_inode) {
4854b82d6e4SYan 			dput(root);
4864b82d6e4SYan 			up_write(&s->s_umount);
4874b82d6e4SYan 			deactivate_super(s);
4884b82d6e4SYan 			error = -ENXIO;
4894b82d6e4SYan 			goto error;
4904b82d6e4SYan 		}
49176fcef19SDavid Woodhouse 	}
4924b82d6e4SYan 
4934b82d6e4SYan 	mnt->mnt_sb = s;
4944b82d6e4SYan 	mnt->mnt_root = root;
495edf24abeSChristoph Hellwig 
496edf24abeSChristoph Hellwig 	kfree(subvol_name);
4974b82d6e4SYan 	return 0;
4984b82d6e4SYan 
4994b82d6e4SYan error_s:
5004b82d6e4SYan 	error = PTR_ERR(s);
501c146afadSYan Zheng error_close_devices:
5028a4b83ccSChris Mason 	btrfs_close_devices(fs_devices);
503edf24abeSChristoph Hellwig error_free_subvol_name:
504edf24abeSChristoph Hellwig 	kfree(subvol_name);
5054b82d6e4SYan error:
5064b82d6e4SYan 	return error;
5074b82d6e4SYan }
5082e635a27SChris Mason 
509c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
510c146afadSYan Zheng {
511c146afadSYan Zheng 	struct btrfs_root *root = btrfs_sb(sb);
512c146afadSYan Zheng 	int ret;
513c146afadSYan Zheng 
514c146afadSYan Zheng 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
515c146afadSYan Zheng 		return 0;
516c146afadSYan Zheng 
517c146afadSYan Zheng 	if (*flags & MS_RDONLY) {
518c146afadSYan Zheng 		sb->s_flags |= MS_RDONLY;
519c146afadSYan Zheng 
520c146afadSYan Zheng 		ret =  btrfs_commit_super(root);
521c146afadSYan Zheng 		WARN_ON(ret);
522c146afadSYan Zheng 	} else {
5232b82032cSYan Zheng 		if (root->fs_info->fs_devices->rw_devices == 0)
5242b82032cSYan Zheng 			return -EACCES;
5252b82032cSYan Zheng 
526c146afadSYan Zheng 		if (btrfs_super_log_root(&root->fs_info->super_copy) != 0)
527c146afadSYan Zheng 			return -EINVAL;
528c146afadSYan Zheng 
529c146afadSYan Zheng 		ret = btrfs_cleanup_reloc_trees(root);
530c146afadSYan Zheng 		WARN_ON(ret);
531c146afadSYan Zheng 
532c146afadSYan Zheng 		ret = btrfs_cleanup_fs_roots(root->fs_info);
533c146afadSYan Zheng 		WARN_ON(ret);
534c146afadSYan Zheng 
535c146afadSYan Zheng 		sb->s_flags &= ~MS_RDONLY;
536c146afadSYan Zheng 	}
537c146afadSYan Zheng 
538c146afadSYan Zheng 	return 0;
539c146afadSYan Zheng }
540c146afadSYan Zheng 
5418fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
5428fd17795SChris Mason {
5438fd17795SChris Mason 	struct btrfs_root *root = btrfs_sb(dentry->d_sb);
5444b52dff6SChris Mason 	struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
545db94535dSChris Mason 	int bits = dentry->d_sb->s_blocksize_bits;
5469d03632eSDavid Woodhouse 	__be32 *fsid = (__be32 *)root->fs_info->fsid;
5478fd17795SChris Mason 
5488fd17795SChris Mason 	buf->f_namelen = BTRFS_NAME_LEN;
549db94535dSChris Mason 	buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
550db94535dSChris Mason 	buf->f_bfree = buf->f_blocks -
551db94535dSChris Mason 		(btrfs_super_bytes_used(disk_super) >> bits);
5528fd17795SChris Mason 	buf->f_bavail = buf->f_bfree;
5538fd17795SChris Mason 	buf->f_bsize = dentry->d_sb->s_blocksize;
5548fd17795SChris Mason 	buf->f_type = BTRFS_SUPER_MAGIC;
5559d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
5569d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
5579d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
5589d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
5599d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
56032d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
56132d48fa1SDavid Woodhouse 	buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
56232d48fa1SDavid Woodhouse 	buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
56332d48fa1SDavid Woodhouse 
5648fd17795SChris Mason 	return 0;
5658fd17795SChris Mason }
566b5133862SChris Mason 
5672e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
5682e635a27SChris Mason 	.owner		= THIS_MODULE,
5692e635a27SChris Mason 	.name		= "btrfs",
5702e635a27SChris Mason 	.get_sb		= btrfs_get_sb,
571a061fc8dSChris Mason 	.kill_sb	= kill_anon_super,
5722e635a27SChris Mason 	.fs_flags	= FS_REQUIRES_DEV,
5732e635a27SChris Mason };
574a9218f6bSChris Mason 
575d352ac68SChris Mason /*
576d352ac68SChris Mason  * used by btrfsctl to scan devices when no FS is mounted
577d352ac68SChris Mason  */
5788a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
5798a4b83ccSChris Mason 				unsigned long arg)
5808a4b83ccSChris Mason {
5818a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
5828a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices;
583f819d837SLinda Knippers 	int ret = 0;
5848a4b83ccSChris Mason 	int len;
5858a4b83ccSChris Mason 
5868a4b83ccSChris Mason 	vol = kmalloc(sizeof(*vol), GFP_KERNEL);
5878a4b83ccSChris Mason 	if (copy_from_user(vol, (void __user *)arg, sizeof(*vol))) {
5888a4b83ccSChris Mason 		ret = -EFAULT;
5898a4b83ccSChris Mason 		goto out;
5908a4b83ccSChris Mason 	}
5918a4b83ccSChris Mason 	len = strnlen(vol->name, BTRFS_PATH_NAME_MAX);
5928a4b83ccSChris Mason 	switch (cmd) {
5938a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
5948a4b83ccSChris Mason 		ret = btrfs_scan_one_device(vol->name, MS_RDONLY,
5958a4b83ccSChris Mason 					    &btrfs_fs_type, &fs_devices);
5968a4b83ccSChris Mason 		break;
5978a4b83ccSChris Mason 	}
5988a4b83ccSChris Mason out:
5998a4b83ccSChris Mason 	kfree(vol);
600f819d837SLinda Knippers 	return ret;
6018a4b83ccSChris Mason }
6028a4b83ccSChris Mason 
603ed0dab6bSYan static void btrfs_write_super_lockfs(struct super_block *sb)
604ed0dab6bSYan {
605ed0dab6bSYan 	struct btrfs_root *root = btrfs_sb(sb);
606a74a4b97SChris Mason 	mutex_lock(&root->fs_info->transaction_kthread_mutex);
607a74a4b97SChris Mason 	mutex_lock(&root->fs_info->cleaner_mutex);
608ed0dab6bSYan }
609ed0dab6bSYan 
610ed0dab6bSYan static void btrfs_unlockfs(struct super_block *sb)
611ed0dab6bSYan {
612ed0dab6bSYan 	struct btrfs_root *root = btrfs_sb(sb);
613a74a4b97SChris Mason 	mutex_unlock(&root->fs_info->cleaner_mutex);
614a74a4b97SChris Mason 	mutex_unlock(&root->fs_info->transaction_kthread_mutex);
615ed0dab6bSYan }
6162e635a27SChris Mason 
617e20d96d6SChris Mason static struct super_operations btrfs_super_ops = {
618134e9731SChris Mason 	.delete_inode	= btrfs_delete_inode,
619e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
620d5719762SChris Mason 	.write_super	= btrfs_write_super,
621d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
6226885f308SChris Mason 	.show_options	= generic_show_options,
6234730a4bcSChris Mason 	.write_inode	= btrfs_write_inode,
624b5133862SChris Mason 	.dirty_inode	= btrfs_dirty_inode,
6252c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
6262c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
6278fd17795SChris Mason 	.statfs		= btrfs_statfs,
628c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
629ed0dab6bSYan 	.write_super_lockfs = btrfs_write_super_lockfs,
630ed0dab6bSYan 	.unlockfs	= btrfs_unlockfs,
631e20d96d6SChris Mason };
632a9218f6bSChris Mason 
633a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
634a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
635a9218f6bSChris Mason 	.compat_ioctl = btrfs_control_ioctl,
636a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
637a9218f6bSChris Mason };
638a9218f6bSChris Mason 
639a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
640a9218f6bSChris Mason 	.minor		= MISC_DYNAMIC_MINOR,
641a9218f6bSChris Mason 	.name		= "btrfs-control",
642a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
643a9218f6bSChris Mason };
644a9218f6bSChris Mason 
645a9218f6bSChris Mason static int btrfs_interface_init(void)
646a9218f6bSChris Mason {
647a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
648a9218f6bSChris Mason }
649a9218f6bSChris Mason 
650*b2950863SChristoph Hellwig static void btrfs_interface_exit(void)
651a9218f6bSChris Mason {
652a9218f6bSChris Mason 	if (misc_deregister(&btrfs_misc) < 0)
653a9218f6bSChris Mason 		printk("misc_deregister failed for control device");
654a9218f6bSChris Mason }
655a9218f6bSChris Mason 
6562e635a27SChris Mason static int __init init_btrfs_fs(void)
6572e635a27SChris Mason {
6582c90e5d6SChris Mason 	int err;
65958176a96SJosef Bacik 
66058176a96SJosef Bacik 	err = btrfs_init_sysfs();
66158176a96SJosef Bacik 	if (err)
66258176a96SJosef Bacik 		return err;
66358176a96SJosef Bacik 
66439279cc3SChris Mason 	err = btrfs_init_cachep();
6652c90e5d6SChris Mason 	if (err)
666a74a4b97SChris Mason 		goto free_sysfs;
667d1310b2eSChris Mason 
668d1310b2eSChris Mason 	err = extent_io_init();
6692f4cbe64SWyatt Banks 	if (err)
6702f4cbe64SWyatt Banks 		goto free_cachep;
6712f4cbe64SWyatt Banks 
672d1310b2eSChris Mason 	err = extent_map_init();
673d1310b2eSChris Mason 	if (err)
674d1310b2eSChris Mason 		goto free_extent_io;
675d1310b2eSChris Mason 
676a9218f6bSChris Mason 	err = btrfs_interface_init();
6772f4cbe64SWyatt Banks 	if (err)
6782f4cbe64SWyatt Banks 		goto free_extent_map;
679c8b97818SChris Mason 
680a9218f6bSChris Mason 	err = register_filesystem(&btrfs_fs_type);
681a9218f6bSChris Mason 	if (err)
682a9218f6bSChris Mason 		goto unregister_ioctl;
683b3c3da71SChris Mason 
684b3c3da71SChris Mason 	printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
6852f4cbe64SWyatt Banks 	return 0;
6862f4cbe64SWyatt Banks 
687a9218f6bSChris Mason unregister_ioctl:
688a9218f6bSChris Mason 	btrfs_interface_exit();
6892f4cbe64SWyatt Banks free_extent_map:
6902f4cbe64SWyatt Banks 	extent_map_exit();
691d1310b2eSChris Mason free_extent_io:
692d1310b2eSChris Mason 	extent_io_exit();
6932f4cbe64SWyatt Banks free_cachep:
6942f4cbe64SWyatt Banks 	btrfs_destroy_cachep();
695a74a4b97SChris Mason free_sysfs:
6962f4cbe64SWyatt Banks 	btrfs_exit_sysfs();
6972c90e5d6SChris Mason 	return err;
6982e635a27SChris Mason }
6992e635a27SChris Mason 
7002e635a27SChris Mason static void __exit exit_btrfs_fs(void)
7012e635a27SChris Mason {
70239279cc3SChris Mason 	btrfs_destroy_cachep();
703a52d9a80SChris Mason 	extent_map_exit();
704d1310b2eSChris Mason 	extent_io_exit();
705a9218f6bSChris Mason 	btrfs_interface_exit();
7062e635a27SChris Mason 	unregister_filesystem(&btrfs_fs_type);
70758176a96SJosef Bacik 	btrfs_exit_sysfs();
7088a4b83ccSChris Mason 	btrfs_cleanup_fs_uuids();
709c8b97818SChris Mason 	btrfs_zlib_exit();
7102e635a27SChris Mason }
7112e635a27SChris Mason 
7122e635a27SChris Mason module_init(init_btrfs_fs)
7132e635a27SChris Mason module_exit(exit_btrfs_fs)
7142e635a27SChris Mason 
7152e635a27SChris Mason MODULE_LICENSE("GPL");
716