Btrfs: remove extent mapping if we fail to add chunk
authorJosef Bacik <jbacik@fusionio.com>
Thu, 31 Jan 2013 15:23:04 +0000 (10:23 -0500)
committerJosef Bacik <jbacik@fusionio.com>
Wed, 20 Feb 2013 17:59:11 +0000 (12:59 -0500)
I got a double free error when unmounting a file system that failed to add a
chunk during its operation.  This is because we will kfree the mapping that
we created but leave the extent_map in the em_tree for chunks.  So to fix
this just remove the extent_map when we error out so we don't run into this
problem.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fusionio.com>
fs/btrfs/volumes.c

index 13efbcf031223417a723961f65c9f876cb3637dc..c7843349c7954adb25dc4419589d8ce9132aa7ff 100644 (file)
@@ -3821,9 +3821,10 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
        write_lock(&em_tree->lock);
        ret = add_extent_mapping(em_tree, em);
        write_unlock(&em_tree->lock);
-       free_extent_map(em);
-       if (ret)
+       if (ret) {
+               free_extent_map(em);
                goto error;
+       }
 
        for (i = 0; i < map->num_stripes; ++i) {
                struct btrfs_device *device;
@@ -3848,6 +3849,7 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
                goto error_dev_extent;
        }
 
+       free_extent_map(em);
        kfree(devices_info);
        return 0;
 
@@ -3863,6 +3865,14 @@ error_dev_extent:
                        break;
                }
        }
+       write_lock(&em_tree->lock);
+       remove_extent_mapping(em_tree, em);
+       write_unlock(&em_tree->lock);
+
+       /* One for our allocation */
+       free_extent_map(em);
+       /* One for the tree reference */
+       free_extent_map(em);
 error:
        kfree(map);
        kfree(devices_info);