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 #include <linux/sched.h>
19 #include <linux/bio.h>
20 #include <linux/buffer_head.h>
21 #include <asm/div64.h>
23 #include "extent_map.h"
25 #include "transaction.h"
26 #include "print-tree.h"
37 struct btrfs_bio_stripe stripes[];
40 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
41 (sizeof(struct btrfs_bio_stripe) * (n)))
43 static DEFINE_MUTEX(uuid_mutex);
44 static LIST_HEAD(fs_uuids);
46 int btrfs_cleanup_fs_uuids(void)
48 struct btrfs_fs_devices *fs_devices;
49 struct list_head *uuid_cur;
50 struct list_head *devices_cur;
51 struct btrfs_device *dev;
53 list_for_each(uuid_cur, &fs_uuids) {
54 fs_devices = list_entry(uuid_cur, struct btrfs_fs_devices,
56 while(!list_empty(&fs_devices->devices)) {
57 devices_cur = fs_devices->devices.next;
58 dev = list_entry(devices_cur, struct btrfs_device,
61 close_bdev_excl(dev->bdev);
63 list_del(&dev->dev_list);
70 static struct btrfs_device *__find_device(struct list_head *head, u64 devid,
73 struct btrfs_device *dev;
74 struct list_head *cur;
76 list_for_each(cur, head) {
77 dev = list_entry(cur, struct btrfs_device, dev_list);
78 if (dev->devid == devid &&
79 !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE)) {
86 static struct btrfs_fs_devices *find_fsid(u8 *fsid)
88 struct list_head *cur;
89 struct btrfs_fs_devices *fs_devices;
91 list_for_each(cur, &fs_uuids) {
92 fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
93 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
99 static int device_list_add(const char *path,
100 struct btrfs_super_block *disk_super,
101 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
103 struct btrfs_device *device;
104 struct btrfs_fs_devices *fs_devices;
105 u64 found_transid = btrfs_super_generation(disk_super);
107 fs_devices = find_fsid(disk_super->fsid);
109 fs_devices = kmalloc(sizeof(*fs_devices), GFP_NOFS);
112 INIT_LIST_HEAD(&fs_devices->devices);
113 list_add(&fs_devices->list, &fs_uuids);
114 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
115 fs_devices->latest_devid = devid;
116 fs_devices->latest_trans = found_transid;
117 fs_devices->lowest_devid = (u64)-1;
118 fs_devices->num_devices = 0;
121 device = __find_device(&fs_devices->devices, devid,
122 disk_super->dev_item.uuid);
125 device = kzalloc(sizeof(*device), GFP_NOFS);
127 /* we can safely leave the fs_devices entry around */
130 device->devid = devid;
131 memcpy(device->uuid, disk_super->dev_item.uuid,
133 device->barriers = 1;
134 spin_lock_init(&device->io_lock);
135 device->name = kstrdup(path, GFP_NOFS);
140 list_add(&device->dev_list, &fs_devices->devices);
141 fs_devices->num_devices++;
144 if (found_transid > fs_devices->latest_trans) {
145 fs_devices->latest_devid = devid;
146 fs_devices->latest_trans = found_transid;
148 if (fs_devices->lowest_devid > devid) {
149 fs_devices->lowest_devid = devid;
151 *fs_devices_ret = fs_devices;
155 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
157 struct list_head *head = &fs_devices->devices;
158 struct list_head *cur;
159 struct btrfs_device *device;
161 mutex_lock(&uuid_mutex);
162 list_for_each(cur, head) {
163 device = list_entry(cur, struct btrfs_device, dev_list);
165 close_bdev_excl(device->bdev);
169 mutex_unlock(&uuid_mutex);
173 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
174 int flags, void *holder)
176 struct block_device *bdev;
177 struct list_head *head = &fs_devices->devices;
178 struct list_head *cur;
179 struct btrfs_device *device;
182 mutex_lock(&uuid_mutex);
183 list_for_each(cur, head) {
184 device = list_entry(cur, struct btrfs_device, dev_list);
185 bdev = open_bdev_excl(device->name, flags, holder);
188 printk("open %s failed\n", device->name);
192 if (device->devid == fs_devices->latest_devid)
193 fs_devices->latest_bdev = bdev;
194 if (device->devid == fs_devices->lowest_devid) {
195 fs_devices->lowest_bdev = bdev;
199 mutex_unlock(&uuid_mutex);
202 mutex_unlock(&uuid_mutex);
203 btrfs_close_devices(fs_devices);
207 int btrfs_scan_one_device(const char *path, int flags, void *holder,
208 struct btrfs_fs_devices **fs_devices_ret)
210 struct btrfs_super_block *disk_super;
211 struct block_device *bdev;
212 struct buffer_head *bh;
217 mutex_lock(&uuid_mutex);
219 bdev = open_bdev_excl(path, flags, holder);
226 ret = set_blocksize(bdev, 4096);
229 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
234 disk_super = (struct btrfs_super_block *)bh->b_data;
235 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
236 sizeof(disk_super->magic))) {
240 devid = le64_to_cpu(disk_super->dev_item.devid);
241 transid = btrfs_super_generation(disk_super);
242 if (disk_super->label[0])
243 printk("device label %s ", disk_super->label);
245 /* FIXME, make a readl uuid parser */
246 printk("device fsid %llx-%llx ",
247 *(unsigned long long *)disk_super->fsid,
248 *(unsigned long long *)(disk_super->fsid + 8));
250 printk("devid %Lu transid %Lu %s\n", devid, transid, path);
251 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
256 close_bdev_excl(bdev);
258 mutex_unlock(&uuid_mutex);
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);
293 key.objectid = device->devid;
294 key.offset = search_start;
295 key.type = BTRFS_DEV_EXTENT_KEY;
296 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
299 ret = btrfs_previous_item(root, path, 0, key.type);
303 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
306 slot = path->slots[0];
307 if (slot >= btrfs_header_nritems(l)) {
308 ret = btrfs_next_leaf(root, path);
315 if (search_start >= search_end) {
319 *start = search_start;
323 *start = last_byte > search_start ?
324 last_byte : search_start;
325 if (search_end <= *start) {
331 btrfs_item_key_to_cpu(l, &key, slot);
333 if (key.objectid < device->devid)
336 if (key.objectid > device->devid)
339 if (key.offset >= search_start && key.offset > last_byte &&
341 if (last_byte < search_start)
342 last_byte = search_start;
343 hole_size = key.offset - last_byte;
344 if (key.offset > last_byte &&
345 hole_size >= num_bytes) {
350 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
355 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
356 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
362 /* we have to make sure we didn't find an extent that has already
363 * been allocated by the map tree or the original allocation
365 btrfs_release_path(root, path);
366 BUG_ON(*start < search_start);
368 if (*start + num_bytes > search_end) {
372 /* check for pending inserts here */
376 btrfs_release_path(root, path);
380 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
381 struct btrfs_device *device,
382 u64 chunk_tree, u64 chunk_objectid,
384 u64 num_bytes, u64 *start)
387 struct btrfs_path *path;
388 struct btrfs_root *root = device->dev_root;
389 struct btrfs_dev_extent *extent;
390 struct extent_buffer *leaf;
391 struct btrfs_key key;
393 path = btrfs_alloc_path();
397 ret = find_free_dev_extent(trans, device, path, num_bytes, start);
402 key.objectid = device->devid;
404 key.type = BTRFS_DEV_EXTENT_KEY;
405 ret = btrfs_insert_empty_item(trans, root, path, &key,
409 leaf = path->nodes[0];
410 extent = btrfs_item_ptr(leaf, path->slots[0],
411 struct btrfs_dev_extent);
412 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
413 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
414 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
416 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
417 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
420 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
421 btrfs_mark_buffer_dirty(leaf);
423 btrfs_free_path(path);
427 static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset)
429 struct btrfs_path *path;
431 struct btrfs_key key;
432 struct btrfs_chunk *chunk;
433 struct btrfs_key found_key;
435 path = btrfs_alloc_path();
438 key.objectid = objectid;
439 key.offset = (u64)-1;
440 key.type = BTRFS_CHUNK_ITEM_KEY;
442 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
448 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
452 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
454 if (found_key.objectid != objectid)
457 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
459 *offset = found_key.offset +
460 btrfs_chunk_length(path->nodes[0], chunk);
465 btrfs_free_path(path);
469 static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
473 struct btrfs_key key;
474 struct btrfs_key found_key;
476 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
477 key.type = BTRFS_DEV_ITEM_KEY;
478 key.offset = (u64)-1;
480 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
486 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
491 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
493 *objectid = found_key.offset + 1;
497 btrfs_release_path(root, path);
502 * the device information is stored in the chunk root
503 * the btrfs_device struct should be fully filled in
505 int btrfs_add_device(struct btrfs_trans_handle *trans,
506 struct btrfs_root *root,
507 struct btrfs_device *device)
510 struct btrfs_path *path;
511 struct btrfs_dev_item *dev_item;
512 struct extent_buffer *leaf;
513 struct btrfs_key key;
517 root = root->fs_info->chunk_root;
519 path = btrfs_alloc_path();
523 ret = find_next_devid(root, path, &free_devid);
527 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
528 key.type = BTRFS_DEV_ITEM_KEY;
529 key.offset = free_devid;
531 ret = btrfs_insert_empty_item(trans, root, path, &key,
536 leaf = path->nodes[0];
537 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
539 device->devid = free_devid;
540 btrfs_set_device_id(leaf, dev_item, device->devid);
541 btrfs_set_device_type(leaf, dev_item, device->type);
542 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
543 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
544 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
545 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
546 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
547 btrfs_set_device_group(leaf, dev_item, 0);
548 btrfs_set_device_seek_speed(leaf, dev_item, 0);
549 btrfs_set_device_bandwidth(leaf, dev_item, 0);
551 ptr = (unsigned long)btrfs_device_uuid(dev_item);
552 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
553 btrfs_mark_buffer_dirty(leaf);
557 btrfs_free_path(path);
560 int btrfs_update_device(struct btrfs_trans_handle *trans,
561 struct btrfs_device *device)
564 struct btrfs_path *path;
565 struct btrfs_root *root;
566 struct btrfs_dev_item *dev_item;
567 struct extent_buffer *leaf;
568 struct btrfs_key key;
570 root = device->dev_root->fs_info->chunk_root;
572 path = btrfs_alloc_path();
576 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
577 key.type = BTRFS_DEV_ITEM_KEY;
578 key.offset = device->devid;
580 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
589 leaf = path->nodes[0];
590 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
592 btrfs_set_device_id(leaf, dev_item, device->devid);
593 btrfs_set_device_type(leaf, dev_item, device->type);
594 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
595 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
596 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
597 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
598 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
599 btrfs_mark_buffer_dirty(leaf);
602 btrfs_free_path(path);
606 int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
607 struct btrfs_root *root,
608 struct btrfs_key *key,
609 struct btrfs_chunk *chunk, int item_size)
611 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
612 struct btrfs_disk_key disk_key;
616 array_size = btrfs_super_sys_array_size(super_copy);
617 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
620 ptr = super_copy->sys_chunk_array + array_size;
621 btrfs_cpu_key_to_disk(&disk_key, key);
622 memcpy(ptr, &disk_key, sizeof(disk_key));
623 ptr += sizeof(disk_key);
624 memcpy(ptr, chunk, item_size);
625 item_size += sizeof(disk_key);
626 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
630 static u64 div_factor(u64 num, int factor)
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);
647 return calc_size * num_stripes;
651 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
652 struct btrfs_root *extent_root, u64 *start,
653 u64 *num_bytes, u64 type)
656 struct btrfs_fs_info *info = extent_root->fs_info;
657 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
658 struct btrfs_stripe *stripes;
659 struct btrfs_device *device = NULL;
660 struct btrfs_chunk *chunk;
661 struct list_head private_devs;
662 struct list_head *dev_list = &extent_root->fs_info->fs_devices->devices;
663 struct list_head *cur;
664 struct extent_map_tree *em_tree;
665 struct map_lookup *map;
666 struct extent_map *em;
667 int min_chunk_size = 8 * 1024 * 1024;
669 u64 calc_size = 1024 * 1024 * 1024;
670 u64 max_chunk_size = calc_size;
680 int stripe_len = 64 * 1024;
681 struct btrfs_key key;
683 if (list_empty(dev_list))
686 if (type & (BTRFS_BLOCK_GROUP_RAID0))
687 num_stripes = btrfs_super_num_devices(&info->super_copy);
688 if (type & (BTRFS_BLOCK_GROUP_DUP))
690 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
691 num_stripes = min_t(u64, 2,
692 btrfs_super_num_devices(&info->super_copy));
696 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
697 num_stripes = btrfs_super_num_devices(&info->super_copy);
700 num_stripes &= ~(u32)1;
704 if (type & BTRFS_BLOCK_GROUP_DATA) {
705 max_chunk_size = 10 * calc_size;
706 min_chunk_size = 256 * 1024 * 1024;
707 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
708 max_chunk_size = 4 * calc_size;
709 min_chunk_size = 64 * 1024 * 1024;
711 min_chunk_size = 32 * 1024 * 1024;
714 /* we don't want a chunk larger than 10% of the FS */
715 percent_max = div_factor(btrfs_super_total_bytes(&info->super_copy), 1);
716 max_chunk_size = min(percent_max, max_chunk_size);
718 if (calc_size * num_stripes > max_chunk_size) {
719 calc_size = max_chunk_size;
720 do_div(calc_size, num_stripes);
721 do_div(calc_size, stripe_len);
722 calc_size *= stripe_len;
724 /* we don't want tiny stripes */
725 *num_bytes = chunk_bytes_by_type(type, calc_size,
726 num_stripes, sub_stripes);
727 calc_size = max_t(u64, chunk_bytes_by_type(type, min_chunk_size,
728 num_stripes, sub_stripes), calc_size);
731 do_div(calc_size, stripe_len);
732 calc_size *= stripe_len;
734 INIT_LIST_HEAD(&private_devs);
735 cur = dev_list->next;
738 if (type & BTRFS_BLOCK_GROUP_DUP)
739 min_free = calc_size * 2;
741 min_free = calc_size;
743 /* build a private list of devices we will allocate from */
744 while(index < num_stripes) {
745 device = list_entry(cur, struct btrfs_device, dev_list);
747 avail = device->total_bytes - device->bytes_used;
749 if (avail > max_avail)
751 if (avail >= min_free) {
752 list_move_tail(&device->dev_list, &private_devs);
754 if (type & BTRFS_BLOCK_GROUP_DUP)
760 if (index < num_stripes) {
761 list_splice(&private_devs, dev_list);
762 if (!looped && max_avail > 0) {
764 calc_size = max_avail;
770 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
771 key.type = BTRFS_CHUNK_ITEM_KEY;
772 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
777 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
781 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
787 stripes = &chunk->stripe;
788 *num_bytes = chunk_bytes_by_type(type, calc_size,
789 num_stripes, sub_stripes);
793 printk("new chunk type %Lu start %Lu size %Lu\n", type, key.offset, *num_bytes);
794 while(index < num_stripes) {
795 struct btrfs_stripe *stripe;
796 BUG_ON(list_empty(&private_devs));
797 cur = private_devs.next;
798 device = list_entry(cur, struct btrfs_device, dev_list);
800 /* loop over this device again if we're doing a dup group */
801 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
802 (index == num_stripes - 1))
803 list_move_tail(&device->dev_list, dev_list);
805 ret = btrfs_alloc_dev_extent(trans, device,
806 info->chunk_root->root_key.objectid,
807 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
808 calc_size, &dev_offset);
810 printk("alloc chunk start %Lu size %Lu from dev %Lu type %Lu\n", key.offset, calc_size, device->devid, type);
811 device->bytes_used += calc_size;
812 ret = btrfs_update_device(trans, device);
815 map->stripes[index].dev = device;
816 map->stripes[index].physical = dev_offset;
817 stripe = stripes + index;
818 btrfs_set_stack_stripe_devid(stripe, device->devid);
819 btrfs_set_stack_stripe_offset(stripe, dev_offset);
820 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
821 physical = dev_offset;
824 BUG_ON(!list_empty(&private_devs));
826 /* key was set above */
827 btrfs_set_stack_chunk_length(chunk, *num_bytes);
828 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
829 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
830 btrfs_set_stack_chunk_type(chunk, type);
831 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
832 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
833 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
834 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
835 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
836 map->sector_size = extent_root->sectorsize;
837 map->stripe_len = stripe_len;
838 map->io_align = stripe_len;
839 map->io_width = stripe_len;
841 map->num_stripes = num_stripes;
842 map->sub_stripes = sub_stripes;
844 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
845 btrfs_chunk_item_size(num_stripes));
847 *start = key.offset;;
849 em = alloc_extent_map(GFP_NOFS);
852 em->bdev = (struct block_device *)map;
853 em->start = key.offset;
854 em->len = *num_bytes;
859 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
860 spin_lock(&em_tree->lock);
861 ret = add_extent_mapping(em_tree, em);
862 spin_unlock(&em_tree->lock);
868 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
870 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
873 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
875 struct extent_map *em;
878 spin_lock(&tree->map_tree.lock);
879 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
881 remove_extent_mapping(&tree->map_tree, em);
882 spin_unlock(&tree->map_tree.lock);
888 /* once for the tree */
893 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
895 struct extent_map *em;
896 struct map_lookup *map;
897 struct extent_map_tree *em_tree = &map_tree->map_tree;
900 spin_lock(&em_tree->lock);
901 em = lookup_extent_mapping(em_tree, logical, len);
902 spin_unlock(&em_tree->lock);
905 BUG_ON(em->start > logical || em->start + em->len < logical);
906 map = (struct map_lookup *)em->bdev;
907 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
908 ret = map->num_stripes;
909 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
910 ret = map->sub_stripes;
917 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
918 u64 logical, u64 *length,
919 struct btrfs_multi_bio **multi_ret, int mirror_num)
921 struct extent_map *em;
922 struct map_lookup *map;
923 struct extent_map_tree *em_tree = &map_tree->map_tree;
927 int stripes_allocated = 8;
928 int stripes_required = 1;
931 struct btrfs_multi_bio *multi = NULL;
933 if (multi_ret && !(rw & (1 << BIO_RW))) {
934 stripes_allocated = 1;
938 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
944 spin_lock(&em_tree->lock);
945 em = lookup_extent_mapping(em_tree, logical, *length);
946 spin_unlock(&em_tree->lock);
948 printk("unable to find logical %Lu\n", logical);
952 BUG_ON(em->start > logical || em->start + em->len < logical);
953 map = (struct map_lookup *)em->bdev;
954 offset = logical - em->start;
956 if (mirror_num > map->num_stripes)
959 /* if our multi bio struct is too small, back off and try again */
960 if (rw & (1 << BIO_RW)) {
961 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
962 BTRFS_BLOCK_GROUP_DUP)) {
963 stripes_required = map->num_stripes;
964 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
965 stripes_required = map->sub_stripes;
968 if (multi_ret && rw == WRITE &&
969 stripes_allocated < stripes_required) {
970 stripes_allocated = map->num_stripes;
977 * stripe_nr counts the total number of stripes we have to stride
978 * to get to this block
980 do_div(stripe_nr, map->stripe_len);
982 stripe_offset = stripe_nr * map->stripe_len;
983 BUG_ON(offset < stripe_offset);
985 /* stripe_offset is the offset of this block in its stripe*/
986 stripe_offset = offset - stripe_offset;
988 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
989 BTRFS_BLOCK_GROUP_RAID10 |
990 BTRFS_BLOCK_GROUP_DUP)) {
991 /* we limit the length of each bio to what fits in a stripe */
992 *length = min_t(u64, em->len - offset,
993 map->stripe_len - stripe_offset);
995 *length = em->len - offset;
1000 multi->num_stripes = 1;
1002 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
1003 if (rw & (1 << BIO_RW))
1004 multi->num_stripes = map->num_stripes;
1005 else if (mirror_num) {
1006 stripe_index = mirror_num - 1;
1009 u64 least = (u64)-1;
1010 struct btrfs_device *cur;
1012 for (i = 0; i < map->num_stripes; i++) {
1013 cur = map->stripes[i].dev;
1014 spin_lock(&cur->io_lock);
1015 if (cur->total_ios < least) {
1016 least = cur->total_ios;
1019 spin_unlock(&cur->io_lock);
1022 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
1023 if (rw & (1 << BIO_RW))
1024 multi->num_stripes = map->num_stripes;
1025 else if (mirror_num)
1026 stripe_index = mirror_num - 1;
1027 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1028 int factor = map->num_stripes / map->sub_stripes;
1029 int orig_stripe_nr = stripe_nr;
1031 stripe_index = do_div(stripe_nr, factor);
1032 stripe_index *= map->sub_stripes;
1034 if (rw & (1 << BIO_RW))
1035 multi->num_stripes = map->sub_stripes;
1036 else if (mirror_num)
1037 stripe_index += mirror_num - 1;
1039 stripe_index += orig_stripe_nr % map->sub_stripes;
1042 * after this do_div call, stripe_nr is the number of stripes
1043 * on this device we have to walk to find the data, and
1044 * stripe_index is the number of our device in the stripe array
1046 stripe_index = do_div(stripe_nr, map->num_stripes);
1048 BUG_ON(stripe_index >= map->num_stripes);
1050 for (i = 0; i < multi->num_stripes; i++) {
1051 multi->stripes[i].physical =
1052 map->stripes[stripe_index].physical + stripe_offset +
1053 stripe_nr * map->stripe_len;
1054 multi->stripes[i].dev = map->stripes[stripe_index].dev;
1059 free_extent_map(em);
1063 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1064 static void end_bio_multi_stripe(struct bio *bio, int err)
1066 static int end_bio_multi_stripe(struct bio *bio,
1067 unsigned int bytes_done, int err)
1070 struct btrfs_multi_bio *multi = bio->bi_private;
1072 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1079 if (atomic_dec_and_test(&multi->stripes_pending)) {
1080 bio->bi_private = multi->private;
1081 bio->bi_end_io = multi->end_io;
1083 if (!err && multi->error)
1087 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1088 bio_endio(bio, bio->bi_size, err);
1090 bio_endio(bio, err);
1095 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1100 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
1103 struct btrfs_mapping_tree *map_tree;
1104 struct btrfs_device *dev;
1105 struct bio *first_bio = bio;
1106 u64 logical = bio->bi_sector << 9;
1109 struct bio_vec *bvec;
1110 struct btrfs_multi_bio *multi = NULL;
1116 bio_for_each_segment(bvec, bio, i) {
1117 length += bvec->bv_len;
1120 map_tree = &root->fs_info->mapping_tree;
1121 map_length = length;
1123 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
1127 total_devs = multi->num_stripes;
1128 if (map_length < length) {
1129 printk("mapping failed logical %Lu bio len %Lu "
1130 "len %Lu\n", logical, length, map_length);
1133 multi->end_io = first_bio->bi_end_io;
1134 multi->private = first_bio->bi_private;
1135 atomic_set(&multi->stripes_pending, multi->num_stripes);
1137 while(dev_nr < total_devs) {
1138 if (total_devs > 1) {
1139 if (dev_nr < total_devs - 1) {
1140 bio = bio_clone(first_bio, GFP_NOFS);
1145 bio->bi_private = multi;
1146 bio->bi_end_io = end_bio_multi_stripe;
1148 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
1149 dev = multi->stripes[dev_nr].dev;
1150 bio->bi_bdev = dev->bdev;
1151 spin_lock(&dev->io_lock);
1153 spin_unlock(&dev->io_lock);
1154 submit_bio(rw, bio);
1157 if (total_devs == 1)
1162 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
1165 struct list_head *head = &root->fs_info->fs_devices->devices;
1167 return __find_device(head, devid, uuid);
1170 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
1171 struct extent_buffer *leaf,
1172 struct btrfs_chunk *chunk)
1174 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1175 struct map_lookup *map;
1176 struct extent_map *em;
1180 u8 uuid[BTRFS_UUID_SIZE];
1185 logical = key->offset;
1186 length = btrfs_chunk_length(leaf, chunk);
1187 spin_lock(&map_tree->map_tree.lock);
1188 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
1189 spin_unlock(&map_tree->map_tree.lock);
1191 /* already mapped? */
1192 if (em && em->start <= logical && em->start + em->len > logical) {
1193 free_extent_map(em);
1196 free_extent_map(em);
1199 map = kzalloc(sizeof(*map), GFP_NOFS);
1203 em = alloc_extent_map(GFP_NOFS);
1206 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1207 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1209 free_extent_map(em);
1213 em->bdev = (struct block_device *)map;
1214 em->start = logical;
1216 em->block_start = 0;
1218 map->num_stripes = num_stripes;
1219 map->io_width = btrfs_chunk_io_width(leaf, chunk);
1220 map->io_align = btrfs_chunk_io_align(leaf, chunk);
1221 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1222 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1223 map->type = btrfs_chunk_type(leaf, chunk);
1224 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
1225 for (i = 0; i < num_stripes; i++) {
1226 map->stripes[i].physical =
1227 btrfs_stripe_offset_nr(leaf, chunk, i);
1228 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
1229 read_extent_buffer(leaf, uuid, (unsigned long)
1230 btrfs_stripe_dev_uuid_nr(chunk, i),
1232 map->stripes[i].dev = btrfs_find_device(root, devid, uuid);
1233 if (!map->stripes[i].dev) {
1235 free_extent_map(em);
1240 spin_lock(&map_tree->map_tree.lock);
1241 ret = add_extent_mapping(&map_tree->map_tree, em);
1242 spin_unlock(&map_tree->map_tree.lock);
1244 free_extent_map(em);
1249 static int fill_device_from_item(struct extent_buffer *leaf,
1250 struct btrfs_dev_item *dev_item,
1251 struct btrfs_device *device)
1255 device->devid = btrfs_device_id(leaf, dev_item);
1256 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1257 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1258 device->type = btrfs_device_type(leaf, dev_item);
1259 device->io_align = btrfs_device_io_align(leaf, dev_item);
1260 device->io_width = btrfs_device_io_width(leaf, dev_item);
1261 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
1263 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1264 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1269 static int read_one_dev(struct btrfs_root *root,
1270 struct extent_buffer *leaf,
1271 struct btrfs_dev_item *dev_item)
1273 struct btrfs_device *device;
1276 u8 dev_uuid[BTRFS_UUID_SIZE];
1278 devid = btrfs_device_id(leaf, dev_item);
1279 read_extent_buffer(leaf, dev_uuid,
1280 (unsigned long)btrfs_device_uuid(dev_item),
1282 device = btrfs_find_device(root, devid, dev_uuid);
1284 printk("warning devid %Lu not found already\n", devid);
1285 device = kzalloc(sizeof(*device), GFP_NOFS);
1288 list_add(&device->dev_list,
1289 &root->fs_info->fs_devices->devices);
1290 device->barriers = 1;
1291 spin_lock_init(&device->io_lock);
1294 fill_device_from_item(leaf, dev_item, device);
1295 device->dev_root = root->fs_info->dev_root;
1298 ret = btrfs_open_device(device);
1306 int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
1308 struct btrfs_dev_item *dev_item;
1310 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
1312 return read_one_dev(root, buf, dev_item);
1315 int btrfs_read_sys_array(struct btrfs_root *root)
1317 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1318 struct extent_buffer *sb = root->fs_info->sb_buffer;
1319 struct btrfs_disk_key *disk_key;
1320 struct btrfs_chunk *chunk;
1321 struct btrfs_key key;
1326 unsigned long sb_ptr;
1330 array_size = btrfs_super_sys_array_size(super_copy);
1333 * we do this loop twice, once for the device items and
1334 * once for all of the chunks. This way there are device
1335 * structs filled in for every chunk
1337 ptr = super_copy->sys_chunk_array;
1338 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
1341 while (cur < array_size) {
1342 disk_key = (struct btrfs_disk_key *)ptr;
1343 btrfs_disk_key_to_cpu(&key, disk_key);
1345 len = sizeof(*disk_key);
1350 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1351 chunk = (struct btrfs_chunk *)sb_ptr;
1352 ret = read_one_chunk(root, &key, sb, chunk);
1354 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
1355 len = btrfs_chunk_item_size(num_stripes);
1366 int btrfs_read_chunk_tree(struct btrfs_root *root)
1368 struct btrfs_path *path;
1369 struct extent_buffer *leaf;
1370 struct btrfs_key key;
1371 struct btrfs_key found_key;
1375 root = root->fs_info->chunk_root;
1377 path = btrfs_alloc_path();
1381 /* first we search for all of the device items, and then we
1382 * read in all of the chunk items. This way we can create chunk
1383 * mappings that reference all of the devices that are afound
1385 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1389 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1391 leaf = path->nodes[0];
1392 slot = path->slots[0];
1393 if (slot >= btrfs_header_nritems(leaf)) {
1394 ret = btrfs_next_leaf(root, path);
1401 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1402 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1403 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
1405 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
1406 struct btrfs_dev_item *dev_item;
1407 dev_item = btrfs_item_ptr(leaf, slot,
1408 struct btrfs_dev_item);
1409 ret = read_one_dev(root, leaf, dev_item);
1412 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
1413 struct btrfs_chunk *chunk;
1414 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1415 ret = read_one_chunk(root, &found_key, leaf, chunk);
1419 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1421 btrfs_release_path(root, path);
1425 btrfs_free_path(path);