Btrfs: Create a work queue for bio writes
[safe/jmp/linux-2.6] / fs / btrfs / disk-io.c
index 8e37fa1..9e41ea9 100644 (file)
 
 #include <linux/fs.h>
 #include <linux/blkdev.h>
-#include <linux/crc32c.h>
 #include <linux/scatterlist.h>
 #include <linux/swap.h>
 #include <linux/radix-tree.h>
 #include <linux/writeback.h>
 #include <linux/buffer_head.h> // for block_sync_page
+#include <linux/workqueue.h>
+#include "crc32c.h"
 #include "ctree.h"
 #include "disk-io.h"
 #include "transaction.h"
@@ -45,27 +46,27 @@ static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
 #endif
 
 static struct extent_io_ops btree_extent_io_ops;
+static struct workqueue_struct *end_io_workqueue;
+static struct workqueue_struct *async_submit_workqueue;
+
+struct end_io_wq {
+       struct bio *bio;
+       bio_end_io_t *end_io;
+       void *private;
+       struct btrfs_fs_info *info;
+       int error;
+       int metadata;
+       struct list_head list;
+};
 
-struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
-                                           u64 bytenr, u32 blocksize)
-{
-       struct inode *btree_inode = root->fs_info->btree_inode;
-       struct extent_buffer *eb;
-       eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
-                               bytenr, blocksize, GFP_NOFS);
-       return eb;
-}
-
-struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
-                                                u64 bytenr, u32 blocksize)
-{
-       struct inode *btree_inode = root->fs_info->btree_inode;
-       struct extent_buffer *eb;
-
-       eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
-                                bytenr, blocksize, NULL, GFP_NOFS);
-       return eb;
-}
+struct async_submit_bio {
+       struct inode *inode;
+       struct bio *bio;
+       struct list_head list;
+       extent_submit_bio_hook_t *submit_bio_hook;
+       int rw;
+       int mirror_num;
+};
 
 struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
                                    size_t page_offset, u64 start, u64 len,
@@ -109,7 +110,7 @@ out:
 
 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
 {
-       return crc32c(seed, data, len);
+       return btrfs_crc32c(seed, data, len);
 }
 
 void btrfs_csum_final(u32 crc, char *result)
@@ -159,17 +160,18 @@ static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
                        from_this_trans = 1;
 
                /* FIXME, this is not good */
-               if (from_this_trans == 0 &&
-                   memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
+               if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
                        u32 val;
                        u32 found = 0;
                        memcpy(&found, result, BTRFS_CRC32_SIZE);
 
                        read_extent_buffer(buf, &val, 0, BTRFS_CRC32_SIZE);
                        printk("btrfs: %s checksum verify failed on %llu "
-                              "wanted %X found %X from_this_trans %d\n",
+                              "wanted %X found %X from_this_trans %d "
+                              "level %d\n",
                               root->fs_info->sb->s_id,
-                              buf->start, val, found, from_this_trans);
+                              buf->start, val, found, from_this_trans,
+                              btrfs_header_level(buf));
                        return 1;
                }
        } else {
@@ -178,6 +180,40 @@ static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
        return 0;
 }
 
+static int btree_read_extent_buffer_pages(struct btrfs_root *root,
+                                         struct extent_buffer *eb,
+                                         u64 start)
+{
+       struct extent_io_tree *io_tree;
+       int ret;
+       int num_copies = 0;
+       int mirror_num = 0;
+
+       io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
+       while (1) {
+               ret = read_extent_buffer_pages(io_tree, eb, start, 1,
+                                              btree_get_extent, mirror_num);
+               if (!ret) {
+                       if (mirror_num)
+printk("good read %Lu mirror %d total %d\n", eb->start, mirror_num, num_copies);
+                       return ret;
+               }
+               num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
+                                             eb->start, eb->len);
+printk("failed to read %Lu mirror %d total %d\n", eb->start, mirror_num, num_copies);
+               if (num_copies == 1) {
+printk("reading %Lu failed only one copy\n", eb->start);
+                       return ret;
+               }
+               mirror_num++;
+               if (mirror_num > num_copies) {
+printk("bailing at mirror %d of %d\n", mirror_num, num_copies);
+                       return ret;
+               }
+       }
+printk("read extent buffer page last\n");
+       return -EIO;
+}
 
 int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
 {
@@ -187,6 +223,8 @@ int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
        int found_level;
        unsigned long len;
        struct extent_buffer *eb;
+       int ret;
+
        tree = &BTRFS_I(page->mapping->host)->io_tree;
 
        if (page->private == EXTENT_PAGE_PRIVATE)
@@ -198,8 +236,8 @@ int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
                WARN_ON(1);
        }
        eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
