Btrfs: rename
[deliverable/linux.git] / fs / btrfs / super.c
index 1b286bb26f126cb60f5e196ecc63b5a193831e3b..f49cad603ee8c807b2c37b41b4fb001c45e69acb 100644 (file)
 #include <linux/mpage.h>
 #include <linux/swap.h>
 #include <linux/writeback.h>
+#include <linux/statfs.h>
 #include "ctree.h"
 #include "disk-io.h"
 #include "transaction.h"
 #include "btrfs_inode.h"
 #include "ioctl.h"
 
-void btrfs_fsinfo_release(struct kobject *obj)
-{
-       struct btrfs_fs_info *fsinfo = container_of(obj,
-                                           struct btrfs_fs_info, kobj);
-       kfree(fsinfo);
-}
-
-struct kobj_type btrfs_fsinfo_ktype = {
-       .release = btrfs_fsinfo_release,
-};
-
 struct btrfs_iget_args {
        u64 ino;
        struct btrfs_root *root;
 };
 
-decl_subsys(btrfs, &btrfs_fsinfo_ktype, NULL);
-
 #define BTRFS_SUPER_MAGIC 0x9123682E
 
 static struct inode_operations btrfs_dir_inode_operations;
@@ -51,6 +39,8 @@ static void btrfs_read_locked_inode(struct inode *inode)
        struct btrfs_inode_item *inode_item;
        struct btrfs_root *root = BTRFS_I(inode)->root;
        struct btrfs_key location;
+       struct btrfs_block_group_cache *alloc_group;
+       u64 alloc_group_block;
        int ret;
 
        path = btrfs_alloc_path();
@@ -81,6 +71,12 @@ static void btrfs_read_locked_inode(struct inode *inode)
        inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
        inode->i_blocks = btrfs_inode_nblocks(inode_item);
        inode->i_generation = btrfs_inode_generation(inode_item);
+       alloc_group_block = btrfs_inode_block_group(inode_item);
+       ret = radix_tree_gang_lookup(&root->fs_info->block_group_radix,
+                                    (void **)&alloc_group,
+                                    alloc_group_block, 1);
+       BUG_ON(!ret);
+       BTRFS_I(inode)->block_group = alloc_group;
 
        btrfs_free_path(path);
        inode_item = NULL;
@@ -119,6 +115,59 @@ make_bad:
        make_bad_inode(inode);
 }
 
+static void fill_inode_item(struct btrfs_inode_item *item,
+                           struct inode *inode)
+{
+       btrfs_set_inode_uid(item, inode->i_uid);
+       btrfs_set_inode_gid(item, inode->i_gid);
+       btrfs_set_inode_size(item, inode->i_size);
+       btrfs_set_inode_mode(item, inode->i_mode);
+       btrfs_set_inode_nlink(item, inode->i_nlink);
+       btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
+       btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
+       btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
+       btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
+       btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
+       btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
+       btrfs_set_inode_nblocks(item, inode->i_blocks);
+       btrfs_set_inode_generation(item, inode->i_generation);
+       btrfs_set_inode_block_group(item,
+                                   BTRFS_I(inode)->block_group->key.objectid);
+}
+
+static int btrfs_update_inode(struct btrfs_trans_handle *trans,
+                             struct btrfs_root *root,
+                             struct inode *inode)
+{
+       struct btrfs_inode_item *inode_item;
+       struct btrfs_path *path;
+       int ret;
+
+       path = btrfs_alloc_path();
+       BUG_ON(!path);
+       btrfs_init_path(path);
+       ret = btrfs_lookup_inode(trans, root, path,
+                                &BTRFS_I(inode)->location, 1);
+       if (ret) {
+               if (ret > 0)
+                       ret = -ENOENT;
+               goto failed;
+       }
+
+       inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
+                                 path->slots[0],
+                                 struct btrfs_inode_item);
+
+       fill_inode_item(inode_item, inode);
+       btrfs_mark_buffer_dirty(path->nodes[0]);
+       ret = 0;
+failed:
+       btrfs_release_path(root, path);
+       btrfs_free_path(path);
+       return ret;
+}
+
+
 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
                              struct btrfs_root *root,
                              struct inode *dir,
@@ -127,37 +176,50 @@ static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
        struct btrfs_path *path;
        const char *name = dentry->d_name.name;
        int name_len = dentry->d_name.len;
-       int ret;
+       int ret = 0;
        u64 objectid;
        struct btrfs_dir_item *di;
 
        path = btrfs_alloc_path();
        BUG_ON(!path);
        btrfs_init_path(path);
-       ret = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
+       di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
                                    name, name_len, -1);
-       if (ret < 0)
+       if (IS_ERR(di)) {
+               ret = PTR_ERR(di);
                goto err;
-       if (ret > 0) {
+       }
+       if (!di) {
                ret = -ENOENT;
                goto err;
        }
-       di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
-                           struct btrfs_dir_item);
        objectid = btrfs_disk_key_objectid(&di->location);
+       ret = btrfs_delete_one_dir_name(trans, root, path, di);
+       BUG_ON(ret);
+       btrfs_release_path(root, path);
 
-       ret = btrfs_del_item(trans, root, path);
+       di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
+                                        objectid, name, name_len, -1);
+       if (IS_ERR(di)) {
+               ret = PTR_ERR(di);
+               goto err;
+       }
+       if (!di) {
+               ret = -ENOENT;
+               goto err;
+       }
+       ret = btrfs_delete_one_dir_name(trans, root, path, di);
        BUG_ON(ret);
 
-       btrfs_release_path(root, path);
        dentry->d_inode->i_ctime = dir->i_ctime;
 err:
-       btrfs_release_path(root, path);
        btrfs_free_path(path);
-       if (ret == 0) {
-               inode_dec_link_count(dentry->d_inode);
+       if (!ret) {
                dir->i_size -= name_len * 2;
-               mark_inode_dirty(dir);
+               btrfs_update_inode(trans, root, dir);
+               drop_nlink(dentry->d_inode);
+               btrfs_update_inode(trans, root, dentry->d_inode);
+               dir->i_sb->s_dirt = 1;
        }
        return ret;
 }
@@ -171,9 +233,11 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
        root = BTRFS_I(dir)->root;
        mutex_lock(&root->fs_info->fs_mutex);
        trans = btrfs_start_transaction(root, 1);
+       btrfs_set_trans_block_group(trans, dir);
        ret = btrfs_unlink_trans(trans, root, dir, dentry);
        btrfs_end_transaction(trans, root);
        mutex_unlock(&root->fs_info->fs_mutex);
+       btrfs_btree_balance_dirty(root);
        return ret;
 }
 
