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"
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 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
56 (sizeof(struct btrfs_bio_stripe) * (n)))
58 static LIST_HEAD(fs_uuids);
60 static struct btrfs_device *__find_device(struct list_head *head, u64 devid,
63 struct btrfs_device *dev;
64 struct list_head *cur;
66 list_for_each(cur, head) {
67 dev = list_entry(cur, struct btrfs_device, dev_list);
68 if (dev->devid == devid &&
69 !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE)) {
76 static struct btrfs_fs_devices *find_fsid(u8 *fsid)
78 struct list_head *cur;
79 struct btrfs_fs_devices *fs_devices;
81 list_for_each(cur, &fs_uuids) {
82 fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
83 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
89 static int device_list_add(const char *path,
90 struct btrfs_super_block *disk_super,
91 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
93 struct btrfs_device *device;
94 struct btrfs_fs_devices *fs_devices;
95 u64 found_transid = btrfs_super_generation(disk_super);
97 fs_devices = find_fsid(disk_super->fsid);
99 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
102 INIT_LIST_HEAD(&fs_devices->devices);
103 list_add(&fs_devices->list, &fs_uuids);
104 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
105 fs_devices->latest_devid = devid;
106 fs_devices->latest_trans = found_transid;
107 fs_devices->lowest_devid = (u64)-1;
110 device = __find_device(&fs_devices->devices, devid,
111 disk_super->dev_item.uuid);
114 device = kzalloc(sizeof(*device), GFP_NOFS);
116 /* we can safely leave the fs_devices entry around */
119 device->devid = devid;
120 memcpy(device->uuid, disk_super->dev_item.uuid,
122 device->name = kstrdup(path, GFP_NOFS);
127 device->label = kstrdup(disk_super->label, GFP_NOFS);
128 device->total_devs = btrfs_super_num_devices(disk_super);
129 device->super_bytes_used = btrfs_super_bytes_used(disk_super);
130 device->total_bytes =
131 btrfs_stack_device_total_bytes(&disk_super->dev_item);
133 btrfs_stack_device_bytes_used(&disk_super->dev_item);
134 list_add(&device->dev_list, &fs_devices->devices);
135 device->fs_devices = fs_devices;
136 } else if (!device->name || strcmp(device->name, path)) {
137 char *name = strdup(path);
145 if (found_transid > fs_devices->latest_trans) {
146 fs_devices->latest_devid = devid;
147 fs_devices->latest_trans = found_transid;
149 if (fs_devices->lowest_devid > devid) {
150 fs_devices->lowest_devid = devid;
152 *fs_devices_ret = fs_devices;
156 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
158 struct btrfs_fs_devices *seed_devices;
159 struct list_head *cur;
160 struct btrfs_device *device;
162 list_for_each(cur, &fs_devices->devices) {
163 device = list_entry(cur, struct btrfs_device, dev_list);
166 device->writeable = 0;
169 seed_devices = fs_devices->seed;
170 fs_devices->seed = NULL;
172 fs_devices = seed_devices;
179 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, int flags)
182 struct list_head *head = &fs_devices->devices;
183 struct list_head *cur;
184 struct btrfs_device *device;
187 list_for_each(cur, head) {
188 device = list_entry(cur, struct btrfs_device, dev_list);
190 printk("no name for device %llu, skip it now\n", device->devid);
194 fd = open(device->name, flags);
200 if (posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED))
201 fprintf(stderr, "Warning, could not drop caches\n");
203 if (device->devid == fs_devices->latest_devid)
204 fs_devices->latest_bdev = fd;
205 if (device->devid == fs_devices->lowest_devid)
206 fs_devices->lowest_bdev = fd;
209 device->writeable = 1;
213 btrfs_close_devices(fs_devices);
217 int btrfs_scan_one_device(int fd, const char *path,
218 struct btrfs_fs_devices **fs_devices_ret,
219 u64 *total_devs, u64 super_offset)
221 struct btrfs_super_block *disk_super;
232 disk_super = (struct btrfs_super_block *)buf;
233 ret = btrfs_read_dev_super(fd, disk_super, super_offset);
238 devid = le64_to_cpu(disk_super->dev_item.devid);
239 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)
242 *total_devs = btrfs_super_num_devices(disk_super);
243 uuid_unparse(disk_super->fsid, uuidbuf);
245 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
254 * this uses a pretty simple search, the expectation is that it is
255 * called very infrequently and that a given device has a small number
258 static int find_free_dev_extent(struct btrfs_trans_handle *trans,
259 struct btrfs_device *device,
260 struct btrfs_path *path,
261 u64 num_bytes, u64 *start)
263 struct btrfs_key key;
264 struct btrfs_root *root = device->dev_root;
265 struct btrfs_dev_extent *dev_extent = NULL;
268 u64 search_start = 0;
269 u64 search_end = device->total_bytes;
273 struct extent_buffer *l;
278 /* FIXME use last free of some kind */
280 /* we don't want to overwrite the superblock on the drive,
281 * so we make sure to start at an offset of at least 1MB
283 search_start = max((u64)1024 * 1024, search_start);
285 if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
286 search_start = max(root->fs_info->alloc_start, search_start);
288 key.objectid = device->devid;
289 key.offset = search_start;
290 key.type = BTRFS_DEV_EXTENT_KEY;
291 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
294 ret = btrfs_previous_item(root, path, 0, key.type);
298 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
301 slot = path->slots[0];
302 if (slot >= btrfs_header_nritems(l)) {
303 ret = btrfs_next_leaf(root, path);
310 if (search_start >= search_end) {
314 *start = search_start;
318 *start = last_byte > search_start ?
319 last_byte : search_start;
320 if (search_end <= *start) {
326 btrfs_item_key_to_cpu(l, &key, slot);
328 if (key.objectid < device->devid)
331 if (key.objectid > device->devid)
334 if (key.offset >= search_start && key.offset > last_byte &&
336 if (last_byte < search_start)
337 last_byte = search_start;
338 hole_size = key.offset - last_byte;
339 if (key.offset > last_byte &&
340 hole_size >= num_bytes) {
345 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
350 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
351 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
357 /* we have to make sure we didn't find an extent that has already
358 * been allocated by the map tree or the original allocation
360 btrfs_release_path(root, path);
361 BUG_ON(*start < search_start);
363 if (*start + num_bytes > search_end) {
367 /* check for pending inserts here */
371 btrfs_release_path(root, path);
375 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
376 struct btrfs_device *device,
377 u64 chunk_tree, u64 chunk_objectid,
379 u64 num_bytes, u64 *start)
382 struct btrfs_path *path;
383 struct btrfs_root *root = device->dev_root;
384 struct btrfs_dev_extent *extent;
385 struct extent_buffer *leaf;
386 struct btrfs_key key;
388 path = btrfs_alloc_path();
392 ret = find_free_dev_extent(trans, device, path, num_bytes, start);
397 key.objectid = device->devid;
399 key.type = BTRFS_DEV_EXTENT_KEY;
400 ret = btrfs_insert_empty_item(trans, root, path, &key,
404 leaf = path->nodes[0];
405 extent = btrfs_item_ptr(leaf, path->slots[0],
406 struct btrfs_dev_extent);
407 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
408 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
409 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
411 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
412 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
415 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
416 btrfs_mark_buffer_dirty(leaf);
418 btrfs_free_path(path);
422 static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset)
424 struct btrfs_path *path;
426 struct btrfs_key key;
427 struct btrfs_chunk *chunk;
428 struct btrfs_key found_key;
430 path = btrfs_alloc_path();
433 key.objectid = objectid;
434 key.offset = (u64)-1;
435 key.type = BTRFS_CHUNK_ITEM_KEY;
437 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
443 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
447 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
449 if (found_key.objectid != objectid)
452 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
454 *offset = found_key.offset +
455 btrfs_chunk_length(path->nodes[0], chunk);
460 btrfs_free_path(path);
464 static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
468 struct btrfs_key key;
469 struct btrfs_key found_key;
471 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
472 key.type = BTRFS_DEV_ITEM_KEY;
473 key.offset = (u64)-1;
475 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
481 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
486 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
488 *objectid = found_key.offset + 1;
492 btrfs_release_path(root, path);
497 * the device information is stored in the chunk root
498 * the btrfs_device struct should be fully filled in
500 int btrfs_add_device(struct btrfs_trans_handle *trans,
501 struct btrfs_root *root,
502 struct btrfs_device *device)
505 struct btrfs_path *path;
506 struct btrfs_dev_item *dev_item;
507 struct extent_buffer *leaf;
508 struct btrfs_key key;
512 root = root->fs_info->chunk_root;
514 path = btrfs_alloc_path();
518 ret = find_next_devid(root, path, &free_devid);
522 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
523 key.type = BTRFS_DEV_ITEM_KEY;
524 key.offset = free_devid;
526 ret = btrfs_insert_empty_item(trans, root, path, &key,
531 leaf = path->nodes[0];
532 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
534 device->devid = free_devid;
535 btrfs_set_device_id(leaf, dev_item, device->devid);
536 btrfs_set_device_generation(leaf, dev_item, 0);
537 btrfs_set_device_type(leaf, dev_item, device->type);
538 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
539 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
540 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
541 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
542 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
543 btrfs_set_device_group(leaf, dev_item, 0);
544 btrfs_set_device_seek_speed(leaf, dev_item, 0);
545 btrfs_set_device_bandwidth(leaf, dev_item, 0);
546 btrfs_set_device_start_offset(leaf, dev_item, 0);
548 ptr = (unsigned long)btrfs_device_uuid(dev_item);
549 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
550 ptr = (unsigned long)btrfs_device_fsid(dev_item);
551 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
552 btrfs_mark_buffer_dirty(leaf);
556 btrfs_free_path(path);
560 int btrfs_update_device(struct btrfs_trans_handle *trans,
561 struct btrfs_device *device)
564 struct btrfs_path *path;
565 struct btrfs_root *root;
566 struct btrfs_dev_item *dev_item;
567 struct extent_buffer *leaf;
568 struct btrfs_key key;
570 root = device->dev_root->fs_info->chunk_root;
572 path = btrfs_alloc_path();
576 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
577 key.type = BTRFS_DEV_ITEM_KEY;
578 key.offset = device->devid;
580 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
589 leaf = path->nodes[0];
590 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
592 btrfs_set_device_id(leaf, dev_item, device->devid);
593 btrfs_set_device_type(leaf, dev_item, device->type);
594 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
595 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
596 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
597 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
598 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
599 btrfs_mark_buffer_dirty(leaf);
602 btrfs_free_path(path);
606 int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
607 struct btrfs_root *root,
608 struct btrfs_key *key,
609 struct btrfs_chunk *chunk, int item_size)
611 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
612 struct btrfs_disk_key disk_key;
616 array_size = btrfs_super_sys_array_size(super_copy);
617 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
620 ptr = super_copy->sys_chunk_array + array_size;
621 btrfs_cpu_key_to_disk(&disk_key, key);
622 memcpy(ptr, &disk_key, sizeof(disk_key));
623 ptr += sizeof(disk_key);
624 memcpy(ptr, chunk, item_size);
625 item_size += sizeof(disk_key);
626 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
630 static u64 div_factor(u64 num, int factor)
638 static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes,
641 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
643 else if (type & BTRFS_BLOCK_GROUP_RAID10)
644 return calc_size * (num_stripes / sub_stripes);
645 else if (type & BTRFS_BLOCK_GROUP_RAID5)
646 return calc_size * (num_stripes - 1);
647 else if (type & BTRFS_BLOCK_GROUP_RAID6)
648 return calc_size * (num_stripes - 2);
650 return calc_size * num_stripes;
654 static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
656 /* TODO, add a way to store the preferred stripe size */
660 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
661 struct btrfs_root *extent_root, u64 *start,
662 u64 *num_bytes, u64 type)
665 struct btrfs_fs_info *info = extent_root->fs_info;
666 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
667 struct btrfs_stripe *stripes;
668 struct btrfs_device *device = NULL;
669 struct btrfs_chunk *chunk;
670 struct list_head private_devs;
671 struct list_head *dev_list = &extent_root->fs_info->fs_devices->devices;
672 struct list_head *cur;
673 struct map_lookup *map;
674 int min_stripe_size = 1 * 1024 * 1024;
675 u64 calc_size = 8 * 1024 * 1024;
677 u64 max_chunk_size = 4 * calc_size;
687 int stripe_len = 64 * 1024;
688 struct btrfs_key key;
691 if (list_empty(dev_list)) {
695 if (type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
696 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
697 BTRFS_BLOCK_GROUP_RAID10 |
698 BTRFS_BLOCK_GROUP_DUP)) {
699 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
700 calc_size = 8 * 1024 * 1024;
701 max_chunk_size = calc_size * 2;
702 min_stripe_size = 1 * 1024 * 1024;
703 } else if (type & BTRFS_BLOCK_GROUP_DATA) {
704 calc_size = 1024 * 1024 * 1024;
705 max_chunk_size = 10 * calc_size;
706 min_stripe_size = 64 * 1024 * 1024;
707 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
708 calc_size = 1024 * 1024 * 1024;
709 max_chunk_size = 4 * calc_size;
710 min_stripe_size = 32 * 1024 * 1024;
713 if (type & BTRFS_BLOCK_GROUP_RAID1) {
714 num_stripes = min_t(u64, 2,
715 btrfs_super_num_devices(info->super_copy));
720 if (type & BTRFS_BLOCK_GROUP_DUP) {
724 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
725 num_stripes = btrfs_super_num_devices(info->super_copy);
728 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
729 num_stripes = btrfs_super_num_devices(info->super_copy);
732 num_stripes &= ~(u32)1;
736 if (type & (BTRFS_BLOCK_GROUP_RAID5)) {
737 num_stripes = btrfs_super_num_devices(info->super_copy);
741 stripe_len = find_raid56_stripe_len(num_stripes - 1,
742 btrfs_super_stripesize(info->super_copy));
744 if (type & (BTRFS_BLOCK_GROUP_RAID6)) {
745 num_stripes = btrfs_super_num_devices(info->super_copy);
749 stripe_len = find_raid56_stripe_len(num_stripes - 2,
750 btrfs_super_stripesize(info->super_copy));
753 /* we don't want a chunk larger than 10% of the FS */
754 percent_max = div_factor(btrfs_super_total_bytes(info->super_copy), 1);
755 max_chunk_size = min(percent_max, max_chunk_size);
758 if (chunk_bytes_by_type(type, calc_size, num_stripes, sub_stripes) >
760 calc_size = max_chunk_size;
761 calc_size /= num_stripes;
762 calc_size /= stripe_len;
763 calc_size *= stripe_len;
765 /* we don't want tiny stripes */
766 calc_size = max_t(u64, calc_size, min_stripe_size);
768 calc_size /= stripe_len;
769 calc_size *= stripe_len;
770 INIT_LIST_HEAD(&private_devs);
771 cur = dev_list->next;
774 if (type & BTRFS_BLOCK_GROUP_DUP)
775 min_free = calc_size * 2;
777 min_free = calc_size;
779 /* build a private list of devices we will allocate from */
780 while(index < num_stripes) {
781 device = list_entry(cur, struct btrfs_device, dev_list);
782 avail = device->total_bytes - device->bytes_used;
784 if (avail >= min_free) {
785 list_move_tail(&device->dev_list, &private_devs);
787 if (type & BTRFS_BLOCK_GROUP_DUP)
789 } else if (avail > max_avail)
794 if (index < num_stripes) {
795 list_splice(&private_devs, dev_list);
796 if (index >= min_stripes) {
798 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
799 num_stripes /= sub_stripes;
800 num_stripes *= sub_stripes;
805 if (!looped && max_avail > 0) {
807 calc_size = max_avail;
812 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
816 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
817 key.type = BTRFS_CHUNK_ITEM_KEY;
820 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
824 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
830 stripes = &chunk->stripe;
831 *num_bytes = chunk_bytes_by_type(type, calc_size,
832 num_stripes, sub_stripes);
834 while(index < num_stripes) {
835 struct btrfs_stripe *stripe;
836 BUG_ON(list_empty(&private_devs));
837 cur = private_devs.next;
838 device = list_entry(cur, struct btrfs_device, dev_list);
840 /* loop over this device again if we're doing a dup group */
841 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
842 (index == num_stripes - 1))
843 list_move_tail(&device->dev_list, dev_list);
845 ret = btrfs_alloc_dev_extent(trans, device,
846 info->chunk_root->root_key.objectid,
847 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
848 calc_size, &dev_offset);
851 device->bytes_used += calc_size;
852 ret = btrfs_update_device(trans, device);
855 map->stripes[index].dev = device;
856 map->stripes[index].physical = dev_offset;
857 stripe = stripes + index;
858 btrfs_set_stack_stripe_devid(stripe, device->devid);
859 btrfs_set_stack_stripe_offset(stripe, dev_offset);
860 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
863 BUG_ON(!list_empty(&private_devs));
865 /* key was set above */
866 btrfs_set_stack_chunk_length(chunk, *num_bytes);
867 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
868 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
869 btrfs_set_stack_chunk_type(chunk, type);
870 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
871 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
872 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
873 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
874 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
875 map->sector_size = extent_root->sectorsize;
876 map->stripe_len = stripe_len;
877 map->io_align = stripe_len;
878 map->io_width = stripe_len;
880 map->num_stripes = num_stripes;
881 map->sub_stripes = sub_stripes;
883 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
884 btrfs_chunk_item_size(num_stripes));
886 *start = key.offset;;
888 map->ce.start = key.offset;
889 map->ce.size = *num_bytes;
891 ret = insert_existing_cache_extent(
892 &extent_root->fs_info->mapping_tree.cache_tree,
896 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
897 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
898 chunk, btrfs_chunk_item_size(num_stripes));
906 int btrfs_alloc_data_chunk(struct btrfs_trans_handle *trans,
907 struct btrfs_root *extent_root, u64 *start,
908 u64 num_bytes, u64 type)
911 struct btrfs_fs_info *info = extent_root->fs_info;
912 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
913 struct btrfs_stripe *stripes;
914 struct btrfs_device *device = NULL;
915 struct btrfs_chunk *chunk;
916 struct list_head *dev_list = &extent_root->fs_info->fs_devices->devices;
917 struct list_head *cur;
918 struct map_lookup *map;
919 u64 calc_size = 8 * 1024 * 1024;
924 int stripe_len = 64 * 1024;
925 struct btrfs_key key;
927 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
928 key.type = BTRFS_CHUNK_ITEM_KEY;
929 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
934 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
938 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
944 stripes = &chunk->stripe;
945 calc_size = num_bytes;
948 cur = dev_list->next;
949 device = list_entry(cur, struct btrfs_device, dev_list);
951 while (index < num_stripes) {
952 struct btrfs_stripe *stripe;
954 ret = btrfs_alloc_dev_extent(trans, device,
955 info->chunk_root->root_key.objectid,
956 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
957 calc_size, &dev_offset);
960 device->bytes_used += calc_size;
961 ret = btrfs_update_device(trans, device);
964 map->stripes[index].dev = device;
965 map->stripes[index].physical = dev_offset;
966 stripe = stripes + index;
967 btrfs_set_stack_stripe_devid(stripe, device->devid);
968 btrfs_set_stack_stripe_offset(stripe, dev_offset);
969 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
973 /* key was set above */
974 btrfs_set_stack_chunk_length(chunk, num_bytes);
975 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
976 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
977 btrfs_set_stack_chunk_type(chunk, type);
978 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
979 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
980 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
981 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
982 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
983 map->sector_size = extent_root->sectorsize;
984 map->stripe_len = stripe_len;
985 map->io_align = stripe_len;
986 map->io_width = stripe_len;
988 map->num_stripes = num_stripes;
989 map->sub_stripes = sub_stripes;
991 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
992 btrfs_chunk_item_size(num_stripes));
996 map->ce.start = key.offset;
997 map->ce.size = num_bytes;
999 ret = insert_existing_cache_extent(
1000 &extent_root->fs_info->mapping_tree.cache_tree,
1008 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
1010 cache_tree_init(&tree->cache_tree);
1013 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
1015 struct cache_extent *ce;
1016 struct map_lookup *map;
1019 ce = find_first_cache_extent(&map_tree->cache_tree, logical);
1021 BUG_ON(ce->start > logical || ce->start + ce->size < logical);
1022 map = container_of(ce, struct map_lookup, ce);
1024 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
1025 ret = map->num_stripes;
1026 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1027 ret = map->sub_stripes;
1028 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
1030 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
1037 int btrfs_next_metadata(struct btrfs_mapping_tree *map_tree, u64 *logical,
1040 struct cache_extent *ce;
1041 struct map_lookup *map;
1043 ce = find_first_cache_extent(&map_tree->cache_tree, *logical);
1046 ce = next_cache_extent(ce);
1050 map = container_of(ce, struct map_lookup, ce);
1051 if (map->type & BTRFS_BLOCK_GROUP_METADATA) {
1052 *logical = ce->start;
1061 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
1062 u64 chunk_start, u64 physical, u64 devid,
1063 u64 **logical, int *naddrs, int *stripe_len)
1065 struct cache_extent *ce;
1066 struct map_lookup *map;
1074 ce = find_first_cache_extent(&map_tree->cache_tree, chunk_start);
1076 map = container_of(ce, struct map_lookup, ce);
1079 rmap_len = map->stripe_len;
1080 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1081 length = ce->size / (map->num_stripes / map->sub_stripes);
1082 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
1083 length = ce->size / map->num_stripes;
1084 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
1085 BTRFS_BLOCK_GROUP_RAID6)) {
1086 length = ce->size / nr_data_stripes(map);
1087 rmap_len = map->stripe_len * nr_data_stripes(map);
1090 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
1092 for (i = 0; i < map->num_stripes; i++) {
1093 if (devid && map->stripes[i].dev->devid != devid)
1095 if (map->stripes[i].physical > physical ||
1096 map->stripes[i].physical + length <= physical)
1099 stripe_nr = (physical - map->stripes[i].physical) /
1102 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1103 stripe_nr = (stripe_nr * map->num_stripes + i) /
1105 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
1106 stripe_nr = stripe_nr * map->num_stripes + i;
1107 } /* else if RAID[56], multiply by nr_data_stripes().
1108 * Alternatively, just use rmap_len below instead of
1109 * map->stripe_len */
1111 bytenr = ce->start + stripe_nr * rmap_len;
1112 for (j = 0; j < nr; j++) {
1113 if (buf[j] == bytenr)
1122 *stripe_len = rmap_len;
1127 static inline int parity_smaller(u64 a, u64 b)
1132 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
1133 static void sort_parity_stripes(struct btrfs_multi_bio *bbio, u64 *raid_map)
1135 struct btrfs_bio_stripe s;
1142 for (i = 0; i < bbio->num_stripes - 1; i++) {
1143 if (parity_smaller(raid_map[i], raid_map[i+1])) {
1144 s = bbio->stripes[i];
1146 bbio->stripes[i] = bbio->stripes[i+1];
1147 raid_map[i] = raid_map[i+1];
1148 bbio->stripes[i+1] = s;
1156 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1157 u64 logical, u64 *length,
1158 struct btrfs_multi_bio **multi_ret, int mirror_num,
1161 return __btrfs_map_block(map_tree, rw, logical, length, NULL,
1162 multi_ret, mirror_num, raid_map_ret);
1165 int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1166 u64 logical, u64 *length, u64 *type,
1167 struct btrfs_multi_bio **multi_ret, int mirror_num,
1170 struct cache_extent *ce;
1171 struct map_lookup *map;
1175 u64 *raid_map = NULL;
1176 int stripes_allocated = 8;
1177 int stripes_required = 1;
1180 struct btrfs_multi_bio *multi = NULL;
1182 if (multi_ret && rw == READ) {
1183 stripes_allocated = 1;
1186 ce = find_first_cache_extent(&map_tree->cache_tree, logical);
1192 if (ce->start > logical || ce->start + ce->size < logical) {
1199 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
1204 map = container_of(ce, struct map_lookup, ce);
1205 offset = logical - ce->start;
1208 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
1209 BTRFS_BLOCK_GROUP_DUP)) {
1210 stripes_required = map->num_stripes;
1211 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1212 stripes_required = map->sub_stripes;
1215 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)
1216 && multi_ret && ((rw & WRITE) || mirror_num > 1) && raid_map_ret) {
1217 /* RAID[56] write or recovery. Return all stripes */
1218 stripes_required = map->num_stripes;
1220 /* Only allocate the map if we've already got a large enough multi_ret */
1221 if (stripes_allocated >= stripes_required) {
1222 raid_map = kmalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
1230 /* if our multi bio struct is too small, back off and try again */
1231 if (multi_ret && stripes_allocated < stripes_required) {
1232 stripes_allocated = stripes_required;
1239 * stripe_nr counts the total number of stripes we have to stride
1240 * to get to this block
1242 stripe_nr = stripe_nr / map->stripe_len;
1244 stripe_offset = stripe_nr * map->stripe_len;
1245 BUG_ON(offset < stripe_offset);
1247 /* stripe_offset is the offset of this block in its stripe*/
1248 stripe_offset = offset - stripe_offset;
1250 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
1251 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
1252 BTRFS_BLOCK_GROUP_RAID10 |
1253 BTRFS_BLOCK_GROUP_DUP)) {
1254 /* we limit the length of each bio to what fits in a stripe */
1255 *length = min_t(u64, ce->size - offset,
1256 map->stripe_len - stripe_offset);
1258 *length = ce->size - offset;
1264 multi->num_stripes = 1;
1266 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
1268 multi->num_stripes = map->num_stripes;
1269 else if (mirror_num)
1270 stripe_index = mirror_num - 1;
1272 stripe_index = stripe_nr % map->num_stripes;
1273 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1274 int factor = map->num_stripes / map->sub_stripes;
1276 stripe_index = stripe_nr % factor;
1277 stripe_index *= map->sub_stripes;
1280 multi->num_stripes = map->sub_stripes;
1281 else if (mirror_num)
1282 stripe_index += mirror_num - 1;
1284 stripe_nr = stripe_nr / factor;
1285 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
1287 multi->num_stripes = map->num_stripes;
1288 else if (mirror_num)
1289 stripe_index = mirror_num - 1;
1290 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
1291 BTRFS_BLOCK_GROUP_RAID6)) {
1296 u64 raid56_full_stripe_start;
1297 u64 full_stripe_len = nr_data_stripes(map) * map->stripe_len;
1300 * align the start of our data stripe in the logical
1303 raid56_full_stripe_start = offset / full_stripe_len;
1304 raid56_full_stripe_start *= full_stripe_len;
1306 /* get the data stripe number */
1307 stripe_nr = raid56_full_stripe_start / map->stripe_len;
1308 stripe_nr = stripe_nr / nr_data_stripes(map);
1310 /* Work out the disk rotation on this stripe-set */
1311 rot = stripe_nr % map->num_stripes;
1313 /* Fill in the logical address of each stripe */
1314 tmp = stripe_nr * nr_data_stripes(map);
1316 for (i = 0; i < nr_data_stripes(map); i++)
1317 raid_map[(i+rot) % map->num_stripes] =
1318 ce->start + (tmp + i) * map->stripe_len;
1320 raid_map[(i+rot) % map->num_stripes] = BTRFS_RAID5_P_STRIPE;
1321 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
1322 raid_map[(i+rot+1) % map->num_stripes] = BTRFS_RAID6_Q_STRIPE;
1324 *length = map->stripe_len;
1327 multi->num_stripes = map->num_stripes;
1329 stripe_index = stripe_nr % nr_data_stripes(map);
1330 stripe_nr = stripe_nr / nr_data_stripes(map);
1333 * Mirror #0 or #1 means the original data block.
1334 * Mirror #2 is RAID5 parity block.
1335 * Mirror #3 is RAID6 Q block.
1338 stripe_index = nr_data_stripes(map) + mirror_num - 2;
1340 /* We distribute the parity blocks across stripes */
1341 stripe_index = (stripe_nr + stripe_index) % map->num_stripes;
1345 * after this do_div call, stripe_nr is the number of stripes
1346 * on this device we have to walk to find the data, and
1347 * stripe_index is the number of our device in the stripe array
1349 stripe_index = stripe_nr % map->num_stripes;
1350 stripe_nr = stripe_nr / map->num_stripes;
1352 BUG_ON(stripe_index >= map->num_stripes);
1354 for (i = 0; i < multi->num_stripes; i++) {
1355 multi->stripes[i].physical =
1356 map->stripes[stripe_index].physical + stripe_offset +
1357 stripe_nr * map->stripe_len;
1358 multi->stripes[i].dev = map->stripes[stripe_index].dev;
1367 sort_parity_stripes(multi, raid_map);
1368 *raid_map_ret = raid_map;
1374 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
1377 struct btrfs_device *device;
1378 struct btrfs_fs_devices *cur_devices;
1380 cur_devices = root->fs_info->fs_devices;
1381 while (cur_devices) {
1383 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
1384 device = __find_device(&cur_devices->devices,
1389 cur_devices = cur_devices->seed;
1394 struct btrfs_device *btrfs_find_device_by_devid(struct btrfs_root *root,
1395 u64 devid, int instance)
1397 struct list_head *head = &root->fs_info->fs_devices->devices;
1398 struct btrfs_device *dev;
1399 struct list_head *cur;
1402 list_for_each(cur, head) {
1403 dev = list_entry(cur, struct btrfs_device, dev_list);
1404 if (dev->devid == devid && num_found++ == instance)
1410 int btrfs_bootstrap_super_map(struct btrfs_mapping_tree *map_tree,
1411 struct btrfs_fs_devices *fs_devices)
1413 struct map_lookup *map;
1414 u64 logical = BTRFS_SUPER_INFO_OFFSET;
1415 u64 length = BTRFS_SUPER_INFO_SIZE;
1416 int num_stripes = 0;
1417 int sub_stripes = 0;
1420 struct list_head *cur;
1422 list_for_each(cur, &fs_devices->devices) {
1425 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1429 map->ce.start = logical;
1430 map->ce.size = length;
1431 map->num_stripes = num_stripes;
1432 map->sub_stripes = sub_stripes;
1433 map->io_width = length;
1434 map->io_align = length;
1435 map->sector_size = length;
1436 map->stripe_len = length;
1437 map->type = BTRFS_BLOCK_GROUP_RAID1;
1440 list_for_each(cur, &fs_devices->devices) {
1441 struct btrfs_device *device = list_entry(cur,
1442 struct btrfs_device,
1444 map->stripes[i].physical = logical;
1445 map->stripes[i].dev = device;
1448 ret = insert_existing_cache_extent(&map_tree->cache_tree, &map->ce);
1449 if (ret == -EEXIST) {
1450 struct cache_extent *old;
1451 struct map_lookup *old_map;
1452 old = find_cache_extent(&map_tree->cache_tree, logical, length);
1453 old_map = container_of(old, struct map_lookup, ce);
1454 remove_cache_extent(&map_tree->cache_tree, old);
1456 ret = insert_existing_cache_extent(&map_tree->cache_tree,
1463 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
1465 struct cache_extent *ce;
1466 struct map_lookup *map;
1467 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1471 ce = find_first_cache_extent(&map_tree->cache_tree, chunk_offset);
1474 map = container_of(ce, struct map_lookup, ce);
1475 for (i = 0; i < map->num_stripes; i++) {
1476 if (!map->stripes[i].dev->writeable) {
1485 static struct btrfs_device *fill_missing_device(u64 devid)
1487 struct btrfs_device *device;
1489 device = kzalloc(sizeof(*device), GFP_NOFS);
1490 device->devid = devid;
1495 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
1496 struct extent_buffer *leaf,
1497 struct btrfs_chunk *chunk)
1499 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1500 struct map_lookup *map;
1501 struct cache_extent *ce;
1505 u8 uuid[BTRFS_UUID_SIZE];
1510 logical = key->offset;
1511 length = btrfs_chunk_length(leaf, chunk);
1513 ce = find_first_cache_extent(&map_tree->cache_tree, logical);
1515 /* already mapped? */
1516 if (ce && ce->start <= logical && ce->start + ce->size > logical) {
1520 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1521 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1525 map->ce.start = logical;
1526 map->ce.size = length;
1527 map->num_stripes = num_stripes;
1528 map->io_width = btrfs_chunk_io_width(leaf, chunk);
1529 map->io_align = btrfs_chunk_io_align(leaf, chunk);
1530 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1531 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1532 map->type = btrfs_chunk_type(leaf, chunk);
1533 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
1535 for (i = 0; i < num_stripes; i++) {
1536 map->stripes[i].physical =
1537 btrfs_stripe_offset_nr(leaf, chunk, i);
1538 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
1539 read_extent_buffer(leaf, uuid, (unsigned long)
1540 btrfs_stripe_dev_uuid_nr(chunk, i),
1542 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
1544 if (!map->stripes[i].dev) {
1545 map->stripes[i].dev = fill_missing_device(devid);
1546 printf("warning, device %llu is missing\n",
1547 (unsigned long long)devid);
1551 ret = insert_existing_cache_extent(&map_tree->cache_tree, &map->ce);
1557 static int fill_device_from_item(struct extent_buffer *leaf,
1558 struct btrfs_dev_item *dev_item,
1559 struct btrfs_device *device)
1563 device->devid = btrfs_device_id(leaf, dev_item);
1564 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1565 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1566 device->type = btrfs_device_type(leaf, dev_item);
1567 device->io_align = btrfs_device_io_align(leaf, dev_item);
1568 device->io_width = btrfs_device_io_width(leaf, dev_item);
1569 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
1571 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1572 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1577 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
1579 struct btrfs_fs_devices *fs_devices;
1582 fs_devices = root->fs_info->fs_devices->seed;
1583 while (fs_devices) {
1584 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
1588 fs_devices = fs_devices->seed;
1591 fs_devices = find_fsid(fsid);
1597 ret = btrfs_open_devices(fs_devices, O_RDONLY);
1601 fs_devices->seed = root->fs_info->fs_devices->seed;
1602 root->fs_info->fs_devices->seed = fs_devices;
1607 static int read_one_dev(struct btrfs_root *root,
1608 struct extent_buffer *leaf,
1609 struct btrfs_dev_item *dev_item)
1611 struct btrfs_device *device;
1614 u8 fs_uuid[BTRFS_UUID_SIZE];
1615 u8 dev_uuid[BTRFS_UUID_SIZE];
1617 devid = btrfs_device_id(leaf, dev_item);
1618 read_extent_buffer(leaf, dev_uuid,
1619 (unsigned long)btrfs_device_uuid(dev_item),
1621 read_extent_buffer(leaf, fs_uuid,
1622 (unsigned long)btrfs_device_fsid(dev_item),
1625 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
1626 ret = open_seed_devices(root, fs_uuid);
1631 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1633 printk("warning devid %llu not found already\n",
1634 (unsigned long long)devid);
1635 device = kmalloc(sizeof(*device), GFP_NOFS);
1638 device->total_ios = 0;
1639 list_add(&device->dev_list,
1640 &root->fs_info->fs_devices->devices);
1643 fill_device_from_item(leaf, dev_item, device);
1644 device->dev_root = root->fs_info->dev_root;
1648 int btrfs_read_sys_array(struct btrfs_root *root)
1650 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
1651 struct extent_buffer *sb;
1652 struct btrfs_disk_key *disk_key;
1653 struct btrfs_chunk *chunk;
1654 struct btrfs_key key;
1659 unsigned long sb_ptr;
1663 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
1664 BTRFS_SUPER_INFO_SIZE);
1667 btrfs_set_buffer_uptodate(sb);
1668 write_extent_buffer(sb, super_copy, 0, sizeof(*super_copy));
1669 array_size = btrfs_super_sys_array_size(super_copy);
1672 * we do this loop twice, once for the device items and
1673 * once for all of the chunks. This way there are device
1674 * structs filled in for every chunk
1676 ptr = super_copy->sys_chunk_array;
1677 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
1680 while (cur < array_size) {
1681 disk_key = (struct btrfs_disk_key *)ptr;
1682 btrfs_disk_key_to_cpu(&key, disk_key);
1684 len = sizeof(*disk_key);
1689 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1690 chunk = (struct btrfs_chunk *)sb_ptr;
1691 ret = read_one_chunk(root, &key, sb, chunk);
1694 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
1695 len = btrfs_chunk_item_size(num_stripes);
1703 free_extent_buffer(sb);
1707 int btrfs_read_chunk_tree(struct btrfs_root *root)
1709 struct btrfs_path *path;
1710 struct extent_buffer *leaf;
1711 struct btrfs_key key;
1712 struct btrfs_key found_key;
1716 root = root->fs_info->chunk_root;
1718 path = btrfs_alloc_path();
1722 /* first we search for all of the device items, and then we
1723 * read in all of the chunk items. This way we can create chunk
1724 * mappings that reference all of the devices that are afound
1726 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1730 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1732 leaf = path->nodes[0];
1733 slot = path->slots[0];
1734 if (slot >= btrfs_header_nritems(leaf)) {
1735 ret = btrfs_next_leaf(root, path);
1742 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1743 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1744 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
1746 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
1747 struct btrfs_dev_item *dev_item;
1748 dev_item = btrfs_item_ptr(leaf, slot,
1749 struct btrfs_dev_item);
1750 ret = read_one_dev(root, leaf, dev_item);
1753 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
1754 struct btrfs_chunk *chunk;
1755 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1756 ret = read_one_chunk(root, &found_key, leaf, chunk);
1761 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1763 btrfs_release_path(root, path);
1769 btrfs_free_path(path);
1773 struct list_head *btrfs_scanned_uuids(void)
1778 static int rmw_eb(struct btrfs_fs_info *info,
1779 struct extent_buffer *eb, struct extent_buffer *orig_eb)
1782 unsigned long orig_off = 0;
1783 unsigned long dest_off = 0;
1784 unsigned long copy_len = eb->len;
1786 ret = read_whole_eb(info, eb, 0);
1790 if (eb->start + eb->len <= orig_eb->start ||
1791 eb->start >= orig_eb->start + orig_eb->len)
1794 * | ----- orig_eb ------- |
1795 * | ----- stripe ------- |
1796 * | ----- orig_eb ------- |
1797 * | ----- orig_eb ------- |
1799 if (eb->start > orig_eb->start)
1800 orig_off = eb->start - orig_eb->start;
1801 if (orig_eb->start > eb->start)
1802 dest_off = orig_eb->start - eb->start;
1804 if (copy_len > orig_eb->len - orig_off)
1805 copy_len = orig_eb->len - orig_off;
1806 if (copy_len > eb->len - dest_off)
1807 copy_len = eb->len - dest_off;
1809 memcpy(eb->data + dest_off, orig_eb->data + orig_off, copy_len);
1813 static void split_eb_for_raid56(struct btrfs_fs_info *info,
1814 struct extent_buffer *orig_eb,
1815 struct extent_buffer **ebs,
1816 u64 stripe_len, u64 *raid_map,
1819 struct extent_buffer *eb;
1820 u64 start = orig_eb->start;
1825 for (i = 0; i < num_stripes; i++) {
1826 if (raid_map[i] >= BTRFS_RAID5_P_STRIPE)
1829 eb = malloc(sizeof(struct extent_buffer) + stripe_len);
1832 memset(eb, 0, sizeof(struct extent_buffer) + stripe_len);
1834 eb->start = raid_map[i];
1835 eb->len = stripe_len;
1839 eb->dev_bytenr = (u64)-1;
1841 this_eb_start = raid_map[i];
1843 if (start > this_eb_start ||
1844 start + orig_eb->len < this_eb_start + stripe_len) {
1845 ret = rmw_eb(info, eb, orig_eb);
1848 memcpy(eb->data, orig_eb->data + eb->start - start, stripe_len);
1854 int write_raid56_with_parity(struct btrfs_fs_info *info,
1855 struct extent_buffer *eb,
1856 struct btrfs_multi_bio *multi,
1857 u64 stripe_len, u64 *raid_map)
1859 struct extent_buffer *ebs[multi->num_stripes], *p_eb = NULL, *q_eb = NULL;
1863 int alloc_size = eb->len;
1865 if (stripe_len > alloc_size)
1866 alloc_size = stripe_len;
1868 split_eb_for_raid56(info, eb, ebs, stripe_len, raid_map,
1869 multi->num_stripes);
1871 for (i = 0; i < multi->num_stripes; i++) {
1872 struct extent_buffer *new_eb;
1873 if (raid_map[i] < BTRFS_RAID5_P_STRIPE) {
1874 ebs[i]->dev_bytenr = multi->stripes[i].physical;
1875 ebs[i]->fd = multi->stripes[i].dev->fd;
1876 multi->stripes[i].dev->total_ios++;
1877 BUG_ON(ebs[i]->start != raid_map[i]);
1880 new_eb = kmalloc(sizeof(*eb) + alloc_size, GFP_NOFS);
1882 new_eb->dev_bytenr = multi->stripes[i].physical;
1883 new_eb->fd = multi->stripes[i].dev->fd;
1884 multi->stripes[i].dev->total_ios++;
1885 new_eb->len = stripe_len;
1887 if (raid_map[i] == BTRFS_RAID5_P_STRIPE)
1889 else if (raid_map[i] == BTRFS_RAID6_Q_STRIPE)
1893 void *pointers[multi->num_stripes];
1894 ebs[multi->num_stripes - 2] = p_eb;
1895 ebs[multi->num_stripes - 1] = q_eb;
1897 for (i = 0; i < multi->num_stripes; i++)
1898 pointers[i] = ebs[i]->data;
1900 raid6_gen_syndrome(multi->num_stripes, stripe_len, pointers);
1902 ebs[multi->num_stripes - 1] = p_eb;
1903 memcpy(p_eb->data, ebs[0]->data, stripe_len);
1904 for (j = 1; j < multi->num_stripes - 1; j++) {
1905 for (i = 0; i < stripe_len; i += sizeof(unsigned long)) {
1906 *(unsigned long *)(p_eb->data + i) ^=
1907 *(unsigned long *)(ebs[j]->data + i);
1912 for (i = 0; i < multi->num_stripes; i++) {
1913 ret = write_extent_to_disk(ebs[i]);