-       read_extent_buffer_pages(tree, eb, start + PAGE_CACHE_SIZE, 1,
-                                btree_get_extent);
+       ret = btree_read_extent_buffer_pages(root, eb, start + PAGE_CACHE_SIZE);
+       BUG_ON(ret);
        btrfs_clear_buffer_defrag(eb);
        found_start = btrfs_header_bytenr(eb);
        if (found_start != start) {
@@ -220,6 +258,9 @@ int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
                goto err;
        }
        found_level = btrfs_header_level(eb);
+       spin_lock(&root->fs_info->hash_lock);
+       btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
+       spin_unlock(&root->fs_info->hash_lock);
        csum_tree_block(root, eb, 0);
 err:
        free_extent_buffer(eb);
@@ -235,17 +276,162 @@ static int btree_writepage_io_hook(struct page *page, u64 start, u64 end)
        return 0;
 }
 
-static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio)
+int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end,
+                              struct extent_state *state)
+{
+       struct extent_io_tree *tree;
+       u64 found_start;
+       int found_level;
+       unsigned long len;
+       struct extent_buffer *eb;
+       struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
+       int ret = 0;
+
+       tree = &BTRFS_I(page->mapping->host)->io_tree;
+       if (page->private == EXTENT_PAGE_PRIVATE)
+               goto out;
+       if (!page->private)
+               goto out;
+       len = page->private >> 2;
+       if (len == 0) {
+               WARN_ON(1);
+       }
+       eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
+
+       btrfs_clear_buffer_defrag(eb);
+       found_start = btrfs_header_bytenr(eb);
+       if (found_start != start) {
+printk("bad start on %Lu found %Lu\n", eb->start, found_start);
+               ret = -EIO;
+               goto err;
+       }
+       if (eb->first_page != page) {
+               printk("bad first page %lu %lu\n", eb->first_page->index,
+                      page->index);
+               WARN_ON(1);
+               ret = -EIO;
+               goto err;
+       }
+       found_level = btrfs_header_level(eb);
+
+       ret = csum_tree_block(root, eb, 1);
+       if (ret)
+               ret = -EIO;
+
+       end = min_t(u64, eb->len, PAGE_CACHE_SIZE);
+       end = eb->start + end - 1;
+       release_extent_buffer_tail_pages(eb);
+err:
+       free_extent_buffer(eb);
+out:
+       return ret;
+}
+
+#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
+static void end_workqueue_bio(struct bio *bio, int err)
+#else
+static int end_workqueue_bio(struct bio *bio,
+                                  unsigned int bytes_done, int err)
+#endif
+{
+       struct end_io_wq *end_io_wq = bio->bi_private;
+       struct btrfs_fs_info *fs_info;
+       unsigned long flags;
+
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
+       if (bio->bi_size)
+               return 1;
+#endif
+
+       fs_info = end_io_wq->info;
+       spin_lock_irqsave(&fs_info->end_io_work_lock, flags);
+       end_io_wq->error = err;
+       list_add_tail(&end_io_wq->list, &fs_info->end_io_work_list);
+       spin_unlock_irqrestore(&fs_info->end_io_work_lock, flags);
+       queue_work(end_io_workqueue, &fs_info->end_io_work);
+
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
+       return 0;
+#endif
+}
+
+int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
+                       int metadata)
+{
+       struct end_io_wq *end_io_wq;
+       end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS);
+       if (!end_io_wq)
+               return -ENOMEM;
+
+       end_io_wq->private = bio->bi_private;
+       end_io_wq->end_io = bio->bi_end_io;
+       end_io_wq->info = info;
+       end_io_wq->error = 0;
+       end_io_wq->bio = bio;
+       end_io_wq->metadata = metadata;
+
+       bio->bi_private = end_io_wq;
+       bio->bi_end_io = end_workqueue_bio;
+       return 0;
+}
+
+int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode,
+                       int rw, struct bio *bio, int mirror_num,
+                       extent_submit_bio_hook_t *submit_bio_hook)
+{
+       struct async_submit_bio *async;
+
+       async = kmalloc(sizeof(*async), GFP_NOFS);
+       if (!async)
+               return -ENOMEM;
+
+       async->inode = inode;
+       async->rw = rw;
+       async->bio = bio;
+       async->mirror_num = mirror_num;
+       async->submit_bio_hook = submit_bio_hook;
+
+       spin_lock(&fs_info->async_submit_work_lock);
+       list_add_tail(&async->list, &fs_info->async_submit_work_list);
+       spin_unlock(&fs_info->async_submit_work_lock);
+
+       queue_work(async_submit_workqueue, &fs_info->async_submit_work);
+       return 0;
+}
+
+static int __btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
+                                int mirror_num)
 {
        struct btrfs_root *root = BTRFS_I(inode)->root;
        u64 offset;
+       int ret;
+
        offset = bio->bi_sector << 9;
+
+       if (rw & (1 << BIO_RW)) {
+               return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num);
+       }
+
+       ret = btrfs_bio_wq_end_io(root->fs_info, bio, 1);
+       BUG_ON(ret);
+
        if (offset == BTRFS_SUPER_INFO_OFFSET) {
-               bio->bi_bdev = root->fs_info->sb->s_bdev;
+               bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
                submit_bio(rw, bio);
                return 0;
        }