@@ -196,6 +260,7 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
        btrfs_init_path(path);
        mutex_lock(&root->fs_info->fs_mutex);
        trans = btrfs_start_transaction(root, 1);
+       btrfs_set_trans_block_group(trans, dir);
        key.objectid = inode->i_ino;
        key.offset = (u64)-1;
        key.flags = (u32)-1;
@@ -246,6 +311,7 @@ out:
        btrfs_free_path(path);
        mutex_unlock(&root->fs_info->fs_mutex);
        ret = btrfs_end_transaction(trans, root);
+       btrfs_btree_balance_dirty(root);
        if (ret && !err)
                err = ret;
        return err;
@@ -272,6 +338,35 @@ static int btrfs_free_inode(struct btrfs_trans_handle *trans,
        return ret;
 }
 
+static void reada_truncate(struct btrfs_root *root, struct btrfs_path *path,
+                          u64 objectid)
+{
+       struct btrfs_node *node;
+       int i;
+       int nritems;
+       u64 item_objectid;
+       u64 blocknr;
+       int slot;
+       int ret;
+
+       if (!path->nodes[1])
+               return;
+       node = btrfs_buffer_node(path->nodes[1]);
+       slot = path->slots[1];
+       if (slot == 0)
+               return;
+       nritems = btrfs_header_nritems(&node->header);
+       for (i = slot - 1; i >= 0; i--) {
+               item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
+               if (item_objectid != objectid)
+                       break;
+               blocknr = btrfs_node_blockptr(node, i);
+               ret = readahead_tree_block(root, blocknr);
+               if (ret)
+                       break;
+       }
+}
+
 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
                                   struct btrfs_root *root,
                                   struct inode *inode)
@@ -280,6 +375,7 @@ static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
        struct btrfs_path *path;
        struct btrfs_key key;
        struct btrfs_disk_key *found_key;
+       u32 found_type;
        struct btrfs_leaf *leaf;
        struct btrfs_file_extent_item *fi = NULL;
        u64 extent_start = 0;
@@ -291,12 +387,7 @@ static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
        /* FIXME, add redo link to tree so we don't leak on crash */
        key.objectid = inode->i_ino;
        key.offset = (u64)-1;
-       key.flags = 0;
-       /*
-        * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys
-        * or extent data
-        */
-       btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY);
+       key.flags = (u32)-1;
        while(1) {
                btrfs_init_path(path);
                ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
@@ -307,13 +398,16 @@ static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
                        BUG_ON(path->slots[0] == 0);
                        path->slots[0]--;
                }
+               reada_truncate(root, path, inode->i_ino);
                leaf = btrfs_buffer_leaf(path->nodes[0]);
                found_key = &leaf->items[path->slots[0]].key;
+               found_type = btrfs_disk_key_type(found_key);
                if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
                        break;
-               if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY &&
-                   btrfs_disk_key_type(found_key) != BTRFS_INLINE_DATA_KEY &&
-                   btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
+               if (found_type != BTRFS_CSUM_ITEM_KEY &&
+                   found_type != BTRFS_DIR_ITEM_KEY &&
+                   found_type != BTRFS_DIR_INDEX_KEY &&
+                   found_type != BTRFS_EXTENT_DATA_KEY)
                        break;
                if (btrfs_disk_key_offset(found_key) < inode->i_size)
                        break;
@@ -347,6 +441,7 @@ static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
 error:
        btrfs_release_path(root, path);
        btrfs_free_path(path);
+       inode->i_sb->s_dirt = 1;
        return ret;
 }
 
@@ -363,13 +458,13 @@ static void btrfs_delete_inode(struct inode *inode)
        inode->i_size = 0;
        mutex_lock(&root->fs_info->fs_mutex);
        trans = btrfs_start_transaction(root, 1);
-       if (S_ISREG(inode->i_mode)) {
-               ret = btrfs_truncate_in_trans(trans, root, inode);
-               BUG_ON(ret);
-       }
+       btrfs_set_trans_block_group(trans, inode);
+       ret = btrfs_truncate_in_trans(trans, root, inode);
+       BUG_ON(ret);
        btrfs_free_inode(trans, root, inode);
        btrfs_end_transaction(trans, root);
        mutex_unlock(&root->fs_info->fs_mutex);
+       btrfs_btree_balance_dirty(root);
        return;
 no_delete:
        clear_inode(inode);
@@ -388,15 +483,13 @@ static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
        path = btrfs_alloc_path();
        BUG_ON(!path);
        btrfs_init_path(path);
-       ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
+       di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
                                    namelen, 0);
-       if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) {
+       if (!di || IS_ERR(di)) {
                location->objectid = 0;
                ret = 0;
                goto out;
        }
-       di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
-                           struct btrfs_dir_item);
        btrfs_disk_key_to_cpu(location, &di->location);
 out:
        btrfs_release_path(root, path);
@@ -404,7 +497,7 @@ out:
        return ret;
 }
 
