2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
18 #include <linux/sched.h>
19 #include <linux/bio.h>
20 #include <linux/buffer_head.h>
21 #include <asm/div64.h>
23 #include "extent_map.h"
25 #include "transaction.h"
26 #include "print-tree.h"
30 struct btrfs_device
*dev
;
41 struct stripe stripes
[];
44 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
45 (sizeof(struct stripe) * (n)))
47 static DEFINE_MUTEX(uuid_mutex
);
48 static LIST_HEAD(fs_uuids
);
50 int btrfs_cleanup_fs_uuids(void)
52 struct btrfs_fs_devices
*fs_devices
;
53 struct list_head
*uuid_cur
;
54 struct list_head
*devices_cur
;
55 struct btrfs_device
*dev
;
57 list_for_each(uuid_cur
, &fs_uuids
) {
58 fs_devices
= list_entry(uuid_cur
, struct btrfs_fs_devices
,
60 while(!list_empty(&fs_devices
->devices
)) {
61 devices_cur
= fs_devices
->devices
.next
;
62 dev
= list_entry(devices_cur
, struct btrfs_device
,
64 printk("uuid cleanup finds %s\n", dev
->name
);
67 close_bdev_excl(dev
->bdev
);
69 list_del(&dev
->dev_list
);
76 static struct btrfs_device
*__find_device(struct list_head
*head
, u64 devid
)
78 struct btrfs_device
*dev
;
79 struct list_head
*cur
;
81 list_for_each(cur
, head
) {
82 dev
= list_entry(cur
, struct btrfs_device
, dev_list
);
83 if (dev
->devid
== devid
)
89 static struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
91 struct list_head
*cur
;
92 struct btrfs_fs_devices
*fs_devices
;
94 list_for_each(cur
, &fs_uuids
) {
95 fs_devices
= list_entry(cur
, struct btrfs_fs_devices
, list
);
96 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
102 static int device_list_add(const char *path
,
103 struct btrfs_super_block
*disk_super
,
104 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
106 struct btrfs_device
*device
;
107 struct btrfs_fs_devices
*fs_devices
;
108 u64 found_transid
= btrfs_super_generation(disk_super
);
110 fs_devices
= find_fsid(disk_super
->fsid
);
112 fs_devices
= kmalloc(sizeof(*fs_devices
), GFP_NOFS
);
115 INIT_LIST_HEAD(&fs_devices
->devices
);
116 list_add(&fs_devices
->list
, &fs_uuids
);
117 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
118 fs_devices
->latest_devid
= devid
;
119 fs_devices
->latest_trans
= found_transid
;
120 fs_devices
->lowest_devid
= (u64
)-1;
121 fs_devices
->num_devices
= 0;
124 device
= __find_device(&fs_devices
->devices
, devid
);
127 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
129 /* we can safely leave the fs_devices entry around */
132 device
->devid
= devid
;
133 device
->name
= kstrdup(path
, GFP_NOFS
);
138 list_add(&device
->dev_list
, &fs_devices
->devices
);
139 fs_devices
->num_devices
++;
142 if (found_transid
> fs_devices
->latest_trans
) {
143 fs_devices
->latest_devid
= devid
;
144 fs_devices
->latest_trans
= found_transid
;
146 if (fs_devices
->lowest_devid
> devid
) {
147 fs_devices
->lowest_devid
= devid
;
148 printk("lowest devid now %Lu\n", devid
);
150 *fs_devices_ret
= fs_devices
;
154 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
156 struct list_head
*head
= &fs_devices
->devices
;
157 struct list_head
*cur
;
158 struct btrfs_device
*device
;
160 mutex_lock(&uuid_mutex
);
161 list_for_each(cur
, head
) {
162 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
164 close_bdev_excl(device
->bdev
);
165 printk("close devices closes %s\n", device
->name
);
169 mutex_unlock(&uuid_mutex
);
173 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
174 int flags
, void *holder
)
176 struct block_device
*bdev
;
177 struct list_head
*head
= &fs_devices
->devices
;
178 struct list_head
*cur
;
179 struct btrfs_device
*device
;
182 mutex_lock(&uuid_mutex
);
183 list_for_each(cur
, head
) {
184 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
185 bdev
= open_bdev_excl(device
->name
, flags
, holder
);
186 printk("opening %s devid %Lu\n", device
->name
, device
->devid
);
188 printk("open %s failed\n", device
->name
);
192 if (device
->devid
== fs_devices
->latest_devid
)
193 fs_devices
->latest_bdev
= bdev
;
194 if (device
->devid
== fs_devices
->lowest_devid
) {
195 fs_devices
->lowest_bdev
= bdev
;
196 printk("lowest bdev %s\n", device
->name
);
200 mutex_unlock(&uuid_mutex
);
203 mutex_unlock(&uuid_mutex
);
204 btrfs_close_devices(fs_devices
);
208 int btrfs_scan_one_device(const char *path
, int flags
, void *holder
,
209 struct btrfs_fs_devices
**fs_devices_ret
)
211 struct btrfs_super_block
*disk_super
;
212 struct block_device
*bdev
;
213 struct buffer_head
*bh
;
217 mutex_lock(&uuid_mutex
);
219 printk("scan one opens %s\n", path
);
220 bdev
= open_bdev_excl(path
, flags
, holder
);
223 printk("open failed\n");
228 ret
= set_blocksize(bdev
, 4096);
231 bh
= __bread(bdev
, BTRFS_SUPER_INFO_OFFSET
/ 4096, 4096);
236 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
237 if (strncmp((char *)(&disk_super
->magic
), BTRFS_MAGIC
,
238 sizeof(disk_super
->magic
))) {
239 printk("no btrfs found on %s\n", path
);
243 devid
= le64_to_cpu(disk_super
->dev_item
.devid
);
244 printk("found device %Lu on %s\n", devid
, path
);
245 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
250 close_bdev_excl(bdev
);
251 printk("scan one closes bdev %s\n", path
);
253 mutex_unlock(&uuid_mutex
);
258 * this uses a pretty simple search, the expectation is that it is
259 * called very infrequently and that a given device has a small number
262 static int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
263 struct btrfs_device
*device
,
264 struct btrfs_path
*path
,
265 u64 num_bytes
, u64
*start
)
267 struct btrfs_key key
;
268 struct btrfs_root
*root
= device
->dev_root
;
269 struct btrfs_dev_extent
*dev_extent
= NULL
;
272 u64 search_start
= 0;
273 u64 search_end
= device
->total_bytes
;
277 struct extent_buffer
*l
;
282 /* FIXME use last free of some kind */
284 /* we don't want to overwrite the superblock on the drive,
285 * so we make sure to start at an offset of at least 1MB
287 search_start
= max((u64
)1024 * 1024, search_start
);
288 key
.objectid
= device
->devid
;
289 key
.offset
= search_start
;
290 key
.type
= BTRFS_DEV_EXTENT_KEY
;
291 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
294 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
298 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
301 slot
= path
->slots
[0];
302 if (slot
>= btrfs_header_nritems(l
)) {
303 ret
= btrfs_next_leaf(root
, path
);
310 if (search_start
>= search_end
) {
314 *start
= search_start
;
318 *start
= last_byte
> search_start
?
319 last_byte
: search_start
;
320 if (search_end
<= *start
) {
326 btrfs_item_key_to_cpu(l
, &key
, slot
);
328 if (key
.objectid
< device
->devid
)
331 if (key
.objectid
> device
->devid
)
334 if (key
.offset
>= search_start
&& key
.offset
> last_byte
&&
336 if (last_byte
< search_start
)
337 last_byte
= search_start
;
338 hole_size
= key
.offset
- last_byte
;
339 if (key
.offset
> last_byte
&&
340 hole_size
>= num_bytes
) {
345 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
) {
350 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
351 last_byte
= key
.offset
+ btrfs_dev_extent_length(l
, dev_extent
);
357 /* we have to make sure we didn't find an extent that has already
358 * been allocated by the map tree or the original allocation
360 btrfs_release_path(root
, path
);
361 BUG_ON(*start
< search_start
);
363 if (*start
+ num_bytes
> search_end
) {
367 /* check for pending inserts here */
371 btrfs_release_path(root
, path
);
375 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
376 struct btrfs_device
*device
,
377 u64 owner
, u64 num_bytes
, u64
*start
)
380 struct btrfs_path
*path
;
381 struct btrfs_root
*root
= device
->dev_root
;
382 struct btrfs_dev_extent
*extent
;
383 struct extent_buffer
*leaf
;
384 struct btrfs_key key
;
386 path
= btrfs_alloc_path();
390 ret
= find_free_dev_extent(trans
, device
, path
, num_bytes
, start
);
395 key
.objectid
= device
->devid
;
397 key
.type
= BTRFS_DEV_EXTENT_KEY
;
398 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
402 leaf
= path
->nodes
[0];
403 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
404 struct btrfs_dev_extent
);
405 btrfs_set_dev_extent_owner(leaf
, extent
, owner
);
406 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
407 btrfs_mark_buffer_dirty(leaf
);
409 btrfs_free_path(path
);
413 static int find_next_chunk(struct btrfs_root
*root
, u64
*objectid
)
415 struct btrfs_path
*path
;
417 struct btrfs_key key
;
418 struct btrfs_key found_key
;
420 path
= btrfs_alloc_path();
423 key
.objectid
= (u64
)-1;
424 key
.offset
= (u64
)-1;
425 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
427 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
433 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
437 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
439 *objectid
= found_key
.objectid
+ found_key
.offset
;
443 btrfs_free_path(path
);
447 static int find_next_devid(struct btrfs_root
*root
, struct btrfs_path
*path
,
451 struct btrfs_key key
;
452 struct btrfs_key found_key
;
454 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
455 key
.type
= BTRFS_DEV_ITEM_KEY
;
456 key
.offset
= (u64
)-1;
458 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
464 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
469 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
471 *objectid
= found_key
.offset
+ 1;
475 btrfs_release_path(root
, path
);
480 * the device information is stored in the chunk root
481 * the btrfs_device struct should be fully filled in
483 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
484 struct btrfs_root
*root
,
485 struct btrfs_device
*device
)
488 struct btrfs_path
*path
;
489 struct btrfs_dev_item
*dev_item
;
490 struct extent_buffer
*leaf
;
491 struct btrfs_key key
;
495 root
= root
->fs_info
->chunk_root
;
497 path
= btrfs_alloc_path();
501 ret
= find_next_devid(root
, path
, &free_devid
);
505 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
506 key
.type
= BTRFS_DEV_ITEM_KEY
;
507 key
.offset
= free_devid
;
509 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
514 leaf
= path
->nodes
[0];
515 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
517 device
->devid
= free_devid
;
518 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
519 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
520 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
521 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
522 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
523 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
524 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
526 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
527 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_DEV_UUID_SIZE
);
528 btrfs_mark_buffer_dirty(leaf
);
532 btrfs_free_path(path
);
535 int btrfs_update_device(struct btrfs_trans_handle
*trans
,
536 struct btrfs_device
*device
)
539 struct btrfs_path
*path
;
540 struct btrfs_root
*root
;
541 struct btrfs_dev_item
*dev_item
;
542 struct extent_buffer
*leaf
;
543 struct btrfs_key key
;
545 root
= device
->dev_root
->fs_info
->chunk_root
;
547 path
= btrfs_alloc_path();
551 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
552 key
.type
= BTRFS_DEV_ITEM_KEY
;
553 key
.offset
= device
->devid
;
555 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
564 leaf
= path
->nodes
[0];
565 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
567 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
568 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
569 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
570 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
571 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
572 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
573 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
574 btrfs_mark_buffer_dirty(leaf
);
577 btrfs_free_path(path
);
581 int btrfs_add_system_chunk(struct btrfs_trans_handle
*trans
,
582 struct btrfs_root
*root
,
583 struct btrfs_key
*key
,
584 struct btrfs_chunk
*chunk
, int item_size
)
586 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
587 struct btrfs_disk_key disk_key
;
591 array_size
= btrfs_super_sys_array_size(super_copy
);
592 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
595 ptr
= super_copy
->sys_chunk_array
+ array_size
;
596 btrfs_cpu_key_to_disk(&disk_key
, key
);
597 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
598 ptr
+= sizeof(disk_key
);
599 memcpy(ptr
, chunk
, item_size
);
600 item_size
+= sizeof(disk_key
);
601 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
605 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
606 struct btrfs_root
*extent_root
, u64
*start
,
607 u64
*num_bytes
, u64 type
)
610 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
611 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
612 struct btrfs_stripe
*stripes
;
613 struct btrfs_device
*device
= NULL
;
614 struct btrfs_chunk
*chunk
;
615 struct list_head private_devs
;
616 struct list_head
*dev_list
= &extent_root
->fs_info
->fs_devices
->devices
;
617 struct list_head
*cur
;
618 struct extent_map_tree
*em_tree
;
619 struct map_lookup
*map
;
620 struct extent_map
*em
;
622 u64 calc_size
= 1024 * 1024 * 1024;
629 int stripe_len
= 64 * 1024;
630 struct btrfs_key key
;
632 if (list_empty(dev_list
))
635 if (type
& BTRFS_BLOCK_GROUP_RAID0
)
636 num_stripes
= btrfs_super_num_devices(&info
->super_copy
);
637 if (type
& BTRFS_BLOCK_GROUP_DATA
)
638 stripe_len
= 64 * 1024;
639 if (type
& (BTRFS_BLOCK_GROUP_METADATA
| BTRFS_BLOCK_GROUP_SYSTEM
))
640 stripe_len
= 32 * 1024;
642 INIT_LIST_HEAD(&private_devs
);
643 cur
= dev_list
->next
;
645 /* build a private list of devices we will allocate from */
646 while(index
< num_stripes
) {
647 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
648 avail
= device
->total_bytes
- device
->bytes_used
;
650 if (avail
> max_avail
)
652 if (avail
>= calc_size
) {
653 list_move_tail(&device
->dev_list
, &private_devs
);
659 if (index
< num_stripes
) {
660 list_splice(&private_devs
, dev_list
);
661 if (!looped
&& max_avail
> 0) {
663 calc_size
= max_avail
;
669 ret
= find_next_chunk(chunk_root
, &key
.objectid
);
673 chunk
= kmalloc(btrfs_chunk_item_size(num_stripes
), GFP_NOFS
);
677 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
683 stripes
= &chunk
->stripe
;
685 *num_bytes
= calc_size
* num_stripes
;
687 while(index
< num_stripes
) {
688 BUG_ON(list_empty(&private_devs
));
689 cur
= private_devs
.next
;
690 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
691 list_move_tail(&device
->dev_list
, dev_list
);
693 ret
= btrfs_alloc_dev_extent(trans
, device
,
695 calc_size
, &dev_offset
);
697 printk("alloc chunk size %Lu from dev %Lu\n", calc_size
, device
->devid
);
698 device
->bytes_used
+= calc_size
;
699 ret
= btrfs_update_device(trans
, device
);
702 map
->stripes
[index
].dev
= device
;
703 map
->stripes
[index
].physical
= dev_offset
;
704 btrfs_set_stack_stripe_devid(stripes
+ index
, device
->devid
);
705 btrfs_set_stack_stripe_offset(stripes
+ index
, dev_offset
);
706 physical
= dev_offset
;
709 BUG_ON(!list_empty(&private_devs
));
711 /* key.objectid was set above */
712 key
.offset
= *num_bytes
;
713 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
714 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
715 btrfs_set_stack_chunk_stripe_len(chunk
, stripe_len
);
716 btrfs_set_stack_chunk_type(chunk
, type
);
717 btrfs_set_stack_chunk_num_stripes(chunk
, num_stripes
);
718 btrfs_set_stack_chunk_io_align(chunk
, stripe_len
);
719 btrfs_set_stack_chunk_io_width(chunk
, stripe_len
);
720 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
721 map
->sector_size
= extent_root
->sectorsize
;
722 map
->stripe_len
= stripe_len
;
723 map
->io_align
= stripe_len
;
724 map
->io_width
= stripe_len
;
726 map
->num_stripes
= num_stripes
;
728 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
,
729 btrfs_chunk_item_size(num_stripes
));
731 *start
= key
.objectid
;
733 em
= alloc_extent_map(GFP_NOFS
);
736 em
->bdev
= (struct block_device
*)map
;
737 em
->start
= key
.objectid
;
738 em
->len
= key
.offset
;
743 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
744 spin_lock(&em_tree
->lock
);
745 ret
= add_extent_mapping(em_tree
, em
);
747 spin_unlock(&em_tree
->lock
);
752 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
754 extent_map_tree_init(&tree
->map_tree
, GFP_NOFS
);
757 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
759 struct extent_map
*em
;
762 spin_lock(&tree
->map_tree
.lock
);
763 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
765 remove_extent_mapping(&tree
->map_tree
, em
);
766 spin_unlock(&tree
->map_tree
.lock
);
772 /* once for the tree */
777 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
,
778 u64 logical
, u64
*phys
, u64
*length
,
779 struct btrfs_device
**dev
)
781 struct extent_map
*em
;
782 struct map_lookup
*map
;
783 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
790 spin_lock(&em_tree
->lock
);
791 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
794 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
795 map
= (struct map_lookup
*)em
->bdev
;
796 offset
= logical
- em
->start
;
800 * stripe_nr counts the total number of stripes we have to stride
801 * to get to this block
803 do_div(stripe_nr
, map
->stripe_len
);
805 stripe_offset
= stripe_nr
* map
->stripe_len
;
806 BUG_ON(offset
< stripe_offset
);
808 /* stripe_offset is the offset of this block in its stripe*/
809 stripe_offset
= offset
- stripe_offset
;
812 * after this do_div call, stripe_nr is the number of stripes
813 * on this device we have to walk to find the data, and
814 * stripe_index is the number of our device in the stripe array
816 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
818 BUG_ON(stripe_index
>= map
->num_stripes
);
820 *phys
= map
->stripes
[stripe_index
].physical
+ stripe_offset
+
821 stripe_nr
* map
->stripe_len
;
823 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
824 /* we limit the length of each bio to what fits in a stripe */
825 *length
= min_t(u64
, em
->len
- offset
,
826 map
->stripe_len
- stripe_offset
);
828 *length
= em
->len
- offset
;
830 *dev
= map
->stripes
[stripe_index
].dev
;
832 spin_unlock(&em_tree
->lock
);
836 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
)
838 struct btrfs_mapping_tree
*map_tree
;
839 struct btrfs_device
*dev
;
840 u64 logical
= bio
->bi_sector
<< 9;
844 struct bio_vec
*bvec
;
848 bio_for_each_segment(bvec
, bio
, i
) {
849 length
+= bvec
->bv_len
;
851 map_tree
= &root
->fs_info
->mapping_tree
;
853 ret
= btrfs_map_block(map_tree
, logical
, &physical
, &map_length
, &dev
);
854 if (map_length
< length
) {
855 printk("mapping failed logical %Lu bio len %Lu physical %Lu "
856 "len %Lu\n", logical
, length
, physical
, map_length
);
859 BUG_ON(map_length
< length
);
860 bio
->bi_sector
= physical
>> 9;
861 bio
->bi_bdev
= dev
->bdev
;
866 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
)
868 struct list_head
*head
= &root
->fs_info
->fs_devices
->devices
;
870 return __find_device(head
, devid
);
873 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
874 struct extent_buffer
*leaf
,
875 struct btrfs_chunk
*chunk
)
877 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
878 struct map_lookup
*map
;
879 struct extent_map
*em
;
887 logical
= key
->objectid
;
888 length
= key
->offset
;
889 spin_lock(&map_tree
->map_tree
.lock
);
890 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
892 /* already mapped? */
893 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
895 spin_unlock(&map_tree
->map_tree
.lock
);
900 spin_unlock(&map_tree
->map_tree
.lock
);
902 map
= kzalloc(sizeof(*map
), GFP_NOFS
);
906 em
= alloc_extent_map(GFP_NOFS
);
909 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
910 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
916 em
->bdev
= (struct block_device
*)map
;
921 map
->num_stripes
= num_stripes
;
922 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
923 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
924 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
925 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
926 map
->type
= btrfs_chunk_type(leaf
, chunk
);
927 for (i
= 0; i
< num_stripes
; i
++) {
928 map
->stripes
[i
].physical
=
929 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
930 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
931 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
);
932 if (!map
->stripes
[i
].dev
) {
939 spin_lock(&map_tree
->map_tree
.lock
);
940 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
942 spin_unlock(&map_tree
->map_tree
.lock
);
948 static int fill_device_from_item(struct extent_buffer
*leaf
,
949 struct btrfs_dev_item
*dev_item
,
950 struct btrfs_device
*device
)
954 device
->devid
= btrfs_device_id(leaf
, dev_item
);
955 device
->total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
956 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
957 device
->type
= btrfs_device_type(leaf
, dev_item
);
958 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
959 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
960 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
962 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
963 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_DEV_UUID_SIZE
);
968 static int read_one_dev(struct btrfs_root
*root
,
969 struct extent_buffer
*leaf
,
970 struct btrfs_dev_item
*dev_item
)
972 struct btrfs_device
*device
;
976 devid
= btrfs_device_id(leaf
, dev_item
);
977 device
= btrfs_find_device(root
, devid
);
979 printk("warning devid %Lu not found already\n", devid
);
980 device
= kmalloc(sizeof(*device
), GFP_NOFS
);
983 list_add(&device
->dev_list
,
984 &root
->fs_info
->fs_devices
->devices
);
987 fill_device_from_item(leaf
, dev_item
, device
);
988 device
->dev_root
= root
->fs_info
->dev_root
;
991 ret
= btrfs_open_device(device
);
999 int btrfs_read_super_device(struct btrfs_root
*root
, struct extent_buffer
*buf
)
1001 struct btrfs_dev_item
*dev_item
;
1003 dev_item
= (struct btrfs_dev_item
*)offsetof(struct btrfs_super_block
,
1005 return read_one_dev(root
, buf
, dev_item
);
1008 int btrfs_read_sys_array(struct btrfs_root
*root
)
1010 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1011 struct extent_buffer
*sb
= root
->fs_info
->sb_buffer
;
1012 struct btrfs_disk_key
*disk_key
;
1013 struct btrfs_chunk
*chunk
;
1014 struct btrfs_key key
;
1019 unsigned long sb_ptr
;
1023 array_size
= btrfs_super_sys_array_size(super_copy
);
1026 * we do this loop twice, once for the device items and
1027 * once for all of the chunks. This way there are device
1028 * structs filled in for every chunk
1030 ptr
= super_copy
->sys_chunk_array
;
1031 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
1034 while (cur
< array_size
) {
1035 disk_key
= (struct btrfs_disk_key
*)ptr
;
1036 btrfs_disk_key_to_cpu(&key
, disk_key
);
1038 len
= sizeof(*disk_key
);
1043 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1044 chunk
= (struct btrfs_chunk
*)sb_ptr
;
1045 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
1047 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
1048 len
= btrfs_chunk_item_size(num_stripes
);
1059 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
1061 struct btrfs_path
*path
;
1062 struct extent_buffer
*leaf
;
1063 struct btrfs_key key
;
1064 struct btrfs_key found_key
;
1068 root
= root
->fs_info
->chunk_root
;
1070 path
= btrfs_alloc_path();
1074 /* first we search for all of the device items, and then we
1075 * read in all of the chunk items. This way we can create chunk
1076 * mappings that reference all of the devices that are afound
1078 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1082 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1084 leaf
= path
->nodes
[0];
1085 slot
= path
->slots
[0];
1086 if (slot
>= btrfs_header_nritems(leaf
)) {
1087 ret
= btrfs_next_leaf(root
, path
);
1094 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1095 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
1096 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
1098 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
1099 struct btrfs_dev_item
*dev_item
;
1100 dev_item
= btrfs_item_ptr(leaf
, slot
,
1101 struct btrfs_dev_item
);
1102 ret
= read_one_dev(root
, leaf
, dev_item
);
1105 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1106 struct btrfs_chunk
*chunk
;
1107 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
1108 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
1112 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
1114 btrfs_release_path(root
, path
);
1118 btrfs_free_path(path
);