Btrfs: Don't pin pages in ram until the entire ordered extent is on disk.
[linux-2.6-block.git] / fs / btrfs / inode.c
index 1a74b5018699d5c5d181afe5e75e84e739ac6e82..293355c92a4ff02361249ea7f889b4acd371ab7d 100644 (file)
@@ -19,6 +19,7 @@
 #include <linux/kernel.h>
 #include <linux/bio.h>
 #include <linux/buffer_head.h>
+#include <linux/file.h>
 #include <linux/fs.h>
 #include <linux/pagemap.h>
 #include <linux/highmem.h>
@@ -42,6 +43,7 @@
 #include "ioctl.h"
 #include "print-tree.h"
 #include "volumes.h"
+#include "ordered-data.h"
 
 struct btrfs_iget_args {
        u64 ino;
@@ -78,12 +80,15 @@ static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
 int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
                           int for_del)
 {
-       u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
-       u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
+       u64 total;
+       u64 used;
        u64 thresh;
        unsigned long flags;
        int ret = 0;
 
+       spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
+       total = btrfs_super_total_bytes(&root->fs_info->super_copy);
+       used = btrfs_super_bytes_used(&root->fs_info->super_copy);
        if (for_del)
                thresh = total * 90;
        else
@@ -91,7 +96,6 @@ int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
 
        do_div(thresh, 100);
 
-       spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
        if (used + root->fs_info->delalloc_bytes + num_required > thresh)
                ret = -ENOSPC;
        spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
@@ -106,44 +110,56 @@ static int cow_file_range(struct inode *inode, u64 start, u64 end)
        u64 num_bytes;
        u64 cur_alloc_size;
        u64 blocksize = root->sectorsize;
-       u64 orig_start = start;
        u64 orig_num_bytes;
        struct btrfs_key ins;
-       int ret;
+       struct extent_map *em;
+       struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
+       int ret = 0;
 
-       trans = btrfs_start_transaction(root, 1);
+       trans = btrfs_join_transaction(root, 1);
        BUG_ON(!trans);
        btrfs_set_trans_block_group(trans, inode);
 
        num_bytes = (end - start + blocksize) & ~(blocksize - 1);
        num_bytes = max(blocksize,  num_bytes);
-       ret = btrfs_drop_extents(trans, root, inode,
-                                start, start + num_bytes, start, &alloc_hint);
        orig_num_bytes = num_bytes;
 
        if (alloc_hint == EXTENT_MAP_INLINE)
                goto out;
 
        BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
+       btrfs_drop_extent_cache(inode, start, start + num_bytes - 1);
 
        while(num_bytes > 0) {
                cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
-               ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
-                                        root->sectorsize,
-                                        root->root_key.objectid,
-                                        trans->transid,
-                                        inode->i_ino, start, 0,
-                                        alloc_hint, (u64)-1, &ins, 1);
+               ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
+                                          root->sectorsize, 0, 0,
+                                          (u64)-1, &ins, 1);
                if (ret) {
                        WARN_ON(1);
                        goto out;
                }
+               em = alloc_extent_map(GFP_NOFS);
+               em->start = start;
+               em->len = ins.offset;
+               em->block_start = ins.objectid;
+               em->bdev = root->fs_info->fs_devices->latest_bdev;
+               while(1) {
+                       spin_lock(&em_tree->lock);
+                       ret = add_extent_mapping(em_tree, em);
+                       spin_unlock(&em_tree->lock);
+                       if (ret != -EEXIST) {
+                               free_extent_map(em);
+                               break;
+                       }
+                       btrfs_drop_extent_cache(inode, start,
+                                               start + ins.offset - 1);
+               }
+
                cur_alloc_size = ins.offset;
-               ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
-                                              start, ins.objectid, ins.offset,
+               ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
                                               ins.offset);
-               inode->i_blocks += ins.offset >> 9;
-               btrfs_check_file(root, inode);
+               BUG_ON(ret);
                if (num_bytes < cur_alloc_size) {
                        printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
                               cur_alloc_size);
@@ -153,10 +169,6 @@ static int cow_file_range(struct inode *inode, u64 start, u64 end)
                alloc_hint = ins.objectid + ins.offset;
                start += cur_alloc_size;
        }
-       btrfs_drop_extent_cache(inode, orig_start,
-                               orig_start + orig_num_bytes - 1);
-       btrfs_add_ordered_inode(inode);
-       btrfs_update_inode(trans, root, inode);
 out:
        btrfs_end_transaction(trans, root);
        return ret;
@@ -171,6 +183,7 @@ static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
        u64 loops = 0;
        u64 total_fs_bytes;
        struct btrfs_root *root = BTRFS_I(inode)->root;
+       struct btrfs_block_group_cache *block_group;
        struct extent_buffer *leaf;
        int found_type;
        struct btrfs_path *path;
@@ -205,9 +218,8 @@ again:
        btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
        found_type = btrfs_key_type(&found_key);
        if (found_key.objectid != inode->i_ino ||
-           found_type != BTRFS_EXTENT_DATA_KEY) {
+           found_type != BTRFS_EXTENT_DATA_KEY)
                goto not_found;
-       }
 
        found_type = btrfs_file_extent_type(leaf, item);
        extent_start = found_key.offset;
@@ -229,17 +241,20 @@ again:
                if (bytenr == 0)
                        goto not_found;
 
+               if (btrfs_count_snapshots_in_path(root, path, inode->i_ino,
+                                                 bytenr) != 1) {
+                       goto not_found;
+               }
+
                /*
                 * we may be called by the resizer, make sure we're inside
                 * the limits of the FS
                 */
-               if (bytenr + extent_num_bytes > total_fs_bytes)
+               block_group = btrfs_lookup_block_group(root->fs_info,
+                                                      bytenr);
+               if (!block_group || block_group->ro)
                        goto not_found;
 
-               if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
-                       goto not_found;
-               }
-
                start = extent_end;
        } else {
                goto not_found;
@@ -254,8 +269,8 @@ loop:
        goto again;
 
 not_found:
-       cow_file_range(inode, start, cow_end);
-       start = cow_end + 1;
+       cow_file_range(inode, start, end);
+       start = end + 1;
        goto loop;
 }
 
@@ -263,14 +278,13 @@ static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
 {
        struct btrfs_root *root = BTRFS_I(inode)->root;
        int ret;
-       mutex_lock(&root->fs_info->fs_mutex);
+
        if (btrfs_test_opt(root, NODATACOW) ||
            btrfs_test_flag(inode, NODATACOW))
                ret = run_delalloc_nocow(inode, start, end);
        else
                ret = cow_file_range(inode, start, end);
 
-       mutex_unlock(&root->fs_info->fs_mutex);
        return ret;
 }
 
@@ -336,26 +350,16 @@ int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
                          int mirror_num)
 {
        struct btrfs_root *root = BTRFS_I(inode)->root;
-       struct btrfs_trans_handle *trans;
        int ret = 0;
-       char *sums = NULL;
+       struct btrfs_ordered_sum *sums;
 
        ret = btrfs_csum_one_bio(root, bio, &sums);
        BUG_ON(ret);
 
-       mutex_lock(&root->fs_info->fs_mutex);
-       trans = btrfs_start_transaction(root, 1);
-
-       btrfs_set_trans_block_group(trans, inode);
-       btrfs_csum_file_blocks(trans, root, inode, bio, sums);
-
-       ret = btrfs_end_transaction(trans, root);
+       ret = btrfs_add_ordered_sum(inode, sums);
        BUG_ON(ret);
-       mutex_unlock(&root->fs_info->fs_mutex);
-
-       kfree(sums);
 
-       return btrfs_map_bio(root, rw, bio, mirror_num);
+       return btrfs_map_bio(root, rw, bio, mirror_num, 1);
 }
 
 int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
@@ -364,14 +368,10 @@ int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
        struct btrfs_root *root = BTRFS_I(inode)->root;
        int ret = 0;
 
-       if (!(rw & (1 << BIO_RW))) {
-               ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
-               BUG_ON(ret);
-               goto mapit;
-       }
+       ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
+       BUG_ON(ret);
 
-       if (btrfs_test_opt(root, NODATASUM) ||
-           btrfs_test_flag(inode, NODATASUM)) {
+       if (!(rw & (1 << BIO_RW))) {
                goto mapit;
        }
 
@@ -379,7 +379,175 @@ int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
                                   inode, rw, bio, mirror_num,
                                   __btrfs_submit_bio_hook);
 mapit:
