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 int check_inode(struct inode *inode)
50 struct btrfs_inode *ei = BTRFS_I(inode);
51 WARN_ON(ei->magic != 0xDEADBEEF);
52 WARN_ON(ei->magic2 != 0xDEADBEAF);
56 static void btrfs_read_locked_inode(struct inode *inode)
58 struct btrfs_path *path;
59 struct btrfs_inode_item *inode_item;
60 struct btrfs_root *root = BTRFS_I(inode)->root;
61 struct btrfs_key location;
64 path = btrfs_alloc_path();
66 btrfs_init_path(path);
67 mutex_lock(&root->fs_info->fs_mutex);
71 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
72 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
74 btrfs_free_path(path);
78 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
80 struct btrfs_inode_item);
82 inode->i_mode = btrfs_inode_mode(inode_item);
83 inode->i_nlink = btrfs_inode_nlink(inode_item);
84 inode->i_uid = btrfs_inode_uid(inode_item);
85 inode->i_gid = btrfs_inode_gid(inode_item);
86 inode->i_size = btrfs_inode_size(inode_item);
87 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
88 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
89 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
90 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
91 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
92 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
93 inode->i_blocks = btrfs_inode_nblocks(inode_item);
94 inode->i_generation = btrfs_inode_generation(inode_item);
96 btrfs_free_path(path);
99 mutex_unlock(&root->fs_info->fs_mutex);
101 switch (inode->i_mode & S_IFMT) {
104 init_special_inode(inode, inode->i_mode,
105 btrfs_inode_rdev(inode_item));
109 inode->i_mapping->a_ops = &btrfs_aops;
110 inode->i_fop = &btrfs_file_operations;
111 inode->i_op = &btrfs_file_inode_operations;
114 inode->i_fop = &btrfs_dir_file_operations;
115 if (root == root->fs_info->tree_root)
116 inode->i_op = &btrfs_dir_ro_inode_operations;
118 inode->i_op = &btrfs_dir_inode_operations;
121 // inode->i_op = &page_symlink_inode_operations;
128 btrfs_release_path(root, path);
129 btrfs_free_path(path);
130 mutex_unlock(&root->fs_info->fs_mutex);
131 make_bad_inode(inode);
134 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
135 struct btrfs_root *root,
137 struct dentry *dentry)
139 struct btrfs_path *path;
140 const char *name = dentry->d_name.name;
141 int name_len = dentry->d_name.len;
144 struct btrfs_dir_item *di;
146 path = btrfs_alloc_path();
148 btrfs_init_path(path);
149 ret = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
157 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
158 struct btrfs_dir_item);
159 objectid = btrfs_disk_key_objectid(&di->location);
161 ret = btrfs_del_item(trans, root, path);
164 btrfs_release_path(root, path);
165 ret = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
168 ret = btrfs_del_item(trans, root, path);
170 dentry->d_inode->i_ctime = dir->i_ctime;
172 btrfs_release_path(root, path);
173 btrfs_free_path(path);
175 inode_dec_link_count(dentry->d_inode);
176 dir->i_size -= name_len * 2;
177 mark_inode_dirty(dir);
182 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
184 struct btrfs_root *root;
185 struct btrfs_trans_handle *trans;
188 root = BTRFS_I(dir)->root;
189 mutex_lock(&root->fs_info->fs_mutex);
190 trans = btrfs_start_transaction(root, 1);
191 ret = btrfs_unlink_trans(trans, root, dir, dentry);
192 btrfs_end_transaction(trans, root);
193 mutex_unlock(&root->fs_info->fs_mutex);
197 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
199 struct inode *inode = dentry->d_inode;
202 struct btrfs_root *root = BTRFS_I(dir)->root;
203 struct btrfs_path *path;
204 struct btrfs_key key;
205 struct btrfs_trans_handle *trans;
206 struct btrfs_key found_key;
208 struct btrfs_leaf *leaf;
209 char *goodnames = "..";
211 path = btrfs_alloc_path();
213 btrfs_init_path(path);
214 mutex_lock(&root->fs_info->fs_mutex);
215 trans = btrfs_start_transaction(root, 1);
216 key.objectid = inode->i_ino;
217 key.offset = (u64)-1;
220 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
226 if (path->slots[0] == 0) {
231 leaf = btrfs_buffer_leaf(path->nodes[0]);
232 btrfs_disk_key_to_cpu(&found_key,
233 &leaf->items[path->slots[0]].key);
234 found_type = btrfs_key_type(&found_key);
235 if (found_key.objectid != inode->i_ino) {
239 if ((found_type != BTRFS_DIR_ITEM_KEY &&
240 found_type != BTRFS_DIR_INDEX_KEY) ||
241 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
242 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
246 ret = btrfs_del_item(trans, root, path);
249 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
251 btrfs_release_path(root, path);
254 btrfs_release_path(root, path);
256 /* now the directory is empty */
257 err = btrfs_unlink_trans(trans, root, dir, dentry);
262 btrfs_release_path(root, path);
263 btrfs_free_path(path);
264 mutex_unlock(&root->fs_info->fs_mutex);
265 ret = btrfs_end_transaction(trans, root);
271 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
272 struct btrfs_root *root,
275 u64 objectid = inode->i_ino;
276 struct btrfs_path *path;
277 struct btrfs_inode_map_item *map;
278 struct btrfs_key stat_data_key;
283 path = btrfs_alloc_path();
285 btrfs_init_path(path);
286 ret = btrfs_lookup_inode_map(trans, root, path, objectid, -1);
292 map = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
293 struct btrfs_inode_map_item);
294 btrfs_disk_key_to_cpu(&stat_data_key, &map->key);
295 ret = btrfs_del_item(trans, root->fs_info->inode_root, path);
297 btrfs_release_path(root, path);
299 ret = btrfs_lookup_inode(trans, root, path,
300 &BTRFS_I(inode)->location, -1);
302 ret = btrfs_del_item(trans, root, path);
305 btrfs_release_path(root, path);
306 btrfs_free_path(path);
310 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
311 struct btrfs_root *root,
315 struct btrfs_path *path;
316 struct btrfs_key key;
317 struct btrfs_disk_key *found_key;
318 struct btrfs_leaf *leaf;
319 struct btrfs_file_extent_item *fi = NULL;
320 u64 extent_start = 0;
321 u64 extent_num_blocks = 0;
324 path = btrfs_alloc_path();
326 /* FIXME, add redo link to tree so we don't leak on crash */
327 key.objectid = inode->i_ino;
328 key.offset = (u64)-1;
331 * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys
334 btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY);
336 btrfs_init_path(path);
337 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
342 BUG_ON(path->slots[0] == 0);
345 leaf = btrfs_buffer_leaf(path->nodes[0]);
346 found_key = &leaf->items[path->slots[0]].key;
347 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
349 if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY &&
350 btrfs_disk_key_type(found_key) != BTRFS_INLINE_DATA_KEY &&
351 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
353 if (btrfs_disk_key_offset(found_key) < inode->i_size)
355 if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
356 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
358 struct btrfs_file_extent_item);
359 extent_start = btrfs_file_extent_disk_blocknr(fi);
361 btrfs_file_extent_disk_num_blocks(fi);
363 btrfs_file_extent_num_blocks(fi) >> 9;
368 ret = btrfs_del_item(trans, root, path);
370 btrfs_release_path(root, path);
372 ret = btrfs_free_extent(trans, root, extent_start,
373 extent_num_blocks, 0);
379 btrfs_release_path(root, path);
380 btrfs_free_path(path);
384 static void btrfs_delete_inode(struct inode *inode)
386 struct btrfs_trans_handle *trans;
387 struct btrfs_root *root = BTRFS_I(inode)->root;
390 truncate_inode_pages(&inode->i_data, 0);
391 if (is_bad_inode(inode)) {
395 mutex_lock(&root->fs_info->fs_mutex);
396 trans = btrfs_start_transaction(root, 1);
397 if (S_ISREG(inode->i_mode)) {
398 ret = btrfs_truncate_in_trans(trans, root, inode);
401 btrfs_free_inode(trans, root, inode);
402 btrfs_end_transaction(trans, root);
403 mutex_unlock(&root->fs_info->fs_mutex);
409 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
410 struct btrfs_key *location)
412 const char *name = dentry->d_name.name;
413 int namelen = dentry->d_name.len;
414 struct btrfs_dir_item *di;
415 struct btrfs_path *path;
416 struct btrfs_root *root = BTRFS_I(dir)->root;
419 path = btrfs_alloc_path();
421 btrfs_init_path(path);
422 ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
424 if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) {
425 location->objectid = 0;
429 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
430 struct btrfs_dir_item);
431 btrfs_disk_key_to_cpu(location, &di->location);
433 btrfs_release_path(root, path);
434 btrfs_free_path(path);
439 int fixup_tree_root_location(struct btrfs_root *root,
440 struct btrfs_key *location,
441 struct btrfs_root **sub_root)
443 struct btrfs_path *path;
444 struct btrfs_root_item *ri;
446 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
448 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
451 path = btrfs_alloc_path();
453 mutex_lock(&root->fs_info->fs_mutex);
455 *sub_root = btrfs_read_fs_root(root->fs_info, location);
456 if (IS_ERR(*sub_root))
457 return PTR_ERR(*sub_root);
459 ri = &(*sub_root)->root_item;
460 location->objectid = btrfs_root_dirid(ri);
462 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
463 location->offset = 0;
465 btrfs_free_path(path);
466 mutex_unlock(&root->fs_info->fs_mutex);
470 int btrfs_init_locked_inode(struct inode *inode, void *p)
472 struct btrfs_iget_args *args = p;
473 inode->i_ino = args->ino;
474 BTRFS_I(inode)->root = args->root;
478 int btrfs_find_actor(struct inode *inode, void *opaque)
480 struct btrfs_iget_args *args = opaque;
481 return (args->ino == inode->i_ino &&
482 args->root == BTRFS_I(inode)->root);
485 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
486 struct btrfs_root *root)
489 struct btrfs_iget_args args;
493 inode = iget5_locked(s, objectid, btrfs_find_actor,
494 btrfs_init_locked_inode,
499 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
500 struct nameidata *nd)
502 struct inode * inode;
503 struct btrfs_inode *bi = BTRFS_I(dir);
504 struct btrfs_root *root = bi->root;
505 struct btrfs_root *sub_root = root;
506 struct btrfs_key location;
509 if (dentry->d_name.len > BTRFS_NAME_LEN)
510 return ERR_PTR(-ENAMETOOLONG);
511 mutex_lock(&root->fs_info->fs_mutex);
512 ret = btrfs_inode_by_name(dir, dentry, &location);
513 mutex_unlock(&root->fs_info->fs_mutex);
517 if (location.objectid) {
518 ret = fixup_tree_root_location(root, &location, &sub_root);
522 return ERR_PTR(-ENOENT);
523 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
526 return ERR_PTR(-EACCES);
527 if (inode->i_state & I_NEW) {
528 if (sub_root != root) {
529 ret = radix_tree_insert(
530 &root->fs_info->fs_roots_radix,
531 (unsigned long)sub_root,
533 printk("adding new root for inode %lu root %p (found %p)\n", inode->i_ino, sub_root, BTRFS_I(inode)->root);
535 sub_root->inode = inode;
537 BTRFS_I(inode)->root = sub_root;
538 memcpy(&BTRFS_I(inode)->location, &location,
540 btrfs_read_locked_inode(inode);
541 unlock_new_inode(inode);
546 return d_splice_alias(inode, dentry);
549 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
551 struct inode *inode = filp->f_path.dentry->d_inode;
552 struct btrfs_root *root = BTRFS_I(inode)->root;
553 struct btrfs_item *item;
554 struct btrfs_dir_item *di;
555 struct btrfs_key key;
556 struct btrfs_path *path;
559 struct btrfs_leaf *leaf;
562 unsigned char d_type = DT_UNKNOWN;
564 int key_type = BTRFS_DIR_INDEX_KEY;
566 /* FIXME, use a real flag for deciding about the key type */
567 if (root->fs_info->tree_root == root)
568 key_type = BTRFS_DIR_ITEM_KEY;
570 mutex_lock(&root->fs_info->fs_mutex);
571 key.objectid = inode->i_ino;
573 btrfs_set_key_type(&key, key_type);
574 key.offset = filp->f_pos;
575 path = btrfs_alloc_path();
576 btrfs_init_path(path);
577 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
583 leaf = btrfs_buffer_leaf(path->nodes[0]);
584 nritems = btrfs_header_nritems(&leaf->header);
585 slot = path->slots[0];
586 if (advance || slot >= nritems) {
587 if (slot >= nritems -1) {
588 ret = btrfs_next_leaf(root, path);
591 leaf = btrfs_buffer_leaf(path->nodes[0]);
592 nritems = btrfs_header_nritems(&leaf->header);
593 slot = path->slots[0];
600 item = leaf->items + slot;
601 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
603 if (key_type == BTRFS_DIR_INDEX_KEY &&
604 btrfs_disk_key_offset(&item->key) >
605 root->fs_info->highest_inode)
607 if (btrfs_disk_key_type(&item->key) != key_type)
609 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
611 filp->f_pos = btrfs_disk_key_offset(&item->key);
613 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
614 over = filldir(dirent, (const char *)(di + 1),
615 btrfs_dir_name_len(di),
616 btrfs_disk_key_offset(&item->key),
617 btrfs_disk_key_objectid(&di->location), d_type);
625 btrfs_release_path(root, path);
626 btrfs_free_path(path);
627 mutex_unlock(&root->fs_info->fs_mutex);
631 static void btrfs_put_super (struct super_block * sb)
633 struct btrfs_root *root = btrfs_sb(sb);
636 ret = close_ctree(root);
638 printk("close ctree returns %d\n", ret);
640 sb->s_fs_info = NULL;
643 static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
645 struct inode * inode;
646 struct dentry * root_dentry;
647 struct btrfs_super_block *disk_super;
648 struct btrfs_root *tree_root;
649 struct btrfs_inode *bi;
651 sb->s_maxbytes = MAX_LFS_FILESIZE;
652 sb->s_magic = BTRFS_SUPER_MAGIC;
653 sb->s_op = &btrfs_super_ops;
656 tree_root = open_ctree(sb);
659 printk("btrfs: open_ctree failed\n");
662 sb->s_fs_info = tree_root;
663 disk_super = tree_root->fs_info->disk_super;
664 printk("read in super total blocks %Lu root %Lu\n",
665 btrfs_super_total_blocks(disk_super),
666 btrfs_super_root_dir(disk_super));
668 inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
671 bi->location.objectid = inode->i_ino;
672 bi->location.offset = 0;
673 bi->location.flags = 0;
674 bi->root = tree_root;
675 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
679 if (inode->i_state & I_NEW) {
680 btrfs_read_locked_inode(inode);
681 unlock_new_inode(inode);
684 root_dentry = d_alloc_root(inode);
689 sb->s_root = root_dentry;
694 static void fill_inode_item(struct btrfs_inode_item *item,
697 btrfs_set_inode_uid(item, inode->i_uid);
698 btrfs_set_inode_gid(item, inode->i_gid);
699 btrfs_set_inode_size(item, inode->i_size);
700 btrfs_set_inode_mode(item, inode->i_mode);
701 btrfs_set_inode_nlink(item, inode->i_nlink);
702 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
703 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
704 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
705 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
706 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
707 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
708 btrfs_set_inode_nblocks(item, inode->i_blocks);
709 btrfs_set_inode_generation(item, inode->i_generation);
713 static int btrfs_update_inode(struct btrfs_trans_handle *trans,
714 struct btrfs_root *root,
717 struct btrfs_inode_item *inode_item;
718 struct btrfs_path *path;
721 path = btrfs_alloc_path();
723 btrfs_init_path(path);
725 ret = btrfs_lookup_inode(trans, root, path,
726 &BTRFS_I(inode)->location, 1);
733 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
735 struct btrfs_inode_item);
737 fill_inode_item(inode_item, inode);
738 btrfs_mark_buffer_dirty(path->nodes[0]);
740 btrfs_release_path(root, path);
741 btrfs_free_path(path);
746 static int btrfs_write_inode(struct inode *inode, int wait)
748 struct btrfs_root *root = BTRFS_I(inode)->root;
749 struct btrfs_trans_handle *trans;
752 mutex_lock(&root->fs_info->fs_mutex);
753 trans = btrfs_start_transaction(root, 1);
754 ret = btrfs_update_inode(trans, root, inode);
756 btrfs_commit_transaction(trans, root);
758 btrfs_end_transaction(trans, root);
759 mutex_unlock(&root->fs_info->fs_mutex);
764 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
765 struct inode *dir, int mode)
768 struct btrfs_inode_item inode_item;
769 struct btrfs_root *root = BTRFS_I(dir)->root;
770 struct btrfs_key *key;
774 inode = new_inode(dir->i_sb);
776 return ERR_PTR(-ENOMEM);
778 BTRFS_I(inode)->root = BTRFS_I(dir)->root;
779 key = &BTRFS_I(inode)->location;
781 ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
784 inode->i_uid = current->fsuid;
785 inode->i_gid = current->fsgid;
786 inode->i_mode = mode;
787 inode->i_ino = objectid;
789 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
790 fill_inode_item(&inode_item, inode);
792 key->objectid = objectid;
795 btrfs_set_key_type(key, BTRFS_INODE_ITEM_KEY);
796 ret = btrfs_insert_inode_map(trans, root, objectid, key);
799 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
802 insert_inode_hash(inode);
808 static int btrfs_add_link(struct btrfs_trans_handle *trans,
809 struct dentry *dentry, struct inode *inode)
812 struct btrfs_key key;
813 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
814 key.objectid = inode->i_ino;
816 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
819 ret = btrfs_insert_dir_item(trans, root,
820 dentry->d_name.name, dentry->d_name.len,
821 dentry->d_parent->d_inode->i_ino,
824 dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
825 ret = btrfs_update_inode(trans, root,
826 dentry->d_parent->d_inode);
829 check_inode(dentry->d_parent->d_inode);
833 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
834 struct dentry *dentry, struct inode *inode)
836 int err = btrfs_add_link(trans, dentry, inode);
838 d_instantiate(dentry, inode);
847 static int btrfs_create(struct inode *dir, struct dentry *dentry,
848 int mode, struct nameidata *nd)
850 struct btrfs_trans_handle *trans;
851 struct btrfs_root *root = BTRFS_I(dir)->root;
856 mutex_lock(&root->fs_info->fs_mutex);
857 trans = btrfs_start_transaction(root, 1);
858 inode = btrfs_new_inode(trans, dir, mode);
859 err = PTR_ERR(inode);
862 // FIXME mark the inode dirty
863 err = btrfs_add_nondir(trans, dentry, inode);
867 inode->i_mapping->a_ops = &btrfs_aops;
868 inode->i_fop = &btrfs_file_operations;
869 inode->i_op = &btrfs_file_inode_operations;
871 dir->i_sb->s_dirt = 1;
873 btrfs_end_transaction(trans, root);
874 mutex_unlock(&root->fs_info->fs_mutex);
879 inode_dec_link_count(inode);
885 static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
886 struct inode *inode, struct inode *dir)
888 struct btrfs_root *root = BTRFS_I(dir)->root;
891 struct btrfs_key key;
896 key.objectid = inode->i_ino;
899 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
901 ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino,
905 key.objectid = dir->i_ino;
906 ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino,
911 ret = btrfs_update_inode(trans, root, inode);
916 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
919 struct btrfs_trans_handle *trans;
920 struct btrfs_root *root = BTRFS_I(dir)->root;
924 mutex_lock(&root->fs_info->fs_mutex);
925 trans = btrfs_start_transaction(root, 1);
927 err = PTR_ERR(trans);
930 inode = btrfs_new_inode(trans, dir, S_IFDIR | mode);
932 err = PTR_ERR(inode);
936 inode->i_op = &btrfs_dir_inode_operations;
937 inode->i_fop = &btrfs_dir_file_operations;
939 err = btrfs_make_empty_dir(trans, inode, dir);
942 err = btrfs_add_link(trans, dentry, inode);
945 d_instantiate(dentry, inode);
949 btrfs_end_transaction(trans, root);
951 mutex_unlock(&root->fs_info->fs_mutex);
957 static int btrfs_sync_fs(struct super_block *sb, int wait)
959 struct btrfs_trans_handle *trans;
960 struct btrfs_root *root;
966 filemap_flush(root->fs_info->btree_inode->i_mapping);
969 filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
970 mutex_lock(&root->fs_info->fs_mutex);
971 trans = btrfs_start_transaction(root, 1);
972 ret = btrfs_commit_transaction(trans, root);
975 printk("btrfs sync_fs\n");
976 mutex_unlock(&root->fs_info->fs_mutex);
981 static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
982 struct buffer_head *result, int create)
984 struct btrfs_root *root = btrfs_sb(inode->i_sb);
985 struct btrfs_path *path;
986 struct btrfs_key key;
987 struct btrfs_leaf *leaf;
988 int num_bytes = result->b_size;
998 path = btrfs_alloc_path();
1005 pos = iblock << inode->i_blkbits;
1006 key.objectid = inode->i_ino;
1008 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
1009 ptr = kmap(result->b_page);
1011 ptr += (pos & (PAGE_CACHE_SIZE -1));
1014 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1022 leaf = btrfs_buffer_leaf(path->nodes[0]);
1023 item_size = btrfs_item_size(leaf->items + path->slots[0]);
1024 copy_size = min(num_bytes, item_size);
1025 data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
1026 WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
1027 memcpy(ptr, data_ptr, copy_size);
1029 num_bytes -= copy_size;
1030 WARN_ON(num_bytes < 0);
1032 btrfs_release_path(root, path);
1033 if (num_bytes != 0) {
1034 if (pos >= i_size_read(inode))
1035 memset(ptr, 0, num_bytes);
1039 set_buffer_uptodate(result);
1040 map_bh(result, inode->i_sb, 0);
1043 btrfs_free_path(path);
1044 kunmap(result->b_page);
1049 static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
1050 struct buffer_head *result, int create)
1055 u64 extent_start = 0;
1057 u64 objectid = inode->i_ino;
1058 struct btrfs_path *path;
1059 struct btrfs_root *root = BTRFS_I(inode)->root;
1060 struct btrfs_trans_handle *trans = NULL;
1061 struct btrfs_file_extent_item *item;
1062 struct btrfs_leaf *leaf;
1063 struct btrfs_disk_key *found_key;
1065 path = btrfs_alloc_path();
1067 btrfs_init_path(path);
1069 trans = btrfs_start_transaction(root, 1);
1071 ret = btrfs_lookup_file_extent(trans, root, path,
1073 iblock << inode->i_blkbits, 0);
1080 if (path->slots[0] == 0) {
1081 btrfs_release_path(root, path);
1087 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
1088 struct btrfs_file_extent_item);
1089 leaf = btrfs_buffer_leaf(path->nodes[0]);
1090 blocknr = btrfs_file_extent_disk_blocknr(item);
1091 blocknr += btrfs_file_extent_offset(item);
1093 /* exact match found, use it */
1096 map_bh(result, inode->i_sb, blocknr);
1100 /* are we inside the extent that was found? */
1101 found_key = &leaf->items[path->slots[0]].key;
1102 if (btrfs_disk_key_objectid(found_key) != objectid ||
1103 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
1106 btrfs_release_path(root, path);
1110 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
1111 extent_start = extent_start >> inode->i_blkbits;
1112 extent_start += btrfs_file_extent_offset(item);
1113 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
1114 if (iblock >= extent_start && iblock < extent_end) {
1116 map_bh(result, inode->i_sb, blocknr + iblock - extent_start);
1120 /* ok, create a new extent */
1125 ret = btrfs_alloc_file_extent(trans, root, objectid,
1126 iblock << inode->i_blkbits,
1127 1, extent_end, &blocknr);
1132 inode->i_blocks += inode->i_sb->s_blocksize >> 9;
1133 set_buffer_new(result);
1134 map_bh(result, inode->i_sb, blocknr);
1137 btrfs_release_path(root, path);
1138 btrfs_free_path(path);
1140 btrfs_end_transaction(trans, root);
1144 static int btrfs_get_block(struct inode *inode, sector_t iblock,
1145 struct buffer_head *result, int create)
1148 struct btrfs_root *root = BTRFS_I(inode)->root;
1149 mutex_lock(&root->fs_info->fs_mutex);
1150 err = btrfs_get_block_lock(inode, iblock, result, create);
1151 // err = btrfs_get_block_inline(inode, iblock, result, create);
1152 mutex_unlock(&root->fs_info->fs_mutex);
1156 static int btrfs_prepare_write(struct file *file, struct page *page,
1157 unsigned from, unsigned to)
1159 return nobh_prepare_write(page, from, to, btrfs_get_block);
1161 static int btrfs_commit_write(struct file *file, struct page *page,
1162 unsigned from, unsigned to)
1164 return nobh_commit_write(file, page, from, to);
1167 static void btrfs_write_super(struct super_block *sb)
1169 btrfs_sync_fs(sb, 1);
1172 static int btrfs_readpage(struct file *file, struct page *page)
1174 return mpage_readpage(page, btrfs_get_block);
1177 static int btrfs_readpages(struct file *file, struct address_space *mapping,
1178 struct list_head *pages, unsigned nr_pages)
1180 return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block);
1183 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1185 return nobh_writepage(page, btrfs_get_block, wbc);
1188 static void btrfs_truncate(struct inode *inode)
1190 struct btrfs_root *root = BTRFS_I(inode)->root;
1192 struct btrfs_trans_handle *trans;
1194 if (!S_ISREG(inode->i_mode))
1196 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1199 nobh_truncate_page(inode->i_mapping, inode->i_size);
1201 /* FIXME, add redo link to tree so we don't leak on crash */
1202 mutex_lock(&root->fs_info->fs_mutex);
1203 trans = btrfs_start_transaction(root, 1);
1204 ret = btrfs_truncate_in_trans(trans, root, inode);
1206 ret = btrfs_end_transaction(trans, root);
1208 mutex_unlock(&root->fs_info->fs_mutex);
1209 mark_inode_dirty(inode);
1212 static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
1213 struct page **prepared_pages,
1214 const char __user * buf)
1216 long page_fault = 0;
1218 int offset = pos & (PAGE_CACHE_SIZE - 1);
1220 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
1221 size_t count = min_t(size_t,
1222 PAGE_CACHE_SIZE - offset, write_bytes);
1223 struct page *page = prepared_pages[i];
1224 fault_in_pages_readable(buf, count);
1226 /* Copy data from userspace to the current page */
1228 page_fault = __copy_from_user(page_address(page) + offset,
1230 /* Flush processor's dcache for this page */
1231 flush_dcache_page(page);
1234 write_bytes -= count;
1239 return page_fault ? -EFAULT : 0;
1242 static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1245 for (i = 0; i < num_pages; i++) {
1248 unlock_page(pages[i]);
1249 mark_page_accessed(pages[i]);
1250 page_cache_release(pages[i]);
1253 static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1254 struct btrfs_root *root,
1256 struct page **pages,
1266 struct inode *inode = file->f_path.dentry->d_inode;
1268 for (i = 0; i < num_pages; i++) {
1269 offset = pos & (PAGE_CACHE_SIZE -1);
1270 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1271 /* FIXME, one block at a time */
1273 mutex_lock(&root->fs_info->fs_mutex);
1274 trans = btrfs_start_transaction(root, 1);
1275 btrfs_csum_file_block(trans, root, inode->i_ino,
1276 pages[i]->index << PAGE_CACHE_SHIFT,
1277 kmap(pages[i]), PAGE_CACHE_SIZE);
1279 SetPageChecked(pages[i]);
1280 ret = btrfs_end_transaction(trans, root);
1282 mutex_unlock(&root->fs_info->fs_mutex);
1284 ret = nobh_commit_write(file, pages[i], offset,
1285 offset + this_write);
1291 WARN_ON(this_write > write_bytes);
1292 write_bytes -= this_write;
1298 static int prepare_pages(struct btrfs_trans_handle *trans,
1299 struct btrfs_root *root,
1301 struct page **pages,
1307 unsigned long index = pos >> PAGE_CACHE_SHIFT;
1308 struct inode *inode = file->f_path.dentry->d_inode;
1313 loff_t isize = i_size_read(inode);
1315 memset(pages, 0, num_pages * sizeof(struct page *));
1317 for (i = 0; i < num_pages; i++) {
1318 pages[i] = grab_cache_page(inode->i_mapping, index + i);
1321 goto failed_release;
1323 offset = pos & (PAGE_CACHE_SIZE -1);
1324 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1325 ret = nobh_prepare_write(pages[i], offset,
1326 offset + this_write,
1331 goto failed_truncate;
1333 WARN_ON(this_write > write_bytes);
1334 write_bytes -= this_write;
1339 btrfs_drop_pages(pages, num_pages);
1343 btrfs_drop_pages(pages, num_pages);
1345 vmtruncate(inode, isize);
1349 static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1350 size_t count, loff_t *ppos)
1353 size_t num_written = 0;
1356 struct inode *inode = file->f_path.dentry->d_inode;
1357 struct btrfs_root *root = BTRFS_I(inode)->root;
1358 struct page *pages[1];
1360 if (file->f_flags & O_DIRECT)
1364 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1365 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1366 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1371 err = remove_suid(file->f_path.dentry);
1374 file_update_time(file);
1375 mutex_lock(&inode->i_mutex);
1377 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1378 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1379 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
1381 ret = prepare_pages(NULL, root, file, pages, num_pages,
1384 ret = btrfs_copy_from_user(pos, num_pages,
1385 write_bytes, pages, buf);
1388 ret = dirty_and_release_pages(NULL, root, file, pages,
1389 num_pages, pos, write_bytes);
1391 btrfs_drop_pages(pages, num_pages);
1394 count -= write_bytes;
1396 num_written += write_bytes;
1398 balance_dirty_pages_ratelimited(inode->i_mapping);
1401 mutex_unlock(&inode->i_mutex);
1404 current->backing_dev_info = NULL;
1405 return num_written ? num_written : err;
1409 static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
1410 struct page *page, loff_t pos,
1411 size_t offset, size_t write_bytes)
1413 struct btrfs_path *path;
1414 struct btrfs_trans_handle *trans;
1415 struct btrfs_key key;
1416 struct btrfs_leaf *leaf;
1417 struct btrfs_key found_key;
1419 size_t copy_size = 0;
1422 size_t num_written = 0;
1424 path = btrfs_alloc_path();
1426 mutex_lock(&root->fs_info->fs_mutex);
1427 trans = btrfs_start_transaction(root, 1);
1428 key.objectid = inode->i_ino;
1430 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
1434 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1440 leaf = btrfs_buffer_leaf(path->nodes[0]);
1441 btrfs_disk_key_to_cpu(&found_key,
1442 &leaf->items[path->slots[0]].key);
1443 copy_size = btrfs_item_size(leaf->items + path->slots[0]);
1444 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1445 copy_size = min(write_bytes, copy_size);
1448 int slot = path->slots[0];
1452 // FIXME find max key
1453 leaf = btrfs_buffer_leaf(path->nodes[0]);
1454 btrfs_disk_key_to_cpu(&found_key,
1455 &leaf->items[slot].key);
1456 if (found_key.objectid != inode->i_ino)
1458 if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
1460 copy_size = btrfs_item_size(leaf->items + slot);
1461 if (found_key.offset + copy_size <= pos)
1463 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1464 dst += pos - found_key.offset;
1465 copy_size = copy_size - (pos - found_key.offset);
1466 BUG_ON(copy_size < 0);
1467 copy_size = min(write_bytes, copy_size);
1468 WARN_ON(copy_size == 0);
1472 btrfs_release_path(root, path);
1473 copy_size = min(write_bytes,
1474 (size_t)BTRFS_LEAF_DATA_SIZE(root) -
1475 sizeof(struct btrfs_item) * 4);
1476 ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
1478 dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1479 path->slots[0], char);
1481 WARN_ON(copy_size == 0);
1482 WARN_ON(dst + copy_size >
1483 btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1484 path->slots[0], char) +
1485 btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
1487 btrfs_memcpy(root, path->nodes[0]->b_data, dst,
1488 page_address(page) + offset, copy_size);
1489 mark_buffer_dirty(path->nodes[0]);
1490 btrfs_release_path(root, path);
1492 offset += copy_size;
1493 num_written += copy_size;
1494 write_bytes -= copy_size;
1498 btrfs_free_path(path);
1499 ret = btrfs_end_transaction(trans, root);
1501 mutex_unlock(&root->fs_info->fs_mutex);
1502 return num_written ? num_written : err;
1505 static ssize_t btrfs_file_inline_write(struct file *file,
1506 const char __user *buf,
1507 size_t count, loff_t *ppos)
1510 size_t num_written = 0;
1513 struct inode *inode = file->f_path.dentry->d_inode;
1514 struct btrfs_root *root = BTRFS_I(inode)->root;
1515 unsigned long page_index;
1517 if (file->f_flags & O_DIRECT)
1521 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1522 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1523 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1528 err = remove_suid(file->f_path.dentry);
1531 file_update_time(file);
1532 mutex_lock(&inode->i_mutex);
1534 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1535 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1538 page_index = pos >> PAGE_CACHE_SHIFT;
1539 page = grab_cache_page(inode->i_mapping, page_index);
1540 if (!PageUptodate(page)) {
1541 ret = mpage_readpage(page, btrfs_get_block);
1545 ret = btrfs_copy_from_user(pos, 1,
1546 write_bytes, &page, buf);
1548 write_bytes = inline_one_page(root, inode, page, pos,
1549 offset, write_bytes);
1550 SetPageUptodate(page);
1551 if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
1552 i_size_write(inode, pos + write_bytes);
1553 mark_inode_dirty(inode);
1555 page_cache_release(page);
1557 if (write_bytes < 0)
1560 count -= write_bytes;
1562 num_written += write_bytes;
1564 balance_dirty_pages_ratelimited(inode->i_mapping);
1568 mutex_unlock(&inode->i_mutex);
1571 current->backing_dev_info = NULL;
1572 return num_written ? num_written : err;
1576 static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
1577 unsigned long offset, unsigned long size)
1580 unsigned long left, count = desc->count;
1581 struct inode *inode = page->mapping->host;
1586 if (!PageChecked(page)) {
1587 /* FIXME, do it per block */
1588 struct btrfs_root *root = BTRFS_I(inode)->root;
1589 int ret = btrfs_csum_verify_file_block(root,
1590 page->mapping->host->i_ino,
1591 page->index << PAGE_CACHE_SHIFT,
1592 kmap(page), PAGE_CACHE_SIZE);
1594 printk("failed to verify ino %lu page %lu\n",
1595 page->mapping->host->i_ino,
1597 memset(page_address(page), 0, PAGE_CACHE_SIZE);
1599 SetPageChecked(page);
1603 * Faults on the destination of a read are common, so do it before
1606 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1607 kaddr = kmap_atomic(page, KM_USER0);
1608 left = __copy_to_user_inatomic(desc->arg.buf,
1609 kaddr + offset, size);
1610 kunmap_atomic(kaddr, KM_USER0);
1615 /* Do it the slow way */
1617 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1622 desc->error = -EFAULT;
1625 desc->count = count - size;
1626 desc->written += size;
1627 desc->arg.buf += size;
1632 * btrfs_file_aio_read - filesystem read routine
1633 * @iocb: kernel I/O control block
1634 * @iov: io vector request
1635 * @nr_segs: number of segments in the iovec
1636 * @pos: current file position
1638 static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1639 unsigned long nr_segs, loff_t pos)
1641 struct file *filp = iocb->ki_filp;
1645 loff_t *ppos = &iocb->ki_pos;
1648 for (seg = 0; seg < nr_segs; seg++) {
1649 const struct iovec *iv = &iov[seg];
1652 * If any segment has a negative length, or the cumulative
1653 * length ever wraps negative then return -EINVAL.
1655 count += iv->iov_len;
1656 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
1658 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
1663 count -= iv->iov_len; /* This segment is no good */
1668 for (seg = 0; seg < nr_segs; seg++) {
1669 read_descriptor_t desc;
1672 desc.arg.buf = iov[seg].iov_base;
1673 desc.count = iov[seg].iov_len;
1674 if (desc.count == 0)
1677 do_generic_file_read(filp, ppos, &desc,
1679 retval += desc.written;
1681 retval = retval ?: desc.error;
1689 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
1691 struct btrfs_trans_handle *trans;
1692 struct btrfs_key key;
1693 struct btrfs_root_item new_root_item;
1697 mutex_lock(&root->fs_info->fs_mutex);
1698 trans = btrfs_start_transaction(root, 1);
1701 ret = btrfs_update_inode(trans, root, root->inode);
1704 ret = btrfs_find_free_objectid(trans, root, 0, &objectid);
1707 memset(&new_root_item, 0, sizeof(new_root_item));
1708 memcpy(&new_root_item, &root->root_item,
1709 sizeof(new_root_item));
1711 key.objectid = objectid;
1714 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1715 ret = btrfs_insert_inode_map(trans, root, objectid, &key);
1718 key.objectid = objectid;
1721 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
1722 btrfs_set_root_blocknr(&new_root_item, root->node->b_blocknr);
1724 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
1728 printk("adding snapshot name %.*s root %Lu %Lu %u\n", namelen, name, key.objectid, key.offset, key.flags);
1731 * insert the directory item
1733 key.offset = (u64)-1;
1734 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
1736 root->fs_info->sb->s_root->d_inode->i_ino,
1741 ret = btrfs_inc_root_ref(trans, root);
1744 ret = btrfs_commit_transaction(trans, root);
1746 mutex_unlock(&root->fs_info->fs_mutex);
1750 static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
1751 cmd, unsigned long arg)
1753 struct btrfs_root *root = BTRFS_I(inode)->root;
1754 struct btrfs_ioctl_vol_args vol_args;
1758 if (!root->ref_cows)
1761 case BTRFS_IOC_SNAP_CREATE:
1762 if (copy_from_user(&vol_args,
1763 (struct btrfs_ioctl_vol_args __user *)arg,
1766 namelen = strlen(vol_args.name);
1767 if (namelen > BTRFS_VOL_NAME_MAX)
1769 ret = create_snapshot(root, vol_args.name, namelen);
1778 static struct kmem_cache *btrfs_inode_cachep;
1779 struct kmem_cache *btrfs_trans_handle_cachep;
1780 struct kmem_cache *btrfs_transaction_cachep;
1781 struct kmem_cache *btrfs_bit_radix_cachep;
1782 struct kmem_cache *btrfs_path_cachep;
1785 * Called inside transaction, so use GFP_NOFS
1787 static struct inode *btrfs_alloc_inode(struct super_block *sb)
1789 struct btrfs_inode *ei;
1791 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
1794 ei->magic = 0xDEADBEEF;
1795 ei->magic2 = 0xDEADBEAF;
1796 return &ei->vfs_inode;
1799 static void btrfs_destroy_inode(struct inode *inode)
1801 struct btrfs_inode *ei = BTRFS_I(inode);
1802 WARN_ON(ei->magic != 0xDEADBEEF);
1803 WARN_ON(ei->magic2 != 0xDEADBEAF);
1804 WARN_ON(!list_empty(&inode->i_dentry));
1805 WARN_ON(inode->i_data.nrpages);
1809 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
1812 static void init_once(void * foo, struct kmem_cache * cachep,
1813 unsigned long flags)
1815 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
1817 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1818 SLAB_CTOR_CONSTRUCTOR) {
1819 inode_init_once(&ei->vfs_inode);
1823 static int init_inodecache(void)
1825 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
1826 sizeof(struct btrfs_inode),
1827 0, (SLAB_RECLAIM_ACCOUNT|
1830 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
1831 sizeof(struct btrfs_trans_handle),
1832 0, (SLAB_RECLAIM_ACCOUNT|
1835 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
1836 sizeof(struct btrfs_transaction),
1837 0, (SLAB_RECLAIM_ACCOUNT|
1840 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
1841 sizeof(struct btrfs_transaction),
1842 0, (SLAB_RECLAIM_ACCOUNT|
1845 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
1847 0, (SLAB_RECLAIM_ACCOUNT|
1849 SLAB_DESTROY_BY_RCU),
1851 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
1852 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
1857 static void destroy_inodecache(void)
1859 kmem_cache_destroy(btrfs_inode_cachep);
1860 kmem_cache_destroy(btrfs_trans_handle_cachep);
1861 kmem_cache_destroy(btrfs_transaction_cachep);
1862 kmem_cache_destroy(btrfs_bit_radix_cachep);
1863 kmem_cache_destroy(btrfs_path_cachep);
1866 static int btrfs_get_sb(struct file_system_type *fs_type,
1867 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1869 return get_sb_bdev(fs_type, flags, dev_name, data,
1870 btrfs_fill_super, mnt);
1873 static struct file_system_type btrfs_fs_type = {
1874 .owner = THIS_MODULE,
1876 .get_sb = btrfs_get_sb,
1877 .kill_sb = kill_block_super,
1878 .fs_flags = FS_REQUIRES_DEV,
1881 static struct super_operations btrfs_super_ops = {
1882 .statfs = simple_statfs,
1883 .delete_inode = btrfs_delete_inode,
1884 .put_super = btrfs_put_super,
1885 .read_inode = btrfs_read_locked_inode,
1886 .write_super = btrfs_write_super,
1887 .sync_fs = btrfs_sync_fs,
1888 .write_inode = btrfs_write_inode,
1889 .alloc_inode = btrfs_alloc_inode,
1890 .destroy_inode = btrfs_destroy_inode,
1893 static struct inode_operations btrfs_dir_inode_operations = {
1894 .lookup = btrfs_lookup,
1895 .create = btrfs_create,
1896 .unlink = btrfs_unlink,
1897 .mkdir = btrfs_mkdir,
1898 .rmdir = btrfs_rmdir,
1901 static struct inode_operations btrfs_dir_ro_inode_operations = {
1902 .lookup = btrfs_lookup,
1905 static struct file_operations btrfs_dir_file_operations = {
1906 .llseek = generic_file_llseek,
1907 .read = generic_read_dir,
1908 .readdir = btrfs_readdir,
1909 .ioctl = btrfs_ioctl,
1912 static struct address_space_operations btrfs_aops = {
1913 .readpage = btrfs_readpage,
1914 .readpages = btrfs_readpages,
1915 .writepage = btrfs_writepage,
1916 .sync_page = block_sync_page,
1917 .prepare_write = btrfs_prepare_write,
1918 .commit_write = btrfs_commit_write,
1921 static struct inode_operations btrfs_file_inode_operations = {
1922 .truncate = btrfs_truncate,
1925 static struct file_operations btrfs_file_operations = {
1926 .llseek = generic_file_llseek,
1927 .read = do_sync_read,
1928 .aio_read = btrfs_file_aio_read,
1929 .write = btrfs_file_write,
1930 .mmap = generic_file_mmap,
1931 .open = generic_file_open,
1932 .ioctl = btrfs_ioctl,
1935 static int __init init_btrfs_fs(void)
1938 printk("btrfs loaded!\n");
1939 err = init_inodecache();
1942 kset_set_kset_s(&btrfs_subsys, fs_subsys);
1943 err = subsystem_register(&btrfs_subsys);
1946 return register_filesystem(&btrfs_fs_type);
1948 destroy_inodecache();
1952 static void __exit exit_btrfs_fs(void)
1954 destroy_inodecache();
1955 unregister_filesystem(&btrfs_fs_type);
1956 subsystem_unregister(&btrfs_subsys);
1957 printk("btrfs unloaded\n");
1960 module_init(init_btrfs_fs)
1961 module_exit(exit_btrfs_fs)
1963 MODULE_LICENSE("GPL");