]> Pileus Git - ~andy/linux/blobdiff - fs/btrfs/inode.c
Btrfs: Search data ordered extents first for checksums on read
[~andy/linux] / fs / btrfs / inode.c
index 5b1514e58de4ade6757cbfbc746c3b266670f94b..0e90315ea803288a37816fe8a73655adb126c066 100644 (file)
  * Boston, MA 021110-1307, USA.
  */
 
+#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>
@@ -41,6 +43,7 @@
 #include "ioctl.h"
 #include "print-tree.h"
 #include "volumes.h"
+#include "ordered-data.h"
 
 struct btrfs_iget_args {
        u64 ino;
@@ -77,11 +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
@@ -89,10 +96,9 @@ int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
 
        do_div(thresh, 100);
 
-       spin_lock(&root->fs_info->delalloc_lock);
        if (used + root->fs_info->delalloc_bytes + num_required > thresh)
                ret = -ENOSPC;
-       spin_unlock(&root->fs_info->delalloc_lock);
+       spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
        return ret;
 }
 
@@ -104,44 +110,61 @@ 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));
+       mutex_lock(&BTRFS_I(inode)->extent_mutex);
+       btrfs_drop_extent_cache(inode, start, start + num_bytes - 1);
+       mutex_unlock(&BTRFS_I(inode)->extent_mutex);
 
        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;
+               mutex_lock(&BTRFS_I(inode)->extent_mutex);
+               set_bit(EXTENT_FLAG_PINNED, &em->flags);
+               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);
+               }
+               mutex_unlock(&BTRFS_I(inode)->extent_mutex);
+
                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);
@@ -151,10 +174,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;
@@ -169,6 +188,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;
@@ -203,9 +223,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;
@@ -227,16 +246,19 @@ 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)
-                       goto not_found;
-
-               if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
+               block_group = btrfs_lookup_block_group(root->fs_info,
+                                                      bytenr);
+               if (!block_group || block_group->ro)
                        goto not_found;
-               }
 
                start = extent_end;
        } else {
@@ -252,8 +274,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;
 }
 
@@ -261,26 +283,26 @@ 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;
 }
 
 int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
                       unsigned long old, unsigned long bits)
 {
+       unsigned long flags;
        if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
                struct btrfs_root *root = BTRFS_I(inode)->root;
-               spin_lock(&root->fs_info->delalloc_lock);
+               spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
                BTRFS_I(inode)->delalloc_bytes += end - start + 1;
                root->fs_info->delalloc_bytes += end - start + 1;
-               spin_unlock(&root->fs_info->delalloc_lock);
+               spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
        }
        return 0;
 }
@@ -290,7 +312,9 @@ int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
 {
        if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
                struct btrfs_root *root = BTRFS_I(inode)->root;
-               spin_lock(&root->fs_info->delalloc_lock);
+               unsigned long flags;
+
+               spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
                if (end - start + 1 > root->fs_info->delalloc_bytes) {
                        printk("warning: delalloc account %Lu %Lu\n",
                               end - start + 1, root->fs_info->delalloc_bytes);
@@ -300,7 +324,7 @@ int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
                        root->fs_info->delalloc_bytes -= end - start + 1;
                        BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
                }
-               spin_unlock(&root->fs_info->delalloc_lock);
+               spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
        }
        return 0;
 }
@@ -313,13 +337,9 @@ int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
        u64 logical = bio->bi_sector << 9;
        u64 length = 0;
        u64 map_length;
-       struct bio_vec *bvec;
-       int i;
        int ret;
 
-       bio_for_each_segment(bvec, bio, i) {
-               length += bvec->bv_len;
-       }
+       length = bio->bi_size;
        map_tree = &root->fs_info->mapping_tree;
        map_length = length;
        ret = btrfs_map_block(map_tree, READ, logical,
@@ -335,26 +355,12 @@ 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;
-
-       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_csum_one_bio(root, inode, bio);
        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,
@@ -363,14 +369,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;
        }
 
