blob: ad4f280ec777047a9d131eabfdefa658f37732e7 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
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
Yan4b82d6e2007-08-29 09:11:44 -040019#include <linux/blkdev.h>
Chris Mason2e635a22007-03-21 11:12:56 -040020#include <linux/module.h>
Chris Masone20d96d2007-03-22 12:13:20 -040021#include <linux/buffer_head.h>
Chris Mason2e635a22007-03-21 11:12:56 -040022#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>
Yan4b82d6e2007-08-29 09:11:44 -040030#include <linux/mount.h>
Chris Masondee26a92007-03-26 16:00:06 -040031#include <linux/mpage.h>
Chris Mason75dfe392007-03-29 11:56:46 -040032#include <linux/swap.h>
33#include <linux/writeback.h>
Chris Mason8fd17792007-04-19 21:01:03 -040034#include <linux/statfs.h>
Chris Mason08607c12007-06-08 15:33:54 -040035#include <linux/compat.h>
Chris Mason95e05282007-08-29 09:11:44 -040036#include <linux/parser.h>
Chris Mason2e635a22007-03-21 11:12:56 -040037#include "ctree.h"
Chris Masone20d96d2007-03-22 12:13:20 -040038#include "disk-io.h"
Chris Masond5719762007-03-23 10:01:08 -040039#include "transaction.h"
Chris Mason2c90e5d2007-04-02 10:50:19 -040040#include "btrfs_inode.h"
Chris Masonc5739bb2007-04-10 09:27:04 -040041#include "ioctl.h"
Chris Mason3a686372007-05-24 13:35:57 -040042#include "print-tree.h"
Josef Bacik5103e942007-11-16 11:45:54 -050043#include "xattr.h"
Chris Mason2e635a22007-03-21 11:12:56 -040044
Chris Mason5f39d392007-10-15 16:14:19 -040045#define BTRFS_SUPER_MAGIC 0x9123683E
Chris Masone20d96d2007-03-22 12:13:20 -040046
Chris Masone20d96d2007-03-22 12:13:20 -040047static struct super_operations btrfs_super_ops;
Chris Masone20d96d2007-03-22 12:13:20 -040048
49static void btrfs_put_super (struct super_block * sb)
50{
51 struct btrfs_root *root = btrfs_sb(sb);
Josef Bacik58176a92007-08-29 15:47:34 -040052 struct btrfs_fs_info *fs = root->fs_info;
Chris Masone20d96d2007-03-22 12:13:20 -040053 int ret;
54
55 ret = close_ctree(root);
56 if (ret) {
57 printk("close ctree returns %d\n", ret);
58 }
Josef Bacik58176a92007-08-29 15:47:34 -040059 btrfs_sysfs_del_super(fs);
Chris Masone20d96d2007-03-22 12:13:20 -040060 sb->s_fs_info = NULL;
61}
Chris Mason2e635a22007-03-21 11:12:56 -040062
Chris Mason95e05282007-08-29 09:11:44 -040063enum {
Chris Masonb6cda9b2007-12-14 15:30:32 -050064 Opt_subvol, Opt_nodatasum, Opt_err,
Chris Mason95e05282007-08-29 09:11:44 -040065};
66
67static match_table_t tokens = {
68 {Opt_subvol, "subvol=%s"},
Chris Masonb6cda9b2007-12-14 15:30:32 -050069 {Opt_nodatasum, "nodatasum"},
Chris Mason95e05282007-08-29 09:11:44 -040070 {Opt_err, NULL}
71};
72
73static int parse_options (char * options,
74 struct btrfs_root *root,
75 char **subvol_name)
76{
77 char * p;
Chris Masonb6cda9b2007-12-14 15:30:32 -050078 struct btrfs_fs_info *info = NULL;
Chris Mason95e05282007-08-29 09:11:44 -040079 substring_t args[MAX_OPT_ARGS];
Chris Masonb6cda9b2007-12-14 15:30:32 -050080
81 if (root)
82 info = root->fs_info;
83
Chris Mason95e05282007-08-29 09:11:44 -040084 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:
Chris Masonb6cda9b2007-12-14 15:30:32 -050095 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);
Chris Mason95e05282007-08-29 09:11:44 -0400101 break;
102 default:
103 return 0;
104 }
105 }
106 return 1;
107}
108
Chris Mason2e635a22007-03-21 11:12:56 -0400109static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
110{
111 struct inode * inode;
Chris Masone20d96d2007-03-22 12:13:20 -0400112 struct dentry * root_dentry;
113 struct btrfs_super_block *disk_super;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400114 struct btrfs_root *tree_root;
Chris Masond6e4a422007-04-06 15:37:36 -0400115 struct btrfs_inode *bi;
Chris Mason39279cc2007-06-12 06:35:45 -0400116 int err;
Chris Mason2e635a22007-03-21 11:12:56 -0400117
118 sb->s_maxbytes = MAX_LFS_FILESIZE;
Chris Mason2e635a22007-03-21 11:12:56 -0400119 sb->s_magic = BTRFS_SUPER_MAGIC;
Chris Masone20d96d2007-03-22 12:13:20 -0400120 sb->s_op = &btrfs_super_ops;
Josef Bacik5103e942007-11-16 11:45:54 -0500121 sb->s_xattr = btrfs_xattr_handlers;
Chris Mason2e635a22007-03-21 11:12:56 -0400122 sb->s_time_gran = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400123
Chris Mason0f7d52f2007-04-09 10:42:37 -0400124 tree_root = open_ctree(sb);
Chris Masond98237b2007-03-28 13:57:48 -0400125
Chris Mason39279cc2007-06-12 06:35:45 -0400126 if (!tree_root || IS_ERR(tree_root)) {
Chris Masone20d96d2007-03-22 12:13:20 -0400127 printk("btrfs: open_ctree failed\n");
128 return -EIO;
129 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400130 sb->s_fs_info = tree_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400131 disk_super = &tree_root->fs_info->super_copy;
Chris Masonc5739bb2007-04-10 09:27:04 -0400132 inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
133 tree_root);
Chris Masond6e4a422007-04-06 15:37:36 -0400134 bi = BTRFS_I(inode);
135 bi->location.objectid = inode->i_ino;
136 bi->location.offset = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400137 bi->root = tree_root;
Chris Masonb888db22007-08-27 16:49:44 -0400138
Chris Masond6e4a422007-04-06 15:37:36 -0400139 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
140
Chris Mason39279cc2007-06-12 06:35:45 -0400141 if (!inode) {
142 err = -ENOMEM;
143 goto fail_close;
144 }
Chris Masone20d96d2007-03-22 12:13:20 -0400145 if (inode->i_state & I_NEW) {
146 btrfs_read_locked_inode(inode);
147 unlock_new_inode(inode);
148 }
Chris Mason2e635a22007-03-21 11:12:56 -0400149
Chris Masone20d96d2007-03-22 12:13:20 -0400150 root_dentry = d_alloc_root(inode);
151 if (!root_dentry) {
Chris Mason2e635a22007-03-21 11:12:56 -0400152 iput(inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400153 err = -ENOMEM;
154 goto fail_close;
Chris Mason2e635a22007-03-21 11:12:56 -0400155 }
Josef Bacik58176a92007-08-29 15:47:34 -0400156
Chris Masonb6cda9b2007-12-14 15:30:32 -0500157 parse_options((char *)data, tree_root, NULL);
158
Josef Bacik58176a92007-08-29 15:47:34 -0400159 /* 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
Chris Masone20d96d2007-03-22 12:13:20 -0400164 sb->s_root = root_dentry;
Chris Mason08607c12007-06-08 15:33:54 -0400165 btrfs_transaction_queue_work(tree_root, HZ * 30);
Chris Mason2e635a22007-03-21 11:12:56 -0400166 return 0;
Chris Mason2e635a22007-03-21 11:12:56 -0400167
Chris Mason39279cc2007-06-12 06:35:45 -0400168fail_close:
169 close_ctree(tree_root);
Chris Masond5719762007-03-23 10:01:08 -0400170 return err;
171}
172
Chris Masond5719762007-03-23 10:01:08 -0400173static int btrfs_sync_fs(struct super_block *sb, int wait)
174{
175 struct btrfs_trans_handle *trans;
176 struct btrfs_root *root;
177 int ret;
Chris Masond98237b2007-03-28 13:57:48 -0400178 root = btrfs_sb(sb);
Chris Masondf2ce342007-03-23 11:00:45 -0400179
Chris Masond5719762007-03-23 10:01:08 -0400180 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -0400181 if (!wait) {
Chris Mason7cfcc172007-04-02 14:53:59 -0400182 filemap_flush(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400183 return 0;
184 }
Chris Masone9d0b132007-08-10 14:06:19 -0400185 btrfs_clean_old_snapshots(root);
Chris Masond561c022007-03-23 19:47:49 -0400186 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone9d0b132007-08-10 14:06:19 -0400187 btrfs_defrag_dirty_roots(root->fs_info);
Chris Masond5719762007-03-23 10:01:08 -0400188 trans = btrfs_start_transaction(root, 1);
189 ret = btrfs_commit_transaction(trans, root);
190 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -0400191 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason54aa1f42007-06-22 14:16:25 -0400192 return ret;
Chris Masond5719762007-03-23 10:01:08 -0400193}
194
Chris Masond561c022007-03-23 19:47:49 -0400195static void btrfs_write_super(struct super_block *sb)
196{
Chris Mason08607c12007-06-08 15:33:54 -0400197 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -0400198}
199
Yan4b82d6e2007-08-29 09:11:44 -0400200/*
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)
Chris Mason2e635a22007-03-21 11:12:56 -0400209{
Yan4b82d6e2007-08-29 09:11:44 -0400210 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
302static int btrfs_get_sb(struct file_system_type *fs_type,
Chris Mason95e05282007-08-29 09:11:44 -0400303 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Yan4b82d6e2007-08-29 09:11:44 -0400304{
305 int ret;
Chris Mason95e05282007-08-29 09:11:44 -0400306 char *subvol_name = NULL;
Yan4b82d6e2007-08-29 09:11:44 -0400307
Chris Mason95e05282007-08-29 09:11:44 -0400308 parse_options((char *)data, NULL, &subvol_name);
Yan4b82d6e2007-08-29 09:11:44 -0400309 ret = btrfs_get_sb_bdev(fs_type, flags, dev_name, data,
310 btrfs_fill_super, mnt,
311 subvol_name ? subvol_name : "default");
Yan4b82d6e2007-08-29 09:11:44 -0400312 return ret;
Chris Mason2e635a22007-03-21 11:12:56 -0400313}
314
Chris Mason8fd17792007-04-19 21:01:03 -0400315static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
316{
317 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
Chris Mason4b52dff2007-06-26 10:06:50 -0400318 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
Chris Masondb945352007-10-15 16:15:53 -0400319 int bits = dentry->d_sb->s_blocksize_bits;
Chris Mason8fd17792007-04-19 21:01:03 -0400320
321 buf->f_namelen = BTRFS_NAME_LEN;
Chris Masondb945352007-10-15 16:15:53 -0400322 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);
Chris Mason8fd17792007-04-19 21:01:03 -0400325 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}
Chris Masonb5133862007-04-24 11:52:22 -0400330
Chris Mason2e635a22007-03-21 11:12:56 -0400331static 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
Chris Masone20d96d2007-03-22 12:13:20 -0400339static struct super_operations btrfs_super_ops = {
Chris Mason134e9732007-03-25 13:44:56 -0400340 .delete_inode = btrfs_delete_inode,
Chris Masone20d96d2007-03-22 12:13:20 -0400341 .put_super = btrfs_put_super,
342 .read_inode = btrfs_read_locked_inode,
Chris Masond5719762007-03-23 10:01:08 -0400343 .write_super = btrfs_write_super,
344 .sync_fs = btrfs_sync_fs,
Chris Mason4730a4b2007-03-26 12:00:39 -0400345 .write_inode = btrfs_write_inode,
Chris Masonb5133862007-04-24 11:52:22 -0400346 .dirty_inode = btrfs_dirty_inode,
Chris Mason2c90e5d2007-04-02 10:50:19 -0400347 .alloc_inode = btrfs_alloc_inode,
348 .destroy_inode = btrfs_destroy_inode,
Chris Mason8fd17792007-04-19 21:01:03 -0400349 .statfs = btrfs_statfs,
Chris Masone20d96d2007-03-22 12:13:20 -0400350};
351
Chris Mason2e635a22007-03-21 11:12:56 -0400352static int __init init_btrfs_fs(void)
353{
Chris Mason2c90e5d2007-04-02 10:50:19 -0400354 int err;
Josef Bacik58176a92007-08-29 15:47:34 -0400355
356 err = btrfs_init_sysfs();
357 if (err)
358 return err;
359
Chris Mason08607c12007-06-08 15:33:54 -0400360 btrfs_init_transaction_sys();
Chris Mason39279cc2007-06-12 06:35:45 -0400361 err = btrfs_init_cachep();
Chris Mason2c90e5d2007-04-02 10:50:19 -0400362 if (err)
Wyatt Banks2f4cbe62007-11-19 10:22:33 -0500363 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;
Chris Mason2e635a22007-03-21 11:12:56 -0400381}
382
383static void __exit exit_btrfs_fs(void)
384{
Chris Mason08607c12007-06-08 15:33:54 -0400385 btrfs_exit_transaction_sys();
Chris Mason39279cc2007-06-12 06:35:45 -0400386 btrfs_destroy_cachep();
Chris Masona52d9a82007-08-27 16:49:44 -0400387 extent_map_exit();
Chris Mason2e635a22007-03-21 11:12:56 -0400388 unregister_filesystem(&btrfs_fs_type);
Josef Bacik58176a92007-08-29 15:47:34 -0400389 btrfs_exit_sysfs();
Chris Mason2e635a22007-03-21 11:12:56 -0400390}
391
392module_init(init_btrfs_fs)
393module_exit(exit_btrfs_fs)
394
395MODULE_LICENSE("GPL");