There are 7 types raid-levels in btrfs, single, dup, raid0, raid1, raid10, raid5 and raid6.
To explain them clearly, let's take a look at a important array.
static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
[BTRFS_RAID_RAID10] = {
.sub_stripes = 2,
.dev_stripes = 1,
.devs_max = 0, /* 0 == as many as possible */
.devs_min = 4,
.devs_increment = 2,
.ncopies = 2,
},
[BTRFS_RAID_RAID1] = {
.sub_stripes = 1,
.dev_stripes = 1,
.devs_max = 2,
.devs_min = 2,
.devs_increment = 2,
.ncopies = 2,
},
[BTRFS_RAID_RAID0] = {
.sub_stripes = 1,
.dev_stripes = 1,
.devs_max = 0,
.devs_min = 2,
.devs_increment = 1,
.ncopies = 1,
},
[BTRFS_RAID_SINGLE] = {
.sub_stripes = 1,
.dev_stripes = 1,
.devs_max = 1,
.devs_min = 1,
.devs_increment = 1,
.ncopies = 1,
},
[BTRFS_RAID_DUP] = {
.sub_stripes = 1,
.dev_stripes = 2,
.devs_max = 1,
.devs_min = 1,
.devs_increment = 1,
.ncopies = 2,
},
[BTRFS_RAID_RAID5] = {
.sub_stripes = 1,
.dev_stripes = 1,
.devs_max = 0,
.devs_min = 2,
.devs_increment = 1,
.ncopies = 2,
},
[BTRFS_RAID_RAID6] = {
.sub_stripes = 1,
.dev_stripes = 1,
.devs_max = 0,
.devs_min = 3,
.devs_increment = 1,
.ncopies = 3,
},
};
At first, let me try to explain the every property for each raid-level.
1).@sub_stripes
It means how many devs in each sub stripe. I have to admit that this name
is very confusing, I will explain it in the figure for RAID10.
2).@dev_stripes
It means how many stripe in a dev. Specially used for DUP.
3).@devs_max
The most devs can be included in each RAID. 0 means no limit.
NOTE, this limit is for one stripe, not for a fs. Will explain it in RAID1.
4).@devs_min
The least devs have to be included in each RAID.
5).@devs_increment
When we want to add device to a btrfs, the number to devs should be N*devs_increment.
6).@ncopies
How many copies in a fs for a data.
1). RAID10.
----------------- ----------------
| A | A | | B | B |
----------------- ----------------
RAID10 == RAID1 + RAID0
There are 4 devs here for the RAID10.
@sub_stripes is 2, means there are 2 devs in each substripe.
In this case, a stripe is [AABB]. A sub stripe is [AA] or [BB].
@dev_stripes is 1, means there is 1 stripe in each dev.
In this case, only one stripe in each dev.
@devs_max is 0, means there is no limit for the dev number.
In this case, we can add devices as more as possible.
@dev_min is 4, means at least we need 4 devs for a RAID10.
@dev_increment is 2, means, when we want to add device,
at least we need 2 devs.
@ncopies is 2, means there are 2 copies in RAID10.
2). RAID1
-----------------
| A | A |
-----------------
RAID1 is simple, we have a copy for every data.
@sub_stripes is 1, means a sub stripe contain 1 dev.
@dev_stripes is 1, means one stripe in each dev,
@devs_max is 2, means there are at most 2 devs in one stripe.
It doesn't say that we can only build a RAID1 fs with 2 devs.
Actually, we can add any number of devs into a RAID1 fs. But,
each stripe only contain two devs. As a counterexample, will
show it in RAID0.
@dev_increment is 1.
@ncopies is 2, it's clear from the figure.
3). RAID0
-----------------
| A | B |
-----------------
This is a typical sample for RAID0.
@sub_stripes is 1.
@dev_stripes is 1.
@devs_max is 0. It means we can add more dev for one stripe as
below.
-------------------------
| A | B | C |
-------------------------
After adding a dev to the raid0 fs, we can create a stripe with
3 sub stripes as below. If things go on like this, we can increase
this devs in one stripe to any possible large number.
But for a RAID1, we can add a dev into a fs also, but the number of
sub stripes is still 2.
-----------------
| A | A |
-------------------------
| B | B |
-----------------
There are 3devs in the RAID1 fs, but in one stripe [AA] or [BB], there
are only 2 sub stripes in it. This is what the @devs_max in raid1 means.
@dev_increment is 1. As shown above.
@ncopies is 1. There is no copy for every data.
---------
| A |
---------
| B |
---------
| C |
---------
SINGLE is not a typical RAID level actually.
All @xxx is 1. Easy to understand.
5). DUP
---------
| A |
---------
| A |
---------
DUP is a *RAID level* in btrfs.
It means there is a copy for every data in the same dev.
The most important for DUP is @dev_stripes.
It's 2 for DUP, it means there are 2 sub stripes in one
dev. The whole stripe is [AA] and the two sub stripes
are all in the same dev.
6). RAID5
-------------------------
| A | B | Parity|
-------------------------
RAID5 means there is a sub stripe in one stripe is parity.
It means there is no copy for the data, but we have the
parity to recover it when the data is corrupt. But we need
to spend one sub stripe to store parity. If we add a dev
to the fs. it will turn into the following
---------------------------------
| A | B | C | Parity|
---------------------------------
7). RAID6
---------------------------------
| A | B |Parity1|Parity2|
---------------------------------
Similar with RAID5. But there are two sub stripes in one stripe
to store parity. parity1 and parity2 are calculated by different
parity system.