Btrfs: Fix allocation profile init
[linux-2.6-block.git] / fs / btrfs / disk-io.c
index 88e21bdbc4780aadbb15b6a94d0e29f54c077508..bc5bd5abb28a67d89a9793497156ae35200f0ce6 100644 (file)
@@ -28,6 +28,7 @@
 #include "disk-io.h"
 #include "transaction.h"
 #include "btrfs_inode.h"
+#include "volumes.h"
 #include "print-tree.h"
 
 #if 0
@@ -45,27 +46,6 @@ static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
 
 static struct extent_io_ops btree_extent_io_ops;
 
-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_map *btree_get_extent(struct inode *inode, struct page *page,
                                    size_t page_offset, u64 start, u64 len,
                                    int create)
@@ -158,17 +138,19 @@ 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);
+                       WARN_ON(1);
                        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 {
@@ -219,6 +201,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);
@@ -234,6 +219,19 @@ 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)
+{
+       struct btrfs_root *root = BTRFS_I(inode)->root;
+       u64 offset;
+       offset = bio->bi_sector << 9;
+       if (offset == BTRFS_SUPER_INFO_OFFSET) {
+               bio->bi_bdev = root->fs_info->sb->s_bdev;
+               submit_bio(rw, bio);
+               return 0;
+       }
+       return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio);
+}
+
 static int btree_writepage(struct page *page, struct writeback_control *wbc)
 {
        struct extent_io_tree *tree;
@@ -345,36 +343,45 @@ int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
        return ret;
 }
 
-struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
-                                     u32 blocksize)
+static int close_all_devices(struct btrfs_fs_info *fs_info)
+{
+       struct list_head *list;
+       struct list_head *next;
+       struct btrfs_device *device;
+
+       list = &fs_info->fs_devices->devices;
+       list_for_each(next, list) {
+               device = list_entry(next, struct btrfs_device, dev_list);
+               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)
 {
-       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;
-
-       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);
-
+       io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
        if (buf->flags & EXTENT_CSUM)
-               return buf;
+               return 0;
 
-       end = buf->start + PAGE_CACHE_SIZE - 1;
+       end = min_t(u64, buf->len, PAGE_CACHE_SIZE);
+       end = buf->start + end - 1;
        if (test_range_bit(io_tree, buf->start, end, EXTENT_CSUM, 1)) {
                buf->flags |= EXTENT_CSUM;
-               return buf;
+               return 0;
        }
 
        lock_extent(io_tree, buf->start, end, GFP_NOFS);
 
        if (test_range_bit(io_tree, buf->start, end, EXTENT_CSUM, 1)) {
                buf->flags |= EXTENT_CSUM;
+               ret = 0;
                goto out_unlock;
        }
 
@@ -384,6 +391,48 @@ struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
 
 out_unlock:
        unlock_extent(io_tree, buf->start, end, GFP_NOFS);
+       return ret;
+}
+
+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;
+       int ret;
+
+       io_tree = &BTRFS_I(btree_inode)->io_tree;
+
+       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);
+
+       ret = btrfs_verify_block_csum(root, buf);
        return buf;
 }
 
@@ -420,6 +469,8 @@ static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
        root->leafsize = leafsize;
        root->stripesize = stripesize;
        root->ref_cows = 0;
+       root->track_dirty = 0;
+
        root->fs_info = fs_info;
        root->objectid = objectid;
        root->last_trans = 0;
@@ -427,6 +478,8 @@ static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
        root->last_inode_alloc = 0;
        root->name = NULL;
        root->in_sysfs = 0;
+
+       INIT_LIST_HEAD(&root->dirty_list);
        memset(&root->root_key, 0, sizeof(root->root_key));
        memset(&root->root_item, 0, sizeof(root->root_item));
        memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
@@ -621,7 +674,57 @@ 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)
+{
+       bdi_init(bdi);
+       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;
+}
+
+struct btrfs_root *open_ctree(struct super_block *sb,
+                             struct btrfs_fs_devices *fs_devices)
 {
        u32 sectorsize;
        u32 nodesize;
@@ -632,10 +735,14 @@ 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) {
@@ -650,24 +757,27 @@ struct btrfs_root *open_ctree(struct super_block *sb)
        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;
        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->space_info);
+       btrfs_mapping_init(&fs_info->mapping_tree);
        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);