-int fixup_tree_root_location(struct btrfs_root *root,
+static int fixup_tree_root_location(struct btrfs_root *root,
                             struct btrfs_key *location,
                             struct btrfs_root **sub_root)
 {
@@ -435,7 +528,7 @@ int fixup_tree_root_location(struct btrfs_root *root,
        return 0;
 }
 
-int btrfs_init_locked_inode(struct inode *inode, void *p)
+static int btrfs_init_locked_inode(struct inode *inode, void *p)
 {
        struct btrfs_iget_args *args = p;
        inode->i_ino = args->ino;
@@ -443,15 +536,15 @@ int btrfs_init_locked_inode(struct inode *inode, void *p)
        return 0;
 }
 
-int btrfs_find_actor(struct inode *inode, void *opaque)
+static int btrfs_find_actor(struct inode *inode, void *opaque)
 {
        struct btrfs_iget_args *args = opaque;
        return (args->ino == inode->i_ino &&
                args->root == BTRFS_I(inode)->root);
 }
 
-struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
-                               struct btrfs_root *root)
+static struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
+                                      struct btrfs_root *root)
 {
        struct inode *inode;
        struct btrfs_iget_args args;
@@ -508,6 +601,33 @@ printk("adding new root for inode %lu root %p (found %p)\n", inode->i_ino, sub_r
        return d_splice_alias(inode, dentry);
 }
 
+static void reada_leaves(struct btrfs_root *root, struct btrfs_path *path,
+                        u64 objectid)
+{
+       struct btrfs_node *node;
+       int i;
+       u32 nritems;
+       u64 item_objectid;
+       u64 blocknr;
+       int slot;
+       int ret;
+
+       if (!path->nodes[1])
+               return;
+       node = btrfs_buffer_node(path->nodes[1]);
+       slot = path->slots[1];
+       nritems = btrfs_header_nritems(&node->header);
+       for (i = slot + 1; i < nritems; i++) {
+               item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
+               if (item_objectid != objectid)
+                       break;
+               blocknr = btrfs_node_blockptr(node, i);
+               ret = readahead_tree_block(root, blocknr);
+               if (ret)
+                       break;
+       }
+}
+
 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
 {
        struct inode *inode = filp->f_path.dentry->d_inode;
@@ -523,7 +643,10 @@ static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
        int advance;
        unsigned char d_type = DT_UNKNOWN;
        int over = 0;
-       int key_type = BTRFS_DIR_ITEM_KEY;
+       u32 di_cur;
+       u32 di_total;
+       u32 di_len;
+       int key_type = BTRFS_DIR_INDEX_KEY;
 
        /* FIXME, use a real flag for deciding about the key type */
        if (root->fs_info->tree_root == root)
@@ -539,12 +662,14 @@ static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
        if (ret < 0)
                goto err;
        advance = 0;
+       reada_leaves(root, path, inode->i_ino);
        while(1) {
                leaf = btrfs_buffer_leaf(path->nodes[0]);
                nritems = btrfs_header_nritems(&leaf->header);
                slot = path->slots[0];
                if (advance || slot >= nritems) {
                        if (slot >= nritems -1) {
+                               reada_leaves(root, path, inode->i_ino);
                                ret = btrfs_next_leaf(root, path);
                                if (ret)
                                        break;
@@ -567,12 +692,20 @@ static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
                filp->f_pos = btrfs_disk_key_offset(&item->key);
                advance = 1;
                di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
-               over = filldir(dirent, (const char *)(di + 1),
-                              btrfs_dir_name_len(di),
-                              btrfs_disk_key_offset(&item->key),
-                              btrfs_disk_key_objectid(&di->location), d_type);
-               if (over)
-                       goto nopos;
+               di_cur = 0;
+               di_total = btrfs_item_size(leaf->items + slot);
+               while(di_cur < di_total) {
+                       over = filldir(dirent, (const char *)(di + 1),
+                                      btrfs_dir_name_len(di),
+                                      btrfs_disk_key_offset(&item->key),
+                                      btrfs_disk_key_objectid(&di->location),
+                                      d_type);
+                       if (over)
+                               goto nopos;
+                       di_len = btrfs_dir_name_len(di) + sizeof(*di);
+                       di_cur += di_len;
+                       di = (struct btrfs_dir_item *)((char *)di + di_len);
+               }
        }
        filp->f_pos++;
 nopos:
@@ -647,87 +780,59 @@ static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
        return 0;
 }
 
-static void fill_inode_item(struct btrfs_inode_item *item,
-                           struct inode *inode)
-{
-       btrfs_set_inode_uid(item, inode->i_uid);
-       btrfs_set_inode_gid(item, inode->i_gid);
-       btrfs_set_inode_size(item, inode->i_size);
-       btrfs_set_inode_mode(item, inode->i_mode);
-       btrfs_set_inode_nlink(item, inode->i_nlink);
-       btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
-       btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
-       btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
-       btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
-       btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
-       btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
-       btrfs_set_inode_nblocks(item, inode->i_blocks);
-       btrfs_set_inode_generation(item, inode->i_generation);
-}
-
-static int btrfs_update_inode(struct btrfs_trans_handle *trans,
-                             struct btrfs_root *root,
-                             struct inode *inode)
+static int btrfs_write_inode(struct inode *inode, int wait)
 {
-       struct btrfs_inode_item *inode_item;
-       struct btrfs_path *path;
-       int ret;
+       struct btrfs_root *root = BTRFS_I(inode)->root;
+       struct btrfs_trans_handle *trans;
+       int ret = 0;
 
-       path = btrfs_alloc_path();
-       BUG_ON(!path);
-       btrfs_init_path(path);
-       ret = btrfs_lookup_inode(trans, root, path,
-                                &BTRFS_I(inode)->location, 1);
-       if (ret) {
-               if (ret > 0)
-                       ret = -ENOENT;
-               goto failed;
+       if (wait) {
+               mutex_lock(&root->fs_info->fs_mutex);
+               trans = btrfs_start_transaction(root, 1);
+               btrfs_set_trans_block_group(trans, inode);
+               ret = btrfs_commit_transaction(trans, root);
+               mutex_unlock(&root->fs_info->fs_mutex);
        }
-
-       inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
-                                 path->slots[0],
-                                 struct btrfs_inode_item);
-
-       fill_inode_item(inode_item, inode);
-       btrfs_mark_buffer_dirty(path->nodes[0]);
-       ret = 0;
-failed:
-       btrfs_release_path(root, path);
-       btrfs_free_path(path);
        return ret;
 }
 
-static int btrfs_write_inode(struct inode *inode, int wait)
+static void btrfs_dirty_inode(struct inode *inode)
 {
        struct btrfs_root *root = BTRFS_I(inode)->root;
        struct btrfs_trans_handle *trans;
-       int ret;
 
        mutex_lock(&root->fs_info->fs_mutex);
        trans = btrfs_start_transaction(root, 1);
-       ret = btrfs_update_inode(trans, root, inode);
-       if (wait)
-               btrfs_commit_transaction(trans, root);
-       else
-               btrfs_end_transaction(trans, root);
+       btrfs_set_trans_block_group(trans, inode);
+       btrfs_update_inode(trans, root, inode);
+       btrfs_end_transaction(trans, root);
        mutex_unlock(&root->fs_info->fs_mutex);
-       return ret;
+       btrfs_btree_balance_dirty(root);
 }
 
 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
                                     struct btrfs_root *root,
-                                    u64 objectid, int mode)
+                                    u64 objectid,
+                                    struct btrfs_block_group_cache *group,
+                                    int mode)
 {
        struct inode *inode;
        struct btrfs_inode_item inode_item;
        struct btrfs_key *location;
        int ret;
+       int owner;
 
        inode = new_inode(root->fs_info->sb);
        if (!inode)
                return ERR_PTR(-ENOMEM);
 
        BTRFS_I(inode)->root = root;
+       if (mode & S_IFDIR)
+               owner = 0;
+       else
+               owner = 1;
+       group = btrfs_find_block_group(root, group, 0, 0, owner);
+       BTRFS_I(inode)->block_group = group;
 
        inode->i_uid = current->fsuid;
        inode->i_gid = current->fsgid;
@@ -797,6 +902,7 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
 
        mutex_lock(&root->fs_info->fs_mutex);
        trans = btrfs_start_transaction(root, 1);
+       btrfs_set_trans_block_group(trans, dir);
 
        err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
        if (err) {
@@ -804,11 +910,13 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
                goto out_unlock;
        }
 
-       inode = btrfs_new_inode(trans, root, objectid, mode);
+       inode = btrfs_new_inode(trans, root, objectid,
+                               BTRFS_I(dir)->block_group, mode);
        err = PTR_ERR(inode);
        if (IS_ERR(inode))
                goto out_unlock;
-       // FIXME mark the inode dirty
+
+       btrfs_set_trans_block_group(trans, inode);
        err = btrfs_add_nondir(trans, dentry, inode);
        if (err)
                drop_inode = 1;
@@ -818,6 +926,8 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
                inode->i_op = &btrfs_file_inode_operations;
        }
        dir->i_sb->s_dirt = 1;
+       btrfs_update_inode_block_group(trans, inode);
+       btrfs_update_inode_block_group(trans, dir);
 out_unlock:
        btrfs_end_transaction(trans, root);
        mutex_unlock(&root->fs_info->fs_mutex);
@@ -826,6 +936,7 @@ out_unlock:
                inode_dec_link_count(inode);
                iput(inode);
        }
