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 spin_lock_init(&device
->io_lock
);
130 device
->name
= kstrdup(path
, GFP_NOFS
);
135 list_add(&device
->dev_list
, &fs_devices
->devices
);
136 fs_devices
->num_devices
++;
139 if (found_transid
> fs_devices
->latest_trans
) {
140 fs_devices
->latest_devid
= devid
;
141 fs_devices
->latest_trans
= found_transid
;
143 if (fs_devices
->lowest_devid
> devid
) {
144 fs_devices
->lowest_devid
= devid
;
145 printk("lowest devid now %Lu\n", devid
);
147 *fs_devices_ret
= fs_devices
;
151 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
153 struct list_head
*head
= &fs_devices
->devices
;
154 struct list_head
*cur
;
155 struct btrfs_device
*device
;
157 mutex_lock(&uuid_mutex
);
158 list_for_each(cur
, head
) {
159 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
161 close_bdev_excl(device
->bdev
);
162 printk("close devices closes %s\n", device
->name
);
166 mutex_unlock(&uuid_mutex
);
170 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
171 int flags
, void *holder
)
173 struct block_device
*bdev
;
174 struct list_head
*head
= &fs_devices
->devices
;
175 struct list_head
*cur
;
176 struct btrfs_device
*device
;
179 mutex_lock(&uuid_mutex
);
180 list_for_each(cur
, head
) {
181 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
182 bdev
= open_bdev_excl(device
->name
, flags
, holder
);
183 printk("opening %s devid %Lu\n", device
->name
, device
->devid
);
185 printk("open %s failed\n", device
->name
);
189 if (device
->devid
== fs_devices
->latest_devid
)
190 fs_devices
->latest_bdev
= bdev
;
191 if (device
->devid
== fs_devices
->lowest_devid
) {
192 fs_devices
->lowest_bdev
= bdev
;
193 printk("lowest bdev %s\n", device
->name
);
197 mutex_unlock(&uuid_mutex
);
200 mutex_unlock(&uuid_mutex
);
201 btrfs_close_devices(fs_devices
);
205 int btrfs_scan_one_device(const char *path
, int flags
, void *holder
,
206 struct btrfs_fs_devices
**fs_devices_ret
)
208 struct btrfs_super_block
*disk_super
;
209 struct block_device
*bdev
;
210 struct buffer_head
*bh
;
215 mutex_lock(&uuid_mutex
);
217 printk("scan one opens %s\n", path
);
218 bdev
= open_bdev_excl(path
, flags
, holder
);
221 printk("open failed\n");
226 ret
= set_blocksize(bdev
, 4096);
229 bh
= __bread(bdev
, BTRFS_SUPER_INFO_OFFSET
/ 4096, 4096);
234 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
235 if (strncmp((char *)(&disk_super
->magic
), BTRFS_MAGIC
,
236 sizeof(disk_super
->magic
))) {
237 printk("no btrfs found on %s\n", path
);
241 devid
= le64_to_cpu(disk_super
->dev_item
.devid
);
242 transid
= btrfs_super_generation(disk_super
);
243 printk("found device %Lu transid %Lu on %s\n", devid
, transid
, path
);
244 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
249 close_bdev_excl(bdev
);
251 mutex_unlock(&uuid_mutex
);
256 * this uses a pretty simple search, the expectation is that it is
257 * called very infrequently and that a given device has a small number
260 static int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
261 struct btrfs_device
*device
,
262 struct btrfs_path
*path
,
263 u64 num_bytes
, u64
*start
)
265 struct btrfs_key key
;
266 struct btrfs_root
*root
= device
->dev_root
;
267 struct btrfs_dev_extent
*dev_extent
= NULL
;
270 u64 search_start
= 0;
271 u64 search_end
= device
->total_bytes
;
275 struct extent_buffer
*l
;
280 /* FIXME use last free of some kind */
282 /* we don't want to overwrite the superblock on the drive,
283 * so we make sure to start at an offset of at least 1MB
285 search_start
= max((u64
)1024 * 1024, search_start
);
286 key
.objectid
= device
->devid
;
287 key
.offset
= search_start
;
288 key
.type
= BTRFS_DEV_EXTENT_KEY
;
289 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
292 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
296 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
299 slot
= path
->slots
[0];
300 if (slot
>= btrfs_header_nritems(l
)) {
301 ret
= btrfs_next_leaf(root
, path
);
308 if (search_start
>= search_end
) {
312 *start
= search_start
;
316 *start
= last_byte
> search_start
?
317 last_byte
: search_start
;
318 if (search_end
<= *start
) {
324 btrfs_item_key_to_cpu(l
, &key
, slot
);
326 if (key
.objectid
< device
->devid
)
329 if (key
.objectid
> device
->devid
)
332 if (key
.offset
>= search_start
&& key
.offset
> last_byte
&&
334 if (last_byte
< search_start
)
335 last_byte
= search_start
;
336 hole_size
= key
.offset
- last_byte
;
337 if (key
.offset
> last_byte
&&
338 hole_size
>= num_bytes
) {
343 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
) {
348 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
349 last_byte
= key
.offset
+ btrfs_dev_extent_length(l
, dev_extent
);
355 /* we have to make sure we didn't find an extent that has already
356 * been allocated by the map tree or the original allocation
358 btrfs_release_path(root
, path
);
359 BUG_ON(*start
< search_start
);
361 if (*start
+ num_bytes
> search_end
) {
365 /* check for pending inserts here */
369 btrfs_release_path(root
, path
);
373 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
374 struct btrfs_device
*device
,
375 u64 owner
, u64 num_bytes
, u64
*start
)
378 struct btrfs_path
*path
;
379 struct btrfs_root
*root
= device
->dev_root
;
380 struct btrfs_dev_extent
*extent
;
381 struct extent_buffer
*leaf
;
382 struct btrfs_key key
;
384 path
= btrfs_alloc_path();
388 ret
= find_free_dev_extent(trans
, device
, path
, num_bytes
, start
);
393 key
.objectid
= device
->devid
;
395 key
.type
= BTRFS_DEV_EXTENT_KEY
;
396 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
400 leaf
= path
->nodes
[0];
401 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
402 struct btrfs_dev_extent
);
403 btrfs_set_dev_extent_owner(leaf
, extent
, owner
);
404 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
405 btrfs_mark_buffer_dirty(leaf
);
407 btrfs_free_path(path
);
411 static int find_next_chunk(struct btrfs_root
*root
, u64
*objectid
)
413 struct btrfs_path
*path
;
415 struct btrfs_key key
;
416 struct btrfs_key found_key
;
418 path
= btrfs_alloc_path();
421 key
.objectid
= (u64
)-1;
422 key
.offset
= (u64
)-1;
423 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
425 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
431 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
435 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
437 *objectid
= found_key
.objectid
+ found_key
.offset
;
441 btrfs_free_path(path
);
445 static int find_next_devid(struct btrfs_root
*root
, struct btrfs_path
*path
,
449 struct btrfs_key key
;
450 struct btrfs_key found_key
;
452 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
453 key
.type
= BTRFS_DEV_ITEM_KEY
;
454 key
.offset
= (u64
)-1;
456 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
462 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
467 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
469 *objectid
= found_key
.offset
+ 1;
473 btrfs_release_path(root
, path
);
478 * the device information is stored in the chunk root
479 * the btrfs_device struct should be fully filled in
481 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
482 struct btrfs_root
*root
,
483 struct btrfs_device
*device
)
486 struct btrfs_path
*path
;
487 struct btrfs_dev_item
*dev_item
;
488 struct extent_buffer
*leaf
;
489 struct btrfs_key key
;
493 root
= root
->fs_info
->chunk_root
;
495 path
= btrfs_alloc_path();
499 ret
= find_next_devid(root
, path
, &free_devid
);
503 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
504 key
.type
= BTRFS_DEV_ITEM_KEY
;
505 key
.offset
= free_devid
;
507 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
512 leaf
= path
->nodes
[0];
513 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
515 device
->devid
= free_devid
;
516 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
517 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
518 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
519 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
520 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
521 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
522 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
524 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
525 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_DEV_UUID_SIZE
);
526 btrfs_mark_buffer_dirty(leaf
);
530 btrfs_free_path(path
);
533 int btrfs_update_device(struct btrfs_trans_handle
*trans
,
534 struct btrfs_device
*device
)
537 struct btrfs_path
*path
;
538 struct btrfs_root
*root
;
539 struct btrfs_dev_item
*dev_item
;
540 struct extent_buffer
*leaf
;
541 struct btrfs_key key
;
543 root
= device
->dev_root
->fs_info
->chunk_root
;
545 path
= btrfs_alloc_path();
549 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
550 key
.type
= BTRFS_DEV_ITEM_KEY
;
551 key
.offset
= device
->devid
;
553 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
562 leaf
= path
->nodes
[0];
563 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
565 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
566 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
567 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
568 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
569 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
570 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
571 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
572 btrfs_mark_buffer_dirty(leaf
);
575 btrfs_free_path(path
);
579 int btrfs_add_system_chunk(struct btrfs_trans_handle
*trans
,
580 struct btrfs_root
*root
,
581 struct btrfs_key
*key
,
582 struct btrfs_chunk
*chunk
, int item_size
)
584 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
585 struct btrfs_disk_key disk_key
;
589 array_size
= btrfs_super_sys_array_size(super_copy
);
590 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
593 ptr
= super_copy
->sys_chunk_array
+ array_size
;
594 btrfs_cpu_key_to_disk(&disk_key
, key
);
595 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
596 ptr
+= sizeof(disk_key
);
597 memcpy(ptr
, chunk
, item_size
);
598 item_size
+= sizeof(disk_key
);
599 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
603 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
604 struct btrfs_root
*extent_root
, u64
*start
,
605 u64
*num_bytes
, u64 type
)
608 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
609 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
610 struct btrfs_stripe
*stripes
;
611 struct btrfs_device
*device
= NULL
;
612 struct btrfs_chunk
*chunk
;
613 struct list_head private_devs
;
614 struct list_head
*dev_list
= &extent_root
->fs_info
->fs_devices
->devices
;
615 struct list_head
*cur
;
616 struct extent_map_tree
*em_tree
;
617 struct map_lookup
*map
;
618 struct extent_map
*em
;
620 u64 calc_size
= 1024 * 1024 * 1024;
621 u64 min_free
= calc_size
;
628 int stripe_len
= 64 * 1024;
629 struct btrfs_key key
;
631 if (list_empty(dev_list
))
634 if (type
& (BTRFS_BLOCK_GROUP_RAID0
))
635 num_stripes
= btrfs_super_num_devices(&info
->super_copy
);
636 if (type
& (BTRFS_BLOCK_GROUP_DUP
))
638 if (type
& (BTRFS_BLOCK_GROUP_RAID1
)) {
639 num_stripes
= min_t(u64
, 2,
640 btrfs_super_num_devices(&info
->super_copy
));
643 INIT_LIST_HEAD(&private_devs
);
644 cur
= dev_list
->next
;
647 if (type
& BTRFS_BLOCK_GROUP_DUP
)
648 min_free
= calc_size
* 2;
650 /* build a private list of devices we will allocate from */
651 while(index
< num_stripes
) {
652 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
654 avail
= device
->total_bytes
- device
->bytes_used
;
656 if (avail
> max_avail
)
658 if (avail
>= min_free
) {
659 list_move_tail(&device
->dev_list
, &private_devs
);
661 if (type
& BTRFS_BLOCK_GROUP_DUP
)
667 if (index
< num_stripes
) {
668 list_splice(&private_devs
, dev_list
);
669 if (!looped
&& max_avail
> 0) {
671 calc_size
= max_avail
;
677 ret
= find_next_chunk(chunk_root
, &key
.objectid
);
681 chunk
= kmalloc(btrfs_chunk_item_size(num_stripes
), GFP_NOFS
);
685 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
691 stripes
= &chunk
->stripe
;
693 if (type
& (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_DUP
))
694 *num_bytes
= calc_size
;
696 *num_bytes
= calc_size
* num_stripes
;
699 printk("new chunk type %Lu start %Lu size %Lu\n", type
, key
.objectid
, *num_bytes
);
700 while(index
< num_stripes
) {
701 BUG_ON(list_empty(&private_devs
));
702 cur
= private_devs
.next
;
703 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
705 /* loop over this device again if we're doing a dup group */
706 if (!(type
& BTRFS_BLOCK_GROUP_DUP
) ||
707 (index
== num_stripes
- 1))
708 list_move_tail(&device
->dev_list
, dev_list
);
710 ret
= btrfs_alloc_dev_extent(trans
, device
,
712 calc_size
, &dev_offset
);
714 printk("alloc chunk start %Lu size %Lu from dev %Lu type %Lu\n", key
.objectid
, calc_size
, device
->devid
, type
);
715 device
->bytes_used
+= calc_size
;
716 ret
= btrfs_update_device(trans
, device
);
719 map
->stripes
[index
].dev
= device
;
720 map
->stripes
[index
].physical
= dev_offset
;
721 btrfs_set_stack_stripe_devid(stripes
+ index
, device
->devid
);
722 btrfs_set_stack_stripe_offset(stripes
+ index
, dev_offset
);
723 physical
= dev_offset
;
726 BUG_ON(!list_empty(&private_devs
));
728 /* key.objectid was set above */
729 key
.offset
= *num_bytes
;
730 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
731 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
732 btrfs_set_stack_chunk_stripe_len(chunk
, stripe_len
);
733 btrfs_set_stack_chunk_type(chunk
, type
);
734 btrfs_set_stack_chunk_num_stripes(chunk
, num_stripes
);
735 btrfs_set_stack_chunk_io_align(chunk
, stripe_len
);
736 btrfs_set_stack_chunk_io_width(chunk
, stripe_len
);
737 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
738 map
->sector_size
= extent_root
->sectorsize
;
739 map
->stripe_len
= stripe_len
;
740 map
->io_align
= stripe_len
;
741 map
->io_width
= stripe_len
;
743 map
->num_stripes
= num_stripes
;
745 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
,
746 btrfs_chunk_item_size(num_stripes
));
748 *start
= key
.objectid
;
750 em
= alloc_extent_map(GFP_NOFS
);
753 em
->bdev
= (struct block_device
*)map
;
754 em
->start
= key
.objectid
;
755 em
->len
= key
.offset
;
760 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
761 spin_lock(&em_tree
->lock
);
762 ret
= add_extent_mapping(em_tree
, em
);
763 spin_unlock(&em_tree
->lock
);
769 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
771 extent_map_tree_init(&tree
->map_tree
, GFP_NOFS
);
774 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
776 struct extent_map
*em
;
779 spin_lock(&tree
->map_tree
.lock
);
780 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
782 remove_extent_mapping(&tree
->map_tree
, em
);
783 spin_unlock(&tree
->map_tree
.lock
);
789 /* once for the tree */
794 int btrfs_num_copies(struct btrfs_mapping_tree
*map_tree
, u64 logical
, u64 len
)
796 struct extent_map
*em
;
797 struct map_lookup
*map
;
798 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
801 spin_lock(&em_tree
->lock
);
802 em
= lookup_extent_mapping(em_tree
, logical
, len
);
803 spin_unlock(&em_tree
->lock
);
806 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
807 map
= (struct map_lookup
*)em
->bdev
;
808 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
809 ret
= map
->num_stripes
;
816 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
817 u64 logical
, u64
*length
,
818 struct btrfs_multi_bio
**multi_ret
, int mirror_num
)
820 struct extent_map
*em
;
821 struct map_lookup
*map
;
822 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
826 int stripes_allocated
= 8;
829 struct btrfs_multi_bio
*multi
= NULL
;
831 if (multi_ret
&& !(rw
& (1 << BIO_RW
))) {
832 stripes_allocated
= 1;
836 multi
= kzalloc(btrfs_multi_bio_size(stripes_allocated
),
842 spin_lock(&em_tree
->lock
);
843 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
844 spin_unlock(&em_tree
->lock
);
847 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
848 map
= (struct map_lookup
*)em
->bdev
;
849 offset
= logical
- em
->start
;
851 if (mirror_num
> map
->num_stripes
)
854 /* if our multi bio struct is too small, back off and try again */
855 if (multi_ret
&& (rw
& (1 << BIO_RW
)) &&
856 stripes_allocated
< map
->num_stripes
&&
857 ((map
->type
& BTRFS_BLOCK_GROUP_RAID1
) ||
858 (map
->type
& BTRFS_BLOCK_GROUP_DUP
))) {
859 stripes_allocated
= map
->num_stripes
;
866 * stripe_nr counts the total number of stripes we have to stride
867 * to get to this block
869 do_div(stripe_nr
, map
->stripe_len
);
871 stripe_offset
= stripe_nr
* map
->stripe_len
;
872 BUG_ON(offset
< stripe_offset
);
874 /* stripe_offset is the offset of this block in its stripe*/
875 stripe_offset
= offset
- stripe_offset
;
877 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
878 BTRFS_BLOCK_GROUP_DUP
)) {
879 /* we limit the length of each bio to what fits in a stripe */
880 *length
= min_t(u64
, em
->len
- offset
,
881 map
->stripe_len
- stripe_offset
);
883 *length
= em
->len
- offset
;
888 multi
->num_stripes
= 1;
890 if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
891 if (rw
& (1 << BIO_RW
))
892 multi
->num_stripes
= map
->num_stripes
;
893 else if (mirror_num
) {
894 stripe_index
= mirror_num
- 1;
898 struct btrfs_device
*cur
;
900 for (i
= 0; i
< map
->num_stripes
; i
++) {
901 cur
= map
->stripes
[i
].dev
;
902 spin_lock(&cur
->io_lock
);
903 if (cur
->total_ios
< least
) {
904 least
= cur
->total_ios
;
907 spin_unlock(&cur
->io_lock
);
910 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
911 if (rw
& (1 << BIO_RW
))
912 multi
->num_stripes
= map
->num_stripes
;
914 stripe_index
= mirror_num
- 1;
917 * after this do_div call, stripe_nr is the number of stripes
918 * on this device we have to walk to find the data, and
919 * stripe_index is the number of our device in the stripe array
921 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
923 BUG_ON(stripe_index
>= map
->num_stripes
);
924 BUG_ON(stripe_index
!= 0 && multi
->num_stripes
> 1);
926 for (i
= 0; i
< multi
->num_stripes
; i
++) {
927 multi
->stripes
[i
].physical
=
928 map
->stripes
[stripe_index
].physical
+ stripe_offset
+
929 stripe_nr
* map
->stripe_len
;
930 multi
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
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);
1063 spin_unlock(&map_tree
->map_tree
.lock
);
1065 /* already mapped? */
1066 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
1067 free_extent_map(em
);
1070 free_extent_map(em
);
1073 map
= kzalloc(sizeof(*map
), GFP_NOFS
);
1077 em
= alloc_extent_map(GFP_NOFS
);
1080 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
1081 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
1083 free_extent_map(em
);
1087 em
->bdev
= (struct block_device
*)map
;
1088 em
->start
= logical
;
1090 em
->block_start
= 0;
1092 map
->num_stripes
= num_stripes
;
1093 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
1094 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
1095 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
1096 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
1097 map
->type
= btrfs_chunk_type(leaf
, chunk
);
1098 for (i
= 0; i
< num_stripes
; i
++) {
1099 map
->stripes
[i
].physical
=
1100 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
1101 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
1102 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
);
1103 if (!map
->stripes
[i
].dev
) {
1105 free_extent_map(em
);
1110 spin_lock(&map_tree
->map_tree
.lock
);
1111 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
1112 spin_unlock(&map_tree
->map_tree
.lock
);
1114 free_extent_map(em
);
1119 static int fill_device_from_item(struct extent_buffer
*leaf
,
1120 struct btrfs_dev_item
*dev_item
,
1121 struct btrfs_device
*device
)
1125 device
->devid
= btrfs_device_id(leaf
, dev_item
);
1126 device
->total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
1127 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
1128 device
->type
= btrfs_device_type(leaf
, dev_item
);
1129 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
1130 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
1131 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
1133 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1134 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_DEV_UUID_SIZE
);
1139 static int read_one_dev(struct btrfs_root
*root
,
1140 struct extent_buffer
*leaf
,
1141 struct btrfs_dev_item
*dev_item
)
1143 struct btrfs_device
*device
;
1147 devid
= btrfs_device_id(leaf
, dev_item
);
1148 device
= btrfs_find_device(root
, devid
);
1150 printk("warning devid %Lu not found already\n", devid
);
1151 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1154 list_add(&device
->dev_list
,
1155 &root
->fs_info
->fs_devices
->devices
);
1156 device
->barriers
= 1;
1157 spin_lock_init(&device
->io_lock
);
1160 fill_device_from_item(leaf
, dev_item
, device
);
1161 device
->dev_root
= root
->fs_info
->dev_root
;
1164 ret
= btrfs_open_device(device
);
1172 int btrfs_read_super_device(struct btrfs_root
*root
, struct extent_buffer
*buf
)
1174 struct btrfs_dev_item
*dev_item
;
1176 dev_item
= (struct btrfs_dev_item
*)offsetof(struct btrfs_super_block
,
1178 return read_one_dev(root
, buf
, dev_item
);
1181 int btrfs_read_sys_array(struct btrfs_root
*root
)
1183 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1184 struct extent_buffer
*sb
= root
->fs_info
->sb_buffer
;
1185 struct btrfs_disk_key
*disk_key
;
1186 struct btrfs_chunk
*chunk
;
1187 struct btrfs_key key
;
1192 unsigned long sb_ptr
;
1196 array_size
= btrfs_super_sys_array_size(super_copy
);
1199 * we do this loop twice, once for the device items and
1200 * once for all of the chunks. This way there are device
1201 * structs filled in for every chunk
1203 ptr
= super_copy
->sys_chunk_array
;
1204 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
1207 while (cur
< array_size
) {
1208 disk_key
= (struct btrfs_disk_key
*)ptr
;
1209 btrfs_disk_key_to_cpu(&key
, disk_key
);
1211 len
= sizeof(*disk_key
);
1216 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1217 chunk
= (struct btrfs_chunk
*)sb_ptr
;
1218 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
1220 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
1221 len
= btrfs_chunk_item_size(num_stripes
);
1232 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
1234 struct btrfs_path
*path
;
1235 struct extent_buffer
*leaf
;
1236 struct btrfs_key key
;
1237 struct btrfs_key found_key
;
1241 root
= root
->fs_info
->chunk_root
;
1243 path
= btrfs_alloc_path();
1247 /* first we search for all of the device items, and then we
1248 * read in all of the chunk items. This way we can create chunk
1249 * mappings that reference all of the devices that are afound
1251 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1255 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1257 leaf
= path
->nodes
[0];
1258 slot
= path
->slots
[0];
1259 if (slot
>= btrfs_header_nritems(leaf
)) {
1260 ret
= btrfs_next_leaf(root
, path
);
1267 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1268 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
1269 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
1271 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
1272 struct btrfs_dev_item
*dev_item
;
1273 dev_item
= btrfs_item_ptr(leaf
, slot
,
1274 struct btrfs_dev_item
);
1275 ret
= read_one_dev(root
, leaf
, dev_item
);
1278 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1279 struct btrfs_chunk
*chunk
;
1280 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
1281 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
1285 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
1287 btrfs_release_path(root
, path
);
1291 btrfs_free_path(path
);