5f515ca
From: Chris Mason <clm@fb.com>
5f515ca
Date: Thu, 11 Jun 2015 18:06:51 +0200
5f515ca
Subject: [PATCH] Btrfs: fix regression in raid level conversion
5f515ca
5f515ca
Commit 2f0810880f082fa8ba66ab2c33b02e4ff9770a5e changed
5f515ca
btrfs_set_block_group_ro to avoid trying to allocate new chunks with the
5f515ca
new raid profile during conversion.  This fixed failures when there was
5f515ca
no space on the drive to allocate a new chunk, but the metadata
5f515ca
reserves were sufficient to continue the conversion.
5f515ca
5f515ca
But this ended up causing a regression when the drive had plenty of
5f515ca
space to allocate new chunks, mostly because reduce_alloc_profile isn't
5f515ca
using the new raid profile.
5f515ca
5f515ca
Fixing btrfs_reduce_alloc_profile is a bigger patch.  For now, do a
5f515ca
partial revert of 2f0810880, and don't error out if we hit ENOSPC.
5f515ca
5f515ca
Signed-off-by: Chris Mason <clm@fb.com>
5f515ca
Tested-by: Dave Sterba <dsterba@suse.cz>
5f515ca
Reported-by: Holger Hoffstaette <holger.hoffstaette@googlemail.com>
5f515ca
[adapted for stable kernel branch, v4.0.5]
5f515ca
Signed-off-by: David Sterba <dsterba@suse.cz>
5f515ca
---
5f515ca
 fs/btrfs/extent-tree.c | 18 ++++++++++++++++++
5f515ca
 1 file changed, 18 insertions(+)
5f515ca
5f515ca
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
5f515ca
index 8b33da6ec3dd..63be2a96ed6a 100644
5f515ca
--- a/fs/btrfs/extent-tree.c
5f515ca
+++ b/fs/btrfs/extent-tree.c
5f515ca
@@ -8535,6 +8535,24 @@ int btrfs_set_block_group_ro(struct btrfs_root *root,
5f515ca
 	trans = btrfs_join_transaction(root);
5f515ca
 	if (IS_ERR(trans))
5f515ca
 		return PTR_ERR(trans);
5f515ca
+	/*
5f515ca
+	 * if we are changing raid levels, try to allocate a corresponding
5f515ca
+	 * block group with the new raid level.
5f515ca
+	 */
5f515ca
+	alloc_flags = update_block_group_flags(root, cache->flags);
5f515ca
+	if (alloc_flags != cache->flags) {
5f515ca
+		ret = do_chunk_alloc(trans, root, alloc_flags,
5f515ca
+				     CHUNK_ALLOC_FORCE);
5f515ca
+		/*
5f515ca
+		 * ENOSPC is allowed here, we may have enough space
5f515ca
+		 * already allocated at the new raid level to
5f515ca
+		 * carry on
5f515ca
+		 */
5f515ca
+		if (ret == -ENOSPC)
5f515ca
+			ret = 0;
5f515ca
+		if (ret < 0)
5f515ca
+			goto out;
5f515ca
+	}
5f515ca
 
5f515ca
 	ret = set_block_group_ro(cache, 0);
5f515ca
 	if (!ret)