Skip to contentSkip to conversation

btrfs: alloc_chunk: fix DUP stripe size handling

PATCH In review Hans van Kranenburg 2 participants 3 messages Feb 5, 2018 – Feb 14, 2018 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: alloc_chunk: fix DUP stripe size handling

In case of using DUP, we search for enough unallocated disk space on a device to hold two stripes.

The devices_info[ndevs-1].max_avail that holds the amount of unallocated space found is directly assigned to stripe_size, while it's actually twice the stripe size.

Later on in the code, an unconditional division of stripe_size by dev_stripes corrects the value, but in the meantime there's a check to see if the stripe_size does not exceed max_chunk_size. Since during this check stripe_size is twice the amount as intended, the check will reduce the stripe_size to max_chunk_size if the actual correct to be used stripe_size is more than half the amount of max_chunk_size.

The unconditional division later tries to correct stripe_size, but will actually make sure we can't allocate more than half the max_chunk_size.

Fix this by moving the division by dev_stripes before the max chunk size check, so it always contains the right value, instead of putting a duct tape division in further on to get it fixed again.

Since in all other cases than DUP, dev_stripes is 1, this change only affects DUP.

Other attempts in the past were made to fix this:
* 37db63a400 "Btrfs: fix max chunk size check in chunk allocator" tried to fix the same problem, but still resulted in part of the code acting on a wrongly doubled stripe_size value.
* 86db25785a "Btrfs: fix max chunk size on raid5/6" unintentionally broke this fix again.

The real problem was already introduced with the rest of the code in 73c5de0051.

The user visible result however will be that the max chunk size for DUP will suddenly double, while it's actually acting according to the limits in the code again like it was 5 years ago.

Fixes 73c5de0051 ("btrfs: quasi-round-robin for chunk allocation")
Fixes 86db25785a ("Btrfs: fix max chunk size on raid5/6")
Cc Naohiro Aota <[email protected]>
Cc Arne Jansen <[email protected]>
Cc Chris Mason <[email protected]>
---
 fs/btrfs/volumes.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
fs/btrfs/volumes.c +1 -3
index 4006b2a1233d..a50bd02b7ada 100644
@@ -4737,7 +4737,7 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
 	 * the primary goal is to maximize the number of stripes, so use as many
 	 * devices as possible, even if the stripes are not maximum sized.
 	 */
-	stripe_size = devices_info[ndevs-1].max_avail;
+	stripe_size = div_u64(devices_info[ndevs-1].max_avail, dev_stripes);
 	num_stripes = ndevs * dev_stripes;
 
 	/*
@@ -4772,8 +4772,6 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
 			stripe_size = devices_info[ndevs-1].max_avail;
 	}
 
-	stripe_size = div_u64(stripe_size, dev_stripes);
-
 	/* align to BTRFS_STRIPE_LEN */
 	stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN);
 
Signature · 1 line
2.11.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 38 lines · Hans van Kranenburg

In case of using DUP, we search for enough unallocated disk space on a device to hold two stripes.

The devices_info[ndevs-1].max_avail that holds the amount of unallocated space found is directly assigned to stripe_size, while it's actually twice the stripe size.

Later on in the code, an unconditional division of stripe_size by dev_stripes corrects the value, but in the meantime there's a check to see if the stripe_size does not exceed max_chunk_size. Since during this check stripe_size is twice the amount as intended, the check will reduce the stripe_size to max_chunk_size if the actual correct to be used stripe_size is more than half the amount of max_chunk_size.

The unconditional division later tries to correct stripe_size, but will actually make sure we can't allocate more than half the max_chunk_size.

Fix this by moving the division by dev_stripes before the max chunk size check, so it always contains the right value, instead of putting a duct tape division in further on to get it fixed again.

Since in all other cases than DUP, dev_stripes is 1, this change only affects DUP.

Other attempts in the past were made to fix this:
* 37db63a400 "Btrfs: fix max chunk size check in chunk allocator" tried to fix the same problem, but still resulted in part of the code acting on a wrongly doubled stripe_size value.
* 86db25785a "Btrfs: fix max chunk size on raid5/6" unintentionally broke this fix again.

The real problem was already introduced with the rest of the code in 73c5de0051.

The user visible result however will be that the max chunk size for DUP will suddenly double, while it's actually acting according to the limits in the code again like it was 5 years ago.

Quoted 8 lines
Fixes 73c5de0051 ("btrfs: quasi-round-robin for chunk allocation")
Fixes 86db25785a ("Btrfs: fix max chunk size on raid5/6")
Cc Naohiro Aota <[email protected]>
Cc Arne Jansen <[email protected]>
Cc Chris Mason <[email protected]>

I guess half of the addresses have bounced :) Have you used the get_maintainer.pl script?

The fix is short, I had to read the allocator code again so it took me longer to review it. Your description in the changelog was really helpful.

Quoted 9 lines
---
 fs/btrfs/volumes.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
fs/btrfs/volumes.c +0 -0
index 4006b2a1233d..a50bd02b7ada 100644

fs/btrfs/volumes.c static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, · line 4740

