Btrfs: rework csums and extent item ordering
[linux-2.6-block.git] / fs / btrfs / super.c
CommitLineData
2e635a27 1#include <linux/module.h>
e20d96d6 2#include <linux/buffer_head.h>
2e635a27
CM
3#include <linux/fs.h>
4#include <linux/pagemap.h>
5#include <linux/highmem.h>
6#include <linux/time.h>
7#include <linux/init.h>
8#include <linux/string.h>
9#include <linux/smp_lock.h>
10#include <linux/backing-dev.h>
dee26a9f 11#include <linux/mpage.h>
75dfe396
CM
12#include <linux/swap.h>
13#include <linux/writeback.h>
2e635a27 14#include "ctree.h"
e20d96d6 15#include "disk-io.h"
d5719762 16#include "transaction.h"
2c90e5d6 17#include "btrfs_inode.h"
c5739bba 18#include "ioctl.h"
2e635a27 19
d6e4a428
CM
20void 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
c5739bba
CM
31struct btrfs_iget_args {
32 u64 ino;
33 struct btrfs_root *root;
34};
35
d6e4a428
CM
36decl_subsys(btrfs, &btrfs_fsinfo_ktype, NULL);
37
2e635a27 38#define BTRFS_SUPER_MAGIC 0x9123682E
e20d96d6
CM
39
40static struct inode_operations btrfs_dir_inode_operations;
d6e4a428 41static struct inode_operations btrfs_dir_ro_inode_operations;
e20d96d6
CM
42static struct super_operations btrfs_super_ops;
43static struct file_operations btrfs_dir_file_operations;
dee26a9f
CM
44static struct inode_operations btrfs_file_inode_operations;
45static struct address_space_operations btrfs_aops;
46static struct file_operations btrfs_file_operations;
e20d96d6 47
e20d96d6 48static void btrfs_read_locked_inode(struct inode *inode)
2e635a27 49{
5caf2a00 50 struct btrfs_path *path;
e20d96d6 51 struct btrfs_inode_item *inode_item;
d6e4a428
CM
52 struct btrfs_root *root = BTRFS_I(inode)->root;
53 struct btrfs_key location;
e20d96d6 54 int ret;
f4b9aa8d 55
5caf2a00
CM
56 path = btrfs_alloc_path();
57 BUG_ON(!path);
58 btrfs_init_path(path);
f4b9aa8d
CM
59 mutex_lock(&root->fs_info->fs_mutex);
60
d6e4a428
CM
61 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
62 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
e20d96d6 63 if (ret) {
7cfcc17e 64 btrfs_free_path(path);
d6e4a428 65 goto make_bad;
e20d96d6 66 }
5caf2a00
CM
67 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
68 path->slots[0],
e20d96d6 69 struct btrfs_inode_item);
2e635a27 70
e20d96d6
CM
71 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);
5caf2a00 84
5caf2a00
CM
85 btrfs_free_path(path);
86 inode_item = NULL;
87
f4b9aa8d 88 mutex_unlock(&root->fs_info->fs_mutex);
1b05da2e 89
e20d96d6
CM
90 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:
dee26a9f
CM
98 inode->i_mapping->a_ops = &btrfs_aops;
99 inode->i_fop = &btrfs_file_operations;
100 inode->i_op = &btrfs_file_inode_operations;
e20d96d6
CM
101 break;
102 case S_IFDIR:
e20d96d6 103 inode->i_fop = &btrfs_dir_file_operations;
d6e4a428
CM
104 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;
e20d96d6
CM
108 break;
109 case S_IFLNK:
e20d96d6
CM
110 // inode->i_op = &page_symlink_inode_operations;
111 break;
2e635a27 112 }
e20d96d6 113 return;
d6e4a428
CM
114
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);
2e635a27
CM
120}
121
5f443fd2
CM
122static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
123 struct btrfs_root *root,
124 struct inode *dir,
125 struct dentry *dentry)
134e9731 126{
5caf2a00 127 struct btrfs_path *path;
134e9731
CM
128 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
5caf2a00
CM
134 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,
134e9731
CM
138 name, name_len, -1);
139 if (ret < 0)
140 goto err;
141 if (ret > 0) {
142 ret = -ENOENT;
143 goto err;
144 }
5caf2a00 145 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
134e9731 146 struct btrfs_dir_item);
d6e4a428 147 objectid = btrfs_disk_key_objectid(&di->location);
134e9731 148
5f26f772
CM
149 ret = btrfs_del_item(trans, root, path);
150 BUG_ON(ret);
151
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);
5caf2a00 156 ret = btrfs_del_item(trans, root, path);
134e9731
CM
157 BUG_ON(ret);
158 dentry->d_inode->i_ctime = dir->i_ctime;
159err:
5caf2a00
CM
160 btrfs_release_path(root, path);
161 btrfs_free_path(path);
d4dbff95 162 if (ret == 0) {
134e9731 163 inode_dec_link_count(dentry->d_inode);
5f26f772 164 dir->i_size -= name_len * 2;
d4dbff95
CM
165 mark_inode_dirty(dir);
166 }
134e9731
CM
167 return ret;
168}
169
5f443fd2
CM
170static 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
d6e4a428 176 root = BTRFS_I(dir)->root;
5f443fd2
CM
177 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;
d6e4a428 190 struct btrfs_root *root = BTRFS_I(dir)->root;
5caf2a00 191 struct btrfs_path *path;
5f443fd2
CM
192 struct btrfs_key key;
193 struct btrfs_trans_handle *trans;
5f26f772
CM
194 struct btrfs_key found_key;
195 int found_type;
5f443fd2 196 struct btrfs_leaf *leaf;
5f26f772 197 char *goodnames = "..";
5f443fd2 198
5caf2a00
CM
199 path = btrfs_alloc_path();
200 BUG_ON(!path);
201 btrfs_init_path(path);
5f443fd2
CM
202 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;
5f26f772
CM
206 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);
5f443fd2 236
5f26f772
CM
237 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
238 break;
239 btrfs_release_path(root, path);
5f443fd2 240 }
5f26f772 241 ret = 0;
5caf2a00 242 btrfs_release_path(root, path);
5f443fd2
CM
243
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:
7cfcc17e
CM
250 btrfs_release_path(root, path);
251 btrfs_free_path(path);
5f443fd2
CM
252 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
134e9731
CM
259static int btrfs_free_inode(struct btrfs_trans_handle *trans,
260 struct btrfs_root *root,
261 struct inode *inode)
262{
5caf2a00 263 struct btrfs_path *path;
134e9731 264 int ret;
5caf2a00 265
134e9731 266 clear_inode(inode);
5caf2a00
CM
267
268 path = btrfs_alloc_path();
269 BUG_ON(!path);
270 btrfs_init_path(path);
d6e4a428
CM
271 ret = btrfs_lookup_inode(trans, root, path,
272 &BTRFS_I(inode)->location, -1);
134e9731 273 BUG_ON(ret);
5caf2a00 274 ret = btrfs_del_item(trans, root, path);
134e9731 275 BUG_ON(ret);
5caf2a00 276 btrfs_free_path(path);
134e9731
CM
277 return ret;
278}
279
f4b9aa8d
CM
280static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
281 struct btrfs_root *root,
282 struct inode *inode)
283{
284 int ret;
5caf2a00 285 struct btrfs_path *path;
f4b9aa8d
CM
286 struct btrfs_key key;
287 struct btrfs_disk_key *found_key;
288 struct btrfs_leaf *leaf;
f254e52c
CM
289 struct btrfs_file_extent_item *fi = NULL;
290 u64 extent_start = 0;
291 u64 extent_num_blocks = 0;
292 int found_extent;
f4b9aa8d 293
5caf2a00
CM
294 path = btrfs_alloc_path();
295 BUG_ON(!path);
f4b9aa8d
CM
296 /* 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;
d4dbff95
CM
300 /*
301 * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys
302 * or extent data
303 */
f254e52c 304 btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY);
f4b9aa8d 305 while(1) {
5caf2a00
CM
306 btrfs_init_path(path);
307 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
f4b9aa8d 308 if (ret < 0) {
f4b9aa8d
CM
309 goto error;
310 }
311 if (ret > 0) {
5caf2a00
CM
312 BUG_ON(path->slots[0] == 0);
313 path->slots[0]--;
f4b9aa8d 314 }
5caf2a00
CM
315 leaf = btrfs_buffer_leaf(path->nodes[0]);
316 found_key = &leaf->items[path->slots[0]].key;
f4b9aa8d
CM
317 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
318 break;
f254e52c 319 if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY &&
d4dbff95 320 btrfs_disk_key_type(found_key) != BTRFS_INLINE_DATA_KEY &&
f254e52c 321 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
f4b9aa8d 322 break;
f4b9aa8d
CM
323 if (btrfs_disk_key_offset(found_key) < inode->i_size)
324 break;
f254e52c 325 if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
5caf2a00
CM
326 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
327 path->slots[0],
f254e52c
CM
328 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 }
5caf2a00 338 ret = btrfs_del_item(trans, root, path);
f4b9aa8d 339 BUG_ON(ret);
5caf2a00 340 btrfs_release_path(root, path);
f254e52c
CM
341 if (found_extent) {
342 ret = btrfs_free_extent(trans, root, extent_start,
343 extent_num_blocks, 0);
344 BUG_ON(ret);
345 }
f4b9aa8d 346 }
f4b9aa8d
CM
347 ret = 0;
348error:
5caf2a00
CM
349 btrfs_release_path(root, path);
350 btrfs_free_path(path);
f4b9aa8d
CM
351 return ret;
352}
353
134e9731
CM
354static void btrfs_delete_inode(struct inode *inode)
355{
356 struct btrfs_trans_handle *trans;
d6e4a428 357 struct btrfs_root *root = BTRFS_I(inode)->root;
f4b9aa8d
CM
358 int ret;
359
134e9731
CM
360 truncate_inode_pages(&inode->i_data, 0);
361 if (is_bad_inode(inode)) {
362 goto no_delete;
363 }
364 inode->i_size = 0;
134e9731
CM
365 mutex_lock(&root->fs_info->fs_mutex);
366 trans = btrfs_start_transaction(root, 1);
f4b9aa8d
CM
367 if (S_ISREG(inode->i_mode)) {
368 ret = btrfs_truncate_in_trans(trans, root, inode);
369 BUG_ON(ret);
370 }
134e9731
CM
371 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
e20d96d6 379static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
d6e4a428 380 struct btrfs_key *location)
e20d96d6
CM
381{
382 const char *name = dentry->d_name.name;
383 int namelen = dentry->d_name.len;
384 struct btrfs_dir_item *di;
5caf2a00 385 struct btrfs_path *path;
d6e4a428 386 struct btrfs_root *root = BTRFS_I(dir)->root;
e20d96d6
CM
387 int ret;
388
5caf2a00
CM
389 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,
e20d96d6 393 namelen, 0);
5caf2a00 394 if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) {
d6e4a428 395 location->objectid = 0;
2c90e5d6 396 ret = 0;
e20d96d6
CM
397 goto out;
398 }
5caf2a00 399 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
e20d96d6 400 struct btrfs_dir_item);
d6e4a428 401 btrfs_disk_key_to_cpu(location, &di->location);
e20d96d6 402out:
5caf2a00
CM
403 btrfs_release_path(root, path);
404 btrfs_free_path(path);
e20d96d6
CM
405 return ret;
406}
407
d6e4a428
CM
408int 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;
d6e4a428
CM
414
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
0f7d52f4
CM
424 *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;
d6e4a428
CM
429 location->objectid = btrfs_root_dirid(ri);
430 location->flags = 0;
431 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
432 location->offset = 0;
0f7d52f4 433
d6e4a428
CM
434 btrfs_free_path(path);
435 mutex_unlock(&root->fs_info->fs_mutex);
0f7d52f4 436 return 0;
d6e4a428
CM
437}
438
c5739bba
CM
439int 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}
d6e4a428 467
e20d96d6
CM
468static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
469 struct nameidata *nd)
470{
471 struct inode * inode;
d6e4a428
CM
472 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;
e20d96d6
CM
476 int ret;
477
478 if (dentry->d_name.len > BTRFS_NAME_LEN)
479 return ERR_PTR(-ENAMETOOLONG);
22b0ebda 480 mutex_lock(&root->fs_info->fs_mutex);
d6e4a428 481 ret = btrfs_inode_by_name(dir, dentry, &location);
22b0ebda 482 mutex_unlock(&root->fs_info->fs_mutex);
e20d96d6
CM
483 if (ret < 0)
484 return ERR_PTR(ret);
485 inode = NULL;
d6e4a428
CM
486 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);
c5739bba
CM
492 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
493 sub_root);
e20d96d6
CM
494 if (!inode)
495 return ERR_PTR(-EACCES);
d6e4a428 496 if (inode->i_state & I_NEW) {
0f7d52f4 497 if (sub_root != root) {
c5739bba 498printk("adding new root for inode %lu root %p (found %p)\n", inode->i_ino, sub_root, BTRFS_I(inode)->root);
0f7d52f4
CM
499 igrab(inode);
500 sub_root->inode = inode;
501 }
d6e4a428
CM
502 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 }
e20d96d6
CM
508 }
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;
d6e4a428 515 struct btrfs_root *root = BTRFS_I(inode)->root;
e20d96d6
CM
516 struct btrfs_item *item;
517 struct btrfs_dir_item *di;
518 struct btrfs_key key;
5caf2a00 519 struct btrfs_path *path;
e20d96d6
CM
520 int ret;
521 u32 nritems;
522 struct btrfs_leaf *leaf;
523 int slot;
524 int advance;
525 unsigned char d_type = DT_UNKNOWN;
7f5c1516 526 int over = 0;
70b2befd 527 int key_type = BTRFS_DIR_ITEM_KEY;
d6e4a428
CM
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;
22b0ebda 532 mutex_lock(&root->fs_info->fs_mutex);
e20d96d6 533 key.objectid = inode->i_ino;
e20d96d6 534 key.flags = 0;
d6e4a428 535 btrfs_set_key_type(&key, key_type);
e20d96d6 536 key.offset = filp->f_pos;
5caf2a00
CM
537 path = btrfs_alloc_path();
538 btrfs_init_path(path);
539 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1b05da2e 540 if (ret < 0)
e20d96d6 541 goto err;
7f5c1516 542 advance = 0;
e20d96d6 543 while(1) {
5caf2a00 544 leaf = btrfs_buffer_leaf(path->nodes[0]);
e20d96d6 545 nritems = btrfs_header_nritems(&leaf->header);
5caf2a00 546 slot = path->slots[0];
dee26a9f
CM
547 if (advance || slot >= nritems) {
548 if (slot >= nritems -1) {
5caf2a00 549 ret = btrfs_next_leaf(root, path);
e20d96d6
CM
550 if (ret)
551 break;
5caf2a00 552 leaf = btrfs_buffer_leaf(path->nodes[0]);
e20d96d6 553 nritems = btrfs_header_nritems(&leaf->header);
5caf2a00 554 slot = path->slots[0];
e20d96d6
CM
555 } else {
556 slot++;
5caf2a00 557 path->slots[0]++;
e20d96d6
CM
558 }
559 }
560 advance = 1;
561 item = leaf->items + slot;
e20d96d6
CM
562 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
563 break;
d6e4a428 564 if (btrfs_disk_key_type(&item->key) != key_type)
e20d96d6 565 continue;
7f5c1516
CM
566 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
567 continue;
7fcde0e3 568 filp->f_pos = btrfs_disk_key_offset(&item->key);
dee26a9f 569 advance = 1;
e20d96d6 570 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
e20d96d6
CM
571 over = filldir(dirent, (const char *)(di + 1),
572 btrfs_dir_name_len(di),
573 btrfs_disk_key_offset(&item->key),
d6e4a428 574 btrfs_disk_key_objectid(&di->location), d_type);
7fcde0e3
CM
575 if (over)
576 goto nopos;
e20d96d6 577 }
7fcde0e3
CM
578 filp->f_pos++;
579nopos:
e20d96d6
CM
580 ret = 0;
581err:
5caf2a00
CM
582 btrfs_release_path(root, path);
583 btrfs_free_path(path);
22b0ebda 584 mutex_unlock(&root->fs_info->fs_mutex);
e20d96d6
CM
585 return ret;
586}
587
588static void btrfs_put_super (struct super_block * sb)
589{
590 struct btrfs_root *root = btrfs_sb(sb);
591 int ret;
592
593 ret = close_ctree(root);
594 if (ret) {
595 printk("close ctree returns %d\n", ret);
596 }
597 sb->s_fs_info = NULL;
598}
2e635a27
CM
599
600static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
601{
602 struct inode * inode;
e20d96d6
CM
603 struct dentry * root_dentry;
604 struct btrfs_super_block *disk_super;
0f7d52f4 605 struct btrfs_root *tree_root;
d6e4a428 606 struct btrfs_inode *bi;
2e635a27
CM
607
608 sb->s_maxbytes = MAX_LFS_FILESIZE;
2e635a27 609 sb->s_magic = BTRFS_SUPER_MAGIC;
e20d96d6 610 sb->s_op = &btrfs_super_ops;
2e635a27 611 sb->s_time_gran = 1;
e20d96d6 612
0f7d52f4 613 tree_root = open_ctree(sb);
d98237b3 614
0f7d52f4 615 if (!tree_root) {
e20d96d6
CM
616 printk("btrfs: open_ctree failed\n");
617 return -EIO;
618 }
0f7d52f4
CM
619 sb->s_fs_info = tree_root;
620 disk_super = tree_root->fs_info->disk_super;
e20d96d6
CM
621 printk("read in super total blocks %Lu root %Lu\n",
622 btrfs_super_total_blocks(disk_super),
623 btrfs_super_root_dir(disk_super));
624
c5739bba
CM
625 inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
626 tree_root);
d6e4a428
CM
627 bi = BTRFS_I(inode);
628 bi->location.objectid = inode->i_ino;
629 bi->location.offset = 0;
630 bi->location.flags = 0;
0f7d52f4 631 bi->root = tree_root;
d6e4a428
CM
632 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
633
2e635a27
CM
634 if (!inode)
635 return -ENOMEM;
e20d96d6
CM
636 if (inode->i_state & I_NEW) {
637 btrfs_read_locked_inode(inode);
638 unlock_new_inode(inode);
639 }
2e635a27 640
e20d96d6
CM
641 root_dentry = d_alloc_root(inode);
642 if (!root_dentry) {
2e635a27
CM
643 iput(inode);
644 return -ENOMEM;
645 }
e20d96d6
CM
646 sb->s_root = root_dentry;
647
2e635a27
CM
648 return 0;
649}
650
d5719762
CM
651static void fill_inode_item(struct btrfs_inode_item *item,
652 struct inode *inode)
653{
654 btrfs_set_inode_uid(item, inode->i_uid);
655 btrfs_set_inode_gid(item, inode->i_gid);
656 btrfs_set_inode_size(item, inode->i_size);
657 btrfs_set_inode_mode(item, inode->i_mode);
658 btrfs_set_inode_nlink(item, inode->i_nlink);
659 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
660 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
661 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
662 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
663 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
664 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
665 btrfs_set_inode_nblocks(item, inode->i_blocks);
666 btrfs_set_inode_generation(item, inode->i_generation);
667}
668
4730a4bc
CM
669static int btrfs_update_inode(struct btrfs_trans_handle *trans,
670 struct btrfs_root *root,
671 struct inode *inode)
672{
673 struct btrfs_inode_item *inode_item;
5caf2a00 674 struct btrfs_path *path;
4730a4bc
CM
675 int ret;
676
5caf2a00
CM
677 path = btrfs_alloc_path();
678 BUG_ON(!path);
679 btrfs_init_path(path);
d6e4a428
CM
680 ret = btrfs_lookup_inode(trans, root, path,
681 &BTRFS_I(inode)->location, 1);
4730a4bc
CM
682 if (ret) {
683 if (ret > 0)
684 ret = -ENOENT;
685 goto failed;
686 }
687
5caf2a00
CM
688 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
689 path->slots[0],
4730a4bc
CM
690 struct btrfs_inode_item);
691
692 fill_inode_item(inode_item, inode);
5caf2a00 693 btrfs_mark_buffer_dirty(path->nodes[0]);
1b05da2e 694 ret = 0;
4730a4bc 695failed:
5caf2a00
CM
696 btrfs_release_path(root, path);
697 btrfs_free_path(path);
1b05da2e 698 return ret;
4730a4bc
CM
699}
700
701static int btrfs_write_inode(struct inode *inode, int wait)
702{
d6e4a428 703 struct btrfs_root *root = BTRFS_I(inode)->root;
4730a4bc
CM
704 struct btrfs_trans_handle *trans;
705 int ret;
706
707 mutex_lock(&root->fs_info->fs_mutex);
708 trans = btrfs_start_transaction(root, 1);
709 ret = btrfs_update_inode(trans, root, inode);
710 if (wait)
711 btrfs_commit_transaction(trans, root);
712 else
713 btrfs_end_transaction(trans, root);
714 mutex_unlock(&root->fs_info->fs_mutex);
715 return ret;
716}
717
d5719762 718static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2619ba1f
CM
719 struct btrfs_root *root,
720 u64 objectid, int mode)
d5719762
CM
721{
722 struct inode *inode;
723 struct btrfs_inode_item inode_item;
1b05da2e 724 struct btrfs_key *location;
d5719762 725 int ret;
d5719762 726
2619ba1f 727 inode = new_inode(root->fs_info->sb);
d5719762
CM
728 if (!inode)
729 return ERR_PTR(-ENOMEM);
730
2619ba1f 731 BTRFS_I(inode)->root = root;
d5719762
CM
732
733 inode->i_uid = current->fsuid;
734 inode->i_gid = current->fsgid;
735 inode->i_mode = mode;
736 inode->i_ino = objectid;
737 inode->i_blocks = 0;
c5739bba 738 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
d5719762 739 fill_inode_item(&inode_item, inode);
1b05da2e
CM
740 location = &BTRFS_I(inode)->location;
741 location->objectid = objectid;
742 location->flags = 0;
743 location->offset = 0;
744 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
d5719762
CM
745
746 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
747 BUG_ON(ret);
748
749 insert_inode_hash(inode);
d5719762
CM
750 return inode;
751}
752
753static int btrfs_add_link(struct btrfs_trans_handle *trans,
754 struct dentry *dentry, struct inode *inode)
755{
756 int ret;
d6e4a428
CM
757 struct btrfs_key key;
758 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
759 key.objectid = inode->i_ino;
760 key.flags = 0;
761 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
762 key.offset = 0;
763
764 ret = btrfs_insert_dir_item(trans, root,
d5719762
CM
765 dentry->d_name.name, dentry->d_name.len,
766 dentry->d_parent->d_inode->i_ino,
d6e4a428 767 &key, 0);
4730a4bc 768 if (ret == 0) {
5f26f772 769 dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
d6e4a428 770 ret = btrfs_update_inode(trans, root,
4730a4bc
CM
771 dentry->d_parent->d_inode);
772 }
d5719762
CM
773 return ret;
774}
775
776static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
777 struct dentry *dentry, struct inode *inode)
778{
779 int err = btrfs_add_link(trans, dentry, inode);
780 if (!err) {
781 d_instantiate(dentry, inode);
782 return 0;
783 }
2c90e5d6
CM
784 if (err > 0)
785 err = -EEXIST;
d5719762
CM
786 return err;
787}
788
789static int btrfs_create(struct inode *dir, struct dentry *dentry,
790 int mode, struct nameidata *nd)
791{
792 struct btrfs_trans_handle *trans;
d6e4a428 793 struct btrfs_root *root = BTRFS_I(dir)->root;
d5719762
CM
794 struct inode *inode;
795 int err;
134e9731 796 int drop_inode = 0;
2619ba1f 797 u64 objectid;
d5719762 798
d561c025 799 mutex_lock(&root->fs_info->fs_mutex);
d5719762 800 trans = btrfs_start_transaction(root, 1);
2619ba1f
CM
801
802 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
803 if (err) {
804 err = -ENOSPC;
805 goto out_unlock;
806 }
807
808 inode = btrfs_new_inode(trans, root, objectid, mode);
d5719762
CM
809 err = PTR_ERR(inode);
810 if (IS_ERR(inode))
d561c025 811 goto out_unlock;
d5719762
CM
812 // FIXME mark the inode dirty
813 err = btrfs_add_nondir(trans, dentry, inode);
134e9731
CM
814 if (err)
815 drop_inode = 1;
dee26a9f
CM
816 else {
817 inode->i_mapping->a_ops = &btrfs_aops;
818 inode->i_fop = &btrfs_file_operations;
819 inode->i_op = &btrfs_file_inode_operations;
820 }
d5719762 821 dir->i_sb->s_dirt = 1;
d561c025 822out_unlock:
22b0ebda 823 btrfs_end_transaction(trans, root);
d561c025 824 mutex_unlock(&root->fs_info->fs_mutex);
2c90e5d6 825
134e9731
CM
826 if (drop_inode) {
827 inode_dec_link_count(inode);
828 iput(inode);
829 }
d5719762
CM
830 return err;
831}
832
f7922033 833static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
2619ba1f
CM
834 struct btrfs_root *root,
835 u64 objectid, u64 dirid)
f7922033 836{
f7922033
CM
837 int ret;
838 char buf[2];
d6e4a428
CM
839 struct btrfs_key key;
840
f7922033
CM
841 buf[0] = '.';
842 buf[1] = '.';
843
2619ba1f 844 key.objectid = objectid;
d6e4a428
CM
845 key.offset = 0;
846 key.flags = 0;
847 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
848
2619ba1f 849 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
d6e4a428 850 &key, 1);
f7922033
CM
851 if (ret)
852 goto error;
2619ba1f
CM
853 key.objectid = dirid;
854 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
d6e4a428 855 &key, 1);
4730a4bc
CM
856 if (ret)
857 goto error;
f7922033
CM
858error:
859 return ret;
860}
861
862static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
863{
864 struct inode *inode;
865 struct btrfs_trans_handle *trans;
d6e4a428 866 struct btrfs_root *root = BTRFS_I(dir)->root;
f7922033
CM
867 int err = 0;
868 int drop_on_err = 0;
2619ba1f 869 u64 objectid;
f7922033
CM
870
871 mutex_lock(&root->fs_info->fs_mutex);
872 trans = btrfs_start_transaction(root, 1);
873 if (IS_ERR(trans)) {
874 err = PTR_ERR(trans);
875 goto out_unlock;
876 }
2619ba1f
CM
877
878 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
879 if (err) {
880 err = -ENOSPC;
881 goto out_unlock;
882 }
883
884 inode = btrfs_new_inode(trans, root, objectid, S_IFDIR | mode);
f7922033
CM
885 if (IS_ERR(inode)) {
886 err = PTR_ERR(inode);
887 goto out_fail;
888 }
889 drop_on_err = 1;
890 inode->i_op = &btrfs_dir_inode_operations;
891 inode->i_fop = &btrfs_dir_file_operations;
892
2619ba1f
CM
893 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
894 if (err)
895 goto out_fail;
896
897 inode->i_size = 6;
898 err = btrfs_update_inode(trans, root, inode);
f7922033
CM
899 if (err)
900 goto out_fail;
901 err = btrfs_add_link(trans, dentry, inode);
902 if (err)
903 goto out_fail;
904 d_instantiate(dentry, inode);
f7922033
CM
905 drop_on_err = 0;
906
907out_fail:
908 btrfs_end_transaction(trans, root);
909out_unlock:
910 mutex_unlock(&root->fs_info->fs_mutex);
911 if (drop_on_err)
912 iput(inode);
913 return err;
914}
915
d5719762
CM
916static int btrfs_sync_fs(struct super_block *sb, int wait)
917{
918 struct btrfs_trans_handle *trans;
919 struct btrfs_root *root;
920 int ret;
d98237b3 921 root = btrfs_sb(sb);
df2ce34c 922
d5719762 923 sb->s_dirt = 0;
d561c025 924 if (!wait) {
7cfcc17e 925 filemap_flush(root->fs_info->btree_inode->i_mapping);
d561c025
CM
926 return 0;
927 }
7cfcc17e 928 filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
d561c025 929 mutex_lock(&root->fs_info->fs_mutex);
d5719762
CM
930 trans = btrfs_start_transaction(root, 1);
931 ret = btrfs_commit_transaction(trans, root);
932 sb->s_dirt = 0;
933 BUG_ON(ret);
934printk("btrfs sync_fs\n");
d561c025 935 mutex_unlock(&root->fs_info->fs_mutex);
d5719762
CM
936 return 0;
937}
938
e8f05c45 939#if 0
df24a2b9
CM
940static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
941 struct buffer_head *result, int create)
942{
943 struct btrfs_root *root = btrfs_sb(inode->i_sb);
944 struct btrfs_path *path;
945 struct btrfs_key key;
946 struct btrfs_leaf *leaf;
947 int num_bytes = result->b_size;
948 int item_size;
949 int ret;
950 u64 pos;
951 char *ptr;
952 int copy_size;
953 int err = 0;
954 char *safe_ptr;
955 char *data_ptr;
956
957 path = btrfs_alloc_path();
958 BUG_ON(!path);
959
960 WARN_ON(create);
961 if (create) {
962 return 0;
963 }
964 pos = iblock << inode->i_blkbits;
965 key.objectid = inode->i_ino;
966 key.flags = 0;
967 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
968 ptr = kmap(result->b_page);
969 safe_ptr = ptr;
970 ptr += (pos & (PAGE_CACHE_SIZE -1));
971again:
972 key.offset = pos;
973 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
974 if (ret) {
975 if (ret < 0)
976 err = ret;
977 else
978 err = 0;
979 goto out;
980 }
981 leaf = btrfs_buffer_leaf(path->nodes[0]);
982 item_size = btrfs_item_size(leaf->items + path->slots[0]);
983 copy_size = min(num_bytes, item_size);
984 data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
985 WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
986 memcpy(ptr, data_ptr, copy_size);
987 pos += copy_size;
988 num_bytes -= copy_size;
989 WARN_ON(num_bytes < 0);
990 ptr += copy_size;
991 btrfs_release_path(root, path);
992 if (num_bytes != 0) {
993 if (pos >= i_size_read(inode))
994 memset(ptr, 0, num_bytes);
995 else
996 goto again;
997 }
998 set_buffer_uptodate(result);
999 map_bh(result, inode->i_sb, 0);
1000 err = 0;
1001out:
1002 btrfs_free_path(path);
1003 kunmap(result->b_page);
1004 return err;
1005}
e8f05c45 1006#endif
df24a2b9 1007
75dfe396 1008static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
dee26a9f
CM
1009 struct buffer_head *result, int create)
1010{
1011 int ret;
1012 int err = 0;
1013 u64 blocknr;
1014 u64 extent_start = 0;
1015 u64 extent_end = 0;
1016 u64 objectid = inode->i_ino;
5caf2a00 1017 struct btrfs_path *path;
d6e4a428 1018 struct btrfs_root *root = BTRFS_I(inode)->root;
dee26a9f
CM
1019 struct btrfs_trans_handle *trans = NULL;
1020 struct btrfs_file_extent_item *item;
1021 struct btrfs_leaf *leaf;
1022 struct btrfs_disk_key *found_key;
1023
5caf2a00
CM
1024 path = btrfs_alloc_path();
1025 BUG_ON(!path);
1026 btrfs_init_path(path);
6567e837 1027 if (create) {
dee26a9f 1028 trans = btrfs_start_transaction(root, 1);
6567e837
CM
1029 WARN_ON(1);
1030 }
dee26a9f 1031
5caf2a00 1032 ret = btrfs_lookup_file_extent(trans, root, path,
9773a788 1033 inode->i_ino,
79b2cb1f 1034 iblock << inode->i_blkbits, create);
dee26a9f 1035 if (ret < 0) {
dee26a9f
CM
1036 err = ret;
1037 goto out;
1038 }
1039
1040 if (ret != 0) {
5caf2a00
CM
1041 if (path->slots[0] == 0) {
1042 btrfs_release_path(root, path);
dee26a9f
CM
1043 goto allocate;
1044 }
5caf2a00 1045 path->slots[0]--;
dee26a9f
CM
1046 }
1047
5caf2a00 1048 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
dee26a9f 1049 struct btrfs_file_extent_item);
5caf2a00 1050 leaf = btrfs_buffer_leaf(path->nodes[0]);
dee26a9f
CM
1051 blocknr = btrfs_file_extent_disk_blocknr(item);
1052 blocknr += btrfs_file_extent_offset(item);
1053
48ddc6f4
CM
1054 /* exact match found, use it, FIXME, deal with extents
1055 * other than the page size
1056 */
6567e837 1057 if (0 && ret == 0) {
dee26a9f 1058 err = 0;
79b2cb1f
CM
1059 if (create &&
1060 btrfs_file_extent_generation(item) != trans->transid) {
48ddc6f4
CM
1061 struct btrfs_key ins;
1062 ret = btrfs_alloc_extent(trans, root, 1,
1063 blocknr, (u64)-1, &ins);
1064 BUG_ON(ret);
1065 btrfs_set_file_extent_disk_blocknr(item, ins.objectid);
1066 mark_buffer_dirty(path->nodes[0]);
1067 ret = btrfs_free_extent(trans, root,
1068 blocknr, 1, 0);
1069 BUG_ON(ret);
1070 blocknr = ins.objectid;
1071
1072 }
8352d8a4 1073 btrfs_map_bh_to_logical(root, result, blocknr);
dee26a9f
CM
1074 goto out;
1075 }
1076
1077 /* are we inside the extent that was found? */
5caf2a00 1078 found_key = &leaf->items[path->slots[0]].key;
dee26a9f
CM
1079 if (btrfs_disk_key_objectid(found_key) != objectid ||
1080 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
1081 extent_end = 0;
1082 extent_start = 0;
5caf2a00 1083 btrfs_release_path(root, path);
dee26a9f
CM
1084 goto allocate;
1085 }
1086
5caf2a00 1087 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
9773a788 1088 extent_start = extent_start >> inode->i_blkbits;
dee26a9f 1089 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
dee26a9f
CM
1090 if (iblock >= extent_start && iblock < extent_end) {
1091 err = 0;
8352d8a4
CM
1092 btrfs_map_bh_to_logical(root, result, blocknr + iblock -
1093 extent_start);
dee26a9f
CM
1094 goto out;
1095 }
1096allocate:
1097 /* ok, create a new extent */
1098 if (!create) {
1099 err = 0;
1100 goto out;
1101 }
b18c6685 1102#if 0
9773a788
CM
1103 ret = btrfs_alloc_file_extent(trans, root, objectid,
1104 iblock << inode->i_blkbits,
dee26a9f
CM
1105 1, extent_end, &blocknr);
1106 if (ret) {
1107 err = ret;
1108 goto out;
1109 }
f4b9aa8d
CM
1110 inode->i_blocks += inode->i_sb->s_blocksize >> 9;
1111 set_buffer_new(result);
dee26a9f
CM
1112 map_bh(result, inode->i_sb, blocknr);
1113
8352d8a4 1114 btrfs_map_bh_to_logical(root, result, blocknr);
b18c6685 1115#endif
dee26a9f 1116out:
5caf2a00
CM
1117 btrfs_release_path(root, path);
1118 btrfs_free_path(path);
dee26a9f
CM
1119 if (trans)
1120 btrfs_end_transaction(trans, root);
75dfe396
CM
1121 return err;
1122}
1123
1124static int btrfs_get_block(struct inode *inode, sector_t iblock,
1125 struct buffer_head *result, int create)
1126{
1127 int err;
d6e4a428 1128 struct btrfs_root *root = BTRFS_I(inode)->root;
75dfe396 1129 mutex_lock(&root->fs_info->fs_mutex);
e8f05c45
CM
1130 err = btrfs_get_block_lock(inode, iblock, result, create);
1131 // err = btrfs_get_block_inline(inode, iblock, result, create);
dee26a9f
CM
1132 mutex_unlock(&root->fs_info->fs_mutex);
1133 return err;
1134}
1135
1136static int btrfs_prepare_write(struct file *file, struct page *page,
1137 unsigned from, unsigned to)
1138{
6407bf6d 1139 return nobh_prepare_write(page, from, to, btrfs_get_block);
dee26a9f 1140}
75dfe396
CM
1141static int btrfs_commit_write(struct file *file, struct page *page,
1142 unsigned from, unsigned to)
1143{
75dfe396
CM
1144 return nobh_commit_write(file, page, from, to);
1145}
dee26a9f 1146
d561c025
CM
1147static void btrfs_write_super(struct super_block *sb)
1148{
1149 btrfs_sync_fs(sb, 1);
1150}
1151
dee26a9f
CM
1152static int btrfs_readpage(struct file *file, struct page *page)
1153{
1154 return mpage_readpage(page, btrfs_get_block);
1155}
1156
dee26a9f
CM
1157static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1158{
6407bf6d 1159 return nobh_writepage(page, btrfs_get_block, wbc);
dee26a9f 1160}
d561c025 1161
f4b9aa8d
CM
1162static void btrfs_truncate(struct inode *inode)
1163{
d6e4a428 1164 struct btrfs_root *root = BTRFS_I(inode)->root;
f4b9aa8d
CM
1165 int ret;
1166 struct btrfs_trans_handle *trans;
1167
1168 if (!S_ISREG(inode->i_mode))
1169 return;
1170 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1171 return;
1172
e8f05c45 1173 nobh_truncate_page(inode->i_mapping, inode->i_size);
f4b9aa8d
CM
1174
1175 /* FIXME, add redo link to tree so we don't leak on crash */
1176 mutex_lock(&root->fs_info->fs_mutex);
1177 trans = btrfs_start_transaction(root, 1);
1178 ret = btrfs_truncate_in_trans(trans, root, inode);
1179 BUG_ON(ret);
1180 ret = btrfs_end_transaction(trans, root);
1181 BUG_ON(ret);
1182 mutex_unlock(&root->fs_info->fs_mutex);
1183 mark_inode_dirty(inode);
1184}
1185
75dfe396
CM
1186static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
1187 struct page **prepared_pages,
1188 const char __user * buf)
1189{
1190 long page_fault = 0;
1191 int i;
1192 int offset = pos & (PAGE_CACHE_SIZE - 1);
1193
1194 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
1195 size_t count = min_t(size_t,
1196 PAGE_CACHE_SIZE - offset, write_bytes);
1197 struct page *page = prepared_pages[i];
1198 fault_in_pages_readable(buf, count);
1199
1200 /* Copy data from userspace to the current page */
1201 kmap(page);
1202 page_fault = __copy_from_user(page_address(page) + offset,
1203 buf, count);
1204 /* Flush processor's dcache for this page */
1205 flush_dcache_page(page);
1206 kunmap(page);
1207 buf += count;
1208 write_bytes -= count;
1209
1210 if (page_fault)
1211 break;
1212 }
1213 return page_fault ? -EFAULT : 0;
1214}
1215
1216static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1217{
1218 size_t i;
1219 for (i = 0; i < num_pages; i++) {
1220 if (!pages[i])
1221 break;
1222 unlock_page(pages[i]);
1223 mark_page_accessed(pages[i]);
1224 page_cache_release(pages[i]);
1225 }
1226}
1227static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1228 struct btrfs_root *root,
1229 struct file *file,
1230 struct page **pages,
1231 size_t num_pages,
1232 loff_t pos,
1233 size_t write_bytes)
1234{
1235 int i;
1236 int offset;
1237 int err = 0;
1238 int ret;
1239 int this_write;
f254e52c 1240 struct inode *inode = file->f_path.dentry->d_inode;
75dfe396
CM
1241
1242 for (i = 0; i < num_pages; i++) {
1243 offset = pos & (PAGE_CACHE_SIZE -1);
1244 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
f254e52c
CM
1245 /* FIXME, one block at a time */
1246
1247 mutex_lock(&root->fs_info->fs_mutex);
1248 trans = btrfs_start_transaction(root, 1);
1249 btrfs_csum_file_block(trans, root, inode->i_ino,
1250 pages[i]->index << PAGE_CACHE_SHIFT,
1251 kmap(pages[i]), PAGE_CACHE_SIZE);
1252 kunmap(pages[i]);
1253 SetPageChecked(pages[i]);
1254 ret = btrfs_end_transaction(trans, root);
1255 BUG_ON(ret);
1256 mutex_unlock(&root->fs_info->fs_mutex);
1257
75dfe396
CM
1258 ret = nobh_commit_write(file, pages[i], offset,
1259 offset + this_write);
1260 pos += this_write;
1261 if (ret) {
1262 err = ret;
1263 goto failed;
1264 }
1265 WARN_ON(this_write > write_bytes);
1266 write_bytes -= this_write;
1267 }
1268failed:
1269 return err;
1270}
1271
b18c6685
CM
1272static int drop_extents(struct btrfs_trans_handle *trans,
1273 struct btrfs_root *root,
1274 struct inode *inode,
1275 u64 start, u64 end)
1276{
1277 int ret;
1278 struct btrfs_key key;
1279 struct btrfs_leaf *leaf;
1280 int slot;
1281 struct btrfs_file_extent_item *extent;
1282 u64 extent_end;
1283 int keep;
1284 struct btrfs_file_extent_item old;
1285 struct btrfs_path *path;
1286 u64 search_start = start;
1287 int bookend;
1288
1289 path = btrfs_alloc_path();
1290 if (!path)
1291 return -ENOMEM;
1292search_again:
b18c6685
CM
1293 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1294 search_start, -1);
70b2befd 1295 if (ret < 0)
b18c6685 1296 goto out;
70b2befd
CM
1297 if (ret > 0) {
1298 if (path->slots[0] == 0) {
1299 ret = -ENOENT;
1300 goto out;
1301 }
1302 path->slots[0]--;
b18c6685
CM
1303 }
1304 while(1) {
1305 keep = 0;
1306 bookend = 0;
1307 leaf = btrfs_buffer_leaf(path->nodes[0]);
1308 slot = path->slots[0];
1309 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
1310
b18c6685
CM
1311 extent = btrfs_item_ptr(leaf, slot,
1312 struct btrfs_file_extent_item);
1313 extent_end = key.offset +
1314 (btrfs_file_extent_num_blocks(extent) <<
1315 inode->i_blkbits);
b18c6685
CM
1316 if (key.offset >= end || key.objectid != inode->i_ino) {
1317 ret = 0;
1318 goto out;
1319 }
1320 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1321 goto next_leaf;
1322
1323 if (end < extent_end && end >= key.offset) {
1324 memcpy(&old, extent, sizeof(old));
1325 ret = btrfs_inc_extent_ref(trans, root,
1326 btrfs_file_extent_disk_blocknr(&old),
1327 btrfs_file_extent_disk_num_blocks(&old));
1328 BUG_ON(ret);
1329 bookend = 1;
1330 }
1331
1332 if (start > key.offset) {
1333 u64 new_num;
1334 /* truncate existing extent */
1335 keep = 1;
1336 WARN_ON(start & (root->blocksize - 1));
1337 new_num = (start - key.offset) >> inode->i_blkbits;
b18c6685 1338 btrfs_set_file_extent_num_blocks(extent, new_num);
b18c6685
CM
1339 mark_buffer_dirty(path->nodes[0]);
1340 }
1341 if (!keep) {
1342 u64 disk_blocknr;
1343 u64 disk_num_blocks;
b18c6685
CM
1344 disk_blocknr = btrfs_file_extent_disk_blocknr(extent);
1345 disk_num_blocks =
1346 btrfs_file_extent_disk_num_blocks(extent);
1347 search_start = key.offset +
1348 (btrfs_file_extent_num_blocks(extent) <<
1349 inode->i_blkbits);
1350 ret = btrfs_del_item(trans, root, path);
1351 BUG_ON(ret);
1352 btrfs_release_path(root, path);
1353
1354 ret = btrfs_free_extent(trans, root, disk_blocknr,
1355 disk_num_blocks, 0);
1356
1357 BUG_ON(ret);
1358 if (!bookend && search_start >= end) {
1359 ret = 0;
1360 goto out;
1361 }
1362 if (!bookend)
1363 goto search_again;
1364 }
1365 if (bookend) {
1366 /* create bookend */
1367 struct btrfs_key ins;
b18c6685
CM
1368 ins.objectid = inode->i_ino;
1369 ins.offset = end;
1370 ins.flags = 0;
1371 btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
1372
1373 btrfs_release_path(root, path);
b18c6685
CM
1374 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1375 sizeof(*extent));
1376 BUG_ON(ret);
1377 extent = btrfs_item_ptr(
1378 btrfs_buffer_leaf(path->nodes[0]),
1379 path->slots[0],
1380 struct btrfs_file_extent_item);
1381 btrfs_set_file_extent_disk_blocknr(extent,
1382 btrfs_file_extent_disk_blocknr(&old));
1383 btrfs_set_file_extent_disk_num_blocks(extent,
1384 btrfs_file_extent_disk_num_blocks(&old));
1385
1386 btrfs_set_file_extent_offset(extent,
1387 btrfs_file_extent_offset(&old) +
1388 ((end - key.offset) >> inode->i_blkbits));
1389 WARN_ON(btrfs_file_extent_num_blocks(&old) <
1390 (end - key.offset) >> inode->i_blkbits);
1391 btrfs_set_file_extent_num_blocks(extent,
1392 btrfs_file_extent_num_blocks(&old) -
1393 ((end - key.offset) >> inode->i_blkbits));
1394
1395 btrfs_set_file_extent_generation(extent,
1396 btrfs_file_extent_generation(&old));
b18c6685
CM
1397 btrfs_mark_buffer_dirty(path->nodes[0]);
1398 ret = 0;
70b2befd 1399 goto out;
b18c6685
CM
1400 }
1401next_leaf:
1402 if (slot >= btrfs_header_nritems(&leaf->header) - 1) {
1403 ret = btrfs_next_leaf(root, path);
1404 if (ret) {
1405 ret = 0;
1406 goto out;
1407 }
1408 } else {
1409 path->slots[0]++;
1410 }
1411 }
1412
1413out:
b18c6685
CM
1414 btrfs_free_path(path);
1415 return ret;
1416}
1417
1418static int prepare_pages(struct btrfs_root *root,
75dfe396
CM
1419 struct file *file,
1420 struct page **pages,
1421 size_t num_pages,
1422 loff_t pos,
2932f3ec
CM
1423 unsigned long first_index,
1424 unsigned long last_index,
6567e837
CM
1425 size_t write_bytes,
1426 u64 alloc_extent_start)
75dfe396
CM
1427{
1428 int i;
1429 unsigned long index = pos >> PAGE_CACHE_SHIFT;
1430 struct inode *inode = file->f_path.dentry->d_inode;
1431 int offset;
1432 int err = 0;
75dfe396 1433 int this_write;
6567e837
CM
1434 struct buffer_head *bh;
1435 struct buffer_head *head;
75dfe396
CM
1436 loff_t isize = i_size_read(inode);
1437
1438 memset(pages, 0, num_pages * sizeof(struct page *));
1439
1440 for (i = 0; i < num_pages; i++) {
1441 pages[i] = grab_cache_page(inode->i_mapping, index + i);
1442 if (!pages[i]) {
1443 err = -ENOMEM;
1444 goto failed_release;
1445 }
1446 offset = pos & (PAGE_CACHE_SIZE -1);
1447 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
b18c6685
CM
1448#if 0
1449 if ((pages[i]->index == first_index ||
1450 pages[i]->index == last_index) && pos < isize &&
1451 !PageUptodate(pages[i])) {
2932f3ec
CM
1452 ret = mpage_readpage(pages[i], btrfs_get_block);
1453 BUG_ON(ret);
1454 lock_page(pages[i]);
1455 }
b18c6685 1456#endif
6567e837
CM
1457 create_empty_buffers(pages[i], root->fs_info->sb->s_blocksize,
1458 (1 << BH_Uptodate));
1459 head = page_buffers(pages[i]);
1460 bh = head;
1461 do {
1462 err = btrfs_map_bh_to_logical(root, bh,
1463 alloc_extent_start);
1464 BUG_ON(err);
1465 if (err)
1466 goto failed_truncate;
1467 bh = bh->b_this_page;
1468 alloc_extent_start++;
1469 } while (bh != head);
75dfe396 1470 pos += this_write;
75dfe396
CM
1471 WARN_ON(this_write > write_bytes);
1472 write_bytes -= this_write;
1473 }
1474 return 0;
1475
1476failed_release:
1477 btrfs_drop_pages(pages, num_pages);
1478 return err;
1479
1480failed_truncate:
1481 btrfs_drop_pages(pages, num_pages);
1482 if (pos > isize)
1483 vmtruncate(inode, isize);
1484 return err;
1485}
1486
1487static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1488 size_t count, loff_t *ppos)
1489{
1490 loff_t pos;
1491 size_t num_written = 0;
1492 int err = 0;
1493 int ret = 0;
75dfe396 1494 struct inode *inode = file->f_path.dentry->d_inode;
d6e4a428 1495 struct btrfs_root *root = BTRFS_I(inode)->root;
b18c6685 1496 struct page *pages[8];
2932f3ec
CM
1497 unsigned long first_index;
1498 unsigned long last_index;
6567e837
CM
1499 u64 start_pos;
1500 u64 num_blocks;
1501 u64 alloc_extent_start;
6567e837 1502 struct btrfs_trans_handle *trans;
b18c6685 1503 struct btrfs_key ins;
75dfe396
CM
1504
1505 if (file->f_flags & O_DIRECT)
1506 return -EINVAL;
1507 pos = *ppos;
1508
6567e837
CM
1509 start_pos = pos & ~(root->blocksize - 1);
1510 /* FIXME */
1511 if (start_pos != pos)
1512 return -EINVAL;
1513 num_blocks = (count + pos - start_pos + root->blocksize - 1) >>
1514 inode->i_blkbits;
1515
75dfe396
CM
1516 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1517 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1518 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1519 if (err)
1520 goto out;
1521 if (count == 0)
1522 goto out;
1523 err = remove_suid(file->f_path.dentry);
1524 if (err)
1525 goto out;
1526 file_update_time(file);
1527 mutex_lock(&inode->i_mutex);
2932f3ec
CM
1528 first_index = pos >> PAGE_CACHE_SHIFT;
1529 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
6567e837
CM
1530
1531 mutex_lock(&root->fs_info->fs_mutex);
1532 trans = btrfs_start_transaction(root, 1);
1533 if (!trans) {
1534 err = -ENOMEM;
b18c6685 1535 mutex_unlock(&root->fs_info->fs_mutex);
6567e837
CM
1536 goto out_unlock;
1537 }
b18c6685
CM
1538 if (start_pos < inode->i_size) {
1539 ret = drop_extents(trans, root, inode,
1540 start_pos,
1541 (pos + count + root->blocksize -1) &
1542 ~(root->blocksize - 1));
1543 }
b18c6685
CM
1544 ret = btrfs_alloc_extent(trans, root, num_blocks, 1,
1545 (u64)-1, &ins);
1546 BUG_ON(ret);
1547 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
1548 start_pos, ins.objectid, ins.offset);
6567e837 1549 BUG_ON(ret);
b18c6685
CM
1550 alloc_extent_start = ins.objectid;
1551 ret = btrfs_end_transaction(trans, root);
6567e837
CM
1552 mutex_unlock(&root->fs_info->fs_mutex);
1553
75dfe396
CM
1554 while(count > 0) {
1555 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1556 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1557 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
1558 PAGE_CACHE_SHIFT;
b18c6685
CM
1559
1560 memset(pages, 0, sizeof(pages));
1561 ret = prepare_pages(root, file, pages, num_pages,
6567e837
CM
1562 pos, first_index, last_index,
1563 write_bytes, alloc_extent_start);
75dfe396 1564 BUG_ON(ret);
b18c6685 1565
6567e837
CM
1566 /* FIXME blocks != pagesize */
1567 alloc_extent_start += num_pages;
75dfe396
CM
1568 ret = btrfs_copy_from_user(pos, num_pages,
1569 write_bytes, pages, buf);
1570 BUG_ON(ret);
1571
f254e52c 1572 ret = dirty_and_release_pages(NULL, root, file, pages,
70b2befd 1573 num_pages, pos, write_bytes);
75dfe396
CM
1574 BUG_ON(ret);
1575 btrfs_drop_pages(pages, num_pages);
1576
75dfe396
CM
1577 buf += write_bytes;
1578 count -= write_bytes;
1579 pos += write_bytes;
1580 num_written += write_bytes;
1581
1582 balance_dirty_pages_ratelimited(inode->i_mapping);
1583 cond_resched();
1584 }
6567e837 1585out_unlock:
75dfe396
CM
1586 mutex_unlock(&inode->i_mutex);
1587out:
1588 *ppos = pos;
1589 current->backing_dev_info = NULL;
1590 return num_written ? num_written : err;
1591}
1592
e8f05c45 1593#if 0
df24a2b9
CM
1594static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
1595 struct page *page, loff_t pos,
1596 size_t offset, size_t write_bytes)
1597{
1598 struct btrfs_path *path;
1599 struct btrfs_trans_handle *trans;
1600 struct btrfs_key key;
1601 struct btrfs_leaf *leaf;
1602 struct btrfs_key found_key;
1603 int ret;
1604 size_t copy_size = 0;
1605 char *dst = NULL;
1606 int err = 0;
1607 size_t num_written = 0;
1608
1609 path = btrfs_alloc_path();
1610 BUG_ON(!path);
1611 mutex_lock(&root->fs_info->fs_mutex);
1612 trans = btrfs_start_transaction(root, 1);
1613 key.objectid = inode->i_ino;
1614 key.flags = 0;
1615 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
1616
1617again:
1618 key.offset = pos;
1619 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1620 if (ret < 0) {
1621 err = ret;
1622 goto out;
1623 }
1624 if (ret == 0) {
1625 leaf = btrfs_buffer_leaf(path->nodes[0]);
1626 btrfs_disk_key_to_cpu(&found_key,
1627 &leaf->items[path->slots[0]].key);
1628 copy_size = btrfs_item_size(leaf->items + path->slots[0]);
1629 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1630 copy_size = min(write_bytes, copy_size);
1631 goto copyit;
1632 } else {
1633 int slot = path->slots[0];
1634 if (slot > 0) {
1635 slot--;
1636 }
1637 // FIXME find max key
1638 leaf = btrfs_buffer_leaf(path->nodes[0]);
1639 btrfs_disk_key_to_cpu(&found_key,
1640 &leaf->items[slot].key);
1641 if (found_key.objectid != inode->i_ino)
1642 goto insert;
1643 if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
1644 goto insert;
1645 copy_size = btrfs_item_size(leaf->items + slot);
1646 if (found_key.offset + copy_size <= pos)
1647 goto insert;
1648 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1649 dst += pos - found_key.offset;
1650 copy_size = copy_size - (pos - found_key.offset);
1651 BUG_ON(copy_size < 0);
1652 copy_size = min(write_bytes, copy_size);
1653 WARN_ON(copy_size == 0);
1654 goto copyit;
1655 }
1656insert:
1657 btrfs_release_path(root, path);
d4dbff95
CM
1658 copy_size = min(write_bytes,
1659 (size_t)BTRFS_LEAF_DATA_SIZE(root) -
1660 sizeof(struct btrfs_item) * 4);
df24a2b9
CM
1661 ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
1662 BUG_ON(ret);
1663 dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1664 path->slots[0], char);
1665copyit:
1666 WARN_ON(copy_size == 0);
1667 WARN_ON(dst + copy_size >
1668 btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1669 path->slots[0], char) +
1670 btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
1671 path->slots[0]));
1672 btrfs_memcpy(root, path->nodes[0]->b_data, dst,
1673 page_address(page) + offset, copy_size);
1674 mark_buffer_dirty(path->nodes[0]);
1675 btrfs_release_path(root, path);
1676 pos += copy_size;
1677 offset += copy_size;
1678 num_written += copy_size;
1679 write_bytes -= copy_size;
1680 if (write_bytes)
1681 goto again;
1682out:
1683 btrfs_free_path(path);
1684 ret = btrfs_end_transaction(trans, root);
1685 BUG_ON(ret);
1686 mutex_unlock(&root->fs_info->fs_mutex);
1687 return num_written ? num_written : err;
1688}
1689
1690static ssize_t btrfs_file_inline_write(struct file *file,
1691 const char __user *buf,
1692 size_t count, loff_t *ppos)
1693{
1694 loff_t pos;
1695 size_t num_written = 0;
1696 int err = 0;
1697 int ret = 0;
1698 struct inode *inode = file->f_path.dentry->d_inode;
d6e4a428 1699 struct btrfs_root *root = BTRFS_I(inode)->root;
df24a2b9
CM
1700 unsigned long page_index;
1701
1702 if (file->f_flags & O_DIRECT)
1703 return -EINVAL;
1704 pos = *ppos;
1705
1706 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1707 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1708 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1709 if (err)
1710 goto out;
1711 if (count == 0)
1712 goto out;
1713 err = remove_suid(file->f_path.dentry);
1714 if (err)
1715 goto out;
1716 file_update_time(file);
1717 mutex_lock(&inode->i_mutex);
1718 while(count > 0) {
1719 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1720 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1721 struct page *page;
1722
1723 page_index = pos >> PAGE_CACHE_SHIFT;
1724 page = grab_cache_page(inode->i_mapping, page_index);
1725 if (!PageUptodate(page)) {
1726 ret = mpage_readpage(page, btrfs_get_block);
1727 BUG_ON(ret);
1728 lock_page(page);
1729 }
1730 ret = btrfs_copy_from_user(pos, 1,
1731 write_bytes, &page, buf);
1732 BUG_ON(ret);
1733 write_bytes = inline_one_page(root, inode, page, pos,
1734 offset, write_bytes);
1735 SetPageUptodate(page);
1736 if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
1737 i_size_write(inode, pos + write_bytes);
1738 mark_inode_dirty(inode);
1739 }
1740 page_cache_release(page);
1741 unlock_page(page);
1742 if (write_bytes < 0)
1743 goto out_unlock;
1744 buf += write_bytes;
1745 count -= write_bytes;
1746 pos += write_bytes;
1747 num_written += write_bytes;
1748
1749 balance_dirty_pages_ratelimited(inode->i_mapping);
1750 cond_resched();
1751 }
1752out_unlock:
1753 mutex_unlock(&inode->i_mutex);
1754out:
1755 *ppos = pos;
1756 current->backing_dev_info = NULL;
1757 return num_written ? num_written : err;
1758}
e8f05c45 1759#endif
df24a2b9 1760
f254e52c
CM
1761static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
1762 unsigned long offset, unsigned long size)
1763{
1764 char *kaddr;
1765 unsigned long left, count = desc->count;
d6e4a428 1766 struct inode *inode = page->mapping->host;
f254e52c
CM
1767
1768 if (size > count)
1769 size = count;
1770
1771 if (!PageChecked(page)) {
1772 /* FIXME, do it per block */
d6e4a428 1773 struct btrfs_root *root = BTRFS_I(inode)->root;
f254e52c
CM
1774 int ret = btrfs_csum_verify_file_block(root,
1775 page->mapping->host->i_ino,
1776 page->index << PAGE_CACHE_SHIFT,
1777 kmap(page), PAGE_CACHE_SIZE);
1778 if (ret) {
1779 printk("failed to verify ino %lu page %lu\n",
1780 page->mapping->host->i_ino,
1781 page->index);
1782 memset(page_address(page), 0, PAGE_CACHE_SIZE);
1783 }
1784 SetPageChecked(page);
1785 kunmap(page);
1786 }
1787 /*
1788 * Faults on the destination of a read are common, so do it before
1789 * taking the kmap.
1790 */
1791 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1792 kaddr = kmap_atomic(page, KM_USER0);
1793 left = __copy_to_user_inatomic(desc->arg.buf,
1794 kaddr + offset, size);
1795 kunmap_atomic(kaddr, KM_USER0);
1796 if (left == 0)
1797 goto success;
1798 }
1799
1800 /* Do it the slow way */
1801 kaddr = kmap(page);
1802 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1803 kunmap(page);
1804
1805 if (left) {
1806 size -= left;
1807 desc->error = -EFAULT;
1808 }
1809success:
1810 desc->count = count - size;
1811 desc->written += size;
1812 desc->arg.buf += size;
1813 return size;
1814}
1815
1816/**
1817 * btrfs_file_aio_read - filesystem read routine
1818 * @iocb: kernel I/O control block
1819 * @iov: io vector request
1820 * @nr_segs: number of segments in the iovec
1821 * @pos: current file position
1822 */
1823static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1824 unsigned long nr_segs, loff_t pos)
1825{
1826 struct file *filp = iocb->ki_filp;
1827 ssize_t retval;
1828 unsigned long seg;
1829 size_t count;
1830 loff_t *ppos = &iocb->ki_pos;
1831
1832 count = 0;
1833 for (seg = 0; seg < nr_segs; seg++) {
1834 const struct iovec *iv = &iov[seg];
1835
1836 /*
1837 * If any segment has a negative length, or the cumulative
1838 * length ever wraps negative then return -EINVAL.
1839 */
1840 count += iv->iov_len;
1841 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
1842 return -EINVAL;
1843 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
1844 continue;
1845 if (seg == 0)
1846 return -EFAULT;
1847 nr_segs = seg;
1848 count -= iv->iov_len; /* This segment is no good */
1849 break;
1850 }
1851 retval = 0;
1852 if (count) {
1853 for (seg = 0; seg < nr_segs; seg++) {
1854 read_descriptor_t desc;
1855
1856 desc.written = 0;
1857 desc.arg.buf = iov[seg].iov_base;
1858 desc.count = iov[seg].iov_len;
1859 if (desc.count == 0)
1860 continue;
1861 desc.error = 0;
1862 do_generic_file_read(filp, ppos, &desc,
1863 btrfs_read_actor);
1864 retval += desc.written;
1865 if (desc.error) {
1866 retval = retval ?: desc.error;
1867 break;
1868 }
1869 }
1870 }
1871 return retval;
1872}
1873
2619ba1f
CM
1874static int create_subvol(struct btrfs_root *root, char *name, int namelen)
1875{
1876 struct btrfs_trans_handle *trans;
1877 struct btrfs_key key;
1878 struct btrfs_root_item root_item;
1879 struct btrfs_inode_item *inode_item;
1880 struct buffer_head *subvol;
1881 struct btrfs_leaf *leaf;
1882 struct btrfs_root *new_root;
1883 struct inode *inode;
1884 int ret;
1885 u64 objectid;
1886 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
1887
1888 mutex_lock(&root->fs_info->fs_mutex);
1889 trans = btrfs_start_transaction(root, 1);
1890 BUG_ON(!trans);
1891
1892 subvol = btrfs_alloc_free_block(trans, root);
1893 leaf = btrfs_buffer_leaf(subvol);
1894 btrfs_set_header_nritems(&leaf->header, 0);
1895 btrfs_set_header_level(&leaf->header, 0);
7eccb903 1896 btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
2619ba1f
CM
1897 btrfs_set_header_generation(&leaf->header, trans->transid);
1898 memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
1899 sizeof(leaf->header.fsid));
1900
1901 inode_item = &root_item.inode;
1902 memset(inode_item, 0, sizeof(*inode_item));
1903 btrfs_set_inode_generation(inode_item, 1);
1904 btrfs_set_inode_size(inode_item, 3);
1905 btrfs_set_inode_nlink(inode_item, 1);
1906 btrfs_set_inode_nblocks(inode_item, 1);
1907 btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
1908
7eccb903 1909 btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
2619ba1f
CM
1910 btrfs_set_root_refs(&root_item, 1);
1911
1912 mark_buffer_dirty(subvol);
1913 brelse(subvol);
1914 subvol = NULL;
1915
1916 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
1917 0, &objectid);
1918 BUG_ON(ret);
1919
1920 btrfs_set_root_dirid(&root_item, new_dirid);
1921
1922 key.objectid = objectid;
1923 key.offset = 1;
1924 key.flags = 0;
1925 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
1926 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
1927 &root_item);
1928 BUG_ON(ret);
1929
1930 /*
1931 * insert the directory item
1932 */
1933 key.offset = (u64)-1;
1934 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
1935 name, namelen,
1936 root->fs_info->sb->s_root->d_inode->i_ino,
1937 &key, 0);
1938 BUG_ON(ret);
1939
1940 ret = btrfs_commit_transaction(trans, root);
1941 BUG_ON(ret);
1942
1943 new_root = btrfs_read_fs_root(root->fs_info, &key);
1944 BUG_ON(!new_root);
1945
1946 trans = btrfs_start_transaction(new_root, 1);
1947 BUG_ON(!trans);
1948
1949 inode = btrfs_new_inode(trans, new_root, new_dirid, S_IFDIR | 0700);
1950 inode->i_op = &btrfs_dir_inode_operations;
1951 inode->i_fop = &btrfs_dir_file_operations;
1952
1953 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
1954 BUG_ON(ret);
1955
1956 inode->i_nlink = 1;
1957 inode->i_size = 6;
1958 ret = btrfs_update_inode(trans, new_root, inode);
1959 BUG_ON(ret);
1960
1961 ret = btrfs_commit_transaction(trans, new_root);
1962 BUG_ON(ret);
1963
1964 iput(inode);
1965
1966 mutex_unlock(&root->fs_info->fs_mutex);
1967 return 0;
1968}
1969
c5739bba
CM
1970static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
1971{
1972 struct btrfs_trans_handle *trans;
1973 struct btrfs_key key;
1974 struct btrfs_root_item new_root_item;
1975 int ret;
1976 u64 objectid;
1977
2619ba1f
CM
1978 if (!root->ref_cows)
1979 return -EINVAL;
1980
c5739bba
CM
1981 mutex_lock(&root->fs_info->fs_mutex);
1982 trans = btrfs_start_transaction(root, 1);
1983 BUG_ON(!trans);
1984
1985 ret = btrfs_update_inode(trans, root, root->inode);
1986 BUG_ON(ret);
1987
1b05da2e
CM
1988 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
1989 0, &objectid);
c5739bba
CM
1990 BUG_ON(ret);
1991
c5739bba
CM
1992 memcpy(&new_root_item, &root->root_item,
1993 sizeof(new_root_item));
1994
c5739bba
CM
1995 key.objectid = objectid;
1996 key.offset = 1;
1997 key.flags = 0;
1998 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
7eccb903 1999 btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
c5739bba
CM
2000
2001 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2002 &new_root_item);
2003 BUG_ON(ret);
2004
c5739bba
CM
2005 /*
2006 * insert the directory item
2007 */
2008 key.offset = (u64)-1;
2009 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2010 name, namelen,
2011 root->fs_info->sb->s_root->d_inode->i_ino,
2012 &key, 0);
2013
2014 BUG_ON(ret);
2015
2016 ret = btrfs_inc_root_ref(trans, root);
2017 BUG_ON(ret);
2018
2019 ret = btrfs_commit_transaction(trans, root);
2020 BUG_ON(ret);
2021 mutex_unlock(&root->fs_info->fs_mutex);
2022 return 0;
2023}
2024
8352d8a4
CM
2025static int add_disk(struct btrfs_root *root, char *name, int namelen)
2026{
2027 struct block_device *bdev;
2028 struct btrfs_path *path;
2029 struct super_block *sb = root->fs_info->sb;
2030 struct btrfs_root *dev_root = root->fs_info->dev_root;
2031 struct btrfs_trans_handle *trans;
2032 struct btrfs_device_item *dev_item;
2033 struct btrfs_key key;
2034 u16 item_size;
2035 u64 num_blocks;
2036 u64 new_blocks;
b4100d64 2037 u64 device_id;
8352d8a4 2038 int ret;
b4100d64 2039
8352d8a4
CM
2040printk("adding disk %s\n", name);
2041 path = btrfs_alloc_path();
2042 if (!path)
2043 return -ENOMEM;
2044 num_blocks = btrfs_super_total_blocks(root->fs_info->disk_super);
2045 bdev = open_bdev_excl(name, O_RDWR, sb);
2046 if (IS_ERR(bdev)) {
2047 ret = PTR_ERR(bdev);
2048printk("open bdev excl failed ret %d\n", ret);
2049 goto out_nolock;
2050 }
2051 set_blocksize(bdev, sb->s_blocksize);
2052 new_blocks = bdev->bd_inode->i_size >> sb->s_blocksize_bits;
2053 key.objectid = num_blocks;
2054 key.offset = new_blocks;
2055 key.flags = 0;
2056 btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY);
2057
2058 mutex_lock(&dev_root->fs_info->fs_mutex);
2059 trans = btrfs_start_transaction(dev_root, 1);
2060 item_size = sizeof(*dev_item) + namelen;
2061printk("insert empty on %Lu %Lu %u size %d\n", num_blocks, new_blocks, key.flags, item_size);
2062 ret = btrfs_insert_empty_item(trans, dev_root, path, &key, item_size);
2063 if (ret) {
2064printk("insert failed %d\n", ret);
2065 close_bdev_excl(bdev);
2066 if (ret > 0)
2067 ret = -EEXIST;
2068 goto out;
2069 }
2070 dev_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
2071 path->slots[0], struct btrfs_device_item);
2072 btrfs_set_device_pathlen(dev_item, namelen);
2073 memcpy(dev_item + 1, name, namelen);
b4100d64
CM
2074
2075 device_id = btrfs_super_last_device_id(root->fs_info->disk_super) + 1;
2076 btrfs_set_super_last_device_id(root->fs_info->disk_super, device_id);
2077 btrfs_set_device_id(dev_item, device_id);
8352d8a4
CM
2078 mark_buffer_dirty(path->nodes[0]);
2079
b4100d64
CM
2080 ret = btrfs_insert_dev_radix(root, bdev, device_id, num_blocks,
2081 new_blocks);
8352d8a4
CM
2082
2083 if (!ret) {
2084 btrfs_set_super_total_blocks(root->fs_info->disk_super,
2085 num_blocks + new_blocks);
2086 i_size_write(root->fs_info->btree_inode,
2087 (num_blocks + new_blocks) <<
2088 root->fs_info->btree_inode->i_blkbits);
2089 }
2090
2091out:
2092 ret = btrfs_commit_transaction(trans, dev_root);
2093 BUG_ON(ret);
2094 mutex_unlock(&root->fs_info->fs_mutex);
2095out_nolock:
2096 btrfs_free_path(path);
2097
2098 return ret;
2099}
2100
c5739bba
CM
2101static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
2102 cmd, unsigned long arg)
2103{
2104 struct btrfs_root *root = BTRFS_I(inode)->root;
2105 struct btrfs_ioctl_vol_args vol_args;
8352d8a4 2106 int ret = 0;
c5739bba 2107 int namelen;
2619ba1f
CM
2108 struct btrfs_path *path;
2109 u64 root_dirid;
c5739bba 2110
c5739bba
CM
2111 switch (cmd) {
2112 case BTRFS_IOC_SNAP_CREATE:
2113 if (copy_from_user(&vol_args,
2114 (struct btrfs_ioctl_vol_args __user *)arg,
2115 sizeof(vol_args)))
2116 return -EFAULT;
2117 namelen = strlen(vol_args.name);
2118 if (namelen > BTRFS_VOL_NAME_MAX)
2119 return -EINVAL;
2619ba1f
CM
2120 path = btrfs_alloc_path();
2121 if (!path)
2122 return -ENOMEM;
2d13d8d0 2123 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2619ba1f
CM
2124 mutex_lock(&root->fs_info->fs_mutex);
2125 ret = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2126 path, root_dirid,
2127 vol_args.name, namelen, 0);
2128 mutex_unlock(&root->fs_info->fs_mutex);
2d13d8d0 2129 btrfs_free_path(path);
2619ba1f
CM
2130 if (ret == 0)
2131 return -EEXIST;
2132
2133 if (root == root->fs_info->tree_root)
2134 ret = create_subvol(root, vol_args.name, namelen);
2135 else
2136 ret = create_snapshot(root, vol_args.name, namelen);
c5739bba
CM
2137 WARN_ON(ret);
2138 break;
8352d8a4
CM
2139 case BTRFS_IOC_ADD_DISK:
2140 if (copy_from_user(&vol_args,
2141 (struct btrfs_ioctl_vol_args __user *)arg,
2142 sizeof(vol_args)))
2143 return -EFAULT;
2144 namelen = strlen(vol_args.name);
2145 if (namelen > BTRFS_VOL_NAME_MAX)
2146 return -EINVAL;
2147 vol_args.name[namelen] = '\0';
2148 ret = add_disk(root, vol_args.name, namelen);
2149 break;
c5739bba
CM
2150 default:
2151 return -ENOTTY;
2152 }
8352d8a4 2153 return ret;
c5739bba
CM
2154}
2155
2c90e5d6
CM
2156static struct kmem_cache *btrfs_inode_cachep;
2157struct kmem_cache *btrfs_trans_handle_cachep;
2158struct kmem_cache *btrfs_transaction_cachep;
2159struct kmem_cache *btrfs_bit_radix_cachep;
2160struct kmem_cache *btrfs_path_cachep;
2161
2162/*
2163 * Called inside transaction, so use GFP_NOFS
2164 */
2165static struct inode *btrfs_alloc_inode(struct super_block *sb)
2166{
2167 struct btrfs_inode *ei;
2168
2169 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2170 if (!ei)
2171 return NULL;
2c90e5d6
CM
2172 return &ei->vfs_inode;
2173}
2174
2175static void btrfs_destroy_inode(struct inode *inode)
2176{
2c90e5d6 2177 WARN_ON(!list_empty(&inode->i_dentry));
2c90e5d6
CM
2178 WARN_ON(inode->i_data.nrpages);
2179
2c90e5d6
CM
2180 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2181}
2182
2183static void init_once(void * foo, struct kmem_cache * cachep,
2184 unsigned long flags)
2185{
2186 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2187
2188 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
2189 SLAB_CTOR_CONSTRUCTOR) {
2190 inode_init_once(&ei->vfs_inode);
2191 }
2192}
2193
2194static int init_inodecache(void)
2195{
2196 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
2197 sizeof(struct btrfs_inode),
2198 0, (SLAB_RECLAIM_ACCOUNT|
2199 SLAB_MEM_SPREAD),
2200 init_once, NULL);
2201 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
2202 sizeof(struct btrfs_trans_handle),
2203 0, (SLAB_RECLAIM_ACCOUNT|
2204 SLAB_MEM_SPREAD),
2205 NULL, NULL);
2206 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
2207 sizeof(struct btrfs_transaction),
2208 0, (SLAB_RECLAIM_ACCOUNT|
2209 SLAB_MEM_SPREAD),
2210 NULL, NULL);
2211 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
2212 sizeof(struct btrfs_transaction),
2213 0, (SLAB_RECLAIM_ACCOUNT|
2214 SLAB_MEM_SPREAD),
2215 NULL, NULL);
2216 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
2217 256,
2218 0, (SLAB_RECLAIM_ACCOUNT|
2219 SLAB_MEM_SPREAD |
2220 SLAB_DESTROY_BY_RCU),
2221 NULL, NULL);
2222 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
2223 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
2224 return -ENOMEM;
2225 return 0;
2226}
2227
2228static void destroy_inodecache(void)
2229{
2230 kmem_cache_destroy(btrfs_inode_cachep);
2231 kmem_cache_destroy(btrfs_trans_handle_cachep);
2232 kmem_cache_destroy(btrfs_transaction_cachep);
2233 kmem_cache_destroy(btrfs_bit_radix_cachep);
2234 kmem_cache_destroy(btrfs_path_cachep);
2235}
2236
2e635a27
CM
2237static int btrfs_get_sb(struct file_system_type *fs_type,
2238 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2239{
2240 return get_sb_bdev(fs_type, flags, dev_name, data,
2241 btrfs_fill_super, mnt);
2242}
2243
2244static struct file_system_type btrfs_fs_type = {
2245 .owner = THIS_MODULE,
2246 .name = "btrfs",
2247 .get_sb = btrfs_get_sb,
2248 .kill_sb = kill_block_super,
2249 .fs_flags = FS_REQUIRES_DEV,
2250};
2251
e20d96d6
CM
2252static struct super_operations btrfs_super_ops = {
2253 .statfs = simple_statfs,
134e9731 2254 .delete_inode = btrfs_delete_inode,
e20d96d6
CM
2255 .put_super = btrfs_put_super,
2256 .read_inode = btrfs_read_locked_inode,
d5719762
CM
2257 .write_super = btrfs_write_super,
2258 .sync_fs = btrfs_sync_fs,
4730a4bc 2259 .write_inode = btrfs_write_inode,
2c90e5d6
CM
2260 .alloc_inode = btrfs_alloc_inode,
2261 .destroy_inode = btrfs_destroy_inode,
e20d96d6
CM
2262};
2263
2264static struct inode_operations btrfs_dir_inode_operations = {
2265 .lookup = btrfs_lookup,
d5719762 2266 .create = btrfs_create,
134e9731 2267 .unlink = btrfs_unlink,
f7922033 2268 .mkdir = btrfs_mkdir,
5f443fd2 2269 .rmdir = btrfs_rmdir,
e20d96d6
CM
2270};
2271
d6e4a428
CM
2272static struct inode_operations btrfs_dir_ro_inode_operations = {
2273 .lookup = btrfs_lookup,
2274};
2275
e20d96d6
CM
2276static struct file_operations btrfs_dir_file_operations = {
2277 .llseek = generic_file_llseek,
2278 .read = generic_read_dir,
2279 .readdir = btrfs_readdir,
c5739bba 2280 .ioctl = btrfs_ioctl,
e20d96d6
CM
2281};
2282
dee26a9f
CM
2283static struct address_space_operations btrfs_aops = {
2284 .readpage = btrfs_readpage,
dee26a9f
CM
2285 .writepage = btrfs_writepage,
2286 .sync_page = block_sync_page,
2287 .prepare_write = btrfs_prepare_write,
75dfe396 2288 .commit_write = btrfs_commit_write,
dee26a9f
CM
2289};
2290
2291static struct inode_operations btrfs_file_inode_operations = {
f4b9aa8d 2292 .truncate = btrfs_truncate,
dee26a9f
CM
2293};
2294
2295static struct file_operations btrfs_file_operations = {
2296 .llseek = generic_file_llseek,
2297 .read = do_sync_read,
e8f05c45
CM
2298 .aio_read = btrfs_file_aio_read,
2299 .write = btrfs_file_write,
dee26a9f
CM
2300 .mmap = generic_file_mmap,
2301 .open = generic_file_open,
c5739bba 2302 .ioctl = btrfs_ioctl,
dee26a9f 2303};
e20d96d6 2304
2e635a27
CM
2305static int __init init_btrfs_fs(void)
2306{
2c90e5d6 2307 int err;
2e635a27 2308 printk("btrfs loaded!\n");
2c90e5d6
CM
2309 err = init_inodecache();
2310 if (err)
2311 return err;
d6e4a428
CM
2312 kset_set_kset_s(&btrfs_subsys, fs_subsys);
2313 err = subsystem_register(&btrfs_subsys);
2314 if (err)
2315 goto out;
2e635a27 2316 return register_filesystem(&btrfs_fs_type);
d6e4a428
CM
2317out:
2318 destroy_inodecache();
2319 return err;
2e635a27
CM
2320}
2321
2322static void __exit exit_btrfs_fs(void)
2323{
2c90e5d6 2324 destroy_inodecache();
2e635a27 2325 unregister_filesystem(&btrfs_fs_type);
d6e4a428 2326 subsystem_unregister(&btrfs_subsys);
2e635a27
CM
2327 printk("btrfs unloaded\n");
2328}
2329
2330module_init(init_btrfs_fs)
2331module_exit(exit_btrfs_fs)
2332
2333MODULE_LICENSE("GPL");