-       return btrfs_map_bio(root, rw, bio, mirror_num);
+       return btrfs_map_bio(root, rw, bio, mirror_num, 0);
+}
+
+static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
+                            struct inode *inode, u64 file_offset,
+                            struct list_head *list)
+{
+       struct list_head *cur;
+       struct btrfs_ordered_sum *sum;
+
+       btrfs_set_trans_block_group(trans, inode);
+       list_for_each(cur, list) {
+               sum = list_entry(cur, struct btrfs_ordered_sum, list);
+               mutex_lock(&BTRFS_I(inode)->csum_mutex);
+               btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
+                                      inode, sum);
+               mutex_unlock(&BTRFS_I(inode)->csum_mutex);
+       }
+       return 0;
+}
+
+struct btrfs_writepage_fixup {
+       struct page *page;
+       struct btrfs_work work;
+};
+
+/* see btrfs_writepage_start_hook for details on why this is required */
+void btrfs_writepage_fixup_worker(struct btrfs_work *work)
+{
+       struct btrfs_writepage_fixup *fixup;
+       struct btrfs_ordered_extent *ordered;
+       struct page *page;
+       struct inode *inode;
+       u64 page_start;
+       u64 page_end;
+
+       fixup = container_of(work, struct btrfs_writepage_fixup, work);
+       page = fixup->page;
+
+       lock_page(page);
+       if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
+               ClearPageChecked(page);
+               goto out_page;
+       }
+
+       inode = page->mapping->host;
+       page_start = page_offset(page);
+       page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
+
+       lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
+       ordered = btrfs_lookup_ordered_extent(inode, page_start);
+       if (ordered)
+               goto out;
+
+       set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start, page_end,
+                           GFP_NOFS);
+       ClearPageChecked(page);
+out:
+       unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
+out_page:
+       unlock_page(page);
+       page_cache_release(page);
+}
+
+/*
+ * There are a few paths in the higher layers of the kernel that directly
+ * set the page dirty bit without asking the filesystem if it is a
+ * good idea.  This causes problems because we want to make sure COW
+ * properly happens and the data=ordered rules are followed.
+ *
+ * In our case any range that doesn't have the EXTENT_ORDERED bit set
+ * hasn't been properly setup for IO.  We kick off an async process
+ * to fix it up.  The async helper will wait for ordered extents, set
+ * the delalloc bit and make it safe to write the page.
+ */
+int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
+{
+       struct inode *inode = page->mapping->host;
+       struct btrfs_writepage_fixup *fixup;
+       struct btrfs_root *root = BTRFS_I(inode)->root;
+       int ret;
+
+       ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
+                            EXTENT_ORDERED, 0);
+       if (ret)
+               return 0;
+
+       if (PageChecked(page))
+               return -EAGAIN;
+
+       fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
+       if (!fixup)
+               return -EAGAIN;
+printk("queueing worker to fixup page %lu %Lu\n", inode->i_ino, page_offset(page));
+       SetPageChecked(page);
+       page_cache_get(page);
+       fixup->work.func = btrfs_writepage_fixup_worker;
+       fixup->page = page;
+       btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
+       return -EAGAIN;
+}
+
+int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
+                               struct extent_state *state, int uptodate)
+{
+       struct inode *inode = page->mapping->host;
+       struct btrfs_root *root = BTRFS_I(inode)->root;
+       struct btrfs_trans_handle *trans;
+       struct btrfs_ordered_extent *ordered_extent;
+       struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
+       u64 alloc_hint = 0;
+       struct list_head list;
+       struct btrfs_key ins;
+       int ret;
+
+       ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
+       if (!ret)
+               return 0;
+
+       trans = btrfs_join_transaction(root, 1);
+
+       ordered_extent = btrfs_lookup_ordered_extent(inode, start);
+       BUG_ON(!ordered_extent);
+
+       lock_extent(io_tree, ordered_extent->file_offset,
+                   ordered_extent->file_offset + ordered_extent->len - 1,
+                   GFP_NOFS);
+
+       INIT_LIST_HEAD(&list);
+
+       ins.objectid = ordered_extent->start;
+       ins.offset = ordered_extent->len;
+       ins.type = BTRFS_EXTENT_ITEM_KEY;
+       ret = btrfs_alloc_reserved_extent(trans, root, root->root_key.objectid,
+                                         trans->transid, inode->i_ino,
+                                         ordered_extent->file_offset, &ins);
+       BUG_ON(ret);
+       ret = btrfs_drop_extents(trans, root, inode,
+                                ordered_extent->file_offset,
+                                ordered_extent->file_offset +
+                                ordered_extent->len,
+                                ordered_extent->file_offset, &alloc_hint);
+       BUG_ON(ret);
+       ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
+                                      ordered_extent->file_offset,
+                                      ordered_extent->start,
+                                      ordered_extent->len,
+                                      ordered_extent->len, 0);
+       BUG_ON(ret);
+       btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
+                               ordered_extent->file_offset +
+                               ordered_extent->len - 1);
+       inode->i_blocks += ordered_extent->len >> 9;
+       unlock_extent(io_tree, ordered_extent->file_offset,
+                   ordered_extent->file_offset + ordered_extent->len - 1,
+                   GFP_NOFS);
+       add_pending_csums(trans, inode, ordered_extent->file_offset,
+                         &ordered_extent->list);
+
+       btrfs_ordered_update_i_size(inode, ordered_extent);
+       btrfs_remove_ordered_extent(inode, ordered_extent);
+       /* once for us */
+       btrfs_put_ordered_extent(ordered_extent);
+       /* once for the tree */
+       btrfs_put_ordered_extent(ordered_extent);
+
+       btrfs_update_inode(trans, root, inode);
+       btrfs_end_transaction(trans, root);
+       return 0;
 }
 
 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
@@ -396,25 +564,37 @@ int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
            btrfs_test_flag(inode, NODATASUM))
                return 0;
 
-       mutex_lock(&root->fs_info->fs_mutex);
        path = btrfs_alloc_path();
        item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
        if (IS_ERR(item)) {
+               /*
+                * It is possible there is an ordered extent that has
+                * not yet finished for this range in the file.  If so,
+                * that extent will have a csum cached, and it will insert
+                * the sum after all the blocks in the extent are fully
+                * on disk.  So, look for an ordered extent and use the
+                * sum if found.
+                */
+               ret = btrfs_find_ordered_sum(inode, start, &csum);
+               if (ret == 0)
+                       goto found;
+
                ret = PTR_ERR(item);
                /* a csum that isn't present is a preallocated region. */
                if (ret == -ENOENT || ret == -EFBIG)
                        ret = 0;
                csum = 0;
-               printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
+               printk("no csum found for inode %lu start %Lu\n", inode->i_ino,
+                      start);
                goto out;
        }
        read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
                           BTRFS_CRC32_SIZE);
+found:
        set_state_private(io_tree, start, csum);
 out:
        if (path)
                btrfs_free_path(path);
-       mutex_unlock(&root->fs_info->fs_mutex);
        return ret;
 }
 
@@ -426,9 +606,9 @@ struct io_failure_record {
        int last_mirror;
 };
 
-int btrfs_readpage_io_failed_hook(struct bio *failed_bio,
-                                 struct page *page, u64 start, u64 end,
-                                 struct extent_state *state)
+int btrfs_io_failed_hook(struct bio *failed_bio,
+                        struct page *page, u64 start, u64 end,
+                        struct extent_state *state)
 {
        struct io_failure_record *failrec = NULL;
        u64 private;
@@ -439,6 +619,7 @@ int btrfs_readpage_io_failed_hook(struct bio *failed_bio,
        struct bio *bio;
        int num_copies;
        int ret;
+       int rw;
        u64 logical;
 
        ret = get_state_private(failure_tree, start, &private);
@@ -501,7 +682,41 @@ int btrfs_readpage_io_failed_hook(struct bio *failed_bio,
        bio->bi_bdev = failed_bio->bi_bdev;
        bio->bi_size = 0;
        bio_add_page(bio, page, failrec->len, start - page_offset(page));
-       btrfs_submit_bio_hook(inode, READ, bio, failrec->last_mirror);
+       if (failed_bio->bi_rw & (1 << BIO_RW))
+               rw = WRITE;
+       else
+               rw = READ;
+
+       BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
+                                                     failrec->last_mirror);
+       return 0;
+}
+
+int btrfs_clean_io_failures(struct inode *inode, u64 start)
+{
+       u64 private;
+       u64 private_failure;
+       struct io_failure_record *failure;
+       int ret;
+
+       private = 0;
+       if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
+                            (u64)-1, 1, EXTENT_DIRTY)) {
+               ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
+                                       start, &private_failure);
+               if (ret == 0) {
+                       failure = (struct io_failure_record *)(unsigned long)
+                                  private_failure;
+                       set_state_private(&BTRFS_I(inode)->io_failure_tree,
+                                         failure->start, 0);
+                       clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
+                                         failure->start,
+                                         failure->start + failure->len - 1,
+                                         EXTENT_DIRTY | EXTENT_LOCKED,
+                                         GFP_NOFS);
+                       kfree(failure);
+               }
+       }
        return 0;
 }
 
