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
)
963 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
964 bio_endio(bio
, bio
->bi_size
, err
);
971 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
976 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
979 struct btrfs_mapping_tree
*map_tree
;
980 struct btrfs_device
*dev
;
981 struct bio
*first_bio
= bio
;
982 u64 logical
= bio
->bi_sector
<< 9;
985 struct bio_vec
*bvec
;
986 struct btrfs_multi_bio
*multi
= NULL
;
992 bio_for_each_segment(bvec
, bio
, i
) {
993 length
+= bvec
->bv_len
;
996 map_tree
= &root
->fs_info
->mapping_tree
;
999 ret
= btrfs_map_block(map_tree
, rw
, logical
, &map_length
, &multi
,
1003 total_devs
= multi
->num_stripes
;
1004 if (map_length
< length
) {
1005 printk("mapping failed logical %Lu bio len %Lu "
1006 "len %Lu\n", logical
, length
, map_length
);
1009 multi
->end_io
= first_bio
->bi_end_io
;
1010 multi
->private = first_bio
->bi_private
;
1011 atomic_set(&multi
->stripes_pending
, multi
->num_stripes
);
1013 while(dev_nr
< total_devs
) {
1014 if (total_devs
> 1) {
1015 if (dev_nr
< total_devs
- 1) {
1016 bio
= bio_clone(first_bio
, GFP_NOFS
);
1021 bio
->bi_private
= multi
;
1022 bio
->bi_end_io
= end_bio_multi_stripe
;
1024 bio
->bi_sector
= multi
->stripes
[dev_nr
].physical
>> 9;
1025 dev
= multi
->stripes
[dev_nr
].dev
;
1026 bio
->bi_bdev
= dev
->bdev
;
1027 spin_lock(&dev
->io_lock
);
1029 spin_unlock(&dev
->io_lock
);
1030 submit_bio(rw
, bio
);
1033 if (total_devs
== 1)
1038 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
)
1040 struct list_head
*head
= &root
->fs_info
->fs_devices
->devices
;
1042 return __find_device(head
, devid
);
1045 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
1046 struct extent_buffer
*leaf
,
1047 struct btrfs_chunk
*chunk
)
1049 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
1050 struct map_lookup
*map
;
1051 struct extent_map
*em
;
1059 logical
= key
->objectid
;
1060 length
= key
->offset
;
1061 spin_lock(&map_tree
->map_tree
.lock
);
1062 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
1064 /* already mapped? */
1065 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
1066 free_extent_map(em
);
1067 spin_unlock(&map_tree
->map_tree
.lock
);
1070 free_extent_map(em
);
1072 spin_unlock(&map_tree
->map_tree
.lock
);
1074 map
= kzalloc(sizeof(*map
), GFP_NOFS
);
1078 em
= alloc_extent_map(GFP_NOFS
);
1081 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
1082 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
1084 free_extent_map(em
);
1088 em
->bdev
= (struct block_device
*)map
;
1089 em
->start
= logical
;
1091 em
->block_start
= 0;
1093 map
->num_stripes
= num_stripes
;
1094 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
1095 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
1096 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
1097 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
1098 map
->type
= btrfs_chunk_type(leaf
, chunk
);
1099 for (i
= 0; i
< num_stripes
; i
++) {
1100 map
->stripes
[i
].physical
=
1101 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
1102 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
1103 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
);
1104 if (!map
->stripes
[i
].dev
) {
1106 free_extent_map(em
);
1111 spin_lock(&map_tree
->map_tree
.lock
);
1112 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
1114 spin_unlock(&map_tree
->map_tree
.lock
);
1115 free_extent_map(em
);
1120 static int fill_device_from_item(struct extent_buffer
*leaf
,
1121 struct btrfs_dev_item
*dev_item
,
1122 struct btrfs_device
*device
)
1126 device
->devid
= btrfs_device_id(leaf
, dev_item
);
1127 device
->total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
1128 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
1129 device
->type
= btrfs_device_type(leaf
, dev_item
);
1130 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
1131 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
1132 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
1134 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1135 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_DEV_UUID_SIZE
);
1140 static int read_one_dev(struct btrfs_root
*root
,
1141 struct extent_buffer
*leaf
,
1142 struct btrfs_dev_item
*dev_item
)
1144 struct btrfs_device
*device
;
1148 devid
= btrfs_device_id(leaf
, dev_item
);
1149 device
= btrfs_find_device(root
, devid
);
1151 printk("warning devid %Lu not found already\n", devid
);
1152 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1155 list_add(&device
->dev_list
,
1156 &root
->fs_info
->fs_devices
->devices
);
1157 device
->total_ios
= 0;
1158 spin_lock_init(&device
->io_lock
);
1161 fill_device_from_item(leaf
, dev_item
, device
);
1162 device
->dev_root
= root
->fs_info
->dev_root
;
1165 ret
= btrfs_open_device(device
);
1173 int btrfs_read_super_device(struct btrfs_root
*root
, struct extent_buffer
*buf
)
1175 struct btrfs_dev_item
*dev_item
;
1177 dev_item
= (struct btrfs_dev_item
*)offsetof(struct btrfs_super_block
,
1179 return read_one_dev(root
, buf
, dev_item
);
1182 int btrfs_read_sys_array(struct btrfs_root
*root
)
1184 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1185 struct extent_buffer
*sb
= root
->fs_info
->sb_buffer
;
1186 struct btrfs_disk_key
*disk_key
;
1187 struct btrfs_chunk
*chunk
;
1188 struct btrfs_key key
;
1193 unsigned long sb_ptr
;
1197 array_size
= btrfs_super_sys_array_size(super_copy
);
1200 * we do this loop twice, once for the device items and
1201 * once for all of the chunks. This way there are device
1202 * structs filled in for every chunk
1204 ptr
= super_copy
->sys_chunk_array
;
1205 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
1208 while (cur
< array_size
) {
1209 disk_key
= (struct btrfs_disk_key
*)ptr
;
1210 btrfs_disk_key_to_cpu(&key
, disk_key
);
1212 len
= sizeof(*disk_key
);
1217 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1218 chunk
= (struct btrfs_chunk
*)sb_ptr
;
1219 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
1221 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
1222 len
= btrfs_chunk_item_size(num_stripes
);
1233 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
1235 struct btrfs_path
*path
;
1236 struct extent_buffer
*leaf
;
1237 struct btrfs_key key
;
1238 struct btrfs_key found_key
;
1242 root
= root
->fs_info
->chunk_root
;
1244 path
= btrfs_alloc_path();
1248 /* first we search for all of the device items, and then we
1249 * read in all of the chunk items. This way we can create chunk
1250 * mappings that reference all of the devices that are afound
1252 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1256 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1258 leaf
= path
->nodes
[0];
1259 slot
= path
->slots
[0];
1260 if (slot
>= btrfs_header_nritems(leaf
)) {
1261 ret
= btrfs_next_leaf(root
, path
);
1268 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1269 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
1270 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
1272 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
1273 struct btrfs_dev_item
*dev_item
;
1274 dev_item
= btrfs_item_ptr(leaf
, slot
,
1275 struct btrfs_dev_item
);
1276 ret
= read_one_dev(root
, leaf
, dev_item
);
1279 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1280 struct btrfs_chunk
*chunk
;
1281 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
1282 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
1286 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
1288 btrfs_release_path(root
, path
);
1292 btrfs_free_path(path
);