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;
39 struct cache_extent ce;
47 struct btrfs_bio_stripe stripes[];
50 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
51 (sizeof(struct btrfs_bio_stripe) * (n)))
53 static LIST_HEAD(fs_uuids);
55 static struct btrfs_device *__find_device(struct list_head *head, u64 devid,
58 struct btrfs_device *dev;
59 struct list_head *cur;
61 list_for_each(cur, head) {
62 dev = list_entry(cur, struct btrfs_device, dev_list);
63 if (dev->devid == devid &&
64 !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE)) {
71 static struct btrfs_fs_devices *find_fsid(u8 *fsid)
73 struct list_head *cur;
74 struct btrfs_fs_devices *fs_devices;
76 list_for_each(cur, &fs_uuids) {
77 fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
78 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
84 static int device_list_add(const char *path,
85 struct btrfs_super_block *disk_super,
86 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
88 struct btrfs_device *device;
89 struct btrfs_fs_devices *fs_devices;
90 u64 found_transid = btrfs_super_generation(disk_super);
92 fs_devices = find_fsid(disk_super->fsid);
94 fs_devices = kmalloc(sizeof(*fs_devices), GFP_NOFS);
97 INIT_LIST_HEAD(&fs_devices->devices);
98 list_add(&fs_devices->list, &fs_uuids);
99 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
100 fs_devices->latest_devid = devid;
101 fs_devices->latest_trans = found_transid;
102 fs_devices->lowest_devid = (u64)-1;
105 device = __find_device(&fs_devices->devices, devid,
106 disk_super->dev_item.uuid);
109 device = kzalloc(sizeof(*device), GFP_NOFS);
111 /* we can safely leave the fs_devices entry around */
114 device->devid = devid;
115 memcpy(device->uuid, disk_super->dev_item.uuid,
117 device->name = kstrdup(path, GFP_NOFS);
122 device->label = kstrdup(disk_super->label, GFP_NOFS);
123 device->total_devs = btrfs_super_num_devices(disk_super);
124 device->super_bytes_used = btrfs_super_bytes_used(disk_super);
125 device->total_bytes =
126 btrfs_stack_device_total_bytes(&disk_super->dev_item);
128 btrfs_stack_device_bytes_used(&disk_super->dev_item);
129 list_add(&device->dev_list, &fs_devices->devices);
132 if (found_transid > fs_devices->latest_trans) {
133 fs_devices->latest_devid = devid;
134 fs_devices->latest_trans = found_transid;
136 if (fs_devices->lowest_devid > devid) {
137 fs_devices->lowest_devid = devid;
139 *fs_devices_ret = fs_devices;
143 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
145 struct list_head *head = &fs_devices->devices;
146 struct list_head *cur;
147 struct btrfs_device *device;
149 list_for_each(cur, head) {
150 device = list_entry(cur, struct btrfs_device, dev_list);
156 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, int flags)
159 struct list_head *head = &fs_devices->devices;
160 struct list_head *cur;
161 struct btrfs_device *device;
164 list_for_each(cur, head) {
165 device = list_entry(cur, struct btrfs_device, dev_list);
167 fd = open(device->name, flags);
173 if (device->devid == fs_devices->latest_devid)
174 fs_devices->latest_bdev = fd;
175 if (device->devid == fs_devices->lowest_devid)
176 fs_devices->lowest_bdev = fd;
181 btrfs_close_devices(fs_devices);
185 int btrfs_scan_one_device(int fd, const char *path,
186 struct btrfs_fs_devices **fs_devices_ret,
187 u64 *total_devs, u64 super_offset)
189 struct btrfs_super_block *disk_super;
200 ret = pread(fd, buf, 4096, super_offset);
205 disk_super = (struct btrfs_super_block *)buf;
206 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
207 sizeof(disk_super->magic))) {
211 devid = le64_to_cpu(disk_super->dev_item.devid);
212 *total_devs = btrfs_super_num_devices(disk_super);
213 uuid_unparse(disk_super->fsid, uuidbuf);
215 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
224 * this uses a pretty simple search, the expectation is that it is
225 * called very infrequently and that a given device has a small number
228 static int find_free_dev_extent(struct btrfs_trans_handle *trans,
229 struct btrfs_device *device,
230 struct btrfs_path *path,
231 u64 num_bytes, u64 *start)
233 struct btrfs_key key;
234 struct btrfs_root *root = device->dev_root;
235 struct btrfs_dev_extent *dev_extent = NULL;
238 u64 search_start = 0;
239 u64 search_end = device->total_bytes;
243 struct extent_buffer *l;
248 /* FIXME use last free of some kind */
250 /* we don't want to overwrite the superblock on the drive,
251 * so we make sure to start at an offset of at least 1MB
253 search_start = max((u64)1024 * 1024, search_start);
254 key.objectid = device->devid;
255 key.offset = search_start;
256 key.type = BTRFS_DEV_EXTENT_KEY;
257 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
260 ret = btrfs_previous_item(root, path, 0, key.type);
264 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
267 slot = path->slots[0];
268 if (slot >= btrfs_header_nritems(l)) {
269 ret = btrfs_next_leaf(root, path);
276 if (search_start >= search_end) {
280 *start = search_start;
284 *start = last_byte > search_start ?
285 last_byte : search_start;
286 if (search_end <= *start) {
292 btrfs_item_key_to_cpu(l, &key, slot);
294 if (key.objectid < device->devid)
297 if (key.objectid > device->devid)
300 if (key.offset >= search_start && key.offset > last_byte &&
302 if (last_byte < search_start)
303 last_byte = search_start;
304 hole_size = key.offset - last_byte;
305 if (key.offset > last_byte &&
306 hole_size >= num_bytes) {
311 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
316 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
317 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
323 /* we have to make sure we didn't find an extent that has already
324 * been allocated by the map tree or the original allocation
326 btrfs_release_path(root, path);
327 BUG_ON(*start < search_start);
329 if (*start + num_bytes > search_end) {
333 /* check for pending inserts here */
337 btrfs_release_path(root, path);
341 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
342 struct btrfs_device *device,
343 u64 chunk_tree, u64 chunk_objectid,
345 u64 num_bytes, u64 *start)
348 struct btrfs_path *path;
349 struct btrfs_root *root = device->dev_root;
350 struct btrfs_dev_extent *extent;
351 struct extent_buffer *leaf;
352 struct btrfs_key key;
354 path = btrfs_alloc_path();
358 ret = find_free_dev_extent(trans, device, path, num_bytes, start);
363 key.objectid = device->devid;
365 key.type = BTRFS_DEV_EXTENT_KEY;
366 ret = btrfs_insert_empty_item(trans, root, path, &key,
370 leaf = path->nodes[0];
371 extent = btrfs_item_ptr(leaf, path->slots[0],
372 struct btrfs_dev_extent);
373 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
374 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
375 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
377 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
378 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
381 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
382 btrfs_mark_buffer_dirty(leaf);
384 btrfs_free_path(path);
388 static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset)
390 struct btrfs_path *path;
392 struct btrfs_key key;
393 struct btrfs_chunk *chunk;
394 struct btrfs_key found_key;
396 path = btrfs_alloc_path();
399 key.objectid = objectid;
400 key.offset = (u64)-1;
401 key.type = BTRFS_CHUNK_ITEM_KEY;
403 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
409 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
413 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
415 if (found_key.objectid != objectid)
418 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
420 *offset = found_key.offset +
421 btrfs_chunk_length(path->nodes[0], chunk);
426 btrfs_free_path(path);
430 static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
434 struct btrfs_key key;
435 struct btrfs_key found_key;
437 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
438 key.type = BTRFS_DEV_ITEM_KEY;
439 key.offset = (u64)-1;
441 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
447 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
452 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
454 *objectid = found_key.offset + 1;
458 btrfs_release_path(root, path);
463 * the device information is stored in the chunk root
464 * the btrfs_device struct should be fully filled in
466 int btrfs_add_device(struct btrfs_trans_handle *trans,
467 struct btrfs_root *root,
468 struct btrfs_device *device)
471 struct btrfs_path *path;
472 struct btrfs_dev_item *dev_item;
473 struct extent_buffer *leaf;
474 struct btrfs_key key;
478 root = root->fs_info->chunk_root;
480 path = btrfs_alloc_path();
484 ret = find_next_devid(root, path, &free_devid);
488 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
489 key.type = BTRFS_DEV_ITEM_KEY;
490 key.offset = free_devid;
492 ret = btrfs_insert_empty_item(trans, root, path, &key,
497 leaf = path->nodes[0];
498 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
500 device->devid = free_devid;
501 btrfs_set_device_id(leaf, dev_item, device->devid);
502 btrfs_set_device_type(leaf, dev_item, device->type);
503 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
504 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
505 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
506 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
507 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
508 btrfs_set_device_group(leaf, dev_item, 0);
509 btrfs_set_device_seek_speed(leaf, dev_item, 0);
510 btrfs_set_device_bandwidth(leaf, dev_item, 0);
512 ptr = (unsigned long)btrfs_device_uuid(dev_item);
513 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
514 btrfs_mark_buffer_dirty(leaf);
518 btrfs_free_path(path);
522 int btrfs_update_device(struct btrfs_trans_handle *trans,
523 struct btrfs_device *device)
526 struct btrfs_path *path;
527 struct btrfs_root *root;
528 struct btrfs_dev_item *dev_item;
529 struct extent_buffer *leaf;
530 struct btrfs_key key;
532 root = device->dev_root->fs_info->chunk_root;
534 path = btrfs_alloc_path();
538 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
539 key.type = BTRFS_DEV_ITEM_KEY;
540 key.offset = device->devid;
542 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
551 leaf = path->nodes[0];
552 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
554 btrfs_set_device_id(leaf, dev_item, device->devid);
555 btrfs_set_device_type(leaf, dev_item, device->type);
556 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
557 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
558 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
559 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
560 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
561 btrfs_mark_buffer_dirty(leaf);
564 btrfs_free_path(path);
568 int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
569 struct btrfs_root *root,
570 struct btrfs_key *key,
571 struct btrfs_chunk *chunk, int item_size)
573 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
574 struct btrfs_disk_key disk_key;
578 array_size = btrfs_super_sys_array_size(super_copy);
579 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
582 ptr = super_copy->sys_chunk_array + array_size;
583 btrfs_cpu_key_to_disk(&disk_key, key);
584 memcpy(ptr, &disk_key, sizeof(disk_key));
585 ptr += sizeof(disk_key);
586 memcpy(ptr, chunk, item_size);
587 item_size += sizeof(disk_key);
588 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
592 static u64 div_factor(u64 num, int factor)
600 static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes,
603 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
605 else if (type & BTRFS_BLOCK_GROUP_RAID10)
606 return calc_size * (num_stripes / sub_stripes);
608 return calc_size * num_stripes;
612 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
613 struct btrfs_root *extent_root, u64 *start,
614 u64 *num_bytes, u64 type)
617 struct btrfs_fs_info *info = extent_root->fs_info;
618 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
619 struct btrfs_stripe *stripes;
620 struct btrfs_device *device = NULL;
621 struct btrfs_chunk *chunk;
622 struct list_head private_devs;
623 struct list_head *dev_list = &extent_root->fs_info->fs_devices->devices;
624 struct list_head *cur;
625 struct map_lookup *map;
626 int min_stripe_size = 1 * 1024 * 1024;
628 u64 calc_size = 8 * 1024 * 1024;
630 u64 max_chunk_size = 4 * calc_size;
640 int stripe_len = 64 * 1024;
641 struct btrfs_key key;
643 if (list_empty(dev_list)) {
647 if (type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
648 BTRFS_BLOCK_GROUP_RAID10 |
649 BTRFS_BLOCK_GROUP_DUP)) {
650 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
651 calc_size = 8 * 1024 * 1024;
652 max_chunk_size = calc_size * 2;
653 min_stripe_size = 1 * 1024 * 1024;
654 } else if (type & BTRFS_BLOCK_GROUP_DATA) {
655 calc_size = 1024 * 1024 * 1024;
656 max_chunk_size = 10 * calc_size;
657 min_stripe_size = 64 * 1024 * 1024;
658 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
659 calc_size = 1024 * 1024 * 1024;
660 max_chunk_size = 4 * calc_size;
661 min_stripe_size = 32 * 1024 * 1024;
664 if (type & BTRFS_BLOCK_GROUP_RAID1) {
665 num_stripes = min_t(u64, 2,
666 btrfs_super_num_devices(&info->super_copy));
671 if (type & BTRFS_BLOCK_GROUP_DUP) {
675 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
676 num_stripes = btrfs_super_num_devices(&info->super_copy);
679 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
680 num_stripes = btrfs_super_num_devices(&info->super_copy);
683 num_stripes &= ~(u32)1;
688 /* we don't want a chunk larger than 10% of the FS */
689 percent_max = div_factor(btrfs_super_total_bytes(&info->super_copy), 1);
690 max_chunk_size = min(percent_max, max_chunk_size);
693 if (chunk_bytes_by_type(type, calc_size, num_stripes, sub_stripes) >
695 calc_size = max_chunk_size;
696 calc_size /= num_stripes;
697 calc_size /= stripe_len;
698 calc_size *= stripe_len;
700 /* we don't want tiny stripes */
701 calc_size = max_t(u64, calc_size, min_stripe_size);
703 calc_size /= stripe_len;
704 calc_size *= stripe_len;
705 INIT_LIST_HEAD(&private_devs);
706 cur = dev_list->next;
709 if (type & BTRFS_BLOCK_GROUP_DUP)
710 min_free = calc_size * 2;
712 min_free = calc_size;
714 /* build a private list of devices we will allocate from */
715 while(index < num_stripes) {
716 device = list_entry(cur, struct btrfs_device, dev_list);
717 avail = device->total_bytes - device->bytes_used;
719 if (avail >= min_free) {
720 list_move_tail(&device->dev_list, &private_devs);
722 if (type & BTRFS_BLOCK_GROUP_DUP)
724 } else if (avail > max_avail)
729 if (index < num_stripes) {
730 list_splice(&private_devs, dev_list);
731 if (index >= min_stripes) {
733 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
734 num_stripes /= sub_stripes;
735 num_stripes *= sub_stripes;
740 if (!looped && max_avail > 0) {
742 calc_size = max_avail;
747 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
748 key.type = BTRFS_CHUNK_ITEM_KEY;
749 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
754 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
758 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
764 stripes = &chunk->stripe;
765 *num_bytes = chunk_bytes_by_type(type, calc_size,
766 num_stripes, sub_stripes);
768 while(index < num_stripes) {
769 struct btrfs_stripe *stripe;
770 BUG_ON(list_empty(&private_devs));
771 cur = private_devs.next;
772 device = list_entry(cur, struct btrfs_device, dev_list);
774 /* loop over this device again if we're doing a dup group */
775 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
776 (index == num_stripes - 1))
777 list_move_tail(&device->dev_list, dev_list);
779 ret = btrfs_alloc_dev_extent(trans, device,
780 info->chunk_root->root_key.objectid,
781 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
782 calc_size, &dev_offset);
785 device->bytes_used += calc_size;
786 ret = btrfs_update_device(trans, device);
789 map->stripes[index].dev = device;
790 map->stripes[index].physical = dev_offset;
791 stripe = stripes + index;
792 btrfs_set_stack_stripe_devid(stripe, device->devid);
793 btrfs_set_stack_stripe_offset(stripe, dev_offset);
794 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
795 physical = dev_offset;
798 BUG_ON(!list_empty(&private_devs));
800 /* key was set above */
801 btrfs_set_stack_chunk_length(chunk, *num_bytes);
802 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
803 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
804 btrfs_set_stack_chunk_type(chunk, type);
805 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
806 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
807 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
808 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
809 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
810 map->sector_size = extent_root->sectorsize;
811 map->stripe_len = stripe_len;
812 map->io_align = stripe_len;
813 map->io_width = stripe_len;
815 map->num_stripes = num_stripes;
816 map->sub_stripes = sub_stripes;
818 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
819 btrfs_chunk_item_size(num_stripes));
821 *start = key.offset;;
823 map->ce.start = key.offset;
824 map->ce.size = *num_bytes;
826 ret = insert_existing_cache_extent(
827 &extent_root->fs_info->mapping_tree.cache_tree,
831 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
832 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
833 chunk, btrfs_chunk_item_size(num_stripes));
841 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
843 cache_tree_init(&tree->cache_tree);
846 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
848 struct cache_extent *ce;
849 struct map_lookup *map;
853 ce = find_first_cache_extent(&map_tree->cache_tree, logical);
855 BUG_ON(ce->start > logical || ce->start + ce->size < logical);
856 map = container_of(ce, struct map_lookup, ce);
858 offset = logical - ce->start;
859 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
860 ret = map->num_stripes;
861 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
862 ret = map->sub_stripes;
868 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
869 u64 logical, u64 *length,
870 struct btrfs_multi_bio **multi_ret, int mirror_num)
872 struct cache_extent *ce;
873 struct map_lookup *map;
877 int stripes_allocated = 8;
878 int stripes_required = 1;
881 struct btrfs_multi_bio *multi = NULL;
883 if (multi_ret && rw == READ) {
884 stripes_allocated = 1;
888 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
894 ce = find_first_cache_extent(&map_tree->cache_tree, logical);
896 BUG_ON(ce->start > logical || ce->start + ce->size < logical);
897 map = container_of(ce, struct map_lookup, ce);
898 offset = logical - ce->start;
901 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
902 BTRFS_BLOCK_GROUP_DUP)) {
903 stripes_required = map->num_stripes;
904 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
905 stripes_required = map->sub_stripes;
908 /* if our multi bio struct is too small, back off and try again */
909 if (multi_ret && rw == WRITE &&
910 stripes_allocated < stripes_required) {
911 stripes_allocated = map->num_stripes;
917 * stripe_nr counts the total number of stripes we have to stride
918 * to get to this block
920 stripe_nr = stripe_nr / map->stripe_len;
922 stripe_offset = stripe_nr * map->stripe_len;
923 BUG_ON(offset < stripe_offset);
925 /* stripe_offset is the offset of this block in its stripe*/
926 stripe_offset = offset - stripe_offset;
928 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
929 BTRFS_BLOCK_GROUP_RAID10 |
930 BTRFS_BLOCK_GROUP_DUP)) {
931 /* we limit the length of each bio to what fits in a stripe */
932 *length = min_t(u64, ce->size - offset,
933 map->stripe_len - stripe_offset);
935 *length = ce->size - offset;
941 multi->num_stripes = 1;
943 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
945 multi->num_stripes = map->num_stripes;
947 stripe_index = mirror_num - 1;
949 stripe_index = stripe_nr % map->num_stripes;
950 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
951 int factor = map->num_stripes / map->sub_stripes;
953 stripe_index = stripe_nr % factor;
954 stripe_index *= map->sub_stripes;
957 multi->num_stripes = map->sub_stripes;
959 stripe_index += mirror_num - 1;
961 stripe_index = stripe_nr % map->sub_stripes;
963 stripe_nr = stripe_nr / factor;
964 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
966 multi->num_stripes = map->num_stripes;
968 stripe_index = mirror_num - 1;
971 * after this do_div call, stripe_nr is the number of stripes
972 * on this device we have to walk to find the data, and
973 * stripe_index is the number of our device in the stripe array
975 stripe_index = stripe_nr % map->num_stripes;
976 stripe_nr = stripe_nr / map->num_stripes;
978 BUG_ON(stripe_index >= map->num_stripes);
980 BUG_ON(stripe_index != 0 && multi->num_stripes > 1);
981 for (i = 0; i < multi->num_stripes; i++) {
982 multi->stripes[i].physical =
983 map->stripes[stripe_index].physical + stripe_offset +
984 stripe_nr * map->stripe_len;
985 multi->stripes[i].dev = map->stripes[stripe_index].dev;
993 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
996 struct list_head *head = &root->fs_info->fs_devices->devices;
998 return __find_device(head, devid, uuid);
1001 int btrfs_bootstrap_super_map(struct btrfs_mapping_tree *map_tree,
1002 struct btrfs_fs_devices *fs_devices)
1004 struct map_lookup *map;
1005 u64 logical = BTRFS_SUPER_INFO_OFFSET;
1006 u64 length = BTRFS_SUPER_INFO_SIZE;
1007 int num_stripes = 0;
1008 int sub_stripes = 0;
1011 struct list_head *cur;
1013 list_for_each(cur, &fs_devices->devices) {
1016 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1020 map->ce.start = logical;
1021 map->ce.size = length;
1022 map->num_stripes = num_stripes;
1023 map->sub_stripes = sub_stripes;
1024 map->io_width = length;
1025 map->io_align = length;
1026 map->sector_size = length;
1027 map->stripe_len = length;
1028 map->type = BTRFS_BLOCK_GROUP_RAID1;
1031 list_for_each(cur, &fs_devices->devices) {
1032 struct btrfs_device *device = list_entry(cur,
1033 struct btrfs_device,
1035 map->stripes[i].physical = logical;
1036 map->stripes[i].dev = device;
1039 ret = insert_existing_cache_extent(&map_tree->cache_tree, &map->ce);
1040 if (ret == -EEXIST) {
1041 struct cache_extent *old;
1042 struct map_lookup *old_map;
1043 old = find_cache_extent(&map_tree->cache_tree, logical, length);
1044 old_map = container_of(old, struct map_lookup, ce);
1045 remove_cache_extent(&map_tree->cache_tree, old);
1047 ret = insert_existing_cache_extent(&map_tree->cache_tree,
1054 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
1055 struct extent_buffer *leaf,
1056 struct btrfs_chunk *chunk)
1058 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1059 struct map_lookup *map;
1060 struct cache_extent *ce;
1064 u64 super_offset_diff = 0;
1065 u8 uuid[BTRFS_UUID_SIZE];
1070 logical = key->offset;
1071 length = btrfs_chunk_length(leaf, chunk);
1073 if (logical < BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE) {
1074 super_offset_diff = BTRFS_SUPER_INFO_OFFSET +
1075 BTRFS_SUPER_INFO_SIZE - logical;
1076 logical = BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE;
1079 ce = find_first_cache_extent(&map_tree->cache_tree, logical);
1081 /* already mapped? */
1082 if (ce && ce->start <= logical && ce->start + ce->size > logical) {
1086 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1087 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1091 map->ce.start = logical;
1092 map->ce.size = length - super_offset_diff;
1093 map->num_stripes = num_stripes;
1094 map->io_width = btrfs_chunk_io_width(leaf, chunk);
1095 map->io_align = btrfs_chunk_io_align(leaf, chunk);
1096 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1097 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1098 map->type = btrfs_chunk_type(leaf, chunk);
1099 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
1101 for (i = 0; i < num_stripes; i++) {
1102 map->stripes[i].physical =
1103 btrfs_stripe_offset_nr(leaf, chunk, i) +
1105 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
1106 read_extent_buffer(leaf, uuid, (unsigned long)
1107 btrfs_stripe_dev_uuid_nr(chunk, i),
1109 map->stripes[i].dev = btrfs_find_device(root, devid, uuid);
1110 if (!map->stripes[i].dev) {
1116 ret = insert_existing_cache_extent(&map_tree->cache_tree, &map->ce);
1122 static int fill_device_from_item(struct extent_buffer *leaf,
1123 struct btrfs_dev_item *dev_item,
1124 struct btrfs_device *device)
1128 device->devid = btrfs_device_id(leaf, dev_item);
1129 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1130 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1131 device->type = btrfs_device_type(leaf, dev_item);
1132 device->io_align = btrfs_device_io_align(leaf, dev_item);
1133 device->io_width = btrfs_device_io_width(leaf, dev_item);
1134 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
1136 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1137 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1142 static int read_one_dev(struct btrfs_root *root,
1143 struct extent_buffer *leaf,
1144 struct btrfs_dev_item *dev_item)
1146 struct btrfs_device *device;
1149 u8 dev_uuid[BTRFS_UUID_SIZE];
1151 devid = btrfs_device_id(leaf, dev_item);
1152 read_extent_buffer(leaf, dev_uuid,
1153 (unsigned long)btrfs_device_uuid(dev_item),
1155 device = btrfs_find_device(root, devid, dev_uuid);
1157 printk("warning devid %llu not found already\n",
1158 (unsigned long long)devid);
1159 device = kmalloc(sizeof(*device), GFP_NOFS);
1162 device->total_ios = 0;
1163 list_add(&device->dev_list,
1164 &root->fs_info->fs_devices->devices);
1167 fill_device_from_item(leaf, dev_item, device);
1168 device->dev_root = root->fs_info->dev_root;
1172 int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
1174 struct btrfs_dev_item *dev_item;
1176 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
1178 return read_one_dev(root, buf, dev_item);
1181 int btrfs_read_sys_array(struct btrfs_root *root)
1183 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1184 struct extent_buffer *sb = root->fs_info->sb_buffer;
1185 struct btrfs_disk_key *disk_key;
1186 struct btrfs_chunk *chunk;
1187 struct btrfs_key key;
1192 unsigned long sb_ptr;
1196 array_size = btrfs_super_sys_array_size(super_copy);
1199 * we do this loop twice, once for the device items and
1200 * once for all of the chunks. This way there are device
1201 * structs filled in for every chunk
1203 ptr = super_copy->sys_chunk_array;
1204 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
1207 while (cur < array_size) {
1208 disk_key = (struct btrfs_disk_key *)ptr;
1209 btrfs_disk_key_to_cpu(&key, disk_key);
1211 len = sizeof(*disk_key);
1216 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1217 chunk = (struct btrfs_chunk *)sb_ptr;
1218 ret = read_one_chunk(root, &key, sb, chunk);
1220 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
1221 len = btrfs_chunk_item_size(num_stripes);
1232 int btrfs_read_chunk_tree(struct btrfs_root *root)
1234 struct btrfs_path *path;
1235 struct extent_buffer *leaf;
1236 struct btrfs_key key;
1237 struct btrfs_key found_key;
1241 root = root->fs_info->chunk_root;
1243 path = btrfs_alloc_path();
1247 /* first we search for all of the device items, and then we
1248 * read in all of the chunk items. This way we can create chunk
1249 * mappings that reference all of the devices that are afound
1251 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1255 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1257 leaf = path->nodes[0];
1258 slot = path->slots[0];
1259 if (slot >= btrfs_header_nritems(leaf)) {
1260 ret = btrfs_next_leaf(root, path);
1267 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1268 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1269 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
1271 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
1272 struct btrfs_dev_item *dev_item;
1273 dev_item = btrfs_item_ptr(leaf, slot,
1274 struct btrfs_dev_item);
1275 ret = read_one_dev(root, leaf, dev_item);
1278 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
1279 struct btrfs_chunk *chunk;
1280 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1281 ret = read_one_chunk(root, &found_key, leaf, chunk);
1285 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1287 btrfs_release_path(root, path);
1291 btrfs_free_path(path);
1297 struct list_head *btrfs_scanned_uuids(void)