-       return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio);
+       return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num);
+}
+
+static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
+                                int mirror_num)
+{
+       if (!(rw & (1 << BIO_RW))) {
+               return __btree_submit_bio_hook(inode, rw, bio, mirror_num);
+       }
+       return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
+                                  inode, rw, bio, mirror_num,
+                                  __btree_submit_bio_hook);
 }
 
 static int btree_writepage(struct page *page, struct writeback_control *wbc)
@@ -295,10 +481,17 @@ static int btree_releasepage(struct page *page, gfp_t gfp_flags)
        struct extent_map_tree *map;
        int ret;
 
+       if (page_count(page) > 3) {
+               /* once for page->private, once for the caller, once
+                * once for the page cache
+                */
+               return 0;
+       }
        tree = &BTRFS_I(page->mapping->host)->io_tree;
        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);
@@ -354,7 +547,7 @@ int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
        if (!buf)
                return 0;
        read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
-                                buf, 0, 0, btree_get_extent);
+                                buf, 0, 0, btree_get_extent, 0);
        free_extent_buffer(buf);
        return ret;
 }
@@ -365,24 +558,50 @@ static int close_all_devices(struct btrfs_fs_info *fs_info)
        struct list_head *next;
        struct btrfs_device *device;
 
-       list = &fs_info->devices;
-       while(!list_empty(list)) {
-               next = list->next;
-               list_del(next);
+       list = &fs_info->fs_devices->devices;
+       list_for_each(next, list) {
                device = list_entry(next, struct btrfs_device, dev_list);
-               kfree(device->name);
-               kfree(device);
+               if (device->bdev && device->bdev != fs_info->sb->s_bdev)
+                       close_bdev_excl(device->bdev);
+               device->bdev = NULL;
        }
        return 0;
 }
 
+int btrfs_verify_block_csum(struct btrfs_root *root,
+                           struct extent_buffer *buf)
+{
+       return btrfs_buffer_uptodate(buf);
+}
+
+struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
+                                           u64 bytenr, u32 blocksize)
+{
+       struct inode *btree_inode = root->fs_info->btree_inode;
+       struct extent_buffer *eb;
+       eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
+                               bytenr, blocksize, GFP_NOFS);
+       return eb;
+}
+
+struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
+                                                u64 bytenr, u32 blocksize)
+{
+       struct inode *btree_inode = root->fs_info->btree_inode;
+       struct extent_buffer *eb;
+
+       eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
+                                bytenr, blocksize, NULL, GFP_NOFS);
+       return eb;
+}
+
+
 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
                                      u32 blocksize)
 {
        struct extent_buffer *buf = NULL;
        struct inode *btree_inode = root->fs_info->btree_inode;
        struct extent_io_tree *io_tree;
-       u64 end;
        int ret;
 
        io_tree = &BTRFS_I(btree_inode)->io_tree;
@@ -390,32 +609,14 @@ struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
        buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
        if (!buf)
                return NULL;
-       read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, buf, 0, 1,
-                                btree_get_extent);
-
-       if (buf->flags & EXTENT_CSUM)
-               return buf;
 
