blob: 94b3535a2a110832cd32ec3a68daeb5718889327 [file] [log] [blame]
Chris Mason2e635a22007-03-21 11:12:56 -04001#include <linux/module.h>
Chris Masone20d96d2007-03-22 12:13:20 -04002#include <linux/buffer_head.h>
Chris Mason2e635a22007-03-21 11:12:56 -04003#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 Masondee26a92007-03-26 16:00:06 -040011#include <linux/mpage.h>
Chris Mason75dfe392007-03-29 11:56:46 -040012#include <linux/swap.h>
13#include <linux/writeback.h>
Chris Mason8fd17792007-04-19 21:01:03 -040014#include <linux/statfs.h>
Chris Mason08607c12007-06-08 15:33:54 -040015#include <linux/compat.h>
Chris Mason2e635a22007-03-21 11:12:56 -040016#include "ctree.h"
Chris Masone20d96d2007-03-22 12:13:20 -040017#include "disk-io.h"
Chris Masond5719762007-03-23 10:01:08 -040018#include "transaction.h"
Chris Mason2c90e5d2007-04-02 10:50:19 -040019#include "btrfs_inode.h"
Chris Masonc5739bb2007-04-10 09:27:04 -040020#include "ioctl.h"
Chris Mason3a686372007-05-24 13:35:57 -040021#include "print-tree.h"
Chris Mason2e635a22007-03-21 11:12:56 -040022
23#define BTRFS_SUPER_MAGIC 0x9123682E
Chris Masone20d96d2007-03-22 12:13:20 -040024
Chris Masone20d96d2007-03-22 12:13:20 -040025static struct super_operations btrfs_super_ops;
Chris Masone20d96d2007-03-22 12:13:20 -040026
27static 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 Mason2e635a22007-03-21 11:12:56 -040038
39static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
40{
41 struct inode * inode;
Chris Masone20d96d2007-03-22 12:13:20 -040042 struct dentry * root_dentry;
43 struct btrfs_super_block *disk_super;
Chris Mason0f7d52f2007-04-09 10:42:37 -040044 struct btrfs_root *tree_root;
Chris Masond6e4a422007-04-06 15:37:36 -040045 struct btrfs_inode *bi;
Chris Mason39279cc2007-06-12 06:35:45 -040046 int err;
Chris Mason2e635a22007-03-21 11:12:56 -040047
48 sb->s_maxbytes = MAX_LFS_FILESIZE;
Chris Mason2e635a22007-03-21 11:12:56 -040049 sb->s_magic = BTRFS_SUPER_MAGIC;
Chris Masone20d96d2007-03-22 12:13:20 -040050 sb->s_op = &btrfs_super_ops;
Chris Mason2e635a22007-03-21 11:12:56 -040051 sb->s_time_gran = 1;
Chris Masone20d96d2007-03-22 12:13:20 -040052
Chris Mason0f7d52f2007-04-09 10:42:37 -040053 tree_root = open_ctree(sb);
Chris Masond98237b2007-03-28 13:57:48 -040054
Chris Mason39279cc2007-06-12 06:35:45 -040055 if (!tree_root || IS_ERR(tree_root)) {
Chris Masone20d96d2007-03-22 12:13:20 -040056 printk("btrfs: open_ctree failed\n");
57 return -EIO;
58 }
Chris Mason0f7d52f2007-04-09 10:42:37 -040059 sb->s_fs_info = tree_root;
60 disk_super = tree_root->fs_info->disk_super;
Chris Masonc5739bb2007-04-10 09:27:04 -040061 inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
62 tree_root);
Chris Masond6e4a422007-04-06 15:37:36 -040063 bi = BTRFS_I(inode);
64 bi->location.objectid = inode->i_ino;
65 bi->location.offset = 0;
66 bi->location.flags = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -040067 bi->root = tree_root;
Chris Masond6e4a422007-04-06 15:37:36 -040068 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
69
Chris Mason39279cc2007-06-12 06:35:45 -040070 if (!inode) {
71 err = -ENOMEM;
72 goto fail_close;
73 }
Chris Masone20d96d2007-03-22 12:13:20 -040074 if (inode->i_state & I_NEW) {
75 btrfs_read_locked_inode(inode);
76 unlock_new_inode(inode);
77 }
Chris Mason2e635a22007-03-21 11:12:56 -040078
Chris Masone20d96d2007-03-22 12:13:20 -040079 root_dentry = d_alloc_root(inode);
80 if (!root_dentry) {
Chris Mason2e635a22007-03-21 11:12:56 -040081 iput(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040082 err = -ENOMEM;
83 goto fail_close;
Chris Mason2e635a22007-03-21 11:12:56 -040084 }
Chris Masone20d96d2007-03-22 12:13:20 -040085 sb->s_root = root_dentry;
Chris Mason08607c12007-06-08 15:33:54 -040086 btrfs_transaction_queue_work(tree_root, HZ * 30);
Chris Mason2e635a22007-03-21 11:12:56 -040087 return 0;
Chris Mason2e635a22007-03-21 11:12:56 -040088
Chris Mason39279cc2007-06-12 06:35:45 -040089fail_close:
90 close_ctree(tree_root);
Chris Masond5719762007-03-23 10:01:08 -040091 return err;
92}
93
Chris Masond5719762007-03-23 10:01:08 -040094static 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 Masond98237b2007-03-28 13:57:48 -040099 root = btrfs_sb(sb);
Chris Masondf2ce342007-03-23 11:00:45 -0400100
Chris Masond5719762007-03-23 10:01:08 -0400101 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -0400102 if (!wait) {
Chris Mason7cfcc172007-04-02 14:53:59 -0400103 filemap_flush(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400104 return 0;
105 }
Chris Masond561c022007-03-23 19:47:49 -0400106 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400107 trans = btrfs_start_transaction(root, 1);
108 ret = btrfs_commit_transaction(trans, root);
109 sb->s_dirt = 0;
110 BUG_ON(ret);
Chris Masond561c022007-03-23 19:47:49 -0400111 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400112 return 0;
113}
114
Chris Masond561c022007-03-23 19:47:49 -0400115static void btrfs_write_super(struct super_block *sb)
116{
Chris Mason08607c12007-06-08 15:33:54 -0400117 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -0400118}
119
Chris Mason2e635a22007-03-21 11:12:56 -0400120static 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 Mason8fd17792007-04-19 21:01:03 -0400127static 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 Masonb5133862007-04-24 11:52:22 -0400140
Chris Mason2e635a22007-03-21 11:12:56 -0400141static 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 Masone20d96d2007-03-22 12:13:20 -0400149static struct super_operations btrfs_super_ops = {
Chris Mason134e9732007-03-25 13:44:56 -0400150 .delete_inode = btrfs_delete_inode,
Chris Masone20d96d2007-03-22 12:13:20 -0400151 .put_super = btrfs_put_super,
152 .read_inode = btrfs_read_locked_inode,
Chris Masond5719762007-03-23 10:01:08 -0400153 .write_super = btrfs_write_super,
154 .sync_fs = btrfs_sync_fs,
Chris Mason4730a4b2007-03-26 12:00:39 -0400155 .write_inode = btrfs_write_inode,
Chris Masonb5133862007-04-24 11:52:22 -0400156 .dirty_inode = btrfs_dirty_inode,
Chris Mason2c90e5d2007-04-02 10:50:19 -0400157 .alloc_inode = btrfs_alloc_inode,
158 .destroy_inode = btrfs_destroy_inode,
Chris Mason8fd17792007-04-19 21:01:03 -0400159 .statfs = btrfs_statfs,
Chris Masone20d96d2007-03-22 12:13:20 -0400160};
161
Chris Mason2e635a22007-03-21 11:12:56 -0400162static int __init init_btrfs_fs(void)
163{
Chris Mason2c90e5d2007-04-02 10:50:19 -0400164 int err;
Chris Mason08607c12007-06-08 15:33:54 -0400165 btrfs_init_transaction_sys();
Chris Mason39279cc2007-06-12 06:35:45 -0400166 err = btrfs_init_cachep();
Chris Mason2c90e5d2007-04-02 10:50:19 -0400167 if (err)
168 return err;
Chris Mason2e635a22007-03-21 11:12:56 -0400169 return register_filesystem(&btrfs_fs_type);
170}
171
172static void __exit exit_btrfs_fs(void)
173{
Chris Mason08607c12007-06-08 15:33:54 -0400174 btrfs_exit_transaction_sys();
Chris Mason39279cc2007-06-12 06:35:45 -0400175 btrfs_destroy_cachep();
Chris Mason2e635a22007-03-21 11:12:56 -0400176 unregister_filesystem(&btrfs_fs_type);
Chris Mason2e635a22007-03-21 11:12:56 -0400177}
178
179module_init(init_btrfs_fs)
180module_exit(exit_btrfs_fs)
181
182MODULE_LICENSE("GPL");