2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
19 #include <linux/blkdev.h>
20 #include <linux/module.h>
21 #include <linux/buffer_head.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>
30 #include <linux/mount.h>
31 #include <linux/mpage.h>
32 #include <linux/swap.h>
33 #include <linux/writeback.h>
34 #include <linux/statfs.h>
35 #include <linux/compat.h>
36 #include <linux/parser.h>
37 #include <linux/ctype.h>
38 #include <linux/namei.h>
39 #include <linux/miscdevice.h>
42 #include "transaction.h"
43 #include "btrfs_inode.h"
45 #include "print-tree.h"
49 #define BTRFS_SUPER_MAGIC 0x9123683E
51 static struct super_operations btrfs_super_ops
;
53 static void btrfs_put_super (struct super_block
* sb
)
55 struct btrfs_root
*root
= btrfs_sb(sb
);
56 struct btrfs_fs_info
*fs
= root
->fs_info
;
59 ret
= close_ctree(root
);
61 printk("close ctree returns %d\n", ret
);
63 btrfs_sysfs_del_super(fs
);
68 Opt_degraded
, Opt_subvol
, Opt_nodatasum
, Opt_nodatacow
,
69 Opt_max_extent
, Opt_max_inline
, Opt_alloc_start
, Opt_nobarrier
,
73 static match_table_t tokens
= {
74 {Opt_degraded
, "degraded"},
75 {Opt_subvol
, "subvol=%s"},
76 {Opt_nodatasum
, "nodatasum"},
77 {Opt_nodatacow
, "nodatacow"},
78 {Opt_nobarrier
, "nobarrier"},
79 {Opt_max_extent
, "max_extent=%s"},
80 {Opt_max_inline
, "max_inline=%s"},
81 {Opt_alloc_start
, "alloc_start=%s"},
86 u64
btrfs_parse_size(char *str
)
93 res
= simple_strtoul(str
, &end
, 10);
111 int btrfs_parse_options(char *options
, struct btrfs_root
*root
,
115 struct btrfs_fs_info
*info
= NULL
;
116 substring_t args
[MAX_OPT_ARGS
];
122 * strsep changes the string, duplicate it because parse_options
125 options
= kstrdup(options
, GFP_NOFS
);
130 info
= root
->fs_info
;
132 while ((p
= strsep (&options
, ",")) != NULL
) {
137 token
= match_token(p
, tokens
, args
);
141 printk("btrfs: allowing degraded mounts\n");
142 btrfs_set_opt(info
->mount_opt
, DEGRADED
);
147 *subvol_name
= match_strdup(&args
[0]);
152 printk("btrfs: setting nodatacsum\n");
153 btrfs_set_opt(info
->mount_opt
, NODATASUM
);
158 printk("btrfs: setting nodatacow\n");
159 btrfs_set_opt(info
->mount_opt
, NODATACOW
);
160 btrfs_set_opt(info
->mount_opt
, NODATASUM
);
165 printk("btrfs: use ssd allocation scheme\n");
166 btrfs_set_opt(info
->mount_opt
, SSD
);
171 printk("btrfs: turning off barriers\n");
172 btrfs_set_opt(info
->mount_opt
, NOBARRIER
);
177 char *num
= match_strdup(&args
[0]);
180 btrfs_parse_size(num
);
183 info
->max_extent
= max_t(u64
,
186 printk("btrfs: max_extent at %Lu\n",
193 char *num
= match_strdup(&args
[0]);
196 btrfs_parse_size(num
);
199 info
->max_inline
= max_t(u64
,
202 printk("btrfs: max_inline at %Lu\n",
207 case Opt_alloc_start
:
209 char *num
= match_strdup(&args
[0]);
212 btrfs_parse_size(num
);
214 printk("btrfs: allocations start at "
215 "%Lu\n", info
->alloc_start
);
227 static int btrfs_fill_super(struct super_block
* sb
,
228 struct btrfs_fs_devices
*fs_devices
,
229 void * data
, int silent
)
231 struct inode
* inode
;
232 struct dentry
* root_dentry
;
233 struct btrfs_super_block
*disk_super
;
234 struct btrfs_root
*tree_root
;
235 struct btrfs_inode
*bi
;
238 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
239 sb
->s_magic
= BTRFS_SUPER_MAGIC
;
240 sb
->s_op
= &btrfs_super_ops
;
241 sb
->s_xattr
= btrfs_xattr_handlers
;
244 tree_root
= open_ctree(sb
, fs_devices
, (char *)data
);
246 if (IS_ERR(tree_root
)) {
247 printk("btrfs: open_ctree failed\n");
248 return PTR_ERR(tree_root
);
250 sb
->s_fs_info
= tree_root
;
251 disk_super
= &tree_root
->fs_info
->super_copy
;
252 inode
= btrfs_iget_locked(sb
, btrfs_super_root_dir(disk_super
),
255 bi
->location
.objectid
= inode
->i_ino
;
256 bi
->location
.offset
= 0;
257 bi
->root
= tree_root
;
259 btrfs_set_key_type(&bi
->location
, BTRFS_INODE_ITEM_KEY
);
265 if (inode
->i_state
& I_NEW
) {
266 btrfs_read_locked_inode(inode
);
267 unlock_new_inode(inode
);
270 root_dentry
= d_alloc_root(inode
);
277 /* this does the super kobj at the same time */
278 err
= btrfs_sysfs_add_super(tree_root
->fs_info
);
282 sb
->s_root
= root_dentry
;
283 btrfs_transaction_queue_work(tree_root
, HZ
* 30);
285 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)
286 save_mount_options(sb
, data
);
292 close_ctree(tree_root
);
296 static int btrfs_sync_fs(struct super_block
*sb
, int wait
)
298 struct btrfs_trans_handle
*trans
;
299 struct btrfs_root
*root
;
305 filemap_flush(root
->fs_info
->btree_inode
->i_mapping
);
308 btrfs_clean_old_snapshots(root
);
309 mutex_lock(&root
->fs_info
->fs_mutex
);
310 btrfs_defrag_dirty_roots(root
->fs_info
);
311 trans
= btrfs_start_transaction(root
, 1);
312 ret
= btrfs_commit_transaction(trans
, root
);
314 mutex_unlock(&root
->fs_info
->fs_mutex
);
318 static void btrfs_write_super(struct super_block
*sb
)
323 static int btrfs_test_super(struct super_block
*s
, void *data
)
325 struct btrfs_fs_devices
*test_fs_devices
= data
;
326 struct btrfs_root
*root
= btrfs_sb(s
);
328 return root
->fs_info
->fs_devices
== test_fs_devices
;
331 int btrfs_get_sb_bdev(struct file_system_type
*fs_type
,
332 int flags
, const char *dev_name
, void *data
,
333 struct vfsmount
*mnt
, const char *subvol
)
335 struct block_device
*bdev
= NULL
;
336 struct super_block
*s
;
338 struct btrfs_fs_devices
*fs_devices
= NULL
;
341 error
= btrfs_scan_one_device(dev_name
, flags
, fs_type
, &fs_devices
);
345 error
= btrfs_open_devices(fs_devices
, flags
, fs_type
);
349 bdev
= fs_devices
->latest_bdev
;
350 btrfs_lock_volumes();
351 s
= sget(fs_type
, btrfs_test_super
, set_anon_super
, fs_devices
);
352 btrfs_unlock_volumes();
357 if ((flags
^ s
->s_flags
) & MS_RDONLY
) {
358 up_write(&s
->s_umount
);
365 char b
[BDEVNAME_SIZE
];
368 strlcpy(s
->s_id
, bdevname(bdev
, b
), sizeof(s
->s_id
));
369 error
= btrfs_fill_super(s
, fs_devices
, data
,
370 flags
& MS_SILENT
? 1 : 0);
372 up_write(&s
->s_umount
);
377 btrfs_sb(s
)->fs_info
->bdev_holder
= fs_type
;
378 s
->s_flags
|= MS_ACTIVE
;
382 root
= lookup_one_len(subvol
, s
->s_root
, strlen(subvol
));
384 up_write(&s
->s_umount
);
386 error
= PTR_ERR(root
);
389 if (!root
->d_inode
) {
391 up_write(&s
->s_umount
);
397 root
= dget(s
->s_root
);
401 mnt
->mnt_root
= root
;
407 btrfs_close_devices(fs_devices
);
411 /* end copy & paste */
413 static int btrfs_get_sb(struct file_system_type
*fs_type
,
414 int flags
, const char *dev_name
, void *data
, struct vfsmount
*mnt
)
417 char *subvol_name
= NULL
;
419 btrfs_parse_options((char *)data
, NULL
, &subvol_name
);
420 ret
= btrfs_get_sb_bdev(fs_type
, flags
, dev_name
, data
, mnt
,
421 subvol_name
? subvol_name
: "default");
427 static int btrfs_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
429 struct btrfs_root
*root
= btrfs_sb(dentry
->d_sb
);
430 struct btrfs_super_block
*disk_super
= &root
->fs_info
->super_copy
;
431 int bits
= dentry
->d_sb
->s_blocksize_bits
;
433 buf
->f_namelen
= BTRFS_NAME_LEN
;
434 buf
->f_blocks
= btrfs_super_total_bytes(disk_super
) >> bits
;
435 buf
->f_bfree
= buf
->f_blocks
-
436 (btrfs_super_bytes_used(disk_super
) >> bits
);
437 buf
->f_bavail
= buf
->f_bfree
;
438 buf
->f_bsize
= dentry
->d_sb
->s_blocksize
;
439 buf
->f_type
= BTRFS_SUPER_MAGIC
;
443 static struct file_system_type btrfs_fs_type
= {
444 .owner
= THIS_MODULE
,
446 .get_sb
= btrfs_get_sb
,
447 .kill_sb
= kill_anon_super
,
448 .fs_flags
= FS_REQUIRES_DEV
,
451 static long btrfs_control_ioctl(struct file
*file
, unsigned int cmd
,
454 struct btrfs_ioctl_vol_args
*vol
;
455 struct btrfs_fs_devices
*fs_devices
;
459 vol
= kmalloc(sizeof(*vol
), GFP_KERNEL
);
460 if (copy_from_user(vol
, (void __user
*)arg
, sizeof(*vol
))) {
464 len
= strnlen(vol
->name
, BTRFS_PATH_NAME_MAX
);
466 case BTRFS_IOC_SCAN_DEV
:
467 ret
= btrfs_scan_one_device(vol
->name
, MS_RDONLY
,
468 &btrfs_fs_type
, &fs_devices
);
476 static void btrfs_write_super_lockfs(struct super_block
*sb
)
478 struct btrfs_root
*root
= btrfs_sb(sb
);
479 btrfs_transaction_flush_work(root
);
482 static void btrfs_unlockfs(struct super_block
*sb
)
484 struct btrfs_root
*root
= btrfs_sb(sb
);
485 btrfs_transaction_queue_work(root
, HZ
* 30);
488 static struct super_operations btrfs_super_ops
= {
489 .delete_inode
= btrfs_delete_inode
,
490 .put_super
= btrfs_put_super
,
491 .write_super
= btrfs_write_super
,
492 .sync_fs
= btrfs_sync_fs
,
493 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
494 .read_inode
= btrfs_read_locked_inode
,
496 .show_options
= generic_show_options
,
498 .write_inode
= btrfs_write_inode
,
499 .dirty_inode
= btrfs_dirty_inode
,
500 .alloc_inode
= btrfs_alloc_inode
,
501 .destroy_inode
= btrfs_destroy_inode
,
502 .statfs
= btrfs_statfs
,
503 .write_super_lockfs
= btrfs_write_super_lockfs
,
504 .unlockfs
= btrfs_unlockfs
,
507 static const struct file_operations btrfs_ctl_fops
= {
508 .unlocked_ioctl
= btrfs_control_ioctl
,
509 .compat_ioctl
= btrfs_control_ioctl
,
510 .owner
= THIS_MODULE
,
513 static struct miscdevice btrfs_misc
= {
514 .minor
= MISC_DYNAMIC_MINOR
,
515 .name
= "btrfs-control",
516 .fops
= &btrfs_ctl_fops
519 static int btrfs_interface_init(void)
521 return misc_register(&btrfs_misc
);
524 void btrfs_interface_exit(void)
526 if (misc_deregister(&btrfs_misc
) < 0)
527 printk("misc_deregister failed for control device");
530 static int __init
init_btrfs_fs(void)
534 err
= btrfs_init_sysfs();
538 btrfs_init_transaction_sys();
539 err
= btrfs_init_cachep();
541 goto free_transaction_sys
;
543 err
= extent_io_init();
547 err
= extent_map_init();
551 err
= btrfs_interface_init();
553 goto free_extent_map
;
554 err
= register_filesystem(&btrfs_fs_type
);
556 goto unregister_ioctl
;
560 btrfs_interface_exit();
566 btrfs_destroy_cachep();
567 free_transaction_sys
:
568 btrfs_exit_transaction_sys();
573 static void __exit
exit_btrfs_fs(void)
575 btrfs_exit_transaction_sys();
576 btrfs_destroy_cachep();
579 btrfs_interface_exit();
580 unregister_filesystem(&btrfs_fs_type
);
582 btrfs_cleanup_fs_uuids();
585 module_init(init_btrfs_fs
)
586 module_exit(exit_btrfs_fs
)
588 MODULE_LICENSE("GPL");