[PATCH AUTOSEL for 4.4 15/35] btrfs: fix deadlock when writing out space cache

From: Sasha Levin
Date: Tue Jan 23 2018 - 23:28:46 EST


From: Josef Bacik <jbacik@xxxxxx>

[ Upstream commit b77000ed558daa3bef0899d29bf171b8c9b5e6a8 ]

If we fail to prepare our pages for whatever reason (out of memory in
our case) we need to make sure to drop the block_group->data_rwsem,
otherwise hilarity ensues.

Signed-off-by: Josef Bacik <jbacik@xxxxxx>
Reviewed-by: Omar Sandoval <osandov@xxxxxx>
Reviewed-by: Liu Bo <bo.li.liu@xxxxxxxxxx>
Reviewed-by: David Sterba <dsterba@xxxxxxxx>
[ add label and use existing unlocking code ]
Signed-off-by: David Sterba <dsterba@xxxxxxxx>

Signed-off-by: Sasha Levin <alexander.levin@xxxxxxxxxxxxx>
---
fs/btrfs/free-space-cache.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c
index cfe99bec49de..45934deacfd7 100644
--- a/fs/btrfs/free-space-cache.c
+++ b/fs/btrfs/free-space-cache.c
@@ -1258,7 +1258,7 @@ static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
/* Lock all pages first so we can lock the extent safely. */
ret = io_ctl_prepare_pages(io_ctl, inode, 0);
if (ret)
- goto out;
+ goto out_unlock;

lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
0, &cached_state);
@@ -1351,6 +1351,7 @@ out_nospc_locked:
out_nospc:
cleanup_write_cache_enospc(inode, io_ctl, &cached_state, &bitmap_list);

+out_unlock:
if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
up_write(&block_group->data_rwsem);

--
2.11.0