@@ -543,26 +758,7 @@ int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
        /* if the io failure tree for this inode is non-empty,
         * check to see if we've recovered from a failed IO
         */
-       private = 0;
-       if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
-                            (u64)-1, 1, EXTENT_DIRTY)) {
-               u64 private_failure;
-               struct io_failure_record *failure;
-               ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
-                                       start, &private_failure);
-               if (ret == 0) {
-                       failure = (struct io_failure_record *)(unsigned long)
-                                  private_failure;
-                       set_state_private(&BTRFS_I(inode)->io_failure_tree,
-                                         failure->start, 0);
-                       clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
-                                         failure->start,
-                                         failure->start + failure->len - 1,
-                                         EXTENT_DIRTY | EXTENT_LOCKED,
-                                         GFP_NOFS);
-                       kfree(failure);
-               }
-       }
+       btrfs_clean_io_failures(inode, start);
        return 0;
 
 zeroit:
@@ -592,7 +788,6 @@ void btrfs_read_locked_inode(struct inode *inode)
 
        path = btrfs_alloc_path();
        BUG_ON(!path);
-       mutex_lock(&root->fs_info->fs_mutex);
        memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
 
        ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
@@ -607,7 +802,7 @@ void btrfs_read_locked_inode(struct inode *inode)
        inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
        inode->i_uid = btrfs_inode_uid(leaf, inode_item);
        inode->i_gid = btrfs_inode_gid(leaf, inode_item);
-       inode->i_size = btrfs_inode_size(leaf, inode_item);
+       btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
 
        tspec = btrfs_inode_atime(inode_item);
        inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
@@ -638,8 +833,6 @@ void btrfs_read_locked_inode(struct inode *inode)
        btrfs_free_path(path);
        inode_item = NULL;
 
-       mutex_unlock(&root->fs_info->fs_mutex);
-
        switch (inode->i_mode & S_IFMT) {
        case S_IFREG:
                inode->i_mapping->a_ops = &btrfs_aops;
@@ -667,9 +860,7 @@ void btrfs_read_locked_inode(struct inode *inode)
        return;
 
 make_bad:
-       btrfs_release_path(root, path);
        btrfs_free_path(path);
-       mutex_unlock(&root->fs_info->fs_mutex);
        make_bad_inode(inode);
 }
 
@@ -679,7 +870,7 @@ static void fill_inode_item(struct extent_buffer *leaf,
 {
        btrfs_set_inode_uid(leaf, item, inode->i_uid);
        btrfs_set_inode_gid(leaf, item, inode->i_gid);
-       btrfs_set_inode_size(leaf, item, inode->i_size);
+       btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
        btrfs_set_inode_mode(leaf, item, inode->i_mode);
        btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
 
@@ -706,7 +897,7 @@ static void fill_inode_item(struct extent_buffer *leaf,
                                    BTRFS_I(inode)->block_group->key.objectid);
 }
 
-int btrfs_update_inode(struct btrfs_trans_handle *trans,
+int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
                              struct btrfs_root *root,
                              struct inode *inode)
 {
@@ -734,7 +925,6 @@ int btrfs_update_inode(struct btrfs_trans_handle *trans,
        btrfs_set_inode_last_trans(trans, inode);
        ret = 0;
 failed:
-       btrfs_release_path(root, path);
        btrfs_free_path(path);
        return ret;
 }
@@ -787,6 +977,7 @@ static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
                goto err;
        }
        ret = btrfs_delete_one_dir_name(trans, root, path, di);
+       btrfs_release_path(root, path);
 
        dentry->d_inode->i_ctime = dir->i_ctime;
        ret = btrfs_del_inode_ref(trans, root, name, name_len,
@@ -801,7 +992,7 @@ static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
 err:
        btrfs_free_path(path);
        if (!ret) {
-               dir->i_size -= name_len * 2;
+               btrfs_i_size_write(dir, dir->i_size - name_len * 2);
                dir->i_mtime = dir->i_ctime = CURRENT_TIME;
                btrfs_update_inode(trans, root, dir);
 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
@@ -819,12 +1010,10 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
 {
        struct btrfs_root *root;
        struct btrfs_trans_handle *trans;
-       struct inode *inode = dentry->d_inode;
        int ret;
        unsigned long nr = 0;
 
        root = BTRFS_I(dir)->root;
-       mutex_lock(&root->fs_info->fs_mutex);
 
        ret = btrfs_check_free_space(root, 1, 1);
        if (ret)
@@ -836,23 +1025,9 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
        ret = btrfs_unlink_trans(trans, root, dir, dentry);
        nr = trans->blocks_used;
 
-       if (inode->i_nlink == 0) {
-               int found;
-               /* if the inode isn't linked anywhere,
-                * we don't need to worry about
-                * data=ordered
-                */
-               found = btrfs_del_ordered_inode(inode);
-               if (found == 1) {
-                       atomic_dec(&inode->i_count);
-               }
-       }
-
-       btrfs_end_transaction(trans, root);
+       btrfs_end_transaction_throttle(trans, root);
 fail:
-       mutex_unlock(&root->fs_info->fs_mutex);
        btrfs_btree_balance_dirty(root, nr);
-       btrfs_throttle(root);
        return ret;
 }
 
@@ -865,10 +1040,10 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
        struct btrfs_trans_handle *trans;
        unsigned long nr = 0;
 
-       if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
+       if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
                return -ENOTEMPTY;
+       }
 
-       mutex_lock(&root->fs_info->fs_mutex);
        ret = btrfs_check_free_space(root, 1, 1);
        if (ret)
                goto fail;
@@ -879,15 +1054,13 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
        /* now the directory is empty */
        err = btrfs_unlink_trans(trans, root, dir, dentry);
        if (!err) {
-               inode->i_size = 0;
+               btrfs_i_size_write(inode, 0);
        }
 
        nr = trans->blocks_used;
-       ret = btrfs_end_transaction(trans, root);
+       ret = btrfs_end_transaction_throttle(trans, root);
 fail:
-       mutex_unlock(&root->fs_info->fs_mutex);
        btrfs_btree_balance_dirty(root, nr);
-       btrfs_throttle(root);
 
        if (ret && !err)
                err = ret;
@@ -1107,40 +1280,11 @@ error:
                ret = btrfs_del_items(trans, root, path, pending_del_slot,
                                      pending_del_nr);
        }
-       btrfs_release_path(root, path);
        btrfs_free_path(path);
        inode->i_sb->s_dirt = 1;
        return ret;
 }
 