-       end = buf->start + PAGE_CACHE_SIZE - 1;
-       if (test_range_bit(io_tree, buf->start, end, EXTENT_CSUM, 1)) {
-               buf->flags |= EXTENT_CSUM;
-               return buf;
-       }
-
-       lock_extent(io_tree, buf->start, end, GFP_NOFS);
+       ret = btree_read_extent_buffer_pages(root, buf, 0);
 
-       if (test_range_bit(io_tree, buf->start, end, EXTENT_CSUM, 1)) {
-               buf->flags |= EXTENT_CSUM;
-               goto out_unlock;
+       if (ret == 0) {
+               buf->flags |= EXTENT_UPTODATE;
        }
-
-       ret = csum_tree_block(root, buf, 1);
-       set_extent_bits(io_tree, buf->start, end, EXTENT_CSUM, GFP_NOFS);
-       buf->flags |= EXTENT_CSUM;
-
-out_unlock:
-       unlock_extent(io_tree, buf->start, end, GFP_NOFS);
        return buf;
+
 }
 
 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
@@ -656,7 +857,188 @@ static int add_hasher(struct btrfs_fs_info *info, char *type) {
        return 0;
 }
 #endif
-struct btrfs_root *open_ctree(struct super_block *sb)
+
+static int btrfs_congested_fn(void *congested_data, int bdi_bits)
+{
+       struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
+       int ret = 0;
+       struct list_head *cur;
+       struct btrfs_device *device;
+       struct backing_dev_info *bdi;
+
+       list_for_each(cur, &info->fs_devices->devices) {
+               device = list_entry(cur, struct btrfs_device, dev_list);
+               bdi = blk_get_backing_dev_info(device->bdev);
+               if (bdi && bdi_congested(bdi, bdi_bits)) {
+                       ret = 1;
+                       break;
+               }
+       }
+       return ret;
+}
+
+void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
+{
+       struct list_head *cur;
+       struct btrfs_device *device;
+       struct btrfs_fs_info *info;
+
+       info = (struct btrfs_fs_info *)bdi->unplug_io_data;
+       list_for_each(cur, &info->fs_devices->devices) {
+               device = list_entry(cur, struct btrfs_device, dev_list);
+               bdi = blk_get_backing_dev_info(device->bdev);
+               if (bdi->unplug_io_fn) {
+                       bdi->unplug_io_fn(bdi, page);
+               }
+       }
+}
+
+static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
+{
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
+       bdi_init(bdi);
+#endif
+       bdi->ra_pages   = default_backing_dev_info.ra_pages * 4;
+       bdi->state              = 0;
+       bdi->capabilities       = default_backing_dev_info.capabilities;
+       bdi->unplug_io_fn       = btrfs_unplug_io_fn;
+       bdi->unplug_io_data     = info;
+       bdi->congested_fn       = btrfs_congested_fn;
+       bdi->congested_data     = info;
+       return 0;
+}
+
+static int bio_ready_for_csum(struct bio *bio)
+{
+       u64 length = 0;
+       u64 buf_len = 0;
+       u64 start = 0;
+       struct page *page;
+       struct extent_io_tree *io_tree = NULL;
+       struct btrfs_fs_info *info = NULL;
+       struct bio_vec *bvec;
+       int i;
+       int ret;
+
+       bio_for_each_segment(bvec, bio, i) {
+               page = bvec->bv_page;
+               if (page->private == EXTENT_PAGE_PRIVATE) {
+                       length += bvec->bv_len;
+                       continue;
+               }
+               if (!page->private) {
+                       length += bvec->bv_len;
+                       continue;
+               }
+               length = bvec->bv_len;
+               buf_len = page->private >> 2;
+               start = page_offset(page) + bvec->bv_offset;
+               io_tree = &BTRFS_I(page->mapping->host)->io_tree;
+               info = BTRFS_I(page->mapping->host)->root->fs_info;
+       }
+       /* are we fully contained in this bio? */
+       if (buf_len <= length)
+               return 1;
+
+       ret = extent_range_uptodate(io_tree, start + length,
+                                   start + buf_len - 1);
+       if (ret == 1)
+               return ret;
+       return ret;
+}
+
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
+static void btrfs_end_io_csum(void *p)
+#else
+static void btrfs_end_io_csum(struct work_struct *work)
+#endif
+{
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
+       struct btrfs_fs_info *fs_info = p;
+#else
+       struct btrfs_fs_info *fs_info = container_of(work,
+                                                    struct btrfs_fs_info,
+                                                    end_io_work);
+#endif
+       unsigned long flags;
+       struct end_io_wq *end_io_wq;
+       struct bio *bio;
+       struct list_head *next;
+       int error;
+       int was_empty;
+
+       while(1) {
+               spin_lock_irqsave(&fs_info->end_io_work_lock, flags);
+               if (list_empty(&fs_info->end_io_work_list)) {
+                       spin_unlock_irqrestore(&fs_info->end_io_work_lock,
+                                              flags);
+                       return;
+               }
+               next = fs_info->end_io_work_list.next;
+               list_del(next);
+               spin_unlock_irqrestore(&fs_info->end_io_work_lock, flags);
+
+               end_io_wq = list_entry(next, struct end_io_wq, list);
+
+               bio = end_io_wq->bio;
+               if (end_io_wq->metadata && !bio_ready_for_csum(bio)) {
+                       spin_lock_irqsave(&fs_info->end_io_work_lock, flags);
+                       was_empty = list_empty(&fs_info->end_io_work_list);
+                       list_add_tail(&end_io_wq->list,
+                                     &fs_info->end_io_work_list);
+                       spin_unlock_irqrestore(&fs_info->end_io_work_lock,
+                                              flags);
+                       if (was_empty)
+                               return;
+                       continue;
+               }
+               error = end_io_wq->error;
+               bio->bi_private = end_io_wq->private;
+               bio->bi_end_io = end_io_wq->end_io;
+               kfree(end_io_wq);
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
+               bio_endio(bio, bio->bi_size, error);
+#else
+               bio_endio(bio, error);
+#endif
+       }
+}
+
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
+static void btrfs_async_submit_work(void *p)
+#else
+static void btrfs_async_submit_work(struct work_struct *work)
+#endif
+{
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
+       struct btrfs_fs_info *fs_info = p;
+#else
+       struct btrfs_fs_info *fs_info = container_of(work,
+                                                    struct btrfs_fs_info,
+                                                    async_submit_work);
+#endif
+       struct async_submit_bio *async;
+       struct list_head *next;
+
+       while(1) {
+               spin_lock(&fs_info->async_submit_work_lock);
+               if (list_empty(&fs_info->async_submit_work_list)) {
+                       spin_unlock(&fs_info->async_submit_work_lock);
+                       return;
+               }
+               next = fs_info->async_submit_work_list.next;
+               list_del(next);
+               spin_unlock(&fs_info->async_submit_work_lock);
+
+               async = list_entry(next, struct async_submit_bio, list);
+               async->submit_bio_hook(async->inode, async->rw, async->bio,
+                                      async->mirror_num);
+               kfree(async);
+       }
+}
+
+struct btrfs_root *open_ctree(struct super_block *sb,
+                             struct btrfs_fs_devices *fs_devices)
 {
        u32 sectorsize;
        u32 nodesize;
@@ -667,52 +1049,57 @@ struct btrfs_root *open_ctree(struct super_block *sb)
                                                 GFP_NOFS);
        struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
                                               GFP_NOFS);
