]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - fs/btrfs/inode.c
Btrfs: Throttle file_write when data=ordered is flushing the inode
[karo-tx-linux.git] / fs / btrfs / inode.c
index 40f8da884090b65c4c3af6358cdda9d029013c20..b31f52d4f2caeb64c125af85ac9e9e4cf86fd38f 100644 (file)
@@ -80,6 +80,7 @@ int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
        u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
        u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
        u64 thresh;
+       unsigned long flags;
        int ret = 0;
 
        if (for_del)
@@ -89,10 +90,10 @@ int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
 
        do_div(thresh, 100);
 
-       spin_lock(&root->fs_info->delalloc_lock);
+       spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
        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;
 }
 
@@ -275,12 +276,13 @@ static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
 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 +292,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 +304,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 +317,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,
@@ -498,6 +498,7 @@ 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);
        return 0;
@@ -1418,6 +1419,7 @@ static int btrfs_init_locked_inode(struct inode *inode, void *p)
                             inode->i_mapping, GFP_NOFS);
        extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
                             inode->i_mapping, GFP_NOFS);
+       atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
        return 0;
 }
 
@@ -1727,6 +1729,7 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
                             inode->i_mapping, GFP_NOFS);
        extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
                             inode->i_mapping, GFP_NOFS);
+       atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
        BTRFS_I(inode)->delalloc_bytes = 0;
        BTRFS_I(inode)->root = root;
 
@@ -1955,6 +1958,7 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
                extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
                                     inode->i_mapping, GFP_NOFS);
                BTRFS_I(inode)->delalloc_bytes = 0;
+               atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
                BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
        }
        dir->i_sb->s_dirt = 1;
@@ -2191,12 +2195,9 @@ again:
        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;
@@ -2344,7 +2345,6 @@ 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
@@ -2352,6 +2352,11 @@ insert:
        if (ret == -EEXIST) {
                struct extent_map *existing;
                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);
@@ -2392,6 +2397,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 +2415,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 +2442,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 +2450,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 +2466,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)
@@ -2510,6 +2523,7 @@ 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);
@@ -2524,6 +2538,12 @@ static void btrfs_invalidatepage(struct page *page, unsigned long offset)
        tree = &BTRFS_I(page->mapping->host)->io_tree;
        extent_invalidatepage(tree, page, offset);
        btrfs_releasepage(page, GFP_NOFS);
+       if (PagePrivate(page)) {
+               invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
+               ClearPagePrivate(page);
+               set_page_private(page, 0);
+               page_cache_release(page);
+       }
 }
 
 /*
@@ -3275,6 +3295,7 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
                extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
                                     inode->i_mapping, GFP_NOFS);
                BTRFS_I(inode)->delalloc_bytes = 0;
+               atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
                BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
        }
        dir->i_sb->s_dirt = 1;