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 <linux/ratelimit.h>
27 #include <linux/kthread.h>
28 #include <linux/raid/pq.h>
29 #include <asm/div64.h>
32 #include "extent_map.h"
34 #include "transaction.h"
35 #include "print-tree.h"
38 #include "async-thread.h"
39 #include "check-integrity.h"
40 #include "rcu-string.h"
42 #include "dev-replace.h"
44 static int init_first_rw_device(struct btrfs_trans_handle
*trans
,
45 struct btrfs_root
*root
,
46 struct btrfs_device
*device
);
47 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
);
48 static void __btrfs_reset_dev_stats(struct btrfs_device
*dev
);
49 static void btrfs_dev_stat_print_on_error(struct btrfs_device
*dev
);
50 static void btrfs_dev_stat_print_on_load(struct btrfs_device
*device
);
52 static DEFINE_MUTEX(uuid_mutex
);
53 static LIST_HEAD(fs_uuids
);
55 static void lock_chunks(struct btrfs_root
*root
)
57 mutex_lock(&root
->fs_info
->chunk_mutex
);
60 static void unlock_chunks(struct btrfs_root
*root
)
62 mutex_unlock(&root
->fs_info
->chunk_mutex
);
65 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
67 struct btrfs_device
*device
;
68 WARN_ON(fs_devices
->opened
);
69 while (!list_empty(&fs_devices
->devices
)) {
70 device
= list_entry(fs_devices
->devices
.next
,
71 struct btrfs_device
, dev_list
);
72 list_del(&device
->dev_list
);
73 rcu_string_free(device
->name
);
79 static void btrfs_kobject_uevent(struct block_device
*bdev
,
80 enum kobject_action action
)
84 ret
= kobject_uevent(&disk_to_dev(bdev
->bd_disk
)->kobj
, action
);
86 pr_warn("Sending event '%d' to kobject: '%s' (%p): failed\n",
88 kobject_name(&disk_to_dev(bdev
->bd_disk
)->kobj
),
89 &disk_to_dev(bdev
->bd_disk
)->kobj
);
92 void btrfs_cleanup_fs_uuids(void)
94 struct btrfs_fs_devices
*fs_devices
;
96 while (!list_empty(&fs_uuids
)) {
97 fs_devices
= list_entry(fs_uuids
.next
,
98 struct btrfs_fs_devices
, list
);
99 list_del(&fs_devices
->list
);
100 free_fs_devices(fs_devices
);
104 static noinline
struct btrfs_device
*__find_device(struct list_head
*head
,
107 struct btrfs_device
*dev
;
109 list_for_each_entry(dev
, head
, dev_list
) {
110 if (dev
->devid
== devid
&&
111 (!uuid
|| !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
))) {
118 static noinline
struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
120 struct btrfs_fs_devices
*fs_devices
;
122 list_for_each_entry(fs_devices
, &fs_uuids
, list
) {
123 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
130 btrfs_get_bdev_and_sb(const char *device_path
, fmode_t flags
, void *holder
,
131 int flush
, struct block_device
**bdev
,
132 struct buffer_head
**bh
)
136 *bdev
= blkdev_get_by_path(device_path
, flags
, holder
);
139 ret
= PTR_ERR(*bdev
);
140 printk(KERN_INFO
"btrfs: open %s failed\n", device_path
);
145 filemap_write_and_wait((*bdev
)->bd_inode
->i_mapping
);
146 ret
= set_blocksize(*bdev
, 4096);
148 blkdev_put(*bdev
, flags
);
151 invalidate_bdev(*bdev
);
152 *bh
= btrfs_read_dev_super(*bdev
);
155 blkdev_put(*bdev
, flags
);
167 static void requeue_list(struct btrfs_pending_bios
*pending_bios
,
168 struct bio
*head
, struct bio
*tail
)
171 struct bio
*old_head
;
173 old_head
= pending_bios
->head
;
174 pending_bios
->head
= head
;
175 if (pending_bios
->tail
)
176 tail
->bi_next
= old_head
;
178 pending_bios
->tail
= tail
;
182 * we try to collect pending bios for a device so we don't get a large
183 * number of procs sending bios down to the same device. This greatly
184 * improves the schedulers ability to collect and merge the bios.
186 * But, it also turns into a long list of bios to process and that is sure
187 * to eventually make the worker thread block. The solution here is to
188 * make some progress and then put this work struct back at the end of
189 * the list if the block device is congested. This way, multiple devices
190 * can make progress from a single worker thread.
192 static noinline
void run_scheduled_bios(struct btrfs_device
*device
)
195 struct backing_dev_info
*bdi
;
196 struct btrfs_fs_info
*fs_info
;
197 struct btrfs_pending_bios
*pending_bios
;
201 unsigned long num_run
;
202 unsigned long batch_run
= 0;
204 unsigned long last_waited
= 0;
206 int sync_pending
= 0;
207 struct blk_plug plug
;
210 * this function runs all the bios we've collected for
211 * a particular device. We don't want to wander off to
212 * another device without first sending all of these down.
213 * So, setup a plug here and finish it off before we return
215 blk_start_plug(&plug
);
217 bdi
= blk_get_backing_dev_info(device
->bdev
);
218 fs_info
= device
->dev_root
->fs_info
;
219 limit
= btrfs_async_submit_limit(fs_info
);
220 limit
= limit
* 2 / 3;
223 spin_lock(&device
->io_lock
);
228 /* take all the bios off the list at once and process them
229 * later on (without the lock held). But, remember the
230 * tail and other pointers so the bios can be properly reinserted
231 * into the list if we hit congestion
233 if (!force_reg
&& device
->pending_sync_bios
.head
) {
234 pending_bios
= &device
->pending_sync_bios
;
237 pending_bios
= &device
->pending_bios
;
241 pending
= pending_bios
->head
;
242 tail
= pending_bios
->tail
;
243 WARN_ON(pending
&& !tail
);
246 * if pending was null this time around, no bios need processing
247 * at all and we can stop. Otherwise it'll loop back up again
248 * and do an additional check so no bios are missed.
250 * device->running_pending is used to synchronize with the
253 if (device
->pending_sync_bios
.head
== NULL
&&
254 device
->pending_bios
.head
== NULL
) {
256 device
->running_pending
= 0;
259 device
->running_pending
= 1;
262 pending_bios
->head
= NULL
;
263 pending_bios
->tail
= NULL
;
265 spin_unlock(&device
->io_lock
);
270 /* we want to work on both lists, but do more bios on the
271 * sync list than the regular list
274 pending_bios
!= &device
->pending_sync_bios
&&
275 device
->pending_sync_bios
.head
) ||
276 (num_run
> 64 && pending_bios
== &device
->pending_sync_bios
&&
277 device
->pending_bios
.head
)) {
278 spin_lock(&device
->io_lock
);
279 requeue_list(pending_bios
, pending
, tail
);
284 pending
= pending
->bi_next
;
287 if (atomic_dec_return(&fs_info
->nr_async_bios
) < limit
&&
288 waitqueue_active(&fs_info
->async_submit_wait
))
289 wake_up(&fs_info
->async_submit_wait
);
291 BUG_ON(atomic_read(&cur
->bi_cnt
) == 0);
294 * if we're doing the sync list, record that our
295 * plug has some sync requests on it
297 * If we're doing the regular list and there are
298 * sync requests sitting around, unplug before
301 if (pending_bios
== &device
->pending_sync_bios
) {
303 } else if (sync_pending
) {
304 blk_finish_plug(&plug
);
305 blk_start_plug(&plug
);
309 btrfsic_submit_bio(cur
->bi_rw
, cur
);
316 * we made progress, there is more work to do and the bdi
317 * is now congested. Back off and let other work structs
320 if (pending
&& bdi_write_congested(bdi
) && batch_run
> 8 &&
321 fs_info
->fs_devices
->open_devices
> 1) {
322 struct io_context
*ioc
;
324 ioc
= current
->io_context
;
327 * the main goal here is that we don't want to
328 * block if we're going to be able to submit
329 * more requests without blocking.
331 * This code does two great things, it pokes into
332 * the elevator code from a filesystem _and_
333 * it makes assumptions about how batching works.
335 if (ioc
&& ioc
->nr_batch_requests
> 0 &&
336 time_before(jiffies
, ioc
->last_waited
+ HZ
/50UL) &&
338 ioc
->last_waited
== last_waited
)) {
340 * we want to go through our batch of
341 * requests and stop. So, we copy out
342 * the ioc->last_waited time and test
343 * against it before looping
345 last_waited
= ioc
->last_waited
;
350 spin_lock(&device
->io_lock
);
351 requeue_list(pending_bios
, pending
, tail
);
352 device
->running_pending
= 1;
354 spin_unlock(&device
->io_lock
);
355 btrfs_requeue_work(&device
->work
);
358 /* unplug every 64 requests just for good measure */
359 if (batch_run
% 64 == 0) {
360 blk_finish_plug(&plug
);
361 blk_start_plug(&plug
);
370 spin_lock(&device
->io_lock
);
371 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
373 spin_unlock(&device
->io_lock
);
376 blk_finish_plug(&plug
);
379 static void pending_bios_fn(struct btrfs_work
*work
)
381 struct btrfs_device
*device
;
383 device
= container_of(work
, struct btrfs_device
, work
);
384 run_scheduled_bios(device
);
387 static noinline
int device_list_add(const char *path
,
388 struct btrfs_super_block
*disk_super
,
389 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
391 struct btrfs_device
*device
;
392 struct btrfs_fs_devices
*fs_devices
;
393 struct rcu_string
*name
;
394 u64 found_transid
= btrfs_super_generation(disk_super
);
396 fs_devices
= find_fsid(disk_super
->fsid
);
398 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
401 INIT_LIST_HEAD(&fs_devices
->devices
);
402 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
403 list_add(&fs_devices
->list
, &fs_uuids
);
404 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
405 fs_devices
->latest_devid
= devid
;
406 fs_devices
->latest_trans
= found_transid
;
407 mutex_init(&fs_devices
->device_list_mutex
);
410 device
= __find_device(&fs_devices
->devices
, devid
,
411 disk_super
->dev_item
.uuid
);
414 if (fs_devices
->opened
)
417 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
419 /* we can safely leave the fs_devices entry around */
422 device
->devid
= devid
;
423 device
->dev_stats_valid
= 0;
424 device
->work
.func
= pending_bios_fn
;
425 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
427 spin_lock_init(&device
->io_lock
);
429 name
= rcu_string_strdup(path
, GFP_NOFS
);
434 rcu_assign_pointer(device
->name
, name
);
435 INIT_LIST_HEAD(&device
->dev_alloc_list
);
437 /* init readahead state */
438 spin_lock_init(&device
->reada_lock
);
439 device
->reada_curr_zone
= NULL
;
440 atomic_set(&device
->reada_in_flight
, 0);
441 device
->reada_next
= 0;
442 INIT_RADIX_TREE(&device
->reada_zones
, GFP_NOFS
& ~__GFP_WAIT
);
443 INIT_RADIX_TREE(&device
->reada_extents
, GFP_NOFS
& ~__GFP_WAIT
);
445 mutex_lock(&fs_devices
->device_list_mutex
);
446 list_add_rcu(&device
->dev_list
, &fs_devices
->devices
);
447 mutex_unlock(&fs_devices
->device_list_mutex
);
449 device
->fs_devices
= fs_devices
;
450 fs_devices
->num_devices
++;
451 } else if (!device
->name
|| strcmp(device
->name
->str
, path
)) {
452 name
= rcu_string_strdup(path
, GFP_NOFS
);
455 rcu_string_free(device
->name
);
456 rcu_assign_pointer(device
->name
, name
);
457 if (device
->missing
) {
458 fs_devices
->missing_devices
--;
463 if (found_transid
> fs_devices
->latest_trans
) {
464 fs_devices
->latest_devid
= devid
;
465 fs_devices
->latest_trans
= found_transid
;
467 *fs_devices_ret
= fs_devices
;
471 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
473 struct btrfs_fs_devices
*fs_devices
;
474 struct btrfs_device
*device
;
475 struct btrfs_device
*orig_dev
;
477 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
479 return ERR_PTR(-ENOMEM
);
481 INIT_LIST_HEAD(&fs_devices
->devices
);
482 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
483 INIT_LIST_HEAD(&fs_devices
->list
);
484 mutex_init(&fs_devices
->device_list_mutex
);
485 fs_devices
->latest_devid
= orig
->latest_devid
;
486 fs_devices
->latest_trans
= orig
->latest_trans
;
487 fs_devices
->total_devices
= orig
->total_devices
;
488 memcpy(fs_devices
->fsid
, orig
->fsid
, sizeof(fs_devices
->fsid
));
490 /* We have held the volume lock, it is safe to get the devices. */
491 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
492 struct rcu_string
*name
;
494 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
499 * This is ok to do without rcu read locked because we hold the
500 * uuid mutex so nothing we touch in here is going to disappear.
502 name
= rcu_string_strdup(orig_dev
->name
->str
, GFP_NOFS
);
507 rcu_assign_pointer(device
->name
, name
);
509 device
->devid
= orig_dev
->devid
;
510 device
->work
.func
= pending_bios_fn
;
511 memcpy(device
->uuid
, orig_dev
->uuid
, sizeof(device
->uuid
));
512 spin_lock_init(&device
->io_lock
);
513 INIT_LIST_HEAD(&device
->dev_list
);
514 INIT_LIST_HEAD(&device
->dev_alloc_list
);
516 list_add(&device
->dev_list
, &fs_devices
->devices
);
517 device
->fs_devices
= fs_devices
;
518 fs_devices
->num_devices
++;
522 free_fs_devices(fs_devices
);
523 return ERR_PTR(-ENOMEM
);
526 void btrfs_close_extra_devices(struct btrfs_fs_info
*fs_info
,
527 struct btrfs_fs_devices
*fs_devices
, int step
)
529 struct btrfs_device
*device
, *next
;
531 struct block_device
*latest_bdev
= NULL
;
532 u64 latest_devid
= 0;
533 u64 latest_transid
= 0;
535 mutex_lock(&uuid_mutex
);
537 /* This is the initialized path, it is safe to release the devices. */
538 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
539 if (device
->in_fs_metadata
) {
540 if (!device
->is_tgtdev_for_dev_replace
&&
542 device
->generation
> latest_transid
)) {
543 latest_devid
= device
->devid
;
544 latest_transid
= device
->generation
;
545 latest_bdev
= device
->bdev
;
550 if (device
->devid
== BTRFS_DEV_REPLACE_DEVID
) {
552 * In the first step, keep the device which has
553 * the correct fsid and the devid that is used
554 * for the dev_replace procedure.
555 * In the second step, the dev_replace state is
556 * read from the device tree and it is known
557 * whether the procedure is really active or
558 * not, which means whether this device is
559 * used or whether it should be removed.
561 if (step
== 0 || device
->is_tgtdev_for_dev_replace
) {
566 blkdev_put(device
->bdev
, device
->mode
);
568 fs_devices
->open_devices
--;
570 if (device
->writeable
) {
571 list_del_init(&device
->dev_alloc_list
);
572 device
->writeable
= 0;
573 if (!device
->is_tgtdev_for_dev_replace
)
574 fs_devices
->rw_devices
--;
576 list_del_init(&device
->dev_list
);
577 fs_devices
->num_devices
--;
578 rcu_string_free(device
->name
);
582 if (fs_devices
->seed
) {
583 fs_devices
= fs_devices
->seed
;
587 fs_devices
->latest_bdev
= latest_bdev
;
588 fs_devices
->latest_devid
= latest_devid
;
589 fs_devices
->latest_trans
= latest_transid
;
591 mutex_unlock(&uuid_mutex
);
594 static void __free_device(struct work_struct
*work
)
596 struct btrfs_device
*device
;
598 device
= container_of(work
, struct btrfs_device
, rcu_work
);
601 blkdev_put(device
->bdev
, device
->mode
);
603 rcu_string_free(device
->name
);
607 static void free_device(struct rcu_head
*head
)
609 struct btrfs_device
*device
;
611 device
= container_of(head
, struct btrfs_device
, rcu
);
613 INIT_WORK(&device
->rcu_work
, __free_device
);
614 schedule_work(&device
->rcu_work
);
617 static int __btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
619 struct btrfs_device
*device
;
621 if (--fs_devices
->opened
> 0)
624 mutex_lock(&fs_devices
->device_list_mutex
);
625 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
626 struct btrfs_device
*new_device
;
627 struct rcu_string
*name
;
630 fs_devices
->open_devices
--;
632 if (device
->writeable
&& !device
->is_tgtdev_for_dev_replace
) {
633 list_del_init(&device
->dev_alloc_list
);
634 fs_devices
->rw_devices
--;
637 if (device
->can_discard
)
638 fs_devices
->num_can_discard
--;
640 new_device
= kmalloc(sizeof(*new_device
), GFP_NOFS
);
641 BUG_ON(!new_device
); /* -ENOMEM */
642 memcpy(new_device
, device
, sizeof(*new_device
));
644 /* Safe because we are under uuid_mutex */
646 name
= rcu_string_strdup(device
->name
->str
, GFP_NOFS
);
647 BUG_ON(device
->name
&& !name
); /* -ENOMEM */
648 rcu_assign_pointer(new_device
->name
, name
);
650 new_device
->bdev
= NULL
;
651 new_device
->writeable
= 0;
652 new_device
->in_fs_metadata
= 0;
653 new_device
->can_discard
= 0;
654 spin_lock_init(&new_device
->io_lock
);
655 list_replace_rcu(&device
->dev_list
, &new_device
->dev_list
);
657 call_rcu(&device
->rcu
, free_device
);
659 mutex_unlock(&fs_devices
->device_list_mutex
);
661 WARN_ON(fs_devices
->open_devices
);
662 WARN_ON(fs_devices
->rw_devices
);
663 fs_devices
->opened
= 0;
664 fs_devices
->seeding
= 0;
669 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
671 struct btrfs_fs_devices
*seed_devices
= NULL
;
674 mutex_lock(&uuid_mutex
);
675 ret
= __btrfs_close_devices(fs_devices
);
676 if (!fs_devices
->opened
) {
677 seed_devices
= fs_devices
->seed
;
678 fs_devices
->seed
= NULL
;
680 mutex_unlock(&uuid_mutex
);
682 while (seed_devices
) {
683 fs_devices
= seed_devices
;
684 seed_devices
= fs_devices
->seed
;
685 __btrfs_close_devices(fs_devices
);
686 free_fs_devices(fs_devices
);
689 * Wait for rcu kworkers under __btrfs_close_devices
690 * to finish all blkdev_puts so device is really
691 * free when umount is done.
697 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
698 fmode_t flags
, void *holder
)
700 struct request_queue
*q
;
701 struct block_device
*bdev
;
702 struct list_head
*head
= &fs_devices
->devices
;
703 struct btrfs_device
*device
;
704 struct block_device
*latest_bdev
= NULL
;
705 struct buffer_head
*bh
;
706 struct btrfs_super_block
*disk_super
;
707 u64 latest_devid
= 0;
708 u64 latest_transid
= 0;
715 list_for_each_entry(device
, head
, dev_list
) {
721 /* Just open everything we can; ignore failures here */
722 if (btrfs_get_bdev_and_sb(device
->name
->str
, flags
, holder
, 1,
726 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
727 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
728 if (devid
!= device
->devid
)
731 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
735 device
->generation
= btrfs_super_generation(disk_super
);
736 if (!latest_transid
|| device
->generation
> latest_transid
) {
737 latest_devid
= devid
;
738 latest_transid
= device
->generation
;
742 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
743 device
->writeable
= 0;
745 device
->writeable
= !bdev_read_only(bdev
);
749 q
= bdev_get_queue(bdev
);
750 if (blk_queue_discard(q
)) {
751 device
->can_discard
= 1;
752 fs_devices
->num_can_discard
++;
756 device
->in_fs_metadata
= 0;
757 device
->mode
= flags
;
759 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
760 fs_devices
->rotating
= 1;
762 fs_devices
->open_devices
++;
763 if (device
->writeable
&& !device
->is_tgtdev_for_dev_replace
) {
764 fs_devices
->rw_devices
++;
765 list_add(&device
->dev_alloc_list
,
766 &fs_devices
->alloc_list
);
773 blkdev_put(bdev
, flags
);
776 if (fs_devices
->open_devices
== 0) {
780 fs_devices
->seeding
= seeding
;
781 fs_devices
->opened
= 1;
782 fs_devices
->latest_bdev
= latest_bdev
;
783 fs_devices
->latest_devid
= latest_devid
;
784 fs_devices
->latest_trans
= latest_transid
;
785 fs_devices
->total_rw_bytes
= 0;
790 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
791 fmode_t flags
, void *holder
)
795 mutex_lock(&uuid_mutex
);
796 if (fs_devices
->opened
) {
797 fs_devices
->opened
++;
800 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
802 mutex_unlock(&uuid_mutex
);
807 * Look for a btrfs signature on a device. This may be called out of the mount path
808 * and we are not allowed to call set_blocksize during the scan. The superblock
809 * is read via pagecache
811 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
812 struct btrfs_fs_devices
**fs_devices_ret
)
814 struct btrfs_super_block
*disk_super
;
815 struct block_device
*bdev
;
826 * we would like to check all the supers, but that would make
827 * a btrfs mount succeed after a mkfs from a different FS.
828 * So, we need to add a special mount option to scan for
829 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
831 bytenr
= btrfs_sb_offset(0);
833 mutex_lock(&uuid_mutex
);
835 bdev
= blkdev_get_by_path(path
, flags
, holder
);
842 /* make sure our super fits in the device */
843 if (bytenr
+ PAGE_CACHE_SIZE
>= i_size_read(bdev
->bd_inode
))
846 /* make sure our super fits in the page */
847 if (sizeof(*disk_super
) > PAGE_CACHE_SIZE
)
850 /* make sure our super doesn't straddle pages on disk */
851 index
= bytenr
>> PAGE_CACHE_SHIFT
;
852 if ((bytenr
+ sizeof(*disk_super
) - 1) >> PAGE_CACHE_SHIFT
!= index
)
855 /* pull in the page with our super */
856 page
= read_cache_page_gfp(bdev
->bd_inode
->i_mapping
,
859 if (IS_ERR_OR_NULL(page
))
864 /* align our pointer to the offset of the super block */
865 disk_super
= p
+ (bytenr
& ~PAGE_CACHE_MASK
);
867 if (btrfs_super_bytenr(disk_super
) != bytenr
||
868 disk_super
->magic
!= cpu_to_le64(BTRFS_MAGIC
))
871 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
872 transid
= btrfs_super_generation(disk_super
);
873 total_devices
= btrfs_super_num_devices(disk_super
);
875 if (disk_super
->label
[0]) {
876 if (disk_super
->label
[BTRFS_LABEL_SIZE
- 1])
877 disk_super
->label
[BTRFS_LABEL_SIZE
- 1] = '\0';
878 printk(KERN_INFO
"device label %s ", disk_super
->label
);
880 printk(KERN_INFO
"device fsid %pU ", disk_super
->fsid
);
883 printk(KERN_CONT
"devid %llu transid %llu %s\n",
884 (unsigned long long)devid
, (unsigned long long)transid
, path
);
886 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
887 if (!ret
&& fs_devices_ret
)
888 (*fs_devices_ret
)->total_devices
= total_devices
;
892 page_cache_release(page
);
895 blkdev_put(bdev
, flags
);
897 mutex_unlock(&uuid_mutex
);
901 /* helper to account the used device space in the range */
902 int btrfs_account_dev_extents_size(struct btrfs_device
*device
, u64 start
,
903 u64 end
, u64
*length
)
905 struct btrfs_key key
;
906 struct btrfs_root
*root
= device
->dev_root
;
907 struct btrfs_dev_extent
*dev_extent
;
908 struct btrfs_path
*path
;
912 struct extent_buffer
*l
;
916 if (start
>= device
->total_bytes
|| device
->is_tgtdev_for_dev_replace
)
919 path
= btrfs_alloc_path();
924 key
.objectid
= device
->devid
;
926 key
.type
= BTRFS_DEV_EXTENT_KEY
;
928 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
932 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
939 slot
= path
->slots
[0];
940 if (slot
>= btrfs_header_nritems(l
)) {
941 ret
= btrfs_next_leaf(root
, path
);
949 btrfs_item_key_to_cpu(l
, &key
, slot
);
951 if (key
.objectid
< device
->devid
)
954 if (key
.objectid
> device
->devid
)
957 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
960 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
961 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
963 if (key
.offset
<= start
&& extent_end
> end
) {
964 *length
= end
- start
+ 1;
966 } else if (key
.offset
<= start
&& extent_end
> start
)
967 *length
+= extent_end
- start
;
968 else if (key
.offset
> start
&& extent_end
<= end
)
969 *length
+= extent_end
- key
.offset
;
970 else if (key
.offset
> start
&& key
.offset
<= end
) {
971 *length
+= end
- key
.offset
+ 1;
973 } else if (key
.offset
> end
)
981 btrfs_free_path(path
);
986 * find_free_dev_extent - find free space in the specified device
987 * @device: the device which we search the free space in
988 * @num_bytes: the size of the free space that we need
989 * @start: store the start of the free space.
990 * @len: the size of the free space. that we find, or the size of the max
991 * free space if we don't find suitable free space
993 * this uses a pretty simple search, the expectation is that it is
994 * called very infrequently and that a given device has a small number
997 * @start is used to store the start of the free space if we find. But if we
998 * don't find suitable free space, it will be used to store the start position
999 * of the max free space.
1001 * @len is used to store the size of the free space that we find.
1002 * But if we don't find suitable free space, it is used to store the size of
1003 * the max free space.
1005 int find_free_dev_extent(struct btrfs_device
*device
, u64 num_bytes
,
1006 u64
*start
, u64
*len
)
1008 struct btrfs_key key
;
1009 struct btrfs_root
*root
= device
->dev_root
;
1010 struct btrfs_dev_extent
*dev_extent
;
1011 struct btrfs_path
*path
;
1017 u64 search_end
= device
->total_bytes
;
1020 struct extent_buffer
*l
;
1022 /* FIXME use last free of some kind */
1024 /* we don't want to overwrite the superblock on the drive,
1025 * so we make sure to start at an offset of at least 1MB
1027 search_start
= max(root
->fs_info
->alloc_start
, 1024ull * 1024);
1029 max_hole_start
= search_start
;
1033 if (search_start
>= search_end
|| device
->is_tgtdev_for_dev_replace
) {
1038 path
= btrfs_alloc_path();
1045 key
.objectid
= device
->devid
;
1046 key
.offset
= search_start
;
1047 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1049 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1053 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
1060 slot
= path
->slots
[0];
1061 if (slot
>= btrfs_header_nritems(l
)) {
1062 ret
= btrfs_next_leaf(root
, path
);
1070 btrfs_item_key_to_cpu(l
, &key
, slot
);
1072 if (key
.objectid
< device
->devid
)
1075 if (key
.objectid
> device
->devid
)
1078 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
1081 if (key
.offset
> search_start
) {
1082 hole_size
= key
.offset
- search_start
;
1084 if (hole_size
> max_hole_size
) {
1085 max_hole_start
= search_start
;
1086 max_hole_size
= hole_size
;
1090 * If this free space is greater than which we need,
1091 * it must be the max free space that we have found
1092 * until now, so max_hole_start must point to the start
1093 * of this free space and the length of this free space
1094 * is stored in max_hole_size. Thus, we return
1095 * max_hole_start and max_hole_size and go back to the
1098 if (hole_size
>= num_bytes
) {
1104 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1105 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
1107 if (extent_end
> search_start
)
1108 search_start
= extent_end
;
1115 * At this point, search_start should be the end of
1116 * allocated dev extents, and when shrinking the device,
1117 * search_end may be smaller than search_start.
1119 if (search_end
> search_start
)
1120 hole_size
= search_end
- search_start
;
1122 if (hole_size
> max_hole_size
) {
1123 max_hole_start
= search_start
;
1124 max_hole_size
= hole_size
;
1128 if (hole_size
< num_bytes
)
1134 btrfs_free_path(path
);
1136 *start
= max_hole_start
;
1138 *len
= max_hole_size
;
1142 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
1143 struct btrfs_device
*device
,
1147 struct btrfs_path
*path
;
1148 struct btrfs_root
*root
= device
->dev_root
;
1149 struct btrfs_key key
;
1150 struct btrfs_key found_key
;
1151 struct extent_buffer
*leaf
= NULL
;
1152 struct btrfs_dev_extent
*extent
= NULL
;
1154 path
= btrfs_alloc_path();
1158 key
.objectid
= device
->devid
;
1160 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1162 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1164 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
1165 BTRFS_DEV_EXTENT_KEY
);
1168 leaf
= path
->nodes
[0];
1169 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1170 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1171 struct btrfs_dev_extent
);
1172 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
1173 btrfs_dev_extent_length(leaf
, extent
) < start
);
1175 btrfs_release_path(path
);
1177 } else if (ret
== 0) {
1178 leaf
= path
->nodes
[0];
1179 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1180 struct btrfs_dev_extent
);
1182 btrfs_error(root
->fs_info
, ret
, "Slot search failed");
1186 if (device
->bytes_used
> 0) {
1187 u64 len
= btrfs_dev_extent_length(leaf
, extent
);
1188 device
->bytes_used
-= len
;
1189 spin_lock(&root
->fs_info
->free_chunk_lock
);
1190 root
->fs_info
->free_chunk_space
+= len
;
1191 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1193 ret
= btrfs_del_item(trans
, root
, path
);
1195 btrfs_error(root
->fs_info
, ret
,
1196 "Failed to remove dev extent item");
1199 btrfs_free_path(path
);
1203 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
1204 struct btrfs_device
*device
,
1205 u64 chunk_tree
, u64 chunk_objectid
,
1206 u64 chunk_offset
, u64 start
, u64 num_bytes
)
1209 struct btrfs_path
*path
;
1210 struct btrfs_root
*root
= device
->dev_root
;
1211 struct btrfs_dev_extent
*extent
;
1212 struct extent_buffer
*leaf
;
1213 struct btrfs_key key
;
1215 WARN_ON(!device
->in_fs_metadata
);
1216 WARN_ON(device
->is_tgtdev_for_dev_replace
);
1217 path
= btrfs_alloc_path();
1221 key
.objectid
= device
->devid
;
1223 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1224 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1229 leaf
= path
->nodes
[0];
1230 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1231 struct btrfs_dev_extent
);
1232 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
1233 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
1234 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1236 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1237 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
1240 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1241 btrfs_mark_buffer_dirty(leaf
);
1243 btrfs_free_path(path
);
1247 static noinline
int find_next_chunk(struct btrfs_root
*root
,
1248 u64 objectid
, u64
*offset
)
1250 struct btrfs_path
*path
;
1252 struct btrfs_key key
;
1253 struct btrfs_chunk
*chunk
;
1254 struct btrfs_key found_key
;
1256 path
= btrfs_alloc_path();
1260 key
.objectid
= objectid
;
1261 key
.offset
= (u64
)-1;
1262 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1264 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1268 BUG_ON(ret
== 0); /* Corruption */
1270 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
1274 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1276 if (found_key
.objectid
!= objectid
)
1279 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1280 struct btrfs_chunk
);
1281 *offset
= found_key
.offset
+
1282 btrfs_chunk_length(path
->nodes
[0], chunk
);
1287 btrfs_free_path(path
);
1291 static noinline
int find_next_devid(struct btrfs_root
*root
, u64
*objectid
)
1294 struct btrfs_key key
;
1295 struct btrfs_key found_key
;
1296 struct btrfs_path
*path
;
1298 root
= root
->fs_info
->chunk_root
;
1300 path
= btrfs_alloc_path();
1304 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1305 key
.type
= BTRFS_DEV_ITEM_KEY
;
1306 key
.offset
= (u64
)-1;
1308 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1312 BUG_ON(ret
== 0); /* Corruption */
1314 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
1315 BTRFS_DEV_ITEM_KEY
);
1319 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1321 *objectid
= found_key
.offset
+ 1;
1325 btrfs_free_path(path
);
1330 * the device information is stored in the chunk root
1331 * the btrfs_device struct should be fully filled in
1333 static int btrfs_add_device(struct btrfs_trans_handle
*trans
,
1334 struct btrfs_root
*root
,
1335 struct btrfs_device
*device
)
1338 struct btrfs_path
*path
;
1339 struct btrfs_dev_item
*dev_item
;
1340 struct extent_buffer
*leaf
;
1341 struct btrfs_key key
;
1344 root
= root
->fs_info
->chunk_root
;
1346 path
= btrfs_alloc_path();
1350 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1351 key
.type
= BTRFS_DEV_ITEM_KEY
;
1352 key
.offset
= device
->devid
;
1354 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1359 leaf
= path
->nodes
[0];
1360 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1362 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1363 btrfs_set_device_generation(leaf
, dev_item
, 0);
1364 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1365 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1366 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1367 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1368 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1369 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1370 btrfs_set_device_group(leaf
, dev_item
, 0);
1371 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1372 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1373 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1375 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1376 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1377 ptr
= (unsigned long)btrfs_device_fsid(dev_item
);
1378 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1379 btrfs_mark_buffer_dirty(leaf
);
1383 btrfs_free_path(path
);
1387 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1388 struct btrfs_device
*device
)
1391 struct btrfs_path
*path
;
1392 struct btrfs_key key
;
1393 struct btrfs_trans_handle
*trans
;
1395 root
= root
->fs_info
->chunk_root
;
1397 path
= btrfs_alloc_path();
1401 trans
= btrfs_start_transaction(root
, 0);
1402 if (IS_ERR(trans
)) {
1403 btrfs_free_path(path
);
1404 return PTR_ERR(trans
);
1406 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1407 key
.type
= BTRFS_DEV_ITEM_KEY
;
1408 key
.offset
= device
->devid
;
1411 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1420 ret
= btrfs_del_item(trans
, root
, path
);
1424 btrfs_free_path(path
);
1425 unlock_chunks(root
);
1426 btrfs_commit_transaction(trans
, root
);
1430 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1432 struct btrfs_device
*device
;
1433 struct btrfs_device
*next_device
;
1434 struct block_device
*bdev
;
1435 struct buffer_head
*bh
= NULL
;
1436 struct btrfs_super_block
*disk_super
;
1437 struct btrfs_fs_devices
*cur_devices
;
1444 bool clear_super
= false;
1446 mutex_lock(&uuid_mutex
);
1449 seq
= read_seqbegin(&root
->fs_info
->profiles_lock
);
1451 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1452 root
->fs_info
->avail_system_alloc_bits
|
1453 root
->fs_info
->avail_metadata_alloc_bits
;
1454 } while (read_seqretry(&root
->fs_info
->profiles_lock
, seq
));
1456 num_devices
= root
->fs_info
->fs_devices
->num_devices
;
1457 btrfs_dev_replace_lock(&root
->fs_info
->dev_replace
);
1458 if (btrfs_dev_replace_is_ongoing(&root
->fs_info
->dev_replace
)) {
1459 WARN_ON(num_devices
< 1);
1462 btrfs_dev_replace_unlock(&root
->fs_info
->dev_replace
);
1464 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) && num_devices
<= 4) {
1465 printk(KERN_ERR
"btrfs: unable to go below four devices "
1471 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) && num_devices
<= 2) {
1472 printk(KERN_ERR
"btrfs: unable to go below two "
1473 "devices on raid1\n");
1478 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID5
) &&
1479 root
->fs_info
->fs_devices
->rw_devices
<= 2) {
1480 printk(KERN_ERR
"btrfs: unable to go below two "
1481 "devices on raid5\n");
1485 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID6
) &&
1486 root
->fs_info
->fs_devices
->rw_devices
<= 3) {
1487 printk(KERN_ERR
"btrfs: unable to go below three "
1488 "devices on raid6\n");
1493 if (strcmp(device_path
, "missing") == 0) {
1494 struct list_head
*devices
;
1495 struct btrfs_device
*tmp
;
1498 devices
= &root
->fs_info
->fs_devices
->devices
;
1500 * It is safe to read the devices since the volume_mutex
1503 list_for_each_entry(tmp
, devices
, dev_list
) {
1504 if (tmp
->in_fs_metadata
&&
1505 !tmp
->is_tgtdev_for_dev_replace
&&
1515 printk(KERN_ERR
"btrfs: no missing devices found to "
1520 ret
= btrfs_get_bdev_and_sb(device_path
,
1521 FMODE_WRITE
| FMODE_EXCL
,
1522 root
->fs_info
->bdev_holder
, 0,
1526 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1527 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1528 dev_uuid
= disk_super
->dev_item
.uuid
;
1529 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
1537 if (device
->is_tgtdev_for_dev_replace
) {
1538 pr_err("btrfs: unable to remove the dev_replace target dev\n");
1543 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1544 printk(KERN_ERR
"btrfs: unable to remove the only writeable "
1550 if (device
->writeable
) {
1552 list_del_init(&device
->dev_alloc_list
);
1553 unlock_chunks(root
);
1554 root
->fs_info
->fs_devices
->rw_devices
--;
1558 ret
= btrfs_shrink_device(device
, 0);
1563 * TODO: the superblock still includes this device in its num_devices
1564 * counter although write_all_supers() is not locked out. This
1565 * could give a filesystem state which requires a degraded mount.
1567 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1571 spin_lock(&root
->fs_info
->free_chunk_lock
);
1572 root
->fs_info
->free_chunk_space
= device
->total_bytes
-
1574 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1576 device
->in_fs_metadata
= 0;
1577 btrfs_scrub_cancel_dev(root
->fs_info
, device
);
1580 * the device list mutex makes sure that we don't change
1581 * the device list while someone else is writing out all
1582 * the device supers.
1585 cur_devices
= device
->fs_devices
;
1586 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1587 list_del_rcu(&device
->dev_list
);
1589 device
->fs_devices
->num_devices
--;
1590 device
->fs_devices
->total_devices
--;
1592 if (device
->missing
)
1593 root
->fs_info
->fs_devices
->missing_devices
--;
1595 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1596 struct btrfs_device
, dev_list
);
1597 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1598 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1599 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1600 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1603 device
->fs_devices
->open_devices
--;
1605 call_rcu(&device
->rcu
, free_device
);
1606 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1608 num_devices
= btrfs_super_num_devices(root
->fs_info
->super_copy
) - 1;
1609 btrfs_set_super_num_devices(root
->fs_info
->super_copy
, num_devices
);
1611 if (cur_devices
->open_devices
== 0) {
1612 struct btrfs_fs_devices
*fs_devices
;
1613 fs_devices
= root
->fs_info
->fs_devices
;
1614 while (fs_devices
) {
1615 if (fs_devices
->seed
== cur_devices
)
1617 fs_devices
= fs_devices
->seed
;
1619 fs_devices
->seed
= cur_devices
->seed
;
1620 cur_devices
->seed
= NULL
;
1622 __btrfs_close_devices(cur_devices
);
1623 unlock_chunks(root
);
1624 free_fs_devices(cur_devices
);
1627 root
->fs_info
->num_tolerated_disk_barrier_failures
=
1628 btrfs_calc_num_tolerated_disk_barrier_failures(root
->fs_info
);
1631 * at this point, the device is zero sized. We want to
1632 * remove it from the devices list and zero out the old super
1634 if (clear_super
&& disk_super
) {
1635 /* make sure this device isn't detected as part of
1638 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1639 set_buffer_dirty(bh
);
1640 sync_dirty_buffer(bh
);
1645 /* Notify udev that device has changed */
1647 btrfs_kobject_uevent(bdev
, KOBJ_CHANGE
);
1652 blkdev_put(bdev
, FMODE_READ
| FMODE_EXCL
);
1654 mutex_unlock(&uuid_mutex
);
1657 if (device
->writeable
) {
1659 list_add(&device
->dev_alloc_list
,
1660 &root
->fs_info
->fs_devices
->alloc_list
);
1661 unlock_chunks(root
);
1662 root
->fs_info
->fs_devices
->rw_devices
++;
1667 void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info
*fs_info
,
1668 struct btrfs_device
*srcdev
)
1670 WARN_ON(!mutex_is_locked(&fs_info
->fs_devices
->device_list_mutex
));
1671 list_del_rcu(&srcdev
->dev_list
);
1672 list_del_rcu(&srcdev
->dev_alloc_list
);
1673 fs_info
->fs_devices
->num_devices
--;
1674 if (srcdev
->missing
) {
1675 fs_info
->fs_devices
->missing_devices
--;
1676 fs_info
->fs_devices
->rw_devices
++;
1678 if (srcdev
->can_discard
)
1679 fs_info
->fs_devices
->num_can_discard
--;
1681 fs_info
->fs_devices
->open_devices
--;
1683 call_rcu(&srcdev
->rcu
, free_device
);
1686 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info
*fs_info
,
1687 struct btrfs_device
*tgtdev
)
1689 struct btrfs_device
*next_device
;
1692 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
1694 btrfs_scratch_superblock(tgtdev
);
1695 fs_info
->fs_devices
->open_devices
--;
1697 fs_info
->fs_devices
->num_devices
--;
1698 if (tgtdev
->can_discard
)
1699 fs_info
->fs_devices
->num_can_discard
++;
1701 next_device
= list_entry(fs_info
->fs_devices
->devices
.next
,
1702 struct btrfs_device
, dev_list
);
1703 if (tgtdev
->bdev
== fs_info
->sb
->s_bdev
)
1704 fs_info
->sb
->s_bdev
= next_device
->bdev
;
1705 if (tgtdev
->bdev
== fs_info
->fs_devices
->latest_bdev
)
1706 fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1707 list_del_rcu(&tgtdev
->dev_list
);
1709 call_rcu(&tgtdev
->rcu
, free_device
);
1711 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
1714 static int btrfs_find_device_by_path(struct btrfs_root
*root
, char *device_path
,
1715 struct btrfs_device
**device
)
1718 struct btrfs_super_block
*disk_super
;
1721 struct block_device
*bdev
;
1722 struct buffer_head
*bh
;
1725 ret
= btrfs_get_bdev_and_sb(device_path
, FMODE_READ
,
1726 root
->fs_info
->bdev_holder
, 0, &bdev
, &bh
);
1729 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1730 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1731 dev_uuid
= disk_super
->dev_item
.uuid
;
1732 *device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
1737 blkdev_put(bdev
, FMODE_READ
);
1741 int btrfs_find_device_missing_or_by_path(struct btrfs_root
*root
,
1743 struct btrfs_device
**device
)
1746 if (strcmp(device_path
, "missing") == 0) {
1747 struct list_head
*devices
;
1748 struct btrfs_device
*tmp
;
1750 devices
= &root
->fs_info
->fs_devices
->devices
;
1752 * It is safe to read the devices since the volume_mutex
1753 * is held by the caller.
1755 list_for_each_entry(tmp
, devices
, dev_list
) {
1756 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1763 pr_err("btrfs: no missing device found\n");
1769 return btrfs_find_device_by_path(root
, device_path
, device
);
1774 * does all the dirty work required for changing file system's UUID.
1776 static int btrfs_prepare_sprout(struct btrfs_root
*root
)
1778 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1779 struct btrfs_fs_devices
*old_devices
;
1780 struct btrfs_fs_devices
*seed_devices
;
1781 struct btrfs_super_block
*disk_super
= root
->fs_info
->super_copy
;
1782 struct btrfs_device
*device
;
1785 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1786 if (!fs_devices
->seeding
)
1789 seed_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
1793 old_devices
= clone_fs_devices(fs_devices
);
1794 if (IS_ERR(old_devices
)) {
1795 kfree(seed_devices
);
1796 return PTR_ERR(old_devices
);
1799 list_add(&old_devices
->list
, &fs_uuids
);
1801 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1802 seed_devices
->opened
= 1;
1803 INIT_LIST_HEAD(&seed_devices
->devices
);
1804 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1805 mutex_init(&seed_devices
->device_list_mutex
);
1807 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1808 list_splice_init_rcu(&fs_devices
->devices
, &seed_devices
->devices
,
1810 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1812 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1813 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1814 device
->fs_devices
= seed_devices
;
1817 fs_devices
->seeding
= 0;
1818 fs_devices
->num_devices
= 0;
1819 fs_devices
->open_devices
= 0;
1820 fs_devices
->total_devices
= 0;
1821 fs_devices
->seed
= seed_devices
;
1823 generate_random_uuid(fs_devices
->fsid
);
1824 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1825 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1826 super_flags
= btrfs_super_flags(disk_super
) &
1827 ~BTRFS_SUPER_FLAG_SEEDING
;
1828 btrfs_set_super_flags(disk_super
, super_flags
);
1834 * strore the expected generation for seed devices in device items.
1836 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
1837 struct btrfs_root
*root
)
1839 struct btrfs_path
*path
;
1840 struct extent_buffer
*leaf
;
1841 struct btrfs_dev_item
*dev_item
;
1842 struct btrfs_device
*device
;
1843 struct btrfs_key key
;
1844 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1845 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1849 path
= btrfs_alloc_path();
1853 root
= root
->fs_info
->chunk_root
;
1854 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1856 key
.type
= BTRFS_DEV_ITEM_KEY
;
1859 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1863 leaf
= path
->nodes
[0];
1865 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1866 ret
= btrfs_next_leaf(root
, path
);
1871 leaf
= path
->nodes
[0];
1872 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1873 btrfs_release_path(path
);
1877 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1878 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
1879 key
.type
!= BTRFS_DEV_ITEM_KEY
)
1882 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1883 struct btrfs_dev_item
);
1884 devid
= btrfs_device_id(leaf
, dev_item
);
1885 read_extent_buffer(leaf
, dev_uuid
,
1886 (unsigned long)btrfs_device_uuid(dev_item
),
1888 read_extent_buffer(leaf
, fs_uuid
,
1889 (unsigned long)btrfs_device_fsid(dev_item
),
1891 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
1893 BUG_ON(!device
); /* Logic error */
1895 if (device
->fs_devices
->seeding
) {
1896 btrfs_set_device_generation(leaf
, dev_item
,
1897 device
->generation
);
1898 btrfs_mark_buffer_dirty(leaf
);
1906 btrfs_free_path(path
);
1910 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
1912 struct request_queue
*q
;
1913 struct btrfs_trans_handle
*trans
;
1914 struct btrfs_device
*device
;
1915 struct block_device
*bdev
;
1916 struct list_head
*devices
;
1917 struct super_block
*sb
= root
->fs_info
->sb
;
1918 struct rcu_string
*name
;
1920 int seeding_dev
= 0;
1923 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
1926 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
1927 root
->fs_info
->bdev_holder
);
1929 return PTR_ERR(bdev
);
1931 if (root
->fs_info
->fs_devices
->seeding
) {
1933 down_write(&sb
->s_umount
);
1934 mutex_lock(&uuid_mutex
);
1937 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
1939 devices
= &root
->fs_info
->fs_devices
->devices
;
1941 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1942 list_for_each_entry(device
, devices
, dev_list
) {
1943 if (device
->bdev
== bdev
) {
1946 &root
->fs_info
->fs_devices
->device_list_mutex
);
1950 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1952 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1954 /* we can safely leave the fs_devices entry around */
1959 name
= rcu_string_strdup(device_path
, GFP_NOFS
);
1965 rcu_assign_pointer(device
->name
, name
);
1967 ret
= find_next_devid(root
, &device
->devid
);
1969 rcu_string_free(device
->name
);
1974 trans
= btrfs_start_transaction(root
, 0);
1975 if (IS_ERR(trans
)) {
1976 rcu_string_free(device
->name
);
1978 ret
= PTR_ERR(trans
);
1984 q
= bdev_get_queue(bdev
);
1985 if (blk_queue_discard(q
))
1986 device
->can_discard
= 1;
1987 device
->writeable
= 1;
1988 device
->work
.func
= pending_bios_fn
;
1989 generate_random_uuid(device
->uuid
);
1990 spin_lock_init(&device
->io_lock
);
1991 device
->generation
= trans
->transid
;
1992 device
->io_width
= root
->sectorsize
;
1993 device
->io_align
= root
->sectorsize
;
1994 device
->sector_size
= root
->sectorsize
;
1995 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1996 device
->disk_total_bytes
= device
->total_bytes
;
1997 device
->dev_root
= root
->fs_info
->dev_root
;
1998 device
->bdev
= bdev
;
1999 device
->in_fs_metadata
= 1;
2000 device
->is_tgtdev_for_dev_replace
= 0;
2001 device
->mode
= FMODE_EXCL
;
2002 set_blocksize(device
->bdev
, 4096);
2005 sb
->s_flags
&= ~MS_RDONLY
;
2006 ret
= btrfs_prepare_sprout(root
);
2007 BUG_ON(ret
); /* -ENOMEM */
2010 device
->fs_devices
= root
->fs_info
->fs_devices
;
2012 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2013 list_add_rcu(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
2014 list_add(&device
->dev_alloc_list
,
2015 &root
->fs_info
->fs_devices
->alloc_list
);
2016 root
->fs_info
->fs_devices
->num_devices
++;
2017 root
->fs_info
->fs_devices
->open_devices
++;
2018 root
->fs_info
->fs_devices
->rw_devices
++;
2019 root
->fs_info
->fs_devices
->total_devices
++;
2020 if (device
->can_discard
)
2021 root
->fs_info
->fs_devices
->num_can_discard
++;
2022 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
2024 spin_lock(&root
->fs_info
->free_chunk_lock
);
2025 root
->fs_info
->free_chunk_space
+= device
->total_bytes
;
2026 spin_unlock(&root
->fs_info
->free_chunk_lock
);
2028 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
2029 root
->fs_info
->fs_devices
->rotating
= 1;
2031 total_bytes
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
2032 btrfs_set_super_total_bytes(root
->fs_info
->super_copy
,
2033 total_bytes
+ device
->total_bytes
);
2035 total_bytes
= btrfs_super_num_devices(root
->fs_info
->super_copy
);
2036 btrfs_set_super_num_devices(root
->fs_info
->super_copy
,
2038 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2041 ret
= init_first_rw_device(trans
, root
, device
);
2043 btrfs_abort_transaction(trans
, root
, ret
);
2046 ret
= btrfs_finish_sprout(trans
, root
);
2048 btrfs_abort_transaction(trans
, root
, ret
);
2052 ret
= btrfs_add_device(trans
, root
, device
);
2054 btrfs_abort_transaction(trans
, root
, ret
);
2060 * we've got more storage, clear any full flags on the space
2063 btrfs_clear_space_info_full(root
->fs_info
);
2065 unlock_chunks(root
);
2066 root
->fs_info
->num_tolerated_disk_barrier_failures
=
2067 btrfs_calc_num_tolerated_disk_barrier_failures(root
->fs_info
);
2068 ret
= btrfs_commit_transaction(trans
, root
);
2071 mutex_unlock(&uuid_mutex
);
2072 up_write(&sb
->s_umount
);
2074 if (ret
) /* transaction commit */
2077 ret
= btrfs_relocate_sys_chunks(root
);
2079 btrfs_error(root
->fs_info
, ret
,
2080 "Failed to relocate sys chunks after "
2081 "device initialization. This can be fixed "
2082 "using the \"btrfs balance\" command.");
2083 trans
= btrfs_attach_transaction(root
);
2084 if (IS_ERR(trans
)) {
2085 if (PTR_ERR(trans
) == -ENOENT
)
2087 return PTR_ERR(trans
);
2089 ret
= btrfs_commit_transaction(trans
, root
);
2095 unlock_chunks(root
);
2096 btrfs_end_transaction(trans
, root
);
2097 rcu_string_free(device
->name
);
2100 blkdev_put(bdev
, FMODE_EXCL
);
2102 mutex_unlock(&uuid_mutex
);
2103 up_write(&sb
->s_umount
);
2108 int btrfs_init_dev_replace_tgtdev(struct btrfs_root
*root
, char *device_path
,
2109 struct btrfs_device
**device_out
)
2111 struct request_queue
*q
;
2112 struct btrfs_device
*device
;
2113 struct block_device
*bdev
;
2114 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2115 struct list_head
*devices
;
2116 struct rcu_string
*name
;
2120 if (fs_info
->fs_devices
->seeding
)
2123 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
2124 fs_info
->bdev_holder
);
2126 return PTR_ERR(bdev
);
2128 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
2130 devices
= &fs_info
->fs_devices
->devices
;
2131 list_for_each_entry(device
, devices
, dev_list
) {
2132 if (device
->bdev
== bdev
) {
2138 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
2144 name
= rcu_string_strdup(device_path
, GFP_NOFS
);
2150 rcu_assign_pointer(device
->name
, name
);
2152 q
= bdev_get_queue(bdev
);
2153 if (blk_queue_discard(q
))
2154 device
->can_discard
= 1;
2155 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2156 device
->writeable
= 1;
2157 device
->work
.func
= pending_bios_fn
;
2158 generate_random_uuid(device
->uuid
);
2159 device
->devid
= BTRFS_DEV_REPLACE_DEVID
;
2160 spin_lock_init(&device
->io_lock
);
2161 device
->generation
= 0;
2162 device
->io_width
= root
->sectorsize
;
2163 device
->io_align
= root
->sectorsize
;
2164 device
->sector_size
= root
->sectorsize
;
2165 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
2166 device
->disk_total_bytes
= device
->total_bytes
;
2167 device
->dev_root
= fs_info
->dev_root
;
2168 device
->bdev
= bdev
;
2169 device
->in_fs_metadata
= 1;
2170 device
->is_tgtdev_for_dev_replace
= 1;
2171 device
->mode
= FMODE_EXCL
;
2172 set_blocksize(device
->bdev
, 4096);
2173 device
->fs_devices
= fs_info
->fs_devices
;
2174 list_add(&device
->dev_list
, &fs_info
->fs_devices
->devices
);
2175 fs_info
->fs_devices
->num_devices
++;
2176 fs_info
->fs_devices
->open_devices
++;
2177 if (device
->can_discard
)
2178 fs_info
->fs_devices
->num_can_discard
++;
2179 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2181 *device_out
= device
;
2185 blkdev_put(bdev
, FMODE_EXCL
);
2189 void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info
*fs_info
,
2190 struct btrfs_device
*tgtdev
)
2192 WARN_ON(fs_info
->fs_devices
->rw_devices
== 0);
2193 tgtdev
->io_width
= fs_info
->dev_root
->sectorsize
;
2194 tgtdev
->io_align
= fs_info
->dev_root
->sectorsize
;
2195 tgtdev
->sector_size
= fs_info
->dev_root
->sectorsize
;
2196 tgtdev
->dev_root
= fs_info
->dev_root
;
2197 tgtdev
->in_fs_metadata
= 1;
2200 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
2201 struct btrfs_device
*device
)
2204 struct btrfs_path
*path
;
2205 struct btrfs_root
*root
;
2206 struct btrfs_dev_item
*dev_item
;
2207 struct extent_buffer
*leaf
;
2208 struct btrfs_key key
;
2210 root
= device
->dev_root
->fs_info
->chunk_root
;
2212 path
= btrfs_alloc_path();
2216 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
2217 key
.type
= BTRFS_DEV_ITEM_KEY
;
2218 key
.offset
= device
->devid
;
2220 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2229 leaf
= path
->nodes
[0];
2230 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
2232 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
2233 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
2234 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
2235 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
2236 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
2237 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
2238 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
2239 btrfs_mark_buffer_dirty(leaf
);
2242 btrfs_free_path(path
);
2246 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
2247 struct btrfs_device
*device
, u64 new_size
)
2249 struct btrfs_super_block
*super_copy
=
2250 device
->dev_root
->fs_info
->super_copy
;
2251 u64 old_total
= btrfs_super_total_bytes(super_copy
);
2252 u64 diff
= new_size
- device
->total_bytes
;
2254 if (!device
->writeable
)
2256 if (new_size
<= device
->total_bytes
||
2257 device
->is_tgtdev_for_dev_replace
)
2260 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
2261 device
->fs_devices
->total_rw_bytes
+= diff
;
2263 device
->total_bytes
= new_size
;
2264 device
->disk_total_bytes
= new_size
;
2265 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
2267 return btrfs_update_device(trans
, device
);
2270 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
2271 struct btrfs_device
*device
, u64 new_size
)
2274 lock_chunks(device
->dev_root
);
2275 ret
= __btrfs_grow_device(trans
, device
, new_size
);
2276 unlock_chunks(device
->dev_root
);
2280 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
2281 struct btrfs_root
*root
,
2282 u64 chunk_tree
, u64 chunk_objectid
,
2286 struct btrfs_path
*path
;
2287 struct btrfs_key key
;
2289 root
= root
->fs_info
->chunk_root
;
2290 path
= btrfs_alloc_path();
2294 key
.objectid
= chunk_objectid
;
2295 key
.offset
= chunk_offset
;
2296 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2298 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2301 else if (ret
> 0) { /* Logic error or corruption */
2302 btrfs_error(root
->fs_info
, -ENOENT
,
2303 "Failed lookup while freeing chunk.");
2308 ret
= btrfs_del_item(trans
, root
, path
);
2310 btrfs_error(root
->fs_info
, ret
,
2311 "Failed to delete chunk item.");
2313 btrfs_free_path(path
);
2317 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
2320 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
2321 struct btrfs_disk_key
*disk_key
;
2322 struct btrfs_chunk
*chunk
;
2329 struct btrfs_key key
;
2331 array_size
= btrfs_super_sys_array_size(super_copy
);
2333 ptr
= super_copy
->sys_chunk_array
;
2336 while (cur
< array_size
) {
2337 disk_key
= (struct btrfs_disk_key
*)ptr
;
2338 btrfs_disk_key_to_cpu(&key
, disk_key
);
2340 len
= sizeof(*disk_key
);
2342 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
2343 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
2344 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
2345 len
+= btrfs_chunk_item_size(num_stripes
);
2350 if (key
.objectid
== chunk_objectid
&&
2351 key
.offset
== chunk_offset
) {
2352 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
2354 btrfs_set_super_sys_array_size(super_copy
, array_size
);
2363 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
2364 u64 chunk_tree
, u64 chunk_objectid
,
2367 struct extent_map_tree
*em_tree
;
2368 struct btrfs_root
*extent_root
;
2369 struct btrfs_trans_handle
*trans
;
2370 struct extent_map
*em
;
2371 struct map_lookup
*map
;
2375 root
= root
->fs_info
->chunk_root
;
2376 extent_root
= root
->fs_info
->extent_root
;
2377 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
2379 ret
= btrfs_can_relocate(extent_root
, chunk_offset
);
2383 /* step one, relocate all the extents inside this chunk */
2384 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
2388 trans
= btrfs_start_transaction(root
, 0);
2389 if (IS_ERR(trans
)) {
2390 ret
= PTR_ERR(trans
);
2391 btrfs_std_error(root
->fs_info
, ret
);
2398 * step two, delete the device extents and the
2399 * chunk tree entries
2401 read_lock(&em_tree
->lock
);
2402 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
2403 read_unlock(&em_tree
->lock
);
2405 BUG_ON(!em
|| em
->start
> chunk_offset
||
2406 em
->start
+ em
->len
< chunk_offset
);
2407 map
= (struct map_lookup
*)em
->bdev
;
2409 for (i
= 0; i
< map
->num_stripes
; i
++) {
2410 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
2411 map
->stripes
[i
].physical
);
2414 if (map
->stripes
[i
].dev
) {
2415 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
2419 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
2424 trace_btrfs_chunk_free(root
, map
, chunk_offset
, em
->len
);
2426 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2427 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
2431 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
2434 write_lock(&em_tree
->lock
);
2435 remove_extent_mapping(em_tree
, em
);
2436 write_unlock(&em_tree
->lock
);
2441 /* once for the tree */
2442 free_extent_map(em
);
2444 free_extent_map(em
);
2446 unlock_chunks(root
);
2447 btrfs_end_transaction(trans
, root
);
2451 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
2453 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
2454 struct btrfs_path
*path
;
2455 struct extent_buffer
*leaf
;
2456 struct btrfs_chunk
*chunk
;
2457 struct btrfs_key key
;
2458 struct btrfs_key found_key
;
2459 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
2461 bool retried
= false;
2465 path
= btrfs_alloc_path();
2470 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2471 key
.offset
= (u64
)-1;
2472 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2475 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2478 BUG_ON(ret
== 0); /* Corruption */
2480 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
2487 leaf
= path
->nodes
[0];
2488 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2490 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
2491 struct btrfs_chunk
);
2492 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2493 btrfs_release_path(path
);
2495 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2496 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
2505 if (found_key
.offset
== 0)
2507 key
.offset
= found_key
.offset
- 1;
2510 if (failed
&& !retried
) {
2514 } else if (failed
&& retried
) {
2519 btrfs_free_path(path
);
2523 static int insert_balance_item(struct btrfs_root
*root
,
2524 struct btrfs_balance_control
*bctl
)
2526 struct btrfs_trans_handle
*trans
;
2527 struct btrfs_balance_item
*item
;
2528 struct btrfs_disk_balance_args disk_bargs
;
2529 struct btrfs_path
*path
;
2530 struct extent_buffer
*leaf
;
2531 struct btrfs_key key
;
2534 path
= btrfs_alloc_path();
2538 trans
= btrfs_start_transaction(root
, 0);
2539 if (IS_ERR(trans
)) {
2540 btrfs_free_path(path
);
2541 return PTR_ERR(trans
);
2544 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2545 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2548 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2553 leaf
= path
->nodes
[0];
2554 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
2556 memset_extent_buffer(leaf
, 0, (unsigned long)item
, sizeof(*item
));
2558 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->data
);
2559 btrfs_set_balance_data(leaf
, item
, &disk_bargs
);
2560 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->meta
);
2561 btrfs_set_balance_meta(leaf
, item
, &disk_bargs
);
2562 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->sys
);
2563 btrfs_set_balance_sys(leaf
, item
, &disk_bargs
);
2565 btrfs_set_balance_flags(leaf
, item
, bctl
->flags
);
2567 btrfs_mark_buffer_dirty(leaf
);
2569 btrfs_free_path(path
);
2570 err
= btrfs_commit_transaction(trans
, root
);
2576 static int del_balance_item(struct btrfs_root
*root
)
2578 struct btrfs_trans_handle
*trans
;
2579 struct btrfs_path
*path
;
2580 struct btrfs_key key
;
2583 path
= btrfs_alloc_path();
2587 trans
= btrfs_start_transaction(root
, 0);
2588 if (IS_ERR(trans
)) {
2589 btrfs_free_path(path
);
2590 return PTR_ERR(trans
);
2593 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2594 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2597 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2605 ret
= btrfs_del_item(trans
, root
, path
);
2607 btrfs_free_path(path
);
2608 err
= btrfs_commit_transaction(trans
, root
);
2615 * This is a heuristic used to reduce the number of chunks balanced on
2616 * resume after balance was interrupted.
2618 static void update_balance_args(struct btrfs_balance_control
*bctl
)
2621 * Turn on soft mode for chunk types that were being converted.
2623 if (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2624 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2625 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2626 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2627 if (bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2628 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2631 * Turn on usage filter if is not already used. The idea is
2632 * that chunks that we have already balanced should be
2633 * reasonably full. Don't do it for chunks that are being
2634 * converted - that will keep us from relocating unconverted
2635 * (albeit full) chunks.
2637 if (!(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2638 !(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2639 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2640 bctl
->data
.usage
= 90;
2642 if (!(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2643 !(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2644 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2645 bctl
->sys
.usage
= 90;
2647 if (!(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2648 !(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2649 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2650 bctl
->meta
.usage
= 90;
2655 * Should be called with both balance and volume mutexes held to
2656 * serialize other volume operations (add_dev/rm_dev/resize) with
2657 * restriper. Same goes for unset_balance_control.
2659 static void set_balance_control(struct btrfs_balance_control
*bctl
)
2661 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
2663 BUG_ON(fs_info
->balance_ctl
);
2665 spin_lock(&fs_info
->balance_lock
);
2666 fs_info
->balance_ctl
= bctl
;
2667 spin_unlock(&fs_info
->balance_lock
);
2670 static void unset_balance_control(struct btrfs_fs_info
*fs_info
)
2672 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
2674 BUG_ON(!fs_info
->balance_ctl
);
2676 spin_lock(&fs_info
->balance_lock
);
2677 fs_info
->balance_ctl
= NULL
;
2678 spin_unlock(&fs_info
->balance_lock
);
2684 * Balance filters. Return 1 if chunk should be filtered out
2685 * (should not be balanced).
2687 static int chunk_profiles_filter(u64 chunk_type
,
2688 struct btrfs_balance_args
*bargs
)
2690 chunk_type
= chunk_to_extended(chunk_type
) &
2691 BTRFS_EXTENDED_PROFILE_MASK
;
2693 if (bargs
->profiles
& chunk_type
)
2699 static int chunk_usage_filter(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
,
2700 struct btrfs_balance_args
*bargs
)
2702 struct btrfs_block_group_cache
*cache
;
2703 u64 chunk_used
, user_thresh
;
2706 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
2707 chunk_used
= btrfs_block_group_used(&cache
->item
);
2709 if (bargs
->usage
== 0)
2711 else if (bargs
->usage
> 100)
2712 user_thresh
= cache
->key
.offset
;
2714 user_thresh
= div_factor_fine(cache
->key
.offset
,
2717 if (chunk_used
< user_thresh
)
2720 btrfs_put_block_group(cache
);
2724 static int chunk_devid_filter(struct extent_buffer
*leaf
,
2725 struct btrfs_chunk
*chunk
,
2726 struct btrfs_balance_args
*bargs
)
2728 struct btrfs_stripe
*stripe
;
2729 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2732 for (i
= 0; i
< num_stripes
; i
++) {
2733 stripe
= btrfs_stripe_nr(chunk
, i
);
2734 if (btrfs_stripe_devid(leaf
, stripe
) == bargs
->devid
)
2741 /* [pstart, pend) */
2742 static int chunk_drange_filter(struct extent_buffer
*leaf
,
2743 struct btrfs_chunk
*chunk
,
2745 struct btrfs_balance_args
*bargs
)
2747 struct btrfs_stripe
*stripe
;
2748 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2754 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
))
2757 if (btrfs_chunk_type(leaf
, chunk
) & (BTRFS_BLOCK_GROUP_DUP
|
2758 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
)) {
2759 factor
= num_stripes
/ 2;
2760 } else if (btrfs_chunk_type(leaf
, chunk
) & BTRFS_BLOCK_GROUP_RAID5
) {
2761 factor
= num_stripes
- 1;
2762 } else if (btrfs_chunk_type(leaf
, chunk
) & BTRFS_BLOCK_GROUP_RAID6
) {
2763 factor
= num_stripes
- 2;
2765 factor
= num_stripes
;
2768 for (i
= 0; i
< num_stripes
; i
++) {
2769 stripe
= btrfs_stripe_nr(chunk
, i
);
2770 if (btrfs_stripe_devid(leaf
, stripe
) != bargs
->devid
)
2773 stripe_offset
= btrfs_stripe_offset(leaf
, stripe
);
2774 stripe_length
= btrfs_chunk_length(leaf
, chunk
);
2775 do_div(stripe_length
, factor
);
2777 if (stripe_offset
< bargs
->pend
&&
2778 stripe_offset
+ stripe_length
> bargs
->pstart
)
2785 /* [vstart, vend) */
2786 static int chunk_vrange_filter(struct extent_buffer
*leaf
,
2787 struct btrfs_chunk
*chunk
,
2789 struct btrfs_balance_args
*bargs
)
2791 if (chunk_offset
< bargs
->vend
&&
2792 chunk_offset
+ btrfs_chunk_length(leaf
, chunk
) > bargs
->vstart
)
2793 /* at least part of the chunk is inside this vrange */
2799 static int chunk_soft_convert_filter(u64 chunk_type
,
2800 struct btrfs_balance_args
*bargs
)
2802 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_CONVERT
))
2805 chunk_type
= chunk_to_extended(chunk_type
) &
2806 BTRFS_EXTENDED_PROFILE_MASK
;
2808 if (bargs
->target
== chunk_type
)
2814 static int should_balance_chunk(struct btrfs_root
*root
,
2815 struct extent_buffer
*leaf
,
2816 struct btrfs_chunk
*chunk
, u64 chunk_offset
)
2818 struct btrfs_balance_control
*bctl
= root
->fs_info
->balance_ctl
;
2819 struct btrfs_balance_args
*bargs
= NULL
;
2820 u64 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2823 if (!((chunk_type
& BTRFS_BLOCK_GROUP_TYPE_MASK
) &
2824 (bctl
->flags
& BTRFS_BALANCE_TYPE_MASK
))) {
2828 if (chunk_type
& BTRFS_BLOCK_GROUP_DATA
)
2829 bargs
= &bctl
->data
;
2830 else if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
)
2832 else if (chunk_type
& BTRFS_BLOCK_GROUP_METADATA
)
2833 bargs
= &bctl
->meta
;
2835 /* profiles filter */
2836 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_PROFILES
) &&
2837 chunk_profiles_filter(chunk_type
, bargs
)) {
2842 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2843 chunk_usage_filter(bctl
->fs_info
, chunk_offset
, bargs
)) {
2848 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
) &&
2849 chunk_devid_filter(leaf
, chunk
, bargs
)) {
2853 /* drange filter, makes sense only with devid filter */
2854 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DRANGE
) &&
2855 chunk_drange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
2860 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_VRANGE
) &&
2861 chunk_vrange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
2865 /* soft profile changing mode */
2866 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_SOFT
) &&
2867 chunk_soft_convert_filter(chunk_type
, bargs
)) {
2874 static int __btrfs_balance(struct btrfs_fs_info
*fs_info
)
2876 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
2877 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
2878 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
2879 struct list_head
*devices
;
2880 struct btrfs_device
*device
;
2883 struct btrfs_chunk
*chunk
;
2884 struct btrfs_path
*path
;
2885 struct btrfs_key key
;
2886 struct btrfs_key found_key
;
2887 struct btrfs_trans_handle
*trans
;
2888 struct extent_buffer
*leaf
;
2891 int enospc_errors
= 0;
2892 bool counting
= true;
2894 /* step one make some room on all the devices */
2895 devices
= &fs_info
->fs_devices
->devices
;
2896 list_for_each_entry(device
, devices
, dev_list
) {
2897 old_size
= device
->total_bytes
;
2898 size_to_free
= div_factor(old_size
, 1);
2899 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
2900 if (!device
->writeable
||
2901 device
->total_bytes
- device
->bytes_used
> size_to_free
||
2902 device
->is_tgtdev_for_dev_replace
)
2905 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
2910 trans
= btrfs_start_transaction(dev_root
, 0);
2911 BUG_ON(IS_ERR(trans
));
2913 ret
= btrfs_grow_device(trans
, device
, old_size
);
2916 btrfs_end_transaction(trans
, dev_root
);
2919 /* step two, relocate all the chunks */
2920 path
= btrfs_alloc_path();
2926 /* zero out stat counters */
2927 spin_lock(&fs_info
->balance_lock
);
2928 memset(&bctl
->stat
, 0, sizeof(bctl
->stat
));
2929 spin_unlock(&fs_info
->balance_lock
);
2931 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2932 key
.offset
= (u64
)-1;
2933 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2936 if ((!counting
&& atomic_read(&fs_info
->balance_pause_req
)) ||
2937 atomic_read(&fs_info
->balance_cancel_req
)) {
2942 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2947 * this shouldn't happen, it means the last relocate
2951 BUG(); /* FIXME break ? */
2953 ret
= btrfs_previous_item(chunk_root
, path
, 0,
2954 BTRFS_CHUNK_ITEM_KEY
);
2960 leaf
= path
->nodes
[0];
2961 slot
= path
->slots
[0];
2962 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2964 if (found_key
.objectid
!= key
.objectid
)
2967 /* chunk zero is special */
2968 if (found_key
.offset
== 0)
2971 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
2974 spin_lock(&fs_info
->balance_lock
);
2975 bctl
->stat
.considered
++;
2976 spin_unlock(&fs_info
->balance_lock
);
2979 ret
= should_balance_chunk(chunk_root
, leaf
, chunk
,
2981 btrfs_release_path(path
);
2986 spin_lock(&fs_info
->balance_lock
);
2987 bctl
->stat
.expected
++;
2988 spin_unlock(&fs_info
->balance_lock
);
2992 ret
= btrfs_relocate_chunk(chunk_root
,
2993 chunk_root
->root_key
.objectid
,
2996 if (ret
&& ret
!= -ENOSPC
)
2998 if (ret
== -ENOSPC
) {
3001 spin_lock(&fs_info
->balance_lock
);
3002 bctl
->stat
.completed
++;
3003 spin_unlock(&fs_info
->balance_lock
);
3006 key
.offset
= found_key
.offset
- 1;
3010 btrfs_release_path(path
);
3015 btrfs_free_path(path
);
3016 if (enospc_errors
) {
3017 printk(KERN_INFO
"btrfs: %d enospc errors during balance\n",
3027 * alloc_profile_is_valid - see if a given profile is valid and reduced
3028 * @flags: profile to validate
3029 * @extended: if true @flags is treated as an extended profile
3031 static int alloc_profile_is_valid(u64 flags
, int extended
)
3033 u64 mask
= (extended
? BTRFS_EXTENDED_PROFILE_MASK
:
3034 BTRFS_BLOCK_GROUP_PROFILE_MASK
);
3036 flags
&= ~BTRFS_BLOCK_GROUP_TYPE_MASK
;
3038 /* 1) check that all other bits are zeroed */
3042 /* 2) see if profile is reduced */
3044 return !extended
; /* "0" is valid for usual profiles */
3046 /* true if exactly one bit set */
3047 return (flags
& (flags
- 1)) == 0;
3050 static inline int balance_need_close(struct btrfs_fs_info
*fs_info
)
3052 /* cancel requested || normal exit path */
3053 return atomic_read(&fs_info
->balance_cancel_req
) ||
3054 (atomic_read(&fs_info
->balance_pause_req
) == 0 &&
3055 atomic_read(&fs_info
->balance_cancel_req
) == 0);
3058 static void __cancel_balance(struct btrfs_fs_info
*fs_info
)
3062 unset_balance_control(fs_info
);
3063 ret
= del_balance_item(fs_info
->tree_root
);
3065 btrfs_std_error(fs_info
, ret
);
3067 atomic_set(&fs_info
->mutually_exclusive_operation_running
, 0);
3070 void update_ioctl_balance_args(struct btrfs_fs_info
*fs_info
, int lock
,
3071 struct btrfs_ioctl_balance_args
*bargs
);
3074 * Should be called with both balance and volume mutexes held
3076 int btrfs_balance(struct btrfs_balance_control
*bctl
,
3077 struct btrfs_ioctl_balance_args
*bargs
)
3079 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
3086 if (btrfs_fs_closing(fs_info
) ||
3087 atomic_read(&fs_info
->balance_pause_req
) ||
3088 atomic_read(&fs_info
->balance_cancel_req
)) {
3093 allowed
= btrfs_super_incompat_flags(fs_info
->super_copy
);
3094 if (allowed
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
3098 * In case of mixed groups both data and meta should be picked,
3099 * and identical options should be given for both of them.
3101 allowed
= BTRFS_BALANCE_DATA
| BTRFS_BALANCE_METADATA
;
3102 if (mixed
&& (bctl
->flags
& allowed
)) {
3103 if (!(bctl
->flags
& BTRFS_BALANCE_DATA
) ||
3104 !(bctl
->flags
& BTRFS_BALANCE_METADATA
) ||
3105 memcmp(&bctl
->data
, &bctl
->meta
, sizeof(bctl
->data
))) {
3106 printk(KERN_ERR
"btrfs: with mixed groups data and "
3107 "metadata balance options must be the same\n");
3113 num_devices
= fs_info
->fs_devices
->num_devices
;
3114 btrfs_dev_replace_lock(&fs_info
->dev_replace
);
3115 if (btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
)) {
3116 BUG_ON(num_devices
< 1);
3119 btrfs_dev_replace_unlock(&fs_info
->dev_replace
);
3120 allowed
= BTRFS_AVAIL_ALLOC_BIT_SINGLE
;
3121 if (num_devices
== 1)
3122 allowed
|= BTRFS_BLOCK_GROUP_DUP
;
3123 else if (num_devices
< 4)
3124 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
);
3126 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
3127 BTRFS_BLOCK_GROUP_RAID10
|
3128 BTRFS_BLOCK_GROUP_RAID5
|
3129 BTRFS_BLOCK_GROUP_RAID6
);
3131 if ((bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3132 (!alloc_profile_is_valid(bctl
->data
.target
, 1) ||
3133 (bctl
->data
.target
& ~allowed
))) {
3134 printk(KERN_ERR
"btrfs: unable to start balance with target "
3135 "data profile %llu\n",
3136 (unsigned long long)bctl
->data
.target
);
3140 if ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3141 (!alloc_profile_is_valid(bctl
->meta
.target
, 1) ||
3142 (bctl
->meta
.target
& ~allowed
))) {
3143 printk(KERN_ERR
"btrfs: unable to start balance with target "
3144 "metadata profile %llu\n",
3145 (unsigned long long)bctl
->meta
.target
);
3149 if ((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3150 (!alloc_profile_is_valid(bctl
->sys
.target
, 1) ||
3151 (bctl
->sys
.target
& ~allowed
))) {
3152 printk(KERN_ERR
"btrfs: unable to start balance with target "
3153 "system profile %llu\n",
3154 (unsigned long long)bctl
->sys
.target
);
3159 /* allow dup'ed data chunks only in mixed mode */
3160 if (!mixed
&& (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3161 (bctl
->data
.target
& BTRFS_BLOCK_GROUP_DUP
)) {
3162 printk(KERN_ERR
"btrfs: dup for data is not allowed\n");
3167 /* allow to reduce meta or sys integrity only if force set */
3168 allowed
= BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
3169 BTRFS_BLOCK_GROUP_RAID10
|
3170 BTRFS_BLOCK_GROUP_RAID5
|
3171 BTRFS_BLOCK_GROUP_RAID6
;
3173 seq
= read_seqbegin(&fs_info
->profiles_lock
);
3175 if (((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3176 (fs_info
->avail_system_alloc_bits
& allowed
) &&
3177 !(bctl
->sys
.target
& allowed
)) ||
3178 ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3179 (fs_info
->avail_metadata_alloc_bits
& allowed
) &&
3180 !(bctl
->meta
.target
& allowed
))) {
3181 if (bctl
->flags
& BTRFS_BALANCE_FORCE
) {
3182 printk(KERN_INFO
"btrfs: force reducing metadata "
3185 printk(KERN_ERR
"btrfs: balance will reduce metadata "
3186 "integrity, use force if you want this\n");
3191 } while (read_seqretry(&fs_info
->profiles_lock
, seq
));
3193 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3194 int num_tolerated_disk_barrier_failures
;
3195 u64 target
= bctl
->sys
.target
;
3197 num_tolerated_disk_barrier_failures
=
3198 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info
);
3199 if (num_tolerated_disk_barrier_failures
> 0 &&
3201 (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID0
|
3202 BTRFS_AVAIL_ALLOC_BIT_SINGLE
)))
3203 num_tolerated_disk_barrier_failures
= 0;
3204 else if (num_tolerated_disk_barrier_failures
> 1 &&
3206 (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
)))
3207 num_tolerated_disk_barrier_failures
= 1;
3209 fs_info
->num_tolerated_disk_barrier_failures
=
3210 num_tolerated_disk_barrier_failures
;
3213 ret
= insert_balance_item(fs_info
->tree_root
, bctl
);
3214 if (ret
&& ret
!= -EEXIST
)
3217 if (!(bctl
->flags
& BTRFS_BALANCE_RESUME
)) {
3218 BUG_ON(ret
== -EEXIST
);
3219 set_balance_control(bctl
);
3221 BUG_ON(ret
!= -EEXIST
);
3222 spin_lock(&fs_info
->balance_lock
);
3223 update_balance_args(bctl
);
3224 spin_unlock(&fs_info
->balance_lock
);
3227 atomic_inc(&fs_info
->balance_running
);
3228 mutex_unlock(&fs_info
->balance_mutex
);
3230 ret
= __btrfs_balance(fs_info
);
3232 mutex_lock(&fs_info
->balance_mutex
);
3233 atomic_dec(&fs_info
->balance_running
);
3235 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3236 fs_info
->num_tolerated_disk_barrier_failures
=
3237 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info
);
3241 memset(bargs
, 0, sizeof(*bargs
));
3242 update_ioctl_balance_args(fs_info
, 0, bargs
);
3245 if ((ret
&& ret
!= -ECANCELED
&& ret
!= -ENOSPC
) ||
3246 balance_need_close(fs_info
)) {
3247 __cancel_balance(fs_info
);
3250 wake_up(&fs_info
->balance_wait_q
);
3254 if (bctl
->flags
& BTRFS_BALANCE_RESUME
)
3255 __cancel_balance(fs_info
);
3258 atomic_set(&fs_info
->mutually_exclusive_operation_running
, 0);
3263 static int balance_kthread(void *data
)
3265 struct btrfs_fs_info
*fs_info
= data
;
3268 mutex_lock(&fs_info
->volume_mutex
);
3269 mutex_lock(&fs_info
->balance_mutex
);
3271 if (fs_info
->balance_ctl
) {
3272 printk(KERN_INFO
"btrfs: continuing balance\n");
3273 ret
= btrfs_balance(fs_info
->balance_ctl
, NULL
);
3276 mutex_unlock(&fs_info
->balance_mutex
);
3277 mutex_unlock(&fs_info
->volume_mutex
);
3282 int btrfs_resume_balance_async(struct btrfs_fs_info
*fs_info
)
3284 struct task_struct
*tsk
;
3286 spin_lock(&fs_info
->balance_lock
);
3287 if (!fs_info
->balance_ctl
) {
3288 spin_unlock(&fs_info
->balance_lock
);
3291 spin_unlock(&fs_info
->balance_lock
);
3293 if (btrfs_test_opt(fs_info
->tree_root
, SKIP_BALANCE
)) {
3294 printk(KERN_INFO
"btrfs: force skipping balance\n");
3298 tsk
= kthread_run(balance_kthread
, fs_info
, "btrfs-balance");
3300 return PTR_ERR(tsk
);
3305 int btrfs_recover_balance(struct btrfs_fs_info
*fs_info
)
3307 struct btrfs_balance_control
*bctl
;
3308 struct btrfs_balance_item
*item
;
3309 struct btrfs_disk_balance_args disk_bargs
;
3310 struct btrfs_path
*path
;
3311 struct extent_buffer
*leaf
;
3312 struct btrfs_key key
;
3315 path
= btrfs_alloc_path();
3319 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
3320 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
3323 ret
= btrfs_search_slot(NULL
, fs_info
->tree_root
, &key
, path
, 0, 0);
3326 if (ret
> 0) { /* ret = -ENOENT; */
3331 bctl
= kzalloc(sizeof(*bctl
), GFP_NOFS
);
3337 leaf
= path
->nodes
[0];
3338 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
3340 bctl
->fs_info
= fs_info
;
3341 bctl
->flags
= btrfs_balance_flags(leaf
, item
);
3342 bctl
->flags
|= BTRFS_BALANCE_RESUME
;
3344 btrfs_balance_data(leaf
, item
, &disk_bargs
);
3345 btrfs_disk_balance_args_to_cpu(&bctl
->data
, &disk_bargs
);
3346 btrfs_balance_meta(leaf
, item
, &disk_bargs
);
3347 btrfs_disk_balance_args_to_cpu(&bctl
->meta
, &disk_bargs
);
3348 btrfs_balance_sys(leaf
, item
, &disk_bargs
);
3349 btrfs_disk_balance_args_to_cpu(&bctl
->sys
, &disk_bargs
);
3351 WARN_ON(atomic_xchg(&fs_info
->mutually_exclusive_operation_running
, 1));
3353 mutex_lock(&fs_info
->volume_mutex
);
3354 mutex_lock(&fs_info
->balance_mutex
);
3356 set_balance_control(bctl
);
3358 mutex_unlock(&fs_info
->balance_mutex
);
3359 mutex_unlock(&fs_info
->volume_mutex
);
3361 btrfs_free_path(path
);
3365 int btrfs_pause_balance(struct btrfs_fs_info
*fs_info
)
3369 mutex_lock(&fs_info
->balance_mutex
);
3370 if (!fs_info
->balance_ctl
) {
3371 mutex_unlock(&fs_info
->balance_mutex
);
3375 if (atomic_read(&fs_info
->balance_running
)) {
3376 atomic_inc(&fs_info
->balance_pause_req
);
3377 mutex_unlock(&fs_info
->balance_mutex
);
3379 wait_event(fs_info
->balance_wait_q
,
3380 atomic_read(&fs_info
->balance_running
) == 0);
3382 mutex_lock(&fs_info
->balance_mutex
);
3383 /* we are good with balance_ctl ripped off from under us */
3384 BUG_ON(atomic_read(&fs_info
->balance_running
));
3385 atomic_dec(&fs_info
->balance_pause_req
);
3390 mutex_unlock(&fs_info
->balance_mutex
);
3394 int btrfs_cancel_balance(struct btrfs_fs_info
*fs_info
)
3396 mutex_lock(&fs_info
->balance_mutex
);
3397 if (!fs_info
->balance_ctl
) {
3398 mutex_unlock(&fs_info
->balance_mutex
);
3402 atomic_inc(&fs_info
->balance_cancel_req
);
3404 * if we are running just wait and return, balance item is
3405 * deleted in btrfs_balance in this case
3407 if (atomic_read(&fs_info
->balance_running
)) {
3408 mutex_unlock(&fs_info
->balance_mutex
);
3409 wait_event(fs_info
->balance_wait_q
,
3410 atomic_read(&fs_info
->balance_running
) == 0);
3411 mutex_lock(&fs_info
->balance_mutex
);
3413 /* __cancel_balance needs volume_mutex */
3414 mutex_unlock(&fs_info
->balance_mutex
);
3415 mutex_lock(&fs_info
->volume_mutex
);
3416 mutex_lock(&fs_info
->balance_mutex
);
3418 if (fs_info
->balance_ctl
)
3419 __cancel_balance(fs_info
);
3421 mutex_unlock(&fs_info
->volume_mutex
);
3424 BUG_ON(fs_info
->balance_ctl
|| atomic_read(&fs_info
->balance_running
));
3425 atomic_dec(&fs_info
->balance_cancel_req
);
3426 mutex_unlock(&fs_info
->balance_mutex
);
3431 * shrinking a device means finding all of the device extents past
3432 * the new size, and then following the back refs to the chunks.
3433 * The chunk relocation code actually frees the device extent
3435 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
3437 struct btrfs_trans_handle
*trans
;
3438 struct btrfs_root
*root
= device
->dev_root
;
3439 struct btrfs_dev_extent
*dev_extent
= NULL
;
3440 struct btrfs_path
*path
;
3448 bool retried
= false;
3449 struct extent_buffer
*l
;
3450 struct btrfs_key key
;
3451 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
3452 u64 old_total
= btrfs_super_total_bytes(super_copy
);
3453 u64 old_size
= device
->total_bytes
;
3454 u64 diff
= device
->total_bytes
- new_size
;
3456 if (device
->is_tgtdev_for_dev_replace
)
3459 path
= btrfs_alloc_path();
3467 device
->total_bytes
= new_size
;
3468 if (device
->writeable
) {
3469 device
->fs_devices
->total_rw_bytes
-= diff
;
3470 spin_lock(&root
->fs_info
->free_chunk_lock
);
3471 root
->fs_info
->free_chunk_space
-= diff
;
3472 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3474 unlock_chunks(root
);
3477 key
.objectid
= device
->devid
;
3478 key
.offset
= (u64
)-1;
3479 key
.type
= BTRFS_DEV_EXTENT_KEY
;
3482 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3486 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
3491 btrfs_release_path(path
);
3496 slot
= path
->slots
[0];
3497 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
3499 if (key
.objectid
!= device
->devid
) {
3500 btrfs_release_path(path
);
3504 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
3505 length
= btrfs_dev_extent_length(l
, dev_extent
);
3507 if (key
.offset
+ length
<= new_size
) {
3508 btrfs_release_path(path
);
3512 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
3513 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
3514 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
3515 btrfs_release_path(path
);
3517 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
3519 if (ret
&& ret
!= -ENOSPC
)
3523 } while (key
.offset
-- > 0);
3525 if (failed
&& !retried
) {
3529 } else if (failed
&& retried
) {
3533 device
->total_bytes
= old_size
;
3534 if (device
->writeable
)
3535 device
->fs_devices
->total_rw_bytes
+= diff
;
3536 spin_lock(&root
->fs_info
->free_chunk_lock
);
3537 root
->fs_info
->free_chunk_space
+= diff
;
3538 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3539 unlock_chunks(root
);
3543 /* Shrinking succeeded, else we would be at "done". */
3544 trans
= btrfs_start_transaction(root
, 0);
3545 if (IS_ERR(trans
)) {
3546 ret
= PTR_ERR(trans
);
3552 device
->disk_total_bytes
= new_size
;
3553 /* Now btrfs_update_device() will change the on-disk size. */
3554 ret
= btrfs_update_device(trans
, device
);
3556 unlock_chunks(root
);
3557 btrfs_end_transaction(trans
, root
);
3560 WARN_ON(diff
> old_total
);
3561 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
3562 unlock_chunks(root
);
3563 btrfs_end_transaction(trans
, root
);
3565 btrfs_free_path(path
);
3569 static int btrfs_add_system_chunk(struct btrfs_root
*root
,
3570 struct btrfs_key
*key
,
3571 struct btrfs_chunk
*chunk
, int item_size
)
3573 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
3574 struct btrfs_disk_key disk_key
;
3578 array_size
= btrfs_super_sys_array_size(super_copy
);
3579 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
3582 ptr
= super_copy
->sys_chunk_array
+ array_size
;
3583 btrfs_cpu_key_to_disk(&disk_key
, key
);
3584 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
3585 ptr
+= sizeof(disk_key
);
3586 memcpy(ptr
, chunk
, item_size
);
3587 item_size
+= sizeof(disk_key
);
3588 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
3593 * sort the devices in descending order by max_avail, total_avail
3595 static int btrfs_cmp_device_info(const void *a
, const void *b
)
3597 const struct btrfs_device_info
*di_a
= a
;
3598 const struct btrfs_device_info
*di_b
= b
;
3600 if (di_a
->max_avail
> di_b
->max_avail
)
3602 if (di_a
->max_avail
< di_b
->max_avail
)
3604 if (di_a
->total_avail
> di_b
->total_avail
)
3606 if (di_a
->total_avail
< di_b
->total_avail
)
3611 static struct btrfs_raid_attr btrfs_raid_array
[BTRFS_NR_RAID_TYPES
] = {
3612 [BTRFS_RAID_RAID10
] = {
3615 .devs_max
= 0, /* 0 == as many as possible */
3617 .devs_increment
= 2,
3620 [BTRFS_RAID_RAID1
] = {
3625 .devs_increment
= 2,
3628 [BTRFS_RAID_DUP
] = {
3633 .devs_increment
= 1,
3636 [BTRFS_RAID_RAID0
] = {
3641 .devs_increment
= 1,
3644 [BTRFS_RAID_SINGLE
] = {
3649 .devs_increment
= 1,
3652 [BTRFS_RAID_RAID5
] = {
3657 .devs_increment
= 1,
3660 [BTRFS_RAID_RAID6
] = {
3665 .devs_increment
= 1,
3670 static u32
find_raid56_stripe_len(u32 data_devices
, u32 dev_stripe_target
)
3672 /* TODO allow them to set a preferred stripe size */
3676 static void check_raid56_incompat_flag(struct btrfs_fs_info
*info
, u64 type
)
3678 if (!(type
& (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
)))
3681 btrfs_set_fs_incompat(info
, RAID56
);
3684 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
3685 struct btrfs_root
*extent_root
,
3686 struct map_lookup
**map_ret
,
3687 u64
*num_bytes_out
, u64
*stripe_size_out
,
3688 u64 start
, u64 type
)
3690 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
3691 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
3692 struct list_head
*cur
;
3693 struct map_lookup
*map
= NULL
;
3694 struct extent_map_tree
*em_tree
;
3695 struct extent_map
*em
;
3696 struct btrfs_device_info
*devices_info
= NULL
;
3698 int num_stripes
; /* total number of stripes to allocate */
3699 int data_stripes
; /* number of stripes that count for
3701 int sub_stripes
; /* sub_stripes info for map */
3702 int dev_stripes
; /* stripes per dev */
3703 int devs_max
; /* max devs to use */
3704 int devs_min
; /* min devs needed */
3705 int devs_increment
; /* ndevs has to be a multiple of this */
3706 int ncopies
; /* how many copies to data has */
3708 u64 max_stripe_size
;
3712 u64 raid_stripe_len
= BTRFS_STRIPE_LEN
;
3718 BUG_ON(!alloc_profile_is_valid(type
, 0));
3720 if (list_empty(&fs_devices
->alloc_list
))
3723 index
= __get_raid_index(type
);
3725 sub_stripes
= btrfs_raid_array
[index
].sub_stripes
;
3726 dev_stripes
= btrfs_raid_array
[index
].dev_stripes
;
3727 devs_max
= btrfs_raid_array
[index
].devs_max
;
3728 devs_min
= btrfs_raid_array
[index
].devs_min
;
3729 devs_increment
= btrfs_raid_array
[index
].devs_increment
;
3730 ncopies
= btrfs_raid_array
[index
].ncopies
;
3732 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
3733 max_stripe_size
= 1024 * 1024 * 1024;
3734 max_chunk_size
= 10 * max_stripe_size
;
3735 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
3736 /* for larger filesystems, use larger metadata chunks */
3737 if (fs_devices
->total_rw_bytes
> 50ULL * 1024 * 1024 * 1024)
3738 max_stripe_size
= 1024 * 1024 * 1024;
3740 max_stripe_size
= 256 * 1024 * 1024;
3741 max_chunk_size
= max_stripe_size
;
3742 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
3743 max_stripe_size
= 32 * 1024 * 1024;
3744 max_chunk_size
= 2 * max_stripe_size
;
3746 printk(KERN_ERR
"btrfs: invalid chunk type 0x%llx requested\n",
3751 /* we don't want a chunk larger than 10% of writeable space */
3752 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
3755 devices_info
= kzalloc(sizeof(*devices_info
) * fs_devices
->rw_devices
,
3760 cur
= fs_devices
->alloc_list
.next
;
3763 * in the first pass through the devices list, we gather information
3764 * about the available holes on each device.
3767 while (cur
!= &fs_devices
->alloc_list
) {
3768 struct btrfs_device
*device
;
3772 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
3776 if (!device
->writeable
) {
3778 "btrfs: read-only device in alloc_list\n");
3782 if (!device
->in_fs_metadata
||
3783 device
->is_tgtdev_for_dev_replace
)
3786 if (device
->total_bytes
> device
->bytes_used
)
3787 total_avail
= device
->total_bytes
- device
->bytes_used
;
3791 /* If there is no space on this device, skip it. */
3792 if (total_avail
== 0)
3795 ret
= find_free_dev_extent(device
,
3796 max_stripe_size
* dev_stripes
,
3797 &dev_offset
, &max_avail
);
3798 if (ret
&& ret
!= -ENOSPC
)
3802 max_avail
= max_stripe_size
* dev_stripes
;
3804 if (max_avail
< BTRFS_STRIPE_LEN
* dev_stripes
)
3807 if (ndevs
== fs_devices
->rw_devices
) {
3808 WARN(1, "%s: found more than %llu devices\n",
3809 __func__
, fs_devices
->rw_devices
);
3812 devices_info
[ndevs
].dev_offset
= dev_offset
;
3813 devices_info
[ndevs
].max_avail
= max_avail
;
3814 devices_info
[ndevs
].total_avail
= total_avail
;
3815 devices_info
[ndevs
].dev
= device
;
3820 * now sort the devices by hole size / available space
3822 sort(devices_info
, ndevs
, sizeof(struct btrfs_device_info
),
3823 btrfs_cmp_device_info
, NULL
);
3825 /* round down to number of usable stripes */
3826 ndevs
-= ndevs
% devs_increment
;
3828 if (ndevs
< devs_increment
* sub_stripes
|| ndevs
< devs_min
) {
3833 if (devs_max
&& ndevs
> devs_max
)
3836 * the primary goal is to maximize the number of stripes, so use as many
3837 * devices as possible, even if the stripes are not maximum sized.
3839 stripe_size
= devices_info
[ndevs
-1].max_avail
;
3840 num_stripes
= ndevs
* dev_stripes
;
3843 * this will have to be fixed for RAID1 and RAID10 over
3846 data_stripes
= num_stripes
/ ncopies
;
3848 if (type
& BTRFS_BLOCK_GROUP_RAID5
) {
3849 raid_stripe_len
= find_raid56_stripe_len(ndevs
- 1,
3850 btrfs_super_stripesize(info
->super_copy
));
3851 data_stripes
= num_stripes
- 1;
3853 if (type
& BTRFS_BLOCK_GROUP_RAID6
) {
3854 raid_stripe_len
= find_raid56_stripe_len(ndevs
- 2,
3855 btrfs_super_stripesize(info
->super_copy
));
3856 data_stripes
= num_stripes
- 2;
3860 * Use the number of data stripes to figure out how big this chunk
3861 * is really going to be in terms of logical address space,
3862 * and compare that answer with the max chunk size
3864 if (stripe_size
* data_stripes
> max_chunk_size
) {
3865 u64 mask
= (1ULL << 24) - 1;
3866 stripe_size
= max_chunk_size
;
3867 do_div(stripe_size
, data_stripes
);
3869 /* bump the answer up to a 16MB boundary */
3870 stripe_size
= (stripe_size
+ mask
) & ~mask
;
3872 /* but don't go higher than the limits we found
3873 * while searching for free extents
3875 if (stripe_size
> devices_info
[ndevs
-1].max_avail
)
3876 stripe_size
= devices_info
[ndevs
-1].max_avail
;
3879 do_div(stripe_size
, dev_stripes
);
3881 /* align to BTRFS_STRIPE_LEN */
3882 do_div(stripe_size
, raid_stripe_len
);
3883 stripe_size
*= raid_stripe_len
;
3885 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
3890 map
->num_stripes
= num_stripes
;
3892 for (i
= 0; i
< ndevs
; ++i
) {
3893 for (j
= 0; j
< dev_stripes
; ++j
) {
3894 int s
= i
* dev_stripes
+ j
;
3895 map
->stripes
[s
].dev
= devices_info
[i
].dev
;
3896 map
->stripes
[s
].physical
= devices_info
[i
].dev_offset
+
3900 map
->sector_size
= extent_root
->sectorsize
;
3901 map
->stripe_len
= raid_stripe_len
;
3902 map
->io_align
= raid_stripe_len
;
3903 map
->io_width
= raid_stripe_len
;
3905 map
->sub_stripes
= sub_stripes
;
3908 num_bytes
= stripe_size
* data_stripes
;
3910 *stripe_size_out
= stripe_size
;
3911 *num_bytes_out
= num_bytes
;
3913 trace_btrfs_chunk_alloc(info
->chunk_root
, map
, start
, num_bytes
);
3915 em
= alloc_extent_map();
3920 em
->bdev
= (struct block_device
*)map
;
3922 em
->len
= num_bytes
;
3923 em
->block_start
= 0;
3924 em
->block_len
= em
->len
;
3926 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
3927 write_lock(&em_tree
->lock
);
3928 ret
= add_extent_mapping(em_tree
, em
, 0);
3929 write_unlock(&em_tree
->lock
);
3931 free_extent_map(em
);
3935 for (i
= 0; i
< map
->num_stripes
; ++i
) {
3936 struct btrfs_device
*device
;
3939 device
= map
->stripes
[i
].dev
;
3940 dev_offset
= map
->stripes
[i
].physical
;
3942 ret
= btrfs_alloc_dev_extent(trans
, device
,
3943 info
->chunk_root
->root_key
.objectid
,
3944 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3945 start
, dev_offset
, stripe_size
);
3947 goto error_dev_extent
;
3950 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
3951 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3954 i
= map
->num_stripes
- 1;
3955 goto error_dev_extent
;
3958 free_extent_map(em
);
3959 check_raid56_incompat_flag(extent_root
->fs_info
, type
);
3961 kfree(devices_info
);
3965 for (; i
>= 0; i
--) {
3966 struct btrfs_device
*device
;
3969 device
= map
->stripes
[i
].dev
;
3970 err
= btrfs_free_dev_extent(trans
, device
, start
);
3972 btrfs_abort_transaction(trans
, extent_root
, err
);
3976 write_lock(&em_tree
->lock
);
3977 remove_extent_mapping(em_tree
, em
);
3978 write_unlock(&em_tree
->lock
);
3980 /* One for our allocation */
3981 free_extent_map(em
);
3982 /* One for the tree reference */
3983 free_extent_map(em
);
3986 kfree(devices_info
);
3990 static int __finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
3991 struct btrfs_root
*extent_root
,
3992 struct map_lookup
*map
, u64 chunk_offset
,
3993 u64 chunk_size
, u64 stripe_size
)
3996 struct btrfs_key key
;
3997 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
3998 struct btrfs_device
*device
;
3999 struct btrfs_chunk
*chunk
;
4000 struct btrfs_stripe
*stripe
;
4001 size_t item_size
= btrfs_chunk_item_size(map
->num_stripes
);
4005 chunk
= kzalloc(item_size
, GFP_NOFS
);
4010 while (index
< map
->num_stripes
) {
4011 device
= map
->stripes
[index
].dev
;
4012 device
->bytes_used
+= stripe_size
;
4013 ret
= btrfs_update_device(trans
, device
);
4019 spin_lock(&extent_root
->fs_info
->free_chunk_lock
);
4020 extent_root
->fs_info
->free_chunk_space
-= (stripe_size
*
4022 spin_unlock(&extent_root
->fs_info
->free_chunk_lock
);
4025 stripe
= &chunk
->stripe
;
4026 while (index
< map
->num_stripes
) {
4027 device
= map
->stripes
[index
].dev
;
4028 dev_offset
= map
->stripes
[index
].physical
;
4030 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
4031 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
4032 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
4037 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
4038 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
4039 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
4040 btrfs_set_stack_chunk_type(chunk
, map
->type
);
4041 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
4042 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
4043 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
4044 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
4045 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
4047 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
4048 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
4049 key
.offset
= chunk_offset
;
4051 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
4053 if (ret
== 0 && map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
4055 * TODO: Cleanup of inserted chunk root in case of
4058 ret
= btrfs_add_system_chunk(chunk_root
, &key
, chunk
,
4068 * Chunk allocation falls into two parts. The first part does works
4069 * that make the new allocated chunk useable, but not do any operation
4070 * that modifies the chunk tree. The second part does the works that
4071 * require modifying the chunk tree. This division is important for the
4072 * bootstrap process of adding storage to a seed btrfs.
4074 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
4075 struct btrfs_root
*extent_root
, u64 type
)
4080 struct map_lookup
*map
;
4081 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
4084 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
4089 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
4090 &stripe_size
, chunk_offset
, type
);
4094 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
4095 chunk_size
, stripe_size
);
4101 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
4102 struct btrfs_root
*root
,
4103 struct btrfs_device
*device
)
4106 u64 sys_chunk_offset
;
4110 u64 sys_stripe_size
;
4112 struct map_lookup
*map
;
4113 struct map_lookup
*sys_map
;
4114 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4115 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
4118 ret
= find_next_chunk(fs_info
->chunk_root
,
4119 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, &chunk_offset
);
4123 alloc_profile
= btrfs_get_alloc_profile(extent_root
, 0);
4124 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
4125 &stripe_size
, chunk_offset
, alloc_profile
);
4129 sys_chunk_offset
= chunk_offset
+ chunk_size
;
4131 alloc_profile
= btrfs_get_alloc_profile(fs_info
->chunk_root
, 0);
4132 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &sys_map
,
4133 &sys_chunk_size
, &sys_stripe_size
,
4134 sys_chunk_offset
, alloc_profile
);
4136 btrfs_abort_transaction(trans
, root
, ret
);
4140 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
4142 btrfs_abort_transaction(trans
, root
, ret
);
4147 * Modifying chunk tree needs allocating new blocks from both
4148 * system block group and metadata block group. So we only can
4149 * do operations require modifying the chunk tree after both
4150 * block groups were created.
4152 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
4153 chunk_size
, stripe_size
);
4155 btrfs_abort_transaction(trans
, root
, ret
);
4159 ret
= __finish_chunk_alloc(trans
, extent_root
, sys_map
,
4160 sys_chunk_offset
, sys_chunk_size
,
4163 btrfs_abort_transaction(trans
, root
, ret
);
4170 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
4172 struct extent_map
*em
;
4173 struct map_lookup
*map
;
4174 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
4178 read_lock(&map_tree
->map_tree
.lock
);
4179 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
4180 read_unlock(&map_tree
->map_tree
.lock
);
4184 if (btrfs_test_opt(root
, DEGRADED
)) {
4185 free_extent_map(em
);
4189 map
= (struct map_lookup
*)em
->bdev
;
4190 for (i
= 0; i
< map
->num_stripes
; i
++) {
4191 if (!map
->stripes
[i
].dev
->writeable
) {
4196 free_extent_map(em
);
4200 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
4202 extent_map_tree_init(&tree
->map_tree
);
4205 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
4207 struct extent_map
*em
;
4210 write_lock(&tree
->map_tree
.lock
);
4211 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
4213 remove_extent_mapping(&tree
->map_tree
, em
);
4214 write_unlock(&tree
->map_tree
.lock
);
4219 free_extent_map(em
);
4220 /* once for the tree */
4221 free_extent_map(em
);
4225 int btrfs_num_copies(struct btrfs_fs_info
*fs_info
, u64 logical
, u64 len
)
4227 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
4228 struct extent_map
*em
;
4229 struct map_lookup
*map
;
4230 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4233 read_lock(&em_tree
->lock
);
4234 em
= lookup_extent_mapping(em_tree
, logical
, len
);
4235 read_unlock(&em_tree
->lock
);
4238 * We could return errors for these cases, but that could get ugly and
4239 * we'd probably do the same thing which is just not do anything else
4240 * and exit, so return 1 so the callers don't try to use other copies.
4243 btrfs_emerg(fs_info
, "No mapping for %Lu-%Lu\n", logical
,
4248 if (em
->start
> logical
|| em
->start
+ em
->len
< logical
) {
4249 btrfs_emerg(fs_info
, "Invalid mapping for %Lu-%Lu, got "
4250 "%Lu-%Lu\n", logical
, logical
+len
, em
->start
,
4251 em
->start
+ em
->len
);
4255 map
= (struct map_lookup
*)em
->bdev
;
4256 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
4257 ret
= map
->num_stripes
;
4258 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
4259 ret
= map
->sub_stripes
;
4260 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID5
)
4262 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
4266 free_extent_map(em
);
4268 btrfs_dev_replace_lock(&fs_info
->dev_replace
);
4269 if (btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
))
4271 btrfs_dev_replace_unlock(&fs_info
->dev_replace
);
4276 unsigned long btrfs_full_stripe_len(struct btrfs_root
*root
,
4277 struct btrfs_mapping_tree
*map_tree
,
4280 struct extent_map
*em
;
4281 struct map_lookup
*map
;
4282 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4283 unsigned long len
= root
->sectorsize
;
4285 read_lock(&em_tree
->lock
);
4286 em
= lookup_extent_mapping(em_tree
, logical
, len
);
4287 read_unlock(&em_tree
->lock
);
4290 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
4291 map
= (struct map_lookup
*)em
->bdev
;
4292 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
4293 BTRFS_BLOCK_GROUP_RAID6
)) {
4294 len
= map
->stripe_len
* nr_data_stripes(map
);
4296 free_extent_map(em
);
4300 int btrfs_is_parity_mirror(struct btrfs_mapping_tree
*map_tree
,
4301 u64 logical
, u64 len
, int mirror_num
)
4303 struct extent_map
*em
;
4304 struct map_lookup
*map
;
4305 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4308 read_lock(&em_tree
->lock
);
4309 em
= lookup_extent_mapping(em_tree
, logical
, len
);
4310 read_unlock(&em_tree
->lock
);
4313 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
4314 map
= (struct map_lookup
*)em
->bdev
;
4315 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
4316 BTRFS_BLOCK_GROUP_RAID6
))
4318 free_extent_map(em
);
4322 static int find_live_mirror(struct btrfs_fs_info
*fs_info
,
4323 struct map_lookup
*map
, int first
, int num
,
4324 int optimal
, int dev_replace_is_ongoing
)
4328 struct btrfs_device
*srcdev
;
4330 if (dev_replace_is_ongoing
&&
4331 fs_info
->dev_replace
.cont_reading_from_srcdev_mode
==
4332 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID
)
4333 srcdev
= fs_info
->dev_replace
.srcdev
;
4338 * try to avoid the drive that is the source drive for a
4339 * dev-replace procedure, only choose it if no other non-missing
4340 * mirror is available
4342 for (tolerance
= 0; tolerance
< 2; tolerance
++) {
4343 if (map
->stripes
[optimal
].dev
->bdev
&&
4344 (tolerance
|| map
->stripes
[optimal
].dev
!= srcdev
))
4346 for (i
= first
; i
< first
+ num
; i
++) {
4347 if (map
->stripes
[i
].dev
->bdev
&&
4348 (tolerance
|| map
->stripes
[i
].dev
!= srcdev
))
4353 /* we couldn't find one that doesn't fail. Just return something
4354 * and the io error handling code will clean up eventually
4359 static inline int parity_smaller(u64 a
, u64 b
)
4364 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4365 static void sort_parity_stripes(struct btrfs_bio
*bbio
, u64
*raid_map
)
4367 struct btrfs_bio_stripe s
;
4374 for (i
= 0; i
< bbio
->num_stripes
- 1; i
++) {
4375 if (parity_smaller(raid_map
[i
], raid_map
[i
+1])) {
4376 s
= bbio
->stripes
[i
];
4378 bbio
->stripes
[i
] = bbio
->stripes
[i
+1];
4379 raid_map
[i
] = raid_map
[i
+1];
4380 bbio
->stripes
[i
+1] = s
;
4388 static int __btrfs_map_block(struct btrfs_fs_info
*fs_info
, int rw
,
4389 u64 logical
, u64
*length
,
4390 struct btrfs_bio
**bbio_ret
,
4391 int mirror_num
, u64
**raid_map_ret
)
4393 struct extent_map
*em
;
4394 struct map_lookup
*map
;
4395 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
4396 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4399 u64 stripe_end_offset
;
4404 u64
*raid_map
= NULL
;
4410 struct btrfs_bio
*bbio
= NULL
;
4411 struct btrfs_dev_replace
*dev_replace
= &fs_info
->dev_replace
;
4412 int dev_replace_is_ongoing
= 0;
4413 int num_alloc_stripes
;
4414 int patch_the_first_stripe_for_dev_replace
= 0;
4415 u64 physical_to_patch_in_first_stripe
= 0;
4416 u64 raid56_full_stripe_start
= (u64
)-1;
4418 read_lock(&em_tree
->lock
);
4419 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
4420 read_unlock(&em_tree
->lock
);
4423 btrfs_crit(fs_info
, "unable to find logical %llu len %llu",
4424 (unsigned long long)logical
,
4425 (unsigned long long)*length
);
4429 if (em
->start
> logical
|| em
->start
+ em
->len
< logical
) {
4430 btrfs_crit(fs_info
, "found a bad mapping, wanted %Lu, "
4431 "found %Lu-%Lu\n", logical
, em
->start
,
4432 em
->start
+ em
->len
);
4436 map
= (struct map_lookup
*)em
->bdev
;
4437 offset
= logical
- em
->start
;
4439 if (mirror_num
> map
->num_stripes
)
4442 stripe_len
= map
->stripe_len
;
4445 * stripe_nr counts the total number of stripes we have to stride
4446 * to get to this block
4448 do_div(stripe_nr
, stripe_len
);
4450 stripe_offset
= stripe_nr
* stripe_len
;
4451 BUG_ON(offset
< stripe_offset
);
4453 /* stripe_offset is the offset of this block in its stripe*/
4454 stripe_offset
= offset
- stripe_offset
;
4456 /* if we're here for raid56, we need to know the stripe aligned start */
4457 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
)) {
4458 unsigned long full_stripe_len
= stripe_len
* nr_data_stripes(map
);
4459 raid56_full_stripe_start
= offset
;
4461 /* allow a write of a full stripe, but make sure we don't
4462 * allow straddling of stripes
4464 do_div(raid56_full_stripe_start
, full_stripe_len
);
4465 raid56_full_stripe_start
*= full_stripe_len
;
4468 if (rw
& REQ_DISCARD
) {
4469 /* we don't discard raid56 yet */
4471 (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
)) {
4475 *length
= min_t(u64
, em
->len
- offset
, *length
);
4476 } else if (map
->type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) {
4478 /* For writes to RAID[56], allow a full stripeset across all disks.
4479 For other RAID types and for RAID[56] reads, just allow a single
4480 stripe (on a single disk). */
4481 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
) &&
4483 max_len
= stripe_len
* nr_data_stripes(map
) -
4484 (offset
- raid56_full_stripe_start
);
4486 /* we limit the length of each bio to what fits in a stripe */
4487 max_len
= stripe_len
- stripe_offset
;
4489 *length
= min_t(u64
, em
->len
- offset
, max_len
);
4491 *length
= em
->len
- offset
;
4494 /* This is for when we're called from btrfs_merge_bio_hook() and all
4495 it cares about is the length */
4499 btrfs_dev_replace_lock(dev_replace
);
4500 dev_replace_is_ongoing
= btrfs_dev_replace_is_ongoing(dev_replace
);
4501 if (!dev_replace_is_ongoing
)
4502 btrfs_dev_replace_unlock(dev_replace
);
4504 if (dev_replace_is_ongoing
&& mirror_num
== map
->num_stripes
+ 1 &&
4505 !(rw
& (REQ_WRITE
| REQ_DISCARD
| REQ_GET_READ_MIRRORS
)) &&
4506 dev_replace
->tgtdev
!= NULL
) {
4508 * in dev-replace case, for repair case (that's the only
4509 * case where the mirror is selected explicitly when
4510 * calling btrfs_map_block), blocks left of the left cursor
4511 * can also be read from the target drive.
4512 * For REQ_GET_READ_MIRRORS, the target drive is added as
4513 * the last one to the array of stripes. For READ, it also
4514 * needs to be supported using the same mirror number.
4515 * If the requested block is not left of the left cursor,
4516 * EIO is returned. This can happen because btrfs_num_copies()
4517 * returns one more in the dev-replace case.
4519 u64 tmp_length
= *length
;
4520 struct btrfs_bio
*tmp_bbio
= NULL
;
4521 int tmp_num_stripes
;
4522 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
4523 int index_srcdev
= 0;
4525 u64 physical_of_found
= 0;
4527 ret
= __btrfs_map_block(fs_info
, REQ_GET_READ_MIRRORS
,
4528 logical
, &tmp_length
, &tmp_bbio
, 0, NULL
);
4530 WARN_ON(tmp_bbio
!= NULL
);
4534 tmp_num_stripes
= tmp_bbio
->num_stripes
;
4535 if (mirror_num
> tmp_num_stripes
) {
4537 * REQ_GET_READ_MIRRORS does not contain this
4538 * mirror, that means that the requested area
4539 * is not left of the left cursor
4547 * process the rest of the function using the mirror_num
4548 * of the source drive. Therefore look it up first.
4549 * At the end, patch the device pointer to the one of the
4552 for (i
= 0; i
< tmp_num_stripes
; i
++) {
4553 if (tmp_bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
4555 * In case of DUP, in order to keep it
4556 * simple, only add the mirror with the
4557 * lowest physical address
4560 physical_of_found
<=
4561 tmp_bbio
->stripes
[i
].physical
)
4566 tmp_bbio
->stripes
[i
].physical
;
4571 mirror_num
= index_srcdev
+ 1;
4572 patch_the_first_stripe_for_dev_replace
= 1;
4573 physical_to_patch_in_first_stripe
= physical_of_found
;
4582 } else if (mirror_num
> map
->num_stripes
) {
4588 stripe_nr_orig
= stripe_nr
;
4589 stripe_nr_end
= ALIGN(offset
+ *length
, map
->stripe_len
);
4590 do_div(stripe_nr_end
, map
->stripe_len
);
4591 stripe_end_offset
= stripe_nr_end
* map
->stripe_len
-
4594 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
4595 if (rw
& REQ_DISCARD
)
4596 num_stripes
= min_t(u64
, map
->num_stripes
,
4597 stripe_nr_end
- stripe_nr_orig
);
4598 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
4599 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
4600 if (rw
& (REQ_WRITE
| REQ_DISCARD
| REQ_GET_READ_MIRRORS
))
4601 num_stripes
= map
->num_stripes
;
4602 else if (mirror_num
)
4603 stripe_index
= mirror_num
- 1;
4605 stripe_index
= find_live_mirror(fs_info
, map
, 0,
4607 current
->pid
% map
->num_stripes
,
4608 dev_replace_is_ongoing
);
4609 mirror_num
= stripe_index
+ 1;
4612 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
4613 if (rw
& (REQ_WRITE
| REQ_DISCARD
| REQ_GET_READ_MIRRORS
)) {
4614 num_stripes
= map
->num_stripes
;
4615 } else if (mirror_num
) {
4616 stripe_index
= mirror_num
- 1;
4621 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
4622 int factor
= map
->num_stripes
/ map
->sub_stripes
;
4624 stripe_index
= do_div(stripe_nr
, factor
);
4625 stripe_index
*= map
->sub_stripes
;
4627 if (rw
& (REQ_WRITE
| REQ_GET_READ_MIRRORS
))
4628 num_stripes
= map
->sub_stripes
;
4629 else if (rw
& REQ_DISCARD
)
4630 num_stripes
= min_t(u64
, map
->sub_stripes
*
4631 (stripe_nr_end
- stripe_nr_orig
),
4633 else if (mirror_num
)
4634 stripe_index
+= mirror_num
- 1;
4636 int old_stripe_index
= stripe_index
;
4637 stripe_index
= find_live_mirror(fs_info
, map
,
4639 map
->sub_stripes
, stripe_index
+
4640 current
->pid
% map
->sub_stripes
,
4641 dev_replace_is_ongoing
);
4642 mirror_num
= stripe_index
- old_stripe_index
+ 1;
4645 } else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
4646 BTRFS_BLOCK_GROUP_RAID6
)) {
4649 if (bbio_ret
&& ((rw
& REQ_WRITE
) || mirror_num
> 1)
4653 /* push stripe_nr back to the start of the full stripe */
4654 stripe_nr
= raid56_full_stripe_start
;
4655 do_div(stripe_nr
, stripe_len
);
4657 stripe_index
= do_div(stripe_nr
, nr_data_stripes(map
));
4659 /* RAID[56] write or recovery. Return all stripes */
4660 num_stripes
= map
->num_stripes
;
4661 max_errors
= nr_parity_stripes(map
);
4663 raid_map
= kmalloc(sizeof(u64
) * num_stripes
,
4670 /* Work out the disk rotation on this stripe-set */
4672 rot
= do_div(tmp
, num_stripes
);
4674 /* Fill in the logical address of each stripe */
4675 tmp
= stripe_nr
* nr_data_stripes(map
);
4676 for (i
= 0; i
< nr_data_stripes(map
); i
++)
4677 raid_map
[(i
+rot
) % num_stripes
] =
4678 em
->start
+ (tmp
+ i
) * map
->stripe_len
;
4680 raid_map
[(i
+rot
) % map
->num_stripes
] = RAID5_P_STRIPE
;
4681 if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
4682 raid_map
[(i
+rot
+1) % num_stripes
] =
4685 *length
= map
->stripe_len
;
4690 * Mirror #0 or #1 means the original data block.
4691 * Mirror #2 is RAID5 parity block.
4692 * Mirror #3 is RAID6 Q block.
4694 stripe_index
= do_div(stripe_nr
, nr_data_stripes(map
));
4696 stripe_index
= nr_data_stripes(map
) +
4699 /* We distribute the parity blocks across stripes */
4700 tmp
= stripe_nr
+ stripe_index
;
4701 stripe_index
= do_div(tmp
, map
->num_stripes
);
4705 * after this do_div call, stripe_nr is the number of stripes
4706 * on this device we have to walk to find the data, and
4707 * stripe_index is the number of our device in the stripe array
4709 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
4710 mirror_num
= stripe_index
+ 1;
4712 BUG_ON(stripe_index
>= map
->num_stripes
);
4714 num_alloc_stripes
= num_stripes
;
4715 if (dev_replace_is_ongoing
) {
4716 if (rw
& (REQ_WRITE
| REQ_DISCARD
))
4717 num_alloc_stripes
<<= 1;
4718 if (rw
& REQ_GET_READ_MIRRORS
)
4719 num_alloc_stripes
++;
4721 bbio
= kzalloc(btrfs_bio_size(num_alloc_stripes
), GFP_NOFS
);
4726 atomic_set(&bbio
->error
, 0);
4728 if (rw
& REQ_DISCARD
) {
4730 int sub_stripes
= 0;
4731 u64 stripes_per_dev
= 0;
4732 u32 remaining_stripes
= 0;
4733 u32 last_stripe
= 0;
4736 (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID10
)) {
4737 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
4740 sub_stripes
= map
->sub_stripes
;
4742 factor
= map
->num_stripes
/ sub_stripes
;
4743 stripes_per_dev
= div_u64_rem(stripe_nr_end
-
4746 &remaining_stripes
);
4747 div_u64_rem(stripe_nr_end
- 1, factor
, &last_stripe
);
4748 last_stripe
*= sub_stripes
;
4751 for (i
= 0; i
< num_stripes
; i
++) {
4752 bbio
->stripes
[i
].physical
=
4753 map
->stripes
[stripe_index
].physical
+
4754 stripe_offset
+ stripe_nr
* map
->stripe_len
;
4755 bbio
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
4757 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
4758 BTRFS_BLOCK_GROUP_RAID10
)) {
4759 bbio
->stripes
[i
].length
= stripes_per_dev
*
4762 if (i
/ sub_stripes
< remaining_stripes
)
4763 bbio
->stripes
[i
].length
+=
4767 * Special for the first stripe and
4770 * |-------|...|-------|
4774 if (i
< sub_stripes
)
4775 bbio
->stripes
[i
].length
-=
4778 if (stripe_index
>= last_stripe
&&
4779 stripe_index
<= (last_stripe
+
4781 bbio
->stripes
[i
].length
-=
4784 if (i
== sub_stripes
- 1)
4787 bbio
->stripes
[i
].length
= *length
;
4790 if (stripe_index
== map
->num_stripes
) {
4791 /* This could only happen for RAID0/10 */
4797 for (i
= 0; i
< num_stripes
; i
++) {
4798 bbio
->stripes
[i
].physical
=
4799 map
->stripes
[stripe_index
].physical
+
4801 stripe_nr
* map
->stripe_len
;
4802 bbio
->stripes
[i
].dev
=
4803 map
->stripes
[stripe_index
].dev
;
4808 if (rw
& (REQ_WRITE
| REQ_GET_READ_MIRRORS
)) {
4809 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
4810 BTRFS_BLOCK_GROUP_RAID10
|
4811 BTRFS_BLOCK_GROUP_RAID5
|
4812 BTRFS_BLOCK_GROUP_DUP
)) {
4814 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
) {
4819 if (dev_replace_is_ongoing
&& (rw
& (REQ_WRITE
| REQ_DISCARD
)) &&
4820 dev_replace
->tgtdev
!= NULL
) {
4821 int index_where_to_add
;
4822 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
4825 * duplicate the write operations while the dev replace
4826 * procedure is running. Since the copying of the old disk
4827 * to the new disk takes place at run time while the
4828 * filesystem is mounted writable, the regular write
4829 * operations to the old disk have to be duplicated to go
4830 * to the new disk as well.
4831 * Note that device->missing is handled by the caller, and
4832 * that the write to the old disk is already set up in the
4835 index_where_to_add
= num_stripes
;
4836 for (i
= 0; i
< num_stripes
; i
++) {
4837 if (bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
4838 /* write to new disk, too */
4839 struct btrfs_bio_stripe
*new =
4840 bbio
->stripes
+ index_where_to_add
;
4841 struct btrfs_bio_stripe
*old
=
4844 new->physical
= old
->physical
;
4845 new->length
= old
->length
;
4846 new->dev
= dev_replace
->tgtdev
;
4847 index_where_to_add
++;
4851 num_stripes
= index_where_to_add
;
4852 } else if (dev_replace_is_ongoing
&& (rw
& REQ_GET_READ_MIRRORS
) &&
4853 dev_replace
->tgtdev
!= NULL
) {
4854 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
4855 int index_srcdev
= 0;
4857 u64 physical_of_found
= 0;
4860 * During the dev-replace procedure, the target drive can
4861 * also be used to read data in case it is needed to repair
4862 * a corrupt block elsewhere. This is possible if the
4863 * requested area is left of the left cursor. In this area,
4864 * the target drive is a full copy of the source drive.
4866 for (i
= 0; i
< num_stripes
; i
++) {
4867 if (bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
4869 * In case of DUP, in order to keep it
4870 * simple, only add the mirror with the
4871 * lowest physical address
4874 physical_of_found
<=
4875 bbio
->stripes
[i
].physical
)
4879 physical_of_found
= bbio
->stripes
[i
].physical
;
4883 u64 length
= map
->stripe_len
;
4885 if (physical_of_found
+ length
<=
4886 dev_replace
->cursor_left
) {
4887 struct btrfs_bio_stripe
*tgtdev_stripe
=
4888 bbio
->stripes
+ num_stripes
;
4890 tgtdev_stripe
->physical
= physical_of_found
;
4891 tgtdev_stripe
->length
=
4892 bbio
->stripes
[index_srcdev
].length
;
4893 tgtdev_stripe
->dev
= dev_replace
->tgtdev
;
4901 bbio
->num_stripes
= num_stripes
;
4902 bbio
->max_errors
= max_errors
;
4903 bbio
->mirror_num
= mirror_num
;
4906 * this is the case that REQ_READ && dev_replace_is_ongoing &&
4907 * mirror_num == num_stripes + 1 && dev_replace target drive is
4908 * available as a mirror
4910 if (patch_the_first_stripe_for_dev_replace
&& num_stripes
> 0) {
4911 WARN_ON(num_stripes
> 1);
4912 bbio
->stripes
[0].dev
= dev_replace
->tgtdev
;
4913 bbio
->stripes
[0].physical
= physical_to_patch_in_first_stripe
;
4914 bbio
->mirror_num
= map
->num_stripes
+ 1;
4917 sort_parity_stripes(bbio
, raid_map
);
4918 *raid_map_ret
= raid_map
;
4921 if (dev_replace_is_ongoing
)
4922 btrfs_dev_replace_unlock(dev_replace
);
4923 free_extent_map(em
);
4927 int btrfs_map_block(struct btrfs_fs_info
*fs_info
, int rw
,
4928 u64 logical
, u64
*length
,
4929 struct btrfs_bio
**bbio_ret
, int mirror_num
)
4931 return __btrfs_map_block(fs_info
, rw
, logical
, length
, bbio_ret
,
4935 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
4936 u64 chunk_start
, u64 physical
, u64 devid
,
4937 u64
**logical
, int *naddrs
, int *stripe_len
)
4939 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4940 struct extent_map
*em
;
4941 struct map_lookup
*map
;
4949 read_lock(&em_tree
->lock
);
4950 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
4951 read_unlock(&em_tree
->lock
);
4954 printk(KERN_ERR
"btrfs: couldn't find em for chunk %Lu\n",
4959 if (em
->start
!= chunk_start
) {
4960 printk(KERN_ERR
"btrfs: bad chunk start, em=%Lu, wanted=%Lu\n",
4961 em
->start
, chunk_start
);
4962 free_extent_map(em
);
4965 map
= (struct map_lookup
*)em
->bdev
;
4968 rmap_len
= map
->stripe_len
;
4970 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
4971 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
4972 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
4973 do_div(length
, map
->num_stripes
);
4974 else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
4975 BTRFS_BLOCK_GROUP_RAID6
)) {
4976 do_div(length
, nr_data_stripes(map
));
4977 rmap_len
= map
->stripe_len
* nr_data_stripes(map
);
4980 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
4981 BUG_ON(!buf
); /* -ENOMEM */
4983 for (i
= 0; i
< map
->num_stripes
; i
++) {
4984 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
4986 if (map
->stripes
[i
].physical
> physical
||
4987 map
->stripes
[i
].physical
+ length
<= physical
)
4990 stripe_nr
= physical
- map
->stripes
[i
].physical
;
4991 do_div(stripe_nr
, map
->stripe_len
);
4993 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
4994 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
4995 do_div(stripe_nr
, map
->sub_stripes
);
4996 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
4997 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
4998 } /* else if RAID[56], multiply by nr_data_stripes().
4999 * Alternatively, just use rmap_len below instead of
5000 * map->stripe_len */
5002 bytenr
= chunk_start
+ stripe_nr
* rmap_len
;
5003 WARN_ON(nr
>= map
->num_stripes
);
5004 for (j
= 0; j
< nr
; j
++) {
5005 if (buf
[j
] == bytenr
)
5009 WARN_ON(nr
>= map
->num_stripes
);
5016 *stripe_len
= rmap_len
;
5018 free_extent_map(em
);
5022 static void *merge_stripe_index_into_bio_private(void *bi_private
,
5023 unsigned int stripe_index
)
5026 * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
5028 * The alternative solution (instead of stealing bits from the
5029 * pointer) would be to allocate an intermediate structure
5030 * that contains the old private pointer plus the stripe_index.
5032 BUG_ON((((uintptr_t)bi_private
) & 3) != 0);
5033 BUG_ON(stripe_index
> 3);
5034 return (void *)(((uintptr_t)bi_private
) | stripe_index
);
5037 static struct btrfs_bio
*extract_bbio_from_bio_private(void *bi_private
)
5039 return (struct btrfs_bio
*)(((uintptr_t)bi_private
) & ~((uintptr_t)3));
5042 static unsigned int extract_stripe_index_from_bio_private(void *bi_private
)
5044 return (unsigned int)((uintptr_t)bi_private
) & 3;
5047 static void btrfs_end_bio(struct bio
*bio
, int err
)
5049 struct btrfs_bio
*bbio
= extract_bbio_from_bio_private(bio
->bi_private
);
5050 int is_orig_bio
= 0;
5053 atomic_inc(&bbio
->error
);
5054 if (err
== -EIO
|| err
== -EREMOTEIO
) {
5055 unsigned int stripe_index
=
5056 extract_stripe_index_from_bio_private(
5058 struct btrfs_device
*dev
;
5060 BUG_ON(stripe_index
>= bbio
->num_stripes
);
5061 dev
= bbio
->stripes
[stripe_index
].dev
;
5063 if (bio
->bi_rw
& WRITE
)
5064 btrfs_dev_stat_inc(dev
,
5065 BTRFS_DEV_STAT_WRITE_ERRS
);
5067 btrfs_dev_stat_inc(dev
,
5068 BTRFS_DEV_STAT_READ_ERRS
);
5069 if ((bio
->bi_rw
& WRITE_FLUSH
) == WRITE_FLUSH
)
5070 btrfs_dev_stat_inc(dev
,
5071 BTRFS_DEV_STAT_FLUSH_ERRS
);
5072 btrfs_dev_stat_print_on_error(dev
);
5077 if (bio
== bbio
->orig_bio
)
5080 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
5083 bio
= bbio
->orig_bio
;
5085 bio
->bi_private
= bbio
->private;
5086 bio
->bi_end_io
= bbio
->end_io
;
5087 bio
->bi_bdev
= (struct block_device
*)
5088 (unsigned long)bbio
->mirror_num
;
5089 /* only send an error to the higher layers if it is
5090 * beyond the tolerance of the btrfs bio
5092 if (atomic_read(&bbio
->error
) > bbio
->max_errors
) {
5096 * this bio is actually up to date, we didn't
5097 * go over the max number of errors
5099 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
5104 bio_endio(bio
, err
);
5105 } else if (!is_orig_bio
) {
5110 struct async_sched
{
5113 struct btrfs_fs_info
*info
;
5114 struct btrfs_work work
;
5118 * see run_scheduled_bios for a description of why bios are collected for
5121 * This will add one bio to the pending list for a device and make sure
5122 * the work struct is scheduled.
5124 static noinline
void btrfs_schedule_bio(struct btrfs_root
*root
,
5125 struct btrfs_device
*device
,
5126 int rw
, struct bio
*bio
)
5128 int should_queue
= 1;
5129 struct btrfs_pending_bios
*pending_bios
;
5131 if (device
->missing
|| !device
->bdev
) {
5132 bio_endio(bio
, -EIO
);
5136 /* don't bother with additional async steps for reads, right now */
5137 if (!(rw
& REQ_WRITE
)) {
5139 btrfsic_submit_bio(rw
, bio
);
5145 * nr_async_bios allows us to reliably return congestion to the
5146 * higher layers. Otherwise, the async bio makes it appear we have
5147 * made progress against dirty pages when we've really just put it
5148 * on a queue for later
5150 atomic_inc(&root
->fs_info
->nr_async_bios
);
5151 WARN_ON(bio
->bi_next
);
5152 bio
->bi_next
= NULL
;
5155 spin_lock(&device
->io_lock
);
5156 if (bio
->bi_rw
& REQ_SYNC
)
5157 pending_bios
= &device
->pending_sync_bios
;
5159 pending_bios
= &device
->pending_bios
;
5161 if (pending_bios
->tail
)
5162 pending_bios
->tail
->bi_next
= bio
;
5164 pending_bios
->tail
= bio
;
5165 if (!pending_bios
->head
)
5166 pending_bios
->head
= bio
;
5167 if (device
->running_pending
)
5170 spin_unlock(&device
->io_lock
);
5173 btrfs_queue_worker(&root
->fs_info
->submit_workers
,
5177 static int bio_size_ok(struct block_device
*bdev
, struct bio
*bio
,
5180 struct bio_vec
*prev
;
5181 struct request_queue
*q
= bdev_get_queue(bdev
);
5182 unsigned short max_sectors
= queue_max_sectors(q
);
5183 struct bvec_merge_data bvm
= {
5185 .bi_sector
= sector
,
5186 .bi_rw
= bio
->bi_rw
,
5189 if (bio
->bi_vcnt
== 0) {
5194 prev
= &bio
->bi_io_vec
[bio
->bi_vcnt
- 1];
5195 if ((bio
->bi_size
>> 9) > max_sectors
)
5198 if (!q
->merge_bvec_fn
)
5201 bvm
.bi_size
= bio
->bi_size
- prev
->bv_len
;
5202 if (q
->merge_bvec_fn(q
, &bvm
, prev
) < prev
->bv_len
)
5207 static void submit_stripe_bio(struct btrfs_root
*root
, struct btrfs_bio
*bbio
,
5208 struct bio
*bio
, u64 physical
, int dev_nr
,
5211 struct btrfs_device
*dev
= bbio
->stripes
[dev_nr
].dev
;
5213 bio
->bi_private
= bbio
;
5214 bio
->bi_private
= merge_stripe_index_into_bio_private(
5215 bio
->bi_private
, (unsigned int)dev_nr
);
5216 bio
->bi_end_io
= btrfs_end_bio
;
5217 bio
->bi_sector
= physical
>> 9;
5220 struct rcu_string
*name
;
5223 name
= rcu_dereference(dev
->name
);
5224 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
5225 "(%s id %llu), size=%u\n", rw
,
5226 (u64
)bio
->bi_sector
, (u_long
)dev
->bdev
->bd_dev
,
5227 name
->str
, dev
->devid
, bio
->bi_size
);
5231 bio
->bi_bdev
= dev
->bdev
;
5233 btrfs_schedule_bio(root
, dev
, rw
, bio
);
5235 btrfsic_submit_bio(rw
, bio
);
5238 static int breakup_stripe_bio(struct btrfs_root
*root
, struct btrfs_bio
*bbio
,
5239 struct bio
*first_bio
, struct btrfs_device
*dev
,
5240 int dev_nr
, int rw
, int async
)
5242 struct bio_vec
*bvec
= first_bio
->bi_io_vec
;
5244 int nr_vecs
= bio_get_nr_vecs(dev
->bdev
);
5245 u64 physical
= bbio
->stripes
[dev_nr
].physical
;
5248 bio
= btrfs_bio_alloc(dev
->bdev
, physical
>> 9, nr_vecs
, GFP_NOFS
);
5252 while (bvec
<= (first_bio
->bi_io_vec
+ first_bio
->bi_vcnt
- 1)) {
5253 if (bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
5254 bvec
->bv_offset
) < bvec
->bv_len
) {
5255 u64 len
= bio
->bi_size
;
5257 atomic_inc(&bbio
->stripes_pending
);
5258 submit_stripe_bio(root
, bbio
, bio
, physical
, dev_nr
,
5266 submit_stripe_bio(root
, bbio
, bio
, physical
, dev_nr
, rw
, async
);
5270 static void bbio_error(struct btrfs_bio
*bbio
, struct bio
*bio
, u64 logical
)
5272 atomic_inc(&bbio
->error
);
5273 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
5274 bio
->bi_private
= bbio
->private;
5275 bio
->bi_end_io
= bbio
->end_io
;
5276 bio
->bi_bdev
= (struct block_device
*)
5277 (unsigned long)bbio
->mirror_num
;
5278 bio
->bi_sector
= logical
>> 9;
5280 bio_endio(bio
, -EIO
);
5284 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
5285 int mirror_num
, int async_submit
)
5287 struct btrfs_device
*dev
;
5288 struct bio
*first_bio
= bio
;
5289 u64 logical
= (u64
)bio
->bi_sector
<< 9;
5292 u64
*raid_map
= NULL
;
5296 struct btrfs_bio
*bbio
= NULL
;
5298 length
= bio
->bi_size
;
5299 map_length
= length
;
5301 ret
= __btrfs_map_block(root
->fs_info
, rw
, logical
, &map_length
, &bbio
,
5302 mirror_num
, &raid_map
);
5303 if (ret
) /* -ENOMEM */
5306 total_devs
= bbio
->num_stripes
;
5307 bbio
->orig_bio
= first_bio
;
5308 bbio
->private = first_bio
->bi_private
;
5309 bbio
->end_io
= first_bio
->bi_end_io
;
5310 atomic_set(&bbio
->stripes_pending
, bbio
->num_stripes
);
5313 /* In this case, map_length has been set to the length of
5314 a single stripe; not the whole write */
5316 return raid56_parity_write(root
, bio
, bbio
,
5317 raid_map
, map_length
);
5319 return raid56_parity_recover(root
, bio
, bbio
,
5320 raid_map
, map_length
,
5325 if (map_length
< length
) {
5326 btrfs_crit(root
->fs_info
, "mapping failed logical %llu bio len %llu len %llu",
5327 (unsigned long long)logical
,
5328 (unsigned long long)length
,
5329 (unsigned long long)map_length
);
5333 while (dev_nr
< total_devs
) {
5334 dev
= bbio
->stripes
[dev_nr
].dev
;
5335 if (!dev
|| !dev
->bdev
|| (rw
& WRITE
&& !dev
->writeable
)) {
5336 bbio_error(bbio
, first_bio
, logical
);
5342 * Check and see if we're ok with this bio based on it's size
5343 * and offset with the given device.
5345 if (!bio_size_ok(dev
->bdev
, first_bio
,
5346 bbio
->stripes
[dev_nr
].physical
>> 9)) {
5347 ret
= breakup_stripe_bio(root
, bbio
, first_bio
, dev
,
5348 dev_nr
, rw
, async_submit
);
5354 if (dev_nr
< total_devs
- 1) {
5355 bio
= bio_clone(first_bio
, GFP_NOFS
);
5356 BUG_ON(!bio
); /* -ENOMEM */
5361 submit_stripe_bio(root
, bbio
, bio
,
5362 bbio
->stripes
[dev_nr
].physical
, dev_nr
, rw
,
5369 struct btrfs_device
*btrfs_find_device(struct btrfs_fs_info
*fs_info
, u64 devid
,
5372 struct btrfs_device
*device
;
5373 struct btrfs_fs_devices
*cur_devices
;
5375 cur_devices
= fs_info
->fs_devices
;
5376 while (cur_devices
) {
5378 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
5379 device
= __find_device(&cur_devices
->devices
,
5384 cur_devices
= cur_devices
->seed
;
5389 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
5390 u64 devid
, u8
*dev_uuid
)
5392 struct btrfs_device
*device
;
5393 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
5395 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
5398 list_add(&device
->dev_list
,
5399 &fs_devices
->devices
);
5400 device
->dev_root
= root
->fs_info
->dev_root
;
5401 device
->devid
= devid
;
5402 device
->work
.func
= pending_bios_fn
;
5403 device
->fs_devices
= fs_devices
;
5404 device
->missing
= 1;
5405 fs_devices
->num_devices
++;
5406 fs_devices
->missing_devices
++;
5407 spin_lock_init(&device
->io_lock
);
5408 INIT_LIST_HEAD(&device
->dev_alloc_list
);
5409 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
5413 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
5414 struct extent_buffer
*leaf
,
5415 struct btrfs_chunk
*chunk
)
5417 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
5418 struct map_lookup
*map
;
5419 struct extent_map
*em
;
5423 u8 uuid
[BTRFS_UUID_SIZE
];
5428 logical
= key
->offset
;
5429 length
= btrfs_chunk_length(leaf
, chunk
);
5431 read_lock(&map_tree
->map_tree
.lock
);
5432 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
5433 read_unlock(&map_tree
->map_tree
.lock
);
5435 /* already mapped? */
5436 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
5437 free_extent_map(em
);
5440 free_extent_map(em
);
5443 em
= alloc_extent_map();
5446 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
5447 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
5449 free_extent_map(em
);
5453 em
->bdev
= (struct block_device
*)map
;
5454 em
->start
= logical
;
5457 em
->block_start
= 0;
5458 em
->block_len
= em
->len
;
5460 map
->num_stripes
= num_stripes
;
5461 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
5462 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
5463 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
5464 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
5465 map
->type
= btrfs_chunk_type(leaf
, chunk
);
5466 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
5467 for (i
= 0; i
< num_stripes
; i
++) {
5468 map
->stripes
[i
].physical
=
5469 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
5470 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
5471 read_extent_buffer(leaf
, uuid
, (unsigned long)
5472 btrfs_stripe_dev_uuid_nr(chunk
, i
),
5474 map
->stripes
[i
].dev
= btrfs_find_device(root
->fs_info
, devid
,
5476 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
5478 free_extent_map(em
);
5481 if (!map
->stripes
[i
].dev
) {
5482 map
->stripes
[i
].dev
=
5483 add_missing_dev(root
, devid
, uuid
);
5484 if (!map
->stripes
[i
].dev
) {
5486 free_extent_map(em
);
5490 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
5493 write_lock(&map_tree
->map_tree
.lock
);
5494 ret
= add_extent_mapping(&map_tree
->map_tree
, em
, 0);
5495 write_unlock(&map_tree
->map_tree
.lock
);
5496 BUG_ON(ret
); /* Tree corruption */
5497 free_extent_map(em
);
5502 static void fill_device_from_item(struct extent_buffer
*leaf
,
5503 struct btrfs_dev_item
*dev_item
,
5504 struct btrfs_device
*device
)
5508 device
->devid
= btrfs_device_id(leaf
, dev_item
);
5509 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
5510 device
->total_bytes
= device
->disk_total_bytes
;
5511 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
5512 device
->type
= btrfs_device_type(leaf
, dev_item
);
5513 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
5514 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
5515 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
5516 WARN_ON(device
->devid
== BTRFS_DEV_REPLACE_DEVID
);
5517 device
->is_tgtdev_for_dev_replace
= 0;
5519 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
5520 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
5523 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
5525 struct btrfs_fs_devices
*fs_devices
;
5528 BUG_ON(!mutex_is_locked(&uuid_mutex
));
5530 fs_devices
= root
->fs_info
->fs_devices
->seed
;
5531 while (fs_devices
) {
5532 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
5536 fs_devices
= fs_devices
->seed
;
5539 fs_devices
= find_fsid(fsid
);
5545 fs_devices
= clone_fs_devices(fs_devices
);
5546 if (IS_ERR(fs_devices
)) {
5547 ret
= PTR_ERR(fs_devices
);
5551 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
5552 root
->fs_info
->bdev_holder
);
5554 free_fs_devices(fs_devices
);
5558 if (!fs_devices
->seeding
) {
5559 __btrfs_close_devices(fs_devices
);
5560 free_fs_devices(fs_devices
);
5565 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
5566 root
->fs_info
->fs_devices
->seed
= fs_devices
;
5571 static int read_one_dev(struct btrfs_root
*root
,
5572 struct extent_buffer
*leaf
,
5573 struct btrfs_dev_item
*dev_item
)
5575 struct btrfs_device
*device
;
5578 u8 fs_uuid
[BTRFS_UUID_SIZE
];
5579 u8 dev_uuid
[BTRFS_UUID_SIZE
];
5581 devid
= btrfs_device_id(leaf
, dev_item
);
5582 read_extent_buffer(leaf
, dev_uuid
,
5583 (unsigned long)btrfs_device_uuid(dev_item
),
5585 read_extent_buffer(leaf
, fs_uuid
,
5586 (unsigned long)btrfs_device_fsid(dev_item
),
5589 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
5590 ret
= open_seed_devices(root
, fs_uuid
);
5591 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
5595 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
, fs_uuid
);
5596 if (!device
|| !device
->bdev
) {
5597 if (!btrfs_test_opt(root
, DEGRADED
))
5601 btrfs_warn(root
->fs_info
, "devid %llu missing",
5602 (unsigned long long)devid
);
5603 device
= add_missing_dev(root
, devid
, dev_uuid
);
5606 } else if (!device
->missing
) {
5608 * this happens when a device that was properly setup
5609 * in the device info lists suddenly goes bad.
5610 * device->bdev is NULL, and so we have to set
5611 * device->missing to one here
5613 root
->fs_info
->fs_devices
->missing_devices
++;
5614 device
->missing
= 1;
5618 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
5619 BUG_ON(device
->writeable
);
5620 if (device
->generation
!=
5621 btrfs_device_generation(leaf
, dev_item
))
5625 fill_device_from_item(leaf
, dev_item
, device
);
5626 device
->dev_root
= root
->fs_info
->dev_root
;
5627 device
->in_fs_metadata
= 1;
5628 if (device
->writeable
&& !device
->is_tgtdev_for_dev_replace
) {
5629 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
5630 spin_lock(&root
->fs_info
->free_chunk_lock
);
5631 root
->fs_info
->free_chunk_space
+= device
->total_bytes
-
5633 spin_unlock(&root
->fs_info
->free_chunk_lock
);
5639 int btrfs_read_sys_array(struct btrfs_root
*root
)
5641 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
5642 struct extent_buffer
*sb
;
5643 struct btrfs_disk_key
*disk_key
;
5644 struct btrfs_chunk
*chunk
;
5646 unsigned long sb_ptr
;
5652 struct btrfs_key key
;
5654 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
5655 BTRFS_SUPER_INFO_SIZE
);
5658 btrfs_set_buffer_uptodate(sb
);
5659 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, sb
, 0);
5661 * The sb extent buffer is artifical and just used to read the system array.
5662 * btrfs_set_buffer_uptodate() call does not properly mark all it's
5663 * pages up-to-date when the page is larger: extent does not cover the
5664 * whole page and consequently check_page_uptodate does not find all
5665 * the page's extents up-to-date (the hole beyond sb),
5666 * write_extent_buffer then triggers a WARN_ON.
5668 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
5669 * but sb spans only this function. Add an explicit SetPageUptodate call
5670 * to silence the warning eg. on PowerPC 64.
5672 if (PAGE_CACHE_SIZE
> BTRFS_SUPER_INFO_SIZE
)
5673 SetPageUptodate(sb
->pages
[0]);
5675 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
5676 array_size
= btrfs_super_sys_array_size(super_copy
);
5678 ptr
= super_copy
->sys_chunk_array
;
5679 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
5682 while (cur
< array_size
) {
5683 disk_key
= (struct btrfs_disk_key
*)ptr
;
5684 btrfs_disk_key_to_cpu(&key
, disk_key
);
5686 len
= sizeof(*disk_key
); ptr
+= len
;
5690 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
5691 chunk
= (struct btrfs_chunk
*)sb_ptr
;
5692 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
5695 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
5696 len
= btrfs_chunk_item_size(num_stripes
);
5705 free_extent_buffer(sb
);
5709 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
5711 struct btrfs_path
*path
;
5712 struct extent_buffer
*leaf
;
5713 struct btrfs_key key
;
5714 struct btrfs_key found_key
;
5718 root
= root
->fs_info
->chunk_root
;
5720 path
= btrfs_alloc_path();
5724 mutex_lock(&uuid_mutex
);
5727 /* first we search for all of the device items, and then we
5728 * read in all of the chunk items. This way we can create chunk
5729 * mappings that reference all of the devices that are afound
5731 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
5735 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5739 leaf
= path
->nodes
[0];
5740 slot
= path
->slots
[0];
5741 if (slot
>= btrfs_header_nritems(leaf
)) {
5742 ret
= btrfs_next_leaf(root
, path
);
5749 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
5750 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
5751 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
5753 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
5754 struct btrfs_dev_item
*dev_item
;
5755 dev_item
= btrfs_item_ptr(leaf
, slot
,
5756 struct btrfs_dev_item
);
5757 ret
= read_one_dev(root
, leaf
, dev_item
);
5761 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
5762 struct btrfs_chunk
*chunk
;
5763 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
5764 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
5770 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
5772 btrfs_release_path(path
);
5777 unlock_chunks(root
);
5778 mutex_unlock(&uuid_mutex
);
5780 btrfs_free_path(path
);
5784 static void __btrfs_reset_dev_stats(struct btrfs_device
*dev
)
5788 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
5789 btrfs_dev_stat_reset(dev
, i
);
5792 int btrfs_init_dev_stats(struct btrfs_fs_info
*fs_info
)
5794 struct btrfs_key key
;
5795 struct btrfs_key found_key
;
5796 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
5797 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
5798 struct extent_buffer
*eb
;
5801 struct btrfs_device
*device
;
5802 struct btrfs_path
*path
= NULL
;
5805 path
= btrfs_alloc_path();
5811 mutex_lock(&fs_devices
->device_list_mutex
);
5812 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
5814 struct btrfs_dev_stats_item
*ptr
;
5817 key
.type
= BTRFS_DEV_STATS_KEY
;
5818 key
.offset
= device
->devid
;
5819 ret
= btrfs_search_slot(NULL
, dev_root
, &key
, path
, 0, 0);
5821 __btrfs_reset_dev_stats(device
);
5822 device
->dev_stats_valid
= 1;
5823 btrfs_release_path(path
);
5826 slot
= path
->slots
[0];
5827 eb
= path
->nodes
[0];
5828 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
5829 item_size
= btrfs_item_size_nr(eb
, slot
);
5831 ptr
= btrfs_item_ptr(eb
, slot
,
5832 struct btrfs_dev_stats_item
);
5834 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
5835 if (item_size
>= (1 + i
) * sizeof(__le64
))
5836 btrfs_dev_stat_set(device
, i
,
5837 btrfs_dev_stats_value(eb
, ptr
, i
));
5839 btrfs_dev_stat_reset(device
, i
);
5842 device
->dev_stats_valid
= 1;
5843 btrfs_dev_stat_print_on_load(device
);
5844 btrfs_release_path(path
);
5846 mutex_unlock(&fs_devices
->device_list_mutex
);
5849 btrfs_free_path(path
);
5850 return ret
< 0 ? ret
: 0;
5853 static int update_dev_stat_item(struct btrfs_trans_handle
*trans
,
5854 struct btrfs_root
*dev_root
,
5855 struct btrfs_device
*device
)
5857 struct btrfs_path
*path
;
5858 struct btrfs_key key
;
5859 struct extent_buffer
*eb
;
5860 struct btrfs_dev_stats_item
*ptr
;
5865 key
.type
= BTRFS_DEV_STATS_KEY
;
5866 key
.offset
= device
->devid
;
5868 path
= btrfs_alloc_path();
5870 ret
= btrfs_search_slot(trans
, dev_root
, &key
, path
, -1, 1);
5872 printk_in_rcu(KERN_WARNING
"btrfs: error %d while searching for dev_stats item for device %s!\n",
5873 ret
, rcu_str_deref(device
->name
));
5878 btrfs_item_size_nr(path
->nodes
[0], path
->slots
[0]) < sizeof(*ptr
)) {
5879 /* need to delete old one and insert a new one */
5880 ret
= btrfs_del_item(trans
, dev_root
, path
);
5882 printk_in_rcu(KERN_WARNING
"btrfs: delete too small dev_stats item for device %s failed %d!\n",
5883 rcu_str_deref(device
->name
), ret
);
5890 /* need to insert a new item */
5891 btrfs_release_path(path
);
5892 ret
= btrfs_insert_empty_item(trans
, dev_root
, path
,
5893 &key
, sizeof(*ptr
));
5895 printk_in_rcu(KERN_WARNING
"btrfs: insert dev_stats item for device %s failed %d!\n",
5896 rcu_str_deref(device
->name
), ret
);
5901 eb
= path
->nodes
[0];
5902 ptr
= btrfs_item_ptr(eb
, path
->slots
[0], struct btrfs_dev_stats_item
);
5903 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
5904 btrfs_set_dev_stats_value(eb
, ptr
, i
,
5905 btrfs_dev_stat_read(device
, i
));
5906 btrfs_mark_buffer_dirty(eb
);
5909 btrfs_free_path(path
);
5914 * called from commit_transaction. Writes all changed device stats to disk.
5916 int btrfs_run_dev_stats(struct btrfs_trans_handle
*trans
,
5917 struct btrfs_fs_info
*fs_info
)
5919 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
5920 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
5921 struct btrfs_device
*device
;
5924 mutex_lock(&fs_devices
->device_list_mutex
);
5925 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
5926 if (!device
->dev_stats_valid
|| !device
->dev_stats_dirty
)
5929 ret
= update_dev_stat_item(trans
, dev_root
, device
);
5931 device
->dev_stats_dirty
= 0;
5933 mutex_unlock(&fs_devices
->device_list_mutex
);
5938 void btrfs_dev_stat_inc_and_print(struct btrfs_device
*dev
, int index
)
5940 btrfs_dev_stat_inc(dev
, index
);
5941 btrfs_dev_stat_print_on_error(dev
);
5944 static void btrfs_dev_stat_print_on_error(struct btrfs_device
*dev
)
5946 if (!dev
->dev_stats_valid
)
5948 printk_ratelimited_in_rcu(KERN_ERR
5949 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5950 rcu_str_deref(dev
->name
),
5951 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
5952 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
5953 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
5954 btrfs_dev_stat_read(dev
,
5955 BTRFS_DEV_STAT_CORRUPTION_ERRS
),
5956 btrfs_dev_stat_read(dev
,
5957 BTRFS_DEV_STAT_GENERATION_ERRS
));
5960 static void btrfs_dev_stat_print_on_load(struct btrfs_device
*dev
)
5964 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
5965 if (btrfs_dev_stat_read(dev
, i
) != 0)
5967 if (i
== BTRFS_DEV_STAT_VALUES_MAX
)
5968 return; /* all values == 0, suppress message */
5970 printk_in_rcu(KERN_INFO
"btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5971 rcu_str_deref(dev
->name
),
5972 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
5973 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
5974 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
5975 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_CORRUPTION_ERRS
),
5976 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_GENERATION_ERRS
));
5979 int btrfs_get_dev_stats(struct btrfs_root
*root
,
5980 struct btrfs_ioctl_get_dev_stats
*stats
)
5982 struct btrfs_device
*dev
;
5983 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
5986 mutex_lock(&fs_devices
->device_list_mutex
);
5987 dev
= btrfs_find_device(root
->fs_info
, stats
->devid
, NULL
, NULL
);
5988 mutex_unlock(&fs_devices
->device_list_mutex
);
5992 "btrfs: get dev_stats failed, device not found\n");
5994 } else if (!dev
->dev_stats_valid
) {
5996 "btrfs: get dev_stats failed, not yet valid\n");
5998 } else if (stats
->flags
& BTRFS_DEV_STATS_RESET
) {
5999 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
6000 if (stats
->nr_items
> i
)
6002 btrfs_dev_stat_read_and_reset(dev
, i
);
6004 btrfs_dev_stat_reset(dev
, i
);
6007 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
6008 if (stats
->nr_items
> i
)
6009 stats
->values
[i
] = btrfs_dev_stat_read(dev
, i
);
6011 if (stats
->nr_items
> BTRFS_DEV_STAT_VALUES_MAX
)
6012 stats
->nr_items
= BTRFS_DEV_STAT_VALUES_MAX
;
6016 int btrfs_scratch_superblock(struct btrfs_device
*device
)
6018 struct buffer_head
*bh
;
6019 struct btrfs_super_block
*disk_super
;
6021 bh
= btrfs_read_dev_super(device
->bdev
);
6024 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
6026 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
6027 set_buffer_dirty(bh
);
6028 sync_dirty_buffer(bh
);