Btrfs: Use the extent map cache to find the logical disk block during data retries
authorChris Mason <chris.mason@oracle.com>
Thu, 17 Apr 2008 15:29:12 +0000 (11:29 -0400)
committerChris Mason <chris.mason@oracle.com>
Thu, 25 Sep 2008 15:04:01 +0000 (11:04 -0400)
The data read retry code needs to find the logical disk block before it
can resubmit new bios.  But, finding this block isn't allowed to take
the fs_mutex because that will deadlock with a number of different callers.

This changes the retry code to use the extent map cache instead, but
that requires the extent map cache to have the extent we're looking for.
This is a problem because btrfs_drop_extent_cache just drops the entire
extent instead of the little tiny part it is invalidating.

The bulk of the code in this patch changes btrfs_drop_extent_cache to
invalidate only a portion of the extent cache, and changes btrfs_get_extent
to deal with the results.

Signed-off-by: Chris Mason <chris.mason@oracle.com>
fs/btrfs/extent-tree.c
fs/btrfs/extent_io.c
fs/btrfs/file.c
fs/btrfs/inode.c
fs/btrfs/volumes.c

index 76fd5d7146e1a34fb230ae72b67a818d64b0cda6..593011e5d455f8359667f91b163265ec0776cd8f 100644 (file)
@@ -1747,6 +1747,7 @@ again:
                               search_start, search_end, hint_byte, ins,
                               trans->alloc_exclude_start,
                               trans->alloc_exclude_nr, data);
+
        if (ret == -ENOSPC && num_bytes > min_alloc_size) {
                num_bytes = num_bytes >> 1;
                num_bytes = max(num_bytes, min_alloc_size);
index 88322684be6aa0b40c274c296b4fd2a8d15ce8c8..21597bea21fdd0435ed736efacea0bf355712910 100644 (file)
@@ -1025,7 +1025,8 @@ u64 find_lock_delalloc_range(struct extent_io_tree *tree,
 search_again:
        node = tree_search(tree, cur_start);
        if (!node) {
-               *end = (u64)-1;
+               if (!found)
+                       *end = (u64)-1;
                goto out;
        }
 
@@ -1540,6 +1541,8 @@ static int end_bio_extent_readpage(struct bio *bio,
                                                         start, end, state);
                        if (ret == 0) {
                                state = NULL;
+                               uptodate =
+                                       test_bit(BIO_UPTODATE, &bio->bi_flags);
                                continue;
                        }
                }
@@ -1555,10 +1558,11 @@ static int end_bio_extent_readpage(struct bio *bio,
                                    !(state->state & EXTENT_LOCKED))
                                        state = NULL;
                        }
-                       if (!state && uptodate) {
+                       if (!state) {
                                spin_unlock_irqrestore(&tree->lock, flags);
-                               set_extent_uptodate(tree, start, end,
-                                                   GFP_ATOMIC);
+                               if (uptodate)
+                                       set_extent_uptodate(tree, start, end,
+                                                           GFP_ATOMIC);
                                unlock_extent(tree, start, end, GFP_ATOMIC);
                                goto next_io;
                        }
index 9fbda6552069338f874186669e7a72f429eb73e2..3f5525f0834c687b36a3e0169bfb74907ba6ed49 100644 (file)
@@ -356,12 +356,23 @@ out_unlock:
 int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end)
 {
        struct extent_map *em;
+       struct extent_map *split = NULL;
+       struct extent_map *split2 = NULL;
        struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
        u64 len = end - start + 1;
+       int ret;
+       int testend = 1;
 
-       if (end == (u64)-1)
+       if (end == (u64)-1) {
                len = (u64)-1;
+               testend = 0;
+       }
        while(1) {
+               if (!split)
+                       split = alloc_extent_map(GFP_NOFS);
+               if (!split2)
+                       split2 = alloc_extent_map(GFP_NOFS);
+
                spin_lock(&em_tree->lock);
                em = lookup_extent_mapping(em_tree, start, len);
                if (!em) {
@@ -369,6 +380,36 @@ int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end)
                        break;
                }
                remove_extent_mapping(em_tree, em);
+
+               if (em->block_start < EXTENT_MAP_LAST_BYTE &&
+                   em->start < start) {
+                       split->start = em->start;
+                       split->len = start - em->start;
+                       split->block_start = em->block_start;
+                       split->bdev = em->bdev;
+                       split->flags = em->flags;
+                       ret = add_extent_mapping(em_tree, split);
+                       BUG_ON(ret);
+                       free_extent_map(split);
+                       split = split2;
+                       split2 = NULL;
+               }
+               if (em->block_start < EXTENT_MAP_LAST_BYTE &&
+                   testend && em->start + em->len > start + len) {
+                       u64 diff = start + len - em->start;
+
+                       split->start = start + len;
+                       split->len = em->start + em->len - (start + len);
+                       split->bdev = em->bdev;
+                       split->flags = em->flags;
+
+                       split->block_start = em->block_start + diff;
+
+                       ret = add_extent_mapping(em_tree, split);
+                       BUG_ON(ret);
+                       free_extent_map(split);
+                       split = NULL;
+               }
                spin_unlock(&em_tree->lock);
 
                /* once for us */
@@ -376,6 +417,10 @@ int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end)
                /* once for the tree*/
                free_extent_map(em);
        }
