blob: 6a56416147e6f1ae09f4c0ca48f05bdd60f096bd [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) {
Chris Masonc5739bb2007-04-10 09:27:04 -0400498printk("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 -0400499 igrab(inode);
500 sub_root->inode = inode;
501 }
Chris Masond6e4a422007-04-06 15:37:36 -0400502 BTRFS_I(inode)->root = sub_root;
503 memcpy(&BTRFS_I(inode)->location, &location,
504 sizeof(location));
505 btrfs_read_locked_inode(inode);
506 unlock_new_inode(inode);
507 }
Chris Masone20d96d2007-03-22 12:13:20 -0400508 }
509 return d_splice_alias(inode, dentry);
510}
511
512static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
513{
514 struct inode *inode = filp->f_path.dentry->d_inode;
Chris Masond6e4a422007-04-06 15:37:36 -0400515 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone20d96d2007-03-22 12:13:20 -0400516 struct btrfs_item *item;
517 struct btrfs_dir_item *di;
518 struct btrfs_key key;
Chris Mason5caf2a02007-04-02 11:20:42 -0400519 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -0400520 int ret;
521 u32 nritems;
522 struct btrfs_leaf *leaf;
523 int slot;
524 int advance;
525 unsigned char d_type = DT_UNKNOWN;
Chris Mason7f5c1512007-03-23 15:56:19 -0400526 int over = 0;
Chris Masond6e4a422007-04-06 15:37:36 -0400527 int key_type = BTRFS_DIR_INDEX_KEY;
528
529 /* FIXME, use a real flag for deciding about the key type */
530 if (root->fs_info->tree_root == root)
531 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason22b0ebd2007-03-30 08:47:31 -0400532 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400533 key.objectid = inode->i_ino;
Chris Masone20d96d2007-03-22 12:13:20 -0400534 key.flags = 0;
Chris Masond6e4a422007-04-06 15:37:36 -0400535 btrfs_set_key_type(&key, key_type);
Chris Masone20d96d2007-03-22 12:13:20 -0400536 key.offset = filp->f_pos;
Chris Mason5caf2a02007-04-02 11:20:42 -0400537 path = btrfs_alloc_path();
538 btrfs_init_path(path);
539 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason1b05da22007-04-10 12:13:09 -0400540 if (ret < 0)
Chris Masone20d96d2007-03-22 12:13:20 -0400541 goto err;
Chris Mason7f5c1512007-03-23 15:56:19 -0400542 advance = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400543 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400544 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400545 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400546 slot = path->slots[0];
Chris Masondee26a92007-03-26 16:00:06 -0400547 if (advance || slot >= nritems) {
548 if (slot >= nritems -1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400549 ret = btrfs_next_leaf(root, path);
Chris Masone20d96d2007-03-22 12:13:20 -0400550 if (ret)
551 break;
Chris Mason5caf2a02007-04-02 11:20:42 -0400552 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400553 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400554 slot = path->slots[0];
Chris Masone20d96d2007-03-22 12:13:20 -0400555 } else {
556 slot++;
Chris Mason5caf2a02007-04-02 11:20:42 -0400557 path->slots[0]++;
Chris Masone20d96d2007-03-22 12:13:20 -0400558 }
559 }
560 advance = 1;
561 item = leaf->items + slot;
Chris Masone20d96d2007-03-22 12:13:20 -0400562 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
563 break;
Chris Masond6e4a422007-04-06 15:37:36 -0400564 if (key_type == BTRFS_DIR_INDEX_KEY &&
Chris Mason1b05da22007-04-10 12:13:09 -0400565 btrfs_disk_key_offset(&item->key) > root->highest_inode)
Chris Mason5be6f7f2007-04-05 13:35:25 -0400566 break;
Chris Masond6e4a422007-04-06 15:37:36 -0400567 if (btrfs_disk_key_type(&item->key) != key_type)
Chris Masone20d96d2007-03-22 12:13:20 -0400568 continue;
Chris Mason7f5c1512007-03-23 15:56:19 -0400569 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
570 continue;
Chris Mason7fcde0e2007-04-05 12:13:21 -0400571 filp->f_pos = btrfs_disk_key_offset(&item->key);
Chris Masondee26a92007-03-26 16:00:06 -0400572 advance = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400573 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Chris Masone20d96d2007-03-22 12:13:20 -0400574 over = filldir(dirent, (const char *)(di + 1),
575 btrfs_dir_name_len(di),
576 btrfs_disk_key_offset(&item->key),
Chris Masond6e4a422007-04-06 15:37:36 -0400577 btrfs_disk_key_objectid(&di->location), d_type);
Chris Mason7fcde0e2007-04-05 12:13:21 -0400578 if (over)
579 goto nopos;
Chris Masone20d96d2007-03-22 12:13:20 -0400580 }
Chris Mason7fcde0e2007-04-05 12:13:21 -0400581 filp->f_pos++;
582nopos:
Chris Masone20d96d2007-03-22 12:13:20 -0400583 ret = 0;
584err:
Chris Mason5caf2a02007-04-02 11:20:42 -0400585 btrfs_release_path(root, path);
586 btrfs_free_path(path);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400587 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400588 return ret;
589}
590
591static void btrfs_put_super (struct super_block * sb)
592{
593 struct btrfs_root *root = btrfs_sb(sb);
594 int ret;
595
596 ret = close_ctree(root);
597 if (ret) {
598 printk("close ctree returns %d\n", ret);
599 }
600 sb->s_fs_info = NULL;
601}
Chris Mason2e635a22007-03-21 11:12:56 -0400602
603static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
604{
605 struct inode * inode;
Chris Masone20d96d2007-03-22 12:13:20 -0400606 struct dentry * root_dentry;
607 struct btrfs_super_block *disk_super;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400608 struct btrfs_root *tree_root;
Chris Masond6e4a422007-04-06 15:37:36 -0400609 struct btrfs_inode *bi;
Chris Mason2e635a22007-03-21 11:12:56 -0400610
611 sb->s_maxbytes = MAX_LFS_FILESIZE;
Chris Mason2e635a22007-03-21 11:12:56 -0400612 sb->s_magic = BTRFS_SUPER_MAGIC;
Chris Masone20d96d2007-03-22 12:13:20 -0400613 sb->s_op = &btrfs_super_ops;
Chris Mason2e635a22007-03-21 11:12:56 -0400614 sb->s_time_gran = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400615
Chris Mason0f7d52f2007-04-09 10:42:37 -0400616 tree_root = open_ctree(sb);
Chris Masond98237b2007-03-28 13:57:48 -0400617
Chris Mason0f7d52f2007-04-09 10:42:37 -0400618 if (!tree_root) {
Chris Masone20d96d2007-03-22 12:13:20 -0400619 printk("btrfs: open_ctree failed\n");
620 return -EIO;
621 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400622 sb->s_fs_info = tree_root;
623 disk_super = tree_root->fs_info->disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400624 printk("read in super total blocks %Lu root %Lu\n",
625 btrfs_super_total_blocks(disk_super),
626 btrfs_super_root_dir(disk_super));
627
Chris Masonc5739bb2007-04-10 09:27:04 -0400628 inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
629 tree_root);
Chris Masond6e4a422007-04-06 15:37:36 -0400630 bi = BTRFS_I(inode);
631 bi->location.objectid = inode->i_ino;
632 bi->location.offset = 0;
633 bi->location.flags = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400634 bi->root = tree_root;
Chris Masond6e4a422007-04-06 15:37:36 -0400635 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
636
Chris Mason2e635a22007-03-21 11:12:56 -0400637 if (!inode)
638 return -ENOMEM;
Chris Masone20d96d2007-03-22 12:13:20 -0400639 if (inode->i_state & I_NEW) {
640 btrfs_read_locked_inode(inode);
641 unlock_new_inode(inode);
642 }
Chris Mason2e635a22007-03-21 11:12:56 -0400643
Chris Masone20d96d2007-03-22 12:13:20 -0400644 root_dentry = d_alloc_root(inode);
645 if (!root_dentry) {
Chris Mason2e635a22007-03-21 11:12:56 -0400646 iput(inode);
647 return -ENOMEM;
648 }
Chris Masone20d96d2007-03-22 12:13:20 -0400649 sb->s_root = root_dentry;
650
Chris Mason2e635a22007-03-21 11:12:56 -0400651 return 0;
652}
653
Chris Masond5719762007-03-23 10:01:08 -0400654static void fill_inode_item(struct btrfs_inode_item *item,
655 struct inode *inode)
656{
657 btrfs_set_inode_uid(item, inode->i_uid);
658 btrfs_set_inode_gid(item, inode->i_gid);
659 btrfs_set_inode_size(item, inode->i_size);
660 btrfs_set_inode_mode(item, inode->i_mode);
661 btrfs_set_inode_nlink(item, inode->i_nlink);
662 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
663 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
664 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
665 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
666 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
667 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
668 btrfs_set_inode_nblocks(item, inode->i_blocks);
669 btrfs_set_inode_generation(item, inode->i_generation);
670}
671
Chris Mason4730a4b2007-03-26 12:00:39 -0400672static int btrfs_update_inode(struct btrfs_trans_handle *trans,
673 struct btrfs_root *root,
674 struct inode *inode)
675{
676 struct btrfs_inode_item *inode_item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400677 struct btrfs_path *path;
Chris Mason4730a4b2007-03-26 12:00:39 -0400678 int ret;
679
Chris Mason5caf2a02007-04-02 11:20:42 -0400680 path = btrfs_alloc_path();
681 BUG_ON(!path);
682 btrfs_init_path(path);
Chris Masond6e4a422007-04-06 15:37:36 -0400683 ret = btrfs_lookup_inode(trans, root, path,
684 &BTRFS_I(inode)->location, 1);
Chris Mason4730a4b2007-03-26 12:00:39 -0400685 if (ret) {
686 if (ret > 0)
687 ret = -ENOENT;
688 goto failed;
689 }
690
Chris Mason5caf2a02007-04-02 11:20:42 -0400691 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
692 path->slots[0],
Chris Mason4730a4b2007-03-26 12:00:39 -0400693 struct btrfs_inode_item);
694
695 fill_inode_item(inode_item, inode);
Chris Mason5caf2a02007-04-02 11:20:42 -0400696 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason1b05da22007-04-10 12:13:09 -0400697 ret = 0;
Chris Mason4730a4b2007-03-26 12:00:39 -0400698failed:
Chris Mason5caf2a02007-04-02 11:20:42 -0400699 btrfs_release_path(root, path);
700 btrfs_free_path(path);
Chris Mason1b05da22007-04-10 12:13:09 -0400701 return ret;
Chris Mason4730a4b2007-03-26 12:00:39 -0400702}
703
704static int btrfs_write_inode(struct inode *inode, int wait)
705{
Chris Masond6e4a422007-04-06 15:37:36 -0400706 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4730a4b2007-03-26 12:00:39 -0400707 struct btrfs_trans_handle *trans;
708 int ret;
709
710 mutex_lock(&root->fs_info->fs_mutex);
711 trans = btrfs_start_transaction(root, 1);
712 ret = btrfs_update_inode(trans, root, inode);
713 if (wait)
714 btrfs_commit_transaction(trans, root);
715 else
716 btrfs_end_transaction(trans, root);
717 mutex_unlock(&root->fs_info->fs_mutex);
718 return ret;
719}
720
Chris Masond5719762007-03-23 10:01:08 -0400721static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
Chris Mason2619ba12007-04-10 16:58:11 -0400722 struct btrfs_root *root,
723 u64 objectid, int mode)
Chris Masond5719762007-03-23 10:01:08 -0400724{
725 struct inode *inode;
726 struct btrfs_inode_item inode_item;
Chris Mason1b05da22007-04-10 12:13:09 -0400727 struct btrfs_key *location;
Chris Masond5719762007-03-23 10:01:08 -0400728 int ret;
Chris Masond5719762007-03-23 10:01:08 -0400729
Chris Mason2619ba12007-04-10 16:58:11 -0400730 inode = new_inode(root->fs_info->sb);
Chris Masond5719762007-03-23 10:01:08 -0400731 if (!inode)
732 return ERR_PTR(-ENOMEM);
733
Chris Mason2619ba12007-04-10 16:58:11 -0400734 BTRFS_I(inode)->root = root;
Chris Masond5719762007-03-23 10:01:08 -0400735
736 inode->i_uid = current->fsuid;
737 inode->i_gid = current->fsgid;
738 inode->i_mode = mode;
739 inode->i_ino = objectid;
740 inode->i_blocks = 0;
Chris Masonc5739bb2007-04-10 09:27:04 -0400741 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Masond5719762007-03-23 10:01:08 -0400742 fill_inode_item(&inode_item, inode);
Chris Mason1b05da22007-04-10 12:13:09 -0400743 location = &BTRFS_I(inode)->location;
744 location->objectid = objectid;
745 location->flags = 0;
746 location->offset = 0;
747 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
Chris Masond5719762007-03-23 10:01:08 -0400748
749 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
750 BUG_ON(ret);
751
752 insert_inode_hash(inode);
Chris Masond5719762007-03-23 10:01:08 -0400753 return inode;
754}
755
756static int btrfs_add_link(struct btrfs_trans_handle *trans,
757 struct dentry *dentry, struct inode *inode)
758{
759 int ret;
Chris Masond6e4a422007-04-06 15:37:36 -0400760 struct btrfs_key key;
761 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
762 key.objectid = inode->i_ino;
763 key.flags = 0;
764 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
765 key.offset = 0;
766
767 ret = btrfs_insert_dir_item(trans, root,
Chris Masond5719762007-03-23 10:01:08 -0400768 dentry->d_name.name, dentry->d_name.len,
769 dentry->d_parent->d_inode->i_ino,
Chris Masond6e4a422007-04-06 15:37:36 -0400770 &key, 0);
Chris Mason4730a4b2007-03-26 12:00:39 -0400771 if (ret == 0) {
Chris Mason5f26f772007-04-05 10:38:44 -0400772 dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
Chris Masond6e4a422007-04-06 15:37:36 -0400773 ret = btrfs_update_inode(trans, root,
Chris Mason4730a4b2007-03-26 12:00:39 -0400774 dentry->d_parent->d_inode);
775 }
Chris Masond5719762007-03-23 10:01:08 -0400776 return ret;
777}
778
779static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
780 struct dentry *dentry, struct inode *inode)
781{
782 int err = btrfs_add_link(trans, dentry, inode);
783 if (!err) {
784 d_instantiate(dentry, inode);
785 return 0;
786 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400787 if (err > 0)
788 err = -EEXIST;
Chris Masond5719762007-03-23 10:01:08 -0400789 return err;
790}
791
792static int btrfs_create(struct inode *dir, struct dentry *dentry,
793 int mode, struct nameidata *nd)
794{
795 struct btrfs_trans_handle *trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400796 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masond5719762007-03-23 10:01:08 -0400797 struct inode *inode;
798 int err;
Chris Mason134e9732007-03-25 13:44:56 -0400799 int drop_inode = 0;
Chris Mason2619ba12007-04-10 16:58:11 -0400800 u64 objectid;
Chris Masond5719762007-03-23 10:01:08 -0400801
Chris Masond561c022007-03-23 19:47:49 -0400802 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400803 trans = btrfs_start_transaction(root, 1);
Chris Mason2619ba12007-04-10 16:58:11 -0400804
805 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
806 if (err) {
807 err = -ENOSPC;
808 goto out_unlock;
809 }
810
811 inode = btrfs_new_inode(trans, root, objectid, mode);
Chris Masond5719762007-03-23 10:01:08 -0400812 err = PTR_ERR(inode);
813 if (IS_ERR(inode))
Chris Masond561c022007-03-23 19:47:49 -0400814 goto out_unlock;
Chris Masond5719762007-03-23 10:01:08 -0400815 // FIXME mark the inode dirty
816 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason134e9732007-03-25 13:44:56 -0400817 if (err)
818 drop_inode = 1;
Chris Masondee26a92007-03-26 16:00:06 -0400819 else {
820 inode->i_mapping->a_ops = &btrfs_aops;
821 inode->i_fop = &btrfs_file_operations;
822 inode->i_op = &btrfs_file_inode_operations;
823 }
Chris Masond5719762007-03-23 10:01:08 -0400824 dir->i_sb->s_dirt = 1;
Chris Masond561c022007-03-23 19:47:49 -0400825out_unlock:
Chris Mason22b0ebd2007-03-30 08:47:31 -0400826 btrfs_end_transaction(trans, root);
Chris Masond561c022007-03-23 19:47:49 -0400827 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400828
Chris Mason134e9732007-03-25 13:44:56 -0400829 if (drop_inode) {
830 inode_dec_link_count(inode);
831 iput(inode);
832 }
Chris Masond5719762007-03-23 10:01:08 -0400833 return err;
834}
835
Chris Masonf7922032007-03-25 20:17:36 -0400836static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
Chris Mason2619ba12007-04-10 16:58:11 -0400837 struct btrfs_root *root,
838 u64 objectid, u64 dirid)
Chris Masonf7922032007-03-25 20:17:36 -0400839{
Chris Masonf7922032007-03-25 20:17:36 -0400840 int ret;
841 char buf[2];
Chris Masond6e4a422007-04-06 15:37:36 -0400842 struct btrfs_key key;
843
Chris Masonf7922032007-03-25 20:17:36 -0400844 buf[0] = '.';
845 buf[1] = '.';
846
Chris Mason2619ba12007-04-10 16:58:11 -0400847 key.objectid = objectid;
Chris Masond6e4a422007-04-06 15:37:36 -0400848 key.offset = 0;
849 key.flags = 0;
850 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
851
Chris Mason2619ba12007-04-10 16:58:11 -0400852 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
Chris Masond6e4a422007-04-06 15:37:36 -0400853 &key, 1);
Chris Masonf7922032007-03-25 20:17:36 -0400854 if (ret)
855 goto error;
Chris Mason2619ba12007-04-10 16:58:11 -0400856 key.objectid = dirid;
857 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
Chris Masond6e4a422007-04-06 15:37:36 -0400858 &key, 1);
Chris Mason4730a4b2007-03-26 12:00:39 -0400859 if (ret)
860 goto error;
Chris Masonf7922032007-03-25 20:17:36 -0400861error:
862 return ret;
863}
864
865static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
866{
867 struct inode *inode;
868 struct btrfs_trans_handle *trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400869 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masonf7922032007-03-25 20:17:36 -0400870 int err = 0;
871 int drop_on_err = 0;
Chris Mason2619ba12007-04-10 16:58:11 -0400872 u64 objectid;
Chris Masonf7922032007-03-25 20:17:36 -0400873
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 }
Chris Mason2619ba12007-04-10 16:58:11 -0400880
881 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
882 if (err) {
883 err = -ENOSPC;
884 goto out_unlock;
885 }
886
887 inode = btrfs_new_inode(trans, root, objectid, S_IFDIR | mode);
Chris Masonf7922032007-03-25 20:17:36 -0400888 if (IS_ERR(inode)) {
889 err = PTR_ERR(inode);
890 goto out_fail;
891 }
892 drop_on_err = 1;
893 inode->i_op = &btrfs_dir_inode_operations;
894 inode->i_fop = &btrfs_dir_file_operations;
895
Chris Mason2619ba12007-04-10 16:58:11 -0400896 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
897 if (err)
898 goto out_fail;
899
900 inode->i_size = 6;
901 err = btrfs_update_inode(trans, root, inode);
Chris Masonf7922032007-03-25 20:17:36 -0400902 if (err)
903 goto out_fail;
904 err = btrfs_add_link(trans, dentry, inode);
905 if (err)
906 goto out_fail;
907 d_instantiate(dentry, inode);
Chris Masonf7922032007-03-25 20:17:36 -0400908 drop_on_err = 0;
909
910out_fail:
911 btrfs_end_transaction(trans, root);
912out_unlock:
913 mutex_unlock(&root->fs_info->fs_mutex);
914 if (drop_on_err)
915 iput(inode);
916 return err;
917}
918
Chris Masond5719762007-03-23 10:01:08 -0400919static int btrfs_sync_fs(struct super_block *sb, int wait)
920{
921 struct btrfs_trans_handle *trans;
922 struct btrfs_root *root;
923 int ret;
Chris Masond98237b2007-03-28 13:57:48 -0400924 root = btrfs_sb(sb);
Chris Masondf2ce342007-03-23 11:00:45 -0400925
Chris Masond5719762007-03-23 10:01:08 -0400926 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -0400927 if (!wait) {
Chris Mason7cfcc172007-04-02 14:53:59 -0400928 filemap_flush(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400929 return 0;
930 }
Chris Mason7cfcc172007-04-02 14:53:59 -0400931 filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400932 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400933 trans = btrfs_start_transaction(root, 1);
934 ret = btrfs_commit_transaction(trans, root);
935 sb->s_dirt = 0;
936 BUG_ON(ret);
937printk("btrfs sync_fs\n");
Chris Masond561c022007-03-23 19:47:49 -0400938 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400939 return 0;
940}
941
Chris Masone8f05c42007-04-04 14:30:09 -0400942#if 0
Chris Masondf24a2b2007-04-04 09:36:31 -0400943static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
944 struct buffer_head *result, int create)
945{
946 struct btrfs_root *root = btrfs_sb(inode->i_sb);
947 struct btrfs_path *path;
948 struct btrfs_key key;
949 struct btrfs_leaf *leaf;
950 int num_bytes = result->b_size;
951 int item_size;
952 int ret;
953 u64 pos;
954 char *ptr;
955 int copy_size;
956 int err = 0;
957 char *safe_ptr;
958 char *data_ptr;
959
960 path = btrfs_alloc_path();
961 BUG_ON(!path);
962
963 WARN_ON(create);
964 if (create) {
965 return 0;
966 }
967 pos = iblock << inode->i_blkbits;
968 key.objectid = inode->i_ino;
969 key.flags = 0;
970 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
971 ptr = kmap(result->b_page);
972 safe_ptr = ptr;
973 ptr += (pos & (PAGE_CACHE_SIZE -1));
974again:
975 key.offset = pos;
976 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
977 if (ret) {
978 if (ret < 0)
979 err = ret;
980 else
981 err = 0;
982 goto out;
983 }
984 leaf = btrfs_buffer_leaf(path->nodes[0]);
985 item_size = btrfs_item_size(leaf->items + path->slots[0]);
986 copy_size = min(num_bytes, item_size);
987 data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
988 WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
989 memcpy(ptr, data_ptr, copy_size);
990 pos += copy_size;
991 num_bytes -= copy_size;
992 WARN_ON(num_bytes < 0);
993 ptr += copy_size;
994 btrfs_release_path(root, path);
995 if (num_bytes != 0) {
996 if (pos >= i_size_read(inode))
997 memset(ptr, 0, num_bytes);
998 else
999 goto again;
1000 }
1001 set_buffer_uptodate(result);
1002 map_bh(result, inode->i_sb, 0);
1003 err = 0;
1004out:
1005 btrfs_free_path(path);
1006 kunmap(result->b_page);
1007 return err;
1008}
Chris Masone8f05c42007-04-04 14:30:09 -04001009#endif
Chris Masondf24a2b2007-04-04 09:36:31 -04001010
Chris Mason75dfe392007-03-29 11:56:46 -04001011static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
Chris Masondee26a92007-03-26 16:00:06 -04001012 struct buffer_head *result, int create)
1013{
1014 int ret;
1015 int err = 0;
1016 u64 blocknr;
1017 u64 extent_start = 0;
1018 u64 extent_end = 0;
1019 u64 objectid = inode->i_ino;
Chris Mason5caf2a02007-04-02 11:20:42 -04001020 struct btrfs_path *path;
Chris Masond6e4a422007-04-06 15:37:36 -04001021 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondee26a92007-03-26 16:00:06 -04001022 struct btrfs_trans_handle *trans = NULL;
1023 struct btrfs_file_extent_item *item;
1024 struct btrfs_leaf *leaf;
1025 struct btrfs_disk_key *found_key;
1026
Chris Mason5caf2a02007-04-02 11:20:42 -04001027 path = btrfs_alloc_path();
1028 BUG_ON(!path);
1029 btrfs_init_path(path);
Chris Mason6567e832007-04-16 09:22:45 -04001030 if (create) {
Chris Masondee26a92007-03-26 16:00:06 -04001031 trans = btrfs_start_transaction(root, 1);
Chris Mason6567e832007-04-16 09:22:45 -04001032 WARN_ON(1);
1033 }
Chris Masondee26a92007-03-26 16:00:06 -04001034
Chris Mason5caf2a02007-04-02 11:20:42 -04001035 ret = btrfs_lookup_file_extent(trans, root, path,
Chris Mason9773a782007-03-27 11:26:26 -04001036 inode->i_ino,
Chris Mason79b2cb12007-04-10 13:51:41 -04001037 iblock << inode->i_blkbits, create);
Chris Masondee26a92007-03-26 16:00:06 -04001038 if (ret < 0) {
Chris Masondee26a92007-03-26 16:00:06 -04001039 err = ret;
1040 goto out;
1041 }
1042
1043 if (ret != 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001044 if (path->slots[0] == 0) {
1045 btrfs_release_path(root, path);
Chris Masondee26a92007-03-26 16:00:06 -04001046 goto allocate;
1047 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001048 path->slots[0]--;
Chris Masondee26a92007-03-26 16:00:06 -04001049 }
1050
Chris Mason5caf2a02007-04-02 11:20:42 -04001051 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masondee26a92007-03-26 16:00:06 -04001052 struct btrfs_file_extent_item);
Chris Mason5caf2a02007-04-02 11:20:42 -04001053 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masondee26a92007-03-26 16:00:06 -04001054 blocknr = btrfs_file_extent_disk_blocknr(item);
1055 blocknr += btrfs_file_extent_offset(item);
1056
Chris Mason48ddc6f2007-04-10 13:20:21 -04001057 /* exact match found, use it, FIXME, deal with extents
1058 * other than the page size
1059 */
Chris Mason6567e832007-04-16 09:22:45 -04001060 if (0 && ret == 0) {
Chris Masondee26a92007-03-26 16:00:06 -04001061 err = 0;
Chris Mason79b2cb12007-04-10 13:51:41 -04001062 if (create &&
1063 btrfs_file_extent_generation(item) != trans->transid) {
Chris Mason48ddc6f2007-04-10 13:20:21 -04001064 struct btrfs_key ins;
1065 ret = btrfs_alloc_extent(trans, root, 1,
1066 blocknr, (u64)-1, &ins);
1067 BUG_ON(ret);
1068 btrfs_set_file_extent_disk_blocknr(item, ins.objectid);
1069 mark_buffer_dirty(path->nodes[0]);
1070 ret = btrfs_free_extent(trans, root,
1071 blocknr, 1, 0);
1072 BUG_ON(ret);
1073 blocknr = ins.objectid;
1074
1075 }
Chris Mason8352d8a2007-04-12 10:43:05 -04001076 btrfs_map_bh_to_logical(root, result, blocknr);
Chris Masondee26a92007-03-26 16:00:06 -04001077 goto out;
1078 }
1079
1080 /* are we inside the extent that was found? */
Chris Mason5caf2a02007-04-02 11:20:42 -04001081 found_key = &leaf->items[path->slots[0]].key;
Chris Masondee26a92007-03-26 16:00:06 -04001082 if (btrfs_disk_key_objectid(found_key) != objectid ||
1083 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
1084 extent_end = 0;
1085 extent_start = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -04001086 btrfs_release_path(root, path);
Chris Masondee26a92007-03-26 16:00:06 -04001087 goto allocate;
1088 }
1089
Chris Mason5caf2a02007-04-02 11:20:42 -04001090 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
Chris Mason9773a782007-03-27 11:26:26 -04001091 extent_start = extent_start >> inode->i_blkbits;
Chris Masondee26a92007-03-26 16:00:06 -04001092 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
Chris Masondee26a92007-03-26 16:00:06 -04001093 if (iblock >= extent_start && iblock < extent_end) {
1094 err = 0;
Chris Mason8352d8a2007-04-12 10:43:05 -04001095 btrfs_map_bh_to_logical(root, result, blocknr + iblock -
1096 extent_start);
Chris Masondee26a92007-03-26 16:00:06 -04001097 goto out;
1098 }
1099allocate:
1100 /* ok, create a new extent */
1101 if (!create) {
1102 err = 0;
1103 goto out;
1104 }
Chris Masonb18c6682007-04-17 13:26:50 -04001105#if 0
Chris Mason9773a782007-03-27 11:26:26 -04001106 ret = btrfs_alloc_file_extent(trans, root, objectid,
1107 iblock << inode->i_blkbits,
Chris Masondee26a92007-03-26 16:00:06 -04001108 1, extent_end, &blocknr);
1109 if (ret) {
1110 err = ret;
1111 goto out;
1112 }
Chris Masonf4b9aa82007-03-27 11:05:53 -04001113 inode->i_blocks += inode->i_sb->s_blocksize >> 9;
1114 set_buffer_new(result);
Chris Masondee26a92007-03-26 16:00:06 -04001115 map_bh(result, inode->i_sb, blocknr);
1116
Chris Mason8352d8a2007-04-12 10:43:05 -04001117 btrfs_map_bh_to_logical(root, result, blocknr);
Chris Masonb18c6682007-04-17 13:26:50 -04001118#endif
Chris Masondee26a92007-03-26 16:00:06 -04001119out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001120 btrfs_release_path(root, path);
1121 btrfs_free_path(path);
Chris Masonb18c6682007-04-17 13:26:50 -04001122printk("mapping iblock %lu to %lu\n", iblock, result->b_blocknr);
Chris Masondee26a92007-03-26 16:00:06 -04001123 if (trans)
1124 btrfs_end_transaction(trans, root);
Chris Mason75dfe392007-03-29 11:56:46 -04001125 return err;
1126}
1127
1128static int btrfs_get_block(struct inode *inode, sector_t iblock,
1129 struct buffer_head *result, int create)
1130{
1131 int err;
Chris Masond6e4a422007-04-06 15:37:36 -04001132 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason75dfe392007-03-29 11:56:46 -04001133 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone8f05c42007-04-04 14:30:09 -04001134 err = btrfs_get_block_lock(inode, iblock, result, create);
1135 // err = btrfs_get_block_inline(inode, iblock, result, create);
Chris Masondee26a92007-03-26 16:00:06 -04001136 mutex_unlock(&root->fs_info->fs_mutex);
1137 return err;
1138}
1139
1140static int btrfs_prepare_write(struct file *file, struct page *page,
1141 unsigned from, unsigned to)
1142{
Chris Mason6407bf62007-03-27 06:33:00 -04001143 return nobh_prepare_write(page, from, to, btrfs_get_block);
Chris Masondee26a92007-03-26 16:00:06 -04001144}
Chris Mason75dfe392007-03-29 11:56:46 -04001145static int btrfs_commit_write(struct file *file, struct page *page,
1146 unsigned from, unsigned to)
1147{
Chris Mason75dfe392007-03-29 11:56:46 -04001148 return nobh_commit_write(file, page, from, to);
1149}
Chris Masondee26a92007-03-26 16:00:06 -04001150
Chris Masond561c022007-03-23 19:47:49 -04001151static void btrfs_write_super(struct super_block *sb)
1152{
1153 btrfs_sync_fs(sb, 1);
1154}
1155
Chris Masondee26a92007-03-26 16:00:06 -04001156static int btrfs_readpage(struct file *file, struct page *page)
1157{
1158 return mpage_readpage(page, btrfs_get_block);
1159}
1160
Chris Masondee26a92007-03-26 16:00:06 -04001161static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1162{
Chris Mason6407bf62007-03-27 06:33:00 -04001163 return nobh_writepage(page, btrfs_get_block, wbc);
Chris Masondee26a92007-03-26 16:00:06 -04001164}
Chris Masond561c022007-03-23 19:47:49 -04001165
Chris Masonf4b9aa82007-03-27 11:05:53 -04001166static void btrfs_truncate(struct inode *inode)
1167{
Chris Masond6e4a422007-04-06 15:37:36 -04001168 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonf4b9aa82007-03-27 11:05:53 -04001169 int ret;
1170 struct btrfs_trans_handle *trans;
1171
1172 if (!S_ISREG(inode->i_mode))
1173 return;
1174 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1175 return;
1176
Chris Masone8f05c42007-04-04 14:30:09 -04001177 nobh_truncate_page(inode->i_mapping, inode->i_size);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001178
1179 /* FIXME, add redo link to tree so we don't leak on crash */
1180 mutex_lock(&root->fs_info->fs_mutex);
1181 trans = btrfs_start_transaction(root, 1);
1182 ret = btrfs_truncate_in_trans(trans, root, inode);
1183 BUG_ON(ret);
1184 ret = btrfs_end_transaction(trans, root);
1185 BUG_ON(ret);
1186 mutex_unlock(&root->fs_info->fs_mutex);
1187 mark_inode_dirty(inode);
1188}
1189
Chris Mason75dfe392007-03-29 11:56:46 -04001190static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
1191 struct page **prepared_pages,
1192 const char __user * buf)
1193{
1194 long page_fault = 0;
1195 int i;
1196 int offset = pos & (PAGE_CACHE_SIZE - 1);
1197
1198 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
1199 size_t count = min_t(size_t,
1200 PAGE_CACHE_SIZE - offset, write_bytes);
1201 struct page *page = prepared_pages[i];
1202 fault_in_pages_readable(buf, count);
1203
1204 /* Copy data from userspace to the current page */
1205 kmap(page);
1206 page_fault = __copy_from_user(page_address(page) + offset,
1207 buf, count);
1208 /* Flush processor's dcache for this page */
1209 flush_dcache_page(page);
1210 kunmap(page);
1211 buf += count;
1212 write_bytes -= count;
1213
1214 if (page_fault)
1215 break;
1216 }
1217 return page_fault ? -EFAULT : 0;
1218}
1219
1220static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1221{
1222 size_t i;
1223 for (i = 0; i < num_pages; i++) {
1224 if (!pages[i])
1225 break;
1226 unlock_page(pages[i]);
1227 mark_page_accessed(pages[i]);
1228 page_cache_release(pages[i]);
1229 }
1230}
1231static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1232 struct btrfs_root *root,
1233 struct file *file,
1234 struct page **pages,
1235 size_t num_pages,
Chris Mason6567e832007-04-16 09:22:45 -04001236 u64 extent_offset,
Chris Mason75dfe392007-03-29 11:56:46 -04001237 loff_t pos,
1238 size_t write_bytes)
1239{
1240 int i;
1241 int offset;
1242 int err = 0;
1243 int ret;
1244 int this_write;
Chris Masonf254e522007-03-29 15:15:27 -04001245 struct inode *inode = file->f_path.dentry->d_inode;
Chris Mason75dfe392007-03-29 11:56:46 -04001246
1247 for (i = 0; i < num_pages; i++) {
1248 offset = pos & (PAGE_CACHE_SIZE -1);
1249 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
Chris Masonf254e522007-03-29 15:15:27 -04001250 /* FIXME, one block at a time */
1251
1252 mutex_lock(&root->fs_info->fs_mutex);
1253 trans = btrfs_start_transaction(root, 1);
1254 btrfs_csum_file_block(trans, root, inode->i_ino,
1255 pages[i]->index << PAGE_CACHE_SHIFT,
Chris Mason6567e832007-04-16 09:22:45 -04001256 extent_offset,
Chris Masonf254e522007-03-29 15:15:27 -04001257 kmap(pages[i]), PAGE_CACHE_SIZE);
1258 kunmap(pages[i]);
1259 SetPageChecked(pages[i]);
1260 ret = btrfs_end_transaction(trans, root);
1261 BUG_ON(ret);
1262 mutex_unlock(&root->fs_info->fs_mutex);
1263
Chris Mason75dfe392007-03-29 11:56:46 -04001264 ret = nobh_commit_write(file, pages[i], offset,
1265 offset + this_write);
1266 pos += this_write;
1267 if (ret) {
1268 err = ret;
1269 goto failed;
1270 }
1271 WARN_ON(this_write > write_bytes);
1272 write_bytes -= this_write;
1273 }
1274failed:
1275 return err;
1276}
1277
Chris Masonb18c6682007-04-17 13:26:50 -04001278static int drop_csums(struct btrfs_trans_handle *trans,
1279 struct btrfs_root *root,
1280 struct inode *inode,
1281 u64 start, u64 end)
1282{
1283 struct btrfs_path *path;
1284 struct btrfs_leaf *leaf;
1285 struct btrfs_key key;
1286 int slot;
1287 struct btrfs_csum_item *item;
1288 char *old_block = NULL;
1289 u64 cur = start;
1290 u64 found_end;
1291 u64 num_csums;
1292 u64 item_size;
1293 int ret;
1294
1295 path = btrfs_alloc_path();
1296 if (!path)
1297 return -ENOMEM;
1298 while(cur < end) {
1299 item = btrfs_lookup_csum(trans, root, path,
1300 inode->i_ino, cur, 1);
1301 if (IS_ERR(item)) {
1302 cur += root->blocksize;
1303 continue;
1304 }
1305 leaf = btrfs_buffer_leaf(path->nodes[0]);
1306 slot = path->slots[0];
1307 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
1308 item_size = btrfs_item_size(leaf->items + slot);
1309 num_csums = item_size / sizeof(struct btrfs_csum_item);
1310 found_end = key.offset + (num_csums << inode->i_blkbits);
1311 cur = found_end;
1312
1313 if (found_end > end) {
1314 char *src;
1315 old_block = kmalloc(root->blocksize, GFP_NOFS);
1316 src = btrfs_item_ptr(leaf, slot, char);
1317 memcpy(old_block, src, item_size);
1318 }
1319 if (key.offset < start) {
1320 u64 new_size = (start - key.offset) >>
1321 inode->i_blkbits;
1322 new_size *= sizeof(struct btrfs_csum_item);
1323 ret = btrfs_truncate_item(trans, root, path, new_size);
1324 BUG_ON(ret);
1325 } else {
1326 btrfs_del_item(trans, root, path);
1327 }
1328 btrfs_release_path(root, path);
1329 if (found_end > end) {
1330 char *dst;
1331 int i;
1332 int new_size;
1333
1334 num_csums = (found_end - end) >> inode->i_blkbits;
1335 new_size = num_csums * sizeof(struct btrfs_csum_item);
1336 key.offset = end;
1337 ret = btrfs_insert_empty_item(trans, root, path,
1338 &key, new_size);
1339 BUG_ON(ret);
1340 dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1341 path->slots[0], char);
1342 memcpy(dst, old_block + item_size - new_size,
1343 new_size);
1344 item = (struct btrfs_csum_item *)dst;
1345 for (i = 0; i < num_csums; i++) {
1346 btrfs_set_csum_extent_offset(item, end);
1347 item++;
1348 }
1349 mark_buffer_dirty(path->nodes[0]);
1350 kfree(old_block);
1351 break;
1352 }
1353 }
1354 btrfs_free_path(path);
1355 return 0;
1356}
1357
1358static int drop_extents(struct btrfs_trans_handle *trans,
1359 struct btrfs_root *root,
1360 struct inode *inode,
1361 u64 start, u64 end)
1362{
1363 int ret;
1364 struct btrfs_key key;
1365 struct btrfs_leaf *leaf;
1366 int slot;
1367 struct btrfs_file_extent_item *extent;
1368 u64 extent_end;
1369 int keep;
1370 struct btrfs_file_extent_item old;
1371 struct btrfs_path *path;
1372 u64 search_start = start;
1373 int bookend;
1374
1375 path = btrfs_alloc_path();
1376 if (!path)
1377 return -ENOMEM;
1378search_again:
1379printk("drop extent inode %lu start %Lu end %Lu\n", inode->i_ino, start, end);
1380 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1381 search_start, -1);
1382 if (ret != 0) {
1383printk("lookup failed\n");
1384 goto out;
1385 }
1386 while(1) {
1387 keep = 0;
1388 bookend = 0;
1389 leaf = btrfs_buffer_leaf(path->nodes[0]);
1390 slot = path->slots[0];
1391 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
1392
1393printk("found key %Lu %Lu %u\n", key.objectid, key.offset, key.flags);
1394
1395 extent = btrfs_item_ptr(leaf, slot,
1396 struct btrfs_file_extent_item);
1397 extent_end = key.offset +
1398 (btrfs_file_extent_num_blocks(extent) <<
1399 inode->i_blkbits);
1400printk("extent end is %Lu\n", extent_end);
1401 if (key.offset >= end || key.objectid != inode->i_ino) {
1402 ret = 0;
1403 goto out;
1404 }
1405 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1406 goto next_leaf;
1407
1408 if (end < extent_end && end >= key.offset) {
1409 memcpy(&old, extent, sizeof(old));
1410 ret = btrfs_inc_extent_ref(trans, root,
1411 btrfs_file_extent_disk_blocknr(&old),
1412 btrfs_file_extent_disk_num_blocks(&old));
1413 BUG_ON(ret);
1414 bookend = 1;
1415 }
1416
1417 if (start > key.offset) {
1418 u64 new_num;
1419 /* truncate existing extent */
1420 keep = 1;
1421 WARN_ON(start & (root->blocksize - 1));
1422 new_num = (start - key.offset) >> inode->i_blkbits;
1423printk("truncating existing extent, was %Lu ", btrfs_file_extent_num_blocks(extent));
1424 btrfs_set_file_extent_num_blocks(extent, new_num);
1425printk("now %Lu\n", btrfs_file_extent_num_blocks(extent));
1426
1427 mark_buffer_dirty(path->nodes[0]);
1428 }
1429 if (!keep) {
1430 u64 disk_blocknr;
1431 u64 disk_num_blocks;
1432printk("del old\n");
1433 disk_blocknr = btrfs_file_extent_disk_blocknr(extent);
1434 disk_num_blocks =
1435 btrfs_file_extent_disk_num_blocks(extent);
1436 search_start = key.offset +
1437 (btrfs_file_extent_num_blocks(extent) <<
1438 inode->i_blkbits);
1439 ret = btrfs_del_item(trans, root, path);
1440 BUG_ON(ret);
1441 btrfs_release_path(root, path);
1442
1443 ret = btrfs_free_extent(trans, root, disk_blocknr,
1444 disk_num_blocks, 0);
1445
1446 BUG_ON(ret);
1447 if (!bookend && search_start >= end) {
1448 ret = 0;
1449 goto out;
1450 }
1451 if (!bookend)
1452 goto search_again;
1453 }
1454 if (bookend) {
1455 /* create bookend */
1456 struct btrfs_key ins;
1457printk("bookend! extent end %Lu\n", extent_end);
1458 ins.objectid = inode->i_ino;
1459 ins.offset = end;
1460 ins.flags = 0;
1461 btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
1462
1463 btrfs_release_path(root, path);
1464 ret = drop_csums(trans, root, inode, start, end);
1465 BUG_ON(ret);
1466 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1467 sizeof(*extent));
1468 BUG_ON(ret);
1469 extent = btrfs_item_ptr(
1470 btrfs_buffer_leaf(path->nodes[0]),
1471 path->slots[0],
1472 struct btrfs_file_extent_item);
1473 btrfs_set_file_extent_disk_blocknr(extent,
1474 btrfs_file_extent_disk_blocknr(&old));
1475 btrfs_set_file_extent_disk_num_blocks(extent,
1476 btrfs_file_extent_disk_num_blocks(&old));
1477
1478 btrfs_set_file_extent_offset(extent,
1479 btrfs_file_extent_offset(&old) +
1480 ((end - key.offset) >> inode->i_blkbits));
1481 WARN_ON(btrfs_file_extent_num_blocks(&old) <
1482 (end - key.offset) >> inode->i_blkbits);
1483 btrfs_set_file_extent_num_blocks(extent,
1484 btrfs_file_extent_num_blocks(&old) -
1485 ((end - key.offset) >> inode->i_blkbits));
1486
1487 btrfs_set_file_extent_generation(extent,
1488 btrfs_file_extent_generation(&old));
1489printk("new bookend at offset %Lu, file_extent_offset %Lu, file_extent_num_blocks %Lu\n", end, btrfs_file_extent_offset(extent), btrfs_file_extent_num_blocks(extent));
1490 btrfs_mark_buffer_dirty(path->nodes[0]);
1491 ret = 0;
1492 goto out_nocsum;
1493 }
1494next_leaf:
1495 if (slot >= btrfs_header_nritems(&leaf->header) - 1) {
1496 ret = btrfs_next_leaf(root, path);
1497 if (ret) {
1498 ret = 0;
1499 goto out;
1500 }
1501 } else {
1502 path->slots[0]++;
1503 }
1504 }
1505
1506out:
1507 ret = drop_csums(trans, root, inode, start, end);
1508 BUG_ON(ret);
1509
1510out_nocsum:
1511 btrfs_free_path(path);
1512 return ret;
1513}
1514
1515static int prepare_pages(struct btrfs_root *root,
Chris Mason75dfe392007-03-29 11:56:46 -04001516 struct file *file,
1517 struct page **pages,
1518 size_t num_pages,
1519 loff_t pos,
Chris Mason2932f3e2007-04-10 14:22:02 -04001520 unsigned long first_index,
1521 unsigned long last_index,
Chris Mason6567e832007-04-16 09:22:45 -04001522 size_t write_bytes,
1523 u64 alloc_extent_start)
Chris Mason75dfe392007-03-29 11:56:46 -04001524{
1525 int i;
1526 unsigned long index = pos >> PAGE_CACHE_SHIFT;
1527 struct inode *inode = file->f_path.dentry->d_inode;
1528 int offset;
1529 int err = 0;
Chris Mason75dfe392007-03-29 11:56:46 -04001530 int this_write;
Chris Mason6567e832007-04-16 09:22:45 -04001531 struct buffer_head *bh;
1532 struct buffer_head *head;
Chris Mason75dfe392007-03-29 11:56:46 -04001533 loff_t isize = i_size_read(inode);
1534
1535 memset(pages, 0, num_pages * sizeof(struct page *));
1536
1537 for (i = 0; i < num_pages; i++) {
1538 pages[i] = grab_cache_page(inode->i_mapping, index + i);
1539 if (!pages[i]) {
1540 err = -ENOMEM;
1541 goto failed_release;
1542 }
1543 offset = pos & (PAGE_CACHE_SIZE -1);
1544 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
Chris Masonb18c6682007-04-17 13:26:50 -04001545#if 0
1546 if ((pages[i]->index == first_index ||
1547 pages[i]->index == last_index) && pos < isize &&
1548 !PageUptodate(pages[i])) {
Chris Mason2932f3e2007-04-10 14:22:02 -04001549 ret = mpage_readpage(pages[i], btrfs_get_block);
1550 BUG_ON(ret);
1551 lock_page(pages[i]);
1552 }
Chris Masonb18c6682007-04-17 13:26:50 -04001553#endif
Chris Mason6567e832007-04-16 09:22:45 -04001554 create_empty_buffers(pages[i], root->fs_info->sb->s_blocksize,
1555 (1 << BH_Uptodate));
1556 head = page_buffers(pages[i]);
1557 bh = head;
1558 do {
Chris Masonb18c6682007-04-17 13:26:50 -04001559printk("mapping page %lu to block %Lu\n", pages[i]->index, alloc_extent_start);
Chris Mason6567e832007-04-16 09:22:45 -04001560 err = btrfs_map_bh_to_logical(root, bh,
1561 alloc_extent_start);
1562 BUG_ON(err);
1563 if (err)
1564 goto failed_truncate;
1565 bh = bh->b_this_page;
1566 alloc_extent_start++;
1567 } while (bh != head);
Chris Mason75dfe392007-03-29 11:56:46 -04001568 pos += this_write;
Chris Mason75dfe392007-03-29 11:56:46 -04001569 WARN_ON(this_write > write_bytes);
1570 write_bytes -= this_write;
1571 }
1572 return 0;
1573
1574failed_release:
1575 btrfs_drop_pages(pages, num_pages);
1576 return err;
1577
1578failed_truncate:
1579 btrfs_drop_pages(pages, num_pages);
1580 if (pos > isize)
1581 vmtruncate(inode, isize);
1582 return err;
1583}
1584
1585static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1586 size_t count, loff_t *ppos)
1587{
1588 loff_t pos;
1589 size_t num_written = 0;
1590 int err = 0;
1591 int ret = 0;
Chris Mason75dfe392007-03-29 11:56:46 -04001592 struct inode *inode = file->f_path.dentry->d_inode;
Chris Masond6e4a422007-04-06 15:37:36 -04001593 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonb18c6682007-04-17 13:26:50 -04001594 struct page *pages[8];
Chris Mason2932f3e2007-04-10 14:22:02 -04001595 unsigned long first_index;
1596 unsigned long last_index;
Chris Mason6567e832007-04-16 09:22:45 -04001597 u64 start_pos;
1598 u64 num_blocks;
1599 u64 alloc_extent_start;
1600 u64 orig_extent_start;
1601 struct btrfs_trans_handle *trans;
Chris Masonb18c6682007-04-17 13:26:50 -04001602 struct btrfs_key ins;
Chris Mason75dfe392007-03-29 11:56:46 -04001603
1604 if (file->f_flags & O_DIRECT)
1605 return -EINVAL;
1606 pos = *ppos;
1607
Chris Mason6567e832007-04-16 09:22:45 -04001608 start_pos = pos & ~(root->blocksize - 1);
1609 /* FIXME */
1610 if (start_pos != pos)
1611 return -EINVAL;
1612 num_blocks = (count + pos - start_pos + root->blocksize - 1) >>
1613 inode->i_blkbits;
1614
Chris Mason75dfe392007-03-29 11:56:46 -04001615 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1616 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1617 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1618 if (err)
1619 goto out;
1620 if (count == 0)
1621 goto out;
1622 err = remove_suid(file->f_path.dentry);
1623 if (err)
1624 goto out;
1625 file_update_time(file);
1626 mutex_lock(&inode->i_mutex);
Chris Mason2932f3e2007-04-10 14:22:02 -04001627 first_index = pos >> PAGE_CACHE_SHIFT;
1628 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
Chris Mason6567e832007-04-16 09:22:45 -04001629
1630 mutex_lock(&root->fs_info->fs_mutex);
1631 trans = btrfs_start_transaction(root, 1);
1632 if (!trans) {
1633 err = -ENOMEM;
Chris Masonb18c6682007-04-17 13:26:50 -04001634 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason6567e832007-04-16 09:22:45 -04001635 goto out_unlock;
1636 }
Chris Masonb18c6682007-04-17 13:26:50 -04001637 if (start_pos < inode->i_size) {
1638 ret = drop_extents(trans, root, inode,
1639 start_pos,
1640 (pos + count + root->blocksize -1) &
1641 ~(root->blocksize - 1));
1642 }
Chris Mason6567e832007-04-16 09:22:45 -04001643 orig_extent_start = start_pos;
Chris Masonb18c6682007-04-17 13:26:50 -04001644 ret = btrfs_alloc_extent(trans, root, num_blocks, 1,
1645 (u64)-1, &ins);
Chris Mason6567e832007-04-16 09:22:45 -04001646 BUG_ON(ret);
Chris Masonb18c6682007-04-17 13:26:50 -04001647 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
1648 start_pos, ins.objectid, ins.offset);
1649 BUG_ON(ret);
1650 alloc_extent_start = ins.objectid;
1651 ret = btrfs_end_transaction(trans, root);
Chris Mason6567e832007-04-16 09:22:45 -04001652 mutex_unlock(&root->fs_info->fs_mutex);
1653
Chris Mason75dfe392007-03-29 11:56:46 -04001654 while(count > 0) {
1655 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1656 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1657 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
1658 PAGE_CACHE_SHIFT;
Chris Masonb18c6682007-04-17 13:26:50 -04001659printk("num_pages is %lu\n", num_pages);
1660
1661 memset(pages, 0, sizeof(pages));
1662 ret = prepare_pages(root, file, pages, num_pages,
Chris Mason6567e832007-04-16 09:22:45 -04001663 pos, first_index, last_index,
1664 write_bytes, alloc_extent_start);
Chris Mason75dfe392007-03-29 11:56:46 -04001665 BUG_ON(ret);
Chris Masonb18c6682007-04-17 13:26:50 -04001666
Chris Mason6567e832007-04-16 09:22:45 -04001667 /* FIXME blocks != pagesize */
1668 alloc_extent_start += num_pages;
Chris Mason75dfe392007-03-29 11:56:46 -04001669 ret = btrfs_copy_from_user(pos, num_pages,
1670 write_bytes, pages, buf);
1671 BUG_ON(ret);
1672
Chris Masonb18c6682007-04-17 13:26:50 -04001673printk("2num_pages is %lu\n", num_pages);
Chris Masonf254e522007-03-29 15:15:27 -04001674 ret = dirty_and_release_pages(NULL, root, file, pages,
Chris Mason6567e832007-04-16 09:22:45 -04001675 num_pages, orig_extent_start,
1676 pos, write_bytes);
Chris Mason75dfe392007-03-29 11:56:46 -04001677 BUG_ON(ret);
1678 btrfs_drop_pages(pages, num_pages);
1679
Chris Mason75dfe392007-03-29 11:56:46 -04001680 buf += write_bytes;
1681 count -= write_bytes;
1682 pos += write_bytes;
1683 num_written += write_bytes;
1684
1685 balance_dirty_pages_ratelimited(inode->i_mapping);
1686 cond_resched();
1687 }
Chris Mason6567e832007-04-16 09:22:45 -04001688out_unlock:
Chris Mason75dfe392007-03-29 11:56:46 -04001689 mutex_unlock(&inode->i_mutex);
1690out:
1691 *ppos = pos;
1692 current->backing_dev_info = NULL;
1693 return num_written ? num_written : err;
1694}
1695
Chris Masone8f05c42007-04-04 14:30:09 -04001696#if 0
Chris Masondf24a2b2007-04-04 09:36:31 -04001697static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
1698 struct page *page, loff_t pos,
1699 size_t offset, size_t write_bytes)
1700{
1701 struct btrfs_path *path;
1702 struct btrfs_trans_handle *trans;
1703 struct btrfs_key key;
1704 struct btrfs_leaf *leaf;
1705 struct btrfs_key found_key;
1706 int ret;
1707 size_t copy_size = 0;
1708 char *dst = NULL;
1709 int err = 0;
1710 size_t num_written = 0;
1711
1712 path = btrfs_alloc_path();
1713 BUG_ON(!path);
1714 mutex_lock(&root->fs_info->fs_mutex);
1715 trans = btrfs_start_transaction(root, 1);
1716 key.objectid = inode->i_ino;
1717 key.flags = 0;
1718 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
1719
1720again:
1721 key.offset = pos;
1722 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1723 if (ret < 0) {
1724 err = ret;
1725 goto out;
1726 }
1727 if (ret == 0) {
1728 leaf = btrfs_buffer_leaf(path->nodes[0]);
1729 btrfs_disk_key_to_cpu(&found_key,
1730 &leaf->items[path->slots[0]].key);
1731 copy_size = btrfs_item_size(leaf->items + path->slots[0]);
1732 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1733 copy_size = min(write_bytes, copy_size);
1734 goto copyit;
1735 } else {
1736 int slot = path->slots[0];
1737 if (slot > 0) {
1738 slot--;
1739 }
1740 // FIXME find max key
1741 leaf = btrfs_buffer_leaf(path->nodes[0]);
1742 btrfs_disk_key_to_cpu(&found_key,
1743 &leaf->items[slot].key);
1744 if (found_key.objectid != inode->i_ino)
1745 goto insert;
1746 if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
1747 goto insert;
1748 copy_size = btrfs_item_size(leaf->items + slot);
1749 if (found_key.offset + copy_size <= pos)
1750 goto insert;
1751 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1752 dst += pos - found_key.offset;
1753 copy_size = copy_size - (pos - found_key.offset);
1754 BUG_ON(copy_size < 0);
1755 copy_size = min(write_bytes, copy_size);
1756 WARN_ON(copy_size == 0);
1757 goto copyit;
1758 }
1759insert:
1760 btrfs_release_path(root, path);
Chris Masond4dbff92007-04-04 14:08:15 -04001761 copy_size = min(write_bytes,
1762 (size_t)BTRFS_LEAF_DATA_SIZE(root) -
1763 sizeof(struct btrfs_item) * 4);
Chris Masondf24a2b2007-04-04 09:36:31 -04001764 ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
1765 BUG_ON(ret);
1766 dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1767 path->slots[0], char);
1768copyit:
1769 WARN_ON(copy_size == 0);
1770 WARN_ON(dst + copy_size >
1771 btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1772 path->slots[0], char) +
1773 btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
1774 path->slots[0]));
1775 btrfs_memcpy(root, path->nodes[0]->b_data, dst,
1776 page_address(page) + offset, copy_size);
1777 mark_buffer_dirty(path->nodes[0]);
1778 btrfs_release_path(root, path);
1779 pos += copy_size;
1780 offset += copy_size;
1781 num_written += copy_size;
1782 write_bytes -= copy_size;
1783 if (write_bytes)
1784 goto again;
1785out:
1786 btrfs_free_path(path);
1787 ret = btrfs_end_transaction(trans, root);
1788 BUG_ON(ret);
1789 mutex_unlock(&root->fs_info->fs_mutex);
1790 return num_written ? num_written : err;
1791}
1792
1793static ssize_t btrfs_file_inline_write(struct file *file,
1794 const char __user *buf,
1795 size_t count, loff_t *ppos)
1796{
1797 loff_t pos;
1798 size_t num_written = 0;
1799 int err = 0;
1800 int ret = 0;
1801 struct inode *inode = file->f_path.dentry->d_inode;
Chris Masond6e4a422007-04-06 15:37:36 -04001802 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondf24a2b2007-04-04 09:36:31 -04001803 unsigned long page_index;
1804
1805 if (file->f_flags & O_DIRECT)
1806 return -EINVAL;
1807 pos = *ppos;
1808
1809 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1810 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1811 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1812 if (err)
1813 goto out;
1814 if (count == 0)
1815 goto out;
1816 err = remove_suid(file->f_path.dentry);
1817 if (err)
1818 goto out;
1819 file_update_time(file);
1820 mutex_lock(&inode->i_mutex);
1821 while(count > 0) {
1822 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1823 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1824 struct page *page;
1825
1826 page_index = pos >> PAGE_CACHE_SHIFT;
1827 page = grab_cache_page(inode->i_mapping, page_index);
1828 if (!PageUptodate(page)) {
1829 ret = mpage_readpage(page, btrfs_get_block);
1830 BUG_ON(ret);
1831 lock_page(page);
1832 }
1833 ret = btrfs_copy_from_user(pos, 1,
1834 write_bytes, &page, buf);
1835 BUG_ON(ret);
1836 write_bytes = inline_one_page(root, inode, page, pos,
1837 offset, write_bytes);
1838 SetPageUptodate(page);
1839 if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
1840 i_size_write(inode, pos + write_bytes);
1841 mark_inode_dirty(inode);
1842 }
1843 page_cache_release(page);
1844 unlock_page(page);
1845 if (write_bytes < 0)
1846 goto out_unlock;
1847 buf += write_bytes;
1848 count -= write_bytes;
1849 pos += write_bytes;
1850 num_written += write_bytes;
1851
1852 balance_dirty_pages_ratelimited(inode->i_mapping);
1853 cond_resched();
1854 }
1855out_unlock:
1856 mutex_unlock(&inode->i_mutex);
1857out:
1858 *ppos = pos;
1859 current->backing_dev_info = NULL;
1860 return num_written ? num_written : err;
1861}
Chris Masone8f05c42007-04-04 14:30:09 -04001862#endif
Chris Masondf24a2b2007-04-04 09:36:31 -04001863
Chris Masonf254e522007-03-29 15:15:27 -04001864static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
1865 unsigned long offset, unsigned long size)
1866{
1867 char *kaddr;
1868 unsigned long left, count = desc->count;
Chris Masond6e4a422007-04-06 15:37:36 -04001869 struct inode *inode = page->mapping->host;
Chris Masonf254e522007-03-29 15:15:27 -04001870
1871 if (size > count)
1872 size = count;
1873
1874 if (!PageChecked(page)) {
1875 /* FIXME, do it per block */
Chris Masond6e4a422007-04-06 15:37:36 -04001876 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonf254e522007-03-29 15:15:27 -04001877 int ret = btrfs_csum_verify_file_block(root,
1878 page->mapping->host->i_ino,
1879 page->index << PAGE_CACHE_SHIFT,
1880 kmap(page), PAGE_CACHE_SIZE);
1881 if (ret) {
1882 printk("failed to verify ino %lu page %lu\n",
1883 page->mapping->host->i_ino,
1884 page->index);
1885 memset(page_address(page), 0, PAGE_CACHE_SIZE);
1886 }
1887 SetPageChecked(page);
1888 kunmap(page);
1889 }
1890 /*
1891 * Faults on the destination of a read are common, so do it before
1892 * taking the kmap.
1893 */
1894 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1895 kaddr = kmap_atomic(page, KM_USER0);
1896 left = __copy_to_user_inatomic(desc->arg.buf,
1897 kaddr + offset, size);
1898 kunmap_atomic(kaddr, KM_USER0);
1899 if (left == 0)
1900 goto success;
1901 }
1902
1903 /* Do it the slow way */
1904 kaddr = kmap(page);
1905 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1906 kunmap(page);
1907
1908 if (left) {
1909 size -= left;
1910 desc->error = -EFAULT;
1911 }
1912success:
1913 desc->count = count - size;
1914 desc->written += size;
1915 desc->arg.buf += size;
1916 return size;
1917}
1918
1919/**
1920 * btrfs_file_aio_read - filesystem read routine
1921 * @iocb: kernel I/O control block
1922 * @iov: io vector request
1923 * @nr_segs: number of segments in the iovec
1924 * @pos: current file position
1925 */
1926static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1927 unsigned long nr_segs, loff_t pos)
1928{
1929 struct file *filp = iocb->ki_filp;
1930 ssize_t retval;
1931 unsigned long seg;
1932 size_t count;
1933 loff_t *ppos = &iocb->ki_pos;
1934
1935 count = 0;
1936 for (seg = 0; seg < nr_segs; seg++) {
1937 const struct iovec *iv = &iov[seg];
1938
1939 /*
1940 * If any segment has a negative length, or the cumulative
1941 * length ever wraps negative then return -EINVAL.
1942 */
1943 count += iv->iov_len;
1944 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
1945 return -EINVAL;
1946 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
1947 continue;
1948 if (seg == 0)
1949 return -EFAULT;
1950 nr_segs = seg;
1951 count -= iv->iov_len; /* This segment is no good */
1952 break;
1953 }
1954 retval = 0;
1955 if (count) {
1956 for (seg = 0; seg < nr_segs; seg++) {
1957 read_descriptor_t desc;
1958
1959 desc.written = 0;
1960 desc.arg.buf = iov[seg].iov_base;
1961 desc.count = iov[seg].iov_len;
1962 if (desc.count == 0)
1963 continue;
1964 desc.error = 0;
1965 do_generic_file_read(filp, ppos, &desc,
1966 btrfs_read_actor);
1967 retval += desc.written;
1968 if (desc.error) {
1969 retval = retval ?: desc.error;
1970 break;
1971 }
1972 }
1973 }
1974 return retval;
1975}
1976
Chris Mason2619ba12007-04-10 16:58:11 -04001977static int create_subvol(struct btrfs_root *root, char *name, int namelen)
1978{
1979 struct btrfs_trans_handle *trans;
1980 struct btrfs_key key;
1981 struct btrfs_root_item root_item;
1982 struct btrfs_inode_item *inode_item;
1983 struct buffer_head *subvol;
1984 struct btrfs_leaf *leaf;
1985 struct btrfs_root *new_root;
1986 struct inode *inode;
1987 int ret;
1988 u64 objectid;
1989 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
1990
1991 mutex_lock(&root->fs_info->fs_mutex);
1992 trans = btrfs_start_transaction(root, 1);
1993 BUG_ON(!trans);
1994
1995 subvol = btrfs_alloc_free_block(trans, root);
1996 leaf = btrfs_buffer_leaf(subvol);
1997 btrfs_set_header_nritems(&leaf->header, 0);
1998 btrfs_set_header_level(&leaf->header, 0);
Chris Mason7eccb902007-04-11 15:53:25 -04001999 btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
Chris Mason2619ba12007-04-10 16:58:11 -04002000 btrfs_set_header_generation(&leaf->header, trans->transid);
2001 memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
2002 sizeof(leaf->header.fsid));
2003
2004 inode_item = &root_item.inode;
2005 memset(inode_item, 0, sizeof(*inode_item));
2006 btrfs_set_inode_generation(inode_item, 1);
2007 btrfs_set_inode_size(inode_item, 3);
2008 btrfs_set_inode_nlink(inode_item, 1);
2009 btrfs_set_inode_nblocks(inode_item, 1);
2010 btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
2011
Chris Mason7eccb902007-04-11 15:53:25 -04002012 btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
Chris Mason2619ba12007-04-10 16:58:11 -04002013 btrfs_set_root_refs(&root_item, 1);
2014
2015 mark_buffer_dirty(subvol);
2016 brelse(subvol);
2017 subvol = NULL;
2018
2019 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2020 0, &objectid);
2021 BUG_ON(ret);
2022
2023 btrfs_set_root_dirid(&root_item, new_dirid);
2024
2025 key.objectid = objectid;
2026 key.offset = 1;
2027 key.flags = 0;
2028 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2029 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2030 &root_item);
2031 BUG_ON(ret);
2032
2033 /*
2034 * insert the directory item
2035 */
2036 key.offset = (u64)-1;
2037 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2038 name, namelen,
2039 root->fs_info->sb->s_root->d_inode->i_ino,
2040 &key, 0);
2041 BUG_ON(ret);
2042
2043 ret = btrfs_commit_transaction(trans, root);
2044 BUG_ON(ret);
2045
2046 new_root = btrfs_read_fs_root(root->fs_info, &key);
2047 BUG_ON(!new_root);
2048
2049 trans = btrfs_start_transaction(new_root, 1);
2050 BUG_ON(!trans);
2051
2052 inode = btrfs_new_inode(trans, new_root, new_dirid, S_IFDIR | 0700);
2053 inode->i_op = &btrfs_dir_inode_operations;
2054 inode->i_fop = &btrfs_dir_file_operations;
2055
2056 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
2057 BUG_ON(ret);
2058
2059 inode->i_nlink = 1;
2060 inode->i_size = 6;
2061 ret = btrfs_update_inode(trans, new_root, inode);
2062 BUG_ON(ret);
2063
2064 ret = btrfs_commit_transaction(trans, new_root);
2065 BUG_ON(ret);
2066
2067 iput(inode);
2068
2069 mutex_unlock(&root->fs_info->fs_mutex);
2070 return 0;
2071}
2072
Chris Masonc5739bb2007-04-10 09:27:04 -04002073static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2074{
2075 struct btrfs_trans_handle *trans;
2076 struct btrfs_key key;
2077 struct btrfs_root_item new_root_item;
2078 int ret;
2079 u64 objectid;
2080
Chris Mason2619ba12007-04-10 16:58:11 -04002081 if (!root->ref_cows)
2082 return -EINVAL;
2083
Chris Masonc5739bb2007-04-10 09:27:04 -04002084 mutex_lock(&root->fs_info->fs_mutex);
2085 trans = btrfs_start_transaction(root, 1);
2086 BUG_ON(!trans);
2087
2088 ret = btrfs_update_inode(trans, root, root->inode);
2089 BUG_ON(ret);
2090
Chris Mason1b05da22007-04-10 12:13:09 -04002091 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2092 0, &objectid);
Chris Masonc5739bb2007-04-10 09:27:04 -04002093 BUG_ON(ret);
2094
Chris Masonc5739bb2007-04-10 09:27:04 -04002095 memcpy(&new_root_item, &root->root_item,
2096 sizeof(new_root_item));
2097
2098 key.objectid = objectid;
Chris Masonc5739bb2007-04-10 09:27:04 -04002099 key.offset = 1;
2100 key.flags = 0;
2101 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
Chris Mason7eccb902007-04-11 15:53:25 -04002102 btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
Chris Masonc5739bb2007-04-10 09:27:04 -04002103
2104 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2105 &new_root_item);
2106 BUG_ON(ret);
2107
Chris Masonc5739bb2007-04-10 09:27:04 -04002108 /*
2109 * insert the directory item
2110 */
2111 key.offset = (u64)-1;
2112 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2113 name, namelen,
2114 root->fs_info->sb->s_root->d_inode->i_ino,
2115 &key, 0);
2116
2117 BUG_ON(ret);
2118
2119 ret = btrfs_inc_root_ref(trans, root);
2120 BUG_ON(ret);
2121
2122 ret = btrfs_commit_transaction(trans, root);
2123 BUG_ON(ret);
2124 mutex_unlock(&root->fs_info->fs_mutex);
2125 return 0;
2126}
2127
Chris Mason8352d8a2007-04-12 10:43:05 -04002128static int add_disk(struct btrfs_root *root, char *name, int namelen)
2129{
2130 struct block_device *bdev;
2131 struct btrfs_path *path;
2132 struct super_block *sb = root->fs_info->sb;
2133 struct btrfs_root *dev_root = root->fs_info->dev_root;
2134 struct btrfs_trans_handle *trans;
2135 struct btrfs_device_item *dev_item;
2136 struct btrfs_key key;
2137 u16 item_size;
2138 u64 num_blocks;
2139 u64 new_blocks;
Chris Masonb4100d62007-04-12 12:14:00 -04002140 u64 device_id;
Chris Mason8352d8a2007-04-12 10:43:05 -04002141 int ret;
Chris Masonb4100d62007-04-12 12:14:00 -04002142
Chris Mason8352d8a2007-04-12 10:43:05 -04002143printk("adding disk %s\n", name);
2144 path = btrfs_alloc_path();
2145 if (!path)
2146 return -ENOMEM;
2147 num_blocks = btrfs_super_total_blocks(root->fs_info->disk_super);
2148 bdev = open_bdev_excl(name, O_RDWR, sb);
2149 if (IS_ERR(bdev)) {
2150 ret = PTR_ERR(bdev);
2151printk("open bdev excl failed ret %d\n", ret);
2152 goto out_nolock;
2153 }
2154 set_blocksize(bdev, sb->s_blocksize);
2155 new_blocks = bdev->bd_inode->i_size >> sb->s_blocksize_bits;
2156 key.objectid = num_blocks;
2157 key.offset = new_blocks;
2158 key.flags = 0;
2159 btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY);
2160
2161 mutex_lock(&dev_root->fs_info->fs_mutex);
2162 trans = btrfs_start_transaction(dev_root, 1);
2163 item_size = sizeof(*dev_item) + namelen;
2164printk("insert empty on %Lu %Lu %u size %d\n", num_blocks, new_blocks, key.flags, item_size);
2165 ret = btrfs_insert_empty_item(trans, dev_root, path, &key, item_size);
2166 if (ret) {
2167printk("insert failed %d\n", ret);
2168 close_bdev_excl(bdev);
2169 if (ret > 0)
2170 ret = -EEXIST;
2171 goto out;
2172 }
2173 dev_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
2174 path->slots[0], struct btrfs_device_item);
2175 btrfs_set_device_pathlen(dev_item, namelen);
2176 memcpy(dev_item + 1, name, namelen);
Chris Masonb4100d62007-04-12 12:14:00 -04002177
2178 device_id = btrfs_super_last_device_id(root->fs_info->disk_super) + 1;
2179 btrfs_set_super_last_device_id(root->fs_info->disk_super, device_id);
2180 btrfs_set_device_id(dev_item, device_id);
Chris Mason8352d8a2007-04-12 10:43:05 -04002181 mark_buffer_dirty(path->nodes[0]);
2182
Chris Masonb4100d62007-04-12 12:14:00 -04002183 ret = btrfs_insert_dev_radix(root, bdev, device_id, num_blocks,
2184 new_blocks);
Chris Mason8352d8a2007-04-12 10:43:05 -04002185
2186 if (!ret) {
2187 btrfs_set_super_total_blocks(root->fs_info->disk_super,
2188 num_blocks + new_blocks);
2189 i_size_write(root->fs_info->btree_inode,
2190 (num_blocks + new_blocks) <<
2191 root->fs_info->btree_inode->i_blkbits);
2192 }
2193
2194out:
2195 ret = btrfs_commit_transaction(trans, dev_root);
2196 BUG_ON(ret);
2197 mutex_unlock(&root->fs_info->fs_mutex);
2198out_nolock:
2199 btrfs_free_path(path);
2200
2201 return ret;
2202}
2203
Chris Masonc5739bb2007-04-10 09:27:04 -04002204static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
2205 cmd, unsigned long arg)
2206{
2207 struct btrfs_root *root = BTRFS_I(inode)->root;
2208 struct btrfs_ioctl_vol_args vol_args;
Chris Mason8352d8a2007-04-12 10:43:05 -04002209 int ret = 0;
Chris Masonc5739bb2007-04-10 09:27:04 -04002210 int namelen;
Chris Mason2619ba12007-04-10 16:58:11 -04002211 struct btrfs_path *path;
2212 u64 root_dirid;
Chris Masonc5739bb2007-04-10 09:27:04 -04002213
Chris Masonc5739bb2007-04-10 09:27:04 -04002214 switch (cmd) {
2215 case BTRFS_IOC_SNAP_CREATE:
2216 if (copy_from_user(&vol_args,
2217 (struct btrfs_ioctl_vol_args __user *)arg,
2218 sizeof(vol_args)))
2219 return -EFAULT;
2220 namelen = strlen(vol_args.name);
2221 if (namelen > BTRFS_VOL_NAME_MAX)
2222 return -EINVAL;
Chris Mason2619ba12007-04-10 16:58:11 -04002223 path = btrfs_alloc_path();
2224 if (!path)
2225 return -ENOMEM;
Chris Mason2d13d8d2007-04-10 20:07:20 -04002226 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
Chris Mason2619ba12007-04-10 16:58:11 -04002227 mutex_lock(&root->fs_info->fs_mutex);
2228 ret = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2229 path, root_dirid,
2230 vol_args.name, namelen, 0);
2231 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2d13d8d2007-04-10 20:07:20 -04002232 btrfs_free_path(path);
Chris Mason2619ba12007-04-10 16:58:11 -04002233 if (ret == 0)
2234 return -EEXIST;
2235
2236 if (root == root->fs_info->tree_root)
2237 ret = create_subvol(root, vol_args.name, namelen);
2238 else
2239 ret = create_snapshot(root, vol_args.name, namelen);
Chris Masonc5739bb2007-04-10 09:27:04 -04002240 WARN_ON(ret);
2241 break;
Chris Mason8352d8a2007-04-12 10:43:05 -04002242 case BTRFS_IOC_ADD_DISK:
2243 if (copy_from_user(&vol_args,
2244 (struct btrfs_ioctl_vol_args __user *)arg,
2245 sizeof(vol_args)))
2246 return -EFAULT;
2247 namelen = strlen(vol_args.name);
2248 if (namelen > BTRFS_VOL_NAME_MAX)
2249 return -EINVAL;
2250 vol_args.name[namelen] = '\0';
2251 ret = add_disk(root, vol_args.name, namelen);
2252 break;
Chris Masonc5739bb2007-04-10 09:27:04 -04002253 default:
2254 return -ENOTTY;
2255 }
Chris Mason8352d8a2007-04-12 10:43:05 -04002256 return ret;
Chris Masonc5739bb2007-04-10 09:27:04 -04002257}
2258
Chris Mason2c90e5d2007-04-02 10:50:19 -04002259static struct kmem_cache *btrfs_inode_cachep;
2260struct kmem_cache *btrfs_trans_handle_cachep;
2261struct kmem_cache *btrfs_transaction_cachep;
2262struct kmem_cache *btrfs_bit_radix_cachep;
2263struct kmem_cache *btrfs_path_cachep;
2264
2265/*
2266 * Called inside transaction, so use GFP_NOFS
2267 */
2268static struct inode *btrfs_alloc_inode(struct super_block *sb)
2269{
2270 struct btrfs_inode *ei;
2271
2272 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2273 if (!ei)
2274 return NULL;
Chris Mason2c90e5d2007-04-02 10:50:19 -04002275 return &ei->vfs_inode;
2276}
2277
2278static void btrfs_destroy_inode(struct inode *inode)
2279{
Chris Mason2c90e5d2007-04-02 10:50:19 -04002280 WARN_ON(!list_empty(&inode->i_dentry));
Chris Mason2c90e5d2007-04-02 10:50:19 -04002281 WARN_ON(inode->i_data.nrpages);
2282
Chris Mason2c90e5d2007-04-02 10:50:19 -04002283 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2284}
2285
2286static void init_once(void * foo, struct kmem_cache * cachep,
2287 unsigned long flags)
2288{
2289 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2290
2291 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
2292 SLAB_CTOR_CONSTRUCTOR) {
2293 inode_init_once(&ei->vfs_inode);
2294 }
2295}
2296
2297static int init_inodecache(void)
2298{
2299 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
2300 sizeof(struct btrfs_inode),
2301 0, (SLAB_RECLAIM_ACCOUNT|
2302 SLAB_MEM_SPREAD),
2303 init_once, NULL);
2304 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
2305 sizeof(struct btrfs_trans_handle),
2306 0, (SLAB_RECLAIM_ACCOUNT|
2307 SLAB_MEM_SPREAD),
2308 NULL, NULL);
2309 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
2310 sizeof(struct btrfs_transaction),
2311 0, (SLAB_RECLAIM_ACCOUNT|
2312 SLAB_MEM_SPREAD),
2313 NULL, NULL);
2314 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
2315 sizeof(struct btrfs_transaction),
2316 0, (SLAB_RECLAIM_ACCOUNT|
2317 SLAB_MEM_SPREAD),
2318 NULL, NULL);
2319 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
2320 256,
2321 0, (SLAB_RECLAIM_ACCOUNT|
2322 SLAB_MEM_SPREAD |
2323 SLAB_DESTROY_BY_RCU),
2324 NULL, NULL);
2325 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
2326 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
2327 return -ENOMEM;
2328 return 0;
2329}
2330
2331static void destroy_inodecache(void)
2332{
2333 kmem_cache_destroy(btrfs_inode_cachep);
2334 kmem_cache_destroy(btrfs_trans_handle_cachep);
2335 kmem_cache_destroy(btrfs_transaction_cachep);
2336 kmem_cache_destroy(btrfs_bit_radix_cachep);
2337 kmem_cache_destroy(btrfs_path_cachep);
2338}
2339
Chris Mason2e635a22007-03-21 11:12:56 -04002340static int btrfs_get_sb(struct file_system_type *fs_type,
2341 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2342{
2343 return get_sb_bdev(fs_type, flags, dev_name, data,
2344 btrfs_fill_super, mnt);
2345}
2346
2347static struct file_system_type btrfs_fs_type = {
2348 .owner = THIS_MODULE,
2349 .name = "btrfs",
2350 .get_sb = btrfs_get_sb,
2351 .kill_sb = kill_block_super,
2352 .fs_flags = FS_REQUIRES_DEV,
2353};
2354
Chris Masone20d96d2007-03-22 12:13:20 -04002355static struct super_operations btrfs_super_ops = {
2356 .statfs = simple_statfs,
Chris Mason134e9732007-03-25 13:44:56 -04002357 .delete_inode = btrfs_delete_inode,
Chris Masone20d96d2007-03-22 12:13:20 -04002358 .put_super = btrfs_put_super,
2359 .read_inode = btrfs_read_locked_inode,
Chris Masond5719762007-03-23 10:01:08 -04002360 .write_super = btrfs_write_super,
2361 .sync_fs = btrfs_sync_fs,
Chris Mason4730a4b2007-03-26 12:00:39 -04002362 .write_inode = btrfs_write_inode,
Chris Mason2c90e5d2007-04-02 10:50:19 -04002363 .alloc_inode = btrfs_alloc_inode,
2364 .destroy_inode = btrfs_destroy_inode,
Chris Masone20d96d2007-03-22 12:13:20 -04002365};
2366
2367static struct inode_operations btrfs_dir_inode_operations = {
2368 .lookup = btrfs_lookup,
Chris Masond5719762007-03-23 10:01:08 -04002369 .create = btrfs_create,
Chris Mason134e9732007-03-25 13:44:56 -04002370 .unlink = btrfs_unlink,
Chris Masonf7922032007-03-25 20:17:36 -04002371 .mkdir = btrfs_mkdir,
Chris Mason5f443fd2007-03-27 13:42:32 -04002372 .rmdir = btrfs_rmdir,
Chris Masone20d96d2007-03-22 12:13:20 -04002373};
2374
Chris Masond6e4a422007-04-06 15:37:36 -04002375static struct inode_operations btrfs_dir_ro_inode_operations = {
2376 .lookup = btrfs_lookup,
2377};
2378
Chris Masone20d96d2007-03-22 12:13:20 -04002379static struct file_operations btrfs_dir_file_operations = {
2380 .llseek = generic_file_llseek,
2381 .read = generic_read_dir,
2382 .readdir = btrfs_readdir,
Chris Masonc5739bb2007-04-10 09:27:04 -04002383 .ioctl = btrfs_ioctl,
Chris Masone20d96d2007-03-22 12:13:20 -04002384};
2385
Chris Masondee26a92007-03-26 16:00:06 -04002386static struct address_space_operations btrfs_aops = {
2387 .readpage = btrfs_readpage,
Chris Masondee26a92007-03-26 16:00:06 -04002388 .writepage = btrfs_writepage,
2389 .sync_page = block_sync_page,
2390 .prepare_write = btrfs_prepare_write,
Chris Mason75dfe392007-03-29 11:56:46 -04002391 .commit_write = btrfs_commit_write,
Chris Masondee26a92007-03-26 16:00:06 -04002392};
2393
2394static struct inode_operations btrfs_file_inode_operations = {
Chris Masonf4b9aa82007-03-27 11:05:53 -04002395 .truncate = btrfs_truncate,
Chris Masondee26a92007-03-26 16:00:06 -04002396};
2397
2398static struct file_operations btrfs_file_operations = {
2399 .llseek = generic_file_llseek,
2400 .read = do_sync_read,
Chris Masone8f05c42007-04-04 14:30:09 -04002401 .aio_read = btrfs_file_aio_read,
2402 .write = btrfs_file_write,
Chris Masondee26a92007-03-26 16:00:06 -04002403 .mmap = generic_file_mmap,
2404 .open = generic_file_open,
Chris Masonc5739bb2007-04-10 09:27:04 -04002405 .ioctl = btrfs_ioctl,
Chris Masondee26a92007-03-26 16:00:06 -04002406};
Chris Masone20d96d2007-03-22 12:13:20 -04002407
Chris Mason2e635a22007-03-21 11:12:56 -04002408static int __init init_btrfs_fs(void)
2409{
Chris Mason2c90e5d2007-04-02 10:50:19 -04002410 int err;
Chris Mason2e635a22007-03-21 11:12:56 -04002411 printk("btrfs loaded!\n");
Chris Mason2c90e5d2007-04-02 10:50:19 -04002412 err = init_inodecache();
2413 if (err)
2414 return err;
Chris Masond6e4a422007-04-06 15:37:36 -04002415 kset_set_kset_s(&btrfs_subsys, fs_subsys);
2416 err = subsystem_register(&btrfs_subsys);
2417 if (err)
2418 goto out;
Chris Mason2e635a22007-03-21 11:12:56 -04002419 return register_filesystem(&btrfs_fs_type);
Chris Masond6e4a422007-04-06 15:37:36 -04002420out:
2421 destroy_inodecache();
2422 return err;
Chris Mason2e635a22007-03-21 11:12:56 -04002423}
2424
2425static void __exit exit_btrfs_fs(void)
2426{
Chris Mason2c90e5d2007-04-02 10:50:19 -04002427 destroy_inodecache();
Chris Mason2e635a22007-03-21 11:12:56 -04002428 unregister_filesystem(&btrfs_fs_type);
Chris Masond6e4a422007-04-06 15:37:36 -04002429 subsystem_unregister(&btrfs_subsys);
Chris Mason2e635a22007-03-21 11:12:56 -04002430 printk("btrfs unloaded\n");
2431}
2432
2433module_init(init_btrfs_fs)
2434module_exit(exit_btrfs_fs)
2435
2436MODULE_LICENSE("GPL");