1 #include <linux/module.h>
2 #include <linux/buffer_head.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>
16 #include "transaction.h"
17 #include "btrfs_inode.h"
20 void btrfs_fsinfo_release(struct kobject *obj)
22 struct btrfs_fs_info *fsinfo = container_of(obj,
23 struct btrfs_fs_info, kobj);
27 struct kobj_type btrfs_fsinfo_ktype = {
28 .release = btrfs_fsinfo_release,
31 struct btrfs_iget_args {
33 struct btrfs_root *root;
36 decl_subsys(btrfs, &btrfs_fsinfo_ktype, NULL);
38 #define BTRFS_SUPER_MAGIC 0x9123682E
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;
48 static void btrfs_read_locked_inode(struct inode *inode)
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;
56 path = btrfs_alloc_path();
58 btrfs_init_path(path);
59 mutex_lock(&root->fs_info->fs_mutex);
61 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
62 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
64 btrfs_free_path(path);
67 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
69 struct btrfs_inode_item);
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);
85 btrfs_free_path(path);
88 mutex_unlock(&root->fs_info->fs_mutex);
90 switch (inode->i_mode & S_IFMT) {
93 init_special_inode(inode, inode->i_mode,
94 btrfs_inode_rdev(inode_item));
98 inode->i_mapping->a_ops = &btrfs_aops;
99 inode->i_fop = &btrfs_file_operations;
100 inode->i_op = &btrfs_file_inode_operations;
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;
107 inode->i_op = &btrfs_dir_inode_operations;
110 // inode->i_op = &page_symlink_inode_operations;
116 btrfs_release_path(root, path);
117 btrfs_free_path(path);
118 mutex_unlock(&root->fs_info->fs_mutex);
119 make_bad_inode(inode);
122 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
123 struct btrfs_root *root,
125 struct dentry *dentry)
127 struct btrfs_path *path;
128 const char *name = dentry->d_name.name;
129 int name_len = dentry->d_name.len;
132 struct btrfs_dir_item *di;
134 path = btrfs_alloc_path();
136 btrfs_init_path(path);
137 ret = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
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);
149 ret = btrfs_del_item(trans, root, path);
152 btrfs_release_path(root, path);
153 dentry->d_inode->i_ctime = dir->i_ctime;
155 btrfs_release_path(root, path);
156 btrfs_free_path(path);
158 inode_dec_link_count(dentry->d_inode);
159 dir->i_size -= name_len * 2;
160 mark_inode_dirty(dir);
165 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
167 struct btrfs_root *root;
168 struct btrfs_trans_handle *trans;
171 root = BTRFS_I(dir)->root;
172 mutex_lock(&root->fs_info->fs_mutex);
173 trans = btrfs_start_transaction(root, 1);
174 ret = btrfs_unlink_trans(trans, root, dir, dentry);
175 btrfs_end_transaction(trans, root);
176 mutex_unlock(&root->fs_info->fs_mutex);
180 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
182 struct inode *inode = dentry->d_inode;
185 struct btrfs_root *root = BTRFS_I(dir)->root;
186 struct btrfs_path *path;
187 struct btrfs_key key;
188 struct btrfs_trans_handle *trans;
189 struct btrfs_key found_key;
191 struct btrfs_leaf *leaf;
192 char *goodnames = "..";
194 path = btrfs_alloc_path();
196 btrfs_init_path(path);
197 mutex_lock(&root->fs_info->fs_mutex);
198 trans = btrfs_start_transaction(root, 1);
199 key.objectid = inode->i_ino;
200 key.offset = (u64)-1;
203 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
209 if (path->slots[0] == 0) {
214 leaf = btrfs_buffer_leaf(path->nodes[0]);
215 btrfs_disk_key_to_cpu(&found_key,
216 &leaf->items[path->slots[0]].key);
217 found_type = btrfs_key_type(&found_key);
218 if (found_key.objectid != inode->i_ino) {
222 if ((found_type != BTRFS_DIR_ITEM_KEY &&
223 found_type != BTRFS_DIR_INDEX_KEY) ||
224 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
225 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
229 ret = btrfs_del_item(trans, root, path);
232 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
234 btrfs_release_path(root, path);
237 btrfs_release_path(root, path);
239 /* now the directory is empty */
240 err = btrfs_unlink_trans(trans, root, dir, dentry);
245 btrfs_release_path(root, path);
246 btrfs_free_path(path);
247 mutex_unlock(&root->fs_info->fs_mutex);
248 ret = btrfs_end_transaction(trans, root);
254 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
255 struct btrfs_root *root,
258 struct btrfs_path *path;
263 path = btrfs_alloc_path();
265 btrfs_init_path(path);
266 ret = btrfs_lookup_inode(trans, root, path,
267 &BTRFS_I(inode)->location, -1);
269 ret = btrfs_del_item(trans, root, path);
271 btrfs_free_path(path);
275 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
276 struct btrfs_root *root,
280 struct btrfs_path *path;
281 struct btrfs_key key;
282 struct btrfs_disk_key *found_key;
283 struct btrfs_leaf *leaf;
284 struct btrfs_file_extent_item *fi = NULL;
285 u64 extent_start = 0;
286 u64 extent_num_blocks = 0;
289 path = btrfs_alloc_path();
291 /* FIXME, add redo link to tree so we don't leak on crash */
292 key.objectid = inode->i_ino;
293 key.offset = (u64)-1;
296 * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys
299 btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY);
301 btrfs_init_path(path);
302 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
307 BUG_ON(path->slots[0] == 0);
310 leaf = btrfs_buffer_leaf(path->nodes[0]);
311 found_key = &leaf->items[path->slots[0]].key;
312 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
314 if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY &&
315 btrfs_disk_key_type(found_key) != BTRFS_INLINE_DATA_KEY &&
316 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
318 if (btrfs_disk_key_offset(found_key) < inode->i_size)
320 if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
321 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
323 struct btrfs_file_extent_item);
324 extent_start = btrfs_file_extent_disk_blocknr(fi);
326 btrfs_file_extent_disk_num_blocks(fi);
327 /* FIXME blocksize != 4096 */
329 btrfs_file_extent_num_blocks(fi) << 3;
334 ret = btrfs_del_item(trans, root, path);
336 btrfs_release_path(root, path);
338 ret = btrfs_free_extent(trans, root, extent_start,
339 extent_num_blocks, 0);
345 btrfs_release_path(root, path);
346 btrfs_free_path(path);
350 static void btrfs_delete_inode(struct inode *inode)
352 struct btrfs_trans_handle *trans;
353 struct btrfs_root *root = BTRFS_I(inode)->root;
356 truncate_inode_pages(&inode->i_data, 0);
357 if (is_bad_inode(inode)) {
361 mutex_lock(&root->fs_info->fs_mutex);
362 trans = btrfs_start_transaction(root, 1);
363 if (S_ISREG(inode->i_mode)) {
364 ret = btrfs_truncate_in_trans(trans, root, inode);
367 btrfs_free_inode(trans, root, inode);
368 btrfs_end_transaction(trans, root);
369 mutex_unlock(&root->fs_info->fs_mutex);
375 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
376 struct btrfs_key *location)
378 const char *name = dentry->d_name.name;
379 int namelen = dentry->d_name.len;
380 struct btrfs_dir_item *di;
381 struct btrfs_path *path;
382 struct btrfs_root *root = BTRFS_I(dir)->root;
385 path = btrfs_alloc_path();
387 btrfs_init_path(path);
388 ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
390 if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) {
391 location->objectid = 0;
395 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
396 struct btrfs_dir_item);
397 btrfs_disk_key_to_cpu(location, &di->location);
399 btrfs_release_path(root, path);
400 btrfs_free_path(path);
404 int fixup_tree_root_location(struct btrfs_root *root,
405 struct btrfs_key *location,
406 struct btrfs_root **sub_root)
408 struct btrfs_path *path;
409 struct btrfs_root_item *ri;
411 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
413 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
416 path = btrfs_alloc_path();
418 mutex_lock(&root->fs_info->fs_mutex);
420 *sub_root = btrfs_read_fs_root(root->fs_info, location);
421 if (IS_ERR(*sub_root))
422 return PTR_ERR(*sub_root);
424 ri = &(*sub_root)->root_item;
425 location->objectid = btrfs_root_dirid(ri);
427 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
428 location->offset = 0;
430 btrfs_free_path(path);
431 mutex_unlock(&root->fs_info->fs_mutex);
435 int btrfs_init_locked_inode(struct inode *inode, void *p)
437 struct btrfs_iget_args *args = p;
438 inode->i_ino = args->ino;
439 BTRFS_I(inode)->root = args->root;
443 int btrfs_find_actor(struct inode *inode, void *opaque)
445 struct btrfs_iget_args *args = opaque;
446 return (args->ino == inode->i_ino &&
447 args->root == BTRFS_I(inode)->root);
450 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
451 struct btrfs_root *root)
454 struct btrfs_iget_args args;
458 inode = iget5_locked(s, objectid, btrfs_find_actor,
459 btrfs_init_locked_inode,
464 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
465 struct nameidata *nd)
467 struct inode * inode;
468 struct btrfs_inode *bi = BTRFS_I(dir);
469 struct btrfs_root *root = bi->root;
470 struct btrfs_root *sub_root = root;
471 struct btrfs_key location;
474 if (dentry->d_name.len > BTRFS_NAME_LEN)
475 return ERR_PTR(-ENAMETOOLONG);
476 mutex_lock(&root->fs_info->fs_mutex);
477 ret = btrfs_inode_by_name(dir, dentry, &location);
478 mutex_unlock(&root->fs_info->fs_mutex);
482 if (location.objectid) {
483 ret = fixup_tree_root_location(root, &location, &sub_root);
487 return ERR_PTR(-ENOENT);
488 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
491 return ERR_PTR(-EACCES);
492 if (inode->i_state & I_NEW) {
493 if (sub_root != root) {
494 printk("adding new root for inode %lu root %p (found %p)\n", inode->i_ino, sub_root, BTRFS_I(inode)->root);
496 sub_root->inode = inode;
498 BTRFS_I(inode)->root = sub_root;
499 memcpy(&BTRFS_I(inode)->location, &location,
501 btrfs_read_locked_inode(inode);
502 unlock_new_inode(inode);
505 return d_splice_alias(inode, dentry);
508 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
510 struct inode *inode = filp->f_path.dentry->d_inode;
511 struct btrfs_root *root = BTRFS_I(inode)->root;
512 struct btrfs_item *item;
513 struct btrfs_dir_item *di;
514 struct btrfs_key key;
515 struct btrfs_path *path;
518 struct btrfs_leaf *leaf;
521 unsigned char d_type = DT_UNKNOWN;
523 int key_type = BTRFS_DIR_ITEM_KEY;
525 /* FIXME, use a real flag for deciding about the key type */
526 if (root->fs_info->tree_root == root)
527 key_type = BTRFS_DIR_ITEM_KEY;
528 mutex_lock(&root->fs_info->fs_mutex);
529 key.objectid = inode->i_ino;
531 btrfs_set_key_type(&key, key_type);
532 key.offset = filp->f_pos;
533 path = btrfs_alloc_path();
534 btrfs_init_path(path);
535 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
540 leaf = btrfs_buffer_leaf(path->nodes[0]);
541 nritems = btrfs_header_nritems(&leaf->header);
542 slot = path->slots[0];
543 if (advance || slot >= nritems) {
544 if (slot >= nritems -1) {
545 ret = btrfs_next_leaf(root, path);
548 leaf = btrfs_buffer_leaf(path->nodes[0]);
549 nritems = btrfs_header_nritems(&leaf->header);
550 slot = path->slots[0];
557 item = leaf->items + slot;
558 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
560 if (btrfs_disk_key_type(&item->key) != key_type)
562 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
564 filp->f_pos = btrfs_disk_key_offset(&item->key);
566 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
567 over = filldir(dirent, (const char *)(di + 1),
568 btrfs_dir_name_len(di),
569 btrfs_disk_key_offset(&item->key),
570 btrfs_disk_key_objectid(&di->location), d_type);
578 btrfs_release_path(root, path);
579 btrfs_free_path(path);
580 mutex_unlock(&root->fs_info->fs_mutex);
584 static void btrfs_put_super (struct super_block * sb)
586 struct btrfs_root *root = btrfs_sb(sb);
589 ret = close_ctree(root);
591 printk("close ctree returns %d\n", ret);
593 sb->s_fs_info = NULL;
596 static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
598 struct inode * inode;
599 struct dentry * root_dentry;
600 struct btrfs_super_block *disk_super;
601 struct btrfs_root *tree_root;
602 struct btrfs_inode *bi;
604 sb->s_maxbytes = MAX_LFS_FILESIZE;
605 sb->s_magic = BTRFS_SUPER_MAGIC;
606 sb->s_op = &btrfs_super_ops;
609 tree_root = open_ctree(sb);
612 printk("btrfs: open_ctree failed\n");
615 sb->s_fs_info = tree_root;
616 disk_super = tree_root->fs_info->disk_super;
617 printk("read in super total blocks %Lu root %Lu\n",
618 btrfs_super_total_blocks(disk_super),
619 btrfs_super_root_dir(disk_super));
621 inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
624 bi->location.objectid = inode->i_ino;
625 bi->location.offset = 0;
626 bi->location.flags = 0;
627 bi->root = tree_root;
628 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
632 if (inode->i_state & I_NEW) {
633 btrfs_read_locked_inode(inode);
634 unlock_new_inode(inode);
637 root_dentry = d_alloc_root(inode);
642 sb->s_root = root_dentry;
647 static void fill_inode_item(struct btrfs_inode_item *item,
650 btrfs_set_inode_uid(item, inode->i_uid);
651 btrfs_set_inode_gid(item, inode->i_gid);
652 btrfs_set_inode_size(item, inode->i_size);
653 btrfs_set_inode_mode(item, inode->i_mode);
654 btrfs_set_inode_nlink(item, inode->i_nlink);
655 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
656 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
657 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
658 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
659 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
660 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
661 btrfs_set_inode_nblocks(item, inode->i_blocks);
662 btrfs_set_inode_generation(item, inode->i_generation);
665 static int btrfs_update_inode(struct btrfs_trans_handle *trans,
666 struct btrfs_root *root,
669 struct btrfs_inode_item *inode_item;
670 struct btrfs_path *path;
673 path = btrfs_alloc_path();
675 btrfs_init_path(path);
676 ret = btrfs_lookup_inode(trans, root, path,
677 &BTRFS_I(inode)->location, 1);
684 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
686 struct btrfs_inode_item);
688 fill_inode_item(inode_item, inode);
689 btrfs_mark_buffer_dirty(path->nodes[0]);
692 btrfs_release_path(root, path);
693 btrfs_free_path(path);
697 static int btrfs_write_inode(struct inode *inode, int wait)
699 struct btrfs_root *root = BTRFS_I(inode)->root;
700 struct btrfs_trans_handle *trans;
703 mutex_lock(&root->fs_info->fs_mutex);
704 trans = btrfs_start_transaction(root, 1);
705 ret = btrfs_update_inode(trans, root, inode);
707 btrfs_commit_transaction(trans, root);
709 btrfs_end_transaction(trans, root);
710 mutex_unlock(&root->fs_info->fs_mutex);
714 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
715 struct btrfs_root *root,
716 u64 objectid, int mode)
719 struct btrfs_inode_item inode_item;
720 struct btrfs_key *location;
723 inode = new_inode(root->fs_info->sb);
725 return ERR_PTR(-ENOMEM);
727 BTRFS_I(inode)->root = root;
729 inode->i_uid = current->fsuid;
730 inode->i_gid = current->fsgid;
731 inode->i_mode = mode;
732 inode->i_ino = objectid;
734 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
735 fill_inode_item(&inode_item, inode);
736 location = &BTRFS_I(inode)->location;
737 location->objectid = objectid;
739 location->offset = 0;
740 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
742 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
745 insert_inode_hash(inode);
749 static int btrfs_add_link(struct btrfs_trans_handle *trans,
750 struct dentry *dentry, struct inode *inode)
753 struct btrfs_key key;
754 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
755 key.objectid = inode->i_ino;
757 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
760 ret = btrfs_insert_dir_item(trans, root,
761 dentry->d_name.name, dentry->d_name.len,
762 dentry->d_parent->d_inode->i_ino,
765 dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
766 ret = btrfs_update_inode(trans, root,
767 dentry->d_parent->d_inode);
772 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
773 struct dentry *dentry, struct inode *inode)
775 int err = btrfs_add_link(trans, dentry, inode);
777 d_instantiate(dentry, inode);
785 static int btrfs_create(struct inode *dir, struct dentry *dentry,
786 int mode, struct nameidata *nd)
788 struct btrfs_trans_handle *trans;
789 struct btrfs_root *root = BTRFS_I(dir)->root;
795 mutex_lock(&root->fs_info->fs_mutex);
796 trans = btrfs_start_transaction(root, 1);
798 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
804 inode = btrfs_new_inode(trans, root, objectid, mode);
805 err = PTR_ERR(inode);
808 // FIXME mark the inode dirty
809 err = btrfs_add_nondir(trans, dentry, inode);
813 inode->i_mapping->a_ops = &btrfs_aops;
814 inode->i_fop = &btrfs_file_operations;
815 inode->i_op = &btrfs_file_inode_operations;
817 dir->i_sb->s_dirt = 1;
819 btrfs_end_transaction(trans, root);
820 mutex_unlock(&root->fs_info->fs_mutex);
823 inode_dec_link_count(inode);
829 static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
830 struct btrfs_root *root,
831 u64 objectid, u64 dirid)
835 struct btrfs_key key;
840 key.objectid = objectid;
843 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
845 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
849 key.objectid = dirid;
850 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
858 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
861 struct btrfs_trans_handle *trans;
862 struct btrfs_root *root = BTRFS_I(dir)->root;
867 mutex_lock(&root->fs_info->fs_mutex);
868 trans = btrfs_start_transaction(root, 1);
870 err = PTR_ERR(trans);
874 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
880 inode = btrfs_new_inode(trans, root, objectid, S_IFDIR | mode);
882 err = PTR_ERR(inode);
886 inode->i_op = &btrfs_dir_inode_operations;
887 inode->i_fop = &btrfs_dir_file_operations;
889 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
894 err = btrfs_update_inode(trans, root, inode);
897 err = btrfs_add_link(trans, dentry, inode);
900 d_instantiate(dentry, inode);
904 btrfs_end_transaction(trans, root);
906 mutex_unlock(&root->fs_info->fs_mutex);
912 static int btrfs_sync_fs(struct super_block *sb, int wait)
914 struct btrfs_trans_handle *trans;
915 struct btrfs_root *root;
921 filemap_flush(root->fs_info->btree_inode->i_mapping);
924 filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
925 mutex_lock(&root->fs_info->fs_mutex);
926 trans = btrfs_start_transaction(root, 1);
927 ret = btrfs_commit_transaction(trans, root);
930 printk("btrfs sync_fs\n");
931 mutex_unlock(&root->fs_info->fs_mutex);
936 static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
937 struct buffer_head *result, int create)
939 struct btrfs_root *root = btrfs_sb(inode->i_sb);
940 struct btrfs_path *path;
941 struct btrfs_key key;
942 struct btrfs_leaf *leaf;
943 int num_bytes = result->b_size;
953 path = btrfs_alloc_path();
960 pos = iblock << inode->i_blkbits;
961 key.objectid = inode->i_ino;
963 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
964 ptr = kmap(result->b_page);
966 ptr += (pos & (PAGE_CACHE_SIZE -1));
969 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
977 leaf = btrfs_buffer_leaf(path->nodes[0]);
978 item_size = btrfs_item_size(leaf->items + path->slots[0]);
979 copy_size = min(num_bytes, item_size);
980 data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
981 WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
982 memcpy(ptr, data_ptr, copy_size);
984 num_bytes -= copy_size;
985 WARN_ON(num_bytes < 0);
987 btrfs_release_path(root, path);
988 if (num_bytes != 0) {
989 if (pos >= i_size_read(inode))
990 memset(ptr, 0, num_bytes);
994 set_buffer_uptodate(result);
995 map_bh(result, inode->i_sb, 0);
998 btrfs_free_path(path);
999 kunmap(result->b_page);
1004 static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
1005 struct buffer_head *result, int create)
1010 u64 extent_start = 0;
1012 u64 objectid = inode->i_ino;
1013 struct btrfs_path *path;
1014 struct btrfs_root *root = BTRFS_I(inode)->root;
1015 struct btrfs_trans_handle *trans = NULL;
1016 struct btrfs_file_extent_item *item;
1017 struct btrfs_leaf *leaf;
1018 struct btrfs_disk_key *found_key;
1020 path = btrfs_alloc_path();
1022 btrfs_init_path(path);
1024 trans = btrfs_start_transaction(root, 1);
1028 ret = btrfs_lookup_file_extent(trans, root, path,
1030 iblock << inode->i_blkbits, create);
1037 if (path->slots[0] == 0) {
1038 btrfs_release_path(root, path);
1044 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
1045 struct btrfs_file_extent_item);
1046 leaf = btrfs_buffer_leaf(path->nodes[0]);
1047 blocknr = btrfs_file_extent_disk_blocknr(item);
1048 blocknr += btrfs_file_extent_offset(item);
1050 /* exact match found, use it, FIXME, deal with extents
1051 * other than the page size
1053 if (0 && ret == 0) {
1056 btrfs_file_extent_generation(item) != trans->transid) {
1057 struct btrfs_key ins;
1058 ret = btrfs_alloc_extent(trans, root, 1,
1059 blocknr, (u64)-1, &ins);
1061 btrfs_set_file_extent_disk_blocknr(item, ins.objectid);
1062 mark_buffer_dirty(path->nodes[0]);
1063 ret = btrfs_free_extent(trans, root,
1066 blocknr = ins.objectid;
1069 btrfs_map_bh_to_logical(root, result, blocknr);
1073 /* are we inside the extent that was found? */
1074 found_key = &leaf->items[path->slots[0]].key;
1075 if (btrfs_disk_key_objectid(found_key) != objectid ||
1076 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
1079 btrfs_release_path(root, path);
1083 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
1084 extent_start = extent_start >> inode->i_blkbits;
1085 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
1086 if (iblock >= extent_start && iblock < extent_end) {
1088 btrfs_map_bh_to_logical(root, result, blocknr + iblock -
1093 /* ok, create a new extent */
1099 ret = btrfs_alloc_file_extent(trans, root, objectid,
1100 iblock << inode->i_blkbits,
1101 1, extent_end, &blocknr);
1106 inode->i_blocks += inode->i_sb->s_blocksize >> 9;
1107 set_buffer_new(result);
1108 map_bh(result, inode->i_sb, blocknr);
1110 btrfs_map_bh_to_logical(root, result, blocknr);
1113 btrfs_release_path(root, path);
1114 btrfs_free_path(path);
1116 btrfs_end_transaction(trans, root);
1120 static int btrfs_get_block(struct inode *inode, sector_t iblock,
1121 struct buffer_head *result, int create)
1124 struct btrfs_root *root = BTRFS_I(inode)->root;
1125 mutex_lock(&root->fs_info->fs_mutex);
1126 err = btrfs_get_block_lock(inode, iblock, result, create);
1127 // err = btrfs_get_block_inline(inode, iblock, result, create);
1128 mutex_unlock(&root->fs_info->fs_mutex);
1132 static int btrfs_prepare_write(struct file *file, struct page *page,
1133 unsigned from, unsigned to)
1135 return nobh_prepare_write(page, from, to, btrfs_get_block);
1137 static int btrfs_commit_write(struct file *file, struct page *page,
1138 unsigned from, unsigned to)
1140 return nobh_commit_write(file, page, from, to);
1143 static void btrfs_write_super(struct super_block *sb)
1145 btrfs_sync_fs(sb, 1);
1148 static int btrfs_readpage(struct file *file, struct page *page)
1150 return mpage_readpage(page, btrfs_get_block);
1153 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1155 return nobh_writepage(page, btrfs_get_block, wbc);
1158 static void btrfs_truncate(struct inode *inode)
1160 struct btrfs_root *root = BTRFS_I(inode)->root;
1162 struct btrfs_trans_handle *trans;
1164 if (!S_ISREG(inode->i_mode))
1166 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1169 nobh_truncate_page(inode->i_mapping, inode->i_size);
1171 /* FIXME, add redo link to tree so we don't leak on crash */
1172 mutex_lock(&root->fs_info->fs_mutex);
1173 trans = btrfs_start_transaction(root, 1);
1174 ret = btrfs_truncate_in_trans(trans, root, inode);
1176 ret = btrfs_end_transaction(trans, root);
1178 mutex_unlock(&root->fs_info->fs_mutex);
1179 mark_inode_dirty(inode);
1182 static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
1183 struct page **prepared_pages,
1184 const char __user * buf)
1186 long page_fault = 0;
1188 int offset = pos & (PAGE_CACHE_SIZE - 1);
1190 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
1191 size_t count = min_t(size_t,
1192 PAGE_CACHE_SIZE - offset, write_bytes);
1193 struct page *page = prepared_pages[i];
1194 fault_in_pages_readable(buf, count);
1196 /* Copy data from userspace to the current page */
1198 page_fault = __copy_from_user(page_address(page) + offset,
1200 /* Flush processor's dcache for this page */
1201 flush_dcache_page(page);
1204 write_bytes -= count;
1209 return page_fault ? -EFAULT : 0;
1212 static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1215 for (i = 0; i < num_pages; i++) {
1218 unlock_page(pages[i]);
1219 mark_page_accessed(pages[i]);
1220 page_cache_release(pages[i]);
1223 static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1224 struct btrfs_root *root,
1226 struct page **pages,
1236 struct inode *inode = file->f_path.dentry->d_inode;
1238 for (i = 0; i < num_pages; i++) {
1239 offset = pos & (PAGE_CACHE_SIZE -1);
1240 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1241 /* FIXME, one block at a time */
1243 mutex_lock(&root->fs_info->fs_mutex);
1244 trans = btrfs_start_transaction(root, 1);
1245 btrfs_csum_file_block(trans, root, inode->i_ino,
1246 pages[i]->index << PAGE_CACHE_SHIFT,
1247 kmap(pages[i]), PAGE_CACHE_SIZE);
1249 SetPageChecked(pages[i]);
1250 ret = btrfs_end_transaction(trans, root);
1252 mutex_unlock(&root->fs_info->fs_mutex);
1254 ret = nobh_commit_write(file, pages[i], offset,
1255 offset + this_write);
1261 WARN_ON(this_write > write_bytes);
1262 write_bytes -= this_write;
1268 static int drop_extents(struct btrfs_trans_handle *trans,
1269 struct btrfs_root *root,
1270 struct inode *inode,
1274 struct btrfs_key key;
1275 struct btrfs_leaf *leaf;
1277 struct btrfs_file_extent_item *extent;
1280 struct btrfs_file_extent_item old;
1281 struct btrfs_path *path;
1282 u64 search_start = start;
1284 path = btrfs_alloc_path();
1288 btrfs_release_path(root, path);
1289 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1294 if (path->slots[0] == 0) {
1302 leaf = btrfs_buffer_leaf(path->nodes[0]);
1303 slot = path->slots[0];
1304 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
1305 extent = btrfs_item_ptr(leaf, slot,
1306 struct btrfs_file_extent_item);
1307 extent_end = key.offset +
1308 (btrfs_file_extent_num_blocks(extent) <<
1310 if (key.offset >= end || key.objectid != inode->i_ino) {
1314 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1316 if (search_start >= extent_end)
1318 search_start = extent_end;
1320 if (end < extent_end && end >= key.offset) {
1321 memcpy(&old, extent, sizeof(old));
1322 ret = btrfs_inc_extent_ref(trans, root,
1323 btrfs_file_extent_disk_blocknr(&old),
1324 btrfs_file_extent_disk_num_blocks(&old));
1329 if (start > key.offset) {
1332 /* truncate existing extent */
1334 WARN_ON(start & (root->blocksize - 1));
1335 new_num = (start - key.offset) >> inode->i_blkbits;
1336 old_num = btrfs_file_extent_num_blocks(extent);
1337 inode->i_blocks -= (old_num - new_num) << 3;
1338 btrfs_set_file_extent_num_blocks(extent, new_num);
1339 mark_buffer_dirty(path->nodes[0]);
1343 u64 disk_num_blocks;
1344 disk_blocknr = btrfs_file_extent_disk_blocknr(extent);
1346 btrfs_file_extent_disk_num_blocks(extent);
1347 ret = btrfs_del_item(trans, root, path);
1350 btrfs_file_extent_num_blocks(extent) << 3;
1351 btrfs_release_path(root, path);
1352 ret = btrfs_free_extent(trans, root, disk_blocknr,
1353 disk_num_blocks, 0);
1356 if (!bookend && search_start >= end) {
1364 /* create bookend */
1365 struct btrfs_key ins;
1366 ins.objectid = inode->i_ino;
1369 btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
1371 btrfs_release_path(root, path);
1372 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1375 extent = btrfs_item_ptr(
1376 btrfs_buffer_leaf(path->nodes[0]),
1378 struct btrfs_file_extent_item);
1379 btrfs_set_file_extent_disk_blocknr(extent,
1380 btrfs_file_extent_disk_blocknr(&old));
1381 btrfs_set_file_extent_disk_num_blocks(extent,
1382 btrfs_file_extent_disk_num_blocks(&old));
1384 btrfs_set_file_extent_offset(extent,
1385 btrfs_file_extent_offset(&old) +
1386 ((end - key.offset) >> inode->i_blkbits));
1387 WARN_ON(btrfs_file_extent_num_blocks(&old) <
1388 (end - key.offset) >> inode->i_blkbits);
1389 btrfs_set_file_extent_num_blocks(extent,
1390 btrfs_file_extent_num_blocks(&old) -
1391 ((end - key.offset) >> inode->i_blkbits));
1393 btrfs_set_file_extent_generation(extent,
1394 btrfs_file_extent_generation(&old));
1395 btrfs_mark_buffer_dirty(path->nodes[0]);
1397 btrfs_file_extent_num_blocks(extent) << 3;
1403 btrfs_free_path(path);
1407 static int prepare_pages(struct btrfs_root *root,
1409 struct page **pages,
1412 unsigned long first_index,
1413 unsigned long last_index,
1415 u64 alloc_extent_start)
1418 unsigned long index = pos >> PAGE_CACHE_SHIFT;
1419 struct inode *inode = file->f_path.dentry->d_inode;
1423 struct buffer_head *bh;
1424 struct buffer_head *head;
1425 loff_t isize = i_size_read(inode);
1427 memset(pages, 0, num_pages * sizeof(struct page *));
1429 for (i = 0; i < num_pages; i++) {
1430 pages[i] = grab_cache_page(inode->i_mapping, index + i);
1433 goto failed_release;
1435 offset = pos & (PAGE_CACHE_SIZE -1);
1436 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1437 create_empty_buffers(pages[i], root->fs_info->sb->s_blocksize,
1438 (1 << BH_Uptodate));
1439 head = page_buffers(pages[i]);
1442 err = btrfs_map_bh_to_logical(root, bh,
1443 alloc_extent_start);
1446 goto failed_truncate;
1447 bh = bh->b_this_page;
1448 alloc_extent_start++;
1449 } while (bh != head);
1451 WARN_ON(this_write > write_bytes);
1452 write_bytes -= this_write;
1457 btrfs_drop_pages(pages, num_pages);
1461 btrfs_drop_pages(pages, num_pages);
1463 vmtruncate(inode, isize);
1467 static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1468 size_t count, loff_t *ppos)
1471 size_t num_written = 0;
1474 struct inode *inode = file->f_path.dentry->d_inode;
1475 struct btrfs_root *root = BTRFS_I(inode)->root;
1476 struct page *pages[8];
1477 struct page *pinned[2] = { NULL, NULL };
1478 unsigned long first_index;
1479 unsigned long last_index;
1482 u64 alloc_extent_start;
1483 struct btrfs_trans_handle *trans;
1484 struct btrfs_key ins;
1486 if (file->f_flags & O_DIRECT)
1489 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1490 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1491 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1496 err = remove_suid(file->f_path.dentry);
1499 file_update_time(file);
1501 start_pos = pos & ~((u64)PAGE_CACHE_SIZE - 1);
1502 num_blocks = (count + pos - start_pos + root->blocksize - 1) >>
1505 mutex_lock(&inode->i_mutex);
1506 first_index = pos >> PAGE_CACHE_SHIFT;
1507 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
1509 if ((first_index << PAGE_CACHE_SHIFT) < inode->i_size &&
1510 (pos & (PAGE_CACHE_SIZE - 1))) {
1511 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
1512 if (!PageUptodate(pinned[0])) {
1513 ret = mpage_readpage(pinned[0], btrfs_get_block);
1516 unlock_page(pinned[0]);
1519 if (first_index != last_index &&
1520 (last_index << PAGE_CACHE_SHIFT) < inode->i_size &&
1521 (count & (PAGE_CACHE_SIZE - 1))) {
1522 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
1523 if (!PageUptodate(pinned[1])) {
1524 ret = mpage_readpage(pinned[1], btrfs_get_block);
1527 unlock_page(pinned[1]);
1531 mutex_lock(&root->fs_info->fs_mutex);
1532 trans = btrfs_start_transaction(root, 1);
1535 mutex_unlock(&root->fs_info->fs_mutex);
1538 /* FIXME blocksize != 4096 */
1539 inode->i_blocks += num_blocks << 3;
1540 if (start_pos < inode->i_size) {
1541 /* FIXME blocksize != pagesize */
1542 ret = drop_extents(trans, root, inode,
1544 (pos + count + root->blocksize -1) &
1545 ~((u64)root->blocksize - 1));
1547 ret = btrfs_alloc_extent(trans, root, num_blocks, 1,
1550 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
1551 start_pos, ins.objectid, ins.offset);
1553 alloc_extent_start = ins.objectid;
1554 ret = btrfs_end_transaction(trans, root);
1555 mutex_unlock(&root->fs_info->fs_mutex);
1558 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1559 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1560 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
1563 memset(pages, 0, sizeof(pages));
1564 ret = prepare_pages(root, file, pages, num_pages,
1565 pos, first_index, last_index,
1566 write_bytes, alloc_extent_start);
1569 /* FIXME blocks != pagesize */
1570 alloc_extent_start += num_pages;
1571 ret = btrfs_copy_from_user(pos, num_pages,
1572 write_bytes, pages, buf);
1575 ret = dirty_and_release_pages(NULL, root, file, pages,
1576 num_pages, pos, write_bytes);
1578 btrfs_drop_pages(pages, num_pages);
1581 count -= write_bytes;
1583 num_written += write_bytes;
1585 balance_dirty_pages_ratelimited(inode->i_mapping);
1589 mutex_unlock(&inode->i_mutex);
1592 page_cache_release(pinned[0]);
1594 page_cache_release(pinned[1]);
1596 current->backing_dev_info = NULL;
1597 mark_inode_dirty(inode);
1598 return num_written ? num_written : err;
1602 static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
1603 struct page *page, loff_t pos,
1604 size_t offset, size_t write_bytes)
1606 struct btrfs_path *path;
1607 struct btrfs_trans_handle *trans;
1608 struct btrfs_key key;
1609 struct btrfs_leaf *leaf;
1610 struct btrfs_key found_key;
1612 size_t copy_size = 0;
1615 size_t num_written = 0;
1617 path = btrfs_alloc_path();
1619 mutex_lock(&root->fs_info->fs_mutex);
1620 trans = btrfs_start_transaction(root, 1);
1621 key.objectid = inode->i_ino;
1623 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
1627 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1633 leaf = btrfs_buffer_leaf(path->nodes[0]);
1634 btrfs_disk_key_to_cpu(&found_key,
1635 &leaf->items[path->slots[0]].key);
1636 copy_size = btrfs_item_size(leaf->items + path->slots[0]);
1637 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1638 copy_size = min(write_bytes, copy_size);
1641 int slot = path->slots[0];
1645 // FIXME find max key
1646 leaf = btrfs_buffer_leaf(path->nodes[0]);
1647 btrfs_disk_key_to_cpu(&found_key,
1648 &leaf->items[slot].key);
1649 if (found_key.objectid != inode->i_ino)
1651 if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
1653 copy_size = btrfs_item_size(leaf->items + slot);
1654 if (found_key.offset + copy_size <= pos)
1656 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1657 dst += pos - found_key.offset;
1658 copy_size = copy_size - (pos - found_key.offset);
1659 BUG_ON(copy_size < 0);
1660 copy_size = min(write_bytes, copy_size);
1661 WARN_ON(copy_size == 0);
1665 btrfs_release_path(root, path);
1666 copy_size = min(write_bytes,
1667 (size_t)BTRFS_LEAF_DATA_SIZE(root) -
1668 sizeof(struct btrfs_item) * 4);
1669 ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
1671 dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1672 path->slots[0], char);
1674 WARN_ON(copy_size == 0);
1675 WARN_ON(dst + copy_size >
1676 btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1677 path->slots[0], char) +
1678 btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
1680 btrfs_memcpy(root, path->nodes[0]->b_data, dst,
1681 page_address(page) + offset, copy_size);
1682 mark_buffer_dirty(path->nodes[0]);
1683 btrfs_release_path(root, path);
1685 offset += copy_size;
1686 num_written += copy_size;
1687 write_bytes -= copy_size;
1691 btrfs_free_path(path);
1692 ret = btrfs_end_transaction(trans, root);
1694 mutex_unlock(&root->fs_info->fs_mutex);
1695 return num_written ? num_written : err;
1698 static ssize_t btrfs_file_inline_write(struct file *file,
1699 const char __user *buf,
1700 size_t count, loff_t *ppos)
1703 size_t num_written = 0;
1706 struct inode *inode = file->f_path.dentry->d_inode;
1707 struct btrfs_root *root = BTRFS_I(inode)->root;
1708 unsigned long page_index;
1710 if (file->f_flags & O_DIRECT)
1714 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1715 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1716 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1721 err = remove_suid(file->f_path.dentry);
1724 file_update_time(file);
1725 mutex_lock(&inode->i_mutex);
1727 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1728 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1731 page_index = pos >> PAGE_CACHE_SHIFT;
1732 page = grab_cache_page(inode->i_mapping, page_index);
1733 if (!PageUptodate(page)) {
1734 ret = mpage_readpage(page, btrfs_get_block);
1738 ret = btrfs_copy_from_user(pos, 1,
1739 write_bytes, &page, buf);
1741 write_bytes = inline_one_page(root, inode, page, pos,
1742 offset, write_bytes);
1743 SetPageUptodate(page);
1744 if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
1745 i_size_write(inode, pos + write_bytes);
1746 mark_inode_dirty(inode);
1748 page_cache_release(page);
1750 if (write_bytes < 0)
1753 count -= write_bytes;
1755 num_written += write_bytes;
1757 balance_dirty_pages_ratelimited(inode->i_mapping);
1761 mutex_unlock(&inode->i_mutex);
1764 current->backing_dev_info = NULL;
1765 return num_written ? num_written : err;
1769 static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
1770 unsigned long offset, unsigned long size)
1773 unsigned long left, count = desc->count;
1774 struct inode *inode = page->mapping->host;
1779 if (!PageChecked(page)) {
1780 /* FIXME, do it per block */
1781 struct btrfs_root *root = BTRFS_I(inode)->root;
1782 int ret = btrfs_csum_verify_file_block(root,
1783 page->mapping->host->i_ino,
1784 page->index << PAGE_CACHE_SHIFT,
1785 kmap(page), PAGE_CACHE_SIZE);
1787 printk("failed to verify ino %lu page %lu\n",
1788 page->mapping->host->i_ino,
1790 memset(page_address(page), 0, PAGE_CACHE_SIZE);
1792 SetPageChecked(page);
1796 * Faults on the destination of a read are common, so do it before
1799 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1800 kaddr = kmap_atomic(page, KM_USER0);
1801 left = __copy_to_user_inatomic(desc->arg.buf,
1802 kaddr + offset, size);
1803 kunmap_atomic(kaddr, KM_USER0);
1808 /* Do it the slow way */
1810 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1815 desc->error = -EFAULT;
1818 desc->count = count - size;
1819 desc->written += size;
1820 desc->arg.buf += size;
1825 * btrfs_file_aio_read - filesystem read routine
1826 * @iocb: kernel I/O control block
1827 * @iov: io vector request
1828 * @nr_segs: number of segments in the iovec
1829 * @pos: current file position
1831 static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1832 unsigned long nr_segs, loff_t pos)
1834 struct file *filp = iocb->ki_filp;
1838 loff_t *ppos = &iocb->ki_pos;
1841 for (seg = 0; seg < nr_segs; seg++) {
1842 const struct iovec *iv = &iov[seg];
1845 * If any segment has a negative length, or the cumulative
1846 * length ever wraps negative then return -EINVAL.
1848 count += iv->iov_len;
1849 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
1851 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
1856 count -= iv->iov_len; /* This segment is no good */
1861 for (seg = 0; seg < nr_segs; seg++) {
1862 read_descriptor_t desc;
1865 desc.arg.buf = iov[seg].iov_base;
1866 desc.count = iov[seg].iov_len;
1867 if (desc.count == 0)
1870 do_generic_file_read(filp, ppos, &desc,
1872 retval += desc.written;
1874 retval = retval ?: desc.error;
1882 static int create_subvol(struct btrfs_root *root, char *name, int namelen)
1884 struct btrfs_trans_handle *trans;
1885 struct btrfs_key key;
1886 struct btrfs_root_item root_item;
1887 struct btrfs_inode_item *inode_item;
1888 struct buffer_head *subvol;
1889 struct btrfs_leaf *leaf;
1890 struct btrfs_root *new_root;
1891 struct inode *inode;
1894 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
1896 mutex_lock(&root->fs_info->fs_mutex);
1897 trans = btrfs_start_transaction(root, 1);
1900 subvol = btrfs_alloc_free_block(trans, root);
1901 leaf = btrfs_buffer_leaf(subvol);
1902 btrfs_set_header_nritems(&leaf->header, 0);
1903 btrfs_set_header_level(&leaf->header, 0);
1904 btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
1905 btrfs_set_header_generation(&leaf->header, trans->transid);
1906 memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
1907 sizeof(leaf->header.fsid));
1909 inode_item = &root_item.inode;
1910 memset(inode_item, 0, sizeof(*inode_item));
1911 btrfs_set_inode_generation(inode_item, 1);
1912 btrfs_set_inode_size(inode_item, 3);
1913 btrfs_set_inode_nlink(inode_item, 1);
1914 btrfs_set_inode_nblocks(inode_item, 1);
1915 btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
1917 btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
1918 btrfs_set_root_refs(&root_item, 1);
1920 mark_buffer_dirty(subvol);
1924 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
1928 btrfs_set_root_dirid(&root_item, new_dirid);
1930 key.objectid = objectid;
1933 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
1934 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
1939 * insert the directory item
1941 key.offset = (u64)-1;
1942 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
1944 root->fs_info->sb->s_root->d_inode->i_ino,
1948 ret = btrfs_commit_transaction(trans, root);
1951 new_root = btrfs_read_fs_root(root->fs_info, &key);
1954 trans = btrfs_start_transaction(new_root, 1);
1957 inode = btrfs_new_inode(trans, new_root, new_dirid, S_IFDIR | 0700);
1958 inode->i_op = &btrfs_dir_inode_operations;
1959 inode->i_fop = &btrfs_dir_file_operations;
1961 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
1966 ret = btrfs_update_inode(trans, new_root, inode);
1969 ret = btrfs_commit_transaction(trans, new_root);
1974 mutex_unlock(&root->fs_info->fs_mutex);
1978 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
1980 struct btrfs_trans_handle *trans;
1981 struct btrfs_key key;
1982 struct btrfs_root_item new_root_item;
1986 if (!root->ref_cows)
1989 mutex_lock(&root->fs_info->fs_mutex);
1990 trans = btrfs_start_transaction(root, 1);
1993 ret = btrfs_update_inode(trans, root, root->inode);
1996 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2000 memcpy(&new_root_item, &root->root_item,
2001 sizeof(new_root_item));
2003 key.objectid = objectid;
2006 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2007 btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
2009 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2014 * insert the directory item
2016 key.offset = (u64)-1;
2017 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2019 root->fs_info->sb->s_root->d_inode->i_ino,
2024 ret = btrfs_inc_root_ref(trans, root);
2027 ret = btrfs_commit_transaction(trans, root);
2029 mutex_unlock(&root->fs_info->fs_mutex);
2033 static int add_disk(struct btrfs_root *root, char *name, int namelen)
2035 struct block_device *bdev;
2036 struct btrfs_path *path;
2037 struct super_block *sb = root->fs_info->sb;
2038 struct btrfs_root *dev_root = root->fs_info->dev_root;
2039 struct btrfs_trans_handle *trans;
2040 struct btrfs_device_item *dev_item;
2041 struct btrfs_key key;
2048 printk("adding disk %s\n", name);
2049 path = btrfs_alloc_path();
2052 num_blocks = btrfs_super_total_blocks(root->fs_info->disk_super);
2053 bdev = open_bdev_excl(name, O_RDWR, sb);
2055 ret = PTR_ERR(bdev);
2056 printk("open bdev excl failed ret %d\n", ret);
2059 set_blocksize(bdev, sb->s_blocksize);
2060 new_blocks = bdev->bd_inode->i_size >> sb->s_blocksize_bits;
2061 key.objectid = num_blocks;
2062 key.offset = new_blocks;
2064 btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY);
2066 mutex_lock(&dev_root->fs_info->fs_mutex);
2067 trans = btrfs_start_transaction(dev_root, 1);
2068 item_size = sizeof(*dev_item) + namelen;
2069 printk("insert empty on %Lu %Lu %u size %d\n", num_blocks, new_blocks, key.flags, item_size);
2070 ret = btrfs_insert_empty_item(trans, dev_root, path, &key, item_size);
2072 printk("insert failed %d\n", ret);
2073 close_bdev_excl(bdev);
2078 dev_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
2079 path->slots[0], struct btrfs_device_item);
2080 btrfs_set_device_pathlen(dev_item, namelen);
2081 memcpy(dev_item + 1, name, namelen);
2083 device_id = btrfs_super_last_device_id(root->fs_info->disk_super) + 1;
2084 btrfs_set_super_last_device_id(root->fs_info->disk_super, device_id);
2085 btrfs_set_device_id(dev_item, device_id);
2086 mark_buffer_dirty(path->nodes[0]);
2088 ret = btrfs_insert_dev_radix(root, bdev, device_id, num_blocks,
2092 btrfs_set_super_total_blocks(root->fs_info->disk_super,
2093 num_blocks + new_blocks);
2094 i_size_write(root->fs_info->btree_inode,
2095 (num_blocks + new_blocks) <<
2096 root->fs_info->btree_inode->i_blkbits);
2100 ret = btrfs_commit_transaction(trans, dev_root);
2102 mutex_unlock(&root->fs_info->fs_mutex);
2104 btrfs_free_path(path);
2109 static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
2110 cmd, unsigned long arg)
2112 struct btrfs_root *root = BTRFS_I(inode)->root;
2113 struct btrfs_ioctl_vol_args vol_args;
2116 struct btrfs_path *path;
2120 case BTRFS_IOC_SNAP_CREATE:
2121 if (copy_from_user(&vol_args,
2122 (struct btrfs_ioctl_vol_args __user *)arg,
2125 namelen = strlen(vol_args.name);
2126 if (namelen > BTRFS_VOL_NAME_MAX)
2128 path = btrfs_alloc_path();
2131 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2132 mutex_lock(&root->fs_info->fs_mutex);
2133 ret = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2135 vol_args.name, namelen, 0);
2136 mutex_unlock(&root->fs_info->fs_mutex);
2137 btrfs_free_path(path);
2141 if (root == root->fs_info->tree_root)
2142 ret = create_subvol(root, vol_args.name, namelen);
2144 ret = create_snapshot(root, vol_args.name, namelen);
2147 case BTRFS_IOC_ADD_DISK:
2148 if (copy_from_user(&vol_args,
2149 (struct btrfs_ioctl_vol_args __user *)arg,
2152 namelen = strlen(vol_args.name);
2153 if (namelen > BTRFS_VOL_NAME_MAX)
2155 vol_args.name[namelen] = '\0';
2156 ret = add_disk(root, vol_args.name, namelen);
2164 static struct kmem_cache *btrfs_inode_cachep;
2165 struct kmem_cache *btrfs_trans_handle_cachep;
2166 struct kmem_cache *btrfs_transaction_cachep;
2167 struct kmem_cache *btrfs_bit_radix_cachep;
2168 struct kmem_cache *btrfs_path_cachep;
2171 * Called inside transaction, so use GFP_NOFS
2173 static struct inode *btrfs_alloc_inode(struct super_block *sb)
2175 struct btrfs_inode *ei;
2177 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2180 return &ei->vfs_inode;
2183 static void btrfs_destroy_inode(struct inode *inode)
2185 WARN_ON(!list_empty(&inode->i_dentry));
2186 WARN_ON(inode->i_data.nrpages);
2188 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2191 static void init_once(void * foo, struct kmem_cache * cachep,
2192 unsigned long flags)
2194 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2196 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
2197 SLAB_CTOR_CONSTRUCTOR) {
2198 inode_init_once(&ei->vfs_inode);
2202 static int init_inodecache(void)
2204 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
2205 sizeof(struct btrfs_inode),
2206 0, (SLAB_RECLAIM_ACCOUNT|
2209 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
2210 sizeof(struct btrfs_trans_handle),
2211 0, (SLAB_RECLAIM_ACCOUNT|
2214 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
2215 sizeof(struct btrfs_transaction),
2216 0, (SLAB_RECLAIM_ACCOUNT|
2219 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
2220 sizeof(struct btrfs_transaction),
2221 0, (SLAB_RECLAIM_ACCOUNT|
2224 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
2226 0, (SLAB_RECLAIM_ACCOUNT|
2228 SLAB_DESTROY_BY_RCU),
2230 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
2231 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
2236 static void destroy_inodecache(void)
2238 kmem_cache_destroy(btrfs_inode_cachep);
2239 kmem_cache_destroy(btrfs_trans_handle_cachep);
2240 kmem_cache_destroy(btrfs_transaction_cachep);
2241 kmem_cache_destroy(btrfs_bit_radix_cachep);
2242 kmem_cache_destroy(btrfs_path_cachep);
2245 static int btrfs_get_sb(struct file_system_type *fs_type,
2246 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2248 return get_sb_bdev(fs_type, flags, dev_name, data,
2249 btrfs_fill_super, mnt);
2252 static struct file_system_type btrfs_fs_type = {
2253 .owner = THIS_MODULE,
2255 .get_sb = btrfs_get_sb,
2256 .kill_sb = kill_block_super,
2257 .fs_flags = FS_REQUIRES_DEV,
2260 static struct super_operations btrfs_super_ops = {
2261 .statfs = simple_statfs,
2262 .delete_inode = btrfs_delete_inode,
2263 .put_super = btrfs_put_super,
2264 .read_inode = btrfs_read_locked_inode,
2265 .write_super = btrfs_write_super,
2266 .sync_fs = btrfs_sync_fs,
2267 .write_inode = btrfs_write_inode,
2268 .alloc_inode = btrfs_alloc_inode,
2269 .destroy_inode = btrfs_destroy_inode,
2272 static struct inode_operations btrfs_dir_inode_operations = {
2273 .lookup = btrfs_lookup,
2274 .create = btrfs_create,
2275 .unlink = btrfs_unlink,
2276 .mkdir = btrfs_mkdir,
2277 .rmdir = btrfs_rmdir,
2280 static struct inode_operations btrfs_dir_ro_inode_operations = {
2281 .lookup = btrfs_lookup,
2284 static struct file_operations btrfs_dir_file_operations = {
2285 .llseek = generic_file_llseek,
2286 .read = generic_read_dir,
2287 .readdir = btrfs_readdir,
2288 .ioctl = btrfs_ioctl,
2291 static struct address_space_operations btrfs_aops = {
2292 .readpage = btrfs_readpage,
2293 .writepage = btrfs_writepage,
2294 .sync_page = block_sync_page,
2295 .prepare_write = btrfs_prepare_write,
2296 .commit_write = btrfs_commit_write,
2299 static struct inode_operations btrfs_file_inode_operations = {
2300 .truncate = btrfs_truncate,
2303 static struct file_operations btrfs_file_operations = {
2304 .llseek = generic_file_llseek,
2305 .read = do_sync_read,
2306 .aio_read = btrfs_file_aio_read,
2307 .write = btrfs_file_write,
2308 .mmap = generic_file_mmap,
2309 .open = generic_file_open,
2310 .ioctl = btrfs_ioctl,
2313 static int __init init_btrfs_fs(void)
2316 printk("btrfs loaded!\n");
2317 err = init_inodecache();
2320 kset_set_kset_s(&btrfs_subsys, fs_subsys);
2321 err = subsystem_register(&btrfs_subsys);
2324 return register_filesystem(&btrfs_fs_type);
2326 destroy_inodecache();
2330 static void __exit exit_btrfs_fs(void)
2332 destroy_inodecache();
2333 unregister_filesystem(&btrfs_fs_type);
2334 subsystem_unregister(&btrfs_subsys);
2335 printk("btrfs unloaded\n");
2338 module_init(init_btrfs_fs)
2339 module_exit(exit_btrfs_fs)
2341 MODULE_LICENSE("GPL");