blob: 9fd87f5edbc9144bfe71d1ce1bb3e8088977d368 [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 Mason2e635a22007-03-21 11:12:56 -040014#include "ctree.h"
Chris Masone20d96d2007-03-22 12:13:20 -040015#include "disk-io.h"
Chris Masond5719762007-03-23 10:01:08 -040016#include "transaction.h"
Chris Mason2c90e5d2007-04-02 10:50:19 -040017#include "btrfs_inode.h"
Chris Mason2e635a22007-03-21 11:12:56 -040018
19#define BTRFS_SUPER_MAGIC 0x9123682E
Chris Masone20d96d2007-03-22 12:13:20 -040020
21static struct inode_operations btrfs_dir_inode_operations;
22static struct super_operations btrfs_super_ops;
23static struct file_operations btrfs_dir_file_operations;
Chris Masondee26a92007-03-26 16:00:06 -040024static struct inode_operations btrfs_file_inode_operations;
25static struct address_space_operations btrfs_aops;
26static struct file_operations btrfs_file_operations;
Chris Masone20d96d2007-03-22 12:13:20 -040027
Chris Mason2c90e5d2007-04-02 10:50:19 -040028static int check_inode(struct inode *inode)
29{
30 struct btrfs_inode *ei = BTRFS_I(inode);
31 WARN_ON(ei->magic != 0xDEADBEEF);
32 WARN_ON(ei->magic2 != 0xDEADBEAF);
33 return 0;
34}
35
Chris Masone20d96d2007-03-22 12:13:20 -040036static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason2e635a22007-03-21 11:12:56 -040037{
Chris Mason5caf2a02007-04-02 11:20:42 -040038 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -040039 struct btrfs_inode_item *inode_item;
40 struct btrfs_root *root = btrfs_sb(inode->i_sb);
41 int ret;
Chris Masonf4b9aa82007-03-27 11:05:53 -040042
Chris Mason5caf2a02007-04-02 11:20:42 -040043 path = btrfs_alloc_path();
44 BUG_ON(!path);
45 btrfs_init_path(path);
Chris Masonf4b9aa82007-03-27 11:05:53 -040046 mutex_lock(&root->fs_info->fs_mutex);
47
Chris Mason2c90e5d2007-04-02 10:50:19 -040048 check_inode(inode);
Chris Mason5caf2a02007-04-02 11:20:42 -040049 ret = btrfs_lookup_inode(NULL, root, path, inode->i_ino, 0);
Chris Masone20d96d2007-03-22 12:13:20 -040050 if (ret) {
Chris Mason5caf2a02007-04-02 11:20:42 -040051 btrfs_release_path(root, path);
Chris Mason7cfcc172007-04-02 14:53:59 -040052 btrfs_free_path(path);
Chris Masonf4b9aa82007-03-27 11:05:53 -040053 mutex_unlock(&root->fs_info->fs_mutex);
54 make_bad_inode(inode);
Chris Masone20d96d2007-03-22 12:13:20 -040055 return;
Chris Mason2e635a22007-03-21 11:12:56 -040056 }
Chris Mason2c90e5d2007-04-02 10:50:19 -040057 check_inode(inode);
Chris Mason5caf2a02007-04-02 11:20:42 -040058 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
59 path->slots[0],
Chris Masone20d96d2007-03-22 12:13:20 -040060 struct btrfs_inode_item);
61
Chris Masone20d96d2007-03-22 12:13:20 -040062 inode->i_mode = btrfs_inode_mode(inode_item);
63 inode->i_nlink = btrfs_inode_nlink(inode_item);
64 inode->i_uid = btrfs_inode_uid(inode_item);
65 inode->i_gid = btrfs_inode_gid(inode_item);
66 inode->i_size = btrfs_inode_size(inode_item);
67 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
68 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
69 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
70 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
71 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
72 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
73 inode->i_blocks = btrfs_inode_nblocks(inode_item);
74 inode->i_generation = btrfs_inode_generation(inode_item);
Chris Mason5caf2a02007-04-02 11:20:42 -040075
76 btrfs_release_path(root, path);
77 btrfs_free_path(path);
78 inode_item = NULL;
79
Chris Masonf4b9aa82007-03-27 11:05:53 -040080 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -040081 check_inode(inode);
Chris Masone20d96d2007-03-22 12:13:20 -040082 switch (inode->i_mode & S_IFMT) {
83#if 0
84 default:
85 init_special_inode(inode, inode->i_mode,
86 btrfs_inode_rdev(inode_item));
87 break;
88#endif
89 case S_IFREG:
Chris Masondee26a92007-03-26 16:00:06 -040090 inode->i_mapping->a_ops = &btrfs_aops;
91 inode->i_fop = &btrfs_file_operations;
92 inode->i_op = &btrfs_file_inode_operations;
Chris Masone20d96d2007-03-22 12:13:20 -040093 break;
94 case S_IFDIR:
Chris Masone20d96d2007-03-22 12:13:20 -040095 inode->i_op = &btrfs_dir_inode_operations;
96 inode->i_fop = &btrfs_dir_file_operations;
97 break;
98 case S_IFLNK:
Chris Masone20d96d2007-03-22 12:13:20 -040099 // inode->i_op = &page_symlink_inode_operations;
100 break;
101 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400102 check_inode(inode);
Chris Masone20d96d2007-03-22 12:13:20 -0400103 return;
Chris Mason2e635a22007-03-21 11:12:56 -0400104}
105
Chris Mason5f443fd2007-03-27 13:42:32 -0400106static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
107 struct btrfs_root *root,
108 struct inode *dir,
109 struct dentry *dentry)
Chris Mason134e9732007-03-25 13:44:56 -0400110{
Chris Mason5caf2a02007-04-02 11:20:42 -0400111 struct btrfs_path *path;
Chris Mason134e9732007-03-25 13:44:56 -0400112 const char *name = dentry->d_name.name;
113 int name_len = dentry->d_name.len;
114 int ret;
115 u64 objectid;
116 struct btrfs_dir_item *di;
117
Chris Mason5caf2a02007-04-02 11:20:42 -0400118 path = btrfs_alloc_path();
119 BUG_ON(!path);
120 btrfs_init_path(path);
121 ret = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
Chris Mason134e9732007-03-25 13:44:56 -0400122 name, name_len, -1);
123 if (ret < 0)
124 goto err;
125 if (ret > 0) {
126 ret = -ENOENT;
127 goto err;
128 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400129 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason134e9732007-03-25 13:44:56 -0400130 struct btrfs_dir_item);
131 objectid = btrfs_dir_objectid(di);
132
Chris Mason5caf2a02007-04-02 11:20:42 -0400133 ret = btrfs_del_item(trans, root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400134 BUG_ON(ret);
135 dentry->d_inode->i_ctime = dir->i_ctime;
136err:
Chris Mason5caf2a02007-04-02 11:20:42 -0400137 btrfs_release_path(root, path);
138 btrfs_free_path(path);
Chris Mason134e9732007-03-25 13:44:56 -0400139 if (ret == 0)
140 inode_dec_link_count(dentry->d_inode);
141 return ret;
142}
143
Chris Mason5f443fd2007-03-27 13:42:32 -0400144static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
145{
146 struct btrfs_root *root;
147 struct btrfs_trans_handle *trans;
148 int ret;
149
150 root = btrfs_sb(dir->i_sb);
151 mutex_lock(&root->fs_info->fs_mutex);
152 trans = btrfs_start_transaction(root, 1);
153 ret = btrfs_unlink_trans(trans, root, dir, dentry);
154 btrfs_end_transaction(trans, root);
155 mutex_unlock(&root->fs_info->fs_mutex);
156 return ret;
157}
158
159static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
160{
161 struct inode *inode = dentry->d_inode;
162 int err;
163 int ret;
164 struct btrfs_root *root = btrfs_sb(dir->i_sb);
Chris Mason5caf2a02007-04-02 11:20:42 -0400165 struct btrfs_path *path;
Chris Mason5f443fd2007-03-27 13:42:32 -0400166 struct btrfs_key key;
167 struct btrfs_trans_handle *trans;
168 struct btrfs_disk_key *found_key;
169 struct btrfs_leaf *leaf;
170
Chris Mason5caf2a02007-04-02 11:20:42 -0400171 path = btrfs_alloc_path();
172 BUG_ON(!path);
173 btrfs_init_path(path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400174 mutex_lock(&root->fs_info->fs_mutex);
175 trans = btrfs_start_transaction(root, 1);
176 key.objectid = inode->i_ino;
177 key.offset = (u64)-1;
178 key.flags = 0;
179 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400180 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Mason5f443fd2007-03-27 13:42:32 -0400181 if (ret < 0) {
182 err = ret;
183 goto out;
184 }
185
186 BUG_ON(ret == 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400187 BUG_ON(path->slots[0] == 0);
188 path->slots[0]--;
189 leaf = btrfs_buffer_leaf(path->nodes[0]);
190 found_key = &leaf->items[path->slots[0]].key;
Chris Mason5f443fd2007-03-27 13:42:32 -0400191 if (btrfs_disk_key_objectid(found_key) != inode->i_ino) {
192 err = -ENOENT;
193 goto out;
194 }
195 if (btrfs_disk_key_type(found_key) != BTRFS_DIR_ITEM_KEY ||
196 btrfs_disk_key_offset(found_key) != 2) {
197 err = -ENOTEMPTY;
198 goto out;
199 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400200 ret = btrfs_del_item(trans, root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400201 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400202 btrfs_release_path(root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400203 key.offset = 1;
Chris Mason5caf2a02007-04-02 11:20:42 -0400204 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Mason5f443fd2007-03-27 13:42:32 -0400205 if (ret < 0) {
206 err = ret;
207 goto out;
208 }
209 if (ret > 0) {
210 err = -ENOTEMPTY;
211 goto out;
212 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400213 ret = btrfs_del_item(trans, root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400214 if (ret) {
215 err = ret;
216 goto out;
217 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400218 btrfs_release_path(root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400219
220 /* now the directory is empty */
221 err = btrfs_unlink_trans(trans, root, dir, dentry);
222 if (!err) {
223 inode->i_size = 0;
224 }
225out:
Chris Mason7cfcc172007-04-02 14:53:59 -0400226 btrfs_release_path(root, path);
227 btrfs_free_path(path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400228 mutex_unlock(&root->fs_info->fs_mutex);
229 ret = btrfs_end_transaction(trans, root);
230 if (ret && !err)
231 err = ret;
232 return err;
233}
234
Chris Mason134e9732007-03-25 13:44:56 -0400235static int btrfs_free_inode(struct btrfs_trans_handle *trans,
236 struct btrfs_root *root,
237 struct inode *inode)
238{
239 u64 objectid = inode->i_ino;
Chris Mason5caf2a02007-04-02 11:20:42 -0400240 struct btrfs_path *path;
Chris Mason134e9732007-03-25 13:44:56 -0400241 struct btrfs_inode_map_item *map;
242 struct btrfs_key stat_data_key;
243 int ret;
Chris Mason5caf2a02007-04-02 11:20:42 -0400244
Chris Mason134e9732007-03-25 13:44:56 -0400245 clear_inode(inode);
Chris Mason5caf2a02007-04-02 11:20:42 -0400246
247 path = btrfs_alloc_path();
248 BUG_ON(!path);
249 btrfs_init_path(path);
250 ret = btrfs_lookup_inode_map(trans, root, path, objectid, -1);
Chris Mason134e9732007-03-25 13:44:56 -0400251 if (ret) {
252 if (ret > 0)
253 ret = -ENOENT;
Chris Mason134e9732007-03-25 13:44:56 -0400254 goto error;
255 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400256 map = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason134e9732007-03-25 13:44:56 -0400257 struct btrfs_inode_map_item);
258 btrfs_disk_key_to_cpu(&stat_data_key, &map->key);
Chris Mason5caf2a02007-04-02 11:20:42 -0400259 ret = btrfs_del_item(trans, root->fs_info->inode_root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400260 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400261 btrfs_release_path(root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400262
Chris Mason5caf2a02007-04-02 11:20:42 -0400263 ret = btrfs_lookup_inode(trans, root, path, objectid, -1);
Chris Mason134e9732007-03-25 13:44:56 -0400264 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400265 ret = btrfs_del_item(trans, root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400266 BUG_ON(ret);
Chris Mason134e9732007-03-25 13:44:56 -0400267error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400268 btrfs_release_path(root, path);
269 btrfs_free_path(path);
Chris Mason134e9732007-03-25 13:44:56 -0400270 return ret;
271}
272
Chris Masonf4b9aa82007-03-27 11:05:53 -0400273static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
274 struct btrfs_root *root,
275 struct inode *inode)
276{
277 int ret;
Chris Mason5caf2a02007-04-02 11:20:42 -0400278 struct btrfs_path *path;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400279 struct btrfs_key key;
280 struct btrfs_disk_key *found_key;
281 struct btrfs_leaf *leaf;
Chris Masonf254e522007-03-29 15:15:27 -0400282 struct btrfs_file_extent_item *fi = NULL;
283 u64 extent_start = 0;
284 u64 extent_num_blocks = 0;
285 int found_extent;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400286
Chris Mason5caf2a02007-04-02 11:20:42 -0400287 path = btrfs_alloc_path();
288 BUG_ON(!path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400289 /* FIXME, add redo link to tree so we don't leak on crash */
290 key.objectid = inode->i_ino;
291 key.offset = (u64)-1;
292 key.flags = 0;
Chris Masonf254e522007-03-29 15:15:27 -0400293 btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400294 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400295 btrfs_init_path(path);
296 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400297 if (ret < 0) {
Chris Masonf4b9aa82007-03-27 11:05:53 -0400298 goto error;
299 }
300 if (ret > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400301 BUG_ON(path->slots[0] == 0);
302 path->slots[0]--;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400303 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400304 leaf = btrfs_buffer_leaf(path->nodes[0]);
305 found_key = &leaf->items[path->slots[0]].key;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400306 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
307 break;
Chris Masonf254e522007-03-29 15:15:27 -0400308 if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY &&
309 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
Chris Masonf4b9aa82007-03-27 11:05:53 -0400310 break;
311 if (btrfs_disk_key_offset(found_key) < inode->i_size)
312 break;
Chris Masonf254e522007-03-29 15:15:27 -0400313 if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400314 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
315 path->slots[0],
Chris Masonf254e522007-03-29 15:15:27 -0400316 struct btrfs_file_extent_item);
317 extent_start = btrfs_file_extent_disk_blocknr(fi);
318 extent_num_blocks =
319 btrfs_file_extent_disk_num_blocks(fi);
320 inode->i_blocks -=
321 btrfs_file_extent_num_blocks(fi) >> 9;
322 found_extent = 1;
323 } else {
324 found_extent = 0;
325 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400326 ret = btrfs_del_item(trans, root, path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400327 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400328 btrfs_release_path(root, path);
Chris Masonf254e522007-03-29 15:15:27 -0400329 if (found_extent) {
330 ret = btrfs_free_extent(trans, root, extent_start,
331 extent_num_blocks, 0);
332 BUG_ON(ret);
333 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400334 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400335 ret = 0;
336error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400337 btrfs_release_path(root, path);
338 btrfs_free_path(path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400339 return ret;
340}
341
Chris Mason134e9732007-03-25 13:44:56 -0400342static void btrfs_delete_inode(struct inode *inode)
343{
344 struct btrfs_trans_handle *trans;
345 struct btrfs_root *root = btrfs_sb(inode->i_sb);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400346 int ret;
347
Chris Mason134e9732007-03-25 13:44:56 -0400348 truncate_inode_pages(&inode->i_data, 0);
349 if (is_bad_inode(inode)) {
350 goto no_delete;
351 }
352 inode->i_size = 0;
Chris Mason134e9732007-03-25 13:44:56 -0400353 mutex_lock(&root->fs_info->fs_mutex);
354 trans = btrfs_start_transaction(root, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400355 if (S_ISREG(inode->i_mode)) {
356 ret = btrfs_truncate_in_trans(trans, root, inode);
357 BUG_ON(ret);
358 }
Chris Mason134e9732007-03-25 13:44:56 -0400359 btrfs_free_inode(trans, root, inode);
360 btrfs_end_transaction(trans, root);
361 mutex_unlock(&root->fs_info->fs_mutex);
362 return;
363no_delete:
364 clear_inode(inode);
365}
366
Chris Masone20d96d2007-03-22 12:13:20 -0400367static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
368 ino_t *ino)
369{
370 const char *name = dentry->d_name.name;
371 int namelen = dentry->d_name.len;
372 struct btrfs_dir_item *di;
Chris Mason5caf2a02007-04-02 11:20:42 -0400373 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -0400374 struct btrfs_root *root = btrfs_sb(dir->i_sb);
375 int ret;
376
Chris Mason5caf2a02007-04-02 11:20:42 -0400377 path = btrfs_alloc_path();
378 BUG_ON(!path);
379 btrfs_init_path(path);
380 ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
Chris Masone20d96d2007-03-22 12:13:20 -0400381 namelen, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400382 if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) {
Chris Masone20d96d2007-03-22 12:13:20 -0400383 *ino = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400384 ret = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400385 goto out;
386 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400387 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masone20d96d2007-03-22 12:13:20 -0400388 struct btrfs_dir_item);
389 *ino = btrfs_dir_objectid(di);
390out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400391 btrfs_release_path(root, path);
392 btrfs_free_path(path);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400393 check_inode(dir);
Chris Masone20d96d2007-03-22 12:13:20 -0400394 return ret;
395}
396
397static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
398 struct nameidata *nd)
399{
400 struct inode * inode;
Chris Mason22b0ebd2007-03-30 08:47:31 -0400401 struct btrfs_root *root = btrfs_sb(dir->i_sb);
Chris Masone20d96d2007-03-22 12:13:20 -0400402 ino_t ino;
403 int ret;
404
405 if (dentry->d_name.len > BTRFS_NAME_LEN)
406 return ERR_PTR(-ENAMETOOLONG);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400407 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400408 ret = btrfs_inode_by_name(dir, dentry, &ino);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400409 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400410 if (ret < 0)
411 return ERR_PTR(ret);
412 inode = NULL;
413 if (ino) {
Chris Masone20d96d2007-03-22 12:13:20 -0400414 inode = iget(dir->i_sb, ino);
415 if (!inode)
416 return ERR_PTR(-EACCES);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400417 check_inode(inode);
Chris Masone20d96d2007-03-22 12:13:20 -0400418 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400419 check_inode(dir);
Chris Masone20d96d2007-03-22 12:13:20 -0400420 return d_splice_alias(inode, dentry);
421}
422
423static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
424{
425 struct inode *inode = filp->f_path.dentry->d_inode;
426 struct btrfs_root *root = btrfs_sb(inode->i_sb);
427 struct btrfs_item *item;
428 struct btrfs_dir_item *di;
429 struct btrfs_key key;
Chris Mason5caf2a02007-04-02 11:20:42 -0400430 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -0400431 int ret;
432 u32 nritems;
433 struct btrfs_leaf *leaf;
434 int slot;
435 int advance;
436 unsigned char d_type = DT_UNKNOWN;
Chris Mason7f5c1512007-03-23 15:56:19 -0400437 int over = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400438
Chris Mason22b0ebd2007-03-30 08:47:31 -0400439 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400440 key.objectid = inode->i_ino;
Chris Masone20d96d2007-03-22 12:13:20 -0400441 key.flags = 0;
442 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
443 key.offset = filp->f_pos;
Chris Mason5caf2a02007-04-02 11:20:42 -0400444 path = btrfs_alloc_path();
445 btrfs_init_path(path);
446 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400447 if (ret < 0) {
448 goto err;
449 }
Chris Mason7f5c1512007-03-23 15:56:19 -0400450 advance = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400451 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400452 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400453 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400454 slot = path->slots[0];
Chris Masondee26a92007-03-26 16:00:06 -0400455 if (advance || slot >= nritems) {
456 if (slot >= nritems -1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400457 ret = btrfs_next_leaf(root, path);
Chris Masone20d96d2007-03-22 12:13:20 -0400458 if (ret)
459 break;
Chris Mason5caf2a02007-04-02 11:20:42 -0400460 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400461 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400462 slot = path->slots[0];
Chris Masone20d96d2007-03-22 12:13:20 -0400463 } else {
464 slot++;
Chris Mason5caf2a02007-04-02 11:20:42 -0400465 path->slots[0]++;
Chris Masone20d96d2007-03-22 12:13:20 -0400466 }
467 }
468 advance = 1;
469 item = leaf->items + slot;
Chris Masone20d96d2007-03-22 12:13:20 -0400470 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
471 break;
472 if (btrfs_disk_key_type(&item->key) != BTRFS_DIR_ITEM_KEY)
473 continue;
Chris Mason7f5c1512007-03-23 15:56:19 -0400474 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
475 continue;
Chris Masondee26a92007-03-26 16:00:06 -0400476
477 advance = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400478 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Chris Masone20d96d2007-03-22 12:13:20 -0400479 over = filldir(dirent, (const char *)(di + 1),
480 btrfs_dir_name_len(di),
481 btrfs_disk_key_offset(&item->key),
482 btrfs_dir_objectid(di), d_type);
Chris Mason7f5c1512007-03-23 15:56:19 -0400483 if (over) {
484 filp->f_pos = btrfs_disk_key_offset(&item->key);
Chris Masone20d96d2007-03-22 12:13:20 -0400485 break;
Chris Mason7f5c1512007-03-23 15:56:19 -0400486 }
Chris Masone20d96d2007-03-22 12:13:20 -0400487 filp->f_pos = btrfs_disk_key_offset(&item->key) + 1;
488 }
Chris Masone20d96d2007-03-22 12:13:20 -0400489 ret = 0;
490err:
Chris Mason5caf2a02007-04-02 11:20:42 -0400491 btrfs_release_path(root, path);
492 btrfs_free_path(path);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400493 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400494 return ret;
495}
496
497static void btrfs_put_super (struct super_block * sb)
498{
499 struct btrfs_root *root = btrfs_sb(sb);
500 int ret;
501
502 ret = close_ctree(root);
503 if (ret) {
504 printk("close ctree returns %d\n", ret);
505 }
506 sb->s_fs_info = NULL;
507}
Chris Mason2e635a22007-03-21 11:12:56 -0400508
509static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
510{
511 struct inode * inode;
Chris Masone20d96d2007-03-22 12:13:20 -0400512 struct dentry * root_dentry;
513 struct btrfs_super_block *disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400514 struct btrfs_root *root;
Chris Mason2e635a22007-03-21 11:12:56 -0400515
516 sb->s_maxbytes = MAX_LFS_FILESIZE;
Chris Mason2e635a22007-03-21 11:12:56 -0400517 sb->s_magic = BTRFS_SUPER_MAGIC;
Chris Masone20d96d2007-03-22 12:13:20 -0400518 sb->s_op = &btrfs_super_ops;
Chris Mason2e635a22007-03-21 11:12:56 -0400519 sb->s_time_gran = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400520
Chris Mason2c90e5d2007-04-02 10:50:19 -0400521 root = open_ctree(sb);
Chris Masond98237b2007-03-28 13:57:48 -0400522
Chris Masone20d96d2007-03-22 12:13:20 -0400523 if (!root) {
524 printk("btrfs: open_ctree failed\n");
525 return -EIO;
526 }
Chris Mason87cbda52007-03-28 19:44:27 -0400527 sb->s_fs_info = root;
528 disk_super = root->fs_info->disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400529 printk("read in super total blocks %Lu root %Lu\n",
530 btrfs_super_total_blocks(disk_super),
531 btrfs_super_root_dir(disk_super));
532
533 inode = iget_locked(sb, btrfs_super_root_dir(disk_super));
Chris Mason2e635a22007-03-21 11:12:56 -0400534 if (!inode)
535 return -ENOMEM;
Chris Masone20d96d2007-03-22 12:13:20 -0400536 if (inode->i_state & I_NEW) {
537 btrfs_read_locked_inode(inode);
538 unlock_new_inode(inode);
539 }
Chris Mason2e635a22007-03-21 11:12:56 -0400540
Chris Masone20d96d2007-03-22 12:13:20 -0400541 root_dentry = d_alloc_root(inode);
542 if (!root_dentry) {
Chris Mason2e635a22007-03-21 11:12:56 -0400543 iput(inode);
544 return -ENOMEM;
545 }
Chris Masone20d96d2007-03-22 12:13:20 -0400546 sb->s_root = root_dentry;
547
Chris Mason2e635a22007-03-21 11:12:56 -0400548 return 0;
549}
550
Chris Masond5719762007-03-23 10:01:08 -0400551static void fill_inode_item(struct btrfs_inode_item *item,
552 struct inode *inode)
553{
554 btrfs_set_inode_uid(item, inode->i_uid);
555 btrfs_set_inode_gid(item, inode->i_gid);
556 btrfs_set_inode_size(item, inode->i_size);
557 btrfs_set_inode_mode(item, inode->i_mode);
558 btrfs_set_inode_nlink(item, inode->i_nlink);
559 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
560 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
561 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
562 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
563 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
564 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
565 btrfs_set_inode_nblocks(item, inode->i_blocks);
566 btrfs_set_inode_generation(item, inode->i_generation);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400567 check_inode(inode);
Chris Masond5719762007-03-23 10:01:08 -0400568}
569
Chris Mason4730a4b2007-03-26 12:00:39 -0400570static int btrfs_update_inode(struct btrfs_trans_handle *trans,
571 struct btrfs_root *root,
572 struct inode *inode)
573{
574 struct btrfs_inode_item *inode_item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400575 struct btrfs_path *path;
Chris Mason4730a4b2007-03-26 12:00:39 -0400576 int ret;
577
Chris Mason5caf2a02007-04-02 11:20:42 -0400578 path = btrfs_alloc_path();
579 BUG_ON(!path);
580 btrfs_init_path(path);
Chris Mason4730a4b2007-03-26 12:00:39 -0400581
Chris Mason5caf2a02007-04-02 11:20:42 -0400582 ret = btrfs_lookup_inode(trans, root, path, inode->i_ino, 1);
Chris Mason4730a4b2007-03-26 12:00:39 -0400583 if (ret) {
584 if (ret > 0)
585 ret = -ENOENT;
586 goto failed;
587 }
588
Chris Mason5caf2a02007-04-02 11:20:42 -0400589 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
590 path->slots[0],
Chris Mason4730a4b2007-03-26 12:00:39 -0400591 struct btrfs_inode_item);
592
593 fill_inode_item(inode_item, inode);
Chris Mason5caf2a02007-04-02 11:20:42 -0400594 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason4730a4b2007-03-26 12:00:39 -0400595failed:
Chris Mason5caf2a02007-04-02 11:20:42 -0400596 btrfs_release_path(root, path);
597 btrfs_free_path(path);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400598 check_inode(inode);
Chris Mason4730a4b2007-03-26 12:00:39 -0400599 return 0;
600}
601
602static int btrfs_write_inode(struct inode *inode, int wait)
603{
604 struct btrfs_root *root = btrfs_sb(inode->i_sb);
605 struct btrfs_trans_handle *trans;
606 int ret;
607
608 mutex_lock(&root->fs_info->fs_mutex);
609 trans = btrfs_start_transaction(root, 1);
610 ret = btrfs_update_inode(trans, root, inode);
611 if (wait)
612 btrfs_commit_transaction(trans, root);
613 else
614 btrfs_end_transaction(trans, root);
615 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400616 check_inode(inode);
Chris Mason4730a4b2007-03-26 12:00:39 -0400617 return ret;
618}
619
Chris Masond5719762007-03-23 10:01:08 -0400620static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
621 struct inode *dir, int mode)
622{
623 struct inode *inode;
624 struct btrfs_inode_item inode_item;
625 struct btrfs_root *root = btrfs_sb(dir->i_sb);
626 struct btrfs_key key;
627 int ret;
628 u64 objectid;
629
630 inode = new_inode(dir->i_sb);
631 if (!inode)
632 return ERR_PTR(-ENOMEM);
633
Chris Mason2c90e5d2007-04-02 10:50:19 -0400634 check_inode(inode);
Chris Masond5719762007-03-23 10:01:08 -0400635 ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
636 BUG_ON(ret);
637
638 inode->i_uid = current->fsuid;
639 inode->i_gid = current->fsgid;
640 inode->i_mode = mode;
641 inode->i_ino = objectid;
642 inode->i_blocks = 0;
643 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
644 fill_inode_item(&inode_item, inode);
645
Chris Masond5719762007-03-23 10:01:08 -0400646 key.objectid = objectid;
647 key.flags = 0;
648 key.offset = 0;
649 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
650 ret = btrfs_insert_inode_map(trans, root, objectid, &key);
651 BUG_ON(ret);
652
653 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
654 BUG_ON(ret);
655
656 insert_inode_hash(inode);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400657 check_inode(inode);
658 check_inode(dir);
Chris Masond5719762007-03-23 10:01:08 -0400659 return inode;
660}
661
662static int btrfs_add_link(struct btrfs_trans_handle *trans,
663 struct dentry *dentry, struct inode *inode)
664{
665 int ret;
666 ret = btrfs_insert_dir_item(trans, btrfs_sb(inode->i_sb),
667 dentry->d_name.name, dentry->d_name.len,
668 dentry->d_parent->d_inode->i_ino,
669 inode->i_ino, 0);
Chris Mason4730a4b2007-03-26 12:00:39 -0400670 if (ret == 0) {
671 dentry->d_parent->d_inode->i_size += dentry->d_name.len;
672 ret = btrfs_update_inode(trans, btrfs_sb(inode->i_sb),
673 dentry->d_parent->d_inode);
674 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400675 check_inode(inode);
676 check_inode(dentry->d_parent->d_inode);
Chris Masond5719762007-03-23 10:01:08 -0400677 return ret;
678}
679
680static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
681 struct dentry *dentry, struct inode *inode)
682{
683 int err = btrfs_add_link(trans, dentry, inode);
684 if (!err) {
685 d_instantiate(dentry, inode);
686 return 0;
687 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400688 if (err > 0)
689 err = -EEXIST;
690 check_inode(inode);
Chris Masond5719762007-03-23 10:01:08 -0400691 return err;
692}
693
694static int btrfs_create(struct inode *dir, struct dentry *dentry,
695 int mode, struct nameidata *nd)
696{
697 struct btrfs_trans_handle *trans;
698 struct btrfs_root *root = btrfs_sb(dir->i_sb);
699 struct inode *inode;
700 int err;
Chris Mason134e9732007-03-25 13:44:56 -0400701 int drop_inode = 0;
Chris Masond5719762007-03-23 10:01:08 -0400702
Chris Masond561c022007-03-23 19:47:49 -0400703 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400704 trans = btrfs_start_transaction(root, 1);
705 inode = btrfs_new_inode(trans, dir, mode);
706 err = PTR_ERR(inode);
707 if (IS_ERR(inode))
Chris Masond561c022007-03-23 19:47:49 -0400708 goto out_unlock;
Chris Masond5719762007-03-23 10:01:08 -0400709 // FIXME mark the inode dirty
710 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason134e9732007-03-25 13:44:56 -0400711 if (err)
712 drop_inode = 1;
Chris Masondee26a92007-03-26 16:00:06 -0400713 else {
714 inode->i_mapping->a_ops = &btrfs_aops;
715 inode->i_fop = &btrfs_file_operations;
716 inode->i_op = &btrfs_file_inode_operations;
717 }
Chris Masond5719762007-03-23 10:01:08 -0400718 dir->i_sb->s_dirt = 1;
Chris Masond561c022007-03-23 19:47:49 -0400719out_unlock:
Chris Mason22b0ebd2007-03-30 08:47:31 -0400720 btrfs_end_transaction(trans, root);
Chris Masond561c022007-03-23 19:47:49 -0400721 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400722 check_inode(inode);
723 check_inode(dir);
724
Chris Mason134e9732007-03-25 13:44:56 -0400725 if (drop_inode) {
726 inode_dec_link_count(inode);
727 iput(inode);
728 }
Chris Masond5719762007-03-23 10:01:08 -0400729 return err;
730}
731
Chris Masonf7922032007-03-25 20:17:36 -0400732static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
733 struct inode *inode, struct inode *dir)
734{
735 struct btrfs_root *root = btrfs_sb(inode->i_sb);
736 int ret;
737 char buf[2];
738 buf[0] = '.';
739 buf[1] = '.';
740
741 ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino,
742 inode->i_ino, 1);
743 if (ret)
744 goto error;
745 ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino,
746 dir->i_ino, 1);
Chris Mason4730a4b2007-03-26 12:00:39 -0400747 if (ret)
748 goto error;
749 inode->i_size = 3;
750 ret = btrfs_update_inode(trans, root, inode);
Chris Masonf7922032007-03-25 20:17:36 -0400751error:
752 return ret;
753}
754
755static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
756{
757 struct inode *inode;
758 struct btrfs_trans_handle *trans;
759 struct btrfs_root *root = btrfs_sb(dir->i_sb);
760 int err = 0;
761 int drop_on_err = 0;
762
763 mutex_lock(&root->fs_info->fs_mutex);
764 trans = btrfs_start_transaction(root, 1);
765 if (IS_ERR(trans)) {
766 err = PTR_ERR(trans);
767 goto out_unlock;
768 }
769 inode = btrfs_new_inode(trans, dir, S_IFDIR | mode);
770 if (IS_ERR(inode)) {
771 err = PTR_ERR(inode);
772 goto out_fail;
773 }
774 drop_on_err = 1;
775 inode->i_op = &btrfs_dir_inode_operations;
776 inode->i_fop = &btrfs_dir_file_operations;
777
778 err = btrfs_make_empty_dir(trans, inode, dir);
779 if (err)
780 goto out_fail;
781 err = btrfs_add_link(trans, dentry, inode);
782 if (err)
783 goto out_fail;
784 d_instantiate(dentry, inode);
Chris Masonf7922032007-03-25 20:17:36 -0400785 drop_on_err = 0;
786
787out_fail:
788 btrfs_end_transaction(trans, root);
789out_unlock:
790 mutex_unlock(&root->fs_info->fs_mutex);
791 if (drop_on_err)
792 iput(inode);
793 return err;
794}
795
Chris Masond5719762007-03-23 10:01:08 -0400796static int btrfs_sync_fs(struct super_block *sb, int wait)
797{
798 struct btrfs_trans_handle *trans;
799 struct btrfs_root *root;
800 int ret;
Chris Masond98237b2007-03-28 13:57:48 -0400801 root = btrfs_sb(sb);
Chris Masondf2ce342007-03-23 11:00:45 -0400802
Chris Masond5719762007-03-23 10:01:08 -0400803 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -0400804 if (!wait) {
Chris Mason7cfcc172007-04-02 14:53:59 -0400805 filemap_flush(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400806 return 0;
807 }
Chris Mason7cfcc172007-04-02 14:53:59 -0400808 filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400809 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400810 trans = btrfs_start_transaction(root, 1);
811 ret = btrfs_commit_transaction(trans, root);
812 sb->s_dirt = 0;
813 BUG_ON(ret);
814printk("btrfs sync_fs\n");
Chris Masond561c022007-03-23 19:47:49 -0400815 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400816 return 0;
817}
818
Chris Mason75dfe392007-03-29 11:56:46 -0400819static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
Chris Masondee26a92007-03-26 16:00:06 -0400820 struct buffer_head *result, int create)
821{
822 int ret;
823 int err = 0;
824 u64 blocknr;
825 u64 extent_start = 0;
826 u64 extent_end = 0;
827 u64 objectid = inode->i_ino;
Chris Mason5caf2a02007-04-02 11:20:42 -0400828 struct btrfs_path *path;
Chris Masondee26a92007-03-26 16:00:06 -0400829 struct btrfs_root *root = btrfs_sb(inode->i_sb);
830 struct btrfs_trans_handle *trans = NULL;
831 struct btrfs_file_extent_item *item;
832 struct btrfs_leaf *leaf;
833 struct btrfs_disk_key *found_key;
834
Chris Mason5caf2a02007-04-02 11:20:42 -0400835 path = btrfs_alloc_path();
836 BUG_ON(!path);
837 btrfs_init_path(path);
Chris Masondee26a92007-03-26 16:00:06 -0400838 if (create)
839 trans = btrfs_start_transaction(root, 1);
840
841
Chris Mason5caf2a02007-04-02 11:20:42 -0400842 ret = btrfs_lookup_file_extent(trans, root, path,
Chris Mason9773a782007-03-27 11:26:26 -0400843 inode->i_ino,
844 iblock << inode->i_blkbits, 0);
Chris Masondee26a92007-03-26 16:00:06 -0400845 if (ret < 0) {
Chris Masondee26a92007-03-26 16:00:06 -0400846 err = ret;
847 goto out;
848 }
849
850 if (ret != 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400851 if (path->slots[0] == 0) {
852 btrfs_release_path(root, path);
Chris Masondee26a92007-03-26 16:00:06 -0400853 goto allocate;
854 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400855 path->slots[0]--;
Chris Masondee26a92007-03-26 16:00:06 -0400856 }
857
Chris Mason5caf2a02007-04-02 11:20:42 -0400858 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masondee26a92007-03-26 16:00:06 -0400859 struct btrfs_file_extent_item);
Chris Mason5caf2a02007-04-02 11:20:42 -0400860 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masondee26a92007-03-26 16:00:06 -0400861 blocknr = btrfs_file_extent_disk_blocknr(item);
862 blocknr += btrfs_file_extent_offset(item);
863
864 /* exact match found, use it */
865 if (ret == 0) {
866 err = 0;
867 map_bh(result, inode->i_sb, blocknr);
Chris Masondee26a92007-03-26 16:00:06 -0400868 goto out;
869 }
870
871 /* are we inside the extent that was found? */
Chris Mason5caf2a02007-04-02 11:20:42 -0400872 found_key = &leaf->items[path->slots[0]].key;
Chris Masondee26a92007-03-26 16:00:06 -0400873 if (btrfs_disk_key_objectid(found_key) != objectid ||
874 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
875 extent_end = 0;
876 extent_start = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400877 btrfs_release_path(root, path);
Chris Masondee26a92007-03-26 16:00:06 -0400878 goto allocate;
879 }
880
Chris Mason5caf2a02007-04-02 11:20:42 -0400881 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
Chris Mason9773a782007-03-27 11:26:26 -0400882 extent_start = extent_start >> inode->i_blkbits;
Chris Masondee26a92007-03-26 16:00:06 -0400883 extent_start += btrfs_file_extent_offset(item);
884 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
Chris Masondee26a92007-03-26 16:00:06 -0400885 if (iblock >= extent_start && iblock < extent_end) {
886 err = 0;
887 map_bh(result, inode->i_sb, blocknr + iblock - extent_start);
888 goto out;
889 }
890allocate:
891 /* ok, create a new extent */
892 if (!create) {
893 err = 0;
894 goto out;
895 }
Chris Mason9773a782007-03-27 11:26:26 -0400896 ret = btrfs_alloc_file_extent(trans, root, objectid,
897 iblock << inode->i_blkbits,
Chris Masondee26a92007-03-26 16:00:06 -0400898 1, extent_end, &blocknr);
899 if (ret) {
900 err = ret;
901 goto out;
902 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400903 inode->i_blocks += inode->i_sb->s_blocksize >> 9;
904 set_buffer_new(result);
Chris Masondee26a92007-03-26 16:00:06 -0400905 map_bh(result, inode->i_sb, blocknr);
906
907out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400908 btrfs_release_path(root, path);
909 btrfs_free_path(path);
Chris Masondee26a92007-03-26 16:00:06 -0400910 if (trans)
911 btrfs_end_transaction(trans, root);
Chris Mason75dfe392007-03-29 11:56:46 -0400912 return err;
913}
914
915static int btrfs_get_block(struct inode *inode, sector_t iblock,
916 struct buffer_head *result, int create)
917{
918 int err;
919 struct btrfs_root *root = btrfs_sb(inode->i_sb);
920 mutex_lock(&root->fs_info->fs_mutex);
921 err = btrfs_get_block_lock(inode, iblock, result, create);
Chris Masondee26a92007-03-26 16:00:06 -0400922 mutex_unlock(&root->fs_info->fs_mutex);
923 return err;
924}
925
926static int btrfs_prepare_write(struct file *file, struct page *page,
927 unsigned from, unsigned to)
928{
Chris Mason75dfe392007-03-29 11:56:46 -0400929 WARN_ON(1);
Chris Mason6407bf62007-03-27 06:33:00 -0400930 return nobh_prepare_write(page, from, to, btrfs_get_block);
Chris Masondee26a92007-03-26 16:00:06 -0400931}
Chris Mason75dfe392007-03-29 11:56:46 -0400932static int btrfs_commit_write(struct file *file, struct page *page,
933 unsigned from, unsigned to)
934{
935 WARN_ON(1);
936 return nobh_commit_write(file, page, from, to);
937}
Chris Masondee26a92007-03-26 16:00:06 -0400938
Chris Masond561c022007-03-23 19:47:49 -0400939static void btrfs_write_super(struct super_block *sb)
940{
941 btrfs_sync_fs(sb, 1);
942}
943
Chris Masondee26a92007-03-26 16:00:06 -0400944static int btrfs_readpage(struct file *file, struct page *page)
945{
946 return mpage_readpage(page, btrfs_get_block);
947}
948
949static int btrfs_readpages(struct file *file, struct address_space *mapping,
950 struct list_head *pages, unsigned nr_pages)
951{
952 return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block);
953}
954
955static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
956{
Chris Mason6407bf62007-03-27 06:33:00 -0400957 return nobh_writepage(page, btrfs_get_block, wbc);
Chris Masondee26a92007-03-26 16:00:06 -0400958}
Chris Masond561c022007-03-23 19:47:49 -0400959
Chris Masonf4b9aa82007-03-27 11:05:53 -0400960static void btrfs_truncate(struct inode *inode)
961{
962 struct btrfs_root *root = btrfs_sb(inode->i_sb);
963 int ret;
964 struct btrfs_trans_handle *trans;
965
966 if (!S_ISREG(inode->i_mode))
967 return;
968 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
969 return;
970
971 nobh_truncate_page(inode->i_mapping, inode->i_size);
972
973 /* FIXME, add redo link to tree so we don't leak on crash */
974 mutex_lock(&root->fs_info->fs_mutex);
975 trans = btrfs_start_transaction(root, 1);
976 ret = btrfs_truncate_in_trans(trans, root, inode);
977 BUG_ON(ret);
978 ret = btrfs_end_transaction(trans, root);
979 BUG_ON(ret);
980 mutex_unlock(&root->fs_info->fs_mutex);
981 mark_inode_dirty(inode);
982}
983
Chris Mason75dfe392007-03-29 11:56:46 -0400984static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
985 struct page **prepared_pages,
986 const char __user * buf)
987{
988 long page_fault = 0;
989 int i;
990 int offset = pos & (PAGE_CACHE_SIZE - 1);
991
992 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
993 size_t count = min_t(size_t,
994 PAGE_CACHE_SIZE - offset, write_bytes);
995 struct page *page = prepared_pages[i];
996 fault_in_pages_readable(buf, count);
997
998 /* Copy data from userspace to the current page */
999 kmap(page);
1000 page_fault = __copy_from_user(page_address(page) + offset,
1001 buf, count);
1002 /* Flush processor's dcache for this page */
1003 flush_dcache_page(page);
1004 kunmap(page);
1005 buf += count;
1006 write_bytes -= count;
1007
1008 if (page_fault)
1009 break;
1010 }
1011 return page_fault ? -EFAULT : 0;
1012}
1013
1014static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1015{
1016 size_t i;
1017 for (i = 0; i < num_pages; i++) {
1018 if (!pages[i])
1019 break;
1020 unlock_page(pages[i]);
1021 mark_page_accessed(pages[i]);
1022 page_cache_release(pages[i]);
1023 }
1024}
1025static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1026 struct btrfs_root *root,
1027 struct file *file,
1028 struct page **pages,
1029 size_t num_pages,
1030 loff_t pos,
1031 size_t write_bytes)
1032{
1033 int i;
1034 int offset;
1035 int err = 0;
1036 int ret;
1037 int this_write;
Chris Masonf254e522007-03-29 15:15:27 -04001038 struct inode *inode = file->f_path.dentry->d_inode;
Chris Mason75dfe392007-03-29 11:56:46 -04001039
1040 for (i = 0; i < num_pages; i++) {
1041 offset = pos & (PAGE_CACHE_SIZE -1);
1042 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
Chris Masonf254e522007-03-29 15:15:27 -04001043 /* FIXME, one block at a time */
1044
1045 mutex_lock(&root->fs_info->fs_mutex);
1046 trans = btrfs_start_transaction(root, 1);
1047 btrfs_csum_file_block(trans, root, inode->i_ino,
1048 pages[i]->index << PAGE_CACHE_SHIFT,
1049 kmap(pages[i]), PAGE_CACHE_SIZE);
1050 kunmap(pages[i]);
1051 SetPageChecked(pages[i]);
1052 ret = btrfs_end_transaction(trans, root);
1053 BUG_ON(ret);
1054 mutex_unlock(&root->fs_info->fs_mutex);
1055
Chris Mason75dfe392007-03-29 11:56:46 -04001056 ret = nobh_commit_write(file, pages[i], offset,
1057 offset + this_write);
1058 pos += this_write;
1059 if (ret) {
1060 err = ret;
1061 goto failed;
1062 }
1063 WARN_ON(this_write > write_bytes);
1064 write_bytes -= this_write;
1065 }
1066failed:
1067 return err;
1068}
1069
1070static int prepare_pages(struct btrfs_trans_handle *trans,
1071 struct btrfs_root *root,
1072 struct file *file,
1073 struct page **pages,
1074 size_t num_pages,
1075 loff_t pos,
1076 size_t write_bytes)
1077{
1078 int i;
1079 unsigned long index = pos >> PAGE_CACHE_SHIFT;
1080 struct inode *inode = file->f_path.dentry->d_inode;
1081 int offset;
1082 int err = 0;
1083 int ret;
1084 int this_write;
1085 loff_t isize = i_size_read(inode);
1086
1087 memset(pages, 0, num_pages * sizeof(struct page *));
1088
1089 for (i = 0; i < num_pages; i++) {
1090 pages[i] = grab_cache_page(inode->i_mapping, index + i);
1091 if (!pages[i]) {
1092 err = -ENOMEM;
1093 goto failed_release;
1094 }
1095 offset = pos & (PAGE_CACHE_SIZE -1);
1096 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1097 ret = nobh_prepare_write(pages[i], offset,
1098 offset + this_write,
Chris Masonf254e522007-03-29 15:15:27 -04001099 btrfs_get_block);
Chris Mason75dfe392007-03-29 11:56:46 -04001100 pos += this_write;
1101 if (ret) {
1102 err = ret;
1103 goto failed_truncate;
1104 }
1105 WARN_ON(this_write > write_bytes);
1106 write_bytes -= this_write;
1107 }
1108 return 0;
1109
1110failed_release:
1111 btrfs_drop_pages(pages, num_pages);
1112 return err;
1113
1114failed_truncate:
1115 btrfs_drop_pages(pages, num_pages);
1116 if (pos > isize)
1117 vmtruncate(inode, isize);
1118 return err;
1119}
1120
1121static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1122 size_t count, loff_t *ppos)
1123{
1124 loff_t pos;
1125 size_t num_written = 0;
1126 int err = 0;
1127 int ret = 0;
Chris Mason75dfe392007-03-29 11:56:46 -04001128 struct inode *inode = file->f_path.dentry->d_inode;
1129 struct btrfs_root *root = btrfs_sb(inode->i_sb);
1130 struct page *pages[1];
1131
1132 if (file->f_flags & O_DIRECT)
1133 return -EINVAL;
1134 pos = *ppos;
1135
1136 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1137 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1138 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1139 if (err)
1140 goto out;
1141 if (count == 0)
1142 goto out;
1143 err = remove_suid(file->f_path.dentry);
1144 if (err)
1145 goto out;
1146 file_update_time(file);
1147 mutex_lock(&inode->i_mutex);
1148 while(count > 0) {
1149 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1150 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1151 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
1152 PAGE_CACHE_SHIFT;
Chris Masonf254e522007-03-29 15:15:27 -04001153 ret = prepare_pages(NULL, root, file, pages, num_pages,
Chris Mason75dfe392007-03-29 11:56:46 -04001154 pos, write_bytes);
1155 BUG_ON(ret);
1156 ret = btrfs_copy_from_user(pos, num_pages,
1157 write_bytes, pages, buf);
1158 BUG_ON(ret);
1159
Chris Masonf254e522007-03-29 15:15:27 -04001160 ret = dirty_and_release_pages(NULL, root, file, pages,
Chris Mason75dfe392007-03-29 11:56:46 -04001161 num_pages, pos, write_bytes);
1162 BUG_ON(ret);
1163 btrfs_drop_pages(pages, num_pages);
1164
Chris Mason75dfe392007-03-29 11:56:46 -04001165 buf += write_bytes;
1166 count -= write_bytes;
1167 pos += write_bytes;
1168 num_written += write_bytes;
1169
1170 balance_dirty_pages_ratelimited(inode->i_mapping);
1171 cond_resched();
1172 }
1173 mutex_unlock(&inode->i_mutex);
1174out:
1175 *ppos = pos;
1176 current->backing_dev_info = NULL;
1177 return num_written ? num_written : err;
1178}
1179
Chris Masonf254e522007-03-29 15:15:27 -04001180static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
1181 unsigned long offset, unsigned long size)
1182{
1183 char *kaddr;
1184 unsigned long left, count = desc->count;
1185
1186 if (size > count)
1187 size = count;
1188
1189 if (!PageChecked(page)) {
1190 /* FIXME, do it per block */
1191 struct btrfs_root *root = btrfs_sb(page->mapping->host->i_sb);
1192 int ret = btrfs_csum_verify_file_block(root,
1193 page->mapping->host->i_ino,
1194 page->index << PAGE_CACHE_SHIFT,
1195 kmap(page), PAGE_CACHE_SIZE);
1196 if (ret) {
1197 printk("failed to verify ino %lu page %lu\n",
1198 page->mapping->host->i_ino,
1199 page->index);
1200 memset(page_address(page), 0, PAGE_CACHE_SIZE);
1201 }
1202 SetPageChecked(page);
1203 kunmap(page);
1204 }
1205 /*
1206 * Faults on the destination of a read are common, so do it before
1207 * taking the kmap.
1208 */
1209 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1210 kaddr = kmap_atomic(page, KM_USER0);
1211 left = __copy_to_user_inatomic(desc->arg.buf,
1212 kaddr + offset, size);
1213 kunmap_atomic(kaddr, KM_USER0);
1214 if (left == 0)
1215 goto success;
1216 }
1217
1218 /* Do it the slow way */
1219 kaddr = kmap(page);
1220 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1221 kunmap(page);
1222
1223 if (left) {
1224 size -= left;
1225 desc->error = -EFAULT;
1226 }
1227success:
1228 desc->count = count - size;
1229 desc->written += size;
1230 desc->arg.buf += size;
1231 return size;
1232}
1233
1234/**
1235 * btrfs_file_aio_read - filesystem read routine
1236 * @iocb: kernel I/O control block
1237 * @iov: io vector request
1238 * @nr_segs: number of segments in the iovec
1239 * @pos: current file position
1240 */
1241static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1242 unsigned long nr_segs, loff_t pos)
1243{
1244 struct file *filp = iocb->ki_filp;
1245 ssize_t retval;
1246 unsigned long seg;
1247 size_t count;
1248 loff_t *ppos = &iocb->ki_pos;
1249
1250 count = 0;
1251 for (seg = 0; seg < nr_segs; seg++) {
1252 const struct iovec *iv = &iov[seg];
1253
1254 /*
1255 * If any segment has a negative length, or the cumulative
1256 * length ever wraps negative then return -EINVAL.
1257 */
1258 count += iv->iov_len;
1259 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
1260 return -EINVAL;
1261 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
1262 continue;
1263 if (seg == 0)
1264 return -EFAULT;
1265 nr_segs = seg;
1266 count -= iv->iov_len; /* This segment is no good */
1267 break;
1268 }
1269 retval = 0;
1270 if (count) {
1271 for (seg = 0; seg < nr_segs; seg++) {
1272 read_descriptor_t desc;
1273
1274 desc.written = 0;
1275 desc.arg.buf = iov[seg].iov_base;
1276 desc.count = iov[seg].iov_len;
1277 if (desc.count == 0)
1278 continue;
1279 desc.error = 0;
1280 do_generic_file_read(filp, ppos, &desc,
1281 btrfs_read_actor);
1282 retval += desc.written;
1283 if (desc.error) {
1284 retval = retval ?: desc.error;
1285 break;
1286 }
1287 }
1288 }
1289 return retval;
1290}
1291
Chris Mason2c90e5d2007-04-02 10:50:19 -04001292static struct kmem_cache *btrfs_inode_cachep;
1293struct kmem_cache *btrfs_trans_handle_cachep;
1294struct kmem_cache *btrfs_transaction_cachep;
1295struct kmem_cache *btrfs_bit_radix_cachep;
1296struct kmem_cache *btrfs_path_cachep;
1297
1298/*
1299 * Called inside transaction, so use GFP_NOFS
1300 */
1301static struct inode *btrfs_alloc_inode(struct super_block *sb)
1302{
1303 struct btrfs_inode *ei;
1304
1305 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
1306 if (!ei)
1307 return NULL;
1308 ei->magic = 0xDEADBEEF;
1309 ei->magic2 = 0xDEADBEAF;
1310 return &ei->vfs_inode;
1311}
1312
1313static void btrfs_destroy_inode(struct inode *inode)
1314{
1315 struct btrfs_inode *ei = BTRFS_I(inode);
1316 WARN_ON(ei->magic != 0xDEADBEEF);
1317 WARN_ON(ei->magic2 != 0xDEADBEAF);
1318 WARN_ON(!list_empty(&inode->i_dentry));
1319 WARN_ON(inode->i_ino == 1);
1320 WARN_ON(inode->i_data.nrpages);
1321
1322 ei->magic = 0;
1323 ei->magic2 = 0;
1324 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
1325}
1326
1327static void init_once(void * foo, struct kmem_cache * cachep,
1328 unsigned long flags)
1329{
1330 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
1331
1332 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1333 SLAB_CTOR_CONSTRUCTOR) {
1334 inode_init_once(&ei->vfs_inode);
1335 }
1336}
1337
1338static int init_inodecache(void)
1339{
1340 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
1341 sizeof(struct btrfs_inode),
1342 0, (SLAB_RECLAIM_ACCOUNT|
1343 SLAB_MEM_SPREAD),
1344 init_once, NULL);
1345 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
1346 sizeof(struct btrfs_trans_handle),
1347 0, (SLAB_RECLAIM_ACCOUNT|
1348 SLAB_MEM_SPREAD),
1349 NULL, NULL);
1350 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
1351 sizeof(struct btrfs_transaction),
1352 0, (SLAB_RECLAIM_ACCOUNT|
1353 SLAB_MEM_SPREAD),
1354 NULL, NULL);
1355 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
1356 sizeof(struct btrfs_transaction),
1357 0, (SLAB_RECLAIM_ACCOUNT|
1358 SLAB_MEM_SPREAD),
1359 NULL, NULL);
1360 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
1361 256,
1362 0, (SLAB_RECLAIM_ACCOUNT|
1363 SLAB_MEM_SPREAD |
1364 SLAB_DESTROY_BY_RCU),
1365 NULL, NULL);
1366 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
1367 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
1368 return -ENOMEM;
1369 return 0;
1370}
1371
1372static void destroy_inodecache(void)
1373{
1374 kmem_cache_destroy(btrfs_inode_cachep);
1375 kmem_cache_destroy(btrfs_trans_handle_cachep);
1376 kmem_cache_destroy(btrfs_transaction_cachep);
1377 kmem_cache_destroy(btrfs_bit_radix_cachep);
1378 kmem_cache_destroy(btrfs_path_cachep);
1379}
1380
Chris Mason2e635a22007-03-21 11:12:56 -04001381static int btrfs_get_sb(struct file_system_type *fs_type,
1382 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1383{
1384 return get_sb_bdev(fs_type, flags, dev_name, data,
1385 btrfs_fill_super, mnt);
1386}
1387
1388static struct file_system_type btrfs_fs_type = {
1389 .owner = THIS_MODULE,
1390 .name = "btrfs",
1391 .get_sb = btrfs_get_sb,
1392 .kill_sb = kill_block_super,
1393 .fs_flags = FS_REQUIRES_DEV,
1394};
1395
Chris Masone20d96d2007-03-22 12:13:20 -04001396static struct super_operations btrfs_super_ops = {
1397 .statfs = simple_statfs,
Chris Mason134e9732007-03-25 13:44:56 -04001398 .delete_inode = btrfs_delete_inode,
Chris Masone20d96d2007-03-22 12:13:20 -04001399 .put_super = btrfs_put_super,
1400 .read_inode = btrfs_read_locked_inode,
Chris Masond5719762007-03-23 10:01:08 -04001401 .write_super = btrfs_write_super,
1402 .sync_fs = btrfs_sync_fs,
Chris Mason4730a4b2007-03-26 12:00:39 -04001403 .write_inode = btrfs_write_inode,
Chris Mason2c90e5d2007-04-02 10:50:19 -04001404 .alloc_inode = btrfs_alloc_inode,
1405 .destroy_inode = btrfs_destroy_inode,
Chris Masone20d96d2007-03-22 12:13:20 -04001406};
1407
1408static struct inode_operations btrfs_dir_inode_operations = {
1409 .lookup = btrfs_lookup,
Chris Masond5719762007-03-23 10:01:08 -04001410 .create = btrfs_create,
Chris Mason134e9732007-03-25 13:44:56 -04001411 .unlink = btrfs_unlink,
Chris Masonf7922032007-03-25 20:17:36 -04001412 .mkdir = btrfs_mkdir,
Chris Mason5f443fd2007-03-27 13:42:32 -04001413 .rmdir = btrfs_rmdir,
Chris Masone20d96d2007-03-22 12:13:20 -04001414};
1415
1416static struct file_operations btrfs_dir_file_operations = {
1417 .llseek = generic_file_llseek,
1418 .read = generic_read_dir,
1419 .readdir = btrfs_readdir,
1420};
1421
Chris Masondee26a92007-03-26 16:00:06 -04001422static struct address_space_operations btrfs_aops = {
1423 .readpage = btrfs_readpage,
1424 .readpages = btrfs_readpages,
1425 .writepage = btrfs_writepage,
1426 .sync_page = block_sync_page,
1427 .prepare_write = btrfs_prepare_write,
Chris Mason75dfe392007-03-29 11:56:46 -04001428 .commit_write = btrfs_commit_write,
Chris Masondee26a92007-03-26 16:00:06 -04001429};
1430
1431static struct inode_operations btrfs_file_inode_operations = {
Chris Masonf4b9aa82007-03-27 11:05:53 -04001432 .truncate = btrfs_truncate,
Chris Masondee26a92007-03-26 16:00:06 -04001433};
1434
1435static struct file_operations btrfs_file_operations = {
1436 .llseek = generic_file_llseek,
1437 .read = do_sync_read,
Chris Masonf254e522007-03-29 15:15:27 -04001438 .aio_read = btrfs_file_aio_read,
Chris Mason75dfe392007-03-29 11:56:46 -04001439 .write = btrfs_file_write,
Chris Masondee26a92007-03-26 16:00:06 -04001440 .mmap = generic_file_mmap,
1441 .open = generic_file_open,
Chris Masondee26a92007-03-26 16:00:06 -04001442};
Chris Masone20d96d2007-03-22 12:13:20 -04001443
Chris Mason2e635a22007-03-21 11:12:56 -04001444static int __init init_btrfs_fs(void)
1445{
Chris Mason2c90e5d2007-04-02 10:50:19 -04001446 int err;
Chris Mason2e635a22007-03-21 11:12:56 -04001447 printk("btrfs loaded!\n");
Chris Mason2c90e5d2007-04-02 10:50:19 -04001448 err = init_inodecache();
1449 if (err)
1450 return err;
Chris Mason2e635a22007-03-21 11:12:56 -04001451 return register_filesystem(&btrfs_fs_type);
1452}
1453
1454static void __exit exit_btrfs_fs(void)
1455{
Chris Mason2c90e5d2007-04-02 10:50:19 -04001456 destroy_inodecache();
Chris Mason2e635a22007-03-21 11:12:56 -04001457 unregister_filesystem(&btrfs_fs_type);
1458 printk("btrfs unloaded\n");
1459}
1460
1461module_init(init_btrfs_fs)
1462module_exit(exit_btrfs_fs)
1463
1464MODULE_LICENSE("GPL");