-static int btrfs_cow_one_page(struct inode *inode, struct page *page,
-                             size_t zero_start)
-{
-       char *kaddr;
-       struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
-       u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
-       u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
-       int ret = 0;
-
-       WARN_ON(!PageLocked(page));
-       set_page_extent_mapped(page);
-
-       lock_extent(io_tree, page_start, page_end, GFP_NOFS);
-       set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
-                           page_end, GFP_NOFS);
-
-       if (zero_start != PAGE_CACHE_SIZE) {
-               kaddr = kmap(page);
-               memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
-               flush_dcache_page(page);
-               kunmap(page);
-       }
-       set_page_dirty(page);
-       unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
-
-       return ret;
-}
-
 /*
  * taken from block_truncate_page, but does cow as it zeros out
  * any bytes left in the last page in the file.
@@ -1149,31 +1293,69 @@ static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
 {
        struct inode *inode = mapping->host;
        struct btrfs_root *root = BTRFS_I(inode)->root;
+       struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
+       struct btrfs_ordered_extent *ordered;
+       char *kaddr;
        u32 blocksize = root->sectorsize;
        pgoff_t index = from >> PAGE_CACHE_SHIFT;
        unsigned offset = from & (PAGE_CACHE_SIZE-1);
        struct page *page;
        int ret = 0;
        u64 page_start;
+       u64 page_end;
 
        if ((offset & (blocksize - 1)) == 0)
                goto out;
 
        ret = -ENOMEM;
+again:
        page = grab_cache_page(mapping, index);
        if (!page)
                goto out;
+
+       page_start = page_offset(page);
+       page_end = page_start + PAGE_CACHE_SIZE - 1;
+
        if (!PageUptodate(page)) {
                ret = btrfs_readpage(NULL, page);
                lock_page(page);
+               if (page->mapping != mapping) {
+                       unlock_page(page);
+                       page_cache_release(page);
+                       goto again;
+               }
                if (!PageUptodate(page)) {
                        ret = -EIO;
                        goto out;
                }
        }
-       page_start = (u64)page->index << PAGE_CACHE_SHIFT;
+       wait_on_page_writeback(page);
+
+       lock_extent(io_tree, page_start, page_end, GFP_NOFS);
+       set_page_extent_mapped(page);
+
+       ordered = btrfs_lookup_ordered_extent(inode, page_start);
+       if (ordered) {
+               unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
+               unlock_page(page);
+               page_cache_release(page);
+               btrfs_wait_ordered_extent(inode, ordered);
+               btrfs_put_ordered_extent(ordered);
+               goto again;
+       }
 
-       ret = btrfs_cow_one_page(inode, page, offset);
+       set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
+                           page_end, GFP_NOFS);
+       ret = 0;
+       if (offset != PAGE_CACHE_SIZE) {
+               kaddr = kmap(page);
+               memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
+               flush_dcache_page(page);
+               kunmap(page);
+       }
+       ClearPageChecked(page);
+       set_page_dirty(page);
+       unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
 
        unlock_page(page);
        page_cache_release(page);
@@ -1205,18 +1387,16 @@ static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
                if (attr->ia_size <= hole_start)
                        goto out;
 
-               mutex_lock(&root->fs_info->fs_mutex);
                err = btrfs_check_free_space(root, 1, 0);
-               mutex_unlock(&root->fs_info->fs_mutex);
                if (err)
                        goto fail;
 
                btrfs_truncate_page(inode->i_mapping, inode->i_size);
 
-               lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
                hole_size = block_end - hole_start;
+               btrfs_wait_ordered_range(inode, hole_start, hole_size);
+               lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
 
-               mutex_lock(&root->fs_info->fs_mutex);
                trans = btrfs_start_transaction(root, 1);
                btrfs_set_trans_block_group(trans, inode);
                err = btrfs_drop_extents(trans, root, inode,
@@ -1227,13 +1407,12 @@ static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
                        err = btrfs_insert_file_extent(trans, root,
                                                       inode->i_ino,
                                                       hole_start, 0, 0,
-                                                      hole_size);
+                                                      hole_size, 0);
                        btrfs_drop_extent_cache(inode, hole_start,
                                                (u64)-1);
                        btrfs_check_file(root, inode);
                }
                btrfs_end_transaction(trans, root);
-               mutex_unlock(&root->fs_info->fs_mutex);
                unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
                if (err)
                        return err;
@@ -1244,24 +1423,6 @@ fail:
        return err;
 }
 
-void btrfs_put_inode(struct inode *inode)
-{
-       int ret;
-
-       if (!BTRFS_I(inode)->ordered_trans) {
-               return;
-       }
-
-       if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
-           mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
-               return;
-
-       ret = btrfs_del_ordered_inode(inode);
-       if (ret == 1) {
-               atomic_dec(&inode->i_count);
-       }
-}
-
 void btrfs_delete_inode(struct inode *inode)
 {
        struct btrfs_trans_handle *trans;
@@ -1269,13 +1430,13 @@ void btrfs_delete_inode(struct inode *inode)
        unsigned long nr;
        int ret;
 
+       btrfs_wait_ordered_range(inode, 0, (u64)-1);
        truncate_inode_pages(&inode->i_data, 0);
        if (is_bad_inode(inode)) {
                goto no_delete;
        }
 
-       inode->i_size = 0;
-       mutex_lock(&root->fs_info->fs_mutex);
+       btrfs_i_size_write(inode, 0);
        trans = btrfs_start_transaction(root, 1);
 
        btrfs_set_trans_block_group(trans, inode);
@@ -1287,17 +1448,13 @@ void btrfs_delete_inode(struct inode *inode)
        clear_inode(inode);
 
        btrfs_end_transaction(trans, root);
-       mutex_unlock(&root->fs_info->fs_mutex);
        btrfs_btree_balance_dirty(root, nr);
-       btrfs_throttle(root);
        return;
 
 no_delete_lock:
        nr = trans->blocks_used;
        btrfs_end_transaction(trans, root);
-       mutex_unlock(&root->fs_info->fs_mutex);
        btrfs_btree_balance_dirty(root, nr);
-       btrfs_throttle(root);
 no_delete:
        clear_inode(inode);
 }
@@ -1391,7 +1548,6 @@ static int fixup_tree_root_location(struct btrfs_root *root,
 
        path = btrfs_alloc_path();
        BUG_ON(!path);
-       mutex_lock(&root->fs_info->fs_mutex);
 
        *sub_root = btrfs_read_fs_root(root->fs_info, location,
                                        dentry->d_name.name,
@@ -1405,7 +1561,6 @@ static int fixup_tree_root_location(struct btrfs_root *root,
        location->offset = 0;
 
        btrfs_free_path(path);
-       mutex_unlock(&root->fs_info->fs_mutex);
        return 0;
 }
 
@@ -1415,12 +1570,14 @@ static int btrfs_init_locked_inode(struct inode *inode, void *p)
        inode->i_ino = args->ino;
        BTRFS_I(inode)->root = args->root;
        BTRFS_I(inode)->delalloc_bytes = 0;
+       BTRFS_I(inode)->disk_i_size = 0;
        extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
        extent_io_tree_init(&BTRFS_I(inode)->io_tree,
                             inode->i_mapping, GFP_NOFS);
        extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
                             inode->i_mapping, GFP_NOFS);
-       atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
+       btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
+       mutex_init(&BTRFS_I(inode)->csum_mutex);
        return 0;
 }
 
@@ -1471,9 +1628,7 @@ static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
        if (dentry->d_name.len > BTRFS_NAME_LEN)
                return ERR_PTR(-ENAMETOOLONG);
 
-       mutex_lock(&root->fs_info->fs_mutex);
        ret = btrfs_inode_by_name(dir, dentry, &location);
-       mutex_unlock(&root->fs_info->fs_mutex);
 
        if (ret < 0)
                return ERR_PTR(ret);
@@ -1548,7 +1703,6 @@ static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
                filp->f_pos = 1;
        }
 
-       mutex_lock(&root->fs_info->fs_mutex);
        key.objectid = inode->i_ino;
        path = btrfs_alloc_path();
        path->reada = 2;
@@ -1657,9 +1811,7 @@ read_dir_items:
 nopos:
        ret = 0;
 err:
-       btrfs_release_path(root, path);
        btrfs_free_path(path);
-       mutex_unlock(&root->fs_info->fs_mutex);
        return ret;
 }
 
@@ -1670,11 +1822,9 @@ int btrfs_write_inode(struct inode *inode, int wait)
        int ret = 0;
 
        if (wait) {
-               mutex_lock(&root->fs_info->fs_mutex);
-               trans = btrfs_start_transaction(root, 1);
+               trans = btrfs_join_transaction(root, 1);
                btrfs_set_trans_block_group(trans, inode);
                ret = btrfs_commit_transaction(trans, root);
-               mutex_unlock(&root->fs_info->fs_mutex);
        }
        return ret;
 }
@@ -1690,12 +1840,10 @@ void btrfs_dirty_inode(struct inode *inode)
        struct btrfs_root *root = BTRFS_I(inode)->root;
        struct btrfs_trans_handle *trans;
 
-       mutex_lock(&root->fs_info->fs_mutex);
-       trans = btrfs_start_transaction(root, 1);
+       trans = btrfs_join_transaction(root, 1);
        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);
 }
 
 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
@@ -1730,8 +1878,10 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
                             inode->i_mapping, GFP_NOFS);
        extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
                             inode->i_mapping, GFP_NOFS);
-       atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
+       btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
+       mutex_init(&BTRFS_I(inode)->csum_mutex);
        BTRFS_I(inode)->delalloc_bytes = 0;
+       BTRFS_I(inode)->disk_i_size = 0;
        BTRFS_I(inode)->root = root;
 
        if (mode & S_IFDIR)
@@ -1827,7 +1977,8 @@ static int btrfs_add_link(struct btrfs_trans_handle *trans,
                                             dentry->d_parent->d_inode->i_ino);
                }
                parent_inode = dentry->d_parent->d_inode;
-               parent_inode->i_size += dentry->d_name.len * 2;
+               btrfs_i_size_write(parent_inode, parent_inode->i_size +
+                                  dentry->d_name.len * 2);
                parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
                ret = btrfs_update_inode(trans, root,
                                         dentry->d_parent->d_inode);
@@ -1863,7 +2014,6 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
        if (!new_valid_dev(rdev))
                return -EINVAL;
 
-       mutex_lock(&root->fs_info->fs_mutex);
        err = btrfs_check_free_space(root, 1, 0);
        if (err)
                goto fail;
@@ -1899,16 +2049,13 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
        btrfs_update_inode_block_group(trans, dir);
 out_unlock:
        nr = trans->blocks_used;
-       btrfs_end_transaction(trans, root);
+       btrfs_end_transaction_throttle(trans, root);
 fail:
-       mutex_unlock(&root->fs_info->fs_mutex);
-
        if (drop_inode) {
                inode_dec_link_count(inode);
                iput(inode);
        }
        btrfs_btree_balance_dirty(root, nr);
-       btrfs_throttle(root);
        return err;
 }
 
@@ -1923,7 +2070,6 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
        unsigned long nr = 0;
        u64 objectid;
 
-       mutex_lock(&root->fs_info->fs_mutex);
        err = btrfs_check_free_space(root, 1, 0);
        if (err)
                goto fail;
@@ -1958,25 +2104,24 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
                                     inode->i_mapping, GFP_NOFS);
                extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
                                     inode->i_mapping, GFP_NOFS);
+               mutex_init(&BTRFS_I(inode)->csum_mutex);
                BTRFS_I(inode)->delalloc_bytes = 0;
-               atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
+               BTRFS_I(inode)->disk_i_size = 0;
                BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
+               btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
        }
        dir->i_sb->s_dirt = 1;
        btrfs_update_inode_block_group(trans, inode);
        btrfs_update_inode_block_group(trans, dir);
 out_unlock:
        nr = trans->blocks_used;
-       btrfs_end_transaction(trans, root);
+       btrfs_end_transaction_throttle(trans, root);
 fail:
-       mutex_unlock(&root->fs_info->fs_mutex);
-
        if (drop_inode) {
                inode_dec_link_count(inode);
                iput(inode);
        }
        btrfs_btree_balance_dirty(root, nr);
-       btrfs_throttle(root);
        return err;
 }
 
@@ -1998,7 +2143,6 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
 #else
        inc_nlink(inode);
 #endif
-       mutex_lock(&root->fs_info->fs_mutex);
        err = btrfs_check_free_space(root, 1, 0);
        if (err)
                goto fail;
@@ -2019,30 +2163,26 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
                drop_inode = 1;
 
        nr = trans->blocks_used;
-       btrfs_end_transaction(trans, root);
+       btrfs_end_transaction_throttle(trans, root);
 fail:
-       mutex_unlock(&root->fs_info->fs_mutex);
-
        if (drop_inode) {
                inode_dec_link_count(inode);
                iput(inode);
        }
        btrfs_btree_balance_dirty(root, nr);
-       btrfs_throttle(root);
        return err;
 }
 
 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
 {
-       struct inode *inode;
+       struct inode *inode = NULL;
        struct btrfs_trans_handle *trans;
        struct btrfs_root *root = BTRFS_I(dir)->root;
        int err = 0;
        int drop_on_err = 0;
-       u64 objectid;
+       u64 objectid = 0;
        unsigned long nr = 1;
 
-       mutex_lock(&root->fs_info->fs_mutex);
        err = btrfs_check_free_space(root, 1, 0);
        if (err)
                goto out_unlock;
@@ -2075,7 +2215,7 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
        inode->i_fop = &btrfs_dir_file_operations;
        btrfs_set_trans_block_group(trans, inode);
 
-       inode->i_size = 0;
+       btrfs_i_size_write(inode, 0);
        err = btrfs_update_inode(trans, root, inode);
        if (err)
                goto out_fail;
@@ -2092,78 +2232,30 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
 
 out_fail:
        nr = trans->blocks_used;
-       btrfs_end_transaction(trans, root);
+       btrfs_end_transaction_throttle(trans, root);
 
 out_unlock:
-       mutex_unlock(&root->fs_info->fs_mutex);
        if (drop_on_err)
                iput(inode);
        btrfs_btree_balance_dirty(root, nr);
-       btrfs_throttle(root);
        return err;
 }
 
 static int merge_extent_mapping(struct extent_map_tree *em_tree,
                                struct extent_map *existing,
-                               struct extent_map *em)
+                               struct extent_map *em,
+                               u64 map_start, u64 map_len)
 {
        u64 start_diff;
-       u64 new_end;
-       int ret = 0;
-       int real_blocks = existing->block_start < EXTENT_MAP_LAST_BYTE;
-
-       if (real_blocks && em->block_start >= EXTENT_MAP_LAST_BYTE)
-               goto invalid;
-
-       if (!real_blocks && em->block_start != existing->block_start)
-               goto invalid;
-
-       new_end = max(existing->start + existing->len, em->start + em->len);
-
-       if (existing->start >= em->start) {
-               if (em->start + em->len < existing->start)
-                       goto invalid;
-
-               start_diff = existing->start - em->start;
-               if (real_blocks && em->block_start + start_diff !=
-                   existing->block_start)
-                       goto invalid;
-
-               em->len = new_end - em->start;
-
-               remove_extent_mapping(em_tree, existing);
-               /* free for the tree */
-               free_extent_map(existing);
-               ret = add_extent_mapping(em_tree, em);
 