+       if (split)
+               free_extent_map(split);
+       if (split2)
+               free_extent_map(split2);
        return 0;
 }
 
index 5632ea760077e7e26af2548e2e14e0f4eefcf79e..40f8da884090b65c4c3af6358cdda9d029013c20 100644 (file)
@@ -122,6 +122,8 @@ static int cow_file_range(struct inode *inode, u64 start, u64 end)
        if (alloc_hint == EXTENT_MAP_INLINE)
                goto out;
 
+       BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
+
        while(num_bytes > 0) {
                cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
                ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
@@ -140,6 +142,11 @@ static int cow_file_range(struct inode *inode, u64 start, u64 end)
                                               ins.offset);
                inode->i_blocks += ins.offset >> 9;
                btrfs_check_file(root, inode);
+               if (num_bytes < cur_alloc_size) {
+                       printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
+                              cur_alloc_size);
+                       break;
+               }
                num_bytes -= cur_alloc_size;
                alloc_hint = ins.objectid + ins.offset;
                start += cur_alloc_size;
@@ -427,6 +434,7 @@ int btrfs_readpage_io_failed_hook(struct bio *failed_bio,
        struct extent_map *em;
        struct inode *inode = page->mapping->host;
        struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
+       struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
        struct bio *bio;
        int num_copies;
        int ret;
@@ -434,7 +442,6 @@ int btrfs_readpage_io_failed_hook(struct bio *failed_bio,
 
        ret = get_state_private(failure_tree, start, &private);
        if (ret) {
-               size_t pg_offset = start - page_offset(page);
                failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
                if (!failrec)
                        return -ENOMEM;
@@ -442,8 +449,13 @@ int btrfs_readpage_io_failed_hook(struct bio *failed_bio,
                failrec->len = end - start + 1;
                failrec->last_mirror = 0;
 
-               em = btrfs_get_extent(inode, NULL, pg_offset, start,
-                                     failrec->len, 0);
+               spin_lock(&em_tree->lock);
+               em = lookup_extent_mapping(em_tree, start, failrec->len);
+               if (em->start > start || em->start + em->len < start) {
+                       free_extent_map(em);
+                       em = NULL;
+               }
+               spin_unlock(&em_tree->lock);
 
                if (!em || IS_ERR(em)) {
                        kfree(failrec);
@@ -559,6 +571,8 @@ zeroit:
        flush_dcache_page(page);
        kunmap_atomic(kaddr, KM_IRQ0);
        local_irq_restore(flags);
+       if (private == 0)
+               return 0;
        return -EIO;
 }
 
@@ -908,8 +922,9 @@ static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
        int pending_del_nr = 0;
        int pending_del_slot = 0;
        int extent_type = -1;
+       u64 mask = root->sectorsize - 1;
 
-       btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
+       btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
        path = btrfs_alloc_path();
        path->reada = -1;
        BUG_ON(!path);
@@ -1212,7 +1227,7 @@ static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
                                                       hole_start, 0, 0,
                                                       hole_size);
                        btrfs_drop_extent_cache(inode, hole_start,
-                                               hole_size - 1);
+                                               (u64)-1);
                        btrfs_check_file(root, inode);
                }
                btrfs_end_transaction(trans, root);
@@ -2083,6 +2098,68 @@ out_unlock:
        return err;
 }
 
+static int merge_extent_mapping(struct extent_map_tree *em_tree,
+                               struct extent_map *existing,
+                               struct extent_map *em)
+{
+       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;
+}
+
 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
                                    size_t pg_offset, u64 start, u64 len,
                                    int create)
@@ -2267,12 +2344,35 @@ 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) {
-               free_extent_map(em);
-               em = lookup_extent_mapping(em_tree, start, len);
-               if (!em) {
-                       err = -EIO;
-                       printk("failing to insert %Lu %Lu\n", start, len);
+               struct extent_map *existing;
+               existing = lookup_extent_mapping(em_tree, start, len);
+               if (!existing) {
+                       existing = lookup_extent_mapping(em_tree, em->start,
+                                                        em->len);
+                       if (existing) {
+                               err = merge_extent_mapping(em_tree, existing,
+                                                          em);
+                               free_extent_map(existing);
+                               if (err) {
+                                       free_extent_map(em);
+                                       em = NULL;
+                               }
+                       } else {
+                               err = -EIO;
+                               printk("failing to insert %Lu %Lu\n",
+                                      start, len);
+                               free_extent_map(em);
+                               em = NULL;
+                       }
+               } else {
+                       free_extent_map(em);
+                       em = existing;
                }
        }
        spin_unlock(&em_tree->lock);
index e6417a573d446182cc801571bf115eb7da130546..0e658c1d8211bd9644c354f1d699ac0a2e0bd757 100644 (file)
@@ -883,6 +883,9 @@ again:
        spin_lock(&em_tree->lock);
        em = lookup_extent_mapping(em_tree, logical, *length);
        spin_unlock(&em_tree->lock);
+       if (!em) {
+               printk("unable to find logical %Lu\n", logical);
+       }
        BUG_ON(!em);
 
        BUG_ON(em->start > logical || em->start + em->len < logical);
This page took 0.041737 seconds and 5 git commands to generate.