@@ -687,10 +797,6 @@ 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->trans_work, btrfs_transaction_cleaner, fs_info);
@@ -714,12 +820,12 @@ struct btrfs_root *open_ctree(struct super_block *sb)
                goto fail_iput;
        }
 #endif
-       __setup_root(512, 512, 512, 512, tree_root,
+       __setup_root(4096, 4096, 4096, 4096, tree_root,
                     fs_info, BTRFS_ROOT_TREE_OBJECTID);
 
        fs_info->sb_buffer = read_tree_block(tree_root,
                                             BTRFS_SUPER_INFO_OFFSET,
-                                            512);
+                                            4096);
 
        if (!fs_info->sb_buffer)
                goto fail_iput;
@@ -730,10 +836,17 @@ struct btrfs_root *open_ctree(struct super_block *sb)
        read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
                           (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
                           BTRFS_FSID_SIZE);
+
        disk_super = &fs_info->super_copy;
        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);
@@ -753,39 +866,74 @@ struct btrfs_root *open_ctree(struct super_block *sb)
                goto fail_sb_buffer;
        }
 
+       mutex_lock(&fs_info->fs_mutex);
+
+       ret = btrfs_read_sys_array(tree_root);
+       BUG_ON(ret);
+
+       blocksize = btrfs_level_size(tree_root,
+                                    btrfs_super_chunk_root_level(disk_super));
+
+       __setup_root(nodesize, leafsize, sectorsize, stripesize,
+                    chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
+
+       chunk_root->node = read_tree_block(chunk_root,
+                                          btrfs_super_chunk_root(disk_super),
+                                          blocksize);
+       BUG_ON(!chunk_root->node);
+
+       ret = btrfs_read_chunk_tree(chunk_root);
+       BUG_ON(ret);
+
        blocksize = btrfs_level_size(tree_root,
                                     btrfs_super_root_level(disk_super));
 
+
        tree_root->node = read_tree_block(tree_root,
                                          btrfs_super_root(disk_super),
                                          blocksize);
        if (!tree_root->node)
                goto fail_sb_buffer;
 
-       mutex_lock(&fs_info->fs_mutex);
 
        ret = find_and_setup_root(tree_root, fs_info,
                                  BTRFS_EXTENT_TREE_OBJECTID, extent_root);
-       if (ret) {
-               mutex_unlock(&fs_info->fs_mutex);
+       if (ret)
                goto fail_tree_root;
-       }
+       extent_root->track_dirty = 1;
+
+       ret = find_and_setup_root(tree_root, fs_info,
+                                 BTRFS_DEV_TREE_OBJECTID, dev_root);
+       dev_root->track_dirty = 1;
+
+       if (ret)
+               goto fail_extent_root;
 
        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;
 
+fail_extent_root:
+       free_extent_buffer(extent_root->node);
 fail_tree_root:
+       mutex_unlock(&fs_info->fs_mutex);
        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);
+       bdi_destroy(&fs_info->bdi);
        kfree(fs_info);
        return ERR_PTR(err);
 }
@@ -874,6 +1022,12 @@ int close_ctree(struct btrfs_root *root)
        if (fs_info->tree_root->node)
                free_extent_buffer(fs_info->tree_root->node);
 
+       if (root->fs_info->chunk_root->node);
+               free_extent_buffer(root->fs_info->chunk_root->node);
+
+       if (root->fs_info->dev_root->node);
+               free_extent_buffer(root->fs_info->dev_root->node);
+
        free_extent_buffer(fs_info->sb_buffer);
 
        btrfs_free_block_groups(root->fs_info);
@@ -901,8 +1055,14 @@ int close_ctree(struct btrfs_root *root)
                kfree(hasher);
        }
 #endif
+       close_all_devices(fs_info);
+       btrfs_mapping_tree_free(&fs_info->mapping_tree);
+       bdi_destroy(&fs_info->bdi);
+
        kfree(fs_info->extent_root);
        kfree(fs_info->tree_root);
+       kfree(fs_info->chunk_root);
+       kfree(fs_info->dev_root);
        return 0;
 }
 
@@ -1016,4 +1176,7 @@ int btrfs_read_buffer(struct extent_buffer *buf)
 
 static struct extent_io_ops btree_extent_io_ops = {
        .writepage_io_hook = btree_writepage_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,
 };