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 <linux/blkdev.h>
22 #include <linux/random.h>
23 #include <asm/div64.h>
25 #include "extent_map.h"
27 #include "transaction.h"
28 #include "print-tree.h"
39 struct btrfs_bio_stripe stripes
[];
42 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
43 (sizeof(struct btrfs_bio_stripe) * (n)))
45 static DEFINE_MUTEX(uuid_mutex
);
46 static LIST_HEAD(fs_uuids
);
48 int btrfs_cleanup_fs_uuids(void)
50 struct btrfs_fs_devices
*fs_devices
;
51 struct list_head
*uuid_cur
;
52 struct list_head
*devices_cur
;
53 struct btrfs_device
*dev
;
55 list_for_each(uuid_cur
, &fs_uuids
) {
56 fs_devices
= list_entry(uuid_cur
, struct btrfs_fs_devices
,
58 while(!list_empty(&fs_devices
->devices
)) {
59 devices_cur
= fs_devices
->devices
.next
;
60 dev
= list_entry(devices_cur
, struct btrfs_device
,
63 close_bdev_excl(dev
->bdev
);
65 list_del(&dev
->dev_list
);
72 static struct btrfs_device
*__find_device(struct list_head
*head
, u64 devid
,
75 struct btrfs_device
*dev
;
76 struct list_head
*cur
;
78 list_for_each(cur
, head
) {
79 dev
= list_entry(cur
, struct btrfs_device
, dev_list
);
80 if (dev
->devid
== devid
&&
81 (!uuid
|| !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
))) {
88 static struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
90 struct list_head
*cur
;
91 struct btrfs_fs_devices
*fs_devices
;
93 list_for_each(cur
, &fs_uuids
) {
94 fs_devices
= list_entry(cur
, struct btrfs_fs_devices
, list
);
95 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
101 static int device_list_add(const char *path
,
102 struct btrfs_super_block
*disk_super
,
103 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
105 struct btrfs_device
*device
;
106 struct btrfs_fs_devices
*fs_devices
;
107 u64 found_transid
= btrfs_super_generation(disk_super
);
109 fs_devices
= find_fsid(disk_super
->fsid
);
111 fs_devices
= kmalloc(sizeof(*fs_devices
), GFP_NOFS
);
114 INIT_LIST_HEAD(&fs_devices
->devices
);
115 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
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
,
125 disk_super
->dev_item
.uuid
);
128 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
130 /* we can safely leave the fs_devices entry around */
133 device
->devid
= devid
;
134 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
136 device
->barriers
= 1;
137 spin_lock_init(&device
->io_lock
);
138 device
->name
= kstrdup(path
, GFP_NOFS
);
143 list_add(&device
->dev_list
, &fs_devices
->devices
);
144 list_add(&device
->dev_alloc_list
, &fs_devices
->alloc_list
);
145 fs_devices
->num_devices
++;
148 if (found_transid
> fs_devices
->latest_trans
) {
149 fs_devices
->latest_devid
= devid
;
150 fs_devices
->latest_trans
= found_transid
;
152 if (fs_devices
->lowest_devid
> devid
) {
153 fs_devices
->lowest_devid
= devid
;
155 *fs_devices_ret
= fs_devices
;
159 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
161 struct list_head
*head
= &fs_devices
->devices
;
162 struct list_head
*cur
;
163 struct btrfs_device
*device
;
165 mutex_lock(&uuid_mutex
);
166 list_for_each(cur
, head
) {
167 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
169 close_bdev_excl(device
->bdev
);
173 mutex_unlock(&uuid_mutex
);
177 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
178 int flags
, void *holder
)
180 struct block_device
*bdev
;
181 struct list_head
*head
= &fs_devices
->devices
;
182 struct list_head
*cur
;
183 struct btrfs_device
*device
;
186 mutex_lock(&uuid_mutex
);
187 list_for_each(cur
, head
) {
188 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
189 bdev
= open_bdev_excl(device
->name
, flags
, holder
);
192 printk("open %s failed\n", device
->name
);
196 if (device
->devid
== fs_devices
->latest_devid
)
197 fs_devices
->latest_bdev
= bdev
;
198 if (device
->devid
== fs_devices
->lowest_devid
) {
199 fs_devices
->lowest_bdev
= bdev
;
203 mutex_unlock(&uuid_mutex
);
206 mutex_unlock(&uuid_mutex
);
207 btrfs_close_devices(fs_devices
);
211 int btrfs_scan_one_device(const char *path
, int flags
, void *holder
,
212 struct btrfs_fs_devices
**fs_devices_ret
)
214 struct btrfs_super_block
*disk_super
;
215 struct block_device
*bdev
;
216 struct buffer_head
*bh
;
221 mutex_lock(&uuid_mutex
);
223 bdev
= open_bdev_excl(path
, flags
, holder
);
230 ret
= set_blocksize(bdev
, 4096);
233 bh
= __bread(bdev
, BTRFS_SUPER_INFO_OFFSET
/ 4096, 4096);
238 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
239 if (strncmp((char *)(&disk_super
->magic
), BTRFS_MAGIC
,
240 sizeof(disk_super
->magic
))) {
244 devid
= le64_to_cpu(disk_super
->dev_item
.devid
);
245 transid
= btrfs_super_generation(disk_super
);
246 if (disk_super
->label
[0])
247 printk("device label %s ", disk_super
->label
);
249 /* FIXME, make a readl uuid parser */
250 printk("device fsid %llx-%llx ",
251 *(unsigned long long *)disk_super
->fsid
,
252 *(unsigned long long *)(disk_super
->fsid
+ 8));
254 printk("devid %Lu transid %Lu %s\n", devid
, transid
, path
);
255 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
260 close_bdev_excl(bdev
);
262 mutex_unlock(&uuid_mutex
);
267 * this uses a pretty simple search, the expectation is that it is
268 * called very infrequently and that a given device has a small number
271 static int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
272 struct btrfs_device
*device
,
273 struct btrfs_path
*path
,
274 u64 num_bytes
, u64
*start
)
276 struct btrfs_key key
;
277 struct btrfs_root
*root
= device
->dev_root
;
278 struct btrfs_dev_extent
*dev_extent
= NULL
;
281 u64 search_start
= 0;
282 u64 search_end
= device
->total_bytes
;
286 struct extent_buffer
*l
;
291 /* FIXME use last free of some kind */
293 /* we don't want to overwrite the superblock on the drive,
294 * so we make sure to start at an offset of at least 1MB
296 search_start
= max((u64
)1024 * 1024, search_start
);
298 if (root
->fs_info
->alloc_start
+ num_bytes
<= device
->total_bytes
)
299 search_start
= max(root
->fs_info
->alloc_start
, search_start
);
301 key
.objectid
= device
->devid
;
302 key
.offset
= search_start
;
303 key
.type
= BTRFS_DEV_EXTENT_KEY
;
304 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
307 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
311 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
314 slot
= path
->slots
[0];
315 if (slot
>= btrfs_header_nritems(l
)) {
316 ret
= btrfs_next_leaf(root
, path
);
323 if (search_start
>= search_end
) {
327 *start
= search_start
;
331 *start
= last_byte
> search_start
?
332 last_byte
: search_start
;
333 if (search_end
<= *start
) {
339 btrfs_item_key_to_cpu(l
, &key
, slot
);
341 if (key
.objectid
< device
->devid
)
344 if (key
.objectid
> device
->devid
)
347 if (key
.offset
>= search_start
&& key
.offset
> last_byte
&&
349 if (last_byte
< search_start
)
350 last_byte
= search_start
;
351 hole_size
= key
.offset
- last_byte
;
352 if (key
.offset
> last_byte
&&
353 hole_size
>= num_bytes
) {
358 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
) {
363 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
364 last_byte
= key
.offset
+ btrfs_dev_extent_length(l
, dev_extent
);
370 /* we have to make sure we didn't find an extent that has already
371 * been allocated by the map tree or the original allocation
373 btrfs_release_path(root
, path
);
374 BUG_ON(*start
< search_start
);
376 if (*start
+ num_bytes
> search_end
) {
380 /* check for pending inserts here */
384 btrfs_release_path(root
, path
);
388 int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
389 struct btrfs_device
*device
,
393 struct btrfs_path
*path
;
394 struct btrfs_root
*root
= device
->dev_root
;
395 struct btrfs_key key
;
397 path
= btrfs_alloc_path();
401 key
.objectid
= device
->devid
;
403 key
.type
= BTRFS_DEV_EXTENT_KEY
;
405 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
408 ret
= btrfs_del_item(trans
, root
, path
);
411 btrfs_free_path(path
);
415 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
416 struct btrfs_device
*device
,
417 u64 chunk_tree
, u64 chunk_objectid
,
419 u64 num_bytes
, u64
*start
)
422 struct btrfs_path
*path
;
423 struct btrfs_root
*root
= device
->dev_root
;
424 struct btrfs_dev_extent
*extent
;
425 struct extent_buffer
*leaf
;
426 struct btrfs_key key
;
428 path
= btrfs_alloc_path();
432 ret
= find_free_dev_extent(trans
, device
, path
, num_bytes
, start
);
437 key
.objectid
= device
->devid
;
439 key
.type
= BTRFS_DEV_EXTENT_KEY
;
440 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
444 leaf
= path
->nodes
[0];
445 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
446 struct btrfs_dev_extent
);
447 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
448 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
449 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
451 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
452 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
455 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
456 btrfs_mark_buffer_dirty(leaf
);
458 btrfs_free_path(path
);
462 static int find_next_chunk(struct btrfs_root
*root
, u64 objectid
, u64
*offset
)
464 struct btrfs_path
*path
;
466 struct btrfs_key key
;
467 struct btrfs_chunk
*chunk
;
468 struct btrfs_key found_key
;
470 path
= btrfs_alloc_path();
473 key
.objectid
= objectid
;
474 key
.offset
= (u64
)-1;
475 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
477 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
483 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
487 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
489 if (found_key
.objectid
!= objectid
)
492 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
494 *offset
= found_key
.offset
+
495 btrfs_chunk_length(path
->nodes
[0], chunk
);
500 btrfs_free_path(path
);
504 static int find_next_devid(struct btrfs_root
*root
, struct btrfs_path
*path
,
508 struct btrfs_key key
;
509 struct btrfs_key found_key
;
511 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
512 key
.type
= BTRFS_DEV_ITEM_KEY
;
513 key
.offset
= (u64
)-1;
515 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
521 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
526 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
528 *objectid
= found_key
.offset
+ 1;
532 btrfs_release_path(root
, path
);
537 * the device information is stored in the chunk root
538 * the btrfs_device struct should be fully filled in
540 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
541 struct btrfs_root
*root
,
542 struct btrfs_device
*device
)
545 struct btrfs_path
*path
;
546 struct btrfs_dev_item
*dev_item
;
547 struct extent_buffer
*leaf
;
548 struct btrfs_key key
;
552 root
= root
->fs_info
->chunk_root
;
554 path
= btrfs_alloc_path();
558 ret
= find_next_devid(root
, path
, &free_devid
);
562 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
563 key
.type
= BTRFS_DEV_ITEM_KEY
;
564 key
.offset
= free_devid
;
566 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
571 leaf
= path
->nodes
[0];
572 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
574 device
->devid
= free_devid
;
575 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
576 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
577 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
578 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
579 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
580 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
581 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
582 btrfs_set_device_group(leaf
, dev_item
, 0);
583 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
584 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
586 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
587 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
588 btrfs_mark_buffer_dirty(leaf
);
592 btrfs_free_path(path
);
596 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
598 struct btrfs_trans_handle
*trans
;
599 struct btrfs_device
*device
;
600 struct block_device
*bdev
;
601 struct list_head
*cur
;
602 struct list_head
*devices
;
607 bdev
= open_bdev_excl(device_path
, 0, root
->fs_info
->bdev_holder
);
611 mutex_lock(&root
->fs_info
->fs_mutex
);
612 trans
= btrfs_start_transaction(root
, 1);
613 devices
= &root
->fs_info
->fs_devices
->devices
;
614 list_for_each(cur
, devices
) {
615 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
616 if (device
->bdev
== bdev
) {
622 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
624 /* we can safely leave the fs_devices entry around */
629 device
->barriers
= 1;
630 generate_random_uuid(device
->uuid
);
631 spin_lock_init(&device
->io_lock
);
632 device
->name
= kstrdup(device_path
, GFP_NOFS
);
637 device
->io_width
= root
->sectorsize
;
638 device
->io_align
= root
->sectorsize
;
639 device
->sector_size
= root
->sectorsize
;
640 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
641 device
->dev_root
= root
->fs_info
->dev_root
;
644 ret
= btrfs_add_device(trans
, root
, device
);
648 total_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
649 btrfs_set_super_total_bytes(&root
->fs_info
->super_copy
,
650 total_bytes
+ device
->total_bytes
);
652 total_bytes
= btrfs_super_num_devices(&root
->fs_info
->super_copy
);
653 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
,
656 list_add(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
657 list_add(&device
->dev_alloc_list
,
658 &root
->fs_info
->fs_devices
->alloc_list
);
659 root
->fs_info
->fs_devices
->num_devices
++;
661 btrfs_end_transaction(trans
, root
);
662 mutex_unlock(&root
->fs_info
->fs_mutex
);
666 close_bdev_excl(bdev
);
670 int btrfs_update_device(struct btrfs_trans_handle
*trans
,
671 struct btrfs_device
*device
)
674 struct btrfs_path
*path
;
675 struct btrfs_root
*root
;
676 struct btrfs_dev_item
*dev_item
;
677 struct extent_buffer
*leaf
;
678 struct btrfs_key key
;
680 root
= device
->dev_root
->fs_info
->chunk_root
;
682 path
= btrfs_alloc_path();
686 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
687 key
.type
= BTRFS_DEV_ITEM_KEY
;
688 key
.offset
= device
->devid
;
690 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
699 leaf
= path
->nodes
[0];
700 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
702 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
703 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
704 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
705 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
706 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
707 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
708 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
709 btrfs_mark_buffer_dirty(leaf
);
712 btrfs_free_path(path
);
716 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
717 struct btrfs_device
*device
, u64 new_size
)
719 struct btrfs_super_block
*super_copy
=
720 &device
->dev_root
->fs_info
->super_copy
;
721 u64 old_total
= btrfs_super_total_bytes(super_copy
);
722 u64 diff
= new_size
- device
->total_bytes
;
724 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
725 return btrfs_update_device(trans
, device
);
728 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
729 struct btrfs_root
*root
,
730 u64 chunk_tree
, u64 chunk_objectid
,
734 struct btrfs_path
*path
;
735 struct btrfs_key key
;
737 root
= root
->fs_info
->chunk_root
;
738 path
= btrfs_alloc_path();
742 key
.objectid
= chunk_objectid
;
743 key
.offset
= chunk_offset
;
744 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
746 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
749 ret
= btrfs_del_item(trans
, root
, path
);
752 btrfs_free_path(path
);
756 int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
759 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
760 struct btrfs_disk_key
*disk_key
;
761 struct btrfs_chunk
*chunk
;
768 struct btrfs_key key
;
770 array_size
= btrfs_super_sys_array_size(super_copy
);
772 ptr
= super_copy
->sys_chunk_array
;
775 while (cur
< array_size
) {
776 disk_key
= (struct btrfs_disk_key
*)ptr
;
777 btrfs_disk_key_to_cpu(&key
, disk_key
);
779 len
= sizeof(*disk_key
);
781 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
782 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
783 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
784 len
+= btrfs_chunk_item_size(num_stripes
);
789 if (key
.objectid
== chunk_objectid
&&
790 key
.offset
== chunk_offset
) {
791 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
793 btrfs_set_super_sys_array_size(super_copy
, array_size
);
803 int btrfs_relocate_chunk(struct btrfs_root
*root
,
804 u64 chunk_tree
, u64 chunk_objectid
,
807 struct extent_map_tree
*em_tree
;
808 struct btrfs_root
*extent_root
;
809 struct btrfs_trans_handle
*trans
;
810 struct extent_map
*em
;
811 struct map_lookup
*map
;
815 root
= root
->fs_info
->chunk_root
;
816 extent_root
= root
->fs_info
->extent_root
;
817 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
819 /* step one, relocate all the extents inside this chunk */
820 ret
= btrfs_shrink_extent_tree(extent_root
, chunk_offset
);
823 trans
= btrfs_start_transaction(root
, 1);
827 * step two, delete the device extents and the
830 spin_lock(&em_tree
->lock
);
831 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
832 spin_unlock(&em_tree
->lock
);
834 BUG_ON(em
->start
> chunk_offset
|| em
->start
+ em
->len
< chunk_offset
);
835 map
= (struct map_lookup
*)em
->bdev
;
837 for (i
= 0; i
< map
->num_stripes
; i
++) {
838 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
839 map
->stripes
[i
].physical
);
842 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
847 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
848 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
855 spin_lock(&em_tree
->lock
);
856 remove_extent_mapping(em_tree
, em
);
860 /* once for the tree */
862 spin_unlock(&em_tree
->lock
);
868 btrfs_end_transaction(trans
, root
);
872 static u64
div_factor(u64 num
, int factor
)
882 int btrfs_balance(struct btrfs_root
*dev_root
)
885 struct list_head
*cur
;
886 struct list_head
*devices
= &dev_root
->fs_info
->fs_devices
->devices
;
887 struct btrfs_device
*device
;
890 struct btrfs_path
*path
;
891 struct btrfs_key key
;
892 struct btrfs_chunk
*chunk
;
893 struct btrfs_root
*chunk_root
= dev_root
->fs_info
->chunk_root
;
894 struct btrfs_trans_handle
*trans
;
895 struct btrfs_key found_key
;
898 dev_root
= dev_root
->fs_info
->dev_root
;
900 mutex_lock(&dev_root
->fs_info
->fs_mutex
);
901 /* step one make some room on all the devices */
902 list_for_each(cur
, devices
) {
903 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
904 old_size
= device
->total_bytes
;
905 size_to_free
= div_factor(old_size
, 1);
906 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
907 if (device
->total_bytes
- device
->bytes_used
> size_to_free
)
910 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
913 trans
= btrfs_start_transaction(dev_root
, 1);
916 ret
= btrfs_grow_device(trans
, device
, old_size
);
919 btrfs_end_transaction(trans
, dev_root
);
922 /* step two, relocate all the chunks */
923 path
= btrfs_alloc_path();
926 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
927 key
.offset
= (u64
)-1;
928 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
931 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
936 * this shouldn't happen, it means the last relocate
942 ret
= btrfs_previous_item(chunk_root
, path
, 0,
943 BTRFS_CHUNK_ITEM_KEY
);
947 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
949 if (found_key
.objectid
!= key
.objectid
)
951 chunk
= btrfs_item_ptr(path
->nodes
[0],
954 key
.offset
= found_key
.offset
;
955 /* chunk zero is special */
959 ret
= btrfs_relocate_chunk(chunk_root
,
960 chunk_root
->root_key
.objectid
,
964 btrfs_release_path(chunk_root
, path
);
968 btrfs_free_path(path
);
969 mutex_unlock(&dev_root
->fs_info
->fs_mutex
);
974 * shrinking a device means finding all of the device extents past
975 * the new size, and then following the back refs to the chunks.
976 * The chunk relocation code actually frees the device extent
978 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
980 struct btrfs_trans_handle
*trans
;
981 struct btrfs_root
*root
= device
->dev_root
;
982 struct btrfs_dev_extent
*dev_extent
= NULL
;
983 struct btrfs_path
*path
;
990 struct extent_buffer
*l
;
991 struct btrfs_key key
;
992 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
993 u64 old_total
= btrfs_super_total_bytes(super_copy
);
994 u64 diff
= device
->total_bytes
- new_size
;
997 path
= btrfs_alloc_path();
1001 trans
= btrfs_start_transaction(root
, 1);
1009 device
->total_bytes
= new_size
;
1010 ret
= btrfs_update_device(trans
, device
);
1012 btrfs_end_transaction(trans
, root
);
1015 WARN_ON(diff
> old_total
);
1016 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
1017 btrfs_end_transaction(trans
, root
);
1019 key
.objectid
= device
->devid
;
1020 key
.offset
= (u64
)-1;
1021 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1024 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1028 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
1037 slot
= path
->slots
[0];
1038 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
1040 if (key
.objectid
!= device
->devid
)
1043 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1044 length
= btrfs_dev_extent_length(l
, dev_extent
);
1046 if (key
.offset
+ length
<= new_size
)
1049 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
1050 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
1051 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
1052 btrfs_release_path(root
, path
);
1054 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
1061 btrfs_free_path(path
);
1065 int btrfs_add_system_chunk(struct btrfs_trans_handle
*trans
,
1066 struct btrfs_root
*root
,
1067 struct btrfs_key
*key
,
1068 struct btrfs_chunk
*chunk
, int item_size
)
1070 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1071 struct btrfs_disk_key disk_key
;
1075 array_size
= btrfs_super_sys_array_size(super_copy
);
1076 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
1079 ptr
= super_copy
->sys_chunk_array
+ array_size
;
1080 btrfs_cpu_key_to_disk(&disk_key
, key
);
1081 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
1082 ptr
+= sizeof(disk_key
);
1083 memcpy(ptr
, chunk
, item_size
);
1084 item_size
+= sizeof(disk_key
);
1085 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
1089 static u64
chunk_bytes_by_type(u64 type
, u64 calc_size
, int num_stripes
,
1092 if (type
& (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_DUP
))
1094 else if (type
& BTRFS_BLOCK_GROUP_RAID10
)
1095 return calc_size
* (num_stripes
/ sub_stripes
);
1097 return calc_size
* num_stripes
;
1101 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
1102 struct btrfs_root
*extent_root
, u64
*start
,
1103 u64
*num_bytes
, u64 type
)
1106 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
1107 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
1108 struct btrfs_path
*path
;
1109 struct btrfs_stripe
*stripes
;
1110 struct btrfs_device
*device
= NULL
;
1111 struct btrfs_chunk
*chunk
;
1112 struct list_head private_devs
;
1113 struct list_head
*dev_list
;
1114 struct list_head
*cur
;
1115 struct extent_map_tree
*em_tree
;
1116 struct map_lookup
*map
;
1117 struct extent_map
*em
;
1118 int min_stripe_size
= 1 * 1024 * 1024;
1120 u64 calc_size
= 1024 * 1024 * 1024;
1121 u64 max_chunk_size
= calc_size
;
1126 int num_stripes
= 1;
1127 int min_stripes
= 1;
1128 int sub_stripes
= 0;
1132 int stripe_len
= 64 * 1024;
1133 struct btrfs_key key
;
1135 if ((type
& BTRFS_BLOCK_GROUP_RAID1
) &&
1136 (type
& BTRFS_BLOCK_GROUP_DUP
)) {
1138 type
&= ~BTRFS_BLOCK_GROUP_DUP
;
1140 dev_list
= &extent_root
->fs_info
->fs_devices
->alloc_list
;
1141 if (list_empty(dev_list
))
1144 if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
1145 num_stripes
= btrfs_super_num_devices(&info
->super_copy
);
1148 if (type
& (BTRFS_BLOCK_GROUP_DUP
)) {
1152 if (type
& (BTRFS_BLOCK_GROUP_RAID1
)) {
1153 num_stripes
= min_t(u64
, 2,
1154 btrfs_super_num_devices(&info
->super_copy
));
1155 if (num_stripes
< 2)
1159 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
1160 num_stripes
= btrfs_super_num_devices(&info
->super_copy
);
1161 if (num_stripes
< 4)
1163 num_stripes
&= ~(u32
)1;
1168 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
1169 max_chunk_size
= 10 * calc_size
;
1170 min_stripe_size
= 64 * 1024 * 1024;
1171 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
1172 max_chunk_size
= 4 * calc_size
;
1173 min_stripe_size
= 32 * 1024 * 1024;
1174 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1175 calc_size
= 8 * 1024 * 1024;
1176 max_chunk_size
= calc_size
* 2;
1177 min_stripe_size
= 1 * 1024 * 1024;
1180 path
= btrfs_alloc_path();
1184 /* we don't want a chunk larger than 10% of the FS */
1185 percent_max
= div_factor(btrfs_super_total_bytes(&info
->super_copy
), 1);
1186 max_chunk_size
= min(percent_max
, max_chunk_size
);
1189 if (calc_size
* num_stripes
> max_chunk_size
) {
1190 calc_size
= max_chunk_size
;
1191 do_div(calc_size
, num_stripes
);
1192 do_div(calc_size
, stripe_len
);
1193 calc_size
*= stripe_len
;
1195 /* we don't want tiny stripes */
1196 calc_size
= max_t(u64
, min_stripe_size
, calc_size
);
1198 do_div(calc_size
, stripe_len
);
1199 calc_size
*= stripe_len
;
1201 INIT_LIST_HEAD(&private_devs
);
1202 cur
= dev_list
->next
;
1205 if (type
& BTRFS_BLOCK_GROUP_DUP
)
1206 min_free
= calc_size
* 2;
1208 min_free
= calc_size
;
1210 /* we add 1MB because we never use the first 1MB of the device */
1211 min_free
+= 1024 * 1024;
1213 /* build a private list of devices we will allocate from */
1214 while(index
< num_stripes
) {
1215 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
1217 avail
= device
->total_bytes
- device
->bytes_used
;
1220 if (avail
>= min_free
) {
1221 u64 ignored_start
= 0;
1222 ret
= find_free_dev_extent(trans
, device
, path
,
1226 list_move_tail(&device
->dev_alloc_list
,
1229 if (type
& BTRFS_BLOCK_GROUP_DUP
)
1232 } else if (avail
> max_avail
)
1234 if (cur
== dev_list
)
1237 if (index
< num_stripes
) {
1238 list_splice(&private_devs
, dev_list
);
1239 if (index
>= min_stripes
) {
1240 num_stripes
= index
;
1241 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
1242 num_stripes
/= sub_stripes
;
1243 num_stripes
*= sub_stripes
;
1248 if (!looped
&& max_avail
> 0) {
1250 calc_size
= max_avail
;
1253 btrfs_free_path(path
);
1256 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1257 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1258 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
1261 btrfs_free_path(path
);
1265 chunk
= kmalloc(btrfs_chunk_item_size(num_stripes
), GFP_NOFS
);
1267 btrfs_free_path(path
);
1271 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
1274 btrfs_free_path(path
);
1277 btrfs_free_path(path
);
1280 stripes
= &chunk
->stripe
;
1281 *num_bytes
= chunk_bytes_by_type(type
, calc_size
,
1282 num_stripes
, sub_stripes
);
1285 printk("new chunk type %Lu start %Lu size %Lu\n", type
, key
.offset
, *num_bytes
);
1286 while(index
< num_stripes
) {
1287 struct btrfs_stripe
*stripe
;
1288 BUG_ON(list_empty(&private_devs
));
1289 cur
= private_devs
.next
;
1290 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
1292 /* loop over this device again if we're doing a dup group */
1293 if (!(type
& BTRFS_BLOCK_GROUP_DUP
) ||
1294 (index
== num_stripes
- 1))
1295 list_move_tail(&device
->dev_alloc_list
, dev_list
);
1297 ret
= btrfs_alloc_dev_extent(trans
, device
,
1298 info
->chunk_root
->root_key
.objectid
,
1299 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, key
.offset
,
1300 calc_size
, &dev_offset
);
1302 printk("alloc chunk start %Lu size %Lu from dev %Lu type %Lu\n", key
.offset
, calc_size
, device
->devid
, type
);
1303 device
->bytes_used
+= calc_size
;
1304 ret
= btrfs_update_device(trans
, device
);
1307 map
->stripes
[index
].dev
= device
;
1308 map
->stripes
[index
].physical
= dev_offset
;
1309 stripe
= stripes
+ index
;
1310 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
1311 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
1312 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
1313 physical
= dev_offset
;
1316 BUG_ON(!list_empty(&private_devs
));
1318 /* key was set above */
1319 btrfs_set_stack_chunk_length(chunk
, *num_bytes
);
1320 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
1321 btrfs_set_stack_chunk_stripe_len(chunk
, stripe_len
);
1322 btrfs_set_stack_chunk_type(chunk
, type
);
1323 btrfs_set_stack_chunk_num_stripes(chunk
, num_stripes
);
1324 btrfs_set_stack_chunk_io_align(chunk
, stripe_len
);
1325 btrfs_set_stack_chunk_io_width(chunk
, stripe_len
);
1326 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
1327 btrfs_set_stack_chunk_sub_stripes(chunk
, sub_stripes
);
1328 map
->sector_size
= extent_root
->sectorsize
;
1329 map
->stripe_len
= stripe_len
;
1330 map
->io_align
= stripe_len
;
1331 map
->io_width
= stripe_len
;
1333 map
->num_stripes
= num_stripes
;
1334 map
->sub_stripes
= sub_stripes
;
1336 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
,
1337 btrfs_chunk_item_size(num_stripes
));
1339 *start
= key
.offset
;;
1341 em
= alloc_extent_map(GFP_NOFS
);
1344 em
->bdev
= (struct block_device
*)map
;
1345 em
->start
= key
.offset
;
1346 em
->len
= *num_bytes
;
1347 em
->block_start
= 0;
1349 if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1350 ret
= btrfs_add_system_chunk(trans
, chunk_root
, &key
,
1351 chunk
, btrfs_chunk_item_size(num_stripes
));
1356 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
1357 spin_lock(&em_tree
->lock
);
1358 ret
= add_extent_mapping(em_tree
, em
);
1359 spin_unlock(&em_tree
->lock
);
1361 free_extent_map(em
);
1365 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
1367 extent_map_tree_init(&tree
->map_tree
, GFP_NOFS
);
1370 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
1372 struct extent_map
*em
;
1375 spin_lock(&tree
->map_tree
.lock
);
1376 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
1378 remove_extent_mapping(&tree
->map_tree
, em
);
1379 spin_unlock(&tree
->map_tree
.lock
);
1384 free_extent_map(em
);
1385 /* once for the tree */
1386 free_extent_map(em
);
1390 int btrfs_num_copies(struct btrfs_mapping_tree
*map_tree
, u64 logical
, u64 len
)
1392 struct extent_map
*em
;
1393 struct map_lookup
*map
;
1394 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
1397 spin_lock(&em_tree
->lock
);
1398 em
= lookup_extent_mapping(em_tree
, logical
, len
);
1399 spin_unlock(&em_tree
->lock
);
1402 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
1403 map
= (struct map_lookup
*)em
->bdev
;
1404 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
1405 ret
= map
->num_stripes
;
1406 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
1407 ret
= map
->sub_stripes
;
1410 free_extent_map(em
);
1414 static int __btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
1415 u64 logical
, u64
*length
,
1416 struct btrfs_multi_bio
**multi_ret
,
1417 int mirror_num
, struct page
*unplug_page
)
1419 struct extent_map
*em
;
1420 struct map_lookup
*map
;
1421 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
1425 int stripes_allocated
= 8;
1426 int stripes_required
= 1;
1430 struct btrfs_multi_bio
*multi
= NULL
;
1432 if (multi_ret
&& !(rw
& (1 << BIO_RW
))) {
1433 stripes_allocated
= 1;
1437 multi
= kzalloc(btrfs_multi_bio_size(stripes_allocated
),
1443 spin_lock(&em_tree
->lock
);
1444 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
1445 spin_unlock(&em_tree
->lock
);
1447 if (!em
&& unplug_page
)
1451 printk("unable to find logical %Lu\n", logical
);
1455 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
1456 map
= (struct map_lookup
*)em
->bdev
;
1457 offset
= logical
- em
->start
;
1459 if (mirror_num
> map
->num_stripes
)
1462 /* if our multi bio struct is too small, back off and try again */
1463 if (rw
& (1 << BIO_RW
)) {
1464 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
1465 BTRFS_BLOCK_GROUP_DUP
)) {
1466 stripes_required
= map
->num_stripes
;
1467 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
1468 stripes_required
= map
->sub_stripes
;
1471 if (multi_ret
&& rw
== WRITE
&&
1472 stripes_allocated
< stripes_required
) {
1473 stripes_allocated
= map
->num_stripes
;
1474 free_extent_map(em
);
1480 * stripe_nr counts the total number of stripes we have to stride
1481 * to get to this block
1483 do_div(stripe_nr
, map
->stripe_len
);
1485 stripe_offset
= stripe_nr
* map
->stripe_len
;
1486 BUG_ON(offset
< stripe_offset
);
1488 /* stripe_offset is the offset of this block in its stripe*/
1489 stripe_offset
= offset
- stripe_offset
;
1491 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
1492 BTRFS_BLOCK_GROUP_RAID10
|
1493 BTRFS_BLOCK_GROUP_DUP
)) {
1494 /* we limit the length of each bio to what fits in a stripe */
1495 *length
= min_t(u64
, em
->len
- offset
,
1496 map
->stripe_len
- stripe_offset
);
1498 *length
= em
->len
- offset
;
1501 if (!multi_ret
&& !unplug_page
)
1506 if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
1507 if (unplug_page
|| (rw
& (1 << BIO_RW
)))
1508 num_stripes
= map
->num_stripes
;
1509 else if (mirror_num
) {
1510 stripe_index
= mirror_num
- 1;
1512 u64 orig_stripe_nr
= stripe_nr
;
1513 stripe_index
= do_div(orig_stripe_nr
, num_stripes
);
1515 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
1516 if (rw
& (1 << BIO_RW
))
1517 num_stripes
= map
->num_stripes
;
1518 else if (mirror_num
)
1519 stripe_index
= mirror_num
- 1;
1520 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
1521 int factor
= map
->num_stripes
/ map
->sub_stripes
;
1523 stripe_index
= do_div(stripe_nr
, factor
);
1524 stripe_index
*= map
->sub_stripes
;
1526 if (unplug_page
|| (rw
& (1 << BIO_RW
)))
1527 num_stripes
= map
->sub_stripes
;
1528 else if (mirror_num
)
1529 stripe_index
+= mirror_num
- 1;
1531 u64 orig_stripe_nr
= stripe_nr
;
1532 stripe_index
+= do_div(orig_stripe_nr
,
1537 * after this do_div call, stripe_nr is the number of stripes
1538 * on this device we have to walk to find the data, and
1539 * stripe_index is the number of our device in the stripe array
1541 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
1543 BUG_ON(stripe_index
>= map
->num_stripes
);
1545 for (i
= 0; i
< num_stripes
; i
++) {
1547 struct btrfs_device
*device
;
1548 struct backing_dev_info
*bdi
;
1550 device
= map
->stripes
[stripe_index
].dev
;
1551 bdi
= blk_get_backing_dev_info(device
->bdev
);
1552 if (bdi
->unplug_io_fn
) {
1553 bdi
->unplug_io_fn(bdi
, unplug_page
);
1556 multi
->stripes
[i
].physical
=
1557 map
->stripes
[stripe_index
].physical
+
1558 stripe_offset
+ stripe_nr
* map
->stripe_len
;
1559 multi
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
1565 multi
->num_stripes
= num_stripes
;
1568 free_extent_map(em
);
1572 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
1573 u64 logical
, u64
*length
,
1574 struct btrfs_multi_bio
**multi_ret
, int mirror_num
)
1576 return __btrfs_map_block(map_tree
, rw
, logical
, length
, multi_ret
,
1580 int btrfs_unplug_page(struct btrfs_mapping_tree
*map_tree
,
1581 u64 logical
, struct page
*page
)
1583 u64 length
= PAGE_CACHE_SIZE
;
1584 return __btrfs_map_block(map_tree
, READ
, logical
, &length
,
1589 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1590 static void end_bio_multi_stripe(struct bio
*bio
, int err
)
1592 static int end_bio_multi_stripe(struct bio
*bio
,
1593 unsigned int bytes_done
, int err
)
1596 struct btrfs_multi_bio
*multi
= bio
->bi_private
;
1598 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1605 if (atomic_dec_and_test(&multi
->stripes_pending
)) {
1606 bio
->bi_private
= multi
->private;
1607 bio
->bi_end_io
= multi
->end_io
;
1609 if (!err
&& multi
->error
)
1613 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1614 bio_endio(bio
, bio
->bi_size
, err
);
1616 bio_endio(bio
, err
);
1621 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1626 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
1629 struct btrfs_mapping_tree
*map_tree
;
1630 struct btrfs_device
*dev
;
1631 struct bio
*first_bio
= bio
;
1632 u64 logical
= bio
->bi_sector
<< 9;
1635 struct btrfs_multi_bio
*multi
= NULL
;
1640 length
= bio
->bi_size
;
1641 map_tree
= &root
->fs_info
->mapping_tree
;
1642 map_length
= length
;
1644 ret
= btrfs_map_block(map_tree
, rw
, logical
, &map_length
, &multi
,
1648 total_devs
= multi
->num_stripes
;
1649 if (map_length
< length
) {
1650 printk("mapping failed logical %Lu bio len %Lu "
1651 "len %Lu\n", logical
, length
, map_length
);
1654 multi
->end_io
= first_bio
->bi_end_io
;
1655 multi
->private = first_bio
->bi_private
;
1656 atomic_set(&multi
->stripes_pending
, multi
->num_stripes
);
1658 while(dev_nr
< total_devs
) {
1659 if (total_devs
> 1) {
1660 if (dev_nr
< total_devs
- 1) {
1661 bio
= bio_clone(first_bio
, GFP_NOFS
);
1666 bio
->bi_private
= multi
;
1667 bio
->bi_end_io
= end_bio_multi_stripe
;
1669 bio
->bi_sector
= multi
->stripes
[dev_nr
].physical
>> 9;
1670 dev
= multi
->stripes
[dev_nr
].dev
;
1672 bio
->bi_bdev
= dev
->bdev
;
1673 spin_lock(&dev
->io_lock
);
1675 spin_unlock(&dev
->io_lock
);
1676 submit_bio(rw
, bio
);
1679 if (total_devs
== 1)
1684 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
,
1687 struct list_head
*head
= &root
->fs_info
->fs_devices
->devices
;
1689 return __find_device(head
, devid
, uuid
);
1692 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
1693 struct extent_buffer
*leaf
,
1694 struct btrfs_chunk
*chunk
)
1696 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
1697 struct map_lookup
*map
;
1698 struct extent_map
*em
;
1702 u8 uuid
[BTRFS_UUID_SIZE
];
1707 logical
= key
->offset
;
1708 length
= btrfs_chunk_length(leaf
, chunk
);
1709 spin_lock(&map_tree
->map_tree
.lock
);
1710 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
1711 spin_unlock(&map_tree
->map_tree
.lock
);
1713 /* already mapped? */
1714 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
1715 free_extent_map(em
);
1718 free_extent_map(em
);
1721 map
= kzalloc(sizeof(*map
), GFP_NOFS
);
1725 em
= alloc_extent_map(GFP_NOFS
);
1728 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
1729 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
1731 free_extent_map(em
);
1735 em
->bdev
= (struct block_device
*)map
;
1736 em
->start
= logical
;
1738 em
->block_start
= 0;
1740 map
->num_stripes
= num_stripes
;
1741 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
1742 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
1743 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
1744 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
1745 map
->type
= btrfs_chunk_type(leaf
, chunk
);
1746 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
1747 for (i
= 0; i
< num_stripes
; i
++) {
1748 map
->stripes
[i
].physical
=
1749 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
1750 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
1751 read_extent_buffer(leaf
, uuid
, (unsigned long)
1752 btrfs_stripe_dev_uuid_nr(chunk
, i
),
1754 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
, uuid
);
1755 if (!map
->stripes
[i
].dev
) {
1757 free_extent_map(em
);
1762 spin_lock(&map_tree
->map_tree
.lock
);
1763 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
1764 spin_unlock(&map_tree
->map_tree
.lock
);
1766 free_extent_map(em
);
1771 static int fill_device_from_item(struct extent_buffer
*leaf
,
1772 struct btrfs_dev_item
*dev_item
,
1773 struct btrfs_device
*device
)
1777 device
->devid
= btrfs_device_id(leaf
, dev_item
);
1778 device
->total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
1779 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
1780 device
->type
= btrfs_device_type(leaf
, dev_item
);
1781 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
1782 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
1783 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
1785 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1786 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1791 static int read_one_dev(struct btrfs_root
*root
,
1792 struct extent_buffer
*leaf
,
1793 struct btrfs_dev_item
*dev_item
)
1795 struct btrfs_device
*device
;
1798 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1800 devid
= btrfs_device_id(leaf
, dev_item
);
1801 read_extent_buffer(leaf
, dev_uuid
,
1802 (unsigned long)btrfs_device_uuid(dev_item
),
1804 device
= btrfs_find_device(root
, devid
, dev_uuid
);
1806 printk("warning devid %Lu not found already\n", devid
);
1807 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1810 list_add(&device
->dev_list
,
1811 &root
->fs_info
->fs_devices
->devices
);
1812 list_add(&device
->dev_alloc_list
,
1813 &root
->fs_info
->fs_devices
->alloc_list
);
1814 device
->barriers
= 1;
1815 spin_lock_init(&device
->io_lock
);
1818 fill_device_from_item(leaf
, dev_item
, device
);
1819 device
->dev_root
= root
->fs_info
->dev_root
;
1822 ret
= btrfs_open_device(device
);
1830 int btrfs_read_super_device(struct btrfs_root
*root
, struct extent_buffer
*buf
)
1832 struct btrfs_dev_item
*dev_item
;
1834 dev_item
= (struct btrfs_dev_item
*)offsetof(struct btrfs_super_block
,
1836 return read_one_dev(root
, buf
, dev_item
);
1839 int btrfs_read_sys_array(struct btrfs_root
*root
)
1841 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1842 struct extent_buffer
*sb
= root
->fs_info
->sb_buffer
;
1843 struct btrfs_disk_key
*disk_key
;
1844 struct btrfs_chunk
*chunk
;
1846 unsigned long sb_ptr
;
1852 struct btrfs_key key
;
1854 array_size
= btrfs_super_sys_array_size(super_copy
);
1856 ptr
= super_copy
->sys_chunk_array
;
1857 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
1860 while (cur
< array_size
) {
1861 disk_key
= (struct btrfs_disk_key
*)ptr
;
1862 btrfs_disk_key_to_cpu(&key
, disk_key
);
1864 len
= sizeof(*disk_key
);
1869 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1870 chunk
= (struct btrfs_chunk
*)sb_ptr
;
1871 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
1874 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
1875 len
= btrfs_chunk_item_size(num_stripes
);
1887 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
1889 struct btrfs_path
*path
;
1890 struct extent_buffer
*leaf
;
1891 struct btrfs_key key
;
1892 struct btrfs_key found_key
;
1896 root
= root
->fs_info
->chunk_root
;
1898 path
= btrfs_alloc_path();
1902 /* first we search for all of the device items, and then we
1903 * read in all of the chunk items. This way we can create chunk
1904 * mappings that reference all of the devices that are afound
1906 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1910 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1912 leaf
= path
->nodes
[0];
1913 slot
= path
->slots
[0];
1914 if (slot
>= btrfs_header_nritems(leaf
)) {
1915 ret
= btrfs_next_leaf(root
, path
);
1922 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1923 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
1924 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
1926 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
1927 struct btrfs_dev_item
*dev_item
;
1928 dev_item
= btrfs_item_ptr(leaf
, slot
,
1929 struct btrfs_dev_item
);
1930 ret
= read_one_dev(root
, leaf
, dev_item
);
1933 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1934 struct btrfs_chunk
*chunk
;
1935 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
1936 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
1940 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
1942 btrfs_release_path(root
, path
);
1946 btrfs_free_path(path
);