-       struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
+       struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info),
                                                GFP_NOFS);
        struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root),
                                                GFP_NOFS);
        struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
                                              GFP_NOFS);
        int ret;
-       int err = -EIO;
+       int err = -EINVAL;
        struct btrfs_super_block *disk_super;
 
        if (!extent_root || !tree_root || !fs_info) {
                err = -ENOMEM;
                goto fail;
        }
+       end_io_workqueue = create_workqueue("btrfs-end-io");
+       BUG_ON(!end_io_workqueue);
+       async_submit_workqueue = create_workqueue("btrfs-async-submit");
+
        INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
        INIT_LIST_HEAD(&fs_info->trans_list);
        INIT_LIST_HEAD(&fs_info->dead_roots);
        INIT_LIST_HEAD(&fs_info->hashers);
+       INIT_LIST_HEAD(&fs_info->end_io_work_list);
+       INIT_LIST_HEAD(&fs_info->async_submit_work_list);
        spin_lock_init(&fs_info->hash_lock);
+       spin_lock_init(&fs_info->end_io_work_lock);
+       spin_lock_init(&fs_info->async_submit_work_lock);
        spin_lock_init(&fs_info->delalloc_lock);
        spin_lock_init(&fs_info->new_trans_lock);
 
-       memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj));
        init_completion(&fs_info->kobj_unregister);