-       } else if (em->start > existing->start) {
-
-               if (existing->start + existing->len < em->start)
-                       goto invalid;
-
-               start_diff = em->start - existing->start;
-               if (real_blocks && existing->block_start + start_diff !=
-                   em->block_start)
-                       goto invalid;
-
-               remove_extent_mapping(em_tree, existing);
-               em->block_start = existing->block_start;
-               em->start = existing->start;
-               em->len = new_end - existing->start;
-               free_extent_map(existing);
-
-               ret = add_extent_mapping(em_tree, em);
-       } else {
-               goto invalid;
-       }
-       return ret;
-
-invalid:
-       printk("invalid extent map merge [%Lu %Lu %Lu] [%Lu %Lu %Lu]\n",
-              existing->start, existing->len, existing->block_start,
-              em->start, em->len, em->block_start);
-       return -EIO;
-}
+       BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
+       start_diff = map_start - em->start;
+       em->start = map_start;
+       em->len = map_len;
+       if (em->block_start < EXTENT_MAP_LAST_BYTE)
+               em->block_start += start_diff;
+       return add_extent_mapping(em_tree, em);
+}
 
 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
                                    size_t pg_offset, u64 start, u64 len,
@@ -2188,11 +2280,12 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
 
        path = btrfs_alloc_path();
        BUG_ON(!path);
-       mutex_lock(&root->fs_info->fs_mutex);
 
 again:
        spin_lock(&em_tree->lock);
        em = lookup_extent_mapping(em_tree, start, len);
+       if (em)
+               em->bdev = root->fs_info->fs_devices->latest_bdev;
        spin_unlock(&em_tree->lock);
 
        if (em) {
@@ -2208,10 +2301,9 @@ again:
                err = -ENOMEM;
                goto out;
        }
-
+       em->bdev = root->fs_info->fs_devices->latest_bdev;
        em->start = EXTENT_MAP_HOLE;
        em->len = (u64)-1;
