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
->fs_mutex
);
870 mutex_lock(&uuid_mutex
);
872 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
873 root
->fs_info
->avail_system_alloc_bits
|
874 root
->fs_info
->avail_metadata_alloc_bits
;
876 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) &&
877 btrfs_super_num_devices(&root
->fs_info
->super_copy
) <= 4) {
878 printk("btrfs: unable to go below four devices on raid10\n");
883 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) &&
884 btrfs_super_num_devices(&root
->fs_info
->super_copy
) <= 2) {
885 printk("btrfs: unable to go below two devices on raid1\n");
890 if (strcmp(device_path
, "missing") == 0) {
891 struct list_head
*cur
;
892 struct list_head
*devices
;
893 struct btrfs_device
*tmp
;
896 devices
= &root
->fs_info
->fs_devices
->devices
;
897 list_for_each(cur
, devices
) {
898 tmp
= list_entry(cur
, struct btrfs_device
, dev_list
);
899 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
908 printk("btrfs: no missing devices found to remove\n");
913 bdev
= open_bdev_excl(device_path
, 0,
914 root
->fs_info
->bdev_holder
);
920 bh
= __bread(bdev
, BTRFS_SUPER_INFO_OFFSET
/ 4096, 4096);
925 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
926 if (strncmp((char *)(&disk_super
->magic
), BTRFS_MAGIC
,
927 sizeof(disk_super
->magic
))) {
931 if (memcmp(disk_super
->fsid
, root
->fs_info
->fsid
,
936 devid
= le64_to_cpu(disk_super
->dev_item
.devid
);
937 device
= btrfs_find_device(root
, devid
, NULL
);
944 root
->fs_info
->fs_devices
->num_devices
--;
945 root
->fs_info
->fs_devices
->open_devices
--;
947 ret
= btrfs_shrink_device(device
, 0);
952 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
957 /* make sure this device isn't detected as part of
960 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
961 set_buffer_dirty(bh
);
962 sync_dirty_buffer(bh
);
968 /* one close for the device struct or super_block */
969 close_bdev_excl(device
->bdev
);
972 /* one close for us */
973 close_bdev_excl(bdev
);
984 close_bdev_excl(bdev
);
986 mutex_unlock(&uuid_mutex
);
987 mutex_unlock(&root
->fs_info
->fs_mutex
);
991 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
993 struct btrfs_trans_handle
*trans
;
994 struct btrfs_device
*device
;
995 struct block_device
*bdev
;
996 struct list_head
*cur
;
997 struct list_head
*devices
;
1002 bdev
= open_bdev_excl(device_path
, 0, root
->fs_info
->bdev_holder
);
1006 mutex_lock(&root
->fs_info
->fs_mutex
);
1007 trans
= btrfs_start_transaction(root
, 1);
1008 devices
= &root
->fs_info
->fs_devices
->devices
;
1009 list_for_each(cur
, devices
) {
1010 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
1011 if (device
->bdev
== bdev
) {
1017 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1019 /* we can safely leave the fs_devices entry around */
1021 goto out_close_bdev
;
1024 device
->barriers
= 1;
1025 device
->work
.func
= pending_bios_fn
;
1026 generate_random_uuid(device
->uuid
);
1027 spin_lock_init(&device
->io_lock
);
1028 device
->name
= kstrdup(device_path
, GFP_NOFS
);
1029 if (!device
->name
) {
1031 goto out_close_bdev
;
1033 device
->io_width
= root
->sectorsize
;
1034 device
->io_align
= root
->sectorsize
;
1035 device
->sector_size
= root
->sectorsize
;
1036 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1037 device
->dev_root
= root
->fs_info
->dev_root
;
1038 device
->bdev
= bdev
;
1039 device
->in_fs_metadata
= 1;
1041 ret
= btrfs_add_device(trans
, root
, device
);
1043 goto out_close_bdev
;
1045 total_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
1046 btrfs_set_super_total_bytes(&root
->fs_info
->super_copy
,
1047 total_bytes
+ device
->total_bytes
);
1049 total_bytes
= btrfs_super_num_devices(&root
->fs_info
->super_copy
);
1050 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
,
1053 list_add(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
1054 list_add(&device
->dev_alloc_list
,
1055 &root
->fs_info
->fs_devices
->alloc_list
);
1056 root
->fs_info
->fs_devices
->num_devices
++;
1057 root
->fs_info
->fs_devices
->open_devices
++;
1059 btrfs_end_transaction(trans
, root
);
1060 mutex_unlock(&root
->fs_info
->fs_mutex
);
1064 close_bdev_excl(bdev
);
1068 int btrfs_update_device(struct btrfs_trans_handle
*trans
,
1069 struct btrfs_device
*device
)
1072 struct btrfs_path
*path
;
1073 struct btrfs_root
*root
;
1074 struct btrfs_dev_item
*dev_item
;
1075 struct extent_buffer
*leaf
;
1076 struct btrfs_key key
;
1078 root
= device
->dev_root
->fs_info
->chunk_root
;
1080 path
= btrfs_alloc_path();
1084 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1085 key
.type
= BTRFS_DEV_ITEM_KEY
;
1086 key
.offset
= device
->devid
;
1088 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1097 leaf
= path
->nodes
[0];
1098 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1100 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1101 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1102 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1103 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1104 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1105 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1106 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1107 btrfs_mark_buffer_dirty(leaf
);
1110 btrfs_free_path(path
);
1114 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1115 struct btrfs_device
*device
, u64 new_size
)
1117 struct btrfs_super_block
*super_copy
=
1118 &device
->dev_root
->fs_info
->super_copy
;
1119 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1120 u64 diff
= new_size
- device
->total_bytes
;
1122 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
1123 return btrfs_update_device(trans
, device
);
1126 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
1127 struct btrfs_root
*root
,
1128 u64 chunk_tree
, u64 chunk_objectid
,
1132 struct btrfs_path
*path
;
1133 struct btrfs_key key
;
1135 root
= root
->fs_info
->chunk_root
;
1136 path
= btrfs_alloc_path();
1140 key
.objectid
= chunk_objectid
;
1141 key
.offset
= chunk_offset
;
1142 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1144 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1147 ret
= btrfs_del_item(trans
, root
, path
);
1150 btrfs_free_path(path
);
1154 int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
1157 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1158 struct btrfs_disk_key
*disk_key
;
1159 struct btrfs_chunk
*chunk
;
1166 struct btrfs_key key
;
1168 array_size
= btrfs_super_sys_array_size(super_copy
);
1170 ptr
= super_copy
->sys_chunk_array
;
1173 while (cur
< array_size
) {
1174 disk_key
= (struct btrfs_disk_key
*)ptr
;
1175 btrfs_disk_key_to_cpu(&key
, disk_key
);
1177 len
= sizeof(*disk_key
);
1179 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1180 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
1181 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
1182 len
+= btrfs_chunk_item_size(num_stripes
);
1187 if (key
.objectid
== chunk_objectid
&&
1188 key
.offset
== chunk_offset
) {
1189 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
1191 btrfs_set_super_sys_array_size(super_copy
, array_size
);
1201 int btrfs_relocate_chunk(struct btrfs_root
*root
,
1202 u64 chunk_tree
, u64 chunk_objectid
,
1205 struct extent_map_tree
*em_tree
;
1206 struct btrfs_root
*extent_root
;
1207 struct btrfs_trans_handle
*trans
;
1208 struct extent_map
*em
;
1209 struct map_lookup
*map
;
1213 printk("btrfs relocating chunk %llu\n",
1214 (unsigned long long)chunk_offset
);
1215 root
= root
->fs_info
->chunk_root
;
1216 extent_root
= root
->fs_info
->extent_root
;
1217 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
1219 /* step one, relocate all the extents inside this chunk */
1220 ret
= btrfs_shrink_extent_tree(extent_root
, chunk_offset
);
1223 trans
= btrfs_start_transaction(root
, 1);
1227 * step two, delete the device extents and the
1228 * chunk tree entries
1230 spin_lock(&em_tree
->lock
);
1231 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
1232 spin_unlock(&em_tree
->lock
);
1234 BUG_ON(em
->start
> chunk_offset
||
1235 em
->start
+ em
->len
< chunk_offset
);
1236 map
= (struct map_lookup
*)em
->bdev
;
1238 for (i
= 0; i
< map
->num_stripes
; i
++) {
1239 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
1240 map
->stripes
[i
].physical
);
1243 if (map
->stripes
[i
].dev
) {
1244 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
1248 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
1253 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1254 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
1258 spin_lock(&em_tree
->lock
);
1259 remove_extent_mapping(em_tree
, em
);
1263 /* once for the tree */
1264 free_extent_map(em
);
1265 spin_unlock(&em_tree
->lock
);
1268 free_extent_map(em
);
1270 btrfs_end_transaction(trans
, root
);
1274 static u64
div_factor(u64 num
, int factor
)
1284 int btrfs_balance(struct btrfs_root
*dev_root
)
1287 struct list_head
*cur
;
1288 struct list_head
*devices
= &dev_root
->fs_info
->fs_devices
->devices
;
1289 struct btrfs_device
*device
;
1292 struct btrfs_path
*path
;
1293 struct btrfs_key key
;
1294 struct btrfs_chunk
*chunk
;
1295 struct btrfs_root
*chunk_root
= dev_root
->fs_info
->chunk_root
;
1296 struct btrfs_trans_handle
*trans
;
1297 struct btrfs_key found_key
;
1300 dev_root
= dev_root
->fs_info
->dev_root
;
1302 mutex_lock(&dev_root
->fs_info
->fs_mutex
);
1303 /* step one make some room on all the devices */
1304 list_for_each(cur
, devices
) {
1305 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
1306 old_size
= device
->total_bytes
;
1307 size_to_free
= div_factor(old_size
, 1);
1308 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
1309 if (device
->total_bytes
- device
->bytes_used
> size_to_free
)
1312 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
1315 trans
= btrfs_start_transaction(dev_root
, 1);
1318 ret
= btrfs_grow_device(trans
, device
, old_size
);
1321 btrfs_end_transaction(trans
, dev_root
);
1324 /* step two, relocate all the chunks */
1325 path
= btrfs_alloc_path();
1328 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1329 key
.offset
= (u64
)-1;
1330 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1333 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
1338 * this shouldn't happen, it means the last relocate
1344 ret
= btrfs_previous_item(chunk_root
, path
, 0,
1345 BTRFS_CHUNK_ITEM_KEY
);
1349 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1351 if (found_key
.objectid
!= key
.objectid
)
1353 chunk
= btrfs_item_ptr(path
->nodes
[0],
1355 struct btrfs_chunk
);
1356 key
.offset
= found_key
.offset
;
1357 /* chunk zero is special */
1358 if (key
.offset
== 0)
1361 ret
= btrfs_relocate_chunk(chunk_root
,
1362 chunk_root
->root_key
.objectid
,
1366 btrfs_release_path(chunk_root
, path
);
1370 btrfs_free_path(path
);
1371 mutex_unlock(&dev_root
->fs_info
->fs_mutex
);
1376 * shrinking a device means finding all of the device extents past
1377 * the new size, and then following the back refs to the chunks.
1378 * The chunk relocation code actually frees the device extent
1380 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
1382 struct btrfs_trans_handle
*trans
;
1383 struct btrfs_root
*root
= device
->dev_root
;
1384 struct btrfs_dev_extent
*dev_extent
= NULL
;
1385 struct btrfs_path
*path
;
1392 struct extent_buffer
*l
;
1393 struct btrfs_key key
;
1394 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1395 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1396 u64 diff
= device
->total_bytes
- new_size
;
1399 path
= btrfs_alloc_path();
1403 trans
= btrfs_start_transaction(root
, 1);
1411 device
->total_bytes
= new_size
;
1412 ret
= btrfs_update_device(trans
, device
);
1414 btrfs_end_transaction(trans
, root
);
1417 WARN_ON(diff
> old_total
);
1418 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
1419 btrfs_end_transaction(trans
, root
);
1421 key
.objectid
= device
->devid
;
1422 key
.offset
= (u64
)-1;
1423 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1426 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1430 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
1439 slot
= path
->slots
[0];
1440 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
1442 if (key
.objectid
!= device
->devid
)
1445 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1446 length
= btrfs_dev_extent_length(l
, dev_extent
);
1448 if (key
.offset
+ length
<= new_size
)
1451 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
1452 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
1453 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
1454 btrfs_release_path(root
, path
);
1456 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
1463 btrfs_free_path(path
);
1467 int btrfs_add_system_chunk(struct btrfs_trans_handle
*trans
,
1468 struct btrfs_root
*root
,
1469 struct btrfs_key
*key
,
1470 struct btrfs_chunk
*chunk
, int item_size
)
1472 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1473 struct btrfs_disk_key disk_key
;
1477 array_size
= btrfs_super_sys_array_size(super_copy
);
1478 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
1481 ptr
= super_copy
->sys_chunk_array
+ array_size
;
1482 btrfs_cpu_key_to_disk(&disk_key
, key
);
1483 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
1484 ptr
+= sizeof(disk_key
);
1485 memcpy(ptr
, chunk
, item_size
);
1486 item_size
+= sizeof(disk_key
);
1487 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
1491 static u64
chunk_bytes_by_type(u64 type
, u64 calc_size
, int num_stripes
,
1494 if (type
& (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_DUP
))
1496 else if (type
& BTRFS_BLOCK_GROUP_RAID10
)
1497 return calc_size
* (num_stripes
/ sub_stripes
);
1499 return calc_size
* num_stripes
;
1503 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
1504 struct btrfs_root
*extent_root
, u64
*start
,
1505 u64
*num_bytes
, u64 type
)
1508 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
1509 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
1510 struct btrfs_path
*path
;
1511 struct btrfs_stripe
*stripes
;
1512 struct btrfs_device
*device
= NULL
;
1513 struct btrfs_chunk
*chunk
;
1514 struct list_head private_devs
;
1515 struct list_head
*dev_list
;
1516 struct list_head
*cur
;
1517 struct extent_map_tree
*em_tree
;
1518 struct map_lookup
*map
;
1519 struct extent_map
*em
;
1520 int min_stripe_size
= 1 * 1024 * 1024;
1522 u64 calc_size
= 1024 * 1024 * 1024;
1523 u64 max_chunk_size
= calc_size
;
1528 int num_stripes
= 1;
1529 int min_stripes
= 1;
1530 int sub_stripes
= 0;
1534 int stripe_len
= 64 * 1024;
1535 struct btrfs_key key
;
1537 if ((type
& BTRFS_BLOCK_GROUP_RAID1
) &&
1538 (type
& BTRFS_BLOCK_GROUP_DUP
)) {
1540 type
&= ~BTRFS_BLOCK_GROUP_DUP
;
1542 dev_list
= &extent_root
->fs_info
->fs_devices
->alloc_list
;
1543 if (list_empty(dev_list
))
1546 if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
1547 num_stripes
= extent_root
->fs_info
->fs_devices
->open_devices
;
1550 if (type
& (BTRFS_BLOCK_GROUP_DUP
)) {
1554 if (type
& (BTRFS_BLOCK_GROUP_RAID1
)) {
1555 num_stripes
= min_t(u64
, 2,
1556 extent_root
->fs_info
->fs_devices
->open_devices
);
1557 if (num_stripes
< 2)
1561 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
1562 num_stripes
= extent_root
->fs_info
->fs_devices
->open_devices
;
1563 if (num_stripes
< 4)
1565 num_stripes
&= ~(u32
)1;
1570 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
1571 max_chunk_size
= 10 * calc_size
;
1572 min_stripe_size
= 64 * 1024 * 1024;
1573 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
1574 max_chunk_size
= 4 * calc_size
;
1575 min_stripe_size
= 32 * 1024 * 1024;
1576 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1577 calc_size
= 8 * 1024 * 1024;
1578 max_chunk_size
= calc_size
* 2;
1579 min_stripe_size
= 1 * 1024 * 1024;
1582 path
= btrfs_alloc_path();
1586 /* we don't want a chunk larger than 10% of the FS */
1587 percent_max
= div_factor(btrfs_super_total_bytes(&info
->super_copy
), 1);
1588 max_chunk_size
= min(percent_max
, max_chunk_size
);
1591 if (calc_size
* num_stripes
> max_chunk_size
) {
1592 calc_size
= max_chunk_size
;
1593 do_div(calc_size
, num_stripes
);
1594 do_div(calc_size
, stripe_len
);
1595 calc_size
*= stripe_len
;
1597 /* we don't want tiny stripes */
1598 calc_size
= max_t(u64
, min_stripe_size
, calc_size
);
1600 do_div(calc_size
, stripe_len
);
1601 calc_size
*= stripe_len
;
1603 INIT_LIST_HEAD(&private_devs
);
1604 cur
= dev_list
->next
;
1607 if (type
& BTRFS_BLOCK_GROUP_DUP
)
1608 min_free
= calc_size
* 2;
1610 min_free
= calc_size
;
1612 /* we add 1MB because we never use the first 1MB of the device */
1613 min_free
+= 1024 * 1024;
1615 /* build a private list of devices we will allocate from */
1616 while(index
< num_stripes
) {
1617 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
1619 if (device
->total_bytes
> device
->bytes_used
)
1620 avail
= device
->total_bytes
- device
->bytes_used
;
1625 if (device
->in_fs_metadata
&& avail
>= min_free
) {
1626 u64 ignored_start
= 0;
1627 ret
= find_free_dev_extent(trans
, device
, path
,
1631 list_move_tail(&device
->dev_alloc_list
,
1634 if (type
& BTRFS_BLOCK_GROUP_DUP
)
1637 } else if (device
->in_fs_metadata
&& avail
> max_avail
)
1639 if (cur
== dev_list
)
1642 if (index
< num_stripes
) {
1643 list_splice(&private_devs
, dev_list
);
1644 if (index
>= min_stripes
) {
1645 num_stripes
= index
;
1646 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
1647 num_stripes
/= sub_stripes
;
1648 num_stripes
*= sub_stripes
;
1653 if (!looped
&& max_avail
> 0) {
1655 calc_size
= max_avail
;
1658 btrfs_free_path(path
);
1661 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1662 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1663 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
1666 btrfs_free_path(path
);
1670 chunk
= kmalloc(btrfs_chunk_item_size(num_stripes
), GFP_NOFS
);
1672 btrfs_free_path(path
);
1676 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
1679 btrfs_free_path(path
);
1682 btrfs_free_path(path
);
1685 stripes
= &chunk
->stripe
;
1686 *num_bytes
= chunk_bytes_by_type(type
, calc_size
,
1687 num_stripes
, sub_stripes
);
1690 while(index
< num_stripes
) {
1691 struct btrfs_stripe
*stripe
;
1692 BUG_ON(list_empty(&private_devs
));
1693 cur
= private_devs
.next
;
1694 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
1696 /* loop over this device again if we're doing a dup group */
1697 if (!(type
& BTRFS_BLOCK_GROUP_DUP
) ||
1698 (index
== num_stripes
- 1))
1699 list_move_tail(&device
->dev_alloc_list
, dev_list
);
1701 ret
= btrfs_alloc_dev_extent(trans
, device
,
1702 info
->chunk_root
->root_key
.objectid
,
1703 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, key
.offset
,
1704 calc_size
, &dev_offset
);
1706 device
->bytes_used
+= calc_size
;
1707 ret
= btrfs_update_device(trans
, device
);
1710 map
->stripes
[index
].dev
= device
;
1711 map
->stripes
[index
].physical
= dev_offset
;
1712 stripe
= stripes
+ index
;
1713 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
1714 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
1715 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
1716 physical
= dev_offset
;
1719 BUG_ON(!list_empty(&private_devs
));
1721 /* key was set above */
1722 btrfs_set_stack_chunk_length(chunk
, *num_bytes
);
1723 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
1724 btrfs_set_stack_chunk_stripe_len(chunk
, stripe_len
);
1725 btrfs_set_stack_chunk_type(chunk
, type
);
1726 btrfs_set_stack_chunk_num_stripes(chunk
, num_stripes
);
1727 btrfs_set_stack_chunk_io_align(chunk
, stripe_len
);
1728 btrfs_set_stack_chunk_io_width(chunk
, stripe_len
);
1729 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
1730 btrfs_set_stack_chunk_sub_stripes(chunk
, sub_stripes
);
1731 map
->sector_size
= extent_root
->sectorsize
;
1732 map
->stripe_len
= stripe_len
;
1733 map
->io_align
= stripe_len
;
1734 map
->io_width
= stripe_len
;
1736 map
->num_stripes
= num_stripes
;
1737 map
->sub_stripes
= sub_stripes
;
1739 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
,
1740 btrfs_chunk_item_size(num_stripes
));
1742 *start
= key
.offset
;;
1744 em
= alloc_extent_map(GFP_NOFS
);
1747 em
->bdev
= (struct block_device
*)map
;
1748 em
->start
= key
.offset
;
1749 em
->len
= *num_bytes
;
1750 em
->block_start
= 0;
1752 if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1753 ret
= btrfs_add_system_chunk(trans
, chunk_root
, &key
,
1754 chunk
, btrfs_chunk_item_size(num_stripes
));
1759 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
1760 spin_lock(&em_tree
->lock
);
1761 ret
= add_extent_mapping(em_tree
, em
);
1762 spin_unlock(&em_tree
->lock
);
1764 free_extent_map(em
);
1768 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
1770 extent_map_tree_init(&tree
->map_tree
, GFP_NOFS
);
1773 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
1775 struct extent_map
*em
;
1778 spin_lock(&tree
->map_tree
.lock
);
1779 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
1781 remove_extent_mapping(&tree
->map_tree
, em
);
1782 spin_unlock(&tree
->map_tree
.lock
);
1787 free_extent_map(em
);
1788 /* once for the tree */
1789 free_extent_map(em
);
1793 int btrfs_num_copies(struct btrfs_mapping_tree
*map_tree
, u64 logical
, u64 len
)
1795 struct extent_map
*em
;
1796 struct map_lookup
*map
;
1797 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
1800 spin_lock(&em_tree
->lock
);
1801 em
= lookup_extent_mapping(em_tree
, logical
, len
);
1802 spin_unlock(&em_tree
->lock
);
1805 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
1806 map
= (struct map_lookup
*)em
->bdev
;
1807 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
1808 ret
= map
->num_stripes
;
1809 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
1810 ret
= map
->sub_stripes
;
1813 free_extent_map(em
);
1817 static int find_live_mirror(struct map_lookup
*map
, int first
, int num
,
1821 if (map
->stripes
[optimal
].dev
->bdev
)
1823 for (i
= first
; i
< first
+ num
; i
++) {
1824 if (map
->stripes
[i
].dev
->bdev
)
1827 /* we couldn't find one that doesn't fail. Just return something
1828 * and the io error handling code will clean up eventually
1833 static int __btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
1834 u64 logical
, u64
*length
,
1835 struct btrfs_multi_bio
**multi_ret
,
1836 int mirror_num
, struct page
*unplug_page
)
1838 struct extent_map
*em
;
1839 struct map_lookup
*map
;
1840 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
1844 int stripes_allocated
= 8;
1845 int stripes_required
= 1;
1850 struct btrfs_multi_bio
*multi
= NULL
;
1852 if (multi_ret
&& !(rw
& (1 << BIO_RW
))) {
1853 stripes_allocated
= 1;
1857 multi
= kzalloc(btrfs_multi_bio_size(stripes_allocated
),
1862 atomic_set(&multi
->error
, 0);
1865 spin_lock(&em_tree
->lock
);
1866 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
1867 spin_unlock(&em_tree
->lock
);
1869 if (!em
&& unplug_page
)
1873 printk("unable to find logical %Lu len %Lu\n", logical
, *length
);
1877 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
1878 map
= (struct map_lookup
*)em
->bdev
;
1879 offset
= logical
- em
->start
;
1881 if (mirror_num
> map
->num_stripes
)
1884 /* if our multi bio struct is too small, back off and try again */
1885 if (rw
& (1 << BIO_RW
)) {
1886 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
1887 BTRFS_BLOCK_GROUP_DUP
)) {
1888 stripes_required
= map
->num_stripes
;
1890 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
1891 stripes_required
= map
->sub_stripes
;
1895 if (multi_ret
&& rw
== WRITE
&&
1896 stripes_allocated
< stripes_required
) {
1897 stripes_allocated
= map
->num_stripes
;
1898 free_extent_map(em
);
1904 * stripe_nr counts the total number of stripes we have to stride
1905 * to get to this block
1907 do_div(stripe_nr
, map
->stripe_len
);
1909 stripe_offset
= stripe_nr
* map
->stripe_len
;
1910 BUG_ON(offset
< stripe_offset
);
1912 /* stripe_offset is the offset of this block in its stripe*/
1913 stripe_offset
= offset
- stripe_offset
;
1915 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
1916 BTRFS_BLOCK_GROUP_RAID10
|
1917 BTRFS_BLOCK_GROUP_DUP
)) {
1918 /* we limit the length of each bio to what fits in a stripe */
1919 *length
= min_t(u64
, em
->len
- offset
,
1920 map
->stripe_len
- stripe_offset
);
1922 *length
= em
->len
- offset
;
1925 if (!multi_ret
&& !unplug_page
)
1930 if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
1931 if (unplug_page
|| (rw
& (1 << BIO_RW
)))
1932 num_stripes
= map
->num_stripes
;
1933 else if (mirror_num
)
1934 stripe_index
= mirror_num
- 1;
1936 stripe_index
= find_live_mirror(map
, 0,
1938 current
->pid
% map
->num_stripes
);
1941 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
1942 if (rw
& (1 << BIO_RW
))
1943 num_stripes
= map
->num_stripes
;
1944 else if (mirror_num
)
1945 stripe_index
= mirror_num
- 1;
1947 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
1948 int factor
= map
->num_stripes
/ map
->sub_stripes
;
1950 stripe_index
= do_div(stripe_nr
, factor
);
1951 stripe_index
*= map
->sub_stripes
;
1953 if (unplug_page
|| (rw
& (1 << BIO_RW
)))
1954 num_stripes
= map
->sub_stripes
;
1955 else if (mirror_num
)
1956 stripe_index
+= mirror_num
- 1;
1958 stripe_index
= find_live_mirror(map
, stripe_index
,
1959 map
->sub_stripes
, stripe_index
+
1960 current
->pid
% map
->sub_stripes
);
1964 * after this do_div call, stripe_nr is the number of stripes
1965 * on this device we have to walk to find the data, and
1966 * stripe_index is the number of our device in the stripe array
1968 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
1970 BUG_ON(stripe_index
>= map
->num_stripes
);
1972 for (i
= 0; i
< num_stripes
; i
++) {
1974 struct btrfs_device
*device
;
1975 struct backing_dev_info
*bdi
;
1977 device
= map
->stripes
[stripe_index
].dev
;
1979 bdi
= blk_get_backing_dev_info(device
->bdev
);
1980 if (bdi
->unplug_io_fn
) {
1981 bdi
->unplug_io_fn(bdi
, unplug_page
);
1985 multi
->stripes
[i
].physical
=
1986 map
->stripes
[stripe_index
].physical
+
1987 stripe_offset
+ stripe_nr
* map
->stripe_len
;
1988 multi
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
1994 multi
->num_stripes
= num_stripes
;
1995 multi
->max_errors
= max_errors
;
1998 free_extent_map(em
);
2002 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
2003 u64 logical
, u64
*length
,
2004 struct btrfs_multi_bio
**multi_ret
, int mirror_num
)
2006 return __btrfs_map_block(map_tree
, rw
, logical
, length
, multi_ret
,
2010 int btrfs_unplug_page(struct btrfs_mapping_tree
*map_tree
,
2011 u64 logical
, struct page
*page
)
2013 u64 length
= PAGE_CACHE_SIZE
;
2014 return __btrfs_map_block(map_tree
, READ
, logical
, &length
,
2019 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2020 static void end_bio_multi_stripe(struct bio
*bio
, int err
)
2022 static int end_bio_multi_stripe(struct bio
*bio
,
2023 unsigned int bytes_done
, int err
)
2026 struct btrfs_multi_bio
*multi
= bio
->bi_private
;
2028 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2033 atomic_inc(&multi
->error
);
2035 if (atomic_dec_and_test(&multi
->stripes_pending
)) {
2036 bio
->bi_private
= multi
->private;
2037 bio
->bi_end_io
= multi
->end_io
;
2038 /* only send an error to the higher layers if it is
2039 * beyond the tolerance of the multi-bio
2041 if (atomic_read(&multi
->error
) > multi
->max_errors
) {
2045 * this bio is actually up to date, we didn't
2046 * go over the max number of errors
2048 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
2053 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2054 bio_endio(bio
, bio
->bi_size
, err
);
2056 bio_endio(bio
, err
);
2061 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2066 struct async_sched
{
2069 struct btrfs_fs_info
*info
;
2070 struct btrfs_work work
;
2074 * see run_scheduled_bios for a description of why bios are collected for
2077 * This will add one bio to the pending list for a device and make sure
2078 * the work struct is scheduled.
2080 int schedule_bio(struct btrfs_root
*root
, struct btrfs_device
*device
,
2081 int rw
, struct bio
*bio
)
2083 int should_queue
= 1;
2085 /* don't bother with additional async steps for reads, right now */
2086 if (!(rw
& (1 << BIO_RW
))) {
2087 submit_bio(rw
, bio
);
2092 * nr_async_sumbits allows us to reliably return congestion to the
2093 * higher layers. Otherwise, the async bio makes it appear we have
2094 * made progress against dirty pages when we've really just put it
2095 * on a queue for later
2097 atomic_inc(&root
->fs_info
->nr_async_submits
);
2098 bio
->bi_next
= NULL
;
2101 spin_lock(&device
->io_lock
);
2103 if (device
->pending_bio_tail
)
2104 device
->pending_bio_tail
->bi_next
= bio
;
2106 device
->pending_bio_tail
= bio
;
2107 if (!device
->pending_bios
)
2108 device
->pending_bios
= bio
;
2109 if (device
->running_pending
)
2112 spin_unlock(&device
->io_lock
);
2115 btrfs_queue_worker(&root
->fs_info
->workers
, &device
->work
);
2119 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
2120 int mirror_num
, int async_submit
)
2122 struct btrfs_mapping_tree
*map_tree
;
2123 struct btrfs_device
*dev
;
2124 struct bio
*first_bio
= bio
;
2125 u64 logical
= bio
->bi_sector
<< 9;
2128 struct btrfs_multi_bio
*multi
= NULL
;
2133 length
= bio
->bi_size
;
2134 map_tree
= &root
->fs_info
->mapping_tree
;
2135 map_length
= length
;
2137 ret
= btrfs_map_block(map_tree
, rw
, logical
, &map_length
, &multi
,
2141 total_devs
= multi
->num_stripes
;
2142 if (map_length
< length
) {
2143 printk("mapping failed logical %Lu bio len %Lu "
2144 "len %Lu\n", logical
, length
, map_length
);
2147 multi
->end_io
= first_bio
->bi_end_io
;
2148 multi
->private = first_bio
->bi_private
;
2149 atomic_set(&multi
->stripes_pending
, multi
->num_stripes
);
2151 while(dev_nr
< total_devs
) {
2152 if (total_devs
> 1) {
2153 if (dev_nr
< total_devs
- 1) {
2154 bio
= bio_clone(first_bio
, GFP_NOFS
);
2159 bio
->bi_private
= multi
;
2160 bio
->bi_end_io
= end_bio_multi_stripe
;
2162 bio
->bi_sector
= multi
->stripes
[dev_nr
].physical
>> 9;
2163 dev
= multi
->stripes
[dev_nr
].dev
;
2164 if (dev
&& dev
->bdev
) {
2165 bio
->bi_bdev
= dev
->bdev
;
2167 schedule_bio(root
, dev
, rw
, bio
);
2169 submit_bio(rw
, bio
);
2171 bio
->bi_bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2172 bio
->bi_sector
= logical
>> 9;
2173 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2174 bio_endio(bio
, bio
->bi_size
, -EIO
);
2176 bio_endio(bio
, -EIO
);
2181 if (total_devs
== 1)
2186 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
,
2189 struct list_head
*head
= &root
->fs_info
->fs_devices
->devices
;
2191 return __find_device(head
, devid
, uuid
);
2194 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
2195 u64 devid
, u8
*dev_uuid
)
2197 struct btrfs_device
*device
;
2198 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
2200 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
2201 list_add(&device
->dev_list
,
2202 &fs_devices
->devices
);
2203 list_add(&device
->dev_alloc_list
,
2204 &fs_devices
->alloc_list
);
2205 device
->barriers
= 1;
2206 device
->dev_root
= root
->fs_info
->dev_root
;
2207 device
->devid
= devid
;
2208 device
->work
.func
= pending_bios_fn
;
2209 fs_devices
->num_devices
++;
2210 spin_lock_init(&device
->io_lock
);
2211 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
2216 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
2217 struct extent_buffer
*leaf
,
2218 struct btrfs_chunk
*chunk
)
2220 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
2221 struct map_lookup
*map
;
2222 struct extent_map
*em
;
2226 u8 uuid
[BTRFS_UUID_SIZE
];
2231 logical
= key
->offset
;
2232 length
= btrfs_chunk_length(leaf
, chunk
);
2234 spin_lock(&map_tree
->map_tree
.lock
);
2235 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
2236 spin_unlock(&map_tree
->map_tree
.lock
);
2238 /* already mapped? */
2239 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
2240 free_extent_map(em
);
2243 free_extent_map(em
);
2246 map
= kzalloc(sizeof(*map
), GFP_NOFS
);
2250 em
= alloc_extent_map(GFP_NOFS
);
2253 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2254 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
2256 free_extent_map(em
);
2260 em
->bdev
= (struct block_device
*)map
;
2261 em
->start
= logical
;
2263 em
->block_start
= 0;
2265 map
->num_stripes
= num_stripes
;
2266 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
2267 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
2268 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
2269 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
2270 map
->type
= btrfs_chunk_type(leaf
, chunk
);
2271 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
2272 for (i
= 0; i
< num_stripes
; i
++) {
2273 map
->stripes
[i
].physical
=
2274 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
2275 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
2276 read_extent_buffer(leaf
, uuid
, (unsigned long)
2277 btrfs_stripe_dev_uuid_nr(chunk
, i
),
2279 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
, uuid
);
2281 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
2283 free_extent_map(em
);
2286 if (!map
->stripes
[i
].dev
) {
2287 map
->stripes
[i
].dev
=
2288 add_missing_dev(root
, devid
, uuid
);
2289 if (!map
->stripes
[i
].dev
) {
2291 free_extent_map(em
);
2295 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
2298 spin_lock(&map_tree
->map_tree
.lock
);
2299 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
2300 spin_unlock(&map_tree
->map_tree
.lock
);
2302 free_extent_map(em
);
2307 static int fill_device_from_item(struct extent_buffer
*leaf
,
2308 struct btrfs_dev_item
*dev_item
,
2309 struct btrfs_device
*device
)
2313 device
->devid
= btrfs_device_id(leaf
, dev_item
);
2314 device
->total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
2315 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
2316 device
->type
= btrfs_device_type(leaf
, dev_item
);
2317 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
2318 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
2319 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
2321 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
2322 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
2327 static int read_one_dev(struct btrfs_root
*root
,
2328 struct extent_buffer
*leaf
,
2329 struct btrfs_dev_item
*dev_item
)
2331 struct btrfs_device
*device
;
2334 u8 dev_uuid
[BTRFS_UUID_SIZE
];
2336 devid
= btrfs_device_id(leaf
, dev_item
);
2337 read_extent_buffer(leaf
, dev_uuid
,
2338 (unsigned long)btrfs_device_uuid(dev_item
),
2340 device
= btrfs_find_device(root
, devid
, dev_uuid
);
2342 printk("warning devid %Lu missing\n", devid
);
2343 device
= add_missing_dev(root
, devid
, dev_uuid
);
2348 fill_device_from_item(leaf
, dev_item
, device
);
2349 device
->dev_root
= root
->fs_info
->dev_root
;
2350 device
->in_fs_metadata
= 1;
2353 ret
= btrfs_open_device(device
);
2361 int btrfs_read_super_device(struct btrfs_root
*root
, struct extent_buffer
*buf
)
2363 struct btrfs_dev_item
*dev_item
;
2365 dev_item
= (struct btrfs_dev_item
*)offsetof(struct btrfs_super_block
,
2367 return read_one_dev(root
, buf
, dev_item
);
2370 int btrfs_read_sys_array(struct btrfs_root
*root
)
2372 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
2373 struct extent_buffer
*sb
;
2374 struct btrfs_disk_key
*disk_key
;
2375 struct btrfs_chunk
*chunk
;
2377 unsigned long sb_ptr
;
2383 struct btrfs_key key
;
2385 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
2386 BTRFS_SUPER_INFO_SIZE
);
2389 btrfs_set_buffer_uptodate(sb
);
2390 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
2391 array_size
= btrfs_super_sys_array_size(super_copy
);
2393 ptr
= super_copy
->sys_chunk_array
;
2394 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
2397 while (cur
< array_size
) {
2398 disk_key
= (struct btrfs_disk_key
*)ptr
;
2399 btrfs_disk_key_to_cpu(&key
, disk_key
);
2401 len
= sizeof(*disk_key
); ptr
+= len
;
2405 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
2406 chunk
= (struct btrfs_chunk
*)sb_ptr
;
2407 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
2410 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
2411 len
= btrfs_chunk_item_size(num_stripes
);
2420 free_extent_buffer(sb
);
2424 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
2426 struct btrfs_path
*path
;
2427 struct extent_buffer
*leaf
;
2428 struct btrfs_key key
;
2429 struct btrfs_key found_key
;
2433 root
= root
->fs_info
->chunk_root
;
2435 path
= btrfs_alloc_path();
2439 /* first we search for all of the device items, and then we
2440 * read in all of the chunk items. This way we can create chunk
2441 * mappings that reference all of the devices that are afound
2443 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
2447 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2449 leaf
= path
->nodes
[0];
2450 slot
= path
->slots
[0];
2451 if (slot
>= btrfs_header_nritems(leaf
)) {
2452 ret
= btrfs_next_leaf(root
, path
);
2459 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2460 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
2461 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
2463 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
2464 struct btrfs_dev_item
*dev_item
;
2465 dev_item
= btrfs_item_ptr(leaf
, slot
,
2466 struct btrfs_dev_item
);
2467 ret
= read_one_dev(root
, leaf
, dev_item
);
2470 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
2471 struct btrfs_chunk
*chunk
;
2472 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
2473 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
2477 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
2479 btrfs_release_path(root
, path
);
2483 btrfs_free_path(path
);