1 // SPDX-License-Identifier: GPL-2.0+
4 #include <fs_internal.h>
10 const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
11 [BTRFS_RAID_RAID10] = {
14 .devs_max = 0, /* 0 == as many as possible */
16 .tolerated_failures = 1,
20 .raid_name = "raid10",
21 .bg_flag = BTRFS_BLOCK_GROUP_RAID10,
23 [BTRFS_RAID_RAID1] = {
28 .tolerated_failures = 1,
33 .bg_flag = BTRFS_BLOCK_GROUP_RAID1,
35 [BTRFS_RAID_RAID1C3] = {
40 .tolerated_failures = 2,
43 .raid_name = "raid1c3",
44 .bg_flag = BTRFS_BLOCK_GROUP_RAID1C3,
46 [BTRFS_RAID_RAID1C4] = {
51 .tolerated_failures = 3,
54 .raid_name = "raid1c4",
55 .bg_flag = BTRFS_BLOCK_GROUP_RAID1C4,
62 .tolerated_failures = 0,
67 .bg_flag = BTRFS_BLOCK_GROUP_DUP,
69 [BTRFS_RAID_RAID0] = {
74 .tolerated_failures = 0,
79 .bg_flag = BTRFS_BLOCK_GROUP_RAID0,
81 [BTRFS_RAID_SINGLE] = {
86 .tolerated_failures = 0,
90 .raid_name = "single",
93 [BTRFS_RAID_RAID5] = {
98 .tolerated_failures = 1,
102 .raid_name = "raid5",
103 .bg_flag = BTRFS_BLOCK_GROUP_RAID5,
105 [BTRFS_RAID_RAID6] = {
110 .tolerated_failures = 2,
114 .raid_name = "raid6",
115 .bg_flag = BTRFS_BLOCK_GROUP_RAID6,
120 struct btrfs_device *dev;
124 static inline int nr_parity_stripes(struct map_lookup *map)
126 if (map->type & BTRFS_BLOCK_GROUP_RAID5)
128 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
134 static inline int nr_data_stripes(struct map_lookup *map)
136 return map->num_stripes - nr_parity_stripes(map);
139 #define is_parity_stripe(x) ( ((x) == BTRFS_RAID5_P_STRIPE) || ((x) == BTRFS_RAID6_Q_STRIPE) )
141 static LIST_HEAD(fs_uuids);
144 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
147 * If devid and uuid are both specified, the match must be exact, otherwise
148 * only devid is used.
150 static struct btrfs_device *find_device(struct btrfs_fs_devices *fs_devices,
153 struct list_head *head = &fs_devices->devices;
154 struct btrfs_device *dev;
156 list_for_each_entry(dev, head, dev_list) {
157 if (dev->devid == devid &&
158 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
165 static struct btrfs_fs_devices *find_fsid(u8 *fsid, u8 *metadata_uuid)
167 struct btrfs_fs_devices *fs_devices;
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)) {
175 } else if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0){
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)
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);
194 fs_devices = find_fsid(disk_super->fsid,
195 disk_super->metadata_uuid);
197 fs_devices = find_fsid(disk_super->fsid, NULL);
200 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
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);
207 memcpy(fs_devices->metadata_uuid,
208 disk_super->metadata_uuid, BTRFS_FSID_SIZE);
210 memcpy(fs_devices->metadata_uuid, fs_devices->fsid,
213 fs_devices->latest_devid = devid;
214 fs_devices->latest_trans = found_transid;
215 fs_devices->lowest_devid = (u64)-1;
218 device = find_device(fs_devices, devid,
219 disk_super->dev_item.uuid);
222 device = kzalloc(sizeof(*device), GFP_NOFS);
224 /* we can safely leave the fs_devices entry around */
227 device->devid = devid;
230 device->generation = found_transid;
231 memcpy(device->uuid, disk_super->dev_item.uuid,
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);
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) {
243 * The existing device has newer generation, so this one could
244 * be a stale one, don't add it.
246 if (found_transid < device->generation) {
248 "adding devid %llu gen %llu but found an existing device gen %llu",
249 device->devid, found_transid,
259 if (found_transid > fs_devices->latest_trans) {
260 fs_devices->latest_devid = devid;
261 fs_devices->latest_trans = found_transid;
263 if (fs_devices->lowest_devid > devid) {
264 fs_devices->lowest_devid = devid;
266 *fs_devices_ret = fs_devices;
270 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
272 struct btrfs_fs_devices *seed_devices;
273 struct btrfs_device *device;
279 while (!list_empty(&fs_devices->devices)) {
280 device = list_entry(fs_devices->devices.next,
281 struct btrfs_device, dev_list);
282 list_del(&device->dev_list);
283 /* free the memory */
287 seed_devices = fs_devices->seed;
288 fs_devices->seed = NULL;
290 struct btrfs_fs_devices *orig;
293 fs_devices = seed_devices;
294 list_del(&orig->list);
298 list_del(&fs_devices->list);
305 void btrfs_close_all_devices(void)
307 struct btrfs_fs_devices *fs_devices;
309 while (!list_empty(&fs_uuids)) {
310 fs_devices = list_entry(fs_uuids.next, struct btrfs_fs_devices,
312 btrfs_close_devices(fs_devices);
316 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices)
318 struct btrfs_device *device;
320 list_for_each_entry(device, &fs_devices->devices, dev_list) {
321 if (!device->desc || !device->part) {
322 printf("no device found for devid %llu, skip it \n",
330 int btrfs_scan_one_device(struct blk_desc *desc, struct disk_partition *part,
331 struct btrfs_fs_devices **fs_devices_ret,
334 struct btrfs_super_block *disk_super;
335 char buf[BTRFS_SUPER_INFO_SIZE];
339 disk_super = (struct btrfs_super_block *)buf;
340 ret = btrfs_read_dev_super(desc, part, disk_super);
343 devid = btrfs_stack_device_id(&disk_super->dev_item);
344 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)
347 *total_devs = btrfs_super_num_devices(disk_super);
349 ret = device_list_add(disk_super, devid, desc, part, fs_devices_ret);
354 struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
357 struct btrfs_device *device;
358 struct btrfs_fs_devices *cur_devices;
360 cur_devices = fs_info->fs_devices;
361 while (cur_devices) {
363 !memcmp(cur_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
364 device = find_device(cur_devices, devid, uuid);
368 cur_devices = cur_devices->seed;
373 static struct btrfs_device *fill_missing_device(u64 devid)
375 struct btrfs_device *device;
377 device = kzalloc(sizeof(*device), GFP_NOFS);
382 * slot == -1: SYSTEM chunk
383 * return -EIO on error, otherwise return 0
385 int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info,
386 struct extent_buffer *leaf,
387 struct btrfs_chunk *chunk,
388 int slot, u64 logical)
395 u32 chunk_ondisk_size;
396 u32 sectorsize = fs_info->sectorsize;
399 * Basic chunk item size check. Note that btrfs_chunk already contains
400 * one stripe, so no "==" check.
403 btrfs_item_size_nr(leaf, slot) < sizeof(struct btrfs_chunk)) {
404 error("invalid chunk item size, have %u expect [%zu, %zu)",
405 btrfs_item_size_nr(leaf, slot),
406 sizeof(struct btrfs_chunk),
407 BTRFS_LEAF_DATA_SIZE(fs_info));
410 length = btrfs_chunk_length(leaf, chunk);
411 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
412 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
413 sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
414 type = btrfs_chunk_type(leaf, chunk);
416 if (num_stripes == 0) {
417 error("invalid num_stripes, have %u expect non-zero",
421 if (slot >= 0 && btrfs_chunk_item_size(num_stripes) !=
422 btrfs_item_size_nr(leaf, slot)) {
423 error("invalid chunk item size, have %u expect %lu",
424 btrfs_item_size_nr(leaf, slot),
425 btrfs_chunk_item_size(num_stripes));
430 * These valid checks may be insufficient to cover every corner cases.
432 if (!IS_ALIGNED(logical, sectorsize)) {
433 error("invalid chunk logical %llu", logical);
436 if (btrfs_chunk_sector_size(leaf, chunk) != sectorsize) {
437 error("invalid chunk sectorsize %llu",
438 (unsigned long long)btrfs_chunk_sector_size(leaf, chunk));
441 if (!length || !IS_ALIGNED(length, sectorsize)) {
442 error("invalid chunk length %llu", length);
445 if (stripe_len != BTRFS_STRIPE_LEN) {
446 error("invalid chunk stripe length: %llu", stripe_len);
449 /* Check on chunk item type */
450 if (slot == -1 && (type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
451 error("invalid chunk type %llu", type);
454 if (type & ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
455 BTRFS_BLOCK_GROUP_PROFILE_MASK)) {
456 error("unrecognized chunk type: %llu",
457 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
458 BTRFS_BLOCK_GROUP_PROFILE_MASK) & type);
461 if (!(type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
462 error("missing chunk type flag: %llu", type);
465 if (!(is_power_of_2(type & BTRFS_BLOCK_GROUP_PROFILE_MASK) ||
466 (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0)) {
467 error("conflicting chunk type detected: %llu", type);
470 if ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) &&
471 !is_power_of_2(type & BTRFS_BLOCK_GROUP_PROFILE_MASK)) {
472 error("conflicting chunk profile detected: %llu", type);
476 chunk_ondisk_size = btrfs_chunk_item_size(num_stripes);
478 * Btrfs_chunk contains at least one stripe, and for sys_chunk
479 * it can't exceed the system chunk array size
480 * For normal chunk, it should match its chunk item size.
482 if (num_stripes < 1 ||
483 (slot == -1 && chunk_ondisk_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) ||
484 (slot >= 0 && chunk_ondisk_size > btrfs_item_size_nr(leaf, slot))) {
485 error("invalid num_stripes: %u", num_stripes);
489 * Device number check against profile
491 if ((type & BTRFS_BLOCK_GROUP_RAID10 && (sub_stripes != 2 ||
492 !IS_ALIGNED(num_stripes, sub_stripes))) ||
493 (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) ||
494 (type & BTRFS_BLOCK_GROUP_RAID1C3 && num_stripes < 3) ||
495 (type & BTRFS_BLOCK_GROUP_RAID1C4 && num_stripes < 4) ||
496 (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
497 (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
498 (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||
499 ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
501 error("Invalid num_stripes:sub_stripes %u:%u for profile %llu",
502 num_stripes, sub_stripes,
503 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
511 * Slot is used to verify the chunk item is valid
513 * For sys chunk in superblock, pass -1 to indicate sys chunk.
515 static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
516 struct extent_buffer *leaf,
517 struct btrfs_chunk *chunk, int slot)
519 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
520 struct map_lookup *map;
521 struct cache_extent *ce;
525 u8 uuid[BTRFS_UUID_SIZE];
530 logical = key->offset;
531 length = btrfs_chunk_length(leaf, chunk);
532 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
533 /* Validation check */
534 ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, slot, logical);
536 error("%s checksums match, but it has an invalid chunk, %s",
537 (slot == -1) ? "Superblock" : "Metadata",
538 (slot == -1) ? "try btrfsck --repair -s <superblock> ie, 0,1,2" : "");
542 ce = search_cache_extent(&map_tree->cache_tree, logical);
544 /* already mapped? */
545 if (ce && ce->start <= logical && ce->start + ce->size > logical) {
549 map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
553 map->ce.start = logical;
554 map->ce.size = length;
555 map->num_stripes = num_stripes;
556 map->io_width = btrfs_chunk_io_width(leaf, chunk);
557 map->io_align = btrfs_chunk_io_align(leaf, chunk);
558 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
559 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
560 map->type = btrfs_chunk_type(leaf, chunk);
561 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
563 for (i = 0; i < num_stripes; i++) {
564 map->stripes[i].physical =
565 btrfs_stripe_offset_nr(leaf, chunk, i);
566 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
567 read_extent_buffer(leaf, uuid, (unsigned long)
568 btrfs_stripe_dev_uuid_nr(chunk, i),
570 map->stripes[i].dev = btrfs_find_device(fs_info, devid, uuid,
572 if (!map->stripes[i].dev) {
573 map->stripes[i].dev = fill_missing_device(devid);
574 printf("warning, device %llu is missing\n",
575 (unsigned long long)devid);
576 list_add(&map->stripes[i].dev->dev_list,
577 &fs_info->fs_devices->devices);
581 ret = insert_cache_extent(&map_tree->cache_tree, &map->ce);
584 error("failed to add chunk map start=%llu len=%llu: %d (%m)",
585 map->ce.start, map->ce.size, ret);
591 static int fill_device_from_item(struct extent_buffer *leaf,
592 struct btrfs_dev_item *dev_item,
593 struct btrfs_device *device)
597 device->devid = btrfs_device_id(leaf, dev_item);
598 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
599 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
600 device->type = btrfs_device_type(leaf, dev_item);
601 device->io_align = btrfs_device_io_align(leaf, dev_item);
602 device->io_width = btrfs_device_io_width(leaf, dev_item);
603 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
605 ptr = (unsigned long)btrfs_device_uuid(dev_item);
606 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
611 static int read_one_dev(struct btrfs_fs_info *fs_info,
612 struct extent_buffer *leaf,
613 struct btrfs_dev_item *dev_item)
615 struct btrfs_device *device;
618 u8 fs_uuid[BTRFS_UUID_SIZE];
619 u8 dev_uuid[BTRFS_UUID_SIZE];
621 devid = btrfs_device_id(leaf, dev_item);
622 read_extent_buffer(leaf, dev_uuid,
623 (unsigned long)btrfs_device_uuid(dev_item),
625 read_extent_buffer(leaf, fs_uuid,
626 (unsigned long)btrfs_device_fsid(dev_item),
629 if (memcmp(fs_uuid, fs_info->fs_devices->fsid, BTRFS_UUID_SIZE)) {
630 error("Seed device is not yet supported\n");
634 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
636 device = kzalloc(sizeof(*device), GFP_NOFS);
639 list_add(&device->dev_list,
640 &fs_info->fs_devices->devices);
643 fill_device_from_item(leaf, dev_item, device);
644 fs_info->fs_devices->total_rw_bytes +=
645 btrfs_device_total_bytes(leaf, dev_item);
649 int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
651 struct btrfs_super_block *super_copy = fs_info->super_copy;
652 struct extent_buffer *sb;
653 struct btrfs_disk_key *disk_key;
654 struct btrfs_chunk *chunk;
656 unsigned long sb_array_offset;
662 struct btrfs_key key;
664 if (fs_info->nodesize < BTRFS_SUPER_INFO_SIZE) {
665 printf("ERROR: nodesize %u too small to read superblock\n",
669 sb = alloc_dummy_extent_buffer(fs_info, BTRFS_SUPER_INFO_OFFSET,
670 BTRFS_SUPER_INFO_SIZE);
673 btrfs_set_buffer_uptodate(sb);
674 write_extent_buffer(sb, super_copy, 0, sizeof(*super_copy));
675 array_size = btrfs_super_sys_array_size(super_copy);
677 array_ptr = super_copy->sys_chunk_array;
678 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
681 while (cur_offset < array_size) {
682 disk_key = (struct btrfs_disk_key *)array_ptr;
683 len = sizeof(*disk_key);
684 if (cur_offset + len > array_size)
687 btrfs_disk_key_to_cpu(&key, disk_key);
690 sb_array_offset += len;
693 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
694 chunk = (struct btrfs_chunk *)sb_array_offset;
696 * At least one btrfs_chunk with one stripe must be
697 * present, exact stripe count check comes afterwards
699 len = btrfs_chunk_item_size(1);
700 if (cur_offset + len > array_size)
703 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
706 "ERROR: invalid number of stripes %u in sys_array at offset %u\n",
707 num_stripes, cur_offset);
712 len = btrfs_chunk_item_size(num_stripes);
713 if (cur_offset + len > array_size)
716 ret = read_one_chunk(fs_info, &key, sb, chunk, -1);
721 "ERROR: unexpected item type %u in sys_array at offset %u\n",
722 (u32)key.type, cur_offset);
727 sb_array_offset += len;
730 free_extent_buffer(sb);
734 printk("ERROR: sys_array too short to read %u bytes at offset %u\n",
736 free_extent_buffer(sb);
740 int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
742 struct btrfs_path *path;
743 struct extent_buffer *leaf;
744 struct btrfs_key key;
745 struct btrfs_key found_key;
746 struct btrfs_root *root = fs_info->chunk_root;
750 path = btrfs_alloc_path();
755 * Read all device items, and then all the chunk items. All
756 * device items are found before any chunk item (their object id
757 * is smaller than the lowest possible object id for a chunk
758 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
760 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
763 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
767 leaf = path->nodes[0];
768 slot = path->slots[0];
769 if (slot >= btrfs_header_nritems(leaf)) {
770 ret = btrfs_next_leaf(root, path);
777 btrfs_item_key_to_cpu(leaf, &found_key, slot);
778 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
779 struct btrfs_dev_item *dev_item;
780 dev_item = btrfs_item_ptr(leaf, slot,
781 struct btrfs_dev_item);
782 ret = read_one_dev(fs_info, leaf, dev_item);
785 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
786 struct btrfs_chunk *chunk;
787 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
788 ret = read_one_chunk(fs_info, &found_key, leaf, chunk,
798 btrfs_free_path(path);
803 * Get stripe length from chunk item and its stripe items
805 * Caller should only call this function after validating the chunk item
806 * by using btrfs_check_chunk_valid().
808 u64 btrfs_stripe_length(struct btrfs_fs_info *fs_info,
809 struct extent_buffer *leaf,
810 struct btrfs_chunk *chunk)
814 u32 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
815 u64 profile = btrfs_chunk_type(leaf, chunk) &
816 BTRFS_BLOCK_GROUP_PROFILE_MASK;
818 chunk_len = btrfs_chunk_length(leaf, chunk);
821 case 0: /* Single profile */
822 case BTRFS_BLOCK_GROUP_RAID1:
823 case BTRFS_BLOCK_GROUP_RAID1C3:
824 case BTRFS_BLOCK_GROUP_RAID1C4:
825 case BTRFS_BLOCK_GROUP_DUP:
826 stripe_len = chunk_len;
828 case BTRFS_BLOCK_GROUP_RAID0:
829 stripe_len = chunk_len / num_stripes;
831 case BTRFS_BLOCK_GROUP_RAID5:
832 stripe_len = chunk_len / (num_stripes - 1);
834 case BTRFS_BLOCK_GROUP_RAID6:
835 stripe_len = chunk_len / (num_stripes - 2);
837 case BTRFS_BLOCK_GROUP_RAID10:
838 stripe_len = chunk_len / (num_stripes /
839 btrfs_chunk_sub_stripes(leaf, chunk));
842 /* Invalid chunk profile found */
848 int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
850 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
851 struct cache_extent *ce;
852 struct map_lookup *map;
855 ce = search_cache_extent(&map_tree->cache_tree, logical);
857 fprintf(stderr, "No mapping for %llu-%llu\n",
858 (unsigned long long)logical,
859 (unsigned long long)logical+len);
862 if (ce->start > logical || ce->start + ce->size < logical) {
863 fprintf(stderr, "Invalid mapping for %llu-%llu, got "
864 "%llu-%llu\n", (unsigned long long)logical,
865 (unsigned long long)logical+len,
866 (unsigned long long)ce->start,
867 (unsigned long long)ce->start + ce->size);
870 map = container_of(ce, struct map_lookup, ce);
872 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
873 BTRFS_BLOCK_GROUP_RAID1C3 | BTRFS_BLOCK_GROUP_RAID1C4))
874 ret = map->num_stripes;
875 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
876 ret = map->sub_stripes;
877 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
879 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
886 int btrfs_next_bg(struct btrfs_fs_info *fs_info, u64 *logical,
889 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
890 struct cache_extent *ce;
891 struct map_lookup *map;
894 ce = search_cache_extent(&map_tree->cache_tree, cur);
898 * only jump to next bg if our cur is not 0
899 * As the initial logical for btrfs_next_bg() is 0, and
900 * if we jump to next bg, we skipped a valid bg.
903 ce = next_cache_extent(ce);
909 map = container_of(ce, struct map_lookup, ce);
910 if (map->type & type) {
911 *logical = ce->start;
916 ce = next_cache_extent(ce);
922 static inline int parity_smaller(u64 a, u64 b)
927 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
928 static void sort_parity_stripes(struct btrfs_multi_bio *bbio, u64 *raid_map)
930 struct btrfs_bio_stripe s;
937 for (i = 0; i < bbio->num_stripes - 1; i++) {
938 if (parity_smaller(raid_map[i], raid_map[i+1])) {
939 s = bbio->stripes[i];
941 bbio->stripes[i] = bbio->stripes[i+1];
942 raid_map[i] = raid_map[i+1];
943 bbio->stripes[i+1] = s;
951 int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
952 u64 logical, u64 *length, u64 *type,
953 struct btrfs_multi_bio **multi_ret, int mirror_num,
956 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
957 struct cache_extent *ce;
958 struct map_lookup *map;
961 u64 *raid_map = NULL;
963 int stripes_allocated = 8;
964 int stripes_required = 1;
967 struct btrfs_multi_bio *multi = NULL;
969 if (multi_ret && rw == READ) {
970 stripes_allocated = 1;
973 ce = search_cache_extent(&map_tree->cache_tree, logical);
979 if (ce->start > logical) {
981 *length = ce->start - logical;
986 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
991 map = container_of(ce, struct map_lookup, ce);
992 offset = logical - ce->start;
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;
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;
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);
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;
1028 * stripe_nr counts the total number of stripes we have to stride
1029 * to get to this block
1031 stripe_nr = stripe_nr / map->stripe_len;
1033 stripe_offset = stripe_nr * map->stripe_len;
1034 BUG_ON(offset < stripe_offset);
1036 /* stripe_offset is the offset of this block in its stripe*/
1037 stripe_offset = offset - stripe_offset;
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);
1048 *length = ce->size - offset;
1054 multi->num_stripes = 1;
1056 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
1057 BTRFS_BLOCK_GROUP_RAID1C3 |
1058 BTRFS_BLOCK_GROUP_RAID1C4)) {
1060 multi->num_stripes = map->num_stripes;
1061 else if (mirror_num)
1062 stripe_index = mirror_num - 1;
1064 stripe_index = stripe_nr % map->num_stripes;
1065 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1066 int factor = map->num_stripes / map->sub_stripes;
1068 stripe_index = stripe_nr % factor;
1069 stripe_index *= map->sub_stripes;
1072 multi->num_stripes = map->sub_stripes;
1073 else if (mirror_num)
1074 stripe_index += mirror_num - 1;
1076 stripe_nr = stripe_nr / factor;
1077 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
1079 multi->num_stripes = map->num_stripes;
1080 else if (mirror_num)
1081 stripe_index = mirror_num - 1;
1082 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
1083 BTRFS_BLOCK_GROUP_RAID6)) {
1088 u64 raid56_full_stripe_start;
1089 u64 full_stripe_len = nr_data_stripes(map) * map->stripe_len;
1092 * align the start of our data stripe in the logical
1095 raid56_full_stripe_start = offset / full_stripe_len;
1096 raid56_full_stripe_start *= full_stripe_len;
1098 /* get the data stripe number */
1099 stripe_nr = raid56_full_stripe_start / map->stripe_len;
1100 stripe_nr = stripe_nr / nr_data_stripes(map);
1102 /* Work out the disk rotation on this stripe-set */
1103 rot = stripe_nr % map->num_stripes;
1105 /* Fill in the logical address of each stripe */
1106 tmp = stripe_nr * nr_data_stripes(map);
1108 for (i = 0; i < nr_data_stripes(map); i++)
1109 raid_map[(i+rot) % map->num_stripes] =
1110 ce->start + (tmp + i) * map->stripe_len;
1112 raid_map[(i+rot) % map->num_stripes] = BTRFS_RAID5_P_STRIPE;
1113 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
1114 raid_map[(i+rot+1) % map->num_stripes] = BTRFS_RAID6_Q_STRIPE;
1116 *length = map->stripe_len;
1119 multi->num_stripes = map->num_stripes;
1121 stripe_index = stripe_nr % nr_data_stripes(map);
1122 stripe_nr = stripe_nr / nr_data_stripes(map);
1125 * Mirror #0 or #1 means the original data block.
1126 * Mirror #2 is RAID5 parity block.
1127 * Mirror #3 is RAID6 Q block.
1130 stripe_index = nr_data_stripes(map) + mirror_num - 2;
1132 /* We distribute the parity blocks across stripes */
1133 stripe_index = (stripe_nr + stripe_index) % map->num_stripes;
1137 * after this do_div call, stripe_nr is the number of stripes
1138 * on this device we have to walk to find the data, and
1139 * stripe_index is the number of our device in the stripe array
1141 stripe_index = stripe_nr % map->num_stripes;
1142 stripe_nr = stripe_nr / map->num_stripes;
1144 BUG_ON(stripe_index >= map->num_stripes);
1146 for (i = 0; i < multi->num_stripes; i++) {
1147 multi->stripes[i].physical =
1148 map->stripes[stripe_index].physical + stripe_offset +
1149 stripe_nr * map->stripe_len;
1150 multi->stripes[i].dev = map->stripes[stripe_index].dev;
1159 sort_parity_stripes(multi, raid_map);
1160 *raid_map_ret = raid_map;
1166 int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
1167 u64 logical, u64 *length,
1168 struct btrfs_multi_bio **multi_ret, int mirror_num,
1171 return __btrfs_map_block(fs_info, rw, logical, length, NULL,
1172 multi_ret, mirror_num, raid_map_ret);