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/slab.h>
21 #include <linux/buffer_head.h>
22 #include <linux/blkdev.h>
23 #include <linux/random.h>
24 #include <linux/iocontext.h>
25 #include <linux/capability.h>
26 #include <asm/div64.h>
29 #include "extent_map.h"
31 #include "transaction.h"
32 #include "print-tree.h"
34 #include "async-thread.h"
36 static int init_first_rw_device(struct btrfs_trans_handle
*trans
,
37 struct btrfs_root
*root
,
38 struct btrfs_device
*device
);
39 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
);
41 static DEFINE_MUTEX(uuid_mutex
);
42 static LIST_HEAD(fs_uuids
);
44 static void lock_chunks(struct btrfs_root
*root
)
46 mutex_lock(&root
->fs_info
->chunk_mutex
);
49 static void unlock_chunks(struct btrfs_root
*root
)
51 mutex_unlock(&root
->fs_info
->chunk_mutex
);
54 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
56 struct btrfs_device
*device
;
57 WARN_ON(fs_devices
->opened
);
58 while (!list_empty(&fs_devices
->devices
)) {
59 device
= list_entry(fs_devices
->devices
.next
,
60 struct btrfs_device
, dev_list
);
61 list_del(&device
->dev_list
);
68 int btrfs_cleanup_fs_uuids(void)
70 struct btrfs_fs_devices
*fs_devices
;
72 while (!list_empty(&fs_uuids
)) {
73 fs_devices
= list_entry(fs_uuids
.next
,
74 struct btrfs_fs_devices
, list
);
75 list_del(&fs_devices
->list
);
76 free_fs_devices(fs_devices
);
81 static noinline
struct btrfs_device
*__find_device(struct list_head
*head
,
84 struct btrfs_device
*dev
;
86 list_for_each_entry(dev
, head
, dev_list
) {
87 if (dev
->devid
== devid
&&
88 (!uuid
|| !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
))) {
95 static noinline
struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
97 struct btrfs_fs_devices
*fs_devices
;
99 list_for_each_entry(fs_devices
, &fs_uuids
, list
) {
100 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
106 static void requeue_list(struct btrfs_pending_bios
*pending_bios
,
107 struct bio
*head
, struct bio
*tail
)
110 struct bio
*old_head
;
112 old_head
= pending_bios
->head
;
113 pending_bios
->head
= head
;
114 if (pending_bios
->tail
)
115 tail
->bi_next
= old_head
;
117 pending_bios
->tail
= tail
;
121 * we try to collect pending bios for a device so we don't get a large
122 * number of procs sending bios down to the same device. This greatly
123 * improves the schedulers ability to collect and merge the bios.
125 * But, it also turns into a long list of bios to process and that is sure
126 * to eventually make the worker thread block. The solution here is to
127 * make some progress and then put this work struct back at the end of
128 * the list if the block device is congested. This way, multiple devices
129 * can make progress from a single worker thread.
131 static noinline
int run_scheduled_bios(struct btrfs_device
*device
)
134 struct backing_dev_info
*bdi
;
135 struct btrfs_fs_info
*fs_info
;
136 struct btrfs_pending_bios
*pending_bios
;
140 unsigned long num_run
;
141 unsigned long batch_run
= 0;
143 unsigned long last_waited
= 0;
145 struct blk_plug plug
;
148 * this function runs all the bios we've collected for
149 * a particular device. We don't want to wander off to
150 * another device without first sending all of these down.
151 * So, setup a plug here and finish it off before we return
153 blk_start_plug(&plug
);
155 bdi
= blk_get_backing_dev_info(device
->bdev
);
156 fs_info
= device
->dev_root
->fs_info
;
157 limit
= btrfs_async_submit_limit(fs_info
);
158 limit
= limit
* 2 / 3;
161 spin_lock(&device
->io_lock
);
166 /* take all the bios off the list at once and process them
167 * later on (without the lock held). But, remember the
168 * tail and other pointers so the bios can be properly reinserted
169 * into the list if we hit congestion
171 if (!force_reg
&& device
->pending_sync_bios
.head
) {
172 pending_bios
= &device
->pending_sync_bios
;
175 pending_bios
= &device
->pending_bios
;
179 pending
= pending_bios
->head
;
180 tail
= pending_bios
->tail
;
181 WARN_ON(pending
&& !tail
);
184 * if pending was null this time around, no bios need processing
185 * at all and we can stop. Otherwise it'll loop back up again
186 * and do an additional check so no bios are missed.
188 * device->running_pending is used to synchronize with the
191 if (device
->pending_sync_bios
.head
== NULL
&&
192 device
->pending_bios
.head
== NULL
) {
194 device
->running_pending
= 0;
197 device
->running_pending
= 1;
200 pending_bios
->head
= NULL
;
201 pending_bios
->tail
= NULL
;
203 spin_unlock(&device
->io_lock
);
208 /* we want to work on both lists, but do more bios on the
209 * sync list than the regular list
212 pending_bios
!= &device
->pending_sync_bios
&&
213 device
->pending_sync_bios
.head
) ||
214 (num_run
> 64 && pending_bios
== &device
->pending_sync_bios
&&
215 device
->pending_bios
.head
)) {
216 spin_lock(&device
->io_lock
);
217 requeue_list(pending_bios
, pending
, tail
);
222 pending
= pending
->bi_next
;
224 atomic_dec(&fs_info
->nr_async_bios
);
226 if (atomic_read(&fs_info
->nr_async_bios
) < limit
&&
227 waitqueue_active(&fs_info
->async_submit_wait
))
228 wake_up(&fs_info
->async_submit_wait
);
230 BUG_ON(atomic_read(&cur
->bi_cnt
) == 0);
232 submit_bio(cur
->bi_rw
, cur
);
239 * we made progress, there is more work to do and the bdi
240 * is now congested. Back off and let other work structs
243 if (pending
&& bdi_write_congested(bdi
) && batch_run
> 8 &&
244 fs_info
->fs_devices
->open_devices
> 1) {
245 struct io_context
*ioc
;
247 ioc
= current
->io_context
;
250 * the main goal here is that we don't want to
251 * block if we're going to be able to submit
252 * more requests without blocking.
254 * This code does two great things, it pokes into
255 * the elevator code from a filesystem _and_
256 * it makes assumptions about how batching works.
258 if (ioc
&& ioc
->nr_batch_requests
> 0 &&
259 time_before(jiffies
, ioc
->last_waited
+ HZ
/50UL) &&
261 ioc
->last_waited
== last_waited
)) {
263 * we want to go through our batch of
264 * requests and stop. So, we copy out
265 * the ioc->last_waited time and test
266 * against it before looping
268 last_waited
= ioc
->last_waited
;
273 spin_lock(&device
->io_lock
);
274 requeue_list(pending_bios
, pending
, tail
);
275 device
->running_pending
= 1;
277 spin_unlock(&device
->io_lock
);
278 btrfs_requeue_work(&device
->work
);
287 spin_lock(&device
->io_lock
);
288 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
290 spin_unlock(&device
->io_lock
);
293 blk_finish_plug(&plug
);
297 static void pending_bios_fn(struct btrfs_work
*work
)
299 struct btrfs_device
*device
;
301 device
= container_of(work
, struct btrfs_device
, work
);
302 run_scheduled_bios(device
);
305 static noinline
int device_list_add(const char *path
,
306 struct btrfs_super_block
*disk_super
,
307 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
309 struct btrfs_device
*device
;
310 struct btrfs_fs_devices
*fs_devices
;
311 u64 found_transid
= btrfs_super_generation(disk_super
);
314 fs_devices
= find_fsid(disk_super
->fsid
);
316 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
319 INIT_LIST_HEAD(&fs_devices
->devices
);
320 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
321 list_add(&fs_devices
->list
, &fs_uuids
);
322 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
323 fs_devices
->latest_devid
= devid
;
324 fs_devices
->latest_trans
= found_transid
;
325 mutex_init(&fs_devices
->device_list_mutex
);
328 device
= __find_device(&fs_devices
->devices
, devid
,
329 disk_super
->dev_item
.uuid
);
332 if (fs_devices
->opened
)
335 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
337 /* we can safely leave the fs_devices entry around */
340 device
->devid
= devid
;
341 device
->work
.func
= pending_bios_fn
;
342 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
344 spin_lock_init(&device
->io_lock
);
345 device
->name
= kstrdup(path
, GFP_NOFS
);
350 INIT_LIST_HEAD(&device
->dev_alloc_list
);
352 mutex_lock(&fs_devices
->device_list_mutex
);
353 list_add_rcu(&device
->dev_list
, &fs_devices
->devices
);
354 mutex_unlock(&fs_devices
->device_list_mutex
);
356 device
->fs_devices
= fs_devices
;
357 fs_devices
->num_devices
++;
358 } else if (!device
->name
|| strcmp(device
->name
, path
)) {
359 name
= kstrdup(path
, GFP_NOFS
);
364 if (device
->missing
) {
365 fs_devices
->missing_devices
--;
370 if (found_transid
> fs_devices
->latest_trans
) {
371 fs_devices
->latest_devid
= devid
;
372 fs_devices
->latest_trans
= found_transid
;
374 *fs_devices_ret
= fs_devices
;
378 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
380 struct btrfs_fs_devices
*fs_devices
;
381 struct btrfs_device
*device
;
382 struct btrfs_device
*orig_dev
;
384 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
386 return ERR_PTR(-ENOMEM
);
388 INIT_LIST_HEAD(&fs_devices
->devices
);
389 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
390 INIT_LIST_HEAD(&fs_devices
->list
);
391 mutex_init(&fs_devices
->device_list_mutex
);
392 fs_devices
->latest_devid
= orig
->latest_devid
;
393 fs_devices
->latest_trans
= orig
->latest_trans
;
394 memcpy(fs_devices
->fsid
, orig
->fsid
, sizeof(fs_devices
->fsid
));
396 /* We have held the volume lock, it is safe to get the devices. */
397 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
398 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
402 device
->name
= kstrdup(orig_dev
->name
, GFP_NOFS
);
408 device
->devid
= orig_dev
->devid
;
409 device
->work
.func
= pending_bios_fn
;
410 memcpy(device
->uuid
, orig_dev
->uuid
, sizeof(device
->uuid
));
411 spin_lock_init(&device
->io_lock
);
412 INIT_LIST_HEAD(&device
->dev_list
);
413 INIT_LIST_HEAD(&device
->dev_alloc_list
);
415 list_add(&device
->dev_list
, &fs_devices
->devices
);
416 device
->fs_devices
= fs_devices
;
417 fs_devices
->num_devices
++;
421 free_fs_devices(fs_devices
);
422 return ERR_PTR(-ENOMEM
);
425 int btrfs_close_extra_devices(struct btrfs_fs_devices
*fs_devices
)
427 struct btrfs_device
*device
, *next
;
429 mutex_lock(&uuid_mutex
);
431 /* This is the initialized path, it is safe to release the devices. */
432 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
433 if (device
->in_fs_metadata
)
437 blkdev_put(device
->bdev
, device
->mode
);
439 fs_devices
->open_devices
--;
441 if (device
->writeable
) {
442 list_del_init(&device
->dev_alloc_list
);
443 device
->writeable
= 0;
444 fs_devices
->rw_devices
--;
446 list_del_init(&device
->dev_list
);
447 fs_devices
->num_devices
--;
452 if (fs_devices
->seed
) {
453 fs_devices
= fs_devices
->seed
;
457 mutex_unlock(&uuid_mutex
);
461 static void __free_device(struct work_struct
*work
)
463 struct btrfs_device
*device
;
465 device
= container_of(work
, struct btrfs_device
, rcu_work
);
468 blkdev_put(device
->bdev
, device
->mode
);
474 static void free_device(struct rcu_head
*head
)
476 struct btrfs_device
*device
;
478 device
= container_of(head
, struct btrfs_device
, rcu
);
480 INIT_WORK(&device
->rcu_work
, __free_device
);
481 schedule_work(&device
->rcu_work
);
484 static int __btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
486 struct btrfs_device
*device
;
488 if (--fs_devices
->opened
> 0)
491 mutex_lock(&fs_devices
->device_list_mutex
);
492 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
493 struct btrfs_device
*new_device
;
496 fs_devices
->open_devices
--;
498 if (device
->writeable
) {
499 list_del_init(&device
->dev_alloc_list
);
500 fs_devices
->rw_devices
--;
503 if (device
->can_discard
)
504 fs_devices
->num_can_discard
--;
506 new_device
= kmalloc(sizeof(*new_device
), GFP_NOFS
);
508 memcpy(new_device
, device
, sizeof(*new_device
));
509 new_device
->name
= kstrdup(device
->name
, GFP_NOFS
);
510 BUG_ON(device
->name
&& !new_device
->name
);
511 new_device
->bdev
= NULL
;
512 new_device
->writeable
= 0;
513 new_device
->in_fs_metadata
= 0;
514 new_device
->can_discard
= 0;
515 list_replace_rcu(&device
->dev_list
, &new_device
->dev_list
);
517 call_rcu(&device
->rcu
, free_device
);
519 mutex_unlock(&fs_devices
->device_list_mutex
);
521 WARN_ON(fs_devices
->open_devices
);
522 WARN_ON(fs_devices
->rw_devices
);
523 fs_devices
->opened
= 0;
524 fs_devices
->seeding
= 0;
529 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
531 struct btrfs_fs_devices
*seed_devices
= NULL
;
534 mutex_lock(&uuid_mutex
);
535 ret
= __btrfs_close_devices(fs_devices
);
536 if (!fs_devices
->opened
) {
537 seed_devices
= fs_devices
->seed
;
538 fs_devices
->seed
= NULL
;
540 mutex_unlock(&uuid_mutex
);
542 while (seed_devices
) {
543 fs_devices
= seed_devices
;
544 seed_devices
= fs_devices
->seed
;
545 __btrfs_close_devices(fs_devices
);
546 free_fs_devices(fs_devices
);
551 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
552 fmode_t flags
, void *holder
)
554 struct request_queue
*q
;
555 struct block_device
*bdev
;
556 struct list_head
*head
= &fs_devices
->devices
;
557 struct btrfs_device
*device
;
558 struct block_device
*latest_bdev
= NULL
;
559 struct buffer_head
*bh
;
560 struct btrfs_super_block
*disk_super
;
561 u64 latest_devid
= 0;
562 u64 latest_transid
= 0;
569 list_for_each_entry(device
, head
, dev_list
) {
575 bdev
= blkdev_get_by_path(device
->name
, flags
, holder
);
577 printk(KERN_INFO
"open %s failed\n", device
->name
);
580 set_blocksize(bdev
, 4096);
582 bh
= btrfs_read_dev_super(bdev
);
588 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
589 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
590 if (devid
!= device
->devid
)
593 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
597 device
->generation
= btrfs_super_generation(disk_super
);
598 if (!latest_transid
|| device
->generation
> latest_transid
) {
599 latest_devid
= devid
;
600 latest_transid
= device
->generation
;
604 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
605 device
->writeable
= 0;
607 device
->writeable
= !bdev_read_only(bdev
);
611 q
= bdev_get_queue(bdev
);
612 if (blk_queue_discard(q
)) {
613 device
->can_discard
= 1;
614 fs_devices
->num_can_discard
++;
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
);
636 blkdev_put(bdev
, flags
);
640 if (fs_devices
->open_devices
== 0) {
644 fs_devices
->seeding
= seeding
;
645 fs_devices
->opened
= 1;
646 fs_devices
->latest_bdev
= latest_bdev
;
647 fs_devices
->latest_devid
= latest_devid
;
648 fs_devices
->latest_trans
= latest_transid
;
649 fs_devices
->total_rw_bytes
= 0;
654 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
655 fmode_t flags
, void *holder
)
659 mutex_lock(&uuid_mutex
);
660 if (fs_devices
->opened
) {
661 fs_devices
->opened
++;
664 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
666 mutex_unlock(&uuid_mutex
);
670 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
671 struct btrfs_fs_devices
**fs_devices_ret
)
673 struct btrfs_super_block
*disk_super
;
674 struct block_device
*bdev
;
675 struct buffer_head
*bh
;
680 mutex_lock(&uuid_mutex
);
683 bdev
= blkdev_get_by_path(path
, flags
, holder
);
690 ret
= set_blocksize(bdev
, 4096);
693 bh
= btrfs_read_dev_super(bdev
);
698 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
699 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
700 transid
= btrfs_super_generation(disk_super
);
701 if (disk_super
->label
[0])
702 printk(KERN_INFO
"device label %s ", disk_super
->label
);
704 printk(KERN_INFO
"device fsid %pU ", disk_super
->fsid
);
705 printk(KERN_CONT
"devid %llu transid %llu %s\n",
706 (unsigned long long)devid
, (unsigned long long)transid
, path
);
707 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
711 blkdev_put(bdev
, flags
);
713 mutex_unlock(&uuid_mutex
);
717 /* helper to account the used device space in the range */
718 int btrfs_account_dev_extents_size(struct btrfs_device
*device
, u64 start
,
719 u64 end
, u64
*length
)
721 struct btrfs_key key
;
722 struct btrfs_root
*root
= device
->dev_root
;
723 struct btrfs_dev_extent
*dev_extent
;
724 struct btrfs_path
*path
;
728 struct extent_buffer
*l
;
732 if (start
>= device
->total_bytes
)
735 path
= btrfs_alloc_path();
740 key
.objectid
= device
->devid
;
742 key
.type
= BTRFS_DEV_EXTENT_KEY
;
744 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
748 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
755 slot
= path
->slots
[0];
756 if (slot
>= btrfs_header_nritems(l
)) {
757 ret
= btrfs_next_leaf(root
, path
);
765 btrfs_item_key_to_cpu(l
, &key
, slot
);
767 if (key
.objectid
< device
->devid
)
770 if (key
.objectid
> device
->devid
)
773 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
776 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
777 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
779 if (key
.offset
<= start
&& extent_end
> end
) {
780 *length
= end
- start
+ 1;
782 } else if (key
.offset
<= start
&& extent_end
> start
)
783 *length
+= extent_end
- start
;
784 else if (key
.offset
> start
&& extent_end
<= end
)
785 *length
+= extent_end
- key
.offset
;
786 else if (key
.offset
> start
&& key
.offset
<= end
) {
787 *length
+= end
- key
.offset
+ 1;
789 } else if (key
.offset
> end
)
797 btrfs_free_path(path
);
802 * find_free_dev_extent - find free space in the specified device
803 * @trans: transaction handler
804 * @device: the device which we search the free space in
805 * @num_bytes: the size of the free space that we need
806 * @start: store the start of the free space.
807 * @len: the size of the free space. that we find, or the size of the max
808 * free space if we don't find suitable free space
810 * this uses a pretty simple search, the expectation is that it is
811 * called very infrequently and that a given device has a small number
814 * @start is used to store the start of the free space if we find. But if we
815 * don't find suitable free space, it will be used to store the start position
816 * of the max free space.
818 * @len is used to store the size of the free space that we find.
819 * But if we don't find suitable free space, it is used to store the size of
820 * the max free space.
822 int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
823 struct btrfs_device
*device
, u64 num_bytes
,
824 u64
*start
, u64
*len
)
826 struct btrfs_key key
;
827 struct btrfs_root
*root
= device
->dev_root
;
828 struct btrfs_dev_extent
*dev_extent
;
829 struct btrfs_path
*path
;
835 u64 search_end
= device
->total_bytes
;
838 struct extent_buffer
*l
;
840 /* FIXME use last free of some kind */
842 /* we don't want to overwrite the superblock on the drive,
843 * so we make sure to start at an offset of at least 1MB
845 search_start
= max(root
->fs_info
->alloc_start
, 1024ull * 1024);
847 max_hole_start
= search_start
;
850 if (search_start
>= search_end
) {
855 path
= btrfs_alloc_path();
862 key
.objectid
= device
->devid
;
863 key
.offset
= search_start
;
864 key
.type
= BTRFS_DEV_EXTENT_KEY
;
866 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
870 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
877 slot
= path
->slots
[0];
878 if (slot
>= btrfs_header_nritems(l
)) {
879 ret
= btrfs_next_leaf(root
, path
);
887 btrfs_item_key_to_cpu(l
, &key
, slot
);
889 if (key
.objectid
< device
->devid
)
892 if (key
.objectid
> device
->devid
)
895 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
898 if (key
.offset
> search_start
) {
899 hole_size
= key
.offset
- search_start
;
901 if (hole_size
> max_hole_size
) {
902 max_hole_start
= search_start
;
903 max_hole_size
= hole_size
;
907 * If this free space is greater than which we need,
908 * it must be the max free space that we have found
909 * until now, so max_hole_start must point to the start
910 * of this free space and the length of this free space
911 * is stored in max_hole_size. Thus, we return
912 * max_hole_start and max_hole_size and go back to the
915 if (hole_size
>= num_bytes
) {
921 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
922 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
924 if (extent_end
> search_start
)
925 search_start
= extent_end
;
931 hole_size
= search_end
- search_start
;
932 if (hole_size
> max_hole_size
) {
933 max_hole_start
= search_start
;
934 max_hole_size
= hole_size
;
938 if (hole_size
< num_bytes
)
944 btrfs_free_path(path
);
946 *start
= max_hole_start
;
948 *len
= max_hole_size
;
952 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
953 struct btrfs_device
*device
,
957 struct btrfs_path
*path
;
958 struct btrfs_root
*root
= device
->dev_root
;
959 struct btrfs_key key
;
960 struct btrfs_key found_key
;
961 struct extent_buffer
*leaf
= NULL
;
962 struct btrfs_dev_extent
*extent
= NULL
;
964 path
= btrfs_alloc_path();
968 key
.objectid
= device
->devid
;
970 key
.type
= BTRFS_DEV_EXTENT_KEY
;
972 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
974 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
975 BTRFS_DEV_EXTENT_KEY
);
978 leaf
= path
->nodes
[0];
979 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
980 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
981 struct btrfs_dev_extent
);
982 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
983 btrfs_dev_extent_length(leaf
, extent
) < start
);
984 } else if (ret
== 0) {
985 leaf
= path
->nodes
[0];
986 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
987 struct btrfs_dev_extent
);
991 if (device
->bytes_used
> 0)
992 device
->bytes_used
-= btrfs_dev_extent_length(leaf
, extent
);
993 ret
= btrfs_del_item(trans
, root
, path
);
996 btrfs_free_path(path
);
1000 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
1001 struct btrfs_device
*device
,
1002 u64 chunk_tree
, u64 chunk_objectid
,
1003 u64 chunk_offset
, u64 start
, u64 num_bytes
)
1006 struct btrfs_path
*path
;
1007 struct btrfs_root
*root
= device
->dev_root
;
1008 struct btrfs_dev_extent
*extent
;
1009 struct extent_buffer
*leaf
;
1010 struct btrfs_key key
;
1012 WARN_ON(!device
->in_fs_metadata
);
1013 path
= btrfs_alloc_path();
1017 key
.objectid
= device
->devid
;
1019 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1020 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1024 leaf
= path
->nodes
[0];
1025 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1026 struct btrfs_dev_extent
);
1027 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
1028 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
1029 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1031 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1032 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
1035 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1036 btrfs_mark_buffer_dirty(leaf
);
1037 btrfs_free_path(path
);
1041 static noinline
int find_next_chunk(struct btrfs_root
*root
,
1042 u64 objectid
, u64
*offset
)
1044 struct btrfs_path
*path
;
1046 struct btrfs_key key
;
1047 struct btrfs_chunk
*chunk
;
1048 struct btrfs_key found_key
;
1050 path
= btrfs_alloc_path();
1053 key
.objectid
= objectid
;
1054 key
.offset
= (u64
)-1;
1055 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1057 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1063 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
1067 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1069 if (found_key
.objectid
!= objectid
)
1072 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1073 struct btrfs_chunk
);
1074 *offset
= found_key
.offset
+
1075 btrfs_chunk_length(path
->nodes
[0], chunk
);
1080 btrfs_free_path(path
);
1084 static noinline
int find_next_devid(struct btrfs_root
*root
, u64
*objectid
)
1087 struct btrfs_key key
;
1088 struct btrfs_key found_key
;
1089 struct btrfs_path
*path
;
1091 root
= root
->fs_info
->chunk_root
;
1093 path
= btrfs_alloc_path();
1097 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1098 key
.type
= BTRFS_DEV_ITEM_KEY
;
1099 key
.offset
= (u64
)-1;
1101 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1107 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
1108 BTRFS_DEV_ITEM_KEY
);
1112 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1114 *objectid
= found_key
.offset
+ 1;
1118 btrfs_free_path(path
);
1123 * the device information is stored in the chunk root
1124 * the btrfs_device struct should be fully filled in
1126 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
1127 struct btrfs_root
*root
,
1128 struct btrfs_device
*device
)
1131 struct btrfs_path
*path
;
1132 struct btrfs_dev_item
*dev_item
;
1133 struct extent_buffer
*leaf
;
1134 struct btrfs_key key
;
1137 root
= root
->fs_info
->chunk_root
;
1139 path
= btrfs_alloc_path();
1143 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1144 key
.type
= BTRFS_DEV_ITEM_KEY
;
1145 key
.offset
= device
->devid
;
1147 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1152 leaf
= path
->nodes
[0];
1153 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1155 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1156 btrfs_set_device_generation(leaf
, dev_item
, 0);
1157 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1158 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1159 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1160 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1161 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1162 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1163 btrfs_set_device_group(leaf
, dev_item
, 0);
1164 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1165 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1166 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1168 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1169 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1170 ptr
= (unsigned long)btrfs_device_fsid(dev_item
);
1171 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1172 btrfs_mark_buffer_dirty(leaf
);
1176 btrfs_free_path(path
);
1180 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1181 struct btrfs_device
*device
)
1184 struct btrfs_path
*path
;
1185 struct btrfs_key key
;
1186 struct btrfs_trans_handle
*trans
;
1188 root
= root
->fs_info
->chunk_root
;
1190 path
= btrfs_alloc_path();
1194 trans
= btrfs_start_transaction(root
, 0);
1195 if (IS_ERR(trans
)) {
1196 btrfs_free_path(path
);
1197 return PTR_ERR(trans
);
1199 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1200 key
.type
= BTRFS_DEV_ITEM_KEY
;
1201 key
.offset
= device
->devid
;
1204 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1213 ret
= btrfs_del_item(trans
, root
, path
);
1217 btrfs_free_path(path
);
1218 unlock_chunks(root
);
1219 btrfs_commit_transaction(trans
, root
);
1223 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1225 struct btrfs_device
*device
;
1226 struct btrfs_device
*next_device
;
1227 struct block_device
*bdev
;
1228 struct buffer_head
*bh
= NULL
;
1229 struct btrfs_super_block
*disk_super
;
1230 struct btrfs_fs_devices
*cur_devices
;
1236 bool clear_super
= false;
1238 mutex_lock(&uuid_mutex
);
1239 mutex_lock(&root
->fs_info
->volume_mutex
);
1241 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1242 root
->fs_info
->avail_system_alloc_bits
|
1243 root
->fs_info
->avail_metadata_alloc_bits
;
1245 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) &&
1246 root
->fs_info
->fs_devices
->num_devices
<= 4) {
1247 printk(KERN_ERR
"btrfs: unable to go below four devices "
1253 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) &&
1254 root
->fs_info
->fs_devices
->num_devices
<= 2) {
1255 printk(KERN_ERR
"btrfs: unable to go below two "
1256 "devices on raid1\n");
1261 if (strcmp(device_path
, "missing") == 0) {
1262 struct list_head
*devices
;
1263 struct btrfs_device
*tmp
;
1266 devices
= &root
->fs_info
->fs_devices
->devices
;
1268 * It is safe to read the devices since the volume_mutex
1271 list_for_each_entry(tmp
, devices
, dev_list
) {
1272 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1281 printk(KERN_ERR
"btrfs: no missing devices found to "
1286 bdev
= blkdev_get_by_path(device_path
, FMODE_READ
| FMODE_EXCL
,
1287 root
->fs_info
->bdev_holder
);
1289 ret
= PTR_ERR(bdev
);
1293 set_blocksize(bdev
, 4096);
1294 bh
= btrfs_read_dev_super(bdev
);
1299 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1300 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1301 dev_uuid
= disk_super
->dev_item
.uuid
;
1302 device
= btrfs_find_device(root
, devid
, dev_uuid
,
1310 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1311 printk(KERN_ERR
"btrfs: unable to remove the only writeable "
1317 if (device
->writeable
) {
1319 list_del_init(&device
->dev_alloc_list
);
1320 unlock_chunks(root
);
1321 root
->fs_info
->fs_devices
->rw_devices
--;
1325 ret
= btrfs_shrink_device(device
, 0);
1329 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1333 device
->in_fs_metadata
= 0;
1334 btrfs_scrub_cancel_dev(root
, device
);
1337 * the device list mutex makes sure that we don't change
1338 * the device list while someone else is writing out all
1339 * the device supers.
1342 cur_devices
= device
->fs_devices
;
1343 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1344 list_del_rcu(&device
->dev_list
);
1346 device
->fs_devices
->num_devices
--;
1348 if (device
->missing
)
1349 root
->fs_info
->fs_devices
->missing_devices
--;
1351 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1352 struct btrfs_device
, dev_list
);
1353 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1354 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1355 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1356 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1359 device
->fs_devices
->open_devices
--;
1361 call_rcu(&device
->rcu
, free_device
);
1362 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1364 num_devices
= btrfs_super_num_devices(&root
->fs_info
->super_copy
) - 1;
1365 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
, num_devices
);
1367 if (cur_devices
->open_devices
== 0) {
1368 struct btrfs_fs_devices
*fs_devices
;
1369 fs_devices
= root
->fs_info
->fs_devices
;
1370 while (fs_devices
) {
1371 if (fs_devices
->seed
== cur_devices
)
1373 fs_devices
= fs_devices
->seed
;
1375 fs_devices
->seed
= cur_devices
->seed
;
1376 cur_devices
->seed
= NULL
;
1378 __btrfs_close_devices(cur_devices
);
1379 unlock_chunks(root
);
1380 free_fs_devices(cur_devices
);
1384 * at this point, the device is zero sized. We want to
1385 * remove it from the devices list and zero out the old super
1388 /* make sure this device isn't detected as part of
1391 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1392 set_buffer_dirty(bh
);
1393 sync_dirty_buffer(bh
);
1402 blkdev_put(bdev
, FMODE_READ
| FMODE_EXCL
);
1404 mutex_unlock(&root
->fs_info
->volume_mutex
);
1405 mutex_unlock(&uuid_mutex
);
1408 if (device
->writeable
) {
1410 list_add(&device
->dev_alloc_list
,
1411 &root
->fs_info
->fs_devices
->alloc_list
);
1412 unlock_chunks(root
);
1413 root
->fs_info
->fs_devices
->rw_devices
++;
1419 * does all the dirty work required for changing file system's UUID.
1421 static int btrfs_prepare_sprout(struct btrfs_trans_handle
*trans
,
1422 struct btrfs_root
*root
)
1424 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1425 struct btrfs_fs_devices
*old_devices
;
1426 struct btrfs_fs_devices
*seed_devices
;
1427 struct btrfs_super_block
*disk_super
= &root
->fs_info
->super_copy
;
1428 struct btrfs_device
*device
;
1431 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1432 if (!fs_devices
->seeding
)
1435 seed_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
1439 old_devices
= clone_fs_devices(fs_devices
);
1440 if (IS_ERR(old_devices
)) {
1441 kfree(seed_devices
);
1442 return PTR_ERR(old_devices
);
1445 list_add(&old_devices
->list
, &fs_uuids
);
1447 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1448 seed_devices
->opened
= 1;
1449 INIT_LIST_HEAD(&seed_devices
->devices
);
1450 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1451 mutex_init(&seed_devices
->device_list_mutex
);
1453 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1454 list_splice_init_rcu(&fs_devices
->devices
, &seed_devices
->devices
,
1456 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1458 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1459 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1460 device
->fs_devices
= seed_devices
;
1463 fs_devices
->seeding
= 0;
1464 fs_devices
->num_devices
= 0;
1465 fs_devices
->open_devices
= 0;
1466 fs_devices
->seed
= seed_devices
;
1468 generate_random_uuid(fs_devices
->fsid
);
1469 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1470 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1471 super_flags
= btrfs_super_flags(disk_super
) &
1472 ~BTRFS_SUPER_FLAG_SEEDING
;
1473 btrfs_set_super_flags(disk_super
, super_flags
);
1479 * strore the expected generation for seed devices in device items.
1481 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
1482 struct btrfs_root
*root
)
1484 struct btrfs_path
*path
;
1485 struct extent_buffer
*leaf
;
1486 struct btrfs_dev_item
*dev_item
;
1487 struct btrfs_device
*device
;
1488 struct btrfs_key key
;
1489 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1490 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1494 path
= btrfs_alloc_path();
1498 root
= root
->fs_info
->chunk_root
;
1499 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1501 key
.type
= BTRFS_DEV_ITEM_KEY
;
1504 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1508 leaf
= path
->nodes
[0];
1510 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1511 ret
= btrfs_next_leaf(root
, path
);
1516 leaf
= path
->nodes
[0];
1517 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1518 btrfs_release_path(path
);
1522 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1523 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
1524 key
.type
!= BTRFS_DEV_ITEM_KEY
)
1527 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1528 struct btrfs_dev_item
);
1529 devid
= btrfs_device_id(leaf
, dev_item
);
1530 read_extent_buffer(leaf
, dev_uuid
,
1531 (unsigned long)btrfs_device_uuid(dev_item
),
1533 read_extent_buffer(leaf
, fs_uuid
,
1534 (unsigned long)btrfs_device_fsid(dev_item
),
1536 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
1539 if (device
->fs_devices
->seeding
) {
1540 btrfs_set_device_generation(leaf
, dev_item
,
1541 device
->generation
);
1542 btrfs_mark_buffer_dirty(leaf
);
1550 btrfs_free_path(path
);
1554 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
1556 struct request_queue
*q
;
1557 struct btrfs_trans_handle
*trans
;
1558 struct btrfs_device
*device
;
1559 struct block_device
*bdev
;
1560 struct list_head
*devices
;
1561 struct super_block
*sb
= root
->fs_info
->sb
;
1563 int seeding_dev
= 0;
1566 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
1569 bdev
= blkdev_get_by_path(device_path
, FMODE_EXCL
,
1570 root
->fs_info
->bdev_holder
);
1572 return PTR_ERR(bdev
);
1574 if (root
->fs_info
->fs_devices
->seeding
) {
1576 down_write(&sb
->s_umount
);
1577 mutex_lock(&uuid_mutex
);
1580 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
1581 mutex_lock(&root
->fs_info
->volume_mutex
);
1583 devices
= &root
->fs_info
->fs_devices
->devices
;
1585 * we have the volume lock, so we don't need the extra
1586 * device list mutex while reading the list here.
1588 list_for_each_entry(device
, devices
, dev_list
) {
1589 if (device
->bdev
== bdev
) {
1595 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1597 /* we can safely leave the fs_devices entry around */
1602 device
->name
= kstrdup(device_path
, GFP_NOFS
);
1603 if (!device
->name
) {
1609 ret
= find_next_devid(root
, &device
->devid
);
1611 kfree(device
->name
);
1616 trans
= btrfs_start_transaction(root
, 0);
1617 if (IS_ERR(trans
)) {
1618 kfree(device
->name
);
1620 ret
= PTR_ERR(trans
);
1626 q
= bdev_get_queue(bdev
);
1627 if (blk_queue_discard(q
))
1628 device
->can_discard
= 1;
1629 device
->writeable
= 1;
1630 device
->work
.func
= pending_bios_fn
;
1631 generate_random_uuid(device
->uuid
);
1632 spin_lock_init(&device
->io_lock
);
1633 device
->generation
= trans
->transid
;
1634 device
->io_width
= root
->sectorsize
;
1635 device
->io_align
= root
->sectorsize
;
1636 device
->sector_size
= root
->sectorsize
;
1637 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1638 device
->disk_total_bytes
= device
->total_bytes
;
1639 device
->dev_root
= root
->fs_info
->dev_root
;
1640 device
->bdev
= bdev
;
1641 device
->in_fs_metadata
= 1;
1642 device
->mode
= FMODE_EXCL
;
1643 set_blocksize(device
->bdev
, 4096);
1646 sb
->s_flags
&= ~MS_RDONLY
;
1647 ret
= btrfs_prepare_sprout(trans
, root
);
1651 device
->fs_devices
= root
->fs_info
->fs_devices
;
1654 * we don't want write_supers to jump in here with our device
1657 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1658 list_add_rcu(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
1659 list_add(&device
->dev_alloc_list
,
1660 &root
->fs_info
->fs_devices
->alloc_list
);
1661 root
->fs_info
->fs_devices
->num_devices
++;
1662 root
->fs_info
->fs_devices
->open_devices
++;
1663 root
->fs_info
->fs_devices
->rw_devices
++;
1664 if (device
->can_discard
)
1665 root
->fs_info
->fs_devices
->num_can_discard
++;
1666 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
1668 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
1669 root
->fs_info
->fs_devices
->rotating
= 1;
1671 total_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
1672 btrfs_set_super_total_bytes(&root
->fs_info
->super_copy
,
1673 total_bytes
+ device
->total_bytes
);
1675 total_bytes
= btrfs_super_num_devices(&root
->fs_info
->super_copy
);
1676 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
,
1678 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1681 ret
= init_first_rw_device(trans
, root
, device
);
1683 ret
= btrfs_finish_sprout(trans
, root
);
1686 ret
= btrfs_add_device(trans
, root
, device
);
1690 * we've got more storage, clear any full flags on the space
1693 btrfs_clear_space_info_full(root
->fs_info
);
1695 unlock_chunks(root
);
1696 btrfs_commit_transaction(trans
, root
);
1699 mutex_unlock(&uuid_mutex
);
1700 up_write(&sb
->s_umount
);
1702 ret
= btrfs_relocate_sys_chunks(root
);
1706 mutex_unlock(&root
->fs_info
->volume_mutex
);
1709 blkdev_put(bdev
, FMODE_EXCL
);
1711 mutex_unlock(&uuid_mutex
);
1712 up_write(&sb
->s_umount
);
1717 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
1718 struct btrfs_device
*device
)
1721 struct btrfs_path
*path
;
1722 struct btrfs_root
*root
;
1723 struct btrfs_dev_item
*dev_item
;
1724 struct extent_buffer
*leaf
;
1725 struct btrfs_key key
;
1727 root
= device
->dev_root
->fs_info
->chunk_root
;
1729 path
= btrfs_alloc_path();
1733 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1734 key
.type
= BTRFS_DEV_ITEM_KEY
;
1735 key
.offset
= device
->devid
;
1737 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1746 leaf
= path
->nodes
[0];
1747 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1749 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1750 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1751 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1752 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1753 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1754 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
1755 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1756 btrfs_mark_buffer_dirty(leaf
);
1759 btrfs_free_path(path
);
1763 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1764 struct btrfs_device
*device
, u64 new_size
)
1766 struct btrfs_super_block
*super_copy
=
1767 &device
->dev_root
->fs_info
->super_copy
;
1768 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1769 u64 diff
= new_size
- device
->total_bytes
;
1771 if (!device
->writeable
)
1773 if (new_size
<= device
->total_bytes
)
1776 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
1777 device
->fs_devices
->total_rw_bytes
+= diff
;
1779 device
->total_bytes
= new_size
;
1780 device
->disk_total_bytes
= new_size
;
1781 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
1783 return btrfs_update_device(trans
, device
);
1786 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1787 struct btrfs_device
*device
, u64 new_size
)
1790 lock_chunks(device
->dev_root
);
1791 ret
= __btrfs_grow_device(trans
, device
, new_size
);
1792 unlock_chunks(device
->dev_root
);
1796 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
1797 struct btrfs_root
*root
,
1798 u64 chunk_tree
, u64 chunk_objectid
,
1802 struct btrfs_path
*path
;
1803 struct btrfs_key key
;
1805 root
= root
->fs_info
->chunk_root
;
1806 path
= btrfs_alloc_path();
1810 key
.objectid
= chunk_objectid
;
1811 key
.offset
= chunk_offset
;
1812 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1814 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1817 ret
= btrfs_del_item(trans
, root
, path
);
1819 btrfs_free_path(path
);
1823 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
1826 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1827 struct btrfs_disk_key
*disk_key
;
1828 struct btrfs_chunk
*chunk
;
1835 struct btrfs_key key
;
1837 array_size
= btrfs_super_sys_array_size(super_copy
);
1839 ptr
= super_copy
->sys_chunk_array
;
1842 while (cur
< array_size
) {
1843 disk_key
= (struct btrfs_disk_key
*)ptr
;
1844 btrfs_disk_key_to_cpu(&key
, disk_key
);
1846 len
= sizeof(*disk_key
);
1848 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1849 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
1850 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
1851 len
+= btrfs_chunk_item_size(num_stripes
);
1856 if (key
.objectid
== chunk_objectid
&&
1857 key
.offset
== chunk_offset
) {
1858 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
1860 btrfs_set_super_sys_array_size(super_copy
, array_size
);
1869 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
1870 u64 chunk_tree
, u64 chunk_objectid
,
1873 struct extent_map_tree
*em_tree
;
1874 struct btrfs_root
*extent_root
;
1875 struct btrfs_trans_handle
*trans
;
1876 struct extent_map
*em
;
1877 struct map_lookup
*map
;
1881 root
= root
->fs_info
->chunk_root
;
1882 extent_root
= root
->fs_info
->extent_root
;
1883 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
1885 ret
= btrfs_can_relocate(extent_root
, chunk_offset
);
1889 /* step one, relocate all the extents inside this chunk */
1890 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
1894 trans
= btrfs_start_transaction(root
, 0);
1895 BUG_ON(IS_ERR(trans
));
1900 * step two, delete the device extents and the
1901 * chunk tree entries
1903 read_lock(&em_tree
->lock
);
1904 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
1905 read_unlock(&em_tree
->lock
);
1907 BUG_ON(em
->start
> chunk_offset
||
1908 em
->start
+ em
->len
< chunk_offset
);
1909 map
= (struct map_lookup
*)em
->bdev
;
1911 for (i
= 0; i
< map
->num_stripes
; i
++) {
1912 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
1913 map
->stripes
[i
].physical
);
1916 if (map
->stripes
[i
].dev
) {
1917 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
1921 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
1926 trace_btrfs_chunk_free(root
, map
, chunk_offset
, em
->len
);
1928 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1929 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
1933 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
1936 write_lock(&em_tree
->lock
);
1937 remove_extent_mapping(em_tree
, em
);
1938 write_unlock(&em_tree
->lock
);
1943 /* once for the tree */
1944 free_extent_map(em
);
1946 free_extent_map(em
);
1948 unlock_chunks(root
);
1949 btrfs_end_transaction(trans
, root
);
1953 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
1955 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
1956 struct btrfs_path
*path
;
1957 struct extent_buffer
*leaf
;
1958 struct btrfs_chunk
*chunk
;
1959 struct btrfs_key key
;
1960 struct btrfs_key found_key
;
1961 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
1963 bool retried
= false;
1967 path
= btrfs_alloc_path();
1972 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1973 key
.offset
= (u64
)-1;
1974 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1977 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
1982 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
1989 leaf
= path
->nodes
[0];
1990 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1992 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
1993 struct btrfs_chunk
);
1994 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
1995 btrfs_release_path(path
);
1997 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1998 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
2007 if (found_key
.offset
== 0)
2009 key
.offset
= found_key
.offset
- 1;
2012 if (failed
&& !retried
) {
2016 } else if (failed
&& retried
) {
2021 btrfs_free_path(path
);
2025 static u64
div_factor(u64 num
, int factor
)
2034 int btrfs_balance(struct btrfs_root
*dev_root
)
2037 struct list_head
*devices
= &dev_root
->fs_info
->fs_devices
->devices
;
2038 struct btrfs_device
*device
;
2041 struct btrfs_path
*path
;
2042 struct btrfs_key key
;
2043 struct btrfs_root
*chunk_root
= dev_root
->fs_info
->chunk_root
;
2044 struct btrfs_trans_handle
*trans
;
2045 struct btrfs_key found_key
;
2047 if (dev_root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
2050 if (!capable(CAP_SYS_ADMIN
))
2053 mutex_lock(&dev_root
->fs_info
->volume_mutex
);
2054 dev_root
= dev_root
->fs_info
->dev_root
;
2056 /* step one make some room on all the devices */
2057 list_for_each_entry(device
, devices
, dev_list
) {
2058 old_size
= device
->total_bytes
;
2059 size_to_free
= div_factor(old_size
, 1);
2060 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
2061 if (!device
->writeable
||
2062 device
->total_bytes
- device
->bytes_used
> size_to_free
)
2065 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
2070 trans
= btrfs_start_transaction(dev_root
, 0);
2071 BUG_ON(IS_ERR(trans
));
2073 ret
= btrfs_grow_device(trans
, device
, old_size
);
2076 btrfs_end_transaction(trans
, dev_root
);
2079 /* step two, relocate all the chunks */
2080 path
= btrfs_alloc_path();
2083 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2084 key
.offset
= (u64
)-1;
2085 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2088 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2093 * this shouldn't happen, it means the last relocate
2099 ret
= btrfs_previous_item(chunk_root
, path
, 0,
2100 BTRFS_CHUNK_ITEM_KEY
);
2104 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
2106 if (found_key
.objectid
!= key
.objectid
)
2109 /* chunk zero is special */
2110 if (found_key
.offset
== 0)
2113 btrfs_release_path(path
);
2114 ret
= btrfs_relocate_chunk(chunk_root
,
2115 chunk_root
->root_key
.objectid
,
2118 if (ret
&& ret
!= -ENOSPC
)
2120 key
.offset
= found_key
.offset
- 1;
2124 btrfs_free_path(path
);
2125 mutex_unlock(&dev_root
->fs_info
->volume_mutex
);
2130 * shrinking a device means finding all of the device extents past
2131 * the new size, and then following the back refs to the chunks.
2132 * The chunk relocation code actually frees the device extent
2134 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
2136 struct btrfs_trans_handle
*trans
;
2137 struct btrfs_root
*root
= device
->dev_root
;
2138 struct btrfs_dev_extent
*dev_extent
= NULL
;
2139 struct btrfs_path
*path
;
2147 bool retried
= false;
2148 struct extent_buffer
*l
;
2149 struct btrfs_key key
;
2150 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
2151 u64 old_total
= btrfs_super_total_bytes(super_copy
);
2152 u64 old_size
= device
->total_bytes
;
2153 u64 diff
= device
->total_bytes
- new_size
;
2155 if (new_size
>= device
->total_bytes
)
2158 path
= btrfs_alloc_path();
2166 device
->total_bytes
= new_size
;
2167 if (device
->writeable
)
2168 device
->fs_devices
->total_rw_bytes
-= diff
;
2169 unlock_chunks(root
);
2172 key
.objectid
= device
->devid
;
2173 key
.offset
= (u64
)-1;
2174 key
.type
= BTRFS_DEV_EXTENT_KEY
;
2177 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2181 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
2186 btrfs_release_path(path
);
2191 slot
= path
->slots
[0];
2192 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
2194 if (key
.objectid
!= device
->devid
) {
2195 btrfs_release_path(path
);
2199 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
2200 length
= btrfs_dev_extent_length(l
, dev_extent
);
2202 if (key
.offset
+ length
<= new_size
) {
2203 btrfs_release_path(path
);
2207 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
2208 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
2209 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
2210 btrfs_release_path(path
);
2212 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
2214 if (ret
&& ret
!= -ENOSPC
)
2221 if (failed
&& !retried
) {
2225 } else if (failed
&& retried
) {
2229 device
->total_bytes
= old_size
;
2230 if (device
->writeable
)
2231 device
->fs_devices
->total_rw_bytes
+= diff
;
2232 unlock_chunks(root
);
2236 /* Shrinking succeeded, else we would be at "done". */
2237 trans
= btrfs_start_transaction(root
, 0);
2238 if (IS_ERR(trans
)) {
2239 ret
= PTR_ERR(trans
);
2245 device
->disk_total_bytes
= new_size
;
2246 /* Now btrfs_update_device() will change the on-disk size. */
2247 ret
= btrfs_update_device(trans
, device
);
2249 unlock_chunks(root
);
2250 btrfs_end_transaction(trans
, root
);
2253 WARN_ON(diff
> old_total
);
2254 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
2255 unlock_chunks(root
);
2256 btrfs_end_transaction(trans
, root
);
2258 btrfs_free_path(path
);
2262 static int btrfs_add_system_chunk(struct btrfs_trans_handle
*trans
,
2263 struct btrfs_root
*root
,
2264 struct btrfs_key
*key
,
2265 struct btrfs_chunk
*chunk
, int item_size
)
2267 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
2268 struct btrfs_disk_key disk_key
;
2272 array_size
= btrfs_super_sys_array_size(super_copy
);
2273 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
2276 ptr
= super_copy
->sys_chunk_array
+ array_size
;
2277 btrfs_cpu_key_to_disk(&disk_key
, key
);
2278 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
2279 ptr
+= sizeof(disk_key
);
2280 memcpy(ptr
, chunk
, item_size
);
2281 item_size
+= sizeof(disk_key
);
2282 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
2287 * sort the devices in descending order by max_avail, total_avail
2289 static int btrfs_cmp_device_info(const void *a
, const void *b
)
2291 const struct btrfs_device_info
*di_a
= a
;
2292 const struct btrfs_device_info
*di_b
= b
;
2294 if (di_a
->max_avail
> di_b
->max_avail
)
2296 if (di_a
->max_avail
< di_b
->max_avail
)
2298 if (di_a
->total_avail
> di_b
->total_avail
)
2300 if (di_a
->total_avail
< di_b
->total_avail
)
2305 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
2306 struct btrfs_root
*extent_root
,
2307 struct map_lookup
**map_ret
,
2308 u64
*num_bytes_out
, u64
*stripe_size_out
,
2309 u64 start
, u64 type
)
2311 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
2312 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
2313 struct list_head
*cur
;
2314 struct map_lookup
*map
= NULL
;
2315 struct extent_map_tree
*em_tree
;
2316 struct extent_map
*em
;
2317 struct btrfs_device_info
*devices_info
= NULL
;
2319 int num_stripes
; /* total number of stripes to allocate */
2320 int sub_stripes
; /* sub_stripes info for map */
2321 int dev_stripes
; /* stripes per dev */
2322 int devs_max
; /* max devs to use */
2323 int devs_min
; /* min devs needed */
2324 int devs_increment
; /* ndevs has to be a multiple of this */
2325 int ncopies
; /* how many copies to data has */
2327 u64 max_stripe_size
;
2335 if ((type
& BTRFS_BLOCK_GROUP_RAID1
) &&
2336 (type
& BTRFS_BLOCK_GROUP_DUP
)) {
2338 type
&= ~BTRFS_BLOCK_GROUP_DUP
;
2341 if (list_empty(&fs_devices
->alloc_list
))
2348 devs_max
= 0; /* 0 == as many as possible */
2352 * define the properties of each RAID type.
2353 * FIXME: move this to a global table and use it in all RAID
2356 if (type
& (BTRFS_BLOCK_GROUP_DUP
)) {
2360 } else if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
2362 } else if (type
& (BTRFS_BLOCK_GROUP_RAID1
)) {
2367 } else if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
2376 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
2377 max_stripe_size
= 1024 * 1024 * 1024;
2378 max_chunk_size
= 10 * max_stripe_size
;
2379 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
2380 max_stripe_size
= 256 * 1024 * 1024;
2381 max_chunk_size
= max_stripe_size
;
2382 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2383 max_stripe_size
= 8 * 1024 * 1024;
2384 max_chunk_size
= 2 * max_stripe_size
;
2386 printk(KERN_ERR
"btrfs: invalid chunk type 0x%llx requested\n",
2391 /* we don't want a chunk larger than 10% of writeable space */
2392 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
2395 devices_info
= kzalloc(sizeof(*devices_info
) * fs_devices
->rw_devices
,
2400 cur
= fs_devices
->alloc_list
.next
;
2403 * in the first pass through the devices list, we gather information
2404 * about the available holes on each device.
2407 while (cur
!= &fs_devices
->alloc_list
) {
2408 struct btrfs_device
*device
;
2412 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
2416 if (!device
->writeable
) {
2418 "btrfs: read-only device in alloc_list\n");
2423 if (!device
->in_fs_metadata
)
2426 if (device
->total_bytes
> device
->bytes_used
)
2427 total_avail
= device
->total_bytes
- device
->bytes_used
;
2430 /* avail is off by max(alloc_start, 1MB), but that is the same
2431 * for all devices, so it doesn't hurt the sorting later on
2434 ret
= find_free_dev_extent(trans
, device
,
2435 max_stripe_size
* dev_stripes
,
2436 &dev_offset
, &max_avail
);
2437 if (ret
&& ret
!= -ENOSPC
)
2441 max_avail
= max_stripe_size
* dev_stripes
;
2443 if (max_avail
< BTRFS_STRIPE_LEN
* dev_stripes
)
2446 devices_info
[ndevs
].dev_offset
= dev_offset
;
2447 devices_info
[ndevs
].max_avail
= max_avail
;
2448 devices_info
[ndevs
].total_avail
= total_avail
;
2449 devices_info
[ndevs
].dev
= device
;
2454 * now sort the devices by hole size / available space
2456 sort(devices_info
, ndevs
, sizeof(struct btrfs_device_info
),
2457 btrfs_cmp_device_info
, NULL
);
2459 /* round down to number of usable stripes */
2460 ndevs
-= ndevs
% devs_increment
;
2462 if (ndevs
< devs_increment
* sub_stripes
|| ndevs
< devs_min
) {
2467 if (devs_max
&& ndevs
> devs_max
)
2470 * the primary goal is to maximize the number of stripes, so use as many
2471 * devices as possible, even if the stripes are not maximum sized.
2473 stripe_size
= devices_info
[ndevs
-1].max_avail
;
2474 num_stripes
= ndevs
* dev_stripes
;
2476 if (stripe_size
* num_stripes
> max_chunk_size
* ncopies
) {
2477 stripe_size
= max_chunk_size
* ncopies
;
2478 do_div(stripe_size
, num_stripes
);
2481 do_div(stripe_size
, dev_stripes
);
2482 do_div(stripe_size
, BTRFS_STRIPE_LEN
);
2483 stripe_size
*= BTRFS_STRIPE_LEN
;
2485 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
2490 map
->num_stripes
= num_stripes
;
2492 for (i
= 0; i
< ndevs
; ++i
) {
2493 for (j
= 0; j
< dev_stripes
; ++j
) {
2494 int s
= i
* dev_stripes
+ j
;
2495 map
->stripes
[s
].dev
= devices_info
[i
].dev
;
2496 map
->stripes
[s
].physical
= devices_info
[i
].dev_offset
+
2500 map
->sector_size
= extent_root
->sectorsize
;
2501 map
->stripe_len
= BTRFS_STRIPE_LEN
;
2502 map
->io_align
= BTRFS_STRIPE_LEN
;
2503 map
->io_width
= BTRFS_STRIPE_LEN
;
2505 map
->sub_stripes
= sub_stripes
;
2508 num_bytes
= stripe_size
* (num_stripes
/ ncopies
);
2510 *stripe_size_out
= stripe_size
;
2511 *num_bytes_out
= num_bytes
;
2513 trace_btrfs_chunk_alloc(info
->chunk_root
, map
, start
, num_bytes
);
2515 em
= alloc_extent_map();
2520 em
->bdev
= (struct block_device
*)map
;
2522 em
->len
= num_bytes
;
2523 em
->block_start
= 0;
2524 em
->block_len
= em
->len
;
2526 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
2527 write_lock(&em_tree
->lock
);
2528 ret
= add_extent_mapping(em_tree
, em
);
2529 write_unlock(&em_tree
->lock
);
2531 free_extent_map(em
);
2533 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
2534 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2538 for (i
= 0; i
< map
->num_stripes
; ++i
) {
2539 struct btrfs_device
*device
;
2542 device
= map
->stripes
[i
].dev
;
2543 dev_offset
= map
->stripes
[i
].physical
;
2545 ret
= btrfs_alloc_dev_extent(trans
, device
,
2546 info
->chunk_root
->root_key
.objectid
,
2547 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2548 start
, dev_offset
, stripe_size
);
2552 kfree(devices_info
);
2557 kfree(devices_info
);
2561 static int __finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
2562 struct btrfs_root
*extent_root
,
2563 struct map_lookup
*map
, u64 chunk_offset
,
2564 u64 chunk_size
, u64 stripe_size
)
2567 struct btrfs_key key
;
2568 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
2569 struct btrfs_device
*device
;
2570 struct btrfs_chunk
*chunk
;
2571 struct btrfs_stripe
*stripe
;
2572 size_t item_size
= btrfs_chunk_item_size(map
->num_stripes
);
2576 chunk
= kzalloc(item_size
, GFP_NOFS
);
2581 while (index
< map
->num_stripes
) {
2582 device
= map
->stripes
[index
].dev
;
2583 device
->bytes_used
+= stripe_size
;
2584 ret
= btrfs_update_device(trans
, device
);
2590 stripe
= &chunk
->stripe
;
2591 while (index
< map
->num_stripes
) {
2592 device
= map
->stripes
[index
].dev
;
2593 dev_offset
= map
->stripes
[index
].physical
;
2595 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
2596 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
2597 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
2602 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
2603 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
2604 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
2605 btrfs_set_stack_chunk_type(chunk
, map
->type
);
2606 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
2607 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
2608 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
2609 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
2610 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
2612 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2613 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2614 key
.offset
= chunk_offset
;
2616 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
2619 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2620 ret
= btrfs_add_system_chunk(trans
, chunk_root
, &key
, chunk
,
2630 * Chunk allocation falls into two parts. The first part does works
2631 * that make the new allocated chunk useable, but not do any operation
2632 * that modifies the chunk tree. The second part does the works that
2633 * require modifying the chunk tree. This division is important for the
2634 * bootstrap process of adding storage to a seed btrfs.
2636 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
2637 struct btrfs_root
*extent_root
, u64 type
)
2642 struct map_lookup
*map
;
2643 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
2646 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2651 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
2652 &stripe_size
, chunk_offset
, type
);
2656 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
2657 chunk_size
, stripe_size
);
2662 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
2663 struct btrfs_root
*root
,
2664 struct btrfs_device
*device
)
2667 u64 sys_chunk_offset
;
2671 u64 sys_stripe_size
;
2673 struct map_lookup
*map
;
2674 struct map_lookup
*sys_map
;
2675 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2676 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
2679 ret
= find_next_chunk(fs_info
->chunk_root
,
2680 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, &chunk_offset
);
2683 alloc_profile
= BTRFS_BLOCK_GROUP_METADATA
|
2684 (fs_info
->metadata_alloc_profile
&
2685 fs_info
->avail_metadata_alloc_bits
);
2686 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
2688 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
2689 &stripe_size
, chunk_offset
, alloc_profile
);
2692 sys_chunk_offset
= chunk_offset
+ chunk_size
;
2694 alloc_profile
= BTRFS_BLOCK_GROUP_SYSTEM
|
2695 (fs_info
->system_alloc_profile
&
2696 fs_info
->avail_system_alloc_bits
);
2697 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
2699 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &sys_map
,
2700 &sys_chunk_size
, &sys_stripe_size
,
2701 sys_chunk_offset
, alloc_profile
);
2704 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
2708 * Modifying chunk tree needs allocating new blocks from both
2709 * system block group and metadata block group. So we only can
2710 * do operations require modifying the chunk tree after both
2711 * block groups were created.
2713 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
2714 chunk_size
, stripe_size
);
2717 ret
= __finish_chunk_alloc(trans
, extent_root
, sys_map
,
2718 sys_chunk_offset
, sys_chunk_size
,
2724 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
2726 struct extent_map
*em
;
2727 struct map_lookup
*map
;
2728 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
2732 read_lock(&map_tree
->map_tree
.lock
);
2733 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
2734 read_unlock(&map_tree
->map_tree
.lock
);
2738 if (btrfs_test_opt(root
, DEGRADED
)) {
2739 free_extent_map(em
);
2743 map
= (struct map_lookup
*)em
->bdev
;
2744 for (i
= 0; i
< map
->num_stripes
; i
++) {
2745 if (!map
->stripes
[i
].dev
->writeable
) {
2750 free_extent_map(em
);
2754 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
2756 extent_map_tree_init(&tree
->map_tree
);
2759 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
2761 struct extent_map
*em
;
2764 write_lock(&tree
->map_tree
.lock
);
2765 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
2767 remove_extent_mapping(&tree
->map_tree
, em
);
2768 write_unlock(&tree
->map_tree
.lock
);
2773 free_extent_map(em
);
2774 /* once for the tree */
2775 free_extent_map(em
);
2779 int btrfs_num_copies(struct btrfs_mapping_tree
*map_tree
, u64 logical
, u64 len
)
2781 struct extent_map
*em
;
2782 struct map_lookup
*map
;
2783 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2786 read_lock(&em_tree
->lock
);
2787 em
= lookup_extent_mapping(em_tree
, logical
, len
);
2788 read_unlock(&em_tree
->lock
);
2791 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
2792 map
= (struct map_lookup
*)em
->bdev
;
2793 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
2794 ret
= map
->num_stripes
;
2795 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
2796 ret
= map
->sub_stripes
;
2799 free_extent_map(em
);
2803 static int find_live_mirror(struct map_lookup
*map
, int first
, int num
,
2807 if (map
->stripes
[optimal
].dev
->bdev
)
2809 for (i
= first
; i
< first
+ num
; i
++) {
2810 if (map
->stripes
[i
].dev
->bdev
)
2813 /* we couldn't find one that doesn't fail. Just return something
2814 * and the io error handling code will clean up eventually
2819 static int __btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
2820 u64 logical
, u64
*length
,
2821 struct btrfs_multi_bio
**multi_ret
,
2824 struct extent_map
*em
;
2825 struct map_lookup
*map
;
2826 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2829 u64 stripe_end_offset
;
2833 int stripes_allocated
= 8;
2834 int stripes_required
= 1;
2839 struct btrfs_multi_bio
*multi
= NULL
;
2841 if (multi_ret
&& !(rw
& (REQ_WRITE
| REQ_DISCARD
)))
2842 stripes_allocated
= 1;
2845 multi
= kzalloc(btrfs_multi_bio_size(stripes_allocated
),
2850 atomic_set(&multi
->error
, 0);
2853 read_lock(&em_tree
->lock
);
2854 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
2855 read_unlock(&em_tree
->lock
);
2858 printk(KERN_CRIT
"unable to find logical %llu len %llu\n",
2859 (unsigned long long)logical
,
2860 (unsigned long long)*length
);
2864 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
2865 map
= (struct map_lookup
*)em
->bdev
;
2866 offset
= logical
- em
->start
;
2868 if (mirror_num
> map
->num_stripes
)
2871 /* if our multi bio struct is too small, back off and try again */
2872 if (rw
& REQ_WRITE
) {
2873 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
2874 BTRFS_BLOCK_GROUP_DUP
)) {
2875 stripes_required
= map
->num_stripes
;
2877 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
2878 stripes_required
= map
->sub_stripes
;
2882 if (rw
& REQ_DISCARD
) {
2883 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
2884 BTRFS_BLOCK_GROUP_RAID1
|
2885 BTRFS_BLOCK_GROUP_DUP
|
2886 BTRFS_BLOCK_GROUP_RAID10
)) {
2887 stripes_required
= map
->num_stripes
;
2890 if (multi_ret
&& (rw
& (REQ_WRITE
| REQ_DISCARD
)) &&
2891 stripes_allocated
< stripes_required
) {
2892 stripes_allocated
= map
->num_stripes
;
2893 free_extent_map(em
);
2899 * stripe_nr counts the total number of stripes we have to stride
2900 * to get to this block
2902 do_div(stripe_nr
, map
->stripe_len
);
2904 stripe_offset
= stripe_nr
* map
->stripe_len
;
2905 BUG_ON(offset
< stripe_offset
);
2907 /* stripe_offset is the offset of this block in its stripe*/
2908 stripe_offset
= offset
- stripe_offset
;
2910 if (rw
& REQ_DISCARD
)
2911 *length
= min_t(u64
, em
->len
- offset
, *length
);
2912 else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
2913 BTRFS_BLOCK_GROUP_RAID1
|
2914 BTRFS_BLOCK_GROUP_RAID10
|
2915 BTRFS_BLOCK_GROUP_DUP
)) {
2916 /* we limit the length of each bio to what fits in a stripe */
2917 *length
= min_t(u64
, em
->len
- offset
,
2918 map
->stripe_len
- stripe_offset
);
2920 *length
= em
->len
- offset
;
2928 stripe_nr_orig
= stripe_nr
;
2929 stripe_nr_end
= (offset
+ *length
+ map
->stripe_len
- 1) &
2930 (~(map
->stripe_len
- 1));
2931 do_div(stripe_nr_end
, map
->stripe_len
);
2932 stripe_end_offset
= stripe_nr_end
* map
->stripe_len
-
2934 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
2935 if (rw
& REQ_DISCARD
)
2936 num_stripes
= min_t(u64
, map
->num_stripes
,
2937 stripe_nr_end
- stripe_nr_orig
);
2938 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
2939 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
2940 if (rw
& (REQ_WRITE
| REQ_DISCARD
))
2941 num_stripes
= map
->num_stripes
;
2942 else if (mirror_num
)
2943 stripe_index
= mirror_num
- 1;
2945 stripe_index
= find_live_mirror(map
, 0,
2947 current
->pid
% map
->num_stripes
);
2950 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
2951 if (rw
& (REQ_WRITE
| REQ_DISCARD
))
2952 num_stripes
= map
->num_stripes
;
2953 else if (mirror_num
)
2954 stripe_index
= mirror_num
- 1;
2956 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
2957 int factor
= map
->num_stripes
/ map
->sub_stripes
;
2959 stripe_index
= do_div(stripe_nr
, factor
);
2960 stripe_index
*= map
->sub_stripes
;
2963 num_stripes
= map
->sub_stripes
;
2964 else if (rw
& REQ_DISCARD
)
2965 num_stripes
= min_t(u64
, map
->sub_stripes
*
2966 (stripe_nr_end
- stripe_nr_orig
),
2968 else if (mirror_num
)
2969 stripe_index
+= mirror_num
- 1;
2971 stripe_index
= find_live_mirror(map
, stripe_index
,
2972 map
->sub_stripes
, stripe_index
+
2973 current
->pid
% map
->sub_stripes
);
2977 * after this do_div call, stripe_nr is the number of stripes
2978 * on this device we have to walk to find the data, and
2979 * stripe_index is the number of our device in the stripe array
2981 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
2983 BUG_ON(stripe_index
>= map
->num_stripes
);
2985 if (rw
& REQ_DISCARD
) {
2986 for (i
= 0; i
< num_stripes
; i
++) {
2987 multi
->stripes
[i
].physical
=
2988 map
->stripes
[stripe_index
].physical
+
2989 stripe_offset
+ stripe_nr
* map
->stripe_len
;
2990 multi
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
2992 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
2994 u32 last_stripe
= 0;
2997 div_u64_rem(stripe_nr_end
- 1,
3001 for (j
= 0; j
< map
->num_stripes
; j
++) {
3004 div_u64_rem(stripe_nr_end
- 1 - j
,
3005 map
->num_stripes
, &test
);
3006 if (test
== stripe_index
)
3009 stripes
= stripe_nr_end
- 1 - j
;
3010 do_div(stripes
, map
->num_stripes
);
3011 multi
->stripes
[i
].length
= map
->stripe_len
*
3012 (stripes
- stripe_nr
+ 1);
3015 multi
->stripes
[i
].length
-=
3019 if (stripe_index
== last_stripe
)
3020 multi
->stripes
[i
].length
-=
3022 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3025 int factor
= map
->num_stripes
/
3027 u32 last_stripe
= 0;
3029 div_u64_rem(stripe_nr_end
- 1,
3030 factor
, &last_stripe
);
3031 last_stripe
*= map
->sub_stripes
;
3033 for (j
= 0; j
< factor
; j
++) {
3036 div_u64_rem(stripe_nr_end
- 1 - j
,
3040 stripe_index
/ map
->sub_stripes
)
3043 stripes
= stripe_nr_end
- 1 - j
;
3044 do_div(stripes
, factor
);
3045 multi
->stripes
[i
].length
= map
->stripe_len
*
3046 (stripes
- stripe_nr
+ 1);
3048 if (i
< map
->sub_stripes
) {
3049 multi
->stripes
[i
].length
-=
3051 if (i
== map
->sub_stripes
- 1)
3054 if (stripe_index
>= last_stripe
&&
3055 stripe_index
<= (last_stripe
+
3056 map
->sub_stripes
- 1)) {
3057 multi
->stripes
[i
].length
-=
3061 multi
->stripes
[i
].length
= *length
;
3064 if (stripe_index
== map
->num_stripes
) {
3065 /* This could only happen for RAID0/10 */
3071 for (i
= 0; i
< num_stripes
; i
++) {
3072 multi
->stripes
[i
].physical
=
3073 map
->stripes
[stripe_index
].physical
+
3075 stripe_nr
* map
->stripe_len
;
3076 multi
->stripes
[i
].dev
=
3077 map
->stripes
[stripe_index
].dev
;
3083 multi
->num_stripes
= num_stripes
;
3084 multi
->max_errors
= max_errors
;
3087 free_extent_map(em
);
3091 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
3092 u64 logical
, u64
*length
,
3093 struct btrfs_multi_bio
**multi_ret
, int mirror_num
)
3095 return __btrfs_map_block(map_tree
, rw
, logical
, length
, multi_ret
,
3099 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
3100 u64 chunk_start
, u64 physical
, u64 devid
,
3101 u64
**logical
, int *naddrs
, int *stripe_len
)
3103 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
3104 struct extent_map
*em
;
3105 struct map_lookup
*map
;
3112 read_lock(&em_tree
->lock
);
3113 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
3114 read_unlock(&em_tree
->lock
);
3116 BUG_ON(!em
|| em
->start
!= chunk_start
);
3117 map
= (struct map_lookup
*)em
->bdev
;
3120 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
3121 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
3122 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
3123 do_div(length
, map
->num_stripes
);
3125 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
3128 for (i
= 0; i
< map
->num_stripes
; i
++) {
3129 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
3131 if (map
->stripes
[i
].physical
> physical
||
3132 map
->stripes
[i
].physical
+ length
<= physical
)
3135 stripe_nr
= physical
- map
->stripes
[i
].physical
;
3136 do_div(stripe_nr
, map
->stripe_len
);
3138 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3139 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
3140 do_div(stripe_nr
, map
->sub_stripes
);
3141 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3142 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
3144 bytenr
= chunk_start
+ stripe_nr
* map
->stripe_len
;
3145 WARN_ON(nr
>= map
->num_stripes
);
3146 for (j
= 0; j
< nr
; j
++) {
3147 if (buf
[j
] == bytenr
)
3151 WARN_ON(nr
>= map
->num_stripes
);
3158 *stripe_len
= map
->stripe_len
;
3160 free_extent_map(em
);
3164 static void end_bio_multi_stripe(struct bio
*bio
, int err
)
3166 struct btrfs_multi_bio
*multi
= bio
->bi_private
;
3167 int is_orig_bio
= 0;
3170 atomic_inc(&multi
->error
);
3172 if (bio
== multi
->orig_bio
)
3175 if (atomic_dec_and_test(&multi
->stripes_pending
)) {
3178 bio
= multi
->orig_bio
;
3180 bio
->bi_private
= multi
->private;
3181 bio
->bi_end_io
= multi
->end_io
;
3182 /* only send an error to the higher layers if it is
3183 * beyond the tolerance of the multi-bio
3185 if (atomic_read(&multi
->error
) > multi
->max_errors
) {
3189 * this bio is actually up to date, we didn't
3190 * go over the max number of errors
3192 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
3197 bio_endio(bio
, err
);
3198 } else if (!is_orig_bio
) {
3203 struct async_sched
{
3206 struct btrfs_fs_info
*info
;
3207 struct btrfs_work work
;
3211 * see run_scheduled_bios for a description of why bios are collected for
3214 * This will add one bio to the pending list for a device and make sure
3215 * the work struct is scheduled.
3217 static noinline
int schedule_bio(struct btrfs_root
*root
,
3218 struct btrfs_device
*device
,
3219 int rw
, struct bio
*bio
)
3221 int should_queue
= 1;
3222 struct btrfs_pending_bios
*pending_bios
;
3224 /* don't bother with additional async steps for reads, right now */
3225 if (!(rw
& REQ_WRITE
)) {
3227 submit_bio(rw
, bio
);
3233 * nr_async_bios allows us to reliably return congestion to the
3234 * higher layers. Otherwise, the async bio makes it appear we have
3235 * made progress against dirty pages when we've really just put it
3236 * on a queue for later
3238 atomic_inc(&root
->fs_info
->nr_async_bios
);
3239 WARN_ON(bio
->bi_next
);
3240 bio
->bi_next
= NULL
;
3243 spin_lock(&device
->io_lock
);
3244 if (bio
->bi_rw
& REQ_SYNC
)
3245 pending_bios
= &device
->pending_sync_bios
;
3247 pending_bios
= &device
->pending_bios
;
3249 if (pending_bios
->tail
)
3250 pending_bios
->tail
->bi_next
= bio
;
3252 pending_bios
->tail
= bio
;
3253 if (!pending_bios
->head
)
3254 pending_bios
->head
= bio
;
3255 if (device
->running_pending
)
3258 spin_unlock(&device
->io_lock
);
3261 btrfs_queue_worker(&root
->fs_info
->submit_workers
,
3266 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
3267 int mirror_num
, int async_submit
)
3269 struct btrfs_mapping_tree
*map_tree
;
3270 struct btrfs_device
*dev
;
3271 struct bio
*first_bio
= bio
;
3272 u64 logical
= (u64
)bio
->bi_sector
<< 9;
3275 struct btrfs_multi_bio
*multi
= NULL
;
3280 length
= bio
->bi_size
;
3281 map_tree
= &root
->fs_info
->mapping_tree
;
3282 map_length
= length
;
3284 ret
= btrfs_map_block(map_tree
, rw
, logical
, &map_length
, &multi
,
3288 total_devs
= multi
->num_stripes
;
3289 if (map_length
< length
) {
3290 printk(KERN_CRIT
"mapping failed logical %llu bio len %llu "
3291 "len %llu\n", (unsigned long long)logical
,
3292 (unsigned long long)length
,
3293 (unsigned long long)map_length
);
3296 multi
->end_io
= first_bio
->bi_end_io
;
3297 multi
->private = first_bio
->bi_private
;
3298 multi
->orig_bio
= first_bio
;
3299 atomic_set(&multi
->stripes_pending
, multi
->num_stripes
);
3301 while (dev_nr
< total_devs
) {
3302 if (total_devs
> 1) {
3303 if (dev_nr
< total_devs
- 1) {
3304 bio
= bio_clone(first_bio
, GFP_NOFS
);
3309 bio
->bi_private
= multi
;
3310 bio
->bi_end_io
= end_bio_multi_stripe
;
3312 bio
->bi_sector
= multi
->stripes
[dev_nr
].physical
>> 9;
3313 dev
= multi
->stripes
[dev_nr
].dev
;
3314 if (dev
&& dev
->bdev
&& (rw
!= WRITE
|| dev
->writeable
)) {
3315 bio
->bi_bdev
= dev
->bdev
;
3317 schedule_bio(root
, dev
, rw
, bio
);
3319 submit_bio(rw
, bio
);
3321 bio
->bi_bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
3322 bio
->bi_sector
= logical
>> 9;
3323 bio_endio(bio
, -EIO
);
3327 if (total_devs
== 1)
3332 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
,
3335 struct btrfs_device
*device
;
3336 struct btrfs_fs_devices
*cur_devices
;
3338 cur_devices
= root
->fs_info
->fs_devices
;
3339 while (cur_devices
) {
3341 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
3342 device
= __find_device(&cur_devices
->devices
,
3347 cur_devices
= cur_devices
->seed
;
3352 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
3353 u64 devid
, u8
*dev_uuid
)
3355 struct btrfs_device
*device
;
3356 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
3358 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
3361 list_add(&device
->dev_list
,
3362 &fs_devices
->devices
);
3363 device
->dev_root
= root
->fs_info
->dev_root
;
3364 device
->devid
= devid
;
3365 device
->work
.func
= pending_bios_fn
;
3366 device
->fs_devices
= fs_devices
;
3367 device
->missing
= 1;
3368 fs_devices
->num_devices
++;
3369 fs_devices
->missing_devices
++;
3370 spin_lock_init(&device
->io_lock
);
3371 INIT_LIST_HEAD(&device
->dev_alloc_list
);
3372 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
3376 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
3377 struct extent_buffer
*leaf
,
3378 struct btrfs_chunk
*chunk
)
3380 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
3381 struct map_lookup
*map
;
3382 struct extent_map
*em
;
3386 u8 uuid
[BTRFS_UUID_SIZE
];
3391 logical
= key
->offset
;
3392 length
= btrfs_chunk_length(leaf
, chunk
);
3394 read_lock(&map_tree
->map_tree
.lock
);
3395 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
3396 read_unlock(&map_tree
->map_tree
.lock
);
3398 /* already mapped? */
3399 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
3400 free_extent_map(em
);
3403 free_extent_map(em
);
3406 em
= alloc_extent_map();
3409 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
3410 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
3412 free_extent_map(em
);
3416 em
->bdev
= (struct block_device
*)map
;
3417 em
->start
= logical
;
3419 em
->block_start
= 0;
3420 em
->block_len
= em
->len
;
3422 map
->num_stripes
= num_stripes
;
3423 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
3424 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
3425 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
3426 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
3427 map
->type
= btrfs_chunk_type(leaf
, chunk
);
3428 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
3429 for (i
= 0; i
< num_stripes
; i
++) {
3430 map
->stripes
[i
].physical
=
3431 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
3432 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
3433 read_extent_buffer(leaf
, uuid
, (unsigned long)
3434 btrfs_stripe_dev_uuid_nr(chunk
, i
),
3436 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
, uuid
,
3438 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
3440 free_extent_map(em
);
3443 if (!map
->stripes
[i
].dev
) {
3444 map
->stripes
[i
].dev
=
3445 add_missing_dev(root
, devid
, uuid
);
3446 if (!map
->stripes
[i
].dev
) {
3448 free_extent_map(em
);
3452 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
3455 write_lock(&map_tree
->map_tree
.lock
);
3456 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
3457 write_unlock(&map_tree
->map_tree
.lock
);
3459 free_extent_map(em
);
3464 static int fill_device_from_item(struct extent_buffer
*leaf
,
3465 struct btrfs_dev_item
*dev_item
,
3466 struct btrfs_device
*device
)
3470 device
->devid
= btrfs_device_id(leaf
, dev_item
);
3471 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
3472 device
->total_bytes
= device
->disk_total_bytes
;
3473 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
3474 device
->type
= btrfs_device_type(leaf
, dev_item
);
3475 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
3476 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
3477 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
3479 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
3480 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
3485 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
3487 struct btrfs_fs_devices
*fs_devices
;
3490 mutex_lock(&uuid_mutex
);
3492 fs_devices
= root
->fs_info
->fs_devices
->seed
;
3493 while (fs_devices
) {
3494 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
3498 fs_devices
= fs_devices
->seed
;
3501 fs_devices
= find_fsid(fsid
);
3507 fs_devices
= clone_fs_devices(fs_devices
);
3508 if (IS_ERR(fs_devices
)) {
3509 ret
= PTR_ERR(fs_devices
);
3513 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
3514 root
->fs_info
->bdev_holder
);
3518 if (!fs_devices
->seeding
) {
3519 __btrfs_close_devices(fs_devices
);
3520 free_fs_devices(fs_devices
);
3525 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
3526 root
->fs_info
->fs_devices
->seed
= fs_devices
;
3528 mutex_unlock(&uuid_mutex
);
3532 static int read_one_dev(struct btrfs_root
*root
,
3533 struct extent_buffer
*leaf
,
3534 struct btrfs_dev_item
*dev_item
)
3536 struct btrfs_device
*device
;
3539 u8 fs_uuid
[BTRFS_UUID_SIZE
];
3540 u8 dev_uuid
[BTRFS_UUID_SIZE
];
3542 devid
= btrfs_device_id(leaf
, dev_item
);
3543 read_extent_buffer(leaf
, dev_uuid
,
3544 (unsigned long)btrfs_device_uuid(dev_item
),
3546 read_extent_buffer(leaf
, fs_uuid
,
3547 (unsigned long)btrfs_device_fsid(dev_item
),
3550 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
3551 ret
= open_seed_devices(root
, fs_uuid
);
3552 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
3556 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
3557 if (!device
|| !device
->bdev
) {
3558 if (!btrfs_test_opt(root
, DEGRADED
))
3562 printk(KERN_WARNING
"warning devid %llu missing\n",
3563 (unsigned long long)devid
);
3564 device
= add_missing_dev(root
, devid
, dev_uuid
);
3567 } else if (!device
->missing
) {
3569 * this happens when a device that was properly setup
3570 * in the device info lists suddenly goes bad.
3571 * device->bdev is NULL, and so we have to set
3572 * device->missing to one here
3574 root
->fs_info
->fs_devices
->missing_devices
++;
3575 device
->missing
= 1;
3579 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
3580 BUG_ON(device
->writeable
);
3581 if (device
->generation
!=
3582 btrfs_device_generation(leaf
, dev_item
))
3586 fill_device_from_item(leaf
, dev_item
, device
);
3587 device
->dev_root
= root
->fs_info
->dev_root
;
3588 device
->in_fs_metadata
= 1;
3589 if (device
->writeable
)
3590 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
3595 int btrfs_read_sys_array(struct btrfs_root
*root
)
3597 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
3598 struct extent_buffer
*sb
;
3599 struct btrfs_disk_key
*disk_key
;
3600 struct btrfs_chunk
*chunk
;
3602 unsigned long sb_ptr
;
3608 struct btrfs_key key
;
3610 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
3611 BTRFS_SUPER_INFO_SIZE
);
3614 btrfs_set_buffer_uptodate(sb
);
3615 btrfs_set_buffer_lockdep_class(sb
, 0);
3617 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
3618 array_size
= btrfs_super_sys_array_size(super_copy
);
3620 ptr
= super_copy
->sys_chunk_array
;
3621 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
3624 while (cur
< array_size
) {
3625 disk_key
= (struct btrfs_disk_key
*)ptr
;
3626 btrfs_disk_key_to_cpu(&key
, disk_key
);
3628 len
= sizeof(*disk_key
); ptr
+= len
;
3632 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
3633 chunk
= (struct btrfs_chunk
*)sb_ptr
;
3634 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
3637 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
3638 len
= btrfs_chunk_item_size(num_stripes
);
3647 free_extent_buffer(sb
);
3651 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
3653 struct btrfs_path
*path
;
3654 struct extent_buffer
*leaf
;
3655 struct btrfs_key key
;
3656 struct btrfs_key found_key
;
3660 root
= root
->fs_info
->chunk_root
;
3662 path
= btrfs_alloc_path();
3666 /* first we search for all of the device items, and then we
3667 * read in all of the chunk items. This way we can create chunk
3668 * mappings that reference all of the devices that are afound
3670 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
3674 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3678 leaf
= path
->nodes
[0];
3679 slot
= path
->slots
[0];
3680 if (slot
>= btrfs_header_nritems(leaf
)) {
3681 ret
= btrfs_next_leaf(root
, path
);
3688 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3689 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
3690 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
3692 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
3693 struct btrfs_dev_item
*dev_item
;
3694 dev_item
= btrfs_item_ptr(leaf
, slot
,
3695 struct btrfs_dev_item
);
3696 ret
= read_one_dev(root
, leaf
, dev_item
);
3700 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
3701 struct btrfs_chunk
*chunk
;
3702 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
3703 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
3709 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
3711 btrfs_release_path(path
);
3716 btrfs_free_path(path
);