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 <linux/version.h>
24 #include <asm/div64.h>
27 #include "extent_map.h"
29 #include "transaction.h"
30 #include "print-tree.h"
32 #include "async-thread.h"
42 struct btrfs_bio_stripe stripes
[];
45 static int init_first_rw_device(struct btrfs_trans_handle
*trans
,
46 struct btrfs_root
*root
,
47 struct btrfs_device
*device
);
48 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
);
50 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
51 (sizeof(struct btrfs_bio_stripe) * (n)))
53 static DEFINE_MUTEX(uuid_mutex
);
54 static LIST_HEAD(fs_uuids
);
56 void btrfs_lock_volumes(void)
58 mutex_lock(&uuid_mutex
);
61 void btrfs_unlock_volumes(void)
63 mutex_unlock(&uuid_mutex
);
66 static void lock_chunks(struct btrfs_root
*root
)
68 mutex_lock(&root
->fs_info
->chunk_mutex
);
71 static void unlock_chunks(struct btrfs_root
*root
)
73 mutex_unlock(&root
->fs_info
->chunk_mutex
);
76 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
78 struct btrfs_device
*device
;
79 WARN_ON(fs_devices
->opened
);
80 while (!list_empty(&fs_devices
->devices
)) {
81 device
= list_entry(fs_devices
->devices
.next
,
82 struct btrfs_device
, dev_list
);
83 list_del(&device
->dev_list
);
90 int btrfs_cleanup_fs_uuids(void)
92 struct btrfs_fs_devices
*fs_devices
;
94 while (!list_empty(&fs_uuids
)) {
95 fs_devices
= list_entry(fs_uuids
.next
,
96 struct btrfs_fs_devices
, list
);
97 list_del(&fs_devices
->list
);
98 free_fs_devices(fs_devices
);
103 static noinline
struct btrfs_device
*__find_device(struct list_head
*head
,
106 struct btrfs_device
*dev
;
107 struct list_head
*cur
;
109 list_for_each(cur
, head
) {
110 dev
= list_entry(cur
, struct btrfs_device
, dev_list
);
111 if (dev
->devid
== devid
&&
112 (!uuid
|| !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
))) {
119 static noinline
struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
121 struct list_head
*cur
;
122 struct btrfs_fs_devices
*fs_devices
;
124 list_for_each(cur
, &fs_uuids
) {
125 fs_devices
= list_entry(cur
, struct btrfs_fs_devices
, list
);
126 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
133 * we try to collect pending bios for a device so we don't get a large
134 * number of procs sending bios down to the same device. This greatly
135 * improves the schedulers ability to collect and merge the bios.
137 * But, it also turns into a long list of bios to process and that is sure
138 * to eventually make the worker thread block. The solution here is to
139 * make some progress and then put this work struct back at the end of
140 * the list if the block device is congested. This way, multiple devices
141 * can make progress from a single worker thread.
143 static noinline
int run_scheduled_bios(struct btrfs_device
*device
)
146 struct backing_dev_info
*bdi
;
147 struct btrfs_fs_info
*fs_info
;
151 unsigned long num_run
= 0;
154 bdi
= device
->bdev
->bd_inode
->i_mapping
->backing_dev_info
;
155 fs_info
= device
->dev_root
->fs_info
;
156 limit
= btrfs_async_submit_limit(fs_info
);
157 limit
= limit
* 2 / 3;
160 spin_lock(&device
->io_lock
);
162 /* take all the bios off the list at once and process them
163 * later on (without the lock held). But, remember the
164 * tail and other pointers so the bios can be properly reinserted
165 * into the list if we hit congestion
167 pending
= device
->pending_bios
;
168 tail
= device
->pending_bio_tail
;
169 WARN_ON(pending
&& !tail
);
170 device
->pending_bios
= NULL
;
171 device
->pending_bio_tail
= NULL
;
174 * if pending was null this time around, no bios need processing
175 * at all and we can stop. Otherwise it'll loop back up again
176 * and do an additional check so no bios are missed.
178 * device->running_pending is used to synchronize with the
183 device
->running_pending
= 1;
186 device
->running_pending
= 0;
188 spin_unlock(&device
->io_lock
);
192 pending
= pending
->bi_next
;
194 atomic_dec(&fs_info
->nr_async_bios
);
196 if (atomic_read(&fs_info
->nr_async_bios
) < limit
&&
197 waitqueue_active(&fs_info
->async_submit_wait
))
198 wake_up(&fs_info
->async_submit_wait
);
200 BUG_ON(atomic_read(&cur
->bi_cnt
) == 0);
202 submit_bio(cur
->bi_rw
, cur
);
207 * we made progress, there is more work to do and the bdi
208 * is now congested. Back off and let other work structs
211 if (pending
&& bdi_write_congested(bdi
) &&
212 fs_info
->fs_devices
->open_devices
> 1) {
213 struct bio
*old_head
;
215 spin_lock(&device
->io_lock
);
217 old_head
= device
->pending_bios
;
218 device
->pending_bios
= pending
;
219 if (device
->pending_bio_tail
)
220 tail
->bi_next
= old_head
;
222 device
->pending_bio_tail
= tail
;
224 spin_unlock(&device
->io_lock
);
225 btrfs_requeue_work(&device
->work
);
235 static void pending_bios_fn(struct btrfs_work
*work
)
237 struct btrfs_device
*device
;
239 device
= container_of(work
, struct btrfs_device
, work
);
240 run_scheduled_bios(device
);
243 static noinline
int device_list_add(const char *path
,
244 struct btrfs_super_block
*disk_super
,
245 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
247 struct btrfs_device
*device
;
248 struct btrfs_fs_devices
*fs_devices
;
249 u64 found_transid
= btrfs_super_generation(disk_super
);
251 fs_devices
= find_fsid(disk_super
->fsid
);
253 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
256 INIT_LIST_HEAD(&fs_devices
->devices
);
257 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
258 list_add(&fs_devices
->list
, &fs_uuids
);
259 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
260 fs_devices
->latest_devid
= devid
;
261 fs_devices
->latest_trans
= found_transid
;
264 device
= __find_device(&fs_devices
->devices
, devid
,
265 disk_super
->dev_item
.uuid
);
268 if (fs_devices
->opened
)
271 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
273 /* we can safely leave the fs_devices entry around */
276 device
->devid
= devid
;
277 device
->work
.func
= pending_bios_fn
;
278 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
280 device
->barriers
= 1;
281 spin_lock_init(&device
->io_lock
);
282 device
->name
= kstrdup(path
, GFP_NOFS
);
287 INIT_LIST_HEAD(&device
->dev_alloc_list
);
288 list_add(&device
->dev_list
, &fs_devices
->devices
);
289 device
->fs_devices
= fs_devices
;
290 fs_devices
->num_devices
++;
293 if (found_transid
> fs_devices
->latest_trans
) {
294 fs_devices
->latest_devid
= devid
;
295 fs_devices
->latest_trans
= found_transid
;
297 *fs_devices_ret
= fs_devices
;
301 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
303 struct btrfs_fs_devices
*fs_devices
;
304 struct btrfs_device
*device
;
305 struct btrfs_device
*orig_dev
;
307 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
309 return ERR_PTR(-ENOMEM
);
311 INIT_LIST_HEAD(&fs_devices
->devices
);
312 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
313 INIT_LIST_HEAD(&fs_devices
->list
);
314 fs_devices
->latest_devid
= orig
->latest_devid
;
315 fs_devices
->latest_trans
= orig
->latest_trans
;
316 memcpy(fs_devices
->fsid
, orig
->fsid
, sizeof(fs_devices
->fsid
));
318 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
319 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
323 device
->name
= kstrdup(orig_dev
->name
, GFP_NOFS
);
327 device
->devid
= orig_dev
->devid
;
328 device
->work
.func
= pending_bios_fn
;
329 memcpy(device
->uuid
, orig_dev
->uuid
, sizeof(device
->uuid
));
330 device
->barriers
= 1;
331 spin_lock_init(&device
->io_lock
);
332 INIT_LIST_HEAD(&device
->dev_list
);
333 INIT_LIST_HEAD(&device
->dev_alloc_list
);
335 list_add(&device
->dev_list
, &fs_devices
->devices
);
336 device
->fs_devices
= fs_devices
;
337 fs_devices
->num_devices
++;
341 free_fs_devices(fs_devices
);
342 return ERR_PTR(-ENOMEM
);
345 int btrfs_close_extra_devices(struct btrfs_fs_devices
*fs_devices
)
347 struct list_head
*tmp
;
348 struct list_head
*cur
;
349 struct btrfs_device
*device
;
351 mutex_lock(&uuid_mutex
);
353 list_for_each_safe(cur
, tmp
, &fs_devices
->devices
) {
354 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
355 if (device
->in_fs_metadata
)
359 close_bdev_exclusive(device
->bdev
, device
->mode
);
361 fs_devices
->open_devices
--;
363 if (device
->writeable
) {
364 list_del_init(&device
->dev_alloc_list
);
365 device
->writeable
= 0;
366 fs_devices
->rw_devices
--;
368 list_del_init(&device
->dev_list
);
369 fs_devices
->num_devices
--;
374 if (fs_devices
->seed
) {
375 fs_devices
= fs_devices
->seed
;
379 mutex_unlock(&uuid_mutex
);
383 static int __btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
385 struct list_head
*cur
;
386 struct btrfs_device
*device
;
388 if (--fs_devices
->opened
> 0)
391 list_for_each(cur
, &fs_devices
->devices
) {
392 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
394 close_bdev_exclusive(device
->bdev
, device
->mode
);
395 fs_devices
->open_devices
--;
397 if (device
->writeable
) {
398 list_del_init(&device
->dev_alloc_list
);
399 fs_devices
->rw_devices
--;
403 device
->writeable
= 0;
404 device
->in_fs_metadata
= 0;
406 WARN_ON(fs_devices
->open_devices
);
407 WARN_ON(fs_devices
->rw_devices
);
408 fs_devices
->opened
= 0;
409 fs_devices
->seeding
= 0;
414 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
416 struct btrfs_fs_devices
*seed_devices
= NULL
;
419 mutex_lock(&uuid_mutex
);
420 ret
= __btrfs_close_devices(fs_devices
);
421 if (!fs_devices
->opened
) {
422 seed_devices
= fs_devices
->seed
;
423 fs_devices
->seed
= NULL
;
425 mutex_unlock(&uuid_mutex
);
427 while (seed_devices
) {
428 fs_devices
= seed_devices
;
429 seed_devices
= fs_devices
->seed
;
430 __btrfs_close_devices(fs_devices
);
431 free_fs_devices(fs_devices
);
436 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
437 fmode_t flags
, void *holder
)
439 struct block_device
*bdev
;
440 struct list_head
*head
= &fs_devices
->devices
;
441 struct list_head
*cur
;
442 struct btrfs_device
*device
;
443 struct block_device
*latest_bdev
= NULL
;
444 struct buffer_head
*bh
;
445 struct btrfs_super_block
*disk_super
;
446 u64 latest_devid
= 0;
447 u64 latest_transid
= 0;
452 list_for_each(cur
, head
) {
453 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
459 bdev
= open_bdev_exclusive(device
->name
, flags
, holder
);
461 printk(KERN_INFO
"open %s failed\n", device
->name
);
464 set_blocksize(bdev
, 4096);
466 bh
= btrfs_read_dev_super(bdev
);
470 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
471 devid
= le64_to_cpu(disk_super
->dev_item
.devid
);
472 if (devid
!= device
->devid
)
475 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
479 device
->generation
= btrfs_super_generation(disk_super
);
480 if (!latest_transid
|| device
->generation
> latest_transid
) {
481 latest_devid
= devid
;
482 latest_transid
= device
->generation
;
486 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
487 device
->writeable
= 0;
489 device
->writeable
= !bdev_read_only(bdev
);
494 device
->in_fs_metadata
= 0;
495 device
->mode
= flags
;
497 fs_devices
->open_devices
++;
498 if (device
->writeable
) {
499 fs_devices
->rw_devices
++;
500 list_add(&device
->dev_alloc_list
,
501 &fs_devices
->alloc_list
);
508 close_bdev_exclusive(bdev
, FMODE_READ
);
512 if (fs_devices
->open_devices
== 0) {
516 fs_devices
->seeding
= seeding
;
517 fs_devices
->opened
= 1;
518 fs_devices
->latest_bdev
= latest_bdev
;
519 fs_devices
->latest_devid
= latest_devid
;
520 fs_devices
->latest_trans
= latest_transid
;
521 fs_devices
->total_rw_bytes
= 0;
526 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
527 fmode_t flags
, void *holder
)
531 mutex_lock(&uuid_mutex
);
532 if (fs_devices
->opened
) {
533 fs_devices
->opened
++;
536 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
538 mutex_unlock(&uuid_mutex
);
542 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
543 struct btrfs_fs_devices
**fs_devices_ret
)
545 struct btrfs_super_block
*disk_super
;
546 struct block_device
*bdev
;
547 struct buffer_head
*bh
;
552 mutex_lock(&uuid_mutex
);
554 bdev
= open_bdev_exclusive(path
, flags
, holder
);
561 ret
= set_blocksize(bdev
, 4096);
564 bh
= btrfs_read_dev_super(bdev
);
569 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
570 devid
= le64_to_cpu(disk_super
->dev_item
.devid
);
571 transid
= btrfs_super_generation(disk_super
);
572 if (disk_super
->label
[0])
573 printk(KERN_INFO
"device label %s ", disk_super
->label
);
575 /* FIXME, make a readl uuid parser */
576 printk(KERN_INFO
"device fsid %llx-%llx ",
577 *(unsigned long long *)disk_super
->fsid
,
578 *(unsigned long long *)(disk_super
->fsid
+ 8));
580 printk(KERN_INFO
"devid %llu transid %llu %s\n",
581 (unsigned long long)devid
, (unsigned long long)transid
, path
);
582 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
586 close_bdev_exclusive(bdev
, flags
);
588 mutex_unlock(&uuid_mutex
);
593 * this uses a pretty simple search, the expectation is that it is
594 * called very infrequently and that a given device has a small number
597 static noinline
int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
598 struct btrfs_device
*device
,
599 u64 num_bytes
, u64
*start
)
601 struct btrfs_key key
;
602 struct btrfs_root
*root
= device
->dev_root
;
603 struct btrfs_dev_extent
*dev_extent
= NULL
;
604 struct btrfs_path
*path
;
607 u64 search_start
= 0;
608 u64 search_end
= device
->total_bytes
;
612 struct extent_buffer
*l
;
614 path
= btrfs_alloc_path();
620 /* FIXME use last free of some kind */
622 /* we don't want to overwrite the superblock on the drive,
623 * so we make sure to start at an offset of at least 1MB
625 search_start
= max((u64
)1024 * 1024, search_start
);
627 if (root
->fs_info
->alloc_start
+ num_bytes
<= device
->total_bytes
)
628 search_start
= max(root
->fs_info
->alloc_start
, search_start
);
630 key
.objectid
= device
->devid
;
631 key
.offset
= search_start
;
632 key
.type
= BTRFS_DEV_EXTENT_KEY
;
633 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
636 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
640 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
643 slot
= path
->slots
[0];
644 if (slot
>= btrfs_header_nritems(l
)) {
645 ret
= btrfs_next_leaf(root
, path
);
652 if (search_start
>= search_end
) {
656 *start
= search_start
;
660 *start
= last_byte
> search_start
?
661 last_byte
: search_start
;
662 if (search_end
<= *start
) {
668 btrfs_item_key_to_cpu(l
, &key
, slot
);
670 if (key
.objectid
< device
->devid
)
673 if (key
.objectid
> device
->devid
)
676 if (key
.offset
>= search_start
&& key
.offset
> last_byte
&&
678 if (last_byte
< search_start
)
679 last_byte
= search_start
;
680 hole_size
= key
.offset
- last_byte
;
681 if (key
.offset
> last_byte
&&
682 hole_size
>= num_bytes
) {
687 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
691 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
692 last_byte
= key
.offset
+ btrfs_dev_extent_length(l
, dev_extent
);
698 /* we have to make sure we didn't find an extent that has already
699 * been allocated by the map tree or the original allocation
701 BUG_ON(*start
< search_start
);
703 if (*start
+ num_bytes
> search_end
) {
707 /* check for pending inserts here */
711 btrfs_free_path(path
);
715 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
716 struct btrfs_device
*device
,
720 struct btrfs_path
*path
;
721 struct btrfs_root
*root
= device
->dev_root
;
722 struct btrfs_key key
;
723 struct btrfs_key found_key
;
724 struct extent_buffer
*leaf
= NULL
;
725 struct btrfs_dev_extent
*extent
= NULL
;
727 path
= btrfs_alloc_path();
731 key
.objectid
= device
->devid
;
733 key
.type
= BTRFS_DEV_EXTENT_KEY
;
735 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
737 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
738 BTRFS_DEV_EXTENT_KEY
);
740 leaf
= path
->nodes
[0];
741 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
742 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
743 struct btrfs_dev_extent
);
744 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
745 btrfs_dev_extent_length(leaf
, extent
) < start
);
747 } else if (ret
== 0) {
748 leaf
= path
->nodes
[0];
749 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
750 struct btrfs_dev_extent
);
754 if (device
->bytes_used
> 0)
755 device
->bytes_used
-= btrfs_dev_extent_length(leaf
, extent
);
756 ret
= btrfs_del_item(trans
, root
, path
);
759 btrfs_free_path(path
);
763 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
764 struct btrfs_device
*device
,
765 u64 chunk_tree
, u64 chunk_objectid
,
766 u64 chunk_offset
, u64 start
, u64 num_bytes
)
769 struct btrfs_path
*path
;
770 struct btrfs_root
*root
= device
->dev_root
;
771 struct btrfs_dev_extent
*extent
;
772 struct extent_buffer
*leaf
;
773 struct btrfs_key key
;
775 WARN_ON(!device
->in_fs_metadata
);
776 path
= btrfs_alloc_path();
780 key
.objectid
= device
->devid
;
782 key
.type
= BTRFS_DEV_EXTENT_KEY
;
783 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
787 leaf
= path
->nodes
[0];
788 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
789 struct btrfs_dev_extent
);
790 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
791 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
792 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
794 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
795 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
798 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
799 btrfs_mark_buffer_dirty(leaf
);
800 btrfs_free_path(path
);
804 static noinline
int find_next_chunk(struct btrfs_root
*root
,
805 u64 objectid
, u64
*offset
)
807 struct btrfs_path
*path
;
809 struct btrfs_key key
;
810 struct btrfs_chunk
*chunk
;
811 struct btrfs_key found_key
;
813 path
= btrfs_alloc_path();
816 key
.objectid
= objectid
;
817 key
.offset
= (u64
)-1;
818 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
820 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
826 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
830 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
832 if (found_key
.objectid
!= objectid
)
835 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
837 *offset
= found_key
.offset
+
838 btrfs_chunk_length(path
->nodes
[0], chunk
);
843 btrfs_free_path(path
);
847 static noinline
int find_next_devid(struct btrfs_root
*root
, u64
*objectid
)
850 struct btrfs_key key
;
851 struct btrfs_key found_key
;
852 struct btrfs_path
*path
;
854 root
= root
->fs_info
->chunk_root
;
856 path
= btrfs_alloc_path();
860 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
861 key
.type
= BTRFS_DEV_ITEM_KEY
;
862 key
.offset
= (u64
)-1;
864 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
870 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
875 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
877 *objectid
= found_key
.offset
+ 1;
881 btrfs_free_path(path
);
886 * the device information is stored in the chunk root
887 * the btrfs_device struct should be fully filled in
889 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
890 struct btrfs_root
*root
,
891 struct btrfs_device
*device
)
894 struct btrfs_path
*path
;
895 struct btrfs_dev_item
*dev_item
;
896 struct extent_buffer
*leaf
;
897 struct btrfs_key key
;
900 root
= root
->fs_info
->chunk_root
;
902 path
= btrfs_alloc_path();
906 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
907 key
.type
= BTRFS_DEV_ITEM_KEY
;
908 key
.offset
= device
->devid
;
910 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
915 leaf
= path
->nodes
[0];
916 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
918 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
919 btrfs_set_device_generation(leaf
, dev_item
, 0);
920 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
921 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
922 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
923 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
924 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
925 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
926 btrfs_set_device_group(leaf
, dev_item
, 0);
927 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
928 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
929 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
931 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
932 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
933 ptr
= (unsigned long)btrfs_device_fsid(dev_item
);
934 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
935 btrfs_mark_buffer_dirty(leaf
);
939 btrfs_free_path(path
);
943 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
944 struct btrfs_device
*device
)
947 struct btrfs_path
*path
;
948 struct btrfs_key key
;
949 struct btrfs_trans_handle
*trans
;
951 root
= root
->fs_info
->chunk_root
;
953 path
= btrfs_alloc_path();
957 trans
= btrfs_start_transaction(root
, 1);
958 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
959 key
.type
= BTRFS_DEV_ITEM_KEY
;
960 key
.offset
= device
->devid
;
963 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
972 ret
= btrfs_del_item(trans
, root
, path
);
976 btrfs_free_path(path
);
978 btrfs_commit_transaction(trans
, root
);
982 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
984 struct btrfs_device
*device
;
985 struct btrfs_device
*next_device
;
986 struct block_device
*bdev
;
987 struct buffer_head
*bh
= NULL
;
988 struct btrfs_super_block
*disk_super
;
995 mutex_lock(&uuid_mutex
);
996 mutex_lock(&root
->fs_info
->volume_mutex
);
998 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
999 root
->fs_info
->avail_system_alloc_bits
|
1000 root
->fs_info
->avail_metadata_alloc_bits
;
1002 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) &&
1003 root
->fs_info
->fs_devices
->rw_devices
<= 4) {
1004 printk(KERN_ERR
"btrfs: unable to go below four devices "
1010 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) &&
1011 root
->fs_info
->fs_devices
->rw_devices
<= 2) {
1012 printk(KERN_ERR
"btrfs: unable to go below two "
1013 "devices on raid1\n");
1018 if (strcmp(device_path
, "missing") == 0) {
1019 struct list_head
*cur
;
1020 struct list_head
*devices
;
1021 struct btrfs_device
*tmp
;
1024 devices
= &root
->fs_info
->fs_devices
->devices
;
1025 list_for_each(cur
, devices
) {
1026 tmp
= list_entry(cur
, struct btrfs_device
, dev_list
);
1027 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1036 printk(KERN_ERR
"btrfs: no missing devices found to "
1041 bdev
= open_bdev_exclusive(device_path
, FMODE_READ
,
1042 root
->fs_info
->bdev_holder
);
1044 ret
= PTR_ERR(bdev
);
1048 set_blocksize(bdev
, 4096);
1049 bh
= btrfs_read_dev_super(bdev
);
1054 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1055 devid
= le64_to_cpu(disk_super
->dev_item
.devid
);
1056 dev_uuid
= disk_super
->dev_item
.uuid
;
1057 device
= btrfs_find_device(root
, devid
, dev_uuid
,
1065 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1066 printk(KERN_ERR
"btrfs: unable to remove the only writeable "
1072 if (device
->writeable
) {
1073 list_del_init(&device
->dev_alloc_list
);
1074 root
->fs_info
->fs_devices
->rw_devices
--;
1077 ret
= btrfs_shrink_device(device
, 0);
1081 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1085 device
->in_fs_metadata
= 0;
1086 list_del_init(&device
->dev_list
);
1087 device
->fs_devices
->num_devices
--;
1089 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1090 struct btrfs_device
, dev_list
);
1091 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1092 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1093 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1094 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1097 close_bdev_exclusive(device
->bdev
, device
->mode
);
1098 device
->bdev
= NULL
;
1099 device
->fs_devices
->open_devices
--;
1102 num_devices
= btrfs_super_num_devices(&root
->fs_info
->super_copy
) - 1;
1103 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
, num_devices
);
1105 if (device
->fs_devices
->open_devices
== 0) {
1106 struct btrfs_fs_devices
*fs_devices
;
1107 fs_devices
= root
->fs_info
->fs_devices
;
1108 while (fs_devices
) {
1109 if (fs_devices
->seed
== device
->fs_devices
)
1111 fs_devices
= fs_devices
->seed
;
1113 fs_devices
->seed
= device
->fs_devices
->seed
;
1114 device
->fs_devices
->seed
= NULL
;
1115 __btrfs_close_devices(device
->fs_devices
);
1116 free_fs_devices(device
->fs_devices
);
1120 * at this point, the device is zero sized. We want to
1121 * remove it from the devices list and zero out the old super
1123 if (device
->writeable
) {
1124 /* make sure this device isn't detected as part of
1127 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1128 set_buffer_dirty(bh
);
1129 sync_dirty_buffer(bh
);
1132 kfree(device
->name
);
1140 close_bdev_exclusive(bdev
, FMODE_READ
);
1142 mutex_unlock(&root
->fs_info
->volume_mutex
);
1143 mutex_unlock(&uuid_mutex
);
1148 * does all the dirty work required for changing file system's UUID.
1150 static int btrfs_prepare_sprout(struct btrfs_trans_handle
*trans
,
1151 struct btrfs_root
*root
)
1153 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1154 struct btrfs_fs_devices
*old_devices
;
1155 struct btrfs_fs_devices
*seed_devices
;
1156 struct btrfs_super_block
*disk_super
= &root
->fs_info
->super_copy
;
1157 struct btrfs_device
*device
;
1160 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1161 if (!fs_devices
->seeding
)
1164 seed_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
1168 old_devices
= clone_fs_devices(fs_devices
);
1169 if (IS_ERR(old_devices
)) {
1170 kfree(seed_devices
);
1171 return PTR_ERR(old_devices
);
1174 list_add(&old_devices
->list
, &fs_uuids
);
1176 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1177 seed_devices
->opened
= 1;
1178 INIT_LIST_HEAD(&seed_devices
->devices
);
1179 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1180 list_splice_init(&fs_devices
->devices
, &seed_devices
->devices
);
1181 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1182 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1183 device
->fs_devices
= seed_devices
;
1186 fs_devices
->seeding
= 0;
1187 fs_devices
->num_devices
= 0;
1188 fs_devices
->open_devices
= 0;
1189 fs_devices
->seed
= seed_devices
;
1191 generate_random_uuid(fs_devices
->fsid
);
1192 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1193 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1194 super_flags
= btrfs_super_flags(disk_super
) &
1195 ~BTRFS_SUPER_FLAG_SEEDING
;
1196 btrfs_set_super_flags(disk_super
, super_flags
);
1202 * strore the expected generation for seed devices in device items.
1204 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
1205 struct btrfs_root
*root
)
1207 struct btrfs_path
*path
;
1208 struct extent_buffer
*leaf
;
1209 struct btrfs_dev_item
*dev_item
;
1210 struct btrfs_device
*device
;
1211 struct btrfs_key key
;
1212 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1213 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1217 path
= btrfs_alloc_path();
1221 root
= root
->fs_info
->chunk_root
;
1222 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1224 key
.type
= BTRFS_DEV_ITEM_KEY
;
1227 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1231 leaf
= path
->nodes
[0];
1233 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1234 ret
= btrfs_next_leaf(root
, path
);
1239 leaf
= path
->nodes
[0];
1240 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1241 btrfs_release_path(root
, path
);
1245 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1246 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
1247 key
.type
!= BTRFS_DEV_ITEM_KEY
)
1250 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1251 struct btrfs_dev_item
);
1252 devid
= btrfs_device_id(leaf
, dev_item
);
1253 read_extent_buffer(leaf
, dev_uuid
,
1254 (unsigned long)btrfs_device_uuid(dev_item
),
1256 read_extent_buffer(leaf
, fs_uuid
,
1257 (unsigned long)btrfs_device_fsid(dev_item
),
1259 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
1262 if (device
->fs_devices
->seeding
) {
1263 btrfs_set_device_generation(leaf
, dev_item
,
1264 device
->generation
);
1265 btrfs_mark_buffer_dirty(leaf
);
1273 btrfs_free_path(path
);
1277 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
1279 struct btrfs_trans_handle
*trans
;
1280 struct btrfs_device
*device
;
1281 struct block_device
*bdev
;
1282 struct list_head
*cur
;
1283 struct list_head
*devices
;
1284 struct super_block
*sb
= root
->fs_info
->sb
;
1286 int seeding_dev
= 0;
1289 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
1292 bdev
= open_bdev_exclusive(device_path
, 0, root
->fs_info
->bdev_holder
);
1296 if (root
->fs_info
->fs_devices
->seeding
) {
1298 down_write(&sb
->s_umount
);
1299 mutex_lock(&uuid_mutex
);
1302 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
1303 mutex_lock(&root
->fs_info
->volume_mutex
);
1305 devices
= &root
->fs_info
->fs_devices
->devices
;
1306 list_for_each(cur
, devices
) {
1307 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
1308 if (device
->bdev
== bdev
) {
1314 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1316 /* we can safely leave the fs_devices entry around */
1321 device
->name
= kstrdup(device_path
, GFP_NOFS
);
1322 if (!device
->name
) {
1328 ret
= find_next_devid(root
, &device
->devid
);
1334 trans
= btrfs_start_transaction(root
, 1);
1337 device
->barriers
= 1;
1338 device
->writeable
= 1;
1339 device
->work
.func
= pending_bios_fn
;
1340 generate_random_uuid(device
->uuid
);
1341 spin_lock_init(&device
->io_lock
);
1342 device
->generation
= trans
->transid
;
1343 device
->io_width
= root
->sectorsize
;
1344 device
->io_align
= root
->sectorsize
;
1345 device
->sector_size
= root
->sectorsize
;
1346 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1347 device
->dev_root
= root
->fs_info
->dev_root
;
1348 device
->bdev
= bdev
;
1349 device
->in_fs_metadata
= 1;
1351 set_blocksize(device
->bdev
, 4096);
1354 sb
->s_flags
&= ~MS_RDONLY
;
1355 ret
= btrfs_prepare_sprout(trans
, root
);
1359 device
->fs_devices
= root
->fs_info
->fs_devices
;
1360 list_add(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
1361 list_add(&device
->dev_alloc_list
,
1362 &root
->fs_info
->fs_devices
->alloc_list
);
1363 root
->fs_info
->fs_devices
->num_devices
++;
1364 root
->fs_info
->fs_devices
->open_devices
++;
1365 root
->fs_info
->fs_devices
->rw_devices
++;
1366 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
1368 total_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
1369 btrfs_set_super_total_bytes(&root
->fs_info
->super_copy
,
1370 total_bytes
+ device
->total_bytes
);
1372 total_bytes
= btrfs_super_num_devices(&root
->fs_info
->super_copy
);
1373 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
,
1377 ret
= init_first_rw_device(trans
, root
, device
);
1379 ret
= btrfs_finish_sprout(trans
, root
);
1382 ret
= btrfs_add_device(trans
, root
, device
);
1385 unlock_chunks(root
);
1386 btrfs_commit_transaction(trans
, root
);
1389 mutex_unlock(&uuid_mutex
);
1390 up_write(&sb
->s_umount
);
1392 ret
= btrfs_relocate_sys_chunks(root
);
1396 mutex_unlock(&root
->fs_info
->volume_mutex
);
1399 close_bdev_exclusive(bdev
, 0);
1401 mutex_unlock(&uuid_mutex
);
1402 up_write(&sb
->s_umount
);
1407 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
1408 struct btrfs_device
*device
)
1411 struct btrfs_path
*path
;
1412 struct btrfs_root
*root
;
1413 struct btrfs_dev_item
*dev_item
;
1414 struct extent_buffer
*leaf
;
1415 struct btrfs_key key
;
1417 root
= device
->dev_root
->fs_info
->chunk_root
;
1419 path
= btrfs_alloc_path();
1423 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1424 key
.type
= BTRFS_DEV_ITEM_KEY
;
1425 key
.offset
= device
->devid
;
1427 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1436 leaf
= path
->nodes
[0];
1437 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1439 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1440 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1441 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1442 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1443 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1444 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1445 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1446 btrfs_mark_buffer_dirty(leaf
);
1449 btrfs_free_path(path
);
1453 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1454 struct btrfs_device
*device
, u64 new_size
)
1456 struct btrfs_super_block
*super_copy
=
1457 &device
->dev_root
->fs_info
->super_copy
;
1458 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1459 u64 diff
= new_size
- device
->total_bytes
;
1461 if (!device
->writeable
)
1463 if (new_size
<= device
->total_bytes
)
1466 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
1467 device
->fs_devices
->total_rw_bytes
+= diff
;
1469 device
->total_bytes
= new_size
;
1470 return btrfs_update_device(trans
, device
);
1473 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1474 struct btrfs_device
*device
, u64 new_size
)
1477 lock_chunks(device
->dev_root
);
1478 ret
= __btrfs_grow_device(trans
, device
, new_size
);
1479 unlock_chunks(device
->dev_root
);
1483 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
1484 struct btrfs_root
*root
,
1485 u64 chunk_tree
, u64 chunk_objectid
,
1489 struct btrfs_path
*path
;
1490 struct btrfs_key key
;
1492 root
= root
->fs_info
->chunk_root
;
1493 path
= btrfs_alloc_path();
1497 key
.objectid
= chunk_objectid
;
1498 key
.offset
= chunk_offset
;
1499 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1501 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1504 ret
= btrfs_del_item(trans
, root
, path
);
1507 btrfs_free_path(path
);
1511 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
1514 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1515 struct btrfs_disk_key
*disk_key
;
1516 struct btrfs_chunk
*chunk
;
1523 struct btrfs_key key
;
1525 array_size
= btrfs_super_sys_array_size(super_copy
);
1527 ptr
= super_copy
->sys_chunk_array
;
1530 while (cur
< array_size
) {
1531 disk_key
= (struct btrfs_disk_key
*)ptr
;
1532 btrfs_disk_key_to_cpu(&key
, disk_key
);
1534 len
= sizeof(*disk_key
);
1536 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1537 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
1538 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
1539 len
+= btrfs_chunk_item_size(num_stripes
);
1544 if (key
.objectid
== chunk_objectid
&&
1545 key
.offset
== chunk_offset
) {
1546 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
1548 btrfs_set_super_sys_array_size(super_copy
, array_size
);
1557 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
1558 u64 chunk_tree
, u64 chunk_objectid
,
1561 struct extent_map_tree
*em_tree
;
1562 struct btrfs_root
*extent_root
;
1563 struct btrfs_trans_handle
*trans
;
1564 struct extent_map
*em
;
1565 struct map_lookup
*map
;
1569 printk(KERN_INFO
"btrfs relocating chunk %llu\n",
1570 (unsigned long long)chunk_offset
);
1571 root
= root
->fs_info
->chunk_root
;
1572 extent_root
= root
->fs_info
->extent_root
;
1573 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
1575 /* step one, relocate all the extents inside this chunk */
1576 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
1579 trans
= btrfs_start_transaction(root
, 1);
1585 * step two, delete the device extents and the
1586 * chunk tree entries
1588 spin_lock(&em_tree
->lock
);
1589 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
1590 spin_unlock(&em_tree
->lock
);
1592 BUG_ON(em
->start
> chunk_offset
||
1593 em
->start
+ em
->len
< chunk_offset
);
1594 map
= (struct map_lookup
*)em
->bdev
;
1596 for (i
= 0; i
< map
->num_stripes
; i
++) {
1597 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
1598 map
->stripes
[i
].physical
);
1601 if (map
->stripes
[i
].dev
) {
1602 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
1606 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
1611 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1612 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
1616 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
1619 spin_lock(&em_tree
->lock
);
1620 remove_extent_mapping(em_tree
, em
);
1621 spin_unlock(&em_tree
->lock
);
1626 /* once for the tree */
1627 free_extent_map(em
);
1629 free_extent_map(em
);
1631 unlock_chunks(root
);
1632 btrfs_end_transaction(trans
, root
);
1636 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
1638 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
1639 struct btrfs_path
*path
;
1640 struct extent_buffer
*leaf
;
1641 struct btrfs_chunk
*chunk
;
1642 struct btrfs_key key
;
1643 struct btrfs_key found_key
;
1644 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
1648 path
= btrfs_alloc_path();
1652 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1653 key
.offset
= (u64
)-1;
1654 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1657 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
1662 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
1669 leaf
= path
->nodes
[0];
1670 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1672 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
1673 struct btrfs_chunk
);
1674 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
1675 btrfs_release_path(chunk_root
, path
);
1677 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1678 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
1684 if (found_key
.offset
== 0)
1686 key
.offset
= found_key
.offset
- 1;
1690 btrfs_free_path(path
);
1694 static u64
div_factor(u64 num
, int factor
)
1703 int btrfs_balance(struct btrfs_root
*dev_root
)
1706 struct list_head
*cur
;
1707 struct list_head
*devices
= &dev_root
->fs_info
->fs_devices
->devices
;
1708 struct btrfs_device
*device
;
1711 struct btrfs_path
*path
;
1712 struct btrfs_key key
;
1713 struct btrfs_chunk
*chunk
;
1714 struct btrfs_root
*chunk_root
= dev_root
->fs_info
->chunk_root
;
1715 struct btrfs_trans_handle
*trans
;
1716 struct btrfs_key found_key
;
1718 if (dev_root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1721 mutex_lock(&dev_root
->fs_info
->volume_mutex
);
1722 dev_root
= dev_root
->fs_info
->dev_root
;
1724 /* step one make some room on all the devices */
1725 list_for_each(cur
, devices
) {
1726 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
1727 old_size
= device
->total_bytes
;
1728 size_to_free
= div_factor(old_size
, 1);
1729 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
1730 if (!device
->writeable
||
1731 device
->total_bytes
- device
->bytes_used
> size_to_free
)
1734 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
1737 trans
= btrfs_start_transaction(dev_root
, 1);
1740 ret
= btrfs_grow_device(trans
, device
, old_size
);
1743 btrfs_end_transaction(trans
, dev_root
);
1746 /* step two, relocate all the chunks */
1747 path
= btrfs_alloc_path();
1750 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1751 key
.offset
= (u64
)-1;
1752 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1755 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
1760 * this shouldn't happen, it means the last relocate
1766 ret
= btrfs_previous_item(chunk_root
, path
, 0,
1767 BTRFS_CHUNK_ITEM_KEY
);
1771 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1773 if (found_key
.objectid
!= key
.objectid
)
1776 chunk
= btrfs_item_ptr(path
->nodes
[0],
1778 struct btrfs_chunk
);
1779 key
.offset
= found_key
.offset
;
1780 /* chunk zero is special */
1781 if (key
.offset
== 0)
1784 btrfs_release_path(chunk_root
, path
);
1785 ret
= btrfs_relocate_chunk(chunk_root
,
1786 chunk_root
->root_key
.objectid
,
1793 btrfs_free_path(path
);
1794 mutex_unlock(&dev_root
->fs_info
->volume_mutex
);
1799 * shrinking a device means finding all of the device extents past
1800 * the new size, and then following the back refs to the chunks.
1801 * The chunk relocation code actually frees the device extent
1803 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
1805 struct btrfs_trans_handle
*trans
;
1806 struct btrfs_root
*root
= device
->dev_root
;
1807 struct btrfs_dev_extent
*dev_extent
= NULL
;
1808 struct btrfs_path
*path
;
1815 struct extent_buffer
*l
;
1816 struct btrfs_key key
;
1817 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1818 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1819 u64 diff
= device
->total_bytes
- new_size
;
1821 if (new_size
>= device
->total_bytes
)
1824 path
= btrfs_alloc_path();
1828 trans
= btrfs_start_transaction(root
, 1);
1838 device
->total_bytes
= new_size
;
1839 if (device
->writeable
)
1840 device
->fs_devices
->total_rw_bytes
-= diff
;
1841 ret
= btrfs_update_device(trans
, device
);
1843 unlock_chunks(root
);
1844 btrfs_end_transaction(trans
, root
);
1847 WARN_ON(diff
> old_total
);
1848 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
1849 unlock_chunks(root
);
1850 btrfs_end_transaction(trans
, root
);
1852 key
.objectid
= device
->devid
;
1853 key
.offset
= (u64
)-1;
1854 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1857 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1861 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
1870 slot
= path
->slots
[0];
1871 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
1873 if (key
.objectid
!= device
->devid
)
1876 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1877 length
= btrfs_dev_extent_length(l
, dev_extent
);
1879 if (key
.offset
+ length
<= new_size
)
1882 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
1883 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
1884 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
1885 btrfs_release_path(root
, path
);
1887 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
1894 btrfs_free_path(path
);
1898 static int btrfs_add_system_chunk(struct btrfs_trans_handle
*trans
,
1899 struct btrfs_root
*root
,
1900 struct btrfs_key
*key
,
1901 struct btrfs_chunk
*chunk
, int item_size
)
1903 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1904 struct btrfs_disk_key disk_key
;
1908 array_size
= btrfs_super_sys_array_size(super_copy
);
1909 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
1912 ptr
= super_copy
->sys_chunk_array
+ array_size
;
1913 btrfs_cpu_key_to_disk(&disk_key
, key
);
1914 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
1915 ptr
+= sizeof(disk_key
);
1916 memcpy(ptr
, chunk
, item_size
);
1917 item_size
+= sizeof(disk_key
);
1918 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
1922 static noinline u64
chunk_bytes_by_type(u64 type
, u64 calc_size
,
1923 int num_stripes
, int sub_stripes
)
1925 if (type
& (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_DUP
))
1927 else if (type
& BTRFS_BLOCK_GROUP_RAID10
)
1928 return calc_size
* (num_stripes
/ sub_stripes
);
1930 return calc_size
* num_stripes
;
1933 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
1934 struct btrfs_root
*extent_root
,
1935 struct map_lookup
**map_ret
,
1936 u64
*num_bytes
, u64
*stripe_size
,
1937 u64 start
, u64 type
)
1939 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
1940 struct btrfs_device
*device
= NULL
;
1941 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
1942 struct list_head
*cur
;
1943 struct map_lookup
*map
= NULL
;
1944 struct extent_map_tree
*em_tree
;
1945 struct extent_map
*em
;
1946 struct list_head private_devs
;
1947 int min_stripe_size
= 1 * 1024 * 1024;
1948 u64 calc_size
= 1024 * 1024 * 1024;
1949 u64 max_chunk_size
= calc_size
;
1954 int num_stripes
= 1;
1955 int min_stripes
= 1;
1956 int sub_stripes
= 0;
1960 int stripe_len
= 64 * 1024;
1962 if ((type
& BTRFS_BLOCK_GROUP_RAID1
) &&
1963 (type
& BTRFS_BLOCK_GROUP_DUP
)) {
1965 type
&= ~BTRFS_BLOCK_GROUP_DUP
;
1967 if (list_empty(&fs_devices
->alloc_list
))
1970 if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
1971 num_stripes
= fs_devices
->rw_devices
;
1974 if (type
& (BTRFS_BLOCK_GROUP_DUP
)) {
1978 if (type
& (BTRFS_BLOCK_GROUP_RAID1
)) {
1979 num_stripes
= min_t(u64
, 2, fs_devices
->rw_devices
);
1980 if (num_stripes
< 2)
1984 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
1985 num_stripes
= fs_devices
->rw_devices
;
1986 if (num_stripes
< 4)
1988 num_stripes
&= ~(u32
)1;
1993 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
1994 max_chunk_size
= 10 * calc_size
;
1995 min_stripe_size
= 64 * 1024 * 1024;
1996 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
1997 max_chunk_size
= 4 * calc_size
;
1998 min_stripe_size
= 32 * 1024 * 1024;
1999 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2000 calc_size
= 8 * 1024 * 1024;
2001 max_chunk_size
= calc_size
* 2;
2002 min_stripe_size
= 1 * 1024 * 1024;
2005 /* we don't want a chunk larger than 10% of writeable space */
2006 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
2010 if (!map
|| map
->num_stripes
!= num_stripes
) {
2012 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
2015 map
->num_stripes
= num_stripes
;
2018 if (calc_size
* num_stripes
> max_chunk_size
) {
2019 calc_size
= max_chunk_size
;
2020 do_div(calc_size
, num_stripes
);
2021 do_div(calc_size
, stripe_len
);
2022 calc_size
*= stripe_len
;
2024 /* we don't want tiny stripes */
2025 calc_size
= max_t(u64
, min_stripe_size
, calc_size
);
2027 do_div(calc_size
, stripe_len
);
2028 calc_size
*= stripe_len
;
2030 cur
= fs_devices
->alloc_list
.next
;
2033 if (type
& BTRFS_BLOCK_GROUP_DUP
)
2034 min_free
= calc_size
* 2;
2036 min_free
= calc_size
;
2039 * we add 1MB because we never use the first 1MB of the device, unless
2040 * we've looped, then we are likely allocating the maximum amount of
2041 * space left already
2044 min_free
+= 1024 * 1024;
2046 INIT_LIST_HEAD(&private_devs
);
2047 while (index
< num_stripes
) {
2048 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
2049 BUG_ON(!device
->writeable
);
2050 if (device
->total_bytes
> device
->bytes_used
)
2051 avail
= device
->total_bytes
- device
->bytes_used
;
2056 if (device
->in_fs_metadata
&& avail
>= min_free
) {
2057 ret
= find_free_dev_extent(trans
, device
,
2058 min_free
, &dev_offset
);
2060 list_move_tail(&device
->dev_alloc_list
,
2062 map
->stripes
[index
].dev
= device
;
2063 map
->stripes
[index
].physical
= dev_offset
;
2065 if (type
& BTRFS_BLOCK_GROUP_DUP
) {
2066 map
->stripes
[index
].dev
= device
;
2067 map
->stripes
[index
].physical
=
2068 dev_offset
+ calc_size
;
2072 } else if (device
->in_fs_metadata
&& avail
> max_avail
)
2074 if (cur
== &fs_devices
->alloc_list
)
2077 list_splice(&private_devs
, &fs_devices
->alloc_list
);
2078 if (index
< num_stripes
) {
2079 if (index
>= min_stripes
) {
2080 num_stripes
= index
;
2081 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
2082 num_stripes
/= sub_stripes
;
2083 num_stripes
*= sub_stripes
;
2088 if (!looped
&& max_avail
> 0) {
2090 calc_size
= max_avail
;
2096 map
->sector_size
= extent_root
->sectorsize
;
2097 map
->stripe_len
= stripe_len
;
2098 map
->io_align
= stripe_len
;
2099 map
->io_width
= stripe_len
;
2101 map
->num_stripes
= num_stripes
;
2102 map
->sub_stripes
= sub_stripes
;
2105 *stripe_size
= calc_size
;
2106 *num_bytes
= chunk_bytes_by_type(type
, calc_size
,
2107 num_stripes
, sub_stripes
);
2109 em
= alloc_extent_map(GFP_NOFS
);
2114 em
->bdev
= (struct block_device
*)map
;
2116 em
->len
= *num_bytes
;
2117 em
->block_start
= 0;
2118 em
->block_len
= em
->len
;
2120 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
2121 spin_lock(&em_tree
->lock
);
2122 ret
= add_extent_mapping(em_tree
, em
);
2123 spin_unlock(&em_tree
->lock
);
2125 free_extent_map(em
);
2127 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
2128 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2133 while (index
< map
->num_stripes
) {
2134 device
= map
->stripes
[index
].dev
;
2135 dev_offset
= map
->stripes
[index
].physical
;
2137 ret
= btrfs_alloc_dev_extent(trans
, device
,
2138 info
->chunk_root
->root_key
.objectid
,
2139 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2140 start
, dev_offset
, calc_size
);
2148 static int __finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
2149 struct btrfs_root
*extent_root
,
2150 struct map_lookup
*map
, u64 chunk_offset
,
2151 u64 chunk_size
, u64 stripe_size
)
2154 struct btrfs_key key
;
2155 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
2156 struct btrfs_device
*device
;
2157 struct btrfs_chunk
*chunk
;
2158 struct btrfs_stripe
*stripe
;
2159 size_t item_size
= btrfs_chunk_item_size(map
->num_stripes
);
2163 chunk
= kzalloc(item_size
, GFP_NOFS
);
2168 while (index
< map
->num_stripes
) {
2169 device
= map
->stripes
[index
].dev
;
2170 device
->bytes_used
+= stripe_size
;
2171 ret
= btrfs_update_device(trans
, device
);
2177 stripe
= &chunk
->stripe
;
2178 while (index
< map
->num_stripes
) {
2179 device
= map
->stripes
[index
].dev
;
2180 dev_offset
= map
->stripes
[index
].physical
;
2182 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
2183 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
2184 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
2189 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
2190 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
2191 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
2192 btrfs_set_stack_chunk_type(chunk
, map
->type
);
2193 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
2194 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
2195 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
2196 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
2197 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
2199 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2200 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2201 key
.offset
= chunk_offset
;
2203 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
2206 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2207 ret
= btrfs_add_system_chunk(trans
, chunk_root
, &key
, chunk
,
2216 * Chunk allocation falls into two parts. The first part does works
2217 * that make the new allocated chunk useable, but not do any operation
2218 * that modifies the chunk tree. The second part does the works that
2219 * require modifying the chunk tree. This division is important for the
2220 * bootstrap process of adding storage to a seed btrfs.
2222 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
2223 struct btrfs_root
*extent_root
, u64 type
)
2228 struct map_lookup
*map
;
2229 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
2232 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2237 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
2238 &stripe_size
, chunk_offset
, type
);
2242 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
2243 chunk_size
, stripe_size
);
2248 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
2249 struct btrfs_root
*root
,
2250 struct btrfs_device
*device
)
2253 u64 sys_chunk_offset
;
2257 u64 sys_stripe_size
;
2259 struct map_lookup
*map
;
2260 struct map_lookup
*sys_map
;
2261 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2262 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
2265 ret
= find_next_chunk(fs_info
->chunk_root
,
2266 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, &chunk_offset
);
2269 alloc_profile
= BTRFS_BLOCK_GROUP_METADATA
|
2270 (fs_info
->metadata_alloc_profile
&
2271 fs_info
->avail_metadata_alloc_bits
);
2272 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
2274 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
2275 &stripe_size
, chunk_offset
, alloc_profile
);
2278 sys_chunk_offset
= chunk_offset
+ chunk_size
;
2280 alloc_profile
= BTRFS_BLOCK_GROUP_SYSTEM
|
2281 (fs_info
->system_alloc_profile
&
2282 fs_info
->avail_system_alloc_bits
);
2283 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
2285 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &sys_map
,
2286 &sys_chunk_size
, &sys_stripe_size
,
2287 sys_chunk_offset
, alloc_profile
);
2290 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
2294 * Modifying chunk tree needs allocating new blocks from both
2295 * system block group and metadata block group. So we only can
2296 * do operations require modifying the chunk tree after both
2297 * block groups were created.
2299 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
2300 chunk_size
, stripe_size
);
2303 ret
= __finish_chunk_alloc(trans
, extent_root
, sys_map
,
2304 sys_chunk_offset
, sys_chunk_size
,
2310 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
2312 struct extent_map
*em
;
2313 struct map_lookup
*map
;
2314 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
2318 spin_lock(&map_tree
->map_tree
.lock
);
2319 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
2320 spin_unlock(&map_tree
->map_tree
.lock
);
2324 map
= (struct map_lookup
*)em
->bdev
;
2325 for (i
= 0; i
< map
->num_stripes
; i
++) {
2326 if (!map
->stripes
[i
].dev
->writeable
) {
2331 free_extent_map(em
);
2335 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
2337 extent_map_tree_init(&tree
->map_tree
, GFP_NOFS
);
2340 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
2342 struct extent_map
*em
;
2345 spin_lock(&tree
->map_tree
.lock
);
2346 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
2348 remove_extent_mapping(&tree
->map_tree
, em
);
2349 spin_unlock(&tree
->map_tree
.lock
);
2354 free_extent_map(em
);
2355 /* once for the tree */
2356 free_extent_map(em
);
2360 int btrfs_num_copies(struct btrfs_mapping_tree
*map_tree
, u64 logical
, u64 len
)
2362 struct extent_map
*em
;
2363 struct map_lookup
*map
;
2364 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2367 spin_lock(&em_tree
->lock
);
2368 em
= lookup_extent_mapping(em_tree
, logical
, len
);
2369 spin_unlock(&em_tree
->lock
);
2372 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
2373 map
= (struct map_lookup
*)em
->bdev
;
2374 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
2375 ret
= map
->num_stripes
;
2376 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
2377 ret
= map
->sub_stripes
;
2380 free_extent_map(em
);
2384 static int find_live_mirror(struct map_lookup
*map
, int first
, int num
,
2388 if (map
->stripes
[optimal
].dev
->bdev
)
2390 for (i
= first
; i
< first
+ num
; i
++) {
2391 if (map
->stripes
[i
].dev
->bdev
)
2394 /* we couldn't find one that doesn't fail. Just return something
2395 * and the io error handling code will clean up eventually
2400 static int __btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
2401 u64 logical
, u64
*length
,
2402 struct btrfs_multi_bio
**multi_ret
,
2403 int mirror_num
, struct page
*unplug_page
)
2405 struct extent_map
*em
;
2406 struct map_lookup
*map
;
2407 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2411 int stripes_allocated
= 8;
2412 int stripes_required
= 1;
2417 struct btrfs_multi_bio
*multi
= NULL
;
2419 if (multi_ret
&& !(rw
& (1 << BIO_RW
)))
2420 stripes_allocated
= 1;
2423 multi
= kzalloc(btrfs_multi_bio_size(stripes_allocated
),
2428 atomic_set(&multi
->error
, 0);
2431 spin_lock(&em_tree
->lock
);
2432 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
2433 spin_unlock(&em_tree
->lock
);
2435 if (!em
&& unplug_page
)
2439 printk(KERN_CRIT
"unable to find logical %llu len %llu\n",
2440 (unsigned long long)logical
,
2441 (unsigned long long)*length
);
2445 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
2446 map
= (struct map_lookup
*)em
->bdev
;
2447 offset
= logical
- em
->start
;
2449 if (mirror_num
> map
->num_stripes
)
2452 /* if our multi bio struct is too small, back off and try again */
2453 if (rw
& (1 << BIO_RW
)) {
2454 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
2455 BTRFS_BLOCK_GROUP_DUP
)) {
2456 stripes_required
= map
->num_stripes
;
2458 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
2459 stripes_required
= map
->sub_stripes
;
2463 if (multi_ret
&& rw
== WRITE
&&
2464 stripes_allocated
< stripes_required
) {
2465 stripes_allocated
= map
->num_stripes
;
2466 free_extent_map(em
);
2472 * stripe_nr counts the total number of stripes we have to stride
2473 * to get to this block
2475 do_div(stripe_nr
, map
->stripe_len
);
2477 stripe_offset
= stripe_nr
* map
->stripe_len
;
2478 BUG_ON(offset
< stripe_offset
);
2480 /* stripe_offset is the offset of this block in its stripe*/
2481 stripe_offset
= offset
- stripe_offset
;
2483 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
2484 BTRFS_BLOCK_GROUP_RAID10
|
2485 BTRFS_BLOCK_GROUP_DUP
)) {
2486 /* we limit the length of each bio to what fits in a stripe */
2487 *length
= min_t(u64
, em
->len
- offset
,
2488 map
->stripe_len
- stripe_offset
);
2490 *length
= em
->len
- offset
;
2493 if (!multi_ret
&& !unplug_page
)
2498 if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
2499 if (unplug_page
|| (rw
& (1 << BIO_RW
)))
2500 num_stripes
= map
->num_stripes
;
2501 else if (mirror_num
)
2502 stripe_index
= mirror_num
- 1;
2504 stripe_index
= find_live_mirror(map
, 0,
2506 current
->pid
% map
->num_stripes
);
2509 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
2510 if (rw
& (1 << BIO_RW
))
2511 num_stripes
= map
->num_stripes
;
2512 else if (mirror_num
)
2513 stripe_index
= mirror_num
- 1;
2515 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
2516 int factor
= map
->num_stripes
/ map
->sub_stripes
;
2518 stripe_index
= do_div(stripe_nr
, factor
);
2519 stripe_index
*= map
->sub_stripes
;
2521 if (unplug_page
|| (rw
& (1 << BIO_RW
)))
2522 num_stripes
= map
->sub_stripes
;
2523 else if (mirror_num
)
2524 stripe_index
+= mirror_num
- 1;
2526 stripe_index
= find_live_mirror(map
, stripe_index
,
2527 map
->sub_stripes
, stripe_index
+
2528 current
->pid
% map
->sub_stripes
);
2532 * after this do_div call, stripe_nr is the number of stripes
2533 * on this device we have to walk to find the data, and
2534 * stripe_index is the number of our device in the stripe array
2536 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
2538 BUG_ON(stripe_index
>= map
->num_stripes
);
2540 for (i
= 0; i
< num_stripes
; i
++) {
2542 struct btrfs_device
*device
;
2543 struct backing_dev_info
*bdi
;
2545 device
= map
->stripes
[stripe_index
].dev
;
2547 bdi
= blk_get_backing_dev_info(device
->bdev
);
2548 if (bdi
->unplug_io_fn
)
2549 bdi
->unplug_io_fn(bdi
, unplug_page
);
2552 multi
->stripes
[i
].physical
=
2553 map
->stripes
[stripe_index
].physical
+
2554 stripe_offset
+ stripe_nr
* map
->stripe_len
;
2555 multi
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
2561 multi
->num_stripes
= num_stripes
;
2562 multi
->max_errors
= max_errors
;
2565 free_extent_map(em
);
2569 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
2570 u64 logical
, u64
*length
,
2571 struct btrfs_multi_bio
**multi_ret
, int mirror_num
)
2573 return __btrfs_map_block(map_tree
, rw
, logical
, length
, multi_ret
,
2577 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
2578 u64 chunk_start
, u64 physical
, u64 devid
,
2579 u64
**logical
, int *naddrs
, int *stripe_len
)
2581 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2582 struct extent_map
*em
;
2583 struct map_lookup
*map
;
2590 spin_lock(&em_tree
->lock
);
2591 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
2592 spin_unlock(&em_tree
->lock
);
2594 BUG_ON(!em
|| em
->start
!= chunk_start
);
2595 map
= (struct map_lookup
*)em
->bdev
;
2598 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
2599 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
2600 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
2601 do_div(length
, map
->num_stripes
);
2603 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
2606 for (i
= 0; i
< map
->num_stripes
; i
++) {
2607 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
2609 if (map
->stripes
[i
].physical
> physical
||
2610 map
->stripes
[i
].physical
+ length
<= physical
)
2613 stripe_nr
= physical
- map
->stripes
[i
].physical
;
2614 do_div(stripe_nr
, map
->stripe_len
);
2616 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
2617 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
2618 do_div(stripe_nr
, map
->sub_stripes
);
2619 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
2620 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
2622 bytenr
= chunk_start
+ stripe_nr
* map
->stripe_len
;
2623 WARN_ON(nr
>= map
->num_stripes
);
2624 for (j
= 0; j
< nr
; j
++) {
2625 if (buf
[j
] == bytenr
)
2629 WARN_ON(nr
>= map
->num_stripes
);
2634 for (i
= 0; i
> nr
; i
++) {
2635 struct btrfs_multi_bio
*multi
;
2636 struct btrfs_bio_stripe
*stripe
;
2640 ret
= btrfs_map_block(map_tree
, WRITE
, buf
[i
],
2641 &length
, &multi
, 0);
2644 stripe
= multi
->stripes
;
2645 for (j
= 0; j
< multi
->num_stripes
; j
++) {
2646 if (stripe
->physical
>= physical
&&
2647 physical
< stripe
->physical
+ length
)
2650 BUG_ON(j
>= multi
->num_stripes
);
2656 *stripe_len
= map
->stripe_len
;
2658 free_extent_map(em
);
2662 int btrfs_unplug_page(struct btrfs_mapping_tree
*map_tree
,
2663 u64 logical
, struct page
*page
)
2665 u64 length
= PAGE_CACHE_SIZE
;
2666 return __btrfs_map_block(map_tree
, READ
, logical
, &length
,
2670 static void end_bio_multi_stripe(struct bio
*bio
, int err
)
2672 struct btrfs_multi_bio
*multi
= bio
->bi_private
;
2673 int is_orig_bio
= 0;
2676 atomic_inc(&multi
->error
);
2678 if (bio
== multi
->orig_bio
)
2681 if (atomic_dec_and_test(&multi
->stripes_pending
)) {
2684 bio
= multi
->orig_bio
;
2686 bio
->bi_private
= multi
->private;
2687 bio
->bi_end_io
= multi
->end_io
;
2688 /* only send an error to the higher layers if it is
2689 * beyond the tolerance of the multi-bio
2691 if (atomic_read(&multi
->error
) > multi
->max_errors
) {
2695 * this bio is actually up to date, we didn't
2696 * go over the max number of errors
2698 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
2703 bio_endio(bio
, err
);
2704 } else if (!is_orig_bio
) {
2709 struct async_sched
{
2712 struct btrfs_fs_info
*info
;
2713 struct btrfs_work work
;
2717 * see run_scheduled_bios for a description of why bios are collected for
2720 * This will add one bio to the pending list for a device and make sure
2721 * the work struct is scheduled.
2723 static noinline
int schedule_bio(struct btrfs_root
*root
,
2724 struct btrfs_device
*device
,
2725 int rw
, struct bio
*bio
)
2727 int should_queue
= 1;
2729 /* don't bother with additional async steps for reads, right now */
2730 if (!(rw
& (1 << BIO_RW
))) {
2732 submit_bio(rw
, bio
);
2738 * nr_async_bios allows us to reliably return congestion to the
2739 * higher layers. Otherwise, the async bio makes it appear we have
2740 * made progress against dirty pages when we've really just put it
2741 * on a queue for later
2743 atomic_inc(&root
->fs_info
->nr_async_bios
);
2744 WARN_ON(bio
->bi_next
);
2745 bio
->bi_next
= NULL
;
2748 spin_lock(&device
->io_lock
);
2750 if (device
->pending_bio_tail
)
2751 device
->pending_bio_tail
->bi_next
= bio
;
2753 device
->pending_bio_tail
= bio
;
2754 if (!device
->pending_bios
)
2755 device
->pending_bios
= bio
;
2756 if (device
->running_pending
)
2759 spin_unlock(&device
->io_lock
);
2762 btrfs_queue_worker(&root
->fs_info
->submit_workers
,
2767 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
2768 int mirror_num
, int async_submit
)
2770 struct btrfs_mapping_tree
*map_tree
;
2771 struct btrfs_device
*dev
;
2772 struct bio
*first_bio
= bio
;
2773 u64 logical
= (u64
)bio
->bi_sector
<< 9;
2776 struct btrfs_multi_bio
*multi
= NULL
;
2781 length
= bio
->bi_size
;
2782 map_tree
= &root
->fs_info
->mapping_tree
;
2783 map_length
= length
;
2785 ret
= btrfs_map_block(map_tree
, rw
, logical
, &map_length
, &multi
,
2789 total_devs
= multi
->num_stripes
;
2790 if (map_length
< length
) {
2791 printk(KERN_CRIT
"mapping failed logical %llu bio len %llu "
2792 "len %llu\n", (unsigned long long)logical
,
2793 (unsigned long long)length
,
2794 (unsigned long long)map_length
);
2797 multi
->end_io
= first_bio
->bi_end_io
;
2798 multi
->private = first_bio
->bi_private
;
2799 multi
->orig_bio
= first_bio
;
2800 atomic_set(&multi
->stripes_pending
, multi
->num_stripes
);
2802 while (dev_nr
< total_devs
) {
2803 if (total_devs
> 1) {
2804 if (dev_nr
< total_devs
- 1) {
2805 bio
= bio_clone(first_bio
, GFP_NOFS
);
2810 bio
->bi_private
= multi
;
2811 bio
->bi_end_io
= end_bio_multi_stripe
;
2813 bio
->bi_sector
= multi
->stripes
[dev_nr
].physical
>> 9;
2814 dev
= multi
->stripes
[dev_nr
].dev
;
2815 BUG_ON(rw
== WRITE
&& !dev
->writeable
);
2816 if (dev
&& dev
->bdev
) {
2817 bio
->bi_bdev
= dev
->bdev
;
2819 schedule_bio(root
, dev
, rw
, bio
);
2821 submit_bio(rw
, bio
);
2823 bio
->bi_bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2824 bio
->bi_sector
= logical
>> 9;
2825 bio_endio(bio
, -EIO
);
2829 if (total_devs
== 1)
2834 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
,
2837 struct btrfs_device
*device
;
2838 struct btrfs_fs_devices
*cur_devices
;
2840 cur_devices
= root
->fs_info
->fs_devices
;
2841 while (cur_devices
) {
2843 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
2844 device
= __find_device(&cur_devices
->devices
,
2849 cur_devices
= cur_devices
->seed
;
2854 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
2855 u64 devid
, u8
*dev_uuid
)
2857 struct btrfs_device
*device
;
2858 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
2860 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
2863 list_add(&device
->dev_list
,
2864 &fs_devices
->devices
);
2865 device
->barriers
= 1;
2866 device
->dev_root
= root
->fs_info
->dev_root
;
2867 device
->devid
= devid
;
2868 device
->work
.func
= pending_bios_fn
;
2869 device
->fs_devices
= fs_devices
;
2870 fs_devices
->num_devices
++;
2871 spin_lock_init(&device
->io_lock
);
2872 INIT_LIST_HEAD(&device
->dev_alloc_list
);
2873 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
2877 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
2878 struct extent_buffer
*leaf
,
2879 struct btrfs_chunk
*chunk
)
2881 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
2882 struct map_lookup
*map
;
2883 struct extent_map
*em
;
2887 u8 uuid
[BTRFS_UUID_SIZE
];
2892 logical
= key
->offset
;
2893 length
= btrfs_chunk_length(leaf
, chunk
);
2895 spin_lock(&map_tree
->map_tree
.lock
);
2896 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
2897 spin_unlock(&map_tree
->map_tree
.lock
);
2899 /* already mapped? */
2900 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
2901 free_extent_map(em
);
2904 free_extent_map(em
);
2907 map
= kzalloc(sizeof(*map
), GFP_NOFS
);
2911 em
= alloc_extent_map(GFP_NOFS
);
2914 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2915 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
2917 free_extent_map(em
);
2921 em
->bdev
= (struct block_device
*)map
;
2922 em
->start
= logical
;
2924 em
->block_start
= 0;
2925 em
->block_len
= em
->len
;
2927 map
->num_stripes
= num_stripes
;
2928 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
2929 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
2930 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
2931 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
2932 map
->type
= btrfs_chunk_type(leaf
, chunk
);
2933 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
2934 for (i
= 0; i
< num_stripes
; i
++) {
2935 map
->stripes
[i
].physical
=
2936 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
2937 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
2938 read_extent_buffer(leaf
, uuid
, (unsigned long)
2939 btrfs_stripe_dev_uuid_nr(chunk
, i
),
2941 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
, uuid
,
2943 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
2945 free_extent_map(em
);
2948 if (!map
->stripes
[i
].dev
) {
2949 map
->stripes
[i
].dev
=
2950 add_missing_dev(root
, devid
, uuid
);
2951 if (!map
->stripes
[i
].dev
) {
2953 free_extent_map(em
);
2957 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
2960 spin_lock(&map_tree
->map_tree
.lock
);
2961 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
2962 spin_unlock(&map_tree
->map_tree
.lock
);
2964 free_extent_map(em
);
2969 static int fill_device_from_item(struct extent_buffer
*leaf
,
2970 struct btrfs_dev_item
*dev_item
,
2971 struct btrfs_device
*device
)
2975 device
->devid
= btrfs_device_id(leaf
, dev_item
);
2976 device
->total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
2977 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
2978 device
->type
= btrfs_device_type(leaf
, dev_item
);
2979 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
2980 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
2981 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
2983 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
2984 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
2989 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
2991 struct btrfs_fs_devices
*fs_devices
;
2994 mutex_lock(&uuid_mutex
);
2996 fs_devices
= root
->fs_info
->fs_devices
->seed
;
2997 while (fs_devices
) {
2998 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
3002 fs_devices
= fs_devices
->seed
;
3005 fs_devices
= find_fsid(fsid
);
3011 fs_devices
= clone_fs_devices(fs_devices
);
3012 if (IS_ERR(fs_devices
)) {
3013 ret
= PTR_ERR(fs_devices
);
3017 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
3018 root
->fs_info
->bdev_holder
);
3022 if (!fs_devices
->seeding
) {
3023 __btrfs_close_devices(fs_devices
);
3024 free_fs_devices(fs_devices
);
3029 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
3030 root
->fs_info
->fs_devices
->seed
= fs_devices
;
3032 mutex_unlock(&uuid_mutex
);
3036 static int read_one_dev(struct btrfs_root
*root
,
3037 struct extent_buffer
*leaf
,
3038 struct btrfs_dev_item
*dev_item
)
3040 struct btrfs_device
*device
;
3043 u8 fs_uuid
[BTRFS_UUID_SIZE
];
3044 u8 dev_uuid
[BTRFS_UUID_SIZE
];
3046 devid
= btrfs_device_id(leaf
, dev_item
);
3047 read_extent_buffer(leaf
, dev_uuid
,
3048 (unsigned long)btrfs_device_uuid(dev_item
),
3050 read_extent_buffer(leaf
, fs_uuid
,
3051 (unsigned long)btrfs_device_fsid(dev_item
),
3054 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
3055 ret
= open_seed_devices(root
, fs_uuid
);
3056 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
3060 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
3061 if (!device
|| !device
->bdev
) {
3062 if (!btrfs_test_opt(root
, DEGRADED
))
3066 printk(KERN_WARNING
"warning devid %llu missing\n",
3067 (unsigned long long)devid
);
3068 device
= add_missing_dev(root
, devid
, dev_uuid
);
3074 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
3075 BUG_ON(device
->writeable
);
3076 if (device
->generation
!=
3077 btrfs_device_generation(leaf
, dev_item
))
3081 fill_device_from_item(leaf
, dev_item
, device
);
3082 device
->dev_root
= root
->fs_info
->dev_root
;
3083 device
->in_fs_metadata
= 1;
3084 if (device
->writeable
)
3085 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
3090 int btrfs_read_super_device(struct btrfs_root
*root
, struct extent_buffer
*buf
)
3092 struct btrfs_dev_item
*dev_item
;
3094 dev_item
= (struct btrfs_dev_item
*)offsetof(struct btrfs_super_block
,
3096 return read_one_dev(root
, buf
, dev_item
);
3099 int btrfs_read_sys_array(struct btrfs_root
*root
)
3101 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
3102 struct extent_buffer
*sb
;
3103 struct btrfs_disk_key
*disk_key
;
3104 struct btrfs_chunk
*chunk
;
3106 unsigned long sb_ptr
;
3112 struct btrfs_key key
;
3114 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
3115 BTRFS_SUPER_INFO_SIZE
);
3118 btrfs_set_buffer_uptodate(sb
);
3119 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
3120 array_size
= btrfs_super_sys_array_size(super_copy
);
3122 ptr
= super_copy
->sys_chunk_array
;
3123 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
3126 while (cur
< array_size
) {
3127 disk_key
= (struct btrfs_disk_key
*)ptr
;
3128 btrfs_disk_key_to_cpu(&key
, disk_key
);
3130 len
= sizeof(*disk_key
); ptr
+= len
;
3134 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
3135 chunk
= (struct btrfs_chunk
*)sb_ptr
;
3136 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
3139 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
3140 len
= btrfs_chunk_item_size(num_stripes
);
3149 free_extent_buffer(sb
);
3153 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
3155 struct btrfs_path
*path
;
3156 struct extent_buffer
*leaf
;
3157 struct btrfs_key key
;
3158 struct btrfs_key found_key
;
3162 root
= root
->fs_info
->chunk_root
;
3164 path
= btrfs_alloc_path();
3168 /* first we search for all of the device items, and then we
3169 * read in all of the chunk items. This way we can create chunk
3170 * mappings that reference all of the devices that are afound
3172 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
3176 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3178 leaf
= path
->nodes
[0];
3179 slot
= path
->slots
[0];
3180 if (slot
>= btrfs_header_nritems(leaf
)) {
3181 ret
= btrfs_next_leaf(root
, path
);
3188 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3189 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
3190 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
3192 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
3193 struct btrfs_dev_item
*dev_item
;
3194 dev_item
= btrfs_item_ptr(leaf
, slot
,
3195 struct btrfs_dev_item
);
3196 ret
= read_one_dev(root
, leaf
, dev_item
);
3200 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
3201 struct btrfs_chunk
*chunk
;
3202 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
3203 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
3209 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
3211 btrfs_release_path(root
, path
);
3216 btrfs_free_path(path
);