Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 1 | #include <linux/module.h> |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2 | #include <linux/buffer_head.h> |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 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> |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 11 | #include <linux/mpage.h> |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 12 | #include <linux/swap.h> |
| 13 | #include <linux/writeback.h> |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 14 | #include <linux/statfs.h> |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 15 | #include <linux/compat.h> |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 16 | #include "ctree.h" |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 17 | #include "disk-io.h" |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 18 | #include "transaction.h" |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 19 | #include "btrfs_inode.h" |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 20 | #include "ioctl.h" |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame] | 21 | #include "print-tree.h" |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 22 | |
| 23 | #define BTRFS_SUPER_MAGIC 0x9123682E |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 24 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 25 | static struct super_operations btrfs_super_ops; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 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 | } |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 38 | |
| 39 | static int btrfs_fill_super(struct super_block * sb, void * data, int silent) |
| 40 | { |
| 41 | struct inode * inode; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 42 | struct dentry * root_dentry; |
| 43 | struct btrfs_super_block *disk_super; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 44 | struct btrfs_root *tree_root; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 45 | struct btrfs_inode *bi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame^] | 46 | int err; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 47 | |
| 48 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 49 | sb->s_magic = BTRFS_SUPER_MAGIC; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 50 | sb->s_op = &btrfs_super_ops; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 51 | sb->s_time_gran = 1; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 52 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 53 | tree_root = open_ctree(sb); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 54 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame^] | 55 | if (!tree_root || IS_ERR(tree_root)) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 56 | printk("btrfs: open_ctree failed\n"); |
| 57 | return -EIO; |
| 58 | } |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 59 | sb->s_fs_info = tree_root; |
| 60 | disk_super = tree_root->fs_info->disk_super; |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 61 | inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super), |
| 62 | tree_root); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 63 | bi = BTRFS_I(inode); |
| 64 | bi->location.objectid = inode->i_ino; |
| 65 | bi->location.offset = 0; |
| 66 | bi->location.flags = 0; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 67 | bi->root = tree_root; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 68 | btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY); |
| 69 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame^] | 70 | if (!inode) { |
| 71 | err = -ENOMEM; |
| 72 | goto fail_close; |
| 73 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 74 | if (inode->i_state & I_NEW) { |
| 75 | btrfs_read_locked_inode(inode); |
| 76 | unlock_new_inode(inode); |
| 77 | } |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 78 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 79 | root_dentry = d_alloc_root(inode); |
| 80 | if (!root_dentry) { |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 81 | iput(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame^] | 82 | err = -ENOMEM; |
| 83 | goto fail_close; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 84 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 85 | sb->s_root = root_dentry; |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 86 | btrfs_transaction_queue_work(tree_root, HZ * 30); |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 87 | return 0; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 88 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame^] | 89 | fail_close: |
| 90 | close_ctree(tree_root); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 91 | return err; |
| 92 | } |
| 93 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 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; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 99 | root = btrfs_sb(sb); |
Chris Mason | df2ce34 | 2007-03-23 11:00:45 -0400 | [diff] [blame] | 100 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 101 | sb->s_dirt = 0; |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 102 | if (!wait) { |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 103 | filemap_flush(root->fs_info->btree_inode->i_mapping); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 104 | return 0; |
| 105 | } |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 106 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 107 | trans = btrfs_start_transaction(root, 1); |
| 108 | ret = btrfs_commit_transaction(trans, root); |
| 109 | sb->s_dirt = 0; |
| 110 | BUG_ON(ret); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 111 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 112 | return 0; |
| 113 | } |
| 114 | |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 115 | static void btrfs_write_super(struct super_block *sb) |
| 116 | { |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 117 | sb->s_dirt = 0; |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 118 | } |
| 119 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 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 | |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 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 | } |
Chris Mason | b513386 | 2007-04-24 11:52:22 -0400 | [diff] [blame] | 140 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 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 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 149 | static struct super_operations btrfs_super_ops = { |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 150 | .delete_inode = btrfs_delete_inode, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 151 | .put_super = btrfs_put_super, |
| 152 | .read_inode = btrfs_read_locked_inode, |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 153 | .write_super = btrfs_write_super, |
| 154 | .sync_fs = btrfs_sync_fs, |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 155 | .write_inode = btrfs_write_inode, |
Chris Mason | b513386 | 2007-04-24 11:52:22 -0400 | [diff] [blame] | 156 | .dirty_inode = btrfs_dirty_inode, |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 157 | .alloc_inode = btrfs_alloc_inode, |
| 158 | .destroy_inode = btrfs_destroy_inode, |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 159 | .statfs = btrfs_statfs, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 160 | }; |
| 161 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 162 | static int __init init_btrfs_fs(void) |
| 163 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 164 | int err; |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 165 | btrfs_init_transaction_sys(); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame^] | 166 | err = btrfs_init_cachep(); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 167 | if (err) |
| 168 | return err; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 169 | return register_filesystem(&btrfs_fs_type); |
| 170 | } |
| 171 | |
| 172 | static void __exit exit_btrfs_fs(void) |
| 173 | { |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 174 | btrfs_exit_transaction_sys(); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame^] | 175 | btrfs_destroy_cachep(); |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 176 | unregister_filesystem(&btrfs_fs_type); |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | module_init(init_btrfs_fs) |
| 180 | module_exit(exit_btrfs_fs) |
| 181 | |
| 182 | MODULE_LICENSE("GPL"); |