+       btrfs_btree_balance_dirty(root);
        return err;
 }
 
@@ -869,6 +980,7 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
 
        mutex_lock(&root->fs_info->fs_mutex);
        trans = btrfs_start_transaction(root, 1);
+       btrfs_set_trans_block_group(trans, dir);
        if (IS_ERR(trans)) {
                err = PTR_ERR(trans);
                goto out_unlock;
@@ -880,7 +992,8 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
                goto out_unlock;
        }
 
-       inode = btrfs_new_inode(trans, root, objectid, S_IFDIR | mode);
+       inode = btrfs_new_inode(trans, root, objectid,
+                               BTRFS_I(dir)->block_group, S_IFDIR | mode);
        if (IS_ERR(inode)) {
                err = PTR_ERR(inode);
                goto out_fail;
@@ -888,6 +1001,7 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
        drop_on_err = 1;
        inode->i_op = &btrfs_dir_inode_operations;
        inode->i_fop = &btrfs_dir_file_operations;
+       btrfs_set_trans_block_group(trans, inode);
 
        err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
        if (err)
@@ -902,6 +1016,9 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
                goto out_fail;
        d_instantiate(dentry, inode);
        drop_on_err = 0;
+       dir->i_sb->s_dirt = 1;
+       btrfs_update_inode_block_group(trans, inode);
+       btrfs_update_inode_block_group(trans, dir);
 
 out_fail:
        btrfs_end_transaction(trans, root);
@@ -909,9 +1026,30 @@ out_unlock:
        mutex_unlock(&root->fs_info->fs_mutex);
        if (drop_on_err)
                iput(inode);
+       btrfs_btree_balance_dirty(root);
        return err;
 }
 
+static int btrfs_sync_file(struct file *file,
+                          struct dentry *dentry, int datasync)
+{
+       struct inode *inode = dentry->d_inode;
+       struct btrfs_root *root = BTRFS_I(inode)->root;
+       int ret;
+       struct btrfs_trans_handle *trans;
+
+       mutex_lock(&root->fs_info->fs_mutex);
+       trans = btrfs_start_transaction(root, 1);
+       if (!trans) {
+               ret = -ENOMEM;
+               goto out;
+       }
+       ret = btrfs_commit_transaction(trans, root);
+       mutex_unlock(&root->fs_info->fs_mutex);
+out:
+       return ret > 0 ? EIO : ret;
+}
+
 static int btrfs_sync_fs(struct super_block *sb, int wait)
 {
        struct btrfs_trans_handle *trans;
@@ -924,7 +1062,6 @@ static int btrfs_sync_fs(struct super_block *sb, int wait)
                filemap_flush(root->fs_info->btree_inode->i_mapping);
                return 0;
        }
-       filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
        mutex_lock(&root->fs_info->fs_mutex);
        trans = btrfs_start_transaction(root, 1);
        ret = btrfs_commit_transaction(trans, root);
@@ -935,75 +1072,6 @@ printk("btrfs sync_fs\n");
        return 0;
 }
 
-#if 0
-static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
-                          struct buffer_head *result, int create)
-{
-       struct btrfs_root *root = btrfs_sb(inode->i_sb);
-       struct btrfs_path *path;
-       struct btrfs_key key;
-       struct btrfs_leaf *leaf;
-       int num_bytes = result->b_size;
-       int item_size;
-       int ret;
-       u64 pos;
-       char *ptr;
-       int copy_size;
-       int err = 0;
-       char *safe_ptr;
-       char *data_ptr;
-
-       path = btrfs_alloc_path();
-       BUG_ON(!path);
-
-       WARN_ON(create);
-       if (create) {
-               return 0;
-       }
-       pos = iblock << inode->i_blkbits;
-       key.objectid = inode->i_ino;
-       key.flags = 0;
-       btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
-       ptr = kmap(result->b_page);
-       safe_ptr = ptr;
-       ptr += (pos & (PAGE_CACHE_SIZE -1));
-again:
-       key.offset = pos;
-       ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
-       if (ret) {
-               if (ret < 0)
-                       err = ret;
-               else
-                       err = 0;
-               goto out;
-       }
-       leaf = btrfs_buffer_leaf(path->nodes[0]);
-       item_size = btrfs_item_size(leaf->items + path->slots[0]);
-       copy_size = min(num_bytes, item_size);
-       data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
-       WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
-       memcpy(ptr, data_ptr, copy_size);
-       pos += copy_size;
-       num_bytes -= copy_size;
-       WARN_ON(num_bytes < 0);
-       ptr += copy_size;
-       btrfs_release_path(root, path);
-       if (num_bytes != 0) {
-               if (pos >= i_size_read(inode))
-                       memset(ptr, 0, num_bytes);
-               else
-                       goto again;
-       }
-       set_buffer_uptodate(result);
-       map_bh(result, inode->i_sb, 0);
-       err = 0;
-out:
-       btrfs_free_path(path);
-       kunmap(result->b_page);
-       return err;
-}
-#endif
-
 static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
                           struct buffer_head *result, int create)
 {
@@ -1056,7 +1124,6 @@ static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
            found_type != BTRFS_EXTENT_DATA_KEY) {
                extent_end = 0;
                extent_start = 0;
-               btrfs_release_path(root, path);
                goto out;
        }
        found_type = btrfs_file_extent_type(item);
@@ -1092,7 +1159,6 @@ static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
                btrfs_map_bh_to_logical(root, result, 0);
        }
 out:
-       btrfs_release_path(root, path);
        btrfs_free_path(path);
        return err;
 }
