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> 39*a9218f6bSChris Mason #include <linux/miscdevice.h> 402e635a27SChris Mason #include "ctree.h" 41e20d96d6SChris Mason #include "disk-io.h" 42d5719762SChris Mason #include "transaction.h" 432c90e5d6SChris Mason #include "btrfs_inode.h" 44c5739bbaSChris Mason #include "ioctl.h" 453a686375SChris Mason #include "print-tree.h" 465103e947SJosef Bacik #include "xattr.h" 472e635a27SChris Mason 485f39d397SChris Mason #define BTRFS_SUPER_MAGIC 0x9123683E 49e20d96d6SChris Mason 50e20d96d6SChris Mason static struct super_operations btrfs_super_ops; 51e20d96d6SChris Mason 52e20d96d6SChris Mason static void btrfs_put_super (struct super_block * sb) 53e20d96d6SChris Mason { 54e20d96d6SChris Mason struct btrfs_root *root = btrfs_sb(sb); 5558176a96SJosef Bacik struct btrfs_fs_info *fs = root->fs_info; 56e20d96d6SChris Mason int ret; 57e20d96d6SChris Mason 58e20d96d6SChris Mason ret = close_ctree(root); 59e20d96d6SChris Mason if (ret) { 60e20d96d6SChris Mason printk("close ctree returns %d\n", ret); 61e20d96d6SChris Mason } 6258176a96SJosef Bacik btrfs_sysfs_del_super(fs); 63e20d96d6SChris Mason sb->s_fs_info = NULL; 64e20d96d6SChris Mason } 652e635a27SChris Mason 6695e05289SChris Mason enum { 678f662a76SChris Mason Opt_subvol, Opt_nodatasum, Opt_nodatacow, Opt_max_extent, 686f568d35SChris Mason Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd, Opt_err, 6995e05289SChris Mason }; 7095e05289SChris Mason 7195e05289SChris Mason static match_table_t tokens = { 7295e05289SChris Mason {Opt_subvol, "subvol=%s"}, 73b6cda9bcSChris Mason {Opt_nodatasum, "nodatasum"}, 74be20aa9dSChris Mason {Opt_nodatacow, "nodatacow"}, 7521ad10cfSChris Mason {Opt_nobarrier, "nobarrier"}, 76c59f8951SChris Mason {Opt_max_extent, "max_extent=%s"}, 776f568d35SChris Mason {Opt_max_inline, "max_inline=%s"}, 788f662a76SChris Mason {Opt_alloc_start, "alloc_start=%s"}, 79e18e4809SChris Mason {Opt_ssd, "ssd"}, 8095e05289SChris Mason {Opt_err, NULL} 8195e05289SChris Mason }; 8295e05289SChris Mason 83edbd8d4eSChris Mason u64 btrfs_parse_size(char *str) 84c59f8951SChris Mason { 85edbd8d4eSChris Mason u64 res; 86c59f8951SChris Mason int mult = 1; 87c59f8951SChris Mason char *end; 88c59f8951SChris Mason char last; 89c59f8951SChris Mason 90c59f8951SChris Mason res = simple_strtoul(str, &end, 10); 91c59f8951SChris Mason 92c59f8951SChris Mason last = end[0]; 93c59f8951SChris Mason if (isalpha(last)) { 94c59f8951SChris Mason last = tolower(last); 95c59f8951SChris Mason switch (last) { 96c59f8951SChris Mason case 'g': 97c59f8951SChris Mason mult *= 1024; 98c59f8951SChris Mason case 'm': 99c59f8951SChris Mason mult *= 1024; 100c59f8951SChris Mason case 'k': 101c59f8951SChris Mason mult *= 1024; 102c59f8951SChris Mason } 103c59f8951SChris Mason res = res * mult; 104c59f8951SChris Mason } 105c59f8951SChris Mason return res; 106c59f8951SChris Mason } 107c59f8951SChris Mason 10895e05289SChris Mason static int parse_options (char * options, 10995e05289SChris Mason struct btrfs_root *root, 11095e05289SChris Mason char **subvol_name) 11195e05289SChris Mason { 11295e05289SChris Mason char * p; 113b6cda9bcSChris Mason struct btrfs_fs_info *info = NULL; 11495e05289SChris Mason substring_t args[MAX_OPT_ARGS]; 115b6cda9bcSChris Mason 11695e05289SChris Mason if (!options) 11795e05289SChris Mason return 1; 11895e05289SChris Mason 119be20aa9dSChris Mason /* 120be20aa9dSChris Mason * strsep changes the string, duplicate it because parse_options 121be20aa9dSChris Mason * gets called twice 122be20aa9dSChris Mason */ 123be20aa9dSChris Mason options = kstrdup(options, GFP_NOFS); 124be20aa9dSChris Mason if (!options) 125be20aa9dSChris Mason return -ENOMEM; 126be20aa9dSChris Mason 127be20aa9dSChris Mason if (root) 128be20aa9dSChris Mason info = root->fs_info; 129be20aa9dSChris Mason 13095e05289SChris Mason while ((p = strsep (&options, ",")) != NULL) { 13195e05289SChris Mason int token; 13295e05289SChris Mason if (!*p) 13395e05289SChris Mason continue; 13495e05289SChris Mason 13595e05289SChris Mason token = match_token(p, tokens, args); 13695e05289SChris Mason switch (token) { 13795e05289SChris Mason case Opt_subvol: 138be20aa9dSChris Mason if (subvol_name) { 13995e05289SChris Mason *subvol_name = match_strdup(&args[0]); 140be20aa9dSChris Mason } 14195e05289SChris Mason break; 142b6cda9bcSChris Mason case Opt_nodatasum: 143be20aa9dSChris Mason if (info) { 144be20aa9dSChris Mason printk("btrfs: setting nodatacsum\n"); 145b6cda9bcSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 146be20aa9dSChris Mason } 147be20aa9dSChris Mason break; 148be20aa9dSChris Mason case Opt_nodatacow: 149be20aa9dSChris Mason if (info) { 150be20aa9dSChris Mason printk("btrfs: setting nodatacow\n"); 151be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATACOW); 152be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 153be20aa9dSChris Mason } 154b6cda9bcSChris Mason break; 155e18e4809SChris Mason case Opt_ssd: 156e18e4809SChris Mason if (info) { 157e18e4809SChris Mason printk("btrfs: use ssd allocation scheme\n"); 158e18e4809SChris Mason btrfs_set_opt(info->mount_opt, SSD); 159e18e4809SChris Mason } 160e18e4809SChris Mason break; 16121ad10cfSChris Mason case Opt_nobarrier: 16221ad10cfSChris Mason if (info) { 16321ad10cfSChris Mason printk("btrfs: turning off barriers\n"); 16421ad10cfSChris Mason btrfs_set_opt(info->mount_opt, NOBARRIER); 16521ad10cfSChris Mason } 16621ad10cfSChris Mason break; 167c59f8951SChris Mason case Opt_max_extent: 168c59f8951SChris Mason if (info) { 169c59f8951SChris Mason char *num = match_strdup(&args[0]); 170c59f8951SChris Mason if (num) { 171edbd8d4eSChris Mason info->max_extent = 172edbd8d4eSChris Mason btrfs_parse_size(num); 173c59f8951SChris Mason kfree(num); 174c59f8951SChris Mason 175c59f8951SChris Mason info->max_extent = max_t(u64, 176c59f8951SChris Mason info->max_extent, 177c59f8951SChris Mason root->sectorsize); 178c59f8951SChris Mason printk("btrfs: max_extent at %Lu\n", 179c59f8951SChris Mason info->max_extent); 180c59f8951SChris Mason } 181c59f8951SChris Mason } 182c59f8951SChris Mason break; 1836f568d35SChris Mason case Opt_max_inline: 1846f568d35SChris Mason if (info) { 1856f568d35SChris Mason char *num = match_strdup(&args[0]); 1866f568d35SChris Mason if (num) { 1876f568d35SChris Mason info->max_inline = 1886f568d35SChris Mason btrfs_parse_size(num); 1896f568d35SChris Mason kfree(num); 1906f568d35SChris Mason 1916f568d35SChris Mason info->max_inline = max_t(u64, 1926f568d35SChris Mason info->max_inline, 1936f568d35SChris Mason root->sectorsize); 1946f568d35SChris Mason printk("btrfs: max_inline at %Lu\n", 1956f568d35SChris Mason info->max_inline); 1966f568d35SChris Mason } 1976f568d35SChris Mason } 1986f568d35SChris Mason break; 1998f662a76SChris Mason case Opt_alloc_start: 2008f662a76SChris Mason if (info) { 2018f662a76SChris Mason char *num = match_strdup(&args[0]); 2028f662a76SChris Mason if (num) { 2038f662a76SChris Mason info->alloc_start = 2048f662a76SChris Mason btrfs_parse_size(num); 2058f662a76SChris Mason kfree(num); 2068f662a76SChris Mason printk("btrfs: allocations start at " 2078f662a76SChris Mason "%Lu\n", info->alloc_start); 2088f662a76SChris Mason } 2098f662a76SChris Mason } 2108f662a76SChris Mason break; 21195e05289SChris Mason default: 212be20aa9dSChris Mason break; 21395e05289SChris Mason } 21495e05289SChris Mason } 215be20aa9dSChris Mason kfree(options); 21695e05289SChris Mason return 1; 21795e05289SChris Mason } 21895e05289SChris Mason 2192e635a27SChris Mason static int btrfs_fill_super(struct super_block * sb, void * data, int silent) 2202e635a27SChris Mason { 2212e635a27SChris Mason struct inode * inode; 222e20d96d6SChris Mason struct dentry * root_dentry; 223e20d96d6SChris Mason struct btrfs_super_block *disk_super; 2240f7d52f4SChris Mason struct btrfs_root *tree_root; 225d6e4a428SChris Mason struct btrfs_inode *bi; 22639279cc3SChris Mason int err; 2272e635a27SChris Mason 2282e635a27SChris Mason sb->s_maxbytes = MAX_LFS_FILESIZE; 2292e635a27SChris Mason sb->s_magic = BTRFS_SUPER_MAGIC; 230e20d96d6SChris Mason sb->s_op = &btrfs_super_ops; 2315103e947SJosef Bacik sb->s_xattr = btrfs_xattr_handlers; 2322e635a27SChris Mason sb->s_time_gran = 1; 233e20d96d6SChris Mason 2340f7d52f4SChris Mason tree_root = open_ctree(sb); 235d98237b3SChris Mason 23639279cc3SChris Mason if (!tree_root || IS_ERR(tree_root)) { 237e20d96d6SChris Mason printk("btrfs: open_ctree failed\n"); 238e20d96d6SChris Mason return -EIO; 239e20d96d6SChris Mason } 2400f7d52f4SChris Mason sb->s_fs_info = tree_root; 2415f39d397SChris Mason disk_super = &tree_root->fs_info->super_copy; 242c5739bbaSChris Mason inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super), 243c5739bbaSChris Mason tree_root); 244d6e4a428SChris Mason bi = BTRFS_I(inode); 245d6e4a428SChris Mason bi->location.objectid = inode->i_ino; 246d6e4a428SChris Mason bi->location.offset = 0; 2470f7d52f4SChris Mason bi->root = tree_root; 248b888db2bSChris Mason 249d6e4a428SChris Mason btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY); 250d6e4a428SChris Mason 25139279cc3SChris Mason if (!inode) { 25239279cc3SChris Mason err = -ENOMEM; 25339279cc3SChris Mason goto fail_close; 25439279cc3SChris Mason } 255e20d96d6SChris Mason if (inode->i_state & I_NEW) { 256e20d96d6SChris Mason btrfs_read_locked_inode(inode); 257e20d96d6SChris Mason unlock_new_inode(inode); 258e20d96d6SChris Mason } 2592e635a27SChris Mason 260e20d96d6SChris Mason root_dentry = d_alloc_root(inode); 261e20d96d6SChris Mason if (!root_dentry) { 2622e635a27SChris Mason iput(inode); 26339279cc3SChris Mason err = -ENOMEM; 26439279cc3SChris Mason goto fail_close; 2652e635a27SChris Mason } 26658176a96SJosef Bacik 267b6cda9bcSChris Mason parse_options((char *)data, tree_root, NULL); 268b6cda9bcSChris Mason 26958176a96SJosef Bacik /* this does the super kobj at the same time */ 27058176a96SJosef Bacik err = btrfs_sysfs_add_super(tree_root->fs_info); 27158176a96SJosef Bacik if (err) 27258176a96SJosef Bacik goto fail_close; 27358176a96SJosef Bacik 274e20d96d6SChris Mason sb->s_root = root_dentry; 27508607c1bSChris Mason btrfs_transaction_queue_work(tree_root, HZ * 30); 2766885f308SChris Mason 2776885f308SChris Mason #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25) 2786885f308SChris Mason save_mount_options(sb, data); 2796885f308SChris Mason #endif 2806885f308SChris Mason 2812e635a27SChris Mason return 0; 2822e635a27SChris Mason 28339279cc3SChris Mason fail_close: 28439279cc3SChris Mason close_ctree(tree_root); 285d5719762SChris Mason return err; 286d5719762SChris Mason } 287d5719762SChris Mason 288d5719762SChris Mason static int btrfs_sync_fs(struct super_block *sb, int wait) 289d5719762SChris Mason { 290d5719762SChris Mason struct btrfs_trans_handle *trans; 291d5719762SChris Mason struct btrfs_root *root; 292d5719762SChris Mason int ret; 293d98237b3SChris Mason root = btrfs_sb(sb); 294df2ce34cSChris Mason 295d5719762SChris Mason sb->s_dirt = 0; 296d561c025SChris Mason if (!wait) { 2977cfcc17eSChris Mason filemap_flush(root->fs_info->btree_inode->i_mapping); 298df2ce34cSChris Mason return 0; 299d561c025SChris Mason } 300e9d0b13bSChris Mason btrfs_clean_old_snapshots(root); 301d561c025SChris Mason mutex_lock(&root->fs_info->fs_mutex); 302e9d0b13bSChris Mason btrfs_defrag_dirty_roots(root->fs_info); 303d5719762SChris Mason trans = btrfs_start_transaction(root, 1); 304d5719762SChris Mason ret = btrfs_commit_transaction(trans, root); 305d5719762SChris Mason sb->s_dirt = 0; 306d561c025SChris Mason mutex_unlock(&root->fs_info->fs_mutex); 30754aa1f4dSChris Mason return ret; 308d5719762SChris Mason } 309d5719762SChris Mason 310d561c025SChris Mason static void btrfs_write_super(struct super_block *sb) 311d561c025SChris Mason { 31208607c1bSChris Mason sb->s_dirt = 0; 313d561c025SChris Mason } 314d561c025SChris Mason 3154b82d6e4SYan /* 3164b82d6e4SYan * This is almost a copy of get_sb_bdev in fs/super.c. 3174b82d6e4SYan * We need the local copy to allow direct mounting of 3184b82d6e4SYan * subvolumes, but this could be easily integrated back 3194b82d6e4SYan * into the generic version. --hch 3204b82d6e4SYan */ 3214b82d6e4SYan 3224b82d6e4SYan /* start copy & paste */ 3234b82d6e4SYan static int set_bdev_super(struct super_block *s, void *data) 3242e635a27SChris Mason { 3254b82d6e4SYan s->s_bdev = data; 3264b82d6e4SYan s->s_dev = s->s_bdev->bd_dev; 3274b82d6e4SYan return 0; 3284b82d6e4SYan } 3294b82d6e4SYan 3304b82d6e4SYan static int test_bdev_super(struct super_block *s, void *data) 3314b82d6e4SYan { 3324b82d6e4SYan return (void *)s->s_bdev == data; 3334b82d6e4SYan } 3344b82d6e4SYan 3354b82d6e4SYan int btrfs_get_sb_bdev(struct file_system_type *fs_type, 3364b82d6e4SYan int flags, const char *dev_name, void *data, 3374b82d6e4SYan int (*fill_super)(struct super_block *, void *, int), 3384b82d6e4SYan struct vfsmount *mnt, const char *subvol) 3394b82d6e4SYan { 3404b82d6e4SYan struct block_device *bdev = NULL; 3414b82d6e4SYan struct super_block *s; 3424b82d6e4SYan struct dentry *root; 3434b82d6e4SYan int error = 0; 3444b82d6e4SYan 3454b82d6e4SYan bdev = open_bdev_excl(dev_name, flags, fs_type); 3464b82d6e4SYan if (IS_ERR(bdev)) 3474b82d6e4SYan return PTR_ERR(bdev); 3484b82d6e4SYan 3494b82d6e4SYan /* 3504b82d6e4SYan * once the super is inserted into the list by sget, s_umount 3514b82d6e4SYan * will protect the lockfs code from trying to start a snapshot 3524b82d6e4SYan * while we are mounting 3534b82d6e4SYan */ 3544b82d6e4SYan down(&bdev->bd_mount_sem); 3554b82d6e4SYan s = sget(fs_type, test_bdev_super, set_bdev_super, bdev); 3564b82d6e4SYan up(&bdev->bd_mount_sem); 3574b82d6e4SYan if (IS_ERR(s)) 3584b82d6e4SYan goto error_s; 3594b82d6e4SYan 3604b82d6e4SYan if (s->s_root) { 3614b82d6e4SYan if ((flags ^ s->s_flags) & MS_RDONLY) { 3624b82d6e4SYan up_write(&s->s_umount); 3634b82d6e4SYan deactivate_super(s); 3644b82d6e4SYan error = -EBUSY; 3654b82d6e4SYan goto error_bdev; 3664b82d6e4SYan } 3674b82d6e4SYan 3684b82d6e4SYan close_bdev_excl(bdev); 3694b82d6e4SYan } else { 3704b82d6e4SYan char b[BDEVNAME_SIZE]; 3714b82d6e4SYan 3724b82d6e4SYan s->s_flags = flags; 3734b82d6e4SYan strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id)); 3744b82d6e4SYan sb_set_blocksize(s, block_size(bdev)); 3754b82d6e4SYan error = fill_super(s, data, flags & MS_SILENT ? 1 : 0); 3764b82d6e4SYan if (error) { 3774b82d6e4SYan up_write(&s->s_umount); 3784b82d6e4SYan deactivate_super(s); 3794b82d6e4SYan goto error; 3804b82d6e4SYan } 3814b82d6e4SYan 3824b82d6e4SYan s->s_flags |= MS_ACTIVE; 3834b82d6e4SYan } 3844b82d6e4SYan 3854b82d6e4SYan if (subvol) { 3864b82d6e4SYan root = lookup_one_len(subvol, s->s_root, strlen(subvol)); 3874b82d6e4SYan if (IS_ERR(root)) { 3884b82d6e4SYan up_write(&s->s_umount); 3894b82d6e4SYan deactivate_super(s); 3904b82d6e4SYan error = PTR_ERR(root); 3914b82d6e4SYan goto error; 3924b82d6e4SYan } 3934b82d6e4SYan if (!root->d_inode) { 3944b82d6e4SYan dput(root); 3954b82d6e4SYan up_write(&s->s_umount); 3964b82d6e4SYan deactivate_super(s); 3974b82d6e4SYan error = -ENXIO; 3984b82d6e4SYan goto error; 3994b82d6e4SYan } 4004b82d6e4SYan } else { 4014b82d6e4SYan root = dget(s->s_root); 4024b82d6e4SYan } 4034b82d6e4SYan 4044b82d6e4SYan mnt->mnt_sb = s; 4054b82d6e4SYan mnt->mnt_root = root; 4064b82d6e4SYan return 0; 4074b82d6e4SYan 4084b82d6e4SYan error_s: 4094b82d6e4SYan error = PTR_ERR(s); 4104b82d6e4SYan error_bdev: 4114b82d6e4SYan close_bdev_excl(bdev); 4124b82d6e4SYan error: 4134b82d6e4SYan return error; 4144b82d6e4SYan } 4154b82d6e4SYan /* end copy & paste */ 4164b82d6e4SYan 4174b82d6e4SYan static int btrfs_get_sb(struct file_system_type *fs_type, 41895e05289SChris Mason int flags, const char *dev_name, void *data, struct vfsmount *mnt) 4194b82d6e4SYan { 4204b82d6e4SYan int ret; 42195e05289SChris Mason char *subvol_name = NULL; 4224b82d6e4SYan 42395e05289SChris Mason parse_options((char *)data, NULL, &subvol_name); 4244b82d6e4SYan ret = btrfs_get_sb_bdev(fs_type, flags, dev_name, data, 4254b82d6e4SYan btrfs_fill_super, mnt, 4264b82d6e4SYan subvol_name ? subvol_name : "default"); 427c59f8951SChris Mason if (subvol_name) 428c59f8951SChris Mason kfree(subvol_name); 4294b82d6e4SYan return ret; 4302e635a27SChris Mason } 4312e635a27SChris Mason 4328fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) 4338fd17795SChris Mason { 4348fd17795SChris Mason struct btrfs_root *root = btrfs_sb(dentry->d_sb); 4354b52dff6SChris Mason struct btrfs_super_block *disk_super = &root->fs_info->super_copy; 436db94535dSChris Mason int bits = dentry->d_sb->s_blocksize_bits; 4378fd17795SChris Mason 4388fd17795SChris Mason buf->f_namelen = BTRFS_NAME_LEN; 439db94535dSChris Mason buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits; 440db94535dSChris Mason buf->f_bfree = buf->f_blocks - 441db94535dSChris Mason (btrfs_super_bytes_used(disk_super) >> bits); 4428fd17795SChris Mason buf->f_bavail = buf->f_bfree; 4438fd17795SChris Mason buf->f_bsize = dentry->d_sb->s_blocksize; 4448fd17795SChris Mason buf->f_type = BTRFS_SUPER_MAGIC; 4458fd17795SChris Mason return 0; 4468fd17795SChris Mason } 447b5133862SChris Mason 448*a9218f6bSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd, 449*a9218f6bSChris Mason unsigned long arg) 450*a9218f6bSChris Mason { 451*a9218f6bSChris Mason printk("btrfs control ioctl %d\n", cmd); 452*a9218f6bSChris Mason return 0; 453*a9218f6bSChris Mason } 454*a9218f6bSChris Mason 4552e635a27SChris Mason static struct file_system_type btrfs_fs_type = { 4562e635a27SChris Mason .owner = THIS_MODULE, 4572e635a27SChris Mason .name = "btrfs", 4582e635a27SChris Mason .get_sb = btrfs_get_sb, 4592e635a27SChris Mason .kill_sb = kill_block_super, 4602e635a27SChris Mason .fs_flags = FS_REQUIRES_DEV, 4612e635a27SChris Mason }; 462*a9218f6bSChris Mason 463ed0dab6bSYan static void btrfs_write_super_lockfs(struct super_block *sb) 464ed0dab6bSYan { 465ed0dab6bSYan struct btrfs_root *root = btrfs_sb(sb); 466ed0dab6bSYan btrfs_transaction_flush_work(root); 467ed0dab6bSYan } 468ed0dab6bSYan 469ed0dab6bSYan static void btrfs_unlockfs(struct super_block *sb) 470ed0dab6bSYan { 471ed0dab6bSYan struct btrfs_root *root = btrfs_sb(sb); 472ed0dab6bSYan btrfs_transaction_queue_work(root, HZ * 30); 473ed0dab6bSYan } 4742e635a27SChris Mason 475e20d96d6SChris Mason static struct super_operations btrfs_super_ops = { 476134e9731SChris Mason .delete_inode = btrfs_delete_inode, 4772da98f00SChris Mason .put_inode = btrfs_put_inode, 478e20d96d6SChris Mason .put_super = btrfs_put_super, 479d5719762SChris Mason .write_super = btrfs_write_super, 480d5719762SChris Mason .sync_fs = btrfs_sync_fs, 4816885f308SChris Mason #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25) 4826885f308SChris Mason .read_inode = btrfs_read_locked_inode, 4836885f308SChris Mason #else 4846885f308SChris Mason .show_options = generic_show_options, 4856885f308SChris Mason #endif 4864730a4bcSChris Mason .write_inode = btrfs_write_inode, 487b5133862SChris Mason .dirty_inode = btrfs_dirty_inode, 4882c90e5d6SChris Mason .alloc_inode = btrfs_alloc_inode, 4892c90e5d6SChris Mason .destroy_inode = btrfs_destroy_inode, 4908fd17795SChris Mason .statfs = btrfs_statfs, 491ed0dab6bSYan .write_super_lockfs = btrfs_write_super_lockfs, 492ed0dab6bSYan .unlockfs = btrfs_unlockfs, 493e20d96d6SChris Mason }; 494*a9218f6bSChris Mason 495*a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = { 496*a9218f6bSChris Mason .unlocked_ioctl = btrfs_control_ioctl, 497*a9218f6bSChris Mason .compat_ioctl = btrfs_control_ioctl, 498*a9218f6bSChris Mason .owner = THIS_MODULE, 499*a9218f6bSChris Mason }; 500*a9218f6bSChris Mason 501*a9218f6bSChris Mason static struct miscdevice btrfs_misc = { 502*a9218f6bSChris Mason .minor = MISC_DYNAMIC_MINOR, 503*a9218f6bSChris Mason .name = "btrfs-control", 504*a9218f6bSChris Mason .fops = &btrfs_ctl_fops 505*a9218f6bSChris Mason }; 506*a9218f6bSChris Mason 507*a9218f6bSChris Mason static int btrfs_interface_init(void) 508*a9218f6bSChris Mason { 509*a9218f6bSChris Mason return misc_register(&btrfs_misc); 510*a9218f6bSChris Mason } 511*a9218f6bSChris Mason 512*a9218f6bSChris Mason void btrfs_interface_exit(void) 513*a9218f6bSChris Mason { 514*a9218f6bSChris Mason if (misc_deregister(&btrfs_misc) < 0) 515*a9218f6bSChris Mason printk("misc_deregister failed for control device"); 516*a9218f6bSChris Mason } 517*a9218f6bSChris Mason 5182e635a27SChris Mason static int __init init_btrfs_fs(void) 5192e635a27SChris Mason { 5202c90e5d6SChris Mason int err; 52158176a96SJosef Bacik 52258176a96SJosef Bacik err = btrfs_init_sysfs(); 52358176a96SJosef Bacik if (err) 52458176a96SJosef Bacik return err; 52558176a96SJosef Bacik 52608607c1bSChris Mason btrfs_init_transaction_sys(); 52739279cc3SChris Mason err = btrfs_init_cachep(); 5282c90e5d6SChris Mason if (err) 5292f4cbe64SWyatt Banks goto free_transaction_sys; 530d1310b2eSChris Mason 531d1310b2eSChris Mason err = extent_io_init(); 5322f4cbe64SWyatt Banks if (err) 5332f4cbe64SWyatt Banks goto free_cachep; 5342f4cbe64SWyatt Banks 535d1310b2eSChris Mason err = extent_map_init(); 536d1310b2eSChris Mason if (err) 537d1310b2eSChris Mason goto free_extent_io; 538d1310b2eSChris Mason 539*a9218f6bSChris Mason err = btrfs_interface_init(); 5402f4cbe64SWyatt Banks if (err) 5412f4cbe64SWyatt Banks goto free_extent_map; 542*a9218f6bSChris Mason err = register_filesystem(&btrfs_fs_type); 543*a9218f6bSChris Mason if (err) 544*a9218f6bSChris Mason goto unregister_ioctl; 5452f4cbe64SWyatt Banks return 0; 5462f4cbe64SWyatt Banks 547*a9218f6bSChris Mason unregister_ioctl: 548*a9218f6bSChris Mason btrfs_interface_exit(); 5492f4cbe64SWyatt Banks free_extent_map: 5502f4cbe64SWyatt Banks extent_map_exit(); 551d1310b2eSChris Mason free_extent_io: 552d1310b2eSChris Mason extent_io_exit(); 5532f4cbe64SWyatt Banks free_cachep: 5542f4cbe64SWyatt Banks btrfs_destroy_cachep(); 5552f4cbe64SWyatt Banks free_transaction_sys: 5562f4cbe64SWyatt Banks btrfs_exit_transaction_sys(); 5572f4cbe64SWyatt Banks btrfs_exit_sysfs(); 5582c90e5d6SChris Mason return err; 5592e635a27SChris Mason } 5602e635a27SChris Mason 5612e635a27SChris Mason static void __exit exit_btrfs_fs(void) 5622e635a27SChris Mason { 56308607c1bSChris Mason btrfs_exit_transaction_sys(); 56439279cc3SChris Mason btrfs_destroy_cachep(); 565a52d9a80SChris Mason extent_map_exit(); 566d1310b2eSChris Mason extent_io_exit(); 567*a9218f6bSChris Mason btrfs_interface_exit(); 5682e635a27SChris Mason unregister_filesystem(&btrfs_fs_type); 56958176a96SJosef Bacik btrfs_exit_sysfs(); 5702e635a27SChris Mason } 5712e635a27SChris Mason 5722e635a27SChris Mason module_init(init_btrfs_fs) 5732e635a27SChris Mason module_exit(exit_btrfs_fs) 5742e635a27SChris Mason 5752e635a27SChris Mason MODULE_LICENSE("GPL"); 576