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
->chunk_mutex
);
68 mutex_unlock(&root
->fs_info
->alloc_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 noinline
struct btrfs_device
*__find_device(struct list_head
*head
,
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 noinline
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 static int noinline
run_scheduled_bios(struct btrfs_device
*device
)
140 struct backing_dev_info
*bdi
;
141 struct btrfs_fs_info
*fs_info
;
145 unsigned long num_run
= 0;
148 bdi
= device
->bdev
->bd_inode
->i_mapping
->backing_dev_info
;
149 fs_info
= device
->dev_root
->fs_info
;
150 limit
= btrfs_async_submit_limit(fs_info
);
151 limit
= limit
* 2 / 3;
154 spin_lock(&device
->io_lock
);
156 /* take all the bios off the list at once and process them
157 * later on (without the lock held). But, remember the
158 * tail and other pointers so the bios can be properly reinserted
159 * into the list if we hit congestion
161 pending
= device
->pending_bios
;
162 tail
= device
->pending_bio_tail
;
163 WARN_ON(pending
&& !tail
);
164 device
->pending_bios
= NULL
;
165 device
->pending_bio_tail
= NULL
;
168 * if pending was null this time around, no bios need processing
169 * at all and we can stop. Otherwise it'll loop back up again
170 * and do an additional check so no bios are missed.
172 * device->running_pending is used to synchronize with the
177 device
->running_pending
= 1;
180 device
->running_pending
= 0;
182 spin_unlock(&device
->io_lock
);
186 pending
= pending
->bi_next
;
188 atomic_dec(&fs_info
->nr_async_bios
);
190 if (atomic_read(&fs_info
->nr_async_bios
) < limit
&&
191 waitqueue_active(&fs_info
->async_submit_wait
))
192 wake_up(&fs_info
->async_submit_wait
);
194 BUG_ON(atomic_read(&cur
->bi_cnt
) == 0);
196 submit_bio(cur
->bi_rw
, cur
);
201 * we made progress, there is more work to do and the bdi
202 * is now congested. Back off and let other work structs
205 if (pending
&& bdi_write_congested(bdi
)) {
206 struct bio
*old_head
;
208 spin_lock(&device
->io_lock
);
210 old_head
= device
->pending_bios
;
211 device
->pending_bios
= pending
;
212 if (device
->pending_bio_tail
)
213 tail
->bi_next
= old_head
;
215 device
->pending_bio_tail
= tail
;
217 spin_unlock(&device
->io_lock
);
218 btrfs_requeue_work(&device
->work
);
228 void pending_bios_fn(struct btrfs_work
*work
)
230 struct btrfs_device
*device
;
232 device
= container_of(work
, struct btrfs_device
, work
);
233 run_scheduled_bios(device
);
236 static noinline
int device_list_add(const char *path
,
237 struct btrfs_super_block
*disk_super
,
238 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
240 struct btrfs_device
*device
;
241 struct btrfs_fs_devices
*fs_devices
;
242 u64 found_transid
= btrfs_super_generation(disk_super
);
244 fs_devices
= find_fsid(disk_super
->fsid
);
246 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
249 INIT_LIST_HEAD(&fs_devices
->devices
);
250 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
251 list_add(&fs_devices
->list
, &fs_uuids
);
252 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
253 fs_devices
->latest_devid
= devid
;
254 fs_devices
->latest_trans
= found_transid
;
257 device
= __find_device(&fs_devices
->devices
, devid
,
258 disk_super
->dev_item
.uuid
);
261 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
263 /* we can safely leave the fs_devices entry around */
266 device
->devid
= devid
;
267 device
->work
.func
= pending_bios_fn
;
268 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
270 device
->barriers
= 1;
271 spin_lock_init(&device
->io_lock
);
272 device
->name
= kstrdup(path
, GFP_NOFS
);
277 list_add(&device
->dev_list
, &fs_devices
->devices
);
278 list_add(&device
->dev_alloc_list
, &fs_devices
->alloc_list
);
279 fs_devices
->num_devices
++;
282 if (found_transid
> fs_devices
->latest_trans
) {
283 fs_devices
->latest_devid
= devid
;
284 fs_devices
->latest_trans
= found_transid
;
286 *fs_devices_ret
= fs_devices
;
290 int btrfs_close_extra_devices(struct btrfs_fs_devices
*fs_devices
)
292 struct list_head
*head
= &fs_devices
->devices
;
293 struct list_head
*cur
;
294 struct btrfs_device
*device
;
296 mutex_lock(&uuid_mutex
);
298 list_for_each(cur
, head
) {
299 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
300 if (!device
->in_fs_metadata
) {
301 struct block_device
*bdev
;
302 list_del(&device
->dev_list
);
303 list_del(&device
->dev_alloc_list
);
304 fs_devices
->num_devices
--;
307 fs_devices
->open_devices
--;
308 mutex_unlock(&uuid_mutex
);
309 close_bdev_excl(bdev
);
310 mutex_lock(&uuid_mutex
);
317 mutex_unlock(&uuid_mutex
);
321 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
323 struct list_head
*head
= &fs_devices
->devices
;
324 struct list_head
*cur
;
325 struct btrfs_device
*device
;
327 mutex_lock(&uuid_mutex
);
328 list_for_each(cur
, head
) {
329 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
331 close_bdev_excl(device
->bdev
);
332 fs_devices
->open_devices
--;
335 device
->in_fs_metadata
= 0;
337 fs_devices
->mounted
= 0;
338 mutex_unlock(&uuid_mutex
);
342 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
343 int flags
, void *holder
)
345 struct block_device
*bdev
;
346 struct list_head
*head
= &fs_devices
->devices
;
347 struct list_head
*cur
;
348 struct btrfs_device
*device
;
349 struct block_device
*latest_bdev
= NULL
;
350 struct buffer_head
*bh
;
351 struct btrfs_super_block
*disk_super
;
352 u64 latest_devid
= 0;
353 u64 latest_transid
= 0;
358 mutex_lock(&uuid_mutex
);
359 if (fs_devices
->mounted
)
362 list_for_each(cur
, head
) {
363 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
370 bdev
= open_bdev_excl(device
->name
, flags
, holder
);
373 printk("open %s failed\n", device
->name
);
376 set_blocksize(bdev
, 4096);
378 bh
= __bread(bdev
, BTRFS_SUPER_INFO_OFFSET
/ 4096, 4096);
382 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
383 if (strncmp((char *)(&disk_super
->magic
), BTRFS_MAGIC
,
384 sizeof(disk_super
->magic
)))
387 devid
= le64_to_cpu(disk_super
->dev_item
.devid
);
388 if (devid
!= device
->devid
)
391 transid
= btrfs_super_generation(disk_super
);
392 if (!latest_transid
|| transid
> latest_transid
) {
393 latest_devid
= devid
;
394 latest_transid
= transid
;
399 device
->in_fs_metadata
= 0;
400 fs_devices
->open_devices
++;
406 close_bdev_excl(bdev
);
410 if (fs_devices
->open_devices
== 0) {
414 fs_devices
->mounted
= 1;
415 fs_devices
->latest_bdev
= latest_bdev
;
416 fs_devices
->latest_devid
= latest_devid
;
417 fs_devices
->latest_trans
= latest_transid
;
419 mutex_unlock(&uuid_mutex
);
423 int btrfs_scan_one_device(const char *path
, int flags
, void *holder
,
424 struct btrfs_fs_devices
**fs_devices_ret
)
426 struct btrfs_super_block
*disk_super
;
427 struct block_device
*bdev
;
428 struct buffer_head
*bh
;
433 mutex_lock(&uuid_mutex
);
435 bdev
= open_bdev_excl(path
, flags
, holder
);
442 ret
= set_blocksize(bdev
, 4096);
445 bh
= __bread(bdev
, BTRFS_SUPER_INFO_OFFSET
/ 4096, 4096);
450 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
451 if (strncmp((char *)(&disk_super
->magic
), BTRFS_MAGIC
,
452 sizeof(disk_super
->magic
))) {
456 devid
= le64_to_cpu(disk_super
->dev_item
.devid
);
457 transid
= btrfs_super_generation(disk_super
);
458 if (disk_super
->label
[0])
459 printk("device label %s ", disk_super
->label
);
461 /* FIXME, make a readl uuid parser */
462 printk("device fsid %llx-%llx ",
463 *(unsigned long long *)disk_super
->fsid
,
464 *(unsigned long long *)(disk_super
->fsid
+ 8));
466 printk("devid %Lu transid %Lu %s\n", devid
, transid
, path
);
467 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
472 close_bdev_excl(bdev
);
474 mutex_unlock(&uuid_mutex
);
479 * this uses a pretty simple search, the expectation is that it is
480 * called very infrequently and that a given device has a small number
483 static noinline
int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
484 struct btrfs_device
*device
,
485 struct btrfs_path
*path
,
486 u64 num_bytes
, u64
*start
)
488 struct btrfs_key key
;
489 struct btrfs_root
*root
= device
->dev_root
;
490 struct btrfs_dev_extent
*dev_extent
= NULL
;
493 u64 search_start
= 0;
494 u64 search_end
= device
->total_bytes
;
498 struct extent_buffer
*l
;
503 /* FIXME use last free of some kind */
505 /* we don't want to overwrite the superblock on the drive,
506 * so we make sure to start at an offset of at least 1MB
508 search_start
= max((u64
)1024 * 1024, search_start
);
510 if (root
->fs_info
->alloc_start
+ num_bytes
<= device
->total_bytes
)
511 search_start
= max(root
->fs_info
->alloc_start
, search_start
);
513 key
.objectid
= device
->devid
;
514 key
.offset
= search_start
;
515 key
.type
= BTRFS_DEV_EXTENT_KEY
;
516 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
519 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
523 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
526 slot
= path
->slots
[0];
527 if (slot
>= btrfs_header_nritems(l
)) {
528 ret
= btrfs_next_leaf(root
, path
);
535 if (search_start
>= search_end
) {
539 *start
= search_start
;
543 *start
= last_byte
> search_start
?
544 last_byte
: search_start
;
545 if (search_end
<= *start
) {
551 btrfs_item_key_to_cpu(l
, &key
, slot
);
553 if (key
.objectid
< device
->devid
)
556 if (key
.objectid
> device
->devid
)
559 if (key
.offset
>= search_start
&& key
.offset
> last_byte
&&
561 if (last_byte
< search_start
)
562 last_byte
= search_start
;
563 hole_size
= key
.offset
- last_byte
;
564 if (key
.offset
> last_byte
&&
565 hole_size
>= num_bytes
) {
570 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
) {
575 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
576 last_byte
= key
.offset
+ btrfs_dev_extent_length(l
, dev_extent
);
582 /* we have to make sure we didn't find an extent that has already
583 * been allocated by the map tree or the original allocation
585 btrfs_release_path(root
, path
);
586 BUG_ON(*start
< search_start
);
588 if (*start
+ num_bytes
> search_end
) {
592 /* check for pending inserts here */
596 btrfs_release_path(root
, path
);
600 int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
601 struct btrfs_device
*device
,
605 struct btrfs_path
*path
;
606 struct btrfs_root
*root
= device
->dev_root
;
607 struct btrfs_key key
;
608 struct btrfs_key found_key
;
609 struct extent_buffer
*leaf
= NULL
;
610 struct btrfs_dev_extent
*extent
= NULL
;
612 path
= btrfs_alloc_path();
616 key
.objectid
= device
->devid
;
618 key
.type
= BTRFS_DEV_EXTENT_KEY
;
620 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
622 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
623 BTRFS_DEV_EXTENT_KEY
);
625 leaf
= path
->nodes
[0];
626 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
627 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
628 struct btrfs_dev_extent
);
629 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
630 btrfs_dev_extent_length(leaf
, extent
) < start
);
632 } else if (ret
== 0) {
633 leaf
= path
->nodes
[0];
634 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
635 struct btrfs_dev_extent
);
639 if (device
->bytes_used
> 0)
640 device
->bytes_used
-= btrfs_dev_extent_length(leaf
, extent
);
641 ret
= btrfs_del_item(trans
, root
, path
);
644 btrfs_free_path(path
);
648 int noinline
btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
649 struct btrfs_device
*device
,
650 u64 chunk_tree
, u64 chunk_objectid
,
652 u64 num_bytes
, u64
*start
)
655 struct btrfs_path
*path
;
656 struct btrfs_root
*root
= device
->dev_root
;
657 struct btrfs_dev_extent
*extent
;
658 struct extent_buffer
*leaf
;
659 struct btrfs_key key
;
661 WARN_ON(!device
->in_fs_metadata
);
662 path
= btrfs_alloc_path();
666 ret
= find_free_dev_extent(trans
, device
, path
, num_bytes
, start
);
671 key
.objectid
= device
->devid
;
673 key
.type
= BTRFS_DEV_EXTENT_KEY
;
674 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
678 leaf
= path
->nodes
[0];
679 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
680 struct btrfs_dev_extent
);
681 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
682 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
683 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
685 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
686 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
689 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
690 btrfs_mark_buffer_dirty(leaf
);
692 btrfs_free_path(path
);
696 static noinline
int find_next_chunk(struct btrfs_root
*root
,
697 u64 objectid
, u64
*offset
)
699 struct btrfs_path
*path
;
701 struct btrfs_key key
;
702 struct btrfs_chunk
*chunk
;
703 struct btrfs_key found_key
;
705 path
= btrfs_alloc_path();
708 key
.objectid
= objectid
;
709 key
.offset
= (u64
)-1;
710 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
712 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
718 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
722 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
724 if (found_key
.objectid
!= objectid
)
727 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
729 *offset
= found_key
.offset
+
730 btrfs_chunk_length(path
->nodes
[0], chunk
);
735 btrfs_free_path(path
);
739 static noinline
int find_next_devid(struct btrfs_root
*root
,
740 struct btrfs_path
*path
, u64
*objectid
)
743 struct btrfs_key key
;
744 struct btrfs_key found_key
;
746 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
747 key
.type
= BTRFS_DEV_ITEM_KEY
;
748 key
.offset
= (u64
)-1;
750 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
756 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
761 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
763 *objectid
= found_key
.offset
+ 1;
767 btrfs_release_path(root
, path
);
772 * the device information is stored in the chunk root
773 * the btrfs_device struct should be fully filled in
775 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
776 struct btrfs_root
*root
,
777 struct btrfs_device
*device
)
780 struct btrfs_path
*path
;
781 struct btrfs_dev_item
*dev_item
;
782 struct extent_buffer
*leaf
;
783 struct btrfs_key key
;
787 root
= root
->fs_info
->chunk_root
;
789 path
= btrfs_alloc_path();
793 ret
= find_next_devid(root
, path
, &free_devid
);
797 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
798 key
.type
= BTRFS_DEV_ITEM_KEY
;
799 key
.offset
= free_devid
;
801 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
806 leaf
= path
->nodes
[0];
807 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
809 device
->devid
= free_devid
;
810 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
811 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
812 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
813 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
814 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
815 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
816 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
817 btrfs_set_device_group(leaf
, dev_item
, 0);
818 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
819 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
821 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
822 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
823 btrfs_mark_buffer_dirty(leaf
);
827 btrfs_free_path(path
);
831 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
832 struct btrfs_device
*device
)
835 struct btrfs_path
*path
;
836 struct block_device
*bdev
= device
->bdev
;
837 struct btrfs_device
*next_dev
;
838 struct btrfs_key key
;
840 struct btrfs_fs_devices
*fs_devices
;
841 struct btrfs_trans_handle
*trans
;
843 root
= root
->fs_info
->chunk_root
;
845 path
= btrfs_alloc_path();
849 trans
= btrfs_start_transaction(root
, 1);
850 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
851 key
.type
= BTRFS_DEV_ITEM_KEY
;
852 key
.offset
= device
->devid
;
855 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
864 ret
= btrfs_del_item(trans
, root
, path
);
869 * at this point, the device is zero sized. We want to
870 * remove it from the devices list and zero out the old super
872 list_del_init(&device
->dev_list
);
873 list_del_init(&device
->dev_alloc_list
);
874 fs_devices
= root
->fs_info
->fs_devices
;
876 next_dev
= list_entry(fs_devices
->devices
.next
, struct btrfs_device
,
878 if (bdev
== root
->fs_info
->sb
->s_bdev
)
879 root
->fs_info
->sb
->s_bdev
= next_dev
->bdev
;
880 if (bdev
== fs_devices
->latest_bdev
)
881 fs_devices
->latest_bdev
= next_dev
->bdev
;
883 total_bytes
= btrfs_super_num_devices(&root
->fs_info
->super_copy
);
884 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
,
887 btrfs_free_path(path
);
889 btrfs_commit_transaction(trans
, root
);
893 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
895 struct btrfs_device
*device
;
896 struct block_device
*bdev
;
897 struct buffer_head
*bh
= NULL
;
898 struct btrfs_super_block
*disk_super
;
903 mutex_lock(&uuid_mutex
);
904 mutex_lock(&root
->fs_info
->volume_mutex
);
906 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
907 root
->fs_info
->avail_system_alloc_bits
|
908 root
->fs_info
->avail_metadata_alloc_bits
;
910 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) &&
911 btrfs_super_num_devices(&root
->fs_info
->super_copy
) <= 4) {
912 printk("btrfs: unable to go below four devices on raid10\n");
917 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) &&
918 btrfs_super_num_devices(&root
->fs_info
->super_copy
) <= 2) {
919 printk("btrfs: unable to go below two devices on raid1\n");
924 if (strcmp(device_path
, "missing") == 0) {
925 struct list_head
*cur
;
926 struct list_head
*devices
;
927 struct btrfs_device
*tmp
;
930 devices
= &root
->fs_info
->fs_devices
->devices
;
931 list_for_each(cur
, devices
) {
932 tmp
= list_entry(cur
, struct btrfs_device
, dev_list
);
933 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
942 printk("btrfs: no missing devices found to remove\n");
947 bdev
= open_bdev_excl(device_path
, 0,
948 root
->fs_info
->bdev_holder
);
954 bh
= __bread(bdev
, BTRFS_SUPER_INFO_OFFSET
/ 4096, 4096);
959 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
960 if (strncmp((char *)(&disk_super
->magic
), BTRFS_MAGIC
,
961 sizeof(disk_super
->magic
))) {
965 if (memcmp(disk_super
->fsid
, root
->fs_info
->fsid
,
970 devid
= le64_to_cpu(disk_super
->dev_item
.devid
);
971 device
= btrfs_find_device(root
, devid
, NULL
);
978 root
->fs_info
->fs_devices
->num_devices
--;
979 root
->fs_info
->fs_devices
->open_devices
--;
981 ret
= btrfs_shrink_device(device
, 0);
986 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
991 /* make sure this device isn't detected as part of
994 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
995 set_buffer_dirty(bh
);
996 sync_dirty_buffer(bh
);
1002 /* one close for the device struct or super_block */
1003 close_bdev_excl(device
->bdev
);
1006 /* one close for us */
1007 close_bdev_excl(bdev
);
1009 kfree(device
->name
);
1018 close_bdev_excl(bdev
);
1020 mutex_unlock(&root
->fs_info
->volume_mutex
);
1021 mutex_unlock(&uuid_mutex
);
1025 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
1027 struct btrfs_trans_handle
*trans
;
1028 struct btrfs_device
*device
;
1029 struct block_device
*bdev
;
1030 struct list_head
*cur
;
1031 struct list_head
*devices
;
1036 bdev
= open_bdev_excl(device_path
, 0, root
->fs_info
->bdev_holder
);
1041 mutex_lock(&root
->fs_info
->volume_mutex
);
1043 trans
= btrfs_start_transaction(root
, 1);
1045 devices
= &root
->fs_info
->fs_devices
->devices
;
1046 list_for_each(cur
, devices
) {
1047 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
1048 if (device
->bdev
== bdev
) {
1054 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1056 /* we can safely leave the fs_devices entry around */
1058 goto out_close_bdev
;
1061 device
->barriers
= 1;
1062 device
->work
.func
= pending_bios_fn
;
1063 generate_random_uuid(device
->uuid
);
1064 spin_lock_init(&device
->io_lock
);
1065 device
->name
= kstrdup(device_path
, GFP_NOFS
);
1066 if (!device
->name
) {
1068 goto out_close_bdev
;
1070 device
->io_width
= root
->sectorsize
;
1071 device
->io_align
= root
->sectorsize
;
1072 device
->sector_size
= root
->sectorsize
;
1073 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1074 device
->dev_root
= root
->fs_info
->dev_root
;
1075 device
->bdev
= bdev
;
1076 device
->in_fs_metadata
= 1;
1078 ret
= btrfs_add_device(trans
, root
, device
);
1080 goto out_close_bdev
;
1082 set_blocksize(device
->bdev
, 4096);
1084 total_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
1085 btrfs_set_super_total_bytes(&root
->fs_info
->super_copy
,
1086 total_bytes
+ device
->total_bytes
);
1088 total_bytes
= btrfs_super_num_devices(&root
->fs_info
->super_copy
);
1089 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
,
1092 list_add(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
1093 list_add(&device
->dev_alloc_list
,
1094 &root
->fs_info
->fs_devices
->alloc_list
);
1095 root
->fs_info
->fs_devices
->num_devices
++;
1096 root
->fs_info
->fs_devices
->open_devices
++;
1098 unlock_chunks(root
);
1099 btrfs_end_transaction(trans
, root
);
1100 mutex_unlock(&root
->fs_info
->volume_mutex
);
1105 close_bdev_excl(bdev
);
1109 int noinline
btrfs_update_device(struct btrfs_trans_handle
*trans
,
1110 struct btrfs_device
*device
)
1113 struct btrfs_path
*path
;
1114 struct btrfs_root
*root
;
1115 struct btrfs_dev_item
*dev_item
;
1116 struct extent_buffer
*leaf
;
1117 struct btrfs_key key
;
1119 root
= device
->dev_root
->fs_info
->chunk_root
;
1121 path
= btrfs_alloc_path();
1125 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1126 key
.type
= BTRFS_DEV_ITEM_KEY
;
1127 key
.offset
= device
->devid
;
1129 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1138 leaf
= path
->nodes
[0];
1139 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1141 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1142 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1143 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1144 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1145 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1146 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1147 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1148 btrfs_mark_buffer_dirty(leaf
);
1151 btrfs_free_path(path
);
1155 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1156 struct btrfs_device
*device
, u64 new_size
)
1158 struct btrfs_super_block
*super_copy
=
1159 &device
->dev_root
->fs_info
->super_copy
;
1160 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1161 u64 diff
= new_size
- device
->total_bytes
;
1163 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
1164 return btrfs_update_device(trans
, device
);
1167 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1168 struct btrfs_device
*device
, u64 new_size
)
1171 lock_chunks(device
->dev_root
);
1172 ret
= __btrfs_grow_device(trans
, device
, new_size
);
1173 unlock_chunks(device
->dev_root
);
1177 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
1178 struct btrfs_root
*root
,
1179 u64 chunk_tree
, u64 chunk_objectid
,
1183 struct btrfs_path
*path
;
1184 struct btrfs_key key
;
1186 root
= root
->fs_info
->chunk_root
;
1187 path
= btrfs_alloc_path();
1191 key
.objectid
= chunk_objectid
;
1192 key
.offset
= chunk_offset
;
1193 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1195 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1198 ret
= btrfs_del_item(trans
, root
, path
);
1201 btrfs_free_path(path
);
1205 int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
1208 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1209 struct btrfs_disk_key
*disk_key
;
1210 struct btrfs_chunk
*chunk
;
1217 struct btrfs_key key
;
1219 array_size
= btrfs_super_sys_array_size(super_copy
);
1221 ptr
= super_copy
->sys_chunk_array
;
1224 while (cur
< array_size
) {
1225 disk_key
= (struct btrfs_disk_key
*)ptr
;
1226 btrfs_disk_key_to_cpu(&key
, disk_key
);
1228 len
= sizeof(*disk_key
);
1230 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1231 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
1232 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
1233 len
+= btrfs_chunk_item_size(num_stripes
);
1238 if (key
.objectid
== chunk_objectid
&&
1239 key
.offset
== chunk_offset
) {
1240 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
1242 btrfs_set_super_sys_array_size(super_copy
, array_size
);
1252 int btrfs_relocate_chunk(struct btrfs_root
*root
,
1253 u64 chunk_tree
, u64 chunk_objectid
,
1256 struct extent_map_tree
*em_tree
;
1257 struct btrfs_root
*extent_root
;
1258 struct btrfs_trans_handle
*trans
;
1259 struct extent_map
*em
;
1260 struct map_lookup
*map
;
1264 printk("btrfs relocating chunk %llu\n",
1265 (unsigned long long)chunk_offset
);
1266 root
= root
->fs_info
->chunk_root
;
1267 extent_root
= root
->fs_info
->extent_root
;
1268 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
1270 /* step one, relocate all the extents inside this chunk */
1271 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
1274 trans
= btrfs_start_transaction(root
, 1);
1280 * step two, delete the device extents and the
1281 * chunk tree entries
1283 spin_lock(&em_tree
->lock
);
1284 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
1285 spin_unlock(&em_tree
->lock
);
1287 BUG_ON(em
->start
> chunk_offset
||
1288 em
->start
+ em
->len
< chunk_offset
);
1289 map
= (struct map_lookup
*)em
->bdev
;
1291 for (i
= 0; i
< map
->num_stripes
; i
++) {
1292 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
1293 map
->stripes
[i
].physical
);
1296 if (map
->stripes
[i
].dev
) {
1297 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
1301 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
1306 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1307 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
1311 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
1314 spin_lock(&em_tree
->lock
);
1315 remove_extent_mapping(em_tree
, em
);
1316 spin_unlock(&em_tree
->lock
);
1321 /* once for the tree */
1322 free_extent_map(em
);
1324 free_extent_map(em
);
1326 unlock_chunks(root
);
1327 btrfs_end_transaction(trans
, root
);
1331 static u64
div_factor(u64 num
, int factor
)
1341 int btrfs_balance(struct btrfs_root
*dev_root
)
1344 struct list_head
*cur
;
1345 struct list_head
*devices
= &dev_root
->fs_info
->fs_devices
->devices
;
1346 struct btrfs_device
*device
;
1349 struct btrfs_path
*path
;
1350 struct btrfs_key key
;
1351 struct btrfs_chunk
*chunk
;
1352 struct btrfs_root
*chunk_root
= dev_root
->fs_info
->chunk_root
;
1353 struct btrfs_trans_handle
*trans
;
1354 struct btrfs_key found_key
;
1357 mutex_lock(&dev_root
->fs_info
->volume_mutex
);
1358 dev_root
= dev_root
->fs_info
->dev_root
;
1360 /* step one make some room on all the devices */
1361 list_for_each(cur
, devices
) {
1362 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
1363 old_size
= device
->total_bytes
;
1364 size_to_free
= div_factor(old_size
, 1);
1365 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
1366 if (device
->total_bytes
- device
->bytes_used
> size_to_free
)
1369 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
1372 trans
= btrfs_start_transaction(dev_root
, 1);
1375 ret
= btrfs_grow_device(trans
, device
, old_size
);
1378 btrfs_end_transaction(trans
, dev_root
);
1381 /* step two, relocate all the chunks */
1382 path
= btrfs_alloc_path();
1385 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1386 key
.offset
= (u64
)-1;
1387 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1390 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
1395 * this shouldn't happen, it means the last relocate
1401 ret
= btrfs_previous_item(chunk_root
, path
, 0,
1402 BTRFS_CHUNK_ITEM_KEY
);
1406 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1408 if (found_key
.objectid
!= key
.objectid
)
1411 chunk
= btrfs_item_ptr(path
->nodes
[0],
1413 struct btrfs_chunk
);
1414 key
.offset
= found_key
.offset
;
1415 /* chunk zero is special */
1416 if (key
.offset
== 0)
1419 btrfs_release_path(chunk_root
, path
);
1420 ret
= btrfs_relocate_chunk(chunk_root
,
1421 chunk_root
->root_key
.objectid
,
1428 btrfs_free_path(path
);
1429 mutex_unlock(&dev_root
->fs_info
->volume_mutex
);
1434 * shrinking a device means finding all of the device extents past
1435 * the new size, and then following the back refs to the chunks.
1436 * The chunk relocation code actually frees the device extent
1438 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
1440 struct btrfs_trans_handle
*trans
;
1441 struct btrfs_root
*root
= device
->dev_root
;
1442 struct btrfs_dev_extent
*dev_extent
= NULL
;
1443 struct btrfs_path
*path
;
1450 struct extent_buffer
*l
;
1451 struct btrfs_key key
;
1452 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1453 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1454 u64 diff
= device
->total_bytes
- new_size
;
1457 path
= btrfs_alloc_path();
1461 trans
= btrfs_start_transaction(root
, 1);
1471 device
->total_bytes
= new_size
;
1472 ret
= btrfs_update_device(trans
, device
);
1474 unlock_chunks(root
);
1475 btrfs_end_transaction(trans
, root
);
1478 WARN_ON(diff
> old_total
);
1479 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
1480 unlock_chunks(root
);
1481 btrfs_end_transaction(trans
, root
);
1483 key
.objectid
= device
->devid
;
1484 key
.offset
= (u64
)-1;
1485 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1488 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1492 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
1501 slot
= path
->slots
[0];
1502 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
1504 if (key
.objectid
!= device
->devid
)
1507 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1508 length
= btrfs_dev_extent_length(l
, dev_extent
);
1510 if (key
.offset
+ length
<= new_size
)
1513 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
1514 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
1515 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
1516 btrfs_release_path(root
, path
);
1518 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
1525 btrfs_free_path(path
);
1529 int btrfs_add_system_chunk(struct btrfs_trans_handle
*trans
,
1530 struct btrfs_root
*root
,
1531 struct btrfs_key
*key
,
1532 struct btrfs_chunk
*chunk
, int item_size
)
1534 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1535 struct btrfs_disk_key disk_key
;
1539 array_size
= btrfs_super_sys_array_size(super_copy
);
1540 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
1543 ptr
= super_copy
->sys_chunk_array
+ array_size
;
1544 btrfs_cpu_key_to_disk(&disk_key
, key
);
1545 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
1546 ptr
+= sizeof(disk_key
);
1547 memcpy(ptr
, chunk
, item_size
);
1548 item_size
+= sizeof(disk_key
);
1549 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
1553 static u64 noinline
chunk_bytes_by_type(u64 type
, u64 calc_size
,
1554 int num_stripes
, int sub_stripes
)
1556 if (type
& (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_DUP
))
1558 else if (type
& BTRFS_BLOCK_GROUP_RAID10
)
1559 return calc_size
* (num_stripes
/ sub_stripes
);
1561 return calc_size
* num_stripes
;
1565 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
1566 struct btrfs_root
*extent_root
, u64
*start
,
1567 u64
*num_bytes
, u64 type
)
1570 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
1571 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
1572 struct btrfs_path
*path
;
1573 struct btrfs_stripe
*stripes
;
1574 struct btrfs_device
*device
= NULL
;
1575 struct btrfs_chunk
*chunk
;
1576 struct list_head private_devs
;
1577 struct list_head
*dev_list
;
1578 struct list_head
*cur
;
1579 struct extent_map_tree
*em_tree
;
1580 struct map_lookup
*map
;
1581 struct extent_map
*em
;
1582 int min_stripe_size
= 1 * 1024 * 1024;
1584 u64 calc_size
= 1024 * 1024 * 1024;
1585 u64 max_chunk_size
= calc_size
;
1590 int num_stripes
= 1;
1591 int min_stripes
= 1;
1592 int sub_stripes
= 0;
1596 int stripe_len
= 64 * 1024;
1597 struct btrfs_key key
;
1599 if ((type
& BTRFS_BLOCK_GROUP_RAID1
) &&
1600 (type
& BTRFS_BLOCK_GROUP_DUP
)) {
1602 type
&= ~BTRFS_BLOCK_GROUP_DUP
;
1604 dev_list
= &extent_root
->fs_info
->fs_devices
->alloc_list
;
1605 if (list_empty(dev_list
))
1608 if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
1609 num_stripes
= extent_root
->fs_info
->fs_devices
->open_devices
;
1612 if (type
& (BTRFS_BLOCK_GROUP_DUP
)) {
1616 if (type
& (BTRFS_BLOCK_GROUP_RAID1
)) {
1617 num_stripes
= min_t(u64
, 2,
1618 extent_root
->fs_info
->fs_devices
->open_devices
);
1619 if (num_stripes
< 2)
1623 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
1624 num_stripes
= extent_root
->fs_info
->fs_devices
->open_devices
;
1625 if (num_stripes
< 4)
1627 num_stripes
&= ~(u32
)1;
1632 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
1633 max_chunk_size
= 10 * calc_size
;
1634 min_stripe_size
= 64 * 1024 * 1024;
1635 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
1636 max_chunk_size
= 4 * calc_size
;
1637 min_stripe_size
= 32 * 1024 * 1024;
1638 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1639 calc_size
= 8 * 1024 * 1024;
1640 max_chunk_size
= calc_size
* 2;
1641 min_stripe_size
= 1 * 1024 * 1024;
1644 path
= btrfs_alloc_path();
1648 /* we don't want a chunk larger than 10% of the FS */
1649 percent_max
= div_factor(btrfs_super_total_bytes(&info
->super_copy
), 1);
1650 max_chunk_size
= min(percent_max
, max_chunk_size
);
1653 if (calc_size
* num_stripes
> max_chunk_size
) {
1654 calc_size
= max_chunk_size
;
1655 do_div(calc_size
, num_stripes
);
1656 do_div(calc_size
, stripe_len
);
1657 calc_size
*= stripe_len
;
1659 /* we don't want tiny stripes */
1660 calc_size
= max_t(u64
, min_stripe_size
, calc_size
);
1662 do_div(calc_size
, stripe_len
);
1663 calc_size
*= stripe_len
;
1665 INIT_LIST_HEAD(&private_devs
);
1666 cur
= dev_list
->next
;
1669 if (type
& BTRFS_BLOCK_GROUP_DUP
)
1670 min_free
= calc_size
* 2;
1672 min_free
= calc_size
;
1675 * we add 1MB because we never use the first 1MB of the device, unless
1676 * we've looped, then we are likely allocating the maximum amount of
1677 * space left already
1680 min_free
+= 1024 * 1024;
1682 /* build a private list of devices we will allocate from */
1683 while(index
< num_stripes
) {
1684 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
1686 if (device
->total_bytes
> device
->bytes_used
)
1687 avail
= device
->total_bytes
- device
->bytes_used
;
1692 if (device
->in_fs_metadata
&& avail
>= min_free
) {
1693 u64 ignored_start
= 0;
1694 ret
= find_free_dev_extent(trans
, device
, path
,
1698 list_move_tail(&device
->dev_alloc_list
,
1701 if (type
& BTRFS_BLOCK_GROUP_DUP
)
1704 } else if (device
->in_fs_metadata
&& avail
> max_avail
)
1706 if (cur
== dev_list
)
1709 if (index
< num_stripes
) {
1710 list_splice(&private_devs
, dev_list
);
1711 if (index
>= min_stripes
) {
1712 num_stripes
= index
;
1713 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
1714 num_stripes
/= sub_stripes
;
1715 num_stripes
*= sub_stripes
;
1720 if (!looped
&& max_avail
> 0) {
1722 calc_size
= max_avail
;
1725 btrfs_free_path(path
);
1728 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1729 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1730 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
1733 btrfs_free_path(path
);
1737 chunk
= kmalloc(btrfs_chunk_item_size(num_stripes
), GFP_NOFS
);
1739 btrfs_free_path(path
);
1743 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
1746 btrfs_free_path(path
);
1749 btrfs_free_path(path
);
1752 stripes
= &chunk
->stripe
;
1753 *num_bytes
= chunk_bytes_by_type(type
, calc_size
,
1754 num_stripes
, sub_stripes
);
1757 while(index
< num_stripes
) {
1758 struct btrfs_stripe
*stripe
;
1759 BUG_ON(list_empty(&private_devs
));
1760 cur
= private_devs
.next
;
1761 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
1763 /* loop over this device again if we're doing a dup group */
1764 if (!(type
& BTRFS_BLOCK_GROUP_DUP
) ||
1765 (index
== num_stripes
- 1))
1766 list_move_tail(&device
->dev_alloc_list
, dev_list
);
1768 ret
= btrfs_alloc_dev_extent(trans
, device
,
1769 info
->chunk_root
->root_key
.objectid
,
1770 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, key
.offset
,
1771 calc_size
, &dev_offset
);
1773 device
->bytes_used
+= calc_size
;
1774 ret
= btrfs_update_device(trans
, device
);
1777 map
->stripes
[index
].dev
= device
;
1778 map
->stripes
[index
].physical
= dev_offset
;
1779 stripe
= stripes
+ index
;
1780 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
1781 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
1782 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
1783 physical
= dev_offset
;
1786 BUG_ON(!list_empty(&private_devs
));
1788 /* key was set above */
1789 btrfs_set_stack_chunk_length(chunk
, *num_bytes
);
1790 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
1791 btrfs_set_stack_chunk_stripe_len(chunk
, stripe_len
);
1792 btrfs_set_stack_chunk_type(chunk
, type
);
1793 btrfs_set_stack_chunk_num_stripes(chunk
, num_stripes
);
1794 btrfs_set_stack_chunk_io_align(chunk
, stripe_len
);
1795 btrfs_set_stack_chunk_io_width(chunk
, stripe_len
);
1796 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
1797 btrfs_set_stack_chunk_sub_stripes(chunk
, sub_stripes
);
1798 map
->sector_size
= extent_root
->sectorsize
;
1799 map
->stripe_len
= stripe_len
;
1800 map
->io_align
= stripe_len
;
1801 map
->io_width
= stripe_len
;
1803 map
->num_stripes
= num_stripes
;
1804 map
->sub_stripes
= sub_stripes
;
1806 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
,
1807 btrfs_chunk_item_size(num_stripes
));
1809 *start
= key
.offset
;;
1811 em
= alloc_extent_map(GFP_NOFS
);
1814 em
->bdev
= (struct block_device
*)map
;
1815 em
->start
= key
.offset
;
1816 em
->len
= *num_bytes
;
1817 em
->block_start
= 0;
1819 if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1820 ret
= btrfs_add_system_chunk(trans
, chunk_root
, &key
,
1821 chunk
, btrfs_chunk_item_size(num_stripes
));
1826 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
1827 spin_lock(&em_tree
->lock
);
1828 ret
= add_extent_mapping(em_tree
, em
);
1829 spin_unlock(&em_tree
->lock
);
1831 free_extent_map(em
);
1835 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
1837 extent_map_tree_init(&tree
->map_tree
, GFP_NOFS
);
1840 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
1842 struct extent_map
*em
;
1845 spin_lock(&tree
->map_tree
.lock
);
1846 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
1848 remove_extent_mapping(&tree
->map_tree
, em
);
1849 spin_unlock(&tree
->map_tree
.lock
);
1854 free_extent_map(em
);
1855 /* once for the tree */
1856 free_extent_map(em
);
1860 int btrfs_num_copies(struct btrfs_mapping_tree
*map_tree
, u64 logical
, u64 len
)
1862 struct extent_map
*em
;
1863 struct map_lookup
*map
;
1864 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
1867 spin_lock(&em_tree
->lock
);
1868 em
= lookup_extent_mapping(em_tree
, logical
, len
);
1869 spin_unlock(&em_tree
->lock
);
1872 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
1873 map
= (struct map_lookup
*)em
->bdev
;
1874 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
1875 ret
= map
->num_stripes
;
1876 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
1877 ret
= map
->sub_stripes
;
1880 free_extent_map(em
);
1884 static int find_live_mirror(struct map_lookup
*map
, int first
, int num
,
1888 if (map
->stripes
[optimal
].dev
->bdev
)
1890 for (i
= first
; i
< first
+ num
; i
++) {
1891 if (map
->stripes
[i
].dev
->bdev
)
1894 /* we couldn't find one that doesn't fail. Just return something
1895 * and the io error handling code will clean up eventually
1900 static int __btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
1901 u64 logical
, u64
*length
,
1902 struct btrfs_multi_bio
**multi_ret
,
1903 int mirror_num
, struct page
*unplug_page
)
1905 struct extent_map
*em
;
1906 struct map_lookup
*map
;
1907 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
1911 int stripes_allocated
= 8;
1912 int stripes_required
= 1;
1917 struct btrfs_multi_bio
*multi
= NULL
;
1919 if (multi_ret
&& !(rw
& (1 << BIO_RW
))) {
1920 stripes_allocated
= 1;
1924 multi
= kzalloc(btrfs_multi_bio_size(stripes_allocated
),
1929 atomic_set(&multi
->error
, 0);
1932 spin_lock(&em_tree
->lock
);
1933 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
1934 spin_unlock(&em_tree
->lock
);
1936 if (!em
&& unplug_page
)
1940 printk("unable to find logical %Lu len %Lu\n", logical
, *length
);
1944 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
1945 map
= (struct map_lookup
*)em
->bdev
;
1946 offset
= logical
- em
->start
;
1948 if (mirror_num
> map
->num_stripes
)
1951 /* if our multi bio struct is too small, back off and try again */
1952 if (rw
& (1 << BIO_RW
)) {
1953 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
1954 BTRFS_BLOCK_GROUP_DUP
)) {
1955 stripes_required
= map
->num_stripes
;
1957 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
1958 stripes_required
= map
->sub_stripes
;
1962 if (multi_ret
&& rw
== WRITE
&&
1963 stripes_allocated
< stripes_required
) {
1964 stripes_allocated
= map
->num_stripes
;
1965 free_extent_map(em
);
1971 * stripe_nr counts the total number of stripes we have to stride
1972 * to get to this block
1974 do_div(stripe_nr
, map
->stripe_len
);
1976 stripe_offset
= stripe_nr
* map
->stripe_len
;
1977 BUG_ON(offset
< stripe_offset
);
1979 /* stripe_offset is the offset of this block in its stripe*/
1980 stripe_offset
= offset
- stripe_offset
;
1982 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
1983 BTRFS_BLOCK_GROUP_RAID10
|
1984 BTRFS_BLOCK_GROUP_DUP
)) {
1985 /* we limit the length of each bio to what fits in a stripe */
1986 *length
= min_t(u64
, em
->len
- offset
,
1987 map
->stripe_len
- stripe_offset
);
1989 *length
= em
->len
- offset
;
1992 if (!multi_ret
&& !unplug_page
)
1997 if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
1998 if (unplug_page
|| (rw
& (1 << BIO_RW
)))
1999 num_stripes
= map
->num_stripes
;
2000 else if (mirror_num
)
2001 stripe_index
= mirror_num
- 1;
2003 stripe_index
= find_live_mirror(map
, 0,
2005 current
->pid
% map
->num_stripes
);
2008 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
2009 if (rw
& (1 << BIO_RW
))
2010 num_stripes
= map
->num_stripes
;
2011 else if (mirror_num
)
2012 stripe_index
= mirror_num
- 1;
2014 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
2015 int factor
= map
->num_stripes
/ map
->sub_stripes
;
2017 stripe_index
= do_div(stripe_nr
, factor
);
2018 stripe_index
*= map
->sub_stripes
;
2020 if (unplug_page
|| (rw
& (1 << BIO_RW
)))
2021 num_stripes
= map
->sub_stripes
;
2022 else if (mirror_num
)
2023 stripe_index
+= mirror_num
- 1;
2025 stripe_index
= find_live_mirror(map
, stripe_index
,
2026 map
->sub_stripes
, stripe_index
+
2027 current
->pid
% map
->sub_stripes
);
2031 * after this do_div call, stripe_nr is the number of stripes
2032 * on this device we have to walk to find the data, and
2033 * stripe_index is the number of our device in the stripe array
2035 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
2037 BUG_ON(stripe_index
>= map
->num_stripes
);
2039 for (i
= 0; i
< num_stripes
; i
++) {
2041 struct btrfs_device
*device
;
2042 struct backing_dev_info
*bdi
;
2044 device
= map
->stripes
[stripe_index
].dev
;
2046 bdi
= blk_get_backing_dev_info(device
->bdev
);
2047 if (bdi
->unplug_io_fn
) {
2048 bdi
->unplug_io_fn(bdi
, unplug_page
);
2052 multi
->stripes
[i
].physical
=
2053 map
->stripes
[stripe_index
].physical
+
2054 stripe_offset
+ stripe_nr
* map
->stripe_len
;
2055 multi
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
2061 multi
->num_stripes
= num_stripes
;
2062 multi
->max_errors
= max_errors
;
2065 free_extent_map(em
);
2069 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
2070 u64 logical
, u64
*length
,
2071 struct btrfs_multi_bio
**multi_ret
, int mirror_num
)
2073 return __btrfs_map_block(map_tree
, rw
, logical
, length
, multi_ret
,
2077 int btrfs_unplug_page(struct btrfs_mapping_tree
*map_tree
,
2078 u64 logical
, struct page
*page
)
2080 u64 length
= PAGE_CACHE_SIZE
;
2081 return __btrfs_map_block(map_tree
, READ
, logical
, &length
,
2086 static void end_bio_multi_stripe(struct bio
*bio
, int err
)
2088 struct btrfs_multi_bio
*multi
= bio
->bi_private
;
2089 int is_orig_bio
= 0;
2092 atomic_inc(&multi
->error
);
2094 if (bio
== multi
->orig_bio
)
2097 if (atomic_dec_and_test(&multi
->stripes_pending
)) {
2100 bio
= multi
->orig_bio
;
2102 bio
->bi_private
= multi
->private;
2103 bio
->bi_end_io
= multi
->end_io
;
2104 /* only send an error to the higher layers if it is
2105 * beyond the tolerance of the multi-bio
2107 if (atomic_read(&multi
->error
) > multi
->max_errors
) {
2111 * this bio is actually up to date, we didn't
2112 * go over the max number of errors
2114 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
2119 bio_endio(bio
, err
);
2120 } else if (!is_orig_bio
) {
2125 struct async_sched
{
2128 struct btrfs_fs_info
*info
;
2129 struct btrfs_work work
;
2133 * see run_scheduled_bios for a description of why bios are collected for
2136 * This will add one bio to the pending list for a device and make sure
2137 * the work struct is scheduled.
2139 static int noinline
schedule_bio(struct btrfs_root
*root
,
2140 struct btrfs_device
*device
,
2141 int rw
, struct bio
*bio
)
2143 int should_queue
= 1;
2145 /* don't bother with additional async steps for reads, right now */
2146 if (!(rw
& (1 << BIO_RW
))) {
2148 submit_bio(rw
, bio
);
2154 * nr_async_bios allows us to reliably return congestion to the
2155 * higher layers. Otherwise, the async bio makes it appear we have
2156 * made progress against dirty pages when we've really just put it
2157 * on a queue for later
2159 atomic_inc(&root
->fs_info
->nr_async_bios
);
2160 WARN_ON(bio
->bi_next
);
2161 bio
->bi_next
= NULL
;
2164 spin_lock(&device
->io_lock
);
2166 if (device
->pending_bio_tail
)
2167 device
->pending_bio_tail
->bi_next
= bio
;
2169 device
->pending_bio_tail
= bio
;
2170 if (!device
->pending_bios
)
2171 device
->pending_bios
= bio
;
2172 if (device
->running_pending
)
2175 spin_unlock(&device
->io_lock
);
2178 btrfs_queue_worker(&root
->fs_info
->submit_workers
,
2183 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
2184 int mirror_num
, int async_submit
)
2186 struct btrfs_mapping_tree
*map_tree
;
2187 struct btrfs_device
*dev
;
2188 struct bio
*first_bio
= bio
;
2189 u64 logical
= bio
->bi_sector
<< 9;
2192 struct btrfs_multi_bio
*multi
= NULL
;
2197 length
= bio
->bi_size
;
2198 map_tree
= &root
->fs_info
->mapping_tree
;
2199 map_length
= length
;
2201 ret
= btrfs_map_block(map_tree
, rw
, logical
, &map_length
, &multi
,
2205 total_devs
= multi
->num_stripes
;
2206 if (map_length
< length
) {
2207 printk("mapping failed logical %Lu bio len %Lu "
2208 "len %Lu\n", logical
, length
, map_length
);
2211 multi
->end_io
= first_bio
->bi_end_io
;
2212 multi
->private = first_bio
->bi_private
;
2213 multi
->orig_bio
= first_bio
;
2214 atomic_set(&multi
->stripes_pending
, multi
->num_stripes
);
2216 while(dev_nr
< total_devs
) {
2217 if (total_devs
> 1) {
2218 if (dev_nr
< total_devs
- 1) {
2219 bio
= bio_clone(first_bio
, GFP_NOFS
);
2224 bio
->bi_private
= multi
;
2225 bio
->bi_end_io
= end_bio_multi_stripe
;
2227 bio
->bi_sector
= multi
->stripes
[dev_nr
].physical
>> 9;
2228 dev
= multi
->stripes
[dev_nr
].dev
;
2229 if (dev
&& dev
->bdev
) {
2230 bio
->bi_bdev
= dev
->bdev
;
2232 schedule_bio(root
, dev
, rw
, bio
);
2234 submit_bio(rw
, bio
);
2236 bio
->bi_bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2237 bio
->bi_sector
= logical
>> 9;
2238 bio_endio(bio
, -EIO
);
2242 if (total_devs
== 1)
2247 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
,
2250 struct list_head
*head
= &root
->fs_info
->fs_devices
->devices
;
2252 return __find_device(head
, devid
, uuid
);
2255 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
2256 u64 devid
, u8
*dev_uuid
)
2258 struct btrfs_device
*device
;
2259 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
2261 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
2262 list_add(&device
->dev_list
,
2263 &fs_devices
->devices
);
2264 list_add(&device
->dev_alloc_list
,
2265 &fs_devices
->alloc_list
);
2266 device
->barriers
= 1;
2267 device
->dev_root
= root
->fs_info
->dev_root
;
2268 device
->devid
= devid
;
2269 device
->work
.func
= pending_bios_fn
;
2270 fs_devices
->num_devices
++;
2271 spin_lock_init(&device
->io_lock
);
2272 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
2277 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
2278 struct extent_buffer
*leaf
,
2279 struct btrfs_chunk
*chunk
)
2281 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
2282 struct map_lookup
*map
;
2283 struct extent_map
*em
;
2287 u8 uuid
[BTRFS_UUID_SIZE
];
2292 logical
= key
->offset
;
2293 length
= btrfs_chunk_length(leaf
, chunk
);
2295 spin_lock(&map_tree
->map_tree
.lock
);
2296 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
2297 spin_unlock(&map_tree
->map_tree
.lock
);
2299 /* already mapped? */
2300 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
2301 free_extent_map(em
);
2304 free_extent_map(em
);
2307 map
= kzalloc(sizeof(*map
), GFP_NOFS
);
2311 em
= alloc_extent_map(GFP_NOFS
);
2314 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2315 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
2317 free_extent_map(em
);
2321 em
->bdev
= (struct block_device
*)map
;
2322 em
->start
= logical
;
2324 em
->block_start
= 0;
2326 map
->num_stripes
= num_stripes
;
2327 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
2328 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
2329 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
2330 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
2331 map
->type
= btrfs_chunk_type(leaf
, chunk
);
2332 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
2333 for (i
= 0; i
< num_stripes
; i
++) {
2334 map
->stripes
[i
].physical
=
2335 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
2336 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
2337 read_extent_buffer(leaf
, uuid
, (unsigned long)
2338 btrfs_stripe_dev_uuid_nr(chunk
, i
),
2340 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
, uuid
);
2342 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
2344 free_extent_map(em
);
2347 if (!map
->stripes
[i
].dev
) {
2348 map
->stripes
[i
].dev
=
2349 add_missing_dev(root
, devid
, uuid
);
2350 if (!map
->stripes
[i
].dev
) {
2352 free_extent_map(em
);
2356 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
2359 spin_lock(&map_tree
->map_tree
.lock
);
2360 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
2361 spin_unlock(&map_tree
->map_tree
.lock
);
2363 free_extent_map(em
);
2368 static int fill_device_from_item(struct extent_buffer
*leaf
,
2369 struct btrfs_dev_item
*dev_item
,
2370 struct btrfs_device
*device
)
2374 device
->devid
= btrfs_device_id(leaf
, dev_item
);
2375 device
->total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
2376 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
2377 device
->type
= btrfs_device_type(leaf
, dev_item
);
2378 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
2379 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
2380 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
2382 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
2383 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
2388 static int read_one_dev(struct btrfs_root
*root
,
2389 struct extent_buffer
*leaf
,
2390 struct btrfs_dev_item
*dev_item
)
2392 struct btrfs_device
*device
;
2395 u8 dev_uuid
[BTRFS_UUID_SIZE
];
2397 devid
= btrfs_device_id(leaf
, dev_item
);
2398 read_extent_buffer(leaf
, dev_uuid
,
2399 (unsigned long)btrfs_device_uuid(dev_item
),
2401 device
= btrfs_find_device(root
, devid
, dev_uuid
);
2403 printk("warning devid %Lu missing\n", devid
);
2404 device
= add_missing_dev(root
, devid
, dev_uuid
);
2409 fill_device_from_item(leaf
, dev_item
, device
);
2410 device
->dev_root
= root
->fs_info
->dev_root
;
2411 device
->in_fs_metadata
= 1;
2414 ret
= btrfs_open_device(device
);
2422 int btrfs_read_super_device(struct btrfs_root
*root
, struct extent_buffer
*buf
)
2424 struct btrfs_dev_item
*dev_item
;
2426 dev_item
= (struct btrfs_dev_item
*)offsetof(struct btrfs_super_block
,
2428 return read_one_dev(root
, buf
, dev_item
);
2431 int btrfs_read_sys_array(struct btrfs_root
*root
)
2433 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
2434 struct extent_buffer
*sb
;
2435 struct btrfs_disk_key
*disk_key
;
2436 struct btrfs_chunk
*chunk
;
2438 unsigned long sb_ptr
;
2444 struct btrfs_key key
;
2446 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
2447 BTRFS_SUPER_INFO_SIZE
);
2450 btrfs_set_buffer_uptodate(sb
);
2451 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
2452 array_size
= btrfs_super_sys_array_size(super_copy
);
2454 ptr
= super_copy
->sys_chunk_array
;
2455 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
2458 while (cur
< array_size
) {
2459 disk_key
= (struct btrfs_disk_key
*)ptr
;
2460 btrfs_disk_key_to_cpu(&key
, disk_key
);
2462 len
= sizeof(*disk_key
); ptr
+= len
;
2466 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
2467 chunk
= (struct btrfs_chunk
*)sb_ptr
;
2468 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
2471 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
2472 len
= btrfs_chunk_item_size(num_stripes
);
2481 free_extent_buffer(sb
);
2485 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
2487 struct btrfs_path
*path
;
2488 struct extent_buffer
*leaf
;
2489 struct btrfs_key key
;
2490 struct btrfs_key found_key
;
2494 root
= root
->fs_info
->chunk_root
;
2496 path
= btrfs_alloc_path();
2500 /* first we search for all of the device items, and then we
2501 * read in all of the chunk items. This way we can create chunk
2502 * mappings that reference all of the devices that are afound
2504 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
2508 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2510 leaf
= path
->nodes
[0];
2511 slot
= path
->slots
[0];
2512 if (slot
>= btrfs_header_nritems(leaf
)) {
2513 ret
= btrfs_next_leaf(root
, path
);
2520 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2521 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
2522 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
2524 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
2525 struct btrfs_dev_item
*dev_item
;
2526 dev_item
= btrfs_item_ptr(leaf
, slot
,
2527 struct btrfs_dev_item
);
2528 ret
= read_one_dev(root
, leaf
, dev_item
);
2531 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
2532 struct btrfs_chunk
*chunk
;
2533 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
2534 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
2538 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
2540 btrfs_release_path(root
, path
);
2544 btrfs_free_path(path
);