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 list_head *cur;
164 struct btrfs_device *device;
166 list_for_each(cur, &fs_devices->devices) {
167 device = list_entry(cur, struct btrfs_device, dev_list);
168 if (device->fd != -1) {
170 if (posix_fadvise(device->fd, 0, 0, POSIX_FADV_DONTNEED))
171 fprintf(stderr, "Warning, could not drop caches\n");
175 device->writeable = 0;
178 seed_devices = fs_devices->seed;
179 fs_devices->seed = NULL;
181 fs_devices = seed_devices;
188 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, int flags)
191 struct list_head *head = &fs_devices->devices;
192 struct list_head *cur;
193 struct btrfs_device *device;
196 list_for_each(cur, head) {
197 device = list_entry(cur, struct btrfs_device, dev_list);
199 printk("no name for device %llu, skip it now\n", device->devid);
203 fd = open(device->name, flags);
209 if (posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED))
210 fprintf(stderr, "Warning, could not drop caches\n");
212 if (device->devid == fs_devices->latest_devid)
213 fs_devices->latest_bdev = fd;
214 if (device->devid == fs_devices->lowest_devid)
215 fs_devices->lowest_bdev = fd;
218 device->writeable = 1;
222 btrfs_close_devices(fs_devices);
226 int btrfs_scan_one_device(int fd, const char *path,
227 struct btrfs_fs_devices **fs_devices_ret,
228 u64 *total_devs, u64 super_offset)
230 struct btrfs_super_block *disk_super;
240 disk_super = (struct btrfs_super_block *)buf;
241 ret = btrfs_read_dev_super(fd, disk_super, super_offset);
246 devid = btrfs_stack_device_id(&disk_super->dev_item);
247 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)
250 *total_devs = btrfs_super_num_devices(disk_super);
252 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
261 * this uses a pretty simple search, the expectation is that it is
262 * called very infrequently and that a given device has a small number
265 static int find_free_dev_extent(struct btrfs_trans_handle *trans,
266 struct btrfs_device *device,
267 struct btrfs_path *path,
268 u64 num_bytes, u64 *start)
270 struct btrfs_key key;
271 struct btrfs_root *root = device->dev_root;
272 struct btrfs_dev_extent *dev_extent = NULL;
275 u64 search_start = 0;
276 u64 search_end = device->total_bytes;
280 struct extent_buffer *l;
285 /* FIXME use last free of some kind */
287 /* we don't want to overwrite the superblock on the drive,
288 * so we make sure to start at an offset of at least 1MB
290 search_start = max((u64)1024 * 1024, search_start);
292 if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
293 search_start = max(root->fs_info->alloc_start, search_start);
295 key.objectid = device->devid;
296 key.offset = search_start;
297 key.type = BTRFS_DEV_EXTENT_KEY;
298 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
301 ret = btrfs_previous_item(root, path, 0, key.type);
305 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
308 slot = path->slots[0];
309 if (slot >= btrfs_header_nritems(l)) {
310 ret = btrfs_next_leaf(root, path);
317 if (search_start >= search_end) {
321 *start = search_start;
325 *start = last_byte > search_start ?
326 last_byte : search_start;
327 if (search_end <= *start) {
333 btrfs_item_key_to_cpu(l, &key, slot);
335 if (key.objectid < device->devid)
338 if (key.objectid > device->devid)
341 if (key.offset >= search_start && key.offset > last_byte &&
343 if (last_byte < search_start)
344 last_byte = search_start;
345 hole_size = key.offset - last_byte;
346 if (key.offset > last_byte &&
347 hole_size >= num_bytes) {
352 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
357 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
358 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
364 /* we have to make sure we didn't find an extent that has already
365 * been allocated by the map tree or the original allocation
367 btrfs_release_path(root, path);
368 BUG_ON(*start < search_start);
370 if (*start + num_bytes > search_end) {
374 /* check for pending inserts here */
378 btrfs_release_path(root, path);
382 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
383 struct btrfs_device *device,
384 u64 chunk_tree, u64 chunk_objectid,
386 u64 num_bytes, u64 *start)
389 struct btrfs_path *path;
390 struct btrfs_root *root = device->dev_root;
391 struct btrfs_dev_extent *extent;
392 struct extent_buffer *leaf;
393 struct btrfs_key key;
395 path = btrfs_alloc_path();
399 ret = find_free_dev_extent(trans, device, path, num_bytes, start);
404 key.objectid = device->devid;
406 key.type = BTRFS_DEV_EXTENT_KEY;
407 ret = btrfs_insert_empty_item(trans, root, path, &key,
411 leaf = path->nodes[0];
412 extent = btrfs_item_ptr(leaf, path->slots[0],
413 struct btrfs_dev_extent);
414 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
415 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
416 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
418 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
419 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
422 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
423 btrfs_mark_buffer_dirty(leaf);
425 btrfs_free_path(path);
429 static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset)
431 struct btrfs_path *path;
433 struct btrfs_key key;
434 struct btrfs_chunk *chunk;
435 struct btrfs_key found_key;
437 path = btrfs_alloc_path();
440 key.objectid = objectid;
441 key.offset = (u64)-1;
442 key.type = BTRFS_CHUNK_ITEM_KEY;
444 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
450 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
454 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
456 if (found_key.objectid != objectid)
459 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
461 *offset = found_key.offset +
462 btrfs_chunk_length(path->nodes[0], chunk);
467 btrfs_free_path(path);
471 static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
475 struct btrfs_key key;
476 struct btrfs_key found_key;
478 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
479 key.type = BTRFS_DEV_ITEM_KEY;
480 key.offset = (u64)-1;
482 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
488 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
493 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
495 *objectid = found_key.offset + 1;
499 btrfs_release_path(root, path);
504 * the device information is stored in the chunk root
505 * the btrfs_device struct should be fully filled in
507 int btrfs_add_device(struct btrfs_trans_handle *trans,
508 struct btrfs_root *root,
509 struct btrfs_device *device)
512 struct btrfs_path *path;
513 struct btrfs_dev_item *dev_item;
514 struct extent_buffer *leaf;
515 struct btrfs_key key;
519 root = root->fs_info->chunk_root;
521 path = btrfs_alloc_path();
525 ret = find_next_devid(root, path, &free_devid);
529 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
530 key.type = BTRFS_DEV_ITEM_KEY;
531 key.offset = free_devid;
533 ret = btrfs_insert_empty_item(trans, root, path, &key,
538 leaf = path->nodes[0];
539 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
541 device->devid = free_devid;
542 btrfs_set_device_id(leaf, dev_item, device->devid);
543 btrfs_set_device_generation(leaf, dev_item, 0);
544 btrfs_set_device_type(leaf, dev_item, device->type);
545 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
546 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
547 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
548 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
549 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
550 btrfs_set_device_group(leaf, dev_item, 0);
551 btrfs_set_device_seek_speed(leaf, dev_item, 0);
552 btrfs_set_device_bandwidth(leaf, dev_item, 0);
553 btrfs_set_device_start_offset(leaf, dev_item, 0);
555 ptr = (unsigned long)btrfs_device_uuid(dev_item);
556 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
557 ptr = (unsigned long)btrfs_device_fsid(dev_item);
558 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
559 btrfs_mark_buffer_dirty(leaf);
563 btrfs_free_path(path);
567 int btrfs_update_device(struct btrfs_trans_handle *trans,
568 struct btrfs_device *device)
571 struct btrfs_path *path;
572 struct btrfs_root *root;
573 struct btrfs_dev_item *dev_item;
574 struct extent_buffer *leaf;
575 struct btrfs_key key;
577 root = device->dev_root->fs_info->chunk_root;
579 path = btrfs_alloc_path();
583 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
584 key.type = BTRFS_DEV_ITEM_KEY;
585 key.offset = device->devid;
587 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
596 leaf = path->nodes[0];
597 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
599 btrfs_set_device_id(leaf, dev_item, device->devid);
600 btrfs_set_device_type(leaf, dev_item, device->type);
601 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
602 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
603 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
604 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
605 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
606 btrfs_mark_buffer_dirty(leaf);
609 btrfs_free_path(path);
613 int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
614 struct btrfs_root *root,
615 struct btrfs_key *key,
616 struct btrfs_chunk *chunk, int item_size)
618 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
619 struct btrfs_disk_key disk_key;
623 array_size = btrfs_super_sys_array_size(super_copy);
624 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
627 ptr = super_copy->sys_chunk_array + array_size;
628 btrfs_cpu_key_to_disk(&disk_key, key);
629 memcpy(ptr, &disk_key, sizeof(disk_key));
630 ptr += sizeof(disk_key);
631 memcpy(ptr, chunk, item_size);
632 item_size += sizeof(disk_key);
633 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
637 static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes,
640 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
642 else if (type & BTRFS_BLOCK_GROUP_RAID10)
643 return calc_size * (num_stripes / sub_stripes);
644 else if (type & BTRFS_BLOCK_GROUP_RAID5)
645 return calc_size * (num_stripes - 1);
646 else if (type & BTRFS_BLOCK_GROUP_RAID6)
647 return calc_size * (num_stripes - 2);
649 return calc_size * num_stripes;
653 static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
655 /* TODO, add a way to store the preferred stripe size */
659 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
660 struct btrfs_root *extent_root, u64 *start,
661 u64 *num_bytes, u64 type)
664 struct btrfs_fs_info *info = extent_root->fs_info;
665 struct btrfs_root *chunk_root = info->chunk_root;
666 struct btrfs_stripe *stripes;
667 struct btrfs_device *device = NULL;
668 struct btrfs_chunk *chunk;
669 struct list_head private_devs;
670 struct list_head *dev_list = &info->fs_devices->devices;
671 struct list_head *cur;
672 struct map_lookup *map;
673 int min_stripe_size = 1 * 1024 * 1024;
674 u64 calc_size = 8 * 1024 * 1024;
676 u64 max_chunk_size = 4 * calc_size;
686 int stripe_len = 64 * 1024;
687 struct btrfs_key key;
690 if (list_empty(dev_list)) {
694 if (type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
695 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
696 BTRFS_BLOCK_GROUP_RAID10 |
697 BTRFS_BLOCK_GROUP_DUP)) {
698 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
699 calc_size = 8 * 1024 * 1024;
700 max_chunk_size = calc_size * 2;
701 min_stripe_size = 1 * 1024 * 1024;
702 } else if (type & BTRFS_BLOCK_GROUP_DATA) {
703 calc_size = 1024 * 1024 * 1024;
704 max_chunk_size = 10 * calc_size;
705 min_stripe_size = 64 * 1024 * 1024;
706 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
707 calc_size = 1024 * 1024 * 1024;
708 max_chunk_size = 4 * calc_size;
709 min_stripe_size = 32 * 1024 * 1024;
712 if (type & BTRFS_BLOCK_GROUP_RAID1) {
713 num_stripes = min_t(u64, 2,
714 btrfs_super_num_devices(info->super_copy));
719 if (type & BTRFS_BLOCK_GROUP_DUP) {
723 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
724 num_stripes = btrfs_super_num_devices(info->super_copy);
727 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
728 num_stripes = btrfs_super_num_devices(info->super_copy);
731 num_stripes &= ~(u32)1;
735 if (type & (BTRFS_BLOCK_GROUP_RAID5)) {
736 num_stripes = btrfs_super_num_devices(info->super_copy);
740 stripe_len = find_raid56_stripe_len(num_stripes - 1,
741 btrfs_super_stripesize(info->super_copy));
743 if (type & (BTRFS_BLOCK_GROUP_RAID6)) {
744 num_stripes = btrfs_super_num_devices(info->super_copy);
748 stripe_len = find_raid56_stripe_len(num_stripes - 2,
749 btrfs_super_stripesize(info->super_copy));
752 /* we don't want a chunk larger than 10% of the FS */
753 percent_max = div_factor(btrfs_super_total_bytes(info->super_copy), 1);
754 max_chunk_size = min(percent_max, max_chunk_size);
757 if (chunk_bytes_by_type(type, calc_size, num_stripes, sub_stripes) >
759 calc_size = max_chunk_size;
760 calc_size /= num_stripes;
761 calc_size /= stripe_len;
762 calc_size *= stripe_len;
764 /* we don't want tiny stripes */
765 calc_size = max_t(u64, calc_size, min_stripe_size);
767 calc_size /= stripe_len;
768 calc_size *= stripe_len;
769 INIT_LIST_HEAD(&private_devs);
770 cur = dev_list->next;
773 if (type & BTRFS_BLOCK_GROUP_DUP)
774 min_free = calc_size * 2;
776 min_free = calc_size;
778 /* build a private list of devices we will allocate from */
779 while(index < num_stripes) {
780 device = list_entry(cur, struct btrfs_device, dev_list);
781 avail = device->total_bytes - device->bytes_used;
783 if (avail >= min_free) {
784 list_move_tail(&device->dev_list, &private_devs);
786 if (type & BTRFS_BLOCK_GROUP_DUP)
788 } else if (avail > max_avail)
793 if (index < num_stripes) {
794 list_splice(&private_devs, dev_list);
795 if (index >= min_stripes) {
797 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
798 num_stripes /= sub_stripes;
799 num_stripes *= sub_stripes;
804 if (!looped && max_avail > 0) {
806 calc_size = max_avail;
811 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
815 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
816 key.type = BTRFS_CHUNK_ITEM_KEY;
819 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
823 map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
829 stripes = &chunk->stripe;
830 *num_bytes = chunk_bytes_by_type(type, calc_size,
831 num_stripes, sub_stripes);
833 while(index < num_stripes) {
834 struct btrfs_stripe *stripe;
835 BUG_ON(list_empty(&private_devs));
836 cur = private_devs.next;
837 device = list_entry(cur, struct btrfs_device, dev_list);
839 /* loop over this device again if we're doing a dup group */
840 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
841 (index == num_stripes - 1))
842 list_move_tail(&device->dev_list, dev_list);
844 ret = btrfs_alloc_dev_extent(trans, device,
845 info->chunk_root->root_key.objectid,
846 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
847 calc_size, &dev_offset);
850 device->bytes_used += calc_size;
851 ret = btrfs_update_device(trans, device);
854 map->stripes[index].dev = device;
855 map->stripes[index].physical = dev_offset;
856 stripe = stripes + index;
857 btrfs_set_stack_stripe_devid(stripe, device->devid);
858 btrfs_set_stack_stripe_offset(stripe, dev_offset);
859 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
862 BUG_ON(!list_empty(&private_devs));
864 /* key was set above */
865 btrfs_set_stack_chunk_length(chunk, *num_bytes);
866 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
867 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
868 btrfs_set_stack_chunk_type(chunk, type);
869 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
870 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
871 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
872 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
873 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
874 map->sector_size = extent_root->sectorsize;
875 map->stripe_len = stripe_len;
876 map->io_align = stripe_len;
877 map->io_width = stripe_len;
879 map->num_stripes = num_stripes;
880 map->sub_stripes = sub_stripes;
882 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
883 btrfs_chunk_item_size(num_stripes));
885 *start = key.offset;;
887 map->ce.start = key.offset;
888 map->ce.size = *num_bytes;
890 ret = insert_cache_extent(&info->mapping_tree.cache_tree, &map->ce);
893 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
894 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
895 chunk, btrfs_chunk_item_size(num_stripes));
903 int btrfs_alloc_data_chunk(struct btrfs_trans_handle *trans,
904 struct btrfs_root *extent_root, u64 *start,
905 u64 num_bytes, u64 type)
908 struct btrfs_fs_info *info = extent_root->fs_info;
909 struct btrfs_root *chunk_root = info->chunk_root;
910 struct btrfs_stripe *stripes;
911 struct btrfs_device *device = NULL;
912 struct btrfs_chunk *chunk;
913 struct list_head *dev_list = &info->fs_devices->devices;
914 struct list_head *cur;
915 struct map_lookup *map;
916 u64 calc_size = 8 * 1024 * 1024;
921 int stripe_len = 64 * 1024;
922 struct btrfs_key key;
924 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
925 key.type = BTRFS_CHUNK_ITEM_KEY;
926 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
931 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
935 map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
941 stripes = &chunk->stripe;
942 calc_size = num_bytes;
945 cur = dev_list->next;
946 device = list_entry(cur, struct btrfs_device, dev_list);
948 while (index < num_stripes) {
949 struct btrfs_stripe *stripe;
951 ret = btrfs_alloc_dev_extent(trans, device,
952 info->chunk_root->root_key.objectid,
953 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
954 calc_size, &dev_offset);
957 device->bytes_used += calc_size;
958 ret = btrfs_update_device(trans, device);
961 map->stripes[index].dev = device;
962 map->stripes[index].physical = dev_offset;
963 stripe = stripes + index;
964 btrfs_set_stack_stripe_devid(stripe, device->devid);
965 btrfs_set_stack_stripe_offset(stripe, dev_offset);
966 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
970 /* key was set above */
971 btrfs_set_stack_chunk_length(chunk, num_bytes);
972 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
973 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
974 btrfs_set_stack_chunk_type(chunk, type);
975 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
976 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
977 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
978 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
979 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
980 map->sector_size = extent_root->sectorsize;
981 map->stripe_len = stripe_len;
982 map->io_align = stripe_len;
983 map->io_width = stripe_len;
985 map->num_stripes = num_stripes;
986 map->sub_stripes = sub_stripes;
988 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
989 btrfs_chunk_item_size(num_stripes));
993 map->ce.start = key.offset;
994 map->ce.size = num_bytes;
996 ret = insert_cache_extent(&info->mapping_tree.cache_tree, &map->ce);
1003 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
1005 cache_tree_init(&tree->cache_tree);
1008 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
1010 struct cache_extent *ce;
1011 struct map_lookup *map;
1014 ce = search_cache_extent(&map_tree->cache_tree, logical);
1016 BUG_ON(ce->start > logical || ce->start + ce->size < logical);
1017 map = container_of(ce, struct map_lookup, ce);
1019 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
1020 ret = map->num_stripes;
1021 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1022 ret = map->sub_stripes;
1023 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
1025 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
1032 int btrfs_next_metadata(struct btrfs_mapping_tree *map_tree, u64 *logical,
1035 struct cache_extent *ce;
1036 struct map_lookup *map;
1038 ce = search_cache_extent(&map_tree->cache_tree, *logical);
1041 ce = next_cache_extent(ce);
1045 map = container_of(ce, struct map_lookup, ce);
1046 if (map->type & BTRFS_BLOCK_GROUP_METADATA) {
1047 *logical = ce->start;
1056 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
1057 u64 chunk_start, u64 physical, u64 devid,
1058 u64 **logical, int *naddrs, int *stripe_len)
1060 struct cache_extent *ce;
1061 struct map_lookup *map;
1069 ce = search_cache_extent(&map_tree->cache_tree, chunk_start);
1071 map = container_of(ce, struct map_lookup, ce);
1074 rmap_len = map->stripe_len;
1075 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1076 length = ce->size / (map->num_stripes / map->sub_stripes);
1077 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
1078 length = ce->size / map->num_stripes;
1079 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
1080 BTRFS_BLOCK_GROUP_RAID6)) {
1081 length = ce->size / nr_data_stripes(map);
1082 rmap_len = map->stripe_len * nr_data_stripes(map);
1085 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
1087 for (i = 0; i < map->num_stripes; i++) {
1088 if (devid && map->stripes[i].dev->devid != devid)
1090 if (map->stripes[i].physical > physical ||
1091 map->stripes[i].physical + length <= physical)
1094 stripe_nr = (physical - map->stripes[i].physical) /
1097 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1098 stripe_nr = (stripe_nr * map->num_stripes + i) /
1100 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
1101 stripe_nr = stripe_nr * map->num_stripes + i;
1102 } /* else if RAID[56], multiply by nr_data_stripes().
1103 * Alternatively, just use rmap_len below instead of
1104 * map->stripe_len */
1106 bytenr = ce->start + stripe_nr * rmap_len;
1107 for (j = 0; j < nr; j++) {
1108 if (buf[j] == bytenr)
1117 *stripe_len = rmap_len;
1122 static inline int parity_smaller(u64 a, u64 b)
1127 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
1128 static void sort_parity_stripes(struct btrfs_multi_bio *bbio, u64 *raid_map)
1130 struct btrfs_bio_stripe s;
1137 for (i = 0; i < bbio->num_stripes - 1; i++) {
1138 if (parity_smaller(raid_map[i], raid_map[i+1])) {
1139 s = bbio->stripes[i];
1141 bbio->stripes[i] = bbio->stripes[i+1];
1142 raid_map[i] = raid_map[i+1];
1143 bbio->stripes[i+1] = s;
1151 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1152 u64 logical, u64 *length,
1153 struct btrfs_multi_bio **multi_ret, int mirror_num,
1156 return __btrfs_map_block(map_tree, rw, logical, length, NULL,
1157 multi_ret, mirror_num, raid_map_ret);
1160 int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1161 u64 logical, u64 *length, u64 *type,
1162 struct btrfs_multi_bio **multi_ret, int mirror_num,
1165 struct cache_extent *ce;
1166 struct map_lookup *map;
1170 u64 *raid_map = NULL;
1171 int stripes_allocated = 8;
1172 int stripes_required = 1;
1175 struct btrfs_multi_bio *multi = NULL;
1177 if (multi_ret && rw == READ) {
1178 stripes_allocated = 1;
1181 ce = search_cache_extent(&map_tree->cache_tree, logical);
1187 if (ce->start > logical || ce->start + ce->size < logical) {
1194 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
1199 map = container_of(ce, struct map_lookup, ce);
1200 offset = logical - ce->start;
1203 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
1204 BTRFS_BLOCK_GROUP_DUP)) {
1205 stripes_required = map->num_stripes;
1206 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1207 stripes_required = map->sub_stripes;
1210 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)
1211 && multi_ret && ((rw & WRITE) || mirror_num > 1) && raid_map_ret) {
1212 /* RAID[56] write or recovery. Return all stripes */
1213 stripes_required = map->num_stripes;
1215 /* Only allocate the map if we've already got a large enough multi_ret */
1216 if (stripes_allocated >= stripes_required) {
1217 raid_map = kmalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
1225 /* if our multi bio struct is too small, back off and try again */
1226 if (multi_ret && stripes_allocated < stripes_required) {
1227 stripes_allocated = stripes_required;
1234 * stripe_nr counts the total number of stripes we have to stride
1235 * to get to this block
1237 stripe_nr = stripe_nr / map->stripe_len;
1239 stripe_offset = stripe_nr * map->stripe_len;
1240 BUG_ON(offset < stripe_offset);
1242 /* stripe_offset is the offset of this block in its stripe*/
1243 stripe_offset = offset - stripe_offset;
1245 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
1246 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
1247 BTRFS_BLOCK_GROUP_RAID10 |
1248 BTRFS_BLOCK_GROUP_DUP)) {
1249 /* we limit the length of each bio to what fits in a stripe */
1250 *length = min_t(u64, ce->size - offset,
1251 map->stripe_len - stripe_offset);
1253 *length = ce->size - offset;
1259 multi->num_stripes = 1;
1261 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
1263 multi->num_stripes = map->num_stripes;
1264 else if (mirror_num)
1265 stripe_index = mirror_num - 1;
1267 stripe_index = stripe_nr % map->num_stripes;
1268 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1269 int factor = map->num_stripes / map->sub_stripes;
1271 stripe_index = stripe_nr % factor;
1272 stripe_index *= map->sub_stripes;
1275 multi->num_stripes = map->sub_stripes;
1276 else if (mirror_num)
1277 stripe_index += mirror_num - 1;
1279 stripe_nr = stripe_nr / factor;
1280 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
1282 multi->num_stripes = map->num_stripes;
1283 else if (mirror_num)
1284 stripe_index = mirror_num - 1;
1285 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
1286 BTRFS_BLOCK_GROUP_RAID6)) {
1291 u64 raid56_full_stripe_start;
1292 u64 full_stripe_len = nr_data_stripes(map) * map->stripe_len;
1295 * align the start of our data stripe in the logical
1298 raid56_full_stripe_start = offset / full_stripe_len;
1299 raid56_full_stripe_start *= full_stripe_len;
1301 /* get the data stripe number */
1302 stripe_nr = raid56_full_stripe_start / map->stripe_len;
1303 stripe_nr = stripe_nr / nr_data_stripes(map);
1305 /* Work out the disk rotation on this stripe-set */
1306 rot = stripe_nr % map->num_stripes;
1308 /* Fill in the logical address of each stripe */
1309 tmp = stripe_nr * nr_data_stripes(map);
1311 for (i = 0; i < nr_data_stripes(map); i++)
1312 raid_map[(i+rot) % map->num_stripes] =
1313 ce->start + (tmp + i) * map->stripe_len;
1315 raid_map[(i+rot) % map->num_stripes] = BTRFS_RAID5_P_STRIPE;
1316 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
1317 raid_map[(i+rot+1) % map->num_stripes] = BTRFS_RAID6_Q_STRIPE;
1319 *length = map->stripe_len;
1322 multi->num_stripes = map->num_stripes;
1324 stripe_index = stripe_nr % nr_data_stripes(map);
1325 stripe_nr = stripe_nr / nr_data_stripes(map);
1328 * Mirror #0 or #1 means the original data block.
1329 * Mirror #2 is RAID5 parity block.
1330 * Mirror #3 is RAID6 Q block.
1333 stripe_index = nr_data_stripes(map) + mirror_num - 2;
1335 /* We distribute the parity blocks across stripes */
1336 stripe_index = (stripe_nr + stripe_index) % map->num_stripes;
1340 * after this do_div call, stripe_nr is the number of stripes
1341 * on this device we have to walk to find the data, and
1342 * stripe_index is the number of our device in the stripe array
1344 stripe_index = stripe_nr % map->num_stripes;
1345 stripe_nr = stripe_nr / map->num_stripes;
1347 BUG_ON(stripe_index >= map->num_stripes);
1349 for (i = 0; i < multi->num_stripes; i++) {
1350 multi->stripes[i].physical =
1351 map->stripes[stripe_index].physical + stripe_offset +
1352 stripe_nr * map->stripe_len;
1353 multi->stripes[i].dev = map->stripes[stripe_index].dev;
1362 sort_parity_stripes(multi, raid_map);
1363 *raid_map_ret = raid_map;
1369 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
1372 struct btrfs_device *device;
1373 struct btrfs_fs_devices *cur_devices;
1375 cur_devices = root->fs_info->fs_devices;
1376 while (cur_devices) {
1378 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
1379 device = __find_device(&cur_devices->devices,
1384 cur_devices = cur_devices->seed;
1389 struct btrfs_device *
1390 btrfs_find_device_by_devid(struct btrfs_fs_devices *fs_devices,
1391 u64 devid, int instance)
1393 struct list_head *head = &fs_devices->devices;
1394 struct btrfs_device *dev;
1397 list_for_each_entry(dev, head, dev_list) {
1398 if (dev->devid == devid && num_found++ == instance)
1404 int btrfs_bootstrap_super_map(struct btrfs_mapping_tree *map_tree,
1405 struct btrfs_fs_devices *fs_devices)
1407 struct map_lookup *map;
1408 u64 logical = BTRFS_SUPER_INFO_OFFSET;
1409 u64 length = BTRFS_SUPER_INFO_SIZE;
1410 int num_stripes = 0;
1411 int sub_stripes = 0;
1414 struct list_head *cur;
1416 list_for_each(cur, &fs_devices->devices) {
1419 map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
1423 map->ce.start = logical;
1424 map->ce.size = length;
1425 map->num_stripes = num_stripes;
1426 map->sub_stripes = sub_stripes;
1427 map->io_width = length;
1428 map->io_align = length;
1429 map->sector_size = length;
1430 map->stripe_len = length;
1431 map->type = BTRFS_BLOCK_GROUP_RAID1;
1434 list_for_each(cur, &fs_devices->devices) {
1435 struct btrfs_device *device = list_entry(cur,
1436 struct btrfs_device,
1438 map->stripes[i].physical = logical;
1439 map->stripes[i].dev = device;
1442 ret = insert_cache_extent(&map_tree->cache_tree, &map->ce);
1443 if (ret == -EEXIST) {
1444 struct cache_extent *old;
1445 struct map_lookup *old_map;
1446 old = lookup_cache_extent(&map_tree->cache_tree,
1448 old_map = container_of(old, struct map_lookup, ce);
1449 remove_cache_extent(&map_tree->cache_tree, old);
1451 ret = insert_cache_extent(&map_tree->cache_tree,
1458 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
1460 struct cache_extent *ce;
1461 struct map_lookup *map;
1462 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1466 ce = search_cache_extent(&map_tree->cache_tree, chunk_offset);
1469 map = container_of(ce, struct map_lookup, ce);
1470 for (i = 0; i < map->num_stripes; i++) {
1471 if (!map->stripes[i].dev->writeable) {
1480 static struct btrfs_device *fill_missing_device(u64 devid)
1482 struct btrfs_device *device;
1484 device = kzalloc(sizeof(*device), GFP_NOFS);
1485 device->devid = devid;
1490 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
1491 struct extent_buffer *leaf,
1492 struct btrfs_chunk *chunk)
1494 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1495 struct map_lookup *map;
1496 struct cache_extent *ce;
1500 u8 uuid[BTRFS_UUID_SIZE];
1505 logical = key->offset;
1506 length = btrfs_chunk_length(leaf, chunk);
1508 ce = search_cache_extent(&map_tree->cache_tree, logical);
1510 /* already mapped? */
1511 if (ce && ce->start <= logical && ce->start + ce->size > logical) {
1515 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1516 map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
1520 map->ce.start = logical;
1521 map->ce.size = length;
1522 map->num_stripes = num_stripes;
1523 map->io_width = btrfs_chunk_io_width(leaf, chunk);
1524 map->io_align = btrfs_chunk_io_align(leaf, chunk);
1525 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1526 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1527 map->type = btrfs_chunk_type(leaf, chunk);
1528 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
1530 for (i = 0; i < num_stripes; i++) {
1531 map->stripes[i].physical =
1532 btrfs_stripe_offset_nr(leaf, chunk, i);
1533 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
1534 read_extent_buffer(leaf, uuid, (unsigned long)
1535 btrfs_stripe_dev_uuid_nr(chunk, i),
1537 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
1539 if (!map->stripes[i].dev) {
1540 map->stripes[i].dev = fill_missing_device(devid);
1541 printf("warning, device %llu is missing\n",
1542 (unsigned long long)devid);
1546 ret = insert_cache_extent(&map_tree->cache_tree, &map->ce);
1552 static int fill_device_from_item(struct extent_buffer *leaf,
1553 struct btrfs_dev_item *dev_item,
1554 struct btrfs_device *device)
1558 device->devid = btrfs_device_id(leaf, dev_item);
1559 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1560 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1561 device->type = btrfs_device_type(leaf, dev_item);
1562 device->io_align = btrfs_device_io_align(leaf, dev_item);
1563 device->io_width = btrfs_device_io_width(leaf, dev_item);
1564 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
1566 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1567 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1572 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
1574 struct btrfs_fs_devices *fs_devices;
1577 fs_devices = root->fs_info->fs_devices->seed;
1578 while (fs_devices) {
1579 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
1583 fs_devices = fs_devices->seed;
1586 fs_devices = find_fsid(fsid);
1592 ret = btrfs_open_devices(fs_devices, O_RDONLY);
1596 fs_devices->seed = root->fs_info->fs_devices->seed;
1597 root->fs_info->fs_devices->seed = fs_devices;
1602 static int read_one_dev(struct btrfs_root *root,
1603 struct extent_buffer *leaf,
1604 struct btrfs_dev_item *dev_item)
1606 struct btrfs_device *device;
1609 u8 fs_uuid[BTRFS_UUID_SIZE];
1610 u8 dev_uuid[BTRFS_UUID_SIZE];
1612 devid = btrfs_device_id(leaf, dev_item);
1613 read_extent_buffer(leaf, dev_uuid,
1614 (unsigned long)btrfs_device_uuid(dev_item),
1616 read_extent_buffer(leaf, fs_uuid,
1617 (unsigned long)btrfs_device_fsid(dev_item),
1620 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
1621 ret = open_seed_devices(root, fs_uuid);
1626 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1628 printk("warning devid %llu not found already\n",
1629 (unsigned long long)devid);
1630 device = kzalloc(sizeof(*device), GFP_NOFS);
1634 list_add(&device->dev_list,
1635 &root->fs_info->fs_devices->devices);
1638 fill_device_from_item(leaf, dev_item, device);
1639 device->dev_root = root->fs_info->dev_root;
1643 int btrfs_read_sys_array(struct btrfs_root *root)
1645 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
1646 struct extent_buffer *sb;
1647 struct btrfs_disk_key *disk_key;
1648 struct btrfs_chunk *chunk;
1649 struct btrfs_key key;
1656 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
1657 BTRFS_SUPER_INFO_SIZE);
1660 btrfs_set_buffer_uptodate(sb);
1661 write_extent_buffer(sb, super_copy, 0, sizeof(*super_copy));
1662 array_end = ((u8 *)super_copy->sys_chunk_array) +
1663 btrfs_super_sys_array_size(super_copy);
1666 * we do this loop twice, once for the device items and
1667 * once for all of the chunks. This way there are device
1668 * structs filled in for every chunk
1670 ptr = super_copy->sys_chunk_array;
1672 while (ptr < array_end) {
1673 disk_key = (struct btrfs_disk_key *)ptr;
1674 btrfs_disk_key_to_cpu(&key, disk_key);
1676 len = sizeof(*disk_key);
1679 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1680 chunk = (struct btrfs_chunk *)(ptr - (u8 *)super_copy);
1681 ret = read_one_chunk(root, &key, sb, chunk);
1684 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
1685 len = btrfs_chunk_item_size(num_stripes);
1691 free_extent_buffer(sb);
1695 int btrfs_read_chunk_tree(struct btrfs_root *root)
1697 struct btrfs_path *path;
1698 struct extent_buffer *leaf;
1699 struct btrfs_key key;
1700 struct btrfs_key found_key;
1704 root = root->fs_info->chunk_root;
1706 path = btrfs_alloc_path();
1710 /* first we search for all of the device items, and then we
1711 * read in all of the chunk items. This way we can create chunk
1712 * mappings that reference all of the devices that are afound
1714 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1718 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1720 leaf = path->nodes[0];
1721 slot = path->slots[0];
1722 if (slot >= btrfs_header_nritems(leaf)) {
1723 ret = btrfs_next_leaf(root, path);
1730 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1731 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1732 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
1734 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
1735 struct btrfs_dev_item *dev_item;
1736 dev_item = btrfs_item_ptr(leaf, slot,
1737 struct btrfs_dev_item);
1738 ret = read_one_dev(root, leaf, dev_item);
1741 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
1742 struct btrfs_chunk *chunk;
1743 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1744 ret = read_one_chunk(root, &found_key, leaf, chunk);
1749 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1751 btrfs_release_path(root, path);
1757 btrfs_free_path(path);
1761 struct list_head *btrfs_scanned_uuids(void)
1766 static int rmw_eb(struct btrfs_fs_info *info,
1767 struct extent_buffer *eb, struct extent_buffer *orig_eb)
1770 unsigned long orig_off = 0;
1771 unsigned long dest_off = 0;
1772 unsigned long copy_len = eb->len;
1774 ret = read_whole_eb(info, eb, 0);
1778 if (eb->start + eb->len <= orig_eb->start ||
1779 eb->start >= orig_eb->start + orig_eb->len)
1782 * | ----- orig_eb ------- |
1783 * | ----- stripe ------- |
1784 * | ----- orig_eb ------- |
1785 * | ----- orig_eb ------- |
1787 if (eb->start > orig_eb->start)
1788 orig_off = eb->start - orig_eb->start;
1789 if (orig_eb->start > eb->start)
1790 dest_off = orig_eb->start - eb->start;
1792 if (copy_len > orig_eb->len - orig_off)
1793 copy_len = orig_eb->len - orig_off;
1794 if (copy_len > eb->len - dest_off)
1795 copy_len = eb->len - dest_off;
1797 memcpy(eb->data + dest_off, orig_eb->data + orig_off, copy_len);
1801 static void split_eb_for_raid56(struct btrfs_fs_info *info,
1802 struct extent_buffer *orig_eb,
1803 struct extent_buffer **ebs,
1804 u64 stripe_len, u64 *raid_map,
1807 struct extent_buffer *eb;
1808 u64 start = orig_eb->start;
1813 for (i = 0; i < num_stripes; i++) {
1814 if (raid_map[i] >= BTRFS_RAID5_P_STRIPE)
1817 eb = malloc(sizeof(struct extent_buffer) + stripe_len);
1820 memset(eb, 0, sizeof(struct extent_buffer) + stripe_len);
1822 eb->start = raid_map[i];
1823 eb->len = stripe_len;
1827 eb->dev_bytenr = (u64)-1;
1829 this_eb_start = raid_map[i];
1831 if (start > this_eb_start ||
1832 start + orig_eb->len < this_eb_start + stripe_len) {
1833 ret = rmw_eb(info, eb, orig_eb);
1836 memcpy(eb->data, orig_eb->data + eb->start - start, stripe_len);
1842 int write_raid56_with_parity(struct btrfs_fs_info *info,
1843 struct extent_buffer *eb,
1844 struct btrfs_multi_bio *multi,
1845 u64 stripe_len, u64 *raid_map)
1847 struct extent_buffer *ebs[multi->num_stripes], *p_eb = NULL, *q_eb = NULL;
1851 int alloc_size = eb->len;
1853 if (stripe_len > alloc_size)
1854 alloc_size = stripe_len;
1856 split_eb_for_raid56(info, eb, ebs, stripe_len, raid_map,
1857 multi->num_stripes);
1859 for (i = 0; i < multi->num_stripes; i++) {
1860 struct extent_buffer *new_eb;
1861 if (raid_map[i] < BTRFS_RAID5_P_STRIPE) {
1862 ebs[i]->dev_bytenr = multi->stripes[i].physical;
1863 ebs[i]->fd = multi->stripes[i].dev->fd;
1864 multi->stripes[i].dev->total_ios++;
1865 BUG_ON(ebs[i]->start != raid_map[i]);
1868 new_eb = kmalloc(sizeof(*eb) + alloc_size, GFP_NOFS);
1870 new_eb->dev_bytenr = multi->stripes[i].physical;
1871 new_eb->fd = multi->stripes[i].dev->fd;
1872 multi->stripes[i].dev->total_ios++;
1873 new_eb->len = stripe_len;
1875 if (raid_map[i] == BTRFS_RAID5_P_STRIPE)
1877 else if (raid_map[i] == BTRFS_RAID6_Q_STRIPE)
1881 void *pointers[multi->num_stripes];
1882 ebs[multi->num_stripes - 2] = p_eb;
1883 ebs[multi->num_stripes - 1] = q_eb;
1885 for (i = 0; i < multi->num_stripes; i++)
1886 pointers[i] = ebs[i]->data;
1888 raid6_gen_syndrome(multi->num_stripes, stripe_len, pointers);
1890 ebs[multi->num_stripes - 1] = p_eb;
1891 memcpy(p_eb->data, ebs[0]->data, stripe_len);
1892 for (j = 1; j < multi->num_stripes - 1; j++) {
1893 for (i = 0; i < stripe_len; i += sizeof(unsigned long)) {
1894 *(unsigned long *)(p_eb->data + i) ^=
1895 *(unsigned long *)(ebs[j]->data + i);
1900 for (i = 0; i < multi->num_stripes; i++) {
1901 ret = write_extent_to_disk(ebs[i]);