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/iocontext.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
;
108 list_for_each_entry(dev
, head
, dev_list
) {
109 if (dev
->devid
== devid
&&
110 (!uuid
|| !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
))) {
117 static noinline
struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
119 struct btrfs_fs_devices
*fs_devices
;
121 list_for_each_entry(fs_devices
, &fs_uuids
, list
) {
122 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
128 static void requeue_list(struct btrfs_pending_bios
*pending_bios
,
129 struct bio
*head
, struct bio
*tail
)
132 struct bio
*old_head
;
134 old_head
= pending_bios
->head
;
135 pending_bios
->head
= head
;
136 if (pending_bios
->tail
)
137 tail
->bi_next
= old_head
;
139 pending_bios
->tail
= tail
;
143 * we try to collect pending bios for a device so we don't get a large
144 * number of procs sending bios down to the same device. This greatly
145 * improves the schedulers ability to collect and merge the bios.
147 * But, it also turns into a long list of bios to process and that is sure
148 * to eventually make the worker thread block. The solution here is to
149 * make some progress and then put this work struct back at the end of
150 * the list if the block device is congested. This way, multiple devices
151 * can make progress from a single worker thread.
153 static noinline
int run_scheduled_bios(struct btrfs_device
*device
)
156 struct backing_dev_info
*bdi
;
157 struct btrfs_fs_info
*fs_info
;
158 struct btrfs_pending_bios
*pending_bios
;
162 unsigned long num_run
;
163 unsigned long num_sync_run
;
164 unsigned long batch_run
= 0;
166 unsigned long last_waited
= 0;
169 bdi
= blk_get_backing_dev_info(device
->bdev
);
170 fs_info
= device
->dev_root
->fs_info
;
171 limit
= btrfs_async_submit_limit(fs_info
);
172 limit
= limit
* 2 / 3;
174 /* we want to make sure that every time we switch from the sync
175 * list to the normal list, we unplug
180 spin_lock(&device
->io_lock
);
185 /* take all the bios off the list at once and process them
186 * later on (without the lock held). But, remember the
187 * tail and other pointers so the bios can be properly reinserted
188 * into the list if we hit congestion
190 if (!force_reg
&& device
->pending_sync_bios
.head
) {
191 pending_bios
= &device
->pending_sync_bios
;
194 pending_bios
= &device
->pending_bios
;
198 pending
= pending_bios
->head
;
199 tail
= pending_bios
->tail
;
200 WARN_ON(pending
&& !tail
);
203 * if pending was null this time around, no bios need processing
204 * at all and we can stop. Otherwise it'll loop back up again
205 * and do an additional check so no bios are missed.
207 * device->running_pending is used to synchronize with the
210 if (device
->pending_sync_bios
.head
== NULL
&&
211 device
->pending_bios
.head
== NULL
) {
213 device
->running_pending
= 0;
216 device
->running_pending
= 1;
219 pending_bios
->head
= NULL
;
220 pending_bios
->tail
= NULL
;
222 spin_unlock(&device
->io_lock
);
225 * if we're doing the regular priority list, make sure we unplug
226 * for any high prio bios we've sent down
228 if (pending_bios
== &device
->pending_bios
&& num_sync_run
> 0) {
230 blk_run_backing_dev(bdi
, NULL
);
236 /* we want to work on both lists, but do more bios on the
237 * sync list than the regular list
240 pending_bios
!= &device
->pending_sync_bios
&&
241 device
->pending_sync_bios
.head
) ||
242 (num_run
> 64 && pending_bios
== &device
->pending_sync_bios
&&
243 device
->pending_bios
.head
)) {
244 spin_lock(&device
->io_lock
);
245 requeue_list(pending_bios
, pending
, tail
);
250 pending
= pending
->bi_next
;
252 atomic_dec(&fs_info
->nr_async_bios
);
254 if (atomic_read(&fs_info
->nr_async_bios
) < limit
&&
255 waitqueue_active(&fs_info
->async_submit_wait
))
256 wake_up(&fs_info
->async_submit_wait
);
258 BUG_ON(atomic_read(&cur
->bi_cnt
) == 0);
259 submit_bio(cur
->bi_rw
, cur
);
263 if (bio_rw_flagged(cur
, BIO_RW_SYNCIO
))
266 if (need_resched()) {
268 blk_run_backing_dev(bdi
, NULL
);
275 * we made progress, there is more work to do and the bdi
276 * is now congested. Back off and let other work structs
279 if (pending
&& bdi_write_congested(bdi
) && batch_run
> 8 &&
280 fs_info
->fs_devices
->open_devices
> 1) {
281 struct io_context
*ioc
;
283 ioc
= current
->io_context
;
286 * the main goal here is that we don't want to
287 * block if we're going to be able to submit
288 * more requests without blocking.
290 * This code does two great things, it pokes into
291 * the elevator code from a filesystem _and_
292 * it makes assumptions about how batching works.
294 if (ioc
&& ioc
->nr_batch_requests
> 0 &&
295 time_before(jiffies
, ioc
->last_waited
+ HZ
/50UL) &&
297 ioc
->last_waited
== last_waited
)) {
299 * we want to go through our batch of
300 * requests and stop. So, we copy out
301 * the ioc->last_waited time and test
302 * against it before looping
304 last_waited
= ioc
->last_waited
;
305 if (need_resched()) {
307 blk_run_backing_dev(bdi
, NULL
);
314 spin_lock(&device
->io_lock
);
315 requeue_list(pending_bios
, pending
, tail
);
316 device
->running_pending
= 1;
318 spin_unlock(&device
->io_lock
);
319 btrfs_requeue_work(&device
->work
);
326 blk_run_backing_dev(bdi
, NULL
);
333 spin_lock(&device
->io_lock
);
334 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
336 spin_unlock(&device
->io_lock
);
339 * IO has already been through a long path to get here. Checksumming,
340 * async helper threads, perhaps compression. We've done a pretty
341 * good job of collecting a batch of IO and should just unplug
342 * the device right away.
344 * This will help anyone who is waiting on the IO, they might have
345 * already unplugged, but managed to do so before the bio they
346 * cared about found its way down here.
348 blk_run_backing_dev(bdi
, NULL
);
353 static void pending_bios_fn(struct btrfs_work
*work
)
355 struct btrfs_device
*device
;
357 device
= container_of(work
, struct btrfs_device
, work
);
358 run_scheduled_bios(device
);
361 static noinline
int device_list_add(const char *path
,
362 struct btrfs_super_block
*disk_super
,
363 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
365 struct btrfs_device
*device
;
366 struct btrfs_fs_devices
*fs_devices
;
367 u64 found_transid
= btrfs_super_generation(disk_super
);
369 fs_devices
= find_fsid(disk_super
->fsid
);
371 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
374 INIT_LIST_HEAD(&fs_devices
->devices
);
375 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
376 list_add(&fs_devices
->list
, &fs_uuids
);
377 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
378 fs_devices
->latest_devid
= devid
;
379 fs_devices
->latest_trans
= found_transid
;
380 mutex_init(&fs_devices
->device_list_mutex
);
383 device
= __find_device(&fs_devices
->devices
, devid
,
384 disk_super
->dev_item
.uuid
);
387 if (fs_devices
->opened
)
390 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
392 /* we can safely leave the fs_devices entry around */
395 device
->devid
= devid
;
396 device
->work
.func
= pending_bios_fn
;
397 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
399 device
->barriers
= 1;
400 spin_lock_init(&device
->io_lock
);
401 device
->name
= kstrdup(path
, GFP_NOFS
);
406 INIT_LIST_HEAD(&device
->dev_alloc_list
);
408 mutex_lock(&fs_devices
->device_list_mutex
);
409 list_add(&device
->dev_list
, &fs_devices
->devices
);
410 mutex_unlock(&fs_devices
->device_list_mutex
);
412 device
->fs_devices
= fs_devices
;
413 fs_devices
->num_devices
++;
416 if (found_transid
> fs_devices
->latest_trans
) {
417 fs_devices
->latest_devid
= devid
;
418 fs_devices
->latest_trans
= found_transid
;
420 *fs_devices_ret
= fs_devices
;
424 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
426 struct btrfs_fs_devices
*fs_devices
;
427 struct btrfs_device
*device
;
428 struct btrfs_device
*orig_dev
;
430 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
432 return ERR_PTR(-ENOMEM
);
434 INIT_LIST_HEAD(&fs_devices
->devices
);
435 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
436 INIT_LIST_HEAD(&fs_devices
->list
);
437 mutex_init(&fs_devices
->device_list_mutex
);
438 fs_devices
->latest_devid
= orig
->latest_devid
;
439 fs_devices
->latest_trans
= orig
->latest_trans
;
440 memcpy(fs_devices
->fsid
, orig
->fsid
, sizeof(fs_devices
->fsid
));
442 mutex_lock(&orig
->device_list_mutex
);
443 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
444 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
448 device
->name
= kstrdup(orig_dev
->name
, GFP_NOFS
);
454 device
->devid
= orig_dev
->devid
;
455 device
->work
.func
= pending_bios_fn
;
456 memcpy(device
->uuid
, orig_dev
->uuid
, sizeof(device
->uuid
));
457 device
->barriers
= 1;
458 spin_lock_init(&device
->io_lock
);
459 INIT_LIST_HEAD(&device
->dev_list
);
460 INIT_LIST_HEAD(&device
->dev_alloc_list
);
462 list_add(&device
->dev_list
, &fs_devices
->devices
);
463 device
->fs_devices
= fs_devices
;
464 fs_devices
->num_devices
++;
466 mutex_unlock(&orig
->device_list_mutex
);
469 mutex_unlock(&orig
->device_list_mutex
);
470 free_fs_devices(fs_devices
);
471 return ERR_PTR(-ENOMEM
);
474 int btrfs_close_extra_devices(struct btrfs_fs_devices
*fs_devices
)
476 struct btrfs_device
*device
, *next
;
478 mutex_lock(&uuid_mutex
);
480 mutex_lock(&fs_devices
->device_list_mutex
);
481 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
482 if (device
->in_fs_metadata
)
486 close_bdev_exclusive(device
->bdev
, device
->mode
);
488 fs_devices
->open_devices
--;
490 if (device
->writeable
) {
491 list_del_init(&device
->dev_alloc_list
);
492 device
->writeable
= 0;
493 fs_devices
->rw_devices
--;
495 list_del_init(&device
->dev_list
);
496 fs_devices
->num_devices
--;
500 mutex_unlock(&fs_devices
->device_list_mutex
);
502 if (fs_devices
->seed
) {
503 fs_devices
= fs_devices
->seed
;
507 mutex_unlock(&uuid_mutex
);
511 static int __btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
513 struct btrfs_device
*device
;
515 if (--fs_devices
->opened
> 0)
518 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
520 close_bdev_exclusive(device
->bdev
, device
->mode
);
521 fs_devices
->open_devices
--;
523 if (device
->writeable
) {
524 list_del_init(&device
->dev_alloc_list
);
525 fs_devices
->rw_devices
--;
529 device
->writeable
= 0;
530 device
->in_fs_metadata
= 0;
532 WARN_ON(fs_devices
->open_devices
);
533 WARN_ON(fs_devices
->rw_devices
);
534 fs_devices
->opened
= 0;
535 fs_devices
->seeding
= 0;
540 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
542 struct btrfs_fs_devices
*seed_devices
= NULL
;
545 mutex_lock(&uuid_mutex
);
546 ret
= __btrfs_close_devices(fs_devices
);
547 if (!fs_devices
->opened
) {
548 seed_devices
= fs_devices
->seed
;
549 fs_devices
->seed
= NULL
;
551 mutex_unlock(&uuid_mutex
);
553 while (seed_devices
) {
554 fs_devices
= seed_devices
;
555 seed_devices
= fs_devices
->seed
;
556 __btrfs_close_devices(fs_devices
);
557 free_fs_devices(fs_devices
);
562 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
563 fmode_t flags
, void *holder
)
565 struct block_device
*bdev
;
566 struct list_head
*head
= &fs_devices
->devices
;
567 struct btrfs_device
*device
;
568 struct block_device
*latest_bdev
= NULL
;
569 struct buffer_head
*bh
;
570 struct btrfs_super_block
*disk_super
;
571 u64 latest_devid
= 0;
572 u64 latest_transid
= 0;
577 list_for_each_entry(device
, head
, dev_list
) {
583 bdev
= open_bdev_exclusive(device
->name
, flags
, holder
);
585 printk(KERN_INFO
"open %s failed\n", device
->name
);
588 set_blocksize(bdev
, 4096);
590 bh
= btrfs_read_dev_super(bdev
);
594 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
595 devid
= le64_to_cpu(disk_super
->dev_item
.devid
);
596 if (devid
!= device
->devid
)
599 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
603 device
->generation
= btrfs_super_generation(disk_super
);
604 if (!latest_transid
|| device
->generation
> latest_transid
) {
605 latest_devid
= devid
;
606 latest_transid
= device
->generation
;
610 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
611 device
->writeable
= 0;
613 device
->writeable
= !bdev_read_only(bdev
);
618 device
->in_fs_metadata
= 0;
619 device
->mode
= flags
;
621 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
622 fs_devices
->rotating
= 1;
624 fs_devices
->open_devices
++;
625 if (device
->writeable
) {
626 fs_devices
->rw_devices
++;
627 list_add(&device
->dev_alloc_list
,
628 &fs_devices
->alloc_list
);
635 close_bdev_exclusive(bdev
, FMODE_READ
);
639 if (fs_devices
->open_devices
== 0) {
643 fs_devices
->seeding
= seeding
;
644 fs_devices
->opened
= 1;
645 fs_devices
->latest_bdev
= latest_bdev
;
646 fs_devices
->latest_devid
= latest_devid
;
647 fs_devices
->latest_trans
= latest_transid
;
648 fs_devices
->total_rw_bytes
= 0;
653 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
654 fmode_t flags
, void *holder
)
658 mutex_lock(&uuid_mutex
);
659 if (fs_devices
->opened
) {
660 fs_devices
->opened
++;
663 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
665 mutex_unlock(&uuid_mutex
);
669 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
670 struct btrfs_fs_devices
**fs_devices_ret
)
672 struct btrfs_super_block
*disk_super
;
673 struct block_device
*bdev
;
674 struct buffer_head
*bh
;
679 mutex_lock(&uuid_mutex
);
681 bdev
= open_bdev_exclusive(path
, flags
, holder
);
688 ret
= set_blocksize(bdev
, 4096);
691 bh
= btrfs_read_dev_super(bdev
);
696 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
697 devid
= le64_to_cpu(disk_super
->dev_item
.devid
);
698 transid
= btrfs_super_generation(disk_super
);
699 if (disk_super
->label
[0])
700 printk(KERN_INFO
"device label %s ", disk_super
->label
);
702 /* FIXME, make a readl uuid parser */
703 printk(KERN_INFO
"device fsid %llx-%llx ",
704 *(unsigned long long *)disk_super
->fsid
,
705 *(unsigned long long *)(disk_super
->fsid
+ 8));
707 printk(KERN_CONT
"devid %llu transid %llu %s\n",
708 (unsigned long long)devid
, (unsigned long long)transid
, path
);
709 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
713 close_bdev_exclusive(bdev
, flags
);
715 mutex_unlock(&uuid_mutex
);
720 * this uses a pretty simple search, the expectation is that it is
721 * called very infrequently and that a given device has a small number
724 int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
725 struct btrfs_device
*device
, u64 num_bytes
,
726 u64
*start
, u64
*max_avail
)
728 struct btrfs_key key
;
729 struct btrfs_root
*root
= device
->dev_root
;
730 struct btrfs_dev_extent
*dev_extent
= NULL
;
731 struct btrfs_path
*path
;
734 u64 search_start
= 0;
735 u64 search_end
= device
->total_bytes
;
739 struct extent_buffer
*l
;
741 path
= btrfs_alloc_path();
747 /* FIXME use last free of some kind */
749 /* we don't want to overwrite the superblock on the drive,
750 * so we make sure to start at an offset of at least 1MB
752 search_start
= max((u64
)1024 * 1024, search_start
);
754 if (root
->fs_info
->alloc_start
+ num_bytes
<= device
->total_bytes
)
755 search_start
= max(root
->fs_info
->alloc_start
, search_start
);
757 key
.objectid
= device
->devid
;
758 key
.offset
= search_start
;
759 key
.type
= BTRFS_DEV_EXTENT_KEY
;
760 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
764 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
771 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
774 slot
= path
->slots
[0];
775 if (slot
>= btrfs_header_nritems(l
)) {
776 ret
= btrfs_next_leaf(root
, path
);
783 if (search_start
>= search_end
) {
787 *start
= search_start
;
791 *start
= last_byte
> search_start
?
792 last_byte
: search_start
;
793 if (search_end
<= *start
) {
799 btrfs_item_key_to_cpu(l
, &key
, slot
);
801 if (key
.objectid
< device
->devid
)
804 if (key
.objectid
> device
->devid
)
807 if (key
.offset
>= search_start
&& key
.offset
> last_byte
&&
809 if (last_byte
< search_start
)
810 last_byte
= search_start
;
811 hole_size
= key
.offset
- last_byte
;
813 if (hole_size
> *max_avail
)
814 *max_avail
= hole_size
;
816 if (key
.offset
> last_byte
&&
817 hole_size
>= num_bytes
) {
822 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
826 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
827 last_byte
= key
.offset
+ btrfs_dev_extent_length(l
, dev_extent
);
833 /* we have to make sure we didn't find an extent that has already
834 * been allocated by the map tree or the original allocation
836 BUG_ON(*start
< search_start
);
838 if (*start
+ num_bytes
> search_end
) {
842 /* check for pending inserts here */
846 btrfs_free_path(path
);
850 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
851 struct btrfs_device
*device
,
855 struct btrfs_path
*path
;
856 struct btrfs_root
*root
= device
->dev_root
;
857 struct btrfs_key key
;
858 struct btrfs_key found_key
;
859 struct extent_buffer
*leaf
= NULL
;
860 struct btrfs_dev_extent
*extent
= NULL
;
862 path
= btrfs_alloc_path();
866 key
.objectid
= device
->devid
;
868 key
.type
= BTRFS_DEV_EXTENT_KEY
;
870 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
872 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
873 BTRFS_DEV_EXTENT_KEY
);
875 leaf
= path
->nodes
[0];
876 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
877 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
878 struct btrfs_dev_extent
);
879 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
880 btrfs_dev_extent_length(leaf
, extent
) < start
);
882 } else if (ret
== 0) {
883 leaf
= path
->nodes
[0];
884 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
885 struct btrfs_dev_extent
);
889 if (device
->bytes_used
> 0)
890 device
->bytes_used
-= btrfs_dev_extent_length(leaf
, extent
);
891 ret
= btrfs_del_item(trans
, root
, path
);
894 btrfs_free_path(path
);
898 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
899 struct btrfs_device
*device
,
900 u64 chunk_tree
, u64 chunk_objectid
,
901 u64 chunk_offset
, u64 start
, u64 num_bytes
)
904 struct btrfs_path
*path
;
905 struct btrfs_root
*root
= device
->dev_root
;
906 struct btrfs_dev_extent
*extent
;
907 struct extent_buffer
*leaf
;
908 struct btrfs_key key
;
910 WARN_ON(!device
->in_fs_metadata
);
911 path
= btrfs_alloc_path();
915 key
.objectid
= device
->devid
;
917 key
.type
= BTRFS_DEV_EXTENT_KEY
;
918 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
922 leaf
= path
->nodes
[0];
923 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
924 struct btrfs_dev_extent
);
925 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
926 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
927 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
929 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
930 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
933 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
934 btrfs_mark_buffer_dirty(leaf
);
935 btrfs_free_path(path
);
939 static noinline
int find_next_chunk(struct btrfs_root
*root
,
940 u64 objectid
, u64
*offset
)
942 struct btrfs_path
*path
;
944 struct btrfs_key key
;
945 struct btrfs_chunk
*chunk
;
946 struct btrfs_key found_key
;
948 path
= btrfs_alloc_path();
951 key
.objectid
= objectid
;
952 key
.offset
= (u64
)-1;
953 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
955 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
961 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
965 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
967 if (found_key
.objectid
!= objectid
)
970 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
972 *offset
= found_key
.offset
+
973 btrfs_chunk_length(path
->nodes
[0], chunk
);
978 btrfs_free_path(path
);
982 static noinline
int find_next_devid(struct btrfs_root
*root
, u64
*objectid
)
985 struct btrfs_key key
;
986 struct btrfs_key found_key
;
987 struct btrfs_path
*path
;
989 root
= root
->fs_info
->chunk_root
;
991 path
= btrfs_alloc_path();
995 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
996 key
.type
= BTRFS_DEV_ITEM_KEY
;
997 key
.offset
= (u64
)-1;
999 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1005 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
1006 BTRFS_DEV_ITEM_KEY
);
1010 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1012 *objectid
= found_key
.offset
+ 1;
1016 btrfs_free_path(path
);
1021 * the device information is stored in the chunk root
1022 * the btrfs_device struct should be fully filled in
1024 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
1025 struct btrfs_root
*root
,
1026 struct btrfs_device
*device
)
1029 struct btrfs_path
*path
;
1030 struct btrfs_dev_item
*dev_item
;
1031 struct extent_buffer
*leaf
;
1032 struct btrfs_key key
;
1035 root
= root
->fs_info
->chunk_root
;
1037 path
= btrfs_alloc_path();
1041 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1042 key
.type
= BTRFS_DEV_ITEM_KEY
;
1043 key
.offset
= device
->devid
;
1045 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1050 leaf
= path
->nodes
[0];
1051 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1053 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1054 btrfs_set_device_generation(leaf
, dev_item
, 0);
1055 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1056 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1057 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1058 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1059 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1060 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1061 btrfs_set_device_group(leaf
, dev_item
, 0);
1062 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1063 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1064 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1066 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1067 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1068 ptr
= (unsigned long)btrfs_device_fsid(dev_item
);
1069 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1070 btrfs_mark_buffer_dirty(leaf
);
1074 btrfs_free_path(path
);
1078 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1079 struct btrfs_device
*device
)
1082 struct btrfs_path
*path
;
1083 struct btrfs_key key
;
1084 struct btrfs_trans_handle
*trans
;
1086 root
= root
->fs_info
->chunk_root
;
1088 path
= btrfs_alloc_path();
1092 trans
= btrfs_start_transaction(root
, 1);
1093 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1094 key
.type
= BTRFS_DEV_ITEM_KEY
;
1095 key
.offset
= device
->devid
;
1098 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1107 ret
= btrfs_del_item(trans
, root
, path
);
1111 btrfs_free_path(path
);
1112 unlock_chunks(root
);
1113 btrfs_commit_transaction(trans
, root
);
1117 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1119 struct btrfs_device
*device
;
1120 struct btrfs_device
*next_device
;
1121 struct block_device
*bdev
;
1122 struct buffer_head
*bh
= NULL
;
1123 struct btrfs_super_block
*disk_super
;
1130 mutex_lock(&uuid_mutex
);
1131 mutex_lock(&root
->fs_info
->volume_mutex
);
1133 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1134 root
->fs_info
->avail_system_alloc_bits
|
1135 root
->fs_info
->avail_metadata_alloc_bits
;
1137 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) &&
1138 root
->fs_info
->fs_devices
->rw_devices
<= 4) {
1139 printk(KERN_ERR
"btrfs: unable to go below four devices "
1145 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) &&
1146 root
->fs_info
->fs_devices
->rw_devices
<= 2) {
1147 printk(KERN_ERR
"btrfs: unable to go below two "
1148 "devices on raid1\n");
1153 if (strcmp(device_path
, "missing") == 0) {
1154 struct list_head
*devices
;
1155 struct btrfs_device
*tmp
;
1158 devices
= &root
->fs_info
->fs_devices
->devices
;
1159 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1160 list_for_each_entry(tmp
, devices
, dev_list
) {
1161 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1166 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1171 printk(KERN_ERR
"btrfs: no missing devices found to "
1176 bdev
= open_bdev_exclusive(device_path
, FMODE_READ
,
1177 root
->fs_info
->bdev_holder
);
1179 ret
= PTR_ERR(bdev
);
1183 set_blocksize(bdev
, 4096);
1184 bh
= btrfs_read_dev_super(bdev
);
1189 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1190 devid
= le64_to_cpu(disk_super
->dev_item
.devid
);
1191 dev_uuid
= disk_super
->dev_item
.uuid
;
1192 device
= btrfs_find_device(root
, devid
, dev_uuid
,
1200 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1201 printk(KERN_ERR
"btrfs: unable to remove the only writeable "
1207 if (device
->writeable
) {
1208 list_del_init(&device
->dev_alloc_list
);
1209 root
->fs_info
->fs_devices
->rw_devices
--;
1212 ret
= btrfs_shrink_device(device
, 0);
1216 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1220 device
->in_fs_metadata
= 0;
1223 * the device list mutex makes sure that we don't change
1224 * the device list while someone else is writing out all
1225 * the device supers.
1227 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1228 list_del_init(&device
->dev_list
);
1229 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1231 device
->fs_devices
->num_devices
--;
1233 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1234 struct btrfs_device
, dev_list
);
1235 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1236 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1237 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1238 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1241 close_bdev_exclusive(device
->bdev
, device
->mode
);
1242 device
->bdev
= NULL
;
1243 device
->fs_devices
->open_devices
--;
1246 num_devices
= btrfs_super_num_devices(&root
->fs_info
->super_copy
) - 1;
1247 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
, num_devices
);
1249 if (device
->fs_devices
->open_devices
== 0) {
1250 struct btrfs_fs_devices
*fs_devices
;
1251 fs_devices
= root
->fs_info
->fs_devices
;
1252 while (fs_devices
) {
1253 if (fs_devices
->seed
== device
->fs_devices
)
1255 fs_devices
= fs_devices
->seed
;
1257 fs_devices
->seed
= device
->fs_devices
->seed
;
1258 device
->fs_devices
->seed
= NULL
;
1259 __btrfs_close_devices(device
->fs_devices
);
1260 free_fs_devices(device
->fs_devices
);
1264 * at this point, the device is zero sized. We want to
1265 * remove it from the devices list and zero out the old super
1267 if (device
->writeable
) {
1268 /* make sure this device isn't detected as part of
1271 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1272 set_buffer_dirty(bh
);
1273 sync_dirty_buffer(bh
);
1276 kfree(device
->name
);
1284 close_bdev_exclusive(bdev
, FMODE_READ
);
1286 mutex_unlock(&root
->fs_info
->volume_mutex
);
1287 mutex_unlock(&uuid_mutex
);
1292 * does all the dirty work required for changing file system's UUID.
1294 static int btrfs_prepare_sprout(struct btrfs_trans_handle
*trans
,
1295 struct btrfs_root
*root
)
1297 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1298 struct btrfs_fs_devices
*old_devices
;
1299 struct btrfs_fs_devices
*seed_devices
;
1300 struct btrfs_super_block
*disk_super
= &root
->fs_info
->super_copy
;
1301 struct btrfs_device
*device
;
1304 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1305 if (!fs_devices
->seeding
)
1308 seed_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
1312 old_devices
= clone_fs_devices(fs_devices
);
1313 if (IS_ERR(old_devices
)) {
1314 kfree(seed_devices
);
1315 return PTR_ERR(old_devices
);
1318 list_add(&old_devices
->list
, &fs_uuids
);
1320 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1321 seed_devices
->opened
= 1;
1322 INIT_LIST_HEAD(&seed_devices
->devices
);
1323 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1324 mutex_init(&seed_devices
->device_list_mutex
);
1325 list_splice_init(&fs_devices
->devices
, &seed_devices
->devices
);
1326 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1327 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1328 device
->fs_devices
= seed_devices
;
1331 fs_devices
->seeding
= 0;
1332 fs_devices
->num_devices
= 0;
1333 fs_devices
->open_devices
= 0;
1334 fs_devices
->seed
= seed_devices
;
1336 generate_random_uuid(fs_devices
->fsid
);
1337 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1338 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1339 super_flags
= btrfs_super_flags(disk_super
) &
1340 ~BTRFS_SUPER_FLAG_SEEDING
;
1341 btrfs_set_super_flags(disk_super
, super_flags
);
1347 * strore the expected generation for seed devices in device items.
1349 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
1350 struct btrfs_root
*root
)
1352 struct btrfs_path
*path
;
1353 struct extent_buffer
*leaf
;
1354 struct btrfs_dev_item
*dev_item
;
1355 struct btrfs_device
*device
;
1356 struct btrfs_key key
;
1357 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1358 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1362 path
= btrfs_alloc_path();
1366 root
= root
->fs_info
->chunk_root
;
1367 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1369 key
.type
= BTRFS_DEV_ITEM_KEY
;
1372 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1376 leaf
= path
->nodes
[0];
1378 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1379 ret
= btrfs_next_leaf(root
, path
);
1384 leaf
= path
->nodes
[0];
1385 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1386 btrfs_release_path(root
, path
);
1390 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1391 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
1392 key
.type
!= BTRFS_DEV_ITEM_KEY
)
1395 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1396 struct btrfs_dev_item
);
1397 devid
= btrfs_device_id(leaf
, dev_item
);
1398 read_extent_buffer(leaf
, dev_uuid
,
1399 (unsigned long)btrfs_device_uuid(dev_item
),
1401 read_extent_buffer(leaf
, fs_uuid
,
1402 (unsigned long)btrfs_device_fsid(dev_item
),
1404 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
1407 if (device
->fs_devices
->seeding
) {
1408 btrfs_set_device_generation(leaf
, dev_item
,
1409 device
->generation
);
1410 btrfs_mark_buffer_dirty(leaf
);
1418 btrfs_free_path(path
);
1422 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
1424 struct btrfs_trans_handle
*trans
;
1425 struct btrfs_device
*device
;
1426 struct block_device
*bdev
;
1427 struct list_head
*devices
;
1428 struct super_block
*sb
= root
->fs_info
->sb
;
1430 int seeding_dev
= 0;
1433 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
1436 bdev
= open_bdev_exclusive(device_path
, 0, root
->fs_info
->bdev_holder
);
1440 if (root
->fs_info
->fs_devices
->seeding
) {
1442 down_write(&sb
->s_umount
);
1443 mutex_lock(&uuid_mutex
);
1446 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
1447 mutex_lock(&root
->fs_info
->volume_mutex
);
1449 devices
= &root
->fs_info
->fs_devices
->devices
;
1451 * we have the volume lock, so we don't need the extra
1452 * device list mutex while reading the list here.
1454 list_for_each_entry(device
, devices
, dev_list
) {
1455 if (device
->bdev
== bdev
) {
1461 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1463 /* we can safely leave the fs_devices entry around */
1468 device
->name
= kstrdup(device_path
, GFP_NOFS
);
1469 if (!device
->name
) {
1475 ret
= find_next_devid(root
, &device
->devid
);
1481 trans
= btrfs_start_transaction(root
, 1);
1484 device
->barriers
= 1;
1485 device
->writeable
= 1;
1486 device
->work
.func
= pending_bios_fn
;
1487 generate_random_uuid(device
->uuid
);
1488 spin_lock_init(&device
->io_lock
);
1489 device
->generation
= trans
->transid
;
1490 device
->io_width
= root
->sectorsize
;
1491 device
->io_align
= root
->sectorsize
;
1492 device
->sector_size
= root
->sectorsize
;
1493 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1494 device
->disk_total_bytes
= device
->total_bytes
;
1495 device
->dev_root
= root
->fs_info
->dev_root
;
1496 device
->bdev
= bdev
;
1497 device
->in_fs_metadata
= 1;
1499 set_blocksize(device
->bdev
, 4096);
1502 sb
->s_flags
&= ~MS_RDONLY
;
1503 ret
= btrfs_prepare_sprout(trans
, root
);
1507 device
->fs_devices
= root
->fs_info
->fs_devices
;
1510 * we don't want write_supers to jump in here with our device
1513 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1514 list_add(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
1515 list_add(&device
->dev_alloc_list
,
1516 &root
->fs_info
->fs_devices
->alloc_list
);
1517 root
->fs_info
->fs_devices
->num_devices
++;
1518 root
->fs_info
->fs_devices
->open_devices
++;
1519 root
->fs_info
->fs_devices
->rw_devices
++;
1520 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
1522 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
1523 root
->fs_info
->fs_devices
->rotating
= 1;
1525 total_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
1526 btrfs_set_super_total_bytes(&root
->fs_info
->super_copy
,
1527 total_bytes
+ device
->total_bytes
);
1529 total_bytes
= btrfs_super_num_devices(&root
->fs_info
->super_copy
);
1530 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
,
1532 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1535 ret
= init_first_rw_device(trans
, root
, device
);
1537 ret
= btrfs_finish_sprout(trans
, root
);
1540 ret
= btrfs_add_device(trans
, root
, device
);
1544 * we've got more storage, clear any full flags on the space
1547 btrfs_clear_space_info_full(root
->fs_info
);
1549 unlock_chunks(root
);
1550 btrfs_commit_transaction(trans
, root
);
1553 mutex_unlock(&uuid_mutex
);
1554 up_write(&sb
->s_umount
);
1556 ret
= btrfs_relocate_sys_chunks(root
);
1560 mutex_unlock(&root
->fs_info
->volume_mutex
);
1563 close_bdev_exclusive(bdev
, 0);
1565 mutex_unlock(&uuid_mutex
);
1566 up_write(&sb
->s_umount
);
1571 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
1572 struct btrfs_device
*device
)
1575 struct btrfs_path
*path
;
1576 struct btrfs_root
*root
;
1577 struct btrfs_dev_item
*dev_item
;
1578 struct extent_buffer
*leaf
;
1579 struct btrfs_key key
;
1581 root
= device
->dev_root
->fs_info
->chunk_root
;
1583 path
= btrfs_alloc_path();
1587 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1588 key
.type
= BTRFS_DEV_ITEM_KEY
;
1589 key
.offset
= device
->devid
;
1591 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1600 leaf
= path
->nodes
[0];
1601 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1603 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1604 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1605 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1606 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1607 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1608 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
1609 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1610 btrfs_mark_buffer_dirty(leaf
);
1613 btrfs_free_path(path
);
1617 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1618 struct btrfs_device
*device
, u64 new_size
)
1620 struct btrfs_super_block
*super_copy
=
1621 &device
->dev_root
->fs_info
->super_copy
;
1622 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1623 u64 diff
= new_size
- device
->total_bytes
;
1625 if (!device
->writeable
)
1627 if (new_size
<= device
->total_bytes
)
1630 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
1631 device
->fs_devices
->total_rw_bytes
+= diff
;
1633 device
->total_bytes
= new_size
;
1634 device
->disk_total_bytes
= new_size
;
1635 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
1637 return btrfs_update_device(trans
, device
);
1640 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1641 struct btrfs_device
*device
, u64 new_size
)
1644 lock_chunks(device
->dev_root
);
1645 ret
= __btrfs_grow_device(trans
, device
, new_size
);
1646 unlock_chunks(device
->dev_root
);
1650 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
1651 struct btrfs_root
*root
,
1652 u64 chunk_tree
, u64 chunk_objectid
,
1656 struct btrfs_path
*path
;
1657 struct btrfs_key key
;
1659 root
= root
->fs_info
->chunk_root
;
1660 path
= btrfs_alloc_path();
1664 key
.objectid
= chunk_objectid
;
1665 key
.offset
= chunk_offset
;
1666 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1668 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1671 ret
= btrfs_del_item(trans
, root
, path
);
1674 btrfs_free_path(path
);
1678 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
1681 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1682 struct btrfs_disk_key
*disk_key
;
1683 struct btrfs_chunk
*chunk
;
1690 struct btrfs_key key
;
1692 array_size
= btrfs_super_sys_array_size(super_copy
);
1694 ptr
= super_copy
->sys_chunk_array
;
1697 while (cur
< array_size
) {
1698 disk_key
= (struct btrfs_disk_key
*)ptr
;
1699 btrfs_disk_key_to_cpu(&key
, disk_key
);
1701 len
= sizeof(*disk_key
);
1703 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1704 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
1705 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
1706 len
+= btrfs_chunk_item_size(num_stripes
);
1711 if (key
.objectid
== chunk_objectid
&&
1712 key
.offset
== chunk_offset
) {
1713 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
1715 btrfs_set_super_sys_array_size(super_copy
, array_size
);
1724 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
1725 u64 chunk_tree
, u64 chunk_objectid
,
1728 struct extent_map_tree
*em_tree
;
1729 struct btrfs_root
*extent_root
;
1730 struct btrfs_trans_handle
*trans
;
1731 struct extent_map
*em
;
1732 struct map_lookup
*map
;
1736 root
= root
->fs_info
->chunk_root
;
1737 extent_root
= root
->fs_info
->extent_root
;
1738 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
1740 ret
= btrfs_can_relocate(extent_root
, chunk_offset
);
1744 /* step one, relocate all the extents inside this chunk */
1745 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
1748 trans
= btrfs_start_transaction(root
, 1);
1754 * step two, delete the device extents and the
1755 * chunk tree entries
1757 read_lock(&em_tree
->lock
);
1758 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
1759 read_unlock(&em_tree
->lock
);
1761 BUG_ON(em
->start
> chunk_offset
||
1762 em
->start
+ em
->len
< chunk_offset
);
1763 map
= (struct map_lookup
*)em
->bdev
;
1765 for (i
= 0; i
< map
->num_stripes
; i
++) {
1766 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
1767 map
->stripes
[i
].physical
);
1770 if (map
->stripes
[i
].dev
) {
1771 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
1775 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
1780 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1781 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
1785 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
1788 write_lock(&em_tree
->lock
);
1789 remove_extent_mapping(em_tree
, em
);
1790 write_unlock(&em_tree
->lock
);
1795 /* once for the tree */
1796 free_extent_map(em
);
1798 free_extent_map(em
);
1800 unlock_chunks(root
);
1801 btrfs_end_transaction(trans
, root
);
1805 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
1807 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
1808 struct btrfs_path
*path
;
1809 struct extent_buffer
*leaf
;
1810 struct btrfs_chunk
*chunk
;
1811 struct btrfs_key key
;
1812 struct btrfs_key found_key
;
1813 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
1815 bool retried
= false;
1819 path
= btrfs_alloc_path();
1824 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1825 key
.offset
= (u64
)-1;
1826 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1829 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
1834 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
1841 leaf
= path
->nodes
[0];
1842 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1844 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
1845 struct btrfs_chunk
);
1846 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
1847 btrfs_release_path(chunk_root
, path
);
1849 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1850 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
1859 if (found_key
.offset
== 0)
1861 key
.offset
= found_key
.offset
- 1;
1864 if (failed
&& !retried
) {
1868 } else if (failed
&& retried
) {
1873 btrfs_free_path(path
);
1877 static u64
div_factor(u64 num
, int factor
)
1886 int btrfs_balance(struct btrfs_root
*dev_root
)
1889 struct list_head
*devices
= &dev_root
->fs_info
->fs_devices
->devices
;
1890 struct btrfs_device
*device
;
1893 struct btrfs_path
*path
;
1894 struct btrfs_key key
;
1895 struct btrfs_chunk
*chunk
;
1896 struct btrfs_root
*chunk_root
= dev_root
->fs_info
->chunk_root
;
1897 struct btrfs_trans_handle
*trans
;
1898 struct btrfs_key found_key
;
1900 if (dev_root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1903 mutex_lock(&dev_root
->fs_info
->volume_mutex
);
1904 dev_root
= dev_root
->fs_info
->dev_root
;
1906 /* step one make some room on all the devices */
1907 list_for_each_entry(device
, devices
, dev_list
) {
1908 old_size
= device
->total_bytes
;
1909 size_to_free
= div_factor(old_size
, 1);
1910 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
1911 if (!device
->writeable
||
1912 device
->total_bytes
- device
->bytes_used
> size_to_free
)
1915 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
1920 trans
= btrfs_start_transaction(dev_root
, 1);
1923 ret
= btrfs_grow_device(trans
, device
, old_size
);
1926 btrfs_end_transaction(trans
, dev_root
);
1929 /* step two, relocate all the chunks */
1930 path
= btrfs_alloc_path();
1933 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1934 key
.offset
= (u64
)-1;
1935 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1938 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
1943 * this shouldn't happen, it means the last relocate
1949 ret
= btrfs_previous_item(chunk_root
, path
, 0,
1950 BTRFS_CHUNK_ITEM_KEY
);
1954 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1956 if (found_key
.objectid
!= key
.objectid
)
1959 chunk
= btrfs_item_ptr(path
->nodes
[0],
1961 struct btrfs_chunk
);
1962 /* chunk zero is special */
1963 if (found_key
.offset
== 0)
1966 btrfs_release_path(chunk_root
, path
);
1967 ret
= btrfs_relocate_chunk(chunk_root
,
1968 chunk_root
->root_key
.objectid
,
1971 BUG_ON(ret
&& ret
!= -ENOSPC
);
1972 key
.offset
= found_key
.offset
- 1;
1976 btrfs_free_path(path
);
1977 mutex_unlock(&dev_root
->fs_info
->volume_mutex
);
1982 * shrinking a device means finding all of the device extents past
1983 * the new size, and then following the back refs to the chunks.
1984 * The chunk relocation code actually frees the device extent
1986 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
1988 struct btrfs_trans_handle
*trans
;
1989 struct btrfs_root
*root
= device
->dev_root
;
1990 struct btrfs_dev_extent
*dev_extent
= NULL
;
1991 struct btrfs_path
*path
;
1999 bool retried
= false;
2000 struct extent_buffer
*l
;
2001 struct btrfs_key key
;
2002 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
2003 u64 old_total
= btrfs_super_total_bytes(super_copy
);
2004 u64 old_size
= device
->total_bytes
;
2005 u64 diff
= device
->total_bytes
- new_size
;
2007 if (new_size
>= device
->total_bytes
)
2010 path
= btrfs_alloc_path();
2018 device
->total_bytes
= new_size
;
2019 if (device
->writeable
)
2020 device
->fs_devices
->total_rw_bytes
-= diff
;
2021 unlock_chunks(root
);
2024 key
.objectid
= device
->devid
;
2025 key
.offset
= (u64
)-1;
2026 key
.type
= BTRFS_DEV_EXTENT_KEY
;
2029 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2033 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
2038 btrfs_release_path(root
, path
);
2043 slot
= path
->slots
[0];
2044 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
2046 if (key
.objectid
!= device
->devid
) {
2047 btrfs_release_path(root
, path
);
2051 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
2052 length
= btrfs_dev_extent_length(l
, dev_extent
);
2054 if (key
.offset
+ length
<= new_size
) {
2055 btrfs_release_path(root
, path
);
2059 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
2060 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
2061 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
2062 btrfs_release_path(root
, path
);
2064 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
2066 if (ret
&& ret
!= -ENOSPC
)
2073 if (failed
&& !retried
) {
2077 } else if (failed
&& retried
) {
2081 device
->total_bytes
= old_size
;
2082 if (device
->writeable
)
2083 device
->fs_devices
->total_rw_bytes
+= diff
;
2084 unlock_chunks(root
);
2088 /* Shrinking succeeded, else we would be at "done". */
2089 trans
= btrfs_start_transaction(root
, 1);
2096 device
->disk_total_bytes
= new_size
;
2097 /* Now btrfs_update_device() will change the on-disk size. */
2098 ret
= btrfs_update_device(trans
, device
);
2100 unlock_chunks(root
);
2101 btrfs_end_transaction(trans
, root
);
2104 WARN_ON(diff
> old_total
);
2105 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
2106 unlock_chunks(root
);
2107 btrfs_end_transaction(trans
, root
);
2109 btrfs_free_path(path
);
2113 static int btrfs_add_system_chunk(struct btrfs_trans_handle
*trans
,
2114 struct btrfs_root
*root
,
2115 struct btrfs_key
*key
,
2116 struct btrfs_chunk
*chunk
, int item_size
)
2118 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
2119 struct btrfs_disk_key disk_key
;
2123 array_size
= btrfs_super_sys_array_size(super_copy
);
2124 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
2127 ptr
= super_copy
->sys_chunk_array
+ array_size
;
2128 btrfs_cpu_key_to_disk(&disk_key
, key
);
2129 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
2130 ptr
+= sizeof(disk_key
);
2131 memcpy(ptr
, chunk
, item_size
);
2132 item_size
+= sizeof(disk_key
);
2133 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
2137 static noinline u64
chunk_bytes_by_type(u64 type
, u64 calc_size
,
2138 int num_stripes
, int sub_stripes
)
2140 if (type
& (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_DUP
))
2142 else if (type
& BTRFS_BLOCK_GROUP_RAID10
)
2143 return calc_size
* (num_stripes
/ sub_stripes
);
2145 return calc_size
* num_stripes
;
2148 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
2149 struct btrfs_root
*extent_root
,
2150 struct map_lookup
**map_ret
,
2151 u64
*num_bytes
, u64
*stripe_size
,
2152 u64 start
, u64 type
)
2154 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
2155 struct btrfs_device
*device
= NULL
;
2156 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
2157 struct list_head
*cur
;
2158 struct map_lookup
*map
= NULL
;
2159 struct extent_map_tree
*em_tree
;
2160 struct extent_map
*em
;
2161 struct list_head private_devs
;
2162 int min_stripe_size
= 1 * 1024 * 1024;
2163 u64 calc_size
= 1024 * 1024 * 1024;
2164 u64 max_chunk_size
= calc_size
;
2169 int num_stripes
= 1;
2170 int min_stripes
= 1;
2171 int sub_stripes
= 0;
2175 int stripe_len
= 64 * 1024;
2177 if ((type
& BTRFS_BLOCK_GROUP_RAID1
) &&
2178 (type
& BTRFS_BLOCK_GROUP_DUP
)) {
2180 type
&= ~BTRFS_BLOCK_GROUP_DUP
;
2182 if (list_empty(&fs_devices
->alloc_list
))
2185 if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
2186 num_stripes
= fs_devices
->rw_devices
;
2189 if (type
& (BTRFS_BLOCK_GROUP_DUP
)) {
2193 if (type
& (BTRFS_BLOCK_GROUP_RAID1
)) {
2194 num_stripes
= min_t(u64
, 2, fs_devices
->rw_devices
);
2195 if (num_stripes
< 2)
2199 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
2200 num_stripes
= fs_devices
->rw_devices
;
2201 if (num_stripes
< 4)
2203 num_stripes
&= ~(u32
)1;
2208 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
2209 max_chunk_size
= 10 * calc_size
;
2210 min_stripe_size
= 64 * 1024 * 1024;
2211 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
2212 max_chunk_size
= 256 * 1024 * 1024;
2213 min_stripe_size
= 32 * 1024 * 1024;
2214 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2215 calc_size
= 8 * 1024 * 1024;
2216 max_chunk_size
= calc_size
* 2;
2217 min_stripe_size
= 1 * 1024 * 1024;
2220 /* we don't want a chunk larger than 10% of writeable space */
2221 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
2226 if (!map
|| map
->num_stripes
!= num_stripes
) {
2228 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
2231 map
->num_stripes
= num_stripes
;
2234 if (calc_size
* num_stripes
> max_chunk_size
) {
2235 calc_size
= max_chunk_size
;
2236 do_div(calc_size
, num_stripes
);
2237 do_div(calc_size
, stripe_len
);
2238 calc_size
*= stripe_len
;
2240 /* we don't want tiny stripes */
2241 calc_size
= max_t(u64
, min_stripe_size
, calc_size
);
2243 do_div(calc_size
, stripe_len
);
2244 calc_size
*= stripe_len
;
2246 cur
= fs_devices
->alloc_list
.next
;
2249 if (type
& BTRFS_BLOCK_GROUP_DUP
)
2250 min_free
= calc_size
* 2;
2252 min_free
= calc_size
;
2255 * we add 1MB because we never use the first 1MB of the device, unless
2256 * we've looped, then we are likely allocating the maximum amount of
2257 * space left already
2260 min_free
+= 1024 * 1024;
2262 INIT_LIST_HEAD(&private_devs
);
2263 while (index
< num_stripes
) {
2264 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
2265 BUG_ON(!device
->writeable
);
2266 if (device
->total_bytes
> device
->bytes_used
)
2267 avail
= device
->total_bytes
- device
->bytes_used
;
2272 if (device
->in_fs_metadata
&& avail
>= min_free
) {
2273 ret
= find_free_dev_extent(trans
, device
,
2274 min_free
, &dev_offset
,
2277 list_move_tail(&device
->dev_alloc_list
,
2279 map
->stripes
[index
].dev
= device
;
2280 map
->stripes
[index
].physical
= dev_offset
;
2282 if (type
& BTRFS_BLOCK_GROUP_DUP
) {
2283 map
->stripes
[index
].dev
= device
;
2284 map
->stripes
[index
].physical
=
2285 dev_offset
+ calc_size
;
2289 } else if (device
->in_fs_metadata
&& avail
> max_avail
)
2291 if (cur
== &fs_devices
->alloc_list
)
2294 list_splice(&private_devs
, &fs_devices
->alloc_list
);
2295 if (index
< num_stripes
) {
2296 if (index
>= min_stripes
) {
2297 num_stripes
= index
;
2298 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
2299 num_stripes
/= sub_stripes
;
2300 num_stripes
*= sub_stripes
;
2305 if (!looped
&& max_avail
> 0) {
2307 calc_size
= max_avail
;
2313 map
->sector_size
= extent_root
->sectorsize
;
2314 map
->stripe_len
= stripe_len
;
2315 map
->io_align
= stripe_len
;
2316 map
->io_width
= stripe_len
;
2318 map
->num_stripes
= num_stripes
;
2319 map
->sub_stripes
= sub_stripes
;
2322 *stripe_size
= calc_size
;
2323 *num_bytes
= chunk_bytes_by_type(type
, calc_size
,
2324 num_stripes
, sub_stripes
);
2326 em
= alloc_extent_map(GFP_NOFS
);
2331 em
->bdev
= (struct block_device
*)map
;
2333 em
->len
= *num_bytes
;
2334 em
->block_start
= 0;
2335 em
->block_len
= em
->len
;
2337 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
2338 write_lock(&em_tree
->lock
);
2339 ret
= add_extent_mapping(em_tree
, em
);
2340 write_unlock(&em_tree
->lock
);
2342 free_extent_map(em
);
2344 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
2345 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2350 while (index
< map
->num_stripes
) {
2351 device
= map
->stripes
[index
].dev
;
2352 dev_offset
= map
->stripes
[index
].physical
;
2354 ret
= btrfs_alloc_dev_extent(trans
, device
,
2355 info
->chunk_root
->root_key
.objectid
,
2356 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2357 start
, dev_offset
, calc_size
);
2365 static int __finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
2366 struct btrfs_root
*extent_root
,
2367 struct map_lookup
*map
, u64 chunk_offset
,
2368 u64 chunk_size
, u64 stripe_size
)
2371 struct btrfs_key key
;
2372 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
2373 struct btrfs_device
*device
;
2374 struct btrfs_chunk
*chunk
;
2375 struct btrfs_stripe
*stripe
;
2376 size_t item_size
= btrfs_chunk_item_size(map
->num_stripes
);
2380 chunk
= kzalloc(item_size
, GFP_NOFS
);
2385 while (index
< map
->num_stripes
) {
2386 device
= map
->stripes
[index
].dev
;
2387 device
->bytes_used
+= stripe_size
;
2388 ret
= btrfs_update_device(trans
, device
);
2394 stripe
= &chunk
->stripe
;
2395 while (index
< map
->num_stripes
) {
2396 device
= map
->stripes
[index
].dev
;
2397 dev_offset
= map
->stripes
[index
].physical
;
2399 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
2400 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
2401 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
2406 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
2407 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
2408 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
2409 btrfs_set_stack_chunk_type(chunk
, map
->type
);
2410 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
2411 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
2412 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
2413 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
2414 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
2416 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2417 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2418 key
.offset
= chunk_offset
;
2420 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
2423 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2424 ret
= btrfs_add_system_chunk(trans
, chunk_root
, &key
, chunk
,
2433 * Chunk allocation falls into two parts. The first part does works
2434 * that make the new allocated chunk useable, but not do any operation
2435 * that modifies the chunk tree. The second part does the works that
2436 * require modifying the chunk tree. This division is important for the
2437 * bootstrap process of adding storage to a seed btrfs.
2439 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
2440 struct btrfs_root
*extent_root
, u64 type
)
2445 struct map_lookup
*map
;
2446 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
2449 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2454 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
2455 &stripe_size
, chunk_offset
, type
);
2459 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
2460 chunk_size
, stripe_size
);
2465 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
2466 struct btrfs_root
*root
,
2467 struct btrfs_device
*device
)
2470 u64 sys_chunk_offset
;
2474 u64 sys_stripe_size
;
2476 struct map_lookup
*map
;
2477 struct map_lookup
*sys_map
;
2478 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2479 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
2482 ret
= find_next_chunk(fs_info
->chunk_root
,
2483 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, &chunk_offset
);
2486 alloc_profile
= BTRFS_BLOCK_GROUP_METADATA
|
2487 (fs_info
->metadata_alloc_profile
&
2488 fs_info
->avail_metadata_alloc_bits
);
2489 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
2491 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
2492 &stripe_size
, chunk_offset
, alloc_profile
);
2495 sys_chunk_offset
= chunk_offset
+ chunk_size
;
2497 alloc_profile
= BTRFS_BLOCK_GROUP_SYSTEM
|
2498 (fs_info
->system_alloc_profile
&
2499 fs_info
->avail_system_alloc_bits
);
2500 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
2502 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &sys_map
,
2503 &sys_chunk_size
, &sys_stripe_size
,
2504 sys_chunk_offset
, alloc_profile
);
2507 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
2511 * Modifying chunk tree needs allocating new blocks from both
2512 * system block group and metadata block group. So we only can
2513 * do operations require modifying the chunk tree after both
2514 * block groups were created.
2516 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
2517 chunk_size
, stripe_size
);
2520 ret
= __finish_chunk_alloc(trans
, extent_root
, sys_map
,
2521 sys_chunk_offset
, sys_chunk_size
,
2527 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
2529 struct extent_map
*em
;
2530 struct map_lookup
*map
;
2531 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
2535 read_lock(&map_tree
->map_tree
.lock
);
2536 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
2537 read_unlock(&map_tree
->map_tree
.lock
);
2541 map
= (struct map_lookup
*)em
->bdev
;
2542 for (i
= 0; i
< map
->num_stripes
; i
++) {
2543 if (!map
->stripes
[i
].dev
->writeable
) {
2548 free_extent_map(em
);
2552 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
2554 extent_map_tree_init(&tree
->map_tree
, GFP_NOFS
);
2557 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
2559 struct extent_map
*em
;
2562 write_lock(&tree
->map_tree
.lock
);
2563 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
2565 remove_extent_mapping(&tree
->map_tree
, em
);
2566 write_unlock(&tree
->map_tree
.lock
);
2571 free_extent_map(em
);
2572 /* once for the tree */
2573 free_extent_map(em
);
2577 int btrfs_num_copies(struct btrfs_mapping_tree
*map_tree
, u64 logical
, u64 len
)
2579 struct extent_map
*em
;
2580 struct map_lookup
*map
;
2581 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2584 read_lock(&em_tree
->lock
);
2585 em
= lookup_extent_mapping(em_tree
, logical
, len
);
2586 read_unlock(&em_tree
->lock
);
2589 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
2590 map
= (struct map_lookup
*)em
->bdev
;
2591 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
2592 ret
= map
->num_stripes
;
2593 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
2594 ret
= map
->sub_stripes
;
2597 free_extent_map(em
);
2601 static int find_live_mirror(struct map_lookup
*map
, int first
, int num
,
2605 if (map
->stripes
[optimal
].dev
->bdev
)
2607 for (i
= first
; i
< first
+ num
; i
++) {
2608 if (map
->stripes
[i
].dev
->bdev
)
2611 /* we couldn't find one that doesn't fail. Just return something
2612 * and the io error handling code will clean up eventually
2617 static int __btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
2618 u64 logical
, u64
*length
,
2619 struct btrfs_multi_bio
**multi_ret
,
2620 int mirror_num
, struct page
*unplug_page
)
2622 struct extent_map
*em
;
2623 struct map_lookup
*map
;
2624 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2628 int stripes_allocated
= 8;
2629 int stripes_required
= 1;
2634 struct btrfs_multi_bio
*multi
= NULL
;
2636 if (multi_ret
&& !(rw
& (1 << BIO_RW
)))
2637 stripes_allocated
= 1;
2640 multi
= kzalloc(btrfs_multi_bio_size(stripes_allocated
),
2645 atomic_set(&multi
->error
, 0);
2648 read_lock(&em_tree
->lock
);
2649 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
2650 read_unlock(&em_tree
->lock
);
2652 if (!em
&& unplug_page
) {
2658 printk(KERN_CRIT
"unable to find logical %llu len %llu\n",
2659 (unsigned long long)logical
,
2660 (unsigned long long)*length
);
2664 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
2665 map
= (struct map_lookup
*)em
->bdev
;
2666 offset
= logical
- em
->start
;
2668 if (mirror_num
> map
->num_stripes
)
2671 /* if our multi bio struct is too small, back off and try again */
2672 if (rw
& (1 << BIO_RW
)) {
2673 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
2674 BTRFS_BLOCK_GROUP_DUP
)) {
2675 stripes_required
= map
->num_stripes
;
2677 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
2678 stripes_required
= map
->sub_stripes
;
2682 if (multi_ret
&& (rw
& (1 << BIO_RW
)) &&
2683 stripes_allocated
< stripes_required
) {
2684 stripes_allocated
= map
->num_stripes
;
2685 free_extent_map(em
);
2691 * stripe_nr counts the total number of stripes we have to stride
2692 * to get to this block
2694 do_div(stripe_nr
, map
->stripe_len
);
2696 stripe_offset
= stripe_nr
* map
->stripe_len
;
2697 BUG_ON(offset
< stripe_offset
);
2699 /* stripe_offset is the offset of this block in its stripe*/
2700 stripe_offset
= offset
- stripe_offset
;
2702 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
2703 BTRFS_BLOCK_GROUP_RAID10
|
2704 BTRFS_BLOCK_GROUP_DUP
)) {
2705 /* we limit the length of each bio to what fits in a stripe */
2706 *length
= min_t(u64
, em
->len
- offset
,
2707 map
->stripe_len
- stripe_offset
);
2709 *length
= em
->len
- offset
;
2712 if (!multi_ret
&& !unplug_page
)
2717 if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
2718 if (unplug_page
|| (rw
& (1 << BIO_RW
)))
2719 num_stripes
= map
->num_stripes
;
2720 else if (mirror_num
)
2721 stripe_index
= mirror_num
- 1;
2723 stripe_index
= find_live_mirror(map
, 0,
2725 current
->pid
% map
->num_stripes
);
2728 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
2729 if (rw
& (1 << BIO_RW
))
2730 num_stripes
= map
->num_stripes
;
2731 else if (mirror_num
)
2732 stripe_index
= mirror_num
- 1;
2734 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
2735 int factor
= map
->num_stripes
/ map
->sub_stripes
;
2737 stripe_index
= do_div(stripe_nr
, factor
);
2738 stripe_index
*= map
->sub_stripes
;
2740 if (unplug_page
|| (rw
& (1 << BIO_RW
)))
2741 num_stripes
= map
->sub_stripes
;
2742 else if (mirror_num
)
2743 stripe_index
+= mirror_num
- 1;
2745 stripe_index
= find_live_mirror(map
, stripe_index
,
2746 map
->sub_stripes
, stripe_index
+
2747 current
->pid
% map
->sub_stripes
);
2751 * after this do_div call, stripe_nr is the number of stripes
2752 * on this device we have to walk to find the data, and
2753 * stripe_index is the number of our device in the stripe array
2755 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
2757 BUG_ON(stripe_index
>= map
->num_stripes
);
2759 for (i
= 0; i
< num_stripes
; i
++) {
2761 struct btrfs_device
*device
;
2762 struct backing_dev_info
*bdi
;
2764 device
= map
->stripes
[stripe_index
].dev
;
2766 bdi
= blk_get_backing_dev_info(device
->bdev
);
2767 if (bdi
->unplug_io_fn
)
2768 bdi
->unplug_io_fn(bdi
, unplug_page
);
2771 multi
->stripes
[i
].physical
=
2772 map
->stripes
[stripe_index
].physical
+
2773 stripe_offset
+ stripe_nr
* map
->stripe_len
;
2774 multi
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
2780 multi
->num_stripes
= num_stripes
;
2781 multi
->max_errors
= max_errors
;
2784 free_extent_map(em
);
2788 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
2789 u64 logical
, u64
*length
,
2790 struct btrfs_multi_bio
**multi_ret
, int mirror_num
)
2792 return __btrfs_map_block(map_tree
, rw
, logical
, length
, multi_ret
,
2796 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
2797 u64 chunk_start
, u64 physical
, u64 devid
,
2798 u64
**logical
, int *naddrs
, int *stripe_len
)
2800 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2801 struct extent_map
*em
;
2802 struct map_lookup
*map
;
2809 read_lock(&em_tree
->lock
);
2810 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
2811 read_unlock(&em_tree
->lock
);
2813 BUG_ON(!em
|| em
->start
!= chunk_start
);
2814 map
= (struct map_lookup
*)em
->bdev
;
2817 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
2818 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
2819 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
2820 do_div(length
, map
->num_stripes
);
2822 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
2825 for (i
= 0; i
< map
->num_stripes
; i
++) {
2826 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
2828 if (map
->stripes
[i
].physical
> physical
||
2829 map
->stripes
[i
].physical
+ length
<= physical
)
2832 stripe_nr
= physical
- map
->stripes
[i
].physical
;
2833 do_div(stripe_nr
, map
->stripe_len
);
2835 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
2836 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
2837 do_div(stripe_nr
, map
->sub_stripes
);
2838 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
2839 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
2841 bytenr
= chunk_start
+ stripe_nr
* map
->stripe_len
;
2842 WARN_ON(nr
>= map
->num_stripes
);
2843 for (j
= 0; j
< nr
; j
++) {
2844 if (buf
[j
] == bytenr
)
2848 WARN_ON(nr
>= map
->num_stripes
);
2855 *stripe_len
= map
->stripe_len
;
2857 free_extent_map(em
);
2861 int btrfs_unplug_page(struct btrfs_mapping_tree
*map_tree
,
2862 u64 logical
, struct page
*page
)
2864 u64 length
= PAGE_CACHE_SIZE
;
2865 return __btrfs_map_block(map_tree
, READ
, logical
, &length
,
2869 static void end_bio_multi_stripe(struct bio
*bio
, int err
)
2871 struct btrfs_multi_bio
*multi
= bio
->bi_private
;
2872 int is_orig_bio
= 0;
2875 atomic_inc(&multi
->error
);
2877 if (bio
== multi
->orig_bio
)
2880 if (atomic_dec_and_test(&multi
->stripes_pending
)) {
2883 bio
= multi
->orig_bio
;
2885 bio
->bi_private
= multi
->private;
2886 bio
->bi_end_io
= multi
->end_io
;
2887 /* only send an error to the higher layers if it is
2888 * beyond the tolerance of the multi-bio
2890 if (atomic_read(&multi
->error
) > multi
->max_errors
) {
2894 * this bio is actually up to date, we didn't
2895 * go over the max number of errors
2897 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
2902 bio_endio(bio
, err
);
2903 } else if (!is_orig_bio
) {
2908 struct async_sched
{
2911 struct btrfs_fs_info
*info
;
2912 struct btrfs_work work
;
2916 * see run_scheduled_bios for a description of why bios are collected for
2919 * This will add one bio to the pending list for a device and make sure
2920 * the work struct is scheduled.
2922 static noinline
int schedule_bio(struct btrfs_root
*root
,
2923 struct btrfs_device
*device
,
2924 int rw
, struct bio
*bio
)
2926 int should_queue
= 1;
2927 struct btrfs_pending_bios
*pending_bios
;
2929 /* don't bother with additional async steps for reads, right now */
2930 if (!(rw
& (1 << BIO_RW
))) {
2932 submit_bio(rw
, bio
);
2938 * nr_async_bios allows us to reliably return congestion to the
2939 * higher layers. Otherwise, the async bio makes it appear we have
2940 * made progress against dirty pages when we've really just put it
2941 * on a queue for later
2943 atomic_inc(&root
->fs_info
->nr_async_bios
);
2944 WARN_ON(bio
->bi_next
);
2945 bio
->bi_next
= NULL
;
2948 spin_lock(&device
->io_lock
);
2949 if (bio_rw_flagged(bio
, BIO_RW_SYNCIO
))
2950 pending_bios
= &device
->pending_sync_bios
;
2952 pending_bios
= &device
->pending_bios
;
2954 if (pending_bios
->tail
)
2955 pending_bios
->tail
->bi_next
= bio
;
2957 pending_bios
->tail
= bio
;
2958 if (!pending_bios
->head
)
2959 pending_bios
->head
= bio
;
2960 if (device
->running_pending
)
2963 spin_unlock(&device
->io_lock
);
2966 btrfs_queue_worker(&root
->fs_info
->submit_workers
,
2971 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
2972 int mirror_num
, int async_submit
)
2974 struct btrfs_mapping_tree
*map_tree
;
2975 struct btrfs_device
*dev
;
2976 struct bio
*first_bio
= bio
;
2977 u64 logical
= (u64
)bio
->bi_sector
<< 9;
2980 struct btrfs_multi_bio
*multi
= NULL
;
2985 length
= bio
->bi_size
;
2986 map_tree
= &root
->fs_info
->mapping_tree
;
2987 map_length
= length
;
2989 ret
= btrfs_map_block(map_tree
, rw
, logical
, &map_length
, &multi
,
2993 total_devs
= multi
->num_stripes
;
2994 if (map_length
< length
) {
2995 printk(KERN_CRIT
"mapping failed logical %llu bio len %llu "
2996 "len %llu\n", (unsigned long long)logical
,
2997 (unsigned long long)length
,
2998 (unsigned long long)map_length
);
3001 multi
->end_io
= first_bio
->bi_end_io
;
3002 multi
->private = first_bio
->bi_private
;
3003 multi
->orig_bio
= first_bio
;
3004 atomic_set(&multi
->stripes_pending
, multi
->num_stripes
);
3006 while (dev_nr
< total_devs
) {
3007 if (total_devs
> 1) {
3008 if (dev_nr
< total_devs
- 1) {
3009 bio
= bio_clone(first_bio
, GFP_NOFS
);
3014 bio
->bi_private
= multi
;
3015 bio
->bi_end_io
= end_bio_multi_stripe
;
3017 bio
->bi_sector
= multi
->stripes
[dev_nr
].physical
>> 9;
3018 dev
= multi
->stripes
[dev_nr
].dev
;
3019 BUG_ON(rw
== WRITE
&& !dev
->writeable
);
3020 if (dev
&& dev
->bdev
) {
3021 bio
->bi_bdev
= dev
->bdev
;
3023 schedule_bio(root
, dev
, rw
, bio
);
3025 submit_bio(rw
, bio
);
3027 bio
->bi_bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
3028 bio
->bi_sector
= logical
>> 9;
3029 bio_endio(bio
, -EIO
);
3033 if (total_devs
== 1)
3038 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
,
3041 struct btrfs_device
*device
;
3042 struct btrfs_fs_devices
*cur_devices
;
3044 cur_devices
= root
->fs_info
->fs_devices
;
3045 while (cur_devices
) {
3047 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
3048 device
= __find_device(&cur_devices
->devices
,
3053 cur_devices
= cur_devices
->seed
;
3058 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
3059 u64 devid
, u8
*dev_uuid
)
3061 struct btrfs_device
*device
;
3062 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
3064 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
3067 list_add(&device
->dev_list
,
3068 &fs_devices
->devices
);
3069 device
->barriers
= 1;
3070 device
->dev_root
= root
->fs_info
->dev_root
;
3071 device
->devid
= devid
;
3072 device
->work
.func
= pending_bios_fn
;
3073 device
->fs_devices
= fs_devices
;
3074 fs_devices
->num_devices
++;
3075 spin_lock_init(&device
->io_lock
);
3076 INIT_LIST_HEAD(&device
->dev_alloc_list
);
3077 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
3081 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
3082 struct extent_buffer
*leaf
,
3083 struct btrfs_chunk
*chunk
)
3085 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
3086 struct map_lookup
*map
;
3087 struct extent_map
*em
;
3091 u8 uuid
[BTRFS_UUID_SIZE
];
3096 logical
= key
->offset
;
3097 length
= btrfs_chunk_length(leaf
, chunk
);
3099 read_lock(&map_tree
->map_tree
.lock
);
3100 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
3101 read_unlock(&map_tree
->map_tree
.lock
);
3103 /* already mapped? */
3104 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
3105 free_extent_map(em
);
3108 free_extent_map(em
);
3111 em
= alloc_extent_map(GFP_NOFS
);
3114 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
3115 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
3117 free_extent_map(em
);
3121 em
->bdev
= (struct block_device
*)map
;
3122 em
->start
= logical
;
3124 em
->block_start
= 0;
3125 em
->block_len
= em
->len
;
3127 map
->num_stripes
= num_stripes
;
3128 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
3129 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
3130 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
3131 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
3132 map
->type
= btrfs_chunk_type(leaf
, chunk
);
3133 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
3134 for (i
= 0; i
< num_stripes
; i
++) {
3135 map
->stripes
[i
].physical
=
3136 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
3137 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
3138 read_extent_buffer(leaf
, uuid
, (unsigned long)
3139 btrfs_stripe_dev_uuid_nr(chunk
, i
),
3141 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
, uuid
,
3143 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
3145 free_extent_map(em
);
3148 if (!map
->stripes
[i
].dev
) {
3149 map
->stripes
[i
].dev
=
3150 add_missing_dev(root
, devid
, uuid
);
3151 if (!map
->stripes
[i
].dev
) {
3153 free_extent_map(em
);
3157 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
3160 write_lock(&map_tree
->map_tree
.lock
);
3161 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
3162 write_unlock(&map_tree
->map_tree
.lock
);
3164 free_extent_map(em
);
3169 static int fill_device_from_item(struct extent_buffer
*leaf
,
3170 struct btrfs_dev_item
*dev_item
,
3171 struct btrfs_device
*device
)
3175 device
->devid
= btrfs_device_id(leaf
, dev_item
);
3176 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
3177 device
->total_bytes
= device
->disk_total_bytes
;
3178 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
3179 device
->type
= btrfs_device_type(leaf
, dev_item
);
3180 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
3181 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
3182 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
3184 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
3185 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
3190 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
3192 struct btrfs_fs_devices
*fs_devices
;
3195 mutex_lock(&uuid_mutex
);
3197 fs_devices
= root
->fs_info
->fs_devices
->seed
;
3198 while (fs_devices
) {
3199 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
3203 fs_devices
= fs_devices
->seed
;
3206 fs_devices
= find_fsid(fsid
);
3212 fs_devices
= clone_fs_devices(fs_devices
);
3213 if (IS_ERR(fs_devices
)) {
3214 ret
= PTR_ERR(fs_devices
);
3218 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
3219 root
->fs_info
->bdev_holder
);
3223 if (!fs_devices
->seeding
) {
3224 __btrfs_close_devices(fs_devices
);
3225 free_fs_devices(fs_devices
);
3230 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
3231 root
->fs_info
->fs_devices
->seed
= fs_devices
;
3233 mutex_unlock(&uuid_mutex
);
3237 static int read_one_dev(struct btrfs_root
*root
,
3238 struct extent_buffer
*leaf
,
3239 struct btrfs_dev_item
*dev_item
)
3241 struct btrfs_device
*device
;
3244 u8 fs_uuid
[BTRFS_UUID_SIZE
];
3245 u8 dev_uuid
[BTRFS_UUID_SIZE
];
3247 devid
= btrfs_device_id(leaf
, dev_item
);
3248 read_extent_buffer(leaf
, dev_uuid
,
3249 (unsigned long)btrfs_device_uuid(dev_item
),
3251 read_extent_buffer(leaf
, fs_uuid
,
3252 (unsigned long)btrfs_device_fsid(dev_item
),
3255 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
3256 ret
= open_seed_devices(root
, fs_uuid
);
3257 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
3261 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
3262 if (!device
|| !device
->bdev
) {
3263 if (!btrfs_test_opt(root
, DEGRADED
))
3267 printk(KERN_WARNING
"warning devid %llu missing\n",
3268 (unsigned long long)devid
);
3269 device
= add_missing_dev(root
, devid
, dev_uuid
);
3275 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
3276 BUG_ON(device
->writeable
);
3277 if (device
->generation
!=
3278 btrfs_device_generation(leaf
, dev_item
))
3282 fill_device_from_item(leaf
, dev_item
, device
);
3283 device
->dev_root
= root
->fs_info
->dev_root
;
3284 device
->in_fs_metadata
= 1;
3285 if (device
->writeable
)
3286 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
3291 int btrfs_read_super_device(struct btrfs_root
*root
, struct extent_buffer
*buf
)
3293 struct btrfs_dev_item
*dev_item
;
3295 dev_item
= (struct btrfs_dev_item
*)offsetof(struct btrfs_super_block
,
3297 return read_one_dev(root
, buf
, dev_item
);
3300 int btrfs_read_sys_array(struct btrfs_root
*root
)
3302 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
3303 struct extent_buffer
*sb
;
3304 struct btrfs_disk_key
*disk_key
;
3305 struct btrfs_chunk
*chunk
;
3307 unsigned long sb_ptr
;
3313 struct btrfs_key key
;
3315 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
3316 BTRFS_SUPER_INFO_SIZE
);
3319 btrfs_set_buffer_uptodate(sb
);
3320 btrfs_set_buffer_lockdep_class(sb
, 0);
3322 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
3323 array_size
= btrfs_super_sys_array_size(super_copy
);
3325 ptr
= super_copy
->sys_chunk_array
;
3326 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
3329 while (cur
< array_size
) {
3330 disk_key
= (struct btrfs_disk_key
*)ptr
;
3331 btrfs_disk_key_to_cpu(&key
, disk_key
);
3333 len
= sizeof(*disk_key
); ptr
+= len
;
3337 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
3338 chunk
= (struct btrfs_chunk
*)sb_ptr
;
3339 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
3342 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
3343 len
= btrfs_chunk_item_size(num_stripes
);
3352 free_extent_buffer(sb
);
3356 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
3358 struct btrfs_path
*path
;
3359 struct extent_buffer
*leaf
;
3360 struct btrfs_key key
;
3361 struct btrfs_key found_key
;
3365 root
= root
->fs_info
->chunk_root
;
3367 path
= btrfs_alloc_path();
3371 /* first we search for all of the device items, and then we
3372 * read in all of the chunk items. This way we can create chunk
3373 * mappings that reference all of the devices that are afound
3375 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
3379 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3381 leaf
= path
->nodes
[0];
3382 slot
= path
->slots
[0];
3383 if (slot
>= btrfs_header_nritems(leaf
)) {
3384 ret
= btrfs_next_leaf(root
, path
);
3391 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3392 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
3393 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
3395 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
3396 struct btrfs_dev_item
*dev_item
;
3397 dev_item
= btrfs_item_ptr(leaf
, slot
,
3398 struct btrfs_dev_item
);
3399 ret
= read_one_dev(root
, leaf
, dev_item
);
3403 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
3404 struct btrfs_chunk
*chunk
;
3405 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
3406 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
3412 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
3414 btrfs_release_path(root
, path
);
3419 btrfs_free_path(path
);