[5/6] btrfs: introduce nparity raid_attr
diff mbox series

Message ID 20181004212443.26519-6-hans.van.kranenburg@mendix.com
State New
Headers show
Series
  • Chunk allocator DUP fix and cleanups
Related show

Commit Message

Hans van Kranenburg Oct. 4, 2018, 9:24 p.m. UTC
Instead of hardcoding exceptions for RAID5 and RAID6 in the code, use an
nparity field in raid_attr.

Signed-off-by: Hans van Kranenburg <hans.van.kranenburg@mendix.com>
---
 fs/btrfs/volumes.c | 18 +++++++++++-------
 fs/btrfs/volumes.h |  2 ++
 2 files changed, 13 insertions(+), 7 deletions(-)

Comments

Hans van Kranenburg Oct. 4, 2018, 10:21 p.m. UTC | #1
On 10/04/2018 11:24 PM, Hans van Kranenburg wrote:
> Instead of hardcoding exceptions for RAID5 and RAID6 in the code, use an
> nparity field in raid_attr.
> 
> Signed-off-by: Hans van Kranenburg <hans.van.kranenburg@mendix.com>
> ---
>  fs/btrfs/volumes.c | 18 +++++++++++-------
>  fs/btrfs/volumes.h |  2 ++
>  2 files changed, 13 insertions(+), 7 deletions(-)
> 
> diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
> index d82b3d735ebe..453046497ac8 100644
> --- a/fs/btrfs/volumes.c
> +++ b/fs/btrfs/volumes.c
> @@ -37,6 +37,7 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
>  		.tolerated_failures = 1,
>  		.devs_increment	= 2,
>  		.ncopies	= 2,
> +		.nparity        = 0,
>  		.raid_name	= "raid10",
>  		.bg_flag	= BTRFS_BLOCK_GROUP_RAID10,
>  		.mindev_error	= BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET,
> @@ -49,6 +50,7 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
>  		.tolerated_failures = 1,
>  		.devs_increment	= 2,
>  		.ncopies	= 2,
> +		.nparity        = 0,
>  		.raid_name	= "raid1",
>  		.bg_flag	= BTRFS_BLOCK_GROUP_RAID1,
>  		.mindev_error	= BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET,
> @@ -61,6 +63,7 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
>  		.tolerated_failures = 0,
>  		.devs_increment	= 1,
>  		.ncopies	= 2,
> +		.nparity        = 0,
>  		.raid_name	= "dup",
>  		.bg_flag	= BTRFS_BLOCK_GROUP_DUP,
>  		.mindev_error	= 0,
> @@ -73,6 +76,7 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
>  		.tolerated_failures = 0,
>  		.devs_increment	= 1,
>  		.ncopies	= 1,
> +		.nparity        = 0,
>  		.raid_name	= "raid0",
>  		.bg_flag	= BTRFS_BLOCK_GROUP_RAID0,
>  		.mindev_error	= 0,
> @@ -85,6 +89,7 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
>  		.tolerated_failures = 0,
>  		.devs_increment	= 1,
>  		.ncopies	= 1,
> +		.nparity        = 0,
>  		.raid_name	= "single",
>  		.bg_flag	= 0,
>  		.mindev_error	= 0,
> @@ -97,6 +102,7 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
>  		.tolerated_failures = 1,
>  		.devs_increment	= 1,
>  		.ncopies	= 1,
> +		.nparity        = 2,

Ahem, this obviously needs to be 1. Another reorganize/rebase-fail.

