blob: 3e8bfb0e5d7e8253d67814c86b45af35177693a9 [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 Masonc5739bb2007-04-10 09:27:04 -040018#include "ioctl.h"
Chris Mason2e635a22007-03-21 11:12:56 -040019
Chris Masond6e4a422007-04-06 15:37:36 -040020void btrfs_fsinfo_release(struct kobject *obj)
21{
22 struct btrfs_fs_info *fsinfo = container_of(obj,
23 struct btrfs_fs_info, kobj);
24 kfree(fsinfo);
25}
26
27struct kobj_type btrfs_fsinfo_ktype = {
28 .release = btrfs_fsinfo_release,
29};
30
Chris Masonc5739bb2007-04-10 09:27:04 -040031struct btrfs_iget_args {
32 u64 ino;
33 struct btrfs_root *root;
34};
35
Chris Masond6e4a422007-04-06 15:37:36 -040036decl_subsys(btrfs, &btrfs_fsinfo_ktype, NULL);
37
Chris Mason2e635a22007-03-21 11:12:56 -040038#define BTRFS_SUPER_MAGIC 0x9123682E
Chris Masone20d96d2007-03-22 12:13:20 -040039
40static struct inode_operations btrfs_dir_inode_operations;
Chris Masond6e4a422007-04-06 15:37:36 -040041static struct inode_operations btrfs_dir_ro_inode_operations;
Chris Masone20d96d2007-03-22 12:13:20 -040042static struct super_operations btrfs_super_ops;
43static struct file_operations btrfs_dir_file_operations;
Chris Masondee26a92007-03-26 16:00:06 -040044static struct inode_operations btrfs_file_inode_operations;
45static struct address_space_operations btrfs_aops;
46static struct file_operations btrfs_file_operations;
Chris Masone20d96d2007-03-22 12:13:20 -040047
Chris Masone20d96d2007-03-22 12:13:20 -040048static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason2e635a22007-03-21 11:12:56 -040049{
Chris Mason5caf2a02007-04-02 11:20:42 -040050 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -040051 struct btrfs_inode_item *inode_item;
Chris Masond6e4a422007-04-06 15:37:36 -040052 struct btrfs_root *root = BTRFS_I(inode)->root;
53 struct btrfs_key location;
Chris Masone20d96d2007-03-22 12:13:20 -040054 int ret;
Chris Masonf4b9aa82007-03-27 11:05:53 -040055
Chris Mason5caf2a02007-04-02 11:20:42 -040056 path = btrfs_alloc_path();
57 BUG_ON(!path);
58 btrfs_init_path(path);
Chris Masonf4b9aa82007-03-27 11:05:53 -040059 mutex_lock(&root->fs_info->fs_mutex);
60
Chris Masond6e4a422007-04-06 15:37:36 -040061 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
62 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Masone20d96d2007-03-22 12:13:20 -040063 if (ret) {
Chris Mason7cfcc172007-04-02 14:53:59 -040064 btrfs_free_path(path);
Chris Masond6e4a422007-04-06 15:37:36 -040065 goto make_bad;
Chris Mason2e635a22007-03-21 11:12:56 -040066 }
Chris Mason5caf2a02007-04-02 11:20:42 -040067 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
68 path->slots[0],
Chris Masone20d96d2007-03-22 12:13:20 -040069 struct btrfs_inode_item);
70
Chris Masone20d96d2007-03-22 12:13:20 -040071 inode->i_mode = btrfs_inode_mode(inode_item);
72 inode->i_nlink = btrfs_inode_nlink(inode_item);
73 inode->i_uid = btrfs_inode_uid(inode_item);
74 inode->i_gid = btrfs_inode_gid(inode_item);
75 inode->i_size = btrfs_inode_size(inode_item);
76 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
77 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
78 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
79 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
80 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
81 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
82 inode->i_blocks = btrfs_inode_nblocks(inode_item);
83 inode->i_generation = btrfs_inode_generation(inode_item);
Chris Mason5caf2a02007-04-02 11:20:42 -040084
Chris Mason5caf2a02007-04-02 11:20:42 -040085 btrfs_free_path(path);
86 inode_item = NULL;
87
Chris Masonf4b9aa82007-03-27 11:05:53 -040088 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1b05da22007-04-10 12:13:09 -040089
Chris Masone20d96d2007-03-22 12:13:20 -040090 switch (inode->i_mode & S_IFMT) {
91#if 0
92 default:
93 init_special_inode(inode, inode->i_mode,
94 btrfs_inode_rdev(inode_item));
95 break;
96#endif
97 case S_IFREG:
Chris Masondee26a92007-03-26 16:00:06 -040098 inode->i_mapping->a_ops = &btrfs_aops;
99 inode->i_fop = &btrfs_file_operations;
100 inode->i_op = &btrfs_file_inode_operations;
Chris Masone20d96d2007-03-22 12:13:20 -0400101 break;
102 case S_IFDIR:
Chris Masone20d96d2007-03-22 12:13:20 -0400103 inode->i_fop = &btrfs_dir_file_operations;
Chris Masond6e4a422007-04-06 15:37:36 -0400104 if (root == root->fs_info->tree_root)
105 inode->i_op = &btrfs_dir_ro_inode_operations;
106 else
107 inode->i_op = &btrfs_dir_inode_operations;
Chris Masone20d96d2007-03-22 12:13:20 -0400108 break;
109 case S_IFLNK:
Chris Masone20d96d2007-03-22 12:13:20 -0400110 // inode->i_op = &page_symlink_inode_operations;
111 break;
112 }
Chris Masone20d96d2007-03-22 12:13:20 -0400113 return;
Chris Masond6e4a422007-04-06 15:37:36 -0400114
115make_bad:
116 btrfs_release_path(root, path);
117 btrfs_free_path(path);
118 mutex_unlock(&root->fs_info->fs_mutex);
119 make_bad_inode(inode);
Chris Mason2e635a22007-03-21 11:12:56 -0400120}
121
Chris Mason5f443fd2007-03-27 13:42:32 -0400122static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
123 struct btrfs_root *root,
124 struct inode *dir,
125 struct dentry *dentry)
Chris Mason134e9732007-03-25 13:44:56 -0400126{
Chris Mason5caf2a02007-04-02 11:20:42 -0400127 struct btrfs_path *path;
Chris Mason134e9732007-03-25 13:44:56 -0400128 const char *name = dentry->d_name.name;
129 int name_len = dentry->d_name.len;
130 int ret;
131 u64 objectid;
132 struct btrfs_dir_item *di;
133
Chris Mason5caf2a02007-04-02 11:20:42 -0400134 path = btrfs_alloc_path();
135 BUG_ON(!path);
136 btrfs_init_path(path);
137 ret = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
Chris Mason134e9732007-03-25 13:44:56 -0400138 name, name_len, -1);
139 if (ret < 0)
140 goto err;
141 if (ret > 0) {
142 ret = -ENOENT;
143 goto err;
144 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400145 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason134e9732007-03-25 13:44:56 -0400146 struct btrfs_dir_item);
Chris Masond6e4a422007-04-06 15:37:36 -0400147 objectid = btrfs_disk_key_objectid(&di->location);
Chris Mason134e9732007-03-25 13:44:56 -0400148
Chris Mason5caf2a02007-04-02 11:20:42 -0400149 ret = btrfs_del_item(trans, root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400150 BUG_ON(ret);
Chris Mason5f26f772007-04-05 10:38:44 -0400151
152 btrfs_release_path(root, path);
153 ret = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
154 objectid, -1);
155 BUG_ON(ret);
156 ret = btrfs_del_item(trans, root, path);
157 BUG_ON(ret);
Chris Mason134e9732007-03-25 13:44:56 -0400158 dentry->d_inode->i_ctime = dir->i_ctime;
159err:
Chris Mason5caf2a02007-04-02 11:20:42 -0400160 btrfs_release_path(root, path);
161 btrfs_free_path(path);
Chris Masond4dbff92007-04-04 14:08:15 -0400162 if (ret == 0) {
Chris Mason134e9732007-03-25 13:44:56 -0400163 inode_dec_link_count(dentry->d_inode);
Chris Mason5f26f772007-04-05 10:38:44 -0400164 dir->i_size -= name_len * 2;
Chris Masond4dbff92007-04-04 14:08:15 -0400165 mark_inode_dirty(dir);
166 }
Chris Mason134e9732007-03-25 13:44:56 -0400167 return ret;
168}
169
Chris Mason5f443fd2007-03-27 13:42:32 -0400170static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
171{
172 struct btrfs_root *root;
173 struct btrfs_trans_handle *trans;
174 int ret;
175
Chris Masond6e4a422007-04-06 15:37:36 -0400176 root = BTRFS_I(dir)->root;
Chris Mason5f443fd2007-03-27 13:42:32 -0400177 mutex_lock(&root->fs_info->fs_mutex);
178 trans = btrfs_start_transaction(root, 1);
179 ret = btrfs_unlink_trans(trans, root, dir, dentry);
180 btrfs_end_transaction(trans, root);
181 mutex_unlock(&root->fs_info->fs_mutex);
182 return ret;
183}
184
185static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
186{
187 struct inode *inode = dentry->d_inode;
188 int err;
189 int ret;
Chris Masond6e4a422007-04-06 15:37:36 -0400190 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason5caf2a02007-04-02 11:20:42 -0400191 struct btrfs_path *path;
Chris Mason5f443fd2007-03-27 13:42:32 -0400192 struct btrfs_key key;
193 struct btrfs_trans_handle *trans;
Chris Mason5f26f772007-04-05 10:38:44 -0400194 struct btrfs_key found_key;
195 int found_type;
Chris Mason5f443fd2007-03-27 13:42:32 -0400196 struct btrfs_leaf *leaf;
Chris Mason5f26f772007-04-05 10:38:44 -0400197 char *goodnames = "..";
Chris Mason5f443fd2007-03-27 13:42:32 -0400198
Chris Mason5caf2a02007-04-02 11:20:42 -0400199 path = btrfs_alloc_path();
200 BUG_ON(!path);
201 btrfs_init_path(path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400202 mutex_lock(&root->fs_info->fs_mutex);
203 trans = btrfs_start_transaction(root, 1);
204 key.objectid = inode->i_ino;
205 key.offset = (u64)-1;
Chris Mason5f26f772007-04-05 10:38:44 -0400206 key.flags = (u32)-1;
207 while(1) {
208 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
209 if (ret < 0) {
210 err = ret;
211 goto out;
212 }
213 BUG_ON(ret == 0);
214 if (path->slots[0] == 0) {
215 err = -ENOENT;
216 goto out;
217 }
218 path->slots[0]--;
219 leaf = btrfs_buffer_leaf(path->nodes[0]);
220 btrfs_disk_key_to_cpu(&found_key,
221 &leaf->items[path->slots[0]].key);
222 found_type = btrfs_key_type(&found_key);
223 if (found_key.objectid != inode->i_ino) {
224 err = -ENOENT;
225 goto out;
226 }
227 if ((found_type != BTRFS_DIR_ITEM_KEY &&
228 found_type != BTRFS_DIR_INDEX_KEY) ||
229 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
230 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
231 err = -ENOTEMPTY;
232 goto out;
233 }
234 ret = btrfs_del_item(trans, root, path);
235 BUG_ON(ret);
Chris Mason5f443fd2007-03-27 13:42:32 -0400236
Chris Mason5f26f772007-04-05 10:38:44 -0400237 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
238 break;
239 btrfs_release_path(root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400240 }
Chris Mason5f26f772007-04-05 10:38:44 -0400241 ret = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400242 btrfs_release_path(root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400243
244 /* now the directory is empty */
245 err = btrfs_unlink_trans(trans, root, dir, dentry);
246 if (!err) {
247 inode->i_size = 0;
248 }
249out:
Chris Mason7cfcc172007-04-02 14:53:59 -0400250 btrfs_release_path(root, path);
251 btrfs_free_path(path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400252 mutex_unlock(&root->fs_info->fs_mutex);
253 ret = btrfs_end_transaction(trans, root);
254 if (ret && !err)
255 err = ret;
256 return err;
257}
258
Chris Mason134e9732007-03-25 13:44:56 -0400259static int btrfs_free_inode(struct btrfs_trans_handle *trans,
260 struct btrfs_root *root,
261 struct inode *inode)
262{
Chris Mason5caf2a02007-04-02 11:20:42 -0400263 struct btrfs_path *path;
Chris Mason134e9732007-03-25 13:44:56 -0400264 int ret;
Chris Mason5caf2a02007-04-02 11:20:42 -0400265
Chris Mason134e9732007-03-25 13:44:56 -0400266 clear_inode(inode);
Chris Mason5caf2a02007-04-02 11:20:42 -0400267
268 path = btrfs_alloc_path();
269 BUG_ON(!path);
270 btrfs_init_path(path);
Chris Masond6e4a422007-04-06 15:37:36 -0400271 ret = btrfs_lookup_inode(trans, root, path,
272 &BTRFS_I(inode)->location, -1);
Chris Mason134e9732007-03-25 13:44:56 -0400273 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400274 ret = btrfs_del_item(trans, root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400275 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400276 btrfs_free_path(path);
Chris Mason134e9732007-03-25 13:44:56 -0400277 return ret;
278}
279
Chris Masonf4b9aa82007-03-27 11:05:53 -0400280static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
281 struct btrfs_root *root,
282 struct inode *inode)
283{
284 int ret;
Chris Mason5caf2a02007-04-02 11:20:42 -0400285 struct btrfs_path *path;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400286 struct btrfs_key key;
287 struct btrfs_disk_key *found_key;
288 struct btrfs_leaf *leaf;
Chris Masonf254e522007-03-29 15:15:27 -0400289 struct btrfs_file_extent_item *fi = NULL;
290 u64 extent_start = 0;
291 u64 extent_num_blocks = 0;
292 int found_extent;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400293
Chris Mason5caf2a02007-04-02 11:20:42 -0400294 path = btrfs_alloc_path();
295 BUG_ON(!path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400296 /* FIXME, add redo link to tree so we don't leak on crash */
297 key.objectid = inode->i_ino;
298 key.offset = (u64)-1;
299 key.flags = 0;
Chris Masond4dbff92007-04-04 14:08:15 -0400300 /*
301 * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys
302 * or extent data
303 */
Chris Masonf254e522007-03-29 15:15:27 -0400304 btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400305 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400306 btrfs_init_path(path);
307 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400308 if (ret < 0) {
Chris Masonf4b9aa82007-03-27 11:05:53 -0400309 goto error;
310 }
311 if (ret > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400312 BUG_ON(path->slots[0] == 0);
313 path->slots[0]--;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400314 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400315 leaf = btrfs_buffer_leaf(path->nodes[0]);
316 found_key = &leaf->items[path->slots[0]].key;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400317 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
318 break;
Chris Masonf254e522007-03-29 15:15:27 -0400319 if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY &&
Chris Masond4dbff92007-04-04 14:08:15 -0400320 btrfs_disk_key_type(found_key) != BTRFS_INLINE_DATA_KEY &&
Chris Masonf254e522007-03-29 15:15:27 -0400321 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
Chris Masonf4b9aa82007-03-27 11:05:53 -0400322 break;
323 if (btrfs_disk_key_offset(found_key) < inode->i_size)
324 break;
Chris Masonf254e522007-03-29 15:15:27 -0400325 if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400326 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
327 path->slots[0],
Chris Masonf254e522007-03-29 15:15:27 -0400328 struct btrfs_file_extent_item);
329 extent_start = btrfs_file_extent_disk_blocknr(fi);
330 extent_num_blocks =
331 btrfs_file_extent_disk_num_blocks(fi);
332 inode->i_blocks -=
333 btrfs_file_extent_num_blocks(fi) >> 9;
334 found_extent = 1;
335 } else {
336 found_extent = 0;
337 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400338 ret = btrfs_del_item(trans, root, path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400339 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400340 btrfs_release_path(root, path);
Chris Masonf254e522007-03-29 15:15:27 -0400341 if (found_extent) {
342 ret = btrfs_free_extent(trans, root, extent_start,
343 extent_num_blocks, 0);
344 BUG_ON(ret);
345 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400346 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400347 ret = 0;
348error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400349 btrfs_release_path(root, path);
350 btrfs_free_path(path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400351 return ret;
352}
353
Chris Mason134e9732007-03-25 13:44:56 -0400354static void btrfs_delete_inode(struct inode *inode)
355{
356 struct btrfs_trans_handle *trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400357 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400358 int ret;
359
Chris Mason134e9732007-03-25 13:44:56 -0400360 truncate_inode_pages(&inode->i_data, 0);
361 if (is_bad_inode(inode)) {
362 goto no_delete;
363 }
364 inode->i_size = 0;
Chris Mason134e9732007-03-25 13:44:56 -0400365 mutex_lock(&root->fs_info->fs_mutex);
366 trans = btrfs_start_transaction(root, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400367 if (S_ISREG(inode->i_mode)) {
368 ret = btrfs_truncate_in_trans(trans, root, inode);
369 BUG_ON(ret);
370 }
Chris Mason134e9732007-03-25 13:44:56 -0400371 btrfs_free_inode(trans, root, inode);
372 btrfs_end_transaction(trans, root);
373 mutex_unlock(&root->fs_info->fs_mutex);
374 return;
375no_delete:
376 clear_inode(inode);
377}
378
Chris Masone20d96d2007-03-22 12:13:20 -0400379static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Chris Masond6e4a422007-04-06 15:37:36 -0400380 struct btrfs_key *location)
Chris Masone20d96d2007-03-22 12:13:20 -0400381{
382 const char *name = dentry->d_name.name;
383 int namelen = dentry->d_name.len;
384 struct btrfs_dir_item *di;
Chris Mason5caf2a02007-04-02 11:20:42 -0400385 struct btrfs_path *path;
Chris Masond6e4a422007-04-06 15:37:36 -0400386 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masone20d96d2007-03-22 12:13:20 -0400387 int ret;
388
Chris Mason5caf2a02007-04-02 11:20:42 -0400389 path = btrfs_alloc_path();
390 BUG_ON(!path);
391 btrfs_init_path(path);
392 ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
Chris Masone20d96d2007-03-22 12:13:20 -0400393 namelen, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400394 if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) {
Chris Masond6e4a422007-04-06 15:37:36 -0400395 location->objectid = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400396 ret = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400397 goto out;
398 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400399 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masone20d96d2007-03-22 12:13:20 -0400400 struct btrfs_dir_item);
Chris Masond6e4a422007-04-06 15:37:36 -0400401 btrfs_disk_key_to_cpu(location, &di->location);
Chris Masone20d96d2007-03-22 12:13:20 -0400402out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400403 btrfs_release_path(root, path);
404 btrfs_free_path(path);
Chris Masone20d96d2007-03-22 12:13:20 -0400405 return ret;
406}
407
Chris Masond6e4a422007-04-06 15:37:36 -0400408int fixup_tree_root_location(struct btrfs_root *root,
409 struct btrfs_key *location,
410 struct btrfs_root **sub_root)
411{
412 struct btrfs_path *path;
413 struct btrfs_root_item *ri;
Chris Masond6e4a422007-04-06 15:37:36 -0400414
415 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
416 return 0;
417 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
418 return 0;
419
420 path = btrfs_alloc_path();
421 BUG_ON(!path);
422 mutex_lock(&root->fs_info->fs_mutex);
423
Chris Mason0f7d52f2007-04-09 10:42:37 -0400424 *sub_root = btrfs_read_fs_root(root->fs_info, location);
425 if (IS_ERR(*sub_root))
426 return PTR_ERR(*sub_root);
427
428 ri = &(*sub_root)->root_item;
Chris Masond6e4a422007-04-06 15:37:36 -0400429 location->objectid = btrfs_root_dirid(ri);
430 location->flags = 0;
431 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
432 location->offset = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400433
Chris Masond6e4a422007-04-06 15:37:36 -0400434 btrfs_free_path(path);
435 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400436 return 0;
Chris Masond6e4a422007-04-06 15:37:36 -0400437}
438
Chris Masonc5739bb2007-04-10 09:27:04 -0400439int btrfs_init_locked_inode(struct inode *inode, void *p)
440{
441 struct btrfs_iget_args *args = p;
442 inode->i_ino = args->ino;
443 BTRFS_I(inode)->root = args->root;
444 return 0;
445}
446
447int btrfs_find_actor(struct inode *inode, void *opaque)
448{
449 struct btrfs_iget_args *args = opaque;
450 return (args->ino == inode->i_ino &&
451 args->root == BTRFS_I(inode)->root);
452}
453
454struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
455 struct btrfs_root *root)
456{
457 struct inode *inode;
458 struct btrfs_iget_args args;
459 args.ino = objectid;
460 args.root = root;
461
462 inode = iget5_locked(s, objectid, btrfs_find_actor,
463 btrfs_init_locked_inode,
464 (void *)&args);
465 return inode;
466}
Chris Masond6e4a422007-04-06 15:37:36 -0400467
Chris Masone20d96d2007-03-22 12:13:20 -0400468static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
469 struct nameidata *nd)
470{
471 struct inode * inode;
Chris Masond6e4a422007-04-06 15:37:36 -0400472 struct btrfs_inode *bi = BTRFS_I(dir);
473 struct btrfs_root *root = bi->root;
474 struct btrfs_root *sub_root = root;
475 struct btrfs_key location;
Chris Masone20d96d2007-03-22 12:13:20 -0400476 int ret;
477
478 if (dentry->d_name.len > BTRFS_NAME_LEN)
479 return ERR_PTR(-ENAMETOOLONG);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400480 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond6e4a422007-04-06 15:37:36 -0400481 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400482 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400483 if (ret < 0)
484 return ERR_PTR(ret);
485 inode = NULL;
Chris Masond6e4a422007-04-06 15:37:36 -0400486 if (location.objectid) {
487 ret = fixup_tree_root_location(root, &location, &sub_root);
488 if (ret < 0)
489 return ERR_PTR(ret);
490 if (ret > 0)
491 return ERR_PTR(-ENOENT);
Chris Masonc5739bb2007-04-10 09:27:04 -0400492 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
493 sub_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400494 if (!inode)
495 return ERR_PTR(-EACCES);
Chris Masond6e4a422007-04-06 15:37:36 -0400496 if (inode->i_state & I_NEW) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400497 if (sub_root != root) {
498 ret = radix_tree_insert(
499 &root->fs_info->fs_roots_radix,
500 (unsigned long)sub_root,
501 sub_root);
Chris Masonc5739bb2007-04-10 09:27:04 -0400502printk("adding new root for inode %lu root %p (found %p)\n", inode->i_ino, sub_root, BTRFS_I(inode)->root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400503 igrab(inode);
504 sub_root->inode = inode;
505 }
Chris Masond6e4a422007-04-06 15:37:36 -0400506 BTRFS_I(inode)->root = sub_root;
507 memcpy(&BTRFS_I(inode)->location, &location,
508 sizeof(location));
509 btrfs_read_locked_inode(inode);
510 unlock_new_inode(inode);
511 }
Chris Masone20d96d2007-03-22 12:13:20 -0400512 }
513 return d_splice_alias(inode, dentry);
514}
515
516static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
517{
518 struct inode *inode = filp->f_path.dentry->d_inode;
Chris Masond6e4a422007-04-06 15:37:36 -0400519 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone20d96d2007-03-22 12:13:20 -0400520 struct btrfs_item *item;
521 struct btrfs_dir_item *di;
522 struct btrfs_key key;
Chris Mason5caf2a02007-04-02 11:20:42 -0400523 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -0400524 int ret;
525 u32 nritems;
526 struct btrfs_leaf *leaf;
527 int slot;
528 int advance;
529 unsigned char d_type = DT_UNKNOWN;
Chris Mason7f5c1512007-03-23 15:56:19 -0400530 int over = 0;
Chris Masond6e4a422007-04-06 15:37:36 -0400531 int key_type = BTRFS_DIR_INDEX_KEY;
532
533 /* FIXME, use a real flag for deciding about the key type */
534 if (root->fs_info->tree_root == root)
535 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason22b0ebd2007-03-30 08:47:31 -0400536 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400537 key.objectid = inode->i_ino;
Chris Masone20d96d2007-03-22 12:13:20 -0400538 key.flags = 0;
Chris Masond6e4a422007-04-06 15:37:36 -0400539 btrfs_set_key_type(&key, key_type);
Chris Masone20d96d2007-03-22 12:13:20 -0400540 key.offset = filp->f_pos;
Chris Mason5caf2a02007-04-02 11:20:42 -0400541 path = btrfs_alloc_path();
542 btrfs_init_path(path);
543 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason1b05da22007-04-10 12:13:09 -0400544 if (ret < 0)
Chris Masone20d96d2007-03-22 12:13:20 -0400545 goto err;
Chris Mason7f5c1512007-03-23 15:56:19 -0400546 advance = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400547 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400548 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400549 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400550 slot = path->slots[0];
Chris Masondee26a92007-03-26 16:00:06 -0400551 if (advance || slot >= nritems) {
552 if (slot >= nritems -1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400553 ret = btrfs_next_leaf(root, path);
Chris Masone20d96d2007-03-22 12:13:20 -0400554 if (ret)
555 break;
Chris Mason5caf2a02007-04-02 11:20:42 -0400556 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400557 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400558 slot = path->slots[0];
Chris Masone20d96d2007-03-22 12:13:20 -0400559 } else {
560 slot++;
Chris Mason5caf2a02007-04-02 11:20:42 -0400561 path->slots[0]++;
Chris Masone20d96d2007-03-22 12:13:20 -0400562 }
563 }
564 advance = 1;
565 item = leaf->items + slot;
Chris Masone20d96d2007-03-22 12:13:20 -0400566 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
567 break;
Chris Masond6e4a422007-04-06 15:37:36 -0400568 if (key_type == BTRFS_DIR_INDEX_KEY &&
Chris Mason1b05da22007-04-10 12:13:09 -0400569 btrfs_disk_key_offset(&item->key) > root->highest_inode)
Chris Mason5be6f7f2007-04-05 13:35:25 -0400570 break;
Chris Masond6e4a422007-04-06 15:37:36 -0400571 if (btrfs_disk_key_type(&item->key) != key_type)
Chris Masone20d96d2007-03-22 12:13:20 -0400572 continue;
Chris Mason7f5c1512007-03-23 15:56:19 -0400573 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
574 continue;
Chris Mason7fcde0e2007-04-05 12:13:21 -0400575 filp->f_pos = btrfs_disk_key_offset(&item->key);
Chris Masondee26a92007-03-26 16:00:06 -0400576 advance = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400577 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Chris Masone20d96d2007-03-22 12:13:20 -0400578 over = filldir(dirent, (const char *)(di + 1),
579 btrfs_dir_name_len(di),
580 btrfs_disk_key_offset(&item->key),
Chris Masond6e4a422007-04-06 15:37:36 -0400581 btrfs_disk_key_objectid(&di->location), d_type);
Chris Mason7fcde0e2007-04-05 12:13:21 -0400582 if (over)
583 goto nopos;
Chris Masone20d96d2007-03-22 12:13:20 -0400584 }
Chris Mason7fcde0e2007-04-05 12:13:21 -0400585 filp->f_pos++;
586nopos:
Chris Masone20d96d2007-03-22 12:13:20 -0400587 ret = 0;
588err:
Chris Mason5caf2a02007-04-02 11:20:42 -0400589 btrfs_release_path(root, path);
590 btrfs_free_path(path);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400591 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400592 return ret;
593}
594
595static void btrfs_put_super (struct super_block * sb)
596{
597 struct btrfs_root *root = btrfs_sb(sb);
598 int ret;
599
600 ret = close_ctree(root);
601 if (ret) {
602 printk("close ctree returns %d\n", ret);
603 }
604 sb->s_fs_info = NULL;
605}
Chris Mason2e635a22007-03-21 11:12:56 -0400606
607static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
608{
609 struct inode * inode;
Chris Masone20d96d2007-03-22 12:13:20 -0400610 struct dentry * root_dentry;
611 struct btrfs_super_block *disk_super;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400612 struct btrfs_root *tree_root;
Chris Masond6e4a422007-04-06 15:37:36 -0400613 struct btrfs_inode *bi;
Chris Mason2e635a22007-03-21 11:12:56 -0400614
615 sb->s_maxbytes = MAX_LFS_FILESIZE;
Chris Mason2e635a22007-03-21 11:12:56 -0400616 sb->s_magic = BTRFS_SUPER_MAGIC;
Chris Masone20d96d2007-03-22 12:13:20 -0400617 sb->s_op = &btrfs_super_ops;
Chris Mason2e635a22007-03-21 11:12:56 -0400618 sb->s_time_gran = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400619
Chris Mason0f7d52f2007-04-09 10:42:37 -0400620 tree_root = open_ctree(sb);
Chris Masond98237b2007-03-28 13:57:48 -0400621
Chris Mason0f7d52f2007-04-09 10:42:37 -0400622 if (!tree_root) {
Chris Masone20d96d2007-03-22 12:13:20 -0400623 printk("btrfs: open_ctree failed\n");
624 return -EIO;
625 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400626 sb->s_fs_info = tree_root;
627 disk_super = tree_root->fs_info->disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400628 printk("read in super total blocks %Lu root %Lu\n",
629 btrfs_super_total_blocks(disk_super),
630 btrfs_super_root_dir(disk_super));
631
Chris Masonc5739bb2007-04-10 09:27:04 -0400632 inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
633 tree_root);
Chris Masond6e4a422007-04-06 15:37:36 -0400634 bi = BTRFS_I(inode);
635 bi->location.objectid = inode->i_ino;
636 bi->location.offset = 0;
637 bi->location.flags = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400638 bi->root = tree_root;
Chris Masond6e4a422007-04-06 15:37:36 -0400639 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
640
Chris Mason2e635a22007-03-21 11:12:56 -0400641 if (!inode)
642 return -ENOMEM;
Chris Masone20d96d2007-03-22 12:13:20 -0400643 if (inode->i_state & I_NEW) {
644 btrfs_read_locked_inode(inode);
645 unlock_new_inode(inode);
646 }
Chris Mason2e635a22007-03-21 11:12:56 -0400647
Chris Masone20d96d2007-03-22 12:13:20 -0400648 root_dentry = d_alloc_root(inode);
649 if (!root_dentry) {
Chris Mason2e635a22007-03-21 11:12:56 -0400650 iput(inode);
651 return -ENOMEM;
652 }
Chris Masone20d96d2007-03-22 12:13:20 -0400653 sb->s_root = root_dentry;
654
Chris Mason2e635a22007-03-21 11:12:56 -0400655 return 0;
656}
657
Chris Masond5719762007-03-23 10:01:08 -0400658static void fill_inode_item(struct btrfs_inode_item *item,
659 struct inode *inode)
660{
661 btrfs_set_inode_uid(item, inode->i_uid);
662 btrfs_set_inode_gid(item, inode->i_gid);
663 btrfs_set_inode_size(item, inode->i_size);
664 btrfs_set_inode_mode(item, inode->i_mode);
665 btrfs_set_inode_nlink(item, inode->i_nlink);
666 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
667 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
668 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
669 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
670 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
671 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
672 btrfs_set_inode_nblocks(item, inode->i_blocks);
673 btrfs_set_inode_generation(item, inode->i_generation);
674}
675
Chris Mason4730a4b2007-03-26 12:00:39 -0400676static int btrfs_update_inode(struct btrfs_trans_handle *trans,
677 struct btrfs_root *root,
678 struct inode *inode)
679{
680 struct btrfs_inode_item *inode_item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400681 struct btrfs_path *path;
Chris Mason4730a4b2007-03-26 12:00:39 -0400682 int ret;
683
Chris Mason5caf2a02007-04-02 11:20:42 -0400684 path = btrfs_alloc_path();
685 BUG_ON(!path);
686 btrfs_init_path(path);
Chris Masond6e4a422007-04-06 15:37:36 -0400687 ret = btrfs_lookup_inode(trans, root, path,
688 &BTRFS_I(inode)->location, 1);
Chris Mason4730a4b2007-03-26 12:00:39 -0400689 if (ret) {
690 if (ret > 0)
691 ret = -ENOENT;
692 goto failed;
693 }
694
Chris Mason5caf2a02007-04-02 11:20:42 -0400695 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
696 path->slots[0],
Chris Mason4730a4b2007-03-26 12:00:39 -0400697 struct btrfs_inode_item);
698
699 fill_inode_item(inode_item, inode);
Chris Mason5caf2a02007-04-02 11:20:42 -0400700 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason1b05da22007-04-10 12:13:09 -0400701 ret = 0;
Chris Mason4730a4b2007-03-26 12:00:39 -0400702failed:
Chris Mason5caf2a02007-04-02 11:20:42 -0400703 btrfs_release_path(root, path);
704 btrfs_free_path(path);
Chris Mason1b05da22007-04-10 12:13:09 -0400705 return ret;
Chris Mason4730a4b2007-03-26 12:00:39 -0400706}
707
708static int btrfs_write_inode(struct inode *inode, int wait)
709{
Chris Masond6e4a422007-04-06 15:37:36 -0400710 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4730a4b2007-03-26 12:00:39 -0400711 struct btrfs_trans_handle *trans;
712 int ret;
713
714 mutex_lock(&root->fs_info->fs_mutex);
715 trans = btrfs_start_transaction(root, 1);
716 ret = btrfs_update_inode(trans, root, inode);
717 if (wait)
718 btrfs_commit_transaction(trans, root);
719 else
720 btrfs_end_transaction(trans, root);
721 mutex_unlock(&root->fs_info->fs_mutex);
722 return ret;
723}
724
Chris Masond5719762007-03-23 10:01:08 -0400725static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
726 struct inode *dir, int mode)
727{
728 struct inode *inode;
729 struct btrfs_inode_item inode_item;
Chris Masond6e4a422007-04-06 15:37:36 -0400730 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1b05da22007-04-10 12:13:09 -0400731 struct btrfs_key *location;
Chris Masond5719762007-03-23 10:01:08 -0400732 int ret;
733 u64 objectid;
734
735 inode = new_inode(dir->i_sb);
736 if (!inode)
737 return ERR_PTR(-ENOMEM);
738
Chris Masond6e4a422007-04-06 15:37:36 -0400739 BTRFS_I(inode)->root = BTRFS_I(dir)->root;
Chris Masond5719762007-03-23 10:01:08 -0400740 ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
741 BUG_ON(ret);
742
743 inode->i_uid = current->fsuid;
744 inode->i_gid = current->fsgid;
745 inode->i_mode = mode;
746 inode->i_ino = objectid;
747 inode->i_blocks = 0;
Chris Masonc5739bb2007-04-10 09:27:04 -0400748 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Masond5719762007-03-23 10:01:08 -0400749 fill_inode_item(&inode_item, inode);
Chris Mason1b05da22007-04-10 12:13:09 -0400750 location = &BTRFS_I(inode)->location;
751 location->objectid = objectid;
752 location->flags = 0;
753 location->offset = 0;
754 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
Chris Masond5719762007-03-23 10:01:08 -0400755
756 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
757 BUG_ON(ret);
758
759 insert_inode_hash(inode);
Chris Masond5719762007-03-23 10:01:08 -0400760 return inode;
761}
762
763static int btrfs_add_link(struct btrfs_trans_handle *trans,
764 struct dentry *dentry, struct inode *inode)
765{
766 int ret;
Chris Masond6e4a422007-04-06 15:37:36 -0400767 struct btrfs_key key;
768 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
769 key.objectid = inode->i_ino;
770 key.flags = 0;
771 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
772 key.offset = 0;
773
774 ret = btrfs_insert_dir_item(trans, root,
Chris Masond5719762007-03-23 10:01:08 -0400775 dentry->d_name.name, dentry->d_name.len,
776 dentry->d_parent->d_inode->i_ino,
Chris Masond6e4a422007-04-06 15:37:36 -0400777 &key, 0);
Chris Mason4730a4b2007-03-26 12:00:39 -0400778 if (ret == 0) {
Chris Mason5f26f772007-04-05 10:38:44 -0400779 dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
Chris Masond6e4a422007-04-06 15:37:36 -0400780 ret = btrfs_update_inode(trans, root,
Chris Mason4730a4b2007-03-26 12:00:39 -0400781 dentry->d_parent->d_inode);
782 }
Chris Masond5719762007-03-23 10:01:08 -0400783 return ret;
784}
785
786static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
787 struct dentry *dentry, struct inode *inode)
788{
789 int err = btrfs_add_link(trans, dentry, inode);
790 if (!err) {
791 d_instantiate(dentry, inode);
792 return 0;
793 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400794 if (err > 0)
795 err = -EEXIST;
Chris Masond5719762007-03-23 10:01:08 -0400796 return err;
797}
798
799static int btrfs_create(struct inode *dir, struct dentry *dentry,
800 int mode, struct nameidata *nd)
801{
802 struct btrfs_trans_handle *trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400803 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masond5719762007-03-23 10:01:08 -0400804 struct inode *inode;
805 int err;
Chris Mason134e9732007-03-25 13:44:56 -0400806 int drop_inode = 0;
Chris Masond5719762007-03-23 10:01:08 -0400807
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 inode = btrfs_new_inode(trans, dir, mode);
811 err = PTR_ERR(inode);
812 if (IS_ERR(inode))
Chris Masond561c022007-03-23 19:47:49 -0400813 goto out_unlock;
Chris Masond5719762007-03-23 10:01:08 -0400814 // FIXME mark the inode dirty
815 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason134e9732007-03-25 13:44:56 -0400816 if (err)
817 drop_inode = 1;
Chris Masondee26a92007-03-26 16:00:06 -0400818 else {
819 inode->i_mapping->a_ops = &btrfs_aops;
820 inode->i_fop = &btrfs_file_operations;
821 inode->i_op = &btrfs_file_inode_operations;
822 }
Chris Masond5719762007-03-23 10:01:08 -0400823 dir->i_sb->s_dirt = 1;
Chris Masond561c022007-03-23 19:47:49 -0400824out_unlock:
Chris Mason22b0ebd2007-03-30 08:47:31 -0400825 btrfs_end_transaction(trans, root);
Chris Masond561c022007-03-23 19:47:49 -0400826 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400827
Chris Mason134e9732007-03-25 13:44:56 -0400828 if (drop_inode) {
829 inode_dec_link_count(inode);
830 iput(inode);
831 }
Chris Masond5719762007-03-23 10:01:08 -0400832 return err;
833}
834
Chris Masonf7922032007-03-25 20:17:36 -0400835static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
836 struct inode *inode, struct inode *dir)
837{
Chris Masond6e4a422007-04-06 15:37:36 -0400838 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masonf7922032007-03-25 20:17:36 -0400839 int ret;
840 char buf[2];
Chris Masond6e4a422007-04-06 15:37:36 -0400841 struct btrfs_key key;
842
Chris Masonf7922032007-03-25 20:17:36 -0400843 buf[0] = '.';
844 buf[1] = '.';
845
Chris Masond6e4a422007-04-06 15:37:36 -0400846 key.objectid = inode->i_ino;
847 key.offset = 0;
848 key.flags = 0;
849 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
850
Chris Masonf7922032007-03-25 20:17:36 -0400851 ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino,
Chris Masond6e4a422007-04-06 15:37:36 -0400852 &key, 1);
Chris Masonf7922032007-03-25 20:17:36 -0400853 if (ret)
854 goto error;
Chris Masond6e4a422007-04-06 15:37:36 -0400855 key.objectid = dir->i_ino;
Chris Masonf7922032007-03-25 20:17:36 -0400856 ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino,
Chris Masond6e4a422007-04-06 15:37:36 -0400857 &key, 1);
Chris Mason4730a4b2007-03-26 12:00:39 -0400858 if (ret)
859 goto error;
Chris Mason5f26f772007-04-05 10:38:44 -0400860 inode->i_size = 6;
Chris Mason4730a4b2007-03-26 12:00:39 -0400861 ret = btrfs_update_inode(trans, root, inode);
Chris Masonf7922032007-03-25 20:17:36 -0400862error:
863 return ret;
864}
865
866static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
867{
868 struct inode *inode;
869 struct btrfs_trans_handle *trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400870 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masonf7922032007-03-25 20:17:36 -0400871 int err = 0;
872 int drop_on_err = 0;
873
874 mutex_lock(&root->fs_info->fs_mutex);
875 trans = btrfs_start_transaction(root, 1);
876 if (IS_ERR(trans)) {
877 err = PTR_ERR(trans);
878 goto out_unlock;
879 }
880 inode = btrfs_new_inode(trans, dir, S_IFDIR | mode);
881 if (IS_ERR(inode)) {
882 err = PTR_ERR(inode);
883 goto out_fail;
884 }
885 drop_on_err = 1;
886 inode->i_op = &btrfs_dir_inode_operations;
887 inode->i_fop = &btrfs_dir_file_operations;
888
889 err = btrfs_make_empty_dir(trans, inode, dir);
890 if (err)
891 goto out_fail;
892 err = btrfs_add_link(trans, dentry, inode);
893 if (err)
894 goto out_fail;
895 d_instantiate(dentry, inode);
Chris Masonf7922032007-03-25 20:17:36 -0400896 drop_on_err = 0;
897
898out_fail:
899 btrfs_end_transaction(trans, root);
900out_unlock:
901 mutex_unlock(&root->fs_info->fs_mutex);
902 if (drop_on_err)
903 iput(inode);
904 return err;
905}
906
Chris Masond5719762007-03-23 10:01:08 -0400907static int btrfs_sync_fs(struct super_block *sb, int wait)
908{
909 struct btrfs_trans_handle *trans;
910 struct btrfs_root *root;
911 int ret;
Chris Masond98237b2007-03-28 13:57:48 -0400912 root = btrfs_sb(sb);
Chris Masondf2ce342007-03-23 11:00:45 -0400913
Chris Masond5719762007-03-23 10:01:08 -0400914 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -0400915 if (!wait) {
Chris Mason7cfcc172007-04-02 14:53:59 -0400916 filemap_flush(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400917 return 0;
918 }
Chris Mason7cfcc172007-04-02 14:53:59 -0400919 filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400920 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400921 trans = btrfs_start_transaction(root, 1);
922 ret = btrfs_commit_transaction(trans, root);
923 sb->s_dirt = 0;
924 BUG_ON(ret);
925printk("btrfs sync_fs\n");
Chris Masond561c022007-03-23 19:47:49 -0400926 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400927 return 0;
928}
929
Chris Masone8f05c42007-04-04 14:30:09 -0400930#if 0
Chris Masondf24a2b2007-04-04 09:36:31 -0400931static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
932 struct buffer_head *result, int create)
933{
934 struct btrfs_root *root = btrfs_sb(inode->i_sb);
935 struct btrfs_path *path;
936 struct btrfs_key key;
937 struct btrfs_leaf *leaf;
938 int num_bytes = result->b_size;
939 int item_size;
940 int ret;
941 u64 pos;
942 char *ptr;
943 int copy_size;
944 int err = 0;
945 char *safe_ptr;
946 char *data_ptr;
947
948 path = btrfs_alloc_path();
949 BUG_ON(!path);
950
951 WARN_ON(create);
952 if (create) {
953 return 0;
954 }
955 pos = iblock << inode->i_blkbits;
956 key.objectid = inode->i_ino;
957 key.flags = 0;
958 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
959 ptr = kmap(result->b_page);
960 safe_ptr = ptr;
961 ptr += (pos & (PAGE_CACHE_SIZE -1));
962again:
963 key.offset = pos;
964 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
965 if (ret) {
966 if (ret < 0)
967 err = ret;
968 else
969 err = 0;
970 goto out;
971 }
972 leaf = btrfs_buffer_leaf(path->nodes[0]);
973 item_size = btrfs_item_size(leaf->items + path->slots[0]);
974 copy_size = min(num_bytes, item_size);
975 data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
976 WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
977 memcpy(ptr, data_ptr, copy_size);
978 pos += copy_size;
979 num_bytes -= copy_size;
980 WARN_ON(num_bytes < 0);
981 ptr += copy_size;
982 btrfs_release_path(root, path);
983 if (num_bytes != 0) {
984 if (pos >= i_size_read(inode))
985 memset(ptr, 0, num_bytes);
986 else
987 goto again;
988 }
989 set_buffer_uptodate(result);
990 map_bh(result, inode->i_sb, 0);
991 err = 0;
992out:
993 btrfs_free_path(path);
994 kunmap(result->b_page);
995 return err;
996}
Chris Masone8f05c42007-04-04 14:30:09 -0400997#endif
Chris Masondf24a2b2007-04-04 09:36:31 -0400998
Chris Mason75dfe392007-03-29 11:56:46 -0400999static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
Chris Masondee26a92007-03-26 16:00:06 -04001000 struct buffer_head *result, int create)
1001{
1002 int ret;
1003 int err = 0;
1004 u64 blocknr;
1005 u64 extent_start = 0;
1006 u64 extent_end = 0;
1007 u64 objectid = inode->i_ino;
Chris Mason5caf2a02007-04-02 11:20:42 -04001008 struct btrfs_path *path;
Chris Masond6e4a422007-04-06 15:37:36 -04001009 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondee26a92007-03-26 16:00:06 -04001010 struct btrfs_trans_handle *trans = NULL;
1011 struct btrfs_file_extent_item *item;
1012 struct btrfs_leaf *leaf;
1013 struct btrfs_disk_key *found_key;
1014
Chris Mason5caf2a02007-04-02 11:20:42 -04001015 path = btrfs_alloc_path();
1016 BUG_ON(!path);
1017 btrfs_init_path(path);
Chris Masondee26a92007-03-26 16:00:06 -04001018 if (create)
1019 trans = btrfs_start_transaction(root, 1);
1020
Chris Mason5caf2a02007-04-02 11:20:42 -04001021 ret = btrfs_lookup_file_extent(trans, root, path,
Chris Mason9773a782007-03-27 11:26:26 -04001022 inode->i_ino,
1023 iblock << inode->i_blkbits, 0);
Chris Masondee26a92007-03-26 16:00:06 -04001024 if (ret < 0) {
Chris Masondee26a92007-03-26 16:00:06 -04001025 err = ret;
1026 goto out;
1027 }
1028
1029 if (ret != 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001030 if (path->slots[0] == 0) {
1031 btrfs_release_path(root, path);
Chris Masondee26a92007-03-26 16:00:06 -04001032 goto allocate;
1033 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001034 path->slots[0]--;
Chris Masondee26a92007-03-26 16:00:06 -04001035 }
1036
Chris Mason5caf2a02007-04-02 11:20:42 -04001037 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masondee26a92007-03-26 16:00:06 -04001038 struct btrfs_file_extent_item);
Chris Mason5caf2a02007-04-02 11:20:42 -04001039 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masondee26a92007-03-26 16:00:06 -04001040 blocknr = btrfs_file_extent_disk_blocknr(item);
1041 blocknr += btrfs_file_extent_offset(item);
1042
1043 /* exact match found, use it */
1044 if (ret == 0) {
1045 err = 0;
1046 map_bh(result, inode->i_sb, blocknr);
Chris Masondee26a92007-03-26 16:00:06 -04001047 goto out;
1048 }
1049
1050 /* are we inside the extent that was found? */
Chris Mason5caf2a02007-04-02 11:20:42 -04001051 found_key = &leaf->items[path->slots[0]].key;
Chris Masondee26a92007-03-26 16:00:06 -04001052 if (btrfs_disk_key_objectid(found_key) != objectid ||
1053 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
1054 extent_end = 0;
1055 extent_start = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -04001056 btrfs_release_path(root, path);
Chris Masondee26a92007-03-26 16:00:06 -04001057 goto allocate;
1058 }
1059
Chris Mason5caf2a02007-04-02 11:20:42 -04001060 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
Chris Mason9773a782007-03-27 11:26:26 -04001061 extent_start = extent_start >> inode->i_blkbits;
Chris Masondee26a92007-03-26 16:00:06 -04001062 extent_start += btrfs_file_extent_offset(item);
1063 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
Chris Masondee26a92007-03-26 16:00:06 -04001064 if (iblock >= extent_start && iblock < extent_end) {
1065 err = 0;
1066 map_bh(result, inode->i_sb, blocknr + iblock - extent_start);
1067 goto out;
1068 }
1069allocate:
1070 /* ok, create a new extent */
1071 if (!create) {
1072 err = 0;
1073 goto out;
1074 }
Chris Mason9773a782007-03-27 11:26:26 -04001075 ret = btrfs_alloc_file_extent(trans, root, objectid,
1076 iblock << inode->i_blkbits,
Chris Masondee26a92007-03-26 16:00:06 -04001077 1, extent_end, &blocknr);
1078 if (ret) {
1079 err = ret;
1080 goto out;
1081 }
Chris Masonf4b9aa82007-03-27 11:05:53 -04001082 inode->i_blocks += inode->i_sb->s_blocksize >> 9;
1083 set_buffer_new(result);
Chris Masondee26a92007-03-26 16:00:06 -04001084 map_bh(result, inode->i_sb, blocknr);
1085
1086out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001087 btrfs_release_path(root, path);
1088 btrfs_free_path(path);
Chris Masondee26a92007-03-26 16:00:06 -04001089 if (trans)
1090 btrfs_end_transaction(trans, root);
Chris Mason75dfe392007-03-29 11:56:46 -04001091 return err;
1092}
1093
1094static int btrfs_get_block(struct inode *inode, sector_t iblock,
1095 struct buffer_head *result, int create)
1096{
1097 int err;
Chris Masond6e4a422007-04-06 15:37:36 -04001098 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason75dfe392007-03-29 11:56:46 -04001099 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone8f05c42007-04-04 14:30:09 -04001100 err = btrfs_get_block_lock(inode, iblock, result, create);
1101 // err = btrfs_get_block_inline(inode, iblock, result, create);
Chris Masondee26a92007-03-26 16:00:06 -04001102 mutex_unlock(&root->fs_info->fs_mutex);
1103 return err;
1104}
1105
1106static int btrfs_prepare_write(struct file *file, struct page *page,
1107 unsigned from, unsigned to)
1108{
Chris Mason6407bf62007-03-27 06:33:00 -04001109 return nobh_prepare_write(page, from, to, btrfs_get_block);
Chris Masondee26a92007-03-26 16:00:06 -04001110}
Chris Mason75dfe392007-03-29 11:56:46 -04001111static int btrfs_commit_write(struct file *file, struct page *page,
1112 unsigned from, unsigned to)
1113{
Chris Mason75dfe392007-03-29 11:56:46 -04001114 return nobh_commit_write(file, page, from, to);
1115}
Chris Masondee26a92007-03-26 16:00:06 -04001116
Chris Masond561c022007-03-23 19:47:49 -04001117static void btrfs_write_super(struct super_block *sb)
1118{
1119 btrfs_sync_fs(sb, 1);
1120}
1121
Chris Masondee26a92007-03-26 16:00:06 -04001122static int btrfs_readpage(struct file *file, struct page *page)
1123{
1124 return mpage_readpage(page, btrfs_get_block);
1125}
1126
1127static int btrfs_readpages(struct file *file, struct address_space *mapping,
1128 struct list_head *pages, unsigned nr_pages)
1129{
1130 return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block);
1131}
1132
1133static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1134{
Chris Mason6407bf62007-03-27 06:33:00 -04001135 return nobh_writepage(page, btrfs_get_block, wbc);
Chris Masondee26a92007-03-26 16:00:06 -04001136}
Chris Masond561c022007-03-23 19:47:49 -04001137
Chris Masonf4b9aa82007-03-27 11:05:53 -04001138static void btrfs_truncate(struct inode *inode)
1139{
Chris Masond6e4a422007-04-06 15:37:36 -04001140 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonf4b9aa82007-03-27 11:05:53 -04001141 int ret;
1142 struct btrfs_trans_handle *trans;
1143
1144 if (!S_ISREG(inode->i_mode))
1145 return;
1146 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1147 return;
1148
Chris Masone8f05c42007-04-04 14:30:09 -04001149 nobh_truncate_page(inode->i_mapping, inode->i_size);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001150
1151 /* FIXME, add redo link to tree so we don't leak on crash */
1152 mutex_lock(&root->fs_info->fs_mutex);
1153 trans = btrfs_start_transaction(root, 1);
1154 ret = btrfs_truncate_in_trans(trans, root, inode);
1155 BUG_ON(ret);
1156 ret = btrfs_end_transaction(trans, root);
1157 BUG_ON(ret);
1158 mutex_unlock(&root->fs_info->fs_mutex);
1159 mark_inode_dirty(inode);
1160}
1161
Chris Mason75dfe392007-03-29 11:56:46 -04001162static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
1163 struct page **prepared_pages,
1164 const char __user * buf)
1165{
1166 long page_fault = 0;
1167 int i;
1168 int offset = pos & (PAGE_CACHE_SIZE - 1);
1169
1170 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
1171 size_t count = min_t(size_t,
1172 PAGE_CACHE_SIZE - offset, write_bytes);
1173 struct page *page = prepared_pages[i];
1174 fault_in_pages_readable(buf, count);
1175
1176 /* Copy data from userspace to the current page */
1177 kmap(page);
1178 page_fault = __copy_from_user(page_address(page) + offset,
1179 buf, count);
1180 /* Flush processor's dcache for this page */
1181 flush_dcache_page(page);
1182 kunmap(page);
1183 buf += count;
1184 write_bytes -= count;
1185
1186 if (page_fault)
1187 break;
1188 }
1189 return page_fault ? -EFAULT : 0;
1190}
1191
1192static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1193{
1194 size_t i;
1195 for (i = 0; i < num_pages; i++) {
1196 if (!pages[i])
1197 break;
1198 unlock_page(pages[i]);
1199 mark_page_accessed(pages[i]);
1200 page_cache_release(pages[i]);
1201 }
1202}
1203static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1204 struct btrfs_root *root,
1205 struct file *file,
1206 struct page **pages,
1207 size_t num_pages,
1208 loff_t pos,
1209 size_t write_bytes)
1210{
1211 int i;
1212 int offset;
1213 int err = 0;
1214 int ret;
1215 int this_write;
Chris Masonf254e522007-03-29 15:15:27 -04001216 struct inode *inode = file->f_path.dentry->d_inode;
Chris Mason75dfe392007-03-29 11:56:46 -04001217
1218 for (i = 0; i < num_pages; i++) {
1219 offset = pos & (PAGE_CACHE_SIZE -1);
1220 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
Chris Masonf254e522007-03-29 15:15:27 -04001221 /* FIXME, one block at a time */
1222
1223 mutex_lock(&root->fs_info->fs_mutex);
1224 trans = btrfs_start_transaction(root, 1);
1225 btrfs_csum_file_block(trans, root, inode->i_ino,
1226 pages[i]->index << PAGE_CACHE_SHIFT,
1227 kmap(pages[i]), PAGE_CACHE_SIZE);
1228 kunmap(pages[i]);
1229 SetPageChecked(pages[i]);
1230 ret = btrfs_end_transaction(trans, root);
1231 BUG_ON(ret);
1232 mutex_unlock(&root->fs_info->fs_mutex);
1233
Chris Mason75dfe392007-03-29 11:56:46 -04001234 ret = nobh_commit_write(file, pages[i], offset,
1235 offset + this_write);
1236 pos += this_write;
1237 if (ret) {
1238 err = ret;
1239 goto failed;
1240 }
1241 WARN_ON(this_write > write_bytes);
1242 write_bytes -= this_write;
1243 }
1244failed:
1245 return err;
1246}
1247
1248static int prepare_pages(struct btrfs_trans_handle *trans,
1249 struct btrfs_root *root,
1250 struct file *file,
1251 struct page **pages,
1252 size_t num_pages,
1253 loff_t pos,
1254 size_t write_bytes)
1255{
1256 int i;
1257 unsigned long index = pos >> PAGE_CACHE_SHIFT;
1258 struct inode *inode = file->f_path.dentry->d_inode;
1259 int offset;
1260 int err = 0;
1261 int ret;
1262 int this_write;
1263 loff_t isize = i_size_read(inode);
1264
1265 memset(pages, 0, num_pages * sizeof(struct page *));
1266
1267 for (i = 0; i < num_pages; i++) {
1268 pages[i] = grab_cache_page(inode->i_mapping, index + i);
1269 if (!pages[i]) {
1270 err = -ENOMEM;
1271 goto failed_release;
1272 }
1273 offset = pos & (PAGE_CACHE_SIZE -1);
1274 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1275 ret = nobh_prepare_write(pages[i], offset,
1276 offset + this_write,
Chris Masonf254e522007-03-29 15:15:27 -04001277 btrfs_get_block);
Chris Mason75dfe392007-03-29 11:56:46 -04001278 pos += this_write;
1279 if (ret) {
1280 err = ret;
1281 goto failed_truncate;
1282 }
1283 WARN_ON(this_write > write_bytes);
1284 write_bytes -= this_write;
1285 }
1286 return 0;
1287
1288failed_release:
1289 btrfs_drop_pages(pages, num_pages);
1290 return err;
1291
1292failed_truncate:
1293 btrfs_drop_pages(pages, num_pages);
1294 if (pos > isize)
1295 vmtruncate(inode, isize);
1296 return err;
1297}
1298
1299static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1300 size_t count, loff_t *ppos)
1301{
1302 loff_t pos;
1303 size_t num_written = 0;
1304 int err = 0;
1305 int ret = 0;
Chris Mason75dfe392007-03-29 11:56:46 -04001306 struct inode *inode = file->f_path.dentry->d_inode;
Chris Masond6e4a422007-04-06 15:37:36 -04001307 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason75dfe392007-03-29 11:56:46 -04001308 struct page *pages[1];
1309
1310 if (file->f_flags & O_DIRECT)
1311 return -EINVAL;
1312 pos = *ppos;
1313
1314 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1315 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1316 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1317 if (err)
1318 goto out;
1319 if (count == 0)
1320 goto out;
1321 err = remove_suid(file->f_path.dentry);
1322 if (err)
1323 goto out;
1324 file_update_time(file);
1325 mutex_lock(&inode->i_mutex);
1326 while(count > 0) {
1327 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1328 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1329 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
1330 PAGE_CACHE_SHIFT;
Chris Masonf254e522007-03-29 15:15:27 -04001331 ret = prepare_pages(NULL, root, file, pages, num_pages,
Chris Mason75dfe392007-03-29 11:56:46 -04001332 pos, write_bytes);
1333 BUG_ON(ret);
1334 ret = btrfs_copy_from_user(pos, num_pages,
1335 write_bytes, pages, buf);
1336 BUG_ON(ret);
1337
Chris Masonf254e522007-03-29 15:15:27 -04001338 ret = dirty_and_release_pages(NULL, root, file, pages,
Chris Mason75dfe392007-03-29 11:56:46 -04001339 num_pages, pos, write_bytes);
1340 BUG_ON(ret);
1341 btrfs_drop_pages(pages, num_pages);
1342
Chris Mason75dfe392007-03-29 11:56:46 -04001343 buf += write_bytes;
1344 count -= write_bytes;
1345 pos += write_bytes;
1346 num_written += write_bytes;
1347
1348 balance_dirty_pages_ratelimited(inode->i_mapping);
1349 cond_resched();
1350 }
1351 mutex_unlock(&inode->i_mutex);
1352out:
1353 *ppos = pos;
1354 current->backing_dev_info = NULL;
1355 return num_written ? num_written : err;
1356}
1357
Chris Masone8f05c42007-04-04 14:30:09 -04001358#if 0
Chris Masondf24a2b2007-04-04 09:36:31 -04001359static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
1360 struct page *page, loff_t pos,
1361 size_t offset, size_t write_bytes)
1362{
1363 struct btrfs_path *path;
1364 struct btrfs_trans_handle *trans;
1365 struct btrfs_key key;
1366 struct btrfs_leaf *leaf;
1367 struct btrfs_key found_key;
1368 int ret;
1369 size_t copy_size = 0;
1370 char *dst = NULL;
1371 int err = 0;
1372 size_t num_written = 0;
1373
1374 path = btrfs_alloc_path();
1375 BUG_ON(!path);
1376 mutex_lock(&root->fs_info->fs_mutex);
1377 trans = btrfs_start_transaction(root, 1);
1378 key.objectid = inode->i_ino;
1379 key.flags = 0;
1380 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
1381
1382again:
1383 key.offset = pos;
1384 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1385 if (ret < 0) {
1386 err = ret;
1387 goto out;
1388 }
1389 if (ret == 0) {
1390 leaf = btrfs_buffer_leaf(path->nodes[0]);
1391 btrfs_disk_key_to_cpu(&found_key,
1392 &leaf->items[path->slots[0]].key);
1393 copy_size = btrfs_item_size(leaf->items + path->slots[0]);
1394 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1395 copy_size = min(write_bytes, copy_size);
1396 goto copyit;
1397 } else {
1398 int slot = path->slots[0];
1399 if (slot > 0) {
1400 slot--;
1401 }
1402 // FIXME find max key
1403 leaf = btrfs_buffer_leaf(path->nodes[0]);
1404 btrfs_disk_key_to_cpu(&found_key,
1405 &leaf->items[slot].key);
1406 if (found_key.objectid != inode->i_ino)
1407 goto insert;
1408 if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
1409 goto insert;
1410 copy_size = btrfs_item_size(leaf->items + slot);
1411 if (found_key.offset + copy_size <= pos)
1412 goto insert;
1413 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1414 dst += pos - found_key.offset;
1415 copy_size = copy_size - (pos - found_key.offset);
1416 BUG_ON(copy_size < 0);
1417 copy_size = min(write_bytes, copy_size);
1418 WARN_ON(copy_size == 0);
1419 goto copyit;
1420 }
1421insert:
1422 btrfs_release_path(root, path);
Chris Masond4dbff92007-04-04 14:08:15 -04001423 copy_size = min(write_bytes,
1424 (size_t)BTRFS_LEAF_DATA_SIZE(root) -
1425 sizeof(struct btrfs_item) * 4);
Chris Masondf24a2b2007-04-04 09:36:31 -04001426 ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
1427 BUG_ON(ret);
1428 dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1429 path->slots[0], char);
1430copyit:
1431 WARN_ON(copy_size == 0);
1432 WARN_ON(dst + copy_size >
1433 btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1434 path->slots[0], char) +
1435 btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
1436 path->slots[0]));
1437 btrfs_memcpy(root, path->nodes[0]->b_data, dst,
1438 page_address(page) + offset, copy_size);
1439 mark_buffer_dirty(path->nodes[0]);
1440 btrfs_release_path(root, path);
1441 pos += copy_size;
1442 offset += copy_size;
1443 num_written += copy_size;
1444 write_bytes -= copy_size;
1445 if (write_bytes)
1446 goto again;
1447out:
1448 btrfs_free_path(path);
1449 ret = btrfs_end_transaction(trans, root);
1450 BUG_ON(ret);
1451 mutex_unlock(&root->fs_info->fs_mutex);
1452 return num_written ? num_written : err;
1453}
1454
1455static ssize_t btrfs_file_inline_write(struct file *file,
1456 const char __user *buf,
1457 size_t count, loff_t *ppos)
1458{
1459 loff_t pos;
1460 size_t num_written = 0;
1461 int err = 0;
1462 int ret = 0;
1463 struct inode *inode = file->f_path.dentry->d_inode;
Chris Masond6e4a422007-04-06 15:37:36 -04001464 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondf24a2b2007-04-04 09:36:31 -04001465 unsigned long page_index;
1466
1467 if (file->f_flags & O_DIRECT)
1468 return -EINVAL;
1469 pos = *ppos;
1470
1471 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1472 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1473 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1474 if (err)
1475 goto out;
1476 if (count == 0)
1477 goto out;
1478 err = remove_suid(file->f_path.dentry);
1479 if (err)
1480 goto out;
1481 file_update_time(file);
1482 mutex_lock(&inode->i_mutex);
1483 while(count > 0) {
1484 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1485 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1486 struct page *page;
1487
1488 page_index = pos >> PAGE_CACHE_SHIFT;
1489 page = grab_cache_page(inode->i_mapping, page_index);
1490 if (!PageUptodate(page)) {
1491 ret = mpage_readpage(page, btrfs_get_block);
1492 BUG_ON(ret);
1493 lock_page(page);
1494 }
1495 ret = btrfs_copy_from_user(pos, 1,
1496 write_bytes, &page, buf);
1497 BUG_ON(ret);
1498 write_bytes = inline_one_page(root, inode, page, pos,
1499 offset, write_bytes);
1500 SetPageUptodate(page);
1501 if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
1502 i_size_write(inode, pos + write_bytes);
1503 mark_inode_dirty(inode);
1504 }
1505 page_cache_release(page);
1506 unlock_page(page);
1507 if (write_bytes < 0)
1508 goto out_unlock;
1509 buf += write_bytes;
1510 count -= write_bytes;
1511 pos += write_bytes;
1512 num_written += write_bytes;
1513
1514 balance_dirty_pages_ratelimited(inode->i_mapping);
1515 cond_resched();
1516 }
1517out_unlock:
1518 mutex_unlock(&inode->i_mutex);
1519out:
1520 *ppos = pos;
1521 current->backing_dev_info = NULL;
1522 return num_written ? num_written : err;
1523}
Chris Masone8f05c42007-04-04 14:30:09 -04001524#endif
Chris Masondf24a2b2007-04-04 09:36:31 -04001525
Chris Masonf254e522007-03-29 15:15:27 -04001526static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
1527 unsigned long offset, unsigned long size)
1528{
1529 char *kaddr;
1530 unsigned long left, count = desc->count;
Chris Masond6e4a422007-04-06 15:37:36 -04001531 struct inode *inode = page->mapping->host;
Chris Masonf254e522007-03-29 15:15:27 -04001532
1533 if (size > count)
1534 size = count;
1535
1536 if (!PageChecked(page)) {
1537 /* FIXME, do it per block */
Chris Masond6e4a422007-04-06 15:37:36 -04001538 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonf254e522007-03-29 15:15:27 -04001539 int ret = btrfs_csum_verify_file_block(root,
1540 page->mapping->host->i_ino,
1541 page->index << PAGE_CACHE_SHIFT,
1542 kmap(page), PAGE_CACHE_SIZE);
1543 if (ret) {
1544 printk("failed to verify ino %lu page %lu\n",
1545 page->mapping->host->i_ino,
1546 page->index);
1547 memset(page_address(page), 0, PAGE_CACHE_SIZE);
1548 }
1549 SetPageChecked(page);
1550 kunmap(page);
1551 }
1552 /*
1553 * Faults on the destination of a read are common, so do it before
1554 * taking the kmap.
1555 */
1556 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1557 kaddr = kmap_atomic(page, KM_USER0);
1558 left = __copy_to_user_inatomic(desc->arg.buf,
1559 kaddr + offset, size);
1560 kunmap_atomic(kaddr, KM_USER0);
1561 if (left == 0)
1562 goto success;
1563 }
1564
1565 /* Do it the slow way */
1566 kaddr = kmap(page);
1567 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1568 kunmap(page);
1569
1570 if (left) {
1571 size -= left;
1572 desc->error = -EFAULT;
1573 }
1574success:
1575 desc->count = count - size;
1576 desc->written += size;
1577 desc->arg.buf += size;
1578 return size;
1579}
1580
1581/**
1582 * btrfs_file_aio_read - filesystem read routine
1583 * @iocb: kernel I/O control block
1584 * @iov: io vector request
1585 * @nr_segs: number of segments in the iovec
1586 * @pos: current file position
1587 */
1588static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1589 unsigned long nr_segs, loff_t pos)
1590{
1591 struct file *filp = iocb->ki_filp;
1592 ssize_t retval;
1593 unsigned long seg;
1594 size_t count;
1595 loff_t *ppos = &iocb->ki_pos;
1596
1597 count = 0;
1598 for (seg = 0; seg < nr_segs; seg++) {
1599 const struct iovec *iv = &iov[seg];
1600
1601 /*
1602 * If any segment has a negative length, or the cumulative
1603 * length ever wraps negative then return -EINVAL.
1604 */
1605 count += iv->iov_len;
1606 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
1607 return -EINVAL;
1608 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
1609 continue;
1610 if (seg == 0)
1611 return -EFAULT;
1612 nr_segs = seg;
1613 count -= iv->iov_len; /* This segment is no good */
1614 break;
1615 }
1616 retval = 0;
1617 if (count) {
1618 for (seg = 0; seg < nr_segs; seg++) {
1619 read_descriptor_t desc;
1620
1621 desc.written = 0;
1622 desc.arg.buf = iov[seg].iov_base;
1623 desc.count = iov[seg].iov_len;
1624 if (desc.count == 0)
1625 continue;
1626 desc.error = 0;
1627 do_generic_file_read(filp, ppos, &desc,
1628 btrfs_read_actor);
1629 retval += desc.written;
1630 if (desc.error) {
1631 retval = retval ?: desc.error;
1632 break;
1633 }
1634 }
1635 }
1636 return retval;
1637}
1638
Chris Masonc5739bb2007-04-10 09:27:04 -04001639static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
1640{
1641 struct btrfs_trans_handle *trans;
1642 struct btrfs_key key;
1643 struct btrfs_root_item new_root_item;
1644 int ret;
1645 u64 objectid;
1646
1647 mutex_lock(&root->fs_info->fs_mutex);
1648 trans = btrfs_start_transaction(root, 1);
1649 BUG_ON(!trans);
1650
1651 ret = btrfs_update_inode(trans, root, root->inode);
1652 BUG_ON(ret);
1653
Chris Mason1b05da22007-04-10 12:13:09 -04001654 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
1655 0, &objectid);
Chris Masonc5739bb2007-04-10 09:27:04 -04001656 BUG_ON(ret);
1657
1658 memset(&new_root_item, 0, sizeof(new_root_item));
1659 memcpy(&new_root_item, &root->root_item,
1660 sizeof(new_root_item));
1661
1662 key.objectid = objectid;
Chris Masonc5739bb2007-04-10 09:27:04 -04001663 key.offset = 1;
1664 key.flags = 0;
1665 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
1666 btrfs_set_root_blocknr(&new_root_item, root->node->b_blocknr);
1667
1668 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
1669 &new_root_item);
1670 BUG_ON(ret);
1671
1672printk("adding snapshot name %.*s root %Lu %Lu %u\n", namelen, name, key.objectid, key.offset, key.flags);
1673
1674 /*
1675 * insert the directory item
1676 */
1677 key.offset = (u64)-1;
1678 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
1679 name, namelen,
1680 root->fs_info->sb->s_root->d_inode->i_ino,
1681 &key, 0);
1682
1683 BUG_ON(ret);
1684
1685 ret = btrfs_inc_root_ref(trans, root);
1686 BUG_ON(ret);
1687
1688 ret = btrfs_commit_transaction(trans, root);
1689 BUG_ON(ret);
1690 mutex_unlock(&root->fs_info->fs_mutex);
1691 return 0;
1692}
1693
1694static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
1695 cmd, unsigned long arg)
1696{
1697 struct btrfs_root *root = BTRFS_I(inode)->root;
1698 struct btrfs_ioctl_vol_args vol_args;
1699 int ret;
1700 int namelen;
1701
1702 if (!root->ref_cows)
1703 return -EINVAL;
1704 switch (cmd) {
1705 case BTRFS_IOC_SNAP_CREATE:
1706 if (copy_from_user(&vol_args,
1707 (struct btrfs_ioctl_vol_args __user *)arg,
1708 sizeof(vol_args)))
1709 return -EFAULT;
1710 namelen = strlen(vol_args.name);
1711 if (namelen > BTRFS_VOL_NAME_MAX)
1712 return -EINVAL;
1713 ret = create_snapshot(root, vol_args.name, namelen);
1714 WARN_ON(ret);
1715 break;
1716 default:
1717 return -ENOTTY;
1718 }
1719 return 0;
1720}
1721
Chris Mason2c90e5d2007-04-02 10:50:19 -04001722static struct kmem_cache *btrfs_inode_cachep;
1723struct kmem_cache *btrfs_trans_handle_cachep;
1724struct kmem_cache *btrfs_transaction_cachep;
1725struct kmem_cache *btrfs_bit_radix_cachep;
1726struct kmem_cache *btrfs_path_cachep;
1727
1728/*
1729 * Called inside transaction, so use GFP_NOFS
1730 */
1731static struct inode *btrfs_alloc_inode(struct super_block *sb)
1732{
1733 struct btrfs_inode *ei;
1734
1735 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
1736 if (!ei)
1737 return NULL;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001738 return &ei->vfs_inode;
1739}
1740
1741static void btrfs_destroy_inode(struct inode *inode)
1742{
Chris Mason2c90e5d2007-04-02 10:50:19 -04001743 WARN_ON(!list_empty(&inode->i_dentry));
Chris Mason2c90e5d2007-04-02 10:50:19 -04001744 WARN_ON(inode->i_data.nrpages);
1745
Chris Mason2c90e5d2007-04-02 10:50:19 -04001746 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
1747}
1748
1749static void init_once(void * foo, struct kmem_cache * cachep,
1750 unsigned long flags)
1751{
1752 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
1753
1754 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1755 SLAB_CTOR_CONSTRUCTOR) {
1756 inode_init_once(&ei->vfs_inode);
1757 }
1758}
1759
1760static int init_inodecache(void)
1761{
1762 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
1763 sizeof(struct btrfs_inode),
1764 0, (SLAB_RECLAIM_ACCOUNT|
1765 SLAB_MEM_SPREAD),
1766 init_once, NULL);
1767 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
1768 sizeof(struct btrfs_trans_handle),
1769 0, (SLAB_RECLAIM_ACCOUNT|
1770 SLAB_MEM_SPREAD),
1771 NULL, NULL);
1772 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
1773 sizeof(struct btrfs_transaction),
1774 0, (SLAB_RECLAIM_ACCOUNT|
1775 SLAB_MEM_SPREAD),
1776 NULL, NULL);
1777 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
1778 sizeof(struct btrfs_transaction),
1779 0, (SLAB_RECLAIM_ACCOUNT|
1780 SLAB_MEM_SPREAD),
1781 NULL, NULL);
1782 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
1783 256,
1784 0, (SLAB_RECLAIM_ACCOUNT|
1785 SLAB_MEM_SPREAD |
1786 SLAB_DESTROY_BY_RCU),
1787 NULL, NULL);
1788 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
1789 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
1790 return -ENOMEM;
1791 return 0;
1792}
1793
1794static void destroy_inodecache(void)
1795{
1796 kmem_cache_destroy(btrfs_inode_cachep);
1797 kmem_cache_destroy(btrfs_trans_handle_cachep);
1798 kmem_cache_destroy(btrfs_transaction_cachep);
1799 kmem_cache_destroy(btrfs_bit_radix_cachep);
1800 kmem_cache_destroy(btrfs_path_cachep);
1801}
1802
Chris Mason2e635a22007-03-21 11:12:56 -04001803static int btrfs_get_sb(struct file_system_type *fs_type,
1804 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1805{
1806 return get_sb_bdev(fs_type, flags, dev_name, data,
1807 btrfs_fill_super, mnt);
1808}
1809
1810static struct file_system_type btrfs_fs_type = {
1811 .owner = THIS_MODULE,
1812 .name = "btrfs",
1813 .get_sb = btrfs_get_sb,
1814 .kill_sb = kill_block_super,
1815 .fs_flags = FS_REQUIRES_DEV,
1816};
1817
Chris Masone20d96d2007-03-22 12:13:20 -04001818static struct super_operations btrfs_super_ops = {
1819 .statfs = simple_statfs,
Chris Mason134e9732007-03-25 13:44:56 -04001820 .delete_inode = btrfs_delete_inode,
Chris Masone20d96d2007-03-22 12:13:20 -04001821 .put_super = btrfs_put_super,
1822 .read_inode = btrfs_read_locked_inode,
Chris Masond5719762007-03-23 10:01:08 -04001823 .write_super = btrfs_write_super,
1824 .sync_fs = btrfs_sync_fs,
Chris Mason4730a4b2007-03-26 12:00:39 -04001825 .write_inode = btrfs_write_inode,
Chris Mason2c90e5d2007-04-02 10:50:19 -04001826 .alloc_inode = btrfs_alloc_inode,
1827 .destroy_inode = btrfs_destroy_inode,
Chris Masone20d96d2007-03-22 12:13:20 -04001828};
1829
1830static struct inode_operations btrfs_dir_inode_operations = {
1831 .lookup = btrfs_lookup,
Chris Masond5719762007-03-23 10:01:08 -04001832 .create = btrfs_create,
Chris Mason134e9732007-03-25 13:44:56 -04001833 .unlink = btrfs_unlink,
Chris Masonf7922032007-03-25 20:17:36 -04001834 .mkdir = btrfs_mkdir,
Chris Mason5f443fd2007-03-27 13:42:32 -04001835 .rmdir = btrfs_rmdir,
Chris Masone20d96d2007-03-22 12:13:20 -04001836};
1837
Chris Masond6e4a422007-04-06 15:37:36 -04001838static struct inode_operations btrfs_dir_ro_inode_operations = {
1839 .lookup = btrfs_lookup,
1840};
1841
Chris Masone20d96d2007-03-22 12:13:20 -04001842static struct file_operations btrfs_dir_file_operations = {
1843 .llseek = generic_file_llseek,
1844 .read = generic_read_dir,
1845 .readdir = btrfs_readdir,
Chris Masonc5739bb2007-04-10 09:27:04 -04001846 .ioctl = btrfs_ioctl,
Chris Masone20d96d2007-03-22 12:13:20 -04001847};
1848
Chris Masondee26a92007-03-26 16:00:06 -04001849static struct address_space_operations btrfs_aops = {
1850 .readpage = btrfs_readpage,
Chris Masone8f05c42007-04-04 14:30:09 -04001851 .readpages = btrfs_readpages,
Chris Masondee26a92007-03-26 16:00:06 -04001852 .writepage = btrfs_writepage,
1853 .sync_page = block_sync_page,
1854 .prepare_write = btrfs_prepare_write,
Chris Mason75dfe392007-03-29 11:56:46 -04001855 .commit_write = btrfs_commit_write,
Chris Masondee26a92007-03-26 16:00:06 -04001856};
1857
1858static struct inode_operations btrfs_file_inode_operations = {
Chris Masonf4b9aa82007-03-27 11:05:53 -04001859 .truncate = btrfs_truncate,
Chris Masondee26a92007-03-26 16:00:06 -04001860};
1861
1862static struct file_operations btrfs_file_operations = {
1863 .llseek = generic_file_llseek,
1864 .read = do_sync_read,
Chris Masone8f05c42007-04-04 14:30:09 -04001865 .aio_read = btrfs_file_aio_read,
1866 .write = btrfs_file_write,
Chris Masondee26a92007-03-26 16:00:06 -04001867 .mmap = generic_file_mmap,
1868 .open = generic_file_open,
Chris Masonc5739bb2007-04-10 09:27:04 -04001869 .ioctl = btrfs_ioctl,
Chris Masondee26a92007-03-26 16:00:06 -04001870};
Chris Masone20d96d2007-03-22 12:13:20 -04001871
Chris Mason2e635a22007-03-21 11:12:56 -04001872static int __init init_btrfs_fs(void)
1873{
Chris Mason2c90e5d2007-04-02 10:50:19 -04001874 int err;
Chris Mason2e635a22007-03-21 11:12:56 -04001875 printk("btrfs loaded!\n");
Chris Mason2c90e5d2007-04-02 10:50:19 -04001876 err = init_inodecache();
1877 if (err)
1878 return err;
Chris Masond6e4a422007-04-06 15:37:36 -04001879 kset_set_kset_s(&btrfs_subsys, fs_subsys);
1880 err = subsystem_register(&btrfs_subsys);
1881 if (err)
1882 goto out;
Chris Mason2e635a22007-03-21 11:12:56 -04001883 return register_filesystem(&btrfs_fs_type);
Chris Masond6e4a422007-04-06 15:37:36 -04001884out:
1885 destroy_inodecache();
1886 return err;
Chris Mason2e635a22007-03-21 11:12:56 -04001887}
1888
1889static void __exit exit_btrfs_fs(void)
1890{
Chris Mason2c90e5d2007-04-02 10:50:19 -04001891 destroy_inodecache();
Chris Mason2e635a22007-03-21 11:12:56 -04001892 unregister_filesystem(&btrfs_fs_type);
Chris Masond6e4a422007-04-06 15:37:36 -04001893 subsystem_unregister(&btrfs_subsys);
Chris Mason2e635a22007-03-21 11:12:56 -04001894 printk("btrfs unloaded\n");
1895}
1896
1897module_init(init_btrfs_fs)
1898module_exit(exit_btrfs_fs)
1899
1900MODULE_LICENSE("GPL");