@@ -1188,13 +1254,13 @@ static int __btrfs_write_full_page(struct inode *inode, struct page *page,
                } else if (!buffer_mapped(bh) && buffer_dirty(bh)) {
                        WARN_ON(bh->b_size != blocksize);
                        err = btrfs_get_block(inode, block, bh, 0);
-                       if (err)
+                       if (err) {
+printk("writepage going to recovery err %d\n", err);
                                goto recover;
+                       }
                        if (buffer_new(bh)) {
                                /* blockdev mappings never come here */
                                clear_buffer_new(bh);
-                               unmap_underlying_metadata(bh->b_bdev,
-                                                       bh->b_blocknr);
                        }
                }
                bh = bh->b_this_page;
@@ -1260,11 +1326,6 @@ done:
                if (uptodate)
                        SetPageUptodate(page);
                end_page_writeback(page);
-               /*
-                * The page and buffer_heads can be released at any time from
-                * here on.
-                */
-               wbc->pages_skipped++;   /* We didn't write this page */
        }
        return err;
 
@@ -1363,12 +1424,14 @@ static void btrfs_truncate(struct inode *inode)
        /* FIXME, add redo link to tree so we don't leak on crash */
        mutex_lock(&root->fs_info->fs_mutex);
        trans = btrfs_start_transaction(root, 1);
+       btrfs_set_trans_block_group(trans, inode);
        ret = btrfs_truncate_in_trans(trans, root, inode);
        BUG_ON(ret);
+       btrfs_update_inode(trans, root, inode);
        ret = btrfs_end_transaction(trans, root);
        BUG_ON(ret);
        mutex_unlock(&root->fs_info->fs_mutex);
-       mark_inode_dirty(inode);
+       btrfs_btree_balance_dirty(root);
 }
 
 /*
@@ -1459,6 +1522,7 @@ static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
 
                mutex_lock(&root->fs_info->fs_mutex);
                trans = btrfs_start_transaction(root, 1);
+               btrfs_set_trans_block_group(trans, inode);
 
                bh = page_buffers(pages[i]);
                if (buffer_mapped(bh) && bh->b_blocknr == 0) {
@@ -1485,7 +1549,8 @@ static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
                        btrfs_set_file_extent_type(ei,
                                                   BTRFS_FILE_EXTENT_INLINE);
                        ptr = btrfs_file_extent_inline_start(ei);
-                       memcpy(ptr, bh->b_data, offset + write_bytes);
+                       btrfs_memcpy(root, path->nodes[0]->b_data,
+                                    ptr, bh->b_data, offset + write_bytes);
                        mark_buffer_dirty(path->nodes[0]);
                        btrfs_free_path(path);
                } else {
@@ -1495,6 +1560,7 @@ static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
                        kunmap(pages[i]);
                }
                SetPageChecked(pages[i]);
+               // btrfs_update_inode_block_group(trans, inode);
                ret = btrfs_end_transaction(trans, root);
                BUG_ON(ret);
                mutex_unlock(&root->fs_info->fs_mutex);
@@ -1516,7 +1582,7 @@ failed:
 static int drop_extents(struct btrfs_trans_handle *trans,
                          struct btrfs_root *root,
                          struct inode *inode,
-                         u64 start, u64 end)
+                         u64 start, u64 end, u64 *hint_block)
 {
        int ret;
        struct btrfs_key key;
@@ -1613,17 +1679,14 @@ static int drop_extents(struct btrfs_trans_handle *trans,
                                new_num = (start - key.offset) >>
                                        inode->i_blkbits;
                                old_num = btrfs_file_extent_num_blocks(extent);
+                               *hint_block =
+                                       btrfs_file_extent_disk_blocknr(extent);
                                inode->i_blocks -= (old_num - new_num) << 3;
                                btrfs_set_file_extent_num_blocks(extent,
                                                                 new_num);
                                mark_buffer_dirty(path->nodes[0]);
                        } else {
                                WARN_ON(1);
-                               /*
-                               ret = btrfs_truncate_item(trans, root, path,
-                                                         start - key.offset);
-                               BUG_ON(ret);
-                               */
                        }
                }
                if (!keep) {
@@ -1637,13 +1700,15 @@ static int drop_extents(struct btrfs_trans_handle *trans,
                                      btrfs_file_extent_disk_num_blocks(extent);
                                extent_num_blocks =
                                      btrfs_file_extent_num_blocks(extent);
+                               *hint_block =
+                                       btrfs_file_extent_disk_blocknr(extent);
                        }
                        ret = btrfs_del_item(trans, root, path);
                        BUG_ON(ret);
                        btrfs_release_path(root, path);
+                       extent = NULL;
                        if (found_extent) {
-                               inode->i_blocks -=
-                               btrfs_file_extent_num_blocks(extent) << 3;
+                               inode->i_blocks -= extent_num_blocks << 3;
                                ret = btrfs_free_extent(trans, root,
                                                        disk_blocknr,
                                                        disk_num_blocks, 0);
@@ -1731,10 +1796,15 @@ static int prepare_pages(struct btrfs_root *root,
                        err = -ENOMEM;
                        goto failed_release;
                }
+               cancel_dirty_page(pages[i], PAGE_CACHE_SIZE);
+               wait_on_page_writeback(pages[i]);
                offset = pos & (PAGE_CACHE_SIZE -1);
                this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
-               create_empty_buffers(pages[i], root->fs_info->sb->s_blocksize,
-                                    (1 << BH_Uptodate));
+               if (!page_has_buffers(pages[i])) {
+                       create_empty_buffers(pages[i],
+                                            root->fs_info->sb->s_blocksize,
+                                            (1 << BH_Uptodate));
+               }
                head = page_buffers(pages[i]);
                bh = head;
                do {
@@ -1774,15 +1844,17 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
        struct inode *inode = file->f_path.dentry->d_inode;
        struct btrfs_root *root = BTRFS_I(inode)->root;
        struct page *pages[8];
-       struct page *pinned[2] = { NULL, NULL };
+       struct page *pinned[2];
        unsigned long first_index;
        unsigned long last_index;
        u64 start_pos;
        u64 num_blocks;
        u64 alloc_extent_start;
+       u64 hint_block;
        struct btrfs_trans_handle *trans;
        struct btrfs_key ins;
-
+       pinned[0] = NULL;
+       pinned[1] = NULL;
        if (file->f_flags & O_DIRECT)
                return -EINVAL;
        pos = *ppos;
@@ -1812,17 +1884,20 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
                if (!PageUptodate(pinned[0])) {
                        ret = mpage_readpage(pinned[0], btrfs_get_block);
                        BUG_ON(ret);
+                       wait_on_page_locked(pinned[0]);
                } else {
                        unlock_page(pinned[0]);
                }
        }
        if (first_index != last_index &&
            (last_index << PAGE_CACHE_SHIFT) < inode->i_size &&
+           pos + count < inode->i_size &&
            (count & (PAGE_CACHE_SIZE - 1))) {
                pinned[1] = grab_cache_page(inode->i_mapping, last_index);
                if (!PageUptodate(pinned[1])) {
                        ret = mpage_readpage(pinned[1], btrfs_get_block);
                        BUG_ON(ret);
+                       wait_on_page_locked(pinned[1]);
                } else {
                        unlock_page(pinned[1]);
                }
@@ -1835,20 +1910,23 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
                mutex_unlock(&root->fs_info->fs_mutex);
                goto out_unlock;
        }
+       btrfs_set_trans_block_group(trans, inode);
        /* FIXME blocksize != 4096 */
        inode->i_blocks += num_blocks << 3;
+       hint_block = 0;
        if (start_pos < inode->i_size) {
                /* FIXME blocksize != pagesize */
                ret = drop_extents(trans, root, inode,
                                   start_pos,
                                   (pos + count + root->blocksize -1) &
-                                  ~((u64)root->blocksize - 1));
+                                  ~((u64)root->blocksize - 1), &hint_block);
                BUG_ON(ret);
        }
        if (inode->i_size >= PAGE_CACHE_SIZE || pos + count < inode->i_size ||
            pos + count - start_pos > BTRFS_MAX_INLINE_DATA_SIZE(root)) {
-               ret = btrfs_alloc_extent(trans, root, num_blocks, 1,
-                                (u64)-1, &ins);
+               ret = btrfs_alloc_extent(trans, root, inode->i_ino,
+                                        num_blocks, hint_block, (u64)-1,
+                                        &ins, 1);
                BUG_ON(ret);
                ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
                                       start_pos, ins.objectid, ins.offset);
@@ -1859,6 +1937,7 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
        }
        BUG_ON(ret);
        alloc_extent_start = ins.objectid;
