Skip to content

Commit 92e222d

Browse files
knorriekdave
authored andcommitted
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: * 37db63a "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. * 86db257 "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 73c5de0. 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. Reported-by: Naohiro Aota <naohiro.aota@wdc.com> Link: https://www.spinics.net/lists/linux-btrfs/msg69752.html Fixes: 73c5de0 ("btrfs: quasi-round-robin for chunk allocation") Fixes: 86db257 ("Btrfs: fix max chunk size on raid5/6") Signed-off-by: Hans van Kranenburg <hans.van.kranenburg@mendix.com> Reviewed-by: David Sterba <dsterba@suse.com> [ update comment ] Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 765f3ce commit 92e222d

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

fs/btrfs/volumes.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4829,10 +4829,13 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
48294829
ndevs = min(ndevs, devs_max);
48304830

48314831
/*
4832-
* the primary goal is to maximize the number of stripes, so use as many
4833-
* devices as possible, even if the stripes are not maximum sized.
4832+
* The primary goal is to maximize the number of stripes, so use as
4833+
* many devices as possible, even if the stripes are not maximum sized.
4834+
*
4835+
* The DUP profile stores more than one stripe per device, the
4836+
* max_avail is the total size so we have to adjust.
48344837
*/
4835-
stripe_size = devices_info[ndevs-1].max_avail;
4838+
stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes);
48364839
num_stripes = ndevs * dev_stripes;
48374840

48384841
/*
@@ -4867,8 +4870,6 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
48674870
stripe_size = devices_info[ndevs-1].max_avail;
48684871
}
48694872

4870-
stripe_size = div_u64(stripe_size, dev_stripes);
4871-
48724873
/* align to BTRFS_STRIPE_LEN */
48734874
stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN);
48744875

0 commit comments

Comments
 (0)