2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
20 #include <sys/types.h>
22 #include <uuid/uuid.h>
27 #include "transaction.h"
28 #include "print-tree.h"
33 struct btrfs_device *dev;
37 static inline int nr_parity_stripes(struct map_lookup *map)
39 if (map->type & BTRFS_BLOCK_GROUP_RAID5)
41 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
47 static inline int nr_data_stripes(struct map_lookup *map)
49 return map->num_stripes - nr_parity_stripes(map);
52 #define is_parity_stripe(x) ( ((x) == BTRFS_RAID5_P_STRIPE) || ((x) == BTRFS_RAID6_Q_STRIPE) )
54 static LIST_HEAD(fs_uuids);
56 static struct btrfs_device *__find_device(struct list_head *head, u64 devid,
59 struct btrfs_device *dev;
60 struct list_head *cur;
62 list_for_each(cur, head) {
63 dev = list_entry(cur, struct btrfs_device, dev_list);
64 if (dev->devid == devid &&
65 !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE)) {
72 static struct btrfs_fs_devices *find_fsid(u8 *fsid)
74 struct list_head *cur;
75 struct btrfs_fs_devices *fs_devices;
77 list_for_each(cur, &fs_uuids) {
78 fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
79 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
85 static int device_list_add(const char *path,
86 struct btrfs_super_block *disk_super,
87 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
89 struct btrfs_device *device;
90 struct btrfs_fs_devices *fs_devices;
91 u64 found_transid = btrfs_super_generation(disk_super);
93 fs_devices = find_fsid(disk_super->fsid);
95 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
98 INIT_LIST_HEAD(&fs_devices->devices);
99 list_add(&fs_devices->list, &fs_uuids);
100 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
101 fs_devices->latest_devid = devid;
102 fs_devices->latest_trans = found_transid;
103 fs_devices->lowest_devid = (u64)-1;
106 device = __find_device(&fs_devices->devices, devid,
107 disk_super->dev_item.uuid);
110 device = kzalloc(sizeof(*device), GFP_NOFS);
112 /* we can safely leave the fs_devices entry around */
116 device->devid = devid;
117 device->generation = found_transid;
118 memcpy(device->uuid, disk_super->dev_item.uuid,
120 device->name = kstrdup(path, GFP_NOFS);
125 device->label = kstrdup(disk_super->label, GFP_NOFS);
126 if (!device->label) {
131 device->total_devs = btrfs_super_num_devices(disk_super);
132 device->super_bytes_used = btrfs_super_bytes_used(disk_super);
133 device->total_bytes =
134 btrfs_stack_device_total_bytes(&disk_super->dev_item);
136 btrfs_stack_device_bytes_used(&disk_super->dev_item);
137 list_add(&device->dev_list, &fs_devices->devices);
138 device->fs_devices = fs_devices;
139 } else if (!device->name || strcmp(device->name, path)) {
140 char *name = strdup(path);
148 if (found_transid > fs_devices->latest_trans) {
149 fs_devices->latest_devid = devid;
150 fs_devices->latest_trans = found_transid;
152 if (fs_devices->lowest_devid > devid) {
153 fs_devices->lowest_devid = devid;
155 *fs_devices_ret = fs_devices;
159 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
161 struct btrfs_fs_devices *seed_devices;
162 struct btrfs_device *device;
165 while (!list_empty(&fs_devices->devices)) {
166 device = list_entry(fs_devices->devices.next,
167 struct btrfs_device, dev_list);
168 if (device->fd != -1) {
170 if (posix_fadvise(device->fd, 0, 0, POSIX_FADV_DONTNEED))
171 fprintf(stderr, "Warning, could not drop caches\n");
175 device->writeable = 0;
176 list_del(&device->dev_list);
177 /* free the memory */
183 seed_devices = fs_devices->seed;
184 fs_devices->seed = NULL;
186 struct btrfs_fs_devices *orig;
189 fs_devices = seed_devices;
190 list_del(&orig->list);
194 list_del(&fs_devices->list);
201 void btrfs_close_all_devices(void)
203 struct btrfs_fs_devices *fs_devices;
205 while (!list_empty(&fs_uuids)) {
206 fs_devices = list_entry(fs_uuids.next, struct btrfs_fs_devices,
208 btrfs_close_devices(fs_devices);
212 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, int flags)
215 struct list_head *head = &fs_devices->devices;
216 struct list_head *cur;
217 struct btrfs_device *device;
220 list_for_each(cur, head) {
221 device = list_entry(cur, struct btrfs_device, dev_list);
223 printk("no name for device %llu, skip it now\n", device->devid);
227 fd = open(device->name, flags);
230 error("cannot open device '%s': %s", device->name,
235 if (posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED))
236 fprintf(stderr, "Warning, could not drop caches\n");
238 if (device->devid == fs_devices->latest_devid)
239 fs_devices->latest_bdev = fd;
240 if (device->devid == fs_devices->lowest_devid)
241 fs_devices->lowest_bdev = fd;
244 device->writeable = 1;
248 btrfs_close_devices(fs_devices);
252 int btrfs_scan_one_device(int fd, const char *path,
253 struct btrfs_fs_devices **fs_devices_ret,
254 u64 *total_devs, u64 super_offset, unsigned sbflags)
256 struct btrfs_super_block *disk_super;
257 char buf[BTRFS_SUPER_INFO_SIZE];
261 disk_super = (struct btrfs_super_block *)buf;
262 ret = btrfs_read_dev_super(fd, disk_super, super_offset, sbflags);
265 devid = btrfs_stack_device_id(&disk_super->dev_item);
266 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)
269 *total_devs = btrfs_super_num_devices(disk_super);
271 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
277 * this uses a pretty simple search, the expectation is that it is
278 * called very infrequently and that a given device has a small number
281 static int find_free_dev_extent(struct btrfs_trans_handle *trans,
282 struct btrfs_device *device,
283 struct btrfs_path *path,
284 u64 num_bytes, u64 *start)
286 struct btrfs_key key;
287 struct btrfs_root *root = device->dev_root;
288 struct btrfs_dev_extent *dev_extent = NULL;
291 u64 search_start = root->fs_info->alloc_start;
292 u64 search_end = device->total_bytes;
296 struct extent_buffer *l;
301 /* FIXME use last free of some kind */
303 /* we don't want to overwrite the superblock on the drive,
304 * so we make sure to start at an offset of at least 1MB
306 search_start = max(BTRFS_BLOCK_RESERVED_1M_FOR_SUPER, search_start);
308 if (search_start >= search_end) {
313 key.objectid = device->devid;
314 key.offset = search_start;
315 key.type = BTRFS_DEV_EXTENT_KEY;
316 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
319 ret = btrfs_previous_item(root, path, 0, key.type);
323 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
326 slot = path->slots[0];
327 if (slot >= btrfs_header_nritems(l)) {
328 ret = btrfs_next_leaf(root, path);
335 if (search_start >= search_end) {
339 *start = search_start;
343 *start = last_byte > search_start ?
344 last_byte : search_start;
345 if (search_end <= *start) {
351 btrfs_item_key_to_cpu(l, &key, slot);
353 if (key.objectid < device->devid)
356 if (key.objectid > device->devid)
359 if (key.offset >= search_start && key.offset > last_byte &&
361 if (last_byte < search_start)
362 last_byte = search_start;
363 hole_size = key.offset - last_byte;
364 if (key.offset > last_byte &&
365 hole_size >= num_bytes) {
370 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
375 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
376 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
382 /* we have to make sure we didn't find an extent that has already
383 * been allocated by the map tree or the original allocation
385 btrfs_release_path(path);
386 BUG_ON(*start < search_start);
388 if (*start + num_bytes > search_end) {
392 /* check for pending inserts here */
396 btrfs_release_path(path);
400 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
401 struct btrfs_device *device,
402 u64 chunk_tree, u64 chunk_objectid,
404 u64 num_bytes, u64 *start, int convert)
407 struct btrfs_path *path;
408 struct btrfs_root *root = device->dev_root;
409 struct btrfs_dev_extent *extent;
410 struct extent_buffer *leaf;
411 struct btrfs_key key;
413 path = btrfs_alloc_path();
418 * For convert case, just skip search free dev_extent, as caller
419 * is responsible to make sure it's free.
422 ret = find_free_dev_extent(trans, device, path, num_bytes,
428 key.objectid = device->devid;
430 key.type = BTRFS_DEV_EXTENT_KEY;
431 ret = btrfs_insert_empty_item(trans, root, path, &key,
435 leaf = path->nodes[0];
436 extent = btrfs_item_ptr(leaf, path->slots[0],
437 struct btrfs_dev_extent);
438 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
439 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
440 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
442 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
443 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
446 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
447 btrfs_mark_buffer_dirty(leaf);
449 btrfs_free_path(path);
453 static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset)
455 struct btrfs_path *path;
457 struct btrfs_key key;
458 struct btrfs_chunk *chunk;
459 struct btrfs_key found_key;
461 path = btrfs_alloc_path();
465 key.objectid = objectid;
466 key.offset = (u64)-1;
467 key.type = BTRFS_CHUNK_ITEM_KEY;
469 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
475 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
479 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
481 if (found_key.objectid != objectid)
484 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
486 *offset = found_key.offset +
487 btrfs_chunk_length(path->nodes[0], chunk);
492 btrfs_free_path(path);
496 static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
500 struct btrfs_key key;
501 struct btrfs_key found_key;
503 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
504 key.type = BTRFS_DEV_ITEM_KEY;
505 key.offset = (u64)-1;
507 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
513 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
518 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
520 *objectid = found_key.offset + 1;
524 btrfs_release_path(path);
529 * the device information is stored in the chunk root
530 * the btrfs_device struct should be fully filled in
532 int btrfs_add_device(struct btrfs_trans_handle *trans,
533 struct btrfs_root *root,
534 struct btrfs_device *device)
537 struct btrfs_path *path;
538 struct btrfs_dev_item *dev_item;
539 struct extent_buffer *leaf;
540 struct btrfs_key key;
544 root = root->fs_info->chunk_root;
546 path = btrfs_alloc_path();
550 ret = find_next_devid(root, path, &free_devid);
554 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
555 key.type = BTRFS_DEV_ITEM_KEY;
556 key.offset = free_devid;
558 ret = btrfs_insert_empty_item(trans, root, path, &key,
563 leaf = path->nodes[0];
564 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
566 device->devid = free_devid;
567 btrfs_set_device_id(leaf, dev_item, device->devid);
568 btrfs_set_device_generation(leaf, dev_item, 0);
569 btrfs_set_device_type(leaf, dev_item, device->type);
570 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
571 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
572 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
573 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
574 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
575 btrfs_set_device_group(leaf, dev_item, 0);
576 btrfs_set_device_seek_speed(leaf, dev_item, 0);
577 btrfs_set_device_bandwidth(leaf, dev_item, 0);
578 btrfs_set_device_start_offset(leaf, dev_item, 0);
580 ptr = (unsigned long)btrfs_device_uuid(dev_item);
581 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
582 ptr = (unsigned long)btrfs_device_fsid(dev_item);
583 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
584 btrfs_mark_buffer_dirty(leaf);
588 btrfs_free_path(path);
592 int btrfs_update_device(struct btrfs_trans_handle *trans,
593 struct btrfs_device *device)
596 struct btrfs_path *path;
597 struct btrfs_root *root;
598 struct btrfs_dev_item *dev_item;
599 struct extent_buffer *leaf;
600 struct btrfs_key key;
602 root = device->dev_root->fs_info->chunk_root;
604 path = btrfs_alloc_path();
608 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
609 key.type = BTRFS_DEV_ITEM_KEY;
610 key.offset = device->devid;
612 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
621 leaf = path->nodes[0];
622 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
624 btrfs_set_device_id(leaf, dev_item, device->devid);
625 btrfs_set_device_type(leaf, dev_item, device->type);
626 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
627 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
628 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
629 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
630 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
631 btrfs_mark_buffer_dirty(leaf);
634 btrfs_free_path(path);
638 int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
639 struct btrfs_root *root,
640 struct btrfs_key *key,
641 struct btrfs_chunk *chunk, int item_size)
643 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
644 struct btrfs_disk_key disk_key;
648 array_size = btrfs_super_sys_array_size(super_copy);
649 if (array_size + item_size + sizeof(disk_key)
650 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
653 ptr = super_copy->sys_chunk_array + array_size;
654 btrfs_cpu_key_to_disk(&disk_key, key);
655 memcpy(ptr, &disk_key, sizeof(disk_key));
656 ptr += sizeof(disk_key);
657 memcpy(ptr, chunk, item_size);
658 item_size += sizeof(disk_key);
659 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
663 static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes,
666 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
668 else if (type & BTRFS_BLOCK_GROUP_RAID10)
669 return calc_size * (num_stripes / sub_stripes);
670 else if (type & BTRFS_BLOCK_GROUP_RAID5)
671 return calc_size * (num_stripes - 1);
672 else if (type & BTRFS_BLOCK_GROUP_RAID6)
673 return calc_size * (num_stripes - 2);
675 return calc_size * num_stripes;
679 static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
681 /* TODO, add a way to store the preferred stripe size */
682 return BTRFS_STRIPE_LEN;
686 * btrfs_device_avail_bytes - count bytes available for alloc_chunk
688 * It is not equal to "device->total_bytes - device->bytes_used".
689 * We do not allocate any chunk in 1M at beginning of device, and not
690 * allowed to allocate any chunk before alloc_start if it is specified.
691 * So search holes from max(1M, alloc_start) to device->total_bytes.
693 static int btrfs_device_avail_bytes(struct btrfs_trans_handle *trans,
694 struct btrfs_device *device,
697 struct btrfs_path *path;
698 struct btrfs_root *root = device->dev_root;
699 struct btrfs_key key;
700 struct btrfs_dev_extent *dev_extent = NULL;
701 struct extent_buffer *l;
702 u64 search_start = root->fs_info->alloc_start;
703 u64 search_end = device->total_bytes;
709 search_start = max(BTRFS_BLOCK_RESERVED_1M_FOR_SUPER, search_start);
711 path = btrfs_alloc_path();
715 key.objectid = device->devid;
716 key.offset = root->fs_info->alloc_start;
717 key.type = BTRFS_DEV_EXTENT_KEY;
720 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
723 ret = btrfs_previous_item(root, path, 0, key.type);
729 slot = path->slots[0];
730 if (slot >= btrfs_header_nritems(l)) {
731 ret = btrfs_next_leaf(root, path);
738 btrfs_item_key_to_cpu(l, &key, slot);
740 if (key.objectid < device->devid)
742 if (key.objectid > device->devid)
744 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
746 if (key.offset > search_end)
748 if (key.offset > search_start)
749 free_bytes += key.offset - search_start;
751 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
752 extent_end = key.offset + btrfs_dev_extent_length(l,
754 if (extent_end > search_start)
755 search_start = extent_end;
756 if (search_start > search_end)
763 if (search_start < search_end)
764 free_bytes += search_end - search_start;
766 *avail_bytes = free_bytes;
769 btrfs_free_path(path);
773 #define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
774 - sizeof(struct btrfs_item) \
775 - sizeof(struct btrfs_chunk)) \
776 / sizeof(struct btrfs_stripe) + 1)
778 #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
779 - 2 * sizeof(struct btrfs_disk_key) \
780 - 2 * sizeof(struct btrfs_chunk)) \
781 / sizeof(struct btrfs_stripe) + 1)
783 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
784 struct btrfs_root *extent_root, u64 *start,
785 u64 *num_bytes, u64 type)
788 struct btrfs_fs_info *info = extent_root->fs_info;
789 struct btrfs_root *chunk_root = info->chunk_root;
790 struct btrfs_stripe *stripes;
791 struct btrfs_device *device = NULL;
792 struct btrfs_chunk *chunk;
793 struct list_head private_devs;
794 struct list_head *dev_list = &info->fs_devices->devices;
795 struct list_head *cur;
796 struct map_lookup *map;
797 int min_stripe_size = 1 * 1024 * 1024;
798 u64 calc_size = 8 * 1024 * 1024;
800 u64 max_chunk_size = 4 * calc_size;
811 int stripe_len = BTRFS_STRIPE_LEN;
812 struct btrfs_key key;
815 if (list_empty(dev_list)) {
819 if (type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
820 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
821 BTRFS_BLOCK_GROUP_RAID10 |
822 BTRFS_BLOCK_GROUP_DUP)) {
823 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
824 calc_size = 8 * 1024 * 1024;
825 max_chunk_size = calc_size * 2;
826 min_stripe_size = 1 * 1024 * 1024;
827 max_stripes = BTRFS_MAX_DEVS_SYS_CHUNK;
828 } else if (type & BTRFS_BLOCK_GROUP_DATA) {
829 calc_size = 1024 * 1024 * 1024;
830 max_chunk_size = 10 * calc_size;
831 min_stripe_size = 64 * 1024 * 1024;
832 max_stripes = BTRFS_MAX_DEVS(chunk_root);
833 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
834 calc_size = 1024 * 1024 * 1024;
835 max_chunk_size = 4 * calc_size;
836 min_stripe_size = 32 * 1024 * 1024;
837 max_stripes = BTRFS_MAX_DEVS(chunk_root);
840 if (type & BTRFS_BLOCK_GROUP_RAID1) {
841 num_stripes = min_t(u64, 2,
842 btrfs_super_num_devices(info->super_copy));
847 if (type & BTRFS_BLOCK_GROUP_DUP) {
851 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
852 num_stripes = btrfs_super_num_devices(info->super_copy);
853 if (num_stripes > max_stripes)
854 num_stripes = max_stripes;
857 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
858 num_stripes = btrfs_super_num_devices(info->super_copy);
859 if (num_stripes > max_stripes)
860 num_stripes = max_stripes;
863 num_stripes &= ~(u32)1;
867 if (type & (BTRFS_BLOCK_GROUP_RAID5)) {
868 num_stripes = btrfs_super_num_devices(info->super_copy);
869 if (num_stripes > max_stripes)
870 num_stripes = max_stripes;
874 stripe_len = find_raid56_stripe_len(num_stripes - 1,
875 btrfs_super_stripesize(info->super_copy));
877 if (type & (BTRFS_BLOCK_GROUP_RAID6)) {
878 num_stripes = btrfs_super_num_devices(info->super_copy);
879 if (num_stripes > max_stripes)
880 num_stripes = max_stripes;
884 stripe_len = find_raid56_stripe_len(num_stripes - 2,
885 btrfs_super_stripesize(info->super_copy));
888 /* we don't want a chunk larger than 10% of the FS */
889 percent_max = div_factor(btrfs_super_total_bytes(info->super_copy), 1);
890 max_chunk_size = min(percent_max, max_chunk_size);
893 if (chunk_bytes_by_type(type, calc_size, num_stripes, sub_stripes) >
895 calc_size = max_chunk_size;
896 calc_size /= num_stripes;
897 calc_size /= stripe_len;
898 calc_size *= stripe_len;
900 /* we don't want tiny stripes */
901 calc_size = max_t(u64, calc_size, min_stripe_size);
903 calc_size /= stripe_len;
904 calc_size *= stripe_len;
905 INIT_LIST_HEAD(&private_devs);
906 cur = dev_list->next;
909 if (type & BTRFS_BLOCK_GROUP_DUP)
910 min_free = calc_size * 2;
912 min_free = calc_size;
914 /* build a private list of devices we will allocate from */
915 while(index < num_stripes) {
916 device = list_entry(cur, struct btrfs_device, dev_list);
917 ret = btrfs_device_avail_bytes(trans, device, &avail);
921 if (avail >= min_free) {
922 list_move_tail(&device->dev_list, &private_devs);
924 if (type & BTRFS_BLOCK_GROUP_DUP)
926 } else if (avail > max_avail)
931 if (index < num_stripes) {
932 list_splice(&private_devs, dev_list);
933 if (index >= min_stripes) {
935 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
936 num_stripes /= sub_stripes;
937 num_stripes *= sub_stripes;
942 if (!looped && max_avail > 0) {
944 calc_size = max_avail;
949 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
953 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
954 key.type = BTRFS_CHUNK_ITEM_KEY;
957 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
961 map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
967 stripes = &chunk->stripe;
968 *num_bytes = chunk_bytes_by_type(type, calc_size,
969 num_stripes, sub_stripes);
971 while(index < num_stripes) {
972 struct btrfs_stripe *stripe;
973 BUG_ON(list_empty(&private_devs));
974 cur = private_devs.next;
975 device = list_entry(cur, struct btrfs_device, dev_list);
977 /* loop over this device again if we're doing a dup group */
978 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
979 (index == num_stripes - 1))
980 list_move_tail(&device->dev_list, dev_list);
982 ret = btrfs_alloc_dev_extent(trans, device,
983 info->chunk_root->root_key.objectid,
984 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
985 calc_size, &dev_offset, 0);
988 device->bytes_used += calc_size;
989 ret = btrfs_update_device(trans, device);
992 map->stripes[index].dev = device;
993 map->stripes[index].physical = dev_offset;
994 stripe = stripes + index;
995 btrfs_set_stack_stripe_devid(stripe, device->devid);
996 btrfs_set_stack_stripe_offset(stripe, dev_offset);
997 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
1000 BUG_ON(!list_empty(&private_devs));
1002 /* key was set above */
1003 btrfs_set_stack_chunk_length(chunk, *num_bytes);
1004 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
1005 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
1006 btrfs_set_stack_chunk_type(chunk, type);
1007 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
1008 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
1009 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
1010 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
1011 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
1012 map->sector_size = extent_root->sectorsize;
1013 map->stripe_len = stripe_len;
1014 map->io_align = stripe_len;
1015 map->io_width = stripe_len;
1017 map->num_stripes = num_stripes;
1018 map->sub_stripes = sub_stripes;
1020 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
1021 btrfs_chunk_item_size(num_stripes));
1023 *start = key.offset;;
1025 map->ce.start = key.offset;
1026 map->ce.size = *num_bytes;
1028 ret = insert_cache_extent(&info->mapping_tree.cache_tree, &map->ce);
1031 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
1032 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
1033 chunk, btrfs_chunk_item_size(num_stripes));
1042 * Alloc a DATA chunk with SINGLE profile.
1044 * If 'convert' is set, it will alloc a chunk with 1:1 mapping
1045 * (btrfs logical bytenr == on-disk bytenr)
1046 * For that case, caller must make sure the chunk and dev_extent are not
1049 int btrfs_alloc_data_chunk(struct btrfs_trans_handle *trans,
1050 struct btrfs_root *extent_root, u64 *start,
1051 u64 num_bytes, u64 type, int convert)
1054 struct btrfs_fs_info *info = extent_root->fs_info;
1055 struct btrfs_root *chunk_root = info->chunk_root;
1056 struct btrfs_stripe *stripes;
1057 struct btrfs_device *device = NULL;
1058 struct btrfs_chunk *chunk;
1059 struct list_head *dev_list = &info->fs_devices->devices;
1060 struct list_head *cur;
1061 struct map_lookup *map;
1062 u64 calc_size = 8 * 1024 * 1024;
1063 int num_stripes = 1;
1064 int sub_stripes = 0;
1067 int stripe_len = BTRFS_STRIPE_LEN;
1068 struct btrfs_key key;
1070 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1071 key.type = BTRFS_CHUNK_ITEM_KEY;
1073 BUG_ON(*start != round_down(*start, extent_root->sectorsize));
1074 key.offset = *start;
1075 dev_offset = *start;
1077 ret = find_next_chunk(chunk_root,
1078 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1084 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
1088 map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
1094 stripes = &chunk->stripe;
1095 calc_size = num_bytes;
1098 cur = dev_list->next;
1099 device = list_entry(cur, struct btrfs_device, dev_list);
1101 while (index < num_stripes) {
1102 struct btrfs_stripe *stripe;
1104 ret = btrfs_alloc_dev_extent(trans, device,
1105 info->chunk_root->root_key.objectid,
1106 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
1107 calc_size, &dev_offset, convert);
1110 device->bytes_used += calc_size;
1111 ret = btrfs_update_device(trans, device);
1114 map->stripes[index].dev = device;
1115 map->stripes[index].physical = dev_offset;
1116 stripe = stripes + index;
1117 btrfs_set_stack_stripe_devid(stripe, device->devid);
1118 btrfs_set_stack_stripe_offset(stripe, dev_offset);
1119 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
1123 /* key was set above */
1124 btrfs_set_stack_chunk_length(chunk, num_bytes);
1125 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
1126 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
1127 btrfs_set_stack_chunk_type(chunk, type);
1128 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
1129 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
1130 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
1131 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
1132 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
1133 map->sector_size = extent_root->sectorsize;
1134 map->stripe_len = stripe_len;
1135 map->io_align = stripe_len;
1136 map->io_width = stripe_len;
1138 map->num_stripes = num_stripes;
1139 map->sub_stripes = sub_stripes;
1141 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
1142 btrfs_chunk_item_size(num_stripes));
1145 *start = key.offset;
1147 map->ce.start = key.offset;
1148 map->ce.size = num_bytes;
1150 ret = insert_cache_extent(&info->mapping_tree.cache_tree, &map->ce);
1157 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
1159 struct cache_extent *ce;
1160 struct map_lookup *map;
1163 ce = search_cache_extent(&map_tree->cache_tree, logical);
1165 fprintf(stderr, "No mapping for %llu-%llu\n",
1166 (unsigned long long)logical,
1167 (unsigned long long)logical+len);
1170 if (ce->start > logical || ce->start + ce->size < logical) {
1171 fprintf(stderr, "Invalid mapping for %llu-%llu, got "
1172 "%llu-%llu\n", (unsigned long long)logical,
1173 (unsigned long long)logical+len,
1174 (unsigned long long)ce->start,
1175 (unsigned long long)ce->start + ce->size);
1178 map = container_of(ce, struct map_lookup, ce);
1180 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
1181 ret = map->num_stripes;
1182 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1183 ret = map->sub_stripes;
1184 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
1186 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
1193 int btrfs_next_bg(struct btrfs_mapping_tree *map_tree, u64 *logical,
1194 u64 *size, u64 type)
1196 struct cache_extent *ce;
1197 struct map_lookup *map;
1200 ce = search_cache_extent(&map_tree->cache_tree, cur);
1204 * only jump to next bg if our cur is not 0
1205 * As the initial logical for btrfs_next_bg() is 0, and
1206 * if we jump to next bg, we skipped a valid bg.
1209 ce = next_cache_extent(ce);
1215 map = container_of(ce, struct map_lookup, ce);
1216 if (map->type & type) {
1217 *logical = ce->start;
1226 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
1227 u64 chunk_start, u64 physical, u64 devid,
1228 u64 **logical, int *naddrs, int *stripe_len)
1230 struct cache_extent *ce;
1231 struct map_lookup *map;
1239 ce = search_cache_extent(&map_tree->cache_tree, chunk_start);
1241 map = container_of(ce, struct map_lookup, ce);
1244 rmap_len = map->stripe_len;
1245 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1246 length = ce->size / (map->num_stripes / map->sub_stripes);
1247 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
1248 length = ce->size / map->num_stripes;
1249 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
1250 BTRFS_BLOCK_GROUP_RAID6)) {
1251 length = ce->size / nr_data_stripes(map);
1252 rmap_len = map->stripe_len * nr_data_stripes(map);
1255 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
1257 for (i = 0; i < map->num_stripes; i++) {
1258 if (devid && map->stripes[i].dev->devid != devid)
1260 if (map->stripes[i].physical > physical ||
1261 map->stripes[i].physical + length <= physical)
1264 stripe_nr = (physical - map->stripes[i].physical) /
1267 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1268 stripe_nr = (stripe_nr * map->num_stripes + i) /
1270 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
1271 stripe_nr = stripe_nr * map->num_stripes + i;
1272 } /* else if RAID[56], multiply by nr_data_stripes().
1273 * Alternatively, just use rmap_len below instead of
1274 * map->stripe_len */
1276 bytenr = ce->start + stripe_nr * rmap_len;
1277 for (j = 0; j < nr; j++) {
1278 if (buf[j] == bytenr)
1287 *stripe_len = rmap_len;
1292 static inline int parity_smaller(u64 a, u64 b)
1297 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
1298 static void sort_parity_stripes(struct btrfs_multi_bio *bbio, u64 *raid_map)
1300 struct btrfs_bio_stripe s;
1307 for (i = 0; i < bbio->num_stripes - 1; i++) {
1308 if (parity_smaller(raid_map[i], raid_map[i+1])) {
1309 s = bbio->stripes[i];
1311 bbio->stripes[i] = bbio->stripes[i+1];
1312 raid_map[i] = raid_map[i+1];
1313 bbio->stripes[i+1] = s;
1321 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1322 u64 logical, u64 *length,
1323 struct btrfs_multi_bio **multi_ret, int mirror_num,
1326 return __btrfs_map_block(map_tree, rw, logical, length, NULL,
1327 multi_ret, mirror_num, raid_map_ret);
1330 int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1331 u64 logical, u64 *length, u64 *type,
1332 struct btrfs_multi_bio **multi_ret, int mirror_num,
1335 struct cache_extent *ce;
1336 struct map_lookup *map;
1340 u64 *raid_map = NULL;
1341 int stripes_allocated = 8;
1342 int stripes_required = 1;
1345 struct btrfs_multi_bio *multi = NULL;
1347 if (multi_ret && rw == READ) {
1348 stripes_allocated = 1;
1351 ce = search_cache_extent(&map_tree->cache_tree, logical);
1357 if (ce->start > logical) {
1359 *length = ce->start - logical;
1364 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
1369 map = container_of(ce, struct map_lookup, ce);
1370 offset = logical - ce->start;
1373 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
1374 BTRFS_BLOCK_GROUP_DUP)) {
1375 stripes_required = map->num_stripes;
1376 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1377 stripes_required = map->sub_stripes;
1380 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)
1381 && multi_ret && ((rw & WRITE) || mirror_num > 1) && raid_map_ret) {
1382 /* RAID[56] write or recovery. Return all stripes */
1383 stripes_required = map->num_stripes;
1385 /* Only allocate the map if we've already got a large enough multi_ret */
1386 if (stripes_allocated >= stripes_required) {
1387 raid_map = kmalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
1395 /* if our multi bio struct is too small, back off and try again */
1396 if (multi_ret && stripes_allocated < stripes_required) {
1397 stripes_allocated = stripes_required;
1404 * stripe_nr counts the total number of stripes we have to stride
1405 * to get to this block
1407 stripe_nr = stripe_nr / map->stripe_len;
1409 stripe_offset = stripe_nr * map->stripe_len;
1410 BUG_ON(offset < stripe_offset);
1412 /* stripe_offset is the offset of this block in its stripe*/
1413 stripe_offset = offset - stripe_offset;
1415 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
1416 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
1417 BTRFS_BLOCK_GROUP_RAID10 |
1418 BTRFS_BLOCK_GROUP_DUP)) {
1419 /* we limit the length of each bio to what fits in a stripe */
1420 *length = min_t(u64, ce->size - offset,
1421 map->stripe_len - stripe_offset);
1423 *length = ce->size - offset;
1429 multi->num_stripes = 1;
1431 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
1433 multi->num_stripes = map->num_stripes;
1434 else if (mirror_num)
1435 stripe_index = mirror_num - 1;
1437 stripe_index = stripe_nr % map->num_stripes;
1438 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1439 int factor = map->num_stripes / map->sub_stripes;
1441 stripe_index = stripe_nr % factor;
1442 stripe_index *= map->sub_stripes;
1445 multi->num_stripes = map->sub_stripes;
1446 else if (mirror_num)
1447 stripe_index += mirror_num - 1;
1449 stripe_nr = stripe_nr / factor;
1450 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
1452 multi->num_stripes = map->num_stripes;
1453 else if (mirror_num)
1454 stripe_index = mirror_num - 1;
1455 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
1456 BTRFS_BLOCK_GROUP_RAID6)) {
1461 u64 raid56_full_stripe_start;
1462 u64 full_stripe_len = nr_data_stripes(map) * map->stripe_len;
1465 * align the start of our data stripe in the logical
1468 raid56_full_stripe_start = offset / full_stripe_len;
1469 raid56_full_stripe_start *= full_stripe_len;
1471 /* get the data stripe number */
1472 stripe_nr = raid56_full_stripe_start / map->stripe_len;
1473 stripe_nr = stripe_nr / nr_data_stripes(map);
1475 /* Work out the disk rotation on this stripe-set */
1476 rot = stripe_nr % map->num_stripes;
1478 /* Fill in the logical address of each stripe */
1479 tmp = stripe_nr * nr_data_stripes(map);
1481 for (i = 0; i < nr_data_stripes(map); i++)
1482 raid_map[(i+rot) % map->num_stripes] =
1483 ce->start + (tmp + i) * map->stripe_len;
1485 raid_map[(i+rot) % map->num_stripes] = BTRFS_RAID5_P_STRIPE;
1486 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
1487 raid_map[(i+rot+1) % map->num_stripes] = BTRFS_RAID6_Q_STRIPE;
1489 *length = map->stripe_len;
1492 multi->num_stripes = map->num_stripes;
1494 stripe_index = stripe_nr % nr_data_stripes(map);
1495 stripe_nr = stripe_nr / nr_data_stripes(map);
1498 * Mirror #0 or #1 means the original data block.
1499 * Mirror #2 is RAID5 parity block.
1500 * Mirror #3 is RAID6 Q block.
1503 stripe_index = nr_data_stripes(map) + mirror_num - 2;
1505 /* We distribute the parity blocks across stripes */
1506 stripe_index = (stripe_nr + stripe_index) % map->num_stripes;
1510 * after this do_div call, stripe_nr is the number of stripes
1511 * on this device we have to walk to find the data, and
1512 * stripe_index is the number of our device in the stripe array
1514 stripe_index = stripe_nr % map->num_stripes;
1515 stripe_nr = stripe_nr / map->num_stripes;
1517 BUG_ON(stripe_index >= map->num_stripes);
1519 for (i = 0; i < multi->num_stripes; i++) {
1520 multi->stripes[i].physical =
1521 map->stripes[stripe_index].physical + stripe_offset +
1522 stripe_nr * map->stripe_len;
1523 multi->stripes[i].dev = map->stripes[stripe_index].dev;
1532 sort_parity_stripes(multi, raid_map);
1533 *raid_map_ret = raid_map;
1539 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
1542 struct btrfs_device *device;
1543 struct btrfs_fs_devices *cur_devices;
1545 cur_devices = root->fs_info->fs_devices;
1546 while (cur_devices) {
1548 (!memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE) ||
1549 root->fs_info->ignore_fsid_mismatch)) {
1550 device = __find_device(&cur_devices->devices,
1555 cur_devices = cur_devices->seed;
1560 struct btrfs_device *
1561 btrfs_find_device_by_devid(struct btrfs_fs_devices *fs_devices,
1562 u64 devid, int instance)
1564 struct list_head *head = &fs_devices->devices;
1565 struct btrfs_device *dev;
1568 list_for_each_entry(dev, head, dev_list) {
1569 if (dev->devid == devid && num_found++ == instance)
1575 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
1577 struct cache_extent *ce;
1578 struct map_lookup *map;
1579 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1584 * During chunk recovering, we may fail to find block group's
1585 * corresponding chunk, we will rebuild it later
1587 ce = search_cache_extent(&map_tree->cache_tree, chunk_offset);
1588 if (!root->fs_info->is_chunk_recover)
1593 map = container_of(ce, struct map_lookup, ce);
1594 for (i = 0; i < map->num_stripes; i++) {
1595 if (!map->stripes[i].dev->writeable) {
1604 static struct btrfs_device *fill_missing_device(u64 devid)
1606 struct btrfs_device *device;
1608 device = kzalloc(sizeof(*device), GFP_NOFS);
1609 device->devid = devid;
1615 * slot == -1: SYSTEM chunk
1616 * return -EIO on error, otherwise return 0
1618 int btrfs_check_chunk_valid(struct btrfs_root *root,
1619 struct extent_buffer *leaf,
1620 struct btrfs_chunk *chunk,
1621 int slot, u64 logical)
1629 length = btrfs_chunk_length(leaf, chunk);
1630 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1631 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1632 sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
1633 type = btrfs_chunk_type(leaf, chunk);
1636 * These valid checks may be insufficient to cover every corner cases.
1638 if (!IS_ALIGNED(logical, root->sectorsize)) {
1639 error("invalid chunk logical %llu", logical);
1642 if (btrfs_chunk_sector_size(leaf, chunk) != root->sectorsize) {
1643 error("invalid chunk sectorsize %llu",
1644 (unsigned long long)btrfs_chunk_sector_size(leaf, chunk));
1647 if (!length || !IS_ALIGNED(length, root->sectorsize)) {
1648 error("invalid chunk length %llu", length);
1651 if (stripe_len != BTRFS_STRIPE_LEN) {
1652 error("invalid chunk stripe length: %llu", stripe_len);
1655 /* Check on chunk item type */
1656 if (slot == -1 && (type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
1657 error("invalid chunk type %llu", type);
1660 if (type & ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
1661 BTRFS_BLOCK_GROUP_PROFILE_MASK)) {
1662 error("unrecognized chunk type: %llu",
1663 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
1664 BTRFS_BLOCK_GROUP_PROFILE_MASK) & type);
1668 * Btrfs_chunk contains at least one stripe, and for sys_chunk
1669 * it can't exceed the system chunk array size
1670 * For normal chunk, it should match its chunk item size.
1672 if (num_stripes < 1 ||
1673 (slot == -1 && sizeof(struct btrfs_stripe) * num_stripes >
1674 BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) ||
1675 (slot >= 0 && sizeof(struct btrfs_stripe) * (num_stripes - 1) >
1676 btrfs_item_size_nr(leaf, slot))) {
1677 error("invalid num_stripes: %u", num_stripes);
1681 * Device number check against profile
1683 if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes == 0) ||
1684 (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) ||
1685 (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
1686 (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
1687 (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||
1688 ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
1689 num_stripes != 1)) {
1690 error("Invalid num_stripes:sub_stripes %u:%u for profile %llu",
1691 num_stripes, sub_stripes,
1692 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
1700 * Slot is used to verify the chunk item is valid
1702 * For sys chunk in superblock, pass -1 to indicate sys chunk.
1704 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
1705 struct extent_buffer *leaf,
1706 struct btrfs_chunk *chunk, int slot)
1708 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1709 struct map_lookup *map;
1710 struct cache_extent *ce;
1714 u8 uuid[BTRFS_UUID_SIZE];
1719 logical = key->offset;
1720 length = btrfs_chunk_length(leaf, chunk);
1721 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1722 /* Validation check */
1723 ret = btrfs_check_chunk_valid(root, leaf, chunk, slot, logical);
1725 error("%s checksums match, but it has an invalid chunk, %s",
1726 (slot == -1) ? "Superblock" : "Metadata",
1727 (slot == -1) ? "try btrfsck --repair -s <superblock> ie, 0,1,2" : "");
1731 ce = search_cache_extent(&map_tree->cache_tree, logical);
1733 /* already mapped? */
1734 if (ce && ce->start <= logical && ce->start + ce->size > logical) {
1738 map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
1742 map->ce.start = logical;
1743 map->ce.size = length;
1744 map->num_stripes = num_stripes;
1745 map->io_width = btrfs_chunk_io_width(leaf, chunk);
1746 map->io_align = btrfs_chunk_io_align(leaf, chunk);
1747 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1748 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1749 map->type = btrfs_chunk_type(leaf, chunk);
1750 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
1752 for (i = 0; i < num_stripes; i++) {
1753 map->stripes[i].physical =
1754 btrfs_stripe_offset_nr(leaf, chunk, i);
1755 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
1756 read_extent_buffer(leaf, uuid, (unsigned long)
1757 btrfs_stripe_dev_uuid_nr(chunk, i),
1759 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
1761 if (!map->stripes[i].dev) {
1762 map->stripes[i].dev = fill_missing_device(devid);
1763 printf("warning, device %llu is missing\n",
1764 (unsigned long long)devid);
1765 list_add(&map->stripes[i].dev->dev_list,
1766 &root->fs_info->fs_devices->devices);
1770 ret = insert_cache_extent(&map_tree->cache_tree, &map->ce);
1776 static int fill_device_from_item(struct extent_buffer *leaf,
1777 struct btrfs_dev_item *dev_item,
1778 struct btrfs_device *device)
1782 device->devid = btrfs_device_id(leaf, dev_item);
1783 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1784 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1785 device->type = btrfs_device_type(leaf, dev_item);
1786 device->io_align = btrfs_device_io_align(leaf, dev_item);
1787 device->io_width = btrfs_device_io_width(leaf, dev_item);
1788 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
1790 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1791 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1796 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
1798 struct btrfs_fs_devices *fs_devices;
1801 fs_devices = root->fs_info->fs_devices->seed;
1802 while (fs_devices) {
1803 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
1807 fs_devices = fs_devices->seed;
1810 fs_devices = find_fsid(fsid);
1812 /* missing all seed devices */
1813 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1818 INIT_LIST_HEAD(&fs_devices->devices);
1819 list_add(&fs_devices->list, &fs_uuids);
1820 memcpy(fs_devices->fsid, fsid, BTRFS_FSID_SIZE);
1823 ret = btrfs_open_devices(fs_devices, O_RDONLY);
1827 fs_devices->seed = root->fs_info->fs_devices->seed;
1828 root->fs_info->fs_devices->seed = fs_devices;
1833 static int read_one_dev(struct btrfs_root *root,
1834 struct extent_buffer *leaf,
1835 struct btrfs_dev_item *dev_item)
1837 struct btrfs_device *device;
1840 u8 fs_uuid[BTRFS_UUID_SIZE];
1841 u8 dev_uuid[BTRFS_UUID_SIZE];
1843 devid = btrfs_device_id(leaf, dev_item);
1844 read_extent_buffer(leaf, dev_uuid,
1845 (unsigned long)btrfs_device_uuid(dev_item),
1847 read_extent_buffer(leaf, fs_uuid,
1848 (unsigned long)btrfs_device_fsid(dev_item),
1851 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
1852 ret = open_seed_devices(root, fs_uuid);
1857 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1859 device = kzalloc(sizeof(*device), GFP_NOFS);
1863 list_add(&device->dev_list,
1864 &root->fs_info->fs_devices->devices);
1867 fill_device_from_item(leaf, dev_item, device);
1868 device->dev_root = root->fs_info->dev_root;
1872 int btrfs_read_sys_array(struct btrfs_root *root)
1874 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
1875 struct extent_buffer *sb;
1876 struct btrfs_disk_key *disk_key;
1877 struct btrfs_chunk *chunk;
1879 unsigned long sb_array_offset;
1885 struct btrfs_key key;
1887 sb = btrfs_find_create_tree_block(root->fs_info,
1888 BTRFS_SUPER_INFO_OFFSET,
1889 BTRFS_SUPER_INFO_SIZE);
1892 btrfs_set_buffer_uptodate(sb);
1893 write_extent_buffer(sb, super_copy, 0, sizeof(*super_copy));
1894 array_size = btrfs_super_sys_array_size(super_copy);
1896 array_ptr = super_copy->sys_chunk_array;
1897 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
1900 while (cur_offset < array_size) {
1901 disk_key = (struct btrfs_disk_key *)array_ptr;
1902 len = sizeof(*disk_key);
1903 if (cur_offset + len > array_size)
1904 goto out_short_read;
1906 btrfs_disk_key_to_cpu(&key, disk_key);
1909 sb_array_offset += len;
1912 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1913 chunk = (struct btrfs_chunk *)sb_array_offset;
1915 * At least one btrfs_chunk with one stripe must be
1916 * present, exact stripe count check comes afterwards
1918 len = btrfs_chunk_item_size(1);
1919 if (cur_offset + len > array_size)
1920 goto out_short_read;
1922 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
1925 "ERROR: invalid number of stripes %u in sys_array at offset %u\n",
1926 num_stripes, cur_offset);
1931 len = btrfs_chunk_item_size(num_stripes);
1932 if (cur_offset + len > array_size)
1933 goto out_short_read;
1935 ret = read_one_chunk(root, &key, sb, chunk, -1);
1940 "ERROR: unexpected item type %u in sys_array at offset %u\n",
1941 (u32)key.type, cur_offset);
1946 sb_array_offset += len;
1949 free_extent_buffer(sb);
1953 printk("ERROR: sys_array too short to read %u bytes at offset %u\n",
1955 free_extent_buffer(sb);
1959 int btrfs_read_chunk_tree(struct btrfs_root *root)
1961 struct btrfs_path *path;
1962 struct extent_buffer *leaf;
1963 struct btrfs_key key;
1964 struct btrfs_key found_key;
1968 root = root->fs_info->chunk_root;
1970 path = btrfs_alloc_path();
1975 * Read all device items, and then all the chunk items. All
1976 * device items are found before any chunk item (their object id
1977 * is smaller than the lowest possible object id for a chunk
1978 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
1980 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1983 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1987 leaf = path->nodes[0];
1988 slot = path->slots[0];
1989 if (slot >= btrfs_header_nritems(leaf)) {
1990 ret = btrfs_next_leaf(root, path);
1997 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1998 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
1999 struct btrfs_dev_item *dev_item;
2000 dev_item = btrfs_item_ptr(leaf, slot,
2001 struct btrfs_dev_item);
2002 ret = read_one_dev(root, leaf, dev_item);
2004 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
2005 struct btrfs_chunk *chunk;
2006 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2007 ret = read_one_chunk(root, &found_key, leaf, chunk,
2016 btrfs_free_path(path);
2020 struct list_head *btrfs_scanned_uuids(void)
2025 static int rmw_eb(struct btrfs_fs_info *info,
2026 struct extent_buffer *eb, struct extent_buffer *orig_eb)
2029 unsigned long orig_off = 0;
2030 unsigned long dest_off = 0;
2031 unsigned long copy_len = eb->len;
2033 ret = read_whole_eb(info, eb, 0);
2037 if (eb->start + eb->len <= orig_eb->start ||
2038 eb->start >= orig_eb->start + orig_eb->len)
2041 * | ----- orig_eb ------- |
2042 * | ----- stripe ------- |
2043 * | ----- orig_eb ------- |
2044 * | ----- orig_eb ------- |
2046 if (eb->start > orig_eb->start)
2047 orig_off = eb->start - orig_eb->start;
2048 if (orig_eb->start > eb->start)
2049 dest_off = orig_eb->start - eb->start;
2051 if (copy_len > orig_eb->len - orig_off)
2052 copy_len = orig_eb->len - orig_off;
2053 if (copy_len > eb->len - dest_off)
2054 copy_len = eb->len - dest_off;
2056 memcpy(eb->data + dest_off, orig_eb->data + orig_off, copy_len);
2060 static void split_eb_for_raid56(struct btrfs_fs_info *info,
2061 struct extent_buffer *orig_eb,
2062 struct extent_buffer **ebs,
2063 u64 stripe_len, u64 *raid_map,
2066 struct extent_buffer *eb;
2067 u64 start = orig_eb->start;
2072 for (i = 0; i < num_stripes; i++) {
2073 if (raid_map[i] >= BTRFS_RAID5_P_STRIPE)
2076 eb = calloc(1, sizeof(struct extent_buffer) + stripe_len);
2080 eb->start = raid_map[i];
2081 eb->len = stripe_len;
2085 eb->dev_bytenr = (u64)-1;
2087 this_eb_start = raid_map[i];
2089 if (start > this_eb_start ||
2090 start + orig_eb->len < this_eb_start + stripe_len) {
2091 ret = rmw_eb(info, eb, orig_eb);
2094 memcpy(eb->data, orig_eb->data + eb->start - start, stripe_len);
2100 int write_raid56_with_parity(struct btrfs_fs_info *info,
2101 struct extent_buffer *eb,
2102 struct btrfs_multi_bio *multi,
2103 u64 stripe_len, u64 *raid_map)
2105 struct extent_buffer **ebs, *p_eb = NULL, *q_eb = NULL;
2109 int alloc_size = eb->len;
2111 ebs = kmalloc(sizeof(*ebs) * multi->num_stripes, GFP_NOFS);
2114 if (stripe_len > alloc_size)
2115 alloc_size = stripe_len;
2117 split_eb_for_raid56(info, eb, ebs, stripe_len, raid_map,
2118 multi->num_stripes);
2120 for (i = 0; i < multi->num_stripes; i++) {
2121 struct extent_buffer *new_eb;
2122 if (raid_map[i] < BTRFS_RAID5_P_STRIPE) {
2123 ebs[i]->dev_bytenr = multi->stripes[i].physical;
2124 ebs[i]->fd = multi->stripes[i].dev->fd;
2125 multi->stripes[i].dev->total_ios++;
2126 BUG_ON(ebs[i]->start != raid_map[i]);
2129 new_eb = kmalloc(sizeof(*eb) + alloc_size, GFP_NOFS);
2131 new_eb->dev_bytenr = multi->stripes[i].physical;
2132 new_eb->fd = multi->stripes[i].dev->fd;
2133 multi->stripes[i].dev->total_ios++;
2134 new_eb->len = stripe_len;
2136 if (raid_map[i] == BTRFS_RAID5_P_STRIPE)
2138 else if (raid_map[i] == BTRFS_RAID6_Q_STRIPE)
2144 pointers = kmalloc(sizeof(*pointers) * multi->num_stripes,
2148 ebs[multi->num_stripes - 2] = p_eb;
2149 ebs[multi->num_stripes - 1] = q_eb;
2151 for (i = 0; i < multi->num_stripes; i++)
2152 pointers[i] = ebs[i]->data;
2154 raid6_gen_syndrome(multi->num_stripes, stripe_len, pointers);
2157 ebs[multi->num_stripes - 1] = p_eb;
2158 memcpy(p_eb->data, ebs[0]->data, stripe_len);
2159 for (j = 1; j < multi->num_stripes - 1; j++) {
2160 for (i = 0; i < stripe_len; i += sizeof(u64)) {
2164 p_eb_data = get_unaligned_64(p_eb->data + i);
2165 ebs_data = get_unaligned_64(ebs[j]->data + i);
2166 p_eb_data ^= ebs_data;
2167 put_unaligned_64(p_eb_data, p_eb->data + i);
2172 for (i = 0; i < multi->num_stripes; i++) {
2173 ret = write_extent_to_disk(ebs[i]);