>  		.raid_name	= "raid5",
>  		.bg_flag	= BTRFS_BLOCK_GROUP_RAID5,
>  		.mindev_error	= BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET,
> @@ -109,6 +115,7 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
>  		.tolerated_failures = 2,
>  		.devs_increment	= 1,
>  		.ncopies	= 1,
> +		.nparity        = 2,
>  		.raid_name	= "raid6",
>  		.bg_flag	= BTRFS_BLOCK_GROUP_RAID6,
>  		.mindev_error	= BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET,
> @@ -4597,6 +4604,8 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
>  	int devs_min;		/* min devs needed */
>  	int devs_increment;	/* ndevs has to be a multiple of this */
>  	int ncopies;		/* how many copies to data has */
> +	int nparity;		/* number of stripes worth of bytes to
> +				   store parity information */
>  	int ret;
>  	u64 max_stripe_size;
>  	u64 max_chunk_size;
> @@ -4623,6 +4632,7 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
>  	devs_min = btrfs_raid_array[index].devs_min;
>  	devs_increment = btrfs_raid_array[index].devs_increment;
>  	ncopies = btrfs_raid_array[index].ncopies;
> +	nparity = btrfs_raid_array[index].nparity;
>  
>  	if (type & BTRFS_BLOCK_GROUP_DATA) {
>  		max_stripe_size = SZ_1G;
> @@ -4752,13 +4762,7 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
>  	 * this will have to be fixed for RAID1 and RAID10 over
>  	 * more drives
>  	 */
> -	data_stripes = num_stripes / ncopies;
> -
> -	if (type & BTRFS_BLOCK_GROUP_RAID5)
> -		data_stripes = num_stripes - 1;
> -
> -	if (type & BTRFS_BLOCK_GROUP_RAID6)
> -		data_stripes = num_stripes - 2;
> +	data_stripes = (num_stripes - nparity) / ncopies;
>  
>  	/*
>  	 * Use the number of data stripes to figure out how big this chunk
> diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
> index 23e9285d88de..0fe005b4295a 100644
> --- a/fs/btrfs/volumes.h
> +++ b/fs/btrfs/volumes.h
> @@ -331,6 +331,8 @@ struct btrfs_raid_attr {
>  	int tolerated_failures; /* max tolerated fail devs */
>  	int devs_increment;	/* ndevs has to be a multiple of this */
>  	int ncopies;		/* how many copies to data has */
> +	int nparity;		/* number of stripes worth of bytes to
> +				   store parity information */
>  	int mindev_error;	/* error code if min devs requisite is unmet */
>  	const char raid_name[8]; /* name of the raid */
>  	u64 bg_flag;		/* block group flag of the raid */
>
Nikolay Borisov Oct. 5, 2018, 2:42 p.m. UTC | #2
On  5.10.2018 00:24, Hans van Kranenburg wrote:
> Instead of hardcoding exceptions for RAID5 and RAID6 in the code, use an
> nparity field in raid_attr.
> 
> Signed-off-by: Hans van Kranenburg <hans.van.kranenburg@mendix.com>
> ---
>  fs/btrfs/volumes.c | 18 +++++++++++-------
>  fs/btrfs/volumes.h |  2 ++
>  2 files changed, 13 insertions(+), 7 deletions(-)
> 
> diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
> index d82b3d735ebe..453046497ac8 100644
> --- a/fs/btrfs/volumes.c
> +++ b/fs/btrfs/volumes.c
> @@ -37,6 +37,7 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
>  		.tolerated_failures = 1,
>  		.devs_increment	= 2,
>  		.ncopies	= 2,
> +		.nparity        = 0,
>  		.raid_name	= "raid10",
>  		.bg_flag	= BTRFS_BLOCK_GROUP_RAID10,
>  		.mindev_error	= BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET,
> @@ -49,6 +50,7 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
>  		.tolerated_failures = 1,
>  		.devs_increment	= 2,
>  		.ncopies	= 2,
> +		.nparity        = 0,
>  		.raid_name	= "raid1",
>  		.bg_flag	= BTRFS_BLOCK_GROUP_RAID1,
>  		.mindev_error	= BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET,
> @@ -61,6 +63,7 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
>  		.tolerated_failures = 0,
>  		.devs_increment	= 1,
>  		.ncopies	= 2,
> +		.nparity        = 0,
>  		.raid_name	= "dup",
>  		.bg_flag	= BTRFS_BLOCK_GROUP_DUP,
>  		.mindev_error	= 0,
> @@ -73,6 +76,7 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
>  		.tolerated_failures = 0,
>  		.devs_increment	= 1,
>  		.ncopies	= 1,
> +		.nparity        = 0,
>  		.raid_name	= "raid0",
>  		.bg_flag	= BTRFS_BLOCK_GROUP_RAID0,
>  		.mindev_error	= 0,
> @@ -85,6 +89,7 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
>  		.tolerated_failures = 0,
>  		.devs_increment	= 1,
>  		.ncopies	= 1,
> +		.nparity        = 0,
>  		.raid_name	= "single",
>  		.bg_flag	= 0,
>  		.mindev_error	= 0,
> @@ -97,6 +102,7 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
>  		.tolerated_failures = 1,
>  		.devs_increment	= 1,
>  		.ncopies	= 1,
> +		.nparity        = 2,
>  		.raid_name	= "raid5",
>  		.bg_flag	= BTRFS_BLOCK_GROUP_RAID5,
>  		.mindev_error	= BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET,
> @@ -109,6 +115,7 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
>  		.tolerated_failures = 2,
>  		.devs_increment	= 1,
>  		.ncopies	= 1,
> +		.nparity        = 2,
>  		.raid_name	= "raid6",
>  		.bg_flag	= BTRFS_BLOCK_GROUP_RAID6,
>  		.mindev_error	= BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET,
> @@ -4597,6 +4604,8 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
>  	int devs_min;		/* min devs needed */
>  	int devs_increment;	/* ndevs has to be a multiple of this */
>  	int ncopies;		/* how many copies to data has */
> +	int nparity;		/* number of stripes worth of bytes to
> +				   store parity information */
>  	int ret;
>  	u64 max_stripe_size;
>  	u64 max_chunk_size;
> @@ -4623,6 +4632,7 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
>  	devs_min = btrfs_raid_array[index].devs_min;
>  	devs_increment = btrfs_raid_array[index].devs_increment;
>  	ncopies = btrfs_raid_array[index].ncopies;
> +	nparity = btrfs_raid_array[index].nparity;
>  
>  	if (type & BTRFS_BLOCK_GROUP_DATA) {
>  		max_stripe_size = SZ_1G;
> @@ -4752,13 +4762,7 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
>  	 * this will have to be fixed for RAID1 and RAID10 over
>  	 * more drives
>  	 */
> -	data_stripes = num_stripes / ncopies;
> -
> -	if (type & BTRFS_BLOCK_GROUP_RAID5)
> -		data_stripes = num_stripes - 1;
> -
> -	if (type & BTRFS_BLOCK_GROUP_RAID6)
> -		data_stripes = num_stripes - 2;
> +	data_stripes = (num_stripes - nparity) / ncopies;
>  
>  	/*
>  	 * Use the number of data stripes to figure out how big this chunk
> diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
> index 23e9285d88de..0fe005b4295a 100644
> --- a/fs/btrfs/volumes.h
> +++ b/fs/btrfs/volumes.h
> @@ -331,6 +331,8 @@ struct btrfs_raid_attr {
>  	int tolerated_failures; /* max tolerated fail devs */
>  	int devs_increment;	/* ndevs has to be a multiple of this */
>  	int ncopies;		/* how many copies to data has */
> +	int nparity;		/* number of stripes worth of bytes to
> +				   store parity information */

The description can be simplified to: number of parity stripes

>  	int mindev_error;	/* error code if min devs requisite is unmet */
>  	const char raid_name[8]; /* name of the raid */
>  	u64 bg_flag;		/* block group flag of the raid */
>
Hans van Kranenburg Oct. 5, 2018, 10:45 p.m. UTC | #3
On 10/05/2018 04:42 PM, Nikolay Borisov wrote:
> 
> 
> On  5.10.2018 00:24, Hans van Kranenburg wrote:
>> Instead of hardcoding exceptions for RAID5 and RAID6 in the code, use an
>> nparity field in raid_attr.
>>
>> Signed-off-by: Hans van Kranenburg <hans.van.kranenburg@mendix.com>
>> ---
>>  fs/btrfs/volumes.c | 18 +++++++++++-------
>>  fs/btrfs/volumes.h |  2 ++
>>  2 files changed, 13 insertions(+), 7 deletions(-)
>>
>> diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
>> index d82b3d735ebe..453046497ac8 100644
>> --- a/fs/btrfs/volumes.c
>> +++ b/fs/btrfs/volumes.c
>> @@ -37,6 +37,7 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
>>  		.tolerated_failures = 1,
>>  		.devs_increment	= 2,
>>  		.ncopies	= 2,
>> +		.nparity        = 0,
>>  		.raid_name	= "raid10",
>>  		.bg_flag	= BTRFS_BLOCK_GROUP_RAID10,
>>  		.mindev_error	= BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET,
>> @@ -49,6 +50,7 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
>>  		.tolerated_failures = 1,
>>  		.devs_increment	= 2,
>>  		.ncopies	= 2,
>> +		.nparity        = 0,
>>  		.raid_name	= "raid1",
>>  		.bg_flag	= BTRFS_BLOCK_GROUP_RAID1,
>>  		.mindev_error	= BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET,
>> @@ -61,6 +63,7 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
>>  		.tolerated_failures = 0,
>>  		.devs_increment	= 1,
>>  		.ncopies	= 2,
>> +		.nparity        = 0,
>>  		.raid_name	= "dup",
>>  		.bg_flag	= BTRFS_BLOCK_GROUP_DUP,
>>  		.mindev_error	= 0,
>> @@ -73,6 +76,7 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
>>  		.tolerated_failures = 0,
>>  		.devs_increment	= 1,
>>  		.ncopies	= 1,
>> +		.nparity        = 0,
>>  		.raid_name	= "raid0",
>>  		.bg_flag	= BTRFS_BLOCK_GROUP_RAID0,
>>  		.mindev_error	= 0,
>> @@ -85,6 +89,7 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
>>  		.tolerated_failures = 0,
>>  		.devs_increment	= 1,
>>  		.ncopies	= 1,
>> +		.nparity        = 0,
>>  		.raid_name	= "single",
>>  		.bg_flag	= 0,
>>  		.mindev_error	= 0,
>> @@ -97,6 +102,7 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
>>  		.tolerated_failures = 1,
>>  		.devs_increment	= 1,
>>  		.ncopies	= 1,
>> +		.nparity        = 2,
>>  		.raid_name	= "raid5",
>>  		.bg_flag	= BTRFS_BLOCK_GROUP_RAID5,
>>  		.mindev_error	= BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET,
>> @@ -109,6 +115,7 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
>>  		.tolerated_failures = 2,
>>  		.devs_increment	= 1,
>>  		.ncopies	= 1,
>> +		.nparity        = 2,
>>  		.raid_name	= "raid6",
>>  		.bg_flag	= BTRFS_BLOCK_GROUP_RAID6,
>>  		.mindev_error	= BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET,
>> @@ -4597,6 +4604,8 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
>>  	int devs_min;		/* min devs needed */
>>  	int devs_increment;	/* ndevs has to be a multiple of this */
>>  	int ncopies;		/* how many copies to data has */
>> +	int nparity;		/* number of stripes worth of bytes to
>> +				   store parity information */
>>  	int ret;
>>  	u64 max_stripe_size;
>>  	u64 max_chunk_size;
>> @@ -4623,6 +4632,7 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
>>  	devs_min = btrfs_raid_array[index].devs_min;
>>  	devs_increment = btrfs_raid_array[index].devs_increment;
>>  	ncopies = btrfs_raid_array[index].ncopies;
>> +	nparity = btrfs_raid_array[index].nparity;
>>  
>>  	if (type & BTRFS_BLOCK_GROUP_DATA) {
>>  		max_stripe_size = SZ_1G;
>> @@ -4752,13 +4762,7 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
>>  	 * this will have to be fixed for RAID1 and RAID10 over
>>  	 * more drives
>>  	 */
>> -	data_stripes = num_stripes / ncopies;
>> -
>> -	if (type & BTRFS_BLOCK_GROUP_RAID5)
>> -		data_stripes = num_stripes - 1;
>> -
>> -	if (type & BTRFS_BLOCK_GROUP_RAID6)
>> -		data_stripes = num_stripes - 2;
>> +	data_stripes = (num_stripes - nparity) / ncopies;
>>  
>>  	/*
>>  	 * Use the number of data stripes to figure out how big this chunk
>> diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
>> index 23e9285d88de..0fe005b4295a 100644
>> --- a/fs/btrfs/volumes.h
>> +++ b/fs/btrfs/volumes.h
>> @@ -331,6 +331,8 @@ struct btrfs_raid_attr {
>>  	int tolerated_failures; /* max tolerated fail devs */
>>  	int devs_increment;	/* ndevs has to be a multiple of this */
>>  	int ncopies;		/* how many copies to data has */
>> +	int nparity;		/* number of stripes worth of bytes to
>> +				   store parity information */
> 
> The description can be simplified to: number of parity stripes

Well, if 'stripe' is a synonym for 'device extent' here, then
technically that's incorrect, since all profiles using parity do
distribute the parity data over all stripes. IOW, it's not RAID4.

For the calculations in here we treat it like that, because on this
level we don't care about what goes where later, just that the total
amount of allocated bytes is right.

That's why I thought putting a little hint in there, to make the reader
think about it and then realize that it's actually not that simple. And
apparently that works. :)

> 
>>  	int mindev_error;	/* error code if min devs requisite is unmet */
>>  	const char raid_name[8]; /* name of the raid */
>>  	u64 bg_flag;		/* block group flag of the raid */
>>

Patch
diff mbox series

diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index d82b3d735ebe..453046497ac8 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -37,6 +37,7 @@  const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
 		.tolerated_failures = 1,
 		.devs_increment	= 2,
 		.ncopies	= 2,
+		.nparity        = 0,
 		.raid_name	= "raid10",
 		.bg_flag	= BTRFS_BLOCK_GROUP_RAID10,
 		.mindev_error	= BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET,
@@ -49,6 +50,7 @@  const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
 		.tolerated_failures = 1,
 		.devs_increment	= 2,
 		.ncopies	= 2,
+		.nparity        = 0,
 		.raid_name	= "raid1",
 		.bg_flag	= BTRFS_BLOCK_GROUP_RAID1,
 		.mindev_error	= BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET,
@@ -61,6 +63,7 @@  const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
 		.tolerated_failures = 0,
 		.devs_increment	= 1,
 		.ncopies	= 2,
+		.nparity        = 0,
 		.raid_name	= "dup",
 		.bg_flag	= BTRFS_BLOCK_GROUP_DUP,
 		.mindev_error	= 0,
@@ -73,6 +76,7 @@  const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
 		.tolerated_failures = 0,
 		.devs_increment	= 1,
 		.ncopies	= 1,
+		.nparity        = 0,
 		.raid_name	= "raid0",
 		.bg_flag	= BTRFS_BLOCK_GROUP_RAID0,
 		.mindev_error	= 0,
@@ -85,6 +89,7 @@  const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
 		.tolerated_failures = 0,
 		.devs_increment	= 1,
 		.ncopies	= 1,
+		.nparity        = 0,
 		.raid_name	= "single",
 		.bg_flag	= 0,
 		.mindev_error	= 0,
@@ -97,6 +102,7 @@  const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
 		.tolerated_failures = 1,
 		.devs_increment	= 1,
 		.ncopies	= 1,
+		.nparity        = 2,
 		.raid_name	= "raid5",
 		.bg_flag	= BTRFS_BLOCK_GROUP_RAID5,
 		.mindev_error	= BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET,
@@ -109,6 +115,7 @@  const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
 		.tolerated_failures = 2,
 		.devs_increment	= 1,
 		.ncopies	= 1,
+		.nparity        = 2,
 		.raid_name	= "raid6",
 		.bg_flag	= BTRFS_BLOCK_GROUP_RAID6,
 		.mindev_error	= BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET,
@@ -4597,6 +4604,8 @@  static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
 	int devs_min;		/* min devs needed */
 	int devs_increment;	/* ndevs has to be a multiple of this */
 	int ncopies;		/* how many copies to data has */
+	int nparity;		/* number of stripes worth of bytes to
+				   store parity information */
 	int ret;
 	u64 max_stripe_size;
 	u64 max_chunk_size;
@@ -4623,6 +4632,7 @@  static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
 	devs_min = btrfs_raid_array[index].devs_min;
 	devs_increment = btrfs_raid_array[index].devs_increment;
 	ncopies = btrfs_raid_array[index].ncopies;
+	nparity = btrfs_raid_array[index].nparity;
 
 	if (type & BTRFS_BLOCK_GROUP_DATA) {
 		max_stripe_size = SZ_1G;
@@ -4752,13 +4762,7 @@  static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
 	 * this will have to be fixed for RAID1 and RAID10 over
 	 * more drives
 	 */
-	data_stripes = num_stripes / ncopies;
-
-	if (type & BTRFS_BLOCK_GROUP_RAID5)
-		data_stripes = num_stripes - 1;
-
-	if (type & BTRFS_BLOCK_GROUP_RAID6)
-		data_stripes = num_stripes - 2;
+	data_stripes = (num_stripes - nparity) / ncopies;
 
 	/*
 	 * Use the number of data stripes to figure out how big this chunk
diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
index 23e9285d88de..0fe005b4295a 100644
--- a/fs/btrfs/volumes.h
+++ b/fs/btrfs/volumes.h
@@ -331,6 +331,8 @@  struct btrfs_raid_attr {
 	int tolerated_failures; /* max tolerated fail devs */
 	int devs_increment;	/* ndevs has to be a multiple of this */
 	int ncopies;		/* how many copies to data has */
+	int nparity;		/* number of stripes worth of bytes to
+				   store parity information */
 	int mindev_error;	/* error code if min devs requisite is unmet */
 	const char raid_name[8]; /* name of the raid */
 	u64 bg_flag;		/* block group flag of the raid */