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"
31 #include "kernel-lib/raid56.h"
34 struct btrfs_device *dev;
38 static inline int nr_parity_stripes(struct map_lookup *map)
40 if (map->type & BTRFS_BLOCK_GROUP_RAID5)
42 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
48 static inline int nr_data_stripes(struct map_lookup *map)
50 return map->num_stripes - nr_parity_stripes(map);
53 #define is_parity_stripe(x) ( ((x) == BTRFS_RAID5_P_STRIPE) || ((x) == BTRFS_RAID6_Q_STRIPE) )
55 static LIST_HEAD(fs_uuids);
57 static struct btrfs_device *__find_device(struct list_head *head, u64 devid,
60 struct btrfs_device *dev;
61 struct list_head *cur;
63 list_for_each(cur, head) {
64 dev = list_entry(cur, struct btrfs_device, dev_list);
65 if (dev->devid == devid &&
66 !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE)) {
73 static struct btrfs_fs_devices *find_fsid(u8 *fsid)
75 struct list_head *cur;
76 struct btrfs_fs_devices *fs_devices;
78 list_for_each(cur, &fs_uuids) {
79 fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
80 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
86 static int device_list_add(const char *path,
87 struct btrfs_super_block *disk_super,
88 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
90 struct btrfs_device *device;
91 struct btrfs_fs_devices *fs_devices;
92 u64 found_transid = btrfs_super_generation(disk_super);
94 fs_devices = find_fsid(disk_super->fsid);
96 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
99 INIT_LIST_HEAD(&fs_devices->devices);
100 list_add(&fs_devices->list, &fs_uuids);
101 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
102 fs_devices->latest_devid = devid;
103 fs_devices->latest_trans = found_transid;
104 fs_devices->lowest_devid = (u64)-1;
107 device = __find_device(&fs_devices->devices, devid,
108 disk_super->dev_item.uuid);
111 device = kzalloc(sizeof(*device), GFP_NOFS);
113 /* we can safely leave the fs_devices entry around */
117 device->devid = devid;
118 device->generation = found_transid;
119 memcpy(device->uuid, disk_super->dev_item.uuid,
121 device->name = kstrdup(path, GFP_NOFS);
126 device->label = kstrdup(disk_super->label, GFP_NOFS);
127 if (!device->label) {
132 device->total_devs = btrfs_super_num_devices(disk_super);
133 device->super_bytes_used = btrfs_super_bytes_used(disk_super);
134 device->total_bytes =
135 btrfs_stack_device_total_bytes(&disk_super->dev_item);
137 btrfs_stack_device_bytes_used(&disk_super->dev_item);
138 list_add(&device->dev_list, &fs_devices->devices);
139 device->fs_devices = fs_devices;
140 } else if (!device->name || strcmp(device->name, path)) {
141 char *name = strdup(path);
149 if (found_transid > fs_devices->latest_trans) {
150 fs_devices->latest_devid = devid;
151 fs_devices->latest_trans = found_transid;
153 if (fs_devices->lowest_devid > devid) {
154 fs_devices->lowest_devid = devid;
156 *fs_devices_ret = fs_devices;
160 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
162 struct btrfs_fs_devices *seed_devices;
163 struct btrfs_device *device;
169 while (!list_empty(&fs_devices->devices)) {
170 device = list_entry(fs_devices->devices.next,
171 struct btrfs_device, dev_list);
172 if (device->fd != -1) {
173 if (fsync(device->fd) == -1) {
174 warning("fsync on device %llu failed: %s",
175 device->devid, strerror(errno));
178 if (posix_fadvise(device->fd, 0, 0, POSIX_FADV_DONTNEED))
179 fprintf(stderr, "Warning, could not drop caches\n");
183 device->writeable = 0;
184 list_del(&device->dev_list);
185 /* free the memory */
191 seed_devices = fs_devices->seed;
192 fs_devices->seed = NULL;
194 struct btrfs_fs_devices *orig;
197 fs_devices = seed_devices;
198 list_del(&orig->list);
202 list_del(&fs_devices->list);
209 void btrfs_close_all_devices(void)
211 struct btrfs_fs_devices *fs_devices;
213 while (!list_empty(&fs_uuids)) {
214 fs_devices = list_entry(fs_uuids.next, struct btrfs_fs_devices,
216 btrfs_close_devices(fs_devices);
220 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, int flags)
223 struct list_head *head = &fs_devices->devices;
224 struct list_head *cur;
225 struct btrfs_device *device;
228 list_for_each(cur, head) {
229 device = list_entry(cur, struct btrfs_device, dev_list);
231 printk("no name for device %llu, skip it now\n", device->devid);
235 fd = open(device->name, flags);
238 error("cannot open device '%s': %s", device->name,
243 if (posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED))
244 fprintf(stderr, "Warning, could not drop caches\n");
246 if (device->devid == fs_devices->latest_devid)
247 fs_devices->latest_bdev = fd;
248 if (device->devid == fs_devices->lowest_devid)
249 fs_devices->lowest_bdev = fd;
252 device->writeable = 1;
256 btrfs_close_devices(fs_devices);
260 int btrfs_scan_one_device(int fd, const char *path,
261 struct btrfs_fs_devices **fs_devices_ret,
262 u64 *total_devs, u64 super_offset, unsigned sbflags)
264 struct btrfs_super_block *disk_super;
265 char buf[BTRFS_SUPER_INFO_SIZE];
269 disk_super = (struct btrfs_super_block *)buf;
270 ret = btrfs_read_dev_super(fd, disk_super, super_offset, sbflags);
273 devid = btrfs_stack_device_id(&disk_super->dev_item);
274 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)
277 *total_devs = btrfs_super_num_devices(disk_super);
279 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
285 * find_free_dev_extent_start - find free space in the specified device
286 * @device: the device which we search the free space in
287 * @num_bytes: the size of the free space that we need
288 * @search_start: the position from which to begin the search
289 * @start: store the start of the free space.
290 * @len: the size of the free space. that we find, or the size
291 * of the max free space if we don't find suitable free space
293 * this uses a pretty simple search, the expectation is that it is
294 * called very infrequently and that a given device has a small number
297 * @start is used to store the start of the free space if we find. But if we
298 * don't find suitable free space, it will be used to store the start position
299 * of the max free space.
301 * @len is used to store the size of the free space that we find.
302 * But if we don't find suitable free space, it is used to store the size of
303 * the max free space.
305 static int find_free_dev_extent_start(struct btrfs_trans_handle *trans,
306 struct btrfs_device *device, u64 num_bytes,
307 u64 search_start, u64 *start, u64 *len)
309 struct btrfs_key key;
310 struct btrfs_root *root = device->dev_root;
311 struct btrfs_dev_extent *dev_extent;
312 struct btrfs_path *path;
317 u64 search_end = device->total_bytes;
320 struct extent_buffer *l;
321 u64 min_search_start;
324 * We don't want to overwrite the superblock on the drive nor any area
325 * used by the boot loader (grub for example), so we make sure to start
326 * at an offset of at least 1MB.
328 min_search_start = max(root->fs_info->alloc_start, (u64)SZ_1M);
329 search_start = max(search_start, min_search_start);
331 path = btrfs_alloc_path();
335 max_hole_start = search_start;
338 if (search_start >= search_end) {
345 key.objectid = device->devid;
346 key.offset = search_start;
347 key.type = BTRFS_DEV_EXTENT_KEY;
349 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
353 ret = btrfs_previous_item(root, path, key.objectid, key.type);
360 slot = path->slots[0];
361 if (slot >= btrfs_header_nritems(l)) {
362 ret = btrfs_next_leaf(root, path);
370 btrfs_item_key_to_cpu(l, &key, slot);
372 if (key.objectid < device->devid)
375 if (key.objectid > device->devid)
378 if (key.type != BTRFS_DEV_EXTENT_KEY)
381 if (key.offset > search_start) {
382 hole_size = key.offset - search_start;
385 * Have to check before we set max_hole_start, otherwise
386 * we could end up sending back this offset anyway.
388 if (hole_size > max_hole_size) {
389 max_hole_start = search_start;
390 max_hole_size = hole_size;
394 * If this free space is greater than which we need,
395 * it must be the max free space that we have found
396 * until now, so max_hole_start must point to the start
397 * of this free space and the length of this free space
398 * is stored in max_hole_size. Thus, we return
399 * max_hole_start and max_hole_size and go back to the
402 if (hole_size >= num_bytes) {
408 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
409 extent_end = key.offset + btrfs_dev_extent_length(l,
411 if (extent_end > search_start)
412 search_start = extent_end;
419 * At this point, search_start should be the end of
420 * allocated dev extents, and when shrinking the device,
421 * search_end may be smaller than search_start.
423 if (search_end > search_start) {
424 hole_size = search_end - search_start;
426 if (hole_size > max_hole_size) {
427 max_hole_start = search_start;
428 max_hole_size = hole_size;
433 if (max_hole_size < num_bytes)
439 btrfs_free_path(path);
440 *start = max_hole_start;
442 *len = max_hole_size;
446 int find_free_dev_extent(struct btrfs_trans_handle *trans,
447 struct btrfs_device *device, u64 num_bytes,
450 /* FIXME use last free of some kind */
451 return find_free_dev_extent_start(trans, device,
452 num_bytes, 0, start, NULL);
455 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
456 struct btrfs_device *device,
457 u64 chunk_tree, u64 chunk_objectid,
459 u64 num_bytes, u64 *start, int convert)
462 struct btrfs_path *path;
463 struct btrfs_root *root = device->dev_root;
464 struct btrfs_dev_extent *extent;
465 struct extent_buffer *leaf;
466 struct btrfs_key key;
468 path = btrfs_alloc_path();
473 * For convert case, just skip search free dev_extent, as caller
474 * is responsible to make sure it's free.
477 ret = find_free_dev_extent(trans, device, num_bytes,
483 key.objectid = device->devid;
485 key.type = BTRFS_DEV_EXTENT_KEY;
486 ret = btrfs_insert_empty_item(trans, root, path, &key,
490 leaf = path->nodes[0];
491 extent = btrfs_item_ptr(leaf, path->slots[0],
492 struct btrfs_dev_extent);
493 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
494 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
495 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
497 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
498 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
501 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
502 btrfs_mark_buffer_dirty(leaf);
504 btrfs_free_path(path);
508 static int find_next_chunk(struct btrfs_fs_info *fs_info, u64 *offset)
510 struct btrfs_root *root = fs_info->chunk_root;
511 struct btrfs_path *path;
513 struct btrfs_key key;
514 struct btrfs_chunk *chunk;
515 struct btrfs_key found_key;
517 path = btrfs_alloc_path();
521 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
522 key.offset = (u64)-1;
523 key.type = BTRFS_CHUNK_ITEM_KEY;
525 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
531 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
535 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
537 if (found_key.objectid != BTRFS_FIRST_CHUNK_TREE_OBJECTID)
540 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
542 *offset = found_key.offset +
543 btrfs_chunk_length(path->nodes[0], chunk);
548 btrfs_free_path(path);
552 static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
556 struct btrfs_key key;
557 struct btrfs_key found_key;
559 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
560 key.type = BTRFS_DEV_ITEM_KEY;
561 key.offset = (u64)-1;
563 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
569 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
574 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
576 *objectid = found_key.offset + 1;
580 btrfs_release_path(path);
585 * the device information is stored in the chunk root
586 * the btrfs_device struct should be fully filled in
588 int btrfs_add_device(struct btrfs_trans_handle *trans,
589 struct btrfs_fs_info *fs_info,
590 struct btrfs_device *device)
593 struct btrfs_path *path;
594 struct btrfs_dev_item *dev_item;
595 struct extent_buffer *leaf;
596 struct btrfs_key key;
597 struct btrfs_root *root = fs_info->chunk_root;
601 path = btrfs_alloc_path();
605 ret = find_next_devid(root, path, &free_devid);
609 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
610 key.type = BTRFS_DEV_ITEM_KEY;
611 key.offset = free_devid;
613 ret = btrfs_insert_empty_item(trans, root, path, &key,
618 leaf = path->nodes[0];
619 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
621 device->devid = free_devid;
622 btrfs_set_device_id(leaf, dev_item, device->devid);
623 btrfs_set_device_generation(leaf, dev_item, 0);
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_set_device_group(leaf, dev_item, 0);
631 btrfs_set_device_seek_speed(leaf, dev_item, 0);
632 btrfs_set_device_bandwidth(leaf, dev_item, 0);
633 btrfs_set_device_start_offset(leaf, dev_item, 0);
635 ptr = (unsigned long)btrfs_device_uuid(dev_item);
636 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
637 ptr = (unsigned long)btrfs_device_fsid(dev_item);
638 write_extent_buffer(leaf, fs_info->fsid, ptr, BTRFS_UUID_SIZE);
639 btrfs_mark_buffer_dirty(leaf);
643 btrfs_free_path(path);
647 int btrfs_update_device(struct btrfs_trans_handle *trans,
648 struct btrfs_device *device)
651 struct btrfs_path *path;
652 struct btrfs_root *root;
653 struct btrfs_dev_item *dev_item;
654 struct extent_buffer *leaf;
655 struct btrfs_key key;
657 root = device->dev_root->fs_info->chunk_root;
659 path = btrfs_alloc_path();
663 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
664 key.type = BTRFS_DEV_ITEM_KEY;
665 key.offset = device->devid;
667 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
676 leaf = path->nodes[0];
677 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
679 btrfs_set_device_id(leaf, dev_item, device->devid);
680 btrfs_set_device_type(leaf, dev_item, device->type);
681 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
682 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
683 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
684 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
685 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
686 btrfs_mark_buffer_dirty(leaf);
689 btrfs_free_path(path);
693 int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
694 struct btrfs_chunk *chunk, int item_size)
696 struct btrfs_super_block *super_copy = fs_info->super_copy;
697 struct btrfs_disk_key disk_key;
701 array_size = btrfs_super_sys_array_size(super_copy);
702 if (array_size + item_size + sizeof(disk_key)
703 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
706 ptr = super_copy->sys_chunk_array + array_size;
707 btrfs_cpu_key_to_disk(&disk_key, key);
708 memcpy(ptr, &disk_key, sizeof(disk_key));
709 ptr += sizeof(disk_key);
710 memcpy(ptr, chunk, item_size);
711 item_size += sizeof(disk_key);
712 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
716 static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes,
719 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
721 else if (type & BTRFS_BLOCK_GROUP_RAID10)
722 return calc_size * (num_stripes / sub_stripes);
723 else if (type & BTRFS_BLOCK_GROUP_RAID5)
724 return calc_size * (num_stripes - 1);
725 else if (type & BTRFS_BLOCK_GROUP_RAID6)
726 return calc_size * (num_stripes - 2);
728 return calc_size * num_stripes;
732 static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
734 /* TODO, add a way to store the preferred stripe size */
735 return BTRFS_STRIPE_LEN;
739 * btrfs_device_avail_bytes - count bytes available for alloc_chunk
741 * It is not equal to "device->total_bytes - device->bytes_used".
742 * We do not allocate any chunk in 1M at beginning of device, and not
743 * allowed to allocate any chunk before alloc_start if it is specified.
744 * So search holes from max(1M, alloc_start) to device->total_bytes.
746 static int btrfs_device_avail_bytes(struct btrfs_trans_handle *trans,
747 struct btrfs_device *device,
750 struct btrfs_path *path;
751 struct btrfs_root *root = device->dev_root;
752 struct btrfs_key key;
753 struct btrfs_dev_extent *dev_extent = NULL;
754 struct extent_buffer *l;
755 u64 search_start = root->fs_info->alloc_start;
756 u64 search_end = device->total_bytes;
762 search_start = max(BTRFS_BLOCK_RESERVED_1M_FOR_SUPER, search_start);
764 path = btrfs_alloc_path();
768 key.objectid = device->devid;
769 key.offset = root->fs_info->alloc_start;
770 key.type = BTRFS_DEV_EXTENT_KEY;
773 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
776 ret = btrfs_previous_item(root, path, 0, key.type);
782 slot = path->slots[0];
783 if (slot >= btrfs_header_nritems(l)) {
784 ret = btrfs_next_leaf(root, path);
791 btrfs_item_key_to_cpu(l, &key, slot);
793 if (key.objectid < device->devid)
795 if (key.objectid > device->devid)
797 if (key.type != BTRFS_DEV_EXTENT_KEY)
799 if (key.offset > search_end)
801 if (key.offset > search_start)
802 free_bytes += key.offset - search_start;
804 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
805 extent_end = key.offset + btrfs_dev_extent_length(l,
807 if (extent_end > search_start)
808 search_start = extent_end;
809 if (search_start > search_end)
816 if (search_start < search_end)
817 free_bytes += search_end - search_start;
819 *avail_bytes = free_bytes;
822 btrfs_free_path(path);
826 #define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
827 - sizeof(struct btrfs_item) \
828 - sizeof(struct btrfs_chunk)) \
829 / sizeof(struct btrfs_stripe) + 1)
831 #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
832 - 2 * sizeof(struct btrfs_disk_key) \
833 - 2 * sizeof(struct btrfs_chunk)) \
834 / sizeof(struct btrfs_stripe) + 1)
836 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
837 struct btrfs_fs_info *info, u64 *start,
838 u64 *num_bytes, u64 type)
841 struct btrfs_root *extent_root = info->extent_root;
842 struct btrfs_root *chunk_root = info->chunk_root;
843 struct btrfs_stripe *stripes;
844 struct btrfs_device *device = NULL;
845 struct btrfs_chunk *chunk;
846 struct list_head private_devs;
847 struct list_head *dev_list = &info->fs_devices->devices;
848 struct list_head *cur;
849 struct map_lookup *map;
850 int min_stripe_size = SZ_1M;
851 u64 calc_size = SZ_8M;
853 u64 max_chunk_size = 4 * calc_size;
864 int stripe_len = BTRFS_STRIPE_LEN;
865 struct btrfs_key key;
868 if (list_empty(dev_list)) {
872 if (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
873 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
875 max_chunk_size = calc_size * 2;
876 min_stripe_size = SZ_1M;
877 max_stripes = BTRFS_MAX_DEVS_SYS_CHUNK;
878 } else if (type & BTRFS_BLOCK_GROUP_DATA) {
880 max_chunk_size = 10 * calc_size;
881 min_stripe_size = SZ_64M;
882 max_stripes = BTRFS_MAX_DEVS(chunk_root);
883 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
885 max_chunk_size = 4 * calc_size;
886 min_stripe_size = SZ_32M;
887 max_stripes = BTRFS_MAX_DEVS(chunk_root);
890 if (type & BTRFS_BLOCK_GROUP_RAID1) {
891 num_stripes = min_t(u64, 2,
892 btrfs_super_num_devices(info->super_copy));
897 if (type & BTRFS_BLOCK_GROUP_DUP) {
901 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
902 num_stripes = btrfs_super_num_devices(info->super_copy);
903 if (num_stripes > max_stripes)
904 num_stripes = max_stripes;
907 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
908 num_stripes = btrfs_super_num_devices(info->super_copy);
909 if (num_stripes > max_stripes)
910 num_stripes = max_stripes;
913 num_stripes &= ~(u32)1;
917 if (type & (BTRFS_BLOCK_GROUP_RAID5)) {
918 num_stripes = btrfs_super_num_devices(info->super_copy);
919 if (num_stripes > max_stripes)
920 num_stripes = max_stripes;
924 stripe_len = find_raid56_stripe_len(num_stripes - 1,
925 btrfs_super_stripesize(info->super_copy));
927 if (type & (BTRFS_BLOCK_GROUP_RAID6)) {
928 num_stripes = btrfs_super_num_devices(info->super_copy);
929 if (num_stripes > max_stripes)
930 num_stripes = max_stripes;
934 stripe_len = find_raid56_stripe_len(num_stripes - 2,
935 btrfs_super_stripesize(info->super_copy));
938 /* we don't want a chunk larger than 10% of the FS */
939 percent_max = div_factor(btrfs_super_total_bytes(info->super_copy), 1);
940 max_chunk_size = min(percent_max, max_chunk_size);
943 if (chunk_bytes_by_type(type, calc_size, num_stripes, sub_stripes) >
945 calc_size = max_chunk_size;
946 calc_size /= num_stripes;
947 calc_size /= stripe_len;
948 calc_size *= stripe_len;
950 /* we don't want tiny stripes */
951 calc_size = max_t(u64, calc_size, min_stripe_size);
953 calc_size /= stripe_len;
954 calc_size *= stripe_len;
955 INIT_LIST_HEAD(&private_devs);
956 cur = dev_list->next;
959 if (type & BTRFS_BLOCK_GROUP_DUP)
960 min_free = calc_size * 2;
962 min_free = calc_size;
964 /* build a private list of devices we will allocate from */
965 while(index < num_stripes) {
966 device = list_entry(cur, struct btrfs_device, dev_list);
967 ret = btrfs_device_avail_bytes(trans, device, &avail);
971 if (avail >= min_free) {
972 list_move_tail(&device->dev_list, &private_devs);
974 if (type & BTRFS_BLOCK_GROUP_DUP)
976 } else if (avail > max_avail)
981 if (index < num_stripes) {
982 list_splice(&private_devs, dev_list);
983 if (index >= min_stripes) {
985 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
986 num_stripes /= sub_stripes;
987 num_stripes *= sub_stripes;
992 if (!looped && max_avail > 0) {
994 calc_size = max_avail;
999 ret = find_next_chunk(info, &offset);
1002 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1003 key.type = BTRFS_CHUNK_ITEM_KEY;
1004 key.offset = offset;
1006 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
1010 map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
1016 stripes = &chunk->stripe;
1017 *num_bytes = chunk_bytes_by_type(type, calc_size,
1018 num_stripes, sub_stripes);
1020 while(index < num_stripes) {
1021 struct btrfs_stripe *stripe;
1022 BUG_ON(list_empty(&private_devs));
1023 cur = private_devs.next;
1024 device = list_entry(cur, struct btrfs_device, dev_list);
1026 /* loop over this device again if we're doing a dup group */
1027 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
1028 (index == num_stripes - 1))
1029 list_move_tail(&device->dev_list, dev_list);
1031 ret = btrfs_alloc_dev_extent(trans, device,
1032 info->chunk_root->root_key.objectid,
1033 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
1034 calc_size, &dev_offset, 0);
1037 device->bytes_used += calc_size;
1038 ret = btrfs_update_device(trans, device);
1041 map->stripes[index].dev = device;
1042 map->stripes[index].physical = dev_offset;
1043 stripe = stripes + index;
1044 btrfs_set_stack_stripe_devid(stripe, device->devid);
1045 btrfs_set_stack_stripe_offset(stripe, dev_offset);
1046 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
1049 BUG_ON(!list_empty(&private_devs));
1051 /* key was set above */
1052 btrfs_set_stack_chunk_length(chunk, *num_bytes);
1053 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
1054 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
1055 btrfs_set_stack_chunk_type(chunk, type);
1056 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
1057 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
1058 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
1059 btrfs_set_stack_chunk_sector_size(chunk, info->sectorsize);
1060 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
1061 map->sector_size = info->sectorsize;
1062 map->stripe_len = stripe_len;
1063 map->io_align = stripe_len;
1064 map->io_width = stripe_len;
1066 map->num_stripes = num_stripes;
1067 map->sub_stripes = sub_stripes;
1069 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
1070 btrfs_chunk_item_size(num_stripes));
1072 *start = key.offset;;
1074 map->ce.start = key.offset;
1075 map->ce.size = *num_bytes;
1077 ret = insert_cache_extent(&info->mapping_tree.cache_tree, &map->ce);
1080 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
1081 ret = btrfs_add_system_chunk(info, &key,
1082 chunk, btrfs_chunk_item_size(num_stripes));
1091 * Alloc a DATA chunk with SINGLE profile.
1093 * If 'convert' is set, it will alloc a chunk with 1:1 mapping
1094 * (btrfs logical bytenr == on-disk bytenr)
1095 * For that case, caller must make sure the chunk and dev_extent are not
1098 int btrfs_alloc_data_chunk(struct btrfs_trans_handle *trans,
1099 struct btrfs_fs_info *info, u64 *start,
1100 u64 num_bytes, u64 type, int convert)
1103 struct btrfs_root *extent_root = info->extent_root;
1104 struct btrfs_root *chunk_root = info->chunk_root;
1105 struct btrfs_stripe *stripes;
1106 struct btrfs_device *device = NULL;
1107 struct btrfs_chunk *chunk;
1108 struct list_head *dev_list = &info->fs_devices->devices;
1109 struct list_head *cur;
1110 struct map_lookup *map;
1111 u64 calc_size = SZ_8M;
1112 int num_stripes = 1;
1113 int sub_stripes = 0;
1116 int stripe_len = BTRFS_STRIPE_LEN;
1117 struct btrfs_key key;
1119 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1120 key.type = BTRFS_CHUNK_ITEM_KEY;
1122 if (*start != round_down(*start, info->sectorsize)) {
1123 error("DATA chunk start not sectorsize aligned: %llu",
1124 (unsigned long long)*start);
1127 key.offset = *start;
1128 dev_offset = *start;
1132 ret = find_next_chunk(info, &tmp);
1138 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
1142 map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
1148 stripes = &chunk->stripe;
1149 calc_size = num_bytes;
1152 cur = dev_list->next;
1153 device = list_entry(cur, struct btrfs_device, dev_list);
1155 while (index < num_stripes) {
1156 struct btrfs_stripe *stripe;
1158 ret = btrfs_alloc_dev_extent(trans, device,
1159 info->chunk_root->root_key.objectid,
1160 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
1161 calc_size, &dev_offset, convert);
1164 device->bytes_used += calc_size;
1165 ret = btrfs_update_device(trans, device);
1168 map->stripes[index].dev = device;
1169 map->stripes[index].physical = dev_offset;
1170 stripe = stripes + index;
1171 btrfs_set_stack_stripe_devid(stripe, device->devid);
1172 btrfs_set_stack_stripe_offset(stripe, dev_offset);
1173 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
1177 /* key was set above */
1178 btrfs_set_stack_chunk_length(chunk, num_bytes);
1179 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
1180 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
1181 btrfs_set_stack_chunk_type(chunk, type);
1182 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
1183 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
1184 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
1185 btrfs_set_stack_chunk_sector_size(chunk, info->sectorsize);
1186 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
1187 map->sector_size = info->sectorsize;
1188 map->stripe_len = stripe_len;
1189 map->io_align = stripe_len;
1190 map->io_width = stripe_len;
1192 map->num_stripes = num_stripes;
1193 map->sub_stripes = sub_stripes;
1195 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
1196 btrfs_chunk_item_size(num_stripes));
1199 *start = key.offset;
1201 map->ce.start = key.offset;
1202 map->ce.size = num_bytes;
1204 ret = insert_cache_extent(&info->mapping_tree.cache_tree, &map->ce);
1211 int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
1213 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
1214 struct cache_extent *ce;
1215 struct map_lookup *map;
1218 ce = search_cache_extent(&map_tree->cache_tree, logical);
1220 fprintf(stderr, "No mapping for %llu-%llu\n",
1221 (unsigned long long)logical,
1222 (unsigned long long)logical+len);
1225 if (ce->start > logical || ce->start + ce->size < logical) {
1226 fprintf(stderr, "Invalid mapping for %llu-%llu, got "
1227 "%llu-%llu\n", (unsigned long long)logical,
1228 (unsigned long long)logical+len,
1229 (unsigned long long)ce->start,
1230 (unsigned long long)ce->start + ce->size);
1233 map = container_of(ce, struct map_lookup, ce);
1235 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
1236 ret = map->num_stripes;
1237 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1238 ret = map->sub_stripes;
1239 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
1241 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
1248 int btrfs_next_bg(struct btrfs_fs_info *fs_info, u64 *logical,
1249 u64 *size, u64 type)
1251 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
1252 struct cache_extent *ce;
1253 struct map_lookup *map;
1256 ce = search_cache_extent(&map_tree->cache_tree, cur);
1260 * only jump to next bg if our cur is not 0
1261 * As the initial logical for btrfs_next_bg() is 0, and
1262 * if we jump to next bg, we skipped a valid bg.
1265 ce = next_cache_extent(ce);
1271 map = container_of(ce, struct map_lookup, ce);
1272 if (map->type & type) {
1273 *logical = ce->start;
1278 ce = next_cache_extent(ce);
1284 int btrfs_rmap_block(struct btrfs_fs_info *fs_info,
1285 u64 chunk_start, u64 physical, u64 devid,
1286 u64 **logical, int *naddrs, int *stripe_len)
1288 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
1289 struct cache_extent *ce;
1290 struct map_lookup *map;
1298 ce = search_cache_extent(&map_tree->cache_tree, chunk_start);
1300 map = container_of(ce, struct map_lookup, ce);
1303 rmap_len = map->stripe_len;
1304 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1305 length = ce->size / (map->num_stripes / map->sub_stripes);
1306 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
1307 length = ce->size / map->num_stripes;
1308 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
1309 BTRFS_BLOCK_GROUP_RAID6)) {
1310 length = ce->size / nr_data_stripes(map);
1311 rmap_len = map->stripe_len * nr_data_stripes(map);
1314 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
1316 for (i = 0; i < map->num_stripes; i++) {
1317 if (devid && map->stripes[i].dev->devid != devid)
1319 if (map->stripes[i].physical > physical ||
1320 map->stripes[i].physical + length <= physical)
1323 stripe_nr = (physical - map->stripes[i].physical) /
1326 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1327 stripe_nr = (stripe_nr * map->num_stripes + i) /
1329 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
1330 stripe_nr = stripe_nr * map->num_stripes + i;
1331 } /* else if RAID[56], multiply by nr_data_stripes().
1332 * Alternatively, just use rmap_len below instead of
1333 * map->stripe_len */
1335 bytenr = ce->start + stripe_nr * rmap_len;
1336 for (j = 0; j < nr; j++) {
1337 if (buf[j] == bytenr)
1346 *stripe_len = rmap_len;
1351 static inline int parity_smaller(u64 a, u64 b)
1356 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
1357 static void sort_parity_stripes(struct btrfs_multi_bio *bbio, u64 *raid_map)
1359 struct btrfs_bio_stripe s;
1366 for (i = 0; i < bbio->num_stripes - 1; i++) {
1367 if (parity_smaller(raid_map[i], raid_map[i+1])) {
1368 s = bbio->stripes[i];
1370 bbio->stripes[i] = bbio->stripes[i+1];
1371 raid_map[i] = raid_map[i+1];
1372 bbio->stripes[i+1] = s;
1380 int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
1381 u64 logical, u64 *length,
1382 struct btrfs_multi_bio **multi_ret, int mirror_num,
1385 return __btrfs_map_block(fs_info, rw, logical, length, NULL,
1386 multi_ret, mirror_num, raid_map_ret);
1389 int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
1390 u64 logical, u64 *length, u64 *type,
1391 struct btrfs_multi_bio **multi_ret, int mirror_num,
1394 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
1395 struct cache_extent *ce;
1396 struct map_lookup *map;
1400 u64 *raid_map = NULL;
1401 int stripes_allocated = 8;
1402 int stripes_required = 1;
1405 struct btrfs_multi_bio *multi = NULL;
1407 if (multi_ret && rw == READ) {
1408 stripes_allocated = 1;
1411 ce = search_cache_extent(&map_tree->cache_tree, logical);
1417 if (ce->start > logical) {
1419 *length = ce->start - logical;
1424 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
1429 map = container_of(ce, struct map_lookup, ce);
1430 offset = logical - ce->start;
1433 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
1434 BTRFS_BLOCK_GROUP_DUP)) {
1435 stripes_required = map->num_stripes;
1436 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1437 stripes_required = map->sub_stripes;
1440 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)
1441 && multi_ret && ((rw & WRITE) || mirror_num > 1) && raid_map_ret) {
1442 /* RAID[56] write or recovery. Return all stripes */
1443 stripes_required = map->num_stripes;
1445 /* Only allocate the map if we've already got a large enough multi_ret */
1446 if (stripes_allocated >= stripes_required) {
1447 raid_map = kmalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
1455 /* if our multi bio struct is too small, back off and try again */
1456 if (multi_ret && stripes_allocated < stripes_required) {
1457 stripes_allocated = stripes_required;
1464 * stripe_nr counts the total number of stripes we have to stride
1465 * to get to this block
1467 stripe_nr = stripe_nr / map->stripe_len;
1469 stripe_offset = stripe_nr * map->stripe_len;
1470 BUG_ON(offset < stripe_offset);
1472 /* stripe_offset is the offset of this block in its stripe*/
1473 stripe_offset = offset - stripe_offset;
1475 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
1476 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
1477 BTRFS_BLOCK_GROUP_RAID10 |
1478 BTRFS_BLOCK_GROUP_DUP)) {
1479 /* we limit the length of each bio to what fits in a stripe */
1480 *length = min_t(u64, ce->size - offset,
1481 map->stripe_len - stripe_offset);
1483 *length = ce->size - offset;
1489 multi->num_stripes = 1;
1491 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
1493 multi->num_stripes = map->num_stripes;
1494 else if (mirror_num)
1495 stripe_index = mirror_num - 1;
1497 stripe_index = stripe_nr % map->num_stripes;
1498 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1499 int factor = map->num_stripes / map->sub_stripes;
1501 stripe_index = stripe_nr % factor;
1502 stripe_index *= map->sub_stripes;
1505 multi->num_stripes = map->sub_stripes;
1506 else if (mirror_num)
1507 stripe_index += mirror_num - 1;
1509 stripe_nr = stripe_nr / factor;
1510 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
1512 multi->num_stripes = map->num_stripes;
1513 else if (mirror_num)
1514 stripe_index = mirror_num - 1;
1515 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
1516 BTRFS_BLOCK_GROUP_RAID6)) {
1521 u64 raid56_full_stripe_start;
1522 u64 full_stripe_len = nr_data_stripes(map) * map->stripe_len;
1525 * align the start of our data stripe in the logical
1528 raid56_full_stripe_start = offset / full_stripe_len;
1529 raid56_full_stripe_start *= full_stripe_len;
1531 /* get the data stripe number */
1532 stripe_nr = raid56_full_stripe_start / map->stripe_len;
1533 stripe_nr = stripe_nr / nr_data_stripes(map);
1535 /* Work out the disk rotation on this stripe-set */
1536 rot = stripe_nr % map->num_stripes;
1538 /* Fill in the logical address of each stripe */
1539 tmp = stripe_nr * nr_data_stripes(map);
1541 for (i = 0; i < nr_data_stripes(map); i++)
1542 raid_map[(i+rot) % map->num_stripes] =
1543 ce->start + (tmp + i) * map->stripe_len;
1545 raid_map[(i+rot) % map->num_stripes] = BTRFS_RAID5_P_STRIPE;
1546 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
1547 raid_map[(i+rot+1) % map->num_stripes] = BTRFS_RAID6_Q_STRIPE;
1549 *length = map->stripe_len;
1552 multi->num_stripes = map->num_stripes;
1554 stripe_index = stripe_nr % nr_data_stripes(map);
1555 stripe_nr = stripe_nr / nr_data_stripes(map);
1558 * Mirror #0 or #1 means the original data block.
1559 * Mirror #2 is RAID5 parity block.
1560 * Mirror #3 is RAID6 Q block.
1563 stripe_index = nr_data_stripes(map) + mirror_num - 2;
1565 /* We distribute the parity blocks across stripes */
1566 stripe_index = (stripe_nr + stripe_index) % map->num_stripes;
1570 * after this do_div call, stripe_nr is the number of stripes
1571 * on this device we have to walk to find the data, and
1572 * stripe_index is the number of our device in the stripe array
1574 stripe_index = stripe_nr % map->num_stripes;
1575 stripe_nr = stripe_nr / map->num_stripes;
1577 BUG_ON(stripe_index >= map->num_stripes);
1579 for (i = 0; i < multi->num_stripes; i++) {
1580 multi->stripes[i].physical =
1581 map->stripes[stripe_index].physical + stripe_offset +
1582 stripe_nr * map->stripe_len;
1583 multi->stripes[i].dev = map->stripes[stripe_index].dev;
1592 sort_parity_stripes(multi, raid_map);
1593 *raid_map_ret = raid_map;
1599 struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
1602 struct btrfs_device *device;
1603 struct btrfs_fs_devices *cur_devices;
1605 cur_devices = fs_info->fs_devices;
1606 while (cur_devices) {
1608 (!memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE) ||
1609 fs_info->ignore_fsid_mismatch)) {
1610 device = __find_device(&cur_devices->devices,
1615 cur_devices = cur_devices->seed;
1620 struct btrfs_device *
1621 btrfs_find_device_by_devid(struct btrfs_fs_devices *fs_devices,
1622 u64 devid, int instance)
1624 struct list_head *head = &fs_devices->devices;
1625 struct btrfs_device *dev;
1628 list_for_each_entry(dev, head, dev_list) {
1629 if (dev->devid == devid && num_found++ == instance)
1635 int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
1637 struct cache_extent *ce;
1638 struct map_lookup *map;
1639 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
1644 * During chunk recovering, we may fail to find block group's
1645 * corresponding chunk, we will rebuild it later
1647 ce = search_cache_extent(&map_tree->cache_tree, chunk_offset);
1648 if (!fs_info->is_chunk_recover)
1653 map = container_of(ce, struct map_lookup, ce);
1654 for (i = 0; i < map->num_stripes; i++) {
1655 if (!map->stripes[i].dev->writeable) {
1664 static struct btrfs_device *fill_missing_device(u64 devid)
1666 struct btrfs_device *device;
1668 device = kzalloc(sizeof(*device), GFP_NOFS);
1669 device->devid = devid;
1675 * slot == -1: SYSTEM chunk
1676 * return -EIO on error, otherwise return 0
1678 int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info,
1679 struct extent_buffer *leaf,
1680 struct btrfs_chunk *chunk,
1681 int slot, u64 logical)
1688 u32 chunk_ondisk_size;
1689 u32 sectorsize = fs_info->sectorsize;
1691 length = btrfs_chunk_length(leaf, chunk);
1692 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1693 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1694 sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
1695 type = btrfs_chunk_type(leaf, chunk);
1698 * These valid checks may be insufficient to cover every corner cases.
1700 if (!IS_ALIGNED(logical, sectorsize)) {
1701 error("invalid chunk logical %llu", logical);
1704 if (btrfs_chunk_sector_size(leaf, chunk) != sectorsize) {
1705 error("invalid chunk sectorsize %llu",
1706 (unsigned long long)btrfs_chunk_sector_size(leaf, chunk));
1709 if (!length || !IS_ALIGNED(length, sectorsize)) {
1710 error("invalid chunk length %llu", length);
1713 if (stripe_len != BTRFS_STRIPE_LEN) {
1714 error("invalid chunk stripe length: %llu", stripe_len);
1717 /* Check on chunk item type */
1718 if (slot == -1 && (type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
1719 error("invalid chunk type %llu", type);
1722 if (type & ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
1723 BTRFS_BLOCK_GROUP_PROFILE_MASK)) {
1724 error("unrecognized chunk type: %llu",
1725 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
1726 BTRFS_BLOCK_GROUP_PROFILE_MASK) & type);
1729 if (!(type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
1730 error("missing chunk type flag: %llu", type);
1733 if (!(is_power_of_2(type & BTRFS_BLOCK_GROUP_PROFILE_MASK) ||
1734 (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0)) {
1735 error("conflicting chunk type detected: %llu", type);
1738 if ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) &&
1739 !is_power_of_2(type & BTRFS_BLOCK_GROUP_PROFILE_MASK)) {
1740 error("conflicting chunk profile detected: %llu", type);
1744 chunk_ondisk_size = btrfs_chunk_item_size(num_stripes);
1746 * Btrfs_chunk contains at least one stripe, and for sys_chunk
1747 * it can't exceed the system chunk array size
1748 * For normal chunk, it should match its chunk item size.
1750 if (num_stripes < 1 ||
1751 (slot == -1 && chunk_ondisk_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) ||
1752 (slot >= 0 && chunk_ondisk_size > btrfs_item_size_nr(leaf, slot))) {
1753 error("invalid num_stripes: %u", num_stripes);
1757 * Device number check against profile
1759 if ((type & BTRFS_BLOCK_GROUP_RAID10 && (sub_stripes != 2 ||
1760 !IS_ALIGNED(num_stripes, sub_stripes))) ||
1761 (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) ||
1762 (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
1763 (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
1764 (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||
1765 ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
1766 num_stripes != 1)) {
1767 error("Invalid num_stripes:sub_stripes %u:%u for profile %llu",
1768 num_stripes, sub_stripes,
1769 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
1777 * Slot is used to verify the chunk item is valid
1779 * For sys chunk in superblock, pass -1 to indicate sys chunk.
1781 static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
1782 struct extent_buffer *leaf,
1783 struct btrfs_chunk *chunk, int slot)
1785 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
1786 struct map_lookup *map;
1787 struct cache_extent *ce;
1791 u8 uuid[BTRFS_UUID_SIZE];
1796 logical = key->offset;
1797 length = btrfs_chunk_length(leaf, chunk);
1798 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1799 /* Validation check */
1800 ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, slot, logical);
1802 error("%s checksums match, but it has an invalid chunk, %s",
1803 (slot == -1) ? "Superblock" : "Metadata",
1804 (slot == -1) ? "try btrfsck --repair -s <superblock> ie, 0,1,2" : "");
1808 ce = search_cache_extent(&map_tree->cache_tree, logical);
1810 /* already mapped? */
1811 if (ce && ce->start <= logical && ce->start + ce->size > logical) {
1815 map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
1819 map->ce.start = logical;
1820 map->ce.size = length;
1821 map->num_stripes = num_stripes;
1822 map->io_width = btrfs_chunk_io_width(leaf, chunk);
1823 map->io_align = btrfs_chunk_io_align(leaf, chunk);
1824 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1825 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1826 map->type = btrfs_chunk_type(leaf, chunk);
1827 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
1829 for (i = 0; i < num_stripes; i++) {
1830 map->stripes[i].physical =
1831 btrfs_stripe_offset_nr(leaf, chunk, i);
1832 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
1833 read_extent_buffer(leaf, uuid, (unsigned long)
1834 btrfs_stripe_dev_uuid_nr(chunk, i),
1836 map->stripes[i].dev = btrfs_find_device(fs_info, devid, uuid,
1838 if (!map->stripes[i].dev) {
1839 map->stripes[i].dev = fill_missing_device(devid);
1840 printf("warning, device %llu is missing\n",
1841 (unsigned long long)devid);
1842 list_add(&map->stripes[i].dev->dev_list,
1843 &fs_info->fs_devices->devices);
1847 ret = insert_cache_extent(&map_tree->cache_tree, &map->ce);
1853 static int fill_device_from_item(struct extent_buffer *leaf,
1854 struct btrfs_dev_item *dev_item,
1855 struct btrfs_device *device)
1859 device->devid = btrfs_device_id(leaf, dev_item);
1860 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1861 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1862 device->type = btrfs_device_type(leaf, dev_item);
1863 device->io_align = btrfs_device_io_align(leaf, dev_item);
1864 device->io_width = btrfs_device_io_width(leaf, dev_item);
1865 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
1867 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1868 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1873 static int open_seed_devices(struct btrfs_fs_info *fs_info, u8 *fsid)
1875 struct btrfs_fs_devices *fs_devices;
1878 fs_devices = fs_info->fs_devices->seed;
1879 while (fs_devices) {
1880 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
1884 fs_devices = fs_devices->seed;
1887 fs_devices = find_fsid(fsid);
1889 /* missing all seed devices */
1890 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1895 INIT_LIST_HEAD(&fs_devices->devices);
1896 list_add(&fs_devices->list, &fs_uuids);
1897 memcpy(fs_devices->fsid, fsid, BTRFS_FSID_SIZE);
1900 ret = btrfs_open_devices(fs_devices, O_RDONLY);
1904 fs_devices->seed = fs_info->fs_devices->seed;
1905 fs_info->fs_devices->seed = fs_devices;
1910 static int read_one_dev(struct btrfs_fs_info *fs_info,
1911 struct extent_buffer *leaf,
1912 struct btrfs_dev_item *dev_item)
1914 struct btrfs_device *device;
1917 u8 fs_uuid[BTRFS_UUID_SIZE];
1918 u8 dev_uuid[BTRFS_UUID_SIZE];
1920 devid = btrfs_device_id(leaf, dev_item);
1921 read_extent_buffer(leaf, dev_uuid,
1922 (unsigned long)btrfs_device_uuid(dev_item),
1924 read_extent_buffer(leaf, fs_uuid,
1925 (unsigned long)btrfs_device_fsid(dev_item),
1928 if (memcmp(fs_uuid, fs_info->fsid, BTRFS_UUID_SIZE)) {
1929 ret = open_seed_devices(fs_info, fs_uuid);
1934 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
1936 device = kzalloc(sizeof(*device), GFP_NOFS);
1940 list_add(&device->dev_list,
1941 &fs_info->fs_devices->devices);
1944 fill_device_from_item(leaf, dev_item, device);
1945 device->dev_root = fs_info->dev_root;
1949 int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
1951 struct btrfs_super_block *super_copy = fs_info->super_copy;
1952 struct extent_buffer *sb;
1953 struct btrfs_disk_key *disk_key;
1954 struct btrfs_chunk *chunk;
1956 unsigned long sb_array_offset;
1962 struct btrfs_key key;
1964 if (fs_info->nodesize < BTRFS_SUPER_INFO_SIZE) {
1965 printf("ERROR: nodesize %u too small to read superblock\n",
1969 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
1972 btrfs_set_buffer_uptodate(sb);
1973 write_extent_buffer(sb, super_copy, 0, sizeof(*super_copy));
1974 array_size = btrfs_super_sys_array_size(super_copy);
1976 array_ptr = super_copy->sys_chunk_array;
1977 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
1980 while (cur_offset < array_size) {
1981 disk_key = (struct btrfs_disk_key *)array_ptr;
1982 len = sizeof(*disk_key);
1983 if (cur_offset + len > array_size)
1984 goto out_short_read;
1986 btrfs_disk_key_to_cpu(&key, disk_key);
1989 sb_array_offset += len;
1992 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1993 chunk = (struct btrfs_chunk *)sb_array_offset;
1995 * At least one btrfs_chunk with one stripe must be
1996 * present, exact stripe count check comes afterwards
1998 len = btrfs_chunk_item_size(1);
1999 if (cur_offset + len > array_size)
2000 goto out_short_read;
2002 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
2005 "ERROR: invalid number of stripes %u in sys_array at offset %u\n",
2006 num_stripes, cur_offset);
2011 len = btrfs_chunk_item_size(num_stripes);
2012 if (cur_offset + len > array_size)
2013 goto out_short_read;
2015 ret = read_one_chunk(fs_info, &key, sb, chunk, -1);
2020 "ERROR: unexpected item type %u in sys_array at offset %u\n",
2021 (u32)key.type, cur_offset);
2026 sb_array_offset += len;
2029 free_extent_buffer(sb);
2033 printk("ERROR: sys_array too short to read %u bytes at offset %u\n",
2035 free_extent_buffer(sb);
2039 int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
2041 struct btrfs_path *path;
2042 struct extent_buffer *leaf;
2043 struct btrfs_key key;
2044 struct btrfs_key found_key;
2045 struct btrfs_root *root = fs_info->chunk_root;
2049 path = btrfs_alloc_path();
2054 * Read all device items, and then all the chunk items. All
2055 * device items are found before any chunk item (their object id
2056 * is smaller than the lowest possible object id for a chunk
2057 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
2059 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2062 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2066 leaf = path->nodes[0];
2067 slot = path->slots[0];
2068 if (slot >= btrfs_header_nritems(leaf)) {
2069 ret = btrfs_next_leaf(root, path);
2076 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2077 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
2078 struct btrfs_dev_item *dev_item;
2079 dev_item = btrfs_item_ptr(leaf, slot,
2080 struct btrfs_dev_item);
2081 ret = read_one_dev(fs_info, leaf, dev_item);
2083 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
2084 struct btrfs_chunk *chunk;
2085 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2086 ret = read_one_chunk(fs_info, &found_key, leaf, chunk,
2095 btrfs_free_path(path);
2099 struct list_head *btrfs_scanned_uuids(void)
2104 static int rmw_eb(struct btrfs_fs_info *info,
2105 struct extent_buffer *eb, struct extent_buffer *orig_eb)
2108 unsigned long orig_off = 0;
2109 unsigned long dest_off = 0;
2110 unsigned long copy_len = eb->len;
2112 ret = read_whole_eb(info, eb, 0);
2116 if (eb->start + eb->len <= orig_eb->start ||
2117 eb->start >= orig_eb->start + orig_eb->len)
2120 * | ----- orig_eb ------- |
2121 * | ----- stripe ------- |
2122 * | ----- orig_eb ------- |
2123 * | ----- orig_eb ------- |
2125 if (eb->start > orig_eb->start)
2126 orig_off = eb->start - orig_eb->start;
2127 if (orig_eb->start > eb->start)
2128 dest_off = orig_eb->start - eb->start;
2130 if (copy_len > orig_eb->len - orig_off)
2131 copy_len = orig_eb->len - orig_off;
2132 if (copy_len > eb->len - dest_off)
2133 copy_len = eb->len - dest_off;
2135 memcpy(eb->data + dest_off, orig_eb->data + orig_off, copy_len);
2139 static int split_eb_for_raid56(struct btrfs_fs_info *info,
2140 struct extent_buffer *orig_eb,
2141 struct extent_buffer **ebs,
2142 u64 stripe_len, u64 *raid_map,
2145 struct extent_buffer **tmp_ebs;
2146 u64 start = orig_eb->start;
2151 tmp_ebs = calloc(num_stripes, sizeof(*tmp_ebs));
2155 /* Alloc memory in a row for data stripes */
2156 for (i = 0; i < num_stripes; i++) {
2157 if (raid_map[i] >= BTRFS_RAID5_P_STRIPE)
2160 tmp_ebs[i] = calloc(1, sizeof(**tmp_ebs) + stripe_len);
2167 for (i = 0; i < num_stripes; i++) {
2168 struct extent_buffer *eb = tmp_ebs[i];
2170 if (raid_map[i] >= BTRFS_RAID5_P_STRIPE)
2173 eb->start = raid_map[i];
2174 eb->len = stripe_len;
2178 eb->dev_bytenr = (u64)-1;
2180 this_eb_start = raid_map[i];
2182 if (start > this_eb_start ||
2183 start + orig_eb->len < this_eb_start + stripe_len) {
2184 ret = rmw_eb(info, eb, orig_eb);
2188 memcpy(eb->data, orig_eb->data + eb->start - start,
2196 for (i = 0; i < num_stripes; i++)
2202 int write_raid56_with_parity(struct btrfs_fs_info *info,
2203 struct extent_buffer *eb,
2204 struct btrfs_multi_bio *multi,
2205 u64 stripe_len, u64 *raid_map)
2207 struct extent_buffer **ebs, *p_eb = NULL, *q_eb = NULL;
2210 int alloc_size = eb->len;
2213 ebs = malloc(sizeof(*ebs) * multi->num_stripes);
2214 pointers = malloc(sizeof(*pointers) * multi->num_stripes);
2215 if (!ebs || !pointers) {
2221 if (stripe_len > alloc_size)
2222 alloc_size = stripe_len;
2224 ret = split_eb_for_raid56(info, eb, ebs, stripe_len, raid_map,
2225 multi->num_stripes);
2229 for (i = 0; i < multi->num_stripes; i++) {
2230 struct extent_buffer *new_eb;
2231 if (raid_map[i] < BTRFS_RAID5_P_STRIPE) {
2232 ebs[i]->dev_bytenr = multi->stripes[i].physical;
2233 ebs[i]->fd = multi->stripes[i].dev->fd;
2234 multi->stripes[i].dev->total_ios++;
2235 if (ebs[i]->start != raid_map[i]) {
2237 goto out_free_split;
2241 new_eb = malloc(sizeof(*eb) + alloc_size);
2244 goto out_free_split;
2246 new_eb->dev_bytenr = multi->stripes[i].physical;
2247 new_eb->fd = multi->stripes[i].dev->fd;
2248 multi->stripes[i].dev->total_ios++;
2249 new_eb->len = stripe_len;
2251 if (raid_map[i] == BTRFS_RAID5_P_STRIPE)
2253 else if (raid_map[i] == BTRFS_RAID6_Q_STRIPE)
2257 ebs[multi->num_stripes - 2] = p_eb;
2258 ebs[multi->num_stripes - 1] = q_eb;
2260 for (i = 0; i < multi->num_stripes; i++)
2261 pointers[i] = ebs[i]->data;
2263 raid6_gen_syndrome(multi->num_stripes, stripe_len, pointers);
2265 ebs[multi->num_stripes - 1] = p_eb;
2266 for (i = 0; i < multi->num_stripes; i++)
2267 pointers[i] = ebs[i]->data;
2268 ret = raid5_gen_result(multi->num_stripes, stripe_len,
2269 multi->num_stripes - 1, pointers);
2271 goto out_free_split;
2274 for (i = 0; i < multi->num_stripes; i++) {
2275 ret = write_extent_to_disk(ebs[i]);
2277 goto out_free_split;
2281 for (i = 0; i < multi->num_stripes; i++) {
2293 * Get stripe length from chunk item and its stripe items
2295 * Caller should only call this function after validating the chunk item
2296 * by using btrfs_check_chunk_valid().
2298 u64 btrfs_stripe_length(struct btrfs_fs_info *fs_info,
2299 struct extent_buffer *leaf,
2300 struct btrfs_chunk *chunk)
2304 u32 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2305 u64 profile = btrfs_chunk_type(leaf, chunk) &
2306 BTRFS_BLOCK_GROUP_PROFILE_MASK;
2308 chunk_len = btrfs_chunk_length(leaf, chunk);
2311 case 0: /* Single profile */
2312 case BTRFS_BLOCK_GROUP_RAID1:
2313 case BTRFS_BLOCK_GROUP_DUP:
2314 stripe_len = chunk_len;
2316 case BTRFS_BLOCK_GROUP_RAID0:
2317 stripe_len = chunk_len / num_stripes;
2319 case BTRFS_BLOCK_GROUP_RAID5:
2320 stripe_len = chunk_len / (num_stripes - 1);
2322 case BTRFS_BLOCK_GROUP_RAID6:
2323 stripe_len = chunk_len / (num_stripes - 2);
2325 case BTRFS_BLOCK_GROUP_RAID10:
2326 stripe_len = chunk_len / (num_stripes /
2327 btrfs_chunk_sub_stripes(leaf, chunk));
2330 /* Invalid chunk profile found */