+       // btrfs_update_inode_block_group(trans, inode);
        ret = btrfs_end_transaction(trans, root);
        mutex_unlock(&root->fs_info->fs_mutex);
 
@@ -1892,6 +1971,7 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
                num_written += write_bytes;
 
                balance_dirty_pages_ratelimited(inode->i_mapping);
+               btrfs_btree_balance_dirty(root);
                cond_resched();
        }
 out_unlock:
@@ -1907,174 +1987,6 @@ out:
        return num_written ? num_written : err;
 }
 
-#if 0
-static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
-                          struct page *page, loff_t pos,
-                          size_t offset, size_t write_bytes)
-{
-       struct btrfs_path *path;
-       struct btrfs_trans_handle *trans;
-       struct btrfs_key key;
-       struct btrfs_leaf *leaf;
-       struct btrfs_key found_key;
-       int ret;
-       size_t copy_size = 0;
-       char *dst = NULL;
-       int err = 0;
-       size_t num_written = 0;
-
-       path = btrfs_alloc_path();
-       BUG_ON(!path);
-       mutex_lock(&root->fs_info->fs_mutex);
-       trans = btrfs_start_transaction(root, 1);
-       key.objectid = inode->i_ino;
-       key.flags = 0;
-       btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
-
-again:
-       key.offset = pos;
-       ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
-       if (ret < 0) {
-               err = ret;
-               goto out;
-       }
-       if (ret == 0) {
-               leaf = btrfs_buffer_leaf(path->nodes[0]);
-               btrfs_disk_key_to_cpu(&found_key,
-                                     &leaf->items[path->slots[0]].key);
-               copy_size = btrfs_item_size(leaf->items + path->slots[0]);
-               dst = btrfs_item_ptr(leaf, path->slots[0], char);
-               copy_size = min(write_bytes, copy_size);
-               goto copyit;
-       } else {
-               int slot = path->slots[0];
-               if (slot > 0) {
-                       slot--;
-               }
-               // FIXME find max key
-               leaf = btrfs_buffer_leaf(path->nodes[0]);
-               btrfs_disk_key_to_cpu(&found_key,
-                                     &leaf->items[slot].key);
-               if (found_key.objectid != inode->i_ino)
-                       goto insert;
-               if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
-                       goto insert;
-               copy_size = btrfs_item_size(leaf->items + slot);
-               if (found_key.offset + copy_size <= pos)
-                       goto insert;
-               dst = btrfs_item_ptr(leaf, path->slots[0], char);
-               dst += pos - found_key.offset;
-               copy_size = copy_size - (pos - found_key.offset);
-               BUG_ON(copy_size < 0);
-               copy_size = min(write_bytes, copy_size);
-               WARN_ON(copy_size == 0);
-               goto copyit;
-       }
-insert:
-       btrfs_release_path(root, path);
-       copy_size = min(write_bytes,
-                       (size_t)BTRFS_LEAF_DATA_SIZE(root) -
-                       sizeof(struct btrfs_item) * 4);
-       ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
-       BUG_ON(ret);
-       dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
-                            path->slots[0], char);
-copyit:
-       WARN_ON(copy_size == 0);
-       WARN_ON(dst + copy_size >
-               btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
-                                                path->slots[0], char) +
-               btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
-                                                 path->slots[0]));
-       btrfs_memcpy(root, path->nodes[0]->b_data, dst,
-                    page_address(page) + offset, copy_size);
-       mark_buffer_dirty(path->nodes[0]);
-       btrfs_release_path(root, path);
-       pos += copy_size;
-       offset += copy_size;
-       num_written += copy_size;
-       write_bytes -= copy_size;
-       if (write_bytes)
-               goto again;
-out:
-       btrfs_free_path(path);
-       ret = btrfs_end_transaction(trans, root);
-       BUG_ON(ret);
-       mutex_unlock(&root->fs_info->fs_mutex);
-       return num_written ? num_written : err;
-}
-
-static ssize_t btrfs_file_inline_write(struct file *file,
-                                      const char __user *buf,
-                                      size_t count, loff_t *ppos)
-{
-       loff_t pos;
-       size_t num_written = 0;
-       int err = 0;
-       int ret = 0;
-       struct inode *inode = file->f_path.dentry->d_inode;
-       struct btrfs_root *root = BTRFS_I(inode)->root;
-       unsigned long page_index;
-
-       if (file->f_flags & O_DIRECT)
-               return -EINVAL;
-       pos = *ppos;
-
-       vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
-       current->backing_dev_info = inode->i_mapping->backing_dev_info;
-       err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
-       if (err)
-               goto out;
-       if (count == 0)
-               goto out;
-       err = remove_suid(file->f_path.dentry);
-       if (err)
-               goto out;
-       file_update_time(file);
-       mutex_lock(&inode->i_mutex);
-       while(count > 0) {
-               size_t offset = pos & (PAGE_CACHE_SIZE - 1);
-               size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
-               struct page *page;
-
-               page_index = pos >> PAGE_CACHE_SHIFT;
-               page = grab_cache_page(inode->i_mapping, page_index);
-               if (!PageUptodate(page)) {
-                       ret = mpage_readpage(page, btrfs_get_block);
-                       BUG_ON(ret);
-                       lock_page(page);
-               }
-               ret = btrfs_copy_from_user(pos, 1,
-                                          write_bytes, &page, buf);
-               BUG_ON(ret);
-               write_bytes = inline_one_page(root, inode, page, pos,
-                                     offset, write_bytes);
-               SetPageUptodate(page);
-               if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
-                       i_size_write(inode, pos + write_bytes);
-                       mark_inode_dirty(inode);
-               }
-               page_cache_release(page);
-               unlock_page(page);
-               if (write_bytes < 0)
-                       goto out_unlock;
-               buf += write_bytes;
-               count -= write_bytes;
-               pos += write_bytes;
-               num_written += write_bytes;
-
-               balance_dirty_pages_ratelimited(inode->i_mapping);
-               cond_resched();
-       }
-out_unlock:
-       mutex_unlock(&inode->i_mutex);
-out:
-       *ppos = pos;
-       current->backing_dev_info = NULL;
-       return num_written ? num_written : err;
-}
-#endif
-
 static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
                        unsigned long offset, unsigned long size)
 {
@@ -2199,6 +2111,7 @@ static int create_subvol(struct btrfs_root *root, char *name, int namelen)
        struct btrfs_leaf *leaf;
        struct btrfs_root *new_root;
        struct inode *inode;
+       struct inode *dir;
        int ret;
        u64 objectid;
        u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
@@ -2207,14 +2120,18 @@ static int create_subvol(struct btrfs_root *root, char *name, int namelen)
        trans = btrfs_start_transaction(root, 1);
        BUG_ON(!trans);
 
-       subvol = btrfs_alloc_free_block(trans, root);
+       subvol = btrfs_alloc_free_block(trans, root, 0);
+       if (subvol == NULL)
+               return -ENOSPC;
        leaf = btrfs_buffer_leaf(subvol);
        btrfs_set_header_nritems(&leaf->header, 0);
        btrfs_set_header_level(&leaf->header, 0);
        btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
        btrfs_set_header_generation(&leaf->header, trans->transid);
+       btrfs_set_header_owner(&leaf->header, root->root_key.objectid);
        memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
               sizeof(leaf->header.fsid));
