Btrfs: cow file extents before writing
[deliverable/linux.git] / fs / btrfs / super.c
1 #include <linux/module.h>
2 #include <linux/buffer_head.h>
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>
11 #include <linux/mpage.h>
12 #include <linux/swap.h>
13 #include <linux/writeback.h>
14 #include "ctree.h"
15 #include "disk-io.h"
16 #include "transaction.h"
17 #include "btrfs_inode.h"
18 #include "ioctl.h"
19
20 void 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
27 struct kobj_type btrfs_fsinfo_ktype = {
28 .release = btrfs_fsinfo_release,
29 };
30
31 struct btrfs_iget_args {
32 u64 ino;
33 struct btrfs_root *root;
34 };
35
36 decl_subsys(btrfs, &btrfs_fsinfo_ktype, NULL);
37
38 #define BTRFS_SUPER_MAGIC 0x9123682E
39
40 static struct inode_operations btrfs_dir_inode_operations;
41 static struct inode_operations btrfs_dir_ro_inode_operations;
42 static struct super_operations btrfs_super_ops;
43 static struct file_operations btrfs_dir_file_operations;
44 static struct inode_operations btrfs_file_inode_operations;
45 static struct address_space_operations btrfs_aops;
46 static struct file_operations btrfs_file_operations;
47
48 static void btrfs_read_locked_inode(struct inode *inode)
49 {
50 struct btrfs_path *path;
51 struct btrfs_inode_item *inode_item;
52 struct btrfs_root *root = BTRFS_I(inode)->root;
53 struct btrfs_key location;
54 int ret;
55
56 path = btrfs_alloc_path();
57 BUG_ON(!path);
58 btrfs_init_path(path);
59 mutex_lock(&root->fs_info->fs_mutex);
60
61 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
62 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
63 if (ret) {
64 btrfs_free_path(path);
65 goto make_bad;
66 }
67 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
68 path->slots[0],
69 struct btrfs_inode_item);
70
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);
84
85 btrfs_free_path(path);
86 inode_item = NULL;
87
88 mutex_unlock(&root->fs_info->fs_mutex);
89
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:
98 inode->i_mapping->a_ops = &btrfs_aops;
99 inode->i_fop = &btrfs_file_operations;
100 inode->i_op = &btrfs_file_inode_operations;
101 break;
102 case S_IFDIR:
103 inode->i_fop = &btrfs_dir_file_operations;
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;
108 break;
109 case S_IFLNK:
110 // inode->i_op = &page_symlink_inode_operations;
111 break;
112 }
113 return;
114
115 make_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);
120 }
121
122 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
123 struct btrfs_root *root,
124 struct inode *dir,
125 struct dentry *dentry)
126 {
127 struct btrfs_path *path;
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
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,
138 name, name_len, -1);
139 if (ret < 0)
140 goto err;
141 if (ret > 0) {
142 ret = -ENOENT;
143 goto err;
144 }
145 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
146 struct btrfs_dir_item);
147 objectid = btrfs_disk_key_objectid(&di->location);
148
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);
156 ret = btrfs_del_item(trans, root, path);
157 BUG_ON(ret);
158 dentry->d_inode->i_ctime = dir->i_ctime;
159 err:
160 btrfs_release_path(root, path);
161 btrfs_free_path(path);
162 if (ret == 0) {
163 inode_dec_link_count(dentry->d_inode);
164 dir->i_size -= name_len * 2;
165 mark_inode_dirty(dir);
166 }
167 return ret;
168 }
169
170 static 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
176 root = BTRFS_I(dir)->root;
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
185 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
186 {
187 struct inode *inode = dentry->d_inode;
188 int err;
189 int ret;
190 struct btrfs_root *root = BTRFS_I(dir)->root;
191 struct btrfs_path *path;
192 struct btrfs_key key;
193 struct btrfs_trans_handle *trans;
194 struct btrfs_key found_key;
195 int found_type;
196 struct btrfs_leaf *leaf;
197 char *goodnames = "..";
198
199 path = btrfs_alloc_path();
200 BUG_ON(!path);
201 btrfs_init_path(path);
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;
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);
236
237 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
238 break;
239 btrfs_release_path(root, path);
240 }
241 ret = 0;
242 btrfs_release_path(root, path);
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 }
249 out:
250 btrfs_release_path(root, path);
251 btrfs_free_path(path);
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
259 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
260 struct btrfs_root *root,
261 struct inode *inode)
262 {
263 struct btrfs_path *path;
264 int ret;
265
266 clear_inode(inode);
267
268 path = btrfs_alloc_path();
269 BUG_ON(!path);
270 btrfs_init_path(path);
271 ret = btrfs_lookup_inode(trans, root, path,
272 &BTRFS_I(inode)->location, -1);
273 BUG_ON(ret);
274 ret = btrfs_del_item(trans, root, path);
275 BUG_ON(ret);
276 btrfs_free_path(path);
277 return ret;
278 }
279
280 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
281 struct btrfs_root *root,
282 struct inode *inode)
283 {
284 int ret;
285 struct btrfs_path *path;
286 struct btrfs_key key;
287 struct btrfs_disk_key *found_key;
288 struct btrfs_leaf *leaf;
289 struct btrfs_file_extent_item *fi = NULL;
290 u64 extent_start = 0;
291 u64 extent_num_blocks = 0;
292 int found_extent;
293
294 path = btrfs_alloc_path();
295 BUG_ON(!path);
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;
300 /*
301 * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys
302 * or extent data
303 */
304 btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY);
305 while(1) {
306 btrfs_init_path(path);
307 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
308 if (ret < 0) {
309 goto error;
310 }
311 if (ret > 0) {
312 BUG_ON(path->slots[0] == 0);
313 path->slots[0]--;
314 }
315 leaf = btrfs_buffer_leaf(path->nodes[0]);
316 found_key = &leaf->items[path->slots[0]].key;
317 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
318 break;
319 if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY &&
320 btrfs_disk_key_type(found_key) != BTRFS_INLINE_DATA_KEY &&
321 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
322 break;
323 if (btrfs_disk_key_offset(found_key) < inode->i_size)
324 break;
325 if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
326 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
327 path->slots[0],
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 }
338 ret = btrfs_del_item(trans, root, path);
339 BUG_ON(ret);
340 btrfs_release_path(root, path);
341 if (found_extent) {
342 ret = btrfs_free_extent(trans, root, extent_start,
343 extent_num_blocks, 0);
344 BUG_ON(ret);
345 }
346 }
347 ret = 0;
348 error:
349 btrfs_release_path(root, path);
350 btrfs_free_path(path);
351 return ret;
352 }
353
354 static void btrfs_delete_inode(struct inode *inode)
355 {
356 struct btrfs_trans_handle *trans;
357 struct btrfs_root *root = BTRFS_I(inode)->root;
358 int ret;
359
360 truncate_inode_pages(&inode->i_data, 0);
361 if (is_bad_inode(inode)) {
362 goto no_delete;
363 }
364 inode->i_size = 0;
365 mutex_lock(&root->fs_info->fs_mutex);
366 trans = btrfs_start_transaction(root, 1);
367 if (S_ISREG(inode->i_mode)) {
368 ret = btrfs_truncate_in_trans(trans, root, inode);
369 BUG_ON(ret);
370 }
371 btrfs_free_inode(trans, root, inode);
372 btrfs_end_transaction(trans, root);
373 mutex_unlock(&root->fs_info->fs_mutex);
374 return;
375 no_delete:
376 clear_inode(inode);
377 }
378
379 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
380 struct btrfs_key *location)
381 {
382 const char *name = dentry->d_name.name;
383 int namelen = dentry->d_name.len;
384 struct btrfs_dir_item *di;
385 struct btrfs_path *path;
386 struct btrfs_root *root = BTRFS_I(dir)->root;
387 int ret;
388
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,
393 namelen, 0);
394 if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) {
395 location->objectid = 0;
396 ret = 0;
397 goto out;
398 }
399 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
400 struct btrfs_dir_item);
401 btrfs_disk_key_to_cpu(location, &di->location);
402 out:
403 btrfs_release_path(root, path);
404 btrfs_free_path(path);
405 return ret;
406 }
407
408 int 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;
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
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;
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;
433
434 btrfs_free_path(path);
435 mutex_unlock(&root->fs_info->fs_mutex);
436 return 0;
437 }
438
439 int 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
447 int 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
454 struct 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 }
467
468 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
469 struct nameidata *nd)
470 {
471 struct inode * inode;
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;
476 int ret;
477
478 if (dentry->d_name.len > BTRFS_NAME_LEN)
479 return ERR_PTR(-ENAMETOOLONG);
480 mutex_lock(&root->fs_info->fs_mutex);
481 ret = btrfs_inode_by_name(dir, dentry, &location);
482 mutex_unlock(&root->fs_info->fs_mutex);
483 if (ret < 0)
484 return ERR_PTR(ret);
485 inode = NULL;
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);
492 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
493 sub_root);
494 if (!inode)
495 return ERR_PTR(-EACCES);
496 if (inode->i_state & I_NEW) {
497 if (sub_root != root) {
498 ret = radix_tree_insert(
499 &root->fs_info->fs_roots_radix,
500 (unsigned long)sub_root,
501 sub_root);
502 printk("adding new root for inode %lu root %p (found %p)\n", inode->i_ino, sub_root, BTRFS_I(inode)->root);
503 igrab(inode);
504 sub_root->inode = inode;
505 }
506 BTRFS_I(inode)->root = sub_root;
507 memcpy(&BTRFS_I(inode)->location, &location,
508 sizeof(location));
509 btrfs_read_locked_inode(inode);
510 unlock_new_inode(inode);
511 }
512 }
513 return d_splice_alias(inode, dentry);
514 }
515
516 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
517 {
518 struct inode *inode = filp->f_path.dentry->d_inode;
519 struct btrfs_root *root = BTRFS_I(inode)->root;
520 struct btrfs_item *item;
521 struct btrfs_dir_item *di;
522 struct btrfs_key key;
523 struct btrfs_path *path;
524 int ret;
525 u32 nritems;
526 struct btrfs_leaf *leaf;
527 int slot;
528 int advance;
529 unsigned char d_type = DT_UNKNOWN;
530 int over = 0;
531 int key_type = BTRFS_DIR_INDEX_KEY;
532
533 /* FIXME, use a real flag for deciding about the key type */
534 if (root->fs_info->tree_root == root)
535 key_type = BTRFS_DIR_ITEM_KEY;
536 mutex_lock(&root->fs_info->fs_mutex);
537 key.objectid = inode->i_ino;
538 key.flags = 0;
539 btrfs_set_key_type(&key, key_type);
540 key.offset = filp->f_pos;
541 path = btrfs_alloc_path();
542 btrfs_init_path(path);
543 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
544 if (ret < 0)
545 goto err;
546 advance = 0;
547 while(1) {
548 leaf = btrfs_buffer_leaf(path->nodes[0]);
549 nritems = btrfs_header_nritems(&leaf->header);
550 slot = path->slots[0];
551 if (advance || slot >= nritems) {
552 if (slot >= nritems -1) {
553 ret = btrfs_next_leaf(root, path);
554 if (ret)
555 break;
556 leaf = btrfs_buffer_leaf(path->nodes[0]);
557 nritems = btrfs_header_nritems(&leaf->header);
558 slot = path->slots[0];
559 } else {
560 slot++;
561 path->slots[0]++;
562 }
563 }
564 advance = 1;
565 item = leaf->items + slot;
566 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
567 break;
568 if (key_type == BTRFS_DIR_INDEX_KEY &&
569 btrfs_disk_key_offset(&item->key) > root->highest_inode)
570 break;
571 if (btrfs_disk_key_type(&item->key) != key_type)
572 continue;
573 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
574 continue;
575 filp->f_pos = btrfs_disk_key_offset(&item->key);
576 advance = 1;
577 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
578 over = filldir(dirent, (const char *)(di + 1),
579 btrfs_dir_name_len(di),
580 btrfs_disk_key_offset(&item->key),
581 btrfs_disk_key_objectid(&di->location), d_type);
582 if (over)
583 goto nopos;
584 }
585 filp->f_pos++;
586 nopos:
587 ret = 0;
588 err:
589 btrfs_release_path(root, path);
590 btrfs_free_path(path);
591 mutex_unlock(&root->fs_info->fs_mutex);
592 return ret;
593 }
594
595 static void btrfs_put_super (struct super_block * sb)
596 {
597 struct btrfs_root *root = btrfs_sb(sb);
598 int ret;
599
600 ret = close_ctree(root);
601 if (ret) {
602 printk("close ctree returns %d\n", ret);
603 }
604 sb->s_fs_info = NULL;
605 }
606
607 static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
608 {
609 struct inode * inode;
610 struct dentry * root_dentry;
611 struct btrfs_super_block *disk_super;
612 struct btrfs_root *tree_root;
613 struct btrfs_inode *bi;
614
615 sb->s_maxbytes = MAX_LFS_FILESIZE;
616 sb->s_magic = BTRFS_SUPER_MAGIC;
617 sb->s_op = &btrfs_super_ops;
618 sb->s_time_gran = 1;
619
620 tree_root = open_ctree(sb);
621
622 if (!tree_root) {
623 printk("btrfs: open_ctree failed\n");
624 return -EIO;
625 }
626 sb->s_fs_info = tree_root;
627 disk_super = tree_root->fs_info->disk_super;
628 printk("read in super total blocks %Lu root %Lu\n",
629 btrfs_super_total_blocks(disk_super),
630 btrfs_super_root_dir(disk_super));
631
632 inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
633 tree_root);
634 bi = BTRFS_I(inode);
635 bi->location.objectid = inode->i_ino;
636 bi->location.offset = 0;
637 bi->location.flags = 0;
638 bi->root = tree_root;
639 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
640
641 if (!inode)
642 return -ENOMEM;
643 if (inode->i_state & I_NEW) {
644 btrfs_read_locked_inode(inode);
645 unlock_new_inode(inode);
646 }
647
648 root_dentry = d_alloc_root(inode);
649 if (!root_dentry) {
650 iput(inode);
651 return -ENOMEM;
652 }
653 sb->s_root = root_dentry;
654
655 return 0;
656 }
657
658 static void fill_inode_item(struct btrfs_inode_item *item,
659 struct inode *inode)
660 {
661 btrfs_set_inode_uid(item, inode->i_uid);
662 btrfs_set_inode_gid(item, inode->i_gid);
663 btrfs_set_inode_size(item, inode->i_size);
664 btrfs_set_inode_mode(item, inode->i_mode);
665 btrfs_set_inode_nlink(item, inode->i_nlink);
666 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
667 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
668 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
669 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
670 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
671 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
672 btrfs_set_inode_nblocks(item, inode->i_blocks);
673 btrfs_set_inode_generation(item, inode->i_generation);
674 }
675
676 static int btrfs_update_inode(struct btrfs_trans_handle *trans,
677 struct btrfs_root *root,
678 struct inode *inode)
679 {
680 struct btrfs_inode_item *inode_item;
681 struct btrfs_path *path;
682 int ret;
683
684 path = btrfs_alloc_path();
685 BUG_ON(!path);
686 btrfs_init_path(path);
687 ret = btrfs_lookup_inode(trans, root, path,
688 &BTRFS_I(inode)->location, 1);
689 if (ret) {
690 if (ret > 0)
691 ret = -ENOENT;
692 goto failed;
693 }
694
695 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
696 path->slots[0],
697 struct btrfs_inode_item);
698
699 fill_inode_item(inode_item, inode);
700 btrfs_mark_buffer_dirty(path->nodes[0]);
701 ret = 0;
702 failed:
703 btrfs_release_path(root, path);
704 btrfs_free_path(path);
705 return ret;
706 }
707
708 static int btrfs_write_inode(struct inode *inode, int wait)
709 {
710 struct btrfs_root *root = BTRFS_I(inode)->root;
711 struct btrfs_trans_handle *trans;
712 int ret;
713
714 mutex_lock(&root->fs_info->fs_mutex);
715 trans = btrfs_start_transaction(root, 1);
716 ret = btrfs_update_inode(trans, root, inode);
717 if (wait)
718 btrfs_commit_transaction(trans, root);
719 else
720 btrfs_end_transaction(trans, root);
721 mutex_unlock(&root->fs_info->fs_mutex);
722 return ret;
723 }
724
725 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
726 struct inode *dir, int mode)
727 {
728 struct inode *inode;
729 struct btrfs_inode_item inode_item;
730 struct btrfs_root *root = BTRFS_I(dir)->root;
731 struct btrfs_key *location;
732 int ret;
733 u64 objectid;
734
735 inode = new_inode(dir->i_sb);
736 if (!inode)
737 return ERR_PTR(-ENOMEM);
738
739 BTRFS_I(inode)->root = BTRFS_I(dir)->root;
740 ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
741 BUG_ON(ret);
742
743 inode->i_uid = current->fsuid;
744 inode->i_gid = current->fsgid;
745 inode->i_mode = mode;
746 inode->i_ino = objectid;
747 inode->i_blocks = 0;
748 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
749 fill_inode_item(&inode_item, inode);
750 location = &BTRFS_I(inode)->location;
751 location->objectid = objectid;
752 location->flags = 0;
753 location->offset = 0;
754 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
755
756 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
757 BUG_ON(ret);
758
759 insert_inode_hash(inode);
760 return inode;
761 }
762
763 static int btrfs_add_link(struct btrfs_trans_handle *trans,
764 struct dentry *dentry, struct inode *inode)
765 {
766 int ret;
767 struct btrfs_key key;
768 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
769 key.objectid = inode->i_ino;
770 key.flags = 0;
771 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
772 key.offset = 0;
773
774 ret = btrfs_insert_dir_item(trans, root,
775 dentry->d_name.name, dentry->d_name.len,
776 dentry->d_parent->d_inode->i_ino,
777 &key, 0);
778 if (ret == 0) {
779 dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
780 ret = btrfs_update_inode(trans, root,
781 dentry->d_parent->d_inode);
782 }
783 return ret;
784 }
785
786 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
787 struct dentry *dentry, struct inode *inode)
788 {
789 int err = btrfs_add_link(trans, dentry, inode);
790 if (!err) {
791 d_instantiate(dentry, inode);
792 return 0;
793 }
794 if (err > 0)
795 err = -EEXIST;
796 return err;
797 }
798
799 static int btrfs_create(struct inode *dir, struct dentry *dentry,
800 int mode, struct nameidata *nd)
801 {
802 struct btrfs_trans_handle *trans;
803 struct btrfs_root *root = BTRFS_I(dir)->root;
804 struct inode *inode;
805 int err;
806 int drop_inode = 0;
807
808 mutex_lock(&root->fs_info->fs_mutex);
809 trans = btrfs_start_transaction(root, 1);
810 inode = btrfs_new_inode(trans, dir, mode);
811 err = PTR_ERR(inode);
812 if (IS_ERR(inode))
813 goto out_unlock;
814 // FIXME mark the inode dirty
815 err = btrfs_add_nondir(trans, dentry, inode);
816 if (err)
817 drop_inode = 1;
818 else {
819 inode->i_mapping->a_ops = &btrfs_aops;
820 inode->i_fop = &btrfs_file_operations;
821 inode->i_op = &btrfs_file_inode_operations;
822 }
823 dir->i_sb->s_dirt = 1;
824 out_unlock:
825 btrfs_end_transaction(trans, root);
826 mutex_unlock(&root->fs_info->fs_mutex);
827
828 if (drop_inode) {
829 inode_dec_link_count(inode);
830 iput(inode);
831 }
832 return err;
833 }
834
835 static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
836 struct inode *inode, struct inode *dir)
837 {
838 struct btrfs_root *root = BTRFS_I(dir)->root;
839 int ret;
840 char buf[2];
841 struct btrfs_key key;
842
843 buf[0] = '.';
844 buf[1] = '.';
845
846 key.objectid = inode->i_ino;
847 key.offset = 0;
848 key.flags = 0;
849 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
850
851 ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino,
852 &key, 1);
853 if (ret)
854 goto error;
855 key.objectid = dir->i_ino;
856 ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino,
857 &key, 1);
858 if (ret)
859 goto error;
860 inode->i_size = 6;
861 ret = btrfs_update_inode(trans, root, inode);
862 error:
863 return ret;
864 }
865
866 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
867 {
868 struct inode *inode;
869 struct btrfs_trans_handle *trans;
870 struct btrfs_root *root = BTRFS_I(dir)->root;
871 int err = 0;
872 int drop_on_err = 0;
873
874 mutex_lock(&root->fs_info->fs_mutex);
875 trans = btrfs_start_transaction(root, 1);
876 if (IS_ERR(trans)) {
877 err = PTR_ERR(trans);
878 goto out_unlock;
879 }
880 inode = btrfs_new_inode(trans, dir, S_IFDIR | mode);
881 if (IS_ERR(inode)) {
882 err = PTR_ERR(inode);
883 goto out_fail;
884 }
885 drop_on_err = 1;
886 inode->i_op = &btrfs_dir_inode_operations;
887 inode->i_fop = &btrfs_dir_file_operations;
888
889 err = btrfs_make_empty_dir(trans, inode, dir);
890 if (err)
891 goto out_fail;
892 err = btrfs_add_link(trans, dentry, inode);
893 if (err)
894 goto out_fail;
895 d_instantiate(dentry, inode);
896 drop_on_err = 0;
897
898 out_fail:
899 btrfs_end_transaction(trans, root);
900 out_unlock:
901 mutex_unlock(&root->fs_info->fs_mutex);
902 if (drop_on_err)
903 iput(inode);
904 return err;
905 }
906
907 static int btrfs_sync_fs(struct super_block *sb, int wait)
908 {
909 struct btrfs_trans_handle *trans;
910 struct btrfs_root *root;
911 int ret;
912 root = btrfs_sb(sb);
913
914 sb->s_dirt = 0;
915 if (!wait) {
916 filemap_flush(root->fs_info->btree_inode->i_mapping);
917 return 0;
918 }
919 filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
920 mutex_lock(&root->fs_info->fs_mutex);
921 trans = btrfs_start_transaction(root, 1);
922 ret = btrfs_commit_transaction(trans, root);
923 sb->s_dirt = 0;
924 BUG_ON(ret);
925 printk("btrfs sync_fs\n");
926 mutex_unlock(&root->fs_info->fs_mutex);
927 return 0;
928 }
929
930 #if 0
931 static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
932 struct buffer_head *result, int create)
933 {
934 struct btrfs_root *root = btrfs_sb(inode->i_sb);
935 struct btrfs_path *path;
936 struct btrfs_key key;
937 struct btrfs_leaf *leaf;
938 int num_bytes = result->b_size;
939 int item_size;
940 int ret;
941 u64 pos;
942 char *ptr;
943 int copy_size;
944 int err = 0;
945 char *safe_ptr;
946 char *data_ptr;
947
948 path = btrfs_alloc_path();
949 BUG_ON(!path);
950
951 WARN_ON(create);
952 if (create) {
953 return 0;
954 }
955 pos = iblock << inode->i_blkbits;
956 key.objectid = inode->i_ino;
957 key.flags = 0;
958 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
959 ptr = kmap(result->b_page);
960 safe_ptr = ptr;
961 ptr += (pos & (PAGE_CACHE_SIZE -1));
962 again:
963 key.offset = pos;
964 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
965 if (ret) {
966 if (ret < 0)
967 err = ret;
968 else
969 err = 0;
970 goto out;
971 }
972 leaf = btrfs_buffer_leaf(path->nodes[0]);
973 item_size = btrfs_item_size(leaf->items + path->slots[0]);
974 copy_size = min(num_bytes, item_size);
975 data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
976 WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
977 memcpy(ptr, data_ptr, copy_size);
978 pos += copy_size;
979 num_bytes -= copy_size;
980 WARN_ON(num_bytes < 0);
981 ptr += copy_size;
982 btrfs_release_path(root, path);
983 if (num_bytes != 0) {
984 if (pos >= i_size_read(inode))
985 memset(ptr, 0, num_bytes);
986 else
987 goto again;
988 }
989 set_buffer_uptodate(result);
990 map_bh(result, inode->i_sb, 0);
991 err = 0;
992 out:
993 btrfs_free_path(path);
994 kunmap(result->b_page);
995 return err;
996 }
997 #endif
998
999 static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
1000 struct buffer_head *result, int create)
1001 {
1002 int ret;
1003 int err = 0;
1004 u64 blocknr;
1005 u64 extent_start = 0;
1006 u64 extent_end = 0;
1007 u64 objectid = inode->i_ino;
1008 struct btrfs_path *path;
1009 struct btrfs_root *root = BTRFS_I(inode)->root;
1010 struct btrfs_trans_handle *trans = NULL;
1011 struct btrfs_file_extent_item *item;
1012 struct btrfs_leaf *leaf;
1013 struct btrfs_disk_key *found_key;
1014
1015 path = btrfs_alloc_path();
1016 BUG_ON(!path);
1017 btrfs_init_path(path);
1018 if (create)
1019 trans = btrfs_start_transaction(root, 1);
1020
1021 ret = btrfs_lookup_file_extent(trans, root, path,
1022 inode->i_ino,
1023 iblock << inode->i_blkbits, 1);
1024 if (ret < 0) {
1025 err = ret;
1026 goto out;
1027 }
1028
1029 if (ret != 0) {
1030 if (path->slots[0] == 0) {
1031 btrfs_release_path(root, path);
1032 goto allocate;
1033 }
1034 path->slots[0]--;
1035 }
1036
1037 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
1038 struct btrfs_file_extent_item);
1039 leaf = btrfs_buffer_leaf(path->nodes[0]);
1040 blocknr = btrfs_file_extent_disk_blocknr(item);
1041 blocknr += btrfs_file_extent_offset(item);
1042
1043 /* exact match found, use it, FIXME, deal with extents
1044 * other than the page size
1045 */
1046 if (ret == 0) {
1047 err = 0;
1048 BUG_ON(btrfs_file_extent_disk_num_blocks(item) != 1);
1049 if (btrfs_file_extent_generation(item) != trans->transid) {
1050 struct btrfs_key ins;
1051 ret = btrfs_alloc_extent(trans, root, 1,
1052 blocknr, (u64)-1, &ins);
1053 BUG_ON(ret);
1054 btrfs_set_file_extent_disk_blocknr(item, ins.objectid);
1055 mark_buffer_dirty(path->nodes[0]);
1056 ret = btrfs_free_extent(trans, root,
1057 blocknr, 1, 0);
1058 BUG_ON(ret);
1059 blocknr = ins.objectid;
1060
1061 }
1062 map_bh(result, inode->i_sb, blocknr);
1063 goto out;
1064 }
1065
1066 /* are we inside the extent that was found? */
1067 found_key = &leaf->items[path->slots[0]].key;
1068 if (btrfs_disk_key_objectid(found_key) != objectid ||
1069 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
1070 extent_end = 0;
1071 extent_start = 0;
1072 btrfs_release_path(root, path);
1073 goto allocate;
1074 }
1075
1076 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
1077 extent_start = extent_start >> inode->i_blkbits;
1078 extent_start += btrfs_file_extent_offset(item);
1079 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
1080 if (iblock >= extent_start && iblock < extent_end) {
1081 err = 0;
1082 map_bh(result, inode->i_sb, blocknr + iblock - extent_start);
1083 goto out;
1084 }
1085 allocate:
1086 /* ok, create a new extent */
1087 if (!create) {
1088 err = 0;
1089 goto out;
1090 }
1091 ret = btrfs_alloc_file_extent(trans, root, objectid,
1092 iblock << inode->i_blkbits,
1093 1, extent_end, &blocknr);
1094 if (ret) {
1095 err = ret;
1096 goto out;
1097 }
1098 inode->i_blocks += inode->i_sb->s_blocksize >> 9;
1099 set_buffer_new(result);
1100 map_bh(result, inode->i_sb, blocknr);
1101
1102 out:
1103 btrfs_release_path(root, path);
1104 btrfs_free_path(path);
1105 if (trans)
1106 btrfs_end_transaction(trans, root);
1107 return err;
1108 }
1109
1110 static int btrfs_get_block(struct inode *inode, sector_t iblock,
1111 struct buffer_head *result, int create)
1112 {
1113 int err;
1114 struct btrfs_root *root = BTRFS_I(inode)->root;
1115 mutex_lock(&root->fs_info->fs_mutex);
1116 err = btrfs_get_block_lock(inode, iblock, result, create);
1117 // err = btrfs_get_block_inline(inode, iblock, result, create);
1118 mutex_unlock(&root->fs_info->fs_mutex);
1119 return err;
1120 }
1121
1122 static int btrfs_prepare_write(struct file *file, struct page *page,
1123 unsigned from, unsigned to)
1124 {
1125 return nobh_prepare_write(page, from, to, btrfs_get_block);
1126 }
1127 static int btrfs_commit_write(struct file *file, struct page *page,
1128 unsigned from, unsigned to)
1129 {
1130 return nobh_commit_write(file, page, from, to);
1131 }
1132
1133 static void btrfs_write_super(struct super_block *sb)
1134 {
1135 btrfs_sync_fs(sb, 1);
1136 }
1137
1138 static int btrfs_readpage(struct file *file, struct page *page)
1139 {
1140 return mpage_readpage(page, btrfs_get_block);
1141 }
1142
1143 static int btrfs_readpages(struct file *file, struct address_space *mapping,
1144 struct list_head *pages, unsigned nr_pages)
1145 {
1146 return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block);
1147 }
1148
1149 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1150 {
1151 return nobh_writepage(page, btrfs_get_block, wbc);
1152 }
1153
1154 static void btrfs_truncate(struct inode *inode)
1155 {
1156 struct btrfs_root *root = BTRFS_I(inode)->root;
1157 int ret;
1158 struct btrfs_trans_handle *trans;
1159
1160 if (!S_ISREG(inode->i_mode))
1161 return;
1162 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1163 return;
1164
1165 nobh_truncate_page(inode->i_mapping, inode->i_size);
1166
1167 /* FIXME, add redo link to tree so we don't leak on crash */
1168 mutex_lock(&root->fs_info->fs_mutex);
1169 trans = btrfs_start_transaction(root, 1);
1170 ret = btrfs_truncate_in_trans(trans, root, inode);
1171 BUG_ON(ret);
1172 ret = btrfs_end_transaction(trans, root);
1173 BUG_ON(ret);
1174 mutex_unlock(&root->fs_info->fs_mutex);
1175 mark_inode_dirty(inode);
1176 }
1177
1178 static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
1179 struct page **prepared_pages,
1180 const char __user * buf)
1181 {
1182 long page_fault = 0;
1183 int i;
1184 int offset = pos & (PAGE_CACHE_SIZE - 1);
1185
1186 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
1187 size_t count = min_t(size_t,
1188 PAGE_CACHE_SIZE - offset, write_bytes);
1189 struct page *page = prepared_pages[i];
1190 fault_in_pages_readable(buf, count);
1191
1192 /* Copy data from userspace to the current page */
1193 kmap(page);
1194 page_fault = __copy_from_user(page_address(page) + offset,
1195 buf, count);
1196 /* Flush processor's dcache for this page */
1197 flush_dcache_page(page);
1198 kunmap(page);
1199 buf += count;
1200 write_bytes -= count;
1201
1202 if (page_fault)
1203 break;
1204 }
1205 return page_fault ? -EFAULT : 0;
1206 }
1207
1208 static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1209 {
1210 size_t i;
1211 for (i = 0; i < num_pages; i++) {
1212 if (!pages[i])
1213 break;
1214 unlock_page(pages[i]);
1215 mark_page_accessed(pages[i]);
1216 page_cache_release(pages[i]);
1217 }
1218 }
1219 static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1220 struct btrfs_root *root,
1221 struct file *file,
1222 struct page **pages,
1223 size_t num_pages,
1224 loff_t pos,
1225 size_t write_bytes)
1226 {
1227 int i;
1228 int offset;
1229 int err = 0;
1230 int ret;
1231 int this_write;
1232 struct inode *inode = file->f_path.dentry->d_inode;
1233
1234 for (i = 0; i < num_pages; i++) {
1235 offset = pos & (PAGE_CACHE_SIZE -1);
1236 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1237 /* FIXME, one block at a time */
1238
1239 mutex_lock(&root->fs_info->fs_mutex);
1240 trans = btrfs_start_transaction(root, 1);
1241 btrfs_csum_file_block(trans, root, inode->i_ino,
1242 pages[i]->index << PAGE_CACHE_SHIFT,
1243 kmap(pages[i]), PAGE_CACHE_SIZE);
1244 kunmap(pages[i]);
1245 SetPageChecked(pages[i]);
1246 ret = btrfs_end_transaction(trans, root);
1247 BUG_ON(ret);
1248 mutex_unlock(&root->fs_info->fs_mutex);
1249
1250 ret = nobh_commit_write(file, pages[i], offset,
1251 offset + this_write);
1252 pos += this_write;
1253 if (ret) {
1254 err = ret;
1255 goto failed;
1256 }
1257 WARN_ON(this_write > write_bytes);
1258 write_bytes -= this_write;
1259 }
1260 failed:
1261 return err;
1262 }
1263
1264 static int prepare_pages(struct btrfs_trans_handle *trans,
1265 struct btrfs_root *root,
1266 struct file *file,
1267 struct page **pages,
1268 size_t num_pages,
1269 loff_t pos,
1270 size_t write_bytes)
1271 {
1272 int i;
1273 unsigned long index = pos >> PAGE_CACHE_SHIFT;
1274 struct inode *inode = file->f_path.dentry->d_inode;
1275 int offset;
1276 int err = 0;
1277 int ret;
1278 int this_write;
1279 loff_t isize = i_size_read(inode);
1280
1281 memset(pages, 0, num_pages * sizeof(struct page *));
1282
1283 for (i = 0; i < num_pages; i++) {
1284 pages[i] = grab_cache_page(inode->i_mapping, index + i);
1285 if (!pages[i]) {
1286 err = -ENOMEM;
1287 goto failed_release;
1288 }
1289 offset = pos & (PAGE_CACHE_SIZE -1);
1290 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1291 ret = nobh_prepare_write(pages[i], offset,
1292 offset + this_write,
1293 btrfs_get_block);
1294 pos += this_write;
1295 if (ret) {
1296 err = ret;
1297 goto failed_truncate;
1298 }
1299 WARN_ON(this_write > write_bytes);
1300 write_bytes -= this_write;
1301 }
1302 return 0;
1303
1304 failed_release:
1305 btrfs_drop_pages(pages, num_pages);
1306 return err;
1307
1308 failed_truncate:
1309 btrfs_drop_pages(pages, num_pages);
1310 if (pos > isize)
1311 vmtruncate(inode, isize);
1312 return err;
1313 }
1314
1315 static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1316 size_t count, loff_t *ppos)
1317 {
1318 loff_t pos;
1319 size_t num_written = 0;
1320 int err = 0;
1321 int ret = 0;
1322 struct inode *inode = file->f_path.dentry->d_inode;
1323 struct btrfs_root *root = BTRFS_I(inode)->root;
1324 struct page *pages[1];
1325
1326 if (file->f_flags & O_DIRECT)
1327 return -EINVAL;
1328 pos = *ppos;
1329
1330 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1331 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1332 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1333 if (err)
1334 goto out;
1335 if (count == 0)
1336 goto out;
1337 err = remove_suid(file->f_path.dentry);
1338 if (err)
1339 goto out;
1340 file_update_time(file);
1341 mutex_lock(&inode->i_mutex);
1342 while(count > 0) {
1343 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1344 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1345 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
1346 PAGE_CACHE_SHIFT;
1347 ret = prepare_pages(NULL, root, file, pages, num_pages,
1348 pos, write_bytes);
1349 BUG_ON(ret);
1350 ret = btrfs_copy_from_user(pos, num_pages,
1351 write_bytes, pages, buf);
1352 BUG_ON(ret);
1353
1354 ret = dirty_and_release_pages(NULL, root, file, pages,
1355 num_pages, pos, write_bytes);
1356 BUG_ON(ret);
1357 btrfs_drop_pages(pages, num_pages);
1358
1359 buf += write_bytes;
1360 count -= write_bytes;
1361 pos += write_bytes;
1362 num_written += write_bytes;
1363
1364 balance_dirty_pages_ratelimited(inode->i_mapping);
1365 cond_resched();
1366 }
1367 mutex_unlock(&inode->i_mutex);
1368 out:
1369 *ppos = pos;
1370 current->backing_dev_info = NULL;
1371 return num_written ? num_written : err;
1372 }
1373
1374 #if 0
1375 static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
1376 struct page *page, loff_t pos,
1377 size_t offset, size_t write_bytes)
1378 {
1379 struct btrfs_path *path;
1380 struct btrfs_trans_handle *trans;
1381 struct btrfs_key key;
1382 struct btrfs_leaf *leaf;
1383 struct btrfs_key found_key;
1384 int ret;
1385 size_t copy_size = 0;
1386 char *dst = NULL;
1387 int err = 0;
1388 size_t num_written = 0;
1389
1390 path = btrfs_alloc_path();
1391 BUG_ON(!path);
1392 mutex_lock(&root->fs_info->fs_mutex);
1393 trans = btrfs_start_transaction(root, 1);
1394 key.objectid = inode->i_ino;
1395 key.flags = 0;
1396 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
1397
1398 again:
1399 key.offset = pos;
1400 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1401 if (ret < 0) {
1402 err = ret;
1403 goto out;
1404 }
1405 if (ret == 0) {
1406 leaf = btrfs_buffer_leaf(path->nodes[0]);
1407 btrfs_disk_key_to_cpu(&found_key,
1408 &leaf->items[path->slots[0]].key);
1409 copy_size = btrfs_item_size(leaf->items + path->slots[0]);
1410 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1411 copy_size = min(write_bytes, copy_size);
1412 goto copyit;
1413 } else {
1414 int slot = path->slots[0];
1415 if (slot > 0) {
1416 slot--;
1417 }
1418 // FIXME find max key
1419 leaf = btrfs_buffer_leaf(path->nodes[0]);
1420 btrfs_disk_key_to_cpu(&found_key,
1421 &leaf->items[slot].key);
1422 if (found_key.objectid != inode->i_ino)
1423 goto insert;
1424 if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
1425 goto insert;
1426 copy_size = btrfs_item_size(leaf->items + slot);
1427 if (found_key.offset + copy_size <= pos)
1428 goto insert;
1429 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1430 dst += pos - found_key.offset;
1431 copy_size = copy_size - (pos - found_key.offset);
1432 BUG_ON(copy_size < 0);
1433 copy_size = min(write_bytes, copy_size);
1434 WARN_ON(copy_size == 0);
1435 goto copyit;
1436 }
1437 insert:
1438 btrfs_release_path(root, path);
1439 copy_size = min(write_bytes,
1440 (size_t)BTRFS_LEAF_DATA_SIZE(root) -
1441 sizeof(struct btrfs_item) * 4);
1442 ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
1443 BUG_ON(ret);
1444 dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1445 path->slots[0], char);
1446 copyit:
1447 WARN_ON(copy_size == 0);
1448 WARN_ON(dst + copy_size >
1449 btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1450 path->slots[0], char) +
1451 btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
1452 path->slots[0]));
1453 btrfs_memcpy(root, path->nodes[0]->b_data, dst,
1454 page_address(page) + offset, copy_size);
1455 mark_buffer_dirty(path->nodes[0]);
1456 btrfs_release_path(root, path);
1457 pos += copy_size;
1458 offset += copy_size;
1459 num_written += copy_size;
1460 write_bytes -= copy_size;
1461 if (write_bytes)
1462 goto again;
1463 out:
1464 btrfs_free_path(path);
1465 ret = btrfs_end_transaction(trans, root);
1466 BUG_ON(ret);
1467 mutex_unlock(&root->fs_info->fs_mutex);
1468 return num_written ? num_written : err;
1469 }
1470
1471 static ssize_t btrfs_file_inline_write(struct file *file,
1472 const char __user *buf,
1473 size_t count, loff_t *ppos)
1474 {
1475 loff_t pos;
1476 size_t num_written = 0;
1477 int err = 0;
1478 int ret = 0;
1479 struct inode *inode = file->f_path.dentry->d_inode;
1480 struct btrfs_root *root = BTRFS_I(inode)->root;
1481 unsigned long page_index;
1482
1483 if (file->f_flags & O_DIRECT)
1484 return -EINVAL;
1485 pos = *ppos;
1486
1487 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1488 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1489 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1490 if (err)
1491 goto out;
1492 if (count == 0)
1493 goto out;
1494 err = remove_suid(file->f_path.dentry);
1495 if (err)
1496 goto out;
1497 file_update_time(file);
1498 mutex_lock(&inode->i_mutex);
1499 while(count > 0) {
1500 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1501 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1502 struct page *page;
1503
1504 page_index = pos >> PAGE_CACHE_SHIFT;
1505 page = grab_cache_page(inode->i_mapping, page_index);
1506 if (!PageUptodate(page)) {
1507 ret = mpage_readpage(page, btrfs_get_block);
1508 BUG_ON(ret);
1509 lock_page(page);
1510 }
1511 ret = btrfs_copy_from_user(pos, 1,
1512 write_bytes, &page, buf);
1513 BUG_ON(ret);
1514 write_bytes = inline_one_page(root, inode, page, pos,
1515 offset, write_bytes);
1516 SetPageUptodate(page);
1517 if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
1518 i_size_write(inode, pos + write_bytes);
1519 mark_inode_dirty(inode);
1520 }
1521 page_cache_release(page);
1522 unlock_page(page);
1523 if (write_bytes < 0)
1524 goto out_unlock;
1525 buf += write_bytes;
1526 count -= write_bytes;
1527 pos += write_bytes;
1528 num_written += write_bytes;
1529
1530 balance_dirty_pages_ratelimited(inode->i_mapping);
1531 cond_resched();
1532 }
1533 out_unlock:
1534 mutex_unlock(&inode->i_mutex);
1535 out:
1536 *ppos = pos;
1537 current->backing_dev_info = NULL;
1538 return num_written ? num_written : err;
1539 }
1540 #endif
1541
1542 static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
1543 unsigned long offset, unsigned long size)
1544 {
1545 char *kaddr;
1546 unsigned long left, count = desc->count;
1547 struct inode *inode = page->mapping->host;
1548
1549 if (size > count)
1550 size = count;
1551
1552 if (!PageChecked(page)) {
1553 /* FIXME, do it per block */
1554 struct btrfs_root *root = BTRFS_I(inode)->root;
1555 int ret = btrfs_csum_verify_file_block(root,
1556 page->mapping->host->i_ino,
1557 page->index << PAGE_CACHE_SHIFT,
1558 kmap(page), PAGE_CACHE_SIZE);
1559 if (ret) {
1560 printk("failed to verify ino %lu page %lu\n",
1561 page->mapping->host->i_ino,
1562 page->index);
1563 memset(page_address(page), 0, PAGE_CACHE_SIZE);
1564 }
1565 SetPageChecked(page);
1566 kunmap(page);
1567 }
1568 /*
1569 * Faults on the destination of a read are common, so do it before
1570 * taking the kmap.
1571 */
1572 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1573 kaddr = kmap_atomic(page, KM_USER0);
1574 left = __copy_to_user_inatomic(desc->arg.buf,
1575 kaddr + offset, size);
1576 kunmap_atomic(kaddr, KM_USER0);
1577 if (left == 0)
1578 goto success;
1579 }
1580
1581 /* Do it the slow way */
1582 kaddr = kmap(page);
1583 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1584 kunmap(page);
1585
1586 if (left) {
1587 size -= left;
1588 desc->error = -EFAULT;
1589 }
1590 success:
1591 desc->count = count - size;
1592 desc->written += size;
1593 desc->arg.buf += size;
1594 return size;
1595 }
1596
1597 /**
1598 * btrfs_file_aio_read - filesystem read routine
1599 * @iocb: kernel I/O control block
1600 * @iov: io vector request
1601 * @nr_segs: number of segments in the iovec
1602 * @pos: current file position
1603 */
1604 static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1605 unsigned long nr_segs, loff_t pos)
1606 {
1607 struct file *filp = iocb->ki_filp;
1608 ssize_t retval;
1609 unsigned long seg;
1610 size_t count;
1611 loff_t *ppos = &iocb->ki_pos;
1612
1613 count = 0;
1614 for (seg = 0; seg < nr_segs; seg++) {
1615 const struct iovec *iv = &iov[seg];
1616
1617 /*
1618 * If any segment has a negative length, or the cumulative
1619 * length ever wraps negative then return -EINVAL.
1620 */
1621 count += iv->iov_len;
1622 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
1623 return -EINVAL;
1624 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
1625 continue;
1626 if (seg == 0)
1627 return -EFAULT;
1628 nr_segs = seg;
1629 count -= iv->iov_len; /* This segment is no good */
1630 break;
1631 }
1632 retval = 0;
1633 if (count) {
1634 for (seg = 0; seg < nr_segs; seg++) {
1635 read_descriptor_t desc;
1636
1637 desc.written = 0;
1638 desc.arg.buf = iov[seg].iov_base;
1639 desc.count = iov[seg].iov_len;
1640 if (desc.count == 0)
1641 continue;
1642 desc.error = 0;
1643 do_generic_file_read(filp, ppos, &desc,
1644 btrfs_read_actor);
1645 retval += desc.written;
1646 if (desc.error) {
1647 retval = retval ?: desc.error;
1648 break;
1649 }
1650 }
1651 }
1652 return retval;
1653 }
1654
1655 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
1656 {
1657 struct btrfs_trans_handle *trans;
1658 struct btrfs_key key;
1659 struct btrfs_root_item new_root_item;
1660 int ret;
1661 u64 objectid;
1662
1663 mutex_lock(&root->fs_info->fs_mutex);
1664 trans = btrfs_start_transaction(root, 1);
1665 BUG_ON(!trans);
1666
1667 ret = btrfs_update_inode(trans, root, root->inode);
1668 BUG_ON(ret);
1669
1670 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
1671 0, &objectid);
1672 BUG_ON(ret);
1673
1674 memset(&new_root_item, 0, sizeof(new_root_item));
1675 memcpy(&new_root_item, &root->root_item,
1676 sizeof(new_root_item));
1677
1678 key.objectid = objectid;
1679 key.offset = 1;
1680 key.flags = 0;
1681 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
1682 btrfs_set_root_blocknr(&new_root_item, root->node->b_blocknr);
1683
1684 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
1685 &new_root_item);
1686 BUG_ON(ret);
1687
1688 printk("adding snapshot name %.*s root %Lu %Lu %u\n", namelen, name, key.objectid, key.offset, key.flags);
1689
1690 /*
1691 * insert the directory item
1692 */
1693 key.offset = (u64)-1;
1694 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
1695 name, namelen,
1696 root->fs_info->sb->s_root->d_inode->i_ino,
1697 &key, 0);
1698
1699 BUG_ON(ret);
1700
1701 ret = btrfs_inc_root_ref(trans, root);
1702 BUG_ON(ret);
1703
1704 ret = btrfs_commit_transaction(trans, root);
1705 BUG_ON(ret);
1706 mutex_unlock(&root->fs_info->fs_mutex);
1707 return 0;
1708 }
1709
1710 static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
1711 cmd, unsigned long arg)
1712 {
1713 struct btrfs_root *root = BTRFS_I(inode)->root;
1714 struct btrfs_ioctl_vol_args vol_args;
1715 int ret;
1716 int namelen;
1717
1718 if (!root->ref_cows)
1719 return -EINVAL;
1720 switch (cmd) {
1721 case BTRFS_IOC_SNAP_CREATE:
1722 if (copy_from_user(&vol_args,
1723 (struct btrfs_ioctl_vol_args __user *)arg,
1724 sizeof(vol_args)))
1725 return -EFAULT;
1726 namelen = strlen(vol_args.name);
1727 if (namelen > BTRFS_VOL_NAME_MAX)
1728 return -EINVAL;
1729 ret = create_snapshot(root, vol_args.name, namelen);
1730 WARN_ON(ret);
1731 break;
1732 default:
1733 return -ENOTTY;
1734 }
1735 return 0;
1736 }
1737
1738 static struct kmem_cache *btrfs_inode_cachep;
1739 struct kmem_cache *btrfs_trans_handle_cachep;
1740 struct kmem_cache *btrfs_transaction_cachep;
1741 struct kmem_cache *btrfs_bit_radix_cachep;
1742 struct kmem_cache *btrfs_path_cachep;
1743
1744 /*
1745 * Called inside transaction, so use GFP_NOFS
1746 */
1747 static struct inode *btrfs_alloc_inode(struct super_block *sb)
1748 {
1749 struct btrfs_inode *ei;
1750
1751 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
1752 if (!ei)
1753 return NULL;
1754 return &ei->vfs_inode;
1755 }
1756
1757 static void btrfs_destroy_inode(struct inode *inode)
1758 {
1759 WARN_ON(!list_empty(&inode->i_dentry));
1760 WARN_ON(inode->i_data.nrpages);
1761
1762 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
1763 }
1764
1765 static void init_once(void * foo, struct kmem_cache * cachep,
1766 unsigned long flags)
1767 {
1768 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
1769
1770 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1771 SLAB_CTOR_CONSTRUCTOR) {
1772 inode_init_once(&ei->vfs_inode);
1773 }
1774 }
1775
1776 static int init_inodecache(void)
1777 {
1778 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
1779 sizeof(struct btrfs_inode),
1780 0, (SLAB_RECLAIM_ACCOUNT|
1781 SLAB_MEM_SPREAD),
1782 init_once, NULL);
1783 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
1784 sizeof(struct btrfs_trans_handle),
1785 0, (SLAB_RECLAIM_ACCOUNT|
1786 SLAB_MEM_SPREAD),
1787 NULL, NULL);
1788 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
1789 sizeof(struct btrfs_transaction),
1790 0, (SLAB_RECLAIM_ACCOUNT|
1791 SLAB_MEM_SPREAD),
1792 NULL, NULL);
1793 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
1794 sizeof(struct btrfs_transaction),
1795 0, (SLAB_RECLAIM_ACCOUNT|
1796 SLAB_MEM_SPREAD),
1797 NULL, NULL);
1798 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
1799 256,
1800 0, (SLAB_RECLAIM_ACCOUNT|
1801 SLAB_MEM_SPREAD |
1802 SLAB_DESTROY_BY_RCU),
1803 NULL, NULL);
1804 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
1805 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
1806 return -ENOMEM;
1807 return 0;
1808 }
1809
1810 static void destroy_inodecache(void)
1811 {
1812 kmem_cache_destroy(btrfs_inode_cachep);
1813 kmem_cache_destroy(btrfs_trans_handle_cachep);
1814 kmem_cache_destroy(btrfs_transaction_cachep);
1815 kmem_cache_destroy(btrfs_bit_radix_cachep);
1816 kmem_cache_destroy(btrfs_path_cachep);
1817 }
1818
1819 static int btrfs_get_sb(struct file_system_type *fs_type,
1820 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1821 {
1822 return get_sb_bdev(fs_type, flags, dev_name, data,
1823 btrfs_fill_super, mnt);
1824 }
1825
1826 static struct file_system_type btrfs_fs_type = {
1827 .owner = THIS_MODULE,
1828 .name = "btrfs",
1829 .get_sb = btrfs_get_sb,
1830 .kill_sb = kill_block_super,
1831 .fs_flags = FS_REQUIRES_DEV,
1832 };
1833
1834 static struct super_operations btrfs_super_ops = {
1835 .statfs = simple_statfs,
1836 .delete_inode = btrfs_delete_inode,
1837 .put_super = btrfs_put_super,
1838 .read_inode = btrfs_read_locked_inode,
1839 .write_super = btrfs_write_super,
1840 .sync_fs = btrfs_sync_fs,
1841 .write_inode = btrfs_write_inode,
1842 .alloc_inode = btrfs_alloc_inode,
1843 .destroy_inode = btrfs_destroy_inode,
1844 };
1845
1846 static struct inode_operations btrfs_dir_inode_operations = {
1847 .lookup = btrfs_lookup,
1848 .create = btrfs_create,
1849 .unlink = btrfs_unlink,
1850 .mkdir = btrfs_mkdir,
1851 .rmdir = btrfs_rmdir,
1852 };
1853
1854 static struct inode_operations btrfs_dir_ro_inode_operations = {
1855 .lookup = btrfs_lookup,
1856 };
1857
1858 static struct file_operations btrfs_dir_file_operations = {
1859 .llseek = generic_file_llseek,
1860 .read = generic_read_dir,
1861 .readdir = btrfs_readdir,
1862 .ioctl = btrfs_ioctl,
1863 };
1864
1865 static struct address_space_operations btrfs_aops = {
1866 .readpage = btrfs_readpage,
1867 .readpages = btrfs_readpages,
1868 .writepage = btrfs_writepage,
1869 .sync_page = block_sync_page,
1870 .prepare_write = btrfs_prepare_write,
1871 .commit_write = btrfs_commit_write,
1872 };
1873
1874 static struct inode_operations btrfs_file_inode_operations = {
1875 .truncate = btrfs_truncate,
1876 };
1877
1878 static struct file_operations btrfs_file_operations = {
1879 .llseek = generic_file_llseek,
1880 .read = do_sync_read,
1881 .aio_read = btrfs_file_aio_read,
1882 .write = btrfs_file_write,
1883 .mmap = generic_file_mmap,
1884 .open = generic_file_open,
1885 .ioctl = btrfs_ioctl,
1886 };
1887
1888 static int __init init_btrfs_fs(void)
1889 {
1890 int err;
1891 printk("btrfs loaded!\n");
1892 err = init_inodecache();
1893 if (err)
1894 return err;
1895 kset_set_kset_s(&btrfs_subsys, fs_subsys);
1896 err = subsystem_register(&btrfs_subsys);
1897 if (err)
1898 goto out;
1899 return register_filesystem(&btrfs_fs_type);
1900 out:
1901 destroy_inodecache();
1902 return err;
1903 }
1904
1905 static void __exit exit_btrfs_fs(void)
1906 {
1907 destroy_inodecache();
1908 unregister_filesystem(&btrfs_fs_type);
1909 subsystem_unregister(&btrfs_subsys);
1910 printk("btrfs unloaded\n");
1911 }
1912
1913 module_init(init_btrfs_fs)
1914 module_exit(exit_btrfs_fs)
1915
1916 MODULE_LICENSE("GPL");
This page took 0.132643 seconds and 6 git commands to generate.