| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
from btrfs.ctree import ( |
| 20 |
BLOCK_GROUP_DATA, BLOCK_GROUP_SYSTEM, BLOCK_GROUP_METADATA, |
| 21 |
SPACE_INFO_GLOBAL_RSV, BLOCK_GROUP_TYPE_MASK, |
| 22 |
BLOCK_GROUP_RAID0, BLOCK_GROUP_RAID1, BLOCK_GROUP_RAID5, |
| 23 |
BLOCK_GROUP_RAID1C3, BLOCK_GROUP_RAID1C4, |
| 24 |
BLOCK_GROUP_RAID6, BLOCK_GROUP_DUP, BLOCK_GROUP_RAID10, |
| 25 |
BLOCK_GROUP_SINGLE, |
| 26 |
BLOCK_GROUP_PROFILE_MASK, |
| 27 |
) |
| 28 |
from btrfs.utils import SZ_1G |
| 29 |
from collections import namedtuple |
| 30 |
|
| 31 |
BTRFS_MAX_DATA_CHUNK_SIZE = 10 * SZ_1G |
| 32 |
|
| 33 |
_RaidAttr = namedtuple('RaidAttr', [ |
| 34 |
'sub_stripes', 'dev_stripes', 'devs_max', 'devs_min', 'tolerated_failures', |
| 35 |
'devs_increment', 'ncopies', 'nparity', 'raid_name', 'bg_flag', |
| 36 |
]) |
| 37 |
|
| 38 |
RAID_RAID10 = 0 |
| 39 |
RAID_RAID1 = 1 |
| 40 |
RAID_RAID1C3 = 2 |
| 41 |
RAID_RAID1C4 = 3 |
| 42 |
RAID_DUP = 4 |
| 43 |
RAID_RAID0 = 5 |
| 44 |
RAID_SINGLE = 6 |
| 45 |
RAID_RAID5 = 7 |
| 46 |
RAID_RAID6 = 8 |
| 47 |
|
| 48 |
|
| 49 |
def _bg_flags_to_raid_index(flags): |
| 50 |
"""Convert block group flags to an index to access _raid_array""" |
| 51 |
if flags & BLOCK_GROUP_RAID10: |
| 52 |
return RAID_RAID10 |
| 53 |
if flags & BLOCK_GROUP_RAID1: |
| 54 |
return RAID_RAID1 |
| 55 |
if flags & BLOCK_GROUP_RAID1C3: |
| 56 |
return RAID_RAID1C3 |
| 57 |
if flags & BLOCK_GROUP_RAID1C4: |
| 58 |
return RAID_RAID1C4 |
| 59 |
if flags & BLOCK_GROUP_DUP: |
| 60 |
return RAID_DUP |
| 61 |
if flags & BLOCK_GROUP_RAID0: |
| 62 |
return RAID_RAID0 |
| 63 |
if flags & BLOCK_GROUP_RAID5: |
| 64 |
return RAID_RAID5 |
| 65 |
if flags & BLOCK_GROUP_RAID6: |
| 66 |
return RAID_RAID6 |
| 67 |
return RAID_SINGLE |
| 68 |
|
| 69 |
|
| 70 |
_raid_array = [ |
| 71 |
_RaidAttr( |
| 72 |
sub_stripes=2, |
| 73 |
dev_stripes=1, |
| 74 |
devs_max=0, |
| 75 |
devs_min=4, |
| 76 |
tolerated_failures=1, |
| 77 |
devs_increment=2, |
| 78 |
ncopies=2, |
| 79 |
nparity=0, |
| 80 |
raid_name='raid10', |
| 81 |
bg_flag=BLOCK_GROUP_RAID10, |
| 82 |
), |
| 83 |
_RaidAttr( |
| 84 |
sub_stripes=1, |
| 85 |
dev_stripes=1, |
| 86 |
devs_max=2, |
| 87 |
devs_min=2, |
| 88 |
tolerated_failures=1, |
| 89 |
devs_increment=2, |
| 90 |
ncopies=2, |
| 91 |
nparity=0, |
| 92 |
raid_name='raid1', |
| 93 |
bg_flag=BLOCK_GROUP_RAID1, |
| 94 |
), |
| 95 |
_RaidAttr( |
| 96 |
sub_stripes=1, |
| 97 |
dev_stripes=1, |
| 98 |
devs_max=3, |
| 99 |
devs_min=3, |
| 100 |
tolerated_failures=2, |
| 101 |
devs_increment=3, |
| 102 |
ncopies=3, |
| 103 |
nparity=0, |
| 104 |
raid_name='raid1c3', |
| 105 |
bg_flag=BLOCK_GROUP_RAID1C3, |
| 106 |
), |
| 107 |
_RaidAttr( |
| 108 |
sub_stripes=1, |
| 109 |
dev_stripes=1, |
| 110 |
devs_max=4, |
| 111 |
devs_min=4, |
| 112 |
tolerated_failures=3, |
| 113 |
devs_increment=4, |
| 114 |
ncopies=4, |
| 115 |
nparity=0, |
| 116 |
raid_name='raid1c4', |
| 117 |
bg_flag=BLOCK_GROUP_RAID1C4, |
| 118 |
), |
| 119 |
_RaidAttr( |
| 120 |
sub_stripes=1, |
| 121 |
dev_stripes=2, |
| 122 |
devs_max=1, |
| 123 |
devs_min=1, |
| 124 |
tolerated_failures=0, |
| 125 |
devs_increment=1, |
| 126 |
ncopies=2, |
| 127 |
nparity=0, |
| 128 |
raid_name='dup', |
| 129 |
bg_flag=BLOCK_GROUP_DUP, |
| 130 |
), |
| 131 |
_RaidAttr( |
| 132 |
sub_stripes=1, |
| 133 |
dev_stripes=1, |
| 134 |
devs_max=0, |
| 135 |
devs_min=2, |
| 136 |
tolerated_failures=0, |
| 137 |
devs_increment=1, |
| 138 |
ncopies=1, |
| 139 |
nparity=0, |
| 140 |
raid_name='raid0', |
| 141 |
bg_flag=BLOCK_GROUP_RAID0, |
| 142 |
), |
| 143 |
_RaidAttr( |
| 144 |
sub_stripes=1, |
| 145 |
dev_stripes=1, |
| 146 |
devs_max=1, |
| 147 |
devs_min=1, |
| 148 |
tolerated_failures=0, |
| 149 |
devs_increment=1, |
| 150 |
ncopies=1, |
| 151 |
nparity=0, |
| 152 |
raid_name='single', |
| 153 |
bg_flag=BLOCK_GROUP_SINGLE, |
| 154 |
), |
| 155 |
_RaidAttr( |
| 156 |
sub_stripes=1, |
| 157 |
dev_stripes=1, |
| 158 |
devs_max=0, |
| 159 |
devs_min=2, |
| 160 |
tolerated_failures=1, |
| 161 |
devs_increment=1, |
| 162 |
ncopies=1, |
| 163 |
nparity=1, |
| 164 |
raid_name='raid5', |
| 165 |
bg_flag=BLOCK_GROUP_RAID5, |
| 166 |
), |
| 167 |
_RaidAttr( |
| 168 |
sub_stripes=1, |
| 169 |
dev_stripes=1, |
| 170 |
devs_max=0, |
| 171 |
devs_min=3, |
| 172 |
tolerated_failures=2, |
| 173 |
devs_increment=1, |
| 174 |
ncopies=1, |
| 175 |
nparity=2, |
| 176 |
raid_name='raid6', |
| 177 |
bg_flag=BLOCK_GROUP_RAID6, |
| 178 |
), |
| 179 |
] |
| 180 |
|
| 181 |
|
| 182 |
def _raid_attrs(flags): |
| 183 |
return _raid_array[_bg_flags_to_raid_index(flags)] |
| 184 |
|
| 185 |
|
| 186 |
def chunk_length_to_dev_extent_length(flags, num_stripes, chunk_length): |
| 187 |
"""Given information about a :class:`~btrfs.ctree.Chunk`, calculate the |
| 188 |
length of :class:`~btrfs.ctree.DevExtent` objects that store the Chunk |
| 189 |
data. |
| 190 |
|
| 191 |
For example, a Data, RAID6 block group of 8GiB, distributed over 6 devices, |
| 192 |
will occupy 2GiB * (4+2) = 12 GiB physical allocated bytes since it has |
| 193 |
4GiB of allocated bytes reserved for parity: |
| 194 |
|
| 195 |
Example:: |
| 196 |
|
| 197 |
>>> btrfs.utils.pretty_size( |
| 198 |
... btrfs.volumes.chunk_length_to_dev_extent_length( |
| 199 |
... btrfs.ctree.BLOCK_GROUP_RAID6, 6, 8 * btrfs.utils.SZ_1G)) |
| 200 |
'2.00GiB' |
| 201 |
""" |
| 202 |
|
| 203 |
|
| 204 |
attrs = _raid_attrs(flags & BLOCK_GROUP_PROFILE_MASK) |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
num_data_stripes = num_stripes - attrs.nparity |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
raw_data_bytes = chunk_length * attrs.ncopies |
| 223 |
dev_extent_length = raw_data_bytes // num_data_stripes |
| 224 |
return dev_extent_length |
| 225 |
|
| 226 |
|
| 227 |
def chunk_to_dev_extent_length(chunk): |
| 228 |
"""Given a :class:`~btrfs.ctree.Chunk` object, calculate the length of |
| 229 |
:class:`~btrfs.ctree.DevExtent` objects that store the chunk data. |
| 230 |
|
| 231 |
Example:: |
| 232 |
|
| 233 |
>>> with btrfs.FileSystem('/') as fs: |
| 234 |
... chunk = list(fs.chunks(min_vaddr=3250585600, nr_items=1))[0] |
| 235 |
... print(chunk) |
| 236 |
... dev_extent_length = btrfs.volumes.chunk_to_dev_extent_length(chunk) |
| 237 |
... print("device extent length is {}".format( |
| 238 |
... btrfs.utils.pretty_size(dev_extent_length))) |
| 239 |
... |
| 240 |
chunk vaddr 3250585600 type DATA|RAID5 length 2147483648 num_stripes 3 |
| 241 |
device extent length is 1.00GiB |
| 242 |
""" |
| 243 |
return chunk_length_to_dev_extent_length(chunk.type, chunk.num_stripes, chunk.length) |
| 244 |
|
| 245 |
|
| 246 |
def dev_extent_length_to_chunk_length(flags, num_stripes, stripe_size): |
| 247 |
"""This function simply reverses the calculation of |
| 248 |
:class:`~btrfs.ctree.Chunk` length to :class:`~btrfs.ctree.DevExtent` |
| 249 |
length. |
| 250 |
|
| 251 |
Example:: |
| 252 |
|
| 253 |
>>> btrfs.utils.pretty_size( |
| 254 |
... btrfs.volumes.dev_extent_length_to_chunk_length( |
| 255 |
... btrfs.ctree.BLOCK_GROUP_RAID6, 6, 2 * btrfs.utils.SZ_1G)) |
| 256 |
'8.00GiB' |
| 257 |
""" |
| 258 |
attrs = _raid_attrs(flags & BLOCK_GROUP_PROFILE_MASK) |
| 259 |
num_data_stripes = num_stripes - attrs.nparity |
| 260 |
|
| 261 |
raw_data_bytes = stripe_size * num_data_stripes |
| 262 |
chunk_length = raw_data_bytes // attrs.ncopies |
| 263 |
return chunk_length |
| 264 |
|
| 265 |
|
| 266 |
def chunk_to_raw_parity_bytes(chunk): |
| 267 |
"""Given a :class:`~btrfs.ctree.Chunk` object, calculate how many bytes are |
| 268 |
reserved for storing parity data for RAID56 profiles. |
| 269 |
|
| 270 |
This number is relevant to understand how much raw disk space that is |
| 271 |
allocated but not used is actually not usable for data, because it's |
| 272 |
reserved for parity. |
| 273 |
|
| 274 |
Example:: |
| 275 |
|
| 276 |
>>> with btrfs.FileSystem('/mnt/tutorial') as fs: |
| 277 |
... chunk = list(fs.chunks(min_vaddr=3250585600, nr_items=1))[0] |
| 278 |
... print(chunk) |
| 279 |
... parity_bytes = btrfs.volumes.chunk_to_raw_parity_bytes(chunk) |
| 280 |
... print("amount of raw parity bytes: {}".format( |
| 281 |
... btrfs.utils.pretty_size(parity_bytes))) |
| 282 |
... |
| 283 |
chunk vaddr 3250585600 type DATA|RAID5 length 2147483648 num_stripes 3 |
| 284 |
amount of raw parity bytes: 1.00GiB |
| 285 |
""" |
| 286 |
dev_extent_length = chunk_to_dev_extent_length(chunk) |
| 287 |
attrs = _raid_attrs(chunk.type & BLOCK_GROUP_PROFILE_MASK) |
| 288 |
return dev_extent_length * attrs.nparity |
| 289 |
|
| 290 |
|
| 291 |
def block_group_profile_ncopies(flags): |
| 292 |
"""This function returns how many times the actual data is replicated on |
| 293 |
disk given block group flags as input. E.g. for RAID1 this is 2, but for |
| 294 |
RAID5, this is just 1. The actual data is stored once in case of RAID5, and |
| 295 |
the redundancy is done using parity blocks, not data itself. |
| 296 |
|
| 297 |
Example:: |
| 298 |
|
| 299 |
>>> btrfs.volumes.block_group_profile_ncopies(btrfs.ctree.BLOCK_GROUP_RAID10) |
| 300 |
2 |
| 301 |
""" |
| 302 |
attrs = _raid_attrs(flags & BLOCK_GROUP_PROFILE_MASK) |
| 303 |
return attrs.ncopies |