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;
630 u64 min_free = calc_size;
637 int stripe_len = 64 * 1024;
638 struct btrfs_key key;
640 if (list_empty(dev_list))
643 if (type & (BTRFS_BLOCK_GROUP_RAID0))
644 num_stripes = btrfs_super_num_devices(&info->super_copy);
645 if (type & (BTRFS_BLOCK_GROUP_DUP))
647 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
648 num_stripes = min_t(u64, 2,
649 btrfs_super_num_devices(&info->super_copy));
652 INIT_LIST_HEAD(&private_devs);
653 cur = dev_list->next;
656 if (type & BTRFS_BLOCK_GROUP_DUP)
657 min_free = calc_size * 2;
659 /* build a private list of devices we will allocate from */
660 while(index < num_stripes) {
661 device = list_entry(cur, struct btrfs_device, dev_list);
663 avail = device->total_bytes - device->bytes_used;
665 if (avail > max_avail)
667 if (avail >= min_free) {
668 list_move_tail(&device->dev_list, &private_devs);
670 if (type & BTRFS_BLOCK_GROUP_DUP)
676 if (index < num_stripes) {
677 list_splice(&private_devs, dev_list);
678 if (!looped && max_avail > 0) {
680 calc_size = max_avail;
686 ret = find_next_chunk(chunk_root, &key.objectid);
690 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
694 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
700 stripes = &chunk->stripe;
702 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
703 *num_bytes = calc_size;
705 *num_bytes = calc_size * num_stripes;
708 printk("new chunk type %Lu start %Lu size %Lu\n", type, key.objectid, *num_bytes);
709 while(index < num_stripes) {
710 BUG_ON(list_empty(&private_devs));
711 cur = private_devs.next;
712 device = list_entry(cur, struct btrfs_device, dev_list);
714 /* loop over this device again if we're doing a dup group */
715 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
716 (index == num_stripes - 1))
717 list_move_tail(&device->dev_list, dev_list);
719 ret = btrfs_alloc_dev_extent(trans, device,
721 calc_size, &dev_offset);
723 printk("alloc chunk start %Lu size %Lu from dev %Lu type %Lu\n", key.objectid, calc_size, device->devid, type);
724 device->bytes_used += calc_size;
725 ret = btrfs_update_device(trans, device);
728 map->stripes[index].dev = device;
729 map->stripes[index].physical = dev_offset;
730 btrfs_set_stack_stripe_devid(stripes + index, device->devid);
731 btrfs_set_stack_stripe_offset(stripes + index, dev_offset);
732 physical = dev_offset;
735 BUG_ON(!list_empty(&private_devs));
737 /* key.objectid was set above */
738 key.offset = *num_bytes;
739 key.type = BTRFS_CHUNK_ITEM_KEY;
740 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
741 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
742 btrfs_set_stack_chunk_type(chunk, type);
743 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
744 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
745 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
746 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
747 map->sector_size = extent_root->sectorsize;
748 map->stripe_len = stripe_len;
749 map->io_align = stripe_len;
750 map->io_width = stripe_len;
752 map->num_stripes = num_stripes;
754 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
755 btrfs_chunk_item_size(num_stripes));
757 *start = key.objectid;
759 em = alloc_extent_map(GFP_NOFS);
762 em->bdev = (struct block_device *)map;
763 em->start = key.objectid;
764 em->len = key.offset;
769 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
770 spin_lock(&em_tree->lock);
771 ret = add_extent_mapping(em_tree, em);
773 spin_unlock(&em_tree->lock);
778 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
780 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
783 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
785 struct extent_map *em;
788 spin_lock(&tree->map_tree.lock);
789 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
791 remove_extent_mapping(&tree->map_tree, em);
792 spin_unlock(&tree->map_tree.lock);
798 /* once for the tree */
803 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
804 int dev_nr, u64 logical, u64 *phys, u64 *length,
805 struct btrfs_device **dev, int *total_devs)
807 struct extent_map *em;
808 struct map_lookup *map;
809 struct extent_map_tree *em_tree = &map_tree->map_tree;
816 spin_lock(&em_tree->lock);
817 em = lookup_extent_mapping(em_tree, logical, *length);
820 BUG_ON(em->start > logical || em->start + em->len < logical);
821 map = (struct map_lookup *)em->bdev;
822 offset = logical - em->start;
826 * stripe_nr counts the total number of stripes we have to stride
827 * to get to this block
829 do_div(stripe_nr, map->stripe_len);
831 stripe_offset = stripe_nr * map->stripe_len;
832 BUG_ON(offset < stripe_offset);
834 /* stripe_offset is the offset of this block in its stripe*/
835 stripe_offset = offset - stripe_offset;
837 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
838 stripe_index = dev_nr;
839 if (rw & (1 << BIO_RW))
840 *total_devs = map->num_stripes;
844 struct btrfs_device *cur;
846 for (i = 0; i < map->num_stripes; i++) {
847 cur = map->stripes[i].dev;
848 spin_lock(&cur->io_lock);
849 if (cur->total_ios < least) {
850 least = cur->total_ios;
853 spin_unlock(&cur->io_lock);
857 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
859 *total_devs = map->num_stripes;
860 stripe_index = dev_nr;
867 * after this do_div call, stripe_nr is the number of stripes
868 * on this device we have to walk to find the data, and
869 * stripe_index is the number of our device in the stripe array
871 stripe_index = do_div(stripe_nr, map->num_stripes);
873 BUG_ON(stripe_index >= map->num_stripes);
874 *phys = map->stripes[stripe_index].physical + stripe_offset +
875 stripe_nr * map->stripe_len;
877 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
878 BTRFS_BLOCK_GROUP_DUP)) {
879 /* we limit the length of each bio to what fits in a stripe */
880 *length = min_t(u64, em->len - offset,
881 map->stripe_len - stripe_offset);
883 *length = em->len - offset;
885 *dev = map->stripes[stripe_index].dev;
887 spin_unlock(&em_tree->lock);
891 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
892 static void end_bio_multi_stripe(struct bio *bio, int err)
894 static int end_bio_multi_stripe(struct bio *bio,
895 unsigned int bytes_done, int err)
898 struct multi_bio *multi = bio->bi_private;
900 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
907 if (atomic_dec_and_test(&multi->stripes)) {
908 bio->bi_private = multi->private;
909 bio->bi_end_io = multi->end_io;
911 if (!err && multi->error)
919 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
924 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio)
926 struct btrfs_mapping_tree *map_tree;
927 struct btrfs_device *dev;
928 struct bio *first_bio = bio;
929 u64 logical = bio->bi_sector << 9;
933 struct bio_vec *bvec;
934 struct multi_bio *multi = NULL;
940 bio_for_each_segment(bvec, bio, i) {
941 length += bvec->bv_len;
944 map_tree = &root->fs_info->mapping_tree;
946 while(dev_nr < total_devs) {
947 ret = btrfs_map_block(map_tree, rw, dev_nr, logical,
948 &physical, &map_length, &dev,
950 if (map_length < length) {
951 printk("mapping failed logical %Lu bio len %Lu physical %Lu "
952 "len %Lu\n", logical, length, physical, map_length);
955 BUG_ON(map_length < length);
956 if (total_devs > 1) {
958 multi = kmalloc(sizeof(*multi), GFP_NOFS);
959 atomic_set(&multi->stripes, 1);
960 multi->end_io = bio->bi_end_io;
961 multi->private = first_bio->bi_private;
964 atomic_inc(&multi->stripes);
966 if (dev_nr < total_devs - 1) {
967 bio = bio_clone(first_bio, GFP_NOFS);
972 bio->bi_private = multi;
973 bio->bi_end_io = end_bio_multi_stripe;
975 bio->bi_sector = physical >> 9;
976 bio->bi_bdev = dev->bdev;
977 spin_lock(&dev->io_lock);
979 spin_unlock(&dev->io_lock);
986 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid)
988 struct list_head *head = &root->fs_info->fs_devices->devices;
990 return __find_device(head, devid);
993 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
994 struct extent_buffer *leaf,
995 struct btrfs_chunk *chunk)
997 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
998 struct map_lookup *map;
999 struct extent_map *em;
1007 logical = key->objectid;
1008 length = key->offset;
1009 spin_lock(&map_tree->map_tree.lock);
1010 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
1012 /* already mapped? */
1013 if (em && em->start <= logical && em->start + em->len > logical) {
1014 free_extent_map(em);
1015 spin_unlock(&map_tree->map_tree.lock);
1018 free_extent_map(em);
1020 spin_unlock(&map_tree->map_tree.lock);
1022 map = kzalloc(sizeof(*map), GFP_NOFS);
1026 em = alloc_extent_map(GFP_NOFS);
1029 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1030 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1032 free_extent_map(em);
1036 em->bdev = (struct block_device *)map;
1037 em->start = logical;
1039 em->block_start = 0;
1041 map->num_stripes = num_stripes;
1042 map->io_width = btrfs_chunk_io_width(leaf, chunk);
1043 map->io_align = btrfs_chunk_io_align(leaf, chunk);
1044 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1045 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1046 map->type = btrfs_chunk_type(leaf, chunk);
1047 for (i = 0; i < num_stripes; i++) {
1048 map->stripes[i].physical =
1049 btrfs_stripe_offset_nr(leaf, chunk, i);
1050 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
1051 map->stripes[i].dev = btrfs_find_device(root, devid);
1052 if (!map->stripes[i].dev) {
1054 free_extent_map(em);
1059 spin_lock(&map_tree->map_tree.lock);
1060 ret = add_extent_mapping(&map_tree->map_tree, em);
1062 spin_unlock(&map_tree->map_tree.lock);
1063 free_extent_map(em);
1068 static int fill_device_from_item(struct extent_buffer *leaf,
1069 struct btrfs_dev_item *dev_item,
1070 struct btrfs_device *device)
1074 device->devid = btrfs_device_id(leaf, dev_item);
1075 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1076 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1077 device->type = btrfs_device_type(leaf, dev_item);
1078 device->io_align = btrfs_device_io_align(leaf, dev_item);
1079 device->io_width = btrfs_device_io_width(leaf, dev_item);
1080 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
1082 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1083 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_DEV_UUID_SIZE);
1088 static int read_one_dev(struct btrfs_root *root,
1089 struct extent_buffer *leaf,
1090 struct btrfs_dev_item *dev_item)
1092 struct btrfs_device *device;
1096 devid = btrfs_device_id(leaf, dev_item);
1097 device = btrfs_find_device(root, devid);
1099 printk("warning devid %Lu not found already\n", devid);
1100 device = kmalloc(sizeof(*device), GFP_NOFS);
1103 list_add(&device->dev_list,
1104 &root->fs_info->fs_devices->devices);
1105 device->total_ios = 0;
1106 spin_lock_init(&device->io_lock);
1109 fill_device_from_item(leaf, dev_item, device);
1110 device->dev_root = root->fs_info->dev_root;
1113 ret = btrfs_open_device(device);
1121 int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
1123 struct btrfs_dev_item *dev_item;
1125 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
1127 return read_one_dev(root, buf, dev_item);
1130 int btrfs_read_sys_array(struct btrfs_root *root)
1132 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1133 struct extent_buffer *sb = root->fs_info->sb_buffer;
1134 struct btrfs_disk_key *disk_key;
1135 struct btrfs_chunk *chunk;
1136 struct btrfs_key key;
1141 unsigned long sb_ptr;
1145 array_size = btrfs_super_sys_array_size(super_copy);
1148 * we do this loop twice, once for the device items and
1149 * once for all of the chunks. This way there are device
1150 * structs filled in for every chunk
1152 ptr = super_copy->sys_chunk_array;
1153 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
1156 while (cur < array_size) {
1157 disk_key = (struct btrfs_disk_key *)ptr;
1158 btrfs_disk_key_to_cpu(&key, disk_key);
1160 len = sizeof(*disk_key);
1165 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1166 chunk = (struct btrfs_chunk *)sb_ptr;
1167 ret = read_one_chunk(root, &key, sb, chunk);
1169 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
1170 len = btrfs_chunk_item_size(num_stripes);
1181 int btrfs_read_chunk_tree(struct btrfs_root *root)
1183 struct btrfs_path *path;
1184 struct extent_buffer *leaf;
1185 struct btrfs_key key;
1186 struct btrfs_key found_key;
1190 root = root->fs_info->chunk_root;
1192 path = btrfs_alloc_path();
1196 /* first we search for all of the device items, and then we
1197 * read in all of the chunk items. This way we can create chunk
1198 * mappings that reference all of the devices that are afound
1200 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1204 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1206 leaf = path->nodes[0];
1207 slot = path->slots[0];
1208 if (slot >= btrfs_header_nritems(leaf)) {
1209 ret = btrfs_next_leaf(root, path);
1216 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1217 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1218 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
1220 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
1221 struct btrfs_dev_item *dev_item;
1222 dev_item = btrfs_item_ptr(leaf, slot,
1223 struct btrfs_dev_item);
1224 ret = read_one_dev(root, leaf, dev_item);
1227 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
1228 struct btrfs_chunk *chunk;
1229 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1230 ret = read_one_chunk(root, &found_key, leaf, chunk);
1234 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1236 btrfs_release_path(root, path);
1240 btrfs_free_path(path);