+       mark_buffer_dirty(subvol);
 
        inode_item = &root_item.inode;
        memset(inode_item, 0, sizeof(*inode_item));
@@ -2226,8 +2143,6 @@ static int create_subvol(struct btrfs_root *root, char *name, int namelen)
 
        btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
        btrfs_set_root_refs(&root_item, 1);
-
-       mark_buffer_dirty(subvol);
        brelse(subvol);
        subvol = NULL;
 
@@ -2249,10 +2164,9 @@ static int create_subvol(struct btrfs_root *root, char *name, int namelen)
         * insert the directory item
         */
        key.offset = (u64)-1;
+       dir = root->fs_info->sb->s_root->d_inode;
        ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
-                                   name, namelen,
-                                   root->fs_info->sb->s_root->d_inode->i_ino,
-                                   &key, 0);
+                                   name, namelen, dir->i_ino, &key, 0);
        BUG_ON(ret);
 
        ret = btrfs_commit_transaction(trans, root);
@@ -2264,7 +2178,8 @@ static int create_subvol(struct btrfs_root *root, char *name, int namelen)
        trans = btrfs_start_transaction(new_root, 1);
        BUG_ON(!trans);
 
-       inode = btrfs_new_inode(trans, new_root, new_dirid, S_IFDIR | 0700);
+       inode = btrfs_new_inode(trans, new_root, new_dirid,
+                               BTRFS_I(dir)->block_group, S_IFDIR | 0700);
        inode->i_op = &btrfs_dir_inode_operations;
        inode->i_fop = &btrfs_dir_file_operations;
 
@@ -2282,6 +2197,7 @@ static int create_subvol(struct btrfs_root *root, char *name, int namelen)
        iput(inode);
 
        mutex_unlock(&root->fs_info->fs_mutex);
+       btrfs_btree_balance_dirty(root);
        return 0;
 }
 
@@ -2337,6 +2253,7 @@ static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
        ret = btrfs_commit_transaction(trans, root);
        BUG_ON(ret);
        mutex_unlock(&root->fs_info->fs_mutex);
+       btrfs_btree_balance_dirty(root);
        return 0;
 }
 
@@ -2412,6 +2329,7 @@ out:
        mutex_unlock(&root->fs_info->fs_mutex);
 out_nolock:
        btrfs_free_path(path);
+       btrfs_btree_balance_dirty(root);
 
        return ret;
 }
@@ -2422,6 +2340,7 @@ static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
        struct btrfs_root *root = BTRFS_I(inode)->root;
        struct btrfs_ioctl_vol_args vol_args;
        int ret = 0;
+       struct btrfs_dir_item *di;
        int namelen;
        struct btrfs_path *path;
        u64 root_dirid;
@@ -2440,12 +2359,12 @@ static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
                        return -ENOMEM;
                root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
                mutex_lock(&root->fs_info->fs_mutex);
-               ret = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
+               di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
                                    path, root_dirid,
                                    vol_args.name, namelen, 0);
                mutex_unlock(&root->fs_info->fs_mutex);
                btrfs_free_path(path);
-               if (ret == 0)
+               if (di && !IS_ERR(di))
                        return -EEXIST;
 
                if (root == root->fs_info->tree_root)
