Replace some BUG_ONs with proper handling and take allocated space back to
free space cache for later use.

We don't have to worry about extent maps since they'd be freed in releasepage
path.

Signed-off-by: Liu Bo <[email protected]>
---
 fs/btrfs/inode.c |   12 +++++++++---
 1 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index b883815..f8b4efa 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -921,7 +921,8 @@ static noinline int __cow_file_range(struct 
btrfs_trans_handle *trans,
                }
 
                em = alloc_extent_map();
-               BUG_ON(!em); /* -ENOMEM */
+               if (!em)
+                       goto out_reserve;
                em->start = start;
                em->orig_start = em->start;
                ram_size = ins.offset;
@@ -950,11 +951,14 @@ static noinline int __cow_file_range(struct 
btrfs_trans_handle *trans,
                        btrfs_drop_extent_cache(inode, start,
                                                start + ram_size - 1, 0);
                }
+               if (ret)
+                       goto out_reserve;
 
                cur_alloc_size = ins.offset;
                ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
                                               ram_size, cur_alloc_size, 0);
-               BUG_ON(ret); /* -ENOMEM */
+               if (ret)
+                       goto out_reserve;
 
                if (root->root_key.objectid ==
                    BTRFS_DATA_RELOC_TREE_OBJECTID) {
@@ -962,7 +966,7 @@ static noinline int __cow_file_range(struct 
btrfs_trans_handle *trans,
                                                      cur_alloc_size);
                        if (ret) {
                                btrfs_abort_transaction(trans, root, ret);
-                               goto out_unlock;
+                               goto out_reserve;
                        }
                }
 
@@ -991,6 +995,8 @@ static noinline int __cow_file_range(struct 
btrfs_trans_handle *trans,
 out:
        return ret;
 
+out_reserve:
+       btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
 out_unlock:
        extent_clear_unlock_delalloc(inode,
                     &BTRFS_I(inode)->io_tree,
-- 
1.7.7.6

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to