| 1 | #include <linux/module.h> |
| 2 | #include <linux/buffer_head.h> |
| 3 | #include <linux/fs.h> |
| 4 | #include <linux/pagemap.h> |
| 5 | #include <linux/highmem.h> |
| 6 | #include <linux/time.h> |
| 7 | #include <linux/init.h> |
| 8 | #include <linux/string.h> |
| 9 | #include <linux/smp_lock.h> |
| 10 | #include <linux/backing-dev.h> |
| 11 | #include <linux/mpage.h> |
| 12 | #include <linux/swap.h> |
| 13 | #include <linux/writeback.h> |
| 14 | #include <linux/statfs.h> |
| 15 | #include <linux/compat.h> |
| 16 | #include "ctree.h" |
| 17 | #include "disk-io.h" |
| 18 | #include "transaction.h" |
| 19 | #include "btrfs_inode.h" |
| 20 | #include "ioctl.h" |
| 21 | #include "print-tree.h" |
| 22 | |
| 23 | #define BTRFS_SUPER_MAGIC 0x9123682E |
| 24 | |
| 25 | static struct super_operations btrfs_super_ops; |
| 26 | |
| 27 | static void btrfs_put_super (struct super_block * sb) |
| 28 | { |
| 29 | struct btrfs_root *root = btrfs_sb(sb); |
| 30 | int ret; |
| 31 | |
| 32 | ret = close_ctree(root); |
| 33 | if (ret) { |
| 34 | printk("close ctree returns %d\n", ret); |
| 35 | } |
| 36 | sb->s_fs_info = NULL; |
| 37 | } |
| 38 | |
| 39 | static int btrfs_fill_super(struct super_block * sb, void * data, int silent) |
| 40 | { |
| 41 | struct inode * inode; |
| 42 | struct dentry * root_dentry; |
| 43 | struct btrfs_super_block *disk_super; |
| 44 | struct btrfs_root *tree_root; |
| 45 | struct btrfs_inode *bi; |
| 46 | int err; |
| 47 | |
| 48 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
| 49 | sb->s_magic = BTRFS_SUPER_MAGIC; |
| 50 | sb->s_op = &btrfs_super_ops; |
| 51 | sb->s_time_gran = 1; |
| 52 | |
| 53 | tree_root = open_ctree(sb); |
| 54 | |
| 55 | if (!tree_root || IS_ERR(tree_root)) { |
| 56 | printk("btrfs: open_ctree failed\n"); |
| 57 | return -EIO; |
| 58 | } |
| 59 | sb->s_fs_info = tree_root; |
| 60 | disk_super = tree_root->fs_info->disk_super; |
| 61 | inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super), |
| 62 | tree_root); |
| 63 | bi = BTRFS_I(inode); |
| 64 | bi->location.objectid = inode->i_ino; |
| 65 | bi->location.offset = 0; |
| 66 | bi->location.flags = 0; |
| 67 | bi->root = tree_root; |
| 68 | btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY); |
| 69 | |
| 70 | if (!inode) { |
| 71 | err = -ENOMEM; |
| 72 | goto fail_close; |
| 73 | } |
| 74 | if (inode->i_state & I_NEW) { |
| 75 | btrfs_read_locked_inode(inode); |
| 76 | unlock_new_inode(inode); |
| 77 | } |
| 78 | |
| 79 | root_dentry = d_alloc_root(inode); |
| 80 | if (!root_dentry) { |
| 81 | iput(inode); |
| 82 | err = -ENOMEM; |
| 83 | goto fail_close; |
| 84 | } |
| 85 | sb->s_root = root_dentry; |
| 86 | btrfs_transaction_queue_work(tree_root, HZ * 30); |
| 87 | return 0; |
| 88 | |
| 89 | fail_close: |
| 90 | close_ctree(tree_root); |
| 91 | return err; |
| 92 | } |
| 93 | |
| 94 | static int btrfs_sync_fs(struct super_block *sb, int wait) |
| 95 | { |
| 96 | struct btrfs_trans_handle *trans; |
| 97 | struct btrfs_root *root; |
| 98 | int ret; |
| 99 | root = btrfs_sb(sb); |
| 100 | |
| 101 | sb->s_dirt = 0; |
| 102 | if (!wait) { |
| 103 | filemap_flush(root->fs_info->btree_inode->i_mapping); |
| 104 | return 0; |
| 105 | } |
| 106 | mutex_lock(&root->fs_info->fs_mutex); |
| 107 | trans = btrfs_start_transaction(root, 1); |
| 108 | ret = btrfs_commit_transaction(trans, root); |
| 109 | sb->s_dirt = 0; |
| 110 | BUG_ON(ret); |
| 111 | mutex_unlock(&root->fs_info->fs_mutex); |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static void btrfs_write_super(struct super_block *sb) |
| 116 | { |
| 117 | sb->s_dirt = 0; |
| 118 | } |
| 119 | |
| 120 | static int btrfs_get_sb(struct file_system_type *fs_type, |
| 121 | int flags, const char *dev_name, void *data, struct vfsmount *mnt) |
| 122 | { |
| 123 | return get_sb_bdev(fs_type, flags, dev_name, data, |
| 124 | btrfs_fill_super, mnt); |
| 125 | } |
| 126 | |
| 127 | static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) |
| 128 | { |
| 129 | struct btrfs_root *root = btrfs_sb(dentry->d_sb); |
| 130 | struct btrfs_super_block *disk_super = root->fs_info->disk_super; |
| 131 | |
| 132 | buf->f_namelen = BTRFS_NAME_LEN; |
| 133 | buf->f_blocks = btrfs_super_total_blocks(disk_super); |
| 134 | buf->f_bfree = buf->f_blocks - btrfs_super_blocks_used(disk_super); |
| 135 | buf->f_bavail = buf->f_bfree; |
| 136 | buf->f_bsize = dentry->d_sb->s_blocksize; |
| 137 | buf->f_type = BTRFS_SUPER_MAGIC; |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static struct file_system_type btrfs_fs_type = { |
| 142 | .owner = THIS_MODULE, |
| 143 | .name = "btrfs", |
| 144 | .get_sb = btrfs_get_sb, |
| 145 | .kill_sb = kill_block_super, |
| 146 | .fs_flags = FS_REQUIRES_DEV, |
| 147 | }; |
| 148 | |
| 149 | static struct super_operations btrfs_super_ops = { |
| 150 | .delete_inode = btrfs_delete_inode, |
| 151 | .put_super = btrfs_put_super, |
| 152 | .read_inode = btrfs_read_locked_inode, |
| 153 | .write_super = btrfs_write_super, |
| 154 | .sync_fs = btrfs_sync_fs, |
| 155 | .write_inode = btrfs_write_inode, |
| 156 | .dirty_inode = btrfs_dirty_inode, |
| 157 | .alloc_inode = btrfs_alloc_inode, |
| 158 | .destroy_inode = btrfs_destroy_inode, |
| 159 | .statfs = btrfs_statfs, |
| 160 | }; |
| 161 | |
| 162 | static int __init init_btrfs_fs(void) |
| 163 | { |
| 164 | int err; |
| 165 | btrfs_init_transaction_sys(); |
| 166 | err = btrfs_init_cachep(); |
| 167 | if (err) |
| 168 | return err; |
| 169 | return register_filesystem(&btrfs_fs_type); |
| 170 | } |
| 171 | |
| 172 | static void __exit exit_btrfs_fs(void) |
| 173 | { |
| 174 | btrfs_exit_transaction_sys(); |
| 175 | btrfs_destroy_cachep(); |
| 176 | unregister_filesystem(&btrfs_fs_type); |
| 177 | } |
| 178 | |
| 179 | module_init(init_btrfs_fs) |
| 180 | module_exit(exit_btrfs_fs) |
| 181 | |
| 182 | MODULE_LICENSE("GPL"); |