@@ -378,7 +380,221 @@ 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;
+again:
+       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);
+
+       /* already ordered? We're done */
+       if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
+                            EXTENT_ORDERED, 0)) {
+               goto out;
+       }
+
+       ordered = btrfs_lookup_ordered_extent(inode, page_start);
+       if (ordered) {
+               unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
+                             page_end, GFP_NOFS);
+               unlock_page(page);
+               btrfs_start_ordered_extent(inode, ordered, 1);
+               goto again;
+       }
+
+       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;
+
+       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;
+}
+
+static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
+{
+       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;
+       struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
+       struct extent_map *em;
+       struct extent_map *em_orig;
+       u64 alloc_hint = 0;
+       u64 clear_start;
+       u64 clear_end;
+       struct list_head list;
+       struct btrfs_key ins;
+       struct rb_node *rb;
+       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);
+
+       mutex_lock(&BTRFS_I(inode)->extent_mutex);
+
+       spin_lock(&em_tree->lock);
+       clear_start = ordered_extent->file_offset;
+       clear_end = ordered_extent->file_offset + ordered_extent->len;
+       em = lookup_extent_mapping(em_tree, clear_start,
+                                  ordered_extent->len);
+       em_orig = em;
+       while(em && clear_start < extent_map_end(em) && clear_end > em->start) {
+               clear_bit(EXTENT_FLAG_PINNED, &em->flags);
+               rb = rb_next(&em->rb_node);
+               if (!rb)
+                       break;
+               em = rb_entry(rb, struct extent_map, rb_node);
+       }
+       free_extent_map(em_orig);
+       spin_unlock(&em_tree->lock);
+
+       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);
+       mutex_unlock(&BTRFS_I(inode)->extent_mutex);
+
+       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_writepage_end_io_hook(struct page *page, u64 start, u64 end,
+                               struct extent_state *state, int uptodate)
+{
+       return btrfs_finish_ordered_io(page->mapping->host, start, end);
 }
 
 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
@@ -395,7 +611,22 @@ 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);
+       /*
+        * 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.  We have to do this before looking in the
+        * btree because csum items are pre-inserted based on
+        * the file size.  btrfs_lookup_csum might find an item
+        * that still hasn't been fully filled.
+        */
+       ret = btrfs_find_ordered_sum(inode, start, &csum);
+       if (ret == 0)
+               goto found;
+
+       ret = 0;
        path = btrfs_alloc_path();
        item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
        if (IS_ERR(item)) {
@@ -404,16 +635,17 @@ int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
                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;
 }
 
@@ -425,9 +657,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;
@@ -438,6 +670,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);
@@ -498,8 +731,43 @@ int btrfs_readpage_io_failed_hook(struct bio *failed_bio,
        bio->bi_end_io = failed_bio->bi_end_io;
        bio->bi_sector = failrec->logical >> 9;
        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;
 }
 
@@ -541,26 +809,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:
@@ -590,7 +839,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);
@@ -605,7 +853,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);
@@ -636,8 +884,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;
@@ -665,9 +911,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);
 }
 
@@ -677,7 +921,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);
 
@@ -704,7 +948,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)
 {
@@ -732,7 +976,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;
 }
@@ -785,6 +1028,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,
@@ -799,7 +1043,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)
@@ -817,12 +1061,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)
@@ -834,23 +1076,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;
 }
 
@@ -863,10 +1091,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;
@@ -877,15 +1105,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;
@@ -1105,40 +1331,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.
@@ -1147,32 +1344,71 @@ 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;
+                       goto out_unlock;
                }
        }
-       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_start_ordered_extent(inode, ordered, 1);
+               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);
 
+out_unlock:
        unlock_page(page);
        page_cache_release(page);
 out:
@@ -1203,20 +1439,19 @@ 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);
+               mutex_lock(&BTRFS_I(inode)->extent_mutex);
                err = btrfs_drop_extents(trans, root, inode,
                                         hole_start, block_end, hole_start,
                                         &alloc_hint);