-       sb_set_blocksize(sb, 4096);
-       fs_info->running_transaction = NULL;
-       fs_info->last_trans_committed = 0;
+       sb_set_blocksize(sb, BTRFS_SUPER_INFO_SIZE);
        fs_info->tree_root = tree_root;
        fs_info->extent_root = extent_root;
        fs_info->chunk_root = chunk_root;
        fs_info->dev_root = dev_root;
+       fs_info->fs_devices = fs_devices;
        INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
-       INIT_LIST_HEAD(&fs_info->devices);
+       INIT_LIST_HEAD(&fs_info->space_info);
        btrfs_mapping_init(&fs_info->mapping_tree);
-       fs_info->last_device = &fs_info->devices;
        fs_info->sb = sb;
-       fs_info->throttles = 0;
-       fs_info->mount_opt = 0;
        fs_info->max_extent = (u64)-1;
        fs_info->max_inline = 8192 * 1024;
-       fs_info->delalloc_bytes = 0;
+       setup_bdi(fs_info, &fs_info->bdi);
        fs_info->btree_inode = new_inode(sb);
        fs_info->btree_inode->i_ino = 1;
        fs_info->btree_inode->i_nlink = 1;
        fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
        fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
+       fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
+
        extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
                             fs_info->btree_inode->i_mapping,
                             GFP_NOFS);
@@ -732,14 +1119,15 @@ struct btrfs_root *open_ctree(struct super_block *sb)
        extent_io_tree_init(&fs_info->extent_ins,
                             fs_info->btree_inode->i_mapping, GFP_NOFS);
        fs_info->do_barriers = 1;
-       fs_info->closing = 0;
-       fs_info->total_pinned = 0;
-       fs_info->last_alloc = 0;
-       fs_info->last_data_alloc = 0;
 
 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
+       INIT_WORK(&fs_info->end_io_work, btrfs_end_io_csum, fs_info);
+       INIT_WORK(&fs_info->async_submit_work, btrfs_async_submit_work,
+                 fs_info);
        INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info);
 #else
+       INIT_WORK(&fs_info->end_io_work, btrfs_end_io_csum);
+       INIT_WORK(&fs_info->async_submit_work, btrfs_async_submit_work);
        INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
 #endif
        BTRFS_I(fs_info->btree_inode)->root = tree_root;
@@ -780,6 +1168,12 @@ struct btrfs_root *open_ctree(struct super_block *sb)
        if (!btrfs_super_root(disk_super))
                goto fail_sb_buffer;
 
+       if (btrfs_super_num_devices(disk_super) != fs_devices->num_devices) {
+               printk("Btrfs: wanted %llu devices, but found %llu\n",
+                      (unsigned long long)btrfs_super_num_devices(disk_super),
+                      (unsigned long long)fs_devices->num_devices);
+               goto fail_sb_buffer;
+       }
        nodesize = btrfs_super_nodesize(disk_super);
        leafsize = btrfs_super_leafsize(disk_super);
        sectorsize = btrfs_super_sectorsize(disk_super);
