Btrfs: Create a work queue for bio writes
[safe/jmp/linux-2.6] / fs / btrfs / disk-io.c
index cf1de75..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"
@@ -47,6 +47,7 @@ static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
 
 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;
@@ -58,6 +59,15 @@ struct end_io_wq {
        struct list_head list;
 };
 
+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,
                                    int create)
@@ -100,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)
@@ -365,7 +375,31 @@ int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
        return 0;
 }
 
-static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
+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;
@@ -389,6 +423,17 @@ static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *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)
 {
        struct extent_io_tree *tree;
@@ -436,6 +481,12 @@ 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);
@@ -844,7 +895,9 @@ void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *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;
@@ -895,9 +948,9 @@ static int bio_ready_for_csum(struct bio *bio)
 }
 
 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
-void btrfs_end_io_csum(void *p)
+static void btrfs_end_io_csum(void *p)
 #else
-void btrfs_end_io_csum(struct work_struct *work)
+static void btrfs_end_io_csum(struct work_struct *work)
 #endif
 {
 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
@@ -943,10 +996,46 @@ void btrfs_end_io_csum(struct work_struct *work)
                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)
@@ -976,14 +1065,17 @@ struct btrfs_root *open_ctree(struct super_block *sb,
        }
        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);
 
@@ -1028,10 +1120,14 @@ struct btrfs_root *open_ctree(struct super_block *sb,
                             fs_info->btree_inode->i_mapping, GFP_NOFS);
        fs_info->do_barriers = 1;
 
-       INIT_WORK(&fs_info->end_io_work, btrfs_end_io_csum);
 #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;
@@ -1113,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);
 
@@ -1164,7 +1264,9 @@ 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);
 }
@@ -1215,7 +1317,7 @@ int write_all_supers(struct btrfs_root *root)
                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_DEV_UUID_SIZE);
+                                   BTRFS_UUID_SIZE);
 
                btrfs_set_header_flag(sb, BTRFS_HEADER_FLAG_WRITTEN);
                csum_tree_block(root, sb, 0);
@@ -1385,6 +1487,9 @@ int close_ctree(struct btrfs_root *root)
        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
        while(!list_empty(&fs_info->hashers)) {
@@ -1398,7 +1503,10 @@ int close_ctree(struct btrfs_root *root)
 #endif
        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);