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 = kzalloc(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);
130 device->fs_devices = fs_devices;
133 if (found_transid > fs_devices->latest_trans) {
134 fs_devices->latest_devid = devid;
135 fs_devices->latest_trans = found_transid;
137 if (fs_devices->lowest_devid > devid) {
138 fs_devices->lowest_devid = devid;
140 *fs_devices_ret = fs_devices;
144 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
146 struct btrfs_fs_devices *seed_devices;
147 struct list_head *cur;
148 struct btrfs_device *device;
150 list_for_each(cur, &fs_devices->devices) {
151 device = list_entry(cur, struct btrfs_device, dev_list);
154 device->writeable = 0;
157 seed_devices = fs_devices->seed;
158 fs_devices->seed = NULL;
160 fs_devices = seed_devices;
167 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, int flags)
170 struct list_head *head = &fs_devices->devices;
171 struct list_head *cur;
172 struct btrfs_device *device;
175 list_for_each(cur, head) {
176 device = list_entry(cur, struct btrfs_device, dev_list);
178 fd = open(device->name, flags);
184 if (device->devid == fs_devices->latest_devid)
185 fs_devices->latest_bdev = fd;
186 if (device->devid == fs_devices->lowest_devid)
187 fs_devices->lowest_bdev = fd;
190 device->writeable = 1;
194 btrfs_close_devices(fs_devices);
198 int btrfs_scan_one_device(int fd, const char *path,
199 struct btrfs_fs_devices **fs_devices_ret,
200 u64 *total_devs, u64 super_offset)
202 struct btrfs_super_block *disk_super;
213 ret = pread(fd, buf, 4096, super_offset);
218 disk_super = (struct btrfs_super_block *)buf;
219 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
220 sizeof(disk_super->magic))) {
224 devid = le64_to_cpu(disk_super->dev_item.devid);
225 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)
228 *total_devs = btrfs_super_num_devices(disk_super);
229 uuid_unparse(disk_super->fsid, uuidbuf);
231 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
240 * this uses a pretty simple search, the expectation is that it is
241 * called very infrequently and that a given device has a small number
244 static int find_free_dev_extent(struct btrfs_trans_handle *trans,
245 struct btrfs_device *device,
246 struct btrfs_path *path,
247 u64 num_bytes, u64 *start)
249 struct btrfs_key key;
250 struct btrfs_root *root = device->dev_root;
251 struct btrfs_dev_extent *dev_extent = NULL;
254 u64 search_start = 0;
255 u64 search_end = device->total_bytes;
259 struct extent_buffer *l;
264 /* FIXME use last free of some kind */
266 /* we don't want to overwrite the superblock on the drive,
267 * so we make sure to start at an offset of at least 1MB
269 search_start = max((u64)1024 * 1024, search_start);
271 if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
272 search_start = max(root->fs_info->alloc_start, search_start);
274 key.objectid = device->devid;
275 key.offset = search_start;
276 key.type = BTRFS_DEV_EXTENT_KEY;
277 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
280 ret = btrfs_previous_item(root, path, 0, key.type);
284 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
287 slot = path->slots[0];
288 if (slot >= btrfs_header_nritems(l)) {
289 ret = btrfs_next_leaf(root, path);
296 if (search_start >= search_end) {
300 *start = search_start;
304 *start = last_byte > search_start ?
305 last_byte : search_start;
306 if (search_end <= *start) {
312 btrfs_item_key_to_cpu(l, &key, slot);
314 if (key.objectid < device->devid)
317 if (key.objectid > device->devid)
320 if (key.offset >= search_start && key.offset > last_byte &&
322 if (last_byte < search_start)
323 last_byte = search_start;
324 hole_size = key.offset - last_byte;
325 if (key.offset > last_byte &&
326 hole_size >= num_bytes) {
331 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
336 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
337 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
343 /* we have to make sure we didn't find an extent that has already
344 * been allocated by the map tree or the original allocation
346 btrfs_release_path(root, path);
347 BUG_ON(*start < search_start);
349 if (*start + num_bytes > search_end) {
353 /* check for pending inserts here */
357 btrfs_release_path(root, path);
361 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
362 struct btrfs_device *device,
363 u64 chunk_tree, u64 chunk_objectid,
365 u64 num_bytes, u64 *start)
368 struct btrfs_path *path;
369 struct btrfs_root *root = device->dev_root;
370 struct btrfs_dev_extent *extent;
371 struct extent_buffer *leaf;
372 struct btrfs_key key;
374 path = btrfs_alloc_path();
378 ret = find_free_dev_extent(trans, device, path, num_bytes, start);
383 key.objectid = device->devid;
385 key.type = BTRFS_DEV_EXTENT_KEY;
386 ret = btrfs_insert_empty_item(trans, root, path, &key,
390 leaf = path->nodes[0];
391 extent = btrfs_item_ptr(leaf, path->slots[0],
392 struct btrfs_dev_extent);
393 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
394 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
395 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
397 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
398 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
401 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
402 btrfs_mark_buffer_dirty(leaf);
404 btrfs_free_path(path);
408 static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset)
410 struct btrfs_path *path;
412 struct btrfs_key key;
413 struct btrfs_chunk *chunk;
414 struct btrfs_key found_key;
416 path = btrfs_alloc_path();
419 key.objectid = objectid;
420 key.offset = (u64)-1;
421 key.type = BTRFS_CHUNK_ITEM_KEY;
423 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
429 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
433 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
435 if (found_key.objectid != objectid)
438 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
440 *offset = found_key.offset +
441 btrfs_chunk_length(path->nodes[0], chunk);
446 btrfs_free_path(path);
450 static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
454 struct btrfs_key key;
455 struct btrfs_key found_key;
457 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
458 key.type = BTRFS_DEV_ITEM_KEY;
459 key.offset = (u64)-1;
461 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
467 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
472 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
474 *objectid = found_key.offset + 1;
478 btrfs_release_path(root, path);
483 * the device information is stored in the chunk root
484 * the btrfs_device struct should be fully filled in
486 int btrfs_add_device(struct btrfs_trans_handle *trans,
487 struct btrfs_root *root,
488 struct btrfs_device *device)
491 struct btrfs_path *path;
492 struct btrfs_dev_item *dev_item;
493 struct extent_buffer *leaf;
494 struct btrfs_key key;
498 root = root->fs_info->chunk_root;
500 path = btrfs_alloc_path();
504 ret = find_next_devid(root, path, &free_devid);
508 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
509 key.type = BTRFS_DEV_ITEM_KEY;
510 key.offset = free_devid;
512 ret = btrfs_insert_empty_item(trans, root, path, &key,
517 leaf = path->nodes[0];
518 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
520 device->devid = free_devid;
521 btrfs_set_device_id(leaf, dev_item, device->devid);
522 btrfs_set_device_generation(leaf, dev_item, 0);
523 btrfs_set_device_type(leaf, dev_item, device->type);
524 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
525 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
526 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
527 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
528 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
529 btrfs_set_device_group(leaf, dev_item, 0);
530 btrfs_set_device_seek_speed(leaf, dev_item, 0);
531 btrfs_set_device_bandwidth(leaf, dev_item, 0);
532 btrfs_set_device_start_offset(leaf, dev_item, 0);
534 ptr = (unsigned long)btrfs_device_uuid(dev_item);
535 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
536 ptr = (unsigned long)btrfs_device_fsid(dev_item);
537 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
538 btrfs_mark_buffer_dirty(leaf);
542 btrfs_free_path(path);
546 int btrfs_update_device(struct btrfs_trans_handle *trans,
547 struct btrfs_device *device)
550 struct btrfs_path *path;
551 struct btrfs_root *root;
552 struct btrfs_dev_item *dev_item;
553 struct extent_buffer *leaf;
554 struct btrfs_key key;
556 root = device->dev_root->fs_info->chunk_root;
558 path = btrfs_alloc_path();
562 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
563 key.type = BTRFS_DEV_ITEM_KEY;
564 key.offset = device->devid;
566 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
575 leaf = path->nodes[0];
576 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
578 btrfs_set_device_id(leaf, dev_item, device->devid);
579 btrfs_set_device_type(leaf, dev_item, device->type);
580 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
581 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
582 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
583 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
584 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
585 btrfs_mark_buffer_dirty(leaf);
588 btrfs_free_path(path);
592 int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
593 struct btrfs_root *root,
594 struct btrfs_key *key,
595 struct btrfs_chunk *chunk, int item_size)
597 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
598 struct btrfs_disk_key disk_key;
602 array_size = btrfs_super_sys_array_size(super_copy);
603 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
606 ptr = super_copy->sys_chunk_array + array_size;
607 btrfs_cpu_key_to_disk(&disk_key, key);
608 memcpy(ptr, &disk_key, sizeof(disk_key));
609 ptr += sizeof(disk_key);
610 memcpy(ptr, chunk, item_size);
611 item_size += sizeof(disk_key);
612 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
616 static u64 div_factor(u64 num, int factor)
624 static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes,
627 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
629 else if (type & BTRFS_BLOCK_GROUP_RAID10)
630 return calc_size * (num_stripes / sub_stripes);
632 return calc_size * num_stripes;
636 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
637 struct btrfs_root *extent_root, u64 *start,
638 u64 *num_bytes, u64 type)
641 struct btrfs_fs_info *info = extent_root->fs_info;
642 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
643 struct btrfs_stripe *stripes;
644 struct btrfs_device *device = NULL;
645 struct btrfs_chunk *chunk;
646 struct list_head private_devs;
647 struct list_head *dev_list = &extent_root->fs_info->fs_devices->devices;
648 struct list_head *cur;
649 struct map_lookup *map;
650 int min_stripe_size = 1 * 1024 * 1024;
652 u64 calc_size = 8 * 1024 * 1024;
654 u64 max_chunk_size = 4 * calc_size;
664 int stripe_len = 64 * 1024;
665 struct btrfs_key key;
667 if (list_empty(dev_list)) {
671 if (type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
672 BTRFS_BLOCK_GROUP_RAID10 |
673 BTRFS_BLOCK_GROUP_DUP)) {
674 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
675 calc_size = 8 * 1024 * 1024;
676 max_chunk_size = calc_size * 2;
677 min_stripe_size = 1 * 1024 * 1024;
678 } else if (type & BTRFS_BLOCK_GROUP_DATA) {
679 calc_size = 1024 * 1024 * 1024;
680 max_chunk_size = 10 * calc_size;
681 min_stripe_size = 64 * 1024 * 1024;
682 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
683 calc_size = 1024 * 1024 * 1024;
684 max_chunk_size = 4 * calc_size;
685 min_stripe_size = 32 * 1024 * 1024;
688 if (type & BTRFS_BLOCK_GROUP_RAID1) {
689 num_stripes = min_t(u64, 2,
690 btrfs_super_num_devices(&info->super_copy));
695 if (type & BTRFS_BLOCK_GROUP_DUP) {
699 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
700 num_stripes = btrfs_super_num_devices(&info->super_copy);
703 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
704 num_stripes = btrfs_super_num_devices(&info->super_copy);
707 num_stripes &= ~(u32)1;
712 /* we don't want a chunk larger than 10% of the FS */
713 percent_max = div_factor(btrfs_super_total_bytes(&info->super_copy), 1);
714 max_chunk_size = min(percent_max, max_chunk_size);
717 if (chunk_bytes_by_type(type, calc_size, num_stripes, sub_stripes) >
719 calc_size = max_chunk_size;
720 calc_size /= num_stripes;
721 calc_size /= stripe_len;
722 calc_size *= stripe_len;
724 /* we don't want tiny stripes */
725 calc_size = max_t(u64, calc_size, min_stripe_size);
727 calc_size /= stripe_len;
728 calc_size *= stripe_len;
729 INIT_LIST_HEAD(&private_devs);
730 cur = dev_list->next;
733 if (type & BTRFS_BLOCK_GROUP_DUP)
734 min_free = calc_size * 2;
736 min_free = calc_size;
738 /* build a private list of devices we will allocate from */
739 while(index < num_stripes) {
740 device = list_entry(cur, struct btrfs_device, dev_list);
741 avail = device->total_bytes - device->bytes_used;
743 if (avail >= min_free) {
744 list_move_tail(&device->dev_list, &private_devs);
746 if (type & BTRFS_BLOCK_GROUP_DUP)
748 } else if (avail > max_avail)
753 if (index < num_stripes) {
754 list_splice(&private_devs, dev_list);
755 if (index >= min_stripes) {
757 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
758 num_stripes /= sub_stripes;
759 num_stripes *= sub_stripes;
764 if (!looped && max_avail > 0) {
766 calc_size = max_avail;
771 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
772 key.type = BTRFS_CHUNK_ITEM_KEY;
773 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
778 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
782 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
788 stripes = &chunk->stripe;
789 *num_bytes = chunk_bytes_by_type(type, calc_size,
790 num_stripes, sub_stripes);
792 while(index < num_stripes) {
793 struct btrfs_stripe *stripe;
794 BUG_ON(list_empty(&private_devs));
795 cur = private_devs.next;
796 device = list_entry(cur, struct btrfs_device, dev_list);
798 /* loop over this device again if we're doing a dup group */
799 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
800 (index == num_stripes - 1))
801 list_move_tail(&device->dev_list, dev_list);
803 ret = btrfs_alloc_dev_extent(trans, device,
804 info->chunk_root->root_key.objectid,
805 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
806 calc_size, &dev_offset);
809 device->bytes_used += calc_size;
810 ret = btrfs_update_device(trans, device);
813 map->stripes[index].dev = device;
814 map->stripes[index].physical = dev_offset;
815 stripe = stripes + index;
816 btrfs_set_stack_stripe_devid(stripe, device->devid);
817 btrfs_set_stack_stripe_offset(stripe, dev_offset);
818 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
819 physical = dev_offset;
822 BUG_ON(!list_empty(&private_devs));
824 /* key was set above */
825 btrfs_set_stack_chunk_length(chunk, *num_bytes);
826 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
827 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
828 btrfs_set_stack_chunk_type(chunk, type);
829 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
830 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
831 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
832 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
833 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
834 map->sector_size = extent_root->sectorsize;
835 map->stripe_len = stripe_len;
836 map->io_align = stripe_len;
837 map->io_width = stripe_len;
839 map->num_stripes = num_stripes;
840 map->sub_stripes = sub_stripes;
842 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
843 btrfs_chunk_item_size(num_stripes));
845 *start = key.offset;;
847 map->ce.start = key.offset;
848 map->ce.size = *num_bytes;
850 ret = insert_existing_cache_extent(
851 &extent_root->fs_info->mapping_tree.cache_tree,
855 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
856 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
857 chunk, btrfs_chunk_item_size(num_stripes));
865 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
867 cache_tree_init(&tree->cache_tree);
870 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
872 struct cache_extent *ce;
873 struct map_lookup *map;
877 ce = find_first_cache_extent(&map_tree->cache_tree, logical);
879 BUG_ON(ce->start > logical || ce->start + ce->size < logical);
880 map = container_of(ce, struct map_lookup, ce);
882 offset = logical - ce->start;
883 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
884 ret = map->num_stripes;
885 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
886 ret = map->sub_stripes;
892 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
893 u64 chunk_start, u64 physical, u64 devid,
894 u64 **logical, int *naddrs, int *stripe_len)
896 struct cache_extent *ce;
897 struct map_lookup *map;
904 ce = find_first_cache_extent(&map_tree->cache_tree, chunk_start);
905 BUG_ON(!ce || ce->start != chunk_start);
906 map = container_of(ce, struct map_lookup, ce);
909 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
910 length = ce->size / (map->num_stripes / map->sub_stripes);
911 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
912 length = ce->size / map->num_stripes;
914 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
916 for (i = 0; i < map->num_stripes; i++) {
917 if (devid && map->stripes[i].dev->devid != devid)
919 if (map->stripes[i].physical > physical ||
920 map->stripes[i].physical + length <= physical)
923 stripe_nr = (physical - map->stripes[i].physical) /
926 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
927 stripe_nr = (stripe_nr * map->num_stripes + i) /
929 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
930 stripe_nr = stripe_nr * map->num_stripes + i;
932 bytenr = chunk_start + stripe_nr * map->stripe_len;
933 for (j = 0; j < nr; j++) {
934 if (buf[j] == bytenr)
943 *stripe_len = map->stripe_len;
948 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
949 u64 logical, u64 *length,
950 struct btrfs_multi_bio **multi_ret, int mirror_num)
952 struct cache_extent *ce;
953 struct map_lookup *map;
957 int stripes_allocated = 8;
958 int stripes_required = 1;
961 struct btrfs_multi_bio *multi = NULL;
963 if (multi_ret && rw == READ) {
964 stripes_allocated = 1;
968 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
974 ce = find_first_cache_extent(&map_tree->cache_tree, logical);
976 BUG_ON(ce->start > logical || ce->start + ce->size < logical);
977 map = container_of(ce, struct map_lookup, ce);
978 offset = logical - ce->start;
981 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
982 BTRFS_BLOCK_GROUP_DUP)) {
983 stripes_required = map->num_stripes;
984 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
985 stripes_required = map->sub_stripes;
988 /* if our multi bio struct is too small, back off and try again */
989 if (multi_ret && rw == WRITE &&
990 stripes_allocated < stripes_required) {
991 stripes_allocated = map->num_stripes;
997 * stripe_nr counts the total number of stripes we have to stride
998 * to get to this block
1000 stripe_nr = stripe_nr / map->stripe_len;
1002 stripe_offset = stripe_nr * map->stripe_len;
1003 BUG_ON(offset < stripe_offset);
1005 /* stripe_offset is the offset of this block in its stripe*/
1006 stripe_offset = offset - stripe_offset;
1008 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
1009 BTRFS_BLOCK_GROUP_RAID10 |
1010 BTRFS_BLOCK_GROUP_DUP)) {
1011 /* we limit the length of each bio to what fits in a stripe */
1012 *length = min_t(u64, ce->size - offset,
1013 map->stripe_len - stripe_offset);
1015 *length = ce->size - offset;
1021 multi->num_stripes = 1;
1023 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
1025 multi->num_stripes = map->num_stripes;
1026 else if (mirror_num)
1027 stripe_index = mirror_num - 1;
1029 stripe_index = stripe_nr % map->num_stripes;
1030 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1031 int factor = map->num_stripes / map->sub_stripes;
1033 stripe_index = stripe_nr % factor;
1034 stripe_index *= map->sub_stripes;
1037 multi->num_stripes = map->sub_stripes;
1038 else if (mirror_num)
1039 stripe_index += mirror_num - 1;
1041 stripe_index = stripe_nr % map->sub_stripes;
1043 stripe_nr = stripe_nr / factor;
1044 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
1046 multi->num_stripes = map->num_stripes;
1047 else if (mirror_num)
1048 stripe_index = mirror_num - 1;
1051 * after this do_div call, stripe_nr is the number of stripes
1052 * on this device we have to walk to find the data, and
1053 * stripe_index is the number of our device in the stripe array
1055 stripe_index = stripe_nr % map->num_stripes;
1056 stripe_nr = stripe_nr / map->num_stripes;
1058 BUG_ON(stripe_index >= map->num_stripes);
1060 for (i = 0; i < multi->num_stripes; i++) {
1061 multi->stripes[i].physical =
1062 map->stripes[stripe_index].physical + stripe_offset +
1063 stripe_nr * map->stripe_len;
1064 multi->stripes[i].dev = map->stripes[stripe_index].dev;
1072 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
1075 struct btrfs_device *device;
1076 struct btrfs_fs_devices *cur_devices;
1078 cur_devices = root->fs_info->fs_devices;
1079 while (cur_devices) {
1081 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
1082 device = __find_device(&cur_devices->devices,
1087 cur_devices = cur_devices->seed;
1092 int btrfs_bootstrap_super_map(struct btrfs_mapping_tree *map_tree,
1093 struct btrfs_fs_devices *fs_devices)
1095 struct map_lookup *map;
1096 u64 logical = BTRFS_SUPER_INFO_OFFSET;
1097 u64 length = BTRFS_SUPER_INFO_SIZE;
1098 int num_stripes = 0;
1099 int sub_stripes = 0;
1102 struct list_head *cur;
1104 list_for_each(cur, &fs_devices->devices) {
1107 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1111 map->ce.start = logical;
1112 map->ce.size = length;
1113 map->num_stripes = num_stripes;
1114 map->sub_stripes = sub_stripes;
1115 map->io_width = length;
1116 map->io_align = length;
1117 map->sector_size = length;
1118 map->stripe_len = length;
1119 map->type = BTRFS_BLOCK_GROUP_RAID1;
1122 list_for_each(cur, &fs_devices->devices) {
1123 struct btrfs_device *device = list_entry(cur,
1124 struct btrfs_device,
1126 map->stripes[i].physical = logical;
1127 map->stripes[i].dev = device;
1130 ret = insert_existing_cache_extent(&map_tree->cache_tree, &map->ce);
1131 if (ret == -EEXIST) {
1132 struct cache_extent *old;
1133 struct map_lookup *old_map;
1134 old = find_cache_extent(&map_tree->cache_tree, logical, length);
1135 old_map = container_of(old, struct map_lookup, ce);
1136 remove_cache_extent(&map_tree->cache_tree, old);
1138 ret = insert_existing_cache_extent(&map_tree->cache_tree,
1145 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
1147 struct cache_extent *ce;
1148 struct map_lookup *map;
1149 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1153 ce = find_first_cache_extent(&map_tree->cache_tree, chunk_offset);
1156 map = container_of(ce, struct map_lookup, ce);
1157 for (i = 0; i < map->num_stripes; i++) {
1158 if (!map->stripes[i].dev->writeable) {
1167 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
1168 struct extent_buffer *leaf,
1169 struct btrfs_chunk *chunk)
1171 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1172 struct map_lookup *map;
1173 struct cache_extent *ce;
1177 u8 uuid[BTRFS_UUID_SIZE];
1182 logical = key->offset;
1183 length = btrfs_chunk_length(leaf, chunk);
1185 ce = find_first_cache_extent(&map_tree->cache_tree, logical);
1187 /* already mapped? */
1188 if (ce && ce->start <= logical && ce->start + ce->size > logical) {
1192 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1193 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1197 map->ce.start = logical;
1198 map->ce.size = length;
1199 map->num_stripes = num_stripes;
1200 map->io_width = btrfs_chunk_io_width(leaf, chunk);
1201 map->io_align = btrfs_chunk_io_align(leaf, chunk);
1202 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1203 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1204 map->type = btrfs_chunk_type(leaf, chunk);
1205 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
1207 for (i = 0; i < num_stripes; i++) {
1208 map->stripes[i].physical =
1209 btrfs_stripe_offset_nr(leaf, chunk, i);
1210 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
1211 read_extent_buffer(leaf, uuid, (unsigned long)
1212 btrfs_stripe_dev_uuid_nr(chunk, i),
1214 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
1216 if (!map->stripes[i].dev) {
1222 ret = insert_existing_cache_extent(&map_tree->cache_tree, &map->ce);
1228 static int fill_device_from_item(struct extent_buffer *leaf,
1229 struct btrfs_dev_item *dev_item,
1230 struct btrfs_device *device)
1234 device->devid = btrfs_device_id(leaf, dev_item);
1235 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1236 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1237 device->type = btrfs_device_type(leaf, dev_item);
1238 device->io_align = btrfs_device_io_align(leaf, dev_item);
1239 device->io_width = btrfs_device_io_width(leaf, dev_item);
1240 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
1242 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1243 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1248 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
1250 struct btrfs_fs_devices *fs_devices;
1253 fs_devices = root->fs_info->fs_devices->seed;
1254 while (fs_devices) {
1255 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
1259 fs_devices = fs_devices->seed;
1262 fs_devices = find_fsid(fsid);
1268 ret = btrfs_open_devices(fs_devices, O_RDONLY);
1272 fs_devices->seed = root->fs_info->fs_devices->seed;
1273 root->fs_info->fs_devices->seed = fs_devices;
1278 static int read_one_dev(struct btrfs_root *root,
1279 struct extent_buffer *leaf,
1280 struct btrfs_dev_item *dev_item)
1282 struct btrfs_device *device;
1285 u8 fs_uuid[BTRFS_UUID_SIZE];
1286 u8 dev_uuid[BTRFS_UUID_SIZE];
1288 devid = btrfs_device_id(leaf, dev_item);
1289 read_extent_buffer(leaf, dev_uuid,
1290 (unsigned long)btrfs_device_uuid(dev_item),
1292 read_extent_buffer(leaf, fs_uuid,
1293 (unsigned long)btrfs_device_fsid(dev_item),
1296 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
1297 ret = open_seed_devices(root, fs_uuid);
1302 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1304 printk("warning devid %llu not found already\n",
1305 (unsigned long long)devid);
1306 device = kmalloc(sizeof(*device), GFP_NOFS);
1309 device->total_ios = 0;
1310 list_add(&device->dev_list,
1311 &root->fs_info->fs_devices->devices);
1314 fill_device_from_item(leaf, dev_item, device);
1315 device->dev_root = root->fs_info->dev_root;
1319 int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
1321 struct btrfs_dev_item *dev_item;
1323 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
1325 return read_one_dev(root, buf, dev_item);
1328 int btrfs_read_sys_array(struct btrfs_root *root)
1330 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1331 struct extent_buffer *sb = root->fs_info->sb_buffer;
1332 struct btrfs_disk_key *disk_key;
1333 struct btrfs_chunk *chunk;
1334 struct btrfs_key key;
1339 unsigned long sb_ptr;
1343 array_size = btrfs_super_sys_array_size(super_copy);
1346 * we do this loop twice, once for the device items and
1347 * once for all of the chunks. This way there are device
1348 * structs filled in for every chunk
1350 ptr = super_copy->sys_chunk_array;
1351 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
1354 while (cur < array_size) {
1355 disk_key = (struct btrfs_disk_key *)ptr;
1356 btrfs_disk_key_to_cpu(&key, disk_key);
1358 len = sizeof(*disk_key);
1363 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1364 chunk = (struct btrfs_chunk *)sb_ptr;
1365 ret = read_one_chunk(root, &key, sb, chunk);
1367 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
1368 len = btrfs_chunk_item_size(num_stripes);
1379 int btrfs_read_chunk_tree(struct btrfs_root *root)
1381 struct btrfs_path *path;
1382 struct extent_buffer *leaf;
1383 struct btrfs_key key;
1384 struct btrfs_key found_key;
1388 root = root->fs_info->chunk_root;
1390 path = btrfs_alloc_path();
1394 /* first we search for all of the device items, and then we
1395 * read in all of the chunk items. This way we can create chunk
1396 * mappings that reference all of the devices that are afound
1398 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1402 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1404 leaf = path->nodes[0];
1405 slot = path->slots[0];
1406 if (slot >= btrfs_header_nritems(leaf)) {
1407 ret = btrfs_next_leaf(root, path);
1414 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1415 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1416 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
1418 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
1419 struct btrfs_dev_item *dev_item;
1420 dev_item = btrfs_item_ptr(leaf, slot,
1421 struct btrfs_dev_item);
1422 ret = read_one_dev(root, leaf, dev_item);
1425 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
1426 struct btrfs_chunk *chunk;
1427 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1428 ret = read_one_chunk(root, &found_key, leaf, chunk);
1433 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1435 btrfs_release_path(root, path);
1439 btrfs_free_path(path);
1445 struct list_head *btrfs_scanned_uuids(void)