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, int super_recover)
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, super_recover);
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();
464 key.objectid = objectid;
465 key.offset = (u64)-1;
466 key.type = BTRFS_CHUNK_ITEM_KEY;
468 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
474 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
478 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
480 if (found_key.objectid != objectid)
483 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
485 *offset = found_key.offset +
486 btrfs_chunk_length(path->nodes[0], chunk);
491 btrfs_free_path(path);
495 static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
499 struct btrfs_key key;
500 struct btrfs_key found_key;
502 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
503 key.type = BTRFS_DEV_ITEM_KEY;
504 key.offset = (u64)-1;
506 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
512 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
517 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
519 *objectid = found_key.offset + 1;
523 btrfs_release_path(path);
528 * the device information is stored in the chunk root
529 * the btrfs_device struct should be fully filled in
531 int btrfs_add_device(struct btrfs_trans_handle *trans,
532 struct btrfs_root *root,
533 struct btrfs_device *device)
536 struct btrfs_path *path;
537 struct btrfs_dev_item *dev_item;
538 struct extent_buffer *leaf;
539 struct btrfs_key key;
543 root = root->fs_info->chunk_root;
545 path = btrfs_alloc_path();
549 ret = find_next_devid(root, path, &free_devid);
553 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
554 key.type = BTRFS_DEV_ITEM_KEY;
555 key.offset = free_devid;
557 ret = btrfs_insert_empty_item(trans, root, path, &key,
562 leaf = path->nodes[0];
563 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
565 device->devid = free_devid;
566 btrfs_set_device_id(leaf, dev_item, device->devid);
567 btrfs_set_device_generation(leaf, dev_item, 0);
568 btrfs_set_device_type(leaf, dev_item, device->type);
569 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
570 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
571 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
572 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
573 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
574 btrfs_set_device_group(leaf, dev_item, 0);
575 btrfs_set_device_seek_speed(leaf, dev_item, 0);
576 btrfs_set_device_bandwidth(leaf, dev_item, 0);
577 btrfs_set_device_start_offset(leaf, dev_item, 0);
579 ptr = (unsigned long)btrfs_device_uuid(dev_item);
580 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
581 ptr = (unsigned long)btrfs_device_fsid(dev_item);
582 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
583 btrfs_mark_buffer_dirty(leaf);
587 btrfs_free_path(path);
591 int btrfs_update_device(struct btrfs_trans_handle *trans,
592 struct btrfs_device *device)
595 struct btrfs_path *path;
596 struct btrfs_root *root;
597 struct btrfs_dev_item *dev_item;
598 struct extent_buffer *leaf;
599 struct btrfs_key key;
601 root = device->dev_root->fs_info->chunk_root;
603 path = btrfs_alloc_path();
607 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
608 key.type = BTRFS_DEV_ITEM_KEY;
609 key.offset = device->devid;
611 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
620 leaf = path->nodes[0];
621 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
623 btrfs_set_device_id(leaf, dev_item, device->devid);
624 btrfs_set_device_type(leaf, dev_item, device->type);
625 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
626 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
627 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
628 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
629 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
630 btrfs_mark_buffer_dirty(leaf);
633 btrfs_free_path(path);
637 int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
638 struct btrfs_root *root,
639 struct btrfs_key *key,
640 struct btrfs_chunk *chunk, int item_size)
642 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
643 struct btrfs_disk_key disk_key;
647 array_size = btrfs_super_sys_array_size(super_copy);
648 if (array_size + item_size + sizeof(disk_key)
649 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
652 ptr = super_copy->sys_chunk_array + array_size;
653 btrfs_cpu_key_to_disk(&disk_key, key);
654 memcpy(ptr, &disk_key, sizeof(disk_key));
655 ptr += sizeof(disk_key);
656 memcpy(ptr, chunk, item_size);
657 item_size += sizeof(disk_key);
658 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
662 static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes,
665 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
667 else if (type & BTRFS_BLOCK_GROUP_RAID10)
668 return calc_size * (num_stripes / sub_stripes);
669 else if (type & BTRFS_BLOCK_GROUP_RAID5)
670 return calc_size * (num_stripes - 1);
671 else if (type & BTRFS_BLOCK_GROUP_RAID6)
672 return calc_size * (num_stripes - 2);
674 return calc_size * num_stripes;
678 static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
680 /* TODO, add a way to store the preferred stripe size */
681 return BTRFS_STRIPE_LEN;
685 * btrfs_device_avail_bytes - count bytes available for alloc_chunk
687 * It is not equal to "device->total_bytes - device->bytes_used".
688 * We do not allocate any chunk in 1M at beginning of device, and not
689 * allowed to allocate any chunk before alloc_start if it is specified.
690 * So search holes from max(1M, alloc_start) to device->total_bytes.
692 static int btrfs_device_avail_bytes(struct btrfs_trans_handle *trans,
693 struct btrfs_device *device,
696 struct btrfs_path *path;
697 struct btrfs_root *root = device->dev_root;
698 struct btrfs_key key;
699 struct btrfs_dev_extent *dev_extent = NULL;
700 struct extent_buffer *l;
701 u64 search_start = root->fs_info->alloc_start;
702 u64 search_end = device->total_bytes;
708 search_start = max(BTRFS_BLOCK_RESERVED_1M_FOR_SUPER, search_start);
710 path = btrfs_alloc_path();
714 key.objectid = device->devid;
715 key.offset = root->fs_info->alloc_start;
716 key.type = BTRFS_DEV_EXTENT_KEY;
719 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
722 ret = btrfs_previous_item(root, path, 0, key.type);
728 slot = path->slots[0];
729 if (slot >= btrfs_header_nritems(l)) {
730 ret = btrfs_next_leaf(root, path);
737 btrfs_item_key_to_cpu(l, &key, slot);
739 if (key.objectid < device->devid)
741 if (key.objectid > device->devid)
743 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
745 if (key.offset > search_end)
747 if (key.offset > search_start)
748 free_bytes += key.offset - search_start;
750 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
751 extent_end = key.offset + btrfs_dev_extent_length(l,
753 if (extent_end > search_start)
754 search_start = extent_end;
755 if (search_start > search_end)
762 if (search_start < search_end)
763 free_bytes += search_end - search_start;
765 *avail_bytes = free_bytes;
768 btrfs_free_path(path);
772 #define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
773 - sizeof(struct btrfs_item) \
774 - sizeof(struct btrfs_chunk)) \
775 / sizeof(struct btrfs_stripe) + 1)
777 #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
778 - 2 * sizeof(struct btrfs_disk_key) \
779 - 2 * sizeof(struct btrfs_chunk)) \
780 / sizeof(struct btrfs_stripe) + 1)
782 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
783 struct btrfs_root *extent_root, u64 *start,
784 u64 *num_bytes, u64 type)
787 struct btrfs_fs_info *info = extent_root->fs_info;
788 struct btrfs_root *chunk_root = info->chunk_root;
789 struct btrfs_stripe *stripes;
790 struct btrfs_device *device = NULL;
791 struct btrfs_chunk *chunk;
792 struct list_head private_devs;
793 struct list_head *dev_list = &info->fs_devices->devices;
794 struct list_head *cur;
795 struct map_lookup *map;
796 int min_stripe_size = 1 * 1024 * 1024;
797 u64 calc_size = 8 * 1024 * 1024;
799 u64 max_chunk_size = 4 * calc_size;
810 int stripe_len = BTRFS_STRIPE_LEN;
811 struct btrfs_key key;
814 if (list_empty(dev_list)) {
818 if (type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
819 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
820 BTRFS_BLOCK_GROUP_RAID10 |
821 BTRFS_BLOCK_GROUP_DUP)) {
822 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
823 calc_size = 8 * 1024 * 1024;
824 max_chunk_size = calc_size * 2;
825 min_stripe_size = 1 * 1024 * 1024;
826 max_stripes = BTRFS_MAX_DEVS_SYS_CHUNK;
827 } else if (type & BTRFS_BLOCK_GROUP_DATA) {
828 calc_size = 1024 * 1024 * 1024;
829 max_chunk_size = 10 * calc_size;
830 min_stripe_size = 64 * 1024 * 1024;
831 max_stripes = BTRFS_MAX_DEVS(chunk_root);
832 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
833 calc_size = 1024 * 1024 * 1024;
834 max_chunk_size = 4 * calc_size;
835 min_stripe_size = 32 * 1024 * 1024;
836 max_stripes = BTRFS_MAX_DEVS(chunk_root);
839 if (type & BTRFS_BLOCK_GROUP_RAID1) {
840 num_stripes = min_t(u64, 2,
841 btrfs_super_num_devices(info->super_copy));
846 if (type & BTRFS_BLOCK_GROUP_DUP) {
850 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
851 num_stripes = btrfs_super_num_devices(info->super_copy);
852 if (num_stripes > max_stripes)
853 num_stripes = max_stripes;
856 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
857 num_stripes = btrfs_super_num_devices(info->super_copy);
858 if (num_stripes > max_stripes)
859 num_stripes = max_stripes;
862 num_stripes &= ~(u32)1;
866 if (type & (BTRFS_BLOCK_GROUP_RAID5)) {
867 num_stripes = btrfs_super_num_devices(info->super_copy);
868 if (num_stripes > max_stripes)
869 num_stripes = max_stripes;
873 stripe_len = find_raid56_stripe_len(num_stripes - 1,
874 btrfs_super_stripesize(info->super_copy));
876 if (type & (BTRFS_BLOCK_GROUP_RAID6)) {
877 num_stripes = btrfs_super_num_devices(info->super_copy);
878 if (num_stripes > max_stripes)
879 num_stripes = max_stripes;
883 stripe_len = find_raid56_stripe_len(num_stripes - 2,
884 btrfs_super_stripesize(info->super_copy));
887 /* we don't want a chunk larger than 10% of the FS */
888 percent_max = div_factor(btrfs_super_total_bytes(info->super_copy), 1);
889 max_chunk_size = min(percent_max, max_chunk_size);
892 if (chunk_bytes_by_type(type, calc_size, num_stripes, sub_stripes) >
894 calc_size = max_chunk_size;
895 calc_size /= num_stripes;
896 calc_size /= stripe_len;
897 calc_size *= stripe_len;
899 /* we don't want tiny stripes */
900 calc_size = max_t(u64, calc_size, min_stripe_size);
902 calc_size /= stripe_len;
903 calc_size *= stripe_len;
904 INIT_LIST_HEAD(&private_devs);
905 cur = dev_list->next;
908 if (type & BTRFS_BLOCK_GROUP_DUP)
909 min_free = calc_size * 2;
911 min_free = calc_size;
913 /* build a private list of devices we will allocate from */
914 while(index < num_stripes) {
915 device = list_entry(cur, struct btrfs_device, dev_list);
916 ret = btrfs_device_avail_bytes(trans, device, &avail);
920 if (avail >= min_free) {
921 list_move_tail(&device->dev_list, &private_devs);
923 if (type & BTRFS_BLOCK_GROUP_DUP)
925 } else if (avail > max_avail)
930 if (index < num_stripes) {
931 list_splice(&private_devs, dev_list);
932 if (index >= min_stripes) {
934 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
935 num_stripes /= sub_stripes;
936 num_stripes *= sub_stripes;
941 if (!looped && max_avail > 0) {
943 calc_size = max_avail;
948 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
952 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
953 key.type = BTRFS_CHUNK_ITEM_KEY;
956 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
960 map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
966 stripes = &chunk->stripe;
967 *num_bytes = chunk_bytes_by_type(type, calc_size,
968 num_stripes, sub_stripes);
970 while(index < num_stripes) {
971 struct btrfs_stripe *stripe;
972 BUG_ON(list_empty(&private_devs));
973 cur = private_devs.next;
974 device = list_entry(cur, struct btrfs_device, dev_list);
976 /* loop over this device again if we're doing a dup group */
977 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
978 (index == num_stripes - 1))
979 list_move_tail(&device->dev_list, dev_list);
981 ret = btrfs_alloc_dev_extent(trans, device,
982 info->chunk_root->root_key.objectid,
983 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
984 calc_size, &dev_offset, 0);
987 device->bytes_used += calc_size;
988 ret = btrfs_update_device(trans, device);
991 map->stripes[index].dev = device;
992 map->stripes[index].physical = dev_offset;
993 stripe = stripes + index;
994 btrfs_set_stack_stripe_devid(stripe, device->devid);
995 btrfs_set_stack_stripe_offset(stripe, dev_offset);
996 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
999 BUG_ON(!list_empty(&private_devs));
1001 /* key was set above */
1002 btrfs_set_stack_chunk_length(chunk, *num_bytes);
1003 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
1004 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
1005 btrfs_set_stack_chunk_type(chunk, type);
1006 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
1007 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
1008 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
1009 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
1010 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
1011 map->sector_size = extent_root->sectorsize;
1012 map->stripe_len = stripe_len;
1013 map->io_align = stripe_len;
1014 map->io_width = stripe_len;
1016 map->num_stripes = num_stripes;
1017 map->sub_stripes = sub_stripes;
1019 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
1020 btrfs_chunk_item_size(num_stripes));
1022 *start = key.offset;;
1024 map->ce.start = key.offset;
1025 map->ce.size = *num_bytes;
1027 ret = insert_cache_extent(&info->mapping_tree.cache_tree, &map->ce);
1030 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
1031 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
1032 chunk, btrfs_chunk_item_size(num_stripes));
1041 * Alloc a DATA chunk with SINGLE profile.
1043 * If 'convert' is set, it will alloc a chunk with 1:1 mapping
1044 * (btrfs logical bytenr == on-disk bytenr)
1045 * For that case, caller must make sure the chunk and dev_extent are not
1048 int btrfs_alloc_data_chunk(struct btrfs_trans_handle *trans,
1049 struct btrfs_root *extent_root, u64 *start,
1050 u64 num_bytes, u64 type, int convert)
1053 struct btrfs_fs_info *info = extent_root->fs_info;
1054 struct btrfs_root *chunk_root = info->chunk_root;
1055 struct btrfs_stripe *stripes;
1056 struct btrfs_device *device = NULL;
1057 struct btrfs_chunk *chunk;
1058 struct list_head *dev_list = &info->fs_devices->devices;
1059 struct list_head *cur;
1060 struct map_lookup *map;
1061 u64 calc_size = 8 * 1024 * 1024;
1062 int num_stripes = 1;
1063 int sub_stripes = 0;
1066 int stripe_len = BTRFS_STRIPE_LEN;
1067 struct btrfs_key key;
1069 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1070 key.type = BTRFS_CHUNK_ITEM_KEY;
1072 BUG_ON(*start != round_down(*start, extent_root->sectorsize));
1073 key.offset = *start;
1074 dev_offset = *start;
1076 ret = find_next_chunk(chunk_root,
1077 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1083 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
1087 map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
1093 stripes = &chunk->stripe;
1094 calc_size = num_bytes;
1097 cur = dev_list->next;
1098 device = list_entry(cur, struct btrfs_device, dev_list);
1100 while (index < num_stripes) {
1101 struct btrfs_stripe *stripe;
1103 ret = btrfs_alloc_dev_extent(trans, device,
1104 info->chunk_root->root_key.objectid,
1105 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
1106 calc_size, &dev_offset, convert);
1109 device->bytes_used += calc_size;
1110 ret = btrfs_update_device(trans, device);
1113 map->stripes[index].dev = device;
1114 map->stripes[index].physical = dev_offset;
1115 stripe = stripes + index;
1116 btrfs_set_stack_stripe_devid(stripe, device->devid);
1117 btrfs_set_stack_stripe_offset(stripe, dev_offset);
1118 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
1122 /* key was set above */
1123 btrfs_set_stack_chunk_length(chunk, num_bytes);
1124 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
1125 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
1126 btrfs_set_stack_chunk_type(chunk, type);
1127 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
1128 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
1129 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
1130 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
1131 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
1132 map->sector_size = extent_root->sectorsize;
1133 map->stripe_len = stripe_len;
1134 map->io_align = stripe_len;
1135 map->io_width = stripe_len;
1137 map->num_stripes = num_stripes;
1138 map->sub_stripes = sub_stripes;
1140 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
1141 btrfs_chunk_item_size(num_stripes));
1144 *start = key.offset;
1146 map->ce.start = key.offset;
1147 map->ce.size = num_bytes;
1149 ret = insert_cache_extent(&info->mapping_tree.cache_tree, &map->ce);
1156 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
1158 struct cache_extent *ce;
1159 struct map_lookup *map;
1162 ce = search_cache_extent(&map_tree->cache_tree, logical);
1164 fprintf(stderr, "No mapping for %llu-%llu\n",
1165 (unsigned long long)logical,
1166 (unsigned long long)logical+len);
1169 if (ce->start > logical || ce->start + ce->size < logical) {
1170 fprintf(stderr, "Invalid mapping for %llu-%llu, got "
1171 "%llu-%llu\n", (unsigned long long)logical,
1172 (unsigned long long)logical+len,
1173 (unsigned long long)ce->start,
1174 (unsigned long long)ce->start + ce->size);
1177 map = container_of(ce, struct map_lookup, ce);
1179 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
1180 ret = map->num_stripes;
1181 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1182 ret = map->sub_stripes;
1183 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
1185 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
1192 int btrfs_next_bg(struct btrfs_mapping_tree *map_tree, u64 *logical,
1193 u64 *size, u64 type)
1195 struct cache_extent *ce;
1196 struct map_lookup *map;
1199 ce = search_cache_extent(&map_tree->cache_tree, cur);
1203 * only jump to next bg if our cur is not 0
1204 * As the initial logical for btrfs_next_bg() is 0, and
1205 * if we jump to next bg, we skipped a valid bg.
1208 ce = next_cache_extent(ce);
1214 map = container_of(ce, struct map_lookup, ce);
1215 if (map->type & type) {
1216 *logical = ce->start;
1225 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
1226 u64 chunk_start, u64 physical, u64 devid,
1227 u64 **logical, int *naddrs, int *stripe_len)
1229 struct cache_extent *ce;
1230 struct map_lookup *map;
1238 ce = search_cache_extent(&map_tree->cache_tree, chunk_start);
1240 map = container_of(ce, struct map_lookup, ce);
1243 rmap_len = map->stripe_len;
1244 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1245 length = ce->size / (map->num_stripes / map->sub_stripes);
1246 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
1247 length = ce->size / map->num_stripes;
1248 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
1249 BTRFS_BLOCK_GROUP_RAID6)) {
1250 length = ce->size / nr_data_stripes(map);
1251 rmap_len = map->stripe_len * nr_data_stripes(map);
1254 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
1256 for (i = 0; i < map->num_stripes; i++) {
1257 if (devid && map->stripes[i].dev->devid != devid)
1259 if (map->stripes[i].physical > physical ||
1260 map->stripes[i].physical + length <= physical)
1263 stripe_nr = (physical - map->stripes[i].physical) /
1266 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1267 stripe_nr = (stripe_nr * map->num_stripes + i) /
1269 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
1270 stripe_nr = stripe_nr * map->num_stripes + i;
1271 } /* else if RAID[56], multiply by nr_data_stripes().
1272 * Alternatively, just use rmap_len below instead of
1273 * map->stripe_len */
1275 bytenr = ce->start + stripe_nr * rmap_len;
1276 for (j = 0; j < nr; j++) {
1277 if (buf[j] == bytenr)
1286 *stripe_len = rmap_len;
1291 static inline int parity_smaller(u64 a, u64 b)
1296 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
1297 static void sort_parity_stripes(struct btrfs_multi_bio *bbio, u64 *raid_map)
1299 struct btrfs_bio_stripe s;
1306 for (i = 0; i < bbio->num_stripes - 1; i++) {
1307 if (parity_smaller(raid_map[i], raid_map[i+1])) {
1308 s = bbio->stripes[i];
1310 bbio->stripes[i] = bbio->stripes[i+1];
1311 raid_map[i] = raid_map[i+1];
1312 bbio->stripes[i+1] = s;
1320 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1321 u64 logical, u64 *length,
1322 struct btrfs_multi_bio **multi_ret, int mirror_num,
1325 return __btrfs_map_block(map_tree, rw, logical, length, NULL,
1326 multi_ret, mirror_num, raid_map_ret);
1329 int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1330 u64 logical, u64 *length, u64 *type,
1331 struct btrfs_multi_bio **multi_ret, int mirror_num,
1334 struct cache_extent *ce;
1335 struct map_lookup *map;
1339 u64 *raid_map = NULL;
1340 int stripes_allocated = 8;
1341 int stripes_required = 1;
1344 struct btrfs_multi_bio *multi = NULL;
1346 if (multi_ret && rw == READ) {
1347 stripes_allocated = 1;
1350 ce = search_cache_extent(&map_tree->cache_tree, logical);
1356 if (ce->start > logical) {
1358 *length = ce->start - logical;
1363 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
1368 map = container_of(ce, struct map_lookup, ce);
1369 offset = logical - ce->start;
1372 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
1373 BTRFS_BLOCK_GROUP_DUP)) {
1374 stripes_required = map->num_stripes;
1375 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1376 stripes_required = map->sub_stripes;
1379 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)
1380 && multi_ret && ((rw & WRITE) || mirror_num > 1) && raid_map_ret) {
1381 /* RAID[56] write or recovery. Return all stripes */
1382 stripes_required = map->num_stripes;
1384 /* Only allocate the map if we've already got a large enough multi_ret */
1385 if (stripes_allocated >= stripes_required) {
1386 raid_map = kmalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
1394 /* if our multi bio struct is too small, back off and try again */
1395 if (multi_ret && stripes_allocated < stripes_required) {
1396 stripes_allocated = stripes_required;
1403 * stripe_nr counts the total number of stripes we have to stride
1404 * to get to this block
1406 stripe_nr = stripe_nr / map->stripe_len;
1408 stripe_offset = stripe_nr * map->stripe_len;
1409 BUG_ON(offset < stripe_offset);
1411 /* stripe_offset is the offset of this block in its stripe*/
1412 stripe_offset = offset - stripe_offset;
1414 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
1415 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
1416 BTRFS_BLOCK_GROUP_RAID10 |
1417 BTRFS_BLOCK_GROUP_DUP)) {
1418 /* we limit the length of each bio to what fits in a stripe */
1419 *length = min_t(u64, ce->size - offset,
1420 map->stripe_len - stripe_offset);
1422 *length = ce->size - offset;
1428 multi->num_stripes = 1;
1430 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
1432 multi->num_stripes = map->num_stripes;
1433 else if (mirror_num)
1434 stripe_index = mirror_num - 1;
1436 stripe_index = stripe_nr % map->num_stripes;
1437 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1438 int factor = map->num_stripes / map->sub_stripes;
1440 stripe_index = stripe_nr % factor;
1441 stripe_index *= map->sub_stripes;
1444 multi->num_stripes = map->sub_stripes;
1445 else if (mirror_num)
1446 stripe_index += mirror_num - 1;
1448 stripe_nr = stripe_nr / factor;
1449 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
1451 multi->num_stripes = map->num_stripes;
1452 else if (mirror_num)
1453 stripe_index = mirror_num - 1;
1454 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
1455 BTRFS_BLOCK_GROUP_RAID6)) {
1460 u64 raid56_full_stripe_start;
1461 u64 full_stripe_len = nr_data_stripes(map) * map->stripe_len;
1464 * align the start of our data stripe in the logical
1467 raid56_full_stripe_start = offset / full_stripe_len;
1468 raid56_full_stripe_start *= full_stripe_len;
1470 /* get the data stripe number */
1471 stripe_nr = raid56_full_stripe_start / map->stripe_len;
1472 stripe_nr = stripe_nr / nr_data_stripes(map);
1474 /* Work out the disk rotation on this stripe-set */
1475 rot = stripe_nr % map->num_stripes;
1477 /* Fill in the logical address of each stripe */
1478 tmp = stripe_nr * nr_data_stripes(map);
1480 for (i = 0; i < nr_data_stripes(map); i++)
1481 raid_map[(i+rot) % map->num_stripes] =
1482 ce->start + (tmp + i) * map->stripe_len;
1484 raid_map[(i+rot) % map->num_stripes] = BTRFS_RAID5_P_STRIPE;
1485 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
1486 raid_map[(i+rot+1) % map->num_stripes] = BTRFS_RAID6_Q_STRIPE;
1488 *length = map->stripe_len;
1491 multi->num_stripes = map->num_stripes;
1493 stripe_index = stripe_nr % nr_data_stripes(map);
1494 stripe_nr = stripe_nr / nr_data_stripes(map);
1497 * Mirror #0 or #1 means the original data block.
1498 * Mirror #2 is RAID5 parity block.
1499 * Mirror #3 is RAID6 Q block.
1502 stripe_index = nr_data_stripes(map) + mirror_num - 2;
1504 /* We distribute the parity blocks across stripes */
1505 stripe_index = (stripe_nr + stripe_index) % map->num_stripes;
1509 * after this do_div call, stripe_nr is the number of stripes
1510 * on this device we have to walk to find the data, and
1511 * stripe_index is the number of our device in the stripe array
1513 stripe_index = stripe_nr % map->num_stripes;
1514 stripe_nr = stripe_nr / map->num_stripes;
1516 BUG_ON(stripe_index >= map->num_stripes);
1518 for (i = 0; i < multi->num_stripes; i++) {
1519 multi->stripes[i].physical =
1520 map->stripes[stripe_index].physical + stripe_offset +
1521 stripe_nr * map->stripe_len;
1522 multi->stripes[i].dev = map->stripes[stripe_index].dev;
1531 sort_parity_stripes(multi, raid_map);
1532 *raid_map_ret = raid_map;
1538 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
1541 struct btrfs_device *device;
1542 struct btrfs_fs_devices *cur_devices;
1544 cur_devices = root->fs_info->fs_devices;
1545 while (cur_devices) {
1547 (!memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE) ||
1548 root->fs_info->ignore_fsid_mismatch)) {
1549 device = __find_device(&cur_devices->devices,
1554 cur_devices = cur_devices->seed;
1559 struct btrfs_device *
1560 btrfs_find_device_by_devid(struct btrfs_fs_devices *fs_devices,
1561 u64 devid, int instance)
1563 struct list_head *head = &fs_devices->devices;
1564 struct btrfs_device *dev;
1567 list_for_each_entry(dev, head, dev_list) {
1568 if (dev->devid == devid && num_found++ == instance)
1574 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
1576 struct cache_extent *ce;
1577 struct map_lookup *map;
1578 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1583 * During chunk recovering, we may fail to find block group's
1584 * corresponding chunk, we will rebuild it later
1586 ce = search_cache_extent(&map_tree->cache_tree, chunk_offset);
1587 if (!root->fs_info->is_chunk_recover)
1592 map = container_of(ce, struct map_lookup, ce);
1593 for (i = 0; i < map->num_stripes; i++) {
1594 if (!map->stripes[i].dev->writeable) {
1603 static struct btrfs_device *fill_missing_device(u64 devid)
1605 struct btrfs_device *device;
1607 device = kzalloc(sizeof(*device), GFP_NOFS);
1608 device->devid = devid;
1614 * slot == -1: SYSTEM chunk
1615 * return -EIO on error, otherwise return 0
1617 static int btrfs_check_chunk_valid(struct btrfs_root *root,
1618 struct extent_buffer *leaf,
1619 struct btrfs_chunk *chunk,
1620 int slot, u64 logical)
1628 length = btrfs_chunk_length(leaf, chunk);
1629 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1630 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1631 sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
1632 type = btrfs_chunk_type(leaf, chunk);
1635 * These valid checks may be insufficient to cover every corner cases.
1637 if (!IS_ALIGNED(logical, root->sectorsize)) {
1638 error("invalid chunk logical %llu", logical);
1641 if (btrfs_chunk_sector_size(leaf, chunk) != root->sectorsize) {
1642 error("invalid chunk sectorsize %llu",
1643 (unsigned long long)btrfs_chunk_sector_size(leaf, chunk));
1646 if (!length || !IS_ALIGNED(length, root->sectorsize)) {
1647 error("invalid chunk length %llu", length);
1650 if (stripe_len != BTRFS_STRIPE_LEN) {
1651 error("invalid chunk stripe length: %llu", stripe_len);
1654 /* Check on chunk item type */
1655 if (slot == -1 && (type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
1656 error("invalid chunk type %llu", type);
1659 if (type & ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
1660 BTRFS_BLOCK_GROUP_PROFILE_MASK)) {
1661 error("unrecognized chunk type: %llu",
1662 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
1663 BTRFS_BLOCK_GROUP_PROFILE_MASK) & type);
1667 * Btrfs_chunk contains at least one stripe, and for sys_chunk
1668 * it can't exceed the system chunk array size
1669 * For normal chunk, it should match its chunk item size.
1671 if (num_stripes < 1 ||
1672 (slot == -1 && sizeof(struct btrfs_stripe) * num_stripes >
1673 BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) ||
1674 (slot >= 0 && sizeof(struct btrfs_stripe) * (num_stripes - 1) >
1675 btrfs_item_size_nr(leaf, slot))) {
1676 error("invalid num_stripes: %u", num_stripes);
1680 * Device number check against profile
1682 if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes == 0) ||
1683 (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) ||
1684 (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
1685 (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
1686 (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||
1687 ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
1688 num_stripes != 1)) {
1689 error("Invalid num_stripes:sub_stripes %u:%u for profile %llu",
1690 num_stripes, sub_stripes,
1691 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
1699 * Slot is used to verify the chunk item is valid
1701 * For sys chunk in superblock, pass -1 to indicate sys chunk.
1703 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
1704 struct extent_buffer *leaf,
1705 struct btrfs_chunk *chunk, int slot)
1707 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1708 struct map_lookup *map;
1709 struct cache_extent *ce;
1713 u8 uuid[BTRFS_UUID_SIZE];
1718 logical = key->offset;
1719 length = btrfs_chunk_length(leaf, chunk);
1720 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1721 /* Validation check */
1722 ret = btrfs_check_chunk_valid(root, leaf, chunk, slot, logical);
1724 error("%s checksums match, but it has an invalid chunk, %s",
1725 (slot == -1) ? "Superblock" : "Metadata",
1726 (slot == -1) ? "try btrfsck --repair -s <superblock> ie, 0,1,2" : "");
1730 ce = search_cache_extent(&map_tree->cache_tree, logical);
1732 /* already mapped? */
1733 if (ce && ce->start <= logical && ce->start + ce->size > logical) {
1737 map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
1741 map->ce.start = logical;
1742 map->ce.size = length;
1743 map->num_stripes = num_stripes;
1744 map->io_width = btrfs_chunk_io_width(leaf, chunk);
1745 map->io_align = btrfs_chunk_io_align(leaf, chunk);
1746 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1747 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1748 map->type = btrfs_chunk_type(leaf, chunk);
1749 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
1751 for (i = 0; i < num_stripes; i++) {
1752 map->stripes[i].physical =
1753 btrfs_stripe_offset_nr(leaf, chunk, i);
1754 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
1755 read_extent_buffer(leaf, uuid, (unsigned long)
1756 btrfs_stripe_dev_uuid_nr(chunk, i),
1758 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
1760 if (!map->stripes[i].dev) {
1761 map->stripes[i].dev = fill_missing_device(devid);
1762 printf("warning, device %llu is missing\n",
1763 (unsigned long long)devid);
1767 ret = insert_cache_extent(&map_tree->cache_tree, &map->ce);
1773 static int fill_device_from_item(struct extent_buffer *leaf,
1774 struct btrfs_dev_item *dev_item,
1775 struct btrfs_device *device)
1779 device->devid = btrfs_device_id(leaf, dev_item);
1780 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1781 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1782 device->type = btrfs_device_type(leaf, dev_item);
1783 device->io_align = btrfs_device_io_align(leaf, dev_item);
1784 device->io_width = btrfs_device_io_width(leaf, dev_item);
1785 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
1787 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1788 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1793 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
1795 struct btrfs_fs_devices *fs_devices;
1798 fs_devices = root->fs_info->fs_devices->seed;
1799 while (fs_devices) {
1800 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
1804 fs_devices = fs_devices->seed;
1807 fs_devices = find_fsid(fsid);
1809 /* missing all seed devices */
1810 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1815 INIT_LIST_HEAD(&fs_devices->devices);
1816 list_add(&fs_devices->list, &fs_uuids);
1817 memcpy(fs_devices->fsid, fsid, BTRFS_FSID_SIZE);
1820 ret = btrfs_open_devices(fs_devices, O_RDONLY);
1824 fs_devices->seed = root->fs_info->fs_devices->seed;
1825 root->fs_info->fs_devices->seed = fs_devices;
1830 static int read_one_dev(struct btrfs_root *root,
1831 struct extent_buffer *leaf,
1832 struct btrfs_dev_item *dev_item)
1834 struct btrfs_device *device;
1837 u8 fs_uuid[BTRFS_UUID_SIZE];
1838 u8 dev_uuid[BTRFS_UUID_SIZE];
1840 devid = btrfs_device_id(leaf, dev_item);
1841 read_extent_buffer(leaf, dev_uuid,
1842 (unsigned long)btrfs_device_uuid(dev_item),
1844 read_extent_buffer(leaf, fs_uuid,
1845 (unsigned long)btrfs_device_fsid(dev_item),
1848 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
1849 ret = open_seed_devices(root, fs_uuid);
1854 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1856 device = kzalloc(sizeof(*device), GFP_NOFS);
1860 list_add(&device->dev_list,
1861 &root->fs_info->fs_devices->devices);
1864 fill_device_from_item(leaf, dev_item, device);
1865 device->dev_root = root->fs_info->dev_root;
1869 int btrfs_read_sys_array(struct btrfs_root *root)
1871 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
1872 struct extent_buffer *sb;
1873 struct btrfs_disk_key *disk_key;
1874 struct btrfs_chunk *chunk;
1876 unsigned long sb_array_offset;
1882 struct btrfs_key key;
1884 sb = btrfs_find_create_tree_block(root->fs_info,
1885 BTRFS_SUPER_INFO_OFFSET,
1886 BTRFS_SUPER_INFO_SIZE);
1889 btrfs_set_buffer_uptodate(sb);
1890 write_extent_buffer(sb, super_copy, 0, sizeof(*super_copy));
1891 array_size = btrfs_super_sys_array_size(super_copy);
1893 array_ptr = super_copy->sys_chunk_array;
1894 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
1897 while (cur_offset < array_size) {
1898 disk_key = (struct btrfs_disk_key *)array_ptr;
1899 len = sizeof(*disk_key);
1900 if (cur_offset + len > array_size)
1901 goto out_short_read;
1903 btrfs_disk_key_to_cpu(&key, disk_key);
1906 sb_array_offset += len;
1909 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1910 chunk = (struct btrfs_chunk *)sb_array_offset;
1912 * At least one btrfs_chunk with one stripe must be
1913 * present, exact stripe count check comes afterwards
1915 len = btrfs_chunk_item_size(1);
1916 if (cur_offset + len > array_size)
1917 goto out_short_read;
1919 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
1922 "ERROR: invalid number of stripes %u in sys_array at offset %u\n",
1923 num_stripes, cur_offset);
1928 len = btrfs_chunk_item_size(num_stripes);
1929 if (cur_offset + len > array_size)
1930 goto out_short_read;
1932 ret = read_one_chunk(root, &key, sb, chunk, -1);
1937 "ERROR: unexpected item type %u in sys_array at offset %u\n",
1938 (u32)key.type, cur_offset);
1943 sb_array_offset += len;
1946 free_extent_buffer(sb);
1950 printk("ERROR: sys_array too short to read %u bytes at offset %u\n",
1952 free_extent_buffer(sb);
1956 int btrfs_read_chunk_tree(struct btrfs_root *root)
1958 struct btrfs_path *path;
1959 struct extent_buffer *leaf;
1960 struct btrfs_key key;
1961 struct btrfs_key found_key;
1965 root = root->fs_info->chunk_root;
1967 path = btrfs_alloc_path();
1972 * Read all device items, and then all the chunk items. All
1973 * device items are found before any chunk item (their object id
1974 * is smaller than the lowest possible object id for a chunk
1975 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
1977 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1980 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1984 leaf = path->nodes[0];
1985 slot = path->slots[0];
1986 if (slot >= btrfs_header_nritems(leaf)) {
1987 ret = btrfs_next_leaf(root, path);
1994 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1995 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
1996 struct btrfs_dev_item *dev_item;
1997 dev_item = btrfs_item_ptr(leaf, slot,
1998 struct btrfs_dev_item);
1999 ret = read_one_dev(root, leaf, dev_item);
2001 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
2002 struct btrfs_chunk *chunk;
2003 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2004 ret = read_one_chunk(root, &found_key, leaf, chunk,
2013 btrfs_free_path(path);
2017 struct list_head *btrfs_scanned_uuids(void)
2022 static int rmw_eb(struct btrfs_fs_info *info,
2023 struct extent_buffer *eb, struct extent_buffer *orig_eb)
2026 unsigned long orig_off = 0;
2027 unsigned long dest_off = 0;
2028 unsigned long copy_len = eb->len;
2030 ret = read_whole_eb(info, eb, 0);
2034 if (eb->start + eb->len <= orig_eb->start ||
2035 eb->start >= orig_eb->start + orig_eb->len)
2038 * | ----- orig_eb ------- |
2039 * | ----- stripe ------- |
2040 * | ----- orig_eb ------- |
2041 * | ----- orig_eb ------- |
2043 if (eb->start > orig_eb->start)
2044 orig_off = eb->start - orig_eb->start;
2045 if (orig_eb->start > eb->start)
2046 dest_off = orig_eb->start - eb->start;
2048 if (copy_len > orig_eb->len - orig_off)
2049 copy_len = orig_eb->len - orig_off;
2050 if (copy_len > eb->len - dest_off)
2051 copy_len = eb->len - dest_off;
2053 memcpy(eb->data + dest_off, orig_eb->data + orig_off, copy_len);
2057 static void split_eb_for_raid56(struct btrfs_fs_info *info,
2058 struct extent_buffer *orig_eb,
2059 struct extent_buffer **ebs,
2060 u64 stripe_len, u64 *raid_map,
2063 struct extent_buffer *eb;
2064 u64 start = orig_eb->start;
2069 for (i = 0; i < num_stripes; i++) {
2070 if (raid_map[i] >= BTRFS_RAID5_P_STRIPE)
2073 eb = calloc(1, sizeof(struct extent_buffer) + stripe_len);
2077 eb->start = raid_map[i];
2078 eb->len = stripe_len;
2082 eb->dev_bytenr = (u64)-1;
2084 this_eb_start = raid_map[i];
2086 if (start > this_eb_start ||
2087 start + orig_eb->len < this_eb_start + stripe_len) {
2088 ret = rmw_eb(info, eb, orig_eb);
2091 memcpy(eb->data, orig_eb->data + eb->start - start, stripe_len);
2097 int write_raid56_with_parity(struct btrfs_fs_info *info,
2098 struct extent_buffer *eb,
2099 struct btrfs_multi_bio *multi,
2100 u64 stripe_len, u64 *raid_map)
2102 struct extent_buffer **ebs, *p_eb = NULL, *q_eb = NULL;
2106 int alloc_size = eb->len;
2108 ebs = kmalloc(sizeof(*ebs) * multi->num_stripes, GFP_NOFS);
2111 if (stripe_len > alloc_size)
2112 alloc_size = stripe_len;
2114 split_eb_for_raid56(info, eb, ebs, stripe_len, raid_map,
2115 multi->num_stripes);
2117 for (i = 0; i < multi->num_stripes; i++) {
2118 struct extent_buffer *new_eb;
2119 if (raid_map[i] < BTRFS_RAID5_P_STRIPE) {
2120 ebs[i]->dev_bytenr = multi->stripes[i].physical;
2121 ebs[i]->fd = multi->stripes[i].dev->fd;
2122 multi->stripes[i].dev->total_ios++;
2123 BUG_ON(ebs[i]->start != raid_map[i]);
2126 new_eb = kmalloc(sizeof(*eb) + alloc_size, GFP_NOFS);
2128 new_eb->dev_bytenr = multi->stripes[i].physical;
2129 new_eb->fd = multi->stripes[i].dev->fd;
2130 multi->stripes[i].dev->total_ios++;
2131 new_eb->len = stripe_len;
2133 if (raid_map[i] == BTRFS_RAID5_P_STRIPE)
2135 else if (raid_map[i] == BTRFS_RAID6_Q_STRIPE)
2141 pointers = kmalloc(sizeof(*pointers) * multi->num_stripes,
2145 ebs[multi->num_stripes - 2] = p_eb;
2146 ebs[multi->num_stripes - 1] = q_eb;
2148 for (i = 0; i < multi->num_stripes; i++)
2149 pointers[i] = ebs[i]->data;
2151 raid6_gen_syndrome(multi->num_stripes, stripe_len, pointers);
2154 ebs[multi->num_stripes - 1] = p_eb;
2155 memcpy(p_eb->data, ebs[0]->data, stripe_len);
2156 for (j = 1; j < multi->num_stripes - 1; j++) {
2157 for (i = 0; i < stripe_len; i += sizeof(unsigned long)) {
2158 *(unsigned long *)(p_eb->data + i) ^=
2159 *(unsigned long *)(ebs[j]->data + i);
2164 for (i = 0; i < multi->num_stripes; i++) {
2165 ret = write_extent_to_disk(ebs[i]);