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"
30 struct btrfs_device *dev;
48 struct stripe stripes[];
51 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
52 (sizeof(struct stripe) * (n)))
54 static DEFINE_MUTEX(uuid_mutex);
55 static LIST_HEAD(fs_uuids);
57 int btrfs_cleanup_fs_uuids(void)
59 struct btrfs_fs_devices *fs_devices;
60 struct list_head *uuid_cur;
61 struct list_head *devices_cur;
62 struct btrfs_device *dev;
64 list_for_each(uuid_cur, &fs_uuids) {
65 fs_devices = list_entry(uuid_cur, struct btrfs_fs_devices,
67 while(!list_empty(&fs_devices->devices)) {
68 devices_cur = fs_devices->devices.next;
69 dev = list_entry(devices_cur, struct btrfs_device,
71 printk("uuid cleanup finds %s\n", dev->name);
74 close_bdev_excl(dev->bdev);
76 list_del(&dev->dev_list);
83 static struct btrfs_device *__find_device(struct list_head *head, u64 devid)
85 struct btrfs_device *dev;
86 struct list_head *cur;
88 list_for_each(cur, head) {
89 dev = list_entry(cur, struct btrfs_device, dev_list);
90 if (dev->devid == devid)
96 static struct btrfs_fs_devices *find_fsid(u8 *fsid)
98 struct list_head *cur;
99 struct btrfs_fs_devices *fs_devices;
101 list_for_each(cur, &fs_uuids) {
102 fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
103 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
109 static int device_list_add(const char *path,
110 struct btrfs_super_block *disk_super,
111 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
113 struct btrfs_device *device;
114 struct btrfs_fs_devices *fs_devices;
115 u64 found_transid = btrfs_super_generation(disk_super);
117 fs_devices = find_fsid(disk_super->fsid);
119 fs_devices = kmalloc(sizeof(*fs_devices), GFP_NOFS);
122 INIT_LIST_HEAD(&fs_devices->devices);
123 list_add(&fs_devices->list, &fs_uuids);
124 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
125 fs_devices->latest_devid = devid;
126 fs_devices->latest_trans = found_transid;
127 fs_devices->lowest_devid = (u64)-1;
128 fs_devices->num_devices = 0;
131 device = __find_device(&fs_devices->devices, devid);
134 device = kzalloc(sizeof(*device), GFP_NOFS);
136 /* we can safely leave the fs_devices entry around */
139 device->devid = devid;
140 device->name = kstrdup(path, GFP_NOFS);
145 list_add(&device->dev_list, &fs_devices->devices);
146 fs_devices->num_devices++;
149 if (found_transid > fs_devices->latest_trans) {
150 fs_devices->latest_devid = devid;
151 fs_devices->latest_trans = found_transid;
153 if (fs_devices->lowest_devid > devid) {
154 fs_devices->lowest_devid = devid;
155 printk("lowest devid now %Lu\n", devid);
157 *fs_devices_ret = fs_devices;
161 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
163 struct list_head *head = &fs_devices->devices;
164 struct list_head *cur;
165 struct btrfs_device *device;
167 mutex_lock(&uuid_mutex);
168 list_for_each(cur, head) {
169 device = list_entry(cur, struct btrfs_device, dev_list);
171 close_bdev_excl(device->bdev);
172 printk("close devices closes %s\n", device->name);
176 mutex_unlock(&uuid_mutex);
180 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
181 int flags, void *holder)
183 struct block_device *bdev;
184 struct list_head *head = &fs_devices->devices;
185 struct list_head *cur;
186 struct btrfs_device *device;
189 mutex_lock(&uuid_mutex);
190 list_for_each(cur, head) {
191 device = list_entry(cur, struct btrfs_device, dev_list);
192 bdev = open_bdev_excl(device->name, flags, holder);
193 printk("opening %s devid %Lu\n", device->name, device->devid);
195 printk("open %s failed\n", device->name);
199 if (device->devid == fs_devices->latest_devid)
200 fs_devices->latest_bdev = bdev;
201 if (device->devid == fs_devices->lowest_devid) {
202 fs_devices->lowest_bdev = bdev;
203 printk("lowest bdev %s\n", device->name);
207 mutex_unlock(&uuid_mutex);
210 mutex_unlock(&uuid_mutex);
211 btrfs_close_devices(fs_devices);
215 int btrfs_scan_one_device(const char *path, int flags, void *holder,
216 struct btrfs_fs_devices **fs_devices_ret)
218 struct btrfs_super_block *disk_super;
219 struct block_device *bdev;
220 struct buffer_head *bh;
224 mutex_lock(&uuid_mutex);
226 printk("scan one opens %s\n", path);
227 bdev = open_bdev_excl(path, flags, holder);
230 printk("open failed\n");
235 ret = set_blocksize(bdev, 4096);
238 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
243 disk_super = (struct btrfs_super_block *)bh->b_data;
244 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
245 sizeof(disk_super->magic))) {
246 printk("no btrfs found on %s\n", path);
250 devid = le64_to_cpu(disk_super->dev_item.devid);
251 printk("found device %Lu on %s\n", devid, path);
252 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
257 close_bdev_excl(bdev);
258 printk("scan one closes bdev %s\n", path);
260 mutex_unlock(&uuid_mutex);
265 * this uses a pretty simple search, the expectation is that it is
266 * called very infrequently and that a given device has a small number
269 static int find_free_dev_extent(struct btrfs_trans_handle *trans,
270 struct btrfs_device *device,
271 struct btrfs_path *path,
272 u64 num_bytes, u64 *start)
274 struct btrfs_key key;
275 struct btrfs_root *root = device->dev_root;
276 struct btrfs_dev_extent *dev_extent = NULL;
279 u64 search_start = 0;
280 u64 search_end = device->total_bytes;
284 struct extent_buffer *l;
289 /* FIXME use last free of some kind */
291 /* we don't want to overwrite the superblock on the drive,
292 * so we make sure to start at an offset of at least 1MB
294 search_start = max((u64)1024 * 1024, search_start);
295 key.objectid = device->devid;
296 key.offset = search_start;
297 key.type = BTRFS_DEV_EXTENT_KEY;
298 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
301 ret = btrfs_previous_item(root, path, 0, key.type);
305 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
308 slot = path->slots[0];
309 if (slot >= btrfs_header_nritems(l)) {
310 ret = btrfs_next_leaf(root, path);
317 if (search_start >= search_end) {
321 *start = search_start;
325 *start = last_byte > search_start ?
326 last_byte : search_start;
327 if (search_end <= *start) {
333 btrfs_item_key_to_cpu(l, &key, slot);
335 if (key.objectid < device->devid)
338 if (key.objectid > device->devid)
341 if (key.offset >= search_start && key.offset > last_byte &&
343 if (last_byte < search_start)
344 last_byte = search_start;
345 hole_size = key.offset - last_byte;
346 if (key.offset > last_byte &&
347 hole_size >= num_bytes) {
352 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
357 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
358 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
364 /* we have to make sure we didn't find an extent that has already
365 * been allocated by the map tree or the original allocation
367 btrfs_release_path(root, path);
368 BUG_ON(*start < search_start);
370 if (*start + num_bytes > search_end) {
374 /* check for pending inserts here */
378 btrfs_release_path(root, path);
382 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
383 struct btrfs_device *device,
384 u64 owner, 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_owner(leaf, extent, owner);
413 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
414 btrfs_mark_buffer_dirty(leaf);
416 btrfs_free_path(path);
420 static int find_next_chunk(struct btrfs_root *root, u64 *objectid)
422 struct btrfs_path *path;
424 struct btrfs_key key;
425 struct btrfs_key found_key;
427 path = btrfs_alloc_path();
430 key.objectid = (u64)-1;
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 *objectid = found_key.objectid + found_key.offset;
450 btrfs_free_path(path);
454 static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
458 struct btrfs_key key;
459 struct btrfs_key found_key;
461 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
462 key.type = BTRFS_DEV_ITEM_KEY;
463 key.offset = (u64)-1;
465 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
471 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
476 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
478 *objectid = found_key.offset + 1;
482 btrfs_release_path(root, path);
487 * the device information is stored in the chunk root
488 * the btrfs_device struct should be fully filled in
490 int btrfs_add_device(struct btrfs_trans_handle *trans,
491 struct btrfs_root *root,
492 struct btrfs_device *device)
495 struct btrfs_path *path;
496 struct btrfs_dev_item *dev_item;
497 struct extent_buffer *leaf;
498 struct btrfs_key key;
502 root = root->fs_info->chunk_root;
504 path = btrfs_alloc_path();
508 ret = find_next_devid(root, path, &free_devid);
512 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
513 key.type = BTRFS_DEV_ITEM_KEY;
514 key.offset = free_devid;
516 ret = btrfs_insert_empty_item(trans, root, path, &key,
521 leaf = path->nodes[0];
522 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
524 device->devid = free_devid;
525 btrfs_set_device_id(leaf, dev_item, device->devid);
526 btrfs_set_device_type(leaf, dev_item, device->type);
527 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
528 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
529 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
530 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
531 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
533 ptr = (unsigned long)btrfs_device_uuid(dev_item);
534 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_DEV_UUID_SIZE);
535 btrfs_mark_buffer_dirty(leaf);
539 btrfs_free_path(path);
542 int btrfs_update_device(struct btrfs_trans_handle *trans,
543 struct btrfs_device *device)
546 struct btrfs_path *path;
547 struct btrfs_root *root;
548 struct btrfs_dev_item *dev_item;
549 struct extent_buffer *leaf;
550 struct btrfs_key key;
552 root = device->dev_root->fs_info->chunk_root;
554 path = btrfs_alloc_path();
558 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
559 key.type = BTRFS_DEV_ITEM_KEY;
560 key.offset = device->devid;
562 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
571 leaf = path->nodes[0];
572 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
574 btrfs_set_device_id(leaf, dev_item, device->devid);
575 btrfs_set_device_type(leaf, dev_item, device->type);
576 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
577 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
578 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
579 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
580 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
581 btrfs_mark_buffer_dirty(leaf);
584 btrfs_free_path(path);
588 int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
589 struct btrfs_root *root,
590 struct btrfs_key *key,
591 struct btrfs_chunk *chunk, int item_size)
593 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
594 struct btrfs_disk_key disk_key;
598 array_size = btrfs_super_sys_array_size(super_copy);
599 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
602 ptr = super_copy->sys_chunk_array + array_size;
603 btrfs_cpu_key_to_disk(&disk_key, key);
604 memcpy(ptr, &disk_key, sizeof(disk_key));
605 ptr += sizeof(disk_key);
606 memcpy(ptr, chunk, item_size);
607 item_size += sizeof(disk_key);
608 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
612 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
613 struct btrfs_root *extent_root, u64 *start,
614 u64 *num_bytes, u64 type)
617 struct btrfs_fs_info *info = extent_root->fs_info;
618 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
619 struct btrfs_stripe *stripes;
620 struct btrfs_device *device = NULL;
621 struct btrfs_chunk *chunk;
622 struct list_head private_devs;
623 struct list_head *dev_list = &extent_root->fs_info->fs_devices->devices;
624 struct list_head *cur;
625 struct extent_map_tree *em_tree;
626 struct map_lookup *map;
627 struct extent_map *em;
629 u64 calc_size = 1024 * 1024 * 1024;
636 int stripe_len = 64 * 1024;
637 struct btrfs_key key;
639 if (list_empty(dev_list))
642 if (type & (BTRFS_BLOCK_GROUP_RAID0))
643 num_stripes = btrfs_super_num_devices(&info->super_copy);
644 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
645 num_stripes = min_t(u64, 2,
646 btrfs_super_num_devices(&info->super_copy));
649 INIT_LIST_HEAD(&private_devs);
650 cur = dev_list->next;
652 /* build a private list of devices we will allocate from */
653 while(index < num_stripes) {
654 device = list_entry(cur, struct btrfs_device, dev_list);
655 avail = device->total_bytes - device->bytes_used;
657 if (avail > max_avail)
659 if (avail >= calc_size) {
660 list_move_tail(&device->dev_list, &private_devs);
666 if (index < num_stripes) {
667 list_splice(&private_devs, dev_list);
668 if (!looped && max_avail > 0) {
670 calc_size = max_avail;
676 ret = find_next_chunk(chunk_root, &key.objectid);
680 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
684 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
690 stripes = &chunk->stripe;
692 if (type & BTRFS_BLOCK_GROUP_RAID1)
693 *num_bytes = calc_size;
695 *num_bytes = calc_size * num_stripes;
698 while(index < num_stripes) {
699 BUG_ON(list_empty(&private_devs));
700 cur = private_devs.next;
701 device = list_entry(cur, struct btrfs_device, dev_list);
702 list_move_tail(&device->dev_list, dev_list);
704 ret = btrfs_alloc_dev_extent(trans, device,
706 calc_size, &dev_offset);
708 printk("alloc chunk start %Lu size %Lu from dev %Lu type %Lu\n", key.objectid, calc_size, device->devid, type);
709 device->bytes_used += calc_size;
710 ret = btrfs_update_device(trans, device);
713 map->stripes[index].dev = device;
714 map->stripes[index].physical = dev_offset;
715 btrfs_set_stack_stripe_devid(stripes + index, device->devid);
716 btrfs_set_stack_stripe_offset(stripes + index, dev_offset);
717 physical = dev_offset;
720 BUG_ON(!list_empty(&private_devs));
722 /* key.objectid was set above */
723 key.offset = *num_bytes;
724 key.type = BTRFS_CHUNK_ITEM_KEY;
725 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
726 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
727 btrfs_set_stack_chunk_type(chunk, type);
728 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
729 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
730 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
731 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
732 map->sector_size = extent_root->sectorsize;
733 map->stripe_len = stripe_len;
734 map->io_align = stripe_len;
735 map->io_width = stripe_len;
737 map->num_stripes = num_stripes;
739 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
740 btrfs_chunk_item_size(num_stripes));
742 *start = key.objectid;
744 em = alloc_extent_map(GFP_NOFS);
747 em->bdev = (struct block_device *)map;
748 em->start = key.objectid;
749 em->len = key.offset;
754 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
755 spin_lock(&em_tree->lock);
756 ret = add_extent_mapping(em_tree, em);
758 spin_unlock(&em_tree->lock);
763 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
765 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
768 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
770 struct extent_map *em;
773 spin_lock(&tree->map_tree.lock);
774 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
776 remove_extent_mapping(&tree->map_tree, em);
777 spin_unlock(&tree->map_tree.lock);
783 /* once for the tree */
788 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
789 int dev_nr, u64 logical, u64 *phys, u64 *length,
790 struct btrfs_device **dev, int *total_devs)
792 struct extent_map *em;
793 struct map_lookup *map;
794 struct extent_map_tree *em_tree = &map_tree->map_tree;
801 spin_lock(&em_tree->lock);
802 em = lookup_extent_mapping(em_tree, logical, *length);
805 BUG_ON(em->start > logical || em->start + em->len < logical);
806 map = (struct map_lookup *)em->bdev;
807 offset = logical - em->start;
811 * stripe_nr counts the total number of stripes we have to stride
812 * to get to this block
814 do_div(stripe_nr, map->stripe_len);
816 stripe_offset = stripe_nr * map->stripe_len;
817 BUG_ON(offset < stripe_offset);
819 /* stripe_offset is the offset of this block in its stripe*/
820 stripe_offset = offset - stripe_offset;
822 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
823 stripe_index = dev_nr;
824 if (rw & (1 << BIO_RW))
825 *total_devs = map->num_stripes;
829 struct btrfs_device *cur;
831 for (i = 0; i < map->num_stripes; i++) {
832 cur = map->stripes[i].dev;
833 spin_lock(&cur->io_lock);
834 if (cur->total_ios < least) {
835 least = cur->total_ios;
838 spin_unlock(&cur->io_lock);
844 * after this do_div call, stripe_nr is the number of stripes
845 * on this device we have to walk to find the data, and
846 * stripe_index is the number of our device in the stripe array
848 stripe_index = do_div(stripe_nr, map->num_stripes);
850 BUG_ON(stripe_index >= map->num_stripes);
851 *phys = map->stripes[stripe_index].physical + stripe_offset +
852 stripe_nr * map->stripe_len;
854 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1)) {
855 /* we limit the length of each bio to what fits in a stripe */
856 *length = min_t(u64, em->len - offset,
857 map->stripe_len - stripe_offset);
859 *length = em->len - offset;
861 *dev = map->stripes[stripe_index].dev;
863 spin_unlock(&em_tree->lock);
867 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
868 static void end_bio_multi_stripe(struct bio *bio, int err)
870 static int end_bio_multi_stripe(struct bio *bio,
871 unsigned int bytes_done, int err)
874 struct multi_bio *multi = bio->bi_private;
876 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
883 if (atomic_dec_and_test(&multi->stripes)) {
884 bio->bi_private = multi->private;
885 bio->bi_end_io = multi->end_io;
887 if (!err && multi->error)
895 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
900 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio)
902 struct btrfs_mapping_tree *map_tree;
903 struct btrfs_device *dev;
904 struct bio *first_bio = bio;
905 u64 logical = bio->bi_sector << 9;
909 struct bio_vec *bvec;
910 struct multi_bio *multi = NULL;
916 bio_for_each_segment(bvec, bio, i) {
917 length += bvec->bv_len;
920 map_tree = &root->fs_info->mapping_tree;
922 while(dev_nr < total_devs) {
923 ret = btrfs_map_block(map_tree, rw, dev_nr, logical,
924 &physical, &map_length, &dev,
926 if (map_length < length) {
927 printk("mapping failed logical %Lu bio len %Lu physical %Lu "
928 "len %Lu\n", logical, length, physical, map_length);
931 BUG_ON(map_length < length);
932 if (total_devs > 1) {
934 multi = kmalloc(sizeof(*multi), GFP_NOFS);
935 atomic_set(&multi->stripes, 1);
936 multi->end_io = bio->bi_end_io;
937 multi->private = first_bio->bi_private;
940 atomic_inc(&multi->stripes);
942 if (dev_nr < total_devs - 1) {
943 bio = bio_clone(first_bio, GFP_NOFS);
948 bio->bi_private = multi;
949 bio->bi_end_io = end_bio_multi_stripe;
951 bio->bi_sector = physical >> 9;
952 bio->bi_bdev = dev->bdev;
953 spin_lock(&dev->io_lock);
955 spin_unlock(&dev->io_lock);
962 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid)
964 struct list_head *head = &root->fs_info->fs_devices->devices;
966 return __find_device(head, devid);
969 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
970 struct extent_buffer *leaf,
971 struct btrfs_chunk *chunk)
973 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
974 struct map_lookup *map;
975 struct extent_map *em;
983 logical = key->objectid;
984 length = key->offset;
985 spin_lock(&map_tree->map_tree.lock);
986 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
988 /* already mapped? */
989 if (em && em->start <= logical && em->start + em->len > logical) {
991 spin_unlock(&map_tree->map_tree.lock);
996 spin_unlock(&map_tree->map_tree.lock);
998 map = kzalloc(sizeof(*map), GFP_NOFS);
1002 em = alloc_extent_map(GFP_NOFS);
1005 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1006 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1008 free_extent_map(em);
1012 em->bdev = (struct block_device *)map;
1013 em->start = logical;
1015 em->block_start = 0;
1017 map->num_stripes = num_stripes;
1018 map->io_width = btrfs_chunk_io_width(leaf, chunk);
1019 map->io_align = btrfs_chunk_io_align(leaf, chunk);
1020 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1021 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1022 map->type = btrfs_chunk_type(leaf, chunk);
1023 for (i = 0; i < num_stripes; i++) {
1024 map->stripes[i].physical =
1025 btrfs_stripe_offset_nr(leaf, chunk, i);
1026 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
1027 map->stripes[i].dev = btrfs_find_device(root, devid);
1028 if (!map->stripes[i].dev) {
1030 free_extent_map(em);
1035 spin_lock(&map_tree->map_tree.lock);
1036 ret = add_extent_mapping(&map_tree->map_tree, em);
1038 spin_unlock(&map_tree->map_tree.lock);
1039 free_extent_map(em);
1044 static int fill_device_from_item(struct extent_buffer *leaf,
1045 struct btrfs_dev_item *dev_item,
1046 struct btrfs_device *device)
1050 device->devid = btrfs_device_id(leaf, dev_item);
1051 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1052 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1053 device->type = btrfs_device_type(leaf, dev_item);
1054 device->io_align = btrfs_device_io_align(leaf, dev_item);
1055 device->io_width = btrfs_device_io_width(leaf, dev_item);
1056 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
1058 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1059 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_DEV_UUID_SIZE);
1064 static int read_one_dev(struct btrfs_root *root,
1065 struct extent_buffer *leaf,
1066 struct btrfs_dev_item *dev_item)
1068 struct btrfs_device *device;
1072 devid = btrfs_device_id(leaf, dev_item);
1073 device = btrfs_find_device(root, devid);
1075 printk("warning devid %Lu not found already\n", devid);
1076 device = kmalloc(sizeof(*device), GFP_NOFS);
1079 list_add(&device->dev_list,
1080 &root->fs_info->fs_devices->devices);
1081 device->total_ios = 0;
1082 spin_lock_init(&device->io_lock);
1085 fill_device_from_item(leaf, dev_item, device);
1086 device->dev_root = root->fs_info->dev_root;
1089 ret = btrfs_open_device(device);
1097 int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
1099 struct btrfs_dev_item *dev_item;
1101 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
1103 return read_one_dev(root, buf, dev_item);
1106 int btrfs_read_sys_array(struct btrfs_root *root)
1108 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1109 struct extent_buffer *sb = root->fs_info->sb_buffer;
1110 struct btrfs_disk_key *disk_key;
1111 struct btrfs_chunk *chunk;
1112 struct btrfs_key key;
1117 unsigned long sb_ptr;
1121 array_size = btrfs_super_sys_array_size(super_copy);
1124 * we do this loop twice, once for the device items and
1125 * once for all of the chunks. This way there are device
1126 * structs filled in for every chunk
1128 ptr = super_copy->sys_chunk_array;
1129 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
1132 while (cur < array_size) {
1133 disk_key = (struct btrfs_disk_key *)ptr;
1134 btrfs_disk_key_to_cpu(&key, disk_key);
1136 len = sizeof(*disk_key);
1141 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1142 chunk = (struct btrfs_chunk *)sb_ptr;
1143 ret = read_one_chunk(root, &key, sb, chunk);
1145 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
1146 len = btrfs_chunk_item_size(num_stripes);
1157 int btrfs_read_chunk_tree(struct btrfs_root *root)
1159 struct btrfs_path *path;
1160 struct extent_buffer *leaf;
1161 struct btrfs_key key;
1162 struct btrfs_key found_key;
1166 root = root->fs_info->chunk_root;
1168 path = btrfs_alloc_path();
1172 /* first we search for all of the device items, and then we
1173 * read in all of the chunk items. This way we can create chunk
1174 * mappings that reference all of the devices that are afound
1176 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1180 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1182 leaf = path->nodes[0];
1183 slot = path->slots[0];
1184 if (slot >= btrfs_header_nritems(leaf)) {
1185 ret = btrfs_next_leaf(root, path);
1192 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1193 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1194 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
1196 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
1197 struct btrfs_dev_item *dev_item;
1198 dev_item = btrfs_item_ptr(leaf, slot,
1199 struct btrfs_dev_item);
1200 ret = read_one_dev(root, leaf, dev_item);
1203 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
1204 struct btrfs_chunk *chunk;
1205 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1206 ret = read_one_chunk(root, &found_key, leaf, chunk);
1210 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1212 btrfs_release_path(root, path);
1216 btrfs_free_path(path);