Commit | Line | Data |
---|---|---|
2e635a27 | 1 | #include <linux/module.h> |
e20d96d6 | 2 | #include <linux/buffer_head.h> |
2e635a27 CM |
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> | |
dee26a9f | 11 | #include <linux/mpage.h> |
75dfe396 CM |
12 | #include <linux/swap.h> |
13 | #include <linux/writeback.h> | |
8fd17795 | 14 | #include <linux/statfs.h> |
08607c1b | 15 | #include <linux/compat.h> |
2e635a27 | 16 | #include "ctree.h" |
e20d96d6 | 17 | #include "disk-io.h" |
d5719762 | 18 | #include "transaction.h" |
2c90e5d6 | 19 | #include "btrfs_inode.h" |
c5739bba | 20 | #include "ioctl.h" |
3a686375 | 21 | #include "print-tree.h" |
2e635a27 | 22 | |
39279cc3 | 23 | #define BTRFS_SUPER_MAGIC 0x9123682E |
f254e52c | 24 | |
39279cc3 | 25 | static struct super_operations btrfs_super_ops; |
75dfe396 | 26 | |
39279cc3 | 27 | static void btrfs_put_super (struct super_block * sb) |
b18c6685 | 28 | { |
39279cc3 | 29 | struct btrfs_root *root = btrfs_sb(sb); |
b18c6685 | 30 | int ret; |
b18c6685 | 31 | |
39279cc3 CM |
32 | ret = close_ctree(root); |
33 | if (ret) { | |
34 | printk("close ctree returns %d\n", ret); | |
75dfe396 | 35 | } |
39279cc3 | 36 | sb->s_fs_info = NULL; |
75dfe396 CM |
37 | } |
38 | ||
39279cc3 | 39 | static int btrfs_fill_super(struct super_block * sb, void * data, int silent) |
75dfe396 | 40 | { |
39279cc3 CM |
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; | |
a429e513 | 47 | |
39279cc3 CM |
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; | |
a429e513 | 52 | |
39279cc3 | 53 | tree_root = open_ctree(sb); |
6567e837 | 54 | |
39279cc3 CM |
55 | if (!tree_root || IS_ERR(tree_root)) { |
56 | printk("btrfs: open_ctree failed\n"); | |
57 | return -EIO; | |
a429e513 | 58 | } |
39279cc3 CM |
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); | |
a429e513 | 69 | |
39279cc3 | 70 | if (!inode) { |
6567e837 | 71 | err = -ENOMEM; |
39279cc3 | 72 | goto fail_close; |
f254e52c | 73 | } |
39279cc3 CM |
74 | if (inode->i_state & I_NEW) { |
75 | btrfs_read_locked_inode(inode); | |
76 | unlock_new_inode(inode); | |
f254e52c | 77 | } |
f254e52c | 78 | |
39279cc3 CM |
79 | root_dentry = d_alloc_root(inode); |
80 | if (!root_dentry) { | |
81 | iput(inode); | |
82 | err = -ENOMEM; | |
83 | goto fail_close; | |
f254e52c | 84 | } |
39279cc3 CM |
85 | sb->s_root = root_dentry; |
86 | btrfs_transaction_queue_work(tree_root, HZ * 30); | |
2619ba1f | 87 | return 0; |
39279cc3 CM |
88 | |
89 | fail_close: | |
90 | close_ctree(tree_root); | |
91 | return err; | |
2619ba1f CM |
92 | } |
93 | ||
39279cc3 | 94 | static int btrfs_sync_fs(struct super_block *sb, int wait) |
c5739bba CM |
95 | { |
96 | struct btrfs_trans_handle *trans; | |
39279cc3 | 97 | struct btrfs_root *root; |
c5739bba | 98 | int ret; |
39279cc3 | 99 | root = btrfs_sb(sb); |
2619ba1f | 100 | |
39279cc3 CM |
101 | sb->s_dirt = 0; |
102 | if (!wait) { | |
103 | filemap_flush(root->fs_info->btree_inode->i_mapping); | |
104 | return 0; | |
105 | } | |
c5739bba CM |
106 | mutex_lock(&root->fs_info->fs_mutex); |
107 | trans = btrfs_start_transaction(root, 1); | |
c5739bba | 108 | ret = btrfs_commit_transaction(trans, root); |
39279cc3 | 109 | sb->s_dirt = 0; |
c5739bba CM |
110 | BUG_ON(ret); |
111 | mutex_unlock(&root->fs_info->fs_mutex); | |
2c90e5d6 CM |
112 | return 0; |
113 | } | |
114 | ||
39279cc3 | 115 | static void btrfs_write_super(struct super_block *sb) |
2c90e5d6 | 116 | { |
39279cc3 | 117 | sb->s_dirt = 0; |
2c90e5d6 CM |
118 | } |
119 | ||
2e635a27 CM |
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 | ||
8fd17795 CM |
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 | } | |
b5133862 | 140 | |
2e635a27 CM |
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 | ||
e20d96d6 | 149 | static struct super_operations btrfs_super_ops = { |
134e9731 | 150 | .delete_inode = btrfs_delete_inode, |
e20d96d6 CM |
151 | .put_super = btrfs_put_super, |
152 | .read_inode = btrfs_read_locked_inode, | |
d5719762 CM |
153 | .write_super = btrfs_write_super, |
154 | .sync_fs = btrfs_sync_fs, | |
4730a4bc | 155 | .write_inode = btrfs_write_inode, |
b5133862 | 156 | .dirty_inode = btrfs_dirty_inode, |
2c90e5d6 CM |
157 | .alloc_inode = btrfs_alloc_inode, |
158 | .destroy_inode = btrfs_destroy_inode, | |
8fd17795 | 159 | .statfs = btrfs_statfs, |
e20d96d6 CM |
160 | }; |
161 | ||
2e635a27 CM |
162 | static int __init init_btrfs_fs(void) |
163 | { | |
2c90e5d6 | 164 | int err; |
08607c1b | 165 | btrfs_init_transaction_sys(); |
39279cc3 | 166 | err = btrfs_init_cachep(); |
2c90e5d6 CM |
167 | if (err) |
168 | return err; | |
2e635a27 CM |
169 | return register_filesystem(&btrfs_fs_type); |
170 | } | |
171 | ||
172 | static void __exit exit_btrfs_fs(void) | |
173 | { | |
08607c1b | 174 | btrfs_exit_transaction_sys(); |
39279cc3 | 175 | btrfs_destroy_cachep(); |
2e635a27 | 176 | unregister_filesystem(&btrfs_fs_type); |
2e635a27 CM |
177 | } |
178 | ||
179 | module_init(init_btrfs_fs) | |
180 | module_exit(exit_btrfs_fs) | |
181 | ||
182 | MODULE_LICENSE("GPL"); |