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"
19 void btrfs_fsinfo_release(struct kobject *obj)
21 struct btrfs_fs_info *fsinfo = container_of(obj,
22 struct btrfs_fs_info, kobj);
26 struct kobj_type btrfs_fsinfo_ktype = {
27 .release = btrfs_fsinfo_release,
30 decl_subsys(btrfs, &btrfs_fsinfo_ktype, NULL);
32 #define BTRFS_SUPER_MAGIC 0x9123682E
34 static struct inode_operations btrfs_dir_inode_operations;
35 static struct inode_operations btrfs_dir_ro_inode_operations;
36 static struct super_operations btrfs_super_ops;
37 static struct file_operations btrfs_dir_file_operations;
38 static struct inode_operations btrfs_file_inode_operations;
39 static struct address_space_operations btrfs_aops;
40 static struct file_operations btrfs_file_operations;
42 static int check_inode(struct inode *inode)
44 struct btrfs_inode *ei = BTRFS_I(inode);
45 WARN_ON(ei->magic != 0xDEADBEEF);
46 WARN_ON(ei->magic2 != 0xDEADBEAF);
50 static void btrfs_read_locked_inode(struct inode *inode)
52 struct btrfs_path *path;
53 struct btrfs_inode_item *inode_item;
54 struct btrfs_root *root = BTRFS_I(inode)->root;
55 struct btrfs_key location;
58 path = btrfs_alloc_path();
60 btrfs_init_path(path);
61 mutex_lock(&root->fs_info->fs_mutex);
65 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
66 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
68 btrfs_free_path(path);
72 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
74 struct btrfs_inode_item);
76 inode->i_mode = btrfs_inode_mode(inode_item);
77 inode->i_nlink = btrfs_inode_nlink(inode_item);
78 inode->i_uid = btrfs_inode_uid(inode_item);
79 inode->i_gid = btrfs_inode_gid(inode_item);
80 inode->i_size = btrfs_inode_size(inode_item);
81 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
82 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
83 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
84 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
85 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
86 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
87 inode->i_blocks = btrfs_inode_nblocks(inode_item);
88 inode->i_generation = btrfs_inode_generation(inode_item);
90 btrfs_free_path(path);
93 mutex_unlock(&root->fs_info->fs_mutex);
95 switch (inode->i_mode & S_IFMT) {
98 init_special_inode(inode, inode->i_mode,
99 btrfs_inode_rdev(inode_item));
103 inode->i_mapping->a_ops = &btrfs_aops;
104 inode->i_fop = &btrfs_file_operations;
105 inode->i_op = &btrfs_file_inode_operations;
108 inode->i_fop = &btrfs_dir_file_operations;
109 if (root == root->fs_info->tree_root)
110 inode->i_op = &btrfs_dir_ro_inode_operations;
112 inode->i_op = &btrfs_dir_inode_operations;
115 // inode->i_op = &page_symlink_inode_operations;
122 btrfs_release_path(root, path);
123 btrfs_free_path(path);
124 mutex_unlock(&root->fs_info->fs_mutex);
125 make_bad_inode(inode);
128 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
129 struct btrfs_root *root,
131 struct dentry *dentry)
133 struct btrfs_path *path;
134 const char *name = dentry->d_name.name;
135 int name_len = dentry->d_name.len;
138 struct btrfs_dir_item *di;
140 path = btrfs_alloc_path();
142 btrfs_init_path(path);
143 ret = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
151 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
152 struct btrfs_dir_item);
153 objectid = btrfs_disk_key_objectid(&di->location);
155 ret = btrfs_del_item(trans, root, path);
158 btrfs_release_path(root, path);
159 ret = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
162 ret = btrfs_del_item(trans, root, path);
164 dentry->d_inode->i_ctime = dir->i_ctime;
166 btrfs_release_path(root, path);
167 btrfs_free_path(path);
169 inode_dec_link_count(dentry->d_inode);
170 dir->i_size -= name_len * 2;
171 mark_inode_dirty(dir);
176 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
178 struct btrfs_root *root;
179 struct btrfs_trans_handle *trans;
182 root = BTRFS_I(dir)->root;
183 mutex_lock(&root->fs_info->fs_mutex);
184 trans = btrfs_start_transaction(root, 1);
185 ret = btrfs_unlink_trans(trans, root, dir, dentry);
186 btrfs_end_transaction(trans, root);
187 mutex_unlock(&root->fs_info->fs_mutex);
191 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
193 struct inode *inode = dentry->d_inode;
196 struct btrfs_root *root = BTRFS_I(dir)->root;
197 struct btrfs_path *path;
198 struct btrfs_key key;
199 struct btrfs_trans_handle *trans;
200 struct btrfs_key found_key;
202 struct btrfs_leaf *leaf;
203 char *goodnames = "..";
205 path = btrfs_alloc_path();
207 btrfs_init_path(path);
208 mutex_lock(&root->fs_info->fs_mutex);
209 trans = btrfs_start_transaction(root, 1);
210 key.objectid = inode->i_ino;
211 key.offset = (u64)-1;
214 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
220 if (path->slots[0] == 0) {
225 leaf = btrfs_buffer_leaf(path->nodes[0]);
226 btrfs_disk_key_to_cpu(&found_key,
227 &leaf->items[path->slots[0]].key);
228 found_type = btrfs_key_type(&found_key);
229 if (found_key.objectid != inode->i_ino) {
233 if ((found_type != BTRFS_DIR_ITEM_KEY &&
234 found_type != BTRFS_DIR_INDEX_KEY) ||
235 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
236 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
240 ret = btrfs_del_item(trans, root, path);
243 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
245 btrfs_release_path(root, path);
248 btrfs_release_path(root, path);
250 /* now the directory is empty */
251 err = btrfs_unlink_trans(trans, root, dir, dentry);
256 btrfs_release_path(root, path);
257 btrfs_free_path(path);
258 mutex_unlock(&root->fs_info->fs_mutex);
259 ret = btrfs_end_transaction(trans, root);
265 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
266 struct btrfs_root *root,
269 u64 objectid = inode->i_ino;
270 struct btrfs_path *path;
271 struct btrfs_inode_map_item *map;
272 struct btrfs_key stat_data_key;
277 path = btrfs_alloc_path();
279 btrfs_init_path(path);
280 ret = btrfs_lookup_inode_map(trans, root, path, objectid, -1);
286 map = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
287 struct btrfs_inode_map_item);
288 btrfs_disk_key_to_cpu(&stat_data_key, &map->key);
289 ret = btrfs_del_item(trans, root->fs_info->inode_root, path);
291 btrfs_release_path(root, path);
293 ret = btrfs_lookup_inode(trans, root, path,
294 &BTRFS_I(inode)->location, -1);
296 ret = btrfs_del_item(trans, root, path);
299 btrfs_release_path(root, path);
300 btrfs_free_path(path);
304 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
305 struct btrfs_root *root,
309 struct btrfs_path *path;
310 struct btrfs_key key;
311 struct btrfs_disk_key *found_key;
312 struct btrfs_leaf *leaf;
313 struct btrfs_file_extent_item *fi = NULL;
314 u64 extent_start = 0;
315 u64 extent_num_blocks = 0;
318 path = btrfs_alloc_path();
320 /* FIXME, add redo link to tree so we don't leak on crash */
321 key.objectid = inode->i_ino;
322 key.offset = (u64)-1;
325 * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys
328 btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY);
330 btrfs_init_path(path);
331 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
336 BUG_ON(path->slots[0] == 0);
339 leaf = btrfs_buffer_leaf(path->nodes[0]);
340 found_key = &leaf->items[path->slots[0]].key;
341 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
343 if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY &&
344 btrfs_disk_key_type(found_key) != BTRFS_INLINE_DATA_KEY &&
345 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
347 if (btrfs_disk_key_offset(found_key) < inode->i_size)
349 if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
350 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
352 struct btrfs_file_extent_item);
353 extent_start = btrfs_file_extent_disk_blocknr(fi);
355 btrfs_file_extent_disk_num_blocks(fi);
357 btrfs_file_extent_num_blocks(fi) >> 9;
362 ret = btrfs_del_item(trans, root, path);
364 btrfs_release_path(root, path);
366 ret = btrfs_free_extent(trans, root, extent_start,
367 extent_num_blocks, 0);
373 btrfs_release_path(root, path);
374 btrfs_free_path(path);
378 static void btrfs_delete_inode(struct inode *inode)
380 struct btrfs_trans_handle *trans;
381 struct btrfs_root *root = BTRFS_I(inode)->root;
384 truncate_inode_pages(&inode->i_data, 0);
385 if (is_bad_inode(inode)) {
389 mutex_lock(&root->fs_info->fs_mutex);
390 trans = btrfs_start_transaction(root, 1);
391 if (S_ISREG(inode->i_mode)) {
392 ret = btrfs_truncate_in_trans(trans, root, inode);
395 btrfs_free_inode(trans, root, inode);
396 btrfs_end_transaction(trans, root);
397 mutex_unlock(&root->fs_info->fs_mutex);
403 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
404 struct btrfs_key *location)
406 const char *name = dentry->d_name.name;
407 int namelen = dentry->d_name.len;
408 struct btrfs_dir_item *di;
409 struct btrfs_path *path;
410 struct btrfs_root *root = BTRFS_I(dir)->root;
413 path = btrfs_alloc_path();
415 btrfs_init_path(path);
416 ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
418 if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) {
419 location->objectid = 0;
423 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
424 struct btrfs_dir_item);
425 btrfs_disk_key_to_cpu(location, &di->location);
427 btrfs_release_path(root, path);
428 btrfs_free_path(path);
433 int fixup_tree_root_location(struct btrfs_root *root,
434 struct btrfs_key *location,
435 struct btrfs_root **sub_root)
437 struct btrfs_path *path;
438 struct btrfs_root_item *ri;
441 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
443 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
446 path = btrfs_alloc_path();
448 mutex_lock(&root->fs_info->fs_mutex);
450 ret = btrfs_lookup_inode(NULL, root, path, location, 0);
453 ri = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
455 struct btrfs_root_item);
456 location->objectid = btrfs_root_dirid(ri);
458 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
459 location->offset = 0;
460 /* FIXME properly select the root */
461 *sub_root = root->fs_info->fs_root;
463 btrfs_free_path(path);
464 mutex_unlock(&root->fs_info->fs_mutex);
469 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
470 struct nameidata *nd)
472 struct inode * inode;
473 struct btrfs_inode *bi = BTRFS_I(dir);
474 struct btrfs_root *root = bi->root;
475 struct btrfs_root *sub_root = root;
476 struct btrfs_key location;
479 if (dentry->d_name.len > BTRFS_NAME_LEN)
480 return ERR_PTR(-ENAMETOOLONG);
481 mutex_lock(&root->fs_info->fs_mutex);
482 ret = btrfs_inode_by_name(dir, dentry, &location);
483 mutex_unlock(&root->fs_info->fs_mutex);
487 if (location.objectid) {
488 ret = fixup_tree_root_location(root, &location, &sub_root);
492 return ERR_PTR(-ENOENT);
493 inode = iget_locked(dir->i_sb, location.objectid);
495 return ERR_PTR(-EACCES);
496 if (inode->i_state & I_NEW) {
497 BTRFS_I(inode)->root = sub_root;
498 memcpy(&BTRFS_I(inode)->location, &location,
500 btrfs_read_locked_inode(inode);
501 unlock_new_inode(inode);
506 return d_splice_alias(inode, dentry);
509 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
511 struct inode *inode = filp->f_path.dentry->d_inode;
512 struct btrfs_root *root = BTRFS_I(inode)->root;
513 struct btrfs_item *item;
514 struct btrfs_dir_item *di;
515 struct btrfs_key key;
516 struct btrfs_path *path;
519 struct btrfs_leaf *leaf;
522 unsigned char d_type = DT_UNKNOWN;
524 int key_type = BTRFS_DIR_INDEX_KEY;
526 /* FIXME, use a real flag for deciding about the key type */
527 if (root->fs_info->tree_root == root)
528 key_type = BTRFS_DIR_ITEM_KEY;
530 mutex_lock(&root->fs_info->fs_mutex);
531 key.objectid = inode->i_ino;
533 btrfs_set_key_type(&key, key_type);
534 key.offset = filp->f_pos;
535 path = btrfs_alloc_path();
536 btrfs_init_path(path);
537 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
543 leaf = btrfs_buffer_leaf(path->nodes[0]);
544 nritems = btrfs_header_nritems(&leaf->header);
545 slot = path->slots[0];
546 if (advance || slot >= nritems) {
547 if (slot >= nritems -1) {
548 ret = btrfs_next_leaf(root, path);
551 leaf = btrfs_buffer_leaf(path->nodes[0]);
552 nritems = btrfs_header_nritems(&leaf->header);
553 slot = path->slots[0];
560 item = leaf->items + slot;
561 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
563 if (key_type == BTRFS_DIR_INDEX_KEY &&
564 btrfs_disk_key_offset(&item->key) >
565 root->fs_info->highest_inode)
567 if (btrfs_disk_key_type(&item->key) != key_type)
569 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
571 filp->f_pos = btrfs_disk_key_offset(&item->key);
573 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
574 over = filldir(dirent, (const char *)(di + 1),
575 btrfs_dir_name_len(di),
576 btrfs_disk_key_offset(&item->key),
577 btrfs_disk_key_objectid(&di->location), d_type);
585 btrfs_release_path(root, path);
586 btrfs_free_path(path);
587 mutex_unlock(&root->fs_info->fs_mutex);
591 static void btrfs_put_super (struct super_block * sb)
593 struct btrfs_root *root = btrfs_sb(sb);
596 ret = close_ctree(root);
598 printk("close ctree returns %d\n", ret);
600 sb->s_fs_info = NULL;
603 static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
605 struct inode * inode;
606 struct dentry * root_dentry;
607 struct btrfs_super_block *disk_super;
608 struct btrfs_root *root;
609 struct btrfs_inode *bi;
611 sb->s_maxbytes = MAX_LFS_FILESIZE;
612 sb->s_magic = BTRFS_SUPER_MAGIC;
613 sb->s_op = &btrfs_super_ops;
616 root = open_ctree(sb);
619 printk("btrfs: open_ctree failed\n");
622 sb->s_fs_info = root;
623 disk_super = root->fs_info->disk_super;
624 printk("read in super total blocks %Lu root %Lu\n",
625 btrfs_super_total_blocks(disk_super),
626 btrfs_super_root_dir(disk_super));
628 inode = iget_locked(sb, btrfs_super_root_dir(disk_super));
630 bi->location.objectid = inode->i_ino;
631 bi->location.offset = 0;
632 bi->location.flags = 0;
633 bi->root = root->fs_info->tree_root;
634 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
638 if (inode->i_state & I_NEW) {
639 btrfs_read_locked_inode(inode);
640 unlock_new_inode(inode);
643 root_dentry = d_alloc_root(inode);
648 sb->s_root = root_dentry;
653 static void fill_inode_item(struct btrfs_inode_item *item,
656 btrfs_set_inode_uid(item, inode->i_uid);
657 btrfs_set_inode_gid(item, inode->i_gid);
658 btrfs_set_inode_size(item, inode->i_size);
659 btrfs_set_inode_mode(item, inode->i_mode);
660 btrfs_set_inode_nlink(item, inode->i_nlink);
661 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
662 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
663 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
664 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
665 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
666 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
667 btrfs_set_inode_nblocks(item, inode->i_blocks);
668 btrfs_set_inode_generation(item, inode->i_generation);
672 static int btrfs_update_inode(struct btrfs_trans_handle *trans,
673 struct btrfs_root *root,
676 struct btrfs_inode_item *inode_item;
677 struct btrfs_path *path;
680 path = btrfs_alloc_path();
682 btrfs_init_path(path);
684 ret = btrfs_lookup_inode(trans, root, path,
685 &BTRFS_I(inode)->location, 1);
692 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
694 struct btrfs_inode_item);
696 fill_inode_item(inode_item, inode);
697 btrfs_mark_buffer_dirty(path->nodes[0]);
699 btrfs_release_path(root, path);
700 btrfs_free_path(path);
705 static int btrfs_write_inode(struct inode *inode, int wait)
707 struct btrfs_root *root = BTRFS_I(inode)->root;
708 struct btrfs_trans_handle *trans;
711 mutex_lock(&root->fs_info->fs_mutex);
712 trans = btrfs_start_transaction(root, 1);
713 ret = btrfs_update_inode(trans, root, inode);
715 btrfs_commit_transaction(trans, root);
717 btrfs_end_transaction(trans, root);
718 mutex_unlock(&root->fs_info->fs_mutex);
723 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
724 struct inode *dir, int mode)
727 struct btrfs_inode_item inode_item;
728 struct btrfs_root *root = BTRFS_I(dir)->root;
729 struct btrfs_key *key;
733 inode = new_inode(dir->i_sb);
735 return ERR_PTR(-ENOMEM);
737 BTRFS_I(inode)->root = BTRFS_I(dir)->root;
738 key = &BTRFS_I(inode)->location;
740 ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
743 inode->i_uid = current->fsuid;
744 inode->i_gid = current->fsgid;
745 inode->i_mode = mode;
746 inode->i_ino = objectid;
748 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
749 fill_inode_item(&inode_item, inode);
751 key->objectid = objectid;
754 btrfs_set_key_type(key, BTRFS_INODE_ITEM_KEY);
755 ret = btrfs_insert_inode_map(trans, root, objectid, key);
758 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
761 insert_inode_hash(inode);
767 static int btrfs_add_link(struct btrfs_trans_handle *trans,
768 struct dentry *dentry, struct inode *inode)
771 struct btrfs_key key;
772 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
773 key.objectid = inode->i_ino;
775 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
778 ret = btrfs_insert_dir_item(trans, root,
779 dentry->d_name.name, dentry->d_name.len,
780 dentry->d_parent->d_inode->i_ino,
783 dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
784 ret = btrfs_update_inode(trans, root,
785 dentry->d_parent->d_inode);
788 check_inode(dentry->d_parent->d_inode);
792 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
793 struct dentry *dentry, struct inode *inode)
795 int err = btrfs_add_link(trans, dentry, inode);
797 d_instantiate(dentry, inode);
806 static int btrfs_create(struct inode *dir, struct dentry *dentry,
807 int mode, struct nameidata *nd)
809 struct btrfs_trans_handle *trans;
810 struct btrfs_root *root = BTRFS_I(dir)->root;
815 mutex_lock(&root->fs_info->fs_mutex);
816 trans = btrfs_start_transaction(root, 1);
817 inode = btrfs_new_inode(trans, dir, mode);
818 err = PTR_ERR(inode);
821 // FIXME mark the inode dirty
822 err = btrfs_add_nondir(trans, dentry, inode);
826 inode->i_mapping->a_ops = &btrfs_aops;
827 inode->i_fop = &btrfs_file_operations;
828 inode->i_op = &btrfs_file_inode_operations;
830 dir->i_sb->s_dirt = 1;
832 btrfs_end_transaction(trans, root);
833 mutex_unlock(&root->fs_info->fs_mutex);
838 inode_dec_link_count(inode);
844 static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
845 struct inode *inode, struct inode *dir)
847 struct btrfs_root *root = BTRFS_I(dir)->root;
850 struct btrfs_key key;
855 key.objectid = inode->i_ino;
858 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
860 ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino,
864 key.objectid = dir->i_ino;
865 ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino,
870 ret = btrfs_update_inode(trans, root, inode);
875 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
878 struct btrfs_trans_handle *trans;
879 struct btrfs_root *root = BTRFS_I(dir)->root;
883 mutex_lock(&root->fs_info->fs_mutex);
884 trans = btrfs_start_transaction(root, 1);
886 err = PTR_ERR(trans);
889 inode = btrfs_new_inode(trans, dir, S_IFDIR | mode);
891 err = PTR_ERR(inode);
895 inode->i_op = &btrfs_dir_inode_operations;
896 inode->i_fop = &btrfs_dir_file_operations;
898 err = btrfs_make_empty_dir(trans, inode, dir);
901 err = btrfs_add_link(trans, dentry, inode);
904 d_instantiate(dentry, inode);
908 btrfs_end_transaction(trans, root);
910 mutex_unlock(&root->fs_info->fs_mutex);
916 static int btrfs_sync_fs(struct super_block *sb, int wait)
918 struct btrfs_trans_handle *trans;
919 struct btrfs_root *root;
925 filemap_flush(root->fs_info->btree_inode->i_mapping);
928 filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
929 mutex_lock(&root->fs_info->fs_mutex);
930 trans = btrfs_start_transaction(root, 1);
931 ret = btrfs_commit_transaction(trans, root);
934 printk("btrfs sync_fs\n");
935 mutex_unlock(&root->fs_info->fs_mutex);
940 static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
941 struct buffer_head *result, int create)
943 struct btrfs_root *root = btrfs_sb(inode->i_sb);
944 struct btrfs_path *path;
945 struct btrfs_key key;
946 struct btrfs_leaf *leaf;
947 int num_bytes = result->b_size;
957 path = btrfs_alloc_path();
964 pos = iblock << inode->i_blkbits;
965 key.objectid = inode->i_ino;
967 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
968 ptr = kmap(result->b_page);
970 ptr += (pos & (PAGE_CACHE_SIZE -1));
973 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
981 leaf = btrfs_buffer_leaf(path->nodes[0]);
982 item_size = btrfs_item_size(leaf->items + path->slots[0]);
983 copy_size = min(num_bytes, item_size);
984 data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
985 WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
986 memcpy(ptr, data_ptr, copy_size);
988 num_bytes -= copy_size;
989 WARN_ON(num_bytes < 0);
991 btrfs_release_path(root, path);
992 if (num_bytes != 0) {
993 if (pos >= i_size_read(inode))
994 memset(ptr, 0, num_bytes);
998 set_buffer_uptodate(result);
999 map_bh(result, inode->i_sb, 0);
1002 btrfs_free_path(path);
1003 kunmap(result->b_page);
1008 static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
1009 struct buffer_head *result, int create)
1014 u64 extent_start = 0;
1016 u64 objectid = inode->i_ino;
1017 struct btrfs_path *path;
1018 struct btrfs_root *root = BTRFS_I(inode)->root;
1019 struct btrfs_trans_handle *trans = NULL;
1020 struct btrfs_file_extent_item *item;
1021 struct btrfs_leaf *leaf;
1022 struct btrfs_disk_key *found_key;
1024 path = btrfs_alloc_path();
1026 btrfs_init_path(path);
1028 trans = btrfs_start_transaction(root, 1);
1030 ret = btrfs_lookup_file_extent(trans, root, path,
1032 iblock << inode->i_blkbits, 0);
1039 if (path->slots[0] == 0) {
1040 btrfs_release_path(root, path);
1046 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
1047 struct btrfs_file_extent_item);
1048 leaf = btrfs_buffer_leaf(path->nodes[0]);
1049 blocknr = btrfs_file_extent_disk_blocknr(item);
1050 blocknr += btrfs_file_extent_offset(item);
1052 /* exact match found, use it */
1055 map_bh(result, inode->i_sb, blocknr);
1059 /* are we inside the extent that was found? */
1060 found_key = &leaf->items[path->slots[0]].key;
1061 if (btrfs_disk_key_objectid(found_key) != objectid ||
1062 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
1065 btrfs_release_path(root, path);
1069 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
1070 extent_start = extent_start >> inode->i_blkbits;
1071 extent_start += btrfs_file_extent_offset(item);
1072 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
1073 if (iblock >= extent_start && iblock < extent_end) {
1075 map_bh(result, inode->i_sb, blocknr + iblock - extent_start);
1079 /* ok, create a new extent */
1084 ret = btrfs_alloc_file_extent(trans, root, objectid,
1085 iblock << inode->i_blkbits,
1086 1, extent_end, &blocknr);
1091 inode->i_blocks += inode->i_sb->s_blocksize >> 9;
1092 set_buffer_new(result);
1093 map_bh(result, inode->i_sb, blocknr);
1096 btrfs_release_path(root, path);
1097 btrfs_free_path(path);
1099 btrfs_end_transaction(trans, root);
1103 static int btrfs_get_block(struct inode *inode, sector_t iblock,
1104 struct buffer_head *result, int create)
1107 struct btrfs_root *root = BTRFS_I(inode)->root;
1108 mutex_lock(&root->fs_info->fs_mutex);
1109 err = btrfs_get_block_lock(inode, iblock, result, create);
1110 // err = btrfs_get_block_inline(inode, iblock, result, create);
1111 mutex_unlock(&root->fs_info->fs_mutex);
1115 static int btrfs_prepare_write(struct file *file, struct page *page,
1116 unsigned from, unsigned to)
1118 return nobh_prepare_write(page, from, to, btrfs_get_block);
1120 static int btrfs_commit_write(struct file *file, struct page *page,
1121 unsigned from, unsigned to)
1123 return nobh_commit_write(file, page, from, to);
1126 static void btrfs_write_super(struct super_block *sb)
1128 btrfs_sync_fs(sb, 1);
1131 static int btrfs_readpage(struct file *file, struct page *page)
1133 return mpage_readpage(page, btrfs_get_block);
1136 static int btrfs_readpages(struct file *file, struct address_space *mapping,
1137 struct list_head *pages, unsigned nr_pages)
1139 return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block);
1142 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1144 return nobh_writepage(page, btrfs_get_block, wbc);
1147 static void btrfs_truncate(struct inode *inode)
1149 struct btrfs_root *root = BTRFS_I(inode)->root;
1151 struct btrfs_trans_handle *trans;
1153 if (!S_ISREG(inode->i_mode))
1155 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1158 nobh_truncate_page(inode->i_mapping, inode->i_size);
1160 /* FIXME, add redo link to tree so we don't leak on crash */
1161 mutex_lock(&root->fs_info->fs_mutex);
1162 trans = btrfs_start_transaction(root, 1);
1163 ret = btrfs_truncate_in_trans(trans, root, inode);
1165 ret = btrfs_end_transaction(trans, root);
1167 mutex_unlock(&root->fs_info->fs_mutex);
1168 mark_inode_dirty(inode);
1171 static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
1172 struct page **prepared_pages,
1173 const char __user * buf)
1175 long page_fault = 0;
1177 int offset = pos & (PAGE_CACHE_SIZE - 1);
1179 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
1180 size_t count = min_t(size_t,
1181 PAGE_CACHE_SIZE - offset, write_bytes);
1182 struct page *page = prepared_pages[i];
1183 fault_in_pages_readable(buf, count);
1185 /* Copy data from userspace to the current page */
1187 page_fault = __copy_from_user(page_address(page) + offset,
1189 /* Flush processor's dcache for this page */
1190 flush_dcache_page(page);
1193 write_bytes -= count;
1198 return page_fault ? -EFAULT : 0;
1201 static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1204 for (i = 0; i < num_pages; i++) {
1207 unlock_page(pages[i]);
1208 mark_page_accessed(pages[i]);
1209 page_cache_release(pages[i]);
1212 static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1213 struct btrfs_root *root,
1215 struct page **pages,
1225 struct inode *inode = file->f_path.dentry->d_inode;
1227 for (i = 0; i < num_pages; i++) {
1228 offset = pos & (PAGE_CACHE_SIZE -1);
1229 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1230 /* FIXME, one block at a time */
1232 mutex_lock(&root->fs_info->fs_mutex);
1233 trans = btrfs_start_transaction(root, 1);
1234 btrfs_csum_file_block(trans, root, inode->i_ino,
1235 pages[i]->index << PAGE_CACHE_SHIFT,
1236 kmap(pages[i]), PAGE_CACHE_SIZE);
1238 SetPageChecked(pages[i]);
1239 ret = btrfs_end_transaction(trans, root);
1241 mutex_unlock(&root->fs_info->fs_mutex);
1243 ret = nobh_commit_write(file, pages[i], offset,
1244 offset + this_write);
1250 WARN_ON(this_write > write_bytes);
1251 write_bytes -= this_write;
1257 static int prepare_pages(struct btrfs_trans_handle *trans,
1258 struct btrfs_root *root,
1260 struct page **pages,
1266 unsigned long index = pos >> PAGE_CACHE_SHIFT;
1267 struct inode *inode = file->f_path.dentry->d_inode;
1272 loff_t isize = i_size_read(inode);
1274 memset(pages, 0, num_pages * sizeof(struct page *));
1276 for (i = 0; i < num_pages; i++) {
1277 pages[i] = grab_cache_page(inode->i_mapping, index + i);
1280 goto failed_release;
1282 offset = pos & (PAGE_CACHE_SIZE -1);
1283 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1284 ret = nobh_prepare_write(pages[i], offset,
1285 offset + this_write,
1290 goto failed_truncate;
1292 WARN_ON(this_write > write_bytes);
1293 write_bytes -= this_write;
1298 btrfs_drop_pages(pages, num_pages);
1302 btrfs_drop_pages(pages, num_pages);
1304 vmtruncate(inode, isize);
1308 static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1309 size_t count, loff_t *ppos)
1312 size_t num_written = 0;
1315 struct inode *inode = file->f_path.dentry->d_inode;
1316 struct btrfs_root *root = BTRFS_I(inode)->root;
1317 struct page *pages[1];
1319 if (file->f_flags & O_DIRECT)
1323 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1324 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1325 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1330 err = remove_suid(file->f_path.dentry);
1333 file_update_time(file);
1334 mutex_lock(&inode->i_mutex);
1336 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1337 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1338 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
1340 ret = prepare_pages(NULL, root, file, pages, num_pages,
1343 ret = btrfs_copy_from_user(pos, num_pages,
1344 write_bytes, pages, buf);
1347 ret = dirty_and_release_pages(NULL, root, file, pages,
1348 num_pages, pos, write_bytes);
1350 btrfs_drop_pages(pages, num_pages);
1353 count -= write_bytes;
1355 num_written += write_bytes;
1357 balance_dirty_pages_ratelimited(inode->i_mapping);
1360 mutex_unlock(&inode->i_mutex);
1363 current->backing_dev_info = NULL;
1364 return num_written ? num_written : err;
1368 static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
1369 struct page *page, loff_t pos,
1370 size_t offset, size_t write_bytes)
1372 struct btrfs_path *path;
1373 struct btrfs_trans_handle *trans;
1374 struct btrfs_key key;
1375 struct btrfs_leaf *leaf;
1376 struct btrfs_key found_key;
1378 size_t copy_size = 0;
1381 size_t num_written = 0;
1383 path = btrfs_alloc_path();
1385 mutex_lock(&root->fs_info->fs_mutex);
1386 trans = btrfs_start_transaction(root, 1);
1387 key.objectid = inode->i_ino;
1389 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
1393 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1399 leaf = btrfs_buffer_leaf(path->nodes[0]);
1400 btrfs_disk_key_to_cpu(&found_key,
1401 &leaf->items[path->slots[0]].key);
1402 copy_size = btrfs_item_size(leaf->items + path->slots[0]);
1403 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1404 copy_size = min(write_bytes, copy_size);
1407 int slot = path->slots[0];
1411 // FIXME find max key
1412 leaf = btrfs_buffer_leaf(path->nodes[0]);
1413 btrfs_disk_key_to_cpu(&found_key,
1414 &leaf->items[slot].key);
1415 if (found_key.objectid != inode->i_ino)
1417 if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
1419 copy_size = btrfs_item_size(leaf->items + slot);
1420 if (found_key.offset + copy_size <= pos)
1422 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1423 dst += pos - found_key.offset;
1424 copy_size = copy_size - (pos - found_key.offset);
1425 BUG_ON(copy_size < 0);
1426 copy_size = min(write_bytes, copy_size);
1427 WARN_ON(copy_size == 0);
1431 btrfs_release_path(root, path);
1432 copy_size = min(write_bytes,
1433 (size_t)BTRFS_LEAF_DATA_SIZE(root) -
1434 sizeof(struct btrfs_item) * 4);
1435 ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
1437 dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1438 path->slots[0], char);
1440 WARN_ON(copy_size == 0);
1441 WARN_ON(dst + copy_size >
1442 btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1443 path->slots[0], char) +
1444 btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
1446 btrfs_memcpy(root, path->nodes[0]->b_data, dst,
1447 page_address(page) + offset, copy_size);
1448 mark_buffer_dirty(path->nodes[0]);
1449 btrfs_release_path(root, path);
1451 offset += copy_size;
1452 num_written += copy_size;
1453 write_bytes -= copy_size;
1457 btrfs_free_path(path);
1458 ret = btrfs_end_transaction(trans, root);
1460 mutex_unlock(&root->fs_info->fs_mutex);
1461 return num_written ? num_written : err;
1464 static ssize_t btrfs_file_inline_write(struct file *file,
1465 const char __user *buf,
1466 size_t count, loff_t *ppos)
1469 size_t num_written = 0;
1472 struct inode *inode = file->f_path.dentry->d_inode;
1473 struct btrfs_root *root = BTRFS_I(inode)->root;
1474 unsigned long page_index;
1476 if (file->f_flags & O_DIRECT)
1480 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1481 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1482 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1487 err = remove_suid(file->f_path.dentry);
1490 file_update_time(file);
1491 mutex_lock(&inode->i_mutex);
1493 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1494 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1497 page_index = pos >> PAGE_CACHE_SHIFT;
1498 page = grab_cache_page(inode->i_mapping, page_index);
1499 if (!PageUptodate(page)) {
1500 ret = mpage_readpage(page, btrfs_get_block);
1504 ret = btrfs_copy_from_user(pos, 1,
1505 write_bytes, &page, buf);
1507 write_bytes = inline_one_page(root, inode, page, pos,
1508 offset, write_bytes);
1509 SetPageUptodate(page);
1510 if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
1511 i_size_write(inode, pos + write_bytes);
1512 mark_inode_dirty(inode);
1514 page_cache_release(page);
1516 if (write_bytes < 0)
1519 count -= write_bytes;
1521 num_written += write_bytes;
1523 balance_dirty_pages_ratelimited(inode->i_mapping);
1527 mutex_unlock(&inode->i_mutex);
1530 current->backing_dev_info = NULL;
1531 return num_written ? num_written : err;
1535 static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
1536 unsigned long offset, unsigned long size)
1539 unsigned long left, count = desc->count;
1540 struct inode *inode = page->mapping->host;
1545 if (!PageChecked(page)) {
1546 /* FIXME, do it per block */
1547 struct btrfs_root *root = BTRFS_I(inode)->root;
1548 int ret = btrfs_csum_verify_file_block(root,
1549 page->mapping->host->i_ino,
1550 page->index << PAGE_CACHE_SHIFT,
1551 kmap(page), PAGE_CACHE_SIZE);
1553 printk("failed to verify ino %lu page %lu\n",
1554 page->mapping->host->i_ino,
1556 memset(page_address(page), 0, PAGE_CACHE_SIZE);
1558 SetPageChecked(page);
1562 * Faults on the destination of a read are common, so do it before
1565 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1566 kaddr = kmap_atomic(page, KM_USER0);
1567 left = __copy_to_user_inatomic(desc->arg.buf,
1568 kaddr + offset, size);
1569 kunmap_atomic(kaddr, KM_USER0);
1574 /* Do it the slow way */
1576 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1581 desc->error = -EFAULT;
1584 desc->count = count - size;
1585 desc->written += size;
1586 desc->arg.buf += size;
1591 * btrfs_file_aio_read - filesystem read routine
1592 * @iocb: kernel I/O control block
1593 * @iov: io vector request
1594 * @nr_segs: number of segments in the iovec
1595 * @pos: current file position
1597 static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1598 unsigned long nr_segs, loff_t pos)
1600 struct file *filp = iocb->ki_filp;
1604 loff_t *ppos = &iocb->ki_pos;
1607 for (seg = 0; seg < nr_segs; seg++) {
1608 const struct iovec *iv = &iov[seg];
1611 * If any segment has a negative length, or the cumulative
1612 * length ever wraps negative then return -EINVAL.
1614 count += iv->iov_len;
1615 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
1617 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
1622 count -= iv->iov_len; /* This segment is no good */
1627 for (seg = 0; seg < nr_segs; seg++) {
1628 read_descriptor_t desc;
1631 desc.arg.buf = iov[seg].iov_base;
1632 desc.count = iov[seg].iov_len;
1633 if (desc.count == 0)
1636 do_generic_file_read(filp, ppos, &desc,
1638 retval += desc.written;
1640 retval = retval ?: desc.error;
1648 static struct kmem_cache *btrfs_inode_cachep;
1649 struct kmem_cache *btrfs_trans_handle_cachep;
1650 struct kmem_cache *btrfs_transaction_cachep;
1651 struct kmem_cache *btrfs_bit_radix_cachep;
1652 struct kmem_cache *btrfs_path_cachep;
1655 * Called inside transaction, so use GFP_NOFS
1657 static struct inode *btrfs_alloc_inode(struct super_block *sb)
1659 struct btrfs_inode *ei;
1661 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
1664 ei->magic = 0xDEADBEEF;
1665 ei->magic2 = 0xDEADBEAF;
1666 return &ei->vfs_inode;
1669 static void btrfs_destroy_inode(struct inode *inode)
1671 struct btrfs_inode *ei = BTRFS_I(inode);
1672 WARN_ON(ei->magic != 0xDEADBEEF);
1673 WARN_ON(ei->magic2 != 0xDEADBEAF);
1674 WARN_ON(!list_empty(&inode->i_dentry));
1675 WARN_ON(inode->i_data.nrpages);
1679 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
1682 static void init_once(void * foo, struct kmem_cache * cachep,
1683 unsigned long flags)
1685 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
1687 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1688 SLAB_CTOR_CONSTRUCTOR) {
1689 inode_init_once(&ei->vfs_inode);
1693 static int init_inodecache(void)
1695 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
1696 sizeof(struct btrfs_inode),
1697 0, (SLAB_RECLAIM_ACCOUNT|
1700 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
1701 sizeof(struct btrfs_trans_handle),
1702 0, (SLAB_RECLAIM_ACCOUNT|
1705 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
1706 sizeof(struct btrfs_transaction),
1707 0, (SLAB_RECLAIM_ACCOUNT|
1710 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
1711 sizeof(struct btrfs_transaction),
1712 0, (SLAB_RECLAIM_ACCOUNT|
1715 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
1717 0, (SLAB_RECLAIM_ACCOUNT|
1719 SLAB_DESTROY_BY_RCU),
1721 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
1722 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
1727 static void destroy_inodecache(void)
1729 kmem_cache_destroy(btrfs_inode_cachep);
1730 kmem_cache_destroy(btrfs_trans_handle_cachep);
1731 kmem_cache_destroy(btrfs_transaction_cachep);
1732 kmem_cache_destroy(btrfs_bit_radix_cachep);
1733 kmem_cache_destroy(btrfs_path_cachep);
1736 static int btrfs_get_sb(struct file_system_type *fs_type,
1737 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1739 return get_sb_bdev(fs_type, flags, dev_name, data,
1740 btrfs_fill_super, mnt);
1743 static struct file_system_type btrfs_fs_type = {
1744 .owner = THIS_MODULE,
1746 .get_sb = btrfs_get_sb,
1747 .kill_sb = kill_block_super,
1748 .fs_flags = FS_REQUIRES_DEV,
1751 static struct super_operations btrfs_super_ops = {
1752 .statfs = simple_statfs,
1753 .delete_inode = btrfs_delete_inode,
1754 .put_super = btrfs_put_super,
1755 .read_inode = btrfs_read_locked_inode,
1756 .write_super = btrfs_write_super,
1757 .sync_fs = btrfs_sync_fs,
1758 .write_inode = btrfs_write_inode,
1759 .alloc_inode = btrfs_alloc_inode,
1760 .destroy_inode = btrfs_destroy_inode,
1763 static struct inode_operations btrfs_dir_inode_operations = {
1764 .lookup = btrfs_lookup,
1765 .create = btrfs_create,
1766 .unlink = btrfs_unlink,
1767 .mkdir = btrfs_mkdir,
1768 .rmdir = btrfs_rmdir,
1771 static struct inode_operations btrfs_dir_ro_inode_operations = {
1772 .lookup = btrfs_lookup,
1775 static struct file_operations btrfs_dir_file_operations = {
1776 .llseek = generic_file_llseek,
1777 .read = generic_read_dir,
1778 .readdir = btrfs_readdir,
1781 static struct address_space_operations btrfs_aops = {
1782 .readpage = btrfs_readpage,
1783 .readpages = btrfs_readpages,
1784 .writepage = btrfs_writepage,
1785 .sync_page = block_sync_page,
1786 .prepare_write = btrfs_prepare_write,
1787 .commit_write = btrfs_commit_write,
1790 static struct inode_operations btrfs_file_inode_operations = {
1791 .truncate = btrfs_truncate,
1794 static struct file_operations btrfs_file_operations = {
1795 .llseek = generic_file_llseek,
1796 .read = do_sync_read,
1797 .aio_read = btrfs_file_aio_read,
1798 .write = btrfs_file_write,
1799 .mmap = generic_file_mmap,
1800 .open = generic_file_open,
1803 static int __init init_btrfs_fs(void)
1806 printk("btrfs loaded!\n");
1807 err = init_inodecache();
1810 kset_set_kset_s(&btrfs_subsys, fs_subsys);
1811 err = subsystem_register(&btrfs_subsys);
1814 return register_filesystem(&btrfs_fs_type);
1816 destroy_inodecache();
1820 static void __exit exit_btrfs_fs(void)
1822 destroy_inodecache();
1823 unregister_filesystem(&btrfs_fs_type);
1824 subsystem_unregister(&btrfs_subsys);
1825 printk("btrfs unloaded\n");
1828 module_init(init_btrfs_fs)
1829 module_exit(exit_btrfs_fs)
1831 MODULE_LICENSE("GPL");