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.
18 #define _XOPEN_SOURCE 600
22 #include <sys/types.h>
24 #include <uuid/uuid.h>
29 #include "transaction.h"
30 #include "print-tree.h"
35 struct btrfs_device *dev;
39 static inline int nr_parity_stripes(struct map_lookup *map)
41 if (map->type & BTRFS_BLOCK_GROUP_RAID5)
43 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
49 static inline int nr_data_stripes(struct map_lookup *map)
51 return map->num_stripes - nr_parity_stripes(map);
54 #define is_parity_stripe(x) ( ((x) == BTRFS_RAID5_P_STRIPE) || ((x) == BTRFS_RAID6_Q_STRIPE) )
56 static LIST_HEAD(fs_uuids);
58 static struct btrfs_device *__find_device(struct list_head *head, u64 devid,
61 struct btrfs_device *dev;
62 struct list_head *cur;
64 list_for_each(cur, head) {
65 dev = list_entry(cur, struct btrfs_device, dev_list);
66 if (dev->devid == devid &&
67 !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE)) {
74 static struct btrfs_fs_devices *find_fsid(u8 *fsid)
76 struct list_head *cur;
77 struct btrfs_fs_devices *fs_devices;
79 list_for_each(cur, &fs_uuids) {
80 fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
81 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
87 static int device_list_add(const char *path,
88 struct btrfs_super_block *disk_super,
89 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
91 struct btrfs_device *device;
92 struct btrfs_fs_devices *fs_devices;
93 u64 found_transid = btrfs_super_generation(disk_super);
95 fs_devices = find_fsid(disk_super->fsid);
97 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
100 INIT_LIST_HEAD(&fs_devices->devices);
101 list_add(&fs_devices->list, &fs_uuids);
102 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
103 fs_devices->latest_devid = devid;
104 fs_devices->latest_trans = found_transid;
105 fs_devices->lowest_devid = (u64)-1;
108 device = __find_device(&fs_devices->devices, devid,
109 disk_super->dev_item.uuid);
112 device = kzalloc(sizeof(*device), GFP_NOFS);
114 /* we can safely leave the fs_devices entry around */
118 device->devid = devid;
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;
166 while (!list_empty(&fs_devices->devices)) {
167 device = list_entry(fs_devices->devices.next,
168 struct btrfs_device, dev_list);
169 if (device->fd != -1) {
171 if (posix_fadvise(device->fd, 0, 0, POSIX_FADV_DONTNEED))
172 fprintf(stderr, "Warning, could not drop caches\n");
176 device->writeable = 0;
177 list_del(&device->dev_list);
178 /* free the memory */
184 seed_devices = fs_devices->seed;
185 fs_devices->seed = NULL;
187 fs_devices = seed_devices;
195 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, int flags)
198 struct list_head *head = &fs_devices->devices;
199 struct list_head *cur;
200 struct btrfs_device *device;
203 list_for_each(cur, head) {
204 device = list_entry(cur, struct btrfs_device, dev_list);
206 printk("no name for device %llu, skip it now\n", device->devid);
210 fd = open(device->name, flags);
216 if (posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED))
217 fprintf(stderr, "Warning, could not drop caches\n");
219 if (device->devid == fs_devices->latest_devid)
220 fs_devices->latest_bdev = fd;
221 if (device->devid == fs_devices->lowest_devid)
222 fs_devices->lowest_bdev = fd;
225 device->writeable = 1;
229 btrfs_close_devices(fs_devices);
233 int btrfs_scan_one_device(int fd, const char *path,
234 struct btrfs_fs_devices **fs_devices_ret,
235 u64 *total_devs, u64 super_offset)
237 struct btrfs_super_block *disk_super;
247 disk_super = (struct btrfs_super_block *)buf;
248 ret = btrfs_read_dev_super(fd, disk_super, super_offset);
253 devid = btrfs_stack_device_id(&disk_super->dev_item);
254 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)
257 *total_devs = btrfs_super_num_devices(disk_super);
259 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
268 * this uses a pretty simple search, the expectation is that it is
269 * called very infrequently and that a given device has a small number
272 static int find_free_dev_extent(struct btrfs_trans_handle *trans,
273 struct btrfs_device *device,
274 struct btrfs_path *path,
275 u64 num_bytes, u64 *start)
277 struct btrfs_key key;
278 struct btrfs_root *root = device->dev_root;
279 struct btrfs_dev_extent *dev_extent = NULL;
282 u64 search_start = root->fs_info->alloc_start;
283 u64 search_end = device->total_bytes;
287 struct extent_buffer *l;
292 /* FIXME use last free of some kind */
294 /* we don't want to overwrite the superblock on the drive,
295 * so we make sure to start at an offset of at least 1MB
297 search_start = max(BTRFS_BLOCK_RESERVED_1M_FOR_SUPER, search_start);
299 if (search_start >= search_end) {
304 key.objectid = device->devid;
305 key.offset = search_start;
306 key.type = BTRFS_DEV_EXTENT_KEY;
307 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
310 ret = btrfs_previous_item(root, path, 0, key.type);
314 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
317 slot = path->slots[0];
318 if (slot >= btrfs_header_nritems(l)) {
319 ret = btrfs_next_leaf(root, path);
326 if (search_start >= search_end) {
330 *start = search_start;
334 *start = last_byte > search_start ?
335 last_byte : search_start;
336 if (search_end <= *start) {
342 btrfs_item_key_to_cpu(l, &key, slot);
344 if (key.objectid < device->devid)
347 if (key.objectid > device->devid)
350 if (key.offset >= search_start && key.offset > last_byte &&
352 if (last_byte < search_start)
353 last_byte = search_start;
354 hole_size = key.offset - last_byte;
355 if (key.offset > last_byte &&
356 hole_size >= num_bytes) {
361 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
366 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
367 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
373 /* we have to make sure we didn't find an extent that has already
374 * been allocated by the map tree or the original allocation
376 btrfs_release_path(path);
377 BUG_ON(*start < search_start);
379 if (*start + num_bytes > search_end) {
383 /* check for pending inserts here */
387 btrfs_release_path(path);
391 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
392 struct btrfs_device *device,
393 u64 chunk_tree, u64 chunk_objectid,
395 u64 num_bytes, u64 *start)
398 struct btrfs_path *path;
399 struct btrfs_root *root = device->dev_root;
400 struct btrfs_dev_extent *extent;
401 struct extent_buffer *leaf;
402 struct btrfs_key key;
404 path = btrfs_alloc_path();
408 ret = find_free_dev_extent(trans, device, path, num_bytes, start);
413 key.objectid = device->devid;
415 key.type = BTRFS_DEV_EXTENT_KEY;
416 ret = btrfs_insert_empty_item(trans, root, path, &key,
420 leaf = path->nodes[0];
421 extent = btrfs_item_ptr(leaf, path->slots[0],
422 struct btrfs_dev_extent);
423 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
424 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
425 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
427 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
428 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
431 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
432 btrfs_mark_buffer_dirty(leaf);
434 btrfs_free_path(path);
438 static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset)
440 struct btrfs_path *path;
442 struct btrfs_key key;
443 struct btrfs_chunk *chunk;
444 struct btrfs_key found_key;
446 path = btrfs_alloc_path();
449 key.objectid = objectid;
450 key.offset = (u64)-1;
451 key.type = BTRFS_CHUNK_ITEM_KEY;
453 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
459 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
463 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
465 if (found_key.objectid != objectid)
468 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
470 *offset = found_key.offset +
471 btrfs_chunk_length(path->nodes[0], chunk);
476 btrfs_free_path(path);
480 static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
484 struct btrfs_key key;
485 struct btrfs_key found_key;
487 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
488 key.type = BTRFS_DEV_ITEM_KEY;
489 key.offset = (u64)-1;
491 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
497 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
502 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
504 *objectid = found_key.offset + 1;
508 btrfs_release_path(path);
513 * the device information is stored in the chunk root
514 * the btrfs_device struct should be fully filled in
516 int btrfs_add_device(struct btrfs_trans_handle *trans,
517 struct btrfs_root *root,
518 struct btrfs_device *device)
521 struct btrfs_path *path;
522 struct btrfs_dev_item *dev_item;
523 struct extent_buffer *leaf;
524 struct btrfs_key key;
528 root = root->fs_info->chunk_root;
530 path = btrfs_alloc_path();
534 ret = find_next_devid(root, path, &free_devid);
538 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
539 key.type = BTRFS_DEV_ITEM_KEY;
540 key.offset = free_devid;
542 ret = btrfs_insert_empty_item(trans, root, path, &key,
547 leaf = path->nodes[0];
548 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
550 device->devid = free_devid;
551 btrfs_set_device_id(leaf, dev_item, device->devid);
552 btrfs_set_device_generation(leaf, dev_item, 0);
553 btrfs_set_device_type(leaf, dev_item, device->type);
554 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
555 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
556 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
557 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
558 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
559 btrfs_set_device_group(leaf, dev_item, 0);
560 btrfs_set_device_seek_speed(leaf, dev_item, 0);
561 btrfs_set_device_bandwidth(leaf, dev_item, 0);
562 btrfs_set_device_start_offset(leaf, dev_item, 0);
564 ptr = (unsigned long)btrfs_device_uuid(dev_item);
565 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
566 ptr = (unsigned long)btrfs_device_fsid(dev_item);
567 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
568 btrfs_mark_buffer_dirty(leaf);
572 btrfs_free_path(path);
576 int btrfs_update_device(struct btrfs_trans_handle *trans,
577 struct btrfs_device *device)
580 struct btrfs_path *path;
581 struct btrfs_root *root;
582 struct btrfs_dev_item *dev_item;
583 struct extent_buffer *leaf;
584 struct btrfs_key key;
586 root = device->dev_root->fs_info->chunk_root;
588 path = btrfs_alloc_path();
592 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
593 key.type = BTRFS_DEV_ITEM_KEY;
594 key.offset = device->devid;
596 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
605 leaf = path->nodes[0];
606 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
608 btrfs_set_device_id(leaf, dev_item, device->devid);
609 btrfs_set_device_type(leaf, dev_item, device->type);
610 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
611 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
612 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
613 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
614 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
615 btrfs_mark_buffer_dirty(leaf);
618 btrfs_free_path(path);
622 int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
623 struct btrfs_root *root,
624 struct btrfs_key *key,
625 struct btrfs_chunk *chunk, int item_size)
627 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
628 struct btrfs_disk_key disk_key;
632 array_size = btrfs_super_sys_array_size(super_copy);
633 if (array_size + item_size + sizeof(disk_key)
634 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
637 ptr = super_copy->sys_chunk_array + array_size;
638 btrfs_cpu_key_to_disk(&disk_key, key);
639 memcpy(ptr, &disk_key, sizeof(disk_key));
640 ptr += sizeof(disk_key);
641 memcpy(ptr, chunk, item_size);
642 item_size += sizeof(disk_key);
643 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
647 static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes,
650 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
652 else if (type & BTRFS_BLOCK_GROUP_RAID10)
653 return calc_size * (num_stripes / sub_stripes);
654 else if (type & BTRFS_BLOCK_GROUP_RAID5)
655 return calc_size * (num_stripes - 1);
656 else if (type & BTRFS_BLOCK_GROUP_RAID6)
657 return calc_size * (num_stripes - 2);
659 return calc_size * num_stripes;
663 static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
665 /* TODO, add a way to store the preferred stripe size */
666 return BTRFS_STRIPE_LEN;
670 * btrfs_device_avail_bytes - count bytes available for alloc_chunk
672 * It is not equal to "device->total_bytes - device->bytes_used".
673 * We do not allocate any chunk in 1M at beginning of device, and not
674 * allowed to allocate any chunk before alloc_start if it is specified.
675 * So search holes from max(1M, alloc_start) to device->total_bytes.
677 static int btrfs_device_avail_bytes(struct btrfs_trans_handle *trans,
678 struct btrfs_device *device,
681 struct btrfs_path *path;
682 struct btrfs_root *root = device->dev_root;
683 struct btrfs_key key;
684 struct btrfs_dev_extent *dev_extent = NULL;
685 struct extent_buffer *l;
686 u64 search_start = root->fs_info->alloc_start;
687 u64 search_end = device->total_bytes;
693 search_start = max(BTRFS_BLOCK_RESERVED_1M_FOR_SUPER, search_start);
695 path = btrfs_alloc_path();
699 key.objectid = device->devid;
700 key.offset = root->fs_info->alloc_start;
701 key.type = BTRFS_DEV_EXTENT_KEY;
704 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
707 ret = btrfs_previous_item(root, path, 0, key.type);
713 slot = path->slots[0];
714 if (slot >= btrfs_header_nritems(l)) {
715 ret = btrfs_next_leaf(root, path);
722 btrfs_item_key_to_cpu(l, &key, slot);
724 if (key.objectid < device->devid)
726 if (key.objectid > device->devid)
728 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
730 if (key.offset > search_end)
732 if (key.offset > search_start)
733 free_bytes += key.offset - search_start;
735 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
736 extent_end = key.offset + btrfs_dev_extent_length(l,
738 if (extent_end > search_start)
739 search_start = extent_end;
740 if (search_start > search_end)
747 if (search_start < search_end)
748 free_bytes += search_end - search_start;
750 *avail_bytes = free_bytes;
753 btrfs_free_path(path);
757 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
758 struct btrfs_root *extent_root, u64 *start,
759 u64 *num_bytes, u64 type)
762 struct btrfs_fs_info *info = extent_root->fs_info;
763 struct btrfs_root *chunk_root = info->chunk_root;
764 struct btrfs_stripe *stripes;
765 struct btrfs_device *device = NULL;
766 struct btrfs_chunk *chunk;
767 struct list_head private_devs;
768 struct list_head *dev_list = &info->fs_devices->devices;
769 struct list_head *cur;
770 struct map_lookup *map;
771 int min_stripe_size = 1 * 1024 * 1024;
772 u64 calc_size = 8 * 1024 * 1024;
774 u64 max_chunk_size = 4 * calc_size;
784 int stripe_len = BTRFS_STRIPE_LEN;
785 struct btrfs_key key;
788 if (list_empty(dev_list)) {
792 if (type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
793 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
794 BTRFS_BLOCK_GROUP_RAID10 |
795 BTRFS_BLOCK_GROUP_DUP)) {
796 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
797 calc_size = 8 * 1024 * 1024;
798 max_chunk_size = calc_size * 2;
799 min_stripe_size = 1 * 1024 * 1024;
800 } else if (type & BTRFS_BLOCK_GROUP_DATA) {
801 calc_size = 1024 * 1024 * 1024;
802 max_chunk_size = 10 * calc_size;
803 min_stripe_size = 64 * 1024 * 1024;
804 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
805 calc_size = 1024 * 1024 * 1024;
806 max_chunk_size = 4 * calc_size;
807 min_stripe_size = 32 * 1024 * 1024;
810 if (type & BTRFS_BLOCK_GROUP_RAID1) {
811 num_stripes = min_t(u64, 2,
812 btrfs_super_num_devices(info->super_copy));
817 if (type & BTRFS_BLOCK_GROUP_DUP) {
821 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
822 num_stripes = btrfs_super_num_devices(info->super_copy);
825 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
826 num_stripes = btrfs_super_num_devices(info->super_copy);
829 num_stripes &= ~(u32)1;
833 if (type & (BTRFS_BLOCK_GROUP_RAID5)) {
834 num_stripes = btrfs_super_num_devices(info->super_copy);
838 stripe_len = find_raid56_stripe_len(num_stripes - 1,
839 btrfs_super_stripesize(info->super_copy));
841 if (type & (BTRFS_BLOCK_GROUP_RAID6)) {
842 num_stripes = btrfs_super_num_devices(info->super_copy);
846 stripe_len = find_raid56_stripe_len(num_stripes - 2,
847 btrfs_super_stripesize(info->super_copy));
850 /* we don't want a chunk larger than 10% of the FS */
851 percent_max = div_factor(btrfs_super_total_bytes(info->super_copy), 1);
852 max_chunk_size = min(percent_max, max_chunk_size);
855 if (chunk_bytes_by_type(type, calc_size, num_stripes, sub_stripes) >
857 calc_size = max_chunk_size;
858 calc_size /= num_stripes;
859 calc_size /= stripe_len;
860 calc_size *= stripe_len;
862 /* we don't want tiny stripes */
863 calc_size = max_t(u64, calc_size, min_stripe_size);
865 calc_size /= stripe_len;
866 calc_size *= stripe_len;
867 INIT_LIST_HEAD(&private_devs);
868 cur = dev_list->next;
871 if (type & BTRFS_BLOCK_GROUP_DUP)
872 min_free = calc_size * 2;
874 min_free = calc_size;
876 /* build a private list of devices we will allocate from */
877 while(index < num_stripes) {
878 device = list_entry(cur, struct btrfs_device, dev_list);
879 ret = btrfs_device_avail_bytes(trans, device, &avail);
883 if (avail >= min_free) {
884 list_move_tail(&device->dev_list, &private_devs);
886 if (type & BTRFS_BLOCK_GROUP_DUP)
888 } else if (avail > max_avail)
893 if (index < num_stripes) {
894 list_splice(&private_devs, dev_list);
895 if (index >= min_stripes) {
897 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
898 num_stripes /= sub_stripes;
899 num_stripes *= sub_stripes;
904 if (!looped && max_avail > 0) {
906 calc_size = max_avail;
911 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
915 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
916 key.type = BTRFS_CHUNK_ITEM_KEY;
919 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
923 map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
929 stripes = &chunk->stripe;
930 *num_bytes = chunk_bytes_by_type(type, calc_size,
931 num_stripes, sub_stripes);
933 while(index < num_stripes) {
934 struct btrfs_stripe *stripe;
935 BUG_ON(list_empty(&private_devs));
936 cur = private_devs.next;
937 device = list_entry(cur, struct btrfs_device, dev_list);
939 /* loop over this device again if we're doing a dup group */
940 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
941 (index == num_stripes - 1))
942 list_move_tail(&device->dev_list, dev_list);
944 ret = btrfs_alloc_dev_extent(trans, device,
945 info->chunk_root->root_key.objectid,
946 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
947 calc_size, &dev_offset);
950 device->bytes_used += calc_size;
951 ret = btrfs_update_device(trans, device);
954 map->stripes[index].dev = device;
955 map->stripes[index].physical = dev_offset;
956 stripe = stripes + index;
957 btrfs_set_stack_stripe_devid(stripe, device->devid);
958 btrfs_set_stack_stripe_offset(stripe, dev_offset);
959 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
962 BUG_ON(!list_empty(&private_devs));
964 /* key was set above */
965 btrfs_set_stack_chunk_length(chunk, *num_bytes);
966 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
967 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
968 btrfs_set_stack_chunk_type(chunk, type);
969 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
970 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
971 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
972 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
973 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
974 map->sector_size = extent_root->sectorsize;
975 map->stripe_len = stripe_len;
976 map->io_align = stripe_len;
977 map->io_width = stripe_len;
979 map->num_stripes = num_stripes;
980 map->sub_stripes = sub_stripes;
982 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
983 btrfs_chunk_item_size(num_stripes));
985 *start = key.offset;;
987 map->ce.start = key.offset;
988 map->ce.size = *num_bytes;
990 ret = insert_cache_extent(&info->mapping_tree.cache_tree, &map->ce);
993 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
994 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
995 chunk, btrfs_chunk_item_size(num_stripes));
1003 int btrfs_alloc_data_chunk(struct btrfs_trans_handle *trans,
1004 struct btrfs_root *extent_root, u64 *start,
1005 u64 num_bytes, u64 type)
1008 struct btrfs_fs_info *info = extent_root->fs_info;
1009 struct btrfs_root *chunk_root = info->chunk_root;
1010 struct btrfs_stripe *stripes;
1011 struct btrfs_device *device = NULL;
1012 struct btrfs_chunk *chunk;
1013 struct list_head *dev_list = &info->fs_devices->devices;
1014 struct list_head *cur;
1015 struct map_lookup *map;
1016 u64 calc_size = 8 * 1024 * 1024;
1017 int num_stripes = 1;
1018 int sub_stripes = 0;
1021 int stripe_len = BTRFS_STRIPE_LEN;
1022 struct btrfs_key key;
1024 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1025 key.type = BTRFS_CHUNK_ITEM_KEY;
1026 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1031 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
1035 map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
1041 stripes = &chunk->stripe;
1042 calc_size = num_bytes;
1045 cur = dev_list->next;
1046 device = list_entry(cur, struct btrfs_device, dev_list);
1048 while (index < num_stripes) {
1049 struct btrfs_stripe *stripe;
1051 ret = btrfs_alloc_dev_extent(trans, device,
1052 info->chunk_root->root_key.objectid,
1053 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
1054 calc_size, &dev_offset);
1057 device->bytes_used += calc_size;
1058 ret = btrfs_update_device(trans, device);
1061 map->stripes[index].dev = device;
1062 map->stripes[index].physical = dev_offset;
1063 stripe = stripes + index;
1064 btrfs_set_stack_stripe_devid(stripe, device->devid);
1065 btrfs_set_stack_stripe_offset(stripe, dev_offset);
1066 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
1070 /* key was set above */
1071 btrfs_set_stack_chunk_length(chunk, num_bytes);
1072 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
1073 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
1074 btrfs_set_stack_chunk_type(chunk, type);
1075 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
1076 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
1077 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
1078 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
1079 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
1080 map->sector_size = extent_root->sectorsize;
1081 map->stripe_len = stripe_len;
1082 map->io_align = stripe_len;
1083 map->io_width = stripe_len;
1085 map->num_stripes = num_stripes;
1086 map->sub_stripes = sub_stripes;
1088 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
1089 btrfs_chunk_item_size(num_stripes));
1091 *start = key.offset;
1093 map->ce.start = key.offset;
1094 map->ce.size = num_bytes;
1096 ret = insert_cache_extent(&info->mapping_tree.cache_tree, &map->ce);
1103 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
1105 struct cache_extent *ce;
1106 struct map_lookup *map;
1109 ce = search_cache_extent(&map_tree->cache_tree, logical);
1111 BUG_ON(ce->start > logical || ce->start + ce->size < logical);
1112 map = container_of(ce, struct map_lookup, ce);
1114 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
1115 ret = map->num_stripes;
1116 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1117 ret = map->sub_stripes;
1118 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
1120 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
1127 int btrfs_next_metadata(struct btrfs_mapping_tree *map_tree, u64 *logical,
1130 struct cache_extent *ce;
1131 struct map_lookup *map;
1133 ce = search_cache_extent(&map_tree->cache_tree, *logical);
1136 ce = next_cache_extent(ce);
1140 map = container_of(ce, struct map_lookup, ce);
1141 if (map->type & BTRFS_BLOCK_GROUP_METADATA) {
1142 *logical = ce->start;
1151 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
1152 u64 chunk_start, u64 physical, u64 devid,
1153 u64 **logical, int *naddrs, int *stripe_len)
1155 struct cache_extent *ce;
1156 struct map_lookup *map;
1164 ce = search_cache_extent(&map_tree->cache_tree, chunk_start);
1166 map = container_of(ce, struct map_lookup, ce);
1169 rmap_len = map->stripe_len;
1170 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1171 length = ce->size / (map->num_stripes / map->sub_stripes);
1172 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
1173 length = ce->size / map->num_stripes;
1174 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
1175 BTRFS_BLOCK_GROUP_RAID6)) {
1176 length = ce->size / nr_data_stripes(map);
1177 rmap_len = map->stripe_len * nr_data_stripes(map);
1180 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
1182 for (i = 0; i < map->num_stripes; i++) {
1183 if (devid && map->stripes[i].dev->devid != devid)
1185 if (map->stripes[i].physical > physical ||
1186 map->stripes[i].physical + length <= physical)
1189 stripe_nr = (physical - map->stripes[i].physical) /
1192 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1193 stripe_nr = (stripe_nr * map->num_stripes + i) /
1195 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
1196 stripe_nr = stripe_nr * map->num_stripes + i;
1197 } /* else if RAID[56], multiply by nr_data_stripes().
1198 * Alternatively, just use rmap_len below instead of
1199 * map->stripe_len */
1201 bytenr = ce->start + stripe_nr * rmap_len;
1202 for (j = 0; j < nr; j++) {
1203 if (buf[j] == bytenr)
1212 *stripe_len = rmap_len;
1217 static inline int parity_smaller(u64 a, u64 b)
1222 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
1223 static void sort_parity_stripes(struct btrfs_multi_bio *bbio, u64 *raid_map)
1225 struct btrfs_bio_stripe s;
1232 for (i = 0; i < bbio->num_stripes - 1; i++) {
1233 if (parity_smaller(raid_map[i], raid_map[i+1])) {
1234 s = bbio->stripes[i];
1236 bbio->stripes[i] = bbio->stripes[i+1];
1237 raid_map[i] = raid_map[i+1];
1238 bbio->stripes[i+1] = s;
1246 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1247 u64 logical, u64 *length,
1248 struct btrfs_multi_bio **multi_ret, int mirror_num,
1251 return __btrfs_map_block(map_tree, rw, logical, length, NULL,
1252 multi_ret, mirror_num, raid_map_ret);
1255 int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1256 u64 logical, u64 *length, u64 *type,
1257 struct btrfs_multi_bio **multi_ret, int mirror_num,
1260 struct cache_extent *ce;
1261 struct map_lookup *map;
1265 u64 *raid_map = NULL;
1266 int stripes_allocated = 8;
1267 int stripes_required = 1;
1270 struct btrfs_multi_bio *multi = NULL;
1272 if (multi_ret && rw == READ) {
1273 stripes_allocated = 1;
1276 ce = search_cache_extent(&map_tree->cache_tree, logical);
1281 if (ce->start > logical || ce->start + ce->size < logical) {
1287 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
1292 map = container_of(ce, struct map_lookup, ce);
1293 offset = logical - ce->start;
1296 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
1297 BTRFS_BLOCK_GROUP_DUP)) {
1298 stripes_required = map->num_stripes;
1299 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1300 stripes_required = map->sub_stripes;
1303 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)
1304 && multi_ret && ((rw & WRITE) || mirror_num > 1) && raid_map_ret) {
1305 /* RAID[56] write or recovery. Return all stripes */
1306 stripes_required = map->num_stripes;
1308 /* Only allocate the map if we've already got a large enough multi_ret */
1309 if (stripes_allocated >= stripes_required) {
1310 raid_map = kmalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
1318 /* if our multi bio struct is too small, back off and try again */
1319 if (multi_ret && stripes_allocated < stripes_required) {
1320 stripes_allocated = stripes_required;
1327 * stripe_nr counts the total number of stripes we have to stride
1328 * to get to this block
1330 stripe_nr = stripe_nr / map->stripe_len;
1332 stripe_offset = stripe_nr * map->stripe_len;
1333 BUG_ON(offset < stripe_offset);
1335 /* stripe_offset is the offset of this block in its stripe*/
1336 stripe_offset = offset - stripe_offset;
1338 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
1339 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
1340 BTRFS_BLOCK_GROUP_RAID10 |
1341 BTRFS_BLOCK_GROUP_DUP)) {
1342 /* we limit the length of each bio to what fits in a stripe */
1343 *length = min_t(u64, ce->size - offset,
1344 map->stripe_len - stripe_offset);
1346 *length = ce->size - offset;
1352 multi->num_stripes = 1;
1354 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
1356 multi->num_stripes = map->num_stripes;
1357 else if (mirror_num)
1358 stripe_index = mirror_num - 1;
1360 stripe_index = stripe_nr % map->num_stripes;
1361 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1362 int factor = map->num_stripes / map->sub_stripes;
1364 stripe_index = stripe_nr % factor;
1365 stripe_index *= map->sub_stripes;
1368 multi->num_stripes = map->sub_stripes;
1369 else if (mirror_num)
1370 stripe_index += mirror_num - 1;
1372 stripe_nr = stripe_nr / factor;
1373 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
1375 multi->num_stripes = map->num_stripes;
1376 else if (mirror_num)
1377 stripe_index = mirror_num - 1;
1378 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
1379 BTRFS_BLOCK_GROUP_RAID6)) {
1384 u64 raid56_full_stripe_start;
1385 u64 full_stripe_len = nr_data_stripes(map) * map->stripe_len;
1388 * align the start of our data stripe in the logical
1391 raid56_full_stripe_start = offset / full_stripe_len;
1392 raid56_full_stripe_start *= full_stripe_len;
1394 /* get the data stripe number */
1395 stripe_nr = raid56_full_stripe_start / map->stripe_len;
1396 stripe_nr = stripe_nr / nr_data_stripes(map);
1398 /* Work out the disk rotation on this stripe-set */
1399 rot = stripe_nr % map->num_stripes;
1401 /* Fill in the logical address of each stripe */
1402 tmp = stripe_nr * nr_data_stripes(map);
1404 for (i = 0; i < nr_data_stripes(map); i++)
1405 raid_map[(i+rot) % map->num_stripes] =
1406 ce->start + (tmp + i) * map->stripe_len;
1408 raid_map[(i+rot) % map->num_stripes] = BTRFS_RAID5_P_STRIPE;
1409 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
1410 raid_map[(i+rot+1) % map->num_stripes] = BTRFS_RAID6_Q_STRIPE;
1412 *length = map->stripe_len;
1415 multi->num_stripes = map->num_stripes;
1417 stripe_index = stripe_nr % nr_data_stripes(map);
1418 stripe_nr = stripe_nr / nr_data_stripes(map);
1421 * Mirror #0 or #1 means the original data block.
1422 * Mirror #2 is RAID5 parity block.
1423 * Mirror #3 is RAID6 Q block.
1426 stripe_index = nr_data_stripes(map) + mirror_num - 2;
1428 /* We distribute the parity blocks across stripes */
1429 stripe_index = (stripe_nr + stripe_index) % map->num_stripes;
1433 * after this do_div call, stripe_nr is the number of stripes
1434 * on this device we have to walk to find the data, and
1435 * stripe_index is the number of our device in the stripe array
1437 stripe_index = stripe_nr % map->num_stripes;
1438 stripe_nr = stripe_nr / map->num_stripes;
1440 BUG_ON(stripe_index >= map->num_stripes);
1442 for (i = 0; i < multi->num_stripes; i++) {
1443 multi->stripes[i].physical =
1444 map->stripes[stripe_index].physical + stripe_offset +
1445 stripe_nr * map->stripe_len;
1446 multi->stripes[i].dev = map->stripes[stripe_index].dev;
1455 sort_parity_stripes(multi, raid_map);
1456 *raid_map_ret = raid_map;
1462 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
1465 struct btrfs_device *device;
1466 struct btrfs_fs_devices *cur_devices;
1468 cur_devices = root->fs_info->fs_devices;
1469 while (cur_devices) {
1471 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
1472 device = __find_device(&cur_devices->devices,
1477 cur_devices = cur_devices->seed;
1482 struct btrfs_device *
1483 btrfs_find_device_by_devid(struct btrfs_fs_devices *fs_devices,
1484 u64 devid, int instance)
1486 struct list_head *head = &fs_devices->devices;
1487 struct btrfs_device *dev;
1490 list_for_each_entry(dev, head, dev_list) {
1491 if (dev->devid == devid && num_found++ == instance)
1497 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
1499 struct cache_extent *ce;
1500 struct map_lookup *map;
1501 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1506 * During chunk recovering, we may fail to find block group's
1507 * corresponding chunk, we will rebuild it later
1509 ce = search_cache_extent(&map_tree->cache_tree, chunk_offset);
1510 if (!root->fs_info->is_chunk_recover)
1515 map = container_of(ce, struct map_lookup, ce);
1516 for (i = 0; i < map->num_stripes; i++) {
1517 if (!map->stripes[i].dev->writeable) {
1526 static struct btrfs_device *fill_missing_device(u64 devid)
1528 struct btrfs_device *device;
1530 device = kzalloc(sizeof(*device), GFP_NOFS);
1531 device->devid = devid;
1536 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
1537 struct extent_buffer *leaf,
1538 struct btrfs_chunk *chunk)
1540 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1541 struct map_lookup *map;
1542 struct cache_extent *ce;
1546 u8 uuid[BTRFS_UUID_SIZE];
1551 logical = key->offset;
1552 length = btrfs_chunk_length(leaf, chunk);
1554 ce = search_cache_extent(&map_tree->cache_tree, logical);
1556 /* already mapped? */
1557 if (ce && ce->start <= logical && ce->start + ce->size > logical) {
1561 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1562 map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
1566 map->ce.start = logical;
1567 map->ce.size = length;
1568 map->num_stripes = num_stripes;
1569 map->io_width = btrfs_chunk_io_width(leaf, chunk);
1570 map->io_align = btrfs_chunk_io_align(leaf, chunk);
1571 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1572 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1573 map->type = btrfs_chunk_type(leaf, chunk);
1574 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
1576 for (i = 0; i < num_stripes; i++) {
1577 map->stripes[i].physical =
1578 btrfs_stripe_offset_nr(leaf, chunk, i);
1579 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
1580 read_extent_buffer(leaf, uuid, (unsigned long)
1581 btrfs_stripe_dev_uuid_nr(chunk, i),
1583 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
1585 if (!map->stripes[i].dev) {
1586 map->stripes[i].dev = fill_missing_device(devid);
1587 printf("warning, device %llu is missing\n",
1588 (unsigned long long)devid);
1592 ret = insert_cache_extent(&map_tree->cache_tree, &map->ce);
1598 static int fill_device_from_item(struct extent_buffer *leaf,
1599 struct btrfs_dev_item *dev_item,
1600 struct btrfs_device *device)
1604 device->devid = btrfs_device_id(leaf, dev_item);
1605 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1606 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1607 device->type = btrfs_device_type(leaf, dev_item);
1608 device->io_align = btrfs_device_io_align(leaf, dev_item);
1609 device->io_width = btrfs_device_io_width(leaf, dev_item);
1610 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
1612 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1613 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1618 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
1620 struct btrfs_fs_devices *fs_devices;
1623 fs_devices = root->fs_info->fs_devices->seed;
1624 while (fs_devices) {
1625 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
1629 fs_devices = fs_devices->seed;
1632 fs_devices = find_fsid(fsid);
1638 ret = btrfs_open_devices(fs_devices, O_RDONLY);
1642 fs_devices->seed = root->fs_info->fs_devices->seed;
1643 root->fs_info->fs_devices->seed = fs_devices;
1648 static int read_one_dev(struct btrfs_root *root,
1649 struct extent_buffer *leaf,
1650 struct btrfs_dev_item *dev_item)
1652 struct btrfs_device *device;
1655 u8 fs_uuid[BTRFS_UUID_SIZE];
1656 u8 dev_uuid[BTRFS_UUID_SIZE];
1658 devid = btrfs_device_id(leaf, dev_item);
1659 read_extent_buffer(leaf, dev_uuid,
1660 (unsigned long)btrfs_device_uuid(dev_item),
1662 read_extent_buffer(leaf, fs_uuid,
1663 (unsigned long)btrfs_device_fsid(dev_item),
1666 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
1667 ret = open_seed_devices(root, fs_uuid);
1672 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1674 printk("warning devid %llu not found already\n",
1675 (unsigned long long)devid);
1676 device = kzalloc(sizeof(*device), GFP_NOFS);
1680 list_add(&device->dev_list,
1681 &root->fs_info->fs_devices->devices);
1684 fill_device_from_item(leaf, dev_item, device);
1685 device->dev_root = root->fs_info->dev_root;
1689 int btrfs_read_sys_array(struct btrfs_root *root)
1691 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
1692 struct extent_buffer *sb;
1693 struct btrfs_disk_key *disk_key;
1694 struct btrfs_chunk *chunk;
1695 struct btrfs_key key;
1702 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
1703 BTRFS_SUPER_INFO_SIZE);
1706 btrfs_set_buffer_uptodate(sb);
1707 write_extent_buffer(sb, super_copy, 0, sizeof(*super_copy));
1708 array_end = ((u8 *)super_copy->sys_chunk_array) +
1709 btrfs_super_sys_array_size(super_copy);
1712 * we do this loop twice, once for the device items and
1713 * once for all of the chunks. This way there are device
1714 * structs filled in for every chunk
1716 ptr = super_copy->sys_chunk_array;
1718 while (ptr < array_end) {
1719 disk_key = (struct btrfs_disk_key *)ptr;
1720 btrfs_disk_key_to_cpu(&key, disk_key);
1722 len = sizeof(*disk_key);
1725 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1726 chunk = (struct btrfs_chunk *)(ptr - (u8 *)super_copy);
1727 ret = read_one_chunk(root, &key, sb, chunk);
1730 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
1731 len = btrfs_chunk_item_size(num_stripes);
1737 free_extent_buffer(sb);
1741 int btrfs_read_chunk_tree(struct btrfs_root *root)
1743 struct btrfs_path *path;
1744 struct extent_buffer *leaf;
1745 struct btrfs_key key;
1746 struct btrfs_key found_key;
1750 root = root->fs_info->chunk_root;
1752 path = btrfs_alloc_path();
1757 * Read all device items, and then all the chunk items. All
1758 * device items are found before any chunk item (their object id
1759 * is smaller than the lowest possible object id for a chunk
1760 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
1762 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1765 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1769 leaf = path->nodes[0];
1770 slot = path->slots[0];
1771 if (slot >= btrfs_header_nritems(leaf)) {
1772 ret = btrfs_next_leaf(root, path);
1779 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1780 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
1781 struct btrfs_dev_item *dev_item;
1782 dev_item = btrfs_item_ptr(leaf, slot,
1783 struct btrfs_dev_item);
1784 ret = read_one_dev(root, leaf, dev_item);
1786 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
1787 struct btrfs_chunk *chunk;
1788 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1789 ret = read_one_chunk(root, &found_key, leaf, chunk);
1797 btrfs_free_path(path);
1801 struct list_head *btrfs_scanned_uuids(void)
1806 static int rmw_eb(struct btrfs_fs_info *info,
1807 struct extent_buffer *eb, struct extent_buffer *orig_eb)
1810 unsigned long orig_off = 0;
1811 unsigned long dest_off = 0;
1812 unsigned long copy_len = eb->len;
1814 ret = read_whole_eb(info, eb, 0);
1818 if (eb->start + eb->len <= orig_eb->start ||
1819 eb->start >= orig_eb->start + orig_eb->len)
1822 * | ----- orig_eb ------- |
1823 * | ----- stripe ------- |
1824 * | ----- orig_eb ------- |
1825 * | ----- orig_eb ------- |
1827 if (eb->start > orig_eb->start)
1828 orig_off = eb->start - orig_eb->start;
1829 if (orig_eb->start > eb->start)
1830 dest_off = orig_eb->start - eb->start;
1832 if (copy_len > orig_eb->len - orig_off)
1833 copy_len = orig_eb->len - orig_off;
1834 if (copy_len > eb->len - dest_off)
1835 copy_len = eb->len - dest_off;
1837 memcpy(eb->data + dest_off, orig_eb->data + orig_off, copy_len);
1841 static void split_eb_for_raid56(struct btrfs_fs_info *info,
1842 struct extent_buffer *orig_eb,
1843 struct extent_buffer **ebs,
1844 u64 stripe_len, u64 *raid_map,
1847 struct extent_buffer *eb;
1848 u64 start = orig_eb->start;
1853 for (i = 0; i < num_stripes; i++) {
1854 if (raid_map[i] >= BTRFS_RAID5_P_STRIPE)
1857 eb = malloc(sizeof(struct extent_buffer) + stripe_len);
1860 memset(eb, 0, sizeof(struct extent_buffer) + stripe_len);
1862 eb->start = raid_map[i];
1863 eb->len = stripe_len;
1867 eb->dev_bytenr = (u64)-1;
1869 this_eb_start = raid_map[i];
1871 if (start > this_eb_start ||
1872 start + orig_eb->len < this_eb_start + stripe_len) {
1873 ret = rmw_eb(info, eb, orig_eb);
1876 memcpy(eb->data, orig_eb->data + eb->start - start, stripe_len);
1882 int write_raid56_with_parity(struct btrfs_fs_info *info,
1883 struct extent_buffer *eb,
1884 struct btrfs_multi_bio *multi,
1885 u64 stripe_len, u64 *raid_map)
1887 struct extent_buffer **ebs, *p_eb = NULL, *q_eb = NULL;
1891 int alloc_size = eb->len;
1893 ebs = kmalloc(sizeof(*ebs) * multi->num_stripes, GFP_NOFS);
1896 if (stripe_len > alloc_size)
1897 alloc_size = stripe_len;
1899 split_eb_for_raid56(info, eb, ebs, stripe_len, raid_map,
1900 multi->num_stripes);
1902 for (i = 0; i < multi->num_stripes; i++) {
1903 struct extent_buffer *new_eb;
1904 if (raid_map[i] < BTRFS_RAID5_P_STRIPE) {
1905 ebs[i]->dev_bytenr = multi->stripes[i].physical;
1906 ebs[i]->fd = multi->stripes[i].dev->fd;
1907 multi->stripes[i].dev->total_ios++;
1908 BUG_ON(ebs[i]->start != raid_map[i]);
1911 new_eb = kmalloc(sizeof(*eb) + alloc_size, GFP_NOFS);
1913 new_eb->dev_bytenr = multi->stripes[i].physical;
1914 new_eb->fd = multi->stripes[i].dev->fd;
1915 multi->stripes[i].dev->total_ios++;
1916 new_eb->len = stripe_len;
1918 if (raid_map[i] == BTRFS_RAID5_P_STRIPE)
1920 else if (raid_map[i] == BTRFS_RAID6_Q_STRIPE)
1926 pointers = kmalloc(sizeof(*pointers) * multi->num_stripes,
1930 ebs[multi->num_stripes - 2] = p_eb;
1931 ebs[multi->num_stripes - 1] = q_eb;
1933 for (i = 0; i < multi->num_stripes; i++)
1934 pointers[i] = ebs[i]->data;
1936 raid6_gen_syndrome(multi->num_stripes, stripe_len, pointers);
1939 ebs[multi->num_stripes - 1] = p_eb;
1940 memcpy(p_eb->data, ebs[0]->data, stripe_len);
1941 for (j = 1; j < multi->num_stripes - 1; j++) {
1942 for (i = 0; i < stripe_len; i += sizeof(unsigned long)) {
1943 *(unsigned long *)(p_eb->data + i) ^=
1944 *(unsigned long *)(ebs[j]->data + i);
1949 for (i = 0; i < multi->num_stripes; i++) {
1950 ret = write_extent_to_disk(ebs[i]);