@@ -1225,13 +1460,13 @@ 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);
                }
+               mutex_unlock(&BTRFS_I(inode)->extent_mutex);
                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;
@@ -1242,39 +1477,21 @@ fail:
        return err;
 }
 
-void btrfs_put_inode(struct inode *inode)
+void btrfs_delete_inode(struct inode *inode)
 {
+       struct btrfs_trans_handle *trans;
+       struct btrfs_root *root = BTRFS_I(inode)->root;
+       unsigned long nr;
        int ret;
 
-       if (!BTRFS_I(inode)->ordered_trans) {
-               return;
+       truncate_inode_pages(&inode->i_data, 0);
+       if (is_bad_inode(inode)) {
+               goto no_delete;
        }
+       btrfs_wait_ordered_range(inode, 0, (u64)-1);
 
-       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;
-       struct btrfs_root *root = BTRFS_I(inode)->root;
-       unsigned long nr;
-       int ret;
-
-       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);
-       trans = btrfs_start_transaction(root, 1);
+       btrfs_i_size_write(inode, 0);
+       trans = btrfs_start_transaction(root, 1);
 
        btrfs_set_trans_block_group(trans, inode);
        ret = btrfs_truncate_in_trans(trans, root, inode, 0);
@@ -1285,17 +1502,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);
 }
@@ -1389,7 +1602,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,
@@ -1403,7 +1615,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;
 }
 
@@ -1413,11 +1624,15 @@ 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);
+       btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
+       mutex_init(&BTRFS_I(inode)->csum_mutex);
+       mutex_init(&BTRFS_I(inode)->extent_mutex);
        return 0;
 }
 
@@ -1468,9 +1683,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);
@@ -1545,7 +1758,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;
@@ -1654,9 +1866,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;
 }
 
@@ -1667,11 +1877,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;
 }
@@ -1687,12 +1895,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,
@@ -1727,7 +1933,11 @@ 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);
+       btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
+       mutex_init(&BTRFS_I(inode)->csum_mutex);
+       mutex_init(&BTRFS_I(inode)->extent_mutex);
        BTRFS_I(inode)->delalloc_bytes = 0;
+       BTRFS_I(inode)->disk_i_size = 0;
        BTRFS_I(inode)->root = root;
 
        if (mode & S_IFDIR)
@@ -1823,7 +2033,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);
@@ -1859,7 +2070,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;
@@ -1895,16 +2105,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;
 }
 
@@ -1919,7 +2126,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;
@@ -1954,24 +2160,25 @@ 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);
+               mutex_init(&BTRFS_I(inode)->extent_mutex);
                BTRFS_I(inode)->delalloc_bytes = 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;
 }
 
@@ -1993,7 +2200,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;
@@ -2014,30 +2220,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;
@@ -2070,7 +2272,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;
@@ -2087,77 +2289,29 @@ 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,
@@ -2171,7 +2325,7 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
        u64 extent_end = 0;
        u64 objectid = inode->i_ino;
        u32 found_type;
-       struct btrfs_path *path;
+       struct btrfs_path *path = NULL;
        struct btrfs_root *root = BTRFS_I(inode)->root;
        struct btrfs_file_extent_item *item;
        struct extent_buffer *leaf;
@@ -2181,22 +2335,17 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
        struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
        struct btrfs_trans_handle *trans = NULL;
 
-       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) {
-               if (em->start > start) {
-                       printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
-                              start, len, em->start, em->len);
-                       WARN_ON(1);
-               }
-               if (em->block_start == EXTENT_MAP_INLINE && page)
+               if (em->start > start || em->start + em->len <= start)
+                       free_extent_map(em);
+               else if (em->block_start == EXTENT_MAP_INLINE && page)
                        free_extent_map(em);
                else
                        goto out;