-       em->bdev = inode->i_sb->s_bdev;
        ret = btrfs_lookup_file_extent(trans, root, path,
                                       objectid, start, trans != NULL);
        if (ret < 0) {
@@ -2315,7 +2407,7 @@ again:
                                free_extent_map(em);
                                em = NULL;
                                btrfs_release_path(root, path);
-                               trans = btrfs_start_transaction(root, 1);
+                               trans = btrfs_join_transaction(root, 1);
                                goto again;
                        }
                        write_extent_buffer(leaf, map + pg_offset, ptr,
@@ -2352,6 +2444,9 @@ insert:
         */
        if (ret == -EEXIST) {
                struct extent_map *existing;
+
+               ret = 0;
+
                existing = lookup_extent_mapping(em_tree, start, len);
                if (existing && (existing->start > start ||
                    existing->start + existing->len <= start)) {
@@ -2363,7 +2458,8 @@ insert:
                                                         em->len);
                        if (existing) {
                                err = merge_extent_mapping(em_tree, existing,
-                                                          em);
+                                                          em, start,
+                                                          root->sectorsize);
                                free_extent_map(existing);
                                if (err) {
                                        free_extent_map(em);
@@ -2379,6 +2475,7 @@ insert:
                } else {
                        free_extent_map(em);
                        em = existing;
+                       err = 0;
                }
        }
        spin_unlock(&em_tree->lock);
@@ -2386,10 +2483,10 @@ out:
        btrfs_free_path(path);
        if (trans) {
                ret = btrfs_end_transaction(trans, root);
-               if (!err)
+               if (!err) {
                        err = ret;
+               }
        }
-       mutex_unlock(&root->fs_info->fs_mutex);
        if (err) {
                free_extent_map(em);
                WARN_ON(1);
@@ -2513,8 +2610,7 @@ btrfs_readpages(struct file *file, struct address_space *mapping,
        return extent_readpages(tree, mapping, pages, nr_pages,
                                btrfs_get_extent);
 }
-
-static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
+static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
 {
        struct extent_io_tree *tree;
        struct extent_map_tree *map;
@@ -2532,15 +2628,46 @@ static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
        return ret;
 }
 
+static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
+{
+       return __btrfs_releasepage(page, gfp_flags);
+}
+
 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
 {
        struct extent_io_tree *tree;
+       struct btrfs_ordered_extent *ordered;
+       u64 page_start = page_offset(page);
+       u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
 
+       wait_on_page_writeback(page);
        tree = &BTRFS_I(page->mapping->host)->io_tree;
-       extent_invalidatepage(tree, page, offset);
-       btrfs_releasepage(page, GFP_NOFS);
+       if (offset) {
+               btrfs_releasepage(page, GFP_NOFS);
+               return;
+       }
+
+       lock_extent(tree, page_start, page_end, GFP_NOFS);
+       ordered = btrfs_lookup_ordered_extent(page->mapping->host,
+                                          page_offset(page));
+       if (ordered) {
+               clear_extent_bit(tree, page_start, page_end,
+                                EXTENT_DIRTY | EXTENT_DELALLOC |
+                                EXTENT_LOCKED, 1, 0, GFP_NOFS);
+               btrfs_writepage_end_io_hook(page, page_start,
+                                           page_end, NULL, 1);
+               btrfs_put_ordered_extent(ordered);
+               lock_extent(tree, page_start, page_end, GFP_NOFS);
+       }
+       clear_extent_bit(tree, page_start, page_end,
+                EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
+                EXTENT_ORDERED,
+                1, 1, GFP_NOFS);
+       __btrfs_releasepage(page, GFP_NOFS);
+
        if (PagePrivate(page)) {
-               invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
+               invalidate_extent_lru(tree, page_offset(page),
+                                     PAGE_CACHE_SIZE);
                ClearPagePrivate(page);
                set_page_private(page, 0);
                page_cache_release(page);
@@ -2566,37 +2693,64 @@ int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
 {
        struct inode *inode = fdentry(vma->vm_file)->d_inode;
        struct btrfs_root *root = BTRFS_I(inode)->root;
-       unsigned long end;
+       struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
+       struct btrfs_ordered_extent *ordered;
+       char *kaddr;
+       unsigned long zero_start;
        loff_t size;
        int ret;
        u64 page_start;
+       u64 page_end;
 
-       mutex_lock(&root->fs_info->fs_mutex);
        ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
-       mutex_unlock(&root->fs_info->fs_mutex);
        if (ret)
                goto out;
 
        ret = -EINVAL;
-
+again:
        lock_page(page);
-       wait_on_page_writeback(page);
        size = i_size_read(inode);
-       page_start = (u64)page->index << PAGE_CACHE_SHIFT;
+       page_start = page_offset(page);
+       page_end = page_start + PAGE_CACHE_SIZE - 1;
 
        if ((page->mapping != inode->i_mapping) ||
-           (page_start > size)) {
+           (page_start >= size)) {
                /* page got truncated out from underneath us */
                goto out_unlock;
        }
+       wait_on_page_writeback(page);
+
+       lock_extent(io_tree, page_start, page_end, GFP_NOFS);
+       set_page_extent_mapped(page);
+
+       ordered = btrfs_lookup_ordered_extent(inode, page_start);
+       if (ordered) {
+               unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
+               unlock_page(page);
+               btrfs_wait_ordered_extent(inode, ordered);
+               btrfs_put_ordered_extent(ordered);
+               goto again;
+       }
+
+       set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
+                           page_end, GFP_NOFS);
+       ret = 0;
 
        /* page is wholly or partially inside EOF */
        if (page_start + PAGE_CACHE_SIZE > size)
-               end = size & ~PAGE_CACHE_MASK;
+               zero_start = size & ~PAGE_CACHE_MASK;
        else
-               end = PAGE_CACHE_SIZE;
+               zero_start = PAGE_CACHE_SIZE;
 
-       ret = btrfs_cow_one_page(inode, page, end);
+       if (zero_start != PAGE_CACHE_SIZE) {
+               kaddr = kmap(page);
+               memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
+               flush_dcache_page(page);
+               kunmap(page);
+       }
+       ClearPageChecked(page);
+       set_page_dirty(page);
+       unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
 
 out_unlock:
        unlock_page(page);
@@ -2610,6 +2764,7 @@ static void btrfs_truncate(struct inode *inode)
        int ret;
        struct btrfs_trans_handle *trans;
        unsigned long nr;
+       u64 mask = root->sectorsize - 1;
 
        if (!S_ISREG(inode->i_mode))
                return;
@@ -2618,9 +2773,10 @@ static void btrfs_truncate(struct inode *inode)
 
        btrfs_truncate_page(inode->i_mapping, inode->i_size);
 
-       mutex_lock(&root->fs_info->fs_mutex);
        trans = btrfs_start_transaction(root, 1);
        btrfs_set_trans_block_group(trans, inode);
+       btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
+       btrfs_i_size_write(inode, inode->i_size);
 
        /* FIXME, add redo link to tree so we don't leak on crash */
        ret = btrfs_truncate_in_trans(trans, root, inode,
@@ -2628,121 +2784,47 @@ static void btrfs_truncate(struct inode *inode)
        btrfs_update_inode(trans, root, inode);
        nr = trans->blocks_used;
 
-       ret = btrfs_end_transaction(trans, root);
+       ret = btrfs_end_transaction_throttle(trans, root);
        BUG_ON(ret);
-       mutex_unlock(&root->fs_info->fs_mutex);
        btrfs_btree_balance_dirty(root, nr);
-       btrfs_throttle(root);
 }
 
-static int noinline create_subvol(struct btrfs_root *root, char *name,
+/*
+ * Invalidate a single dcache entry at the root of the filesystem.
+ * Needed after creation of snapshot or subvolume.
+ */
+void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
                                  int namelen)
 {
-       struct btrfs_trans_handle *trans;
-       struct btrfs_key key;
-       struct btrfs_root_item root_item;
-       struct btrfs_inode_item *inode_item;
-       struct extent_buffer *leaf;
-       struct btrfs_root *new_root = root;
+       struct dentry *alias, *entry;
+       struct qstr qstr;
+
+       alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
+       if (alias) {
+               qstr.name = name;
+               qstr.len = namelen;
+               /* change me if btrfs ever gets a d_hash operation */
+               qstr.hash = full_name_hash(qstr.name, qstr.len);
+               entry = d_lookup(alias, &qstr);
+               dput(alias);
+               if (entry) {
+                       d_invalidate(entry);
+                       dput(entry);
+               }
+       }
+}
+
+int btrfs_create_subvol_root(struct btrfs_root *new_root,
+               struct btrfs_trans_handle *trans, u64 new_dirid,
+               struct btrfs_block_group_cache *block_group)
+{
        struct inode *inode;
-       struct inode *dir;
        int ret;
-       int err;
-       u64 objectid;
-       u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
-       unsigned long nr = 1;
-
-       mutex_lock(&root->fs_info->fs_mutex);
-       ret = btrfs_check_free_space(root, 1, 0);
-       if (ret)
-               goto fail_commit;
-
-       trans = btrfs_start_transaction(root, 1);
-       BUG_ON(!trans);
-
-       ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
-                                      0, &objectid);
-       if (ret)
-               goto fail;
-
-       leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
-                                       objectid, trans->transid, 0, 0,
-                                       0, 0);
-       if (IS_ERR(leaf))
-               return PTR_ERR(leaf);
-
-       btrfs_set_header_nritems(leaf, 0);
-       btrfs_set_header_level(leaf, 0);
-       btrfs_set_header_bytenr(leaf, leaf->start);
-       btrfs_set_header_generation(leaf, trans->transid);
-       btrfs_set_header_owner(leaf, objectid);
-
-       write_extent_buffer(leaf, root->fs_info->fsid,
-                           (unsigned long)btrfs_header_fsid(leaf),
-                           BTRFS_FSID_SIZE);
-       btrfs_mark_buffer_dirty(leaf);
-
-       inode_item = &root_item.inode;
-       memset(inode_item, 0, sizeof(*inode_item));
-       inode_item->generation = cpu_to_le64(1);
-       inode_item->size = cpu_to_le64(3);
-       inode_item->nlink = cpu_to_le32(1);
-       inode_item->nblocks = cpu_to_le64(1);
-       inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
-
-       btrfs_set_root_bytenr(&root_item, leaf->start);
-       btrfs_set_root_level(&root_item, 0);
-       btrfs_set_root_refs(&root_item, 1);
-       btrfs_set_root_used(&root_item, 0);
-
-       memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
-       root_item.drop_level = 0;
-
-       free_extent_buffer(leaf);
-       leaf = NULL;
-
-       btrfs_set_root_dirid(&root_item, new_dirid);
-
-       key.objectid = objectid;
-       key.offset = 1;
-       btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
-       ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
-                               &root_item);
-       if (ret)
-               goto fail;
-
-       /*
-        * 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, dir->i_ino, &key,
-                                   BTRFS_FT_DIR);
-       if (ret)
-               goto fail;
-
-       ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
-                            name, namelen, objectid,
-                            root->fs_info->sb->s_root->d_inode->i_ino);
-       if (ret)
-               goto fail;
-
-       ret = btrfs_commit_transaction(trans, root);
-       if (ret)
-               goto fail_commit;
-
-       new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
-       BUG_ON(!new_root);
-
-       trans = btrfs_start_transaction(new_root, 1);
-       BUG_ON(!trans);
 
        inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
-                               new_dirid,
-                               BTRFS_I(dir)->block_group, S_IFDIR | 0700);
+                               new_dirid, block_group, S_IFDIR | 0700);
        if (IS_ERR(inode))
-               goto fail;
+               return PTR_ERR(inode);
        inode->i_op = &btrfs_dir_inode_operations;
        inode->i_fop = &btrfs_dir_file_operations;
        new_root->inode = inode;
@@ -2750,64 +2832,9 @@ static int noinline create_subvol(struct btrfs_root *root, char *name,
        ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
                                     new_dirid);
        inode->i_nlink = 1;
-       inode->i_size = 0;
-       ret = btrfs_update_inode(trans, new_root, inode);
-       if (ret)
-               goto fail;
-fail:
-       nr = trans->blocks_used;
-       err = btrfs_commit_transaction(trans, new_root);
-       if (err && !ret)
-               ret = err;
-fail_commit:
-       mutex_unlock(&root->fs_info->fs_mutex);
-       btrfs_btree_balance_dirty(root, nr);
-       btrfs_throttle(root);
-       return ret;
-}
+       btrfs_i_size_write(inode, 0);
 
-static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
-{
-       struct btrfs_pending_snapshot *pending_snapshot;
-       struct btrfs_trans_handle *trans;
-       int ret;
-       int err;
-       unsigned long nr = 0;
-
-       if (!root->ref_cows)
-               return -EINVAL;
-
-       mutex_lock(&root->fs_info->fs_mutex);
-       ret = btrfs_check_free_space(root, 1, 0);
-       if (ret)
-               goto fail_unlock;
-
-       pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
-       if (!pending_snapshot) {
-               ret = -ENOMEM;
-               goto fail_unlock;
-       }
-       pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
-       if (!pending_snapshot->name) {
-               ret = -ENOMEM;
-               kfree(pending_snapshot);
-               goto fail_unlock;
-       }
-       memcpy(pending_snapshot->name, name, namelen);
-       pending_snapshot->name[namelen] = '\0';
-       trans = btrfs_start_transaction(root, 1);
-       BUG_ON(!trans);
-       pending_snapshot->root = root;
-       list_add(&pending_snapshot->list,
-                &trans->transaction->pending_snapshots);
-       ret = btrfs_update_inode(trans, root, root->inode);
-       err = btrfs_commit_transaction(trans, root);
-
-fail_unlock:
-       mutex_unlock(&root->fs_info->fs_mutex);
-       btrfs_btree_balance_dirty(root, nr);
-       btrfs_throttle(root);
-       return ret;
+       return btrfs_update_inode(trans, new_root, inode);
 }
 
 unsigned long btrfs_force_ra(struct address_space *mapping,
@@ -2825,294 +2852,6 @@ unsigned long btrfs_force_ra(struct address_space *mapping,
 #endif
 }
 
-int btrfs_defrag_file(struct file *file) {
-       struct inode *inode = fdentry(file)->d_inode;
-       struct btrfs_root *root = BTRFS_I(inode)->root;
-       struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
-       struct page *page;
-       unsigned long last_index;
-       unsigned long ra_pages = root->fs_info->bdi.ra_pages;
-       unsigned long total_read = 0;
-       u64 page_start;
-       u64 page_end;
-       unsigned long i;
-       int ret;
-
-       mutex_lock(&root->fs_info->fs_mutex);
-       ret = btrfs_check_free_space(root, inode->i_size, 0);
-       mutex_unlock(&root->fs_info->fs_mutex);
-       if (ret)
-               return -ENOSPC;
-
-       mutex_lock(&inode->i_mutex);
-       last_index = inode->i_size >> PAGE_CACHE_SHIFT;
-       for (i = 0; i <= last_index; i++) {
-               if (total_read % ra_pages == 0) {
-                       btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
-                                      min(last_index, i + ra_pages - 1));
-               }
-               total_read++;
-               page = grab_cache_page(inode->i_mapping, i);
-               if (!page)
-                       goto out_unlock;
-               if (!PageUptodate(page)) {
-                       btrfs_readpage(NULL, page);
-                       lock_page(page);
-                       if (!PageUptodate(page)) {
-                               unlock_page(page);
-                               page_cache_release(page);
-                               goto out_unlock;
-                       }
-               }
-               page_start = (u64)page->index << PAGE_CACHE_SHIFT;
-               page_end = page_start + PAGE_CACHE_SIZE - 1;
-
-               lock_extent(io_tree, page_start, page_end, GFP_NOFS);
-               set_extent_delalloc(io_tree, page_start,
-                                   page_end, GFP_NOFS);
-
-               unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
-               set_page_dirty(page);
-               unlock_page(page);
-               page_cache_release(page);
-               balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
-       }
-
-out_unlock:
-       mutex_unlock(&inode->i_mutex);
-       return 0;
-}
-
-static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
-{
-       u64 new_size;
-       u64 old_size;
-       u64 devid = 1;
-       struct btrfs_ioctl_vol_args *vol_args;
-       struct btrfs_trans_handle *trans;
-       struct btrfs_device *device = NULL;
-       char *sizestr;
-       char *devstr = NULL;
-       int ret = 0;
-       int namelen;
-       int mod = 0;
-
-       vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
-
-       if (!vol_args)
-               return -ENOMEM;
-
-       if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
-               ret = -EFAULT;
-               goto out;
-       }
-       namelen = strlen(vol_args->name);
-       if (namelen > BTRFS_VOL_NAME_MAX) {
-               ret = -EINVAL;
-               goto out;
-       }
-
-       mutex_lock(&root->fs_info->fs_mutex);
-       sizestr = vol_args->name;
-       devstr = strchr(sizestr, ':');
-       if (devstr) {
-               char *end;
-               sizestr = devstr + 1;
-               *devstr = '\0';
-               devstr = vol_args->name;
-               devid = simple_strtoull(devstr, &end, 10);
-printk("resizing devid %Lu\n", devid);
-       }
-       device = btrfs_find_device(root, devid, NULL);
-       if (!device) {
-               printk("resizer unable to find device %Lu\n", devid);
-               ret = -EINVAL;
-               goto out_unlock;
-       }
-       if (!strcmp(sizestr, "max"))
-               new_size = device->bdev->bd_inode->i_size;
-       else {
-               if (sizestr[0] == '-') {
-                       mod = -1;
-                       sizestr++;
-               } else if (sizestr[0] == '+') {
-                       mod = 1;
-                       sizestr++;
-               }
-               new_size = btrfs_parse_size(sizestr);
-               if (new_size == 0) {
-                       ret = -EINVAL;
-                       goto out_unlock;
-               }
-       }
-
-       old_size = device->total_bytes;
-
-       if (mod < 0) {
-               if (new_size > old_size) {
-                       ret = -EINVAL;
-                       goto out_unlock;
-               }
-               new_size = old_size - new_size;
-       } else if (mod > 0) {
-               new_size = old_size + new_size;
-       }
-
-       if (new_size < 256 * 1024 * 1024) {
-               ret = -EINVAL;
-               goto out_unlock;
-       }
-       if (new_size > device->bdev->bd_inode->i_size) {
-               ret = -EFBIG;
-               goto out_unlock;
-       }
-
-       do_div(new_size, root->sectorsize);
-       new_size *= root->sectorsize;
-
-printk("new size for %s is %llu\n", device->name, (unsigned long long)new_size);
-
-       if (new_size > old_size) {
-               trans = btrfs_start_transaction(root, 1);
-               ret = btrfs_grow_device(trans, device, new_size);
-               btrfs_commit_transaction(trans, root);
-       } else {
-               ret = btrfs_shrink_device(device, new_size);
-       }
-
-out_unlock:
-       mutex_unlock(&root->fs_info->fs_mutex);
-out:
-       kfree(vol_args);
-       return ret;
-}
-
-static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
-                                           void __user *arg)
-{
-       struct btrfs_ioctl_vol_args *vol_args;
-       struct btrfs_dir_item *di;
-       struct btrfs_path *path;
-       u64 root_dirid;
-       int namelen;
-       int ret;
-
-       vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
-
-       if (!vol_args)
-               return -ENOMEM;
-
-       if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
-               ret = -EFAULT;
-               goto out;
-       }
-
-       namelen = strlen(vol_args->name);
-       if (namelen > BTRFS_VOL_NAME_MAX) {
-               ret = -EINVAL;
-               goto out;
-       }
-       if (strchr(vol_args->name, '/')) {
-               ret = -EINVAL;
-               goto out;
-       }
-
-       path = btrfs_alloc_path();
-       if (!path) {
-               ret = -ENOMEM;
-               goto out;
-       }
-
-       root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
-       mutex_lock(&root->fs_info->fs_mutex);
-       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 (di && !IS_ERR(di)) {
-               ret = -EEXIST;
-               goto out;
-       }
-
-       if (IS_ERR(di)) {
-               ret = PTR_ERR(di);
-               goto out;
-       }
-
-       if (root == root->fs_info->tree_root)
-               ret = create_subvol(root, vol_args->name, namelen);
-       else
-               ret = create_snapshot(root, vol_args->name, namelen);
-out:
-       kfree(vol_args);
-       return ret;
-}
-
-static int btrfs_ioctl_defrag(struct file *file)
-{
-       struct inode *inode = fdentry(file)->d_inode;
-       struct btrfs_root *root = BTRFS_I(inode)->root;
-
-       switch (inode->i_mode & S_IFMT) {
-       case S_IFDIR:
-               mutex_lock(&root->fs_info->fs_mutex);
-               btrfs_defrag_root(root, 0);
-               btrfs_defrag_root(root->fs_info->extent_root, 0);
-               mutex_unlock(&root->fs_info->fs_mutex);
-               break;
-       case S_IFREG:
-               btrfs_defrag_file(file);
-               break;
-       }
-
-       return 0;
-}
-
-long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
-{
-       struct btrfs_ioctl_vol_args *vol_args;
-       int ret;
-
-       vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
-
-       if (!vol_args)
-               return -ENOMEM;
-
-       if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
-               ret = -EFAULT;
-               goto out;
-       }
-       ret = btrfs_init_new_device(root, vol_args->name);
-
-out:
-       kfree(vol_args);
-       return ret;
-}
-
-long btrfs_ioctl(struct file *file, unsigned int
-               cmd, unsigned long arg)
-{
-       struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
-
-       switch (cmd) {
-       case BTRFS_IOC_SNAP_CREATE:
-               return btrfs_ioctl_snap_create(root, (void __user *)arg);
-       case BTRFS_IOC_DEFRAG:
-               return btrfs_ioctl_defrag(file);
-       case BTRFS_IOC_RESIZE:
-               return btrfs_ioctl_resize(root, (void __user *)arg);
-       case BTRFS_IOC_ADD_DEV:
-               return btrfs_ioctl_add_dev(root, (void __user *)arg);
-       }
-
-       return -ENOTTY;
-}
-
-/*
- * Called inside transaction, so use GFP_NOFS
- */
 struct inode *btrfs_alloc_inode(struct super_block *sb)
 {
        struct btrfs_inode *ei;
@@ -3121,15 +2860,28 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
        if (!ei)
                return NULL;
        ei->last_trans = 0;
-       ei->ordered_trans = 0;
+       btrfs_ordered_inode_tree_init(&ei->ordered_tree);
        return &ei->vfs_inode;
 }
 
 void btrfs_destroy_inode(struct inode *inode)
 {
+       struct btrfs_ordered_extent *ordered;
        WARN_ON(!list_empty(&inode->i_dentry));
        WARN_ON(inode->i_data.nrpages);
 
+       while(1) {
+               ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
+               if (!ordered)
+                       break;
+               else {
+                       printk("found ordered extent %Lu %Lu\n",
+                              ordered->file_offset, ordered->len);
+                       btrfs_remove_ordered_extent(inode, ordered);
+                       btrfs_put_ordered_extent(ordered);
+                       btrfs_put_ordered_extent(ordered);
+               }
+       }
        btrfs_drop_extent_cache(inode, 0, (u64)-1);
        kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
 }
@@ -3229,7 +2981,6 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
        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;
        int ret;
 
        if (S_ISDIR(old_inode->i_mode) && new_inode &&
@@ -3237,7 +2988,6 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
                return -ENOTEMPTY;
        }
 
-       mutex_lock(&root->fs_info->fs_mutex);
        ret = btrfs_check_free_space(root, 1, 0);
        if (ret)
                goto out_unlock;
@@ -3245,11 +2995,6 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
        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;
@@ -3271,10 +3016,8 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
                goto out_fail;
 
 out_fail:
-       btrfs_free_path(path);
        btrfs_end_transaction(trans, root);
 out_unlock:
-       mutex_unlock(&root->fs_info->fs_mutex);
        return ret;
 }
 
@@ -3300,7 +3043,6 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
        if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
                return -ENAMETOOLONG;
 
-       mutex_lock(&root->fs_info->fs_mutex);
        err = btrfs_check_free_space(root, 1, 0);
        if (err)
                goto out_fail;
@@ -3336,9 +3078,11 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
                                     inode->i_mapping, GFP_NOFS);
                extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
                                     inode->i_mapping, GFP_NOFS);
