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 int sync_pending
= 0;
146 struct blk_plug plug
;
149 * this function runs all the bios we've collected for
150 * a particular device. We don't want to wander off to
151 * another device without first sending all of these down.
152 * So, setup a plug here and finish it off before we return
154 blk_start_plug(&plug
);
156 bdi
= blk_get_backing_dev_info(device
->bdev
);
157 fs_info
= device
->dev_root
->fs_info
;
158 limit
= btrfs_async_submit_limit(fs_info
);
159 limit
= limit
* 2 / 3;
162 spin_lock(&device
->io_lock
);
167 /* take all the bios off the list at once and process them
168 * later on (without the lock held). But, remember the
169 * tail and other pointers so the bios can be properly reinserted
170 * into the list if we hit congestion
172 if (!force_reg
&& device
->pending_sync_bios
.head
) {
173 pending_bios
= &device
->pending_sync_bios
;
176 pending_bios
= &device
->pending_bios
;
180 pending
= pending_bios
->head
;
181 tail
= pending_bios
->tail
;
182 WARN_ON(pending
&& !tail
);
185 * if pending was null this time around, no bios need processing
186 * at all and we can stop. Otherwise it'll loop back up again
187 * and do an additional check so no bios are missed.
189 * device->running_pending is used to synchronize with the
192 if (device
->pending_sync_bios
.head
== NULL
&&
193 device
->pending_bios
.head
== NULL
) {
195 device
->running_pending
= 0;
198 device
->running_pending
= 1;
201 pending_bios
->head
= NULL
;
202 pending_bios
->tail
= NULL
;
204 spin_unlock(&device
->io_lock
);
209 /* we want to work on both lists, but do more bios on the
210 * sync list than the regular list
213 pending_bios
!= &device
->pending_sync_bios
&&
214 device
->pending_sync_bios
.head
) ||
215 (num_run
> 64 && pending_bios
== &device
->pending_sync_bios
&&
216 device
->pending_bios
.head
)) {
217 spin_lock(&device
->io_lock
);
218 requeue_list(pending_bios
, pending
, tail
);
223 pending
= pending
->bi_next
;
225 atomic_dec(&fs_info
->nr_async_bios
);
227 if (atomic_read(&fs_info
->nr_async_bios
) < limit
&&
228 waitqueue_active(&fs_info
->async_submit_wait
))
229 wake_up(&fs_info
->async_submit_wait
);
231 BUG_ON(atomic_read(&cur
->bi_cnt
) == 0);
234 * if we're doing the sync list, record that our
235 * plug has some sync requests on it
237 * If we're doing the regular list and there are
238 * sync requests sitting around, unplug before
241 if (pending_bios
== &device
->pending_sync_bios
) {
243 } else if (sync_pending
) {
244 blk_finish_plug(&plug
);
245 blk_start_plug(&plug
);
249 submit_bio(cur
->bi_rw
, cur
);
256 * we made progress, there is more work to do and the bdi
257 * is now congested. Back off and let other work structs
260 if (pending
&& bdi_write_congested(bdi
) && batch_run
> 8 &&
261 fs_info
->fs_devices
->open_devices
> 1) {
262 struct io_context
*ioc
;
264 ioc
= current
->io_context
;
267 * the main goal here is that we don't want to
268 * block if we're going to be able to submit
269 * more requests without blocking.
271 * This code does two great things, it pokes into
272 * the elevator code from a filesystem _and_
273 * it makes assumptions about how batching works.
275 if (ioc
&& ioc
->nr_batch_requests
> 0 &&
276 time_before(jiffies
, ioc
->last_waited
+ HZ
/50UL) &&
278 ioc
->last_waited
== last_waited
)) {
280 * we want to go through our batch of
281 * requests and stop. So, we copy out
282 * the ioc->last_waited time and test
283 * against it before looping
285 last_waited
= ioc
->last_waited
;
290 spin_lock(&device
->io_lock
);
291 requeue_list(pending_bios
, pending
, tail
);
292 device
->running_pending
= 1;
294 spin_unlock(&device
->io_lock
);
295 btrfs_requeue_work(&device
->work
);
298 /* unplug every 64 requests just for good measure */
299 if (batch_run
% 64 == 0) {
300 blk_finish_plug(&plug
);
301 blk_start_plug(&plug
);
310 spin_lock(&device
->io_lock
);
311 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
313 spin_unlock(&device
->io_lock
);
316 blk_finish_plug(&plug
);
320 static void pending_bios_fn(struct btrfs_work
*work
)
322 struct btrfs_device
*device
;
324 device
= container_of(work
, struct btrfs_device
, work
);
325 run_scheduled_bios(device
);
328 static noinline
int device_list_add(const char *path
,
329 struct btrfs_super_block
*disk_super
,
330 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
332 struct btrfs_device
*device
;
333 struct btrfs_fs_devices
*fs_devices
;
334 u64 found_transid
= btrfs_super_generation(disk_super
);
337 fs_devices
= find_fsid(disk_super
->fsid
);
339 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
342 INIT_LIST_HEAD(&fs_devices
->devices
);
343 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
344 list_add(&fs_devices
->list
, &fs_uuids
);
345 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
346 fs_devices
->latest_devid
= devid
;
347 fs_devices
->latest_trans
= found_transid
;
348 mutex_init(&fs_devices
->device_list_mutex
);
351 device
= __find_device(&fs_devices
->devices
, devid
,
352 disk_super
->dev_item
.uuid
);
355 if (fs_devices
->opened
)
358 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
360 /* we can safely leave the fs_devices entry around */
363 device
->devid
= devid
;
364 device
->work
.func
= pending_bios_fn
;
365 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
367 spin_lock_init(&device
->io_lock
);
368 device
->name
= kstrdup(path
, GFP_NOFS
);
373 INIT_LIST_HEAD(&device
->dev_alloc_list
);
375 /* init readahead state */
376 spin_lock_init(&device
->reada_lock
);
377 device
->reada_curr_zone
= NULL
;
378 atomic_set(&device
->reada_in_flight
, 0);
379 device
->reada_next
= 0;
380 INIT_RADIX_TREE(&device
->reada_zones
, GFP_NOFS
& ~__GFP_WAIT
);
381 INIT_RADIX_TREE(&device
->reada_extents
, GFP_NOFS
& ~__GFP_WAIT
);
383 mutex_lock(&fs_devices
->device_list_mutex
);
384 list_add_rcu(&device
->dev_list
, &fs_devices
->devices
);
385 mutex_unlock(&fs_devices
->device_list_mutex
);
387 device
->fs_devices
= fs_devices
;
388 fs_devices
->num_devices
++;
389 } else if (!device
->name
|| strcmp(device
->name
, path
)) {
390 name
= kstrdup(path
, GFP_NOFS
);
395 if (device
->missing
) {
396 fs_devices
->missing_devices
--;
401 if (found_transid
> fs_devices
->latest_trans
) {
402 fs_devices
->latest_devid
= devid
;
403 fs_devices
->latest_trans
= found_transid
;
405 *fs_devices_ret
= fs_devices
;
409 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
411 struct btrfs_fs_devices
*fs_devices
;
412 struct btrfs_device
*device
;
413 struct btrfs_device
*orig_dev
;
415 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
417 return ERR_PTR(-ENOMEM
);
419 INIT_LIST_HEAD(&fs_devices
->devices
);
420 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
421 INIT_LIST_HEAD(&fs_devices
->list
);
422 mutex_init(&fs_devices
->device_list_mutex
);
423 fs_devices
->latest_devid
= orig
->latest_devid
;
424 fs_devices
->latest_trans
= orig
->latest_trans
;
425 memcpy(fs_devices
->fsid
, orig
->fsid
, sizeof(fs_devices
->fsid
));
427 /* We have held the volume lock, it is safe to get the devices. */
428 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
429 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
433 device
->name
= kstrdup(orig_dev
->name
, GFP_NOFS
);
439 device
->devid
= orig_dev
->devid
;
440 device
->work
.func
= pending_bios_fn
;
441 memcpy(device
->uuid
, orig_dev
->uuid
, sizeof(device
->uuid
));
442 spin_lock_init(&device
->io_lock
);
443 INIT_LIST_HEAD(&device
->dev_list
);
444 INIT_LIST_HEAD(&device
->dev_alloc_list
);
446 list_add(&device
->dev_list
, &fs_devices
->devices
);
447 device
->fs_devices
= fs_devices
;
448 fs_devices
->num_devices
++;
452 free_fs_devices(fs_devices
);
453 return ERR_PTR(-ENOMEM
);
456 int btrfs_close_extra_devices(struct btrfs_fs_devices
*fs_devices
)
458 struct btrfs_device
*device
, *next
;
460 mutex_lock(&uuid_mutex
);
462 /* This is the initialized path, it is safe to release the devices. */
463 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
464 if (device
->in_fs_metadata
)
468 blkdev_put(device
->bdev
, device
->mode
);
470 fs_devices
->open_devices
--;
472 if (device
->writeable
) {
473 list_del_init(&device
->dev_alloc_list
);
474 device
->writeable
= 0;
475 fs_devices
->rw_devices
--;
477 list_del_init(&device
->dev_list
);
478 fs_devices
->num_devices
--;
483 if (fs_devices
->seed
) {
484 fs_devices
= fs_devices
->seed
;
488 mutex_unlock(&uuid_mutex
);
492 static void __free_device(struct work_struct
*work
)
494 struct btrfs_device
*device
;
496 device
= container_of(work
, struct btrfs_device
, rcu_work
);
499 blkdev_put(device
->bdev
, device
->mode
);
505 static void free_device(struct rcu_head
*head
)
507 struct btrfs_device
*device
;
509 device
= container_of(head
, struct btrfs_device
, rcu
);
511 INIT_WORK(&device
->rcu_work
, __free_device
);
512 schedule_work(&device
->rcu_work
);
515 static int __btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
517 struct btrfs_device
*device
;
519 if (--fs_devices
->opened
> 0)
522 mutex_lock(&fs_devices
->device_list_mutex
);
523 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
524 struct btrfs_device
*new_device
;
527 fs_devices
->open_devices
--;
529 if (device
->writeable
) {
530 list_del_init(&device
->dev_alloc_list
);
531 fs_devices
->rw_devices
--;
534 if (device
->can_discard
)
535 fs_devices
->num_can_discard
--;
537 new_device
= kmalloc(sizeof(*new_device
), GFP_NOFS
);
539 memcpy(new_device
, device
, sizeof(*new_device
));
540 new_device
->name
= kstrdup(device
->name
, GFP_NOFS
);
541 BUG_ON(device
->name
&& !new_device
->name
);
542 new_device
->bdev
= NULL
;
543 new_device
->writeable
= 0;
544 new_device
->in_fs_metadata
= 0;
545 new_device
->can_discard
= 0;
546 list_replace_rcu(&device
->dev_list
, &new_device
->dev_list
);
548 call_rcu(&device
->rcu
, free_device
);
550 mutex_unlock(&fs_devices
->device_list_mutex
);
552 WARN_ON(fs_devices
->open_devices
);
553 WARN_ON(fs_devices
->rw_devices
);
554 fs_devices
->opened
= 0;
555 fs_devices
->seeding
= 0;
560 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
562 struct btrfs_fs_devices
*seed_devices
= NULL
;
565 mutex_lock(&uuid_mutex
);
566 ret
= __btrfs_close_devices(fs_devices
);
567 if (!fs_devices
->opened
) {
568 seed_devices
= fs_devices
->seed
;
569 fs_devices
->seed
= NULL
;
571 mutex_unlock(&uuid_mutex
);
573 while (seed_devices
) {
574 fs_devices
= seed_devices
;
575 seed_devices
= fs_devices
->seed
;
576 __btrfs_close_devices(fs_devices
);
577 free_fs_devices(fs_devices
);
582 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
583 fmode_t flags
, void *holder
)
585 struct request_queue
*q
;
586 struct block_device
*bdev
;
587 struct list_head
*head
= &fs_devices
->devices
;
588 struct btrfs_device
*device
;
589 struct block_device
*latest_bdev
= NULL
;
590 struct buffer_head
*bh
;
591 struct btrfs_super_block
*disk_super
;
592 u64 latest_devid
= 0;
593 u64 latest_transid
= 0;
600 list_for_each_entry(device
, head
, dev_list
) {
606 bdev
= blkdev_get_by_path(device
->name
, flags
, holder
);
608 printk(KERN_INFO
"open %s failed\n", device
->name
);
611 set_blocksize(bdev
, 4096);
613 bh
= btrfs_read_dev_super(bdev
);
617 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
618 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
619 if (devid
!= device
->devid
)
622 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
626 device
->generation
= btrfs_super_generation(disk_super
);
627 if (!latest_transid
|| device
->generation
> latest_transid
) {
628 latest_devid
= devid
;
629 latest_transid
= device
->generation
;
633 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
634 device
->writeable
= 0;
636 device
->writeable
= !bdev_read_only(bdev
);
640 q
= bdev_get_queue(bdev
);
641 if (blk_queue_discard(q
)) {
642 device
->can_discard
= 1;
643 fs_devices
->num_can_discard
++;
647 device
->in_fs_metadata
= 0;
648 device
->mode
= flags
;
650 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
651 fs_devices
->rotating
= 1;
653 fs_devices
->open_devices
++;
654 if (device
->writeable
) {
655 fs_devices
->rw_devices
++;
656 list_add(&device
->dev_alloc_list
,
657 &fs_devices
->alloc_list
);
665 blkdev_put(bdev
, flags
);
669 if (fs_devices
->open_devices
== 0) {
673 fs_devices
->seeding
= seeding
;
674 fs_devices
->opened
= 1;
675 fs_devices
->latest_bdev
= latest_bdev
;
676 fs_devices
->latest_devid
= latest_devid
;
677 fs_devices
->latest_trans
= latest_transid
;
678 fs_devices
->total_rw_bytes
= 0;
683 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
684 fmode_t flags
, void *holder
)
688 mutex_lock(&uuid_mutex
);
689 if (fs_devices
->opened
) {
690 fs_devices
->opened
++;
693 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
695 mutex_unlock(&uuid_mutex
);
699 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
700 struct btrfs_fs_devices
**fs_devices_ret
)
702 struct btrfs_super_block
*disk_super
;
703 struct block_device
*bdev
;
704 struct buffer_head
*bh
;
709 mutex_lock(&uuid_mutex
);
712 bdev
= blkdev_get_by_path(path
, flags
, holder
);
719 ret
= set_blocksize(bdev
, 4096);
722 bh
= btrfs_read_dev_super(bdev
);
727 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
728 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
729 transid
= btrfs_super_generation(disk_super
);
730 if (disk_super
->label
[0])
731 printk(KERN_INFO
"device label %s ", disk_super
->label
);
733 printk(KERN_INFO
"device fsid %pU ", disk_super
->fsid
);
734 printk(KERN_CONT
"devid %llu transid %llu %s\n",
735 (unsigned long long)devid
, (unsigned long long)transid
, path
);
736 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
740 blkdev_put(bdev
, flags
);
742 mutex_unlock(&uuid_mutex
);
746 /* helper to account the used device space in the range */
747 int btrfs_account_dev_extents_size(struct btrfs_device
*device
, u64 start
,
748 u64 end
, u64
*length
)
750 struct btrfs_key key
;
751 struct btrfs_root
*root
= device
->dev_root
;
752 struct btrfs_dev_extent
*dev_extent
;
753 struct btrfs_path
*path
;
757 struct extent_buffer
*l
;
761 if (start
>= device
->total_bytes
)
764 path
= btrfs_alloc_path();
769 key
.objectid
= device
->devid
;
771 key
.type
= BTRFS_DEV_EXTENT_KEY
;
773 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
777 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
784 slot
= path
->slots
[0];
785 if (slot
>= btrfs_header_nritems(l
)) {
786 ret
= btrfs_next_leaf(root
, path
);
794 btrfs_item_key_to_cpu(l
, &key
, slot
);
796 if (key
.objectid
< device
->devid
)
799 if (key
.objectid
> device
->devid
)
802 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
805 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
806 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
808 if (key
.offset
<= start
&& extent_end
> end
) {
809 *length
= end
- start
+ 1;
811 } else if (key
.offset
<= start
&& extent_end
> start
)
812 *length
+= extent_end
- start
;
813 else if (key
.offset
> start
&& extent_end
<= end
)
814 *length
+= extent_end
- key
.offset
;
815 else if (key
.offset
> start
&& key
.offset
<= end
) {
816 *length
+= end
- key
.offset
+ 1;
818 } else if (key
.offset
> end
)
826 btrfs_free_path(path
);
831 * find_free_dev_extent - find free space in the specified device
832 * @device: the device which we search the free space in
833 * @num_bytes: the size of the free space that we need
834 * @start: store the start of the free space.
835 * @len: the size of the free space. that we find, or the size of the max
836 * free space if we don't find suitable free space
838 * this uses a pretty simple search, the expectation is that it is
839 * called very infrequently and that a given device has a small number
842 * @start is used to store the start of the free space if we find. But if we
843 * don't find suitable free space, it will be used to store the start position
844 * of the max free space.
846 * @len is used to store the size of the free space that we find.
847 * But if we don't find suitable free space, it is used to store the size of
848 * the max free space.
850 int find_free_dev_extent(struct btrfs_device
*device
, u64 num_bytes
,
851 u64
*start
, u64
*len
)
853 struct btrfs_key key
;
854 struct btrfs_root
*root
= device
->dev_root
;
855 struct btrfs_dev_extent
*dev_extent
;
856 struct btrfs_path
*path
;
862 u64 search_end
= device
->total_bytes
;
865 struct extent_buffer
*l
;
867 /* FIXME use last free of some kind */
869 /* we don't want to overwrite the superblock on the drive,
870 * so we make sure to start at an offset of at least 1MB
872 search_start
= max(root
->fs_info
->alloc_start
, 1024ull * 1024);
874 max_hole_start
= search_start
;
878 if (search_start
>= search_end
) {
883 path
= btrfs_alloc_path();
890 key
.objectid
= device
->devid
;
891 key
.offset
= search_start
;
892 key
.type
= BTRFS_DEV_EXTENT_KEY
;
894 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
898 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
905 slot
= path
->slots
[0];
906 if (slot
>= btrfs_header_nritems(l
)) {
907 ret
= btrfs_next_leaf(root
, path
);
915 btrfs_item_key_to_cpu(l
, &key
, slot
);
917 if (key
.objectid
< device
->devid
)
920 if (key
.objectid
> device
->devid
)
923 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
926 if (key
.offset
> search_start
) {
927 hole_size
= key
.offset
- search_start
;
929 if (hole_size
> max_hole_size
) {
930 max_hole_start
= search_start
;
931 max_hole_size
= hole_size
;
935 * If this free space is greater than which we need,
936 * it must be the max free space that we have found
937 * until now, so max_hole_start must point to the start
938 * of this free space and the length of this free space
939 * is stored in max_hole_size. Thus, we return
940 * max_hole_start and max_hole_size and go back to the
943 if (hole_size
>= num_bytes
) {
949 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
950 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
952 if (extent_end
> search_start
)
953 search_start
= extent_end
;
960 * At this point, search_start should be the end of
961 * allocated dev extents, and when shrinking the device,
962 * search_end may be smaller than search_start.
964 if (search_end
> search_start
)
965 hole_size
= search_end
- search_start
;
967 if (hole_size
> max_hole_size
) {
968 max_hole_start
= search_start
;
969 max_hole_size
= hole_size
;
973 if (hole_size
< num_bytes
)
979 btrfs_free_path(path
);
981 *start
= max_hole_start
;
983 *len
= max_hole_size
;
987 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
988 struct btrfs_device
*device
,
992 struct btrfs_path
*path
;
993 struct btrfs_root
*root
= device
->dev_root
;
994 struct btrfs_key key
;
995 struct btrfs_key found_key
;
996 struct extent_buffer
*leaf
= NULL
;
997 struct btrfs_dev_extent
*extent
= NULL
;
999 path
= btrfs_alloc_path();
1003 key
.objectid
= device
->devid
;
1005 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1007 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1009 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
1010 BTRFS_DEV_EXTENT_KEY
);
1013 leaf
= path
->nodes
[0];
1014 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1015 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1016 struct btrfs_dev_extent
);
1017 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
1018 btrfs_dev_extent_length(leaf
, extent
) < start
);
1020 btrfs_release_path(path
);
1022 } else if (ret
== 0) {
1023 leaf
= path
->nodes
[0];
1024 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1025 struct btrfs_dev_extent
);
1029 if (device
->bytes_used
> 0) {
1030 u64 len
= btrfs_dev_extent_length(leaf
, extent
);
1031 device
->bytes_used
-= len
;
1032 spin_lock(&root
->fs_info
->free_chunk_lock
);
1033 root
->fs_info
->free_chunk_space
+= len
;
1034 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1036 ret
= btrfs_del_item(trans
, root
, path
);
1039 btrfs_free_path(path
);
1043 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
1044 struct btrfs_device
*device
,
1045 u64 chunk_tree
, u64 chunk_objectid
,
1046 u64 chunk_offset
, u64 start
, u64 num_bytes
)
1049 struct btrfs_path
*path
;
1050 struct btrfs_root
*root
= device
->dev_root
;
1051 struct btrfs_dev_extent
*extent
;
1052 struct extent_buffer
*leaf
;
1053 struct btrfs_key key
;
1055 WARN_ON(!device
->in_fs_metadata
);
1056 path
= btrfs_alloc_path();
1060 key
.objectid
= device
->devid
;
1062 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1063 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1067 leaf
= path
->nodes
[0];
1068 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1069 struct btrfs_dev_extent
);
1070 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
1071 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
1072 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1074 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1075 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
1078 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1079 btrfs_mark_buffer_dirty(leaf
);
1080 btrfs_free_path(path
);
1084 static noinline
int find_next_chunk(struct btrfs_root
*root
,
1085 u64 objectid
, u64
*offset
)
1087 struct btrfs_path
*path
;
1089 struct btrfs_key key
;
1090 struct btrfs_chunk
*chunk
;
1091 struct btrfs_key found_key
;
1093 path
= btrfs_alloc_path();
1097 key
.objectid
= objectid
;
1098 key
.offset
= (u64
)-1;
1099 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1101 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1107 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
1111 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1113 if (found_key
.objectid
!= objectid
)
1116 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1117 struct btrfs_chunk
);
1118 *offset
= found_key
.offset
+
1119 btrfs_chunk_length(path
->nodes
[0], chunk
);
1124 btrfs_free_path(path
);
1128 static noinline
int find_next_devid(struct btrfs_root
*root
, u64
*objectid
)
1131 struct btrfs_key key
;
1132 struct btrfs_key found_key
;
1133 struct btrfs_path
*path
;
1135 root
= root
->fs_info
->chunk_root
;
1137 path
= btrfs_alloc_path();
1141 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1142 key
.type
= BTRFS_DEV_ITEM_KEY
;
1143 key
.offset
= (u64
)-1;
1145 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1151 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
1152 BTRFS_DEV_ITEM_KEY
);
1156 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1158 *objectid
= found_key
.offset
+ 1;
1162 btrfs_free_path(path
);
1167 * the device information is stored in the chunk root
1168 * the btrfs_device struct should be fully filled in
1170 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
1171 struct btrfs_root
*root
,
1172 struct btrfs_device
*device
)
1175 struct btrfs_path
*path
;
1176 struct btrfs_dev_item
*dev_item
;
1177 struct extent_buffer
*leaf
;
1178 struct btrfs_key key
;
1181 root
= root
->fs_info
->chunk_root
;
1183 path
= btrfs_alloc_path();
1187 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1188 key
.type
= BTRFS_DEV_ITEM_KEY
;
1189 key
.offset
= device
->devid
;
1191 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1196 leaf
= path
->nodes
[0];
1197 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1199 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1200 btrfs_set_device_generation(leaf
, dev_item
, 0);
1201 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1202 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1203 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1204 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1205 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1206 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1207 btrfs_set_device_group(leaf
, dev_item
, 0);
1208 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1209 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1210 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1212 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1213 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1214 ptr
= (unsigned long)btrfs_device_fsid(dev_item
);
1215 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1216 btrfs_mark_buffer_dirty(leaf
);
1220 btrfs_free_path(path
);
1224 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1225 struct btrfs_device
*device
)
1228 struct btrfs_path
*path
;
1229 struct btrfs_key key
;
1230 struct btrfs_trans_handle
*trans
;
1232 root
= root
->fs_info
->chunk_root
;
1234 path
= btrfs_alloc_path();
1238 trans
= btrfs_start_transaction(root
, 0);
1239 if (IS_ERR(trans
)) {
1240 btrfs_free_path(path
);
1241 return PTR_ERR(trans
);
1243 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1244 key
.type
= BTRFS_DEV_ITEM_KEY
;
1245 key
.offset
= device
->devid
;
1248 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1257 ret
= btrfs_del_item(trans
, root
, path
);
1261 btrfs_free_path(path
);
1262 unlock_chunks(root
);
1263 btrfs_commit_transaction(trans
, root
);
1267 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1269 struct btrfs_device
*device
;
1270 struct btrfs_device
*next_device
;
1271 struct block_device
*bdev
;
1272 struct buffer_head
*bh
= NULL
;
1273 struct btrfs_super_block
*disk_super
;
1274 struct btrfs_fs_devices
*cur_devices
;
1280 bool clear_super
= false;
1282 mutex_lock(&uuid_mutex
);
1283 mutex_lock(&root
->fs_info
->volume_mutex
);
1285 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1286 root
->fs_info
->avail_system_alloc_bits
|
1287 root
->fs_info
->avail_metadata_alloc_bits
;
1289 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) &&
1290 root
->fs_info
->fs_devices
->num_devices
<= 4) {
1291 printk(KERN_ERR
"btrfs: unable to go below four devices "
1297 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) &&
1298 root
->fs_info
->fs_devices
->num_devices
<= 2) {
1299 printk(KERN_ERR
"btrfs: unable to go below two "
1300 "devices on raid1\n");
1305 if (strcmp(device_path
, "missing") == 0) {
1306 struct list_head
*devices
;
1307 struct btrfs_device
*tmp
;
1310 devices
= &root
->fs_info
->fs_devices
->devices
;
1312 * It is safe to read the devices since the volume_mutex
1315 list_for_each_entry(tmp
, devices
, dev_list
) {
1316 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1325 printk(KERN_ERR
"btrfs: no missing devices found to "
1330 bdev
= blkdev_get_by_path(device_path
, FMODE_READ
| FMODE_EXCL
,
1331 root
->fs_info
->bdev_holder
);
1333 ret
= PTR_ERR(bdev
);
1337 set_blocksize(bdev
, 4096);
1338 bh
= btrfs_read_dev_super(bdev
);
1343 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1344 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1345 dev_uuid
= disk_super
->dev_item
.uuid
;
1346 device
= btrfs_find_device(root
, devid
, dev_uuid
,
1354 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1355 printk(KERN_ERR
"btrfs: unable to remove the only writeable "
1361 if (device
->writeable
) {
1363 list_del_init(&device
->dev_alloc_list
);
1364 unlock_chunks(root
);
1365 root
->fs_info
->fs_devices
->rw_devices
--;
1369 ret
= btrfs_shrink_device(device
, 0);
1373 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1377 spin_lock(&root
->fs_info
->free_chunk_lock
);
1378 root
->fs_info
->free_chunk_space
= device
->total_bytes
-
1380 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1382 device
->in_fs_metadata
= 0;
1383 btrfs_scrub_cancel_dev(root
, device
);
1386 * the device list mutex makes sure that we don't change
1387 * the device list while someone else is writing out all
1388 * the device supers.
1391 cur_devices
= device
->fs_devices
;
1392 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1393 list_del_rcu(&device
->dev_list
);
1395 device
->fs_devices
->num_devices
--;
1397 if (device
->missing
)
1398 root
->fs_info
->fs_devices
->missing_devices
--;
1400 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1401 struct btrfs_device
, dev_list
);
1402 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1403 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1404 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1405 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1408 device
->fs_devices
->open_devices
--;
1410 call_rcu(&device
->rcu
, free_device
);
1411 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1413 num_devices
= btrfs_super_num_devices(root
->fs_info
->super_copy
) - 1;
1414 btrfs_set_super_num_devices(root
->fs_info
->super_copy
, num_devices
);
1416 if (cur_devices
->open_devices
== 0) {
1417 struct btrfs_fs_devices
*fs_devices
;
1418 fs_devices
= root
->fs_info
->fs_devices
;
1419 while (fs_devices
) {
1420 if (fs_devices
->seed
== cur_devices
)
1422 fs_devices
= fs_devices
->seed
;
1424 fs_devices
->seed
= cur_devices
->seed
;
1425 cur_devices
->seed
= NULL
;
1427 __btrfs_close_devices(cur_devices
);
1428 unlock_chunks(root
);
1429 free_fs_devices(cur_devices
);
1433 * at this point, the device is zero sized. We want to
1434 * remove it from the devices list and zero out the old super
1437 /* make sure this device isn't detected as part of
1440 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1441 set_buffer_dirty(bh
);
1442 sync_dirty_buffer(bh
);
1451 blkdev_put(bdev
, FMODE_READ
| FMODE_EXCL
);
1453 mutex_unlock(&root
->fs_info
->volume_mutex
);
1454 mutex_unlock(&uuid_mutex
);
1457 if (device
->writeable
) {
1459 list_add(&device
->dev_alloc_list
,
1460 &root
->fs_info
->fs_devices
->alloc_list
);
1461 unlock_chunks(root
);
1462 root
->fs_info
->fs_devices
->rw_devices
++;
1468 * does all the dirty work required for changing file system's UUID.
1470 static int btrfs_prepare_sprout(struct btrfs_root
*root
)
1472 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1473 struct btrfs_fs_devices
*old_devices
;
1474 struct btrfs_fs_devices
*seed_devices
;
1475 struct btrfs_super_block
*disk_super
= root
->fs_info
->super_copy
;
1476 struct btrfs_device
*device
;
1479 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1480 if (!fs_devices
->seeding
)
1483 seed_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
1487 old_devices
= clone_fs_devices(fs_devices
);
1488 if (IS_ERR(old_devices
)) {
1489 kfree(seed_devices
);
1490 return PTR_ERR(old_devices
);
1493 list_add(&old_devices
->list
, &fs_uuids
);
1495 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1496 seed_devices
->opened
= 1;
1497 INIT_LIST_HEAD(&seed_devices
->devices
);
1498 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1499 mutex_init(&seed_devices
->device_list_mutex
);
1501 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1502 list_splice_init_rcu(&fs_devices
->devices
, &seed_devices
->devices
,
1504 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1506 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1507 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1508 device
->fs_devices
= seed_devices
;
1511 fs_devices
->seeding
= 0;
1512 fs_devices
->num_devices
= 0;
1513 fs_devices
->open_devices
= 0;
1514 fs_devices
->seed
= seed_devices
;
1516 generate_random_uuid(fs_devices
->fsid
);
1517 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1518 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1519 super_flags
= btrfs_super_flags(disk_super
) &
1520 ~BTRFS_SUPER_FLAG_SEEDING
;
1521 btrfs_set_super_flags(disk_super
, super_flags
);
1527 * strore the expected generation for seed devices in device items.
1529 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
1530 struct btrfs_root
*root
)
1532 struct btrfs_path
*path
;
1533 struct extent_buffer
*leaf
;
1534 struct btrfs_dev_item
*dev_item
;
1535 struct btrfs_device
*device
;
1536 struct btrfs_key key
;
1537 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1538 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1542 path
= btrfs_alloc_path();
1546 root
= root
->fs_info
->chunk_root
;
1547 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1549 key
.type
= BTRFS_DEV_ITEM_KEY
;
1552 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1556 leaf
= path
->nodes
[0];
1558 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1559 ret
= btrfs_next_leaf(root
, path
);
1564 leaf
= path
->nodes
[0];
1565 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1566 btrfs_release_path(path
);
1570 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1571 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
1572 key
.type
!= BTRFS_DEV_ITEM_KEY
)
1575 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1576 struct btrfs_dev_item
);
1577 devid
= btrfs_device_id(leaf
, dev_item
);
1578 read_extent_buffer(leaf
, dev_uuid
,
1579 (unsigned long)btrfs_device_uuid(dev_item
),
1581 read_extent_buffer(leaf
, fs_uuid
,
1582 (unsigned long)btrfs_device_fsid(dev_item
),
1584 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
1587 if (device
->fs_devices
->seeding
) {
1588 btrfs_set_device_generation(leaf
, dev_item
,
1589 device
->generation
);
1590 btrfs_mark_buffer_dirty(leaf
);
1598 btrfs_free_path(path
);
1602 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
1604 struct request_queue
*q
;
1605 struct btrfs_trans_handle
*trans
;
1606 struct btrfs_device
*device
;
1607 struct block_device
*bdev
;
1608 struct list_head
*devices
;
1609 struct super_block
*sb
= root
->fs_info
->sb
;
1611 int seeding_dev
= 0;
1614 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
1617 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
1618 root
->fs_info
->bdev_holder
);
1620 return PTR_ERR(bdev
);
1622 if (root
->fs_info
->fs_devices
->seeding
) {
1624 down_write(&sb
->s_umount
);
1625 mutex_lock(&uuid_mutex
);
1628 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
1629 mutex_lock(&root
->fs_info
->volume_mutex
);
1631 devices
= &root
->fs_info
->fs_devices
->devices
;
1633 * we have the volume lock, so we don't need the extra
1634 * device list mutex while reading the list here.
1636 list_for_each_entry(device
, devices
, dev_list
) {
1637 if (device
->bdev
== bdev
) {
1643 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1645 /* we can safely leave the fs_devices entry around */
1650 device
->name
= kstrdup(device_path
, GFP_NOFS
);
1651 if (!device
->name
) {
1657 ret
= find_next_devid(root
, &device
->devid
);
1659 kfree(device
->name
);
1664 trans
= btrfs_start_transaction(root
, 0);
1665 if (IS_ERR(trans
)) {
1666 kfree(device
->name
);
1668 ret
= PTR_ERR(trans
);
1674 q
= bdev_get_queue(bdev
);
1675 if (blk_queue_discard(q
))
1676 device
->can_discard
= 1;
1677 device
->writeable
= 1;
1678 device
->work
.func
= pending_bios_fn
;
1679 generate_random_uuid(device
->uuid
);
1680 spin_lock_init(&device
->io_lock
);
1681 device
->generation
= trans
->transid
;
1682 device
->io_width
= root
->sectorsize
;
1683 device
->io_align
= root
->sectorsize
;
1684 device
->sector_size
= root
->sectorsize
;
1685 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1686 device
->disk_total_bytes
= device
->total_bytes
;
1687 device
->dev_root
= root
->fs_info
->dev_root
;
1688 device
->bdev
= bdev
;
1689 device
->in_fs_metadata
= 1;
1690 device
->mode
= FMODE_EXCL
;
1691 set_blocksize(device
->bdev
, 4096);
1694 sb
->s_flags
&= ~MS_RDONLY
;
1695 ret
= btrfs_prepare_sprout(root
);
1699 device
->fs_devices
= root
->fs_info
->fs_devices
;
1702 * we don't want write_supers to jump in here with our device
1705 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1706 list_add_rcu(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
1707 list_add(&device
->dev_alloc_list
,
1708 &root
->fs_info
->fs_devices
->alloc_list
);
1709 root
->fs_info
->fs_devices
->num_devices
++;
1710 root
->fs_info
->fs_devices
->open_devices
++;
1711 root
->fs_info
->fs_devices
->rw_devices
++;
1712 if (device
->can_discard
)
1713 root
->fs_info
->fs_devices
->num_can_discard
++;
1714 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
1716 spin_lock(&root
->fs_info
->free_chunk_lock
);
1717 root
->fs_info
->free_chunk_space
+= device
->total_bytes
;
1718 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1720 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
1721 root
->fs_info
->fs_devices
->rotating
= 1;
1723 total_bytes
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
1724 btrfs_set_super_total_bytes(root
->fs_info
->super_copy
,
1725 total_bytes
+ device
->total_bytes
);
1727 total_bytes
= btrfs_super_num_devices(root
->fs_info
->super_copy
);
1728 btrfs_set_super_num_devices(root
->fs_info
->super_copy
,
1730 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1733 ret
= init_first_rw_device(trans
, root
, device
);
1735 ret
= btrfs_finish_sprout(trans
, root
);
1738 ret
= btrfs_add_device(trans
, root
, device
);
1742 * we've got more storage, clear any full flags on the space
1745 btrfs_clear_space_info_full(root
->fs_info
);
1747 unlock_chunks(root
);
1748 btrfs_commit_transaction(trans
, root
);
1751 mutex_unlock(&uuid_mutex
);
1752 up_write(&sb
->s_umount
);
1754 ret
= btrfs_relocate_sys_chunks(root
);
1758 mutex_unlock(&root
->fs_info
->volume_mutex
);
1761 blkdev_put(bdev
, FMODE_EXCL
);
1763 mutex_unlock(&uuid_mutex
);
1764 up_write(&sb
->s_umount
);
1769 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
1770 struct btrfs_device
*device
)
1773 struct btrfs_path
*path
;
1774 struct btrfs_root
*root
;
1775 struct btrfs_dev_item
*dev_item
;
1776 struct extent_buffer
*leaf
;
1777 struct btrfs_key key
;
1779 root
= device
->dev_root
->fs_info
->chunk_root
;
1781 path
= btrfs_alloc_path();
1785 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1786 key
.type
= BTRFS_DEV_ITEM_KEY
;
1787 key
.offset
= device
->devid
;
1789 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1798 leaf
= path
->nodes
[0];
1799 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1801 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1802 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1803 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1804 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1805 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1806 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
1807 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1808 btrfs_mark_buffer_dirty(leaf
);
1811 btrfs_free_path(path
);
1815 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1816 struct btrfs_device
*device
, u64 new_size
)
1818 struct btrfs_super_block
*super_copy
=
1819 device
->dev_root
->fs_info
->super_copy
;
1820 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1821 u64 diff
= new_size
- device
->total_bytes
;
1823 if (!device
->writeable
)
1825 if (new_size
<= device
->total_bytes
)
1828 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
1829 device
->fs_devices
->total_rw_bytes
+= diff
;
1831 device
->total_bytes
= new_size
;
1832 device
->disk_total_bytes
= new_size
;
1833 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
1835 return btrfs_update_device(trans
, device
);
1838 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1839 struct btrfs_device
*device
, u64 new_size
)
1842 lock_chunks(device
->dev_root
);
1843 ret
= __btrfs_grow_device(trans
, device
, new_size
);
1844 unlock_chunks(device
->dev_root
);
1848 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
1849 struct btrfs_root
*root
,
1850 u64 chunk_tree
, u64 chunk_objectid
,
1854 struct btrfs_path
*path
;
1855 struct btrfs_key key
;
1857 root
= root
->fs_info
->chunk_root
;
1858 path
= btrfs_alloc_path();
1862 key
.objectid
= chunk_objectid
;
1863 key
.offset
= chunk_offset
;
1864 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1866 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1869 ret
= btrfs_del_item(trans
, root
, path
);
1871 btrfs_free_path(path
);
1875 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
1878 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
1879 struct btrfs_disk_key
*disk_key
;
1880 struct btrfs_chunk
*chunk
;
1887 struct btrfs_key key
;
1889 array_size
= btrfs_super_sys_array_size(super_copy
);
1891 ptr
= super_copy
->sys_chunk_array
;
1894 while (cur
< array_size
) {
1895 disk_key
= (struct btrfs_disk_key
*)ptr
;
1896 btrfs_disk_key_to_cpu(&key
, disk_key
);
1898 len
= sizeof(*disk_key
);
1900 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1901 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
1902 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
1903 len
+= btrfs_chunk_item_size(num_stripes
);
1908 if (key
.objectid
== chunk_objectid
&&
1909 key
.offset
== chunk_offset
) {
1910 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
1912 btrfs_set_super_sys_array_size(super_copy
, array_size
);
1921 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
1922 u64 chunk_tree
, u64 chunk_objectid
,
1925 struct extent_map_tree
*em_tree
;
1926 struct btrfs_root
*extent_root
;
1927 struct btrfs_trans_handle
*trans
;
1928 struct extent_map
*em
;
1929 struct map_lookup
*map
;
1933 root
= root
->fs_info
->chunk_root
;
1934 extent_root
= root
->fs_info
->extent_root
;
1935 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
1937 ret
= btrfs_can_relocate(extent_root
, chunk_offset
);
1941 /* step one, relocate all the extents inside this chunk */
1942 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
1946 trans
= btrfs_start_transaction(root
, 0);
1947 BUG_ON(IS_ERR(trans
));
1952 * step two, delete the device extents and the
1953 * chunk tree entries
1955 read_lock(&em_tree
->lock
);
1956 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
1957 read_unlock(&em_tree
->lock
);
1959 BUG_ON(em
->start
> chunk_offset
||
1960 em
->start
+ em
->len
< chunk_offset
);
1961 map
= (struct map_lookup
*)em
->bdev
;
1963 for (i
= 0; i
< map
->num_stripes
; i
++) {
1964 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
1965 map
->stripes
[i
].physical
);
1968 if (map
->stripes
[i
].dev
) {
1969 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
1973 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
1978 trace_btrfs_chunk_free(root
, map
, chunk_offset
, em
->len
);
1980 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1981 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
1985 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
1988 write_lock(&em_tree
->lock
);
1989 remove_extent_mapping(em_tree
, em
);
1990 write_unlock(&em_tree
->lock
);
1995 /* once for the tree */
1996 free_extent_map(em
);
1998 free_extent_map(em
);
2000 unlock_chunks(root
);
2001 btrfs_end_transaction(trans
, root
);
2005 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
2007 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
2008 struct btrfs_path
*path
;
2009 struct extent_buffer
*leaf
;
2010 struct btrfs_chunk
*chunk
;
2011 struct btrfs_key key
;
2012 struct btrfs_key found_key
;
2013 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
2015 bool retried
= false;
2019 path
= btrfs_alloc_path();
2024 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2025 key
.offset
= (u64
)-1;
2026 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2029 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2034 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
2041 leaf
= path
->nodes
[0];
2042 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2044 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
2045 struct btrfs_chunk
);
2046 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2047 btrfs_release_path(path
);
2049 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2050 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
2059 if (found_key
.offset
== 0)
2061 key
.offset
= found_key
.offset
- 1;
2064 if (failed
&& !retried
) {
2068 } else if (failed
&& retried
) {
2073 btrfs_free_path(path
);
2077 static u64
div_factor(u64 num
, int factor
)
2086 int btrfs_balance(struct btrfs_root
*dev_root
)
2089 struct list_head
*devices
= &dev_root
->fs_info
->fs_devices
->devices
;
2090 struct btrfs_device
*device
;
2093 struct btrfs_path
*path
;
2094 struct btrfs_key key
;
2095 struct btrfs_root
*chunk_root
= dev_root
->fs_info
->chunk_root
;
2096 struct btrfs_trans_handle
*trans
;
2097 struct btrfs_key found_key
;
2099 if (dev_root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
2102 if (!capable(CAP_SYS_ADMIN
))
2105 mutex_lock(&dev_root
->fs_info
->volume_mutex
);
2106 dev_root
= dev_root
->fs_info
->dev_root
;
2108 /* step one make some room on all the devices */
2109 list_for_each_entry(device
, devices
, dev_list
) {
2110 old_size
= device
->total_bytes
;
2111 size_to_free
= div_factor(old_size
, 1);
2112 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
2113 if (!device
->writeable
||
2114 device
->total_bytes
- device
->bytes_used
> size_to_free
)
2117 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
2122 trans
= btrfs_start_transaction(dev_root
, 0);
2123 BUG_ON(IS_ERR(trans
));
2125 ret
= btrfs_grow_device(trans
, device
, old_size
);
2128 btrfs_end_transaction(trans
, dev_root
);
2131 /* step two, relocate all the chunks */
2132 path
= btrfs_alloc_path();
2137 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2138 key
.offset
= (u64
)-1;
2139 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2142 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2147 * this shouldn't happen, it means the last relocate
2153 ret
= btrfs_previous_item(chunk_root
, path
, 0,
2154 BTRFS_CHUNK_ITEM_KEY
);
2158 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
2160 if (found_key
.objectid
!= key
.objectid
)
2163 /* chunk zero is special */
2164 if (found_key
.offset
== 0)
2167 btrfs_release_path(path
);
2168 ret
= btrfs_relocate_chunk(chunk_root
,
2169 chunk_root
->root_key
.objectid
,
2172 if (ret
&& ret
!= -ENOSPC
)
2174 key
.offset
= found_key
.offset
- 1;
2178 btrfs_free_path(path
);
2179 mutex_unlock(&dev_root
->fs_info
->volume_mutex
);
2184 * shrinking a device means finding all of the device extents past
2185 * the new size, and then following the back refs to the chunks.
2186 * The chunk relocation code actually frees the device extent
2188 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
2190 struct btrfs_trans_handle
*trans
;
2191 struct btrfs_root
*root
= device
->dev_root
;
2192 struct btrfs_dev_extent
*dev_extent
= NULL
;
2193 struct btrfs_path
*path
;
2201 bool retried
= false;
2202 struct extent_buffer
*l
;
2203 struct btrfs_key key
;
2204 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
2205 u64 old_total
= btrfs_super_total_bytes(super_copy
);
2206 u64 old_size
= device
->total_bytes
;
2207 u64 diff
= device
->total_bytes
- new_size
;
2209 if (new_size
>= device
->total_bytes
)
2212 path
= btrfs_alloc_path();
2220 device
->total_bytes
= new_size
;
2221 if (device
->writeable
) {
2222 device
->fs_devices
->total_rw_bytes
-= diff
;
2223 spin_lock(&root
->fs_info
->free_chunk_lock
);
2224 root
->fs_info
->free_chunk_space
-= diff
;
2225 spin_unlock(&root
->fs_info
->free_chunk_lock
);
2227 unlock_chunks(root
);
2230 key
.objectid
= device
->devid
;
2231 key
.offset
= (u64
)-1;
2232 key
.type
= BTRFS_DEV_EXTENT_KEY
;
2235 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2239 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
2244 btrfs_release_path(path
);
2249 slot
= path
->slots
[0];
2250 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
2252 if (key
.objectid
!= device
->devid
) {
2253 btrfs_release_path(path
);
2257 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
2258 length
= btrfs_dev_extent_length(l
, dev_extent
);
2260 if (key
.offset
+ length
<= new_size
) {
2261 btrfs_release_path(path
);
2265 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
2266 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
2267 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
2268 btrfs_release_path(path
);
2270 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
2272 if (ret
&& ret
!= -ENOSPC
)
2279 if (failed
&& !retried
) {
2283 } else if (failed
&& retried
) {
2287 device
->total_bytes
= old_size
;
2288 if (device
->writeable
)
2289 device
->fs_devices
->total_rw_bytes
+= diff
;
2290 spin_lock(&root
->fs_info
->free_chunk_lock
);
2291 root
->fs_info
->free_chunk_space
+= diff
;
2292 spin_unlock(&root
->fs_info
->free_chunk_lock
);
2293 unlock_chunks(root
);
2297 /* Shrinking succeeded, else we would be at "done". */
2298 trans
= btrfs_start_transaction(root
, 0);
2299 if (IS_ERR(trans
)) {
2300 ret
= PTR_ERR(trans
);
2306 device
->disk_total_bytes
= new_size
;
2307 /* Now btrfs_update_device() will change the on-disk size. */
2308 ret
= btrfs_update_device(trans
, device
);
2310 unlock_chunks(root
);
2311 btrfs_end_transaction(trans
, root
);
2314 WARN_ON(diff
> old_total
);
2315 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
2316 unlock_chunks(root
);
2317 btrfs_end_transaction(trans
, root
);
2319 btrfs_free_path(path
);
2323 static int btrfs_add_system_chunk(struct btrfs_root
*root
,
2324 struct btrfs_key
*key
,
2325 struct btrfs_chunk
*chunk
, int item_size
)
2327 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
2328 struct btrfs_disk_key disk_key
;
2332 array_size
= btrfs_super_sys_array_size(super_copy
);
2333 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
2336 ptr
= super_copy
->sys_chunk_array
+ array_size
;
2337 btrfs_cpu_key_to_disk(&disk_key
, key
);
2338 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
2339 ptr
+= sizeof(disk_key
);
2340 memcpy(ptr
, chunk
, item_size
);
2341 item_size
+= sizeof(disk_key
);
2342 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
2347 * sort the devices in descending order by max_avail, total_avail
2349 static int btrfs_cmp_device_info(const void *a
, const void *b
)
2351 const struct btrfs_device_info
*di_a
= a
;
2352 const struct btrfs_device_info
*di_b
= b
;
2354 if (di_a
->max_avail
> di_b
->max_avail
)
2356 if (di_a
->max_avail
< di_b
->max_avail
)
2358 if (di_a
->total_avail
> di_b
->total_avail
)
2360 if (di_a
->total_avail
< di_b
->total_avail
)
2365 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
2366 struct btrfs_root
*extent_root
,
2367 struct map_lookup
**map_ret
,
2368 u64
*num_bytes_out
, u64
*stripe_size_out
,
2369 u64 start
, u64 type
)
2371 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
2372 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
2373 struct list_head
*cur
;
2374 struct map_lookup
*map
= NULL
;
2375 struct extent_map_tree
*em_tree
;
2376 struct extent_map
*em
;
2377 struct btrfs_device_info
*devices_info
= NULL
;
2379 int num_stripes
; /* total number of stripes to allocate */
2380 int sub_stripes
; /* sub_stripes info for map */
2381 int dev_stripes
; /* stripes per dev */
2382 int devs_max
; /* max devs to use */
2383 int devs_min
; /* min devs needed */
2384 int devs_increment
; /* ndevs has to be a multiple of this */
2385 int ncopies
; /* how many copies to data has */
2387 u64 max_stripe_size
;
2395 if ((type
& BTRFS_BLOCK_GROUP_RAID1
) &&
2396 (type
& BTRFS_BLOCK_GROUP_DUP
)) {
2398 type
&= ~BTRFS_BLOCK_GROUP_DUP
;
2401 if (list_empty(&fs_devices
->alloc_list
))
2408 devs_max
= 0; /* 0 == as many as possible */
2412 * define the properties of each RAID type.
2413 * FIXME: move this to a global table and use it in all RAID
2416 if (type
& (BTRFS_BLOCK_GROUP_DUP
)) {
2420 } else if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
2422 } else if (type
& (BTRFS_BLOCK_GROUP_RAID1
)) {
2427 } else if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
2436 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
2437 max_stripe_size
= 1024 * 1024 * 1024;
2438 max_chunk_size
= 10 * max_stripe_size
;
2439 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
2440 max_stripe_size
= 256 * 1024 * 1024;
2441 max_chunk_size
= max_stripe_size
;
2442 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2443 max_stripe_size
= 8 * 1024 * 1024;
2444 max_chunk_size
= 2 * max_stripe_size
;
2446 printk(KERN_ERR
"btrfs: invalid chunk type 0x%llx requested\n",
2451 /* we don't want a chunk larger than 10% of writeable space */
2452 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
2455 devices_info
= kzalloc(sizeof(*devices_info
) * fs_devices
->rw_devices
,
2460 cur
= fs_devices
->alloc_list
.next
;
2463 * in the first pass through the devices list, we gather information
2464 * about the available holes on each device.
2467 while (cur
!= &fs_devices
->alloc_list
) {
2468 struct btrfs_device
*device
;
2472 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
2476 if (!device
->writeable
) {
2478 "btrfs: read-only device in alloc_list\n");
2483 if (!device
->in_fs_metadata
)
2486 if (device
->total_bytes
> device
->bytes_used
)
2487 total_avail
= device
->total_bytes
- device
->bytes_used
;
2491 /* If there is no space on this device, skip it. */
2492 if (total_avail
== 0)
2495 ret
= find_free_dev_extent(device
,
2496 max_stripe_size
* dev_stripes
,
2497 &dev_offset
, &max_avail
);
2498 if (ret
&& ret
!= -ENOSPC
)
2502 max_avail
= max_stripe_size
* dev_stripes
;
2504 if (max_avail
< BTRFS_STRIPE_LEN
* dev_stripes
)
2507 devices_info
[ndevs
].dev_offset
= dev_offset
;
2508 devices_info
[ndevs
].max_avail
= max_avail
;
2509 devices_info
[ndevs
].total_avail
= total_avail
;
2510 devices_info
[ndevs
].dev
= device
;
2515 * now sort the devices by hole size / available space
2517 sort(devices_info
, ndevs
, sizeof(struct btrfs_device_info
),
2518 btrfs_cmp_device_info
, NULL
);
2520 /* round down to number of usable stripes */
2521 ndevs
-= ndevs
% devs_increment
;
2523 if (ndevs
< devs_increment
* sub_stripes
|| ndevs
< devs_min
) {
2528 if (devs_max
&& ndevs
> devs_max
)
2531 * the primary goal is to maximize the number of stripes, so use as many
2532 * devices as possible, even if the stripes are not maximum sized.
2534 stripe_size
= devices_info
[ndevs
-1].max_avail
;
2535 num_stripes
= ndevs
* dev_stripes
;
2537 if (stripe_size
* num_stripes
> max_chunk_size
* ncopies
) {
2538 stripe_size
= max_chunk_size
* ncopies
;
2539 do_div(stripe_size
, num_stripes
);
2542 do_div(stripe_size
, dev_stripes
);
2543 do_div(stripe_size
, BTRFS_STRIPE_LEN
);
2544 stripe_size
*= BTRFS_STRIPE_LEN
;
2546 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
2551 map
->num_stripes
= num_stripes
;
2553 for (i
= 0; i
< ndevs
; ++i
) {
2554 for (j
= 0; j
< dev_stripes
; ++j
) {
2555 int s
= i
* dev_stripes
+ j
;
2556 map
->stripes
[s
].dev
= devices_info
[i
].dev
;
2557 map
->stripes
[s
].physical
= devices_info
[i
].dev_offset
+
2561 map
->sector_size
= extent_root
->sectorsize
;
2562 map
->stripe_len
= BTRFS_STRIPE_LEN
;
2563 map
->io_align
= BTRFS_STRIPE_LEN
;
2564 map
->io_width
= BTRFS_STRIPE_LEN
;
2566 map
->sub_stripes
= sub_stripes
;
2569 num_bytes
= stripe_size
* (num_stripes
/ ncopies
);
2571 *stripe_size_out
= stripe_size
;
2572 *num_bytes_out
= num_bytes
;
2574 trace_btrfs_chunk_alloc(info
->chunk_root
, map
, start
, num_bytes
);
2576 em
= alloc_extent_map();
2581 em
->bdev
= (struct block_device
*)map
;
2583 em
->len
= num_bytes
;
2584 em
->block_start
= 0;
2585 em
->block_len
= em
->len
;
2587 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
2588 write_lock(&em_tree
->lock
);
2589 ret
= add_extent_mapping(em_tree
, em
);
2590 write_unlock(&em_tree
->lock
);
2592 free_extent_map(em
);
2594 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
2595 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2599 for (i
= 0; i
< map
->num_stripes
; ++i
) {
2600 struct btrfs_device
*device
;
2603 device
= map
->stripes
[i
].dev
;
2604 dev_offset
= map
->stripes
[i
].physical
;
2606 ret
= btrfs_alloc_dev_extent(trans
, device
,
2607 info
->chunk_root
->root_key
.objectid
,
2608 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2609 start
, dev_offset
, stripe_size
);
2613 kfree(devices_info
);
2618 kfree(devices_info
);
2622 static int __finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
2623 struct btrfs_root
*extent_root
,
2624 struct map_lookup
*map
, u64 chunk_offset
,
2625 u64 chunk_size
, u64 stripe_size
)
2628 struct btrfs_key key
;
2629 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
2630 struct btrfs_device
*device
;
2631 struct btrfs_chunk
*chunk
;
2632 struct btrfs_stripe
*stripe
;
2633 size_t item_size
= btrfs_chunk_item_size(map
->num_stripes
);
2637 chunk
= kzalloc(item_size
, GFP_NOFS
);
2642 while (index
< map
->num_stripes
) {
2643 device
= map
->stripes
[index
].dev
;
2644 device
->bytes_used
+= stripe_size
;
2645 ret
= btrfs_update_device(trans
, device
);
2650 spin_lock(&extent_root
->fs_info
->free_chunk_lock
);
2651 extent_root
->fs_info
->free_chunk_space
-= (stripe_size
*
2653 spin_unlock(&extent_root
->fs_info
->free_chunk_lock
);
2656 stripe
= &chunk
->stripe
;
2657 while (index
< map
->num_stripes
) {
2658 device
= map
->stripes
[index
].dev
;
2659 dev_offset
= map
->stripes
[index
].physical
;
2661 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
2662 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
2663 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
2668 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
2669 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
2670 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
2671 btrfs_set_stack_chunk_type(chunk
, map
->type
);
2672 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
2673 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
2674 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
2675 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
2676 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
2678 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2679 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2680 key
.offset
= chunk_offset
;
2682 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
2685 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2686 ret
= btrfs_add_system_chunk(chunk_root
, &key
, chunk
,
2696 * Chunk allocation falls into two parts. The first part does works
2697 * that make the new allocated chunk useable, but not do any operation
2698 * that modifies the chunk tree. The second part does the works that
2699 * require modifying the chunk tree. This division is important for the
2700 * bootstrap process of adding storage to a seed btrfs.
2702 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
2703 struct btrfs_root
*extent_root
, u64 type
)
2708 struct map_lookup
*map
;
2709 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
2712 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2717 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
2718 &stripe_size
, chunk_offset
, type
);
2722 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
2723 chunk_size
, stripe_size
);
2728 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
2729 struct btrfs_root
*root
,
2730 struct btrfs_device
*device
)
2733 u64 sys_chunk_offset
;
2737 u64 sys_stripe_size
;
2739 struct map_lookup
*map
;
2740 struct map_lookup
*sys_map
;
2741 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2742 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
2745 ret
= find_next_chunk(fs_info
->chunk_root
,
2746 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, &chunk_offset
);
2750 alloc_profile
= BTRFS_BLOCK_GROUP_METADATA
|
2751 (fs_info
->metadata_alloc_profile
&
2752 fs_info
->avail_metadata_alloc_bits
);
2753 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
2755 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
2756 &stripe_size
, chunk_offset
, alloc_profile
);
2759 sys_chunk_offset
= chunk_offset
+ chunk_size
;
2761 alloc_profile
= BTRFS_BLOCK_GROUP_SYSTEM
|
2762 (fs_info
->system_alloc_profile
&
2763 fs_info
->avail_system_alloc_bits
);
2764 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
2766 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &sys_map
,
2767 &sys_chunk_size
, &sys_stripe_size
,
2768 sys_chunk_offset
, alloc_profile
);
2771 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
2775 * Modifying chunk tree needs allocating new blocks from both
2776 * system block group and metadata block group. So we only can
2777 * do operations require modifying the chunk tree after both
2778 * block groups were created.
2780 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
2781 chunk_size
, stripe_size
);
2784 ret
= __finish_chunk_alloc(trans
, extent_root
, sys_map
,
2785 sys_chunk_offset
, sys_chunk_size
,
2791 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
2793 struct extent_map
*em
;
2794 struct map_lookup
*map
;
2795 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
2799 read_lock(&map_tree
->map_tree
.lock
);
2800 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
2801 read_unlock(&map_tree
->map_tree
.lock
);
2805 if (btrfs_test_opt(root
, DEGRADED
)) {
2806 free_extent_map(em
);
2810 map
= (struct map_lookup
*)em
->bdev
;
2811 for (i
= 0; i
< map
->num_stripes
; i
++) {
2812 if (!map
->stripes
[i
].dev
->writeable
) {
2817 free_extent_map(em
);
2821 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
2823 extent_map_tree_init(&tree
->map_tree
);
2826 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
2828 struct extent_map
*em
;
2831 write_lock(&tree
->map_tree
.lock
);
2832 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
2834 remove_extent_mapping(&tree
->map_tree
, em
);
2835 write_unlock(&tree
->map_tree
.lock
);
2840 free_extent_map(em
);
2841 /* once for the tree */
2842 free_extent_map(em
);
2846 int btrfs_num_copies(struct btrfs_mapping_tree
*map_tree
, u64 logical
, u64 len
)
2848 struct extent_map
*em
;
2849 struct map_lookup
*map
;
2850 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2853 read_lock(&em_tree
->lock
);
2854 em
= lookup_extent_mapping(em_tree
, logical
, len
);
2855 read_unlock(&em_tree
->lock
);
2858 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
2859 map
= (struct map_lookup
*)em
->bdev
;
2860 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
2861 ret
= map
->num_stripes
;
2862 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
2863 ret
= map
->sub_stripes
;
2866 free_extent_map(em
);
2870 static int find_live_mirror(struct map_lookup
*map
, int first
, int num
,
2874 if (map
->stripes
[optimal
].dev
->bdev
)
2876 for (i
= first
; i
< first
+ num
; i
++) {
2877 if (map
->stripes
[i
].dev
->bdev
)
2880 /* we couldn't find one that doesn't fail. Just return something
2881 * and the io error handling code will clean up eventually
2886 static int __btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
2887 u64 logical
, u64
*length
,
2888 struct btrfs_bio
**bbio_ret
,
2891 struct extent_map
*em
;
2892 struct map_lookup
*map
;
2893 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2896 u64 stripe_end_offset
;
2900 int stripes_allocated
= 8;
2901 int stripes_required
= 1;
2906 struct btrfs_bio
*bbio
= NULL
;
2908 if (bbio_ret
&& !(rw
& (REQ_WRITE
| REQ_DISCARD
)))
2909 stripes_allocated
= 1;
2912 bbio
= kzalloc(btrfs_bio_size(stripes_allocated
),
2917 atomic_set(&bbio
->error
, 0);
2920 read_lock(&em_tree
->lock
);
2921 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
2922 read_unlock(&em_tree
->lock
);
2925 printk(KERN_CRIT
"unable to find logical %llu len %llu\n",
2926 (unsigned long long)logical
,
2927 (unsigned long long)*length
);
2931 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
2932 map
= (struct map_lookup
*)em
->bdev
;
2933 offset
= logical
- em
->start
;
2935 if (mirror_num
> map
->num_stripes
)
2938 /* if our btrfs_bio struct is too small, back off and try again */
2939 if (rw
& REQ_WRITE
) {
2940 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
2941 BTRFS_BLOCK_GROUP_DUP
)) {
2942 stripes_required
= map
->num_stripes
;
2944 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
2945 stripes_required
= map
->sub_stripes
;
2949 if (rw
& REQ_DISCARD
) {
2950 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
2951 BTRFS_BLOCK_GROUP_RAID1
|
2952 BTRFS_BLOCK_GROUP_DUP
|
2953 BTRFS_BLOCK_GROUP_RAID10
)) {
2954 stripes_required
= map
->num_stripes
;
2957 if (bbio_ret
&& (rw
& (REQ_WRITE
| REQ_DISCARD
)) &&
2958 stripes_allocated
< stripes_required
) {
2959 stripes_allocated
= map
->num_stripes
;
2960 free_extent_map(em
);
2966 * stripe_nr counts the total number of stripes we have to stride
2967 * to get to this block
2969 do_div(stripe_nr
, map
->stripe_len
);
2971 stripe_offset
= stripe_nr
* map
->stripe_len
;
2972 BUG_ON(offset
< stripe_offset
);
2974 /* stripe_offset is the offset of this block in its stripe*/
2975 stripe_offset
= offset
- stripe_offset
;
2977 if (rw
& REQ_DISCARD
)
2978 *length
= min_t(u64
, em
->len
- offset
, *length
);
2979 else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
2980 BTRFS_BLOCK_GROUP_RAID1
|
2981 BTRFS_BLOCK_GROUP_RAID10
|
2982 BTRFS_BLOCK_GROUP_DUP
)) {
2983 /* we limit the length of each bio to what fits in a stripe */
2984 *length
= min_t(u64
, em
->len
- offset
,
2985 map
->stripe_len
- stripe_offset
);
2987 *length
= em
->len
- offset
;
2995 stripe_nr_orig
= stripe_nr
;
2996 stripe_nr_end
= (offset
+ *length
+ map
->stripe_len
- 1) &
2997 (~(map
->stripe_len
- 1));
2998 do_div(stripe_nr_end
, map
->stripe_len
);
2999 stripe_end_offset
= stripe_nr_end
* map
->stripe_len
-
3001 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3002 if (rw
& REQ_DISCARD
)
3003 num_stripes
= min_t(u64
, map
->num_stripes
,
3004 stripe_nr_end
- stripe_nr_orig
);
3005 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
3006 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
3007 if (rw
& (REQ_WRITE
| REQ_DISCARD
))
3008 num_stripes
= map
->num_stripes
;
3009 else if (mirror_num
)
3010 stripe_index
= mirror_num
- 1;
3012 stripe_index
= find_live_mirror(map
, 0,
3014 current
->pid
% map
->num_stripes
);
3015 mirror_num
= stripe_index
+ 1;
3018 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
3019 if (rw
& (REQ_WRITE
| REQ_DISCARD
)) {
3020 num_stripes
= map
->num_stripes
;
3021 } else if (mirror_num
) {
3022 stripe_index
= mirror_num
- 1;
3027 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3028 int factor
= map
->num_stripes
/ map
->sub_stripes
;
3030 stripe_index
= do_div(stripe_nr
, factor
);
3031 stripe_index
*= map
->sub_stripes
;
3034 num_stripes
= map
->sub_stripes
;
3035 else if (rw
& REQ_DISCARD
)
3036 num_stripes
= min_t(u64
, map
->sub_stripes
*
3037 (stripe_nr_end
- stripe_nr_orig
),
3039 else if (mirror_num
)
3040 stripe_index
+= mirror_num
- 1;
3042 stripe_index
= find_live_mirror(map
, stripe_index
,
3043 map
->sub_stripes
, stripe_index
+
3044 current
->pid
% map
->sub_stripes
);
3045 mirror_num
= stripe_index
+ 1;
3049 * after this do_div call, stripe_nr is the number of stripes
3050 * on this device we have to walk to find the data, and
3051 * stripe_index is the number of our device in the stripe array
3053 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
3054 mirror_num
= stripe_index
+ 1;
3056 BUG_ON(stripe_index
>= map
->num_stripes
);
3058 if (rw
& REQ_DISCARD
) {
3059 for (i
= 0; i
< num_stripes
; i
++) {
3060 bbio
->stripes
[i
].physical
=
3061 map
->stripes
[stripe_index
].physical
+
3062 stripe_offset
+ stripe_nr
* map
->stripe_len
;
3063 bbio
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
3065 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3067 u32 last_stripe
= 0;
3070 div_u64_rem(stripe_nr_end
- 1,
3074 for (j
= 0; j
< map
->num_stripes
; j
++) {
3077 div_u64_rem(stripe_nr_end
- 1 - j
,
3078 map
->num_stripes
, &test
);
3079 if (test
== stripe_index
)
3082 stripes
= stripe_nr_end
- 1 - j
;
3083 do_div(stripes
, map
->num_stripes
);
3084 bbio
->stripes
[i
].length
= map
->stripe_len
*
3085 (stripes
- stripe_nr
+ 1);
3088 bbio
->stripes
[i
].length
-=
3092 if (stripe_index
== last_stripe
)
3093 bbio
->stripes
[i
].length
-=
3095 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3098 int factor
= map
->num_stripes
/
3100 u32 last_stripe
= 0;
3102 div_u64_rem(stripe_nr_end
- 1,
3103 factor
, &last_stripe
);
3104 last_stripe
*= map
->sub_stripes
;
3106 for (j
= 0; j
< factor
; j
++) {
3109 div_u64_rem(stripe_nr_end
- 1 - j
,
3113 stripe_index
/ map
->sub_stripes
)
3116 stripes
= stripe_nr_end
- 1 - j
;
3117 do_div(stripes
, factor
);
3118 bbio
->stripes
[i
].length
= map
->stripe_len
*
3119 (stripes
- stripe_nr
+ 1);
3121 if (i
< map
->sub_stripes
) {
3122 bbio
->stripes
[i
].length
-=
3124 if (i
== map
->sub_stripes
- 1)
3127 if (stripe_index
>= last_stripe
&&
3128 stripe_index
<= (last_stripe
+
3129 map
->sub_stripes
- 1)) {
3130 bbio
->stripes
[i
].length
-=
3134 bbio
->stripes
[i
].length
= *length
;
3137 if (stripe_index
== map
->num_stripes
) {
3138 /* This could only happen for RAID0/10 */
3144 for (i
= 0; i
< num_stripes
; i
++) {
3145 bbio
->stripes
[i
].physical
=
3146 map
->stripes
[stripe_index
].physical
+
3148 stripe_nr
* map
->stripe_len
;
3149 bbio
->stripes
[i
].dev
=
3150 map
->stripes
[stripe_index
].dev
;
3156 bbio
->num_stripes
= num_stripes
;
3157 bbio
->max_errors
= max_errors
;
3158 bbio
->mirror_num
= mirror_num
;
3161 free_extent_map(em
);
3165 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
3166 u64 logical
, u64
*length
,
3167 struct btrfs_bio
**bbio_ret
, int mirror_num
)
3169 return __btrfs_map_block(map_tree
, rw
, logical
, length
, bbio_ret
,
3173 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
3174 u64 chunk_start
, u64 physical
, u64 devid
,
3175 u64
**logical
, int *naddrs
, int *stripe_len
)
3177 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
3178 struct extent_map
*em
;
3179 struct map_lookup
*map
;
3186 read_lock(&em_tree
->lock
);
3187 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
3188 read_unlock(&em_tree
->lock
);
3190 BUG_ON(!em
|| em
->start
!= chunk_start
);
3191 map
= (struct map_lookup
*)em
->bdev
;
3194 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
3195 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
3196 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
3197 do_div(length
, map
->num_stripes
);
3199 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
3202 for (i
= 0; i
< map
->num_stripes
; i
++) {
3203 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
3205 if (map
->stripes
[i
].physical
> physical
||
3206 map
->stripes
[i
].physical
+ length
<= physical
)
3209 stripe_nr
= physical
- map
->stripes
[i
].physical
;
3210 do_div(stripe_nr
, map
->stripe_len
);
3212 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3213 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
3214 do_div(stripe_nr
, map
->sub_stripes
);
3215 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3216 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
3218 bytenr
= chunk_start
+ stripe_nr
* map
->stripe_len
;
3219 WARN_ON(nr
>= map
->num_stripes
);
3220 for (j
= 0; j
< nr
; j
++) {
3221 if (buf
[j
] == bytenr
)
3225 WARN_ON(nr
>= map
->num_stripes
);
3232 *stripe_len
= map
->stripe_len
;
3234 free_extent_map(em
);
3238 static void btrfs_end_bio(struct bio
*bio
, int err
)
3240 struct btrfs_bio
*bbio
= bio
->bi_private
;
3241 int is_orig_bio
= 0;
3244 atomic_inc(&bbio
->error
);
3246 if (bio
== bbio
->orig_bio
)
3249 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
3252 bio
= bbio
->orig_bio
;
3254 bio
->bi_private
= bbio
->private;
3255 bio
->bi_end_io
= bbio
->end_io
;
3256 bio
->bi_bdev
= (struct block_device
*)
3257 (unsigned long)bbio
->mirror_num
;
3258 /* only send an error to the higher layers if it is
3259 * beyond the tolerance of the multi-bio
3261 if (atomic_read(&bbio
->error
) > bbio
->max_errors
) {
3265 * this bio is actually up to date, we didn't
3266 * go over the max number of errors
3268 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
3273 bio_endio(bio
, err
);
3274 } else if (!is_orig_bio
) {
3279 struct async_sched
{
3282 struct btrfs_fs_info
*info
;
3283 struct btrfs_work work
;
3287 * see run_scheduled_bios for a description of why bios are collected for
3290 * This will add one bio to the pending list for a device and make sure
3291 * the work struct is scheduled.
3293 static noinline
int schedule_bio(struct btrfs_root
*root
,
3294 struct btrfs_device
*device
,
3295 int rw
, struct bio
*bio
)
3297 int should_queue
= 1;
3298 struct btrfs_pending_bios
*pending_bios
;
3300 /* don't bother with additional async steps for reads, right now */
3301 if (!(rw
& REQ_WRITE
)) {
3303 submit_bio(rw
, bio
);
3309 * nr_async_bios allows us to reliably return congestion to the
3310 * higher layers. Otherwise, the async bio makes it appear we have
3311 * made progress against dirty pages when we've really just put it
3312 * on a queue for later
3314 atomic_inc(&root
->fs_info
->nr_async_bios
);
3315 WARN_ON(bio
->bi_next
);
3316 bio
->bi_next
= NULL
;
3319 spin_lock(&device
->io_lock
);
3320 if (bio
->bi_rw
& REQ_SYNC
)
3321 pending_bios
= &device
->pending_sync_bios
;
3323 pending_bios
= &device
->pending_bios
;
3325 if (pending_bios
->tail
)
3326 pending_bios
->tail
->bi_next
= bio
;
3328 pending_bios
->tail
= bio
;
3329 if (!pending_bios
->head
)
3330 pending_bios
->head
= bio
;
3331 if (device
->running_pending
)
3334 spin_unlock(&device
->io_lock
);
3337 btrfs_queue_worker(&root
->fs_info
->submit_workers
,
3342 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
3343 int mirror_num
, int async_submit
)
3345 struct btrfs_mapping_tree
*map_tree
;
3346 struct btrfs_device
*dev
;
3347 struct bio
*first_bio
= bio
;
3348 u64 logical
= (u64
)bio
->bi_sector
<< 9;
3354 struct btrfs_bio
*bbio
= NULL
;
3356 length
= bio
->bi_size
;
3357 map_tree
= &root
->fs_info
->mapping_tree
;
3358 map_length
= length
;
3360 ret
= btrfs_map_block(map_tree
, rw
, logical
, &map_length
, &bbio
,
3364 total_devs
= bbio
->num_stripes
;
3365 if (map_length
< length
) {
3366 printk(KERN_CRIT
"mapping failed logical %llu bio len %llu "
3367 "len %llu\n", (unsigned long long)logical
,
3368 (unsigned long long)length
,
3369 (unsigned long long)map_length
);
3373 bbio
->orig_bio
= first_bio
;
3374 bbio
->private = first_bio
->bi_private
;
3375 bbio
->end_io
= first_bio
->bi_end_io
;
3376 atomic_set(&bbio
->stripes_pending
, bbio
->num_stripes
);
3378 while (dev_nr
< total_devs
) {
3379 if (dev_nr
< total_devs
- 1) {
3380 bio
= bio_clone(first_bio
, GFP_NOFS
);
3385 bio
->bi_private
= bbio
;
3386 bio
->bi_end_io
= btrfs_end_bio
;
3387 bio
->bi_sector
= bbio
->stripes
[dev_nr
].physical
>> 9;
3388 dev
= bbio
->stripes
[dev_nr
].dev
;
3389 if (dev
&& dev
->bdev
&& (rw
!= WRITE
|| dev
->writeable
)) {
3390 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
3391 "(%s id %llu), size=%u\n", rw
,
3392 (u64
)bio
->bi_sector
, (u_long
)dev
->bdev
->bd_dev
,
3393 dev
->name
, dev
->devid
, bio
->bi_size
);
3394 bio
->bi_bdev
= dev
->bdev
;
3396 schedule_bio(root
, dev
, rw
, bio
);
3398 submit_bio(rw
, bio
);
3400 bio
->bi_bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
3401 bio
->bi_sector
= logical
>> 9;
3402 bio_endio(bio
, -EIO
);
3409 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
,
3412 struct btrfs_device
*device
;
3413 struct btrfs_fs_devices
*cur_devices
;
3415 cur_devices
= root
->fs_info
->fs_devices
;
3416 while (cur_devices
) {
3418 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
3419 device
= __find_device(&cur_devices
->devices
,
3424 cur_devices
= cur_devices
->seed
;
3429 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
3430 u64 devid
, u8
*dev_uuid
)
3432 struct btrfs_device
*device
;
3433 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
3435 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
3438 list_add(&device
->dev_list
,
3439 &fs_devices
->devices
);
3440 device
->dev_root
= root
->fs_info
->dev_root
;
3441 device
->devid
= devid
;
3442 device
->work
.func
= pending_bios_fn
;
3443 device
->fs_devices
= fs_devices
;
3444 device
->missing
= 1;
3445 fs_devices
->num_devices
++;
3446 fs_devices
->missing_devices
++;
3447 spin_lock_init(&device
->io_lock
);
3448 INIT_LIST_HEAD(&device
->dev_alloc_list
);
3449 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
3453 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
3454 struct extent_buffer
*leaf
,
3455 struct btrfs_chunk
*chunk
)
3457 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
3458 struct map_lookup
*map
;
3459 struct extent_map
*em
;
3463 u8 uuid
[BTRFS_UUID_SIZE
];
3468 logical
= key
->offset
;
3469 length
= btrfs_chunk_length(leaf
, chunk
);
3471 read_lock(&map_tree
->map_tree
.lock
);
3472 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
3473 read_unlock(&map_tree
->map_tree
.lock
);
3475 /* already mapped? */
3476 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
3477 free_extent_map(em
);
3480 free_extent_map(em
);
3483 em
= alloc_extent_map();
3486 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
3487 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
3489 free_extent_map(em
);
3493 em
->bdev
= (struct block_device
*)map
;
3494 em
->start
= logical
;
3496 em
->block_start
= 0;
3497 em
->block_len
= em
->len
;
3499 map
->num_stripes
= num_stripes
;
3500 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
3501 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
3502 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
3503 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
3504 map
->type
= btrfs_chunk_type(leaf
, chunk
);
3505 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
3506 for (i
= 0; i
< num_stripes
; i
++) {
3507 map
->stripes
[i
].physical
=
3508 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
3509 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
3510 read_extent_buffer(leaf
, uuid
, (unsigned long)
3511 btrfs_stripe_dev_uuid_nr(chunk
, i
),
3513 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
, uuid
,
3515 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
3517 free_extent_map(em
);
3520 if (!map
->stripes
[i
].dev
) {
3521 map
->stripes
[i
].dev
=
3522 add_missing_dev(root
, devid
, uuid
);
3523 if (!map
->stripes
[i
].dev
) {
3525 free_extent_map(em
);
3529 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
3532 write_lock(&map_tree
->map_tree
.lock
);
3533 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
3534 write_unlock(&map_tree
->map_tree
.lock
);
3536 free_extent_map(em
);
3541 static int fill_device_from_item(struct extent_buffer
*leaf
,
3542 struct btrfs_dev_item
*dev_item
,
3543 struct btrfs_device
*device
)
3547 device
->devid
= btrfs_device_id(leaf
, dev_item
);
3548 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
3549 device
->total_bytes
= device
->disk_total_bytes
;
3550 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
3551 device
->type
= btrfs_device_type(leaf
, dev_item
);
3552 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
3553 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
3554 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
3556 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
3557 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
3562 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
3564 struct btrfs_fs_devices
*fs_devices
;
3567 mutex_lock(&uuid_mutex
);
3569 fs_devices
= root
->fs_info
->fs_devices
->seed
;
3570 while (fs_devices
) {
3571 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
3575 fs_devices
= fs_devices
->seed
;
3578 fs_devices
= find_fsid(fsid
);
3584 fs_devices
= clone_fs_devices(fs_devices
);
3585 if (IS_ERR(fs_devices
)) {
3586 ret
= PTR_ERR(fs_devices
);
3590 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
3591 root
->fs_info
->bdev_holder
);
3595 if (!fs_devices
->seeding
) {
3596 __btrfs_close_devices(fs_devices
);
3597 free_fs_devices(fs_devices
);
3602 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
3603 root
->fs_info
->fs_devices
->seed
= fs_devices
;
3605 mutex_unlock(&uuid_mutex
);
3609 static int read_one_dev(struct btrfs_root
*root
,
3610 struct extent_buffer
*leaf
,
3611 struct btrfs_dev_item
*dev_item
)
3613 struct btrfs_device
*device
;
3616 u8 fs_uuid
[BTRFS_UUID_SIZE
];
3617 u8 dev_uuid
[BTRFS_UUID_SIZE
];
3619 devid
= btrfs_device_id(leaf
, dev_item
);
3620 read_extent_buffer(leaf
, dev_uuid
,
3621 (unsigned long)btrfs_device_uuid(dev_item
),
3623 read_extent_buffer(leaf
, fs_uuid
,
3624 (unsigned long)btrfs_device_fsid(dev_item
),
3627 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
3628 ret
= open_seed_devices(root
, fs_uuid
);
3629 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
3633 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
3634 if (!device
|| !device
->bdev
) {
3635 if (!btrfs_test_opt(root
, DEGRADED
))
3639 printk(KERN_WARNING
"warning devid %llu missing\n",
3640 (unsigned long long)devid
);
3641 device
= add_missing_dev(root
, devid
, dev_uuid
);
3644 } else if (!device
->missing
) {
3646 * this happens when a device that was properly setup
3647 * in the device info lists suddenly goes bad.
3648 * device->bdev is NULL, and so we have to set
3649 * device->missing to one here
3651 root
->fs_info
->fs_devices
->missing_devices
++;
3652 device
->missing
= 1;
3656 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
3657 BUG_ON(device
->writeable
);
3658 if (device
->generation
!=
3659 btrfs_device_generation(leaf
, dev_item
))
3663 fill_device_from_item(leaf
, dev_item
, device
);
3664 device
->dev_root
= root
->fs_info
->dev_root
;
3665 device
->in_fs_metadata
= 1;
3666 if (device
->writeable
) {
3667 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
3668 spin_lock(&root
->fs_info
->free_chunk_lock
);
3669 root
->fs_info
->free_chunk_space
+= device
->total_bytes
-
3671 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3677 int btrfs_read_sys_array(struct btrfs_root
*root
)
3679 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
3680 struct extent_buffer
*sb
;
3681 struct btrfs_disk_key
*disk_key
;
3682 struct btrfs_chunk
*chunk
;
3684 unsigned long sb_ptr
;
3690 struct btrfs_key key
;
3692 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
3693 BTRFS_SUPER_INFO_SIZE
);
3696 btrfs_set_buffer_uptodate(sb
);
3697 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, sb
, 0);
3699 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
3700 array_size
= btrfs_super_sys_array_size(super_copy
);
3702 ptr
= super_copy
->sys_chunk_array
;
3703 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
3706 while (cur
< array_size
) {
3707 disk_key
= (struct btrfs_disk_key
*)ptr
;
3708 btrfs_disk_key_to_cpu(&key
, disk_key
);
3710 len
= sizeof(*disk_key
); ptr
+= len
;
3714 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
3715 chunk
= (struct btrfs_chunk
*)sb_ptr
;
3716 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
3719 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
3720 len
= btrfs_chunk_item_size(num_stripes
);
3729 free_extent_buffer(sb
);
3733 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
3735 struct btrfs_path
*path
;
3736 struct extent_buffer
*leaf
;
3737 struct btrfs_key key
;
3738 struct btrfs_key found_key
;
3742 root
= root
->fs_info
->chunk_root
;
3744 path
= btrfs_alloc_path();
3748 /* first we search for all of the device items, and then we
3749 * read in all of the chunk items. This way we can create chunk
3750 * mappings that reference all of the devices that are afound
3752 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
3756 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3760 leaf
= path
->nodes
[0];
3761 slot
= path
->slots
[0];
3762 if (slot
>= btrfs_header_nritems(leaf
)) {
3763 ret
= btrfs_next_leaf(root
, path
);
3770 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3771 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
3772 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
3774 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
3775 struct btrfs_dev_item
*dev_item
;
3776 dev_item
= btrfs_item_ptr(leaf
, slot
,
3777 struct btrfs_dev_item
);
3778 ret
= read_one_dev(root
, leaf
, dev_item
);
3782 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
3783 struct btrfs_chunk
*chunk
;
3784 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
3785 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
3791 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
3793 btrfs_release_path(path
);
3798 btrfs_free_path(path
);