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"
30 #include "async-thread.h"
40 struct btrfs_bio_stripe stripes
[];
43 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
44 (sizeof(struct btrfs_bio_stripe) * (n)))
46 static DEFINE_MUTEX(uuid_mutex
);
47 static LIST_HEAD(fs_uuids
);
49 void btrfs_lock_volumes(void)
51 mutex_lock(&uuid_mutex
);
54 void btrfs_unlock_volumes(void)
56 mutex_unlock(&uuid_mutex
);
59 static void lock_chunks(struct btrfs_root
*root
)
61 mutex_lock(&root
->fs_info
->alloc_mutex
);
62 mutex_lock(&root
->fs_info
->chunk_mutex
);
65 static void unlock_chunks(struct btrfs_root
*root
)
67 mutex_unlock(&root
->fs_info
->alloc_mutex
);
68 mutex_unlock(&root
->fs_info
->chunk_mutex
);
71 int btrfs_cleanup_fs_uuids(void)
73 struct btrfs_fs_devices
*fs_devices
;
74 struct list_head
*uuid_cur
;
75 struct list_head
*devices_cur
;
76 struct btrfs_device
*dev
;
78 list_for_each(uuid_cur
, &fs_uuids
) {
79 fs_devices
= list_entry(uuid_cur
, struct btrfs_fs_devices
,
81 while(!list_empty(&fs_devices
->devices
)) {
82 devices_cur
= fs_devices
->devices
.next
;
83 dev
= list_entry(devices_cur
, struct btrfs_device
,
86 close_bdev_excl(dev
->bdev
);
87 fs_devices
->open_devices
--;
89 list_del(&dev
->dev_list
);
97 static struct btrfs_device
*__find_device(struct list_head
*head
, u64 devid
,
100 struct btrfs_device
*dev
;
101 struct list_head
*cur
;
103 list_for_each(cur
, head
) {
104 dev
= list_entry(cur
, struct btrfs_device
, dev_list
);
105 if (dev
->devid
== devid
&&
106 (!uuid
|| !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
))) {
113 static struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
115 struct list_head
*cur
;
116 struct btrfs_fs_devices
*fs_devices
;
118 list_for_each(cur
, &fs_uuids
) {
119 fs_devices
= list_entry(cur
, struct btrfs_fs_devices
, list
);
120 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
127 * we try to collect pending bios for a device so we don't get a large
128 * number of procs sending bios down to the same device. This greatly
129 * improves the schedulers ability to collect and merge the bios.
131 * But, it also turns into a long list of bios to process and that is sure
132 * to eventually make the worker thread block. The solution here is to
133 * make some progress and then put this work struct back at the end of
134 * the list if the block device is congested. This way, multiple devices
135 * can make progress from a single worker thread.
137 int run_scheduled_bios(struct btrfs_device
*device
)
140 struct backing_dev_info
*bdi
;
144 unsigned long num_run
= 0;
146 bdi
= device
->bdev
->bd_inode
->i_mapping
->backing_dev_info
;
148 spin_lock(&device
->io_lock
);
150 /* take all the bios off the list at once and process them
151 * later on (without the lock held). But, remember the
152 * tail and other pointers so the bios can be properly reinserted
153 * into the list if we hit congestion
155 pending
= device
->pending_bios
;
156 tail
= device
->pending_bio_tail
;
157 WARN_ON(pending
&& !tail
);
158 device
->pending_bios
= NULL
;
159 device
->pending_bio_tail
= NULL
;
162 * if pending was null this time around, no bios need processing
163 * at all and we can stop. Otherwise it'll loop back up again
164 * and do an additional check so no bios are missed.
166 * device->running_pending is used to synchronize with the
171 device
->running_pending
= 1;
174 device
->running_pending
= 0;
176 spin_unlock(&device
->io_lock
);
180 pending
= pending
->bi_next
;
182 atomic_dec(&device
->dev_root
->fs_info
->nr_async_submits
);
183 submit_bio(cur
->bi_rw
, cur
);
187 * we made progress, there is more work to do and the bdi
188 * is now congested. Back off and let other work structs
191 if (pending
&& num_run
&& bdi_write_congested(bdi
)) {
192 struct bio
*old_head
;
194 spin_lock(&device
->io_lock
);
195 old_head
= device
->pending_bios
;
196 device
->pending_bios
= pending
;
197 if (device
->pending_bio_tail
)
198 tail
->bi_next
= old_head
;
200 device
->pending_bio_tail
= tail
;
202 spin_unlock(&device
->io_lock
);
203 btrfs_requeue_work(&device
->work
);
213 void pending_bios_fn(struct btrfs_work
*work
)
215 struct btrfs_device
*device
;
217 device
= container_of(work
, struct btrfs_device
, work
);
218 run_scheduled_bios(device
);
221 static int device_list_add(const char *path
,
222 struct btrfs_super_block
*disk_super
,
223 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
225 struct btrfs_device
*device
;
226 struct btrfs_fs_devices
*fs_devices
;
227 u64 found_transid
= btrfs_super_generation(disk_super
);
229 fs_devices
= find_fsid(disk_super
->fsid
);
231 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
234 INIT_LIST_HEAD(&fs_devices
->devices
);
235 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
236 list_add(&fs_devices
->list
, &fs_uuids
);
237 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
238 fs_devices
->latest_devid
= devid
;
239 fs_devices
->latest_trans
= found_transid
;
242 device
= __find_device(&fs_devices
->devices
, devid
,
243 disk_super
->dev_item
.uuid
);
246 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
248 /* we can safely leave the fs_devices entry around */
251 device
->devid
= devid
;
252 device
->work
.func
= pending_bios_fn
;
253 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
255 device
->barriers
= 1;
256 spin_lock_init(&device
->io_lock
);
257 device
->name
= kstrdup(path
, GFP_NOFS
);
262 list_add(&device
->dev_list
, &fs_devices
->devices
);
263 list_add(&device
->dev_alloc_list
, &fs_devices
->alloc_list
);
264 fs_devices
->num_devices
++;
267 if (found_transid
> fs_devices
->latest_trans
) {
268 fs_devices
->latest_devid
= devid
;
269 fs_devices
->latest_trans
= found_transid
;
271 *fs_devices_ret
= fs_devices
;
275 int btrfs_close_extra_devices(struct btrfs_fs_devices
*fs_devices
)
277 struct list_head
*head
= &fs_devices
->devices
;
278 struct list_head
*cur
;
279 struct btrfs_device
*device
;
281 mutex_lock(&uuid_mutex
);
283 list_for_each(cur
, head
) {
284 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
285 if (!device
->in_fs_metadata
) {
286 struct block_device
*bdev
;
287 list_del(&device
->dev_list
);
288 list_del(&device
->dev_alloc_list
);
289 fs_devices
->num_devices
--;
292 fs_devices
->open_devices
--;
293 mutex_unlock(&uuid_mutex
);
294 close_bdev_excl(bdev
);
295 mutex_lock(&uuid_mutex
);
302 mutex_unlock(&uuid_mutex
);
306 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
308 struct list_head
*head
= &fs_devices
->devices
;
309 struct list_head
*cur
;
310 struct btrfs_device
*device
;
312 mutex_lock(&uuid_mutex
);
313 list_for_each(cur
, head
) {
314 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
316 close_bdev_excl(device
->bdev
);
317 fs_devices
->open_devices
--;
320 device
->in_fs_metadata
= 0;
322 fs_devices
->mounted
= 0;
323 mutex_unlock(&uuid_mutex
);
327 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
328 int flags
, void *holder
)
330 struct block_device
*bdev
;
331 struct list_head
*head
= &fs_devices
->devices
;
332 struct list_head
*cur
;
333 struct btrfs_device
*device
;
334 struct block_device
*latest_bdev
= NULL
;
335 struct buffer_head
*bh
;
336 struct btrfs_super_block
*disk_super
;
337 u64 latest_devid
= 0;
338 u64 latest_transid
= 0;
343 mutex_lock(&uuid_mutex
);
344 if (fs_devices
->mounted
)
347 list_for_each(cur
, head
) {
348 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
355 bdev
= open_bdev_excl(device
->name
, flags
, holder
);
358 printk("open %s failed\n", device
->name
);
361 set_blocksize(bdev
, 4096);
363 bh
= __bread(bdev
, BTRFS_SUPER_INFO_OFFSET
/ 4096, 4096);
367 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
368 if (strncmp((char *)(&disk_super
->magic
), BTRFS_MAGIC
,
369 sizeof(disk_super
->magic
)))
372 devid
= le64_to_cpu(disk_super
->dev_item
.devid
);
373 if (devid
!= device
->devid
)
376 transid
= btrfs_super_generation(disk_super
);
377 if (!latest_transid
|| transid
> latest_transid
) {
378 latest_devid
= devid
;
379 latest_transid
= transid
;
384 device
->in_fs_metadata
= 0;
385 fs_devices
->open_devices
++;
391 close_bdev_excl(bdev
);
395 if (fs_devices
->open_devices
== 0) {
399 fs_devices
->mounted
= 1;
400 fs_devices
->latest_bdev
= latest_bdev
;
401 fs_devices
->latest_devid
= latest_devid
;
402 fs_devices
->latest_trans
= latest_transid
;
404 mutex_unlock(&uuid_mutex
);
408 int btrfs_scan_one_device(const char *path
, int flags
, void *holder
,
409 struct btrfs_fs_devices
**fs_devices_ret
)
411 struct btrfs_super_block
*disk_super
;
412 struct block_device
*bdev
;
413 struct buffer_head
*bh
;
418 mutex_lock(&uuid_mutex
);
420 bdev
= open_bdev_excl(path
, flags
, holder
);
427 ret
= set_blocksize(bdev
, 4096);
430 bh
= __bread(bdev
, BTRFS_SUPER_INFO_OFFSET
/ 4096, 4096);
435 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
436 if (strncmp((char *)(&disk_super
->magic
), BTRFS_MAGIC
,
437 sizeof(disk_super
->magic
))) {
441 devid
= le64_to_cpu(disk_super
->dev_item
.devid
);
442 transid
= btrfs_super_generation(disk_super
);
443 if (disk_super
->label
[0])
444 printk("device label %s ", disk_super
->label
);
446 /* FIXME, make a readl uuid parser */
447 printk("device fsid %llx-%llx ",
448 *(unsigned long long *)disk_super
->fsid
,
449 *(unsigned long long *)(disk_super
->fsid
+ 8));
451 printk("devid %Lu transid %Lu %s\n", devid
, transid
, path
);
452 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
457 close_bdev_excl(bdev
);
459 mutex_unlock(&uuid_mutex
);
464 * this uses a pretty simple search, the expectation is that it is
465 * called very infrequently and that a given device has a small number
468 static int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
469 struct btrfs_device
*device
,
470 struct btrfs_path
*path
,
471 u64 num_bytes
, u64
*start
)
473 struct btrfs_key key
;
474 struct btrfs_root
*root
= device
->dev_root
;
475 struct btrfs_dev_extent
*dev_extent
= NULL
;
478 u64 search_start
= 0;
479 u64 search_end
= device
->total_bytes
;
483 struct extent_buffer
*l
;
488 /* FIXME use last free of some kind */
490 /* we don't want to overwrite the superblock on the drive,
491 * so we make sure to start at an offset of at least 1MB
493 search_start
= max((u64
)1024 * 1024, search_start
);
495 if (root
->fs_info
->alloc_start
+ num_bytes
<= device
->total_bytes
)
496 search_start
= max(root
->fs_info
->alloc_start
, search_start
);
498 key
.objectid
= device
->devid
;
499 key
.offset
= search_start
;
500 key
.type
= BTRFS_DEV_EXTENT_KEY
;
501 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
504 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
508 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
511 slot
= path
->slots
[0];
512 if (slot
>= btrfs_header_nritems(l
)) {
513 ret
= btrfs_next_leaf(root
, path
);
520 if (search_start
>= search_end
) {
524 *start
= search_start
;
528 *start
= last_byte
> search_start
?
529 last_byte
: search_start
;
530 if (search_end
<= *start
) {
536 btrfs_item_key_to_cpu(l
, &key
, slot
);
538 if (key
.objectid
< device
->devid
)
541 if (key
.objectid
> device
->devid
)
544 if (key
.offset
>= search_start
&& key
.offset
> last_byte
&&
546 if (last_byte
< search_start
)
547 last_byte
= search_start
;
548 hole_size
= key
.offset
- last_byte
;
549 if (key
.offset
> last_byte
&&
550 hole_size
>= num_bytes
) {
555 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
) {
560 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
561 last_byte
= key
.offset
+ btrfs_dev_extent_length(l
, dev_extent
);
567 /* we have to make sure we didn't find an extent that has already
568 * been allocated by the map tree or the original allocation
570 btrfs_release_path(root
, path
);
571 BUG_ON(*start
< search_start
);
573 if (*start
+ num_bytes
> search_end
) {
577 /* check for pending inserts here */
581 btrfs_release_path(root
, path
);
585 int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
586 struct btrfs_device
*device
,
590 struct btrfs_path
*path
;
591 struct btrfs_root
*root
= device
->dev_root
;
592 struct btrfs_key key
;
593 struct btrfs_key found_key
;
594 struct extent_buffer
*leaf
= NULL
;
595 struct btrfs_dev_extent
*extent
= NULL
;
597 path
= btrfs_alloc_path();
601 key
.objectid
= device
->devid
;
603 key
.type
= BTRFS_DEV_EXTENT_KEY
;
605 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
607 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
608 BTRFS_DEV_EXTENT_KEY
);
610 leaf
= path
->nodes
[0];
611 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
612 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
613 struct btrfs_dev_extent
);
614 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
615 btrfs_dev_extent_length(leaf
, extent
) < start
);
617 } else if (ret
== 0) {
618 leaf
= path
->nodes
[0];
619 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
620 struct btrfs_dev_extent
);
624 if (device
->bytes_used
> 0)
625 device
->bytes_used
-= btrfs_dev_extent_length(leaf
, extent
);
626 ret
= btrfs_del_item(trans
, root
, path
);
629 btrfs_free_path(path
);
633 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
634 struct btrfs_device
*device
,
635 u64 chunk_tree
, u64 chunk_objectid
,
637 u64 num_bytes
, u64
*start
)
640 struct btrfs_path
*path
;
641 struct btrfs_root
*root
= device
->dev_root
;
642 struct btrfs_dev_extent
*extent
;
643 struct extent_buffer
*leaf
;
644 struct btrfs_key key
;
646 WARN_ON(!device
->in_fs_metadata
);
647 path
= btrfs_alloc_path();
651 ret
= find_free_dev_extent(trans
, device
, path
, num_bytes
, start
);
656 key
.objectid
= device
->devid
;
658 key
.type
= BTRFS_DEV_EXTENT_KEY
;
659 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
663 leaf
= path
->nodes
[0];
664 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
665 struct btrfs_dev_extent
);
666 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
667 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
668 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
670 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
671 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
674 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
675 btrfs_mark_buffer_dirty(leaf
);
677 btrfs_free_path(path
);
681 static int find_next_chunk(struct btrfs_root
*root
, u64 objectid
, u64
*offset
)
683 struct btrfs_path
*path
;
685 struct btrfs_key key
;
686 struct btrfs_chunk
*chunk
;
687 struct btrfs_key found_key
;
689 path
= btrfs_alloc_path();
692 key
.objectid
= objectid
;
693 key
.offset
= (u64
)-1;
694 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
696 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
702 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
706 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
708 if (found_key
.objectid
!= objectid
)
711 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
713 *offset
= found_key
.offset
+
714 btrfs_chunk_length(path
->nodes
[0], chunk
);
719 btrfs_free_path(path
);
723 static int find_next_devid(struct btrfs_root
*root
, struct btrfs_path
*path
,
727 struct btrfs_key key
;
728 struct btrfs_key found_key
;
730 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
731 key
.type
= BTRFS_DEV_ITEM_KEY
;
732 key
.offset
= (u64
)-1;
734 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
740 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
745 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
747 *objectid
= found_key
.offset
+ 1;
751 btrfs_release_path(root
, path
);
756 * the device information is stored in the chunk root
757 * the btrfs_device struct should be fully filled in
759 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
760 struct btrfs_root
*root
,
761 struct btrfs_device
*device
)
764 struct btrfs_path
*path
;
765 struct btrfs_dev_item
*dev_item
;
766 struct extent_buffer
*leaf
;
767 struct btrfs_key key
;
771 root
= root
->fs_info
->chunk_root
;
773 path
= btrfs_alloc_path();
777 ret
= find_next_devid(root
, path
, &free_devid
);
781 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
782 key
.type
= BTRFS_DEV_ITEM_KEY
;
783 key
.offset
= free_devid
;
785 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
790 leaf
= path
->nodes
[0];
791 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
793 device
->devid
= free_devid
;
794 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
795 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
796 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
797 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
798 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
799 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
800 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
801 btrfs_set_device_group(leaf
, dev_item
, 0);
802 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
803 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
805 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
806 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
807 btrfs_mark_buffer_dirty(leaf
);
811 btrfs_free_path(path
);
815 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
816 struct btrfs_device
*device
)
819 struct btrfs_path
*path
;
820 struct block_device
*bdev
= device
->bdev
;
821 struct btrfs_device
*next_dev
;
822 struct btrfs_key key
;
824 struct btrfs_fs_devices
*fs_devices
;
825 struct btrfs_trans_handle
*trans
;
827 root
= root
->fs_info
->chunk_root
;
829 path
= btrfs_alloc_path();
833 trans
= btrfs_start_transaction(root
, 1);
834 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
835 key
.type
= BTRFS_DEV_ITEM_KEY
;
836 key
.offset
= device
->devid
;
839 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
848 ret
= btrfs_del_item(trans
, root
, path
);
853 * at this point, the device is zero sized. We want to
854 * remove it from the devices list and zero out the old super
856 list_del_init(&device
->dev_list
);
857 list_del_init(&device
->dev_alloc_list
);
858 fs_devices
= root
->fs_info
->fs_devices
;
860 next_dev
= list_entry(fs_devices
->devices
.next
, struct btrfs_device
,
862 if (bdev
== root
->fs_info
->sb
->s_bdev
)
863 root
->fs_info
->sb
->s_bdev
= next_dev
->bdev
;
864 if (bdev
== fs_devices
->latest_bdev
)
865 fs_devices
->latest_bdev
= next_dev
->bdev
;
867 total_bytes
= btrfs_super_num_devices(&root
->fs_info
->super_copy
);
868 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
,
871 btrfs_free_path(path
);
873 btrfs_commit_transaction(trans
, root
);
877 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
879 struct btrfs_device
*device
;
880 struct block_device
*bdev
;
881 struct buffer_head
*bh
= NULL
;
882 struct btrfs_super_block
*disk_super
;
887 mutex_lock(&uuid_mutex
);
888 mutex_lock(&root
->fs_info
->volume_mutex
);
890 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
891 root
->fs_info
->avail_system_alloc_bits
|
892 root
->fs_info
->avail_metadata_alloc_bits
;
894 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) &&
895 btrfs_super_num_devices(&root
->fs_info
->super_copy
) <= 4) {
896 printk("btrfs: unable to go below four devices on raid10\n");
901 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) &&
902 btrfs_super_num_devices(&root
->fs_info
->super_copy
) <= 2) {
903 printk("btrfs: unable to go below two devices on raid1\n");
908 if (strcmp(device_path
, "missing") == 0) {
909 struct list_head
*cur
;
910 struct list_head
*devices
;
911 struct btrfs_device
*tmp
;
914 devices
= &root
->fs_info
->fs_devices
->devices
;
915 list_for_each(cur
, devices
) {
916 tmp
= list_entry(cur
, struct btrfs_device
, dev_list
);
917 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
926 printk("btrfs: no missing devices found to remove\n");
931 bdev
= open_bdev_excl(device_path
, 0,
932 root
->fs_info
->bdev_holder
);
938 bh
= __bread(bdev
, BTRFS_SUPER_INFO_OFFSET
/ 4096, 4096);
943 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
944 if (strncmp((char *)(&disk_super
->magic
), BTRFS_MAGIC
,
945 sizeof(disk_super
->magic
))) {
949 if (memcmp(disk_super
->fsid
, root
->fs_info
->fsid
,
954 devid
= le64_to_cpu(disk_super
->dev_item
.devid
);
955 device
= btrfs_find_device(root
, devid
, NULL
);
962 root
->fs_info
->fs_devices
->num_devices
--;
963 root
->fs_info
->fs_devices
->open_devices
--;
965 ret
= btrfs_shrink_device(device
, 0);
970 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
975 /* make sure this device isn't detected as part of
978 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
979 set_buffer_dirty(bh
);
980 sync_dirty_buffer(bh
);
986 /* one close for the device struct or super_block */
987 close_bdev_excl(device
->bdev
);
990 /* one close for us */
991 close_bdev_excl(bdev
);
1002 close_bdev_excl(bdev
);
1004 mutex_unlock(&root
->fs_info
->volume_mutex
);
1005 mutex_unlock(&uuid_mutex
);
1009 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
1011 struct btrfs_trans_handle
*trans
;
1012 struct btrfs_device
*device
;
1013 struct block_device
*bdev
;
1014 struct list_head
*cur
;
1015 struct list_head
*devices
;
1020 bdev
= open_bdev_excl(device_path
, 0, root
->fs_info
->bdev_holder
);
1025 mutex_lock(&root
->fs_info
->volume_mutex
);
1027 trans
= btrfs_start_transaction(root
, 1);
1029 devices
= &root
->fs_info
->fs_devices
->devices
;
1030 list_for_each(cur
, devices
) {
1031 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
1032 if (device
->bdev
== bdev
) {
1038 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1040 /* we can safely leave the fs_devices entry around */
1042 goto out_close_bdev
;
1045 device
->barriers
= 1;
1046 device
->work
.func
= pending_bios_fn
;
1047 generate_random_uuid(device
->uuid
);
1048 spin_lock_init(&device
->io_lock
);
1049 device
->name
= kstrdup(device_path
, GFP_NOFS
);
1050 if (!device
->name
) {
1052 goto out_close_bdev
;
1054 device
->io_width
= root
->sectorsize
;
1055 device
->io_align
= root
->sectorsize
;
1056 device
->sector_size
= root
->sectorsize
;
1057 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1058 device
->dev_root
= root
->fs_info
->dev_root
;
1059 device
->bdev
= bdev
;
1060 device
->in_fs_metadata
= 1;
1062 ret
= btrfs_add_device(trans
, root
, device
);
1064 goto out_close_bdev
;
1066 total_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
1067 btrfs_set_super_total_bytes(&root
->fs_info
->super_copy
,
1068 total_bytes
+ device
->total_bytes
);
1070 total_bytes
= btrfs_super_num_devices(&root
->fs_info
->super_copy
);
1071 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
,
1074 list_add(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
1075 list_add(&device
->dev_alloc_list
,
1076 &root
->fs_info
->fs_devices
->alloc_list
);
1077 root
->fs_info
->fs_devices
->num_devices
++;
1078 root
->fs_info
->fs_devices
->open_devices
++;
1080 unlock_chunks(root
);
1081 btrfs_end_transaction(trans
, root
);
1082 mutex_unlock(&root
->fs_info
->volume_mutex
);
1087 close_bdev_excl(bdev
);
1091 int btrfs_update_device(struct btrfs_trans_handle
*trans
,
1092 struct btrfs_device
*device
)
1095 struct btrfs_path
*path
;
1096 struct btrfs_root
*root
;
1097 struct btrfs_dev_item
*dev_item
;
1098 struct extent_buffer
*leaf
;
1099 struct btrfs_key key
;
1101 root
= device
->dev_root
->fs_info
->chunk_root
;
1103 path
= btrfs_alloc_path();
1107 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1108 key
.type
= BTRFS_DEV_ITEM_KEY
;
1109 key
.offset
= device
->devid
;
1111 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1120 leaf
= path
->nodes
[0];
1121 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1123 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1124 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1125 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1126 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1127 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1128 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1129 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1130 btrfs_mark_buffer_dirty(leaf
);
1133 btrfs_free_path(path
);
1137 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1138 struct btrfs_device
*device
, u64 new_size
)
1140 struct btrfs_super_block
*super_copy
=
1141 &device
->dev_root
->fs_info
->super_copy
;
1142 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1143 u64 diff
= new_size
- device
->total_bytes
;
1145 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
1146 return btrfs_update_device(trans
, device
);
1149 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1150 struct btrfs_device
*device
, u64 new_size
)
1153 lock_chunks(device
->dev_root
);
1154 ret
= __btrfs_grow_device(trans
, device
, new_size
);
1155 unlock_chunks(device
->dev_root
);
1159 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
1160 struct btrfs_root
*root
,
1161 u64 chunk_tree
, u64 chunk_objectid
,
1165 struct btrfs_path
*path
;
1166 struct btrfs_key key
;
1168 root
= root
->fs_info
->chunk_root
;
1169 path
= btrfs_alloc_path();
1173 key
.objectid
= chunk_objectid
;
1174 key
.offset
= chunk_offset
;
1175 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1177 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1180 ret
= btrfs_del_item(trans
, root
, path
);
1183 btrfs_free_path(path
);
1187 int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
1190 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1191 struct btrfs_disk_key
*disk_key
;
1192 struct btrfs_chunk
*chunk
;
1199 struct btrfs_key key
;
1201 array_size
= btrfs_super_sys_array_size(super_copy
);
1203 ptr
= super_copy
->sys_chunk_array
;
1206 while (cur
< array_size
) {
1207 disk_key
= (struct btrfs_disk_key
*)ptr
;
1208 btrfs_disk_key_to_cpu(&key
, disk_key
);
1210 len
= sizeof(*disk_key
);
1212 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1213 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
1214 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
1215 len
+= btrfs_chunk_item_size(num_stripes
);
1220 if (key
.objectid
== chunk_objectid
&&
1221 key
.offset
== chunk_offset
) {
1222 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
1224 btrfs_set_super_sys_array_size(super_copy
, array_size
);
1234 int btrfs_relocate_chunk(struct btrfs_root
*root
,
1235 u64 chunk_tree
, u64 chunk_objectid
,
1238 struct extent_map_tree
*em_tree
;
1239 struct btrfs_root
*extent_root
;
1240 struct btrfs_trans_handle
*trans
;
1241 struct extent_map
*em
;
1242 struct map_lookup
*map
;
1246 printk("btrfs relocating chunk %llu\n",
1247 (unsigned long long)chunk_offset
);
1248 root
= root
->fs_info
->chunk_root
;
1249 extent_root
= root
->fs_info
->extent_root
;
1250 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
1252 /* step one, relocate all the extents inside this chunk */
1253 ret
= btrfs_shrink_extent_tree(extent_root
, chunk_offset
);
1256 trans
= btrfs_start_transaction(root
, 1);
1262 * step two, delete the device extents and the
1263 * chunk tree entries
1265 spin_lock(&em_tree
->lock
);
1266 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
1267 spin_unlock(&em_tree
->lock
);
1269 BUG_ON(em
->start
> chunk_offset
||
1270 em
->start
+ em
->len
< chunk_offset
);
1271 map
= (struct map_lookup
*)em
->bdev
;
1273 for (i
= 0; i
< map
->num_stripes
; i
++) {
1274 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
1275 map
->stripes
[i
].physical
);
1278 if (map
->stripes
[i
].dev
) {
1279 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
1283 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
1288 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1289 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
1293 spin_lock(&em_tree
->lock
);
1294 remove_extent_mapping(em_tree
, em
);
1298 /* once for the tree */
1299 free_extent_map(em
);
1300 spin_unlock(&em_tree
->lock
);
1303 free_extent_map(em
);
1305 unlock_chunks(root
);
1306 btrfs_end_transaction(trans
, root
);
1310 static u64
div_factor(u64 num
, int factor
)
1320 int btrfs_balance(struct btrfs_root
*dev_root
)
1323 struct list_head
*cur
;
1324 struct list_head
*devices
= &dev_root
->fs_info
->fs_devices
->devices
;
1325 struct btrfs_device
*device
;
1328 struct btrfs_path
*path
;
1329 struct btrfs_key key
;
1330 struct btrfs_chunk
*chunk
;
1331 struct btrfs_root
*chunk_root
= dev_root
->fs_info
->chunk_root
;
1332 struct btrfs_trans_handle
*trans
;
1333 struct btrfs_key found_key
;
1336 mutex_lock(&dev_root
->fs_info
->volume_mutex
);
1337 dev_root
= dev_root
->fs_info
->dev_root
;
1339 /* step one make some room on all the devices */
1340 list_for_each(cur
, devices
) {
1341 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
1342 old_size
= device
->total_bytes
;
1343 size_to_free
= div_factor(old_size
, 1);
1344 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
1345 if (device
->total_bytes
- device
->bytes_used
> size_to_free
)
1348 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
1351 trans
= btrfs_start_transaction(dev_root
, 1);
1354 ret
= btrfs_grow_device(trans
, device
, old_size
);
1357 btrfs_end_transaction(trans
, dev_root
);
1360 /* step two, relocate all the chunks */
1361 path
= btrfs_alloc_path();
1364 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1365 key
.offset
= (u64
)-1;
1366 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1369 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
1374 * this shouldn't happen, it means the last relocate
1380 ret
= btrfs_previous_item(chunk_root
, path
, 0,
1381 BTRFS_CHUNK_ITEM_KEY
);
1385 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1387 if (found_key
.objectid
!= key
.objectid
)
1390 chunk
= btrfs_item_ptr(path
->nodes
[0],
1392 struct btrfs_chunk
);
1393 key
.offset
= found_key
.offset
;
1394 /* chunk zero is special */
1395 if (key
.offset
== 0)
1398 btrfs_release_path(chunk_root
, path
);
1399 ret
= btrfs_relocate_chunk(chunk_root
,
1400 chunk_root
->root_key
.objectid
,
1407 btrfs_free_path(path
);
1408 mutex_unlock(&dev_root
->fs_info
->volume_mutex
);
1413 * shrinking a device means finding all of the device extents past
1414 * the new size, and then following the back refs to the chunks.
1415 * The chunk relocation code actually frees the device extent
1417 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
1419 struct btrfs_trans_handle
*trans
;
1420 struct btrfs_root
*root
= device
->dev_root
;
1421 struct btrfs_dev_extent
*dev_extent
= NULL
;
1422 struct btrfs_path
*path
;
1429 struct extent_buffer
*l
;
1430 struct btrfs_key key
;
1431 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1432 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1433 u64 diff
= device
->total_bytes
- new_size
;
1436 path
= btrfs_alloc_path();
1440 trans
= btrfs_start_transaction(root
, 1);
1450 device
->total_bytes
= new_size
;
1451 ret
= btrfs_update_device(trans
, device
);
1453 unlock_chunks(root
);
1454 btrfs_end_transaction(trans
, root
);
1457 WARN_ON(diff
> old_total
);
1458 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
1459 unlock_chunks(root
);
1460 btrfs_end_transaction(trans
, root
);
1462 key
.objectid
= device
->devid
;
1463 key
.offset
= (u64
)-1;
1464 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1467 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1471 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
1480 slot
= path
->slots
[0];
1481 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
1483 if (key
.objectid
!= device
->devid
)
1486 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1487 length
= btrfs_dev_extent_length(l
, dev_extent
);
1489 if (key
.offset
+ length
<= new_size
)
1492 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
1493 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
1494 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
1495 btrfs_release_path(root
, path
);
1497 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
1504 btrfs_free_path(path
);
1508 int btrfs_add_system_chunk(struct btrfs_trans_handle
*trans
,
1509 struct btrfs_root
*root
,
1510 struct btrfs_key
*key
,
1511 struct btrfs_chunk
*chunk
, int item_size
)
1513 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1514 struct btrfs_disk_key disk_key
;
1518 array_size
= btrfs_super_sys_array_size(super_copy
);
1519 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
1522 ptr
= super_copy
->sys_chunk_array
+ array_size
;
1523 btrfs_cpu_key_to_disk(&disk_key
, key
);
1524 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
1525 ptr
+= sizeof(disk_key
);
1526 memcpy(ptr
, chunk
, item_size
);
1527 item_size
+= sizeof(disk_key
);
1528 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
1532 static u64
chunk_bytes_by_type(u64 type
, u64 calc_size
, int num_stripes
,
1535 if (type
& (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_DUP
))
1537 else if (type
& BTRFS_BLOCK_GROUP_RAID10
)
1538 return calc_size
* (num_stripes
/ sub_stripes
);
1540 return calc_size
* num_stripes
;
1544 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
1545 struct btrfs_root
*extent_root
, u64
*start
,
1546 u64
*num_bytes
, u64 type
)
1549 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
1550 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
1551 struct btrfs_path
*path
;
1552 struct btrfs_stripe
*stripes
;
1553 struct btrfs_device
*device
= NULL
;
1554 struct btrfs_chunk
*chunk
;
1555 struct list_head private_devs
;
1556 struct list_head
*dev_list
;
1557 struct list_head
*cur
;
1558 struct extent_map_tree
*em_tree
;
1559 struct map_lookup
*map
;
1560 struct extent_map
*em
;
1561 int min_stripe_size
= 1 * 1024 * 1024;
1563 u64 calc_size
= 1024 * 1024 * 1024;
1564 u64 max_chunk_size
= calc_size
;
1569 int num_stripes
= 1;
1570 int min_stripes
= 1;
1571 int sub_stripes
= 0;
1575 int stripe_len
= 64 * 1024;
1576 struct btrfs_key key
;
1578 if ((type
& BTRFS_BLOCK_GROUP_RAID1
) &&
1579 (type
& BTRFS_BLOCK_GROUP_DUP
)) {
1581 type
&= ~BTRFS_BLOCK_GROUP_DUP
;
1583 dev_list
= &extent_root
->fs_info
->fs_devices
->alloc_list
;
1584 if (list_empty(dev_list
))
1587 if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
1588 num_stripes
= extent_root
->fs_info
->fs_devices
->open_devices
;
1591 if (type
& (BTRFS_BLOCK_GROUP_DUP
)) {
1595 if (type
& (BTRFS_BLOCK_GROUP_RAID1
)) {
1596 num_stripes
= min_t(u64
, 2,
1597 extent_root
->fs_info
->fs_devices
->open_devices
);
1598 if (num_stripes
< 2)
1602 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
1603 num_stripes
= extent_root
->fs_info
->fs_devices
->open_devices
;
1604 if (num_stripes
< 4)
1606 num_stripes
&= ~(u32
)1;
1611 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
1612 max_chunk_size
= 10 * calc_size
;
1613 min_stripe_size
= 64 * 1024 * 1024;
1614 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
1615 max_chunk_size
= 4 * calc_size
;
1616 min_stripe_size
= 32 * 1024 * 1024;
1617 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1618 calc_size
= 8 * 1024 * 1024;
1619 max_chunk_size
= calc_size
* 2;
1620 min_stripe_size
= 1 * 1024 * 1024;
1623 path
= btrfs_alloc_path();
1627 /* we don't want a chunk larger than 10% of the FS */
1628 percent_max
= div_factor(btrfs_super_total_bytes(&info
->super_copy
), 1);
1629 max_chunk_size
= min(percent_max
, max_chunk_size
);
1632 if (calc_size
* num_stripes
> max_chunk_size
) {
1633 calc_size
= max_chunk_size
;
1634 do_div(calc_size
, num_stripes
);
1635 do_div(calc_size
, stripe_len
);
1636 calc_size
*= stripe_len
;
1638 /* we don't want tiny stripes */
1639 calc_size
= max_t(u64
, min_stripe_size
, calc_size
);
1641 do_div(calc_size
, stripe_len
);
1642 calc_size
*= stripe_len
;
1644 INIT_LIST_HEAD(&private_devs
);
1645 cur
= dev_list
->next
;
1648 if (type
& BTRFS_BLOCK_GROUP_DUP
)
1649 min_free
= calc_size
* 2;
1651 min_free
= calc_size
;
1653 /* we add 1MB because we never use the first 1MB of the device */
1654 min_free
+= 1024 * 1024;
1656 /* build a private list of devices we will allocate from */
1657 while(index
< num_stripes
) {
1658 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
1660 if (device
->total_bytes
> device
->bytes_used
)
1661 avail
= device
->total_bytes
- device
->bytes_used
;
1666 if (device
->in_fs_metadata
&& avail
>= min_free
) {
1667 u64 ignored_start
= 0;
1668 ret
= find_free_dev_extent(trans
, device
, path
,
1672 list_move_tail(&device
->dev_alloc_list
,
1675 if (type
& BTRFS_BLOCK_GROUP_DUP
)
1678 } else if (device
->in_fs_metadata
&& avail
> max_avail
)
1680 if (cur
== dev_list
)
1683 if (index
< num_stripes
) {
1684 list_splice(&private_devs
, dev_list
);
1685 if (index
>= min_stripes
) {
1686 num_stripes
= index
;
1687 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
1688 num_stripes
/= sub_stripes
;
1689 num_stripes
*= sub_stripes
;
1694 if (!looped
&& max_avail
> 0) {
1696 calc_size
= max_avail
;
1699 btrfs_free_path(path
);
1702 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1703 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1704 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
1707 btrfs_free_path(path
);
1711 chunk
= kmalloc(btrfs_chunk_item_size(num_stripes
), GFP_NOFS
);
1713 btrfs_free_path(path
);
1717 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
1720 btrfs_free_path(path
);
1723 btrfs_free_path(path
);
1726 stripes
= &chunk
->stripe
;
1727 *num_bytes
= chunk_bytes_by_type(type
, calc_size
,
1728 num_stripes
, sub_stripes
);
1731 while(index
< num_stripes
) {
1732 struct btrfs_stripe
*stripe
;
1733 BUG_ON(list_empty(&private_devs
));
1734 cur
= private_devs
.next
;
1735 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
1737 /* loop over this device again if we're doing a dup group */
1738 if (!(type
& BTRFS_BLOCK_GROUP_DUP
) ||
1739 (index
== num_stripes
- 1))
1740 list_move_tail(&device
->dev_alloc_list
, dev_list
);
1742 ret
= btrfs_alloc_dev_extent(trans
, device
,
1743 info
->chunk_root
->root_key
.objectid
,
1744 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, key
.offset
,
1745 calc_size
, &dev_offset
);
1747 device
->bytes_used
+= calc_size
;
1748 ret
= btrfs_update_device(trans
, device
);
1751 map
->stripes
[index
].dev
= device
;
1752 map
->stripes
[index
].physical
= dev_offset
;
1753 stripe
= stripes
+ index
;
1754 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
1755 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
1756 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
1757 physical
= dev_offset
;
1760 BUG_ON(!list_empty(&private_devs
));
1762 /* key was set above */
1763 btrfs_set_stack_chunk_length(chunk
, *num_bytes
);
1764 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
1765 btrfs_set_stack_chunk_stripe_len(chunk
, stripe_len
);
1766 btrfs_set_stack_chunk_type(chunk
, type
);
1767 btrfs_set_stack_chunk_num_stripes(chunk
, num_stripes
);
1768 btrfs_set_stack_chunk_io_align(chunk
, stripe_len
);
1769 btrfs_set_stack_chunk_io_width(chunk
, stripe_len
);
1770 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
1771 btrfs_set_stack_chunk_sub_stripes(chunk
, sub_stripes
);
1772 map
->sector_size
= extent_root
->sectorsize
;
1773 map
->stripe_len
= stripe_len
;
1774 map
->io_align
= stripe_len
;
1775 map
->io_width
= stripe_len
;
1777 map
->num_stripes
= num_stripes
;
1778 map
->sub_stripes
= sub_stripes
;
1780 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
,
1781 btrfs_chunk_item_size(num_stripes
));
1783 *start
= key
.offset
;;
1785 em
= alloc_extent_map(GFP_NOFS
);
1788 em
->bdev
= (struct block_device
*)map
;
1789 em
->start
= key
.offset
;
1790 em
->len
= *num_bytes
;
1791 em
->block_start
= 0;
1793 if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1794 ret
= btrfs_add_system_chunk(trans
, chunk_root
, &key
,
1795 chunk
, btrfs_chunk_item_size(num_stripes
));
1800 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
1801 spin_lock(&em_tree
->lock
);
1802 ret
= add_extent_mapping(em_tree
, em
);
1803 spin_unlock(&em_tree
->lock
);
1805 free_extent_map(em
);
1809 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
1811 extent_map_tree_init(&tree
->map_tree
, GFP_NOFS
);
1814 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
1816 struct extent_map
*em
;
1819 spin_lock(&tree
->map_tree
.lock
);
1820 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
1822 remove_extent_mapping(&tree
->map_tree
, em
);
1823 spin_unlock(&tree
->map_tree
.lock
);
1828 free_extent_map(em
);
1829 /* once for the tree */
1830 free_extent_map(em
);
1834 int btrfs_num_copies(struct btrfs_mapping_tree
*map_tree
, u64 logical
, u64 len
)
1836 struct extent_map
*em
;
1837 struct map_lookup
*map
;
1838 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
1841 spin_lock(&em_tree
->lock
);
1842 em
= lookup_extent_mapping(em_tree
, logical
, len
);
1843 spin_unlock(&em_tree
->lock
);
1846 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
1847 map
= (struct map_lookup
*)em
->bdev
;
1848 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
1849 ret
= map
->num_stripes
;
1850 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
1851 ret
= map
->sub_stripes
;
1854 free_extent_map(em
);
1858 static int find_live_mirror(struct map_lookup
*map
, int first
, int num
,
1862 if (map
->stripes
[optimal
].dev
->bdev
)
1864 for (i
= first
; i
< first
+ num
; i
++) {
1865 if (map
->stripes
[i
].dev
->bdev
)
1868 /* we couldn't find one that doesn't fail. Just return something
1869 * and the io error handling code will clean up eventually
1874 static int __btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
1875 u64 logical
, u64
*length
,
1876 struct btrfs_multi_bio
**multi_ret
,
1877 int mirror_num
, struct page
*unplug_page
)
1879 struct extent_map
*em
;
1880 struct map_lookup
*map
;
1881 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
1885 int stripes_allocated
= 8;
1886 int stripes_required
= 1;
1891 struct btrfs_multi_bio
*multi
= NULL
;
1893 if (multi_ret
&& !(rw
& (1 << BIO_RW
))) {
1894 stripes_allocated
= 1;
1898 multi
= kzalloc(btrfs_multi_bio_size(stripes_allocated
),
1903 atomic_set(&multi
->error
, 0);
1906 spin_lock(&em_tree
->lock
);
1907 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
1908 spin_unlock(&em_tree
->lock
);
1910 if (!em
&& unplug_page
)
1914 printk("unable to find logical %Lu len %Lu\n", logical
, *length
);
1918 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
1919 map
= (struct map_lookup
*)em
->bdev
;
1920 offset
= logical
- em
->start
;
1922 if (mirror_num
> map
->num_stripes
)
1925 /* if our multi bio struct is too small, back off and try again */
1926 if (rw
& (1 << BIO_RW
)) {
1927 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
1928 BTRFS_BLOCK_GROUP_DUP
)) {
1929 stripes_required
= map
->num_stripes
;
1931 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
1932 stripes_required
= map
->sub_stripes
;
1936 if (multi_ret
&& rw
== WRITE
&&
1937 stripes_allocated
< stripes_required
) {
1938 stripes_allocated
= map
->num_stripes
;
1939 free_extent_map(em
);
1945 * stripe_nr counts the total number of stripes we have to stride
1946 * to get to this block
1948 do_div(stripe_nr
, map
->stripe_len
);
1950 stripe_offset
= stripe_nr
* map
->stripe_len
;
1951 BUG_ON(offset
< stripe_offset
);
1953 /* stripe_offset is the offset of this block in its stripe*/
1954 stripe_offset
= offset
- stripe_offset
;
1956 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
1957 BTRFS_BLOCK_GROUP_RAID10
|
1958 BTRFS_BLOCK_GROUP_DUP
)) {
1959 /* we limit the length of each bio to what fits in a stripe */
1960 *length
= min_t(u64
, em
->len
- offset
,
1961 map
->stripe_len
- stripe_offset
);
1963 *length
= em
->len
- offset
;
1966 if (!multi_ret
&& !unplug_page
)
1971 if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
1972 if (unplug_page
|| (rw
& (1 << BIO_RW
)))
1973 num_stripes
= map
->num_stripes
;
1974 else if (mirror_num
)
1975 stripe_index
= mirror_num
- 1;
1977 stripe_index
= find_live_mirror(map
, 0,
1979 current
->pid
% map
->num_stripes
);
1982 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
1983 if (rw
& (1 << BIO_RW
))
1984 num_stripes
= map
->num_stripes
;
1985 else if (mirror_num
)
1986 stripe_index
= mirror_num
- 1;
1988 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
1989 int factor
= map
->num_stripes
/ map
->sub_stripes
;
1991 stripe_index
= do_div(stripe_nr
, factor
);
1992 stripe_index
*= map
->sub_stripes
;
1994 if (unplug_page
|| (rw
& (1 << BIO_RW
)))
1995 num_stripes
= map
->sub_stripes
;
1996 else if (mirror_num
)
1997 stripe_index
+= mirror_num
- 1;
1999 stripe_index
= find_live_mirror(map
, stripe_index
,
2000 map
->sub_stripes
, stripe_index
+
2001 current
->pid
% map
->sub_stripes
);
2005 * after this do_div call, stripe_nr is the number of stripes
2006 * on this device we have to walk to find the data, and
2007 * stripe_index is the number of our device in the stripe array
2009 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
2011 BUG_ON(stripe_index
>= map
->num_stripes
);
2013 for (i
= 0; i
< num_stripes
; i
++) {
2015 struct btrfs_device
*device
;
2016 struct backing_dev_info
*bdi
;
2018 device
= map
->stripes
[stripe_index
].dev
;
2020 bdi
= blk_get_backing_dev_info(device
->bdev
);
2021 if (bdi
->unplug_io_fn
) {
2022 bdi
->unplug_io_fn(bdi
, unplug_page
);
2026 multi
->stripes
[i
].physical
=
2027 map
->stripes
[stripe_index
].physical
+
2028 stripe_offset
+ stripe_nr
* map
->stripe_len
;
2029 multi
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
2035 multi
->num_stripes
= num_stripes
;
2036 multi
->max_errors
= max_errors
;
2039 free_extent_map(em
);
2043 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
2044 u64 logical
, u64
*length
,
2045 struct btrfs_multi_bio
**multi_ret
, int mirror_num
)
2047 return __btrfs_map_block(map_tree
, rw
, logical
, length
, multi_ret
,
2051 int btrfs_unplug_page(struct btrfs_mapping_tree
*map_tree
,
2052 u64 logical
, struct page
*page
)
2054 u64 length
= PAGE_CACHE_SIZE
;
2055 return __btrfs_map_block(map_tree
, READ
, logical
, &length
,
2060 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2061 static void end_bio_multi_stripe(struct bio
*bio
, int err
)
2063 static int end_bio_multi_stripe(struct bio
*bio
,
2064 unsigned int bytes_done
, int err
)
2067 struct btrfs_multi_bio
*multi
= bio
->bi_private
;
2069 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2074 atomic_inc(&multi
->error
);
2076 if (atomic_dec_and_test(&multi
->stripes_pending
)) {
2077 bio
->bi_private
= multi
->private;
2078 bio
->bi_end_io
= multi
->end_io
;
2079 /* only send an error to the higher layers if it is
2080 * beyond the tolerance of the multi-bio
2082 if (atomic_read(&multi
->error
) > multi
->max_errors
) {
2086 * this bio is actually up to date, we didn't
2087 * go over the max number of errors
2089 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
2094 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2095 bio_endio(bio
, bio
->bi_size
, err
);
2097 bio_endio(bio
, err
);
2102 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2107 struct async_sched
{
2110 struct btrfs_fs_info
*info
;
2111 struct btrfs_work work
;
2115 * see run_scheduled_bios for a description of why bios are collected for
2118 * This will add one bio to the pending list for a device and make sure
2119 * the work struct is scheduled.
2121 int schedule_bio(struct btrfs_root
*root
, struct btrfs_device
*device
,
2122 int rw
, struct bio
*bio
)
2124 int should_queue
= 1;
2126 /* don't bother with additional async steps for reads, right now */
2127 if (!(rw
& (1 << BIO_RW
))) {
2128 submit_bio(rw
, bio
);
2133 * nr_async_sumbits allows us to reliably return congestion to the
2134 * higher layers. Otherwise, the async bio makes it appear we have
2135 * made progress against dirty pages when we've really just put it
2136 * on a queue for later
2138 atomic_inc(&root
->fs_info
->nr_async_submits
);
2139 bio
->bi_next
= NULL
;
2142 spin_lock(&device
->io_lock
);
2144 if (device
->pending_bio_tail
)
2145 device
->pending_bio_tail
->bi_next
= bio
;
2147 device
->pending_bio_tail
= bio
;
2148 if (!device
->pending_bios
)
2149 device
->pending_bios
= bio
;
2150 if (device
->running_pending
)
2153 spin_unlock(&device
->io_lock
);
2156 btrfs_queue_worker(&root
->fs_info
->submit_workers
,
2161 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
2162 int mirror_num
, int async_submit
)
2164 struct btrfs_mapping_tree
*map_tree
;
2165 struct btrfs_device
*dev
;
2166 struct bio
*first_bio
= bio
;
2167 u64 logical
= bio
->bi_sector
<< 9;
2170 struct btrfs_multi_bio
*multi
= NULL
;
2175 length
= bio
->bi_size
;
2176 map_tree
= &root
->fs_info
->mapping_tree
;
2177 map_length
= length
;
2179 ret
= btrfs_map_block(map_tree
, rw
, logical
, &map_length
, &multi
,
2183 total_devs
= multi
->num_stripes
;
2184 if (map_length
< length
) {
2185 printk("mapping failed logical %Lu bio len %Lu "
2186 "len %Lu\n", logical
, length
, map_length
);
2189 multi
->end_io
= first_bio
->bi_end_io
;
2190 multi
->private = first_bio
->bi_private
;
2191 atomic_set(&multi
->stripes_pending
, multi
->num_stripes
);
2193 while(dev_nr
< total_devs
) {
2194 if (total_devs
> 1) {
2195 if (dev_nr
< total_devs
- 1) {
2196 bio
= bio_clone(first_bio
, GFP_NOFS
);
2201 bio
->bi_private
= multi
;
2202 bio
->bi_end_io
= end_bio_multi_stripe
;
2204 bio
->bi_sector
= multi
->stripes
[dev_nr
].physical
>> 9;
2205 dev
= multi
->stripes
[dev_nr
].dev
;
2206 if (dev
&& dev
->bdev
) {
2207 bio
->bi_bdev
= dev
->bdev
;
2209 schedule_bio(root
, dev
, rw
, bio
);
2211 submit_bio(rw
, bio
);
2213 bio
->bi_bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2214 bio
->bi_sector
= logical
>> 9;
2215 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2216 bio_endio(bio
, bio
->bi_size
, -EIO
);
2218 bio_endio(bio
, -EIO
);
2223 if (total_devs
== 1)
2228 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
,
2231 struct list_head
*head
= &root
->fs_info
->fs_devices
->devices
;
2233 return __find_device(head
, devid
, uuid
);
2236 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
2237 u64 devid
, u8
*dev_uuid
)
2239 struct btrfs_device
*device
;
2240 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
2242 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
2243 list_add(&device
->dev_list
,
2244 &fs_devices
->devices
);
2245 list_add(&device
->dev_alloc_list
,
2246 &fs_devices
->alloc_list
);
2247 device
->barriers
= 1;
2248 device
->dev_root
= root
->fs_info
->dev_root
;
2249 device
->devid
= devid
;
2250 device
->work
.func
= pending_bios_fn
;
2251 fs_devices
->num_devices
++;
2252 spin_lock_init(&device
->io_lock
);
2253 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
2258 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
2259 struct extent_buffer
*leaf
,
2260 struct btrfs_chunk
*chunk
)
2262 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
2263 struct map_lookup
*map
;
2264 struct extent_map
*em
;
2268 u8 uuid
[BTRFS_UUID_SIZE
];
2273 logical
= key
->offset
;
2274 length
= btrfs_chunk_length(leaf
, chunk
);
2276 spin_lock(&map_tree
->map_tree
.lock
);
2277 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
2278 spin_unlock(&map_tree
->map_tree
.lock
);
2280 /* already mapped? */
2281 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
2282 free_extent_map(em
);
2285 free_extent_map(em
);
2288 map
= kzalloc(sizeof(*map
), GFP_NOFS
);
2292 em
= alloc_extent_map(GFP_NOFS
);
2295 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2296 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
2298 free_extent_map(em
);
2302 em
->bdev
= (struct block_device
*)map
;
2303 em
->start
= logical
;
2305 em
->block_start
= 0;
2307 map
->num_stripes
= num_stripes
;
2308 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
2309 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
2310 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
2311 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
2312 map
->type
= btrfs_chunk_type(leaf
, chunk
);
2313 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
2314 for (i
= 0; i
< num_stripes
; i
++) {
2315 map
->stripes
[i
].physical
=
2316 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
2317 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
2318 read_extent_buffer(leaf
, uuid
, (unsigned long)
2319 btrfs_stripe_dev_uuid_nr(chunk
, i
),
2321 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
, uuid
);
2323 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
2325 free_extent_map(em
);
2328 if (!map
->stripes
[i
].dev
) {
2329 map
->stripes
[i
].dev
=
2330 add_missing_dev(root
, devid
, uuid
);
2331 if (!map
->stripes
[i
].dev
) {
2333 free_extent_map(em
);
2337 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
2340 spin_lock(&map_tree
->map_tree
.lock
);
2341 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
2342 spin_unlock(&map_tree
->map_tree
.lock
);
2344 free_extent_map(em
);
2349 static int fill_device_from_item(struct extent_buffer
*leaf
,
2350 struct btrfs_dev_item
*dev_item
,
2351 struct btrfs_device
*device
)
2355 device
->devid
= btrfs_device_id(leaf
, dev_item
);
2356 device
->total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
2357 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
2358 device
->type
= btrfs_device_type(leaf
, dev_item
);
2359 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
2360 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
2361 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
2363 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
2364 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
2369 static int read_one_dev(struct btrfs_root
*root
,
2370 struct extent_buffer
*leaf
,
2371 struct btrfs_dev_item
*dev_item
)
2373 struct btrfs_device
*device
;
2376 u8 dev_uuid
[BTRFS_UUID_SIZE
];
2378 devid
= btrfs_device_id(leaf
, dev_item
);
2379 read_extent_buffer(leaf
, dev_uuid
,
2380 (unsigned long)btrfs_device_uuid(dev_item
),
2382 device
= btrfs_find_device(root
, devid
, dev_uuid
);
2384 printk("warning devid %Lu missing\n", devid
);
2385 device
= add_missing_dev(root
, devid
, dev_uuid
);
2390 fill_device_from_item(leaf
, dev_item
, device
);
2391 device
->dev_root
= root
->fs_info
->dev_root
;
2392 device
->in_fs_metadata
= 1;
2395 ret
= btrfs_open_device(device
);
2403 int btrfs_read_super_device(struct btrfs_root
*root
, struct extent_buffer
*buf
)
2405 struct btrfs_dev_item
*dev_item
;
2407 dev_item
= (struct btrfs_dev_item
*)offsetof(struct btrfs_super_block
,
2409 return read_one_dev(root
, buf
, dev_item
);
2412 int btrfs_read_sys_array(struct btrfs_root
*root
)
2414 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
2415 struct extent_buffer
*sb
;
2416 struct btrfs_disk_key
*disk_key
;
2417 struct btrfs_chunk
*chunk
;
2419 unsigned long sb_ptr
;
2425 struct btrfs_key key
;
2427 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
2428 BTRFS_SUPER_INFO_SIZE
);
2431 btrfs_set_buffer_uptodate(sb
);
2432 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
2433 array_size
= btrfs_super_sys_array_size(super_copy
);
2435 ptr
= super_copy
->sys_chunk_array
;
2436 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
2439 while (cur
< array_size
) {
2440 disk_key
= (struct btrfs_disk_key
*)ptr
;
2441 btrfs_disk_key_to_cpu(&key
, disk_key
);
2443 len
= sizeof(*disk_key
); ptr
+= len
;
2447 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
2448 chunk
= (struct btrfs_chunk
*)sb_ptr
;
2449 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
2452 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
2453 len
= btrfs_chunk_item_size(num_stripes
);
2462 free_extent_buffer(sb
);
2466 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
2468 struct btrfs_path
*path
;
2469 struct extent_buffer
*leaf
;
2470 struct btrfs_key key
;
2471 struct btrfs_key found_key
;
2475 root
= root
->fs_info
->chunk_root
;
2477 path
= btrfs_alloc_path();
2481 /* first we search for all of the device items, and then we
2482 * read in all of the chunk items. This way we can create chunk
2483 * mappings that reference all of the devices that are afound
2485 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
2489 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2491 leaf
= path
->nodes
[0];
2492 slot
= path
->slots
[0];
2493 if (slot
>= btrfs_header_nritems(leaf
)) {
2494 ret
= btrfs_next_leaf(root
, path
);
2501 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2502 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
2503 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
2505 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
2506 struct btrfs_dev_item
*dev_item
;
2507 dev_item
= btrfs_item_ptr(leaf
, slot
,
2508 struct btrfs_dev_item
);
2509 ret
= read_one_dev(root
, leaf
, dev_item
);
2512 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
2513 struct btrfs_chunk
*chunk
;
2514 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
2515 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
2519 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
2521 btrfs_release_path(root
, path
);
2525 btrfs_free_path(path
);