@@ -2503,7 +2422,7 @@ static void init_once(void * foo, struct kmem_cache * cachep,
 {
        struct btrfs_inode *ei = (struct btrfs_inode *) foo;
 
-       if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
+       if ((flags & (SLAB_CTOR_CONSTRUCTOR)) ==
            SLAB_CTOR_CONSTRUCTOR) {
                inode_init_once(&ei->vfs_inode);
        }
@@ -2559,7 +2478,6 @@ static int btrfs_get_sb(struct file_system_type *fs_type,
                           btrfs_fill_super, mnt);
 }
 
-
 static int btrfs_getattr(struct vfsmount *mnt,
                         struct dentry *dentry, struct kstat *stat)
 {
@@ -2569,6 +2487,130 @@ static int btrfs_getattr(struct vfsmount *mnt,
        return 0;
 }
 
+static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
+{
+       struct btrfs_root *root = btrfs_sb(dentry->d_sb);
+       struct btrfs_super_block *disk_super = root->fs_info->disk_super;
+
+       buf->f_namelen = BTRFS_NAME_LEN;
+       buf->f_blocks = btrfs_super_total_blocks(disk_super);
+       buf->f_bfree = buf->f_blocks - btrfs_super_blocks_used(disk_super);
+       buf->f_bavail = buf->f_bfree;
+       buf->f_bsize = dentry->d_sb->s_blocksize;
+       buf->f_type = BTRFS_SUPER_MAGIC;
+       return 0;
+}
+
+static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
+                          struct inode * new_dir,struct dentry *new_dentry)
+{
+       struct btrfs_trans_handle *trans;
+       struct btrfs_root *root = BTRFS_I(old_dir)->root;
+       struct inode *new_inode = new_dentry->d_inode;
+       struct inode *old_inode = old_dentry->d_inode;
+       struct timespec ctime = CURRENT_TIME;
+       struct btrfs_path *path;
+       struct btrfs_dir_item *di;
+       int ret;
+
+       if (S_ISDIR(old_inode->i_mode) && new_inode &&
+           new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
+               return -ENOTEMPTY;
+       }
+       mutex_lock(&root->fs_info->fs_mutex);
+       trans = btrfs_start_transaction(root, 1);
+       btrfs_set_trans_block_group(trans, new_dir);
+       path = btrfs_alloc_path();
+       if (!path) {
+               ret = -ENOMEM;
+               goto out_fail;
+       }
+
+       old_dentry->d_inode->i_nlink++;
+       old_dir->i_ctime = old_dir->i_mtime = ctime;
+       new_dir->i_ctime = new_dir->i_mtime = ctime;
+       old_inode->i_ctime = ctime;
+       if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
+               struct btrfs_key *location = &BTRFS_I(new_dir)->location;
+               u64 old_parent_oid;
+               di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
+                                          "..", 2, -1);
+               if (IS_ERR(di)) {
+                       ret = PTR_ERR(di);
+                       goto out_fail;
+               }
+               if (!di) {
+                       ret = -ENOENT;
+                       goto out_fail;
+               }
+               old_parent_oid = btrfs_disk_key_objectid(&di->location);
+               ret = btrfs_del_item(trans, root, path);
+               if (ret) {
+                       ret = -EIO;
+                       goto out_fail;
+               }
+               btrfs_release_path(root, path);
+
+               di = btrfs_lookup_dir_index_item(trans, root, path,
+                                                old_inode->i_ino,
+                                                old_parent_oid,
+                                                "..", 2, -1);
+               if (IS_ERR(di)) {
+                       ret = PTR_ERR(di);
+                       goto out_fail;
+               }
+               if (!di) {
+                       ret = -ENOENT;
+                       goto out_fail;
+               }
+               ret = btrfs_del_item(trans, root, path);
+               if (ret) {
+                       ret = -EIO;
+                       goto out_fail;
+               }
+               btrfs_release_path(root, path);
+
+               ret = btrfs_insert_dir_item(trans, root, "..", 2,
+                                           old_inode->i_ino, location, 0);
+               if (ret)
+                       goto out_fail;
+       }
+
+
+       ret = btrfs_add_link(trans, new_dentry, old_inode);
+       if (ret == -EEXIST && new_inode)
+               ret = 0;
+       else if (ret)
+               goto out_fail;
+
+       ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
+       if (ret)
+               goto out_fail;
+
+       if (new_inode) {
+               new_inode->i_ctime = CURRENT_TIME;
+               di = btrfs_lookup_dir_index_item(trans, root, path,
+                                                new_dir->i_ino,
+                                                new_inode->i_ino,
+                                                new_dentry->d_name.name,
+                                                new_dentry->d_name.len, -1);
+               if (di && !IS_ERR(di)) {
+                       btrfs_del_item(trans, root, path);
+                       btrfs_release_path(root, path);
+               }
+               if (S_ISDIR(new_inode->i_mode))
+                       clear_nlink(new_inode);
+               else
+                       drop_nlink(new_inode);
+               btrfs_update_inode(trans, root, new_inode);
+       }
+out_fail:
+       btrfs_free_path(path);
+       btrfs_end_transaction(trans, root);
+       mutex_unlock(&root->fs_info->fs_mutex);
+       return ret;
+}
+
 static struct file_system_type btrfs_fs_type = {
        .owner          = THIS_MODULE,
        .name           = "btrfs",
@@ -2578,15 +2620,16 @@ static struct file_system_type btrfs_fs_type = {
 };
 
 static struct super_operations btrfs_super_ops = {
-       .statfs         = simple_statfs,
        .delete_inode   = btrfs_delete_inode,
        .put_super      = btrfs_put_super,
        .read_inode     = btrfs_read_locked_inode,
        .write_super    = btrfs_write_super,
        .sync_fs        = btrfs_sync_fs,
        .write_inode    = btrfs_write_inode,
+       .dirty_inode    = btrfs_dirty_inode,
        .alloc_inode    = btrfs_alloc_inode,
        .destroy_inode  = btrfs_destroy_inode,
+       .statfs         = btrfs_statfs,
 };
 
 static struct inode_operations btrfs_dir_inode_operations = {
@@ -2595,6 +2638,7 @@ static struct inode_operations btrfs_dir_inode_operations = {
        .unlink         = btrfs_unlink,
        .mkdir          = btrfs_mkdir,
        .rmdir          = btrfs_rmdir,
+       .rename         = btrfs_rename,
 };
 
 static struct inode_operations btrfs_dir_ro_inode_operations = {
@@ -2629,6 +2673,7 @@ static struct file_operations btrfs_file_operations = {
        .mmap           = generic_file_mmap,
        .open           = generic_file_open,
        .ioctl          = btrfs_ioctl,
+       .fsync          = btrfs_sync_file,
 };
 
 static int __init init_btrfs_fs(void)
@@ -2638,12 +2683,7 @@ static int __init init_btrfs_fs(void)
        err = init_inodecache();
        if (err)
                return err;
-       kset_set_kset_s(&btrfs_subsys, fs_subsys);
-       err = subsystem_register(&btrfs_subsys);
-       if (err)
-               goto out;
        return register_filesystem(&btrfs_fs_type);
-out:
        destroy_inodecache();
        return err;
 }
@@ -2652,7 +2692,6 @@ static void __exit exit_btrfs_fs(void)
 {
        destroy_inodecache();
        unregister_filesystem(&btrfs_fs_type);
-       subsystem_unregister(&btrfs_subsys);
        printk("btrfs unloaded\n");
 }
 
This page took 0.042504 seconds and 5 git commands to generate.