blob: c260fcad17b3ae082c0554810313180059a040b3 [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 Masonf4b9aa82007-03-27 11:05:53 -040052 mutex_unlock(&root->fs_info->fs_mutex);
53 make_bad_inode(inode);
Chris Masone20d96d2007-03-22 12:13:20 -040054 return;
Chris Mason2e635a22007-03-21 11:12:56 -040055 }
Chris Mason2c90e5d2007-04-02 10:50:19 -040056 check_inode(inode);
Chris Mason5caf2a02007-04-02 11:20:42 -040057 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
58 path->slots[0],
Chris Masone20d96d2007-03-22 12:13:20 -040059 struct btrfs_inode_item);
60
Chris Masone20d96d2007-03-22 12:13:20 -040061 inode->i_mode = btrfs_inode_mode(inode_item);
62 inode->i_nlink = btrfs_inode_nlink(inode_item);
63 inode->i_uid = btrfs_inode_uid(inode_item);
64 inode->i_gid = btrfs_inode_gid(inode_item);
65 inode->i_size = btrfs_inode_size(inode_item);
66 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
67 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
68 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
69 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
70 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
71 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
72 inode->i_blocks = btrfs_inode_nblocks(inode_item);
73 inode->i_generation = btrfs_inode_generation(inode_item);
Chris Mason5caf2a02007-04-02 11:20:42 -040074
75 btrfs_release_path(root, path);
76 btrfs_free_path(path);
77 inode_item = NULL;
78
Chris Masonf4b9aa82007-03-27 11:05:53 -040079 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -040080 check_inode(inode);
Chris Masone20d96d2007-03-22 12:13:20 -040081 switch (inode->i_mode & S_IFMT) {
82#if 0
83 default:
84 init_special_inode(inode, inode->i_mode,
85 btrfs_inode_rdev(inode_item));
86 break;
87#endif
88 case S_IFREG:
Chris Masondee26a92007-03-26 16:00:06 -040089 inode->i_mapping->a_ops = &btrfs_aops;
90 inode->i_fop = &btrfs_file_operations;
91 inode->i_op = &btrfs_file_inode_operations;
Chris Masone20d96d2007-03-22 12:13:20 -040092 break;
93 case S_IFDIR:
Chris Masone20d96d2007-03-22 12:13:20 -040094 inode->i_op = &btrfs_dir_inode_operations;
95 inode->i_fop = &btrfs_dir_file_operations;
96 break;
97 case S_IFLNK:
Chris Masone20d96d2007-03-22 12:13:20 -040098 // inode->i_op = &page_symlink_inode_operations;
99 break;
100 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400101 check_inode(inode);
Chris Masone20d96d2007-03-22 12:13:20 -0400102 return;
Chris Mason2e635a22007-03-21 11:12:56 -0400103}
104
Chris Mason5f443fd2007-03-27 13:42:32 -0400105static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
106 struct btrfs_root *root,
107 struct inode *dir,
108 struct dentry *dentry)
Chris Mason134e9732007-03-25 13:44:56 -0400109{
Chris Mason5caf2a02007-04-02 11:20:42 -0400110 struct btrfs_path *path;
Chris Mason134e9732007-03-25 13:44:56 -0400111 const char *name = dentry->d_name.name;
112 int name_len = dentry->d_name.len;
113 int ret;
114 u64 objectid;
115 struct btrfs_dir_item *di;
116
Chris Mason5caf2a02007-04-02 11:20:42 -0400117 path = btrfs_alloc_path();
118 BUG_ON(!path);
119 btrfs_init_path(path);
120 ret = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
Chris Mason134e9732007-03-25 13:44:56 -0400121 name, name_len, -1);
122 if (ret < 0)
123 goto err;
124 if (ret > 0) {
125 ret = -ENOENT;
126 goto err;
127 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400128 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason134e9732007-03-25 13:44:56 -0400129 struct btrfs_dir_item);
130 objectid = btrfs_dir_objectid(di);
131
Chris Mason5caf2a02007-04-02 11:20:42 -0400132 ret = btrfs_del_item(trans, root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400133 BUG_ON(ret);
134 dentry->d_inode->i_ctime = dir->i_ctime;
135err:
Chris Mason5caf2a02007-04-02 11:20:42 -0400136 btrfs_release_path(root, path);
137 btrfs_free_path(path);
Chris Mason134e9732007-03-25 13:44:56 -0400138 if (ret == 0)
139 inode_dec_link_count(dentry->d_inode);
140 return ret;
141}
142
Chris Mason5f443fd2007-03-27 13:42:32 -0400143static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
144{
145 struct btrfs_root *root;
146 struct btrfs_trans_handle *trans;
147 int ret;
148
149 root = btrfs_sb(dir->i_sb);
150 mutex_lock(&root->fs_info->fs_mutex);
151 trans = btrfs_start_transaction(root, 1);
152 ret = btrfs_unlink_trans(trans, root, dir, dentry);
153 btrfs_end_transaction(trans, root);
154 mutex_unlock(&root->fs_info->fs_mutex);
155 return ret;
156}
157
158static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
159{
160 struct inode *inode = dentry->d_inode;
161 int err;
162 int ret;
163 struct btrfs_root *root = btrfs_sb(dir->i_sb);
Chris Mason5caf2a02007-04-02 11:20:42 -0400164 struct btrfs_path *path;
Chris Mason5f443fd2007-03-27 13:42:32 -0400165 struct btrfs_key key;
166 struct btrfs_trans_handle *trans;
167 struct btrfs_disk_key *found_key;
168 struct btrfs_leaf *leaf;
169
Chris Mason5caf2a02007-04-02 11:20:42 -0400170 path = btrfs_alloc_path();
171 BUG_ON(!path);
172 btrfs_init_path(path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400173 mutex_lock(&root->fs_info->fs_mutex);
174 trans = btrfs_start_transaction(root, 1);
175 key.objectid = inode->i_ino;
176 key.offset = (u64)-1;
177 key.flags = 0;
178 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400179 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Mason5f443fd2007-03-27 13:42:32 -0400180 if (ret < 0) {
181 err = ret;
182 goto out;
183 }
184
185 BUG_ON(ret == 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400186 BUG_ON(path->slots[0] == 0);
187 path->slots[0]--;
188 leaf = btrfs_buffer_leaf(path->nodes[0]);
189 found_key = &leaf->items[path->slots[0]].key;
Chris Mason5f443fd2007-03-27 13:42:32 -0400190 if (btrfs_disk_key_objectid(found_key) != inode->i_ino) {
191 err = -ENOENT;
192 goto out;
193 }
194 if (btrfs_disk_key_type(found_key) != BTRFS_DIR_ITEM_KEY ||
195 btrfs_disk_key_offset(found_key) != 2) {
196 err = -ENOTEMPTY;
197 goto out;
198 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400199 ret = btrfs_del_item(trans, root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400200 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400201 btrfs_release_path(root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400202 key.offset = 1;
Chris Mason5caf2a02007-04-02 11:20:42 -0400203 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Mason5f443fd2007-03-27 13:42:32 -0400204 if (ret < 0) {
205 err = ret;
206 goto out;
207 }
208 if (ret > 0) {
209 err = -ENOTEMPTY;
210 goto out;
211 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400212 ret = btrfs_del_item(trans, root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400213 if (ret) {
214 err = ret;
215 goto out;
216 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400217 btrfs_release_path(root, path);
218 btrfs_free_path(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:
226 mutex_unlock(&root->fs_info->fs_mutex);
227 ret = btrfs_end_transaction(trans, root);
228 if (ret && !err)
229 err = ret;
230 return err;
231}
232
Chris Mason134e9732007-03-25 13:44:56 -0400233static int btrfs_free_inode(struct btrfs_trans_handle *trans,
234 struct btrfs_root *root,
235 struct inode *inode)
236{
237 u64 objectid = inode->i_ino;
Chris Mason5caf2a02007-04-02 11:20:42 -0400238 struct btrfs_path *path;
Chris Mason134e9732007-03-25 13:44:56 -0400239 struct btrfs_inode_map_item *map;
240 struct btrfs_key stat_data_key;
241 int ret;
Chris Mason5caf2a02007-04-02 11:20:42 -0400242
Chris Mason134e9732007-03-25 13:44:56 -0400243 clear_inode(inode);
Chris Mason5caf2a02007-04-02 11:20:42 -0400244
245 path = btrfs_alloc_path();
246 BUG_ON(!path);
247 btrfs_init_path(path);
248 ret = btrfs_lookup_inode_map(trans, root, path, objectid, -1);
Chris Mason134e9732007-03-25 13:44:56 -0400249 if (ret) {
250 if (ret > 0)
251 ret = -ENOENT;
Chris Mason134e9732007-03-25 13:44:56 -0400252 goto error;
253 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400254 map = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason134e9732007-03-25 13:44:56 -0400255 struct btrfs_inode_map_item);
256 btrfs_disk_key_to_cpu(&stat_data_key, &map->key);
Chris Mason5caf2a02007-04-02 11:20:42 -0400257 ret = btrfs_del_item(trans, root->fs_info->inode_root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400258 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400259 btrfs_release_path(root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400260
Chris Mason5caf2a02007-04-02 11:20:42 -0400261 ret = btrfs_lookup_inode(trans, root, path, objectid, -1);
Chris Mason134e9732007-03-25 13:44:56 -0400262 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400263 ret = btrfs_del_item(trans, root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400264 BUG_ON(ret);
Chris Mason134e9732007-03-25 13:44:56 -0400265error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400266 btrfs_release_path(root, path);
267 btrfs_free_path(path);
Chris Mason134e9732007-03-25 13:44:56 -0400268 return ret;
269}
270
Chris Masonf4b9aa82007-03-27 11:05:53 -0400271static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
272 struct btrfs_root *root,
273 struct inode *inode)
274{
275 int ret;
Chris Mason5caf2a02007-04-02 11:20:42 -0400276 struct btrfs_path *path;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400277 struct btrfs_key key;
278 struct btrfs_disk_key *found_key;
279 struct btrfs_leaf *leaf;
Chris Masonf254e522007-03-29 15:15:27 -0400280 struct btrfs_file_extent_item *fi = NULL;
281 u64 extent_start = 0;
282 u64 extent_num_blocks = 0;
283 int found_extent;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400284
Chris Mason5caf2a02007-04-02 11:20:42 -0400285 path = btrfs_alloc_path();
286 BUG_ON(!path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400287 /* FIXME, add redo link to tree so we don't leak on crash */
288 key.objectid = inode->i_ino;
289 key.offset = (u64)-1;
290 key.flags = 0;
Chris Masonf254e522007-03-29 15:15:27 -0400291 btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400292 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400293 btrfs_init_path(path);
294 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400295 if (ret < 0) {
Chris Masonf4b9aa82007-03-27 11:05:53 -0400296 goto error;
297 }
298 if (ret > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400299 BUG_ON(path->slots[0] == 0);
300 path->slots[0]--;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400301 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400302 leaf = btrfs_buffer_leaf(path->nodes[0]);
303 found_key = &leaf->items[path->slots[0]].key;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400304 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
305 break;
Chris Masonf254e522007-03-29 15:15:27 -0400306 if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY &&
307 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
Chris Masonf4b9aa82007-03-27 11:05:53 -0400308 break;
309 if (btrfs_disk_key_offset(found_key) < inode->i_size)
310 break;
Chris Masonf254e522007-03-29 15:15:27 -0400311 if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400312 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
313 path->slots[0],
Chris Masonf254e522007-03-29 15:15:27 -0400314 struct btrfs_file_extent_item);
315 extent_start = btrfs_file_extent_disk_blocknr(fi);
316 extent_num_blocks =
317 btrfs_file_extent_disk_num_blocks(fi);
318 inode->i_blocks -=
319 btrfs_file_extent_num_blocks(fi) >> 9;
320 found_extent = 1;
321 } else {
322 found_extent = 0;
323 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400324 ret = btrfs_del_item(trans, root, path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400325 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400326 btrfs_release_path(root, path);
Chris Masonf254e522007-03-29 15:15:27 -0400327 if (found_extent) {
328 ret = btrfs_free_extent(trans, root, extent_start,
329 extent_num_blocks, 0);
330 BUG_ON(ret);
331 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400332 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400333 ret = 0;
334error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400335 btrfs_release_path(root, path);
336 btrfs_free_path(path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400337 return ret;
338}
339
Chris Mason134e9732007-03-25 13:44:56 -0400340static void btrfs_delete_inode(struct inode *inode)
341{
342 struct btrfs_trans_handle *trans;
343 struct btrfs_root *root = btrfs_sb(inode->i_sb);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400344 int ret;
345
Chris Mason134e9732007-03-25 13:44:56 -0400346 truncate_inode_pages(&inode->i_data, 0);
347 if (is_bad_inode(inode)) {
348 goto no_delete;
349 }
350 inode->i_size = 0;
Chris Mason134e9732007-03-25 13:44:56 -0400351 mutex_lock(&root->fs_info->fs_mutex);
352 trans = btrfs_start_transaction(root, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400353 if (S_ISREG(inode->i_mode)) {
354 ret = btrfs_truncate_in_trans(trans, root, inode);
355 BUG_ON(ret);
356 }
Chris Mason134e9732007-03-25 13:44:56 -0400357 btrfs_free_inode(trans, root, inode);
358 btrfs_end_transaction(trans, root);
359 mutex_unlock(&root->fs_info->fs_mutex);
360 return;
361no_delete:
362 clear_inode(inode);
363}
364
Chris Masone20d96d2007-03-22 12:13:20 -0400365static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
366 ino_t *ino)
367{
368 const char *name = dentry->d_name.name;
369 int namelen = dentry->d_name.len;
370 struct btrfs_dir_item *di;
Chris Mason5caf2a02007-04-02 11:20:42 -0400371 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -0400372 struct btrfs_root *root = btrfs_sb(dir->i_sb);
373 int ret;
374
Chris Mason5caf2a02007-04-02 11:20:42 -0400375 path = btrfs_alloc_path();
376 BUG_ON(!path);
377 btrfs_init_path(path);
378 ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
Chris Masone20d96d2007-03-22 12:13:20 -0400379 namelen, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400380 if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) {
Chris Masone20d96d2007-03-22 12:13:20 -0400381 *ino = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400382 ret = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400383 goto out;
384 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400385 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masone20d96d2007-03-22 12:13:20 -0400386 struct btrfs_dir_item);
387 *ino = btrfs_dir_objectid(di);
388out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400389 btrfs_release_path(root, path);
390 btrfs_free_path(path);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400391 check_inode(dir);
Chris Masone20d96d2007-03-22 12:13:20 -0400392 return ret;
393}
394
395static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
396 struct nameidata *nd)
397{
398 struct inode * inode;
Chris Mason22b0ebd2007-03-30 08:47:31 -0400399 struct btrfs_root *root = btrfs_sb(dir->i_sb);
Chris Masone20d96d2007-03-22 12:13:20 -0400400 ino_t ino;
401 int ret;
402
403 if (dentry->d_name.len > BTRFS_NAME_LEN)
404 return ERR_PTR(-ENAMETOOLONG);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400405 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400406 ret = btrfs_inode_by_name(dir, dentry, &ino);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400407 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400408 if (ret < 0)
409 return ERR_PTR(ret);
410 inode = NULL;
411 if (ino) {
Chris Masone20d96d2007-03-22 12:13:20 -0400412 inode = iget(dir->i_sb, ino);
413 if (!inode)
414 return ERR_PTR(-EACCES);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400415 check_inode(inode);
Chris Masone20d96d2007-03-22 12:13:20 -0400416 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400417 check_inode(dir);
Chris Masone20d96d2007-03-22 12:13:20 -0400418 return d_splice_alias(inode, dentry);
419}
420
421static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
422{
423 struct inode *inode = filp->f_path.dentry->d_inode;
424 struct btrfs_root *root = btrfs_sb(inode->i_sb);
425 struct btrfs_item *item;
426 struct btrfs_dir_item *di;
427 struct btrfs_key key;
Chris Mason5caf2a02007-04-02 11:20:42 -0400428 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -0400429 int ret;
430 u32 nritems;
431 struct btrfs_leaf *leaf;
432 int slot;
433 int advance;
434 unsigned char d_type = DT_UNKNOWN;
Chris Mason7f5c1512007-03-23 15:56:19 -0400435 int over = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400436
Chris Mason22b0ebd2007-03-30 08:47:31 -0400437 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400438 key.objectid = inode->i_ino;
Chris Masone20d96d2007-03-22 12:13:20 -0400439 key.flags = 0;
440 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
441 key.offset = filp->f_pos;
Chris Mason5caf2a02007-04-02 11:20:42 -0400442 path = btrfs_alloc_path();
443 btrfs_init_path(path);
444 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400445 if (ret < 0) {
446 goto err;
447 }
Chris Mason7f5c1512007-03-23 15:56:19 -0400448 advance = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400449 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400450 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400451 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400452 slot = path->slots[0];
Chris Masondee26a92007-03-26 16:00:06 -0400453 if (advance || slot >= nritems) {
454 if (slot >= nritems -1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400455 ret = btrfs_next_leaf(root, path);
Chris Masone20d96d2007-03-22 12:13:20 -0400456 if (ret)
457 break;
Chris Mason5caf2a02007-04-02 11:20:42 -0400458 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400459 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400460 slot = path->slots[0];
Chris Masone20d96d2007-03-22 12:13:20 -0400461 } else {
462 slot++;
Chris Mason5caf2a02007-04-02 11:20:42 -0400463 path->slots[0]++;
Chris Masone20d96d2007-03-22 12:13:20 -0400464 }
465 }
466 advance = 1;
467 item = leaf->items + slot;
Chris Masone20d96d2007-03-22 12:13:20 -0400468 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
469 break;
470 if (btrfs_disk_key_type(&item->key) != BTRFS_DIR_ITEM_KEY)
471 continue;
Chris Mason7f5c1512007-03-23 15:56:19 -0400472 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
473 continue;
Chris Masondee26a92007-03-26 16:00:06 -0400474
475 advance = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400476 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Chris Masone20d96d2007-03-22 12:13:20 -0400477 over = filldir(dirent, (const char *)(di + 1),
478 btrfs_dir_name_len(di),
479 btrfs_disk_key_offset(&item->key),
480 btrfs_dir_objectid(di), d_type);
Chris Mason7f5c1512007-03-23 15:56:19 -0400481 if (over) {
482 filp->f_pos = btrfs_disk_key_offset(&item->key);
Chris Masone20d96d2007-03-22 12:13:20 -0400483 break;
Chris Mason7f5c1512007-03-23 15:56:19 -0400484 }
Chris Masone20d96d2007-03-22 12:13:20 -0400485 filp->f_pos = btrfs_disk_key_offset(&item->key) + 1;
486 }
Chris Masone20d96d2007-03-22 12:13:20 -0400487 ret = 0;
488err:
Chris Mason5caf2a02007-04-02 11:20:42 -0400489 btrfs_release_path(root, path);
490 btrfs_free_path(path);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400491 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400492 return ret;
493}
494
495static void btrfs_put_super (struct super_block * sb)
496{
497 struct btrfs_root *root = btrfs_sb(sb);
498 int ret;
499
500 ret = close_ctree(root);
501 if (ret) {
502 printk("close ctree returns %d\n", ret);
503 }
504 sb->s_fs_info = NULL;
505}
Chris Mason2e635a22007-03-21 11:12:56 -0400506
507static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
508{
509 struct inode * inode;
Chris Masone20d96d2007-03-22 12:13:20 -0400510 struct dentry * root_dentry;
511 struct btrfs_super_block *disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400512 struct btrfs_root *root;
Chris Mason2e635a22007-03-21 11:12:56 -0400513
514 sb->s_maxbytes = MAX_LFS_FILESIZE;
Chris Mason2e635a22007-03-21 11:12:56 -0400515 sb->s_magic = BTRFS_SUPER_MAGIC;
Chris Masone20d96d2007-03-22 12:13:20 -0400516 sb->s_op = &btrfs_super_ops;
Chris Mason2e635a22007-03-21 11:12:56 -0400517 sb->s_time_gran = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400518
Chris Mason2c90e5d2007-04-02 10:50:19 -0400519 root = open_ctree(sb);
Chris Masond98237b2007-03-28 13:57:48 -0400520
Chris Masone20d96d2007-03-22 12:13:20 -0400521 if (!root) {
522 printk("btrfs: open_ctree failed\n");
523 return -EIO;
524 }
Chris Mason87cbda52007-03-28 19:44:27 -0400525 sb->s_fs_info = root;
526 disk_super = root->fs_info->disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400527 printk("read in super total blocks %Lu root %Lu\n",
528 btrfs_super_total_blocks(disk_super),
529 btrfs_super_root_dir(disk_super));
530
531 inode = iget_locked(sb, btrfs_super_root_dir(disk_super));
Chris Mason2e635a22007-03-21 11:12:56 -0400532 if (!inode)
533 return -ENOMEM;
Chris Masone20d96d2007-03-22 12:13:20 -0400534 if (inode->i_state & I_NEW) {
535 btrfs_read_locked_inode(inode);
536 unlock_new_inode(inode);
537 }
Chris Mason2e635a22007-03-21 11:12:56 -0400538
Chris Masone20d96d2007-03-22 12:13:20 -0400539 root_dentry = d_alloc_root(inode);
540 if (!root_dentry) {
Chris Mason2e635a22007-03-21 11:12:56 -0400541 iput(inode);
542 return -ENOMEM;
543 }
Chris Masone20d96d2007-03-22 12:13:20 -0400544 sb->s_root = root_dentry;
545
Chris Mason2e635a22007-03-21 11:12:56 -0400546 return 0;
547}
548
Chris Masond5719762007-03-23 10:01:08 -0400549static void fill_inode_item(struct btrfs_inode_item *item,
550 struct inode *inode)
551{
552 btrfs_set_inode_uid(item, inode->i_uid);
553 btrfs_set_inode_gid(item, inode->i_gid);
554 btrfs_set_inode_size(item, inode->i_size);
555 btrfs_set_inode_mode(item, inode->i_mode);
556 btrfs_set_inode_nlink(item, inode->i_nlink);
557 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
558 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
559 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
560 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
561 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
562 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
563 btrfs_set_inode_nblocks(item, inode->i_blocks);
564 btrfs_set_inode_generation(item, inode->i_generation);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400565 check_inode(inode);
Chris Masond5719762007-03-23 10:01:08 -0400566}
567
Chris Mason4730a4b2007-03-26 12:00:39 -0400568static int btrfs_update_inode(struct btrfs_trans_handle *trans,
569 struct btrfs_root *root,
570 struct inode *inode)
571{
572 struct btrfs_inode_item *inode_item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400573 struct btrfs_path *path;
Chris Mason4730a4b2007-03-26 12:00:39 -0400574 int ret;
575
Chris Mason5caf2a02007-04-02 11:20:42 -0400576 path = btrfs_alloc_path();
577 BUG_ON(!path);
578 btrfs_init_path(path);
Chris Mason4730a4b2007-03-26 12:00:39 -0400579
Chris Mason5caf2a02007-04-02 11:20:42 -0400580 ret = btrfs_lookup_inode(trans, root, path, inode->i_ino, 1);
Chris Mason4730a4b2007-03-26 12:00:39 -0400581 if (ret) {
582 if (ret > 0)
583 ret = -ENOENT;
584 goto failed;
585 }
586
Chris Mason5caf2a02007-04-02 11:20:42 -0400587 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
588 path->slots[0],
Chris Mason4730a4b2007-03-26 12:00:39 -0400589 struct btrfs_inode_item);
590
591 fill_inode_item(inode_item, inode);
Chris Mason5caf2a02007-04-02 11:20:42 -0400592 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason4730a4b2007-03-26 12:00:39 -0400593failed:
Chris Mason5caf2a02007-04-02 11:20:42 -0400594 btrfs_release_path(root, path);
595 btrfs_free_path(path);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400596 check_inode(inode);
Chris Mason4730a4b2007-03-26 12:00:39 -0400597 return 0;
598}
599
600static int btrfs_write_inode(struct inode *inode, int wait)
601{
602 struct btrfs_root *root = btrfs_sb(inode->i_sb);
603 struct btrfs_trans_handle *trans;
604 int ret;
605
606 mutex_lock(&root->fs_info->fs_mutex);
607 trans = btrfs_start_transaction(root, 1);
608 ret = btrfs_update_inode(trans, root, inode);
609 if (wait)
610 btrfs_commit_transaction(trans, root);
611 else
612 btrfs_end_transaction(trans, root);
613 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400614 check_inode(inode);
Chris Mason4730a4b2007-03-26 12:00:39 -0400615 return ret;
616}
617
Chris Masond5719762007-03-23 10:01:08 -0400618static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
619 struct inode *dir, int mode)
620{
621 struct inode *inode;
622 struct btrfs_inode_item inode_item;
623 struct btrfs_root *root = btrfs_sb(dir->i_sb);
624 struct btrfs_key key;
625 int ret;
626 u64 objectid;
627
628 inode = new_inode(dir->i_sb);
629 if (!inode)
630 return ERR_PTR(-ENOMEM);
631
Chris Mason2c90e5d2007-04-02 10:50:19 -0400632 check_inode(inode);
Chris Masond5719762007-03-23 10:01:08 -0400633 ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
634 BUG_ON(ret);
635
636 inode->i_uid = current->fsuid;
637 inode->i_gid = current->fsgid;
638 inode->i_mode = mode;
639 inode->i_ino = objectid;
640 inode->i_blocks = 0;
641 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
642 fill_inode_item(&inode_item, inode);
643
Chris Masond5719762007-03-23 10:01:08 -0400644 key.objectid = objectid;
645 key.flags = 0;
646 key.offset = 0;
647 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
648 ret = btrfs_insert_inode_map(trans, root, objectid, &key);
649 BUG_ON(ret);
650
651 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
652 BUG_ON(ret);
653
654 insert_inode_hash(inode);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400655 check_inode(inode);
656 check_inode(dir);
Chris Masond5719762007-03-23 10:01:08 -0400657 return inode;
658}
659
660static int btrfs_add_link(struct btrfs_trans_handle *trans,
661 struct dentry *dentry, struct inode *inode)
662{
663 int ret;
664 ret = btrfs_insert_dir_item(trans, btrfs_sb(inode->i_sb),
665 dentry->d_name.name, dentry->d_name.len,
666 dentry->d_parent->d_inode->i_ino,
667 inode->i_ino, 0);
Chris Mason4730a4b2007-03-26 12:00:39 -0400668 if (ret == 0) {
669 dentry->d_parent->d_inode->i_size += dentry->d_name.len;
670 ret = btrfs_update_inode(trans, btrfs_sb(inode->i_sb),
671 dentry->d_parent->d_inode);
672 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400673 check_inode(inode);
674 check_inode(dentry->d_parent->d_inode);
Chris Masond5719762007-03-23 10:01:08 -0400675 return ret;
676}
677
678static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
679 struct dentry *dentry, struct inode *inode)
680{
681 int err = btrfs_add_link(trans, dentry, inode);
682 if (!err) {
683 d_instantiate(dentry, inode);
684 return 0;
685 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400686 if (err > 0)
687 err = -EEXIST;
688 check_inode(inode);
Chris Masond5719762007-03-23 10:01:08 -0400689 return err;
690}
691
692static int btrfs_create(struct inode *dir, struct dentry *dentry,
693 int mode, struct nameidata *nd)
694{
695 struct btrfs_trans_handle *trans;
696 struct btrfs_root *root = btrfs_sb(dir->i_sb);
697 struct inode *inode;
698 int err;
Chris Mason134e9732007-03-25 13:44:56 -0400699 int drop_inode = 0;
Chris Masond5719762007-03-23 10:01:08 -0400700
Chris Masond561c022007-03-23 19:47:49 -0400701 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400702 trans = btrfs_start_transaction(root, 1);
703 inode = btrfs_new_inode(trans, dir, mode);
704 err = PTR_ERR(inode);
705 if (IS_ERR(inode))
Chris Masond561c022007-03-23 19:47:49 -0400706 goto out_unlock;
Chris Masond5719762007-03-23 10:01:08 -0400707 // FIXME mark the inode dirty
708 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason134e9732007-03-25 13:44:56 -0400709 if (err)
710 drop_inode = 1;
Chris Masondee26a92007-03-26 16:00:06 -0400711 else {
712 inode->i_mapping->a_ops = &btrfs_aops;
713 inode->i_fop = &btrfs_file_operations;
714 inode->i_op = &btrfs_file_inode_operations;
715 }
Chris Masond5719762007-03-23 10:01:08 -0400716 dir->i_sb->s_dirt = 1;
Chris Masond561c022007-03-23 19:47:49 -0400717out_unlock:
Chris Mason22b0ebd2007-03-30 08:47:31 -0400718 btrfs_end_transaction(trans, root);
Chris Masond561c022007-03-23 19:47:49 -0400719 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400720 check_inode(inode);
721 check_inode(dir);
722
Chris Mason134e9732007-03-25 13:44:56 -0400723 if (drop_inode) {
724 inode_dec_link_count(inode);
725 iput(inode);
726 }
Chris Masond5719762007-03-23 10:01:08 -0400727 return err;
728}
729
Chris Masonf7922032007-03-25 20:17:36 -0400730static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
731 struct inode *inode, struct inode *dir)
732{
733 struct btrfs_root *root = btrfs_sb(inode->i_sb);
734 int ret;
735 char buf[2];
736 buf[0] = '.';
737 buf[1] = '.';
738
739 ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino,
740 inode->i_ino, 1);
741 if (ret)
742 goto error;
743 ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino,
744 dir->i_ino, 1);
Chris Mason4730a4b2007-03-26 12:00:39 -0400745 if (ret)
746 goto error;
747 inode->i_size = 3;
748 ret = btrfs_update_inode(trans, root, inode);
Chris Masonf7922032007-03-25 20:17:36 -0400749error:
750 return ret;
751}
752
753static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
754{
755 struct inode *inode;
756 struct btrfs_trans_handle *trans;
757 struct btrfs_root *root = btrfs_sb(dir->i_sb);
758 int err = 0;
759 int drop_on_err = 0;
760
761 mutex_lock(&root->fs_info->fs_mutex);
762 trans = btrfs_start_transaction(root, 1);
763 if (IS_ERR(trans)) {
764 err = PTR_ERR(trans);
765 goto out_unlock;
766 }
767 inode = btrfs_new_inode(trans, dir, S_IFDIR | mode);
768 if (IS_ERR(inode)) {
769 err = PTR_ERR(inode);
770 goto out_fail;
771 }
772 drop_on_err = 1;
773 inode->i_op = &btrfs_dir_inode_operations;
774 inode->i_fop = &btrfs_dir_file_operations;
775
776 err = btrfs_make_empty_dir(trans, inode, dir);
777 if (err)
778 goto out_fail;
779 err = btrfs_add_link(trans, dentry, inode);
780 if (err)
781 goto out_fail;
782 d_instantiate(dentry, inode);
Chris Masonf7922032007-03-25 20:17:36 -0400783 drop_on_err = 0;
784
785out_fail:
786 btrfs_end_transaction(trans, root);
787out_unlock:
788 mutex_unlock(&root->fs_info->fs_mutex);
789 if (drop_on_err)
790 iput(inode);
791 return err;
792}
793
Chris Masond5719762007-03-23 10:01:08 -0400794static int btrfs_sync_fs(struct super_block *sb, int wait)
795{
796 struct btrfs_trans_handle *trans;
797 struct btrfs_root *root;
798 int ret;
Chris Masond98237b2007-03-28 13:57:48 -0400799 root = btrfs_sb(sb);
Chris Masondf2ce342007-03-23 11:00:45 -0400800
Chris Masond5719762007-03-23 10:01:08 -0400801 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -0400802 if (!wait) {
Chris Mason2c90e5d2007-04-02 10:50:19 -0400803 // filemap_flush(root->fs_info->btree_inode->i_mapping);
804 filemap_flush(root->fs_info->sb->s_bdev->bd_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400805 return 0;
806 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400807 filemap_write_and_wait(root->fs_info->sb->s_bdev->bd_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400808 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400809 trans = btrfs_start_transaction(root, 1);
810 ret = btrfs_commit_transaction(trans, root);
811 sb->s_dirt = 0;
812 BUG_ON(ret);
813printk("btrfs sync_fs\n");
Chris Masond561c022007-03-23 19:47:49 -0400814 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400815 return 0;
816}
817
Chris Mason75dfe392007-03-29 11:56:46 -0400818static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
Chris Masondee26a92007-03-26 16:00:06 -0400819 struct buffer_head *result, int create)
820{
821 int ret;
822 int err = 0;
823 u64 blocknr;
824 u64 extent_start = 0;
825 u64 extent_end = 0;
826 u64 objectid = inode->i_ino;
Chris Mason5caf2a02007-04-02 11:20:42 -0400827 struct btrfs_path *path;
Chris Masondee26a92007-03-26 16:00:06 -0400828 struct btrfs_root *root = btrfs_sb(inode->i_sb);
829 struct btrfs_trans_handle *trans = NULL;
830 struct btrfs_file_extent_item *item;
831 struct btrfs_leaf *leaf;
832 struct btrfs_disk_key *found_key;
833
Chris Mason5caf2a02007-04-02 11:20:42 -0400834 path = btrfs_alloc_path();
835 BUG_ON(!path);
836 btrfs_init_path(path);
Chris Masondee26a92007-03-26 16:00:06 -0400837 if (create)
838 trans = btrfs_start_transaction(root, 1);
839
840
Chris Mason5caf2a02007-04-02 11:20:42 -0400841 ret = btrfs_lookup_file_extent(trans, root, path,
Chris Mason9773a782007-03-27 11:26:26 -0400842 inode->i_ino,
843 iblock << inode->i_blkbits, 0);
Chris Masondee26a92007-03-26 16:00:06 -0400844 if (ret < 0) {
Chris Masondee26a92007-03-26 16:00:06 -0400845 err = ret;
846 goto out;
847 }
848
849 if (ret != 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400850 if (path->slots[0] == 0) {
851 btrfs_release_path(root, path);
Chris Masondee26a92007-03-26 16:00:06 -0400852 goto allocate;
853 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400854 path->slots[0]--;
Chris Masondee26a92007-03-26 16:00:06 -0400855 }
856
Chris Mason5caf2a02007-04-02 11:20:42 -0400857 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masondee26a92007-03-26 16:00:06 -0400858 struct btrfs_file_extent_item);
Chris Mason5caf2a02007-04-02 11:20:42 -0400859 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masondee26a92007-03-26 16:00:06 -0400860 blocknr = btrfs_file_extent_disk_blocknr(item);
861 blocknr += btrfs_file_extent_offset(item);
862
863 /* exact match found, use it */
864 if (ret == 0) {
865 err = 0;
866 map_bh(result, inode->i_sb, blocknr);
Chris Masondee26a92007-03-26 16:00:06 -0400867 goto out;
868 }
869
870 /* are we inside the extent that was found? */
Chris Mason5caf2a02007-04-02 11:20:42 -0400871 found_key = &leaf->items[path->slots[0]].key;
Chris Masondee26a92007-03-26 16:00:06 -0400872 if (btrfs_disk_key_objectid(found_key) != objectid ||
873 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
874 extent_end = 0;
875 extent_start = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400876 btrfs_release_path(root, path);
Chris Masondee26a92007-03-26 16:00:06 -0400877 goto allocate;
878 }
879
Chris Mason5caf2a02007-04-02 11:20:42 -0400880 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
Chris Mason9773a782007-03-27 11:26:26 -0400881 extent_start = extent_start >> inode->i_blkbits;
Chris Masondee26a92007-03-26 16:00:06 -0400882 extent_start += btrfs_file_extent_offset(item);
883 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
Chris Masondee26a92007-03-26 16:00:06 -0400884 if (iblock >= extent_start && iblock < extent_end) {
885 err = 0;
886 map_bh(result, inode->i_sb, blocknr + iblock - extent_start);
887 goto out;
888 }
889allocate:
890 /* ok, create a new extent */
891 if (!create) {
892 err = 0;
893 goto out;
894 }
Chris Mason9773a782007-03-27 11:26:26 -0400895 ret = btrfs_alloc_file_extent(trans, root, objectid,
896 iblock << inode->i_blkbits,
Chris Masondee26a92007-03-26 16:00:06 -0400897 1, extent_end, &blocknr);
898 if (ret) {
899 err = ret;
900 goto out;
901 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400902 inode->i_blocks += inode->i_sb->s_blocksize >> 9;
903 set_buffer_new(result);
Chris Masondee26a92007-03-26 16:00:06 -0400904 map_bh(result, inode->i_sb, blocknr);
905
906out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400907 btrfs_release_path(root, path);
908 btrfs_free_path(path);
Chris Masondee26a92007-03-26 16:00:06 -0400909 if (trans)
910 btrfs_end_transaction(trans, root);
Chris Mason75dfe392007-03-29 11:56:46 -0400911 return err;
912}
913
914static int btrfs_get_block(struct inode *inode, sector_t iblock,
915 struct buffer_head *result, int create)
916{
917 int err;
918 struct btrfs_root *root = btrfs_sb(inode->i_sb);
919 mutex_lock(&root->fs_info->fs_mutex);
920 err = btrfs_get_block_lock(inode, iblock, result, create);
Chris Masondee26a92007-03-26 16:00:06 -0400921 mutex_unlock(&root->fs_info->fs_mutex);
922 return err;
923}
924
925static int btrfs_prepare_write(struct file *file, struct page *page,
926 unsigned from, unsigned to)
927{
Chris Mason75dfe392007-03-29 11:56:46 -0400928 WARN_ON(1);
Chris Mason6407bf62007-03-27 06:33:00 -0400929 return nobh_prepare_write(page, from, to, btrfs_get_block);
Chris Masondee26a92007-03-26 16:00:06 -0400930}
Chris Mason75dfe392007-03-29 11:56:46 -0400931static int btrfs_commit_write(struct file *file, struct page *page,
932 unsigned from, unsigned to)
933{
934 WARN_ON(1);
935 return nobh_commit_write(file, page, from, to);
936}
Chris Masondee26a92007-03-26 16:00:06 -0400937
Chris Masond561c022007-03-23 19:47:49 -0400938static void btrfs_write_super(struct super_block *sb)
939{
940 btrfs_sync_fs(sb, 1);
941}
942
Chris Masondee26a92007-03-26 16:00:06 -0400943static int btrfs_readpage(struct file *file, struct page *page)
944{
945 return mpage_readpage(page, btrfs_get_block);
946}
947
948static int btrfs_readpages(struct file *file, struct address_space *mapping,
949 struct list_head *pages, unsigned nr_pages)
950{
951 return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block);
952}
953
954static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
955{
Chris Mason6407bf62007-03-27 06:33:00 -0400956 return nobh_writepage(page, btrfs_get_block, wbc);
Chris Masondee26a92007-03-26 16:00:06 -0400957}
Chris Masond561c022007-03-23 19:47:49 -0400958
Chris Masonf4b9aa82007-03-27 11:05:53 -0400959static void btrfs_truncate(struct inode *inode)
960{
961 struct btrfs_root *root = btrfs_sb(inode->i_sb);
962 int ret;
963 struct btrfs_trans_handle *trans;
964
965 if (!S_ISREG(inode->i_mode))
966 return;
967 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
968 return;
969
970 nobh_truncate_page(inode->i_mapping, inode->i_size);
971
972 /* FIXME, add redo link to tree so we don't leak on crash */
973 mutex_lock(&root->fs_info->fs_mutex);
974 trans = btrfs_start_transaction(root, 1);
975 ret = btrfs_truncate_in_trans(trans, root, inode);
976 BUG_ON(ret);
977 ret = btrfs_end_transaction(trans, root);
978 BUG_ON(ret);
979 mutex_unlock(&root->fs_info->fs_mutex);
980 mark_inode_dirty(inode);
981}
982
Chris Mason75dfe392007-03-29 11:56:46 -0400983static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
984 struct page **prepared_pages,
985 const char __user * buf)
986{
987 long page_fault = 0;
988 int i;
989 int offset = pos & (PAGE_CACHE_SIZE - 1);
990
991 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
992 size_t count = min_t(size_t,
993 PAGE_CACHE_SIZE - offset, write_bytes);
994 struct page *page = prepared_pages[i];
995 fault_in_pages_readable(buf, count);
996
997 /* Copy data from userspace to the current page */
998 kmap(page);
999 page_fault = __copy_from_user(page_address(page) + offset,
1000 buf, count);
1001 /* Flush processor's dcache for this page */
1002 flush_dcache_page(page);
1003 kunmap(page);
1004 buf += count;
1005 write_bytes -= count;
1006
1007 if (page_fault)
1008 break;
1009 }
1010 return page_fault ? -EFAULT : 0;
1011}
1012
1013static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1014{
1015 size_t i;
1016 for (i = 0; i < num_pages; i++) {
1017 if (!pages[i])
1018 break;
1019 unlock_page(pages[i]);
1020 mark_page_accessed(pages[i]);
1021 page_cache_release(pages[i]);
1022 }
1023}
1024static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1025 struct btrfs_root *root,
1026 struct file *file,
1027 struct page **pages,
1028 size_t num_pages,
1029 loff_t pos,
1030 size_t write_bytes)
1031{
1032 int i;
1033 int offset;
1034 int err = 0;
1035 int ret;
1036 int this_write;
Chris Masonf254e522007-03-29 15:15:27 -04001037 struct inode *inode = file->f_path.dentry->d_inode;
Chris Mason75dfe392007-03-29 11:56:46 -04001038
1039 for (i = 0; i < num_pages; i++) {
1040 offset = pos & (PAGE_CACHE_SIZE -1);
1041 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
Chris Masonf254e522007-03-29 15:15:27 -04001042 /* FIXME, one block at a time */
1043
1044 mutex_lock(&root->fs_info->fs_mutex);
1045 trans = btrfs_start_transaction(root, 1);
1046 btrfs_csum_file_block(trans, root, inode->i_ino,
1047 pages[i]->index << PAGE_CACHE_SHIFT,
1048 kmap(pages[i]), PAGE_CACHE_SIZE);
1049 kunmap(pages[i]);
1050 SetPageChecked(pages[i]);
1051 ret = btrfs_end_transaction(trans, root);
1052 BUG_ON(ret);
1053 mutex_unlock(&root->fs_info->fs_mutex);
1054
Chris Mason75dfe392007-03-29 11:56:46 -04001055 ret = nobh_commit_write(file, pages[i], offset,
1056 offset + this_write);
1057 pos += this_write;
1058 if (ret) {
1059 err = ret;
1060 goto failed;
1061 }
1062 WARN_ON(this_write > write_bytes);
1063 write_bytes -= this_write;
1064 }
1065failed:
1066 return err;
1067}
1068
1069static int prepare_pages(struct btrfs_trans_handle *trans,
1070 struct btrfs_root *root,
1071 struct file *file,
1072 struct page **pages,
1073 size_t num_pages,
1074 loff_t pos,
1075 size_t write_bytes)
1076{
1077 int i;
1078 unsigned long index = pos >> PAGE_CACHE_SHIFT;
1079 struct inode *inode = file->f_path.dentry->d_inode;
1080 int offset;
1081 int err = 0;
1082 int ret;
1083 int this_write;
1084 loff_t isize = i_size_read(inode);
1085
1086 memset(pages, 0, num_pages * sizeof(struct page *));
1087
1088 for (i = 0; i < num_pages; i++) {
1089 pages[i] = grab_cache_page(inode->i_mapping, index + i);
1090 if (!pages[i]) {
1091 err = -ENOMEM;
1092 goto failed_release;
1093 }
1094 offset = pos & (PAGE_CACHE_SIZE -1);
1095 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1096 ret = nobh_prepare_write(pages[i], offset,
1097 offset + this_write,
Chris Masonf254e522007-03-29 15:15:27 -04001098 btrfs_get_block);
Chris Mason75dfe392007-03-29 11:56:46 -04001099 pos += this_write;
1100 if (ret) {
1101 err = ret;
1102 goto failed_truncate;
1103 }
1104 WARN_ON(this_write > write_bytes);
1105 write_bytes -= this_write;
1106 }
1107 return 0;
1108
1109failed_release:
1110 btrfs_drop_pages(pages, num_pages);
1111 return err;
1112
1113failed_truncate:
1114 btrfs_drop_pages(pages, num_pages);
1115 if (pos > isize)
1116 vmtruncate(inode, isize);
1117 return err;
1118}
1119
1120static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1121 size_t count, loff_t *ppos)
1122{
1123 loff_t pos;
1124 size_t num_written = 0;
1125 int err = 0;
1126 int ret = 0;
Chris Mason75dfe392007-03-29 11:56:46 -04001127 struct inode *inode = file->f_path.dentry->d_inode;
1128 struct btrfs_root *root = btrfs_sb(inode->i_sb);
1129 struct page *pages[1];
1130
1131 if (file->f_flags & O_DIRECT)
1132 return -EINVAL;
1133 pos = *ppos;
1134
1135 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1136 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1137 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1138 if (err)
1139 goto out;
1140 if (count == 0)
1141 goto out;
1142 err = remove_suid(file->f_path.dentry);
1143 if (err)
1144 goto out;
1145 file_update_time(file);
1146 mutex_lock(&inode->i_mutex);
1147 while(count > 0) {
1148 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1149 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1150 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
1151 PAGE_CACHE_SHIFT;
Chris Masonf254e522007-03-29 15:15:27 -04001152 ret = prepare_pages(NULL, root, file, pages, num_pages,
Chris Mason75dfe392007-03-29 11:56:46 -04001153 pos, write_bytes);
1154 BUG_ON(ret);
1155 ret = btrfs_copy_from_user(pos, num_pages,
1156 write_bytes, pages, buf);
1157 BUG_ON(ret);
1158
Chris Masonf254e522007-03-29 15:15:27 -04001159 ret = dirty_and_release_pages(NULL, root, file, pages,
Chris Mason75dfe392007-03-29 11:56:46 -04001160 num_pages, pos, write_bytes);
1161 BUG_ON(ret);
1162 btrfs_drop_pages(pages, num_pages);
1163
Chris Mason75dfe392007-03-29 11:56:46 -04001164 buf += write_bytes;
1165 count -= write_bytes;
1166 pos += write_bytes;
1167 num_written += write_bytes;
1168
1169 balance_dirty_pages_ratelimited(inode->i_mapping);
1170 cond_resched();
1171 }
1172 mutex_unlock(&inode->i_mutex);
1173out:
1174 *ppos = pos;
1175 current->backing_dev_info = NULL;
1176 return num_written ? num_written : err;
1177}
1178
Chris Masonf254e522007-03-29 15:15:27 -04001179static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
1180 unsigned long offset, unsigned long size)
1181{
1182 char *kaddr;
1183 unsigned long left, count = desc->count;
1184
1185 if (size > count)
1186 size = count;
1187
1188 if (!PageChecked(page)) {
1189 /* FIXME, do it per block */
1190 struct btrfs_root *root = btrfs_sb(page->mapping->host->i_sb);
1191 int ret = btrfs_csum_verify_file_block(root,
1192 page->mapping->host->i_ino,
1193 page->index << PAGE_CACHE_SHIFT,
1194 kmap(page), PAGE_CACHE_SIZE);
1195 if (ret) {
1196 printk("failed to verify ino %lu page %lu\n",
1197 page->mapping->host->i_ino,
1198 page->index);
1199 memset(page_address(page), 0, PAGE_CACHE_SIZE);
1200 }
1201 SetPageChecked(page);
1202 kunmap(page);
1203 }
1204 /*
1205 * Faults on the destination of a read are common, so do it before
1206 * taking the kmap.
1207 */
1208 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1209 kaddr = kmap_atomic(page, KM_USER0);
1210 left = __copy_to_user_inatomic(desc->arg.buf,
1211 kaddr + offset, size);
1212 kunmap_atomic(kaddr, KM_USER0);
1213 if (left == 0)
1214 goto success;
1215 }
1216
1217 /* Do it the slow way */
1218 kaddr = kmap(page);
1219 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1220 kunmap(page);
1221
1222 if (left) {
1223 size -= left;
1224 desc->error = -EFAULT;
1225 }
1226success:
1227 desc->count = count - size;
1228 desc->written += size;
1229 desc->arg.buf += size;
1230 return size;
1231}
1232
1233/**
1234 * btrfs_file_aio_read - filesystem read routine
1235 * @iocb: kernel I/O control block
1236 * @iov: io vector request
1237 * @nr_segs: number of segments in the iovec
1238 * @pos: current file position
1239 */
1240static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1241 unsigned long nr_segs, loff_t pos)
1242{
1243 struct file *filp = iocb->ki_filp;
1244 ssize_t retval;
1245 unsigned long seg;
1246 size_t count;
1247 loff_t *ppos = &iocb->ki_pos;
1248
1249 count = 0;
1250 for (seg = 0; seg < nr_segs; seg++) {
1251 const struct iovec *iv = &iov[seg];
1252
1253 /*
1254 * If any segment has a negative length, or the cumulative
1255 * length ever wraps negative then return -EINVAL.
1256 */
1257 count += iv->iov_len;
1258 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
1259 return -EINVAL;
1260 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
1261 continue;
1262 if (seg == 0)
1263 return -EFAULT;
1264 nr_segs = seg;
1265 count -= iv->iov_len; /* This segment is no good */
1266 break;
1267 }
1268 retval = 0;
1269 if (count) {
1270 for (seg = 0; seg < nr_segs; seg++) {
1271 read_descriptor_t desc;
1272
1273 desc.written = 0;
1274 desc.arg.buf = iov[seg].iov_base;
1275 desc.count = iov[seg].iov_len;
1276 if (desc.count == 0)
1277 continue;
1278 desc.error = 0;
1279 do_generic_file_read(filp, ppos, &desc,
1280 btrfs_read_actor);
1281 retval += desc.written;
1282 if (desc.error) {
1283 retval = retval ?: desc.error;
1284 break;
1285 }
1286 }
1287 }
1288 return retval;
1289}
1290
Chris Mason2c90e5d2007-04-02 10:50:19 -04001291static struct kmem_cache *btrfs_inode_cachep;
1292struct kmem_cache *btrfs_trans_handle_cachep;
1293struct kmem_cache *btrfs_transaction_cachep;
1294struct kmem_cache *btrfs_bit_radix_cachep;
1295struct kmem_cache *btrfs_path_cachep;
1296
1297/*
1298 * Called inside transaction, so use GFP_NOFS
1299 */
1300static struct inode *btrfs_alloc_inode(struct super_block *sb)
1301{
1302 struct btrfs_inode *ei;
1303
1304 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
1305 if (!ei)
1306 return NULL;
1307 ei->magic = 0xDEADBEEF;
1308 ei->magic2 = 0xDEADBEAF;
1309 return &ei->vfs_inode;
1310}
1311
1312static void btrfs_destroy_inode(struct inode *inode)
1313{
1314 struct btrfs_inode *ei = BTRFS_I(inode);
1315 WARN_ON(ei->magic != 0xDEADBEEF);
1316 WARN_ON(ei->magic2 != 0xDEADBEAF);
1317 WARN_ON(!list_empty(&inode->i_dentry));
1318 WARN_ON(inode->i_ino == 1);
1319 WARN_ON(inode->i_data.nrpages);
1320
1321 ei->magic = 0;
1322 ei->magic2 = 0;
1323 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
1324}
1325
1326static void init_once(void * foo, struct kmem_cache * cachep,
1327 unsigned long flags)
1328{
1329 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
1330
1331 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1332 SLAB_CTOR_CONSTRUCTOR) {
1333 inode_init_once(&ei->vfs_inode);
1334 }
1335}
1336
1337static int init_inodecache(void)
1338{
1339 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
1340 sizeof(struct btrfs_inode),
1341 0, (SLAB_RECLAIM_ACCOUNT|
1342 SLAB_MEM_SPREAD),
1343 init_once, NULL);
1344 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
1345 sizeof(struct btrfs_trans_handle),
1346 0, (SLAB_RECLAIM_ACCOUNT|
1347 SLAB_MEM_SPREAD),
1348 NULL, NULL);
1349 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
1350 sizeof(struct btrfs_transaction),
1351 0, (SLAB_RECLAIM_ACCOUNT|
1352 SLAB_MEM_SPREAD),
1353 NULL, NULL);
1354 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
1355 sizeof(struct btrfs_transaction),
1356 0, (SLAB_RECLAIM_ACCOUNT|
1357 SLAB_MEM_SPREAD),
1358 NULL, NULL);
1359 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
1360 256,
1361 0, (SLAB_RECLAIM_ACCOUNT|
1362 SLAB_MEM_SPREAD |
1363 SLAB_DESTROY_BY_RCU),
1364 NULL, NULL);
1365 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
1366 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
1367 return -ENOMEM;
1368 return 0;
1369}
1370
1371static void destroy_inodecache(void)
1372{
1373 kmem_cache_destroy(btrfs_inode_cachep);
1374 kmem_cache_destroy(btrfs_trans_handle_cachep);
1375 kmem_cache_destroy(btrfs_transaction_cachep);
1376 kmem_cache_destroy(btrfs_bit_radix_cachep);
1377 kmem_cache_destroy(btrfs_path_cachep);
1378}
1379
Chris Mason2e635a22007-03-21 11:12:56 -04001380static int btrfs_get_sb(struct file_system_type *fs_type,
1381 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1382{
1383 return get_sb_bdev(fs_type, flags, dev_name, data,
1384 btrfs_fill_super, mnt);
1385}
1386
1387static struct file_system_type btrfs_fs_type = {
1388 .owner = THIS_MODULE,
1389 .name = "btrfs",
1390 .get_sb = btrfs_get_sb,
1391 .kill_sb = kill_block_super,
1392 .fs_flags = FS_REQUIRES_DEV,
1393};
1394
Chris Masone20d96d2007-03-22 12:13:20 -04001395static struct super_operations btrfs_super_ops = {
1396 .statfs = simple_statfs,
Chris Mason134e9732007-03-25 13:44:56 -04001397 .delete_inode = btrfs_delete_inode,
Chris Masone20d96d2007-03-22 12:13:20 -04001398 .put_super = btrfs_put_super,
1399 .read_inode = btrfs_read_locked_inode,
Chris Masond5719762007-03-23 10:01:08 -04001400 .write_super = btrfs_write_super,
1401 .sync_fs = btrfs_sync_fs,
Chris Mason4730a4b2007-03-26 12:00:39 -04001402 .write_inode = btrfs_write_inode,
Chris Mason2c90e5d2007-04-02 10:50:19 -04001403 .alloc_inode = btrfs_alloc_inode,
1404 .destroy_inode = btrfs_destroy_inode,
Chris Masone20d96d2007-03-22 12:13:20 -04001405};
1406
1407static struct inode_operations btrfs_dir_inode_operations = {
1408 .lookup = btrfs_lookup,
Chris Masond5719762007-03-23 10:01:08 -04001409 .create = btrfs_create,
Chris Mason134e9732007-03-25 13:44:56 -04001410 .unlink = btrfs_unlink,
Chris Masonf7922032007-03-25 20:17:36 -04001411 .mkdir = btrfs_mkdir,
Chris Mason5f443fd2007-03-27 13:42:32 -04001412 .rmdir = btrfs_rmdir,
Chris Masone20d96d2007-03-22 12:13:20 -04001413};
1414
1415static struct file_operations btrfs_dir_file_operations = {
1416 .llseek = generic_file_llseek,
1417 .read = generic_read_dir,
1418 .readdir = btrfs_readdir,
1419};
1420
Chris Masondee26a92007-03-26 16:00:06 -04001421static struct address_space_operations btrfs_aops = {
1422 .readpage = btrfs_readpage,
1423 .readpages = btrfs_readpages,
1424 .writepage = btrfs_writepage,
1425 .sync_page = block_sync_page,
1426 .prepare_write = btrfs_prepare_write,
Chris Mason75dfe392007-03-29 11:56:46 -04001427 .commit_write = btrfs_commit_write,
Chris Masondee26a92007-03-26 16:00:06 -04001428};
1429
1430static struct inode_operations btrfs_file_inode_operations = {
Chris Masonf4b9aa82007-03-27 11:05:53 -04001431 .truncate = btrfs_truncate,
Chris Masondee26a92007-03-26 16:00:06 -04001432};
1433
1434static struct file_operations btrfs_file_operations = {
1435 .llseek = generic_file_llseek,
1436 .read = do_sync_read,
Chris Masonf254e522007-03-29 15:15:27 -04001437 .aio_read = btrfs_file_aio_read,
Chris Mason75dfe392007-03-29 11:56:46 -04001438 .write = btrfs_file_write,
Chris Masondee26a92007-03-26 16:00:06 -04001439 .mmap = generic_file_mmap,
1440 .open = generic_file_open,
Chris Masondee26a92007-03-26 16:00:06 -04001441};
Chris Masone20d96d2007-03-22 12:13:20 -04001442
Chris Mason2e635a22007-03-21 11:12:56 -04001443static int __init init_btrfs_fs(void)
1444{
Chris Mason2c90e5d2007-04-02 10:50:19 -04001445 int err;
Chris Mason2e635a22007-03-21 11:12:56 -04001446 printk("btrfs loaded!\n");
Chris Mason2c90e5d2007-04-02 10:50:19 -04001447 err = init_inodecache();
1448 if (err)
1449 return err;
Chris Mason2e635a22007-03-21 11:12:56 -04001450 return register_filesystem(&btrfs_fs_type);
1451}
1452
1453static void __exit exit_btrfs_fs(void)
1454{
Chris Mason2c90e5d2007-04-02 10:50:19 -04001455 destroy_inodecache();
Chris Mason2e635a22007-03-21 11:12:56 -04001456 unregister_filesystem(&btrfs_fs_type);
1457 printk("btrfs unloaded\n");
1458}
1459
1460module_init(init_btrfs_fs)
1461module_exit(exit_btrfs_fs)
1462
1463MODULE_LICENSE("GPL");