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 int btrfs_cleanup_fs_uuids(void)
61 struct btrfs_fs_devices
*fs_devices
;
62 struct list_head
*uuid_cur
;
63 struct list_head
*devices_cur
;
64 struct btrfs_device
*dev
;
66 list_for_each(uuid_cur
, &fs_uuids
) {
67 fs_devices
= list_entry(uuid_cur
, struct btrfs_fs_devices
,
69 while(!list_empty(&fs_devices
->devices
)) {
70 devices_cur
= fs_devices
->devices
.next
;
71 dev
= list_entry(devices_cur
, struct btrfs_device
,
74 close_bdev_excl(dev
->bdev
);
75 fs_devices
->open_devices
--;
77 list_del(&dev
->dev_list
);
85 static struct btrfs_device
*__find_device(struct list_head
*head
, u64 devid
,
88 struct btrfs_device
*dev
;
89 struct list_head
*cur
;
91 list_for_each(cur
, head
) {
92 dev
= list_entry(cur
, struct btrfs_device
, dev_list
);
93 if (dev
->devid
== devid
&&
94 (!uuid
|| !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
))) {
101 static struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
103 struct list_head
*cur
;
104 struct btrfs_fs_devices
*fs_devices
;
106 list_for_each(cur
, &fs_uuids
) {
107 fs_devices
= list_entry(cur
, struct btrfs_fs_devices
, list
);
108 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
115 * we try to collect pending bios for a device so we don't get a large
116 * number of procs sending bios down to the same device. This greatly
117 * improves the schedulers ability to collect and merge the bios.
119 * But, it also turns into a long list of bios to process and that is sure
120 * to eventually make the worker thread block. The solution here is to
121 * make some progress and then put this work struct back at the end of
122 * the list if the block device is congested. This way, multiple devices
123 * can make progress from a single worker thread.
125 int run_scheduled_bios(struct btrfs_device
*device
)
128 struct backing_dev_info
*bdi
;
132 unsigned long num_run
= 0;
134 bdi
= device
->bdev
->bd_inode
->i_mapping
->backing_dev_info
;
136 spin_lock(&device
->io_lock
);
138 /* take all the bios off the list at once and process them
139 * later on (without the lock held). But, remember the
140 * tail and other pointers so the bios can be properly reinserted
141 * into the list if we hit congestion
143 pending
= device
->pending_bios
;
144 tail
= device
->pending_bio_tail
;
145 WARN_ON(pending
&& !tail
);
146 device
->pending_bios
= NULL
;
147 device
->pending_bio_tail
= NULL
;
150 * if pending was null this time around, no bios need processing
151 * at all and we can stop. Otherwise it'll loop back up again
152 * and do an additional check so no bios are missed.
154 * device->running_pending is used to synchronize with the
159 device
->running_pending
= 1;
162 device
->running_pending
= 0;
164 spin_unlock(&device
->io_lock
);
168 pending
= pending
->bi_next
;
170 atomic_dec(&device
->dev_root
->fs_info
->nr_async_submits
);
171 submit_bio(cur
->bi_rw
, cur
);
175 * we made progress, there is more work to do and the bdi
176 * is now congested. Back off and let other work structs
179 if (pending
&& num_run
&& bdi_write_congested(bdi
)) {
180 struct bio
*old_head
;
182 spin_lock(&device
->io_lock
);
183 old_head
= device
->pending_bios
;
184 device
->pending_bios
= pending
;
185 if (device
->pending_bio_tail
)
186 tail
->bi_next
= old_head
;
188 device
->pending_bio_tail
= tail
;
190 spin_unlock(&device
->io_lock
);
191 btrfs_requeue_work(&device
->work
);
201 void pending_bios_fn(struct btrfs_work
*work
)
203 struct btrfs_device
*device
;
205 device
= container_of(work
, struct btrfs_device
, work
);
206 run_scheduled_bios(device
);
209 static int device_list_add(const char *path
,
210 struct btrfs_super_block
*disk_super
,
211 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
213 struct btrfs_device
*device
;
214 struct btrfs_fs_devices
*fs_devices
;
215 u64 found_transid
= btrfs_super_generation(disk_super
);
217 fs_devices
= find_fsid(disk_super
->fsid
);
219 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
222 INIT_LIST_HEAD(&fs_devices
->devices
);
223 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
224 list_add(&fs_devices
->list
, &fs_uuids
);
225 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
226 fs_devices
->latest_devid
= devid
;
227 fs_devices
->latest_trans
= found_transid
;
230 device
= __find_device(&fs_devices
->devices
, devid
,
231 disk_super
->dev_item
.uuid
);
234 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
236 /* we can safely leave the fs_devices entry around */
239 device
->devid
= devid
;
240 device
->work
.func
= pending_bios_fn
;
241 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
243 device
->barriers
= 1;
244 spin_lock_init(&device
->io_lock
);
245 device
->name
= kstrdup(path
, GFP_NOFS
);
250 list_add(&device
->dev_list
, &fs_devices
->devices
);
251 list_add(&device
->dev_alloc_list
, &fs_devices
->alloc_list
);
252 fs_devices
->num_devices
++;
255 if (found_transid
> fs_devices
->latest_trans
) {
256 fs_devices
->latest_devid
= devid
;
257 fs_devices
->latest_trans
= found_transid
;
259 *fs_devices_ret
= fs_devices
;
263 int btrfs_close_extra_devices(struct btrfs_fs_devices
*fs_devices
)
265 struct list_head
*head
= &fs_devices
->devices
;
266 struct list_head
*cur
;
267 struct btrfs_device
*device
;
269 mutex_lock(&uuid_mutex
);
271 list_for_each(cur
, head
) {
272 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
273 if (!device
->in_fs_metadata
) {
275 close_bdev_excl(device
->bdev
);
276 fs_devices
->open_devices
--;
278 list_del(&device
->dev_list
);
279 list_del(&device
->dev_alloc_list
);
280 fs_devices
->num_devices
--;
286 mutex_unlock(&uuid_mutex
);
290 int btrfs_close_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
);
297 list_for_each(cur
, head
) {
298 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
300 close_bdev_excl(device
->bdev
);
301 fs_devices
->open_devices
--;
304 device
->in_fs_metadata
= 0;
306 fs_devices
->mounted
= 0;
307 mutex_unlock(&uuid_mutex
);
311 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
312 int flags
, void *holder
)
314 struct block_device
*bdev
;
315 struct list_head
*head
= &fs_devices
->devices
;
316 struct list_head
*cur
;
317 struct btrfs_device
*device
;
318 struct block_device
*latest_bdev
= NULL
;
319 struct buffer_head
*bh
;
320 struct btrfs_super_block
*disk_super
;
321 u64 latest_devid
= 0;
322 u64 latest_transid
= 0;
327 mutex_lock(&uuid_mutex
);
328 if (fs_devices
->mounted
)
331 list_for_each(cur
, head
) {
332 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
339 bdev
= open_bdev_excl(device
->name
, flags
, holder
);
342 printk("open %s failed\n", device
->name
);
345 set_blocksize(bdev
, 4096);
347 bh
= __bread(bdev
, BTRFS_SUPER_INFO_OFFSET
/ 4096, 4096);
351 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
352 if (strncmp((char *)(&disk_super
->magic
), BTRFS_MAGIC
,
353 sizeof(disk_super
->magic
)))
356 devid
= le64_to_cpu(disk_super
->dev_item
.devid
);
357 if (devid
!= device
->devid
)
360 transid
= btrfs_super_generation(disk_super
);
361 if (!latest_transid
|| transid
> latest_transid
) {
362 latest_devid
= devid
;
363 latest_transid
= transid
;
368 device
->in_fs_metadata
= 0;
369 fs_devices
->open_devices
++;
375 close_bdev_excl(bdev
);
379 if (fs_devices
->open_devices
== 0) {
383 fs_devices
->mounted
= 1;
384 fs_devices
->latest_bdev
= latest_bdev
;
385 fs_devices
->latest_devid
= latest_devid
;
386 fs_devices
->latest_trans
= latest_transid
;
388 mutex_unlock(&uuid_mutex
);
392 int btrfs_scan_one_device(const char *path
, int flags
, void *holder
,
393 struct btrfs_fs_devices
**fs_devices_ret
)
395 struct btrfs_super_block
*disk_super
;
396 struct block_device
*bdev
;
397 struct buffer_head
*bh
;
402 mutex_lock(&uuid_mutex
);
404 bdev
= open_bdev_excl(path
, flags
, holder
);
411 ret
= set_blocksize(bdev
, 4096);
414 bh
= __bread(bdev
, BTRFS_SUPER_INFO_OFFSET
/ 4096, 4096);
419 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
420 if (strncmp((char *)(&disk_super
->magic
), BTRFS_MAGIC
,
421 sizeof(disk_super
->magic
))) {
425 devid
= le64_to_cpu(disk_super
->dev_item
.devid
);
426 transid
= btrfs_super_generation(disk_super
);
427 if (disk_super
->label
[0])
428 printk("device label %s ", disk_super
->label
);
430 /* FIXME, make a readl uuid parser */
431 printk("device fsid %llx-%llx ",
432 *(unsigned long long *)disk_super
->fsid
,
433 *(unsigned long long *)(disk_super
->fsid
+ 8));
435 printk("devid %Lu transid %Lu %s\n", devid
, transid
, path
);
436 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
441 close_bdev_excl(bdev
);
443 mutex_unlock(&uuid_mutex
);
448 * this uses a pretty simple search, the expectation is that it is
449 * called very infrequently and that a given device has a small number
452 static int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
453 struct btrfs_device
*device
,
454 struct btrfs_path
*path
,
455 u64 num_bytes
, u64
*start
)
457 struct btrfs_key key
;
458 struct btrfs_root
*root
= device
->dev_root
;
459 struct btrfs_dev_extent
*dev_extent
= NULL
;
462 u64 search_start
= 0;
463 u64 search_end
= device
->total_bytes
;
467 struct extent_buffer
*l
;
472 /* FIXME use last free of some kind */
474 /* we don't want to overwrite the superblock on the drive,
475 * so we make sure to start at an offset of at least 1MB
477 search_start
= max((u64
)1024 * 1024, search_start
);
479 if (root
->fs_info
->alloc_start
+ num_bytes
<= device
->total_bytes
)
480 search_start
= max(root
->fs_info
->alloc_start
, search_start
);
482 key
.objectid
= device
->devid
;
483 key
.offset
= search_start
;
484 key
.type
= BTRFS_DEV_EXTENT_KEY
;
485 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
488 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
492 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
495 slot
= path
->slots
[0];
496 if (slot
>= btrfs_header_nritems(l
)) {
497 ret
= btrfs_next_leaf(root
, path
);
504 if (search_start
>= search_end
) {
508 *start
= search_start
;
512 *start
= last_byte
> search_start
?
513 last_byte
: search_start
;
514 if (search_end
<= *start
) {
520 btrfs_item_key_to_cpu(l
, &key
, slot
);
522 if (key
.objectid
< device
->devid
)
525 if (key
.objectid
> device
->devid
)
528 if (key
.offset
>= search_start
&& key
.offset
> last_byte
&&
530 if (last_byte
< search_start
)
531 last_byte
= search_start
;
532 hole_size
= key
.offset
- last_byte
;
533 if (key
.offset
> last_byte
&&
534 hole_size
>= num_bytes
) {
539 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
) {
544 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
545 last_byte
= key
.offset
+ btrfs_dev_extent_length(l
, dev_extent
);
551 /* we have to make sure we didn't find an extent that has already
552 * been allocated by the map tree or the original allocation
554 btrfs_release_path(root
, path
);
555 BUG_ON(*start
< search_start
);
557 if (*start
+ num_bytes
> search_end
) {
561 /* check for pending inserts here */
565 btrfs_release_path(root
, path
);
569 int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
570 struct btrfs_device
*device
,
574 struct btrfs_path
*path
;
575 struct btrfs_root
*root
= device
->dev_root
;
576 struct btrfs_key key
;
577 struct btrfs_key found_key
;
578 struct extent_buffer
*leaf
= NULL
;
579 struct btrfs_dev_extent
*extent
= NULL
;
581 path
= btrfs_alloc_path();
585 key
.objectid
= device
->devid
;
587 key
.type
= BTRFS_DEV_EXTENT_KEY
;
589 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
591 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
592 BTRFS_DEV_EXTENT_KEY
);
594 leaf
= path
->nodes
[0];
595 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
596 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
597 struct btrfs_dev_extent
);
598 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
599 btrfs_dev_extent_length(leaf
, extent
) < start
);
601 } else if (ret
== 0) {
602 leaf
= path
->nodes
[0];
603 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
604 struct btrfs_dev_extent
);
608 if (device
->bytes_used
> 0)
609 device
->bytes_used
-= btrfs_dev_extent_length(leaf
, extent
);
610 ret
= btrfs_del_item(trans
, root
, path
);
613 btrfs_free_path(path
);
617 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
618 struct btrfs_device
*device
,
619 u64 chunk_tree
, u64 chunk_objectid
,
621 u64 num_bytes
, u64
*start
)
624 struct btrfs_path
*path
;
625 struct btrfs_root
*root
= device
->dev_root
;
626 struct btrfs_dev_extent
*extent
;
627 struct extent_buffer
*leaf
;
628 struct btrfs_key key
;
630 WARN_ON(!device
->in_fs_metadata
);
631 path
= btrfs_alloc_path();
635 ret
= find_free_dev_extent(trans
, device
, path
, num_bytes
, start
);
640 key
.objectid
= device
->devid
;
642 key
.type
= BTRFS_DEV_EXTENT_KEY
;
643 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
647 leaf
= path
->nodes
[0];
648 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
649 struct btrfs_dev_extent
);
650 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
651 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
652 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
654 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
655 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
658 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
659 btrfs_mark_buffer_dirty(leaf
);
661 btrfs_free_path(path
);
665 static int find_next_chunk(struct btrfs_root
*root
, u64 objectid
, u64
*offset
)
667 struct btrfs_path
*path
;
669 struct btrfs_key key
;
670 struct btrfs_chunk
*chunk
;
671 struct btrfs_key found_key
;
673 path
= btrfs_alloc_path();
676 key
.objectid
= objectid
;
677 key
.offset
= (u64
)-1;
678 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
680 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
686 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
690 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
692 if (found_key
.objectid
!= objectid
)
695 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
697 *offset
= found_key
.offset
+
698 btrfs_chunk_length(path
->nodes
[0], chunk
);
703 btrfs_free_path(path
);
707 static int find_next_devid(struct btrfs_root
*root
, struct btrfs_path
*path
,
711 struct btrfs_key key
;
712 struct btrfs_key found_key
;
714 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
715 key
.type
= BTRFS_DEV_ITEM_KEY
;
716 key
.offset
= (u64
)-1;
718 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
724 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
729 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
731 *objectid
= found_key
.offset
+ 1;
735 btrfs_release_path(root
, path
);
740 * the device information is stored in the chunk root
741 * the btrfs_device struct should be fully filled in
743 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
744 struct btrfs_root
*root
,
745 struct btrfs_device
*device
)
748 struct btrfs_path
*path
;
749 struct btrfs_dev_item
*dev_item
;
750 struct extent_buffer
*leaf
;
751 struct btrfs_key key
;
755 root
= root
->fs_info
->chunk_root
;
757 path
= btrfs_alloc_path();
761 ret
= find_next_devid(root
, path
, &free_devid
);
765 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
766 key
.type
= BTRFS_DEV_ITEM_KEY
;
767 key
.offset
= free_devid
;
769 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
774 leaf
= path
->nodes
[0];
775 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
777 device
->devid
= free_devid
;
778 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
779 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
780 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
781 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
782 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
783 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
784 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
785 btrfs_set_device_group(leaf
, dev_item
, 0);
786 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
787 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
789 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
790 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
791 btrfs_mark_buffer_dirty(leaf
);
795 btrfs_free_path(path
);
799 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
800 struct btrfs_device
*device
)
803 struct btrfs_path
*path
;
804 struct block_device
*bdev
= device
->bdev
;
805 struct btrfs_device
*next_dev
;
806 struct btrfs_key key
;
808 struct btrfs_fs_devices
*fs_devices
;
809 struct btrfs_trans_handle
*trans
;
811 root
= root
->fs_info
->chunk_root
;
813 path
= btrfs_alloc_path();
817 trans
= btrfs_start_transaction(root
, 1);
818 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
819 key
.type
= BTRFS_DEV_ITEM_KEY
;
820 key
.offset
= device
->devid
;
822 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
831 ret
= btrfs_del_item(trans
, root
, path
);
836 * at this point, the device is zero sized. We want to
837 * remove it from the devices list and zero out the old super
839 list_del_init(&device
->dev_list
);
840 list_del_init(&device
->dev_alloc_list
);
841 fs_devices
= root
->fs_info
->fs_devices
;
843 next_dev
= list_entry(fs_devices
->devices
.next
, struct btrfs_device
,
845 if (bdev
== root
->fs_info
->sb
->s_bdev
)
846 root
->fs_info
->sb
->s_bdev
= next_dev
->bdev
;
847 if (bdev
== fs_devices
->latest_bdev
)
848 fs_devices
->latest_bdev
= next_dev
->bdev
;
850 total_bytes
= btrfs_super_num_devices(&root
->fs_info
->super_copy
);
851 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
,
854 btrfs_free_path(path
);
855 btrfs_commit_transaction(trans
, root
);
859 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
861 struct btrfs_device
*device
;
862 struct block_device
*bdev
;
863 struct buffer_head
*bh
= NULL
;
864 struct btrfs_super_block
*disk_super
;
869 mutex_lock(&root
->fs_info
->alloc_mutex
);
870 mutex_lock(&root
->fs_info
->chunk_mutex
);
871 mutex_lock(&uuid_mutex
);
873 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
874 root
->fs_info
->avail_system_alloc_bits
|
875 root
->fs_info
->avail_metadata_alloc_bits
;
877 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) &&
878 btrfs_super_num_devices(&root
->fs_info
->super_copy
) <= 4) {
879 printk("btrfs: unable to go below four devices on raid10\n");
884 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) &&
885 btrfs_super_num_devices(&root
->fs_info
->super_copy
) <= 2) {
886 printk("btrfs: unable to go below two devices on raid1\n");
891 if (strcmp(device_path
, "missing") == 0) {
892 struct list_head
*cur
;
893 struct list_head
*devices
;
894 struct btrfs_device
*tmp
;
897 devices
= &root
->fs_info
->fs_devices
->devices
;
898 list_for_each(cur
, devices
) {
899 tmp
= list_entry(cur
, struct btrfs_device
, dev_list
);
900 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
909 printk("btrfs: no missing devices found to remove\n");
914 bdev
= open_bdev_excl(device_path
, 0,
915 root
->fs_info
->bdev_holder
);
921 bh
= __bread(bdev
, BTRFS_SUPER_INFO_OFFSET
/ 4096, 4096);
926 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
927 if (strncmp((char *)(&disk_super
->magic
), BTRFS_MAGIC
,
928 sizeof(disk_super
->magic
))) {
932 if (memcmp(disk_super
->fsid
, root
->fs_info
->fsid
,
937 devid
= le64_to_cpu(disk_super
->dev_item
.devid
);
938 device
= btrfs_find_device(root
, devid
, NULL
);
945 root
->fs_info
->fs_devices
->num_devices
--;
946 root
->fs_info
->fs_devices
->open_devices
--;
948 ret
= btrfs_shrink_device(device
, 0);
953 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
958 /* make sure this device isn't detected as part of
961 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
962 set_buffer_dirty(bh
);
963 sync_dirty_buffer(bh
);
969 /* one close for the device struct or super_block */
970 close_bdev_excl(device
->bdev
);
973 /* one close for us */
974 close_bdev_excl(bdev
);
985 close_bdev_excl(bdev
);
987 mutex_unlock(&uuid_mutex
);
988 mutex_unlock(&root
->fs_info
->chunk_mutex
);
989 mutex_unlock(&root
->fs_info
->alloc_mutex
);
993 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
995 struct btrfs_trans_handle
*trans
;
996 struct btrfs_device
*device
;
997 struct block_device
*bdev
;
998 struct list_head
*cur
;
999 struct list_head
*devices
;
1004 bdev
= open_bdev_excl(device_path
, 0, root
->fs_info
->bdev_holder
);
1009 mutex_lock(&root
->fs_info
->alloc_mutex
);
1010 mutex_lock(&root
->fs_info
->chunk_mutex
);
1012 trans
= btrfs_start_transaction(root
, 1);
1013 devices
= &root
->fs_info
->fs_devices
->devices
;
1014 list_for_each(cur
, devices
) {
1015 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
1016 if (device
->bdev
== bdev
) {
1022 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1024 /* we can safely leave the fs_devices entry around */
1026 goto out_close_bdev
;
1029 device
->barriers
= 1;
1030 device
->work
.func
= pending_bios_fn
;
1031 generate_random_uuid(device
->uuid
);
1032 spin_lock_init(&device
->io_lock
);
1033 device
->name
= kstrdup(device_path
, GFP_NOFS
);
1034 if (!device
->name
) {
1036 goto out_close_bdev
;
1038 device
->io_width
= root
->sectorsize
;
1039 device
->io_align
= root
->sectorsize
;
1040 device
->sector_size
= root
->sectorsize
;
1041 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1042 device
->dev_root
= root
->fs_info
->dev_root
;
1043 device
->bdev
= bdev
;
1044 device
->in_fs_metadata
= 1;
1046 ret
= btrfs_add_device(trans
, root
, device
);
1048 goto out_close_bdev
;
1050 total_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
1051 btrfs_set_super_total_bytes(&root
->fs_info
->super_copy
,
1052 total_bytes
+ device
->total_bytes
);
1054 total_bytes
= btrfs_super_num_devices(&root
->fs_info
->super_copy
);
1055 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
,
1058 list_add(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
1059 list_add(&device
->dev_alloc_list
,
1060 &root
->fs_info
->fs_devices
->alloc_list
);
1061 root
->fs_info
->fs_devices
->num_devices
++;
1062 root
->fs_info
->fs_devices
->open_devices
++;
1064 btrfs_end_transaction(trans
, root
);
1065 mutex_unlock(&root
->fs_info
->chunk_mutex
);
1066 mutex_unlock(&root
->fs_info
->alloc_mutex
);
1071 close_bdev_excl(bdev
);
1075 int btrfs_update_device(struct btrfs_trans_handle
*trans
,
1076 struct btrfs_device
*device
)
1079 struct btrfs_path
*path
;
1080 struct btrfs_root
*root
;
1081 struct btrfs_dev_item
*dev_item
;
1082 struct extent_buffer
*leaf
;
1083 struct btrfs_key key
;
1085 root
= device
->dev_root
->fs_info
->chunk_root
;
1087 path
= btrfs_alloc_path();
1091 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1092 key
.type
= BTRFS_DEV_ITEM_KEY
;
1093 key
.offset
= device
->devid
;
1095 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1104 leaf
= path
->nodes
[0];
1105 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1107 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1108 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1109 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1110 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1111 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1112 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1113 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1114 btrfs_mark_buffer_dirty(leaf
);
1117 btrfs_free_path(path
);
1121 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1122 struct btrfs_device
*device
, u64 new_size
)
1124 struct btrfs_super_block
*super_copy
=
1125 &device
->dev_root
->fs_info
->super_copy
;
1126 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1127 u64 diff
= new_size
- device
->total_bytes
;
1129 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
1130 return btrfs_update_device(trans
, device
);
1133 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
1134 struct btrfs_root
*root
,
1135 u64 chunk_tree
, u64 chunk_objectid
,
1139 struct btrfs_path
*path
;
1140 struct btrfs_key key
;
1142 root
= root
->fs_info
->chunk_root
;
1143 path
= btrfs_alloc_path();
1147 key
.objectid
= chunk_objectid
;
1148 key
.offset
= chunk_offset
;
1149 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1151 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1154 ret
= btrfs_del_item(trans
, root
, path
);
1157 btrfs_free_path(path
);
1161 int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
1164 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1165 struct btrfs_disk_key
*disk_key
;
1166 struct btrfs_chunk
*chunk
;
1173 struct btrfs_key key
;
1175 array_size
= btrfs_super_sys_array_size(super_copy
);
1177 ptr
= super_copy
->sys_chunk_array
;
1180 while (cur
< array_size
) {
1181 disk_key
= (struct btrfs_disk_key
*)ptr
;
1182 btrfs_disk_key_to_cpu(&key
, disk_key
);
1184 len
= sizeof(*disk_key
);
1186 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1187 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
1188 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
1189 len
+= btrfs_chunk_item_size(num_stripes
);
1194 if (key
.objectid
== chunk_objectid
&&
1195 key
.offset
== chunk_offset
) {
1196 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
1198 btrfs_set_super_sys_array_size(super_copy
, array_size
);
1208 int btrfs_relocate_chunk(struct btrfs_root
*root
,
1209 u64 chunk_tree
, u64 chunk_objectid
,
1212 struct extent_map_tree
*em_tree
;
1213 struct btrfs_root
*extent_root
;
1214 struct btrfs_trans_handle
*trans
;
1215 struct extent_map
*em
;
1216 struct map_lookup
*map
;
1220 printk("btrfs relocating chunk %llu\n",
1221 (unsigned long long)chunk_offset
);
1222 root
= root
->fs_info
->chunk_root
;
1223 extent_root
= root
->fs_info
->extent_root
;
1224 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
1226 /* step one, relocate all the extents inside this chunk */
1227 ret
= btrfs_shrink_extent_tree(extent_root
, chunk_offset
);
1230 trans
= btrfs_start_transaction(root
, 1);
1234 * step two, delete the device extents and the
1235 * chunk tree entries
1237 spin_lock(&em_tree
->lock
);
1238 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
1239 spin_unlock(&em_tree
->lock
);
1241 BUG_ON(em
->start
> chunk_offset
||
1242 em
->start
+ em
->len
< chunk_offset
);
1243 map
= (struct map_lookup
*)em
->bdev
;
1245 for (i
= 0; i
< map
->num_stripes
; i
++) {
1246 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
1247 map
->stripes
[i
].physical
);
1250 if (map
->stripes
[i
].dev
) {
1251 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
1255 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
1260 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1261 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
1265 spin_lock(&em_tree
->lock
);
1266 remove_extent_mapping(em_tree
, em
);
1270 /* once for the tree */
1271 free_extent_map(em
);
1272 spin_unlock(&em_tree
->lock
);
1275 free_extent_map(em
);
1277 btrfs_end_transaction(trans
, root
);
1281 static u64
div_factor(u64 num
, int factor
)
1291 int btrfs_balance(struct btrfs_root
*dev_root
)
1294 struct list_head
*cur
;
1295 struct list_head
*devices
= &dev_root
->fs_info
->fs_devices
->devices
;
1296 struct btrfs_device
*device
;
1299 struct btrfs_path
*path
;
1300 struct btrfs_key key
;
1301 struct btrfs_chunk
*chunk
;
1302 struct btrfs_root
*chunk_root
= dev_root
->fs_info
->chunk_root
;
1303 struct btrfs_trans_handle
*trans
;
1304 struct btrfs_key found_key
;
1307 BUG(); /* FIXME, needs locking */
1309 dev_root
= dev_root
->fs_info
->dev_root
;
1311 /* step one make some room on all the devices */
1312 list_for_each(cur
, devices
) {
1313 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
1314 old_size
= device
->total_bytes
;
1315 size_to_free
= div_factor(old_size
, 1);
1316 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
1317 if (device
->total_bytes
- device
->bytes_used
> size_to_free
)
1320 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
1323 trans
= btrfs_start_transaction(dev_root
, 1);
1326 ret
= btrfs_grow_device(trans
, device
, old_size
);
1329 btrfs_end_transaction(trans
, dev_root
);
1332 /* step two, relocate all the chunks */
1333 path
= btrfs_alloc_path();
1336 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1337 key
.offset
= (u64
)-1;
1338 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1341 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
1346 * this shouldn't happen, it means the last relocate
1352 ret
= btrfs_previous_item(chunk_root
, path
, 0,
1353 BTRFS_CHUNK_ITEM_KEY
);
1357 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1359 if (found_key
.objectid
!= key
.objectid
)
1361 chunk
= btrfs_item_ptr(path
->nodes
[0],
1363 struct btrfs_chunk
);
1364 key
.offset
= found_key
.offset
;
1365 /* chunk zero is special */
1366 if (key
.offset
== 0)
1369 ret
= btrfs_relocate_chunk(chunk_root
,
1370 chunk_root
->root_key
.objectid
,
1374 btrfs_release_path(chunk_root
, path
);
1378 btrfs_free_path(path
);
1383 * shrinking a device means finding all of the device extents past
1384 * the new size, and then following the back refs to the chunks.
1385 * The chunk relocation code actually frees the device extent
1387 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
1389 struct btrfs_trans_handle
*trans
;
1390 struct btrfs_root
*root
= device
->dev_root
;
1391 struct btrfs_dev_extent
*dev_extent
= NULL
;
1392 struct btrfs_path
*path
;
1399 struct extent_buffer
*l
;
1400 struct btrfs_key key
;
1401 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1402 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1403 u64 diff
= device
->total_bytes
- new_size
;
1406 path
= btrfs_alloc_path();
1410 trans
= btrfs_start_transaction(root
, 1);
1418 device
->total_bytes
= new_size
;
1419 ret
= btrfs_update_device(trans
, device
);
1421 btrfs_end_transaction(trans
, root
);
1424 WARN_ON(diff
> old_total
);
1425 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
1426 btrfs_end_transaction(trans
, root
);
1428 key
.objectid
= device
->devid
;
1429 key
.offset
= (u64
)-1;
1430 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1433 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1437 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
1446 slot
= path
->slots
[0];
1447 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
1449 if (key
.objectid
!= device
->devid
)
1452 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1453 length
= btrfs_dev_extent_length(l
, dev_extent
);
1455 if (key
.offset
+ length
<= new_size
)
1458 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
1459 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
1460 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
1461 btrfs_release_path(root
, path
);
1463 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
1470 btrfs_free_path(path
);
1474 int btrfs_add_system_chunk(struct btrfs_trans_handle
*trans
,
1475 struct btrfs_root
*root
,
1476 struct btrfs_key
*key
,
1477 struct btrfs_chunk
*chunk
, int item_size
)
1479 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1480 struct btrfs_disk_key disk_key
;
1484 array_size
= btrfs_super_sys_array_size(super_copy
);
1485 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
1488 ptr
= super_copy
->sys_chunk_array
+ array_size
;
1489 btrfs_cpu_key_to_disk(&disk_key
, key
);
1490 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
1491 ptr
+= sizeof(disk_key
);
1492 memcpy(ptr
, chunk
, item_size
);
1493 item_size
+= sizeof(disk_key
);
1494 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
1498 static u64
chunk_bytes_by_type(u64 type
, u64 calc_size
, int num_stripes
,
1501 if (type
& (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_DUP
))
1503 else if (type
& BTRFS_BLOCK_GROUP_RAID10
)
1504 return calc_size
* (num_stripes
/ sub_stripes
);
1506 return calc_size
* num_stripes
;
1510 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
1511 struct btrfs_root
*extent_root
, u64
*start
,
1512 u64
*num_bytes
, u64 type
)
1515 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
1516 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
1517 struct btrfs_path
*path
;
1518 struct btrfs_stripe
*stripes
;
1519 struct btrfs_device
*device
= NULL
;
1520 struct btrfs_chunk
*chunk
;
1521 struct list_head private_devs
;
1522 struct list_head
*dev_list
;
1523 struct list_head
*cur
;
1524 struct extent_map_tree
*em_tree
;
1525 struct map_lookup
*map
;
1526 struct extent_map
*em
;
1527 int min_stripe_size
= 1 * 1024 * 1024;
1529 u64 calc_size
= 1024 * 1024 * 1024;
1530 u64 max_chunk_size
= calc_size
;
1535 int num_stripes
= 1;
1536 int min_stripes
= 1;
1537 int sub_stripes
= 0;
1541 int stripe_len
= 64 * 1024;
1542 struct btrfs_key key
;
1544 if ((type
& BTRFS_BLOCK_GROUP_RAID1
) &&
1545 (type
& BTRFS_BLOCK_GROUP_DUP
)) {
1547 type
&= ~BTRFS_BLOCK_GROUP_DUP
;
1549 dev_list
= &extent_root
->fs_info
->fs_devices
->alloc_list
;
1550 if (list_empty(dev_list
))
1553 if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
1554 num_stripes
= extent_root
->fs_info
->fs_devices
->open_devices
;
1557 if (type
& (BTRFS_BLOCK_GROUP_DUP
)) {
1561 if (type
& (BTRFS_BLOCK_GROUP_RAID1
)) {
1562 num_stripes
= min_t(u64
, 2,
1563 extent_root
->fs_info
->fs_devices
->open_devices
);
1564 if (num_stripes
< 2)
1568 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
1569 num_stripes
= extent_root
->fs_info
->fs_devices
->open_devices
;
1570 if (num_stripes
< 4)
1572 num_stripes
&= ~(u32
)1;
1577 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
1578 max_chunk_size
= 10 * calc_size
;
1579 min_stripe_size
= 64 * 1024 * 1024;
1580 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
1581 max_chunk_size
= 4 * calc_size
;
1582 min_stripe_size
= 32 * 1024 * 1024;
1583 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1584 calc_size
= 8 * 1024 * 1024;
1585 max_chunk_size
= calc_size
* 2;
1586 min_stripe_size
= 1 * 1024 * 1024;
1589 path
= btrfs_alloc_path();
1593 /* we don't want a chunk larger than 10% of the FS */
1594 percent_max
= div_factor(btrfs_super_total_bytes(&info
->super_copy
), 1);
1595 max_chunk_size
= min(percent_max
, max_chunk_size
);
1598 if (calc_size
* num_stripes
> max_chunk_size
) {
1599 calc_size
= max_chunk_size
;
1600 do_div(calc_size
, num_stripes
);
1601 do_div(calc_size
, stripe_len
);
1602 calc_size
*= stripe_len
;
1604 /* we don't want tiny stripes */
1605 calc_size
= max_t(u64
, min_stripe_size
, calc_size
);
1607 do_div(calc_size
, stripe_len
);
1608 calc_size
*= stripe_len
;
1610 INIT_LIST_HEAD(&private_devs
);
1611 cur
= dev_list
->next
;
1614 if (type
& BTRFS_BLOCK_GROUP_DUP
)
1615 min_free
= calc_size
* 2;
1617 min_free
= calc_size
;
1619 /* we add 1MB because we never use the first 1MB of the device */
1620 min_free
+= 1024 * 1024;
1622 /* build a private list of devices we will allocate from */
1623 while(index
< num_stripes
) {
1624 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
1626 if (device
->total_bytes
> device
->bytes_used
)
1627 avail
= device
->total_bytes
- device
->bytes_used
;
1632 if (device
->in_fs_metadata
&& avail
>= min_free
) {
1633 u64 ignored_start
= 0;
1634 ret
= find_free_dev_extent(trans
, device
, path
,
1638 list_move_tail(&device
->dev_alloc_list
,
1641 if (type
& BTRFS_BLOCK_GROUP_DUP
)
1644 } else if (device
->in_fs_metadata
&& avail
> max_avail
)
1646 if (cur
== dev_list
)
1649 if (index
< num_stripes
) {
1650 list_splice(&private_devs
, dev_list
);
1651 if (index
>= min_stripes
) {
1652 num_stripes
= index
;
1653 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
1654 num_stripes
/= sub_stripes
;
1655 num_stripes
*= sub_stripes
;
1660 if (!looped
&& max_avail
> 0) {
1662 calc_size
= max_avail
;
1665 btrfs_free_path(path
);
1668 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1669 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1670 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
1673 btrfs_free_path(path
);
1677 chunk
= kmalloc(btrfs_chunk_item_size(num_stripes
), GFP_NOFS
);
1679 btrfs_free_path(path
);
1683 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
1686 btrfs_free_path(path
);
1689 btrfs_free_path(path
);
1692 stripes
= &chunk
->stripe
;
1693 *num_bytes
= chunk_bytes_by_type(type
, calc_size
,
1694 num_stripes
, sub_stripes
);
1697 while(index
< num_stripes
) {
1698 struct btrfs_stripe
*stripe
;
1699 BUG_ON(list_empty(&private_devs
));
1700 cur
= private_devs
.next
;
1701 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
1703 /* loop over this device again if we're doing a dup group */
1704 if (!(type
& BTRFS_BLOCK_GROUP_DUP
) ||
1705 (index
== num_stripes
- 1))
1706 list_move_tail(&device
->dev_alloc_list
, dev_list
);
1708 ret
= btrfs_alloc_dev_extent(trans
, device
,
1709 info
->chunk_root
->root_key
.objectid
,
1710 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, key
.offset
,
1711 calc_size
, &dev_offset
);
1713 device
->bytes_used
+= calc_size
;
1714 ret
= btrfs_update_device(trans
, device
);
1717 map
->stripes
[index
].dev
= device
;
1718 map
->stripes
[index
].physical
= dev_offset
;
1719 stripe
= stripes
+ index
;
1720 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
1721 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
1722 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
1723 physical
= dev_offset
;
1726 BUG_ON(!list_empty(&private_devs
));
1728 /* key was set above */
1729 btrfs_set_stack_chunk_length(chunk
, *num_bytes
);
1730 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
1731 btrfs_set_stack_chunk_stripe_len(chunk
, stripe_len
);
1732 btrfs_set_stack_chunk_type(chunk
, type
);
1733 btrfs_set_stack_chunk_num_stripes(chunk
, num_stripes
);
1734 btrfs_set_stack_chunk_io_align(chunk
, stripe_len
);
1735 btrfs_set_stack_chunk_io_width(chunk
, stripe_len
);
1736 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
1737 btrfs_set_stack_chunk_sub_stripes(chunk
, sub_stripes
);
1738 map
->sector_size
= extent_root
->sectorsize
;
1739 map
->stripe_len
= stripe_len
;
1740 map
->io_align
= stripe_len
;
1741 map
->io_width
= stripe_len
;
1743 map
->num_stripes
= num_stripes
;
1744 map
->sub_stripes
= sub_stripes
;
1746 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
,
1747 btrfs_chunk_item_size(num_stripes
));
1749 *start
= key
.offset
;;
1751 em
= alloc_extent_map(GFP_NOFS
);
1754 em
->bdev
= (struct block_device
*)map
;
1755 em
->start
= key
.offset
;
1756 em
->len
= *num_bytes
;
1757 em
->block_start
= 0;
1759 if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1760 ret
= btrfs_add_system_chunk(trans
, chunk_root
, &key
,
1761 chunk
, btrfs_chunk_item_size(num_stripes
));
1766 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
1767 spin_lock(&em_tree
->lock
);
1768 ret
= add_extent_mapping(em_tree
, em
);
1769 spin_unlock(&em_tree
->lock
);
1771 free_extent_map(em
);
1775 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
1777 extent_map_tree_init(&tree
->map_tree
, GFP_NOFS
);
1780 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
1782 struct extent_map
*em
;
1785 spin_lock(&tree
->map_tree
.lock
);
1786 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
1788 remove_extent_mapping(&tree
->map_tree
, em
);
1789 spin_unlock(&tree
->map_tree
.lock
);
1794 free_extent_map(em
);
1795 /* once for the tree */
1796 free_extent_map(em
);
1800 int btrfs_num_copies(struct btrfs_mapping_tree
*map_tree
, u64 logical
, u64 len
)
1802 struct extent_map
*em
;
1803 struct map_lookup
*map
;
1804 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
1807 spin_lock(&em_tree
->lock
);
1808 em
= lookup_extent_mapping(em_tree
, logical
, len
);
1809 spin_unlock(&em_tree
->lock
);
1812 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
1813 map
= (struct map_lookup
*)em
->bdev
;
1814 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
1815 ret
= map
->num_stripes
;
1816 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
1817 ret
= map
->sub_stripes
;
1820 free_extent_map(em
);
1824 static int find_live_mirror(struct map_lookup
*map
, int first
, int num
,
1828 if (map
->stripes
[optimal
].dev
->bdev
)
1830 for (i
= first
; i
< first
+ num
; i
++) {
1831 if (map
->stripes
[i
].dev
->bdev
)
1834 /* we couldn't find one that doesn't fail. Just return something
1835 * and the io error handling code will clean up eventually
1840 static int __btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
1841 u64 logical
, u64
*length
,
1842 struct btrfs_multi_bio
**multi_ret
,
1843 int mirror_num
, struct page
*unplug_page
)
1845 struct extent_map
*em
;
1846 struct map_lookup
*map
;
1847 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
1851 int stripes_allocated
= 8;
1852 int stripes_required
= 1;
1857 struct btrfs_multi_bio
*multi
= NULL
;
1859 if (multi_ret
&& !(rw
& (1 << BIO_RW
))) {
1860 stripes_allocated
= 1;
1864 multi
= kzalloc(btrfs_multi_bio_size(stripes_allocated
),
1869 atomic_set(&multi
->error
, 0);
1872 spin_lock(&em_tree
->lock
);
1873 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
1874 spin_unlock(&em_tree
->lock
);
1876 if (!em
&& unplug_page
)
1880 printk("unable to find logical %Lu len %Lu\n", logical
, *length
);
1884 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
1885 map
= (struct map_lookup
*)em
->bdev
;
1886 offset
= logical
- em
->start
;
1888 if (mirror_num
> map
->num_stripes
)
1891 /* if our multi bio struct is too small, back off and try again */
1892 if (rw
& (1 << BIO_RW
)) {
1893 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
1894 BTRFS_BLOCK_GROUP_DUP
)) {
1895 stripes_required
= map
->num_stripes
;
1897 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
1898 stripes_required
= map
->sub_stripes
;
1902 if (multi_ret
&& rw
== WRITE
&&
1903 stripes_allocated
< stripes_required
) {
1904 stripes_allocated
= map
->num_stripes
;
1905 free_extent_map(em
);
1911 * stripe_nr counts the total number of stripes we have to stride
1912 * to get to this block
1914 do_div(stripe_nr
, map
->stripe_len
);
1916 stripe_offset
= stripe_nr
* map
->stripe_len
;
1917 BUG_ON(offset
< stripe_offset
);
1919 /* stripe_offset is the offset of this block in its stripe*/
1920 stripe_offset
= offset
- stripe_offset
;
1922 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
1923 BTRFS_BLOCK_GROUP_RAID10
|
1924 BTRFS_BLOCK_GROUP_DUP
)) {
1925 /* we limit the length of each bio to what fits in a stripe */
1926 *length
= min_t(u64
, em
->len
- offset
,
1927 map
->stripe_len
- stripe_offset
);
1929 *length
= em
->len
- offset
;
1932 if (!multi_ret
&& !unplug_page
)
1937 if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
1938 if (unplug_page
|| (rw
& (1 << BIO_RW
)))
1939 num_stripes
= map
->num_stripes
;
1940 else if (mirror_num
)
1941 stripe_index
= mirror_num
- 1;
1943 stripe_index
= find_live_mirror(map
, 0,
1945 current
->pid
% map
->num_stripes
);
1948 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
1949 if (rw
& (1 << BIO_RW
))
1950 num_stripes
= map
->num_stripes
;
1951 else if (mirror_num
)
1952 stripe_index
= mirror_num
- 1;
1954 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
1955 int factor
= map
->num_stripes
/ map
->sub_stripes
;
1957 stripe_index
= do_div(stripe_nr
, factor
);
1958 stripe_index
*= map
->sub_stripes
;
1960 if (unplug_page
|| (rw
& (1 << BIO_RW
)))
1961 num_stripes
= map
->sub_stripes
;
1962 else if (mirror_num
)
1963 stripe_index
+= mirror_num
- 1;
1965 stripe_index
= find_live_mirror(map
, stripe_index
,
1966 map
->sub_stripes
, stripe_index
+
1967 current
->pid
% map
->sub_stripes
);
1971 * after this do_div call, stripe_nr is the number of stripes
1972 * on this device we have to walk to find the data, and
1973 * stripe_index is the number of our device in the stripe array
1975 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
1977 BUG_ON(stripe_index
>= map
->num_stripes
);
1979 for (i
= 0; i
< num_stripes
; i
++) {
1981 struct btrfs_device
*device
;
1982 struct backing_dev_info
*bdi
;
1984 device
= map
->stripes
[stripe_index
].dev
;
1986 bdi
= blk_get_backing_dev_info(device
->bdev
);
1987 if (bdi
->unplug_io_fn
) {
1988 bdi
->unplug_io_fn(bdi
, unplug_page
);
1992 multi
->stripes
[i
].physical
=
1993 map
->stripes
[stripe_index
].physical
+
1994 stripe_offset
+ stripe_nr
* map
->stripe_len
;
1995 multi
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
2001 multi
->num_stripes
= num_stripes
;
2002 multi
->max_errors
= max_errors
;
2005 free_extent_map(em
);
2009 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
2010 u64 logical
, u64
*length
,
2011 struct btrfs_multi_bio
**multi_ret
, int mirror_num
)
2013 return __btrfs_map_block(map_tree
, rw
, logical
, length
, multi_ret
,
2017 int btrfs_unplug_page(struct btrfs_mapping_tree
*map_tree
,
2018 u64 logical
, struct page
*page
)
2020 u64 length
= PAGE_CACHE_SIZE
;
2021 return __btrfs_map_block(map_tree
, READ
, logical
, &length
,
2026 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2027 static void end_bio_multi_stripe(struct bio
*bio
, int err
)
2029 static int end_bio_multi_stripe(struct bio
*bio
,
2030 unsigned int bytes_done
, int err
)
2033 struct btrfs_multi_bio
*multi
= bio
->bi_private
;
2035 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2040 atomic_inc(&multi
->error
);
2042 if (atomic_dec_and_test(&multi
->stripes_pending
)) {
2043 bio
->bi_private
= multi
->private;
2044 bio
->bi_end_io
= multi
->end_io
;
2045 /* only send an error to the higher layers if it is
2046 * beyond the tolerance of the multi-bio
2048 if (atomic_read(&multi
->error
) > multi
->max_errors
) {
2052 * this bio is actually up to date, we didn't
2053 * go over the max number of errors
2055 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
2060 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2061 bio_endio(bio
, bio
->bi_size
, err
);
2063 bio_endio(bio
, err
);
2068 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2073 struct async_sched
{
2076 struct btrfs_fs_info
*info
;
2077 struct btrfs_work work
;
2081 * see run_scheduled_bios for a description of why bios are collected for
2084 * This will add one bio to the pending list for a device and make sure
2085 * the work struct is scheduled.
2087 int schedule_bio(struct btrfs_root
*root
, struct btrfs_device
*device
,
2088 int rw
, struct bio
*bio
)
2090 int should_queue
= 1;
2092 /* don't bother with additional async steps for reads, right now */
2093 if (!(rw
& (1 << BIO_RW
))) {
2094 submit_bio(rw
, bio
);
2099 * nr_async_sumbits allows us to reliably return congestion to the
2100 * higher layers. Otherwise, the async bio makes it appear we have
2101 * made progress against dirty pages when we've really just put it
2102 * on a queue for later
2104 atomic_inc(&root
->fs_info
->nr_async_submits
);
2105 bio
->bi_next
= NULL
;
2108 spin_lock(&device
->io_lock
);
2110 if (device
->pending_bio_tail
)
2111 device
->pending_bio_tail
->bi_next
= bio
;
2113 device
->pending_bio_tail
= bio
;
2114 if (!device
->pending_bios
)
2115 device
->pending_bios
= bio
;
2116 if (device
->running_pending
)
2119 spin_unlock(&device
->io_lock
);
2122 btrfs_queue_worker(&root
->fs_info
->submit_workers
,
2127 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
2128 int mirror_num
, int async_submit
)
2130 struct btrfs_mapping_tree
*map_tree
;
2131 struct btrfs_device
*dev
;
2132 struct bio
*first_bio
= bio
;
2133 u64 logical
= bio
->bi_sector
<< 9;
2136 struct btrfs_multi_bio
*multi
= NULL
;
2141 length
= bio
->bi_size
;
2142 map_tree
= &root
->fs_info
->mapping_tree
;
2143 map_length
= length
;
2145 ret
= btrfs_map_block(map_tree
, rw
, logical
, &map_length
, &multi
,
2149 total_devs
= multi
->num_stripes
;
2150 if (map_length
< length
) {
2151 printk("mapping failed logical %Lu bio len %Lu "
2152 "len %Lu\n", logical
, length
, map_length
);
2155 multi
->end_io
= first_bio
->bi_end_io
;
2156 multi
->private = first_bio
->bi_private
;
2157 atomic_set(&multi
->stripes_pending
, multi
->num_stripes
);
2159 while(dev_nr
< total_devs
) {
2160 if (total_devs
> 1) {
2161 if (dev_nr
< total_devs
- 1) {
2162 bio
= bio_clone(first_bio
, GFP_NOFS
);
2167 bio
->bi_private
= multi
;
2168 bio
->bi_end_io
= end_bio_multi_stripe
;
2170 bio
->bi_sector
= multi
->stripes
[dev_nr
].physical
>> 9;
2171 dev
= multi
->stripes
[dev_nr
].dev
;
2172 if (dev
&& dev
->bdev
) {
2173 bio
->bi_bdev
= dev
->bdev
;
2175 schedule_bio(root
, dev
, rw
, bio
);
2177 submit_bio(rw
, bio
);
2179 bio
->bi_bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2180 bio
->bi_sector
= logical
>> 9;
2181 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2182 bio_endio(bio
, bio
->bi_size
, -EIO
);
2184 bio_endio(bio
, -EIO
);
2189 if (total_devs
== 1)
2194 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
,
2197 struct list_head
*head
= &root
->fs_info
->fs_devices
->devices
;
2199 return __find_device(head
, devid
, uuid
);
2202 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
2203 u64 devid
, u8
*dev_uuid
)
2205 struct btrfs_device
*device
;
2206 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
2208 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
2209 list_add(&device
->dev_list
,
2210 &fs_devices
->devices
);
2211 list_add(&device
->dev_alloc_list
,
2212 &fs_devices
->alloc_list
);
2213 device
->barriers
= 1;
2214 device
->dev_root
= root
->fs_info
->dev_root
;
2215 device
->devid
= devid
;
2216 device
->work
.func
= pending_bios_fn
;
2217 fs_devices
->num_devices
++;
2218 spin_lock_init(&device
->io_lock
);
2219 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
2224 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
2225 struct extent_buffer
*leaf
,
2226 struct btrfs_chunk
*chunk
)
2228 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
2229 struct map_lookup
*map
;
2230 struct extent_map
*em
;
2234 u8 uuid
[BTRFS_UUID_SIZE
];
2239 logical
= key
->offset
;
2240 length
= btrfs_chunk_length(leaf
, chunk
);
2242 spin_lock(&map_tree
->map_tree
.lock
);
2243 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
2244 spin_unlock(&map_tree
->map_tree
.lock
);
2246 /* already mapped? */
2247 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
2248 free_extent_map(em
);
2251 free_extent_map(em
);
2254 map
= kzalloc(sizeof(*map
), GFP_NOFS
);
2258 em
= alloc_extent_map(GFP_NOFS
);
2261 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2262 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
2264 free_extent_map(em
);
2268 em
->bdev
= (struct block_device
*)map
;
2269 em
->start
= logical
;
2271 em
->block_start
= 0;
2273 map
->num_stripes
= num_stripes
;
2274 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
2275 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
2276 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
2277 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
2278 map
->type
= btrfs_chunk_type(leaf
, chunk
);
2279 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
2280 for (i
= 0; i
< num_stripes
; i
++) {
2281 map
->stripes
[i
].physical
=
2282 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
2283 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
2284 read_extent_buffer(leaf
, uuid
, (unsigned long)
2285 btrfs_stripe_dev_uuid_nr(chunk
, i
),
2287 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
, uuid
);
2289 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
2291 free_extent_map(em
);
2294 if (!map
->stripes
[i
].dev
) {
2295 map
->stripes
[i
].dev
=
2296 add_missing_dev(root
, devid
, uuid
);
2297 if (!map
->stripes
[i
].dev
) {
2299 free_extent_map(em
);
2303 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
2306 spin_lock(&map_tree
->map_tree
.lock
);
2307 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
2308 spin_unlock(&map_tree
->map_tree
.lock
);
2310 free_extent_map(em
);
2315 static int fill_device_from_item(struct extent_buffer
*leaf
,
2316 struct btrfs_dev_item
*dev_item
,
2317 struct btrfs_device
*device
)
2321 device
->devid
= btrfs_device_id(leaf
, dev_item
);
2322 device
->total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
2323 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
2324 device
->type
= btrfs_device_type(leaf
, dev_item
);
2325 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
2326 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
2327 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
2329 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
2330 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
2335 static int read_one_dev(struct btrfs_root
*root
,
2336 struct extent_buffer
*leaf
,
2337 struct btrfs_dev_item
*dev_item
)
2339 struct btrfs_device
*device
;
2342 u8 dev_uuid
[BTRFS_UUID_SIZE
];
2344 devid
= btrfs_device_id(leaf
, dev_item
);
2345 read_extent_buffer(leaf
, dev_uuid
,
2346 (unsigned long)btrfs_device_uuid(dev_item
),
2348 device
= btrfs_find_device(root
, devid
, dev_uuid
);
2350 printk("warning devid %Lu missing\n", devid
);
2351 device
= add_missing_dev(root
, devid
, dev_uuid
);
2356 fill_device_from_item(leaf
, dev_item
, device
);
2357 device
->dev_root
= root
->fs_info
->dev_root
;
2358 device
->in_fs_metadata
= 1;
2361 ret
= btrfs_open_device(device
);
2369 int btrfs_read_super_device(struct btrfs_root
*root
, struct extent_buffer
*buf
)
2371 struct btrfs_dev_item
*dev_item
;
2373 dev_item
= (struct btrfs_dev_item
*)offsetof(struct btrfs_super_block
,
2375 return read_one_dev(root
, buf
, dev_item
);
2378 int btrfs_read_sys_array(struct btrfs_root
*root
)
2380 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
2381 struct extent_buffer
*sb
;
2382 struct btrfs_disk_key
*disk_key
;
2383 struct btrfs_chunk
*chunk
;
2385 unsigned long sb_ptr
;
2391 struct btrfs_key key
;
2393 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
2394 BTRFS_SUPER_INFO_SIZE
);
2397 btrfs_set_buffer_uptodate(sb
);
2398 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
2399 array_size
= btrfs_super_sys_array_size(super_copy
);
2401 ptr
= super_copy
->sys_chunk_array
;
2402 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
2405 while (cur
< array_size
) {
2406 disk_key
= (struct btrfs_disk_key
*)ptr
;
2407 btrfs_disk_key_to_cpu(&key
, disk_key
);
2409 len
= sizeof(*disk_key
); ptr
+= len
;
2413 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
2414 chunk
= (struct btrfs_chunk
*)sb_ptr
;
2415 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
2418 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
2419 len
= btrfs_chunk_item_size(num_stripes
);
2428 free_extent_buffer(sb
);
2432 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
2434 struct btrfs_path
*path
;
2435 struct extent_buffer
*leaf
;
2436 struct btrfs_key key
;
2437 struct btrfs_key found_key
;
2441 root
= root
->fs_info
->chunk_root
;
2443 path
= btrfs_alloc_path();
2447 /* first we search for all of the device items, and then we
2448 * read in all of the chunk items. This way we can create chunk
2449 * mappings that reference all of the devices that are afound
2451 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
2455 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2457 leaf
= path
->nodes
[0];
2458 slot
= path
->slots
[0];
2459 if (slot
>= btrfs_header_nritems(leaf
)) {
2460 ret
= btrfs_next_leaf(root
, path
);
2467 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2468 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
2469 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
2471 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
2472 struct btrfs_dev_item
*dev_item
;
2473 dev_item
= btrfs_item_ptr(leaf
, slot
,
2474 struct btrfs_dev_item
);
2475 ret
= read_one_dev(root
, leaf
, dev_item
);
2478 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
2479 struct btrfs_chunk
*chunk
;
2480 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
2481 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
2485 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
2487 btrfs_release_path(root
, path
);
2491 btrfs_free_path(path
);