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 #define _XOPEN_SOURCE 600
22 #include <sys/types.h>
24 #include <uuid/uuid.h>
29 #include "transaction.h"
30 #include "print-tree.h"
34 struct btrfs_device *dev;
38 static inline int nr_parity_stripes(struct map_lookup *map)
40 if (map->type & BTRFS_BLOCK_GROUP_RAID5)
42 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
48 static inline int nr_data_stripes(struct map_lookup *map)
50 return map->num_stripes - nr_parity_stripes(map);
53 #define is_parity_stripe(x) ( ((x) == BTRFS_RAID5_P_STRIPE) || ((x) == BTRFS_RAID6_Q_STRIPE) )
55 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
56 (sizeof(struct btrfs_bio_stripe) * (n)))
58 static LIST_HEAD(fs_uuids);
60 static struct btrfs_device *__find_device(struct list_head *head, u64 devid,
63 struct btrfs_device *dev;
64 struct list_head *cur;
66 list_for_each(cur, head) {
67 dev = list_entry(cur, struct btrfs_device, dev_list);
68 if (dev->devid == devid &&
69 !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE)) {
76 static struct btrfs_fs_devices *find_fsid(u8 *fsid)
78 struct list_head *cur;
79 struct btrfs_fs_devices *fs_devices;
81 list_for_each(cur, &fs_uuids) {
82 fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
83 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
89 static int device_list_add(const char *path,
90 struct btrfs_super_block *disk_super,
91 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
93 struct btrfs_device *device;
94 struct btrfs_fs_devices *fs_devices;
95 u64 found_transid = btrfs_super_generation(disk_super);
97 fs_devices = find_fsid(disk_super->fsid);
99 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
102 INIT_LIST_HEAD(&fs_devices->devices);
103 list_add(&fs_devices->list, &fs_uuids);
104 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
105 fs_devices->latest_devid = devid;
106 fs_devices->latest_trans = found_transid;
107 fs_devices->lowest_devid = (u64)-1;
110 device = __find_device(&fs_devices->devices, devid,
111 disk_super->dev_item.uuid);
114 device = kzalloc(sizeof(*device), GFP_NOFS);
116 /* we can safely leave the fs_devices entry around */
120 device->devid = devid;
121 memcpy(device->uuid, disk_super->dev_item.uuid,
123 device->name = kstrdup(path, GFP_NOFS);
128 device->label = kstrdup(disk_super->label, GFP_NOFS);
129 device->total_devs = btrfs_super_num_devices(disk_super);
130 device->super_bytes_used = btrfs_super_bytes_used(disk_super);
131 device->total_bytes =
132 btrfs_stack_device_total_bytes(&disk_super->dev_item);
134 btrfs_stack_device_bytes_used(&disk_super->dev_item);
135 list_add(&device->dev_list, &fs_devices->devices);
136 device->fs_devices = fs_devices;
137 } else if (!device->name || strcmp(device->name, path)) {
138 char *name = strdup(path);
146 if (found_transid > fs_devices->latest_trans) {
147 fs_devices->latest_devid = devid;
148 fs_devices->latest_trans = found_transid;
150 if (fs_devices->lowest_devid > devid) {
151 fs_devices->lowest_devid = devid;
153 *fs_devices_ret = fs_devices;
157 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
159 struct btrfs_fs_devices *seed_devices;
160 struct list_head *cur;
161 struct btrfs_device *device;
163 list_for_each(cur, &fs_devices->devices) {
164 device = list_entry(cur, struct btrfs_device, dev_list);
165 if (device->fd != -1) {
169 device->writeable = 0;
172 seed_devices = fs_devices->seed;
173 fs_devices->seed = NULL;
175 fs_devices = seed_devices;
182 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, int flags)
185 struct list_head *head = &fs_devices->devices;
186 struct list_head *cur;
187 struct btrfs_device *device;
190 list_for_each(cur, head) {
191 device = list_entry(cur, struct btrfs_device, dev_list);
193 fd = open(device->name, flags);
199 if (posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED))
200 fprintf(stderr, "Warning, could not drop caches\n");
202 if (device->devid == fs_devices->latest_devid)
203 fs_devices->latest_bdev = fd;
204 if (device->devid == fs_devices->lowest_devid)
205 fs_devices->lowest_bdev = fd;
208 device->writeable = 1;
212 btrfs_close_devices(fs_devices);
216 int btrfs_scan_one_device(int fd, const char *path,
217 struct btrfs_fs_devices **fs_devices_ret,
218 u64 *total_devs, u64 super_offset)
220 struct btrfs_super_block *disk_super;
231 disk_super = (struct btrfs_super_block *)buf;
232 ret = btrfs_read_dev_super(fd, disk_super, super_offset);
237 devid = le64_to_cpu(disk_super->dev_item.devid);
238 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)
241 *total_devs = btrfs_super_num_devices(disk_super);
242 uuid_unparse(disk_super->fsid, uuidbuf);
244 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
253 * this uses a pretty simple search, the expectation is that it is
254 * called very infrequently and that a given device has a small number
257 static int find_free_dev_extent(struct btrfs_trans_handle *trans,
258 struct btrfs_device *device,
259 struct btrfs_path *path,
260 u64 num_bytes, u64 *start)
262 struct btrfs_key key;
263 struct btrfs_root *root = device->dev_root;
264 struct btrfs_dev_extent *dev_extent = NULL;
267 u64 search_start = 0;
268 u64 search_end = device->total_bytes;
272 struct extent_buffer *l;
277 /* FIXME use last free of some kind */
279 /* we don't want to overwrite the superblock on the drive,
280 * so we make sure to start at an offset of at least 1MB
282 search_start = max((u64)1024 * 1024, search_start);
284 if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
285 search_start = max(root->fs_info->alloc_start, search_start);
287 key.objectid = device->devid;
288 key.offset = search_start;
289 key.type = BTRFS_DEV_EXTENT_KEY;
290 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
293 ret = btrfs_previous_item(root, path, 0, key.type);
297 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
300 slot = path->slots[0];
301 if (slot >= btrfs_header_nritems(l)) {
302 ret = btrfs_next_leaf(root, path);
309 if (search_start >= search_end) {
313 *start = search_start;
317 *start = last_byte > search_start ?
318 last_byte : search_start;
319 if (search_end <= *start) {
325 btrfs_item_key_to_cpu(l, &key, slot);
327 if (key.objectid < device->devid)
330 if (key.objectid > device->devid)
333 if (key.offset >= search_start && key.offset > last_byte &&
335 if (last_byte < search_start)
336 last_byte = search_start;
337 hole_size = key.offset - last_byte;
338 if (key.offset > last_byte &&
339 hole_size >= num_bytes) {
344 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
349 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
350 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
356 /* we have to make sure we didn't find an extent that has already
357 * been allocated by the map tree or the original allocation
359 btrfs_release_path(root, path);
360 BUG_ON(*start < search_start);
362 if (*start + num_bytes > search_end) {
366 /* check for pending inserts here */
370 btrfs_release_path(root, path);
374 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
375 struct btrfs_device *device,
376 u64 chunk_tree, u64 chunk_objectid,
378 u64 num_bytes, u64 *start)
381 struct btrfs_path *path;
382 struct btrfs_root *root = device->dev_root;
383 struct btrfs_dev_extent *extent;
384 struct extent_buffer *leaf;
385 struct btrfs_key key;
387 path = btrfs_alloc_path();
391 ret = find_free_dev_extent(trans, device, path, num_bytes, start);
396 key.objectid = device->devid;
398 key.type = BTRFS_DEV_EXTENT_KEY;
399 ret = btrfs_insert_empty_item(trans, root, path, &key,
403 leaf = path->nodes[0];
404 extent = btrfs_item_ptr(leaf, path->slots[0],
405 struct btrfs_dev_extent);
406 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
407 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
408 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
410 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
411 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
414 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
415 btrfs_mark_buffer_dirty(leaf);
417 btrfs_free_path(path);
421 static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset)
423 struct btrfs_path *path;
425 struct btrfs_key key;
426 struct btrfs_chunk *chunk;
427 struct btrfs_key found_key;
429 path = btrfs_alloc_path();
432 key.objectid = objectid;
433 key.offset = (u64)-1;
434 key.type = BTRFS_CHUNK_ITEM_KEY;
436 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
442 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
446 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
448 if (found_key.objectid != objectid)
451 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
453 *offset = found_key.offset +
454 btrfs_chunk_length(path->nodes[0], chunk);
459 btrfs_free_path(path);
463 static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
467 struct btrfs_key key;
468 struct btrfs_key found_key;
470 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
471 key.type = BTRFS_DEV_ITEM_KEY;
472 key.offset = (u64)-1;
474 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
480 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
485 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
487 *objectid = found_key.offset + 1;
491 btrfs_release_path(root, path);
496 * the device information is stored in the chunk root
497 * the btrfs_device struct should be fully filled in
499 int btrfs_add_device(struct btrfs_trans_handle *trans,
500 struct btrfs_root *root,
501 struct btrfs_device *device)
504 struct btrfs_path *path;
505 struct btrfs_dev_item *dev_item;
506 struct extent_buffer *leaf;
507 struct btrfs_key key;
511 root = root->fs_info->chunk_root;
513 path = btrfs_alloc_path();
517 ret = find_next_devid(root, path, &free_devid);
521 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
522 key.type = BTRFS_DEV_ITEM_KEY;
523 key.offset = free_devid;
525 ret = btrfs_insert_empty_item(trans, root, path, &key,
530 leaf = path->nodes[0];
531 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
533 device->devid = free_devid;
534 btrfs_set_device_id(leaf, dev_item, device->devid);
535 btrfs_set_device_generation(leaf, dev_item, 0);
536 btrfs_set_device_type(leaf, dev_item, device->type);
537 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
538 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
539 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
540 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
541 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
542 btrfs_set_device_group(leaf, dev_item, 0);
543 btrfs_set_device_seek_speed(leaf, dev_item, 0);
544 btrfs_set_device_bandwidth(leaf, dev_item, 0);
545 btrfs_set_device_start_offset(leaf, dev_item, 0);
547 ptr = (unsigned long)btrfs_device_uuid(dev_item);
548 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
549 ptr = (unsigned long)btrfs_device_fsid(dev_item);
550 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
551 btrfs_mark_buffer_dirty(leaf);
555 btrfs_free_path(path);
559 int btrfs_update_device(struct btrfs_trans_handle *trans,
560 struct btrfs_device *device)
563 struct btrfs_path *path;
564 struct btrfs_root *root;
565 struct btrfs_dev_item *dev_item;
566 struct extent_buffer *leaf;
567 struct btrfs_key key;
569 root = device->dev_root->fs_info->chunk_root;
571 path = btrfs_alloc_path();
575 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
576 key.type = BTRFS_DEV_ITEM_KEY;
577 key.offset = device->devid;
579 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
588 leaf = path->nodes[0];
589 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
591 btrfs_set_device_id(leaf, dev_item, device->devid);
592 btrfs_set_device_type(leaf, dev_item, device->type);
593 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
594 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
595 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
596 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
597 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
598 btrfs_mark_buffer_dirty(leaf);
601 btrfs_free_path(path);
605 int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
606 struct btrfs_root *root,
607 struct btrfs_key *key,
608 struct btrfs_chunk *chunk, int item_size)
610 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
611 struct btrfs_disk_key disk_key;
615 array_size = btrfs_super_sys_array_size(super_copy);
616 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
619 ptr = super_copy->sys_chunk_array + array_size;
620 btrfs_cpu_key_to_disk(&disk_key, key);
621 memcpy(ptr, &disk_key, sizeof(disk_key));
622 ptr += sizeof(disk_key);
623 memcpy(ptr, chunk, item_size);
624 item_size += sizeof(disk_key);
625 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
629 static u64 div_factor(u64 num, int factor)
637 static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes,
640 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
642 else if (type & BTRFS_BLOCK_GROUP_RAID10)
643 return calc_size * (num_stripes / sub_stripes);
644 else if (type & BTRFS_BLOCK_GROUP_RAID5)
645 return calc_size * (num_stripes - 1);
646 else if (type & BTRFS_BLOCK_GROUP_RAID6)
647 return calc_size * (num_stripes - 2);
649 return calc_size * num_stripes;
653 static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
655 /* TODO, add a way to store the preferred stripe size */
659 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
660 struct btrfs_root *extent_root, u64 *start,
661 u64 *num_bytes, u64 type)
664 struct btrfs_fs_info *info = extent_root->fs_info;
665 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
666 struct btrfs_stripe *stripes;
667 struct btrfs_device *device = NULL;
668 struct btrfs_chunk *chunk;
669 struct list_head private_devs;
670 struct list_head *dev_list = &extent_root->fs_info->fs_devices->devices;
671 struct list_head *cur;
672 struct map_lookup *map;
673 int min_stripe_size = 1 * 1024 * 1024;
674 u64 calc_size = 8 * 1024 * 1024;
676 u64 max_chunk_size = 4 * calc_size;
686 int stripe_len = 64 * 1024;
687 struct btrfs_key key;
690 if (list_empty(dev_list)) {
694 if (type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
695 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
696 BTRFS_BLOCK_GROUP_RAID10 |
697 BTRFS_BLOCK_GROUP_DUP)) {
698 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
699 calc_size = 8 * 1024 * 1024;
700 max_chunk_size = calc_size * 2;
701 min_stripe_size = 1 * 1024 * 1024;
702 } else if (type & BTRFS_BLOCK_GROUP_DATA) {
703 calc_size = 1024 * 1024 * 1024;
704 max_chunk_size = 10 * calc_size;
705 min_stripe_size = 64 * 1024 * 1024;
706 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
707 calc_size = 1024 * 1024 * 1024;
708 max_chunk_size = 4 * calc_size;
709 min_stripe_size = 32 * 1024 * 1024;
712 if (type & BTRFS_BLOCK_GROUP_RAID1) {
713 num_stripes = min_t(u64, 2,
714 btrfs_super_num_devices(info->super_copy));
719 if (type & BTRFS_BLOCK_GROUP_DUP) {
723 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
724 num_stripes = btrfs_super_num_devices(info->super_copy);
727 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
728 num_stripes = btrfs_super_num_devices(info->super_copy);
731 num_stripes &= ~(u32)1;
735 if (type & (BTRFS_BLOCK_GROUP_RAID5)) {
736 num_stripes = btrfs_super_num_devices(info->super_copy);
740 stripe_len = find_raid56_stripe_len(num_stripes - 1,
741 btrfs_super_stripesize(info->super_copy));
743 if (type & (BTRFS_BLOCK_GROUP_RAID6)) {
744 num_stripes = btrfs_super_num_devices(info->super_copy);
748 stripe_len = find_raid56_stripe_len(num_stripes - 2,
749 btrfs_super_stripesize(info->super_copy));
752 /* we don't want a chunk larger than 10% of the FS */
753 percent_max = div_factor(btrfs_super_total_bytes(info->super_copy), 1);
754 max_chunk_size = min(percent_max, max_chunk_size);
757 if (chunk_bytes_by_type(type, calc_size, num_stripes, sub_stripes) >
759 calc_size = max_chunk_size;
760 calc_size /= num_stripes;
761 calc_size /= stripe_len;
762 calc_size *= stripe_len;
764 /* we don't want tiny stripes */
765 calc_size = max_t(u64, calc_size, min_stripe_size);
767 calc_size /= stripe_len;
768 calc_size *= stripe_len;
769 INIT_LIST_HEAD(&private_devs);
770 cur = dev_list->next;
773 if (type & BTRFS_BLOCK_GROUP_DUP)
774 min_free = calc_size * 2;
776 min_free = calc_size;
778 /* build a private list of devices we will allocate from */
779 while(index < num_stripes) {
780 device = list_entry(cur, struct btrfs_device, dev_list);
781 avail = device->total_bytes - device->bytes_used;
783 if (avail >= min_free) {
784 list_move_tail(&device->dev_list, &private_devs);
786 if (type & BTRFS_BLOCK_GROUP_DUP)
788 } else if (avail > max_avail)
793 if (index < num_stripes) {
794 list_splice(&private_devs, dev_list);
795 if (index >= min_stripes) {
797 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
798 num_stripes /= sub_stripes;
799 num_stripes *= sub_stripes;
804 if (!looped && max_avail > 0) {
806 calc_size = max_avail;
811 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
815 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
816 key.type = BTRFS_CHUNK_ITEM_KEY;
819 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
823 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
829 stripes = &chunk->stripe;
830 *num_bytes = chunk_bytes_by_type(type, calc_size,
831 num_stripes, sub_stripes);
833 while(index < num_stripes) {
834 struct btrfs_stripe *stripe;
835 BUG_ON(list_empty(&private_devs));
836 cur = private_devs.next;
837 device = list_entry(cur, struct btrfs_device, dev_list);
839 /* loop over this device again if we're doing a dup group */
840 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
841 (index == num_stripes - 1))
842 list_move_tail(&device->dev_list, dev_list);
844 ret = btrfs_alloc_dev_extent(trans, device,
845 info->chunk_root->root_key.objectid,
846 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
847 calc_size, &dev_offset);
850 device->bytes_used += calc_size;
851 ret = btrfs_update_device(trans, device);
854 map->stripes[index].dev = device;
855 map->stripes[index].physical = dev_offset;
856 stripe = stripes + index;
857 btrfs_set_stack_stripe_devid(stripe, device->devid);
858 btrfs_set_stack_stripe_offset(stripe, dev_offset);
859 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
862 BUG_ON(!list_empty(&private_devs));
864 /* key was set above */
865 btrfs_set_stack_chunk_length(chunk, *num_bytes);
866 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
867 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
868 btrfs_set_stack_chunk_type(chunk, type);
869 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
870 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
871 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
872 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
873 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
874 map->sector_size = extent_root->sectorsize;
875 map->stripe_len = stripe_len;
876 map->io_align = stripe_len;
877 map->io_width = stripe_len;
879 map->num_stripes = num_stripes;
880 map->sub_stripes = sub_stripes;
882 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
883 btrfs_chunk_item_size(num_stripes));
885 *start = key.offset;;
887 map->ce.start = key.offset;
888 map->ce.size = *num_bytes;
890 ret = insert_existing_cache_extent(
891 &extent_root->fs_info->mapping_tree.cache_tree,
895 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
896 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
897 chunk, btrfs_chunk_item_size(num_stripes));
905 int btrfs_alloc_data_chunk(struct btrfs_trans_handle *trans,
906 struct btrfs_root *extent_root, u64 *start,
907 u64 num_bytes, u64 type)
910 struct btrfs_fs_info *info = extent_root->fs_info;
911 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
912 struct btrfs_stripe *stripes;
913 struct btrfs_device *device = NULL;
914 struct btrfs_chunk *chunk;
915 struct list_head *dev_list = &extent_root->fs_info->fs_devices->devices;
916 struct list_head *cur;
917 struct map_lookup *map;
918 u64 calc_size = 8 * 1024 * 1024;
923 int stripe_len = 64 * 1024;
924 struct btrfs_key key;
926 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
927 key.type = BTRFS_CHUNK_ITEM_KEY;
928 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
933 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
937 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
943 stripes = &chunk->stripe;
944 calc_size = num_bytes;
947 cur = dev_list->next;
948 device = list_entry(cur, struct btrfs_device, dev_list);
950 while (index < num_stripes) {
951 struct btrfs_stripe *stripe;
953 ret = btrfs_alloc_dev_extent(trans, device,
954 info->chunk_root->root_key.objectid,
955 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
956 calc_size, &dev_offset);
959 device->bytes_used += calc_size;
960 ret = btrfs_update_device(trans, device);
963 map->stripes[index].dev = device;
964 map->stripes[index].physical = dev_offset;
965 stripe = stripes + index;
966 btrfs_set_stack_stripe_devid(stripe, device->devid);
967 btrfs_set_stack_stripe_offset(stripe, dev_offset);
968 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
972 /* key was set above */
973 btrfs_set_stack_chunk_length(chunk, num_bytes);
974 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
975 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
976 btrfs_set_stack_chunk_type(chunk, type);
977 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
978 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
979 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
980 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
981 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
982 map->sector_size = extent_root->sectorsize;
983 map->stripe_len = stripe_len;
984 map->io_align = stripe_len;
985 map->io_width = stripe_len;
987 map->num_stripes = num_stripes;
988 map->sub_stripes = sub_stripes;
990 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
991 btrfs_chunk_item_size(num_stripes));
995 map->ce.start = key.offset;
996 map->ce.size = num_bytes;
998 ret = insert_existing_cache_extent(
999 &extent_root->fs_info->mapping_tree.cache_tree,
1007 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
1009 cache_tree_init(&tree->cache_tree);
1012 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
1014 struct cache_extent *ce;
1015 struct map_lookup *map;
1018 ce = find_first_cache_extent(&map_tree->cache_tree, logical);
1020 BUG_ON(ce->start > logical || ce->start + ce->size < logical);
1021 map = container_of(ce, struct map_lookup, ce);
1023 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
1024 ret = map->num_stripes;
1025 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1026 ret = map->sub_stripes;
1027 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
1029 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
1036 int btrfs_next_metadata(struct btrfs_mapping_tree *map_tree, u64 *logical,
1039 struct cache_extent *ce;
1040 struct map_lookup *map;
1042 ce = find_first_cache_extent(&map_tree->cache_tree, *logical);
1045 ce = next_cache_extent(ce);
1049 map = container_of(ce, struct map_lookup, ce);
1050 if (map->type & BTRFS_BLOCK_GROUP_METADATA) {
1051 *logical = ce->start;
1060 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
1061 u64 chunk_start, u64 physical, u64 devid,
1062 u64 **logical, int *naddrs, int *stripe_len)
1064 struct cache_extent *ce;
1065 struct map_lookup *map;
1073 ce = find_first_cache_extent(&map_tree->cache_tree, chunk_start);
1075 map = container_of(ce, struct map_lookup, ce);
1078 rmap_len = map->stripe_len;
1079 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1080 length = ce->size / (map->num_stripes / map->sub_stripes);
1081 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
1082 length = ce->size / map->num_stripes;
1083 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
1084 BTRFS_BLOCK_GROUP_RAID6)) {
1085 length = ce->size / nr_data_stripes(map);
1086 rmap_len = map->stripe_len * nr_data_stripes(map);
1089 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
1091 for (i = 0; i < map->num_stripes; i++) {
1092 if (devid && map->stripes[i].dev->devid != devid)
1094 if (map->stripes[i].physical > physical ||
1095 map->stripes[i].physical + length <= physical)
1098 stripe_nr = (physical - map->stripes[i].physical) /
1101 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1102 stripe_nr = (stripe_nr * map->num_stripes + i) /
1104 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
1105 stripe_nr = stripe_nr * map->num_stripes + i;
1106 } /* else if RAID[56], multiply by nr_data_stripes().
1107 * Alternatively, just use rmap_len below instead of
1108 * map->stripe_len */
1110 bytenr = ce->start + stripe_nr * rmap_len;
1111 for (j = 0; j < nr; j++) {
1112 if (buf[j] == bytenr)
1121 *stripe_len = rmap_len;
1126 static inline int parity_smaller(u64 a, u64 b)
1131 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
1132 static void sort_parity_stripes(struct btrfs_multi_bio *bbio, u64 *raid_map)
1134 struct btrfs_bio_stripe s;
1141 for (i = 0; i < bbio->num_stripes - 1; i++) {
1142 if (parity_smaller(raid_map[i], raid_map[i+1])) {
1143 s = bbio->stripes[i];
1145 bbio->stripes[i] = bbio->stripes[i+1];
1146 raid_map[i] = raid_map[i+1];
1147 bbio->stripes[i+1] = s;
1155 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1156 u64 logical, u64 *length,
1157 struct btrfs_multi_bio **multi_ret, int mirror_num,
1160 return __btrfs_map_block(map_tree, rw, logical, length, NULL,
1161 multi_ret, mirror_num, raid_map_ret);
1164 int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1165 u64 logical, u64 *length, u64 *type,
1166 struct btrfs_multi_bio **multi_ret, int mirror_num,
1169 struct cache_extent *ce;
1170 struct map_lookup *map;
1174 u64 *raid_map = NULL;
1175 int stripes_allocated = 8;
1176 int stripes_required = 1;
1179 struct btrfs_multi_bio *multi = NULL;
1181 if (multi_ret && rw == READ) {
1182 stripes_allocated = 1;
1185 ce = find_first_cache_extent(&map_tree->cache_tree, logical);
1191 if (ce->start > logical || ce->start + ce->size < logical) {
1198 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
1203 map = container_of(ce, struct map_lookup, ce);
1204 offset = logical - ce->start;
1207 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
1208 BTRFS_BLOCK_GROUP_DUP)) {
1209 stripes_required = map->num_stripes;
1210 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1211 stripes_required = map->sub_stripes;
1214 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)
1215 && multi_ret && ((rw & WRITE) || mirror_num > 1) && raid_map_ret) {
1216 /* RAID[56] write or recovery. Return all stripes */
1217 stripes_required = map->num_stripes;
1219 /* Only allocate the map if we've already got a large enough multi_ret */
1220 if (stripes_allocated >= stripes_required) {
1221 raid_map = kmalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
1229 /* if our multi bio struct is too small, back off and try again */
1230 if (multi_ret && stripes_allocated < stripes_required) {
1231 stripes_allocated = stripes_required;
1238 * stripe_nr counts the total number of stripes we have to stride
1239 * to get to this block
1241 stripe_nr = stripe_nr / map->stripe_len;
1243 stripe_offset = stripe_nr * map->stripe_len;
1244 BUG_ON(offset < stripe_offset);
1246 /* stripe_offset is the offset of this block in its stripe*/
1247 stripe_offset = offset - stripe_offset;
1249 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
1250 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
1251 BTRFS_BLOCK_GROUP_RAID10 |
1252 BTRFS_BLOCK_GROUP_DUP)) {
1253 /* we limit the length of each bio to what fits in a stripe */
1254 *length = min_t(u64, ce->size - offset,
1255 map->stripe_len - stripe_offset);
1257 *length = ce->size - offset;
1263 multi->num_stripes = 1;
1265 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
1267 multi->num_stripes = map->num_stripes;
1268 else if (mirror_num)
1269 stripe_index = mirror_num - 1;
1271 stripe_index = stripe_nr % map->num_stripes;
1272 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1273 int factor = map->num_stripes / map->sub_stripes;
1275 stripe_index = stripe_nr % factor;
1276 stripe_index *= map->sub_stripes;
1279 multi->num_stripes = map->sub_stripes;
1280 else if (mirror_num)
1281 stripe_index += mirror_num - 1;
1283 stripe_nr = stripe_nr / factor;
1284 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
1286 multi->num_stripes = map->num_stripes;
1287 else if (mirror_num)
1288 stripe_index = mirror_num - 1;
1289 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
1290 BTRFS_BLOCK_GROUP_RAID6)) {
1295 u64 raid56_full_stripe_start;
1296 u64 full_stripe_len = nr_data_stripes(map) * map->stripe_len;
1299 * align the start of our data stripe in the logical
1302 raid56_full_stripe_start = offset / full_stripe_len;
1303 raid56_full_stripe_start *= full_stripe_len;
1305 /* get the data stripe number */
1306 stripe_nr = raid56_full_stripe_start / map->stripe_len;
1307 stripe_nr = stripe_nr / nr_data_stripes(map);
1309 /* Work out the disk rotation on this stripe-set */
1310 rot = stripe_nr % map->num_stripes;
1312 /* Fill in the logical address of each stripe */
1313 tmp = stripe_nr * nr_data_stripes(map);
1315 for (i = 0; i < nr_data_stripes(map); i++)
1316 raid_map[(i+rot) % map->num_stripes] =
1317 ce->start + (tmp + i) * map->stripe_len;
1319 raid_map[(i+rot) % map->num_stripes] = BTRFS_RAID5_P_STRIPE;
1320 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
1321 raid_map[(i+rot+1) % map->num_stripes] = BTRFS_RAID6_Q_STRIPE;
1323 *length = map->stripe_len;
1326 multi->num_stripes = map->num_stripes;
1328 stripe_index = stripe_nr % nr_data_stripes(map);
1329 stripe_nr = stripe_nr / nr_data_stripes(map);
1332 * Mirror #0 or #1 means the original data block.
1333 * Mirror #2 is RAID5 parity block.
1334 * Mirror #3 is RAID6 Q block.
1337 stripe_index = nr_data_stripes(map) + mirror_num - 2;
1339 /* We distribute the parity blocks across stripes */
1340 stripe_index = (stripe_nr + stripe_index) % map->num_stripes;
1344 * after this do_div call, stripe_nr is the number of stripes
1345 * on this device we have to walk to find the data, and
1346 * stripe_index is the number of our device in the stripe array
1348 stripe_index = stripe_nr % map->num_stripes;
1349 stripe_nr = stripe_nr / map->num_stripes;
1351 BUG_ON(stripe_index >= map->num_stripes);
1353 for (i = 0; i < multi->num_stripes; i++) {
1354 multi->stripes[i].physical =
1355 map->stripes[stripe_index].physical + stripe_offset +
1356 stripe_nr * map->stripe_len;
1357 multi->stripes[i].dev = map->stripes[stripe_index].dev;
1366 sort_parity_stripes(multi, raid_map);
1367 *raid_map_ret = raid_map;
1373 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
1376 struct btrfs_device *device;
1377 struct btrfs_fs_devices *cur_devices;
1379 cur_devices = root->fs_info->fs_devices;
1380 while (cur_devices) {
1382 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
1383 device = __find_device(&cur_devices->devices,
1388 cur_devices = cur_devices->seed;
1393 struct btrfs_device *btrfs_find_device_by_devid(struct btrfs_root *root,
1394 u64 devid, int instance)
1396 struct list_head *head = &root->fs_info->fs_devices->devices;
1397 struct btrfs_device *dev;
1398 struct list_head *cur;
1401 list_for_each(cur, head) {
1402 dev = list_entry(cur, struct btrfs_device, dev_list);
1403 if (dev->devid == devid && num_found++ == instance)
1409 int btrfs_bootstrap_super_map(struct btrfs_mapping_tree *map_tree,
1410 struct btrfs_fs_devices *fs_devices)
1412 struct map_lookup *map;
1413 u64 logical = BTRFS_SUPER_INFO_OFFSET;
1414 u64 length = BTRFS_SUPER_INFO_SIZE;
1415 int num_stripes = 0;
1416 int sub_stripes = 0;
1419 struct list_head *cur;
1421 list_for_each(cur, &fs_devices->devices) {
1424 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1428 map->ce.start = logical;
1429 map->ce.size = length;
1430 map->num_stripes = num_stripes;
1431 map->sub_stripes = sub_stripes;
1432 map->io_width = length;
1433 map->io_align = length;
1434 map->sector_size = length;
1435 map->stripe_len = length;
1436 map->type = BTRFS_BLOCK_GROUP_RAID1;
1439 list_for_each(cur, &fs_devices->devices) {
1440 struct btrfs_device *device = list_entry(cur,
1441 struct btrfs_device,
1443 map->stripes[i].physical = logical;
1444 map->stripes[i].dev = device;
1447 ret = insert_existing_cache_extent(&map_tree->cache_tree, &map->ce);
1448 if (ret == -EEXIST) {
1449 struct cache_extent *old;
1450 struct map_lookup *old_map;
1451 old = find_cache_extent(&map_tree->cache_tree, logical, length);
1452 old_map = container_of(old, struct map_lookup, ce);
1453 remove_cache_extent(&map_tree->cache_tree, old);
1455 ret = insert_existing_cache_extent(&map_tree->cache_tree,
1462 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
1464 struct cache_extent *ce;
1465 struct map_lookup *map;
1466 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1470 ce = find_first_cache_extent(&map_tree->cache_tree, chunk_offset);
1473 map = container_of(ce, struct map_lookup, ce);
1474 for (i = 0; i < map->num_stripes; i++) {
1475 if (!map->stripes[i].dev->writeable) {
1484 static struct btrfs_device *fill_missing_device(u64 devid)
1486 struct btrfs_device *device;
1488 device = kzalloc(sizeof(*device), GFP_NOFS);
1489 device->devid = devid;
1494 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
1495 struct extent_buffer *leaf,
1496 struct btrfs_chunk *chunk)
1498 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1499 struct map_lookup *map;
1500 struct cache_extent *ce;
1504 u8 uuid[BTRFS_UUID_SIZE];
1509 logical = key->offset;
1510 length = btrfs_chunk_length(leaf, chunk);
1512 ce = find_first_cache_extent(&map_tree->cache_tree, logical);
1514 /* already mapped? */
1515 if (ce && ce->start <= logical && ce->start + ce->size > logical) {
1519 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1520 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1524 map->ce.start = logical;
1525 map->ce.size = length;
1526 map->num_stripes = num_stripes;
1527 map->io_width = btrfs_chunk_io_width(leaf, chunk);
1528 map->io_align = btrfs_chunk_io_align(leaf, chunk);
1529 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1530 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1531 map->type = btrfs_chunk_type(leaf, chunk);
1532 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
1534 for (i = 0; i < num_stripes; i++) {
1535 map->stripes[i].physical =
1536 btrfs_stripe_offset_nr(leaf, chunk, i);
1537 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
1538 read_extent_buffer(leaf, uuid, (unsigned long)
1539 btrfs_stripe_dev_uuid_nr(chunk, i),
1541 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
1543 if (!map->stripes[i].dev) {
1544 map->stripes[i].dev = fill_missing_device(devid);
1545 printf("warning, device %llu is missing\n",
1546 (unsigned long long)devid);
1550 ret = insert_existing_cache_extent(&map_tree->cache_tree, &map->ce);
1556 static int fill_device_from_item(struct extent_buffer *leaf,
1557 struct btrfs_dev_item *dev_item,
1558 struct btrfs_device *device)
1562 device->devid = btrfs_device_id(leaf, dev_item);
1563 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1564 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1565 device->type = btrfs_device_type(leaf, dev_item);
1566 device->io_align = btrfs_device_io_align(leaf, dev_item);
1567 device->io_width = btrfs_device_io_width(leaf, dev_item);
1568 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
1570 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1571 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1576 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
1578 struct btrfs_fs_devices *fs_devices;
1581 fs_devices = root->fs_info->fs_devices->seed;
1582 while (fs_devices) {
1583 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
1587 fs_devices = fs_devices->seed;
1590 fs_devices = find_fsid(fsid);
1596 ret = btrfs_open_devices(fs_devices, O_RDONLY);
1600 fs_devices->seed = root->fs_info->fs_devices->seed;
1601 root->fs_info->fs_devices->seed = fs_devices;
1606 static int read_one_dev(struct btrfs_root *root,
1607 struct extent_buffer *leaf,
1608 struct btrfs_dev_item *dev_item)
1610 struct btrfs_device *device;
1613 u8 fs_uuid[BTRFS_UUID_SIZE];
1614 u8 dev_uuid[BTRFS_UUID_SIZE];
1616 devid = btrfs_device_id(leaf, dev_item);
1617 read_extent_buffer(leaf, dev_uuid,
1618 (unsigned long)btrfs_device_uuid(dev_item),
1620 read_extent_buffer(leaf, fs_uuid,
1621 (unsigned long)btrfs_device_fsid(dev_item),
1624 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
1625 ret = open_seed_devices(root, fs_uuid);
1630 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1632 printk("warning devid %llu not found already\n",
1633 (unsigned long long)devid);
1634 device = kmalloc(sizeof(*device), GFP_NOFS);
1637 device->total_ios = 0;
1638 list_add(&device->dev_list,
1639 &root->fs_info->fs_devices->devices);
1642 fill_device_from_item(leaf, dev_item, device);
1643 device->dev_root = root->fs_info->dev_root;
1647 int btrfs_read_sys_array(struct btrfs_root *root)
1649 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
1650 struct extent_buffer *sb;
1651 struct btrfs_disk_key *disk_key;
1652 struct btrfs_chunk *chunk;
1653 struct btrfs_key key;
1658 unsigned long sb_ptr;
1662 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
1663 BTRFS_SUPER_INFO_SIZE);
1666 btrfs_set_buffer_uptodate(sb);
1667 write_extent_buffer(sb, super_copy, 0, sizeof(*super_copy));
1668 array_size = btrfs_super_sys_array_size(super_copy);
1671 * we do this loop twice, once for the device items and
1672 * once for all of the chunks. This way there are device
1673 * structs filled in for every chunk
1675 ptr = super_copy->sys_chunk_array;
1676 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
1679 while (cur < array_size) {
1680 disk_key = (struct btrfs_disk_key *)ptr;
1681 btrfs_disk_key_to_cpu(&key, disk_key);
1683 len = sizeof(*disk_key);
1688 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1689 chunk = (struct btrfs_chunk *)sb_ptr;
1690 ret = read_one_chunk(root, &key, sb, chunk);
1693 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
1694 len = btrfs_chunk_item_size(num_stripes);
1702 free_extent_buffer(sb);
1706 int btrfs_read_chunk_tree(struct btrfs_root *root)
1708 struct btrfs_path *path;
1709 struct extent_buffer *leaf;
1710 struct btrfs_key key;
1711 struct btrfs_key found_key;
1715 root = root->fs_info->chunk_root;
1717 path = btrfs_alloc_path();
1721 /* first we search for all of the device items, and then we
1722 * read in all of the chunk items. This way we can create chunk
1723 * mappings that reference all of the devices that are afound
1725 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1729 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1731 leaf = path->nodes[0];
1732 slot = path->slots[0];
1733 if (slot >= btrfs_header_nritems(leaf)) {
1734 ret = btrfs_next_leaf(root, path);
1741 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1742 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1743 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
1745 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
1746 struct btrfs_dev_item *dev_item;
1747 dev_item = btrfs_item_ptr(leaf, slot,
1748 struct btrfs_dev_item);
1749 ret = read_one_dev(root, leaf, dev_item);
1752 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
1753 struct btrfs_chunk *chunk;
1754 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1755 ret = read_one_chunk(root, &found_key, leaf, chunk);
1760 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
1762 btrfs_release_path(root, path);
1768 btrfs_free_path(path);
1772 struct list_head *btrfs_scanned_uuids(void)