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;
241 disk_super = (struct btrfs_super_block *)buf;
242 ret = btrfs_read_dev_super(fd, disk_super, super_offset);
247 devid = btrfs_stack_device_id(&disk_super->dev_item);
248 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)
251 *total_devs = btrfs_super_num_devices(disk_super);
252 uuid_unparse(disk_super->fsid, uuidbuf);
254 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
263 * this uses a pretty simple search, the expectation is that it is
264 * called very infrequently and that a given device has a small number
267 static int find_free_dev_extent(struct btrfs_trans_handle *trans,
268 struct btrfs_device *device,
269 struct btrfs_path *path,
270 u64 num_bytes, u64 *start)
272 struct btrfs_key key;
273 struct btrfs_root *root = device->dev_root;
274 struct btrfs_dev_extent *dev_extent = NULL;
277 u64 search_start = 0;
278 u64 search_end = device->total_bytes;
282 struct extent_buffer *l;
287 /* FIXME use last free of some kind */
289 /* we don't want to overwrite the superblock on the drive,
290 * so we make sure to start at an offset of at least 1MB
292 search_start = max((u64)1024 * 1024, search_start);
294 if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
295 search_start = max(root->fs_info->alloc_start, search_start);
297 key.objectid = device->devid;
298 key.offset = search_start;
299 key.type = BTRFS_DEV_EXTENT_KEY;
300 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
303 ret = btrfs_previous_item(root, path, 0, key.type);
307 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
310 slot = path->slots[0];
311 if (slot >= btrfs_header_nritems(l)) {
312 ret = btrfs_next_leaf(root, path);
319 if (search_start >= search_end) {
323 *start = search_start;
327 *start = last_byte > search_start ?
328 last_byte : search_start;
329 if (search_end <= *start) {
335 btrfs_item_key_to_cpu(l, &key, slot);
337 if (key.objectid < device->devid)
340 if (key.objectid > device->devid)
343 if (key.offset >= search_start && key.offset > last_byte &&
345 if (last_byte < search_start)
346 last_byte = search_start;
347 hole_size = key.offset - last_byte;
348 if (key.offset > last_byte &&
349 hole_size >= num_bytes) {
354 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
359 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
360 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
366 /* we have to make sure we didn't find an extent that has already
367 * been allocated by the map tree or the original allocation
369 btrfs_release_path(root, path);
370 BUG_ON(*start < search_start);
372 if (*start + num_bytes > search_end) {
376 /* check for pending inserts here */
380 btrfs_release_path(root, path);
384 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
385 struct btrfs_device *device,
386 u64 chunk_tree, u64 chunk_objectid,
388 u64 num_bytes, u64 *start)
391 struct btrfs_path *path;
392 struct btrfs_root *root = device->dev_root;
393 struct btrfs_dev_extent *extent;
394 struct extent_buffer *leaf;
395 struct btrfs_key key;
397 path = btrfs_alloc_path();
401 ret = find_free_dev_extent(trans, device, path, num_bytes, start);
406 key.objectid = device->devid;
408 key.type = BTRFS_DEV_EXTENT_KEY;
409 ret = btrfs_insert_empty_item(trans, root, path, &key,
413 leaf = path->nodes[0];
414 extent = btrfs_item_ptr(leaf, path->slots[0],
415 struct btrfs_dev_extent);
416 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
417 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
418 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
420 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
421 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
424 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
425 btrfs_mark_buffer_dirty(leaf);
427 btrfs_free_path(path);
431 static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset)
433 struct btrfs_path *path;
435 struct btrfs_key key;
436 struct btrfs_chunk *chunk;
437 struct btrfs_key found_key;
439 path = btrfs_alloc_path();
442 key.objectid = objectid;
443 key.offset = (u64)-1;
444 key.type = BTRFS_CHUNK_ITEM_KEY;
446 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
452 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
456 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
458 if (found_key.objectid != objectid)
461 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
463 *offset = found_key.offset +
464 btrfs_chunk_length(path->nodes[0], chunk);
469 btrfs_free_path(path);
473 static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
477 struct btrfs_key key;
478 struct btrfs_key found_key;
480 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
481 key.type = BTRFS_DEV_ITEM_KEY;
482 key.offset = (u64)-1;
484 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
490 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
495 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
497 *objectid = found_key.offset + 1;
501 btrfs_release_path(root, path);
506 * the device information is stored in the chunk root
507 * the btrfs_device struct should be fully filled in
509 int btrfs_add_device(struct btrfs_trans_handle *trans,
510 struct btrfs_root *root,
511 struct btrfs_device *device)
514 struct btrfs_path *path;
515 struct btrfs_dev_item *dev_item;
516 struct extent_buffer *leaf;
517 struct btrfs_key key;
521 root = root->fs_info->chunk_root;
523 path = btrfs_alloc_path();
527 ret = find_next_devid(root, path, &free_devid);
531 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
532 key.type = BTRFS_DEV_ITEM_KEY;
533 key.offset = free_devid;
535 ret = btrfs_insert_empty_item(trans, root, path, &key,
540 leaf = path->nodes[0];
541 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
543 device->devid = free_devid;
544 btrfs_set_device_id(leaf, dev_item, device->devid);
545 btrfs_set_device_generation(leaf, dev_item, 0);
546 btrfs_set_device_type(leaf, dev_item, device->type);
547 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
548 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
549 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
550 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
551 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
552 btrfs_set_device_group(leaf, dev_item, 0);
553 btrfs_set_device_seek_speed(leaf, dev_item, 0);
554 btrfs_set_device_bandwidth(leaf, dev_item, 0);
555 btrfs_set_device_start_offset(leaf, dev_item, 0);
557 ptr = (unsigned long)btrfs_device_uuid(dev_item);
558 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
559 ptr = (unsigned long)btrfs_device_fsid(dev_item);
560 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
561 btrfs_mark_buffer_dirty(leaf);
565 btrfs_free_path(path);
569 int btrfs_update_device(struct btrfs_trans_handle *trans,
570 struct btrfs_device *device)
573 struct btrfs_path *path;
574 struct btrfs_root *root;
575 struct btrfs_dev_item *dev_item;
576 struct extent_buffer *leaf;
577 struct btrfs_key key;
579 root = device->dev_root->fs_info->chunk_root;
581 path = btrfs_alloc_path();
585 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
586 key.type = BTRFS_DEV_ITEM_KEY;
587 key.offset = device->devid;
589 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
598 leaf = path->nodes[0];
599 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
601 btrfs_set_device_id(leaf, dev_item, device->devid);
602 btrfs_set_device_type(leaf, dev_item, device->type);
603 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
604 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
605 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
606 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
607 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
608 btrfs_mark_buffer_dirty(leaf);
611 btrfs_free_path(path);
615 int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
616 struct btrfs_root *root,
617 struct btrfs_key *key,
618 struct btrfs_chunk *chunk, int item_size)
620 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
621 struct btrfs_disk_key disk_key;
625 array_size = btrfs_super_sys_array_size(super_copy);
626 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
629 ptr = super_copy->sys_chunk_array + array_size;
630 btrfs_cpu_key_to_disk(&disk_key, key);
631 memcpy(ptr, &disk_key, sizeof(disk_key));
632 ptr += sizeof(disk_key);
633 memcpy(ptr, chunk, item_size);
634 item_size += sizeof(disk_key);
635 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
639 static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes,
642 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
644 else if (type & BTRFS_BLOCK_GROUP_RAID10)
645 return calc_size * (num_stripes / sub_stripes);
646 else if (type & BTRFS_BLOCK_GROUP_RAID5)
647 return calc_size * (num_stripes - 1);
648 else if (type & BTRFS_BLOCK_GROUP_RAID6)
649 return calc_size * (num_stripes - 2);
651 return calc_size * num_stripes;
655 static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
657 /* TODO, add a way to store the preferred stripe size */
661 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
662 struct btrfs_root *extent_root, u64 *start,
663 u64 *num_bytes, u64 type)
666 struct btrfs_fs_info *info = extent_root->fs_info;
667 struct btrfs_root *chunk_root = info->chunk_root;
668 struct btrfs_stripe *stripes;
669 struct btrfs_device *device = NULL;
670 struct btrfs_chunk *chunk;
671 struct list_head private_devs;
672 struct list_head *dev_list = &info->fs_devices->devices;
673 struct list_head *cur;
674 struct map_lookup *map;
675 int min_stripe_size = 1 * 1024 * 1024;
676 u64 calc_size = 8 * 1024 * 1024;
678 u64 max_chunk_size = 4 * calc_size;
688 int stripe_len = 64 * 1024;
689 struct btrfs_key key;
692 if (list_empty(dev_list)) {
696 if (type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
697 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
698 BTRFS_BLOCK_GROUP_RAID10 |
699 BTRFS_BLOCK_GROUP_DUP)) {
700 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
701 calc_size = 8 * 1024 * 1024;
702 max_chunk_size = calc_size * 2;
703 min_stripe_size = 1 * 1024 * 1024;
704 } else if (type & BTRFS_BLOCK_GROUP_DATA) {
705 calc_size = 1024 * 1024 * 1024;
706 max_chunk_size = 10 * calc_size;
707 min_stripe_size = 64 * 1024 * 1024;
708 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
709 calc_size = 1024 * 1024 * 1024;
710 max_chunk_size = 4 * calc_size;
711 min_stripe_size = 32 * 1024 * 1024;
714 if (type & BTRFS_BLOCK_GROUP_RAID1) {
715 num_stripes = min_t(u64, 2,
716 btrfs_super_num_devices(info->super_copy));
721 if (type & BTRFS_BLOCK_GROUP_DUP) {
725 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
726 num_stripes = btrfs_super_num_devices(info->super_copy);
729 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
730 num_stripes = btrfs_super_num_devices(info->super_copy);
733 num_stripes &= ~(u32)1;
737 if (type & (BTRFS_BLOCK_GROUP_RAID5)) {
738 num_stripes = btrfs_super_num_devices(info->super_copy);
742 stripe_len = find_raid56_stripe_len(num_stripes - 1,
743 btrfs_super_stripesize(info->super_copy));
745 if (type & (BTRFS_BLOCK_GROUP_RAID6)) {
746 num_stripes = btrfs_super_num_devices(info->super_copy);
750 stripe_len = find_raid56_stripe_len(num_stripes - 2,
751 btrfs_super_stripesize(info->super_copy));
754 /* we don't want a chunk larger than 10% of the FS */
755 percent_max = div_factor(btrfs_super_total_bytes(info->super_copy), 1);
756 max_chunk_size = min(percent_max, max_chunk_size);
759 if (chunk_bytes_by_type(type, calc_size, num_stripes, sub_stripes) >
761 calc_size = max_chunk_size;
762 calc_size /= num_stripes;
763 calc_size /= stripe_len;
764 calc_size *= stripe_len;
766 /* we don't want tiny stripes */
767 calc_size = max_t(u64, calc_size, min_stripe_size);
769 calc_size /= stripe_len;
770 calc_size *= stripe_len;
771 INIT_LIST_HEAD(&private_devs);
772 cur = dev_list->next;
775 if (type & BTRFS_BLOCK_GROUP_DUP)
776 min_free = calc_size * 2;
778 min_free = calc_size;
780 /* build a private list of devices we will allocate from */
781 while(index < num_stripes) {
782 device = list_entry(cur, struct btrfs_device, dev_list);
783 avail = device->total_bytes - device->bytes_used;
785 if (avail >= min_free) {
786 list_move_tail(&device->dev_list, &private_devs);
788 if (type & BTRFS_BLOCK_GROUP_DUP)
790 } else if (avail > max_avail)
795 if (index < num_stripes) {
796 list_splice(&private_devs, dev_list);
797 if (index >= min_stripes) {
799 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
800 num_stripes /= sub_stripes;
801 num_stripes *= sub_stripes;
806 if (!looped && max_avail > 0) {
808 calc_size = max_avail;
813 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
817 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
818 key.type = BTRFS_CHUNK_ITEM_KEY;
821 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
825 map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
831 stripes = &chunk->stripe;
832 *num_bytes = chunk_bytes_by_type(type, calc_size,
833 num_stripes, sub_stripes);
835 while(index < num_stripes) {
836 struct btrfs_stripe *stripe;
837 BUG_ON(list_empty(&private_devs));
838 cur = private_devs.next;
839 device = list_entry(cur, struct btrfs_device, dev_list);
841 /* loop over this device again if we're doing a dup group */
842 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
843 (index == num_stripes - 1))
844 list_move_tail(&device->dev_list, dev_list);
846 ret = btrfs_alloc_dev_extent(trans, device,
847 info->chunk_root->root_key.objectid,
848 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
849 calc_size, &dev_offset);
852 device->bytes_used += calc_size;
853 ret = btrfs_update_device(trans, device);
856 map->stripes[index].dev = device;
857 map->stripes[index].physical = dev_offset;
858 stripe = stripes + index;
859 btrfs_set_stack_stripe_devid(stripe, device->devid);
860 btrfs_set_stack_stripe_offset(stripe, dev_offset);
861 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
864 BUG_ON(!list_empty(&private_devs));
866 /* key was set above */
867 btrfs_set_stack_chunk_length(chunk, *num_bytes);
868 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
869 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
870 btrfs_set_stack_chunk_type(chunk, type);
871 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
872 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
873 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
874 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
875 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
876 map->sector_size = extent_root->sectorsize;
877 map->stripe_len = stripe_len;
878 map->io_align = stripe_len;
879 map->io_width = stripe_len;
881 map->num_stripes = num_stripes;
882 map->sub_stripes = sub_stripes;
884 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
885 btrfs_chunk_item_size(num_stripes));
887 *start = key.offset;;
889 map->ce.start = key.offset;
890 map->ce.size = *num_bytes;
892 ret = insert_cache_extent(&info->mapping_tree.cache_tree, &map->ce);
895 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
896 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
897 chunk, btrfs_chunk_item_size(num_stripes));
905 int btrfs_alloc_data_chunk(struct btrfs_trans_handle *trans,
906 struct btrfs_root *extent_root, u64 *start,
907 u64 num_bytes, u64 type)
910 struct btrfs_fs_info *info = extent_root->fs_info;
911 struct btrfs_root *chunk_root = info->chunk_root;
912 struct btrfs_stripe *stripes;
913 struct btrfs_device *device = NULL;
914 struct btrfs_chunk *chunk;
915 struct list_head *dev_list = &info->fs_devices->devices;
916 struct list_head *cur;
917 struct map_lookup *map;
918 u64 calc_size = 8 * 1024 * 1024;
923 int stripe_len = 64 * 1024;
924 struct btrfs_key key;
926 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
927 key.type = BTRFS_CHUNK_ITEM_KEY;
928 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
933 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
937 map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
943 stripes = &chunk->stripe;
944 calc_size = num_bytes;
947 cur = dev_list->next;
948 device = list_entry(cur, struct btrfs_device, dev_list);
950 while (index < num_stripes) {
951 struct btrfs_stripe *stripe;
953 ret = btrfs_alloc_dev_extent(trans, device,
954 info->chunk_root->root_key.objectid,
955 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
956 calc_size, &dev_offset);
959 device->bytes_used += calc_size;
960 ret = btrfs_update_device(trans, device);
963 map->stripes[index].dev = device;
964 map->stripes[index].physical = dev_offset;
965 stripe = stripes + index;
966 btrfs_set_stack_stripe_devid(stripe, device->devid);
967 btrfs_set_stack_stripe_offset(stripe, dev_offset);
968 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
972 /* key was set above */
973 btrfs_set_stack_chunk_length(chunk, num_bytes);
974 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
975 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
976 btrfs_set_stack_chunk_type(chunk, type);
977 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
978 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
979 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
980 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
981 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
982 map->sector_size = extent_root->sectorsize;
983 map->stripe_len = stripe_len;
984 map->io_align = stripe_len;
985 map->io_width = stripe_len;
987 map->num_stripes = num_stripes;
988 map->sub_stripes = sub_stripes;
990 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
991 btrfs_chunk_item_size(num_stripes));
995 map->ce.start = key.offset;
996 map->ce.size = num_bytes;
998 ret = insert_cache_extent(&info->mapping_tree.cache_tree, &map->ce);
1005 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
1007 cache_tree_init(&tree->cache_tree);
1010 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
1012 struct cache_extent *ce;
1013 struct map_lookup *map;
1016 ce = search_cache_extent(&map_tree->cache_tree, logical);
1018 BUG_ON(ce->start > logical || ce->start + ce->size < logical);
1019 map = container_of(ce, struct map_lookup, ce);
1021 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
1022 ret = map->num_stripes;
1023 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1024 ret = map->sub_stripes;
1025 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
1027 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
1034 int btrfs_next_metadata(struct btrfs_mapping_tree *map_tree, u64 *logical,
1037 struct cache_extent *ce;
1038 struct map_lookup *map;
1040 ce = search_cache_extent(&map_tree->cache_tree, *logical);
1043 ce = next_cache_extent(ce);
1047 map = container_of(ce, struct map_lookup, ce);
1048 if (map->type & BTRFS_BLOCK_GROUP_METADATA) {
1049 *logical = ce->start;
1058 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
1059 u64 chunk_start, u64 physical, u64 devid,
1060 u64 **logical, int *naddrs, int *stripe_len)
1062 struct cache_extent *ce;
1063 struct map_lookup *map;
1071 ce = search_cache_extent(&map_tree->cache_tree, chunk_start);
1073 map = container_of(ce, struct map_lookup, ce);
1076 rmap_len = map->stripe_len;
1077 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1078 length = ce->size / (map->num_stripes / map->sub_stripes);
1079 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
1080 length = ce->size / map->num_stripes;
1081 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
1082 BTRFS_BLOCK_GROUP_RAID6)) {
1083 length = ce->size / nr_data_stripes(map);
1084 rmap_len = map->stripe_len * nr_data_stripes(map);
1087 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
1089 for (i = 0; i < map->num_stripes; i++) {
1090 if (devid && map->stripes[i].dev->devid != devid)
1092 if (map->stripes[i].physical > physical ||
1093 map->stripes[i].physical + length <= physical)
1096 stripe_nr = (physical - map->stripes[i].physical) /
1099 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1100 stripe_nr = (stripe_nr * map->num_stripes + i) /
1102 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
1103 stripe_nr = stripe_nr * map->num_stripes + i;
1104 } /* else if RAID[56], multiply by nr_data_stripes().
1105 * Alternatively, just use rmap_len below instead of
1106 * map->stripe_len */
1108 bytenr = ce->start + stripe_nr * rmap_len;
1109 for (j = 0; j < nr; j++) {
1110 if (buf[j] == bytenr)
1119 *stripe_len = rmap_len;
1124 static inline int parity_smaller(u64 a, u64 b)
1129 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
1130 static void sort_parity_stripes(struct btrfs_multi_bio *bbio, u64 *raid_map)
1132 struct btrfs_bio_stripe s;
1139 for (i = 0; i < bbio->num_stripes - 1; i++) {
1140 if (parity_smaller(raid_map[i], raid_map[i+1])) {
1141 s = bbio->stripes[i];
1143 bbio->stripes[i] = bbio->stripes[i+1];
1144 raid_map[i] = raid_map[i+1];
1145 bbio->stripes[i+1] = s;
1153 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1154 u64 logical, u64 *length,
1155 struct btrfs_multi_bio **multi_ret, int mirror_num,
1158 return __btrfs_map_block(map_tree, rw, logical, length, NULL,
1159 multi_ret, mirror_num, raid_map_ret);
1162 int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1163 u64 logical, u64 *length, u64 *type,
1164 struct btrfs_multi_bio **multi_ret, int mirror_num,
1167 struct cache_extent *ce;
1168 struct map_lookup *map;
1172 u64 *raid_map = NULL;
1173 int stripes_allocated = 8;
1174 int stripes_required = 1;
1177 struct btrfs_multi_bio *multi = NULL;
1179 if (multi_ret && rw == READ) {
1180 stripes_allocated = 1;
1183 ce = search_cache_extent(&map_tree->cache_tree, logical);
1189 if (ce->start > logical || ce->start + ce->size < logical) {
1196 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
1201 map = container_of(ce, struct map_lookup, ce);
1202 offset = logical - ce->start;
1205 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
1206 BTRFS_BLOCK_GROUP_DUP)) {
1207 stripes_required = map->num_stripes;
1208 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1209 stripes_required = map->sub_stripes;
1212 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)
1213 && multi_ret && ((rw & WRITE) || mirror_num > 1) && raid_map_ret) {
1214 /* RAID[56] write or recovery. Return all stripes */
1215 stripes_required = map->num_stripes;
1217 /* Only allocate the map if we've already got a large enough multi_ret */
1218 if (stripes_allocated >= stripes_required) {
1219 raid_map = kmalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
1227 /* if our multi bio struct is too small, back off and try again */
1228 if (multi_ret && stripes_allocated < stripes_required) {
1229 stripes_allocated = stripes_required;
1236 * stripe_nr counts the total number of stripes we have to stride
1237 * to get to this block
1239 stripe_nr = stripe_nr / map->stripe_len;
1241 stripe_offset = stripe_nr * map->stripe_len;
1242 BUG_ON(offset < stripe_offset);
1244 /* stripe_offset is the offset of this block in its stripe*/
1245 stripe_offset = offset - stripe_offset;
1247 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
1248 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
1249 BTRFS_BLOCK_GROUP_RAID10 |
1250 BTRFS_BLOCK_GROUP_DUP)) {
1251 /* we limit the length of each bio to what fits in a stripe */
1252 *length = min_t(u64, ce->size - offset,
1253 map->stripe_len - stripe_offset);
1255 *length = ce->size - offset;
1261 multi->num_stripes = 1;
1263 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
1265 multi->num_stripes = map->num_stripes;
1266 else if (mirror_num)
1267 stripe_index = mirror_num - 1;
1269 stripe_index = stripe_nr % map->num_stripes;
1270 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1271 int factor = map->num_stripes / map->sub_stripes;
1273 stripe_index = stripe_nr % factor;
1274 stripe_index *= map->sub_stripes;
1277 multi->num_stripes = map->sub_stripes;
1278 else if (mirror_num)
1279 stripe_index += mirror_num - 1;
1281 stripe_nr = stripe_nr / factor;
1282 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
1284 multi->num_stripes = map->num_stripes;
1285 else if (mirror_num)
1286 stripe_index = mirror_num - 1;
1287 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
1288 BTRFS_BLOCK_GROUP_RAID6)) {
1293 u64 raid56_full_stripe_start;
1294 u64 full_stripe_len = nr_data_stripes(map) * map->stripe_len;
1297 * align the start of our data stripe in the logical
1300 raid56_full_stripe_start = offset / full_stripe_len;
1301 raid56_full_stripe_start *= full_stripe_len;
1303 /* get the data stripe number */
1304 stripe_nr = raid56_full_stripe_start / map->stripe_len;
1305 stripe_nr = stripe_nr / nr_data_stripes(map);
1307 /* Work out the disk rotation on this stripe-set */
1308 rot = stripe_nr % map->num_stripes;
1310 /* Fill in the logical address of each stripe */
1311 tmp = stripe_nr * nr_data_stripes(map);
1313 for (i = 0; i < nr_data_stripes(map); i++)
1314 raid_map[(i+rot) % map->num_stripes] =
1315 ce->start + (tmp + i) * map->stripe_len;
1317 raid_map[(i+rot) % map->num_stripes] = BTRFS_RAID5_P_STRIPE;
1318 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
1319 raid_map[(i+rot+1) % map->num_stripes] = BTRFS_RAID6_Q_STRIPE;
1321 *length = map->stripe_len;
1324 multi->num_stripes = map->num_stripes;
1326 stripe_index = stripe_nr % nr_data_stripes(map);
1327 stripe_nr = stripe_nr / nr_data_stripes(map);
1330 * Mirror #0 or #1 means the original data block.
1331 * Mirror #2 is RAID5 parity block.
1332 * Mirror #3 is RAID6 Q block.
1335 stripe_index = nr_data_stripes(map) + mirror_num - 2;
1337 /* We distribute the parity blocks across stripes */
1338 stripe_index = (stripe_nr + stripe_index) % map->num_stripes;
1342 * after this do_div call, stripe_nr is the number of stripes
1343 * on this device we have to walk to find the data, and
1344 * stripe_index is the number of our device in the stripe array
1346 stripe_index = stripe_nr % map->num_stripes;
1347 stripe_nr = stripe_nr / map->num_stripes;
1349 BUG_ON(stripe_index >= map->num_stripes);
1351 for (i = 0; i < multi->num_stripes; i++) {
1352 multi->stripes[i].physical =
1353 map->stripes[stripe_index].physical + stripe_offset +
1354 stripe_nr * map->stripe_len;
1355 multi->stripes[i].dev = map->stripes[stripe_index].dev;
1364 sort_parity_stripes(multi, raid_map);
1365 *raid_map_ret = raid_map;
1371 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
1374 struct btrfs_device *device;
1375 struct btrfs_fs_devices *cur_devices;
1377 cur_devices = root->fs_info->fs_devices;
1378 while (cur_devices) {
1380 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
1381 device = __find_device(&cur_devices->devices,
1386 cur_devices = cur_devices->seed;
1391 struct btrfs_device *
1392 btrfs_find_device_by_devid(struct btrfs_fs_devices *fs_devices,
1393 u64 devid, int instance)
1395 struct list_head *head = &fs_devices->devices;
1396 struct btrfs_device *dev;
1399 list_for_each_entry(dev, head, dev_list) {
1400 if (dev->devid == devid && num_found++ == instance)
1406 int btrfs_bootstrap_super_map(struct btrfs_mapping_tree *map_tree,
1407 struct btrfs_fs_devices *fs_devices)
1409 struct map_lookup *map;
1410 u64 logical = BTRFS_SUPER_INFO_OFFSET;
1411 u64 length = BTRFS_SUPER_INFO_SIZE;
1412 int num_stripes = 0;
1413 int sub_stripes = 0;
1416 struct list_head *cur;
1418 list_for_each(cur, &fs_devices->devices) {
1421 map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
1425 map->ce.start = logical;
1426 map->ce.size = length;
1427 map->num_stripes = num_stripes;
1428 map->sub_stripes = sub_stripes;
1429 map->io_width = length;
1430 map->io_align = length;
1431 map->sector_size = length;
1432 map->stripe_len = length;
1433 map->type = BTRFS_BLOCK_GROUP_RAID1;
1436 list_for_each(cur, &fs_devices->devices) {
1437 struct btrfs_device *device = list_entry(cur,
1438 struct btrfs_device,
1440 map->stripes[i].physical = logical;
1441 map->stripes[i].dev = device;
1444 ret = insert_cache_extent(&map_tree->cache_tree, &map->ce);
1445 if (ret == -EEXIST) {
1446 struct cache_extent *old;
1447 struct map_lookup *old_map;
1448 old = lookup_cache_extent(&map_tree->cache_tree,
1450 old_map = container_of(old, struct map_lookup, ce);
1451 remove_cache_extent(&map_tree->cache_tree, old);
1453 ret = insert_cache_extent(&map_tree->cache_tree,
1460 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
1462 struct cache_extent *ce;
1463 struct map_lookup *map;
1464 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1468 ce = search_cache_extent(&map_tree->cache_tree, chunk_offset);
1471 map = container_of(ce, struct map_lookup, ce);
1472 for (i = 0; i < map->num_stripes; i++) {
1473 if (!map->stripes[i].dev->writeable) {
1482 static struct btrfs_device *fill_missing_device(u64 devid)
1484 struct btrfs_device *device;
1486 device = kzalloc(sizeof(*device), GFP_NOFS);
1487 device->devid = devid;
1492 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
1493 struct extent_buffer *leaf,
1494 struct btrfs_chunk *chunk)
1496 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1497 struct map_lookup *map;
1498 struct cache_extent *ce;
1502 u8 uuid[BTRFS_UUID_SIZE];
1507 logical = key->offset;
1508 length = btrfs_chunk_length(leaf, chunk);
1510 ce = search_cache_extent(&map_tree->cache_tree, logical);
1512 /* already mapped? */
1513 if (ce && ce->start <= logical && ce->start + ce->size > logical) {
1517 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1518 map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
1522 map->ce.start = logical;
1523 map->ce.size = length;
1524 map->num_stripes = num_stripes;
1525 map->io_width = btrfs_chunk_io_width(leaf, chunk);
1526 map->io_align = btrfs_chunk_io_align(leaf, chunk);
1527 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1528 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1529 map->type = btrfs_chunk_type(leaf, chunk);
1530 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
1532 for (i = 0; i < num_stripes; i++) {
1533 map->stripes[i].physical =
1534 btrfs_stripe_offset_nr(leaf, chunk, i);
1535 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
1536 read_extent_buffer(leaf, uuid, (unsigned long)
1537 btrfs_stripe_dev_uuid_nr(chunk, i),
1539 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
1541 if (!map->stripes[i].dev) {
1542 map->stripes[i].dev = fill_missing_device(devid);
1543 printf("warning, device %llu is missing\n",
1544 (unsigned long long)devid);
1548 ret = insert_cache_extent(&map_tree->cache_tree, &map->ce);
1554 static int fill_device_from_item(struct extent_buffer *leaf,
1555 struct btrfs_dev_item *dev_item,
1556 struct btrfs_device *device)
1560 device->devid = btrfs_device_id(leaf, dev_item);
1561 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1562 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1563 device->type = btrfs_device_type(leaf, dev_item);
1564 device->io_align = btrfs_device_io_align(leaf, dev_item);
1565 device->io_width = btrfs_device_io_width(leaf, dev_item);
1566 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
1568 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1569 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1574 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
1576 struct btrfs_fs_devices *fs_devices;
1579 fs_devices = root->fs_info->fs_devices->seed;
1580 while (fs_devices) {
1581 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
1585 fs_devices = fs_devices->seed;
1588 fs_devices = find_fsid(fsid);
1594 ret = btrfs_open_devices(fs_devices, O_RDONLY);
1598 fs_devices->seed = root->fs_info->fs_devices->seed;
1599 root->fs_info->fs_devices->seed = fs_devices;
1604 static int read_one_dev(struct btrfs_root *root,
1605 struct extent_buffer *leaf,
1606 struct btrfs_dev_item *dev_item)
1608 struct btrfs_device *device;
1611 u8 fs_uuid[BTRFS_UUID_SIZE];
1612 u8 dev_uuid[BTRFS_UUID_SIZE];
1614 devid = btrfs_device_id(leaf, dev_item);
1615 read_extent_buffer(leaf, dev_uuid,
1616 (unsigned long)btrfs_device_uuid(dev_item),
1618 read_extent_buffer(leaf, fs_uuid,
1619 (unsigned long)btrfs_device_fsid(dev_item),
1622 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
1623 ret = open_seed_devices(root, fs_uuid);
1628 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1630 printk("warning devid %llu not found already\n",
1631 (unsigned long long)devid);
1632 device = kzalloc(sizeof(*device), GFP_NOFS);
1636 list_add(&device->dev_list,
1637 &root->fs_info->fs_devices->devices);
1640 fill_device_from_item(leaf, dev_item, device);
1641 device->dev_root = root->fs_info->dev_root;
1645 int btrfs_read_sys_array(struct btrfs_root *root)
1647 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
1648 struct extent_buffer *sb;
1649 struct btrfs_disk_key *disk_key;
1650 struct btrfs_chunk *chunk;
1651 struct btrfs_key key;
1658 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
1659 BTRFS_SUPER_INFO_SIZE);
1662 btrfs_set_buffer_uptodate(sb);
1663 write_extent_buffer(sb, super_copy, 0, sizeof(*super_copy));
1664 array_end = ((u8 *)super_copy->sys_chunk_array) +
1665 btrfs_super_sys_array_size(super_copy);
1668 * we do this loop twice, once for the device items and
1669 * once for all of the chunks. This way there are device
1670 * structs filled in for every chunk
1672 ptr = super_copy->sys_chunk_array;
1674 while (ptr < array_end) {
1675 disk_key = (struct btrfs_disk_key *)ptr;
1676 btrfs_disk_key_to_cpu(&key, disk_key);
1678 len = sizeof(*disk_key);
1681 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1682 chunk = (struct btrfs_chunk *)(ptr - (u8 *)super_copy);
1683 ret = read_one_chunk(root, &key, sb, chunk);
1686 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
1687 len = btrfs_chunk_item_size(num_stripes);
1693 free_extent_buffer(sb);
1697 int btrfs_read_chunk_tree(struct btrfs_root *root)
1699 struct btrfs_path *path;
1700 struct extent_buffer *leaf;
1701 struct btrfs_key key;
1702 struct btrfs_key found_key;
1706 root = root->fs_info->chunk_root;
1708 path = btrfs_alloc_path();
1712 /* first we search for all of the device items, and then we
1713 * read in all of the chunk items. This way we can create chunk
1714 * mappings that reference all of the devices that are afound
1716 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1720 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1722 leaf = path->nodes[0];
1723 slot = path->slots[0];
1724 if (slot >= btrfs_header_nritems(leaf)) {
1725 ret = btrfs_next_leaf(root, path);
1732 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1733 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1734 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
1736 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
1737 struct btrfs_dev_item *dev_item;
1738 dev_item = btrfs_item_ptr(leaf, slot,
1739 struct btrfs_dev_item);
1740 ret = read_one_dev(root, leaf, dev_item);
1743 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
1744 struct btrfs_chunk *chunk;
1745 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1746 ret = read_one_chunk(root, &found_key, leaf, chunk);
1751 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1753 btrfs_release_path(root, path);
1759 btrfs_free_path(path);
1763 struct list_head *btrfs_scanned_uuids(void)
1768 static int rmw_eb(struct btrfs_fs_info *info,
1769 struct extent_buffer *eb, struct extent_buffer *orig_eb)
1772 unsigned long orig_off = 0;
1773 unsigned long dest_off = 0;
1774 unsigned long copy_len = eb->len;
1776 ret = read_whole_eb(info, eb, 0);
1780 if (eb->start + eb->len <= orig_eb->start ||
1781 eb->start >= orig_eb->start + orig_eb->len)
1784 * | ----- orig_eb ------- |
1785 * | ----- stripe ------- |
1786 * | ----- orig_eb ------- |
1787 * | ----- orig_eb ------- |
1789 if (eb->start > orig_eb->start)
1790 orig_off = eb->start - orig_eb->start;
1791 if (orig_eb->start > eb->start)
1792 dest_off = orig_eb->start - eb->start;
1794 if (copy_len > orig_eb->len - orig_off)
1795 copy_len = orig_eb->len - orig_off;
1796 if (copy_len > eb->len - dest_off)
1797 copy_len = eb->len - dest_off;
1799 memcpy(eb->data + dest_off, orig_eb->data + orig_off, copy_len);
1803 static void split_eb_for_raid56(struct btrfs_fs_info *info,
1804 struct extent_buffer *orig_eb,
1805 struct extent_buffer **ebs,
1806 u64 stripe_len, u64 *raid_map,
1809 struct extent_buffer *eb;
1810 u64 start = orig_eb->start;
1815 for (i = 0; i < num_stripes; i++) {
1816 if (raid_map[i] >= BTRFS_RAID5_P_STRIPE)
1819 eb = malloc(sizeof(struct extent_buffer) + stripe_len);
1822 memset(eb, 0, sizeof(struct extent_buffer) + stripe_len);
1824 eb->start = raid_map[i];
1825 eb->len = stripe_len;
1829 eb->dev_bytenr = (u64)-1;
1831 this_eb_start = raid_map[i];
1833 if (start > this_eb_start ||
1834 start + orig_eb->len < this_eb_start + stripe_len) {
1835 ret = rmw_eb(info, eb, orig_eb);
1838 memcpy(eb->data, orig_eb->data + eb->start - start, stripe_len);
1844 int write_raid56_with_parity(struct btrfs_fs_info *info,
1845 struct extent_buffer *eb,
1846 struct btrfs_multi_bio *multi,
1847 u64 stripe_len, u64 *raid_map)
1849 struct extent_buffer *ebs[multi->num_stripes], *p_eb = NULL, *q_eb = NULL;
1853 int alloc_size = eb->len;
1855 if (stripe_len > alloc_size)
1856 alloc_size = stripe_len;
1858 split_eb_for_raid56(info, eb, ebs, stripe_len, raid_map,
1859 multi->num_stripes);
1861 for (i = 0; i < multi->num_stripes; i++) {
1862 struct extent_buffer *new_eb;
1863 if (raid_map[i] < BTRFS_RAID5_P_STRIPE) {
1864 ebs[i]->dev_bytenr = multi->stripes[i].physical;
1865 ebs[i]->fd = multi->stripes[i].dev->fd;
1866 multi->stripes[i].dev->total_ios++;
1867 BUG_ON(ebs[i]->start != raid_map[i]);
1870 new_eb = kmalloc(sizeof(*eb) + alloc_size, GFP_NOFS);
1872 new_eb->dev_bytenr = multi->stripes[i].physical;
1873 new_eb->fd = multi->stripes[i].dev->fd;
1874 multi->stripes[i].dev->total_ios++;
1875 new_eb->len = stripe_len;
1877 if (raid_map[i] == BTRFS_RAID5_P_STRIPE)
1879 else if (raid_map[i] == BTRFS_RAID6_Q_STRIPE)
1883 void *pointers[multi->num_stripes];
1884 ebs[multi->num_stripes - 2] = p_eb;
1885 ebs[multi->num_stripes - 1] = q_eb;
1887 for (i = 0; i < multi->num_stripes; i++)
1888 pointers[i] = ebs[i]->data;
1890 raid6_gen_syndrome(multi->num_stripes, stripe_len, pointers);
1892 ebs[multi->num_stripes - 1] = p_eb;
1893 memcpy(p_eb->data, ebs[0]->data, stripe_len);
1894 for (j = 1; j < multi->num_stripes - 1; j++) {
1895 for (i = 0; i < stripe_len; i += sizeof(unsigned long)) {
1896 *(unsigned long *)(p_eb->data + i) ^=
1897 *(unsigned long *)(ebs[j]->data + i);
1902 for (i = 0; i < multi->num_stripes; i++) {
1903 ret = write_extent_to_disk(ebs[i]);