@@ -2206,10 +2355,15 @@ 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;
+
+       if (!path) {
+               path = btrfs_alloc_path();
+               BUG_ON(!path);
+       }
+
        ret = btrfs_lookup_file_extent(trans, root, path,
                                       objectid, start, trans != NULL);
        if (ret < 0) {
@@ -2313,7 +2467,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,
@@ -2344,20 +2498,28 @@ insert:
        err = 0;
        spin_lock(&em_tree->lock);
        ret = add_extent_mapping(em_tree, em);
-
        /* it is possible that someone inserted the extent into the tree
         * while we had the lock dropped.  It is also possible that
         * an overlapping map exists in the tree
         */
        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)) {
+                       free_extent_map(existing);
+                       existing = NULL;
+               }
                if (!existing) {
                        existing = lookup_extent_mapping(em_tree, em->start,
                                                         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);
@@ -2373,17 +2535,19 @@ insert:
                } else {
                        free_extent_map(em);
                        em = existing;
+                       err = 0;
                }
        }
        spin_unlock(&em_tree->lock);
 out:
-       btrfs_free_path(path);
+       if (path)
+               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);
@@ -2392,6 +2556,7 @@ out:
        return em;
 }
 
+#if 0 /* waiting for O_DIRECT reads */
 static int btrfs_get_block(struct inode *inode, sector_t iblock,
                        struct buffer_head *bh_result, int create)
 {
@@ -2409,22 +2574,24 @@ static int btrfs_get_block(struct inode *inode, sector_t iblock,
        if (!em || IS_ERR(em))
                goto out;
 
-       if (em->start > start || em->start + em->len <= start)
+       if (em->start > start || em->start + em->len <= start) {
            goto out;
+       }
 
        if (em->block_start == EXTENT_MAP_INLINE) {
                ret = -EINVAL;
                goto out;
        }
 
+       len = em->start + em->len - start;
+       len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
+
        if (em->block_start == EXTENT_MAP_HOLE ||
            em->block_start == EXTENT_MAP_DELALLOC) {
+               bh_result->b_size = len;
                goto out;
        }
 
-       len = em->start + em->len - start;
-       len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
-
        logical = start - em->start;
        logical = em->block_start + logical;
 
@@ -2434,6 +2601,7 @@ static int btrfs_get_block(struct inode *inode, sector_t iblock,
        BUG_ON(ret);
        bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
        bh_result->b_size = min(map_length, len);
+
        bh_result->b_bdev = multi->stripes[0].dev->bdev;
        set_buffer_mapped(bh_result);
        kfree(multi);
@@ -2441,11 +2609,14 @@ out:
        free_extent_map(em);
        return ret;
 }
+#endif
 
 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
                        const struct iovec *iov, loff_t offset,
                        unsigned long nr_segs)
 {
+       return -EINVAL;
+#if 0
        struct file *file = iocb->ki_filp;
        struct inode *inode = file->f_mapping->host;
 
@@ -2454,6 +2625,7 @@ static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
 
        return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
                                  offset, nr_segs, btrfs_get_block, NULL);
+#endif
 }
 
 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
@@ -2482,8 +2654,8 @@ static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
        return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
 }
 
