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 device->name = kstrdup(path, GFP_NOFS);
134 list_add(&device->dev_list, &fs_devices->devices);
135 fs_devices->num_devices++;
138 if (found_transid > fs_devices->latest_trans) {
139 fs_devices->latest_devid = devid;
140 fs_devices->latest_trans = found_transid;
142 if (fs_devices->lowest_devid > devid) {
143 fs_devices->lowest_devid = devid;
144 printk("lowest devid now %Lu\n", devid);
146 *fs_devices_ret = fs_devices;
150 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
152 struct list_head *head = &fs_devices->devices;
153 struct list_head *cur;
154 struct btrfs_device *device;
156 mutex_lock(&uuid_mutex);
157 list_for_each(cur, head) {
158 device = list_entry(cur, struct btrfs_device, dev_list);
160 close_bdev_excl(device->bdev);
161 printk("close devices closes %s\n", device->name);
165 mutex_unlock(&uuid_mutex);
169 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
170 int flags, void *holder)
172 struct block_device *bdev;
173 struct list_head *head = &fs_devices->devices;
174 struct list_head *cur;
175 struct btrfs_device *device;
178 mutex_lock(&uuid_mutex);
179 list_for_each(cur, head) {
180 device = list_entry(cur, struct btrfs_device, dev_list);
181 bdev = open_bdev_excl(device->name, flags, holder);
182 printk("opening %s devid %Lu\n", device->name, device->devid);
184 printk("open %s failed\n", device->name);
188 if (device->devid == fs_devices->latest_devid)
189 fs_devices->latest_bdev = bdev;
190 if (device->devid == fs_devices->lowest_devid) {
191 fs_devices->lowest_bdev = bdev;
192 printk("lowest bdev %s\n", device->name);
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 owner, u64 num_bytes, u64 *start)
377 struct btrfs_path *path;
378 struct btrfs_root *root = device->dev_root;
379 struct btrfs_dev_extent *extent;
380 struct extent_buffer *leaf;
381 struct btrfs_key key;
383 path = btrfs_alloc_path();
387 ret = find_free_dev_extent(trans, device, path, num_bytes, start);
392 key.objectid = device->devid;
394 key.type = BTRFS_DEV_EXTENT_KEY;
395 ret = btrfs_insert_empty_item(trans, root, path, &key,
399 leaf = path->nodes[0];
400 extent = btrfs_item_ptr(leaf, path->slots[0],
401 struct btrfs_dev_extent);
402 btrfs_set_dev_extent_owner(leaf, extent, owner);
403 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
404 btrfs_mark_buffer_dirty(leaf);
406 btrfs_free_path(path);
410 static int find_next_chunk(struct btrfs_root *root, u64 *objectid)
412 struct btrfs_path *path;
414 struct btrfs_key key;
415 struct btrfs_key found_key;
417 path = btrfs_alloc_path();
420 key.objectid = (u64)-1;
421 key.offset = (u64)-1;
422 key.type = BTRFS_CHUNK_ITEM_KEY;
424 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
430 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
434 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
436 *objectid = found_key.objectid + found_key.offset;
440 btrfs_free_path(path);
444 static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
448 struct btrfs_key key;
449 struct btrfs_key found_key;
451 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
452 key.type = BTRFS_DEV_ITEM_KEY;
453 key.offset = (u64)-1;
455 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
461 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
466 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
468 *objectid = found_key.offset + 1;
472 btrfs_release_path(root, path);
477 * the device information is stored in the chunk root
478 * the btrfs_device struct should be fully filled in
480 int btrfs_add_device(struct btrfs_trans_handle *trans,
481 struct btrfs_root *root,
482 struct btrfs_device *device)
485 struct btrfs_path *path;
486 struct btrfs_dev_item *dev_item;
487 struct extent_buffer *leaf;
488 struct btrfs_key key;
492 root = root->fs_info->chunk_root;
494 path = btrfs_alloc_path();
498 ret = find_next_devid(root, path, &free_devid);
502 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
503 key.type = BTRFS_DEV_ITEM_KEY;
504 key.offset = free_devid;
506 ret = btrfs_insert_empty_item(trans, root, path, &key,
511 leaf = path->nodes[0];
512 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
514 device->devid = free_devid;
515 btrfs_set_device_id(leaf, dev_item, device->devid);
516 btrfs_set_device_type(leaf, dev_item, device->type);
517 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
518 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
519 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
520 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
521 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
523 ptr = (unsigned long)btrfs_device_uuid(dev_item);
524 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_DEV_UUID_SIZE);
525 btrfs_mark_buffer_dirty(leaf);
529 btrfs_free_path(path);
532 int btrfs_update_device(struct btrfs_trans_handle *trans,
533 struct btrfs_device *device)
536 struct btrfs_path *path;
537 struct btrfs_root *root;
538 struct btrfs_dev_item *dev_item;
539 struct extent_buffer *leaf;
540 struct btrfs_key key;
542 root = device->dev_root->fs_info->chunk_root;
544 path = btrfs_alloc_path();
548 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
549 key.type = BTRFS_DEV_ITEM_KEY;
550 key.offset = device->devid;
552 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
561 leaf = path->nodes[0];
562 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
564 btrfs_set_device_id(leaf, dev_item, device->devid);
565 btrfs_set_device_type(leaf, dev_item, device->type);
566 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
567 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
568 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
569 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
570 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
571 btrfs_mark_buffer_dirty(leaf);
574 btrfs_free_path(path);
578 int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
579 struct btrfs_root *root,
580 struct btrfs_key *key,
581 struct btrfs_chunk *chunk, int item_size)
583 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
584 struct btrfs_disk_key disk_key;
588 array_size = btrfs_super_sys_array_size(super_copy);
589 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
592 ptr = super_copy->sys_chunk_array + array_size;
593 btrfs_cpu_key_to_disk(&disk_key, key);
594 memcpy(ptr, &disk_key, sizeof(disk_key));
595 ptr += sizeof(disk_key);
596 memcpy(ptr, chunk, item_size);
597 item_size += sizeof(disk_key);
598 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
602 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
603 struct btrfs_root *extent_root, u64 *start,
604 u64 *num_bytes, u64 type)
607 struct btrfs_fs_info *info = extent_root->fs_info;
608 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
609 struct btrfs_stripe *stripes;
610 struct btrfs_device *device = NULL;
611 struct btrfs_chunk *chunk;
612 struct list_head private_devs;
613 struct list_head *dev_list = &extent_root->fs_info->fs_devices->devices;
614 struct list_head *cur;
615 struct extent_map_tree *em_tree;
616 struct map_lookup *map;
617 struct extent_map *em;
619 u64 calc_size = 1024 * 1024 * 1024;
620 u64 min_free = calc_size;
627 int stripe_len = 64 * 1024;
628 struct btrfs_key key;
630 if (list_empty(dev_list))
633 if (type & (BTRFS_BLOCK_GROUP_RAID0))
634 num_stripes = btrfs_super_num_devices(&info->super_copy);
635 if (type & (BTRFS_BLOCK_GROUP_DUP))
637 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
638 num_stripes = min_t(u64, 2,
639 btrfs_super_num_devices(&info->super_copy));
642 INIT_LIST_HEAD(&private_devs);
643 cur = dev_list->next;
646 if (type & BTRFS_BLOCK_GROUP_DUP)
647 min_free = calc_size * 2;
649 /* build a private list of devices we will allocate from */
650 while(index < num_stripes) {
651 device = list_entry(cur, struct btrfs_device, dev_list);
653 avail = device->total_bytes - device->bytes_used;
655 if (avail > max_avail)
657 if (avail >= min_free) {
658 list_move_tail(&device->dev_list, &private_devs);
660 if (type & BTRFS_BLOCK_GROUP_DUP)
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 | BTRFS_BLOCK_GROUP_DUP))
693 *num_bytes = calc_size;
695 *num_bytes = calc_size * num_stripes;
698 printk("new chunk type %Lu start %Lu size %Lu\n", type, key.objectid, *num_bytes);
699 while(index < num_stripes) {
700 BUG_ON(list_empty(&private_devs));
701 cur = private_devs.next;
702 device = list_entry(cur, struct btrfs_device, dev_list);
704 /* loop over this device again if we're doing a dup group */
705 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
706 (index == num_stripes - 1))
707 list_move_tail(&device->dev_list, dev_list);
709 ret = btrfs_alloc_dev_extent(trans, device,
711 calc_size, &dev_offset);
713 printk("alloc chunk start %Lu size %Lu from dev %Lu type %Lu\n", key.objectid, calc_size, device->devid, type);
714 device->bytes_used += calc_size;
715 ret = btrfs_update_device(trans, device);
718 map->stripes[index].dev = device;
719 map->stripes[index].physical = dev_offset;
720 btrfs_set_stack_stripe_devid(stripes + index, device->devid);
721 btrfs_set_stack_stripe_offset(stripes + index, dev_offset);
722 physical = dev_offset;
725 BUG_ON(!list_empty(&private_devs));
727 /* key.objectid was set above */
728 key.offset = *num_bytes;
729 key.type = BTRFS_CHUNK_ITEM_KEY;
730 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
731 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
732 btrfs_set_stack_chunk_type(chunk, type);
733 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
734 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
735 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
736 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
737 map->sector_size = extent_root->sectorsize;
738 map->stripe_len = stripe_len;
739 map->io_align = stripe_len;
740 map->io_width = stripe_len;
742 map->num_stripes = num_stripes;
744 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
745 btrfs_chunk_item_size(num_stripes));
747 *start = key.objectid;
749 em = alloc_extent_map(GFP_NOFS);
752 em->bdev = (struct block_device *)map;
753 em->start = key.objectid;
754 em->len = key.offset;
759 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
760 spin_lock(&em_tree->lock);
761 ret = add_extent_mapping(em_tree, em);
763 spin_unlock(&em_tree->lock);
768 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
770 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
773 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
775 struct extent_map *em;
778 spin_lock(&tree->map_tree.lock);
779 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
781 remove_extent_mapping(&tree->map_tree, em);
782 spin_unlock(&tree->map_tree.lock);
788 /* once for the tree */
793 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
795 struct extent_map *em;
796 struct map_lookup *map;
797 struct extent_map_tree *em_tree = &map_tree->map_tree;
800 spin_lock(&em_tree->lock);
801 em = lookup_extent_mapping(em_tree, logical, len);
804 BUG_ON(em->start > logical || em->start + em->len < logical);
805 map = (struct map_lookup *)em->bdev;
806 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
807 ret = map->num_stripes;
811 spin_unlock(&em_tree->lock);
815 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
816 u64 logical, u64 *length,
817 struct btrfs_multi_bio **multi_ret, int mirror_num)
819 struct extent_map *em;
820 struct map_lookup *map;
821 struct extent_map_tree *em_tree = &map_tree->map_tree;
825 int stripes_allocated = 8;
828 struct btrfs_multi_bio *multi = NULL;
830 if (multi_ret && !(rw & (1 << BIO_RW))) {
831 stripes_allocated = 1;
835 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
841 spin_lock(&em_tree->lock);
842 em = lookup_extent_mapping(em_tree, logical, *length);
845 BUG_ON(em->start > logical || em->start + em->len < logical);
846 map = (struct map_lookup *)em->bdev;
847 offset = logical - em->start;
849 if (mirror_num > map->num_stripes)
852 /* if our multi bio struct is too small, back off and try again */
853 if (multi_ret && (rw & (1 << BIO_RW)) &&
854 stripes_allocated < map->num_stripes &&
855 ((map->type & BTRFS_BLOCK_GROUP_RAID1) ||
856 (map->type & BTRFS_BLOCK_GROUP_DUP))) {
857 stripes_allocated = map->num_stripes;
858 spin_unlock(&em_tree->lock);
865 * stripe_nr counts the total number of stripes we have to stride
866 * to get to this block
868 do_div(stripe_nr, map->stripe_len);
870 stripe_offset = stripe_nr * map->stripe_len;
871 BUG_ON(offset < stripe_offset);
873 /* stripe_offset is the offset of this block in its stripe*/
874 stripe_offset = offset - stripe_offset;
876 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
877 BTRFS_BLOCK_GROUP_DUP)) {
878 /* we limit the length of each bio to what fits in a stripe */
879 *length = min_t(u64, em->len - offset,
880 map->stripe_len - stripe_offset);
882 *length = em->len - offset;
887 multi->num_stripes = 1;
889 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
890 if (rw & (1 << BIO_RW))
891 multi->num_stripes = map->num_stripes;
892 else if (mirror_num) {
893 stripe_index = mirror_num - 1;
897 struct btrfs_device *cur;
899 for (i = 0; i < map->num_stripes; i++) {
900 cur = map->stripes[i].dev;
901 spin_lock(&cur->io_lock);
902 if (cur->total_ios < least) {
903 least = cur->total_ios;
906 spin_unlock(&cur->io_lock);
909 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
910 if (rw & (1 << BIO_RW))
911 multi->num_stripes = map->num_stripes;
913 stripe_index = mirror_num - 1;
916 * after this do_div call, stripe_nr is the number of stripes
917 * on this device we have to walk to find the data, and
918 * stripe_index is the number of our device in the stripe array
920 stripe_index = do_div(stripe_nr, map->num_stripes);
922 BUG_ON(stripe_index >= map->num_stripes);
923 BUG_ON(stripe_index != 0 && multi->num_stripes > 1);
925 for (i = 0; i < multi->num_stripes; i++) {
926 multi->stripes[i].physical =
927 map->stripes[stripe_index].physical + stripe_offset +
928 stripe_nr * map->stripe_len;
929 multi->stripes[i].dev = map->stripes[stripe_index].dev;
935 spin_unlock(&em_tree->lock);
939 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
940 static void end_bio_multi_stripe(struct bio *bio, int err)
942 static int end_bio_multi_stripe(struct bio *bio,
943 unsigned int bytes_done, int err)
946 struct btrfs_multi_bio *multi = bio->bi_private;
948 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
955 if (atomic_dec_and_test(&multi->stripes_pending)) {
956 bio->bi_private = multi->private;
957 bio->bi_end_io = multi->end_io;
959 if (!err && multi->error)
967 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
972 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
975 struct btrfs_mapping_tree *map_tree;
976 struct btrfs_device *dev;
977 struct bio *first_bio = bio;
978 u64 logical = bio->bi_sector << 9;
981 struct bio_vec *bvec;
982 struct btrfs_multi_bio *multi = NULL;
988 bio_for_each_segment(bvec, bio, i) {
989 length += bvec->bv_len;
992 map_tree = &root->fs_info->mapping_tree;
995 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
999 total_devs = multi->num_stripes;
1000 if (map_length < length) {
1001 printk("mapping failed logical %Lu bio len %Lu "
1002 "len %Lu\n", logical, length, map_length);
1005 multi->end_io = first_bio->bi_end_io;
1006 multi->private = first_bio->bi_private;
1007 atomic_set(&multi->stripes_pending, multi->num_stripes);
1009 while(dev_nr < total_devs) {
1010 if (total_devs > 1) {
1011 if (dev_nr < total_devs - 1) {
1012 bio = bio_clone(first_bio, GFP_NOFS);
1017 bio->bi_private = multi;
1018 bio->bi_end_io = end_bio_multi_stripe;
1020 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
1021 dev = multi->stripes[dev_nr].dev;
1022 bio->bi_bdev = dev->bdev;
1023 spin_lock(&dev->io_lock);
1025 spin_unlock(&dev->io_lock);
1026 submit_bio(rw, bio);
1029 if (total_devs == 1)
1034 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid)
1036 struct list_head *head = &root->fs_info->fs_devices->devices;
1038 return __find_device(head, devid);
1041 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
1042 struct extent_buffer *leaf,
1043 struct btrfs_chunk *chunk)
1045 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1046 struct map_lookup *map;
1047 struct extent_map *em;
1055 logical = key->objectid;
1056 length = key->offset;
1057 spin_lock(&map_tree->map_tree.lock);
1058 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
1060 /* already mapped? */
1061 if (em && em->start <= logical && em->start + em->len > logical) {
1062 free_extent_map(em);
1063 spin_unlock(&map_tree->map_tree.lock);
1066 free_extent_map(em);
1068 spin_unlock(&map_tree->map_tree.lock);
1070 map = kzalloc(sizeof(*map), GFP_NOFS);
1074 em = alloc_extent_map(GFP_NOFS);
1077 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1078 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1080 free_extent_map(em);
1084 em->bdev = (struct block_device *)map;
1085 em->start = logical;
1087 em->block_start = 0;
1089 map->num_stripes = num_stripes;
1090 map->io_width = btrfs_chunk_io_width(leaf, chunk);
1091 map->io_align = btrfs_chunk_io_align(leaf, chunk);
1092 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1093 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1094 map->type = btrfs_chunk_type(leaf, chunk);
1095 for (i = 0; i < num_stripes; i++) {
1096 map->stripes[i].physical =
1097 btrfs_stripe_offset_nr(leaf, chunk, i);
1098 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
1099 map->stripes[i].dev = btrfs_find_device(root, devid);
1100 if (!map->stripes[i].dev) {
1102 free_extent_map(em);
1107 spin_lock(&map_tree->map_tree.lock);
1108 ret = add_extent_mapping(&map_tree->map_tree, em);
1110 spin_unlock(&map_tree->map_tree.lock);
1111 free_extent_map(em);
1116 static int fill_device_from_item(struct extent_buffer *leaf,
1117 struct btrfs_dev_item *dev_item,
1118 struct btrfs_device *device)
1122 device->devid = btrfs_device_id(leaf, dev_item);
1123 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1124 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1125 device->type = btrfs_device_type(leaf, dev_item);
1126 device->io_align = btrfs_device_io_align(leaf, dev_item);
1127 device->io_width = btrfs_device_io_width(leaf, dev_item);
1128 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
1130 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1131 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_DEV_UUID_SIZE);
1136 static int read_one_dev(struct btrfs_root *root,
1137 struct extent_buffer *leaf,
1138 struct btrfs_dev_item *dev_item)
1140 struct btrfs_device *device;
1144 devid = btrfs_device_id(leaf, dev_item);
1145 device = btrfs_find_device(root, devid);
1147 printk("warning devid %Lu not found already\n", devid);
1148 device = kzalloc(sizeof(*device), GFP_NOFS);
1151 list_add(&device->dev_list,
1152 &root->fs_info->fs_devices->devices);
1153 device->total_ios = 0;
1154 spin_lock_init(&device->io_lock);
1157 fill_device_from_item(leaf, dev_item, device);
1158 device->dev_root = root->fs_info->dev_root;
1161 ret = btrfs_open_device(device);
1169 int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
1171 struct btrfs_dev_item *dev_item;
1173 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
1175 return read_one_dev(root, buf, dev_item);
1178 int btrfs_read_sys_array(struct btrfs_root *root)
1180 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1181 struct extent_buffer *sb = root->fs_info->sb_buffer;
1182 struct btrfs_disk_key *disk_key;
1183 struct btrfs_chunk *chunk;
1184 struct btrfs_key key;
1189 unsigned long sb_ptr;
1193 array_size = btrfs_super_sys_array_size(super_copy);
1196 * we do this loop twice, once for the device items and
1197 * once for all of the chunks. This way there are device
1198 * structs filled in for every chunk
1200 ptr = super_copy->sys_chunk_array;
1201 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
1204 while (cur < array_size) {
1205 disk_key = (struct btrfs_disk_key *)ptr;
1206 btrfs_disk_key_to_cpu(&key, disk_key);
1208 len = sizeof(*disk_key);
1213 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1214 chunk = (struct btrfs_chunk *)sb_ptr;
1215 ret = read_one_chunk(root, &key, sb, chunk);
1217 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
1218 len = btrfs_chunk_item_size(num_stripes);
1229 int btrfs_read_chunk_tree(struct btrfs_root *root)
1231 struct btrfs_path *path;
1232 struct extent_buffer *leaf;
1233 struct btrfs_key key;
1234 struct btrfs_key found_key;
1238 root = root->fs_info->chunk_root;
1240 path = btrfs_alloc_path();
1244 /* first we search for all of the device items, and then we
1245 * read in all of the chunk items. This way we can create chunk
1246 * mappings that reference all of the devices that are afound
1248 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1252 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1254 leaf = path->nodes[0];
1255 slot = path->slots[0];
1256 if (slot >= btrfs_header_nritems(leaf)) {
1257 ret = btrfs_next_leaf(root, path);
1264 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1265 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1266 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
1268 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
1269 struct btrfs_dev_item *dev_item;
1270 dev_item = btrfs_item_ptr(leaf, slot,
1271 struct btrfs_dev_item);
1272 ret = read_one_dev(root, leaf, dev_item);
1275 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
1276 struct btrfs_chunk *chunk;
1277 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1278 ret = read_one_chunk(root, &found_key, leaf, chunk);
1282 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1284 btrfs_release_path(root, path);
1288 btrfs_free_path(path);