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)
321 if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
322 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
324 struct btrfs_file_extent_item);
325 if (btrfs_file_extent_type(fi) !=
326 BTRFS_FILE_EXTENT_INLINE) {
328 btrfs_file_extent_disk_blocknr(fi);
330 btrfs_file_extent_disk_num_blocks(fi);
331 /* FIXME blocksize != 4096 */
333 btrfs_file_extent_num_blocks(fi) << 3;
337 ret = btrfs_del_item(trans, root, path);
339 btrfs_release_path(root, path);
341 ret = btrfs_free_extent(trans, root, extent_start,
342 extent_num_blocks, 0);
348 btrfs_release_path(root, path);
349 btrfs_free_path(path);
353 static void btrfs_delete_inode(struct inode *inode)
355 struct btrfs_trans_handle *trans;
356 struct btrfs_root *root = BTRFS_I(inode)->root;
359 truncate_inode_pages(&inode->i_data, 0);
360 if (is_bad_inode(inode)) {
364 mutex_lock(&root->fs_info->fs_mutex);
365 trans = btrfs_start_transaction(root, 1);
366 if (S_ISREG(inode->i_mode)) {
367 ret = btrfs_truncate_in_trans(trans, root, inode);
370 btrfs_free_inode(trans, root, inode);
371 btrfs_end_transaction(trans, root);
372 mutex_unlock(&root->fs_info->fs_mutex);
378 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
379 struct btrfs_key *location)
381 const char *name = dentry->d_name.name;
382 int namelen = dentry->d_name.len;
383 struct btrfs_dir_item *di;
384 struct btrfs_path *path;
385 struct btrfs_root *root = BTRFS_I(dir)->root;
388 path = btrfs_alloc_path();
390 btrfs_init_path(path);
391 ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
393 if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) {
394 location->objectid = 0;
398 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
399 struct btrfs_dir_item);
400 btrfs_disk_key_to_cpu(location, &di->location);
402 btrfs_release_path(root, path);
403 btrfs_free_path(path);
407 int fixup_tree_root_location(struct btrfs_root *root,
408 struct btrfs_key *location,
409 struct btrfs_root **sub_root)
411 struct btrfs_path *path;
412 struct btrfs_root_item *ri;
414 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
416 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
419 path = btrfs_alloc_path();
421 mutex_lock(&root->fs_info->fs_mutex);
423 *sub_root = btrfs_read_fs_root(root->fs_info, location);
424 if (IS_ERR(*sub_root))
425 return PTR_ERR(*sub_root);
427 ri = &(*sub_root)->root_item;
428 location->objectid = btrfs_root_dirid(ri);
430 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
431 location->offset = 0;
433 btrfs_free_path(path);
434 mutex_unlock(&root->fs_info->fs_mutex);
438 int btrfs_init_locked_inode(struct inode *inode, void *p)
440 struct btrfs_iget_args *args = p;
441 inode->i_ino = args->ino;
442 BTRFS_I(inode)->root = args->root;
446 int btrfs_find_actor(struct inode *inode, void *opaque)
448 struct btrfs_iget_args *args = opaque;
449 return (args->ino == inode->i_ino &&
450 args->root == BTRFS_I(inode)->root);
453 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
454 struct btrfs_root *root)
457 struct btrfs_iget_args args;
461 inode = iget5_locked(s, objectid, btrfs_find_actor,
462 btrfs_init_locked_inode,
467 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
468 struct nameidata *nd)
470 struct inode * inode;
471 struct btrfs_inode *bi = BTRFS_I(dir);
472 struct btrfs_root *root = bi->root;
473 struct btrfs_root *sub_root = root;
474 struct btrfs_key location;
477 if (dentry->d_name.len > BTRFS_NAME_LEN)
478 return ERR_PTR(-ENAMETOOLONG);
479 mutex_lock(&root->fs_info->fs_mutex);
480 ret = btrfs_inode_by_name(dir, dentry, &location);
481 mutex_unlock(&root->fs_info->fs_mutex);
485 if (location.objectid) {
486 ret = fixup_tree_root_location(root, &location, &sub_root);
490 return ERR_PTR(-ENOENT);
491 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
494 return ERR_PTR(-EACCES);
495 if (inode->i_state & I_NEW) {
496 if (sub_root != root) {
497 printk("adding new root for inode %lu root %p (found %p)\n", inode->i_ino, sub_root, BTRFS_I(inode)->root);
499 sub_root->inode = inode;
501 BTRFS_I(inode)->root = sub_root;
502 memcpy(&BTRFS_I(inode)->location, &location,
504 btrfs_read_locked_inode(inode);
505 unlock_new_inode(inode);
508 return d_splice_alias(inode, dentry);
511 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
513 struct inode *inode = filp->f_path.dentry->d_inode;
514 struct btrfs_root *root = BTRFS_I(inode)->root;
515 struct btrfs_item *item;
516 struct btrfs_dir_item *di;
517 struct btrfs_key key;
518 struct btrfs_path *path;
521 struct btrfs_leaf *leaf;
524 unsigned char d_type = DT_UNKNOWN;
526 int key_type = BTRFS_DIR_ITEM_KEY;
528 /* FIXME, use a real flag for deciding about the key type */
529 if (root->fs_info->tree_root == root)
530 key_type = BTRFS_DIR_ITEM_KEY;
531 mutex_lock(&root->fs_info->fs_mutex);
532 key.objectid = inode->i_ino;
534 btrfs_set_key_type(&key, key_type);
535 key.offset = filp->f_pos;
536 path = btrfs_alloc_path();
537 btrfs_init_path(path);
538 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 (btrfs_disk_key_type(&item->key) != key_type)
565 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
567 filp->f_pos = btrfs_disk_key_offset(&item->key);
569 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
570 over = filldir(dirent, (const char *)(di + 1),
571 btrfs_dir_name_len(di),
572 btrfs_disk_key_offset(&item->key),
573 btrfs_disk_key_objectid(&di->location), d_type);
581 btrfs_release_path(root, path);
582 btrfs_free_path(path);
583 mutex_unlock(&root->fs_info->fs_mutex);
587 static void btrfs_put_super (struct super_block * sb)
589 struct btrfs_root *root = btrfs_sb(sb);
592 ret = close_ctree(root);
594 printk("close ctree returns %d\n", ret);
596 sb->s_fs_info = NULL;
599 static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
601 struct inode * inode;
602 struct dentry * root_dentry;
603 struct btrfs_super_block *disk_super;
604 struct btrfs_root *tree_root;
605 struct btrfs_inode *bi;
607 sb->s_maxbytes = MAX_LFS_FILESIZE;
608 sb->s_magic = BTRFS_SUPER_MAGIC;
609 sb->s_op = &btrfs_super_ops;
612 tree_root = open_ctree(sb);
615 printk("btrfs: open_ctree failed\n");
618 sb->s_fs_info = tree_root;
619 disk_super = tree_root->fs_info->disk_super;
620 printk("read in super total blocks %Lu root %Lu\n",
621 btrfs_super_total_blocks(disk_super),
622 btrfs_super_root_dir(disk_super));
624 inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
627 bi->location.objectid = inode->i_ino;
628 bi->location.offset = 0;
629 bi->location.flags = 0;
630 bi->root = tree_root;
631 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
635 if (inode->i_state & I_NEW) {
636 btrfs_read_locked_inode(inode);
637 unlock_new_inode(inode);
640 root_dentry = d_alloc_root(inode);
645 sb->s_root = root_dentry;
650 static void fill_inode_item(struct btrfs_inode_item *item,
653 btrfs_set_inode_uid(item, inode->i_uid);
654 btrfs_set_inode_gid(item, inode->i_gid);
655 btrfs_set_inode_size(item, inode->i_size);
656 btrfs_set_inode_mode(item, inode->i_mode);
657 btrfs_set_inode_nlink(item, inode->i_nlink);
658 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
659 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
660 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
661 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
662 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
663 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
664 btrfs_set_inode_nblocks(item, inode->i_blocks);
665 btrfs_set_inode_generation(item, inode->i_generation);
668 static int btrfs_update_inode(struct btrfs_trans_handle *trans,
669 struct btrfs_root *root,
672 struct btrfs_inode_item *inode_item;
673 struct btrfs_path *path;
676 path = btrfs_alloc_path();
678 btrfs_init_path(path);
679 ret = btrfs_lookup_inode(trans, root, path,
680 &BTRFS_I(inode)->location, 1);
687 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
689 struct btrfs_inode_item);
691 fill_inode_item(inode_item, inode);
692 btrfs_mark_buffer_dirty(path->nodes[0]);
695 btrfs_release_path(root, path);
696 btrfs_free_path(path);
700 static int btrfs_write_inode(struct inode *inode, int wait)
702 struct btrfs_root *root = BTRFS_I(inode)->root;
703 struct btrfs_trans_handle *trans;
706 mutex_lock(&root->fs_info->fs_mutex);
707 trans = btrfs_start_transaction(root, 1);
708 ret = btrfs_update_inode(trans, root, inode);
710 btrfs_commit_transaction(trans, root);
712 btrfs_end_transaction(trans, root);
713 mutex_unlock(&root->fs_info->fs_mutex);
717 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
718 struct btrfs_root *root,
719 u64 objectid, int mode)
722 struct btrfs_inode_item inode_item;
723 struct btrfs_key *location;
726 inode = new_inode(root->fs_info->sb);
728 return ERR_PTR(-ENOMEM);
730 BTRFS_I(inode)->root = root;
732 inode->i_uid = current->fsuid;
733 inode->i_gid = current->fsgid;
734 inode->i_mode = mode;
735 inode->i_ino = objectid;
737 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
738 fill_inode_item(&inode_item, inode);
739 location = &BTRFS_I(inode)->location;
740 location->objectid = objectid;
742 location->offset = 0;
743 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
745 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
748 insert_inode_hash(inode);
752 static int btrfs_add_link(struct btrfs_trans_handle *trans,
753 struct dentry *dentry, struct inode *inode)
756 struct btrfs_key key;
757 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
758 key.objectid = inode->i_ino;
760 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
763 ret = btrfs_insert_dir_item(trans, root,
764 dentry->d_name.name, dentry->d_name.len,
765 dentry->d_parent->d_inode->i_ino,
768 dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
769 ret = btrfs_update_inode(trans, root,
770 dentry->d_parent->d_inode);
775 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
776 struct dentry *dentry, struct inode *inode)
778 int err = btrfs_add_link(trans, dentry, inode);
780 d_instantiate(dentry, inode);
788 static int btrfs_create(struct inode *dir, struct dentry *dentry,
789 int mode, struct nameidata *nd)
791 struct btrfs_trans_handle *trans;
792 struct btrfs_root *root = BTRFS_I(dir)->root;
798 mutex_lock(&root->fs_info->fs_mutex);
799 trans = btrfs_start_transaction(root, 1);
801 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
807 inode = btrfs_new_inode(trans, root, objectid, mode);
808 err = PTR_ERR(inode);
811 // FIXME mark the inode dirty
812 err = btrfs_add_nondir(trans, dentry, inode);
816 inode->i_mapping->a_ops = &btrfs_aops;
817 inode->i_fop = &btrfs_file_operations;
818 inode->i_op = &btrfs_file_inode_operations;
820 dir->i_sb->s_dirt = 1;
822 btrfs_end_transaction(trans, root);
823 mutex_unlock(&root->fs_info->fs_mutex);
826 inode_dec_link_count(inode);
832 static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
833 struct btrfs_root *root,
834 u64 objectid, u64 dirid)
838 struct btrfs_key key;
843 key.objectid = objectid;
846 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
848 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
852 key.objectid = dirid;
853 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
861 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
864 struct btrfs_trans_handle *trans;
865 struct btrfs_root *root = BTRFS_I(dir)->root;
870 mutex_lock(&root->fs_info->fs_mutex);
871 trans = btrfs_start_transaction(root, 1);
873 err = PTR_ERR(trans);
877 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
883 inode = btrfs_new_inode(trans, root, objectid, S_IFDIR | mode);
885 err = PTR_ERR(inode);
889 inode->i_op = &btrfs_dir_inode_operations;
890 inode->i_fop = &btrfs_dir_file_operations;
892 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
897 err = btrfs_update_inode(trans, root, inode);
900 err = btrfs_add_link(trans, dentry, inode);
903 d_instantiate(dentry, inode);
907 btrfs_end_transaction(trans, root);
909 mutex_unlock(&root->fs_info->fs_mutex);
915 static int btrfs_sync_fs(struct super_block *sb, int wait)
917 struct btrfs_trans_handle *trans;
918 struct btrfs_root *root;
924 filemap_flush(root->fs_info->btree_inode->i_mapping);
927 filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
928 mutex_lock(&root->fs_info->fs_mutex);
929 trans = btrfs_start_transaction(root, 1);
930 ret = btrfs_commit_transaction(trans, root);
933 printk("btrfs sync_fs\n");
934 mutex_unlock(&root->fs_info->fs_mutex);
939 static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
940 struct buffer_head *result, int create)
942 struct btrfs_root *root = btrfs_sb(inode->i_sb);
943 struct btrfs_path *path;
944 struct btrfs_key key;
945 struct btrfs_leaf *leaf;
946 int num_bytes = result->b_size;
956 path = btrfs_alloc_path();
963 pos = iblock << inode->i_blkbits;
964 key.objectid = inode->i_ino;
966 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
967 ptr = kmap(result->b_page);
969 ptr += (pos & (PAGE_CACHE_SIZE -1));
972 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
980 leaf = btrfs_buffer_leaf(path->nodes[0]);
981 item_size = btrfs_item_size(leaf->items + path->slots[0]);
982 copy_size = min(num_bytes, item_size);
983 data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
984 WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
985 memcpy(ptr, data_ptr, copy_size);
987 num_bytes -= copy_size;
988 WARN_ON(num_bytes < 0);
990 btrfs_release_path(root, path);
991 if (num_bytes != 0) {
992 if (pos >= i_size_read(inode))
993 memset(ptr, 0, num_bytes);
997 set_buffer_uptodate(result);
998 map_bh(result, inode->i_sb, 0);
1001 btrfs_free_path(path);
1002 kunmap(result->b_page);
1007 static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
1008 struct buffer_head *result, int create)
1013 u64 extent_start = 0;
1015 u64 objectid = inode->i_ino;
1017 struct btrfs_path *path;
1018 struct btrfs_root *root = BTRFS_I(inode)->root;
1019 struct btrfs_file_extent_item *item;
1020 struct btrfs_leaf *leaf;
1021 struct btrfs_disk_key *found_key;
1023 path = btrfs_alloc_path();
1025 btrfs_init_path(path);
1030 ret = btrfs_lookup_file_extent(NULL, 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 /* are we inside the extent that was found? */
1053 found_key = &leaf->items[path->slots[0]].key;
1054 found_type = btrfs_disk_key_type(found_key);
1055 if (btrfs_disk_key_objectid(found_key) != objectid ||
1056 found_type != BTRFS_EXTENT_DATA_KEY) {
1059 btrfs_release_path(root, path);
1062 found_type = btrfs_file_extent_type(item);
1063 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
1064 if (found_type == BTRFS_FILE_EXTENT_REG) {
1065 extent_start = extent_start >> inode->i_blkbits;
1066 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
1067 if (iblock >= extent_start && iblock < extent_end) {
1069 btrfs_map_bh_to_logical(root, result, blocknr +
1070 iblock - extent_start);
1073 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1077 size = btrfs_file_extent_inline_len(leaf->items +
1079 extent_end = (extent_start + size) >> inode->i_blkbits;
1080 extent_start >>= inode->i_blkbits;
1081 if (iblock < extent_start || iblock > extent_end) {
1084 ptr = btrfs_file_extent_inline_start(item);
1085 map = kmap(result->b_page);
1086 memcpy(map, ptr, size);
1087 memset(map + size, 0, PAGE_CACHE_SIZE - size);
1088 flush_dcache_page(result->b_page);
1089 kunmap(result->b_page);
1090 set_buffer_uptodate(result);
1091 SetPageChecked(result->b_page);
1092 btrfs_map_bh_to_logical(root, result, 0);
1095 btrfs_release_path(root, path);
1096 btrfs_free_path(path);
1100 static int btrfs_get_block(struct inode *inode, sector_t iblock,
1101 struct buffer_head *result, int create)
1104 struct btrfs_root *root = BTRFS_I(inode)->root;
1105 mutex_lock(&root->fs_info->fs_mutex);
1106 err = btrfs_get_block_lock(inode, iblock, result, create);
1107 mutex_unlock(&root->fs_info->fs_mutex);
1111 static int btrfs_prepare_write(struct file *file, struct page *page,
1112 unsigned from, unsigned to)
1114 return nobh_prepare_write(page, from, to, btrfs_get_block);
1117 static void btrfs_write_super(struct super_block *sb)
1119 btrfs_sync_fs(sb, 1);
1122 static int btrfs_readpage(struct file *file, struct page *page)
1124 return mpage_readpage(page, btrfs_get_block);
1128 * While block_write_full_page is writing back the dirty buffers under
1129 * the page lock, whoever dirtied the buffers may decide to clean them
1130 * again at any time. We handle that by only looking at the buffer
1131 * state inside lock_buffer().
1133 * If block_write_full_page() is called for regular writeback
1134 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1135 * locked buffer. This only can happen if someone has written the buffer
1136 * directly, with submit_bh(). At the address_space level PageWriteback
1137 * prevents this contention from occurring.
1139 static int __btrfs_write_full_page(struct inode *inode, struct page *page,
1140 struct writeback_control *wbc)
1144 sector_t last_block;
1145 struct buffer_head *bh, *head;
1146 const unsigned blocksize = 1 << inode->i_blkbits;
1147 int nr_underway = 0;
1149 BUG_ON(!PageLocked(page));
1151 last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
1153 if (!page_has_buffers(page)) {
1154 create_empty_buffers(page, blocksize,
1155 (1 << BH_Dirty)|(1 << BH_Uptodate));
1159 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1160 * here, and the (potentially unmapped) buffers may become dirty at
1161 * any time. If a buffer becomes dirty here after we've inspected it
1162 * then we just miss that fact, and the page stays dirty.
1164 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1165 * handle that here by just cleaning them.
1168 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1169 head = page_buffers(page);
1173 * Get all the dirty buffers mapped to disk addresses and
1174 * handle any aliases from the underlying blockdev's mapping.
1177 if (block > last_block) {
1179 * mapped buffers outside i_size will occur, because
1180 * this page can be outside i_size when there is a
1181 * truncate in progress.
1184 * The buffer was zeroed by block_write_full_page()
1186 clear_buffer_dirty(bh);
1187 set_buffer_uptodate(bh);
1188 } else if (!buffer_mapped(bh) && buffer_dirty(bh)) {
1189 WARN_ON(bh->b_size != blocksize);
1190 err = btrfs_get_block(inode, block, bh, 0);
1193 if (buffer_new(bh)) {
1194 /* blockdev mappings never come here */
1195 clear_buffer_new(bh);
1196 unmap_underlying_metadata(bh->b_bdev,
1200 bh = bh->b_this_page;
1202 } while (bh != head);
1205 if (!buffer_mapped(bh))
1208 * If it's a fully non-blocking write attempt and we cannot
1209 * lock the buffer then redirty the page. Note that this can
1210 * potentially cause a busy-wait loop from pdflush and kswapd
1211 * activity, but those code paths have their own higher-level
1214 if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
1216 } else if (test_set_buffer_locked(bh)) {
1217 redirty_page_for_writepage(wbc, page);
1220 if (test_clear_buffer_dirty(bh) && bh->b_blocknr != 0) {
1221 mark_buffer_async_write(bh);
1225 } while ((bh = bh->b_this_page) != head);
1228 * The page and its buffers are protected by PageWriteback(), so we can
1229 * drop the bh refcounts early.
1231 BUG_ON(PageWriteback(page));
1232 set_page_writeback(page);
1235 struct buffer_head *next = bh->b_this_page;
1236 if (buffer_async_write(bh)) {
1237 submit_bh(WRITE, bh);
1241 } while (bh != head);
1246 if (nr_underway == 0) {
1248 * The page was marked dirty, but the buffers were
1249 * clean. Someone wrote them back by hand with
1250 * ll_rw_block/submit_bh. A rare case.
1254 if (!buffer_uptodate(bh)) {
1258 bh = bh->b_this_page;
1259 } while (bh != head);
1261 SetPageUptodate(page);
1262 end_page_writeback(page);
1264 * The page and buffer_heads can be released at any time from
1267 wbc->pages_skipped++; /* We didn't write this page */
1273 * ENOSPC, or some other error. We may already have added some
1274 * blocks to the file, so we need to write these out to avoid
1275 * exposing stale data.
1276 * The page is currently locked and not marked for writeback
1279 /* Recovery: lock and submit the mapped buffers */
1281 if (buffer_mapped(bh) && buffer_dirty(bh)) {
1283 mark_buffer_async_write(bh);
1286 * The buffer may have been set dirty during
1287 * attachment to a dirty page.
1289 clear_buffer_dirty(bh);
1291 } while ((bh = bh->b_this_page) != head);
1293 BUG_ON(PageWriteback(page));
1294 set_page_writeback(page);
1296 struct buffer_head *next = bh->b_this_page;
1297 if (buffer_async_write(bh)) {
1298 clear_buffer_dirty(bh);
1299 submit_bh(WRITE, bh);
1303 } while (bh != head);
1309 * The generic ->writepage function for buffer-backed address_spaces
1311 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1313 struct inode * const inode = page->mapping->host;
1314 loff_t i_size = i_size_read(inode);
1315 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
1319 /* Is the page fully inside i_size? */
1320 if (page->index < end_index)
1321 return __btrfs_write_full_page(inode, page, wbc);
1323 /* Is the page fully outside i_size? (truncate in progress) */
1324 offset = i_size & (PAGE_CACHE_SIZE-1);
1325 if (page->index >= end_index+1 || !offset) {
1327 * The page may have dirty, unmapped buffers. For example,
1328 * they may have been added in ext3_writepage(). Make them
1329 * freeable here, so the page does not leak.
1331 block_invalidatepage(page, 0);
1333 return 0; /* don't care */
1337 * The page straddles i_size. It must be zeroed out on each and every
1338 * writepage invokation because it may be mmapped. "A file is mapped
1339 * in multiples of the page size. For a file that is not a multiple of
1340 * the page size, the remaining memory is zeroed when mapped, and
1341 * writes to that region are not written out to the file."
1343 kaddr = kmap_atomic(page, KM_USER0);
1344 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1345 flush_dcache_page(page);
1346 kunmap_atomic(kaddr, KM_USER0);
1347 return __btrfs_write_full_page(inode, page, wbc);
1350 static void btrfs_truncate(struct inode *inode)
1352 struct btrfs_root *root = BTRFS_I(inode)->root;
1354 struct btrfs_trans_handle *trans;
1356 if (!S_ISREG(inode->i_mode))
1358 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1361 nobh_truncate_page(inode->i_mapping, inode->i_size);
1363 /* FIXME, add redo link to tree so we don't leak on crash */
1364 mutex_lock(&root->fs_info->fs_mutex);
1365 trans = btrfs_start_transaction(root, 1);
1366 ret = btrfs_truncate_in_trans(trans, root, inode);
1368 ret = btrfs_end_transaction(trans, root);
1370 mutex_unlock(&root->fs_info->fs_mutex);
1371 mark_inode_dirty(inode);
1375 * Make sure any changes to nobh_commit_write() are reflected in
1376 * nobh_truncate_page(), since it doesn't call commit_write().
1378 static int btrfs_commit_write(struct file *file, struct page *page,
1379 unsigned from, unsigned to)
1381 struct inode *inode = page->mapping->host;
1382 struct buffer_head *bh;
1383 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1385 SetPageUptodate(page);
1386 bh = page_buffers(page);
1387 if (buffer_mapped(bh) && bh->b_blocknr != 0) {
1388 set_page_dirty(page);
1390 if (pos > inode->i_size) {
1391 i_size_write(inode, pos);
1392 mark_inode_dirty(inode);
1397 static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
1398 struct page **prepared_pages,
1399 const char __user * buf)
1401 long page_fault = 0;
1403 int offset = pos & (PAGE_CACHE_SIZE - 1);
1405 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
1406 size_t count = min_t(size_t,
1407 PAGE_CACHE_SIZE - offset, write_bytes);
1408 struct page *page = prepared_pages[i];
1409 fault_in_pages_readable(buf, count);
1411 /* Copy data from userspace to the current page */
1413 page_fault = __copy_from_user(page_address(page) + offset,
1415 /* Flush processor's dcache for this page */
1416 flush_dcache_page(page);
1419 write_bytes -= count;
1424 return page_fault ? -EFAULT : 0;
1427 static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1430 for (i = 0; i < num_pages; i++) {
1433 unlock_page(pages[i]);
1434 mark_page_accessed(pages[i]);
1435 page_cache_release(pages[i]);
1438 static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1439 struct btrfs_root *root,
1441 struct page **pages,
1451 struct inode *inode = file->f_path.dentry->d_inode;
1452 struct buffer_head *bh;
1453 struct btrfs_file_extent_item *ei;
1455 for (i = 0; i < num_pages; i++) {
1456 offset = pos & (PAGE_CACHE_SIZE -1);
1457 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1458 /* FIXME, one block at a time */
1460 mutex_lock(&root->fs_info->fs_mutex);
1461 trans = btrfs_start_transaction(root, 1);
1463 bh = page_buffers(pages[i]);
1464 if (buffer_mapped(bh) && bh->b_blocknr == 0) {
1465 struct btrfs_key key;
1466 struct btrfs_path *path;
1470 path = btrfs_alloc_path();
1472 key.objectid = inode->i_ino;
1473 key.offset = pages[i]->index << PAGE_CACHE_SHIFT;
1475 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
1476 BUG_ON(write_bytes >= PAGE_CACHE_SIZE);
1478 btrfs_file_extent_calc_inline_size(write_bytes);
1479 ret = btrfs_insert_empty_item(trans, root, path, &key,
1482 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1483 path->slots[0], struct btrfs_file_extent_item);
1484 btrfs_set_file_extent_generation(ei, trans->transid);
1485 btrfs_set_file_extent_type(ei,
1486 BTRFS_FILE_EXTENT_INLINE);
1487 ptr = btrfs_file_extent_inline_start(ei);
1488 memcpy(ptr, bh->b_data, offset + write_bytes);
1489 mark_buffer_dirty(path->nodes[0]);
1490 btrfs_free_path(path);
1492 btrfs_csum_file_block(trans, root, inode->i_ino,
1493 pages[i]->index << PAGE_CACHE_SHIFT,
1494 kmap(pages[i]), PAGE_CACHE_SIZE);
1497 SetPageChecked(pages[i]);
1498 ret = btrfs_end_transaction(trans, root);
1500 mutex_unlock(&root->fs_info->fs_mutex);
1502 ret = btrfs_commit_write(file, pages[i], offset,
1503 offset + this_write);
1509 WARN_ON(this_write > write_bytes);
1510 write_bytes -= this_write;
1516 static int drop_extents(struct btrfs_trans_handle *trans,
1517 struct btrfs_root *root,
1518 struct inode *inode,
1522 struct btrfs_key key;
1523 struct btrfs_leaf *leaf;
1525 struct btrfs_file_extent_item *extent;
1528 struct btrfs_file_extent_item old;
1529 struct btrfs_path *path;
1530 u64 search_start = start;
1536 path = btrfs_alloc_path();
1540 btrfs_release_path(root, path);
1541 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1546 if (path->slots[0] == 0) {
1557 leaf = btrfs_buffer_leaf(path->nodes[0]);
1558 slot = path->slots[0];
1559 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
1560 if (key.offset >= end || key.objectid != inode->i_ino) {
1564 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) {
1568 extent = btrfs_item_ptr(leaf, slot,
1569 struct btrfs_file_extent_item);
1570 found_type = btrfs_file_extent_type(extent);
1571 if (found_type == BTRFS_FILE_EXTENT_REG) {
1572 extent_end = key.offset +
1573 (btrfs_file_extent_num_blocks(extent) <<
1576 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1578 extent_end = key.offset +
1579 btrfs_file_extent_inline_len(leaf->items + slot);
1582 if (!found_extent && !found_inline) {
1587 if (search_start >= extent_end) {
1592 search_start = extent_end;
1594 if (end < extent_end && end >= key.offset) {
1596 memcpy(&old, extent, sizeof(old));
1597 ret = btrfs_inc_extent_ref(trans, root,
1598 btrfs_file_extent_disk_blocknr(&old),
1599 btrfs_file_extent_disk_num_blocks(&old));
1602 WARN_ON(found_inline);
1606 if (start > key.offset) {
1609 /* truncate existing extent */
1611 WARN_ON(start & (root->blocksize - 1));
1613 new_num = (start - key.offset) >>
1615 old_num = btrfs_file_extent_num_blocks(extent);
1616 inode->i_blocks -= (old_num - new_num) << 3;
1617 btrfs_set_file_extent_num_blocks(extent,
1619 mark_buffer_dirty(path->nodes[0]);
1623 ret = btrfs_truncate_item(trans, root, path,
1624 start - key.offset);
1630 u64 disk_blocknr = 0;
1631 u64 disk_num_blocks = 0;
1632 u64 extent_num_blocks = 0;
1635 btrfs_file_extent_disk_blocknr(extent);
1637 btrfs_file_extent_disk_num_blocks(extent);
1639 btrfs_file_extent_num_blocks(extent);
1641 ret = btrfs_del_item(trans, root, path);
1643 btrfs_release_path(root, path);
1646 btrfs_file_extent_num_blocks(extent) << 3;
1647 ret = btrfs_free_extent(trans, root,
1649 disk_num_blocks, 0);
1653 if (!bookend && search_start >= end) {
1660 if (bookend && found_extent) {
1661 /* create bookend */
1662 struct btrfs_key ins;
1663 ins.objectid = inode->i_ino;
1666 btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
1668 btrfs_release_path(root, path);
1669 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1672 extent = btrfs_item_ptr(
1673 btrfs_buffer_leaf(path->nodes[0]),
1675 struct btrfs_file_extent_item);
1676 btrfs_set_file_extent_disk_blocknr(extent,
1677 btrfs_file_extent_disk_blocknr(&old));
1678 btrfs_set_file_extent_disk_num_blocks(extent,
1679 btrfs_file_extent_disk_num_blocks(&old));
1681 btrfs_set_file_extent_offset(extent,
1682 btrfs_file_extent_offset(&old) +
1683 ((end - key.offset) >> inode->i_blkbits));
1684 WARN_ON(btrfs_file_extent_num_blocks(&old) <
1685 (end - key.offset) >> inode->i_blkbits);
1686 btrfs_set_file_extent_num_blocks(extent,
1687 btrfs_file_extent_num_blocks(&old) -
1688 ((end - key.offset) >> inode->i_blkbits));
1690 btrfs_set_file_extent_type(extent,
1691 BTRFS_FILE_EXTENT_REG);
1692 btrfs_set_file_extent_generation(extent,
1693 btrfs_file_extent_generation(&old));
1694 btrfs_mark_buffer_dirty(path->nodes[0]);
1696 btrfs_file_extent_num_blocks(extent) << 3;
1702 btrfs_free_path(path);
1706 static int prepare_pages(struct btrfs_root *root,
1708 struct page **pages,
1711 unsigned long first_index,
1712 unsigned long last_index,
1714 u64 alloc_extent_start)
1717 unsigned long index = pos >> PAGE_CACHE_SHIFT;
1718 struct inode *inode = file->f_path.dentry->d_inode;
1722 struct buffer_head *bh;
1723 struct buffer_head *head;
1724 loff_t isize = i_size_read(inode);
1726 memset(pages, 0, num_pages * sizeof(struct page *));
1728 for (i = 0; i < num_pages; i++) {
1729 pages[i] = grab_cache_page(inode->i_mapping, index + i);
1732 goto failed_release;
1734 offset = pos & (PAGE_CACHE_SIZE -1);
1735 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1736 create_empty_buffers(pages[i], root->fs_info->sb->s_blocksize,
1737 (1 << BH_Uptodate));
1738 head = page_buffers(pages[i]);
1741 err = btrfs_map_bh_to_logical(root, bh,
1742 alloc_extent_start);
1745 goto failed_truncate;
1746 bh = bh->b_this_page;
1747 if (alloc_extent_start)
1748 alloc_extent_start++;
1749 } while (bh != head);
1751 WARN_ON(this_write > write_bytes);
1752 write_bytes -= this_write;
1757 btrfs_drop_pages(pages, num_pages);
1761 btrfs_drop_pages(pages, num_pages);
1763 vmtruncate(inode, isize);
1767 static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1768 size_t count, loff_t *ppos)
1771 size_t num_written = 0;
1774 struct inode *inode = file->f_path.dentry->d_inode;
1775 struct btrfs_root *root = BTRFS_I(inode)->root;
1776 struct page *pages[8];
1777 struct page *pinned[2] = { NULL, NULL };
1778 unsigned long first_index;
1779 unsigned long last_index;
1782 u64 alloc_extent_start;
1783 struct btrfs_trans_handle *trans;
1784 struct btrfs_key ins;
1786 if (file->f_flags & O_DIRECT)
1789 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1790 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1791 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1796 err = remove_suid(file->f_path.dentry);
1799 file_update_time(file);
1801 start_pos = pos & ~((u64)PAGE_CACHE_SIZE - 1);
1802 num_blocks = (count + pos - start_pos + root->blocksize - 1) >>
1805 mutex_lock(&inode->i_mutex);
1806 first_index = pos >> PAGE_CACHE_SHIFT;
1807 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
1809 if ((first_index << PAGE_CACHE_SHIFT) < inode->i_size &&
1810 (pos & (PAGE_CACHE_SIZE - 1))) {
1811 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
1812 if (!PageUptodate(pinned[0])) {
1813 ret = mpage_readpage(pinned[0], btrfs_get_block);
1816 unlock_page(pinned[0]);
1819 if (first_index != last_index &&
1820 (last_index << PAGE_CACHE_SHIFT) < inode->i_size &&
1821 (count & (PAGE_CACHE_SIZE - 1))) {
1822 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
1823 if (!PageUptodate(pinned[1])) {
1824 ret = mpage_readpage(pinned[1], btrfs_get_block);
1827 unlock_page(pinned[1]);
1831 mutex_lock(&root->fs_info->fs_mutex);
1832 trans = btrfs_start_transaction(root, 1);
1835 mutex_unlock(&root->fs_info->fs_mutex);
1838 /* FIXME blocksize != 4096 */
1839 inode->i_blocks += num_blocks << 3;
1840 if (start_pos < inode->i_size) {
1841 /* FIXME blocksize != pagesize */
1842 ret = drop_extents(trans, root, inode,
1844 (pos + count + root->blocksize -1) &
1845 ~((u64)root->blocksize - 1));
1848 if (inode->i_size >= PAGE_CACHE_SIZE || pos + count < inode->i_size ||
1849 pos + count - start_pos > BTRFS_MAX_INLINE_DATA_SIZE(root)) {
1850 ret = btrfs_alloc_extent(trans, root, num_blocks, 1,
1853 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
1854 start_pos, ins.objectid, ins.offset);
1861 alloc_extent_start = ins.objectid;
1862 ret = btrfs_end_transaction(trans, root);
1863 mutex_unlock(&root->fs_info->fs_mutex);
1866 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1867 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1868 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
1871 memset(pages, 0, sizeof(pages));
1872 ret = prepare_pages(root, file, pages, num_pages,
1873 pos, first_index, last_index,
1874 write_bytes, alloc_extent_start);
1877 /* FIXME blocks != pagesize */
1878 if (alloc_extent_start)
1879 alloc_extent_start += num_pages;
1880 ret = btrfs_copy_from_user(pos, num_pages,
1881 write_bytes, pages, buf);
1884 ret = dirty_and_release_pages(NULL, root, file, pages,
1885 num_pages, pos, write_bytes);
1887 btrfs_drop_pages(pages, num_pages);
1890 count -= write_bytes;
1892 num_written += write_bytes;
1894 balance_dirty_pages_ratelimited(inode->i_mapping);
1898 mutex_unlock(&inode->i_mutex);
1901 page_cache_release(pinned[0]);
1903 page_cache_release(pinned[1]);
1905 current->backing_dev_info = NULL;
1906 mark_inode_dirty(inode);
1907 return num_written ? num_written : err;
1911 static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
1912 struct page *page, loff_t pos,
1913 size_t offset, size_t write_bytes)
1915 struct btrfs_path *path;
1916 struct btrfs_trans_handle *trans;
1917 struct btrfs_key key;
1918 struct btrfs_leaf *leaf;
1919 struct btrfs_key found_key;
1921 size_t copy_size = 0;
1924 size_t num_written = 0;
1926 path = btrfs_alloc_path();
1928 mutex_lock(&root->fs_info->fs_mutex);
1929 trans = btrfs_start_transaction(root, 1);
1930 key.objectid = inode->i_ino;
1932 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
1936 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1942 leaf = btrfs_buffer_leaf(path->nodes[0]);
1943 btrfs_disk_key_to_cpu(&found_key,
1944 &leaf->items[path->slots[0]].key);
1945 copy_size = btrfs_item_size(leaf->items + path->slots[0]);
1946 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1947 copy_size = min(write_bytes, copy_size);
1950 int slot = path->slots[0];
1954 // FIXME find max key
1955 leaf = btrfs_buffer_leaf(path->nodes[0]);
1956 btrfs_disk_key_to_cpu(&found_key,
1957 &leaf->items[slot].key);
1958 if (found_key.objectid != inode->i_ino)
1960 if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
1962 copy_size = btrfs_item_size(leaf->items + slot);
1963 if (found_key.offset + copy_size <= pos)
1965 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1966 dst += pos - found_key.offset;
1967 copy_size = copy_size - (pos - found_key.offset);
1968 BUG_ON(copy_size < 0);
1969 copy_size = min(write_bytes, copy_size);
1970 WARN_ON(copy_size == 0);
1974 btrfs_release_path(root, path);
1975 copy_size = min(write_bytes,
1976 (size_t)BTRFS_LEAF_DATA_SIZE(root) -
1977 sizeof(struct btrfs_item) * 4);
1978 ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
1980 dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1981 path->slots[0], char);
1983 WARN_ON(copy_size == 0);
1984 WARN_ON(dst + copy_size >
1985 btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1986 path->slots[0], char) +
1987 btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
1989 btrfs_memcpy(root, path->nodes[0]->b_data, dst,
1990 page_address(page) + offset, copy_size);
1991 mark_buffer_dirty(path->nodes[0]);
1992 btrfs_release_path(root, path);
1994 offset += copy_size;
1995 num_written += copy_size;
1996 write_bytes -= copy_size;
2000 btrfs_free_path(path);
2001 ret = btrfs_end_transaction(trans, root);
2003 mutex_unlock(&root->fs_info->fs_mutex);
2004 return num_written ? num_written : err;
2007 static ssize_t btrfs_file_inline_write(struct file *file,
2008 const char __user *buf,
2009 size_t count, loff_t *ppos)
2012 size_t num_written = 0;
2015 struct inode *inode = file->f_path.dentry->d_inode;
2016 struct btrfs_root *root = BTRFS_I(inode)->root;
2017 unsigned long page_index;
2019 if (file->f_flags & O_DIRECT)
2023 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
2024 current->backing_dev_info = inode->i_mapping->backing_dev_info;
2025 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
2030 err = remove_suid(file->f_path.dentry);
2033 file_update_time(file);
2034 mutex_lock(&inode->i_mutex);
2036 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
2037 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
2040 page_index = pos >> PAGE_CACHE_SHIFT;
2041 page = grab_cache_page(inode->i_mapping, page_index);
2042 if (!PageUptodate(page)) {
2043 ret = mpage_readpage(page, btrfs_get_block);
2047 ret = btrfs_copy_from_user(pos, 1,
2048 write_bytes, &page, buf);
2050 write_bytes = inline_one_page(root, inode, page, pos,
2051 offset, write_bytes);
2052 SetPageUptodate(page);
2053 if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
2054 i_size_write(inode, pos + write_bytes);
2055 mark_inode_dirty(inode);
2057 page_cache_release(page);
2059 if (write_bytes < 0)
2062 count -= write_bytes;
2064 num_written += write_bytes;
2066 balance_dirty_pages_ratelimited(inode->i_mapping);
2070 mutex_unlock(&inode->i_mutex);
2073 current->backing_dev_info = NULL;
2074 return num_written ? num_written : err;
2078 static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
2079 unsigned long offset, unsigned long size)
2082 unsigned long left, count = desc->count;
2083 struct inode *inode = page->mapping->host;
2088 if (!PageChecked(page)) {
2089 /* FIXME, do it per block */
2090 struct btrfs_root *root = BTRFS_I(inode)->root;
2092 int ret = btrfs_csum_verify_file_block(root,
2093 page->mapping->host->i_ino,
2094 page->index << PAGE_CACHE_SHIFT,
2095 kmap(page), PAGE_CACHE_SIZE);
2097 printk("failed to verify ino %lu page %lu\n",
2098 page->mapping->host->i_ino,
2100 memset(page_address(page), 0, PAGE_CACHE_SIZE);
2102 SetPageChecked(page);
2106 * Faults on the destination of a read are common, so do it before
2109 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
2110 kaddr = kmap_atomic(page, KM_USER0);
2111 left = __copy_to_user_inatomic(desc->arg.buf,
2112 kaddr + offset, size);
2113 kunmap_atomic(kaddr, KM_USER0);
2118 /* Do it the slow way */
2120 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
2125 desc->error = -EFAULT;
2128 desc->count = count - size;
2129 desc->written += size;
2130 desc->arg.buf += size;
2135 * btrfs_file_aio_read - filesystem read routine
2136 * @iocb: kernel I/O control block
2137 * @iov: io vector request
2138 * @nr_segs: number of segments in the iovec
2139 * @pos: current file position
2141 static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
2142 unsigned long nr_segs, loff_t pos)
2144 struct file *filp = iocb->ki_filp;
2148 loff_t *ppos = &iocb->ki_pos;
2151 for (seg = 0; seg < nr_segs; seg++) {
2152 const struct iovec *iv = &iov[seg];
2155 * If any segment has a negative length, or the cumulative
2156 * length ever wraps negative then return -EINVAL.
2158 count += iv->iov_len;
2159 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
2161 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
2166 count -= iv->iov_len; /* This segment is no good */
2171 for (seg = 0; seg < nr_segs; seg++) {
2172 read_descriptor_t desc;
2175 desc.arg.buf = iov[seg].iov_base;
2176 desc.count = iov[seg].iov_len;
2177 if (desc.count == 0)
2180 do_generic_file_read(filp, ppos, &desc,
2182 retval += desc.written;
2184 retval = retval ?: desc.error;
2192 static int create_subvol(struct btrfs_root *root, char *name, int namelen)
2194 struct btrfs_trans_handle *trans;
2195 struct btrfs_key key;
2196 struct btrfs_root_item root_item;
2197 struct btrfs_inode_item *inode_item;
2198 struct buffer_head *subvol;
2199 struct btrfs_leaf *leaf;
2200 struct btrfs_root *new_root;
2201 struct inode *inode;
2204 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2206 mutex_lock(&root->fs_info->fs_mutex);
2207 trans = btrfs_start_transaction(root, 1);
2210 subvol = btrfs_alloc_free_block(trans, root);
2211 leaf = btrfs_buffer_leaf(subvol);
2212 btrfs_set_header_nritems(&leaf->header, 0);
2213 btrfs_set_header_level(&leaf->header, 0);
2214 btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
2215 btrfs_set_header_generation(&leaf->header, trans->transid);
2216 memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
2217 sizeof(leaf->header.fsid));
2219 inode_item = &root_item.inode;
2220 memset(inode_item, 0, sizeof(*inode_item));
2221 btrfs_set_inode_generation(inode_item, 1);
2222 btrfs_set_inode_size(inode_item, 3);
2223 btrfs_set_inode_nlink(inode_item, 1);
2224 btrfs_set_inode_nblocks(inode_item, 1);
2225 btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
2227 btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
2228 btrfs_set_root_refs(&root_item, 1);
2230 mark_buffer_dirty(subvol);
2234 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2238 btrfs_set_root_dirid(&root_item, new_dirid);
2240 key.objectid = objectid;
2243 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2244 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2249 * insert the directory item
2251 key.offset = (u64)-1;
2252 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2254 root->fs_info->sb->s_root->d_inode->i_ino,
2258 ret = btrfs_commit_transaction(trans, root);
2261 new_root = btrfs_read_fs_root(root->fs_info, &key);
2264 trans = btrfs_start_transaction(new_root, 1);
2267 inode = btrfs_new_inode(trans, new_root, new_dirid, S_IFDIR | 0700);
2268 inode->i_op = &btrfs_dir_inode_operations;
2269 inode->i_fop = &btrfs_dir_file_operations;
2271 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
2276 ret = btrfs_update_inode(trans, new_root, inode);
2279 ret = btrfs_commit_transaction(trans, new_root);
2284 mutex_unlock(&root->fs_info->fs_mutex);
2288 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2290 struct btrfs_trans_handle *trans;
2291 struct btrfs_key key;
2292 struct btrfs_root_item new_root_item;
2296 if (!root->ref_cows)
2299 mutex_lock(&root->fs_info->fs_mutex);
2300 trans = btrfs_start_transaction(root, 1);
2303 ret = btrfs_update_inode(trans, root, root->inode);
2306 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2310 memcpy(&new_root_item, &root->root_item,
2311 sizeof(new_root_item));
2313 key.objectid = objectid;
2316 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2317 btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
2319 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2324 * insert the directory item
2326 key.offset = (u64)-1;
2327 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2329 root->fs_info->sb->s_root->d_inode->i_ino,
2334 ret = btrfs_inc_root_ref(trans, root);
2337 ret = btrfs_commit_transaction(trans, root);
2339 mutex_unlock(&root->fs_info->fs_mutex);
2343 static int add_disk(struct btrfs_root *root, char *name, int namelen)
2345 struct block_device *bdev;
2346 struct btrfs_path *path;
2347 struct super_block *sb = root->fs_info->sb;
2348 struct btrfs_root *dev_root = root->fs_info->dev_root;
2349 struct btrfs_trans_handle *trans;
2350 struct btrfs_device_item *dev_item;
2351 struct btrfs_key key;
2358 printk("adding disk %s\n", name);
2359 path = btrfs_alloc_path();
2362 num_blocks = btrfs_super_total_blocks(root->fs_info->disk_super);
2363 bdev = open_bdev_excl(name, O_RDWR, sb);
2365 ret = PTR_ERR(bdev);
2366 printk("open bdev excl failed ret %d\n", ret);
2369 set_blocksize(bdev, sb->s_blocksize);
2370 new_blocks = bdev->bd_inode->i_size >> sb->s_blocksize_bits;
2371 key.objectid = num_blocks;
2372 key.offset = new_blocks;
2374 btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY);
2376 mutex_lock(&dev_root->fs_info->fs_mutex);
2377 trans = btrfs_start_transaction(dev_root, 1);
2378 item_size = sizeof(*dev_item) + namelen;
2379 printk("insert empty on %Lu %Lu %u size %d\n", num_blocks, new_blocks, key.flags, item_size);
2380 ret = btrfs_insert_empty_item(trans, dev_root, path, &key, item_size);
2382 printk("insert failed %d\n", ret);
2383 close_bdev_excl(bdev);
2388 dev_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
2389 path->slots[0], struct btrfs_device_item);
2390 btrfs_set_device_pathlen(dev_item, namelen);
2391 memcpy(dev_item + 1, name, namelen);
2393 device_id = btrfs_super_last_device_id(root->fs_info->disk_super) + 1;
2394 btrfs_set_super_last_device_id(root->fs_info->disk_super, device_id);
2395 btrfs_set_device_id(dev_item, device_id);
2396 mark_buffer_dirty(path->nodes[0]);
2398 ret = btrfs_insert_dev_radix(root, bdev, device_id, num_blocks,
2402 btrfs_set_super_total_blocks(root->fs_info->disk_super,
2403 num_blocks + new_blocks);
2404 i_size_write(root->fs_info->btree_inode,
2405 (num_blocks + new_blocks) <<
2406 root->fs_info->btree_inode->i_blkbits);
2410 ret = btrfs_commit_transaction(trans, dev_root);
2412 mutex_unlock(&root->fs_info->fs_mutex);
2414 btrfs_free_path(path);
2419 static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
2420 cmd, unsigned long arg)
2422 struct btrfs_root *root = BTRFS_I(inode)->root;
2423 struct btrfs_ioctl_vol_args vol_args;
2426 struct btrfs_path *path;
2430 case BTRFS_IOC_SNAP_CREATE:
2431 if (copy_from_user(&vol_args,
2432 (struct btrfs_ioctl_vol_args __user *)arg,
2435 namelen = strlen(vol_args.name);
2436 if (namelen > BTRFS_VOL_NAME_MAX)
2438 path = btrfs_alloc_path();
2441 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2442 mutex_lock(&root->fs_info->fs_mutex);
2443 ret = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2445 vol_args.name, namelen, 0);
2446 mutex_unlock(&root->fs_info->fs_mutex);
2447 btrfs_free_path(path);
2451 if (root == root->fs_info->tree_root)
2452 ret = create_subvol(root, vol_args.name, namelen);
2454 ret = create_snapshot(root, vol_args.name, namelen);
2457 case BTRFS_IOC_ADD_DISK:
2458 if (copy_from_user(&vol_args,
2459 (struct btrfs_ioctl_vol_args __user *)arg,
2462 namelen = strlen(vol_args.name);
2463 if (namelen > BTRFS_VOL_NAME_MAX)
2465 vol_args.name[namelen] = '\0';
2466 ret = add_disk(root, vol_args.name, namelen);
2474 static struct kmem_cache *btrfs_inode_cachep;
2475 struct kmem_cache *btrfs_trans_handle_cachep;
2476 struct kmem_cache *btrfs_transaction_cachep;
2477 struct kmem_cache *btrfs_bit_radix_cachep;
2478 struct kmem_cache *btrfs_path_cachep;
2481 * Called inside transaction, so use GFP_NOFS
2483 static struct inode *btrfs_alloc_inode(struct super_block *sb)
2485 struct btrfs_inode *ei;
2487 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2490 return &ei->vfs_inode;
2493 static void btrfs_destroy_inode(struct inode *inode)
2495 WARN_ON(!list_empty(&inode->i_dentry));
2496 WARN_ON(inode->i_data.nrpages);
2498 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2501 static void init_once(void * foo, struct kmem_cache * cachep,
2502 unsigned long flags)
2504 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2506 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
2507 SLAB_CTOR_CONSTRUCTOR) {
2508 inode_init_once(&ei->vfs_inode);
2512 static int init_inodecache(void)
2514 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
2515 sizeof(struct btrfs_inode),
2516 0, (SLAB_RECLAIM_ACCOUNT|
2519 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
2520 sizeof(struct btrfs_trans_handle),
2521 0, (SLAB_RECLAIM_ACCOUNT|
2524 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
2525 sizeof(struct btrfs_transaction),
2526 0, (SLAB_RECLAIM_ACCOUNT|
2529 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
2530 sizeof(struct btrfs_transaction),
2531 0, (SLAB_RECLAIM_ACCOUNT|
2534 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
2536 0, (SLAB_RECLAIM_ACCOUNT|
2538 SLAB_DESTROY_BY_RCU),
2540 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
2541 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
2546 static void destroy_inodecache(void)
2548 kmem_cache_destroy(btrfs_inode_cachep);
2549 kmem_cache_destroy(btrfs_trans_handle_cachep);
2550 kmem_cache_destroy(btrfs_transaction_cachep);
2551 kmem_cache_destroy(btrfs_bit_radix_cachep);
2552 kmem_cache_destroy(btrfs_path_cachep);
2555 static int btrfs_get_sb(struct file_system_type *fs_type,
2556 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2558 return get_sb_bdev(fs_type, flags, dev_name, data,
2559 btrfs_fill_super, mnt);
2563 static int btrfs_getattr(struct vfsmount *mnt,
2564 struct dentry *dentry, struct kstat *stat)
2566 struct inode *inode = dentry->d_inode;
2567 generic_fillattr(inode, stat);
2568 stat->blksize = 256 * 1024;
2572 static struct file_system_type btrfs_fs_type = {
2573 .owner = THIS_MODULE,
2575 .get_sb = btrfs_get_sb,
2576 .kill_sb = kill_block_super,
2577 .fs_flags = FS_REQUIRES_DEV,
2580 static struct super_operations btrfs_super_ops = {
2581 .statfs = simple_statfs,
2582 .delete_inode = btrfs_delete_inode,
2583 .put_super = btrfs_put_super,
2584 .read_inode = btrfs_read_locked_inode,
2585 .write_super = btrfs_write_super,
2586 .sync_fs = btrfs_sync_fs,
2587 .write_inode = btrfs_write_inode,
2588 .alloc_inode = btrfs_alloc_inode,
2589 .destroy_inode = btrfs_destroy_inode,
2592 static struct inode_operations btrfs_dir_inode_operations = {
2593 .lookup = btrfs_lookup,
2594 .create = btrfs_create,
2595 .unlink = btrfs_unlink,
2596 .mkdir = btrfs_mkdir,
2597 .rmdir = btrfs_rmdir,
2600 static struct inode_operations btrfs_dir_ro_inode_operations = {
2601 .lookup = btrfs_lookup,
2604 static struct file_operations btrfs_dir_file_operations = {
2605 .llseek = generic_file_llseek,
2606 .read = generic_read_dir,
2607 .readdir = btrfs_readdir,
2608 .ioctl = btrfs_ioctl,
2611 static struct address_space_operations btrfs_aops = {
2612 .readpage = btrfs_readpage,
2613 .writepage = btrfs_writepage,
2614 .sync_page = block_sync_page,
2615 .prepare_write = btrfs_prepare_write,
2616 .commit_write = btrfs_commit_write,
2619 static struct inode_operations btrfs_file_inode_operations = {
2620 .truncate = btrfs_truncate,
2621 .getattr = btrfs_getattr,
2624 static struct file_operations btrfs_file_operations = {
2625 .llseek = generic_file_llseek,
2626 .read = do_sync_read,
2627 .aio_read = btrfs_file_aio_read,
2628 .write = btrfs_file_write,
2629 .mmap = generic_file_mmap,
2630 .open = generic_file_open,
2631 .ioctl = btrfs_ioctl,
2634 static int __init init_btrfs_fs(void)
2637 printk("btrfs loaded!\n");
2638 err = init_inodecache();
2641 kset_set_kset_s(&btrfs_subsys, fs_subsys);
2642 err = subsystem_register(&btrfs_subsys);
2645 return register_filesystem(&btrfs_fs_type);
2647 destroy_inodecache();
2651 static void __exit exit_btrfs_fs(void)
2653 destroy_inodecache();
2654 unregister_filesystem(&btrfs_fs_type);
2655 subsystem_unregister(&btrfs_subsys);
2656 printk("btrfs unloaded\n");
2659 module_init(init_btrfs_fs)
2660 module_exit(exit_btrfs_fs)
2662 MODULE_LICENSE("GPL");