-static int btrfs_writepages(struct address_space *mapping,
-                           struct writeback_control *wbc)
+int btrfs_writepages(struct address_space *mapping,
+                    struct writeback_control *wbc)
 {
        struct extent_io_tree *tree;
        tree = &BTRFS_I(mapping->host)->io_tree;
@@ -2499,8 +2671,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;
@@ -2510,7 +2681,6 @@ static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
        map = &BTRFS_I(page->mapping->host)->extent_tree;
        ret = try_release_extent_mapping(map, tree, page, gfp_flags);
        if (ret == 1) {
-               invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
                ClearPagePrivate(page);
                set_page_private(page, 0);
                page_cache_release(page);
@@ -2518,15 +2688,49 @@ 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) {
+               /*
+                * IO on this page will never be started, so we need
+                * to account for any ordered extents now
+                */
+               clear_extent_bit(tree, page_start, page_end,
+                                EXTENT_DIRTY | EXTENT_DELALLOC |
+                                EXTENT_LOCKED, 1, 0, GFP_NOFS);
+               btrfs_finish_ordered_io(page->mapping->host,
+                                       page_start, page_end);
+               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);
+
+       ClearPageChecked(page);
        if (PagePrivate(page)) {
-               invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
                ClearPagePrivate(page);
                set_page_private(page, 0);
                page_cache_release(page);
@@ -2552,37 +2756,68 @@ 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);
+
+       /*
+        * we can't set the delalloc bits if there are pending ordered
+        * extents.  Drop our locks and wait for them to finish
+        */
+       ordered = btrfs_lookup_ordered_extent(inode, page_start);
+       if (ordered) {
+               unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
+               unlock_page(page);
+               btrfs_start_ordered_extent(inode, ordered, 1);
+               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);
@@ -2596,6 +2831,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;
@@ -2603,10 +2839,11 @@ static void btrfs_truncate(struct inode *inode)
                return;
 
        btrfs_truncate_page(inode->i_mapping, inode->i_size);
+       btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
 
-       mutex_lock(&root->fs_info->fs_mutex);
        trans = btrfs_start_transaction(root, 1);
        btrfs_set_trans_block_group(trans, inode);
+       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,
@@ -2614,121 +2851,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;
@@ -2736,328 +2899,26 @@ 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;
-}
-
-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;
+       btrfs_i_size_write(inode, 0);
 
-       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,
                              struct file_ra_state *ra, struct file *file,
                              pgoff_t offset, pgoff_t last_index)
 {
-       pgoff_t req_size;
+       pgoff_t req_size = last_index - offset + 1;
 
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
-       req_size = last_index - offset + 1;
        offset = page_cache_readahead(mapping, ra, file, offset, req_size);
        return offset;
 #else
-       req_size = min(last_index - offset + 1, (pgoff_t)128);
        page_cache_sync_readahead(mapping, ra, file, offset, req_size);
        return offset + req_size;
 #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_index = 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 (i == ra_index) {
-                       ra_index = btrfs_force_ra(inode->i_mapping,
-                                                 &file->f_ra,
-                                                 file, ra_index, last_index);
-               }
-               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;
-       struct btrfs_ioctl_vol_args *vol_args;
-       struct btrfs_trans_handle *trans;
-       char *sizestr;
-       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;
-       }
-
-       sizestr = vol_args->name;
-       if (!strcmp(sizestr, "max"))
-               new_size = root->fs_info->sb->s_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;
-               }
-       }
-
-       mutex_lock(&root->fs_info->fs_mutex);
-       old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
-
-       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 > root->fs_info->sb->s_bdev->bd_inode->i_size) {
-               ret = -EFBIG;
-               goto out_unlock;
-       }
-
-       do_div(new_size, root->sectorsize);
-       new_size *= root->sectorsize;
-
-printk("new size is %Lu\n", new_size);
-       if (new_size > old_size) {
-               trans = btrfs_start_transaction(root, 1);
-               ret = btrfs_grow_extent_tree(trans, root, new_size);
-               btrfs_commit_transaction(trans, root);
-       } else {
-               ret = btrfs_shrink_extent_tree(root, 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(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);
-       }
-
-       return -ENOTTY;
-}
-
-/*
- * Called inside transaction, so use GFP_NOFS
- */
 struct inode *btrfs_alloc_inode(struct super_block *sb)
 {
        struct btrfs_inode *ei;
@@ -3066,15 +2927,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));
 }
@@ -3174,7 +3048,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 &&
@@ -3182,7 +3055,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;
@@ -3190,11 +3062,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;
@@ -3216,10 +3083,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;
 }
 
@@ -3245,7 +3110,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;
@@ -3281,8 +3145,12 @@ 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);
+               mutex_init(&BTRFS_I(inode)->extent_mutex);
                BTRFS_I(inode)->delalloc_bytes = 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);
@@ -3316,25 +3184,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)
 {
@@ -3372,6 +3243,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 = {
@@ -3380,7 +3252,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,
 };
@@ -3395,7 +3269,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 = {