Btrfs: Add mount -o nodatasum to turn of file data checksumming
[linux-2.6-block.git] / fs / btrfs / super.c
CommitLineData
6cbd5570
CM
1/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
4b82d6e4 19#include <linux/blkdev.h>
2e635a27 20#include <linux/module.h>
e20d96d6 21#include <linux/buffer_head.h>
2e635a27
CM
22#include <linux/fs.h>
23#include <linux/pagemap.h>
24#include <linux/highmem.h>
25#include <linux/time.h>
26#include <linux/init.h>
27#include <linux/string.h>
28#include <linux/smp_lock.h>
29#include <linux/backing-dev.h>
4b82d6e4 30#include <linux/mount.h>
dee26a9f 31#include <linux/mpage.h>
75dfe396
CM
32#include <linux/swap.h>
33#include <linux/writeback.h>
8fd17795 34#include <linux/statfs.h>
08607c1b 35#include <linux/compat.h>
95e05289 36#include <linux/parser.h>
2e635a27 37#include "ctree.h"
e20d96d6 38#include "disk-io.h"
d5719762 39#include "transaction.h"
2c90e5d6 40#include "btrfs_inode.h"
c5739bba 41#include "ioctl.h"
3a686375 42#include "print-tree.h"
5103e947 43#include "xattr.h"
2e635a27 44
5f39d397 45#define BTRFS_SUPER_MAGIC 0x9123683E
f254e52c 46
39279cc3 47static struct super_operations btrfs_super_ops;
75dfe396 48
39279cc3 49static void btrfs_put_super (struct super_block * sb)
b18c6685 50{
39279cc3 51 struct btrfs_root *root = btrfs_sb(sb);
58176a96 52 struct btrfs_fs_info *fs = root->fs_info;
b18c6685 53 int ret;
b18c6685 54
39279cc3
CM
55 ret = close_ctree(root);
56 if (ret) {
57 printk("close ctree returns %d\n", ret);
75dfe396 58 }
58176a96 59 btrfs_sysfs_del_super(fs);
39279cc3 60 sb->s_fs_info = NULL;
75dfe396
CM
61}
62
95e05289 63enum {
b6cda9bc 64 Opt_subvol, Opt_nodatasum, Opt_err,
95e05289
CM
65};
66
67static match_table_t tokens = {
68 {Opt_subvol, "subvol=%s"},
b6cda9bc 69 {Opt_nodatasum, "nodatasum"},
95e05289
CM
70 {Opt_err, NULL}
71};
72
73static int parse_options (char * options,
74 struct btrfs_root *root,
75 char **subvol_name)
76{
77 char * p;
b6cda9bc 78 struct btrfs_fs_info *info = NULL;
95e05289 79 substring_t args[MAX_OPT_ARGS];
b6cda9bc
CM
80
81 if (root)
82 info = root->fs_info;
83
95e05289
CM
84 if (!options)
85 return 1;
86
87 while ((p = strsep (&options, ",")) != NULL) {
88 int token;
89 if (!*p)
90 continue;
91
92 token = match_token(p, tokens, args);
93 switch (token) {
94 case Opt_subvol:
b6cda9bc
CM
95 if (subvol_name)
96 *subvol_name = match_strdup(&args[0]);
97 break;
98 case Opt_nodatasum:
99 if (root)
100 btrfs_set_opt(info->mount_opt, NODATASUM);
95e05289
CM
101 break;
102 default:
103 return 0;
104 }
105 }
106 return 1;
107}
108
39279cc3 109static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
75dfe396 110{
39279cc3
CM
111 struct inode * inode;
112 struct dentry * root_dentry;
113 struct btrfs_super_block *disk_super;
114 struct btrfs_root *tree_root;
115 struct btrfs_inode *bi;
116 int err;
a429e513 117
39279cc3
CM
118 sb->s_maxbytes = MAX_LFS_FILESIZE;
119 sb->s_magic = BTRFS_SUPER_MAGIC;
120 sb->s_op = &btrfs_super_ops;
5103e947 121 sb->s_xattr = btrfs_xattr_handlers;
39279cc3 122 sb->s_time_gran = 1;
a429e513 123
39279cc3 124 tree_root = open_ctree(sb);
6567e837 125
39279cc3
CM
126 if (!tree_root || IS_ERR(tree_root)) {
127 printk("btrfs: open_ctree failed\n");
128 return -EIO;
a429e513 129 }
39279cc3 130 sb->s_fs_info = tree_root;
5f39d397 131 disk_super = &tree_root->fs_info->super_copy;
39279cc3
CM
132 inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
133 tree_root);
134 bi = BTRFS_I(inode);
135 bi->location.objectid = inode->i_ino;
136 bi->location.offset = 0;
39279cc3 137 bi->root = tree_root;
b888db2b 138
39279cc3 139 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
a429e513 140
39279cc3 141 if (!inode) {
6567e837 142 err = -ENOMEM;
39279cc3 143 goto fail_close;
f254e52c 144 }
39279cc3
CM
145 if (inode->i_state & I_NEW) {
146 btrfs_read_locked_inode(inode);
147 unlock_new_inode(inode);
f254e52c 148 }
f254e52c 149
39279cc3
CM
150 root_dentry = d_alloc_root(inode);
151 if (!root_dentry) {
152 iput(inode);
153 err = -ENOMEM;
154 goto fail_close;
f254e52c 155 }
58176a96 156
b6cda9bc
CM
157 parse_options((char *)data, tree_root, NULL);
158
58176a96
JB
159 /* this does the super kobj at the same time */
160 err = btrfs_sysfs_add_super(tree_root->fs_info);
161 if (err)
162 goto fail_close;
163
39279cc3
CM
164 sb->s_root = root_dentry;
165 btrfs_transaction_queue_work(tree_root, HZ * 30);
2619ba1f 166 return 0;
39279cc3
CM
167
168fail_close:
169 close_ctree(tree_root);
170 return err;
2619ba1f
CM
171}
172
39279cc3 173static int btrfs_sync_fs(struct super_block *sb, int wait)
c5739bba
CM
174{
175 struct btrfs_trans_handle *trans;
39279cc3 176 struct btrfs_root *root;
c5739bba 177 int ret;
39279cc3 178 root = btrfs_sb(sb);
2619ba1f 179
39279cc3
CM
180 sb->s_dirt = 0;
181 if (!wait) {
182 filemap_flush(root->fs_info->btree_inode->i_mapping);
183 return 0;
184 }
e9d0b13b 185 btrfs_clean_old_snapshots(root);
c5739bba 186 mutex_lock(&root->fs_info->fs_mutex);
e9d0b13b 187 btrfs_defrag_dirty_roots(root->fs_info);
c5739bba 188 trans = btrfs_start_transaction(root, 1);
c5739bba 189 ret = btrfs_commit_transaction(trans, root);
39279cc3 190 sb->s_dirt = 0;
c5739bba 191 mutex_unlock(&root->fs_info->fs_mutex);
54aa1f4d 192 return ret;
2c90e5d6
CM
193}
194
39279cc3 195static void btrfs_write_super(struct super_block *sb)
2c90e5d6 196{
39279cc3 197 sb->s_dirt = 0;
2c90e5d6
CM
198}
199
4b82d6e4
Y
200/*
201 * This is almost a copy of get_sb_bdev in fs/super.c.
202 * We need the local copy to allow direct mounting of
203 * subvolumes, but this could be easily integrated back
204 * into the generic version. --hch
205 */
206
207/* start copy & paste */
208static int set_bdev_super(struct super_block *s, void *data)
209{
210 s->s_bdev = data;
211 s->s_dev = s->s_bdev->bd_dev;
212 return 0;
213}
214
215static int test_bdev_super(struct super_block *s, void *data)
216{
217 return (void *)s->s_bdev == data;
218}
219
220int btrfs_get_sb_bdev(struct file_system_type *fs_type,
221 int flags, const char *dev_name, void *data,
222 int (*fill_super)(struct super_block *, void *, int),
223 struct vfsmount *mnt, const char *subvol)
224{
225 struct block_device *bdev = NULL;
226 struct super_block *s;
227 struct dentry *root;
228 int error = 0;
229
230 bdev = open_bdev_excl(dev_name, flags, fs_type);
231 if (IS_ERR(bdev))
232 return PTR_ERR(bdev);
233
234 /*
235 * once the super is inserted into the list by sget, s_umount
236 * will protect the lockfs code from trying to start a snapshot
237 * while we are mounting
238 */
239 down(&bdev->bd_mount_sem);
240 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
241 up(&bdev->bd_mount_sem);
242 if (IS_ERR(s))
243 goto error_s;
244
245 if (s->s_root) {
246 if ((flags ^ s->s_flags) & MS_RDONLY) {
247 up_write(&s->s_umount);
248 deactivate_super(s);
249 error = -EBUSY;
250 goto error_bdev;
251 }
252
253 close_bdev_excl(bdev);
254 } else {
255 char b[BDEVNAME_SIZE];
256
257 s->s_flags = flags;
258 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
259 sb_set_blocksize(s, block_size(bdev));
260 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
261 if (error) {
262 up_write(&s->s_umount);
263 deactivate_super(s);
264 goto error;
265 }
266
267 s->s_flags |= MS_ACTIVE;
268 }
269
270 if (subvol) {
271 root = lookup_one_len(subvol, s->s_root, strlen(subvol));
272 if (IS_ERR(root)) {
273 up_write(&s->s_umount);
274 deactivate_super(s);
275 error = PTR_ERR(root);
276 goto error;
277 }
278 if (!root->d_inode) {
279 dput(root);
280 up_write(&s->s_umount);
281 deactivate_super(s);
282 error = -ENXIO;
283 goto error;
284 }
285 } else {
286 root = dget(s->s_root);
287 }
288
289 mnt->mnt_sb = s;
290 mnt->mnt_root = root;
291 return 0;
292
293error_s:
294 error = PTR_ERR(s);
295error_bdev:
296 close_bdev_excl(bdev);
297error:
298 return error;
299}
300/* end copy & paste */
301
2e635a27 302static int btrfs_get_sb(struct file_system_type *fs_type,
95e05289 303 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2e635a27 304{
4b82d6e4 305 int ret;
95e05289 306 char *subvol_name = NULL;
4b82d6e4 307
95e05289 308 parse_options((char *)data, NULL, &subvol_name);
4b82d6e4
Y
309 ret = btrfs_get_sb_bdev(fs_type, flags, dev_name, data,
310 btrfs_fill_super, mnt,
311 subvol_name ? subvol_name : "default");
4b82d6e4 312 return ret;
2e635a27
CM
313}
314
8fd17795
CM
315static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
316{
317 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
4b52dff6 318 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
db94535d 319 int bits = dentry->d_sb->s_blocksize_bits;
8fd17795
CM
320
321 buf->f_namelen = BTRFS_NAME_LEN;
db94535d
CM
322 buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
323 buf->f_bfree = buf->f_blocks -
324 (btrfs_super_bytes_used(disk_super) >> bits);
8fd17795
CM
325 buf->f_bavail = buf->f_bfree;
326 buf->f_bsize = dentry->d_sb->s_blocksize;
327 buf->f_type = BTRFS_SUPER_MAGIC;
328 return 0;
329}
b5133862 330
2e635a27
CM
331static struct file_system_type btrfs_fs_type = {
332 .owner = THIS_MODULE,
333 .name = "btrfs",
334 .get_sb = btrfs_get_sb,
335 .kill_sb = kill_block_super,
336 .fs_flags = FS_REQUIRES_DEV,
337};
338
e20d96d6 339static struct super_operations btrfs_super_ops = {
134e9731 340 .delete_inode = btrfs_delete_inode,
e20d96d6
CM
341 .put_super = btrfs_put_super,
342 .read_inode = btrfs_read_locked_inode,
d5719762
CM
343 .write_super = btrfs_write_super,
344 .sync_fs = btrfs_sync_fs,
4730a4bc 345 .write_inode = btrfs_write_inode,
b5133862 346 .dirty_inode = btrfs_dirty_inode,
2c90e5d6
CM
347 .alloc_inode = btrfs_alloc_inode,
348 .destroy_inode = btrfs_destroy_inode,
8fd17795 349 .statfs = btrfs_statfs,
e20d96d6
CM
350};
351
2e635a27
CM
352static int __init init_btrfs_fs(void)
353{
2c90e5d6 354 int err;
58176a96
JB
355
356 err = btrfs_init_sysfs();
357 if (err)
358 return err;
359
08607c1b 360 btrfs_init_transaction_sys();
39279cc3 361 err = btrfs_init_cachep();
2c90e5d6 362 if (err)
2f4cbe64
WB
363 goto free_transaction_sys;
364 err = extent_map_init();
365 if (err)
366 goto free_cachep;
367
368 err = register_filesystem(&btrfs_fs_type);
369 if (err)
370 goto free_extent_map;
371 return 0;
372
373free_extent_map:
374 extent_map_exit();
375free_cachep:
376 btrfs_destroy_cachep();
377free_transaction_sys:
378 btrfs_exit_transaction_sys();
379 btrfs_exit_sysfs();
380 return err;
2e635a27
CM
381}
382
383static void __exit exit_btrfs_fs(void)
384{
08607c1b 385 btrfs_exit_transaction_sys();
39279cc3 386 btrfs_destroy_cachep();
a52d9a80 387 extent_map_exit();
2e635a27 388 unregister_filesystem(&btrfs_fs_type);
58176a96 389 btrfs_exit_sysfs();
2e635a27
CM
390}
391
392module_init(init_btrfs_fs)
393module_exit(exit_btrfs_fs)
394
395MODULE_LICENSE("GPL");