@@ -800,6 +1194,7 @@ struct btrfs_root *open_ctree(struct super_block *sb)
        }
 
        mutex_lock(&fs_info->fs_mutex);
+
        ret = btrfs_read_sys_array(tree_root);
        BUG_ON(ret);
 
@@ -814,6 +1209,10 @@ struct btrfs_root *open_ctree(struct super_block *sb)
                                           blocksize);
        BUG_ON(!chunk_root->node);
 
+       read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
+                (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
+                BTRFS_UUID_SIZE);
+
        ret = btrfs_read_chunk_tree(chunk_root);
        BUG_ON(ret);
 
@@ -844,6 +1243,10 @@ struct btrfs_root *open_ctree(struct super_block *sb)
        btrfs_read_block_groups(extent_root);
 
        fs_info->generation = btrfs_super_generation(disk_super) + 1;
+       fs_info->data_alloc_profile = (u64)-1;
+       fs_info->metadata_alloc_profile = (u64)-1;
+       fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
+
        mutex_unlock(&fs_info->fs_mutex);
        return tree_root;
 
@@ -854,23 +1257,135 @@ fail_tree_root:
        free_extent_buffer(tree_root->node);
 fail_sb_buffer:
        free_extent_buffer(fs_info->sb_buffer);
+       extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
 fail_iput:
        iput(fs_info->btree_inode);
 fail:
+       close_all_devices(fs_info);
        kfree(extent_root);
        kfree(tree_root);
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
+       bdi_destroy(&fs_info->bdi);
+#endif
        kfree(fs_info);
        return ERR_PTR(err);
 }
 
+static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
+{
+       char b[BDEVNAME_SIZE];
+
+       if (uptodate) {
+               set_buffer_uptodate(bh);
+       } else {
+               if (!buffer_eopnotsupp(bh) && printk_ratelimit()) {
+                       printk(KERN_WARNING "lost page write due to "
+                                       "I/O error on %s\n",
+                                      bdevname(bh->b_bdev, b));
+               }
+               set_buffer_write_io_error(bh);
+               clear_buffer_uptodate(bh);
+       }
+       unlock_buffer(bh);
+       put_bh(bh);
+}
+
+int write_all_supers(struct btrfs_root *root)
+{
+       struct list_head *cur;
+       struct list_head *head = &root->fs_info->fs_devices->devices;
+       struct btrfs_device *dev;
+       struct extent_buffer *sb;
+       struct btrfs_dev_item *dev_item;
+       struct buffer_head *bh;
+       int ret;
+       int do_barriers;
+
+       do_barriers = !btrfs_test_opt(root, NOBARRIER);
+
+       sb = root->fs_info->sb_buffer;
+       dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
+                                                     dev_item);
+       list_for_each(cur, head) {
+               dev = list_entry(cur, struct btrfs_device, dev_list);
+               btrfs_set_device_type(sb, dev_item, dev->type);
+               btrfs_set_device_id(sb, dev_item, dev->devid);
+               btrfs_set_device_total_bytes(sb, dev_item, dev->total_bytes);
+               btrfs_set_device_bytes_used(sb, dev_item, dev->bytes_used);
+               btrfs_set_device_io_align(sb, dev_item, dev->io_align);
+               btrfs_set_device_io_width(sb, dev_item, dev->io_width);
+               btrfs_set_device_sector_size(sb, dev_item, dev->sector_size);
+               write_extent_buffer(sb, dev->uuid,
+                                   (unsigned long)btrfs_device_uuid(dev_item),
+                                   BTRFS_UUID_SIZE);
+
+               btrfs_set_header_flag(sb, BTRFS_HEADER_FLAG_WRITTEN);
+               csum_tree_block(root, sb, 0);
+
+               bh = __getblk(dev->bdev, BTRFS_SUPER_INFO_OFFSET /
+                             root->fs_info->sb->s_blocksize,
+                             BTRFS_SUPER_INFO_SIZE);
+
+               read_extent_buffer(sb, bh->b_data, 0, BTRFS_SUPER_INFO_SIZE);
+               dev->pending_io = bh;
+
+               get_bh(bh);
+               set_buffer_uptodate(bh);
+               lock_buffer(bh);
+               bh->b_end_io = btrfs_end_buffer_write_sync;
+
+               if (do_barriers && dev->barriers) {
+                       ret = submit_bh(WRITE_BARRIER, bh);
+                       if (ret == -EOPNOTSUPP) {
+                               printk("btrfs: disabling barriers on dev %s\n",
+                                      dev->name);
+                               set_buffer_uptodate(bh);
+                               dev->barriers = 0;
+                               get_bh(bh);
+                               lock_buffer(bh);
+                               ret = submit_bh(WRITE, bh);
+                       }
+               } else {
+                       ret = submit_bh(WRITE, bh);
+               }
+               BUG_ON(ret);
+       }
+
+       list_for_each(cur, head) {
+               dev = list_entry(cur, struct btrfs_device, dev_list);
+               BUG_ON(!dev->pending_io);
+               bh = dev->pending_io;
+               wait_on_buffer(bh);
+               if (!buffer_uptodate(dev->pending_io)) {
+                       if (do_barriers && dev->barriers) {
+                               printk("btrfs: disabling barriers on dev %s\n",
+                                      dev->name);
+                               set_buffer_uptodate(bh);
+                               get_bh(bh);
+                               lock_buffer(bh);
+                               dev->barriers = 0;
+                               ret = submit_bh(WRITE, bh);
+                               BUG_ON(ret);
+                               wait_on_buffer(bh);
+                               BUG_ON(!buffer_uptodate(bh));
+                       } else {
+                               BUG();
+                       }
+
+               }
+               dev->pending_io = NULL;
+               brelse(bh);
+       }
+       return 0;
+}
+
 int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
                      *root)
 {
        int ret;
-       struct extent_buffer *super = root->fs_info->sb_buffer;
-       struct inode *btree_inode = root->fs_info->btree_inode;
-       struct super_block *sb = root->fs_info->sb;
 
+       ret = write_all_supers(root);
+#if 0
        if (!btrfs_test_opt(root, NOBARRIER))
                blkdev_issue_flush(sb->s_bdev, NULL);
        set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, super);
