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>
14 #include <linux/statfs.h>
17 #include "transaction.h"
18 #include "btrfs_inode.h"
21 struct btrfs_iget_args {
23 struct btrfs_root *root;
26 #define BTRFS_SUPER_MAGIC 0x9123682E
28 static struct inode_operations btrfs_dir_inode_operations;
29 static struct inode_operations btrfs_dir_ro_inode_operations;
30 static struct super_operations btrfs_super_ops;
31 static struct file_operations btrfs_dir_file_operations;
32 static struct inode_operations btrfs_file_inode_operations;
33 static struct address_space_operations btrfs_aops;
34 static struct file_operations btrfs_file_operations;
36 static void btrfs_read_locked_inode(struct inode *inode)
38 struct btrfs_path *path;
39 struct btrfs_inode_item *inode_item;
40 struct btrfs_root *root = BTRFS_I(inode)->root;
41 struct btrfs_key location;
42 struct btrfs_block_group_cache *alloc_group;
43 u64 alloc_group_block;
46 path = btrfs_alloc_path();
48 btrfs_init_path(path);
49 mutex_lock(&root->fs_info->fs_mutex);
51 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
52 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
54 btrfs_free_path(path);
57 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
59 struct btrfs_inode_item);
61 inode->i_mode = btrfs_inode_mode(inode_item);
62 inode->i_nlink = btrfs_inode_nlink(inode_item);
63 inode->i_uid = btrfs_inode_uid(inode_item);
64 inode->i_gid = btrfs_inode_gid(inode_item);
65 inode->i_size = btrfs_inode_size(inode_item);
66 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
67 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
68 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
69 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
70 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
71 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
72 inode->i_blocks = btrfs_inode_nblocks(inode_item);
73 inode->i_generation = btrfs_inode_generation(inode_item);
74 alloc_group_block = btrfs_inode_block_group(inode_item);
75 ret = radix_tree_gang_lookup(&root->fs_info->block_group_radix,
76 (void **)&alloc_group,
77 alloc_group_block, 1);
79 BTRFS_I(inode)->block_group = alloc_group;
81 btrfs_free_path(path);
84 mutex_unlock(&root->fs_info->fs_mutex);
86 switch (inode->i_mode & S_IFMT) {
89 init_special_inode(inode, inode->i_mode,
90 btrfs_inode_rdev(inode_item));
94 inode->i_mapping->a_ops = &btrfs_aops;
95 inode->i_fop = &btrfs_file_operations;
96 inode->i_op = &btrfs_file_inode_operations;
99 inode->i_fop = &btrfs_dir_file_operations;
100 if (root == root->fs_info->tree_root)
101 inode->i_op = &btrfs_dir_ro_inode_operations;
103 inode->i_op = &btrfs_dir_inode_operations;
106 // inode->i_op = &page_symlink_inode_operations;
112 btrfs_release_path(root, path);
113 btrfs_free_path(path);
114 mutex_unlock(&root->fs_info->fs_mutex);
115 make_bad_inode(inode);
118 static void fill_inode_item(struct btrfs_inode_item *item,
121 btrfs_set_inode_uid(item, inode->i_uid);
122 btrfs_set_inode_gid(item, inode->i_gid);
123 btrfs_set_inode_size(item, inode->i_size);
124 btrfs_set_inode_mode(item, inode->i_mode);
125 btrfs_set_inode_nlink(item, inode->i_nlink);
126 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
127 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
128 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
129 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
130 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
131 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
132 btrfs_set_inode_nblocks(item, inode->i_blocks);
133 btrfs_set_inode_generation(item, inode->i_generation);
134 btrfs_set_inode_block_group(item,
135 BTRFS_I(inode)->block_group->key.objectid);
138 static int btrfs_update_inode(struct btrfs_trans_handle *trans,
139 struct btrfs_root *root,
142 struct btrfs_inode_item *inode_item;
143 struct btrfs_path *path;
146 path = btrfs_alloc_path();
148 btrfs_init_path(path);
149 ret = btrfs_lookup_inode(trans, root, path,
150 &BTRFS_I(inode)->location, 1);
157 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
159 struct btrfs_inode_item);
161 fill_inode_item(inode_item, inode);
162 btrfs_mark_buffer_dirty(path->nodes[0]);
165 btrfs_release_path(root, path);
166 btrfs_free_path(path);
171 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
172 struct btrfs_root *root,
174 struct dentry *dentry)
176 struct btrfs_path *path;
177 const char *name = dentry->d_name.name;
178 int name_len = dentry->d_name.len;
181 struct btrfs_dir_item *di;
183 path = btrfs_alloc_path();
185 btrfs_init_path(path);
186 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
196 objectid = btrfs_disk_key_objectid(&di->location);
197 ret = btrfs_delete_one_dir_name(trans, root, path, di);
199 btrfs_release_path(root, path);
201 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
202 objectid, name, name_len, -1);
211 ret = btrfs_delete_one_dir_name(trans, root, path, di);
214 dentry->d_inode->i_ctime = dir->i_ctime;
216 btrfs_free_path(path);
218 dir->i_size -= name_len * 2;
219 btrfs_update_inode(trans, root, dir);
220 drop_nlink(dentry->d_inode);
221 btrfs_update_inode(trans, root, dentry->d_inode);
222 dir->i_sb->s_dirt = 1;
227 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
229 struct btrfs_root *root;
230 struct btrfs_trans_handle *trans;
233 root = BTRFS_I(dir)->root;
234 mutex_lock(&root->fs_info->fs_mutex);
235 trans = btrfs_start_transaction(root, 1);
236 btrfs_set_trans_block_group(trans, dir);
237 ret = btrfs_unlink_trans(trans, root, dir, dentry);
238 btrfs_end_transaction(trans, root);
239 mutex_unlock(&root->fs_info->fs_mutex);
240 btrfs_btree_balance_dirty(root);
244 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
246 struct inode *inode = dentry->d_inode;
249 struct btrfs_root *root = BTRFS_I(dir)->root;
250 struct btrfs_path *path;
251 struct btrfs_key key;
252 struct btrfs_trans_handle *trans;
253 struct btrfs_key found_key;
255 struct btrfs_leaf *leaf;
256 char *goodnames = "..";
258 path = btrfs_alloc_path();
260 btrfs_init_path(path);
261 mutex_lock(&root->fs_info->fs_mutex);
262 trans = btrfs_start_transaction(root, 1);
263 btrfs_set_trans_block_group(trans, dir);
264 key.objectid = inode->i_ino;
265 key.offset = (u64)-1;
268 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
274 if (path->slots[0] == 0) {
279 leaf = btrfs_buffer_leaf(path->nodes[0]);
280 btrfs_disk_key_to_cpu(&found_key,
281 &leaf->items[path->slots[0]].key);
282 found_type = btrfs_key_type(&found_key);
283 if (found_key.objectid != inode->i_ino) {
287 if ((found_type != BTRFS_DIR_ITEM_KEY &&
288 found_type != BTRFS_DIR_INDEX_KEY) ||
289 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
290 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
294 ret = btrfs_del_item(trans, root, path);
297 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
299 btrfs_release_path(root, path);
302 btrfs_release_path(root, path);
304 /* now the directory is empty */
305 err = btrfs_unlink_trans(trans, root, dir, dentry);
310 btrfs_release_path(root, path);
311 btrfs_free_path(path);
312 mutex_unlock(&root->fs_info->fs_mutex);
313 ret = btrfs_end_transaction(trans, root);
314 btrfs_btree_balance_dirty(root);
320 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
321 struct btrfs_root *root,
324 struct btrfs_path *path;
329 path = btrfs_alloc_path();
331 btrfs_init_path(path);
332 ret = btrfs_lookup_inode(trans, root, path,
333 &BTRFS_I(inode)->location, -1);
335 ret = btrfs_del_item(trans, root, path);
337 btrfs_free_path(path);
341 static void reada_truncate(struct btrfs_root *root, struct btrfs_path *path,
344 struct btrfs_node *node;
354 node = btrfs_buffer_node(path->nodes[1]);
355 slot = path->slots[1];
358 nritems = btrfs_header_nritems(&node->header);
359 for (i = slot - 1; i >= 0; i--) {
360 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
361 if (item_objectid != objectid)
363 blocknr = btrfs_node_blockptr(node, i);
364 ret = readahead_tree_block(root, blocknr);
370 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
371 struct btrfs_root *root,
375 struct btrfs_path *path;
376 struct btrfs_key key;
377 struct btrfs_disk_key *found_key;
379 struct btrfs_leaf *leaf;
380 struct btrfs_file_extent_item *fi = NULL;
381 u64 extent_start = 0;
382 u64 extent_num_blocks = 0;
385 path = btrfs_alloc_path();
387 /* FIXME, add redo link to tree so we don't leak on crash */
388 key.objectid = inode->i_ino;
389 key.offset = (u64)-1;
392 btrfs_init_path(path);
393 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
398 BUG_ON(path->slots[0] == 0);
401 reada_truncate(root, path, inode->i_ino);
402 leaf = btrfs_buffer_leaf(path->nodes[0]);
403 found_key = &leaf->items[path->slots[0]].key;
404 found_type = btrfs_disk_key_type(found_key);
405 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
407 if (found_type != BTRFS_CSUM_ITEM_KEY &&
408 found_type != BTRFS_DIR_ITEM_KEY &&
409 found_type != BTRFS_DIR_INDEX_KEY &&
410 found_type != BTRFS_EXTENT_DATA_KEY)
412 if (btrfs_disk_key_offset(found_key) < inode->i_size)
415 if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
416 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
418 struct btrfs_file_extent_item);
419 if (btrfs_file_extent_type(fi) !=
420 BTRFS_FILE_EXTENT_INLINE) {
422 btrfs_file_extent_disk_blocknr(fi);
424 btrfs_file_extent_disk_num_blocks(fi);
425 /* FIXME blocksize != 4096 */
427 btrfs_file_extent_num_blocks(fi) << 3;
431 ret = btrfs_del_item(trans, root, path);
433 btrfs_release_path(root, path);
435 ret = btrfs_free_extent(trans, root, extent_start,
436 extent_num_blocks, 0);
442 btrfs_release_path(root, path);
443 btrfs_free_path(path);
444 inode->i_sb->s_dirt = 1;
448 static void btrfs_delete_inode(struct inode *inode)
450 struct btrfs_trans_handle *trans;
451 struct btrfs_root *root = BTRFS_I(inode)->root;
454 truncate_inode_pages(&inode->i_data, 0);
455 if (is_bad_inode(inode)) {
459 mutex_lock(&root->fs_info->fs_mutex);
460 trans = btrfs_start_transaction(root, 1);
461 btrfs_set_trans_block_group(trans, inode);
462 ret = btrfs_truncate_in_trans(trans, root, inode);
464 btrfs_free_inode(trans, root, inode);
465 btrfs_end_transaction(trans, root);
466 mutex_unlock(&root->fs_info->fs_mutex);
467 btrfs_btree_balance_dirty(root);
473 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
474 struct btrfs_key *location)
476 const char *name = dentry->d_name.name;
477 int namelen = dentry->d_name.len;
478 struct btrfs_dir_item *di;
479 struct btrfs_path *path;
480 struct btrfs_root *root = BTRFS_I(dir)->root;
483 path = btrfs_alloc_path();
485 btrfs_init_path(path);
486 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
488 if (!di || IS_ERR(di)) {
489 location->objectid = 0;
493 btrfs_disk_key_to_cpu(location, &di->location);
495 btrfs_release_path(root, path);
496 btrfs_free_path(path);
500 static int fixup_tree_root_location(struct btrfs_root *root,
501 struct btrfs_key *location,
502 struct btrfs_root **sub_root)
504 struct btrfs_path *path;
505 struct btrfs_root_item *ri;
507 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
509 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
512 path = btrfs_alloc_path();
514 mutex_lock(&root->fs_info->fs_mutex);
516 *sub_root = btrfs_read_fs_root(root->fs_info, location);
517 if (IS_ERR(*sub_root))
518 return PTR_ERR(*sub_root);
520 ri = &(*sub_root)->root_item;
521 location->objectid = btrfs_root_dirid(ri);
523 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
524 location->offset = 0;
526 btrfs_free_path(path);
527 mutex_unlock(&root->fs_info->fs_mutex);
531 static int btrfs_init_locked_inode(struct inode *inode, void *p)
533 struct btrfs_iget_args *args = p;
534 inode->i_ino = args->ino;
535 BTRFS_I(inode)->root = args->root;
539 static int btrfs_find_actor(struct inode *inode, void *opaque)
541 struct btrfs_iget_args *args = opaque;
542 return (args->ino == inode->i_ino &&
543 args->root == BTRFS_I(inode)->root);
546 static struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
547 struct btrfs_root *root)
550 struct btrfs_iget_args args;
554 inode = iget5_locked(s, objectid, btrfs_find_actor,
555 btrfs_init_locked_inode,
560 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
561 struct nameidata *nd)
563 struct inode * inode;
564 struct btrfs_inode *bi = BTRFS_I(dir);
565 struct btrfs_root *root = bi->root;
566 struct btrfs_root *sub_root = root;
567 struct btrfs_key location;
570 if (dentry->d_name.len > BTRFS_NAME_LEN)
571 return ERR_PTR(-ENAMETOOLONG);
572 mutex_lock(&root->fs_info->fs_mutex);
573 ret = btrfs_inode_by_name(dir, dentry, &location);
574 mutex_unlock(&root->fs_info->fs_mutex);
578 if (location.objectid) {
579 ret = fixup_tree_root_location(root, &location, &sub_root);
583 return ERR_PTR(-ENOENT);
584 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
587 return ERR_PTR(-EACCES);
588 if (inode->i_state & I_NEW) {
589 if (sub_root != root) {
590 printk("adding new root for inode %lu root %p (found %p)\n", inode->i_ino, sub_root, BTRFS_I(inode)->root);
592 sub_root->inode = inode;
594 BTRFS_I(inode)->root = sub_root;
595 memcpy(&BTRFS_I(inode)->location, &location,
597 btrfs_read_locked_inode(inode);
598 unlock_new_inode(inode);
601 return d_splice_alias(inode, dentry);
604 static void reada_leaves(struct btrfs_root *root, struct btrfs_path *path,
607 struct btrfs_node *node;
617 node = btrfs_buffer_node(path->nodes[1]);
618 slot = path->slots[1];
619 nritems = btrfs_header_nritems(&node->header);
620 for (i = slot + 1; i < nritems; i++) {
621 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
622 if (item_objectid != objectid)
624 blocknr = btrfs_node_blockptr(node, i);
625 ret = readahead_tree_block(root, blocknr);
631 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
633 struct inode *inode = filp->f_path.dentry->d_inode;
634 struct btrfs_root *root = BTRFS_I(inode)->root;
635 struct btrfs_item *item;
636 struct btrfs_dir_item *di;
637 struct btrfs_key key;
638 struct btrfs_path *path;
641 struct btrfs_leaf *leaf;
644 unsigned char d_type = DT_UNKNOWN;
649 int key_type = BTRFS_DIR_INDEX_KEY;
651 /* FIXME, use a real flag for deciding about the key type */
652 if (root->fs_info->tree_root == root)
653 key_type = BTRFS_DIR_ITEM_KEY;
654 mutex_lock(&root->fs_info->fs_mutex);
655 key.objectid = inode->i_ino;
657 btrfs_set_key_type(&key, key_type);
658 key.offset = filp->f_pos;
659 path = btrfs_alloc_path();
660 btrfs_init_path(path);
661 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
665 reada_leaves(root, path, inode->i_ino);
667 leaf = btrfs_buffer_leaf(path->nodes[0]);
668 nritems = btrfs_header_nritems(&leaf->header);
669 slot = path->slots[0];
670 if (advance || slot >= nritems) {
671 if (slot >= nritems -1) {
672 reada_leaves(root, path, inode->i_ino);
673 ret = btrfs_next_leaf(root, path);
676 leaf = btrfs_buffer_leaf(path->nodes[0]);
677 nritems = btrfs_header_nritems(&leaf->header);
678 slot = path->slots[0];
685 item = leaf->items + slot;
686 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
688 if (btrfs_disk_key_type(&item->key) != key_type)
690 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
692 filp->f_pos = btrfs_disk_key_offset(&item->key);
694 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
696 di_total = btrfs_item_size(leaf->items + slot);
697 while(di_cur < di_total) {
698 over = filldir(dirent, (const char *)(di + 1),
699 btrfs_dir_name_len(di),
700 btrfs_disk_key_offset(&item->key),
701 btrfs_disk_key_objectid(&di->location),
705 di_len = btrfs_dir_name_len(di) + sizeof(*di);
707 di = (struct btrfs_dir_item *)((char *)di + di_len);
714 btrfs_release_path(root, path);
715 btrfs_free_path(path);
716 mutex_unlock(&root->fs_info->fs_mutex);
720 static void btrfs_put_super (struct super_block * sb)
722 struct btrfs_root *root = btrfs_sb(sb);
725 ret = close_ctree(root);
727 printk("close ctree returns %d\n", ret);
729 sb->s_fs_info = NULL;
732 static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
734 struct inode * inode;
735 struct dentry * root_dentry;
736 struct btrfs_super_block *disk_super;
737 struct btrfs_root *tree_root;
738 struct btrfs_inode *bi;
740 sb->s_maxbytes = MAX_LFS_FILESIZE;
741 sb->s_magic = BTRFS_SUPER_MAGIC;
742 sb->s_op = &btrfs_super_ops;
745 tree_root = open_ctree(sb);
748 printk("btrfs: open_ctree failed\n");
751 sb->s_fs_info = tree_root;
752 disk_super = tree_root->fs_info->disk_super;
753 printk("read in super total blocks %Lu root %Lu\n",
754 btrfs_super_total_blocks(disk_super),
755 btrfs_super_root_dir(disk_super));
757 inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
760 bi->location.objectid = inode->i_ino;
761 bi->location.offset = 0;
762 bi->location.flags = 0;
763 bi->root = tree_root;
764 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
768 if (inode->i_state & I_NEW) {
769 btrfs_read_locked_inode(inode);
770 unlock_new_inode(inode);
773 root_dentry = d_alloc_root(inode);
778 sb->s_root = root_dentry;
783 static int btrfs_write_inode(struct inode *inode, int wait)
785 struct btrfs_root *root = BTRFS_I(inode)->root;
786 struct btrfs_trans_handle *trans;
790 mutex_lock(&root->fs_info->fs_mutex);
791 trans = btrfs_start_transaction(root, 1);
792 btrfs_set_trans_block_group(trans, inode);
793 ret = btrfs_commit_transaction(trans, root);
794 mutex_unlock(&root->fs_info->fs_mutex);
799 static void btrfs_dirty_inode(struct inode *inode)
801 struct btrfs_root *root = BTRFS_I(inode)->root;
802 struct btrfs_trans_handle *trans;
804 mutex_lock(&root->fs_info->fs_mutex);
805 trans = btrfs_start_transaction(root, 1);
806 btrfs_set_trans_block_group(trans, inode);
807 btrfs_update_inode(trans, root, inode);
808 btrfs_end_transaction(trans, root);
809 mutex_unlock(&root->fs_info->fs_mutex);
810 btrfs_btree_balance_dirty(root);
813 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
814 struct btrfs_root *root,
816 struct btrfs_block_group_cache *group,
820 struct btrfs_inode_item inode_item;
821 struct btrfs_key *location;
825 inode = new_inode(root->fs_info->sb);
827 return ERR_PTR(-ENOMEM);
829 BTRFS_I(inode)->root = root;
834 group = btrfs_find_block_group(root, group, 0, 0, owner);
835 BTRFS_I(inode)->block_group = group;
837 inode->i_uid = current->fsuid;
838 inode->i_gid = current->fsgid;
839 inode->i_mode = mode;
840 inode->i_ino = objectid;
842 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
843 fill_inode_item(&inode_item, inode);
844 location = &BTRFS_I(inode)->location;
845 location->objectid = objectid;
847 location->offset = 0;
848 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
850 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
853 insert_inode_hash(inode);
857 static int btrfs_add_link(struct btrfs_trans_handle *trans,
858 struct dentry *dentry, struct inode *inode)
861 struct btrfs_key key;
862 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
863 key.objectid = inode->i_ino;
865 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
868 ret = btrfs_insert_dir_item(trans, root,
869 dentry->d_name.name, dentry->d_name.len,
870 dentry->d_parent->d_inode->i_ino,
873 dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
874 ret = btrfs_update_inode(trans, root,
875 dentry->d_parent->d_inode);
880 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
881 struct dentry *dentry, struct inode *inode)
883 int err = btrfs_add_link(trans, dentry, inode);
885 d_instantiate(dentry, inode);
893 static int btrfs_create(struct inode *dir, struct dentry *dentry,
894 int mode, struct nameidata *nd)
896 struct btrfs_trans_handle *trans;
897 struct btrfs_root *root = BTRFS_I(dir)->root;
903 mutex_lock(&root->fs_info->fs_mutex);
904 trans = btrfs_start_transaction(root, 1);
905 btrfs_set_trans_block_group(trans, dir);
907 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
913 inode = btrfs_new_inode(trans, root, objectid,
914 BTRFS_I(dir)->block_group, mode);
915 err = PTR_ERR(inode);
919 btrfs_set_trans_block_group(trans, inode);
920 err = btrfs_add_nondir(trans, dentry, inode);
924 inode->i_mapping->a_ops = &btrfs_aops;
925 inode->i_fop = &btrfs_file_operations;
926 inode->i_op = &btrfs_file_inode_operations;
928 dir->i_sb->s_dirt = 1;
929 btrfs_update_inode_block_group(trans, inode);
930 btrfs_update_inode_block_group(trans, dir);
932 btrfs_end_transaction(trans, root);
933 mutex_unlock(&root->fs_info->fs_mutex);
936 inode_dec_link_count(inode);
939 btrfs_btree_balance_dirty(root);
943 static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
944 struct btrfs_root *root,
945 u64 objectid, u64 dirid)
949 struct btrfs_key key;
954 key.objectid = objectid;
957 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
959 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
963 key.objectid = dirid;
964 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
972 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
975 struct btrfs_trans_handle *trans;
976 struct btrfs_root *root = BTRFS_I(dir)->root;
981 mutex_lock(&root->fs_info->fs_mutex);
982 trans = btrfs_start_transaction(root, 1);
983 btrfs_set_trans_block_group(trans, dir);
985 err = PTR_ERR(trans);
989 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
995 inode = btrfs_new_inode(trans, root, objectid,
996 BTRFS_I(dir)->block_group, S_IFDIR | mode);
998 err = PTR_ERR(inode);
1002 inode->i_op = &btrfs_dir_inode_operations;
1003 inode->i_fop = &btrfs_dir_file_operations;
1004 btrfs_set_trans_block_group(trans, inode);
1006 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
1011 err = btrfs_update_inode(trans, root, inode);
1014 err = btrfs_add_link(trans, dentry, inode);
1017 d_instantiate(dentry, inode);
1019 dir->i_sb->s_dirt = 1;
1020 btrfs_update_inode_block_group(trans, inode);
1021 btrfs_update_inode_block_group(trans, dir);
1024 btrfs_end_transaction(trans, root);
1026 mutex_unlock(&root->fs_info->fs_mutex);
1029 btrfs_btree_balance_dirty(root);
1033 static int btrfs_sync_file(struct file *file,
1034 struct dentry *dentry, int datasync)
1036 struct inode *inode = dentry->d_inode;
1037 struct btrfs_root *root = BTRFS_I(inode)->root;
1039 struct btrfs_trans_handle *trans;
1041 mutex_lock(&root->fs_info->fs_mutex);
1042 trans = btrfs_start_transaction(root, 1);
1047 ret = btrfs_commit_transaction(trans, root);
1048 mutex_unlock(&root->fs_info->fs_mutex);
1050 return ret > 0 ? EIO : ret;
1053 static int btrfs_sync_fs(struct super_block *sb, int wait)
1055 struct btrfs_trans_handle *trans;
1056 struct btrfs_root *root;
1058 root = btrfs_sb(sb);
1062 filemap_flush(root->fs_info->btree_inode->i_mapping);
1065 mutex_lock(&root->fs_info->fs_mutex);
1066 trans = btrfs_start_transaction(root, 1);
1067 ret = btrfs_commit_transaction(trans, root);
1070 printk("btrfs sync_fs\n");
1071 mutex_unlock(&root->fs_info->fs_mutex);
1075 static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
1076 struct buffer_head *result, int create)
1081 u64 extent_start = 0;
1083 u64 objectid = inode->i_ino;
1085 struct btrfs_path *path;
1086 struct btrfs_root *root = BTRFS_I(inode)->root;
1087 struct btrfs_file_extent_item *item;
1088 struct btrfs_leaf *leaf;
1089 struct btrfs_disk_key *found_key;
1091 path = btrfs_alloc_path();
1093 btrfs_init_path(path);
1098 ret = btrfs_lookup_file_extent(NULL, root, path,
1100 iblock << inode->i_blkbits, 0);
1107 if (path->slots[0] == 0) {
1108 btrfs_release_path(root, path);
1114 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
1115 struct btrfs_file_extent_item);
1116 leaf = btrfs_buffer_leaf(path->nodes[0]);
1117 blocknr = btrfs_file_extent_disk_blocknr(item);
1118 blocknr += btrfs_file_extent_offset(item);
1120 /* are we inside the extent that was found? */
1121 found_key = &leaf->items[path->slots[0]].key;
1122 found_type = btrfs_disk_key_type(found_key);
1123 if (btrfs_disk_key_objectid(found_key) != objectid ||
1124 found_type != BTRFS_EXTENT_DATA_KEY) {
1129 found_type = btrfs_file_extent_type(item);
1130 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
1131 if (found_type == BTRFS_FILE_EXTENT_REG) {
1132 extent_start = extent_start >> inode->i_blkbits;
1133 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
1134 if (iblock >= extent_start && iblock < extent_end) {
1136 btrfs_map_bh_to_logical(root, result, blocknr +
1137 iblock - extent_start);
1140 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1144 size = btrfs_file_extent_inline_len(leaf->items +
1146 extent_end = (extent_start + size) >> inode->i_blkbits;
1147 extent_start >>= inode->i_blkbits;
1148 if (iblock < extent_start || iblock > extent_end) {
1151 ptr = btrfs_file_extent_inline_start(item);
1152 map = kmap(result->b_page);
1153 memcpy(map, ptr, size);
1154 memset(map + size, 0, PAGE_CACHE_SIZE - size);
1155 flush_dcache_page(result->b_page);
1156 kunmap(result->b_page);
1157 set_buffer_uptodate(result);
1158 SetPageChecked(result->b_page);
1159 btrfs_map_bh_to_logical(root, result, 0);
1162 btrfs_free_path(path);
1166 static int btrfs_get_block(struct inode *inode, sector_t iblock,
1167 struct buffer_head *result, int create)
1170 struct btrfs_root *root = BTRFS_I(inode)->root;
1171 mutex_lock(&root->fs_info->fs_mutex);
1172 err = btrfs_get_block_lock(inode, iblock, result, create);
1173 mutex_unlock(&root->fs_info->fs_mutex);
1177 static int btrfs_prepare_write(struct file *file, struct page *page,
1178 unsigned from, unsigned to)
1180 return nobh_prepare_write(page, from, to, btrfs_get_block);
1183 static void btrfs_write_super(struct super_block *sb)
1185 btrfs_sync_fs(sb, 1);
1188 static int btrfs_readpage(struct file *file, struct page *page)
1190 return mpage_readpage(page, btrfs_get_block);
1194 * While block_write_full_page is writing back the dirty buffers under
1195 * the page lock, whoever dirtied the buffers may decide to clean them
1196 * again at any time. We handle that by only looking at the buffer
1197 * state inside lock_buffer().
1199 * If block_write_full_page() is called for regular writeback
1200 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1201 * locked buffer. This only can happen if someone has written the buffer
1202 * directly, with submit_bh(). At the address_space level PageWriteback
1203 * prevents this contention from occurring.
1205 static int __btrfs_write_full_page(struct inode *inode, struct page *page,
1206 struct writeback_control *wbc)
1210 sector_t last_block;
1211 struct buffer_head *bh, *head;
1212 const unsigned blocksize = 1 << inode->i_blkbits;
1213 int nr_underway = 0;
1215 BUG_ON(!PageLocked(page));
1217 last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
1219 if (!page_has_buffers(page)) {
1220 create_empty_buffers(page, blocksize,
1221 (1 << BH_Dirty)|(1 << BH_Uptodate));
1225 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1226 * here, and the (potentially unmapped) buffers may become dirty at
1227 * any time. If a buffer becomes dirty here after we've inspected it
1228 * then we just miss that fact, and the page stays dirty.
1230 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1231 * handle that here by just cleaning them.
1234 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1235 head = page_buffers(page);
1239 * Get all the dirty buffers mapped to disk addresses and
1240 * handle any aliases from the underlying blockdev's mapping.
1243 if (block > last_block) {
1245 * mapped buffers outside i_size will occur, because
1246 * this page can be outside i_size when there is a
1247 * truncate in progress.
1250 * The buffer was zeroed by block_write_full_page()
1252 clear_buffer_dirty(bh);
1253 set_buffer_uptodate(bh);
1254 } else if (!buffer_mapped(bh) && buffer_dirty(bh)) {
1255 WARN_ON(bh->b_size != blocksize);
1256 err = btrfs_get_block(inode, block, bh, 0);
1258 printk("writepage going to recovery err %d\n", err);
1261 if (buffer_new(bh)) {
1262 /* blockdev mappings never come here */
1263 clear_buffer_new(bh);
1266 bh = bh->b_this_page;
1268 } while (bh != head);
1271 if (!buffer_mapped(bh))
1274 * If it's a fully non-blocking write attempt and we cannot
1275 * lock the buffer then redirty the page. Note that this can
1276 * potentially cause a busy-wait loop from pdflush and kswapd
1277 * activity, but those code paths have their own higher-level
1280 if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
1282 } else if (test_set_buffer_locked(bh)) {
1283 redirty_page_for_writepage(wbc, page);
1286 if (test_clear_buffer_dirty(bh) && bh->b_blocknr != 0) {
1287 mark_buffer_async_write(bh);
1291 } while ((bh = bh->b_this_page) != head);
1294 * The page and its buffers are protected by PageWriteback(), so we can
1295 * drop the bh refcounts early.
1297 BUG_ON(PageWriteback(page));
1298 set_page_writeback(page);
1301 struct buffer_head *next = bh->b_this_page;
1302 if (buffer_async_write(bh)) {
1303 submit_bh(WRITE, bh);
1307 } while (bh != head);
1312 if (nr_underway == 0) {
1314 * The page was marked dirty, but the buffers were
1315 * clean. Someone wrote them back by hand with
1316 * ll_rw_block/submit_bh. A rare case.
1320 if (!buffer_uptodate(bh)) {
1324 bh = bh->b_this_page;
1325 } while (bh != head);
1327 SetPageUptodate(page);
1328 end_page_writeback(page);
1334 * ENOSPC, or some other error. We may already have added some
1335 * blocks to the file, so we need to write these out to avoid
1336 * exposing stale data.
1337 * The page is currently locked and not marked for writeback
1340 /* Recovery: lock and submit the mapped buffers */
1342 if (buffer_mapped(bh) && buffer_dirty(bh)) {
1344 mark_buffer_async_write(bh);
1347 * The buffer may have been set dirty during
1348 * attachment to a dirty page.
1350 clear_buffer_dirty(bh);
1352 } while ((bh = bh->b_this_page) != head);
1354 BUG_ON(PageWriteback(page));
1355 set_page_writeback(page);
1357 struct buffer_head *next = bh->b_this_page;
1358 if (buffer_async_write(bh)) {
1359 clear_buffer_dirty(bh);
1360 submit_bh(WRITE, bh);
1364 } while (bh != head);
1370 * The generic ->writepage function for buffer-backed address_spaces
1372 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1374 struct inode * const inode = page->mapping->host;
1375 loff_t i_size = i_size_read(inode);
1376 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
1380 /* Is the page fully inside i_size? */
1381 if (page->index < end_index)
1382 return __btrfs_write_full_page(inode, page, wbc);
1384 /* Is the page fully outside i_size? (truncate in progress) */
1385 offset = i_size & (PAGE_CACHE_SIZE-1);
1386 if (page->index >= end_index+1 || !offset) {
1388 * The page may have dirty, unmapped buffers. For example,
1389 * they may have been added in ext3_writepage(). Make them
1390 * freeable here, so the page does not leak.
1392 block_invalidatepage(page, 0);
1394 return 0; /* don't care */
1398 * The page straddles i_size. It must be zeroed out on each and every
1399 * writepage invokation because it may be mmapped. "A file is mapped
1400 * in multiples of the page size. For a file that is not a multiple of
1401 * the page size, the remaining memory is zeroed when mapped, and
1402 * writes to that region are not written out to the file."
1404 kaddr = kmap_atomic(page, KM_USER0);
1405 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1406 flush_dcache_page(page);
1407 kunmap_atomic(kaddr, KM_USER0);
1408 return __btrfs_write_full_page(inode, page, wbc);
1411 static void btrfs_truncate(struct inode *inode)
1413 struct btrfs_root *root = BTRFS_I(inode)->root;
1415 struct btrfs_trans_handle *trans;
1417 if (!S_ISREG(inode->i_mode))
1419 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1422 nobh_truncate_page(inode->i_mapping, inode->i_size);
1424 /* FIXME, add redo link to tree so we don't leak on crash */
1425 mutex_lock(&root->fs_info->fs_mutex);
1426 trans = btrfs_start_transaction(root, 1);
1427 btrfs_set_trans_block_group(trans, inode);
1428 ret = btrfs_truncate_in_trans(trans, root, inode);
1430 btrfs_update_inode(trans, root, inode);
1431 ret = btrfs_end_transaction(trans, root);
1433 mutex_unlock(&root->fs_info->fs_mutex);
1434 btrfs_btree_balance_dirty(root);
1438 * Make sure any changes to nobh_commit_write() are reflected in
1439 * nobh_truncate_page(), since it doesn't call commit_write().
1441 static int btrfs_commit_write(struct file *file, struct page *page,
1442 unsigned from, unsigned to)
1444 struct inode *inode = page->mapping->host;
1445 struct buffer_head *bh;
1446 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1448 SetPageUptodate(page);
1449 bh = page_buffers(page);
1450 if (buffer_mapped(bh) && bh->b_blocknr != 0) {
1451 set_page_dirty(page);
1453 if (pos > inode->i_size) {
1454 i_size_write(inode, pos);
1455 mark_inode_dirty(inode);
1460 static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
1461 struct page **prepared_pages,
1462 const char __user * buf)
1464 long page_fault = 0;
1466 int offset = pos & (PAGE_CACHE_SIZE - 1);
1468 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
1469 size_t count = min_t(size_t,
1470 PAGE_CACHE_SIZE - offset, write_bytes);
1471 struct page *page = prepared_pages[i];
1472 fault_in_pages_readable(buf, count);
1474 /* Copy data from userspace to the current page */
1476 page_fault = __copy_from_user(page_address(page) + offset,
1478 /* Flush processor's dcache for this page */
1479 flush_dcache_page(page);
1482 write_bytes -= count;
1487 return page_fault ? -EFAULT : 0;
1490 static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1493 for (i = 0; i < num_pages; i++) {
1496 unlock_page(pages[i]);
1497 mark_page_accessed(pages[i]);
1498 page_cache_release(pages[i]);
1501 static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1502 struct btrfs_root *root,
1504 struct page **pages,
1514 struct inode *inode = file->f_path.dentry->d_inode;
1515 struct buffer_head *bh;
1516 struct btrfs_file_extent_item *ei;
1518 for (i = 0; i < num_pages; i++) {
1519 offset = pos & (PAGE_CACHE_SIZE -1);
1520 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1521 /* FIXME, one block at a time */
1523 mutex_lock(&root->fs_info->fs_mutex);
1524 trans = btrfs_start_transaction(root, 1);
1525 btrfs_set_trans_block_group(trans, inode);
1527 bh = page_buffers(pages[i]);
1528 if (buffer_mapped(bh) && bh->b_blocknr == 0) {
1529 struct btrfs_key key;
1530 struct btrfs_path *path;
1534 path = btrfs_alloc_path();
1536 key.objectid = inode->i_ino;
1537 key.offset = pages[i]->index << PAGE_CACHE_SHIFT;
1539 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
1540 BUG_ON(write_bytes >= PAGE_CACHE_SIZE);
1542 btrfs_file_extent_calc_inline_size(write_bytes);
1543 ret = btrfs_insert_empty_item(trans, root, path, &key,
1546 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1547 path->slots[0], struct btrfs_file_extent_item);
1548 btrfs_set_file_extent_generation(ei, trans->transid);
1549 btrfs_set_file_extent_type(ei,
1550 BTRFS_FILE_EXTENT_INLINE);
1551 ptr = btrfs_file_extent_inline_start(ei);
1552 btrfs_memcpy(root, path->nodes[0]->b_data,
1553 ptr, bh->b_data, offset + write_bytes);
1554 mark_buffer_dirty(path->nodes[0]);
1555 btrfs_free_path(path);
1557 btrfs_csum_file_block(trans, root, inode->i_ino,
1558 pages[i]->index << PAGE_CACHE_SHIFT,
1559 kmap(pages[i]), PAGE_CACHE_SIZE);
1562 SetPageChecked(pages[i]);
1563 // btrfs_update_inode_block_group(trans, inode);
1564 ret = btrfs_end_transaction(trans, root);
1566 mutex_unlock(&root->fs_info->fs_mutex);
1568 ret = btrfs_commit_write(file, pages[i], offset,
1569 offset + this_write);
1575 WARN_ON(this_write > write_bytes);
1576 write_bytes -= this_write;
1582 static int drop_extents(struct btrfs_trans_handle *trans,
1583 struct btrfs_root *root,
1584 struct inode *inode,
1585 u64 start, u64 end, u64 *hint_block)
1588 struct btrfs_key key;
1589 struct btrfs_leaf *leaf;
1591 struct btrfs_file_extent_item *extent;
1594 struct btrfs_file_extent_item old;
1595 struct btrfs_path *path;
1596 u64 search_start = start;
1602 path = btrfs_alloc_path();
1606 btrfs_release_path(root, path);
1607 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1612 if (path->slots[0] == 0) {
1623 leaf = btrfs_buffer_leaf(path->nodes[0]);
1624 slot = path->slots[0];
1625 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
1626 if (key.offset >= end || key.objectid != inode->i_ino) {
1630 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) {
1634 extent = btrfs_item_ptr(leaf, slot,
1635 struct btrfs_file_extent_item);
1636 found_type = btrfs_file_extent_type(extent);
1637 if (found_type == BTRFS_FILE_EXTENT_REG) {
1638 extent_end = key.offset +
1639 (btrfs_file_extent_num_blocks(extent) <<
1642 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1644 extent_end = key.offset +
1645 btrfs_file_extent_inline_len(leaf->items + slot);
1648 if (!found_extent && !found_inline) {
1653 if (search_start >= extent_end) {
1658 search_start = extent_end;
1660 if (end < extent_end && end >= key.offset) {
1662 memcpy(&old, extent, sizeof(old));
1663 ret = btrfs_inc_extent_ref(trans, root,
1664 btrfs_file_extent_disk_blocknr(&old),
1665 btrfs_file_extent_disk_num_blocks(&old));
1668 WARN_ON(found_inline);
1672 if (start > key.offset) {
1675 /* truncate existing extent */
1677 WARN_ON(start & (root->blocksize - 1));
1679 new_num = (start - key.offset) >>
1681 old_num = btrfs_file_extent_num_blocks(extent);
1683 btrfs_file_extent_disk_blocknr(extent);
1684 inode->i_blocks -= (old_num - new_num) << 3;
1685 btrfs_set_file_extent_num_blocks(extent,
1687 mark_buffer_dirty(path->nodes[0]);
1693 u64 disk_blocknr = 0;
1694 u64 disk_num_blocks = 0;
1695 u64 extent_num_blocks = 0;
1698 btrfs_file_extent_disk_blocknr(extent);
1700 btrfs_file_extent_disk_num_blocks(extent);
1702 btrfs_file_extent_num_blocks(extent);
1704 btrfs_file_extent_disk_blocknr(extent);
1706 ret = btrfs_del_item(trans, root, path);
1708 btrfs_release_path(root, path);
1711 inode->i_blocks -= extent_num_blocks << 3;
1712 ret = btrfs_free_extent(trans, root,
1714 disk_num_blocks, 0);
1718 if (!bookend && search_start >= end) {
1725 if (bookend && found_extent) {
1726 /* create bookend */
1727 struct btrfs_key ins;
1728 ins.objectid = inode->i_ino;
1731 btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
1733 btrfs_release_path(root, path);
1734 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1737 extent = btrfs_item_ptr(
1738 btrfs_buffer_leaf(path->nodes[0]),
1740 struct btrfs_file_extent_item);
1741 btrfs_set_file_extent_disk_blocknr(extent,
1742 btrfs_file_extent_disk_blocknr(&old));
1743 btrfs_set_file_extent_disk_num_blocks(extent,
1744 btrfs_file_extent_disk_num_blocks(&old));
1746 btrfs_set_file_extent_offset(extent,
1747 btrfs_file_extent_offset(&old) +
1748 ((end - key.offset) >> inode->i_blkbits));
1749 WARN_ON(btrfs_file_extent_num_blocks(&old) <
1750 (end - key.offset) >> inode->i_blkbits);
1751 btrfs_set_file_extent_num_blocks(extent,
1752 btrfs_file_extent_num_blocks(&old) -
1753 ((end - key.offset) >> inode->i_blkbits));
1755 btrfs_set_file_extent_type(extent,
1756 BTRFS_FILE_EXTENT_REG);
1757 btrfs_set_file_extent_generation(extent,
1758 btrfs_file_extent_generation(&old));
1759 btrfs_mark_buffer_dirty(path->nodes[0]);
1761 btrfs_file_extent_num_blocks(extent) << 3;
1767 btrfs_free_path(path);
1771 static int prepare_pages(struct btrfs_root *root,
1773 struct page **pages,
1776 unsigned long first_index,
1777 unsigned long last_index,
1779 u64 alloc_extent_start)
1782 unsigned long index = pos >> PAGE_CACHE_SHIFT;
1783 struct inode *inode = file->f_path.dentry->d_inode;
1787 struct buffer_head *bh;
1788 struct buffer_head *head;
1789 loff_t isize = i_size_read(inode);
1791 memset(pages, 0, num_pages * sizeof(struct page *));
1793 for (i = 0; i < num_pages; i++) {
1794 pages[i] = grab_cache_page(inode->i_mapping, index + i);
1797 goto failed_release;
1799 cancel_dirty_page(pages[i], PAGE_CACHE_SIZE);
1800 wait_on_page_writeback(pages[i]);
1801 offset = pos & (PAGE_CACHE_SIZE -1);
1802 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1803 if (!page_has_buffers(pages[i])) {
1804 create_empty_buffers(pages[i],
1805 root->fs_info->sb->s_blocksize,
1806 (1 << BH_Uptodate));
1808 head = page_buffers(pages[i]);
1811 err = btrfs_map_bh_to_logical(root, bh,
1812 alloc_extent_start);
1815 goto failed_truncate;
1816 bh = bh->b_this_page;
1817 if (alloc_extent_start)
1818 alloc_extent_start++;
1819 } while (bh != head);
1821 WARN_ON(this_write > write_bytes);
1822 write_bytes -= this_write;
1827 btrfs_drop_pages(pages, num_pages);
1831 btrfs_drop_pages(pages, num_pages);
1833 vmtruncate(inode, isize);
1837 static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1838 size_t count, loff_t *ppos)
1841 size_t num_written = 0;
1844 struct inode *inode = file->f_path.dentry->d_inode;
1845 struct btrfs_root *root = BTRFS_I(inode)->root;
1846 struct page *pages[8];
1847 struct page *pinned[2];
1848 unsigned long first_index;
1849 unsigned long last_index;
1852 u64 alloc_extent_start;
1854 struct btrfs_trans_handle *trans;
1855 struct btrfs_key ins;
1858 if (file->f_flags & O_DIRECT)
1861 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1862 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1863 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1868 err = remove_suid(file->f_path.dentry);
1871 file_update_time(file);
1873 start_pos = pos & ~((u64)PAGE_CACHE_SIZE - 1);
1874 num_blocks = (count + pos - start_pos + root->blocksize - 1) >>
1877 mutex_lock(&inode->i_mutex);
1878 first_index = pos >> PAGE_CACHE_SHIFT;
1879 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
1881 if ((first_index << PAGE_CACHE_SHIFT) < inode->i_size &&
1882 (pos & (PAGE_CACHE_SIZE - 1))) {
1883 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
1884 if (!PageUptodate(pinned[0])) {
1885 ret = mpage_readpage(pinned[0], btrfs_get_block);
1887 wait_on_page_locked(pinned[0]);
1889 unlock_page(pinned[0]);
1892 if (first_index != last_index &&
1893 (last_index << PAGE_CACHE_SHIFT) < inode->i_size &&
1894 pos + count < inode->i_size &&
1895 (count & (PAGE_CACHE_SIZE - 1))) {
1896 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
1897 if (!PageUptodate(pinned[1])) {
1898 ret = mpage_readpage(pinned[1], btrfs_get_block);
1900 wait_on_page_locked(pinned[1]);
1902 unlock_page(pinned[1]);
1906 mutex_lock(&root->fs_info->fs_mutex);
1907 trans = btrfs_start_transaction(root, 1);
1910 mutex_unlock(&root->fs_info->fs_mutex);
1913 btrfs_set_trans_block_group(trans, inode);
1914 /* FIXME blocksize != 4096 */
1915 inode->i_blocks += num_blocks << 3;
1917 if (start_pos < inode->i_size) {
1918 /* FIXME blocksize != pagesize */
1919 ret = drop_extents(trans, root, inode,
1921 (pos + count + root->blocksize -1) &
1922 ~((u64)root->blocksize - 1), &hint_block);
1925 if (inode->i_size >= PAGE_CACHE_SIZE || pos + count < inode->i_size ||
1926 pos + count - start_pos > BTRFS_MAX_INLINE_DATA_SIZE(root)) {
1927 ret = btrfs_alloc_extent(trans, root, inode->i_ino,
1928 num_blocks, hint_block, (u64)-1,
1931 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
1932 start_pos, ins.objectid, ins.offset);
1939 alloc_extent_start = ins.objectid;
1940 // btrfs_update_inode_block_group(trans, inode);
1941 ret = btrfs_end_transaction(trans, root);
1942 mutex_unlock(&root->fs_info->fs_mutex);
1945 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1946 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1947 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
1950 memset(pages, 0, sizeof(pages));
1951 ret = prepare_pages(root, file, pages, num_pages,
1952 pos, first_index, last_index,
1953 write_bytes, alloc_extent_start);
1956 /* FIXME blocks != pagesize */
1957 if (alloc_extent_start)
1958 alloc_extent_start += num_pages;
1959 ret = btrfs_copy_from_user(pos, num_pages,
1960 write_bytes, pages, buf);
1963 ret = dirty_and_release_pages(NULL, root, file, pages,
1964 num_pages, pos, write_bytes);
1966 btrfs_drop_pages(pages, num_pages);
1969 count -= write_bytes;
1971 num_written += write_bytes;
1973 balance_dirty_pages_ratelimited(inode->i_mapping);
1974 btrfs_btree_balance_dirty(root);
1978 mutex_unlock(&inode->i_mutex);
1981 page_cache_release(pinned[0]);
1983 page_cache_release(pinned[1]);
1985 current->backing_dev_info = NULL;
1986 mark_inode_dirty(inode);
1987 return num_written ? num_written : err;
1990 static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
1991 unsigned long offset, unsigned long size)
1994 unsigned long left, count = desc->count;
1995 struct inode *inode = page->mapping->host;
2000 if (!PageChecked(page)) {
2001 /* FIXME, do it per block */
2002 struct btrfs_root *root = BTRFS_I(inode)->root;
2004 int ret = btrfs_csum_verify_file_block(root,
2005 page->mapping->host->i_ino,
2006 page->index << PAGE_CACHE_SHIFT,
2007 kmap(page), PAGE_CACHE_SIZE);
2009 printk("failed to verify ino %lu page %lu\n",
2010 page->mapping->host->i_ino,
2012 memset(page_address(page), 0, PAGE_CACHE_SIZE);
2014 SetPageChecked(page);
2018 * Faults on the destination of a read are common, so do it before
2021 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
2022 kaddr = kmap_atomic(page, KM_USER0);
2023 left = __copy_to_user_inatomic(desc->arg.buf,
2024 kaddr + offset, size);
2025 kunmap_atomic(kaddr, KM_USER0);
2030 /* Do it the slow way */
2032 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
2037 desc->error = -EFAULT;
2040 desc->count = count - size;
2041 desc->written += size;
2042 desc->arg.buf += size;
2047 * btrfs_file_aio_read - filesystem read routine
2048 * @iocb: kernel I/O control block
2049 * @iov: io vector request
2050 * @nr_segs: number of segments in the iovec
2051 * @pos: current file position
2053 static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
2054 unsigned long nr_segs, loff_t pos)
2056 struct file *filp = iocb->ki_filp;
2060 loff_t *ppos = &iocb->ki_pos;
2063 for (seg = 0; seg < nr_segs; seg++) {
2064 const struct iovec *iv = &iov[seg];
2067 * If any segment has a negative length, or the cumulative
2068 * length ever wraps negative then return -EINVAL.
2070 count += iv->iov_len;
2071 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
2073 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
2078 count -= iv->iov_len; /* This segment is no good */
2083 for (seg = 0; seg < nr_segs; seg++) {
2084 read_descriptor_t desc;
2087 desc.arg.buf = iov[seg].iov_base;
2088 desc.count = iov[seg].iov_len;
2089 if (desc.count == 0)
2092 do_generic_file_read(filp, ppos, &desc,
2094 retval += desc.written;
2096 retval = retval ?: desc.error;
2104 static int create_subvol(struct btrfs_root *root, char *name, int namelen)
2106 struct btrfs_trans_handle *trans;
2107 struct btrfs_key key;
2108 struct btrfs_root_item root_item;
2109 struct btrfs_inode_item *inode_item;
2110 struct buffer_head *subvol;
2111 struct btrfs_leaf *leaf;
2112 struct btrfs_root *new_root;
2113 struct inode *inode;
2117 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2119 mutex_lock(&root->fs_info->fs_mutex);
2120 trans = btrfs_start_transaction(root, 1);
2123 subvol = btrfs_alloc_free_block(trans, root, 0);
2126 leaf = btrfs_buffer_leaf(subvol);
2127 btrfs_set_header_nritems(&leaf->header, 0);
2128 btrfs_set_header_level(&leaf->header, 0);
2129 btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
2130 btrfs_set_header_generation(&leaf->header, trans->transid);
2131 btrfs_set_header_owner(&leaf->header, root->root_key.objectid);
2132 memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
2133 sizeof(leaf->header.fsid));
2134 mark_buffer_dirty(subvol);
2136 inode_item = &root_item.inode;
2137 memset(inode_item, 0, sizeof(*inode_item));
2138 btrfs_set_inode_generation(inode_item, 1);
2139 btrfs_set_inode_size(inode_item, 3);
2140 btrfs_set_inode_nlink(inode_item, 1);
2141 btrfs_set_inode_nblocks(inode_item, 1);
2142 btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
2144 btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
2145 btrfs_set_root_refs(&root_item, 1);
2149 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2153 btrfs_set_root_dirid(&root_item, new_dirid);
2155 key.objectid = objectid;
2158 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2159 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2164 * insert the directory item
2166 key.offset = (u64)-1;
2167 dir = root->fs_info->sb->s_root->d_inode;
2168 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2169 name, namelen, dir->i_ino, &key, 0);
2172 ret = btrfs_commit_transaction(trans, root);
2175 new_root = btrfs_read_fs_root(root->fs_info, &key);
2178 trans = btrfs_start_transaction(new_root, 1);
2181 inode = btrfs_new_inode(trans, new_root, new_dirid,
2182 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2183 inode->i_op = &btrfs_dir_inode_operations;
2184 inode->i_fop = &btrfs_dir_file_operations;
2186 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
2191 ret = btrfs_update_inode(trans, new_root, inode);
2194 ret = btrfs_commit_transaction(trans, new_root);
2199 mutex_unlock(&root->fs_info->fs_mutex);
2200 btrfs_btree_balance_dirty(root);
2204 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2206 struct btrfs_trans_handle *trans;
2207 struct btrfs_key key;
2208 struct btrfs_root_item new_root_item;
2212 if (!root->ref_cows)
2215 mutex_lock(&root->fs_info->fs_mutex);
2216 trans = btrfs_start_transaction(root, 1);
2219 ret = btrfs_update_inode(trans, root, root->inode);
2222 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2226 memcpy(&new_root_item, &root->root_item,
2227 sizeof(new_root_item));
2229 key.objectid = objectid;
2232 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2233 btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
2235 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2240 * insert the directory item
2242 key.offset = (u64)-1;
2243 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2245 root->fs_info->sb->s_root->d_inode->i_ino,
2250 ret = btrfs_inc_root_ref(trans, root);
2253 ret = btrfs_commit_transaction(trans, root);
2255 mutex_unlock(&root->fs_info->fs_mutex);
2256 btrfs_btree_balance_dirty(root);
2260 static int add_disk(struct btrfs_root *root, char *name, int namelen)
2262 struct block_device *bdev;
2263 struct btrfs_path *path;
2264 struct super_block *sb = root->fs_info->sb;
2265 struct btrfs_root *dev_root = root->fs_info->dev_root;
2266 struct btrfs_trans_handle *trans;
2267 struct btrfs_device_item *dev_item;
2268 struct btrfs_key key;
2275 printk("adding disk %s\n", name);
2276 path = btrfs_alloc_path();
2279 num_blocks = btrfs_super_total_blocks(root->fs_info->disk_super);
2280 bdev = open_bdev_excl(name, O_RDWR, sb);
2282 ret = PTR_ERR(bdev);
2283 printk("open bdev excl failed ret %d\n", ret);
2286 set_blocksize(bdev, sb->s_blocksize);
2287 new_blocks = bdev->bd_inode->i_size >> sb->s_blocksize_bits;
2288 key.objectid = num_blocks;
2289 key.offset = new_blocks;
2291 btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY);
2293 mutex_lock(&dev_root->fs_info->fs_mutex);
2294 trans = btrfs_start_transaction(dev_root, 1);
2295 item_size = sizeof(*dev_item) + namelen;
2296 printk("insert empty on %Lu %Lu %u size %d\n", num_blocks, new_blocks, key.flags, item_size);
2297 ret = btrfs_insert_empty_item(trans, dev_root, path, &key, item_size);
2299 printk("insert failed %d\n", ret);
2300 close_bdev_excl(bdev);
2305 dev_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
2306 path->slots[0], struct btrfs_device_item);
2307 btrfs_set_device_pathlen(dev_item, namelen);
2308 memcpy(dev_item + 1, name, namelen);
2310 device_id = btrfs_super_last_device_id(root->fs_info->disk_super) + 1;
2311 btrfs_set_super_last_device_id(root->fs_info->disk_super, device_id);
2312 btrfs_set_device_id(dev_item, device_id);
2313 mark_buffer_dirty(path->nodes[0]);
2315 ret = btrfs_insert_dev_radix(root, bdev, device_id, num_blocks,
2319 btrfs_set_super_total_blocks(root->fs_info->disk_super,
2320 num_blocks + new_blocks);
2321 i_size_write(root->fs_info->btree_inode,
2322 (num_blocks + new_blocks) <<
2323 root->fs_info->btree_inode->i_blkbits);
2327 ret = btrfs_commit_transaction(trans, dev_root);
2329 mutex_unlock(&root->fs_info->fs_mutex);
2331 btrfs_free_path(path);
2332 btrfs_btree_balance_dirty(root);
2337 static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
2338 cmd, unsigned long arg)
2340 struct btrfs_root *root = BTRFS_I(inode)->root;
2341 struct btrfs_ioctl_vol_args vol_args;
2343 struct btrfs_dir_item *di;
2345 struct btrfs_path *path;
2349 case BTRFS_IOC_SNAP_CREATE:
2350 if (copy_from_user(&vol_args,
2351 (struct btrfs_ioctl_vol_args __user *)arg,
2354 namelen = strlen(vol_args.name);
2355 if (namelen > BTRFS_VOL_NAME_MAX)
2357 path = btrfs_alloc_path();
2360 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2361 mutex_lock(&root->fs_info->fs_mutex);
2362 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2364 vol_args.name, namelen, 0);
2365 mutex_unlock(&root->fs_info->fs_mutex);
2366 btrfs_free_path(path);
2367 if (di && !IS_ERR(di))
2370 if (root == root->fs_info->tree_root)
2371 ret = create_subvol(root, vol_args.name, namelen);
2373 ret = create_snapshot(root, vol_args.name, namelen);
2376 case BTRFS_IOC_ADD_DISK:
2377 if (copy_from_user(&vol_args,
2378 (struct btrfs_ioctl_vol_args __user *)arg,
2381 namelen = strlen(vol_args.name);
2382 if (namelen > BTRFS_VOL_NAME_MAX)
2384 vol_args.name[namelen] = '\0';
2385 ret = add_disk(root, vol_args.name, namelen);
2393 static struct kmem_cache *btrfs_inode_cachep;
2394 struct kmem_cache *btrfs_trans_handle_cachep;
2395 struct kmem_cache *btrfs_transaction_cachep;
2396 struct kmem_cache *btrfs_bit_radix_cachep;
2397 struct kmem_cache *btrfs_path_cachep;
2400 * Called inside transaction, so use GFP_NOFS
2402 static struct inode *btrfs_alloc_inode(struct super_block *sb)
2404 struct btrfs_inode *ei;
2406 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2409 return &ei->vfs_inode;
2412 static void btrfs_destroy_inode(struct inode *inode)
2414 WARN_ON(!list_empty(&inode->i_dentry));
2415 WARN_ON(inode->i_data.nrpages);
2417 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2420 static void init_once(void * foo, struct kmem_cache * cachep,
2421 unsigned long flags)
2423 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2425 if ((flags & (SLAB_CTOR_CONSTRUCTOR)) ==
2426 SLAB_CTOR_CONSTRUCTOR) {
2427 inode_init_once(&ei->vfs_inode);
2431 static int init_inodecache(void)
2433 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
2434 sizeof(struct btrfs_inode),
2435 0, (SLAB_RECLAIM_ACCOUNT|
2438 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
2439 sizeof(struct btrfs_trans_handle),
2440 0, (SLAB_RECLAIM_ACCOUNT|
2443 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
2444 sizeof(struct btrfs_transaction),
2445 0, (SLAB_RECLAIM_ACCOUNT|
2448 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
2449 sizeof(struct btrfs_transaction),
2450 0, (SLAB_RECLAIM_ACCOUNT|
2453 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
2455 0, (SLAB_RECLAIM_ACCOUNT|
2457 SLAB_DESTROY_BY_RCU),
2459 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
2460 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
2465 static void destroy_inodecache(void)
2467 kmem_cache_destroy(btrfs_inode_cachep);
2468 kmem_cache_destroy(btrfs_trans_handle_cachep);
2469 kmem_cache_destroy(btrfs_transaction_cachep);
2470 kmem_cache_destroy(btrfs_bit_radix_cachep);
2471 kmem_cache_destroy(btrfs_path_cachep);
2474 static int btrfs_get_sb(struct file_system_type *fs_type,
2475 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2477 return get_sb_bdev(fs_type, flags, dev_name, data,
2478 btrfs_fill_super, mnt);
2481 static int btrfs_getattr(struct vfsmount *mnt,
2482 struct dentry *dentry, struct kstat *stat)
2484 struct inode *inode = dentry->d_inode;
2485 generic_fillattr(inode, stat);
2486 stat->blksize = 256 * 1024;
2490 static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
2492 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
2493 struct btrfs_super_block *disk_super = root->fs_info->disk_super;
2495 buf->f_namelen = BTRFS_NAME_LEN;
2496 buf->f_blocks = btrfs_super_total_blocks(disk_super);
2497 buf->f_bfree = buf->f_blocks - btrfs_super_blocks_used(disk_super);
2498 buf->f_bavail = buf->f_bfree;
2499 buf->f_bsize = dentry->d_sb->s_blocksize;
2500 buf->f_type = BTRFS_SUPER_MAGIC;
2504 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2505 struct inode * new_dir,struct dentry *new_dentry)
2507 struct btrfs_trans_handle *trans;
2508 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2509 struct inode *new_inode = new_dentry->d_inode;
2510 struct inode *old_inode = old_dentry->d_inode;
2511 struct timespec ctime = CURRENT_TIME;
2512 struct btrfs_path *path;
2513 struct btrfs_dir_item *di;
2516 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2517 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2520 mutex_lock(&root->fs_info->fs_mutex);
2521 trans = btrfs_start_transaction(root, 1);
2522 btrfs_set_trans_block_group(trans, new_dir);
2523 path = btrfs_alloc_path();
2529 old_dentry->d_inode->i_nlink++;
2530 old_dir->i_ctime = old_dir->i_mtime = ctime;
2531 new_dir->i_ctime = new_dir->i_mtime = ctime;
2532 old_inode->i_ctime = ctime;
2533 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
2534 struct btrfs_key *location = &BTRFS_I(new_dir)->location;
2536 di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
2546 old_parent_oid = btrfs_disk_key_objectid(&di->location);
2547 ret = btrfs_del_item(trans, root, path);
2552 btrfs_release_path(root, path);
2554 di = btrfs_lookup_dir_index_item(trans, root, path,
2566 ret = btrfs_del_item(trans, root, path);
2571 btrfs_release_path(root, path);
2573 ret = btrfs_insert_dir_item(trans, root, "..", 2,
2574 old_inode->i_ino, location, 0);
2580 ret = btrfs_add_link(trans, new_dentry, old_inode);
2581 if (ret == -EEXIST && new_inode)
2586 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2591 new_inode->i_ctime = CURRENT_TIME;
2592 di = btrfs_lookup_dir_index_item(trans, root, path,
2595 new_dentry->d_name.name,
2596 new_dentry->d_name.len, -1);
2597 if (di && !IS_ERR(di)) {
2598 btrfs_del_item(trans, root, path);
2599 btrfs_release_path(root, path);
2601 if (S_ISDIR(new_inode->i_mode))
2602 clear_nlink(new_inode);
2604 drop_nlink(new_inode);
2605 btrfs_update_inode(trans, root, new_inode);
2608 btrfs_free_path(path);
2609 btrfs_end_transaction(trans, root);
2610 mutex_unlock(&root->fs_info->fs_mutex);
2614 static struct file_system_type btrfs_fs_type = {
2615 .owner = THIS_MODULE,
2617 .get_sb = btrfs_get_sb,
2618 .kill_sb = kill_block_super,
2619 .fs_flags = FS_REQUIRES_DEV,
2622 static struct super_operations btrfs_super_ops = {
2623 .delete_inode = btrfs_delete_inode,
2624 .put_super = btrfs_put_super,
2625 .read_inode = btrfs_read_locked_inode,
2626 .write_super = btrfs_write_super,
2627 .sync_fs = btrfs_sync_fs,
2628 .write_inode = btrfs_write_inode,
2629 .dirty_inode = btrfs_dirty_inode,
2630 .alloc_inode = btrfs_alloc_inode,
2631 .destroy_inode = btrfs_destroy_inode,
2632 .statfs = btrfs_statfs,
2635 static struct inode_operations btrfs_dir_inode_operations = {
2636 .lookup = btrfs_lookup,
2637 .create = btrfs_create,
2638 .unlink = btrfs_unlink,
2639 .mkdir = btrfs_mkdir,
2640 .rmdir = btrfs_rmdir,
2641 .rename = btrfs_rename,
2644 static struct inode_operations btrfs_dir_ro_inode_operations = {
2645 .lookup = btrfs_lookup,
2648 static struct file_operations btrfs_dir_file_operations = {
2649 .llseek = generic_file_llseek,
2650 .read = generic_read_dir,
2651 .readdir = btrfs_readdir,
2652 .ioctl = btrfs_ioctl,
2655 static struct address_space_operations btrfs_aops = {
2656 .readpage = btrfs_readpage,
2657 .writepage = btrfs_writepage,
2658 .sync_page = block_sync_page,
2659 .prepare_write = btrfs_prepare_write,
2660 .commit_write = btrfs_commit_write,
2663 static struct inode_operations btrfs_file_inode_operations = {
2664 .truncate = btrfs_truncate,
2665 .getattr = btrfs_getattr,
2668 static struct file_operations btrfs_file_operations = {
2669 .llseek = generic_file_llseek,
2670 .read = do_sync_read,
2671 .aio_read = btrfs_file_aio_read,
2672 .write = btrfs_file_write,
2673 .mmap = generic_file_mmap,
2674 .open = generic_file_open,
2675 .ioctl = btrfs_ioctl,
2676 .fsync = btrfs_sync_file,
2679 static int __init init_btrfs_fs(void)
2682 printk("btrfs loaded!\n");
2683 err = init_inodecache();
2686 return register_filesystem(&btrfs_fs_type);
2687 destroy_inodecache();
2691 static void __exit exit_btrfs_fs(void)
2693 destroy_inodecache();
2694 unregister_filesystem(&btrfs_fs_type);
2695 printk("btrfs unloaded\n");
2698 module_init(init_btrfs_fs)
2699 module_exit(exit_btrfs_fs)
2701 MODULE_LICENSE("GPL");