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
;
39 struct cache_extent ce
;
46 struct stripe stripes
[];
49 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
50 (sizeof(struct stripe) * (n)))
52 static LIST_HEAD(fs_uuids
);
54 static struct btrfs_device
*__find_device(struct list_head
*head
, u64 devid
)
56 struct btrfs_device
*dev
;
57 struct list_head
*cur
;
59 list_for_each(cur
, head
) {
60 dev
= list_entry(cur
, struct btrfs_device
, dev_list
);
61 if (dev
->devid
== devid
)
67 static struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
69 struct list_head
*cur
;
70 struct btrfs_fs_devices
*fs_devices
;
72 list_for_each(cur
, &fs_uuids
) {
73 fs_devices
= list_entry(cur
, struct btrfs_fs_devices
, list
);
74 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
80 static int device_list_add(const char *path
,
81 struct btrfs_super_block
*disk_super
,
82 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
84 struct btrfs_device
*device
;
85 struct btrfs_fs_devices
*fs_devices
;
86 u64 found_transid
= btrfs_super_generation(disk_super
);
88 fs_devices
= find_fsid(disk_super
->fsid
);
90 fs_devices
= kmalloc(sizeof(*fs_devices
), GFP_NOFS
);
93 INIT_LIST_HEAD(&fs_devices
->devices
);
94 list_add(&fs_devices
->list
, &fs_uuids
);
95 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
96 fs_devices
->latest_devid
= devid
;
97 fs_devices
->latest_trans
= found_transid
;
98 fs_devices
->lowest_devid
= (u64
)-1;
101 device
= __find_device(&fs_devices
->devices
, devid
);
104 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
106 /* we can safely leave the fs_devices entry around */
109 device
->devid
= devid
;
110 device
->name
= kstrdup(path
, GFP_NOFS
);
115 list_add(&device
->dev_list
, &fs_devices
->devices
);
118 if (found_transid
> fs_devices
->latest_trans
) {
119 fs_devices
->latest_devid
= devid
;
120 fs_devices
->latest_trans
= found_transid
;
122 if (fs_devices
->lowest_devid
> devid
) {
123 fs_devices
->lowest_devid
= devid
;
124 printk("lowest devid now %llu\n", (unsigned long long)devid
);
126 *fs_devices_ret
= fs_devices
;
130 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
132 struct list_head
*head
= &fs_devices
->devices
;
133 struct list_head
*cur
;
134 struct btrfs_device
*device
;
136 list_for_each(cur
, head
) {
137 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
143 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
, int flags
)
146 struct list_head
*head
= &fs_devices
->devices
;
147 struct list_head
*cur
;
148 struct btrfs_device
*device
;
151 list_for_each(cur
, head
) {
152 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
153 fd
= open(device
->name
, flags
);
154 printk("opening %s devid %llu fd %d\n", device
->name
,
155 (unsigned long long)device
->devid
, fd
);
160 if (device
->devid
== fs_devices
->latest_devid
)
161 fs_devices
->latest_bdev
= fd
;
162 if (device
->devid
== fs_devices
->lowest_devid
)
163 fs_devices
->lowest_bdev
= fd
;
168 btrfs_close_devices(fs_devices
);
172 int btrfs_scan_one_device(int fd
, const char *path
,
173 struct btrfs_fs_devices
**fs_devices_ret
,
174 u64
*total_devs
, u64 super_offset
)
176 struct btrfs_super_block
*disk_super
;
186 ret
= pread(fd
, buf
, 4096, super_offset
);
191 disk_super
= (struct btrfs_super_block
*)buf
;
192 if (strncmp((char *)(&disk_super
->magic
), BTRFS_MAGIC
,
193 sizeof(disk_super
->magic
))) {
197 devid
= le64_to_cpu(disk_super
->dev_item
.devid
);
198 *total_devs
= btrfs_super_num_devices(disk_super
);
199 printk("found device %llu on %s\n", (unsigned long long)devid
, path
);
200 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
209 * this uses a pretty simple search, the expectation is that it is
210 * called very infrequently and that a given device has a small number
213 static int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
214 struct btrfs_device
*device
,
215 struct btrfs_path
*path
,
216 u64 num_bytes
, u64
*start
)
218 struct btrfs_key key
;
219 struct btrfs_root
*root
= device
->dev_root
;
220 struct btrfs_dev_extent
*dev_extent
= NULL
;
223 u64 search_start
= 0;
224 u64 search_end
= device
->total_bytes
;
228 struct extent_buffer
*l
;
233 /* FIXME use last free of some kind */
235 /* we don't want to overwrite the superblock on the drive,
236 * so we make sure to start at an offset of at least 1MB
238 search_start
= max((u64
)1024 * 1024, search_start
);
239 key
.objectid
= device
->devid
;
240 key
.offset
= search_start
;
241 key
.type
= BTRFS_DEV_EXTENT_KEY
;
242 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
245 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
249 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
252 slot
= path
->slots
[0];
253 if (slot
>= btrfs_header_nritems(l
)) {
254 ret
= btrfs_next_leaf(root
, path
);
261 if (search_start
>= search_end
) {
265 *start
= search_start
;
269 *start
= last_byte
> search_start
?
270 last_byte
: search_start
;
271 if (search_end
<= *start
) {
277 btrfs_item_key_to_cpu(l
, &key
, slot
);
279 if (key
.objectid
< device
->devid
)
282 if (key
.objectid
> device
->devid
)
285 if (key
.offset
>= search_start
&& key
.offset
> last_byte
&&
287 if (last_byte
< search_start
)
288 last_byte
= search_start
;
289 hole_size
= key
.offset
- last_byte
;
290 if (key
.offset
> last_byte
&&
291 hole_size
>= num_bytes
) {
296 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
) {
301 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
302 last_byte
= key
.offset
+ btrfs_dev_extent_length(l
, dev_extent
);
308 /* we have to make sure we didn't find an extent that has already
309 * been allocated by the map tree or the original allocation
311 btrfs_release_path(root
, path
);
312 BUG_ON(*start
< search_start
);
314 if (*start
+ num_bytes
> search_end
) {
318 /* check for pending inserts here */
322 btrfs_release_path(root
, path
);
326 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
327 struct btrfs_device
*device
,
328 u64 owner
, u64 num_bytes
, u64
*start
)
331 struct btrfs_path
*path
;
332 struct btrfs_root
*root
= device
->dev_root
;
333 struct btrfs_dev_extent
*extent
;
334 struct extent_buffer
*leaf
;
335 struct btrfs_key key
;
337 path
= btrfs_alloc_path();
341 ret
= find_free_dev_extent(trans
, device
, path
, num_bytes
, start
);
346 key
.objectid
= device
->devid
;
348 key
.type
= BTRFS_DEV_EXTENT_KEY
;
349 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
353 leaf
= path
->nodes
[0];
354 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
355 struct btrfs_dev_extent
);
356 btrfs_set_dev_extent_owner(leaf
, extent
, owner
);
357 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
358 btrfs_mark_buffer_dirty(leaf
);
360 btrfs_free_path(path
);
364 static int find_next_chunk(struct btrfs_root
*root
, u64
*objectid
)
366 struct btrfs_path
*path
;
368 struct btrfs_key key
;
369 struct btrfs_key found_key
;
371 path
= btrfs_alloc_path();
374 key
.objectid
= (u64
)-1;
375 key
.offset
= (u64
)-1;
376 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
378 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
384 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
388 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
390 *objectid
= found_key
.objectid
+ found_key
.offset
;
394 btrfs_free_path(path
);
398 static int find_next_devid(struct btrfs_root
*root
, struct btrfs_path
*path
,
402 struct btrfs_key key
;
403 struct btrfs_key found_key
;
405 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
406 key
.type
= BTRFS_DEV_ITEM_KEY
;
407 key
.offset
= (u64
)-1;
409 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
415 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
420 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
422 *objectid
= found_key
.offset
+ 1;
426 btrfs_release_path(root
, path
);
431 * the device information is stored in the chunk root
432 * the btrfs_device struct should be fully filled in
434 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
435 struct btrfs_root
*root
,
436 struct btrfs_device
*device
)
439 struct btrfs_path
*path
;
440 struct btrfs_dev_item
*dev_item
;
441 struct extent_buffer
*leaf
;
442 struct btrfs_key key
;
446 root
= root
->fs_info
->chunk_root
;
448 path
= btrfs_alloc_path();
452 ret
= find_next_devid(root
, path
, &free_devid
);
456 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
457 key
.type
= BTRFS_DEV_ITEM_KEY
;
458 key
.offset
= free_devid
;
460 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
465 leaf
= path
->nodes
[0];
466 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
468 device
->devid
= free_devid
;
469 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
470 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
471 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
472 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
473 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
474 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
475 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
477 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
478 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_DEV_UUID_SIZE
);
479 btrfs_mark_buffer_dirty(leaf
);
483 btrfs_free_path(path
);
487 int btrfs_update_device(struct btrfs_trans_handle
*trans
,
488 struct btrfs_device
*device
)
491 struct btrfs_path
*path
;
492 struct btrfs_root
*root
;
493 struct btrfs_dev_item
*dev_item
;
494 struct extent_buffer
*leaf
;
495 struct btrfs_key key
;
497 root
= device
->dev_root
->fs_info
->chunk_root
;
499 path
= btrfs_alloc_path();
503 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
504 key
.type
= BTRFS_DEV_ITEM_KEY
;
505 key
.offset
= device
->devid
;
507 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
516 leaf
= path
->nodes
[0];
517 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
519 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
520 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
521 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
522 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
523 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
524 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
525 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
526 btrfs_mark_buffer_dirty(leaf
);
529 btrfs_free_path(path
);
533 int btrfs_add_system_chunk(struct btrfs_trans_handle
*trans
,
534 struct btrfs_root
*root
,
535 struct btrfs_key
*key
,
536 struct btrfs_chunk
*chunk
, int item_size
)
538 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
539 struct btrfs_disk_key disk_key
;
543 array_size
= btrfs_super_sys_array_size(super_copy
);
544 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
547 ptr
= super_copy
->sys_chunk_array
+ array_size
;
548 btrfs_cpu_key_to_disk(&disk_key
, key
);
549 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
550 ptr
+= sizeof(disk_key
);
551 memcpy(ptr
, chunk
, item_size
);
552 item_size
+= sizeof(disk_key
);
553 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
557 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
558 struct btrfs_root
*extent_root
, u64
*start
,
559 u64
*num_bytes
, u64 type
)
562 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
563 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
564 struct btrfs_stripe
*stripes
;
565 struct btrfs_device
*device
= NULL
;
566 struct btrfs_chunk
*chunk
;
567 struct list_head private_devs
;
568 struct list_head
*dev_list
= &extent_root
->fs_info
->fs_devices
->devices
;
569 struct list_head
*cur
;
570 struct map_lookup
*map
;
572 u64 calc_size
= 8 * 1024 * 1024;
579 int stripe_len
= 64 * 1024;
580 struct btrfs_key key
;
582 if (list_empty(dev_list
))
585 if (type
& (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
)) {
586 calc_size
= 1024 * 1024 * 1024;
588 if (type
& (BTRFS_BLOCK_GROUP_RAID1
)) {
589 num_stripes
= min_t(u64
, 2,
590 btrfs_super_num_devices(&info
->super_copy
));
592 if (type
& (BTRFS_BLOCK_GROUP_RAID0
))
593 num_stripes
= btrfs_super_num_devices(&info
->super_copy
);
595 INIT_LIST_HEAD(&private_devs
);
596 cur
= dev_list
->next
;
598 /* build a private list of devices we will allocate from */
599 while(index
< num_stripes
) {
600 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
601 avail
= device
->total_bytes
- device
->bytes_used
;
603 if (avail
> max_avail
)
605 if (avail
>= calc_size
) {
606 list_move_tail(&device
->dev_list
, &private_devs
);
612 if (index
< num_stripes
) {
613 list_splice(&private_devs
, dev_list
);
614 if (!looped
&& max_avail
> 0) {
616 calc_size
= max_avail
;
622 ret
= find_next_chunk(chunk_root
, &key
.objectid
);
626 chunk
= kmalloc(btrfs_chunk_item_size(num_stripes
), GFP_NOFS
);
630 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
636 stripes
= &chunk
->stripe
;
638 if (type
& BTRFS_BLOCK_GROUP_RAID1
)
639 *num_bytes
= calc_size
;
641 *num_bytes
= calc_size
* num_stripes
;
644 while(index
< num_stripes
) {
645 BUG_ON(list_empty(&private_devs
));
646 cur
= private_devs
.next
;
647 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
648 list_move_tail(&device
->dev_list
, dev_list
);
650 ret
= btrfs_alloc_dev_extent(trans
, device
,
652 calc_size
, &dev_offset
);
654 printk("alloc chunk size %llu from dev %llu\n",
655 (unsigned long long)calc_size
,
656 (unsigned long long)device
->devid
);
657 device
->bytes_used
+= calc_size
;
658 ret
= btrfs_update_device(trans
, device
);
661 map
->stripes
[index
].dev
= device
;
662 map
->stripes
[index
].physical
= dev_offset
;
663 btrfs_set_stack_stripe_devid(stripes
+ index
, device
->devid
);
664 btrfs_set_stack_stripe_offset(stripes
+ index
, dev_offset
);
665 physical
= dev_offset
;
668 BUG_ON(!list_empty(&private_devs
));
670 /* key.objectid was set above */
671 key
.offset
= *num_bytes
;
672 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
673 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
674 btrfs_set_stack_chunk_stripe_len(chunk
, stripe_len
);
675 btrfs_set_stack_chunk_type(chunk
, type
);
676 btrfs_set_stack_chunk_num_stripes(chunk
, num_stripes
);
677 btrfs_set_stack_chunk_io_align(chunk
, stripe_len
);
678 btrfs_set_stack_chunk_io_width(chunk
, stripe_len
);
679 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
680 map
->sector_size
= extent_root
->sectorsize
;
681 map
->stripe_len
= stripe_len
;
682 map
->io_align
= stripe_len
;
683 map
->io_width
= stripe_len
;
685 map
->num_stripes
= num_stripes
;
687 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
,
688 btrfs_chunk_item_size(num_stripes
));
690 *start
= key
.objectid
;
692 map
->ce
.start
= key
.objectid
;
693 map
->ce
.size
= key
.offset
;
695 ret
= insert_existing_cache_extent(
696 &extent_root
->fs_info
->mapping_tree
.cache_tree
,
704 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
706 cache_tree_init(&tree
->cache_tree
);
709 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
710 int dev_nr
, u64 logical
, u64
*phys
, u64
*length
,
711 struct btrfs_device
**dev
, int *total_devs
)
713 struct cache_extent
*ce
;
714 struct map_lookup
*map
;
720 ce
= find_first_cache_extent(&map_tree
->cache_tree
, logical
);
722 BUG_ON(ce
->start
> logical
|| ce
->start
+ ce
->size
< logical
);
723 map
= container_of(ce
, struct map_lookup
, ce
);
724 offset
= logical
- ce
->start
;
728 * stripe_nr counts the total number of stripes we have to stride
729 * to get to this block
731 stripe_nr
= stripe_nr
/ map
->stripe_len
;
733 stripe_offset
= stripe_nr
* map
->stripe_len
;
734 BUG_ON(offset
< stripe_offset
);
736 /* stripe_offset is the offset of this block in its stripe*/
737 stripe_offset
= offset
- stripe_offset
;
739 if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
740 stripe_index
= dev_nr
;
742 *total_devs
= map
->num_stripes
;
744 stripe_index
= stripe_nr
% map
->num_stripes
;
749 * after this do_div call, stripe_nr is the number of stripes
750 * on this device we have to walk to find the data, and
751 * stripe_index is the number of our device in the stripe array
753 stripe_index
= stripe_nr
% map
->num_stripes
;
754 stripe_nr
= stripe_nr
/ map
->num_stripes
;
756 BUG_ON(stripe_index
>= map
->num_stripes
);
757 *phys
= map
->stripes
[stripe_index
].physical
+ stripe_offset
+
758 stripe_nr
* map
->stripe_len
;
760 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
)) {
761 /* we limit the length of each bio to what fits in a stripe */
762 *length
= min_t(u64
, ce
->size
- offset
,
763 map
->stripe_len
- stripe_offset
);
765 *length
= ce
->size
- offset
;
767 *dev
= map
->stripes
[stripe_index
].dev
;
771 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
)
773 struct list_head
*head
= &root
->fs_info
->fs_devices
->devices
;
775 return __find_device(head
, devid
);
778 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
779 struct extent_buffer
*leaf
,
780 struct btrfs_chunk
*chunk
)
782 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
783 struct map_lookup
*map
;
784 struct cache_extent
*ce
;
792 logical
= key
->objectid
;
793 length
= key
->offset
;
794 ce
= find_first_cache_extent(&map_tree
->cache_tree
, logical
);
796 /* already mapped? */
797 if (ce
&& ce
->start
<= logical
&& ce
->start
+ ce
->size
> logical
) {
801 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
802 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
806 map
->ce
.start
= logical
;
807 map
->ce
.size
= length
;
809 map
->num_stripes
= num_stripes
;
810 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
811 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
812 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
813 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
814 map
->type
= btrfs_chunk_type(leaf
, chunk
);
815 for (i
= 0; i
< num_stripes
; i
++) {
816 map
->stripes
[i
].physical
=
817 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
818 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
819 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
);
820 if (!map
->stripes
[i
].dev
) {
826 ret
= insert_existing_cache_extent(&map_tree
->cache_tree
, &map
->ce
);
832 static int fill_device_from_item(struct extent_buffer
*leaf
,
833 struct btrfs_dev_item
*dev_item
,
834 struct btrfs_device
*device
)
838 device
->devid
= btrfs_device_id(leaf
, dev_item
);
839 device
->total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
840 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
841 device
->type
= btrfs_device_type(leaf
, dev_item
);
842 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
843 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
844 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
846 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
847 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_DEV_UUID_SIZE
);
852 static int read_one_dev(struct btrfs_root
*root
,
853 struct extent_buffer
*leaf
,
854 struct btrfs_dev_item
*dev_item
)
856 struct btrfs_device
*device
;
860 devid
= btrfs_device_id(leaf
, dev_item
);
861 device
= btrfs_find_device(root
, devid
);
863 printk("warning devid %llu not found already\n",
864 (unsigned long long)devid
);
865 device
= kmalloc(sizeof(*device
), GFP_NOFS
);
868 device
->total_ios
= 0;
869 list_add(&device
->dev_list
,
870 &root
->fs_info
->fs_devices
->devices
);
873 fill_device_from_item(leaf
, dev_item
, device
);
874 device
->dev_root
= root
->fs_info
->dev_root
;
878 int btrfs_read_super_device(struct btrfs_root
*root
, struct extent_buffer
*buf
)
880 struct btrfs_dev_item
*dev_item
;
882 dev_item
= (struct btrfs_dev_item
*)offsetof(struct btrfs_super_block
,
884 return read_one_dev(root
, buf
, dev_item
);
887 int btrfs_read_sys_array(struct btrfs_root
*root
)
889 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
890 struct extent_buffer
*sb
= root
->fs_info
->sb_buffer
;
891 struct btrfs_disk_key
*disk_key
;
892 struct btrfs_chunk
*chunk
;
893 struct btrfs_key key
;
898 unsigned long sb_ptr
;
902 array_size
= btrfs_super_sys_array_size(super_copy
);
905 * we do this loop twice, once for the device items and
906 * once for all of the chunks. This way there are device
907 * structs filled in for every chunk
909 ptr
= super_copy
->sys_chunk_array
;
910 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
913 while (cur
< array_size
) {
914 disk_key
= (struct btrfs_disk_key
*)ptr
;
915 btrfs_disk_key_to_cpu(&key
, disk_key
);
917 len
= sizeof(*disk_key
);
922 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
923 chunk
= (struct btrfs_chunk
*)sb_ptr
;
924 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
926 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
927 len
= btrfs_chunk_item_size(num_stripes
);
938 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
940 struct btrfs_path
*path
;
941 struct extent_buffer
*leaf
;
942 struct btrfs_key key
;
943 struct btrfs_key found_key
;
947 root
= root
->fs_info
->chunk_root
;
949 path
= btrfs_alloc_path();
953 /* first we search for all of the device items, and then we
954 * read in all of the chunk items. This way we can create chunk
955 * mappings that reference all of the devices that are afound
957 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
961 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
963 leaf
= path
->nodes
[0];
964 slot
= path
->slots
[0];
965 if (slot
>= btrfs_header_nritems(leaf
)) {
966 ret
= btrfs_next_leaf(root
, path
);
973 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
974 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
975 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
977 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
978 struct btrfs_dev_item
*dev_item
;
979 dev_item
= btrfs_item_ptr(leaf
, slot
,
980 struct btrfs_dev_item
);
981 ret
= read_one_dev(root
, leaf
, dev_item
);
984 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
985 struct btrfs_chunk
*chunk
;
986 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
987 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
991 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
993 btrfs_release_path(root
, path
);
997 btrfs_free_path(path
);