@@ -878,6 +1393,7 @@ int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
                                     super->start, super->len);
        if (!btrfs_test_opt(root, NOBARRIER))
                blkdev_issue_flush(sb->s_bdev, NULL);
+#endif
        return ret;
 }
 
@@ -968,6 +1484,11 @@ int close_ctree(struct btrfs_root *root)
        extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
 
        truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
+       flush_workqueue(end_io_workqueue);
+       destroy_workqueue(end_io_workqueue);
+
+       flush_workqueue(async_submit_workqueue);
+       destroy_workqueue(async_submit_workqueue);
 
        iput(fs_info->btree_inode);
 #if 0
@@ -983,6 +1504,10 @@ int close_ctree(struct btrfs_root *root)
        close_all_devices(fs_info);
        btrfs_mapping_tree_free(&fs_info->mapping_tree);
 
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
+       bdi_destroy(&fs_info->bdi);
+#endif
+
        kfree(fs_info->extent_root);
        kfree(fs_info->tree_root);
        kfree(fs_info->chunk_root);
@@ -1093,12 +1618,18 @@ int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
 int btrfs_read_buffer(struct extent_buffer *buf)
 {
        struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
-       struct inode *btree_inode = root->fs_info->btree_inode;
-       return read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
-                                       buf, 0, 1, btree_get_extent);
+       int ret;
+       ret = btree_read_extent_buffer_pages(root, buf, 0);
+       if (ret == 0) {
+               buf->flags |= EXTENT_UPTODATE;
+       }
+       return ret;
 }
 
 static struct extent_io_ops btree_extent_io_ops = {
        .writepage_io_hook = btree_writepage_io_hook,
+       .readpage_end_io_hook = btree_readpage_end_io_hook,
        .submit_bio_hook = btree_submit_bio_hook,
+       /* note we're sharing with inode.c for the merge bio hook */
+       .merge_bio_hook = btrfs_merge_bio_hook,
 };