Qu Wenruo | f6377ff | 2020-06-24 18:02:54 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | #include <stdlib.h> |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 3 | #include <errno.h> |
Qu Wenruo | f6377ff | 2020-06-24 18:02:54 +0200 | [diff] [blame] | 4 | #include <fs_internal.h> |
| 5 | #include "ctree.h" |
| 6 | #include "disk-io.h" |
| 7 | #include "volumes.h" |
Qu Wenruo | 8f267cf | 2020-06-24 18:03:00 +0200 | [diff] [blame] | 8 | #include "extent-io.h" |
Qu Wenruo | f6377ff | 2020-06-24 18:02:54 +0200 | [diff] [blame] | 9 | |
| 10 | const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { |
| 11 | [BTRFS_RAID_RAID10] = { |
| 12 | .sub_stripes = 2, |
| 13 | .dev_stripes = 1, |
| 14 | .devs_max = 0, /* 0 == as many as possible */ |
| 15 | .devs_min = 4, |
| 16 | .tolerated_failures = 1, |
| 17 | .devs_increment = 2, |
| 18 | .ncopies = 2, |
| 19 | .nparity = 0, |
| 20 | .raid_name = "raid10", |
| 21 | .bg_flag = BTRFS_BLOCK_GROUP_RAID10, |
| 22 | }, |
| 23 | [BTRFS_RAID_RAID1] = { |
| 24 | .sub_stripes = 1, |
| 25 | .dev_stripes = 1, |
| 26 | .devs_max = 2, |
| 27 | .devs_min = 2, |
| 28 | .tolerated_failures = 1, |
| 29 | .devs_increment = 2, |
| 30 | .ncopies = 2, |
| 31 | .nparity = 0, |
| 32 | .raid_name = "raid1", |
| 33 | .bg_flag = BTRFS_BLOCK_GROUP_RAID1, |
| 34 | }, |
| 35 | [BTRFS_RAID_RAID1C3] = { |
| 36 | .sub_stripes = 1, |
| 37 | .dev_stripes = 1, |
| 38 | .devs_max = 3, |
| 39 | .devs_min = 3, |
| 40 | .tolerated_failures = 2, |
| 41 | .devs_increment = 3, |
| 42 | .ncopies = 3, |
| 43 | .raid_name = "raid1c3", |
| 44 | .bg_flag = BTRFS_BLOCK_GROUP_RAID1C3, |
| 45 | }, |
| 46 | [BTRFS_RAID_RAID1C4] = { |
| 47 | .sub_stripes = 1, |
| 48 | .dev_stripes = 1, |
| 49 | .devs_max = 4, |
| 50 | .devs_min = 4, |
| 51 | .tolerated_failures = 3, |
| 52 | .devs_increment = 4, |
| 53 | .ncopies = 4, |
| 54 | .raid_name = "raid1c4", |
| 55 | .bg_flag = BTRFS_BLOCK_GROUP_RAID1C4, |
| 56 | }, |
| 57 | [BTRFS_RAID_DUP] = { |
| 58 | .sub_stripes = 1, |
| 59 | .dev_stripes = 2, |
| 60 | .devs_max = 1, |
| 61 | .devs_min = 1, |
| 62 | .tolerated_failures = 0, |
| 63 | .devs_increment = 1, |
| 64 | .ncopies = 2, |
| 65 | .nparity = 0, |
| 66 | .raid_name = "dup", |
| 67 | .bg_flag = BTRFS_BLOCK_GROUP_DUP, |
| 68 | }, |
| 69 | [BTRFS_RAID_RAID0] = { |
| 70 | .sub_stripes = 1, |
| 71 | .dev_stripes = 1, |
| 72 | .devs_max = 0, |
| 73 | .devs_min = 2, |
| 74 | .tolerated_failures = 0, |
| 75 | .devs_increment = 1, |
| 76 | .ncopies = 1, |
| 77 | .nparity = 0, |
| 78 | .raid_name = "raid0", |
| 79 | .bg_flag = BTRFS_BLOCK_GROUP_RAID0, |
| 80 | }, |
| 81 | [BTRFS_RAID_SINGLE] = { |
| 82 | .sub_stripes = 1, |
| 83 | .dev_stripes = 1, |
| 84 | .devs_max = 1, |
| 85 | .devs_min = 1, |
| 86 | .tolerated_failures = 0, |
| 87 | .devs_increment = 1, |
| 88 | .ncopies = 1, |
| 89 | .nparity = 0, |
| 90 | .raid_name = "single", |
| 91 | .bg_flag = 0, |
| 92 | }, |
| 93 | [BTRFS_RAID_RAID5] = { |
| 94 | .sub_stripes = 1, |
| 95 | .dev_stripes = 1, |
| 96 | .devs_max = 0, |
| 97 | .devs_min = 2, |
| 98 | .tolerated_failures = 1, |
| 99 | .devs_increment = 1, |
| 100 | .ncopies = 1, |
| 101 | .nparity = 1, |
| 102 | .raid_name = "raid5", |
| 103 | .bg_flag = BTRFS_BLOCK_GROUP_RAID5, |
| 104 | }, |
| 105 | [BTRFS_RAID_RAID6] = { |
| 106 | .sub_stripes = 1, |
| 107 | .dev_stripes = 1, |
| 108 | .devs_max = 0, |
| 109 | .devs_min = 3, |
| 110 | .tolerated_failures = 2, |
| 111 | .devs_increment = 1, |
| 112 | .ncopies = 1, |
| 113 | .nparity = 2, |
| 114 | .raid_name = "raid6", |
| 115 | .bg_flag = BTRFS_BLOCK_GROUP_RAID6, |
| 116 | }, |
| 117 | }; |
| 118 | |
| 119 | struct stripe { |
| 120 | struct btrfs_device *dev; |
| 121 | u64 physical; |
| 122 | }; |
| 123 | |
| 124 | static inline int nr_parity_stripes(struct map_lookup *map) |
| 125 | { |
| 126 | if (map->type & BTRFS_BLOCK_GROUP_RAID5) |
| 127 | return 1; |
| 128 | else if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 129 | return 2; |
| 130 | else |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | static inline int nr_data_stripes(struct map_lookup *map) |
| 135 | { |
| 136 | return map->num_stripes - nr_parity_stripes(map); |
| 137 | } |
| 138 | |
| 139 | #define is_parity_stripe(x) ( ((x) == BTRFS_RAID5_P_STRIPE) || ((x) == BTRFS_RAID6_Q_STRIPE) ) |
| 140 | |
| 141 | static LIST_HEAD(fs_uuids); |
| 142 | |
| 143 | /* |
| 144 | * Find a device specified by @devid or @uuid in the list of @fs_devices, or |
| 145 | * return NULL. |
| 146 | * |
| 147 | * If devid and uuid are both specified, the match must be exact, otherwise |
| 148 | * only devid is used. |
| 149 | */ |
| 150 | static struct btrfs_device *find_device(struct btrfs_fs_devices *fs_devices, |
| 151 | u64 devid, u8 *uuid) |
| 152 | { |
| 153 | struct list_head *head = &fs_devices->devices; |
| 154 | struct btrfs_device *dev; |
| 155 | |
| 156 | list_for_each_entry(dev, head, dev_list) { |
| 157 | if (dev->devid == devid && |
| 158 | (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) { |
| 159 | return dev; |
| 160 | } |
| 161 | } |
| 162 | return NULL; |
| 163 | } |
| 164 | |
| 165 | static struct btrfs_fs_devices *find_fsid(u8 *fsid, u8 *metadata_uuid) |
| 166 | { |
| 167 | struct btrfs_fs_devices *fs_devices; |
| 168 | |
| 169 | list_for_each_entry(fs_devices, &fs_uuids, list) { |
| 170 | if (metadata_uuid && (memcmp(fsid, fs_devices->fsid, |
| 171 | BTRFS_FSID_SIZE) == 0) && |
| 172 | (memcmp(metadata_uuid, fs_devices->metadata_uuid, |
| 173 | BTRFS_FSID_SIZE) == 0)) { |
| 174 | return fs_devices; |
| 175 | } else if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0){ |
| 176 | return fs_devices; |
| 177 | } |
| 178 | } |
| 179 | return NULL; |
| 180 | } |
| 181 | |
| 182 | static int device_list_add(struct btrfs_super_block *disk_super, |
| 183 | u64 devid, struct blk_desc *desc, |
| 184 | struct disk_partition *part, |
| 185 | struct btrfs_fs_devices **fs_devices_ret) |
| 186 | { |
| 187 | struct btrfs_device *device; |
| 188 | struct btrfs_fs_devices *fs_devices; |
| 189 | u64 found_transid = btrfs_super_generation(disk_super); |
| 190 | bool metadata_uuid = (btrfs_super_incompat_flags(disk_super) & |
| 191 | BTRFS_FEATURE_INCOMPAT_METADATA_UUID); |
| 192 | |
| 193 | if (metadata_uuid) |
| 194 | fs_devices = find_fsid(disk_super->fsid, |
| 195 | disk_super->metadata_uuid); |
| 196 | else |
| 197 | fs_devices = find_fsid(disk_super->fsid, NULL); |
| 198 | |
| 199 | if (!fs_devices) { |
| 200 | fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS); |
| 201 | if (!fs_devices) |
| 202 | return -ENOMEM; |
| 203 | INIT_LIST_HEAD(&fs_devices->devices); |
| 204 | list_add(&fs_devices->list, &fs_uuids); |
| 205 | memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE); |
| 206 | if (metadata_uuid) |
| 207 | memcpy(fs_devices->metadata_uuid, |
| 208 | disk_super->metadata_uuid, BTRFS_FSID_SIZE); |
| 209 | else |
| 210 | memcpy(fs_devices->metadata_uuid, fs_devices->fsid, |
| 211 | BTRFS_FSID_SIZE); |
| 212 | |
| 213 | fs_devices->latest_devid = devid; |
| 214 | fs_devices->latest_trans = found_transid; |
| 215 | fs_devices->lowest_devid = (u64)-1; |
| 216 | device = NULL; |
| 217 | } else { |
| 218 | device = find_device(fs_devices, devid, |
| 219 | disk_super->dev_item.uuid); |
| 220 | } |
| 221 | if (!device) { |
| 222 | device = kzalloc(sizeof(*device), GFP_NOFS); |
| 223 | if (!device) { |
| 224 | /* we can safely leave the fs_devices entry around */ |
| 225 | return -ENOMEM; |
| 226 | } |
| 227 | device->devid = devid; |
| 228 | device->desc = desc; |
| 229 | device->part = part; |
| 230 | device->generation = found_transid; |
| 231 | memcpy(device->uuid, disk_super->dev_item.uuid, |
| 232 | BTRFS_UUID_SIZE); |
| 233 | device->total_devs = btrfs_super_num_devices(disk_super); |
| 234 | device->super_bytes_used = btrfs_super_bytes_used(disk_super); |
| 235 | device->total_bytes = |
| 236 | btrfs_stack_device_total_bytes(&disk_super->dev_item); |
| 237 | device->bytes_used = |
| 238 | btrfs_stack_device_bytes_used(&disk_super->dev_item); |
| 239 | list_add(&device->dev_list, &fs_devices->devices); |
| 240 | device->fs_devices = fs_devices; |
| 241 | } else if (!device->desc || !device->part) { |
| 242 | /* |
| 243 | * The existing device has newer generation, so this one could |
| 244 | * be a stale one, don't add it. |
| 245 | */ |
| 246 | if (found_transid < device->generation) { |
| 247 | error( |
| 248 | "adding devid %llu gen %llu but found an existing device gen %llu", |
| 249 | device->devid, found_transid, |
| 250 | device->generation); |
| 251 | return -EEXIST; |
| 252 | } else { |
| 253 | device->desc = desc; |
| 254 | device->part = part; |
| 255 | } |
| 256 | } |
| 257 | |
Qu Wenruo | f6377ff | 2020-06-24 18:02:54 +0200 | [diff] [blame] | 258 | if (found_transid > fs_devices->latest_trans) { |
| 259 | fs_devices->latest_devid = devid; |
| 260 | fs_devices->latest_trans = found_transid; |
| 261 | } |
| 262 | if (fs_devices->lowest_devid > devid) { |
| 263 | fs_devices->lowest_devid = devid; |
| 264 | } |
| 265 | *fs_devices_ret = fs_devices; |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| 270 | { |
| 271 | struct btrfs_fs_devices *seed_devices; |
| 272 | struct btrfs_device *device; |
| 273 | int ret = 0; |
| 274 | |
| 275 | again: |
| 276 | if (!fs_devices) |
| 277 | return 0; |
| 278 | while (!list_empty(&fs_devices->devices)) { |
| 279 | device = list_entry(fs_devices->devices.next, |
| 280 | struct btrfs_device, dev_list); |
| 281 | list_del(&device->dev_list); |
| 282 | /* free the memory */ |
| 283 | free(device); |
| 284 | } |
| 285 | |
| 286 | seed_devices = fs_devices->seed; |
| 287 | fs_devices->seed = NULL; |
| 288 | if (seed_devices) { |
| 289 | struct btrfs_fs_devices *orig; |
| 290 | |
| 291 | orig = fs_devices; |
| 292 | fs_devices = seed_devices; |
| 293 | list_del(&orig->list); |
| 294 | free(orig); |
| 295 | goto again; |
| 296 | } else { |
| 297 | list_del(&fs_devices->list); |
| 298 | free(fs_devices); |
| 299 | } |
| 300 | |
| 301 | return ret; |
| 302 | } |
| 303 | |
| 304 | void btrfs_close_all_devices(void) |
| 305 | { |
| 306 | struct btrfs_fs_devices *fs_devices; |
| 307 | |
| 308 | while (!list_empty(&fs_uuids)) { |
| 309 | fs_devices = list_entry(fs_uuids.next, struct btrfs_fs_devices, |
| 310 | list); |
| 311 | btrfs_close_devices(fs_devices); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices) |
| 316 | { |
| 317 | struct btrfs_device *device; |
| 318 | |
| 319 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 320 | if (!device->desc || !device->part) { |
| 321 | printf("no device found for devid %llu, skip it \n", |
| 322 | device->devid); |
| 323 | continue; |
| 324 | } |
| 325 | } |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | int btrfs_scan_one_device(struct blk_desc *desc, struct disk_partition *part, |
| 330 | struct btrfs_fs_devices **fs_devices_ret, |
| 331 | u64 *total_devs) |
| 332 | { |
| 333 | struct btrfs_super_block *disk_super; |
| 334 | char buf[BTRFS_SUPER_INFO_SIZE]; |
| 335 | int ret; |
| 336 | u64 devid; |
| 337 | |
| 338 | disk_super = (struct btrfs_super_block *)buf; |
| 339 | ret = btrfs_read_dev_super(desc, part, disk_super); |
| 340 | if (ret < 0) |
| 341 | return -EIO; |
| 342 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 343 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP) |
| 344 | *total_devs = 1; |
| 345 | else |
| 346 | *total_devs = btrfs_super_num_devices(disk_super); |
| 347 | |
| 348 | ret = device_list_add(disk_super, devid, desc, part, fs_devices_ret); |
| 349 | |
| 350 | return ret; |
| 351 | } |
| 352 | |
| 353 | struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid, |
| 354 | u8 *uuid, u8 *fsid) |
| 355 | { |
| 356 | struct btrfs_device *device; |
| 357 | struct btrfs_fs_devices *cur_devices; |
| 358 | |
| 359 | cur_devices = fs_info->fs_devices; |
| 360 | while (cur_devices) { |
| 361 | if (!fsid || |
| 362 | !memcmp(cur_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) { |
| 363 | device = find_device(cur_devices, devid, uuid); |
| 364 | if (device) |
| 365 | return device; |
| 366 | } |
| 367 | cur_devices = cur_devices->seed; |
| 368 | } |
| 369 | return NULL; |
| 370 | } |
| 371 | |
Qu Wenruo | 8f267cf | 2020-06-24 18:03:00 +0200 | [diff] [blame] | 372 | static struct btrfs_device *fill_missing_device(u64 devid) |
| 373 | { |
| 374 | struct btrfs_device *device; |
| 375 | |
| 376 | device = kzalloc(sizeof(*device), GFP_NOFS); |
| 377 | return device; |
| 378 | } |
| 379 | |
Qu Wenruo | f6377ff | 2020-06-24 18:02:54 +0200 | [diff] [blame] | 380 | /* |
| 381 | * slot == -1: SYSTEM chunk |
| 382 | * return -EIO on error, otherwise return 0 |
| 383 | */ |
| 384 | int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info, |
| 385 | struct extent_buffer *leaf, |
| 386 | struct btrfs_chunk *chunk, |
| 387 | int slot, u64 logical) |
| 388 | { |
| 389 | u64 length; |
| 390 | u64 stripe_len; |
| 391 | u16 num_stripes; |
| 392 | u16 sub_stripes; |
| 393 | u64 type; |
| 394 | u32 chunk_ondisk_size; |
| 395 | u32 sectorsize = fs_info->sectorsize; |
| 396 | |
| 397 | /* |
| 398 | * Basic chunk item size check. Note that btrfs_chunk already contains |
| 399 | * one stripe, so no "==" check. |
| 400 | */ |
| 401 | if (slot >= 0 && |
| 402 | btrfs_item_size_nr(leaf, slot) < sizeof(struct btrfs_chunk)) { |
| 403 | error("invalid chunk item size, have %u expect [%zu, %zu)", |
| 404 | btrfs_item_size_nr(leaf, slot), |
| 405 | sizeof(struct btrfs_chunk), |
| 406 | BTRFS_LEAF_DATA_SIZE(fs_info)); |
| 407 | return -EUCLEAN; |
| 408 | } |
| 409 | length = btrfs_chunk_length(leaf, chunk); |
| 410 | stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 411 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 412 | sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
| 413 | type = btrfs_chunk_type(leaf, chunk); |
| 414 | |
| 415 | if (num_stripes == 0) { |
| 416 | error("invalid num_stripes, have %u expect non-zero", |
| 417 | num_stripes); |
| 418 | return -EUCLEAN; |
| 419 | } |
| 420 | if (slot >= 0 && btrfs_chunk_item_size(num_stripes) != |
| 421 | btrfs_item_size_nr(leaf, slot)) { |
| 422 | error("invalid chunk item size, have %u expect %lu", |
| 423 | btrfs_item_size_nr(leaf, slot), |
| 424 | btrfs_chunk_item_size(num_stripes)); |
| 425 | return -EUCLEAN; |
| 426 | } |
| 427 | |
| 428 | /* |
| 429 | * These valid checks may be insufficient to cover every corner cases. |
| 430 | */ |
| 431 | if (!IS_ALIGNED(logical, sectorsize)) { |
| 432 | error("invalid chunk logical %llu", logical); |
| 433 | return -EIO; |
| 434 | } |
| 435 | if (btrfs_chunk_sector_size(leaf, chunk) != sectorsize) { |
| 436 | error("invalid chunk sectorsize %llu", |
| 437 | (unsigned long long)btrfs_chunk_sector_size(leaf, chunk)); |
| 438 | return -EIO; |
| 439 | } |
| 440 | if (!length || !IS_ALIGNED(length, sectorsize)) { |
| 441 | error("invalid chunk length %llu", length); |
| 442 | return -EIO; |
| 443 | } |
| 444 | if (stripe_len != BTRFS_STRIPE_LEN) { |
| 445 | error("invalid chunk stripe length: %llu", stripe_len); |
| 446 | return -EIO; |
| 447 | } |
| 448 | /* Check on chunk item type */ |
| 449 | if (slot == -1 && (type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) { |
| 450 | error("invalid chunk type %llu", type); |
| 451 | return -EIO; |
| 452 | } |
| 453 | if (type & ~(BTRFS_BLOCK_GROUP_TYPE_MASK | |
| 454 | BTRFS_BLOCK_GROUP_PROFILE_MASK)) { |
| 455 | error("unrecognized chunk type: %llu", |
| 456 | ~(BTRFS_BLOCK_GROUP_TYPE_MASK | |
| 457 | BTRFS_BLOCK_GROUP_PROFILE_MASK) & type); |
| 458 | return -EIO; |
| 459 | } |
| 460 | if (!(type & BTRFS_BLOCK_GROUP_TYPE_MASK)) { |
| 461 | error("missing chunk type flag: %llu", type); |
| 462 | return -EIO; |
| 463 | } |
| 464 | if (!(is_power_of_2(type & BTRFS_BLOCK_GROUP_PROFILE_MASK) || |
| 465 | (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0)) { |
| 466 | error("conflicting chunk type detected: %llu", type); |
| 467 | return -EIO; |
| 468 | } |
| 469 | if ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) && |
| 470 | !is_power_of_2(type & BTRFS_BLOCK_GROUP_PROFILE_MASK)) { |
| 471 | error("conflicting chunk profile detected: %llu", type); |
| 472 | return -EIO; |
| 473 | } |
| 474 | |
| 475 | chunk_ondisk_size = btrfs_chunk_item_size(num_stripes); |
| 476 | /* |
| 477 | * Btrfs_chunk contains at least one stripe, and for sys_chunk |
| 478 | * it can't exceed the system chunk array size |
| 479 | * For normal chunk, it should match its chunk item size. |
| 480 | */ |
| 481 | if (num_stripes < 1 || |
| 482 | (slot == -1 && chunk_ondisk_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) || |
| 483 | (slot >= 0 && chunk_ondisk_size > btrfs_item_size_nr(leaf, slot))) { |
| 484 | error("invalid num_stripes: %u", num_stripes); |
| 485 | return -EIO; |
| 486 | } |
| 487 | /* |
| 488 | * Device number check against profile |
| 489 | */ |
| 490 | if ((type & BTRFS_BLOCK_GROUP_RAID10 && (sub_stripes != 2 || |
| 491 | !IS_ALIGNED(num_stripes, sub_stripes))) || |
| 492 | (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) || |
| 493 | (type & BTRFS_BLOCK_GROUP_RAID1C3 && num_stripes < 3) || |
| 494 | (type & BTRFS_BLOCK_GROUP_RAID1C4 && num_stripes < 4) || |
| 495 | (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) || |
| 496 | (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) || |
| 497 | (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) || |
| 498 | ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 && |
| 499 | num_stripes != 1)) { |
| 500 | error("Invalid num_stripes:sub_stripes %u:%u for profile %llu", |
| 501 | num_stripes, sub_stripes, |
| 502 | type & BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 503 | return -EIO; |
| 504 | } |
| 505 | |
Qu Wenruo | 8f267cf | 2020-06-24 18:03:00 +0200 | [diff] [blame] | 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | /* |
| 510 | * Slot is used to verify the chunk item is valid |
| 511 | * |
| 512 | * For sys chunk in superblock, pass -1 to indicate sys chunk. |
| 513 | */ |
| 514 | static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key, |
| 515 | struct extent_buffer *leaf, |
| 516 | struct btrfs_chunk *chunk, int slot) |
| 517 | { |
| 518 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
| 519 | struct map_lookup *map; |
| 520 | struct cache_extent *ce; |
| 521 | u64 logical; |
| 522 | u64 length; |
| 523 | u64 devid; |
| 524 | u8 uuid[BTRFS_UUID_SIZE]; |
| 525 | int num_stripes; |
| 526 | int ret; |
| 527 | int i; |
| 528 | |
| 529 | logical = key->offset; |
| 530 | length = btrfs_chunk_length(leaf, chunk); |
| 531 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 532 | /* Validation check */ |
| 533 | ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, slot, logical); |
| 534 | if (ret) { |
| 535 | error("%s checksums match, but it has an invalid chunk, %s", |
| 536 | (slot == -1) ? "Superblock" : "Metadata", |
| 537 | (slot == -1) ? "try btrfsck --repair -s <superblock> ie, 0,1,2" : ""); |
| 538 | return ret; |
| 539 | } |
| 540 | |
| 541 | ce = search_cache_extent(&map_tree->cache_tree, logical); |
| 542 | |
| 543 | /* already mapped? */ |
| 544 | if (ce && ce->start <= logical && ce->start + ce->size > logical) { |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS); |
| 549 | if (!map) |
| 550 | return -ENOMEM; |
| 551 | |
| 552 | map->ce.start = logical; |
| 553 | map->ce.size = length; |
| 554 | map->num_stripes = num_stripes; |
| 555 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 556 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
| 557 | map->sector_size = btrfs_chunk_sector_size(leaf, chunk); |
| 558 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 559 | map->type = btrfs_chunk_type(leaf, chunk); |
| 560 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
| 561 | |
| 562 | for (i = 0; i < num_stripes; i++) { |
| 563 | map->stripes[i].physical = |
| 564 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 565 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
| 566 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 567 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 568 | BTRFS_UUID_SIZE); |
| 569 | map->stripes[i].dev = btrfs_find_device(fs_info, devid, uuid, |
| 570 | NULL); |
| 571 | if (!map->stripes[i].dev) { |
| 572 | map->stripes[i].dev = fill_missing_device(devid); |
| 573 | printf("warning, device %llu is missing\n", |
| 574 | (unsigned long long)devid); |
| 575 | list_add(&map->stripes[i].dev->dev_list, |
| 576 | &fs_info->fs_devices->devices); |
| 577 | } |
| 578 | |
| 579 | } |
| 580 | ret = insert_cache_extent(&map_tree->cache_tree, &map->ce); |
| 581 | if (ret < 0) { |
| 582 | errno = -ret; |
| 583 | error("failed to add chunk map start=%llu len=%llu: %d (%m)", |
| 584 | map->ce.start, map->ce.size, ret); |
| 585 | } |
| 586 | |
| 587 | return ret; |
| 588 | } |
| 589 | |
| 590 | static int fill_device_from_item(struct extent_buffer *leaf, |
| 591 | struct btrfs_dev_item *dev_item, |
| 592 | struct btrfs_device *device) |
| 593 | { |
| 594 | unsigned long ptr; |
| 595 | |
| 596 | device->devid = btrfs_device_id(leaf, dev_item); |
| 597 | device->total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 598 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
| 599 | device->type = btrfs_device_type(leaf, dev_item); |
| 600 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 601 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 602 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
| 603 | |
| 604 | ptr = (unsigned long)btrfs_device_uuid(dev_item); |
| 605 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
| 606 | |
Qu Wenruo | f6377ff | 2020-06-24 18:02:54 +0200 | [diff] [blame] | 607 | return 0; |
| 608 | } |
| 609 | |
Qu Wenruo | 8f267cf | 2020-06-24 18:03:00 +0200 | [diff] [blame] | 610 | static int read_one_dev(struct btrfs_fs_info *fs_info, |
| 611 | struct extent_buffer *leaf, |
| 612 | struct btrfs_dev_item *dev_item) |
| 613 | { |
| 614 | struct btrfs_device *device; |
| 615 | u64 devid; |
| 616 | int ret = 0; |
| 617 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
| 618 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 619 | |
| 620 | devid = btrfs_device_id(leaf, dev_item); |
| 621 | read_extent_buffer(leaf, dev_uuid, |
| 622 | (unsigned long)btrfs_device_uuid(dev_item), |
| 623 | BTRFS_UUID_SIZE); |
| 624 | read_extent_buffer(leaf, fs_uuid, |
| 625 | (unsigned long)btrfs_device_fsid(dev_item), |
| 626 | BTRFS_FSID_SIZE); |
| 627 | |
| 628 | if (memcmp(fs_uuid, fs_info->fs_devices->fsid, BTRFS_UUID_SIZE)) { |
| 629 | error("Seed device is not yet supported\n"); |
| 630 | return -ENOTSUPP; |
| 631 | } |
| 632 | |
| 633 | device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid); |
| 634 | if (!device) { |
| 635 | device = kzalloc(sizeof(*device), GFP_NOFS); |
| 636 | if (!device) |
| 637 | return -ENOMEM; |
| 638 | list_add(&device->dev_list, |
| 639 | &fs_info->fs_devices->devices); |
| 640 | } |
| 641 | |
| 642 | fill_device_from_item(leaf, dev_item, device); |
| 643 | fs_info->fs_devices->total_rw_bytes += |
| 644 | btrfs_device_total_bytes(leaf, dev_item); |
| 645 | return ret; |
| 646 | } |
| 647 | |
| 648 | int btrfs_read_sys_array(struct btrfs_fs_info *fs_info) |
| 649 | { |
| 650 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
| 651 | struct extent_buffer *sb; |
| 652 | struct btrfs_disk_key *disk_key; |
| 653 | struct btrfs_chunk *chunk; |
| 654 | u8 *array_ptr; |
| 655 | unsigned long sb_array_offset; |
| 656 | int ret = 0; |
| 657 | u32 num_stripes; |
| 658 | u32 array_size; |
| 659 | u32 len = 0; |
| 660 | u32 cur_offset; |
| 661 | struct btrfs_key key; |
| 662 | |
| 663 | if (fs_info->nodesize < BTRFS_SUPER_INFO_SIZE) { |
| 664 | printf("ERROR: nodesize %u too small to read superblock\n", |
| 665 | fs_info->nodesize); |
| 666 | return -EINVAL; |
| 667 | } |
| 668 | sb = alloc_dummy_extent_buffer(fs_info, BTRFS_SUPER_INFO_OFFSET, |
| 669 | BTRFS_SUPER_INFO_SIZE); |
| 670 | if (!sb) |
| 671 | return -ENOMEM; |
| 672 | btrfs_set_buffer_uptodate(sb); |
| 673 | write_extent_buffer(sb, super_copy, 0, sizeof(*super_copy)); |
| 674 | array_size = btrfs_super_sys_array_size(super_copy); |
| 675 | |
| 676 | array_ptr = super_copy->sys_chunk_array; |
| 677 | sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 678 | cur_offset = 0; |
| 679 | |
| 680 | while (cur_offset < array_size) { |
| 681 | disk_key = (struct btrfs_disk_key *)array_ptr; |
| 682 | len = sizeof(*disk_key); |
| 683 | if (cur_offset + len > array_size) |
| 684 | goto out_short_read; |
| 685 | |
| 686 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 687 | |
| 688 | array_ptr += len; |
| 689 | sb_array_offset += len; |
| 690 | cur_offset += len; |
| 691 | |
| 692 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 693 | chunk = (struct btrfs_chunk *)sb_array_offset; |
| 694 | /* |
| 695 | * At least one btrfs_chunk with one stripe must be |
| 696 | * present, exact stripe count check comes afterwards |
| 697 | */ |
| 698 | len = btrfs_chunk_item_size(1); |
| 699 | if (cur_offset + len > array_size) |
| 700 | goto out_short_read; |
| 701 | |
| 702 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
| 703 | if (!num_stripes) { |
| 704 | printk( |
| 705 | "ERROR: invalid number of stripes %u in sys_array at offset %u\n", |
| 706 | num_stripes, cur_offset); |
| 707 | ret = -EIO; |
| 708 | break; |
| 709 | } |
| 710 | |
| 711 | len = btrfs_chunk_item_size(num_stripes); |
| 712 | if (cur_offset + len > array_size) |
| 713 | goto out_short_read; |
| 714 | |
| 715 | ret = read_one_chunk(fs_info, &key, sb, chunk, -1); |
| 716 | if (ret) |
| 717 | break; |
| 718 | } else { |
| 719 | printk( |
| 720 | "ERROR: unexpected item type %u in sys_array at offset %u\n", |
| 721 | (u32)key.type, cur_offset); |
| 722 | ret = -EIO; |
| 723 | break; |
| 724 | } |
| 725 | array_ptr += len; |
| 726 | sb_array_offset += len; |
| 727 | cur_offset += len; |
| 728 | } |
| 729 | free_extent_buffer(sb); |
| 730 | return ret; |
| 731 | |
| 732 | out_short_read: |
| 733 | printk("ERROR: sys_array too short to read %u bytes at offset %u\n", |
| 734 | len, cur_offset); |
| 735 | free_extent_buffer(sb); |
| 736 | return -EIO; |
| 737 | } |
| 738 | |
| 739 | int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info) |
| 740 | { |
| 741 | struct btrfs_path *path; |
| 742 | struct extent_buffer *leaf; |
| 743 | struct btrfs_key key; |
| 744 | struct btrfs_key found_key; |
| 745 | struct btrfs_root *root = fs_info->chunk_root; |
| 746 | int ret; |
| 747 | int slot; |
| 748 | |
| 749 | path = btrfs_alloc_path(); |
| 750 | if (!path) |
| 751 | return -ENOMEM; |
| 752 | |
| 753 | /* |
| 754 | * Read all device items, and then all the chunk items. All |
| 755 | * device items are found before any chunk item (their object id |
| 756 | * is smaller than the lowest possible object id for a chunk |
| 757 | * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). |
| 758 | */ |
| 759 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 760 | key.offset = 0; |
| 761 | key.type = 0; |
| 762 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 763 | if (ret < 0) |
| 764 | goto error; |
| 765 | while(1) { |
| 766 | leaf = path->nodes[0]; |
| 767 | slot = path->slots[0]; |
| 768 | if (slot >= btrfs_header_nritems(leaf)) { |
| 769 | ret = btrfs_next_leaf(root, path); |
| 770 | if (ret == 0) |
| 771 | continue; |
| 772 | if (ret < 0) |
| 773 | goto error; |
| 774 | break; |
| 775 | } |
| 776 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 777 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 778 | struct btrfs_dev_item *dev_item; |
| 779 | dev_item = btrfs_item_ptr(leaf, slot, |
| 780 | struct btrfs_dev_item); |
| 781 | ret = read_one_dev(fs_info, leaf, dev_item); |
| 782 | if (ret < 0) |
| 783 | goto error; |
| 784 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 785 | struct btrfs_chunk *chunk; |
| 786 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 787 | ret = read_one_chunk(fs_info, &found_key, leaf, chunk, |
| 788 | slot); |
| 789 | if (ret < 0) |
| 790 | goto error; |
| 791 | } |
| 792 | path->slots[0]++; |
| 793 | } |
| 794 | |
| 795 | ret = 0; |
| 796 | error: |
| 797 | btrfs_free_path(path); |
| 798 | return ret; |
| 799 | } |
| 800 | |
Qu Wenruo | f6377ff | 2020-06-24 18:02:54 +0200 | [diff] [blame] | 801 | /* |
| 802 | * Get stripe length from chunk item and its stripe items |
| 803 | * |
| 804 | * Caller should only call this function after validating the chunk item |
| 805 | * by using btrfs_check_chunk_valid(). |
| 806 | */ |
| 807 | u64 btrfs_stripe_length(struct btrfs_fs_info *fs_info, |
| 808 | struct extent_buffer *leaf, |
| 809 | struct btrfs_chunk *chunk) |
| 810 | { |
| 811 | u64 stripe_len; |
| 812 | u64 chunk_len; |
| 813 | u32 num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 814 | u64 profile = btrfs_chunk_type(leaf, chunk) & |
| 815 | BTRFS_BLOCK_GROUP_PROFILE_MASK; |
| 816 | |
| 817 | chunk_len = btrfs_chunk_length(leaf, chunk); |
| 818 | |
| 819 | switch (profile) { |
| 820 | case 0: /* Single profile */ |
| 821 | case BTRFS_BLOCK_GROUP_RAID1: |
| 822 | case BTRFS_BLOCK_GROUP_RAID1C3: |
| 823 | case BTRFS_BLOCK_GROUP_RAID1C4: |
| 824 | case BTRFS_BLOCK_GROUP_DUP: |
| 825 | stripe_len = chunk_len; |
| 826 | break; |
| 827 | case BTRFS_BLOCK_GROUP_RAID0: |
| 828 | stripe_len = chunk_len / num_stripes; |
| 829 | break; |
| 830 | case BTRFS_BLOCK_GROUP_RAID5: |
| 831 | stripe_len = chunk_len / (num_stripes - 1); |
| 832 | break; |
| 833 | case BTRFS_BLOCK_GROUP_RAID6: |
| 834 | stripe_len = chunk_len / (num_stripes - 2); |
| 835 | break; |
| 836 | case BTRFS_BLOCK_GROUP_RAID10: |
| 837 | stripe_len = chunk_len / (num_stripes / |
| 838 | btrfs_chunk_sub_stripes(leaf, chunk)); |
| 839 | break; |
| 840 | default: |
| 841 | /* Invalid chunk profile found */ |
| 842 | BUG_ON(1); |
| 843 | } |
| 844 | return stripe_len; |
| 845 | } |
| 846 | |
| 847 | int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len) |
| 848 | { |
| 849 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
| 850 | struct cache_extent *ce; |
| 851 | struct map_lookup *map; |
| 852 | int ret; |
| 853 | |
| 854 | ce = search_cache_extent(&map_tree->cache_tree, logical); |
| 855 | if (!ce) { |
| 856 | fprintf(stderr, "No mapping for %llu-%llu\n", |
| 857 | (unsigned long long)logical, |
| 858 | (unsigned long long)logical+len); |
| 859 | return 1; |
| 860 | } |
| 861 | if (ce->start > logical || ce->start + ce->size < logical) { |
| 862 | fprintf(stderr, "Invalid mapping for %llu-%llu, got " |
| 863 | "%llu-%llu\n", (unsigned long long)logical, |
| 864 | (unsigned long long)logical+len, |
| 865 | (unsigned long long)ce->start, |
| 866 | (unsigned long long)ce->start + ce->size); |
| 867 | return 1; |
| 868 | } |
| 869 | map = container_of(ce, struct map_lookup, ce); |
| 870 | |
| 871 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
| 872 | BTRFS_BLOCK_GROUP_RAID1C3 | BTRFS_BLOCK_GROUP_RAID1C4)) |
| 873 | ret = map->num_stripes; |
| 874 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 875 | ret = map->sub_stripes; |
| 876 | else if (map->type & BTRFS_BLOCK_GROUP_RAID5) |
| 877 | ret = 2; |
| 878 | else if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 879 | ret = 3; |
| 880 | else |
| 881 | ret = 1; |
| 882 | return ret; |
| 883 | } |
| 884 | |
| 885 | int btrfs_next_bg(struct btrfs_fs_info *fs_info, u64 *logical, |
| 886 | u64 *size, u64 type) |
| 887 | { |
| 888 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
| 889 | struct cache_extent *ce; |
| 890 | struct map_lookup *map; |
| 891 | u64 cur = *logical; |
| 892 | |
| 893 | ce = search_cache_extent(&map_tree->cache_tree, cur); |
| 894 | |
| 895 | while (ce) { |
| 896 | /* |
| 897 | * only jump to next bg if our cur is not 0 |
| 898 | * As the initial logical for btrfs_next_bg() is 0, and |
| 899 | * if we jump to next bg, we skipped a valid bg. |
| 900 | */ |
| 901 | if (cur) { |
| 902 | ce = next_cache_extent(ce); |
| 903 | if (!ce) |
| 904 | return -ENOENT; |
| 905 | } |
| 906 | |
| 907 | cur = ce->start; |
| 908 | map = container_of(ce, struct map_lookup, ce); |
| 909 | if (map->type & type) { |
| 910 | *logical = ce->start; |
| 911 | *size = ce->size; |
| 912 | return 0; |
| 913 | } |
| 914 | if (!cur) |
| 915 | ce = next_cache_extent(ce); |
| 916 | } |
| 917 | |
| 918 | return -ENOENT; |
| 919 | } |
| 920 | |
| 921 | static inline int parity_smaller(u64 a, u64 b) |
| 922 | { |
| 923 | return a > b; |
| 924 | } |
| 925 | |
| 926 | /* Bubble-sort the stripe set to put the parity/syndrome stripes last */ |
| 927 | static void sort_parity_stripes(struct btrfs_multi_bio *bbio, u64 *raid_map) |
| 928 | { |
| 929 | struct btrfs_bio_stripe s; |
| 930 | int i; |
| 931 | u64 l; |
| 932 | int again = 1; |
| 933 | |
| 934 | while (again) { |
| 935 | again = 0; |
| 936 | for (i = 0; i < bbio->num_stripes - 1; i++) { |
| 937 | if (parity_smaller(raid_map[i], raid_map[i+1])) { |
| 938 | s = bbio->stripes[i]; |
| 939 | l = raid_map[i]; |
| 940 | bbio->stripes[i] = bbio->stripes[i+1]; |
| 941 | raid_map[i] = raid_map[i+1]; |
| 942 | bbio->stripes[i+1] = s; |
| 943 | raid_map[i+1] = l; |
| 944 | again = 1; |
| 945 | } |
| 946 | } |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw, |
| 951 | u64 logical, u64 *length, u64 *type, |
| 952 | struct btrfs_multi_bio **multi_ret, int mirror_num, |
| 953 | u64 **raid_map_ret) |
| 954 | { |
| 955 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
| 956 | struct cache_extent *ce; |
| 957 | struct map_lookup *map; |
Qu Wenruo | 8309e11 | 2023-02-13 08:37:59 +0800 | [diff] [blame] | 958 | u64 orig_len = *length; |
Qu Wenruo | f6377ff | 2020-06-24 18:02:54 +0200 | [diff] [blame] | 959 | u64 offset; |
| 960 | u64 stripe_offset; |
| 961 | u64 *raid_map = NULL; |
| 962 | int stripe_nr; |
| 963 | int stripes_allocated = 8; |
| 964 | int stripes_required = 1; |
| 965 | int stripe_index; |
| 966 | int i; |
| 967 | struct btrfs_multi_bio *multi = NULL; |
| 968 | |
| 969 | if (multi_ret && rw == READ) { |
| 970 | stripes_allocated = 1; |
| 971 | } |
| 972 | again: |
| 973 | ce = search_cache_extent(&map_tree->cache_tree, logical); |
| 974 | if (!ce) { |
| 975 | kfree(multi); |
| 976 | *length = (u64)-1; |
| 977 | return -ENOENT; |
| 978 | } |
| 979 | if (ce->start > logical) { |
| 980 | kfree(multi); |
| 981 | *length = ce->start - logical; |
| 982 | return -ENOENT; |
| 983 | } |
| 984 | |
| 985 | if (multi_ret) { |
| 986 | multi = kzalloc(btrfs_multi_bio_size(stripes_allocated), |
| 987 | GFP_NOFS); |
| 988 | if (!multi) |
| 989 | return -ENOMEM; |
| 990 | } |
| 991 | map = container_of(ce, struct map_lookup, ce); |
| 992 | offset = logical - ce->start; |
| 993 | |
| 994 | if (rw == WRITE) { |
| 995 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
| 996 | BTRFS_BLOCK_GROUP_RAID1C3 | |
| 997 | BTRFS_BLOCK_GROUP_RAID1C4 | |
| 998 | BTRFS_BLOCK_GROUP_DUP)) { |
| 999 | stripes_required = map->num_stripes; |
| 1000 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 1001 | stripes_required = map->sub_stripes; |
| 1002 | } |
| 1003 | } |
| 1004 | if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) |
| 1005 | && multi_ret && ((rw & WRITE) || mirror_num > 1) && raid_map_ret) { |
| 1006 | /* RAID[56] write or recovery. Return all stripes */ |
| 1007 | stripes_required = map->num_stripes; |
| 1008 | |
| 1009 | /* Only allocate the map if we've already got a large enough multi_ret */ |
| 1010 | if (stripes_allocated >= stripes_required) { |
| 1011 | raid_map = kmalloc(sizeof(u64) * map->num_stripes, GFP_NOFS); |
| 1012 | if (!raid_map) { |
| 1013 | kfree(multi); |
| 1014 | return -ENOMEM; |
| 1015 | } |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | /* if our multi bio struct is too small, back off and try again */ |
| 1020 | if (multi_ret && stripes_allocated < stripes_required) { |
| 1021 | stripes_allocated = stripes_required; |
| 1022 | kfree(multi); |
| 1023 | multi = NULL; |
| 1024 | goto again; |
| 1025 | } |
| 1026 | stripe_nr = offset; |
| 1027 | /* |
| 1028 | * stripe_nr counts the total number of stripes we have to stride |
| 1029 | * to get to this block |
| 1030 | */ |
| 1031 | stripe_nr = stripe_nr / map->stripe_len; |
| 1032 | |
Qu Wenruo | 12bbff0 | 2020-10-31 09:07:50 +0800 | [diff] [blame] | 1033 | stripe_offset = stripe_nr * (u64)map->stripe_len; |
Qu Wenruo | f6377ff | 2020-06-24 18:02:54 +0200 | [diff] [blame] | 1034 | BUG_ON(offset < stripe_offset); |
| 1035 | |
| 1036 | /* stripe_offset is the offset of this block in its stripe*/ |
| 1037 | stripe_offset = offset - stripe_offset; |
| 1038 | |
| 1039 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 | |
| 1040 | BTRFS_BLOCK_GROUP_RAID1C3 | BTRFS_BLOCK_GROUP_RAID1C4 | |
| 1041 | BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 | |
| 1042 | BTRFS_BLOCK_GROUP_RAID10 | |
| 1043 | BTRFS_BLOCK_GROUP_DUP)) { |
| 1044 | /* we limit the length of each bio to what fits in a stripe */ |
| 1045 | *length = min_t(u64, ce->size - offset, |
| 1046 | map->stripe_len - stripe_offset); |
| 1047 | } else { |
| 1048 | *length = ce->size - offset; |
| 1049 | } |
Qu Wenruo | 8309e11 | 2023-02-13 08:37:59 +0800 | [diff] [blame] | 1050 | *length = min_t(u64, *length, orig_len); |
Qu Wenruo | f6377ff | 2020-06-24 18:02:54 +0200 | [diff] [blame] | 1051 | |
| 1052 | if (!multi_ret) |
| 1053 | goto out; |
| 1054 | |
| 1055 | multi->num_stripes = 1; |
| 1056 | stripe_index = 0; |
| 1057 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
| 1058 | BTRFS_BLOCK_GROUP_RAID1C3 | |
| 1059 | BTRFS_BLOCK_GROUP_RAID1C4)) { |
| 1060 | if (rw == WRITE) |
| 1061 | multi->num_stripes = map->num_stripes; |
| 1062 | else if (mirror_num) |
| 1063 | stripe_index = mirror_num - 1; |
| 1064 | else |
| 1065 | stripe_index = stripe_nr % map->num_stripes; |
| 1066 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 1067 | int factor = map->num_stripes / map->sub_stripes; |
| 1068 | |
| 1069 | stripe_index = stripe_nr % factor; |
| 1070 | stripe_index *= map->sub_stripes; |
| 1071 | |
| 1072 | if (rw == WRITE) |
| 1073 | multi->num_stripes = map->sub_stripes; |
| 1074 | else if (mirror_num) |
| 1075 | stripe_index += mirror_num - 1; |
| 1076 | |
| 1077 | stripe_nr = stripe_nr / factor; |
| 1078 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
| 1079 | if (rw == WRITE) |
| 1080 | multi->num_stripes = map->num_stripes; |
| 1081 | else if (mirror_num) |
| 1082 | stripe_index = mirror_num - 1; |
| 1083 | } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | |
| 1084 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 1085 | |
| 1086 | if (raid_map) { |
| 1087 | int rot; |
| 1088 | u64 tmp; |
| 1089 | u64 raid56_full_stripe_start; |
| 1090 | u64 full_stripe_len = nr_data_stripes(map) * map->stripe_len; |
| 1091 | |
| 1092 | /* |
| 1093 | * align the start of our data stripe in the logical |
| 1094 | * address space |
| 1095 | */ |
| 1096 | raid56_full_stripe_start = offset / full_stripe_len; |
| 1097 | raid56_full_stripe_start *= full_stripe_len; |
| 1098 | |
| 1099 | /* get the data stripe number */ |
| 1100 | stripe_nr = raid56_full_stripe_start / map->stripe_len; |
| 1101 | stripe_nr = stripe_nr / nr_data_stripes(map); |
| 1102 | |
| 1103 | /* Work out the disk rotation on this stripe-set */ |
| 1104 | rot = stripe_nr % map->num_stripes; |
| 1105 | |
| 1106 | /* Fill in the logical address of each stripe */ |
Qu Wenruo | 12bbff0 | 2020-10-31 09:07:50 +0800 | [diff] [blame] | 1107 | tmp = (u64)stripe_nr * nr_data_stripes(map); |
Qu Wenruo | f6377ff | 2020-06-24 18:02:54 +0200 | [diff] [blame] | 1108 | |
| 1109 | for (i = 0; i < nr_data_stripes(map); i++) |
| 1110 | raid_map[(i+rot) % map->num_stripes] = |
| 1111 | ce->start + (tmp + i) * map->stripe_len; |
| 1112 | |
| 1113 | raid_map[(i+rot) % map->num_stripes] = BTRFS_RAID5_P_STRIPE; |
| 1114 | if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 1115 | raid_map[(i+rot+1) % map->num_stripes] = BTRFS_RAID6_Q_STRIPE; |
| 1116 | |
| 1117 | *length = map->stripe_len; |
| 1118 | stripe_index = 0; |
| 1119 | stripe_offset = 0; |
| 1120 | multi->num_stripes = map->num_stripes; |
| 1121 | } else { |
| 1122 | stripe_index = stripe_nr % nr_data_stripes(map); |
| 1123 | stripe_nr = stripe_nr / nr_data_stripes(map); |
| 1124 | |
| 1125 | /* |
| 1126 | * Mirror #0 or #1 means the original data block. |
| 1127 | * Mirror #2 is RAID5 parity block. |
| 1128 | * Mirror #3 is RAID6 Q block. |
| 1129 | */ |
| 1130 | if (mirror_num > 1) |
| 1131 | stripe_index = nr_data_stripes(map) + mirror_num - 2; |
| 1132 | |
| 1133 | /* We distribute the parity blocks across stripes */ |
| 1134 | stripe_index = (stripe_nr + stripe_index) % map->num_stripes; |
| 1135 | } |
| 1136 | } else { |
| 1137 | /* |
| 1138 | * after this do_div call, stripe_nr is the number of stripes |
| 1139 | * on this device we have to walk to find the data, and |
| 1140 | * stripe_index is the number of our device in the stripe array |
| 1141 | */ |
| 1142 | stripe_index = stripe_nr % map->num_stripes; |
| 1143 | stripe_nr = stripe_nr / map->num_stripes; |
| 1144 | } |
| 1145 | BUG_ON(stripe_index >= map->num_stripes); |
| 1146 | |
| 1147 | for (i = 0; i < multi->num_stripes; i++) { |
| 1148 | multi->stripes[i].physical = |
| 1149 | map->stripes[stripe_index].physical + stripe_offset + |
| 1150 | stripe_nr * map->stripe_len; |
| 1151 | multi->stripes[i].dev = map->stripes[stripe_index].dev; |
| 1152 | stripe_index++; |
| 1153 | } |
| 1154 | *multi_ret = multi; |
| 1155 | |
| 1156 | if (type) |
| 1157 | *type = map->type; |
| 1158 | |
| 1159 | if (raid_map) { |
| 1160 | sort_parity_stripes(multi, raid_map); |
| 1161 | *raid_map_ret = raid_map; |
| 1162 | } |
| 1163 | out: |
| 1164 | return 0; |
| 1165 | } |
| 1166 | |
| 1167 | int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw, |
| 1168 | u64 logical, u64 *length, |
| 1169 | struct btrfs_multi_bio **multi_ret, int mirror_num, |
| 1170 | u64 **raid_map_ret) |
| 1171 | { |
| 1172 | return __btrfs_map_block(fs_info, rw, logical, length, NULL, |
| 1173 | multi_ret, mirror_num, raid_map_ret); |
| 1174 | } |