Quoted 5 lines
fs/btrfs/volumes.c +1 -1
@@ -4737,7 +4737,7 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
 	 * the primary goal is to maximize the number of stripes, so use as many
 	 * devices as possible, even if the stripes are not maximum sized.
 	 */
-	stripe_size = devices_info[ndevs-1].max_avail;
+	stripe_size = div_u64(devices_info[ndevs-1].max_avail, dev_stripes);

I'll enhance the comment above with more explanation why do it here, otherwise consider this

Quoted 19 lines
fs/btrfs/volumes.c +0 -2
 	num_stripes = ndevs * dev_stripes;
 
 	/*
@@ -4772,8 +4772,6 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
 			stripe_size = devices_info[ndevs-1].max_avail;
 	}
 
-	stripe_size = div_u64(stripe_size, dev_stripes);
-
 	/* align to BTRFS_STRIPE_LEN */
 	stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN);
 
Signature · 6 lines
2.11.0

--
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
#2 lore raw
Quoted 48 lines · David Sterba
Quoted 38 lines · Hans van Kranenburg

In case of using DUP, we search for enough unallocated disk space on a device to hold two stripes.

The devices_info[ndevs-1].max_avail that holds the amount of unallocated space found is directly assigned to stripe_size, while it's actually twice the stripe size.

Later on in the code, an unconditional division of stripe_size by dev_stripes corrects the value, but in the meantime there's a check to see if the stripe_size does not exceed max_chunk_size. Since during this check stripe_size is twice the amount as intended, the check will reduce the stripe_size to max_chunk_size if the actual correct to be used stripe_size is more than half the amount of max_chunk_size.

The unconditional division later tries to correct stripe_size, but will actually make sure we can't allocate more than half the max_chunk_size.

Fix this by moving the division by dev_stripes before the max chunk size check, so it always contains the right value, instead of putting a duct tape division in further on to get it fixed again.

Since in all other cases than DUP, dev_stripes is 1, this change only affects DUP.

Other attempts in the past were made to fix this:
* 37db63a400 "Btrfs: fix max chunk size check in chunk allocator" tried to fix the same problem, but still resulted in part of the code acting on a wrongly doubled stripe_size value.
* 86db25785a "Btrfs: fix max chunk size on raid5/6" unintentionally broke this fix again.

The real problem was already introduced with the rest of the code in 73c5de0051.

The user visible result however will be that the max chunk size for DUP will suddenly double, while it's actually acting according to the limits in the code again like it was 5 years ago.

Quoted 8 lines
Fixes 73c5de0051 ("btrfs: quasi-round-robin for chunk allocation")
Fixes 86db25785a ("Btrfs: fix max chunk size on raid5/6")
Cc Naohiro Aota <[email protected]>
Cc Arne Jansen <[email protected]>
Cc Chris Mason <[email protected]>
Quoted 2 lines

I guess half of the addresses have bounced :) Have you used the get_maintainer.pl script?

Yes, indeed, :s I was copy/pasting addresses too quickly from old commits which touched the relevant pieces of the code in the past and only looked at the names. :|

And no, I didn't use get_maintainer.pl, thanks for pointing at it.

Instead of sending out even more spam when I saw it shortly after, I waited for first feedback.

I corrected (I think) addresses in the To of this mail now. Can you fix them up at the end of the commit message? Your other comments indicate you're not asking for a v2.

Quoted 3 lines

The fix is short, I had to read the allocator code again so it took me longer to review it. Your description in the changelog was really helpful.

Thanks. Yes, it's already a while ago the other mail thread about this was going on, and it didn't end up with a final patch yet.

I started digging into the issue back then because it matched some behaviour I'd seen before: 512MB dup metadata chunks being replaced with the same number of 1024MB chunks when doing a conversion to single, which was a bit weird... And this explained it.

Quoted 4 lines
Quoted 4 lines
---
 fs/btrfs/volumes.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 12 lines

fs/btrfs/volumes.c static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, · line 4740

Quoted 10 lines
fs/btrfs/volumes.c +1 -1
index 4006b2a1233d..a50bd02b7ada 100644
@@ -4737,7 +4737,7 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
 	 * the primary goal is to maximize the number of stripes, so use as many
 	 * devices as possible, even if the stripes are not maximum sized.
 	 */
-	stripe_size = devices_info[ndevs-1].max_avail;
+	stripe_size = div_u64(devices_info[ndevs-1].max_avail, dev_stripes);

I'll enhance the comment above with more explanation why do it here,

Because it *is* the actual stripe_size. :) Feel free to enhance!

Quoted 3 lines

otherwise consider this

Yay!

Quoted 19 lines
Quoted 19 lines
fs/btrfs/volumes.c +0 -2
 	num_stripes = ndevs * dev_stripes;
 
 	/*
@@ -4772,8 +4772,6 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
 			stripe_size = devices_info[ndevs-1].max_avail;
 	}
 
-	stripe_size = div_u64(stripe_size, dev_stripes);
-
 	/* align to BTRFS_STRIPE_LEN */
 	stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN);
 
Signature · 6 lines
2.11.0

--
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
Signature · 1 line
Hans van Kranenburg