+               mutex_init(&BTRFS_I(inode)->csum_mutex);
                BTRFS_I(inode)->delalloc_bytes = 0;
-               atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
+               BTRFS_I(inode)->disk_i_size = 0;
                BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
+               btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
        }
        dir->i_sb->s_dirt = 1;
        btrfs_update_inode_block_group(trans, inode);
@@ -3372,25 +3116,28 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
        inode->i_op = &btrfs_symlink_inode_operations;
        inode->i_mapping->a_ops = &btrfs_symlink_aops;
        inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
-       inode->i_size = name_len - 1;
+       btrfs_i_size_write(inode, name_len - 1);
        err = btrfs_update_inode(trans, root, inode);
        if (err)
                drop_inode = 1;
 
 out_unlock:
        nr = trans->blocks_used;
-       btrfs_end_transaction(trans, root);
+       btrfs_end_transaction_throttle(trans, root);
 out_fail:
-       mutex_unlock(&root->fs_info->fs_mutex);
        if (drop_inode) {
                inode_dec_link_count(inode);
                iput(inode);
        }
        btrfs_btree_balance_dirty(root, nr);
-       btrfs_throttle(root);
        return err;
 }
 
+static int btrfs_set_page_dirty(struct page *page)
+{
+       return __set_page_dirty_nobuffers(page);
+}
+
 static int btrfs_permission(struct inode *inode, int mask,
                            struct nameidata *nd)
 {
@@ -3428,6 +3175,7 @@ static struct file_operations btrfs_dir_file_operations = {
 #ifdef CONFIG_COMPAT
        .compat_ioctl   = btrfs_ioctl,
 #endif
+       .release        = btrfs_release_file,
 };
 
 static struct extent_io_ops btrfs_extent_io_ops = {
@@ -3436,7 +3184,9 @@ static struct extent_io_ops btrfs_extent_io_ops = {
        .merge_bio_hook = btrfs_merge_bio_hook,
        .readpage_io_hook = btrfs_readpage_io_hook,
        .readpage_end_io_hook = btrfs_readpage_end_io_hook,
-       .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
+       .writepage_end_io_hook = btrfs_writepage_end_io_hook,
+       .writepage_start_hook = btrfs_writepage_start_hook,
+       .readpage_io_failed_hook = btrfs_io_failed_hook,
        .set_bit_hook = btrfs_set_bit_hook,
        .clear_bit_hook = btrfs_clear_bit_hook,
 };
@@ -3451,7 +3201,7 @@ static struct address_space_operations btrfs_aops = {
        .direct_IO      = btrfs_direct_IO,
        .invalidatepage = btrfs_invalidatepage,
        .releasepage    = btrfs_releasepage,
-       .set_page_dirty = __set_page_dirty_nobuffers,
+       .set_page_dirty = btrfs_set_page_dirty,
 };
 
 static struct address_space_operations btrfs_symlink_aops = {