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"
36 struct btrfs_bio_stripe stripes[];
39 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
40 (sizeof(struct btrfs_bio_stripe) * (n)))
42 static DEFINE_MUTEX(uuid_mutex);
43 static LIST_HEAD(fs_uuids);
45 int btrfs_cleanup_fs_uuids(void)
47 struct btrfs_fs_devices *fs_devices;
48 struct list_head *uuid_cur;
49 struct list_head *devices_cur;
50 struct btrfs_device *dev;
52 list_for_each(uuid_cur, &fs_uuids) {
53 fs_devices = list_entry(uuid_cur, struct btrfs_fs_devices,
55 while(!list_empty(&fs_devices->devices)) {
56 devices_cur = fs_devices->devices.next;
57 dev = list_entry(devices_cur, struct btrfs_device,
59 printk("uuid cleanup finds %s\n", dev->name);
62 close_bdev_excl(dev->bdev);
64 list_del(&dev->dev_list);
71 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)
84 static struct btrfs_fs_devices *find_fsid(u8 *fsid)
86 struct list_head *cur;
87 struct btrfs_fs_devices *fs_devices;
89 list_for_each(cur, &fs_uuids) {
90 fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
91 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
97 static int device_list_add(const char *path,
98 struct btrfs_super_block *disk_super,
99 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
101 struct btrfs_device *device;
102 struct btrfs_fs_devices *fs_devices;
103 u64 found_transid = btrfs_super_generation(disk_super);
105 fs_devices = find_fsid(disk_super->fsid);
107 fs_devices = kmalloc(sizeof(*fs_devices), GFP_NOFS);
110 INIT_LIST_HEAD(&fs_devices->devices);
111 list_add(&fs_devices->list, &fs_uuids);
112 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
113 fs_devices->latest_devid = devid;
114 fs_devices->latest_trans = found_transid;
115 fs_devices->lowest_devid = (u64)-1;
116 fs_devices->num_devices = 0;
119 device = __find_device(&fs_devices->devices, devid);
122 device = kzalloc(sizeof(*device), GFP_NOFS);
124 /* we can safely leave the fs_devices entry around */
127 device->devid = devid;
128 device->barriers = 1;
129 spin_lock_init(&device->io_lock);
130 device->name = kstrdup(path, GFP_NOFS);
135 list_add(&device->dev_list, &fs_devices->devices);
136 fs_devices->num_devices++;
139 if (found_transid > fs_devices->latest_trans) {
140 fs_devices->latest_devid = devid;
141 fs_devices->latest_trans = found_transid;
143 if (fs_devices->lowest_devid > devid) {
144 fs_devices->lowest_devid = devid;
145 printk("lowest devid now %Lu\n", devid);
147 *fs_devices_ret = fs_devices;
151 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
153 struct list_head *head = &fs_devices->devices;
154 struct list_head *cur;
155 struct btrfs_device *device;
157 mutex_lock(&uuid_mutex);
158 list_for_each(cur, head) {
159 device = list_entry(cur, struct btrfs_device, dev_list);
161 close_bdev_excl(device->bdev);
162 printk("close devices closes %s\n", device->name);
166 mutex_unlock(&uuid_mutex);
170 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
171 int flags, void *holder)
173 struct block_device *bdev;
174 struct list_head *head = &fs_devices->devices;
175 struct list_head *cur;
176 struct btrfs_device *device;
179 mutex_lock(&uuid_mutex);
180 list_for_each(cur, head) {
181 device = list_entry(cur, struct btrfs_device, dev_list);
182 bdev = open_bdev_excl(device->name, flags, holder);
185 printk("open %s failed\n", device->name);
189 if (device->devid == fs_devices->latest_devid)
190 fs_devices->latest_bdev = bdev;
191 if (device->devid == fs_devices->lowest_devid) {
192 fs_devices->lowest_bdev = bdev;
196 mutex_unlock(&uuid_mutex);
199 mutex_unlock(&uuid_mutex);
200 btrfs_close_devices(fs_devices);
204 int btrfs_scan_one_device(const char *path, int flags, void *holder,
205 struct btrfs_fs_devices **fs_devices_ret)
207 struct btrfs_super_block *disk_super;
208 struct block_device *bdev;
209 struct buffer_head *bh;
214 mutex_lock(&uuid_mutex);
216 printk("scan one opens %s\n", path);
217 bdev = open_bdev_excl(path, flags, holder);
220 printk("open failed\n");
225 ret = set_blocksize(bdev, 4096);
228 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
233 disk_super = (struct btrfs_super_block *)bh->b_data;
234 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
235 sizeof(disk_super->magic))) {
236 printk("no btrfs found on %s\n", path);
240 devid = le64_to_cpu(disk_super->dev_item.devid);
241 transid = btrfs_super_generation(disk_super);
242 printk("found device %Lu transid %Lu on %s\n", devid, transid, path);
243 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
248 close_bdev_excl(bdev);
250 mutex_unlock(&uuid_mutex);
255 * this uses a pretty simple search, the expectation is that it is
256 * called very infrequently and that a given device has a small number
259 static int find_free_dev_extent(struct btrfs_trans_handle *trans,
260 struct btrfs_device *device,
261 struct btrfs_path *path,
262 u64 num_bytes, u64 *start)
264 struct btrfs_key key;
265 struct btrfs_root *root = device->dev_root;
266 struct btrfs_dev_extent *dev_extent = NULL;
269 u64 search_start = 0;
270 u64 search_end = device->total_bytes;
274 struct extent_buffer *l;
279 /* FIXME use last free of some kind */
281 /* we don't want to overwrite the superblock on the drive,
282 * so we make sure to start at an offset of at least 1MB
284 search_start = max((u64)1024 * 1024, search_start);
285 key.objectid = device->devid;
286 key.offset = search_start;
287 key.type = BTRFS_DEV_EXTENT_KEY;
288 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
291 ret = btrfs_previous_item(root, path, 0, key.type);
295 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
298 slot = path->slots[0];
299 if (slot >= btrfs_header_nritems(l)) {
300 ret = btrfs_next_leaf(root, path);
307 if (search_start >= search_end) {
311 *start = search_start;
315 *start = last_byte > search_start ?
316 last_byte : search_start;
317 if (search_end <= *start) {
323 btrfs_item_key_to_cpu(l, &key, slot);
325 if (key.objectid < device->devid)
328 if (key.objectid > device->devid)
331 if (key.offset >= search_start && key.offset > last_byte &&
333 if (last_byte < search_start)
334 last_byte = search_start;
335 hole_size = key.offset - last_byte;
336 if (key.offset > last_byte &&
337 hole_size >= num_bytes) {
342 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
347 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
348 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
354 /* we have to make sure we didn't find an extent that has already
355 * been allocated by the map tree or the original allocation
357 btrfs_release_path(root, path);
358 BUG_ON(*start < search_start);
360 if (*start + num_bytes > search_end) {
364 /* check for pending inserts here */
368 btrfs_release_path(root, path);
372 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
373 struct btrfs_device *device,
374 u64 chunk_tree, u64 chunk_objectid,
376 u64 num_bytes, u64 *start)
379 struct btrfs_path *path;
380 struct btrfs_root *root = device->dev_root;
381 struct btrfs_dev_extent *extent;
382 struct extent_buffer *leaf;
383 struct btrfs_key key;
385 path = btrfs_alloc_path();
389 ret = find_free_dev_extent(trans, device, path, num_bytes, start);
394 key.objectid = device->devid;
396 key.type = BTRFS_DEV_EXTENT_KEY;
397 ret = btrfs_insert_empty_item(trans, root, path, &key,
401 leaf = path->nodes[0];
402 extent = btrfs_item_ptr(leaf, path->slots[0],
403 struct btrfs_dev_extent);
404 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
405 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
406 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
408 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
409 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
412 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
413 btrfs_mark_buffer_dirty(leaf);
415 btrfs_free_path(path);
419 static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset)
421 struct btrfs_path *path;
423 struct btrfs_key key;
424 struct btrfs_chunk *chunk;
425 struct btrfs_key found_key;
427 path = btrfs_alloc_path();
430 key.objectid = objectid;
431 key.offset = (u64)-1;
432 key.type = BTRFS_CHUNK_ITEM_KEY;
434 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
440 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
444 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
446 if (found_key.objectid != objectid)
449 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
451 *offset = found_key.offset +
452 btrfs_chunk_length(path->nodes[0], chunk);
457 btrfs_free_path(path);
461 static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
465 struct btrfs_key key;
466 struct btrfs_key found_key;
468 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
469 key.type = BTRFS_DEV_ITEM_KEY;
470 key.offset = (u64)-1;
472 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
478 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
483 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
485 *objectid = found_key.offset + 1;
489 btrfs_release_path(root, path);
494 * the device information is stored in the chunk root
495 * the btrfs_device struct should be fully filled in
497 int btrfs_add_device(struct btrfs_trans_handle *trans,
498 struct btrfs_root *root,
499 struct btrfs_device *device)
502 struct btrfs_path *path;
503 struct btrfs_dev_item *dev_item;
504 struct extent_buffer *leaf;
505 struct btrfs_key key;
509 root = root->fs_info->chunk_root;
511 path = btrfs_alloc_path();
515 ret = find_next_devid(root, path, &free_devid);
519 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
520 key.type = BTRFS_DEV_ITEM_KEY;
521 key.offset = free_devid;
523 ret = btrfs_insert_empty_item(trans, root, path, &key,
528 leaf = path->nodes[0];
529 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
531 device->devid = free_devid;
532 btrfs_set_device_id(leaf, dev_item, device->devid);
533 btrfs_set_device_type(leaf, dev_item, device->type);
534 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
535 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
536 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
537 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
538 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
539 btrfs_set_device_group(leaf, dev_item, 0);
540 btrfs_set_device_seek_speed(leaf, dev_item, 0);
541 btrfs_set_device_bandwidth(leaf, dev_item, 0);
543 ptr = (unsigned long)btrfs_device_uuid(dev_item);
544 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
545 btrfs_mark_buffer_dirty(leaf);
549 btrfs_free_path(path);
552 int btrfs_update_device(struct btrfs_trans_handle *trans,
553 struct btrfs_device *device)
556 struct btrfs_path *path;
557 struct btrfs_root *root;
558 struct btrfs_dev_item *dev_item;
559 struct extent_buffer *leaf;
560 struct btrfs_key key;
562 root = device->dev_root->fs_info->chunk_root;
564 path = btrfs_alloc_path();
568 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
569 key.type = BTRFS_DEV_ITEM_KEY;
570 key.offset = device->devid;
572 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
581 leaf = path->nodes[0];
582 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
584 btrfs_set_device_id(leaf, dev_item, device->devid);
585 btrfs_set_device_type(leaf, dev_item, device->type);
586 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
587 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
588 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
589 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
590 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
591 btrfs_mark_buffer_dirty(leaf);
594 btrfs_free_path(path);
598 int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
599 struct btrfs_root *root,
600 struct btrfs_key *key,
601 struct btrfs_chunk *chunk, int item_size)
603 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
604 struct btrfs_disk_key disk_key;
608 array_size = btrfs_super_sys_array_size(super_copy);
609 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
612 ptr = super_copy->sys_chunk_array + array_size;
613 btrfs_cpu_key_to_disk(&disk_key, key);
614 memcpy(ptr, &disk_key, sizeof(disk_key));
615 ptr += sizeof(disk_key);
616 memcpy(ptr, chunk, item_size);
617 item_size += sizeof(disk_key);
618 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
622 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
623 struct btrfs_root *extent_root, u64 *start,
624 u64 *num_bytes, u64 type)
627 struct btrfs_fs_info *info = extent_root->fs_info;
628 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
629 struct btrfs_stripe *stripes;
630 struct btrfs_device *device = NULL;
631 struct btrfs_chunk *chunk;
632 struct list_head private_devs;
633 struct list_head *dev_list = &extent_root->fs_info->fs_devices->devices;
634 struct list_head *cur;
635 struct extent_map_tree *em_tree;
636 struct map_lookup *map;
637 struct extent_map *em;
639 u64 calc_size = 1024 * 1024 * 1024;
640 u64 min_free = calc_size;
647 int stripe_len = 64 * 1024;
648 struct btrfs_key key;
650 if (list_empty(dev_list))
653 if (type & (BTRFS_BLOCK_GROUP_RAID0))
654 num_stripes = btrfs_super_num_devices(&info->super_copy);
655 if (type & (BTRFS_BLOCK_GROUP_DUP))
657 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
658 num_stripes = min_t(u64, 2,
659 btrfs_super_num_devices(&info->super_copy));
662 INIT_LIST_HEAD(&private_devs);
663 cur = dev_list->next;
666 if (type & BTRFS_BLOCK_GROUP_DUP)
667 min_free = calc_size * 2;
669 /* build a private list of devices we will allocate from */
670 while(index < num_stripes) {
671 device = list_entry(cur, struct btrfs_device, dev_list);
673 avail = device->total_bytes - device->bytes_used;
675 if (avail > max_avail)
677 if (avail >= min_free) {
678 list_move_tail(&device->dev_list, &private_devs);
680 if (type & BTRFS_BLOCK_GROUP_DUP)
686 if (index < num_stripes) {
687 list_splice(&private_devs, dev_list);
688 if (!looped && max_avail > 0) {
690 calc_size = max_avail;
696 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
697 key.type = BTRFS_CHUNK_ITEM_KEY;
698 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
703 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
707 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
713 stripes = &chunk->stripe;
715 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
716 *num_bytes = calc_size;
718 *num_bytes = calc_size * num_stripes;
721 printk("new chunk type %Lu start %Lu size %Lu\n", type, key.offset, *num_bytes);
722 while(index < num_stripes) {
723 struct btrfs_stripe *stripe;
724 BUG_ON(list_empty(&private_devs));
725 cur = private_devs.next;
726 device = list_entry(cur, struct btrfs_device, dev_list);
728 /* loop over this device again if we're doing a dup group */
729 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
730 (index == num_stripes - 1))
731 list_move_tail(&device->dev_list, dev_list);
733 ret = btrfs_alloc_dev_extent(trans, device,
734 info->chunk_root->root_key.objectid,
735 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
736 calc_size, &dev_offset);
738 printk("alloc chunk start %Lu size %Lu from dev %Lu type %Lu\n", key.offset, calc_size, device->devid, type);
739 device->bytes_used += calc_size;
740 ret = btrfs_update_device(trans, device);
743 map->stripes[index].dev = device;
744 map->stripes[index].physical = dev_offset;
745 stripe = stripes + index;
746 btrfs_set_stack_stripe_devid(stripe, device->devid);
747 btrfs_set_stack_stripe_offset(stripe, dev_offset);
748 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
749 physical = dev_offset;
752 BUG_ON(!list_empty(&private_devs));
754 /* key was set above */
755 btrfs_set_stack_chunk_length(chunk, *num_bytes);
756 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
757 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
758 btrfs_set_stack_chunk_type(chunk, type);
759 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
760 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
761 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
762 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
763 map->sector_size = extent_root->sectorsize;
764 map->stripe_len = stripe_len;
765 map->io_align = stripe_len;
766 map->io_width = stripe_len;
768 map->num_stripes = num_stripes;
770 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
771 btrfs_chunk_item_size(num_stripes));
773 *start = key.offset;;
775 em = alloc_extent_map(GFP_NOFS);
778 em->bdev = (struct block_device *)map;
779 em->start = key.offset;
780 em->len = *num_bytes;
785 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
786 spin_lock(&em_tree->lock);
787 ret = add_extent_mapping(em_tree, em);
788 spin_unlock(&em_tree->lock);
794 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
796 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
799 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
801 struct extent_map *em;
804 spin_lock(&tree->map_tree.lock);
805 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
807 remove_extent_mapping(&tree->map_tree, em);
808 spin_unlock(&tree->map_tree.lock);
814 /* once for the tree */
819 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
821 struct extent_map *em;
822 struct map_lookup *map;
823 struct extent_map_tree *em_tree = &map_tree->map_tree;
826 spin_lock(&em_tree->lock);
827 em = lookup_extent_mapping(em_tree, logical, len);
828 spin_unlock(&em_tree->lock);
831 BUG_ON(em->start > logical || em->start + em->len < logical);
832 map = (struct map_lookup *)em->bdev;
833 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
834 ret = map->num_stripes;
841 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
842 u64 logical, u64 *length,
843 struct btrfs_multi_bio **multi_ret, int mirror_num)
845 struct extent_map *em;
846 struct map_lookup *map;
847 struct extent_map_tree *em_tree = &map_tree->map_tree;
851 int stripes_allocated = 8;
854 struct btrfs_multi_bio *multi = NULL;
856 if (multi_ret && !(rw & (1 << BIO_RW))) {
857 stripes_allocated = 1;
861 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
867 spin_lock(&em_tree->lock);
868 em = lookup_extent_mapping(em_tree, logical, *length);
869 spin_unlock(&em_tree->lock);
872 BUG_ON(em->start > logical || em->start + em->len < logical);
873 map = (struct map_lookup *)em->bdev;
874 offset = logical - em->start;
876 if (mirror_num > map->num_stripes)
879 /* if our multi bio struct is too small, back off and try again */
880 if (multi_ret && (rw & (1 << BIO_RW)) &&
881 stripes_allocated < map->num_stripes &&
882 ((map->type & BTRFS_BLOCK_GROUP_RAID1) ||
883 (map->type & BTRFS_BLOCK_GROUP_DUP))) {
884 stripes_allocated = map->num_stripes;
891 * stripe_nr counts the total number of stripes we have to stride
892 * to get to this block
894 do_div(stripe_nr, map->stripe_len);
896 stripe_offset = stripe_nr * map->stripe_len;
897 BUG_ON(offset < stripe_offset);
899 /* stripe_offset is the offset of this block in its stripe*/
900 stripe_offset = offset - stripe_offset;
902 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
903 BTRFS_BLOCK_GROUP_DUP)) {
904 /* we limit the length of each bio to what fits in a stripe */
905 *length = min_t(u64, em->len - offset,
906 map->stripe_len - stripe_offset);
908 *length = em->len - offset;
913 multi->num_stripes = 1;
915 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
916 if (rw & (1 << BIO_RW))
917 multi->num_stripes = map->num_stripes;
918 else if (mirror_num) {
919 stripe_index = mirror_num - 1;
923 struct btrfs_device *cur;
925 for (i = 0; i < map->num_stripes; i++) {
926 cur = map->stripes[i].dev;
927 spin_lock(&cur->io_lock);
928 if (cur->total_ios < least) {
929 least = cur->total_ios;
932 spin_unlock(&cur->io_lock);
935 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
936 if (rw & (1 << BIO_RW))
937 multi->num_stripes = map->num_stripes;
939 stripe_index = mirror_num - 1;
942 * after this do_div call, stripe_nr is the number of stripes
943 * on this device we have to walk to find the data, and
944 * stripe_index is the number of our device in the stripe array
946 stripe_index = do_div(stripe_nr, map->num_stripes);
948 BUG_ON(stripe_index >= map->num_stripes);
949 BUG_ON(stripe_index != 0 && multi->num_stripes > 1);
951 for (i = 0; i < multi->num_stripes; i++) {
952 multi->stripes[i].physical =
953 map->stripes[stripe_index].physical + stripe_offset +
954 stripe_nr * map->stripe_len;
955 multi->stripes[i].dev = map->stripes[stripe_index].dev;
964 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
965 static void end_bio_multi_stripe(struct bio *bio, int err)
967 static int end_bio_multi_stripe(struct bio *bio,
968 unsigned int bytes_done, int err)
971 struct btrfs_multi_bio *multi = bio->bi_private;
973 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
980 if (atomic_dec_and_test(&multi->stripes_pending)) {
981 bio->bi_private = multi->private;
982 bio->bi_end_io = multi->end_io;
984 if (!err && multi->error)
988 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
989 bio_endio(bio, bio->bi_size, err);
996 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1001 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
1004 struct btrfs_mapping_tree *map_tree;
1005 struct btrfs_device *dev;
1006 struct bio *first_bio = bio;
1007 u64 logical = bio->bi_sector << 9;
1010 struct bio_vec *bvec;
1011 struct btrfs_multi_bio *multi = NULL;
1017 bio_for_each_segment(bvec, bio, i) {
1018 length += bvec->bv_len;
1021 map_tree = &root->fs_info->mapping_tree;
1022 map_length = length;
1024 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
1028 total_devs = multi->num_stripes;
1029 if (map_length < length) {
1030 printk("mapping failed logical %Lu bio len %Lu "
1031 "len %Lu\n", logical, length, map_length);
1034 multi->end_io = first_bio->bi_end_io;
1035 multi->private = first_bio->bi_private;
1036 atomic_set(&multi->stripes_pending, multi->num_stripes);
1038 while(dev_nr < total_devs) {
1039 if (total_devs > 1) {
1040 if (dev_nr < total_devs - 1) {
1041 bio = bio_clone(first_bio, GFP_NOFS);
1046 bio->bi_private = multi;
1047 bio->bi_end_io = end_bio_multi_stripe;
1049 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
1050 dev = multi->stripes[dev_nr].dev;
1051 bio->bi_bdev = dev->bdev;
1052 spin_lock(&dev->io_lock);
1054 spin_unlock(&dev->io_lock);
1055 submit_bio(rw, bio);
1058 if (total_devs == 1)
1063 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid)
1065 struct list_head *head = &root->fs_info->fs_devices->devices;
1067 return __find_device(head, devid);
1070 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
1071 struct extent_buffer *leaf,
1072 struct btrfs_chunk *chunk)
1074 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1075 struct map_lookup *map;
1076 struct extent_map *em;
1084 logical = key->offset;
1085 length = btrfs_chunk_length(leaf, chunk);
1086 spin_lock(&map_tree->map_tree.lock);
1087 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
1088 spin_unlock(&map_tree->map_tree.lock);
1090 /* already mapped? */
1091 if (em && em->start <= logical && em->start + em->len > logical) {
1092 free_extent_map(em);
1095 free_extent_map(em);
1098 map = kzalloc(sizeof(*map), GFP_NOFS);
1102 em = alloc_extent_map(GFP_NOFS);
1105 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1106 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1108 free_extent_map(em);
1112 em->bdev = (struct block_device *)map;
1113 em->start = logical;
1115 em->block_start = 0;
1117 map->num_stripes = num_stripes;
1118 map->io_width = btrfs_chunk_io_width(leaf, chunk);
1119 map->io_align = btrfs_chunk_io_align(leaf, chunk);
1120 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1121 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1122 map->type = btrfs_chunk_type(leaf, chunk);
1123 for (i = 0; i < num_stripes; i++) {
1124 map->stripes[i].physical =
1125 btrfs_stripe_offset_nr(leaf, chunk, i);
1126 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
1127 map->stripes[i].dev = btrfs_find_device(root, devid);
1128 if (!map->stripes[i].dev) {
1130 free_extent_map(em);
1135 spin_lock(&map_tree->map_tree.lock);
1136 ret = add_extent_mapping(&map_tree->map_tree, em);
1137 spin_unlock(&map_tree->map_tree.lock);
1139 free_extent_map(em);
1144 static int fill_device_from_item(struct extent_buffer *leaf,
1145 struct btrfs_dev_item *dev_item,
1146 struct btrfs_device *device)
1150 device->devid = btrfs_device_id(leaf, dev_item);
1151 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1152 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1153 device->type = btrfs_device_type(leaf, dev_item);
1154 device->io_align = btrfs_device_io_align(leaf, dev_item);
1155 device->io_width = btrfs_device_io_width(leaf, dev_item);
1156 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
1158 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1159 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1164 static int read_one_dev(struct btrfs_root *root,
1165 struct extent_buffer *leaf,
1166 struct btrfs_dev_item *dev_item)
1168 struct btrfs_device *device;
1171 devid = btrfs_device_id(leaf, dev_item);
1172 device = btrfs_find_device(root, devid);
1174 printk("warning devid %Lu not found already\n", devid);
1175 device = kzalloc(sizeof(*device), GFP_NOFS);
1178 list_add(&device->dev_list,
1179 &root->fs_info->fs_devices->devices);
1180 device->barriers = 1;
1181 spin_lock_init(&device->io_lock);
1184 fill_device_from_item(leaf, dev_item, device);
1185 device->dev_root = root->fs_info->dev_root;
1188 ret = btrfs_open_device(device);
1196 int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
1198 struct btrfs_dev_item *dev_item;
1200 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
1202 return read_one_dev(root, buf, dev_item);
1205 int btrfs_read_sys_array(struct btrfs_root *root)
1207 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1208 struct extent_buffer *sb = root->fs_info->sb_buffer;
1209 struct btrfs_disk_key *disk_key;
1210 struct btrfs_chunk *chunk;
1211 struct btrfs_key key;
1216 unsigned long sb_ptr;
1220 array_size = btrfs_super_sys_array_size(super_copy);
1223 * we do this loop twice, once for the device items and
1224 * once for all of the chunks. This way there are device
1225 * structs filled in for every chunk
1227 ptr = super_copy->sys_chunk_array;
1228 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
1231 while (cur < array_size) {
1232 disk_key = (struct btrfs_disk_key *)ptr;
1233 btrfs_disk_key_to_cpu(&key, disk_key);
1235 len = sizeof(*disk_key);
1240 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1241 chunk = (struct btrfs_chunk *)sb_ptr;
1242 ret = read_one_chunk(root, &key, sb, chunk);
1244 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
1245 len = btrfs_chunk_item_size(num_stripes);
1256 int btrfs_read_chunk_tree(struct btrfs_root *root)
1258 struct btrfs_path *path;
1259 struct extent_buffer *leaf;
1260 struct btrfs_key key;
1261 struct btrfs_key found_key;
1265 root = root->fs_info->chunk_root;
1267 path = btrfs_alloc_path();
1271 /* first we search for all of the device items, and then we
1272 * read in all of the chunk items. This way we can create chunk
1273 * mappings that reference all of the devices that are afound
1275 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1279 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1281 leaf = path->nodes[0];
1282 slot = path->slots[0];
1283 if (slot >= btrfs_header_nritems(leaf)) {
1284 ret = btrfs_next_leaf(root, path);
1291 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1292 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1293 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
1295 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
1296 struct btrfs_dev_item *dev_item;
1297 dev_item = btrfs_item_ptr(leaf, slot,
1298 struct btrfs_dev_item);
1299 ret = read_one_dev(root, leaf, dev_item);
1302 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
1303 struct btrfs_chunk *chunk;
1304 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1305 ret = read_one_chunk(root, &found_key, leaf, chunk);
1309 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1311 btrfs_release_path(root, path);
1315 btrfs_free_path(path);