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 <linux/semaphore.h>
30 #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"
45 static int init_first_rw_device(struct btrfs_trans_handle
*trans
,
46 struct btrfs_root
*root
,
47 struct btrfs_device
*device
);
48 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
);
49 static void __btrfs_reset_dev_stats(struct btrfs_device
*dev
);
50 static void btrfs_dev_stat_print_on_error(struct btrfs_device
*dev
);
51 static void btrfs_dev_stat_print_on_load(struct btrfs_device
*device
);
53 static DEFINE_MUTEX(uuid_mutex
);
54 static LIST_HEAD(fs_uuids
);
56 static void lock_chunks(struct btrfs_root
*root
)
58 mutex_lock(&root
->fs_info
->chunk_mutex
);
61 static void unlock_chunks(struct btrfs_root
*root
)
63 mutex_unlock(&root
->fs_info
->chunk_mutex
);
66 static struct btrfs_fs_devices
*__alloc_fs_devices(void)
68 struct btrfs_fs_devices
*fs_devs
;
70 fs_devs
= kzalloc(sizeof(*fs_devs
), GFP_NOFS
);
72 return ERR_PTR(-ENOMEM
);
74 mutex_init(&fs_devs
->device_list_mutex
);
76 INIT_LIST_HEAD(&fs_devs
->devices
);
77 INIT_LIST_HEAD(&fs_devs
->resized_devices
);
78 INIT_LIST_HEAD(&fs_devs
->alloc_list
);
79 INIT_LIST_HEAD(&fs_devs
->list
);
85 * alloc_fs_devices - allocate struct btrfs_fs_devices
86 * @fsid: a pointer to UUID for this FS. If NULL a new UUID is
89 * Return: a pointer to a new &struct btrfs_fs_devices on success;
90 * ERR_PTR() on error. Returned struct is not linked onto any lists and
91 * can be destroyed with kfree() right away.
93 static struct btrfs_fs_devices
*alloc_fs_devices(const u8
*fsid
)
95 struct btrfs_fs_devices
*fs_devs
;
97 fs_devs
= __alloc_fs_devices();
102 memcpy(fs_devs
->fsid
, fsid
, BTRFS_FSID_SIZE
);
104 generate_random_uuid(fs_devs
->fsid
);
109 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
111 struct btrfs_device
*device
;
112 WARN_ON(fs_devices
->opened
);
113 while (!list_empty(&fs_devices
->devices
)) {
114 device
= list_entry(fs_devices
->devices
.next
,
115 struct btrfs_device
, dev_list
);
116 list_del(&device
->dev_list
);
117 rcu_string_free(device
->name
);
123 static void btrfs_kobject_uevent(struct block_device
*bdev
,
124 enum kobject_action action
)
128 ret
= kobject_uevent(&disk_to_dev(bdev
->bd_disk
)->kobj
, action
);
130 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
132 kobject_name(&disk_to_dev(bdev
->bd_disk
)->kobj
),
133 &disk_to_dev(bdev
->bd_disk
)->kobj
);
136 void btrfs_cleanup_fs_uuids(void)
138 struct btrfs_fs_devices
*fs_devices
;
140 while (!list_empty(&fs_uuids
)) {
141 fs_devices
= list_entry(fs_uuids
.next
,
142 struct btrfs_fs_devices
, list
);
143 list_del(&fs_devices
->list
);
144 free_fs_devices(fs_devices
);
148 static struct btrfs_device
*__alloc_device(void)
150 struct btrfs_device
*dev
;
152 dev
= kzalloc(sizeof(*dev
), GFP_NOFS
);
154 return ERR_PTR(-ENOMEM
);
156 INIT_LIST_HEAD(&dev
->dev_list
);
157 INIT_LIST_HEAD(&dev
->dev_alloc_list
);
158 INIT_LIST_HEAD(&dev
->resized_list
);
160 spin_lock_init(&dev
->io_lock
);
162 spin_lock_init(&dev
->reada_lock
);
163 atomic_set(&dev
->reada_in_flight
, 0);
164 atomic_set(&dev
->dev_stats_ccnt
, 0);
165 INIT_RADIX_TREE(&dev
->reada_zones
, GFP_NOFS
& ~__GFP_WAIT
);
166 INIT_RADIX_TREE(&dev
->reada_extents
, GFP_NOFS
& ~__GFP_WAIT
);
171 static noinline
struct btrfs_device
*__find_device(struct list_head
*head
,
174 struct btrfs_device
*dev
;
176 list_for_each_entry(dev
, head
, dev_list
) {
177 if (dev
->devid
== devid
&&
178 (!uuid
|| !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
))) {
185 static noinline
struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
187 struct btrfs_fs_devices
*fs_devices
;
189 list_for_each_entry(fs_devices
, &fs_uuids
, list
) {
190 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
197 btrfs_get_bdev_and_sb(const char *device_path
, fmode_t flags
, void *holder
,
198 int flush
, struct block_device
**bdev
,
199 struct buffer_head
**bh
)
203 *bdev
= blkdev_get_by_path(device_path
, flags
, holder
);
206 ret
= PTR_ERR(*bdev
);
207 printk(KERN_INFO
"BTRFS: open %s failed\n", device_path
);
212 filemap_write_and_wait((*bdev
)->bd_inode
->i_mapping
);
213 ret
= set_blocksize(*bdev
, 4096);
215 blkdev_put(*bdev
, flags
);
218 invalidate_bdev(*bdev
);
219 *bh
= btrfs_read_dev_super(*bdev
);
222 blkdev_put(*bdev
, flags
);
234 static void requeue_list(struct btrfs_pending_bios
*pending_bios
,
235 struct bio
*head
, struct bio
*tail
)
238 struct bio
*old_head
;
240 old_head
= pending_bios
->head
;
241 pending_bios
->head
= head
;
242 if (pending_bios
->tail
)
243 tail
->bi_next
= old_head
;
245 pending_bios
->tail
= tail
;
249 * we try to collect pending bios for a device so we don't get a large
250 * number of procs sending bios down to the same device. This greatly
251 * improves the schedulers ability to collect and merge the bios.
253 * But, it also turns into a long list of bios to process and that is sure
254 * to eventually make the worker thread block. The solution here is to
255 * make some progress and then put this work struct back at the end of
256 * the list if the block device is congested. This way, multiple devices
257 * can make progress from a single worker thread.
259 static noinline
void run_scheduled_bios(struct btrfs_device
*device
)
262 struct backing_dev_info
*bdi
;
263 struct btrfs_fs_info
*fs_info
;
264 struct btrfs_pending_bios
*pending_bios
;
268 unsigned long num_run
;
269 unsigned long batch_run
= 0;
271 unsigned long last_waited
= 0;
273 int sync_pending
= 0;
274 struct blk_plug plug
;
277 * this function runs all the bios we've collected for
278 * a particular device. We don't want to wander off to
279 * another device without first sending all of these down.
280 * So, setup a plug here and finish it off before we return
282 blk_start_plug(&plug
);
284 bdi
= blk_get_backing_dev_info(device
->bdev
);
285 fs_info
= device
->dev_root
->fs_info
;
286 limit
= btrfs_async_submit_limit(fs_info
);
287 limit
= limit
* 2 / 3;
290 spin_lock(&device
->io_lock
);
295 /* take all the bios off the list at once and process them
296 * later on (without the lock held). But, remember the
297 * tail and other pointers so the bios can be properly reinserted
298 * into the list if we hit congestion
300 if (!force_reg
&& device
->pending_sync_bios
.head
) {
301 pending_bios
= &device
->pending_sync_bios
;
304 pending_bios
= &device
->pending_bios
;
308 pending
= pending_bios
->head
;
309 tail
= pending_bios
->tail
;
310 WARN_ON(pending
&& !tail
);
313 * if pending was null this time around, no bios need processing
314 * at all and we can stop. Otherwise it'll loop back up again
315 * and do an additional check so no bios are missed.
317 * device->running_pending is used to synchronize with the
320 if (device
->pending_sync_bios
.head
== NULL
&&
321 device
->pending_bios
.head
== NULL
) {
323 device
->running_pending
= 0;
326 device
->running_pending
= 1;
329 pending_bios
->head
= NULL
;
330 pending_bios
->tail
= NULL
;
332 spin_unlock(&device
->io_lock
);
337 /* we want to work on both lists, but do more bios on the
338 * sync list than the regular list
341 pending_bios
!= &device
->pending_sync_bios
&&
342 device
->pending_sync_bios
.head
) ||
343 (num_run
> 64 && pending_bios
== &device
->pending_sync_bios
&&
344 device
->pending_bios
.head
)) {
345 spin_lock(&device
->io_lock
);
346 requeue_list(pending_bios
, pending
, tail
);
351 pending
= pending
->bi_next
;
354 if (atomic_dec_return(&fs_info
->nr_async_bios
) < limit
&&
355 waitqueue_active(&fs_info
->async_submit_wait
))
356 wake_up(&fs_info
->async_submit_wait
);
358 BUG_ON(atomic_read(&cur
->bi_cnt
) == 0);
361 * if we're doing the sync list, record that our
362 * plug has some sync requests on it
364 * If we're doing the regular list and there are
365 * sync requests sitting around, unplug before
368 if (pending_bios
== &device
->pending_sync_bios
) {
370 } else if (sync_pending
) {
371 blk_finish_plug(&plug
);
372 blk_start_plug(&plug
);
376 btrfsic_submit_bio(cur
->bi_rw
, cur
);
383 * we made progress, there is more work to do and the bdi
384 * is now congested. Back off and let other work structs
387 if (pending
&& bdi_write_congested(bdi
) && batch_run
> 8 &&
388 fs_info
->fs_devices
->open_devices
> 1) {
389 struct io_context
*ioc
;
391 ioc
= current
->io_context
;
394 * the main goal here is that we don't want to
395 * block if we're going to be able to submit
396 * more requests without blocking.
398 * This code does two great things, it pokes into
399 * the elevator code from a filesystem _and_
400 * it makes assumptions about how batching works.
402 if (ioc
&& ioc
->nr_batch_requests
> 0 &&
403 time_before(jiffies
, ioc
->last_waited
+ HZ
/50UL) &&
405 ioc
->last_waited
== last_waited
)) {
407 * we want to go through our batch of
408 * requests and stop. So, we copy out
409 * the ioc->last_waited time and test
410 * against it before looping
412 last_waited
= ioc
->last_waited
;
417 spin_lock(&device
->io_lock
);
418 requeue_list(pending_bios
, pending
, tail
);
419 device
->running_pending
= 1;
421 spin_unlock(&device
->io_lock
);
422 btrfs_queue_work(fs_info
->submit_workers
,
426 /* unplug every 64 requests just for good measure */
427 if (batch_run
% 64 == 0) {
428 blk_finish_plug(&plug
);
429 blk_start_plug(&plug
);
438 spin_lock(&device
->io_lock
);
439 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
441 spin_unlock(&device
->io_lock
);
444 blk_finish_plug(&plug
);
447 static void pending_bios_fn(struct btrfs_work
*work
)
449 struct btrfs_device
*device
;
451 device
= container_of(work
, struct btrfs_device
, work
);
452 run_scheduled_bios(device
);
456 * Add new device to list of registered devices
459 * 1 - first time device is seen
460 * 0 - device already known
463 static noinline
int device_list_add(const char *path
,
464 struct btrfs_super_block
*disk_super
,
465 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
467 struct btrfs_device
*device
;
468 struct btrfs_fs_devices
*fs_devices
;
469 struct rcu_string
*name
;
471 u64 found_transid
= btrfs_super_generation(disk_super
);
473 fs_devices
= find_fsid(disk_super
->fsid
);
475 fs_devices
= alloc_fs_devices(disk_super
->fsid
);
476 if (IS_ERR(fs_devices
))
477 return PTR_ERR(fs_devices
);
479 list_add(&fs_devices
->list
, &fs_uuids
);
483 device
= __find_device(&fs_devices
->devices
, devid
,
484 disk_super
->dev_item
.uuid
);
488 if (fs_devices
->opened
)
491 device
= btrfs_alloc_device(NULL
, &devid
,
492 disk_super
->dev_item
.uuid
);
493 if (IS_ERR(device
)) {
494 /* we can safely leave the fs_devices entry around */
495 return PTR_ERR(device
);
498 name
= rcu_string_strdup(path
, GFP_NOFS
);
503 rcu_assign_pointer(device
->name
, name
);
505 mutex_lock(&fs_devices
->device_list_mutex
);
506 list_add_rcu(&device
->dev_list
, &fs_devices
->devices
);
507 fs_devices
->num_devices
++;
508 mutex_unlock(&fs_devices
->device_list_mutex
);
511 device
->fs_devices
= fs_devices
;
512 } else if (!device
->name
|| strcmp(device
->name
->str
, path
)) {
514 * When FS is already mounted.
515 * 1. If you are here and if the device->name is NULL that
516 * means this device was missing at time of FS mount.
517 * 2. If you are here and if the device->name is different
518 * from 'path' that means either
519 * a. The same device disappeared and reappeared with
521 * b. The missing-disk-which-was-replaced, has
524 * We must allow 1 and 2a above. But 2b would be a spurious
527 * Further in case of 1 and 2a above, the disk at 'path'
528 * would have missed some transaction when it was away and
529 * in case of 2a the stale bdev has to be updated as well.
530 * 2b must not be allowed at all time.
534 * As of now don't allow update to btrfs_fs_device through
535 * the btrfs dev scan cli, after FS has been mounted.
537 if (fs_devices
->opened
) {
541 * That is if the FS is _not_ mounted and if you
542 * are here, that means there is more than one
543 * disk with same uuid and devid.We keep the one
544 * with larger generation number or the last-in if
545 * generation are equal.
547 if (found_transid
< device
->generation
)
551 name
= rcu_string_strdup(path
, GFP_NOFS
);
554 rcu_string_free(device
->name
);
555 rcu_assign_pointer(device
->name
, name
);
556 if (device
->missing
) {
557 fs_devices
->missing_devices
--;
563 * Unmount does not free the btrfs_device struct but would zero
564 * generation along with most of the other members. So just update
565 * it back. We need it to pick the disk with largest generation
568 if (!fs_devices
->opened
)
569 device
->generation
= found_transid
;
571 *fs_devices_ret
= fs_devices
;
576 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
578 struct btrfs_fs_devices
*fs_devices
;
579 struct btrfs_device
*device
;
580 struct btrfs_device
*orig_dev
;
582 fs_devices
= alloc_fs_devices(orig
->fsid
);
583 if (IS_ERR(fs_devices
))
586 fs_devices
->total_devices
= orig
->total_devices
;
588 /* We have held the volume lock, it is safe to get the devices. */
589 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
590 struct rcu_string
*name
;
592 device
= btrfs_alloc_device(NULL
, &orig_dev
->devid
,
598 * This is ok to do without rcu read locked because we hold the
599 * uuid mutex so nothing we touch in here is going to disappear.
601 if (orig_dev
->name
) {
602 name
= rcu_string_strdup(orig_dev
->name
->str
, GFP_NOFS
);
607 rcu_assign_pointer(device
->name
, name
);
610 list_add(&device
->dev_list
, &fs_devices
->devices
);
611 device
->fs_devices
= fs_devices
;
612 fs_devices
->num_devices
++;
616 free_fs_devices(fs_devices
);
617 return ERR_PTR(-ENOMEM
);
620 void btrfs_close_extra_devices(struct btrfs_fs_info
*fs_info
,
621 struct btrfs_fs_devices
*fs_devices
, int step
)
623 struct btrfs_device
*device
, *next
;
624 struct btrfs_device
*latest_dev
= NULL
;
626 mutex_lock(&uuid_mutex
);
628 /* This is the initialized path, it is safe to release the devices. */
629 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
630 if (device
->in_fs_metadata
) {
631 if (!device
->is_tgtdev_for_dev_replace
&&
633 device
->generation
> latest_dev
->generation
)) {
639 if (device
->devid
== BTRFS_DEV_REPLACE_DEVID
) {
641 * In the first step, keep the device which has
642 * the correct fsid and the devid that is used
643 * for the dev_replace procedure.
644 * In the second step, the dev_replace state is
645 * read from the device tree and it is known
646 * whether the procedure is really active or
647 * not, which means whether this device is
648 * used or whether it should be removed.
650 if (step
== 0 || device
->is_tgtdev_for_dev_replace
) {
655 blkdev_put(device
->bdev
, device
->mode
);
657 fs_devices
->open_devices
--;
659 if (device
->writeable
) {
660 list_del_init(&device
->dev_alloc_list
);
661 device
->writeable
= 0;
662 if (!device
->is_tgtdev_for_dev_replace
)
663 fs_devices
->rw_devices
--;
665 list_del_init(&device
->dev_list
);
666 fs_devices
->num_devices
--;
667 rcu_string_free(device
->name
);
671 if (fs_devices
->seed
) {
672 fs_devices
= fs_devices
->seed
;
676 fs_devices
->latest_bdev
= latest_dev
->bdev
;
678 mutex_unlock(&uuid_mutex
);
681 static void __free_device(struct work_struct
*work
)
683 struct btrfs_device
*device
;
685 device
= container_of(work
, struct btrfs_device
, rcu_work
);
688 blkdev_put(device
->bdev
, device
->mode
);
690 rcu_string_free(device
->name
);
694 static void free_device(struct rcu_head
*head
)
696 struct btrfs_device
*device
;
698 device
= container_of(head
, struct btrfs_device
, rcu
);
700 INIT_WORK(&device
->rcu_work
, __free_device
);
701 schedule_work(&device
->rcu_work
);
704 static int __btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
706 struct btrfs_device
*device
;
708 if (--fs_devices
->opened
> 0)
711 mutex_lock(&fs_devices
->device_list_mutex
);
712 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
713 struct btrfs_device
*new_device
;
714 struct rcu_string
*name
;
717 fs_devices
->open_devices
--;
719 if (device
->writeable
&&
720 device
->devid
!= BTRFS_DEV_REPLACE_DEVID
) {
721 list_del_init(&device
->dev_alloc_list
);
722 fs_devices
->rw_devices
--;
726 fs_devices
->missing_devices
--;
728 new_device
= btrfs_alloc_device(NULL
, &device
->devid
,
730 BUG_ON(IS_ERR(new_device
)); /* -ENOMEM */
732 /* Safe because we are under uuid_mutex */
734 name
= rcu_string_strdup(device
->name
->str
, GFP_NOFS
);
735 BUG_ON(!name
); /* -ENOMEM */
736 rcu_assign_pointer(new_device
->name
, name
);
739 list_replace_rcu(&device
->dev_list
, &new_device
->dev_list
);
740 new_device
->fs_devices
= device
->fs_devices
;
742 call_rcu(&device
->rcu
, free_device
);
744 mutex_unlock(&fs_devices
->device_list_mutex
);
746 WARN_ON(fs_devices
->open_devices
);
747 WARN_ON(fs_devices
->rw_devices
);
748 fs_devices
->opened
= 0;
749 fs_devices
->seeding
= 0;
754 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
756 struct btrfs_fs_devices
*seed_devices
= NULL
;
759 mutex_lock(&uuid_mutex
);
760 ret
= __btrfs_close_devices(fs_devices
);
761 if (!fs_devices
->opened
) {
762 seed_devices
= fs_devices
->seed
;
763 fs_devices
->seed
= NULL
;
765 mutex_unlock(&uuid_mutex
);
767 while (seed_devices
) {
768 fs_devices
= seed_devices
;
769 seed_devices
= fs_devices
->seed
;
770 __btrfs_close_devices(fs_devices
);
771 free_fs_devices(fs_devices
);
774 * Wait for rcu kworkers under __btrfs_close_devices
775 * to finish all blkdev_puts so device is really
776 * free when umount is done.
782 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
783 fmode_t flags
, void *holder
)
785 struct request_queue
*q
;
786 struct block_device
*bdev
;
787 struct list_head
*head
= &fs_devices
->devices
;
788 struct btrfs_device
*device
;
789 struct btrfs_device
*latest_dev
= NULL
;
790 struct buffer_head
*bh
;
791 struct btrfs_super_block
*disk_super
;
798 list_for_each_entry(device
, head
, dev_list
) {
804 /* Just open everything we can; ignore failures here */
805 if (btrfs_get_bdev_and_sb(device
->name
->str
, flags
, holder
, 1,
809 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
810 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
811 if (devid
!= device
->devid
)
814 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
818 device
->generation
= btrfs_super_generation(disk_super
);
820 device
->generation
> latest_dev
->generation
)
823 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
824 device
->writeable
= 0;
826 device
->writeable
= !bdev_read_only(bdev
);
830 q
= bdev_get_queue(bdev
);
831 if (blk_queue_discard(q
))
832 device
->can_discard
= 1;
835 device
->in_fs_metadata
= 0;
836 device
->mode
= flags
;
838 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
839 fs_devices
->rotating
= 1;
841 fs_devices
->open_devices
++;
842 if (device
->writeable
&&
843 device
->devid
!= BTRFS_DEV_REPLACE_DEVID
) {
844 fs_devices
->rw_devices
++;
845 list_add(&device
->dev_alloc_list
,
846 &fs_devices
->alloc_list
);
853 blkdev_put(bdev
, flags
);
856 if (fs_devices
->open_devices
== 0) {
860 fs_devices
->seeding
= seeding
;
861 fs_devices
->opened
= 1;
862 fs_devices
->latest_bdev
= latest_dev
->bdev
;
863 fs_devices
->total_rw_bytes
= 0;
868 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
869 fmode_t flags
, void *holder
)
873 mutex_lock(&uuid_mutex
);
874 if (fs_devices
->opened
) {
875 fs_devices
->opened
++;
878 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
880 mutex_unlock(&uuid_mutex
);
885 * Look for a btrfs signature on a device. This may be called out of the mount path
886 * and we are not allowed to call set_blocksize during the scan. The superblock
887 * is read via pagecache
889 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
890 struct btrfs_fs_devices
**fs_devices_ret
)
892 struct btrfs_super_block
*disk_super
;
893 struct block_device
*bdev
;
904 * we would like to check all the supers, but that would make
905 * a btrfs mount succeed after a mkfs from a different FS.
906 * So, we need to add a special mount option to scan for
907 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
909 bytenr
= btrfs_sb_offset(0);
911 mutex_lock(&uuid_mutex
);
913 bdev
= blkdev_get_by_path(path
, flags
, holder
);
920 /* make sure our super fits in the device */
921 if (bytenr
+ PAGE_CACHE_SIZE
>= i_size_read(bdev
->bd_inode
))
924 /* make sure our super fits in the page */
925 if (sizeof(*disk_super
) > PAGE_CACHE_SIZE
)
928 /* make sure our super doesn't straddle pages on disk */
929 index
= bytenr
>> PAGE_CACHE_SHIFT
;
930 if ((bytenr
+ sizeof(*disk_super
) - 1) >> PAGE_CACHE_SHIFT
!= index
)
933 /* pull in the page with our super */
934 page
= read_cache_page_gfp(bdev
->bd_inode
->i_mapping
,
937 if (IS_ERR_OR_NULL(page
))
942 /* align our pointer to the offset of the super block */
943 disk_super
= p
+ (bytenr
& ~PAGE_CACHE_MASK
);
945 if (btrfs_super_bytenr(disk_super
) != bytenr
||
946 btrfs_super_magic(disk_super
) != BTRFS_MAGIC
)
949 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
950 transid
= btrfs_super_generation(disk_super
);
951 total_devices
= btrfs_super_num_devices(disk_super
);
953 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
955 if (disk_super
->label
[0]) {
956 if (disk_super
->label
[BTRFS_LABEL_SIZE
- 1])
957 disk_super
->label
[BTRFS_LABEL_SIZE
- 1] = '\0';
958 printk(KERN_INFO
"BTRFS: device label %s ", disk_super
->label
);
960 printk(KERN_INFO
"BTRFS: device fsid %pU ", disk_super
->fsid
);
963 printk(KERN_CONT
"devid %llu transid %llu %s\n", devid
, transid
, path
);
966 if (!ret
&& fs_devices_ret
)
967 (*fs_devices_ret
)->total_devices
= total_devices
;
971 page_cache_release(page
);
974 blkdev_put(bdev
, flags
);
976 mutex_unlock(&uuid_mutex
);
980 /* helper to account the used device space in the range */
981 int btrfs_account_dev_extents_size(struct btrfs_device
*device
, u64 start
,
982 u64 end
, u64
*length
)
984 struct btrfs_key key
;
985 struct btrfs_root
*root
= device
->dev_root
;
986 struct btrfs_dev_extent
*dev_extent
;
987 struct btrfs_path
*path
;
991 struct extent_buffer
*l
;
995 if (start
>= device
->total_bytes
|| device
->is_tgtdev_for_dev_replace
)
998 path
= btrfs_alloc_path();
1003 key
.objectid
= device
->devid
;
1005 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1007 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1011 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
1018 slot
= path
->slots
[0];
1019 if (slot
>= btrfs_header_nritems(l
)) {
1020 ret
= btrfs_next_leaf(root
, path
);
1028 btrfs_item_key_to_cpu(l
, &key
, slot
);
1030 if (key
.objectid
< device
->devid
)
1033 if (key
.objectid
> device
->devid
)
1036 if (key
.type
!= BTRFS_DEV_EXTENT_KEY
)
1039 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1040 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
1042 if (key
.offset
<= start
&& extent_end
> end
) {
1043 *length
= end
- start
+ 1;
1045 } else if (key
.offset
<= start
&& extent_end
> start
)
1046 *length
+= extent_end
- start
;
1047 else if (key
.offset
> start
&& extent_end
<= end
)
1048 *length
+= extent_end
- key
.offset
;
1049 else if (key
.offset
> start
&& key
.offset
<= end
) {
1050 *length
+= end
- key
.offset
+ 1;
1052 } else if (key
.offset
> end
)
1060 btrfs_free_path(path
);
1064 static int contains_pending_extent(struct btrfs_trans_handle
*trans
,
1065 struct btrfs_device
*device
,
1066 u64
*start
, u64 len
)
1068 struct extent_map
*em
;
1071 list_for_each_entry(em
, &trans
->transaction
->pending_chunks
, list
) {
1072 struct map_lookup
*map
;
1075 map
= (struct map_lookup
*)em
->bdev
;
1076 for (i
= 0; i
< map
->num_stripes
; i
++) {
1077 if (map
->stripes
[i
].dev
!= device
)
1079 if (map
->stripes
[i
].physical
>= *start
+ len
||
1080 map
->stripes
[i
].physical
+ em
->orig_block_len
<=
1083 *start
= map
->stripes
[i
].physical
+
1094 * find_free_dev_extent - find free space in the specified device
1095 * @device: the device which we search the free space in
1096 * @num_bytes: the size of the free space that we need
1097 * @start: store the start of the free space.
1098 * @len: the size of the free space. that we find, or the size of the max
1099 * free space if we don't find suitable free space
1101 * this uses a pretty simple search, the expectation is that it is
1102 * called very infrequently and that a given device has a small number
1105 * @start is used to store the start of the free space if we find. But if we
1106 * don't find suitable free space, it will be used to store the start position
1107 * of the max free space.
1109 * @len is used to store the size of the free space that we find.
1110 * But if we don't find suitable free space, it is used to store the size of
1111 * the max free space.
1113 int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
1114 struct btrfs_device
*device
, u64 num_bytes
,
1115 u64
*start
, u64
*len
)
1117 struct btrfs_key key
;
1118 struct btrfs_root
*root
= device
->dev_root
;
1119 struct btrfs_dev_extent
*dev_extent
;
1120 struct btrfs_path
*path
;
1126 u64 search_end
= device
->total_bytes
;
1129 struct extent_buffer
*l
;
1131 /* FIXME use last free of some kind */
1133 /* we don't want to overwrite the superblock on the drive,
1134 * so we make sure to start at an offset of at least 1MB
1136 search_start
= max(root
->fs_info
->alloc_start
, 1024ull * 1024);
1138 path
= btrfs_alloc_path();
1142 max_hole_start
= search_start
;
1146 if (search_start
>= search_end
|| device
->is_tgtdev_for_dev_replace
) {
1152 path
->search_commit_root
= 1;
1153 path
->skip_locking
= 1;
1155 key
.objectid
= device
->devid
;
1156 key
.offset
= search_start
;
1157 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1159 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1163 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
1170 slot
= path
->slots
[0];
1171 if (slot
>= btrfs_header_nritems(l
)) {
1172 ret
= btrfs_next_leaf(root
, path
);
1180 btrfs_item_key_to_cpu(l
, &key
, slot
);
1182 if (key
.objectid
< device
->devid
)
1185 if (key
.objectid
> device
->devid
)
1188 if (key
.type
!= BTRFS_DEV_EXTENT_KEY
)
1191 if (key
.offset
> search_start
) {
1192 hole_size
= key
.offset
- search_start
;
1195 * Have to check before we set max_hole_start, otherwise
1196 * we could end up sending back this offset anyway.
1198 if (contains_pending_extent(trans
, device
,
1203 if (hole_size
> max_hole_size
) {
1204 max_hole_start
= search_start
;
1205 max_hole_size
= hole_size
;
1209 * If this free space is greater than which we need,
1210 * it must be the max free space that we have found
1211 * until now, so max_hole_start must point to the start
1212 * of this free space and the length of this free space
1213 * is stored in max_hole_size. Thus, we return
1214 * max_hole_start and max_hole_size and go back to the
1217 if (hole_size
>= num_bytes
) {
1223 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1224 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
1226 if (extent_end
> search_start
)
1227 search_start
= extent_end
;
1234 * At this point, search_start should be the end of
1235 * allocated dev extents, and when shrinking the device,
1236 * search_end may be smaller than search_start.
1238 if (search_end
> search_start
)
1239 hole_size
= search_end
- search_start
;
1241 if (hole_size
> max_hole_size
) {
1242 max_hole_start
= search_start
;
1243 max_hole_size
= hole_size
;
1246 if (contains_pending_extent(trans
, device
, &search_start
, hole_size
)) {
1247 btrfs_release_path(path
);
1252 if (hole_size
< num_bytes
)
1258 btrfs_free_path(path
);
1259 *start
= max_hole_start
;
1261 *len
= max_hole_size
;
1265 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
1266 struct btrfs_device
*device
,
1270 struct btrfs_path
*path
;
1271 struct btrfs_root
*root
= device
->dev_root
;
1272 struct btrfs_key key
;
1273 struct btrfs_key found_key
;
1274 struct extent_buffer
*leaf
= NULL
;
1275 struct btrfs_dev_extent
*extent
= NULL
;
1277 path
= btrfs_alloc_path();
1281 key
.objectid
= device
->devid
;
1283 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1285 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1287 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
1288 BTRFS_DEV_EXTENT_KEY
);
1291 leaf
= path
->nodes
[0];
1292 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1293 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1294 struct btrfs_dev_extent
);
1295 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
1296 btrfs_dev_extent_length(leaf
, extent
) < start
);
1298 btrfs_release_path(path
);
1300 } else if (ret
== 0) {
1301 leaf
= path
->nodes
[0];
1302 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1303 struct btrfs_dev_extent
);
1305 btrfs_error(root
->fs_info
, ret
, "Slot search failed");
1309 if (device
->bytes_used
> 0) {
1310 u64 len
= btrfs_dev_extent_length(leaf
, extent
);
1311 device
->bytes_used
-= len
;
1312 spin_lock(&root
->fs_info
->free_chunk_lock
);
1313 root
->fs_info
->free_chunk_space
+= len
;
1314 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1316 ret
= btrfs_del_item(trans
, root
, path
);
1318 btrfs_error(root
->fs_info
, ret
,
1319 "Failed to remove dev extent item");
1322 btrfs_free_path(path
);
1326 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
1327 struct btrfs_device
*device
,
1328 u64 chunk_tree
, u64 chunk_objectid
,
1329 u64 chunk_offset
, u64 start
, u64 num_bytes
)
1332 struct btrfs_path
*path
;
1333 struct btrfs_root
*root
= device
->dev_root
;
1334 struct btrfs_dev_extent
*extent
;
1335 struct extent_buffer
*leaf
;
1336 struct btrfs_key key
;
1338 WARN_ON(!device
->in_fs_metadata
);
1339 WARN_ON(device
->is_tgtdev_for_dev_replace
);
1340 path
= btrfs_alloc_path();
1344 key
.objectid
= device
->devid
;
1346 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1347 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1352 leaf
= path
->nodes
[0];
1353 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1354 struct btrfs_dev_extent
);
1355 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
1356 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
1357 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1359 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1360 btrfs_dev_extent_chunk_tree_uuid(extent
), BTRFS_UUID_SIZE
);
1362 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1363 btrfs_mark_buffer_dirty(leaf
);
1365 btrfs_free_path(path
);
1369 static u64
find_next_chunk(struct btrfs_fs_info
*fs_info
)
1371 struct extent_map_tree
*em_tree
;
1372 struct extent_map
*em
;
1376 em_tree
= &fs_info
->mapping_tree
.map_tree
;
1377 read_lock(&em_tree
->lock
);
1378 n
= rb_last(&em_tree
->map
);
1380 em
= rb_entry(n
, struct extent_map
, rb_node
);
1381 ret
= em
->start
+ em
->len
;
1383 read_unlock(&em_tree
->lock
);
1388 static noinline
int find_next_devid(struct btrfs_fs_info
*fs_info
,
1392 struct btrfs_key key
;
1393 struct btrfs_key found_key
;
1394 struct btrfs_path
*path
;
1396 path
= btrfs_alloc_path();
1400 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1401 key
.type
= BTRFS_DEV_ITEM_KEY
;
1402 key
.offset
= (u64
)-1;
1404 ret
= btrfs_search_slot(NULL
, fs_info
->chunk_root
, &key
, path
, 0, 0);
1408 BUG_ON(ret
== 0); /* Corruption */
1410 ret
= btrfs_previous_item(fs_info
->chunk_root
, path
,
1411 BTRFS_DEV_ITEMS_OBJECTID
,
1412 BTRFS_DEV_ITEM_KEY
);
1416 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1418 *devid_ret
= found_key
.offset
+ 1;
1422 btrfs_free_path(path
);
1427 * the device information is stored in the chunk root
1428 * the btrfs_device struct should be fully filled in
1430 static int btrfs_add_device(struct btrfs_trans_handle
*trans
,
1431 struct btrfs_root
*root
,
1432 struct btrfs_device
*device
)
1435 struct btrfs_path
*path
;
1436 struct btrfs_dev_item
*dev_item
;
1437 struct extent_buffer
*leaf
;
1438 struct btrfs_key key
;
1441 root
= root
->fs_info
->chunk_root
;
1443 path
= btrfs_alloc_path();
1447 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1448 key
.type
= BTRFS_DEV_ITEM_KEY
;
1449 key
.offset
= device
->devid
;
1451 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1456 leaf
= path
->nodes
[0];
1457 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1459 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1460 btrfs_set_device_generation(leaf
, dev_item
, 0);
1461 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1462 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1463 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1464 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1465 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
1466 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1467 btrfs_set_device_group(leaf
, dev_item
, 0);
1468 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1469 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1470 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1472 ptr
= btrfs_device_uuid(dev_item
);
1473 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1474 ptr
= btrfs_device_fsid(dev_item
);
1475 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1476 btrfs_mark_buffer_dirty(leaf
);
1480 btrfs_free_path(path
);
1485 * Function to update ctime/mtime for a given device path.
1486 * Mainly used for ctime/mtime based probe like libblkid.
1488 static void update_dev_time(char *path_name
)
1492 filp
= filp_open(path_name
, O_RDWR
, 0);
1495 file_update_time(filp
);
1496 filp_close(filp
, NULL
);
1500 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1501 struct btrfs_device
*device
)
1504 struct btrfs_path
*path
;
1505 struct btrfs_key key
;
1506 struct btrfs_trans_handle
*trans
;
1508 root
= root
->fs_info
->chunk_root
;
1510 path
= btrfs_alloc_path();
1514 trans
= btrfs_start_transaction(root
, 0);
1515 if (IS_ERR(trans
)) {
1516 btrfs_free_path(path
);
1517 return PTR_ERR(trans
);
1519 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1520 key
.type
= BTRFS_DEV_ITEM_KEY
;
1521 key
.offset
= device
->devid
;
1524 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1533 ret
= btrfs_del_item(trans
, root
, path
);
1537 btrfs_free_path(path
);
1538 unlock_chunks(root
);
1539 btrfs_commit_transaction(trans
, root
);
1543 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1545 struct btrfs_device
*device
;
1546 struct btrfs_device
*next_device
;
1547 struct block_device
*bdev
;
1548 struct buffer_head
*bh
= NULL
;
1549 struct btrfs_super_block
*disk_super
;
1550 struct btrfs_fs_devices
*cur_devices
;
1557 bool clear_super
= false;
1559 mutex_lock(&uuid_mutex
);
1562 seq
= read_seqbegin(&root
->fs_info
->profiles_lock
);
1564 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1565 root
->fs_info
->avail_system_alloc_bits
|
1566 root
->fs_info
->avail_metadata_alloc_bits
;
1567 } while (read_seqretry(&root
->fs_info
->profiles_lock
, seq
));
1569 num_devices
= root
->fs_info
->fs_devices
->num_devices
;
1570 btrfs_dev_replace_lock(&root
->fs_info
->dev_replace
);
1571 if (btrfs_dev_replace_is_ongoing(&root
->fs_info
->dev_replace
)) {
1572 WARN_ON(num_devices
< 1);
1575 btrfs_dev_replace_unlock(&root
->fs_info
->dev_replace
);
1577 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) && num_devices
<= 4) {
1578 ret
= BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET
;
1582 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) && num_devices
<= 2) {
1583 ret
= BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET
;
1587 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID5
) &&
1588 root
->fs_info
->fs_devices
->rw_devices
<= 2) {
1589 ret
= BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET
;
1592 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID6
) &&
1593 root
->fs_info
->fs_devices
->rw_devices
<= 3) {
1594 ret
= BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET
;
1598 if (strcmp(device_path
, "missing") == 0) {
1599 struct list_head
*devices
;
1600 struct btrfs_device
*tmp
;
1603 devices
= &root
->fs_info
->fs_devices
->devices
;
1605 * It is safe to read the devices since the volume_mutex
1608 list_for_each_entry(tmp
, devices
, dev_list
) {
1609 if (tmp
->in_fs_metadata
&&
1610 !tmp
->is_tgtdev_for_dev_replace
&&
1620 ret
= BTRFS_ERROR_DEV_MISSING_NOT_FOUND
;
1624 ret
= btrfs_get_bdev_and_sb(device_path
,
1625 FMODE_WRITE
| FMODE_EXCL
,
1626 root
->fs_info
->bdev_holder
, 0,
1630 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1631 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1632 dev_uuid
= disk_super
->dev_item
.uuid
;
1633 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
1641 if (device
->is_tgtdev_for_dev_replace
) {
1642 ret
= BTRFS_ERROR_DEV_TGT_REPLACE
;
1646 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1647 ret
= BTRFS_ERROR_DEV_ONLY_WRITABLE
;
1651 if (device
->writeable
) {
1653 list_del_init(&device
->dev_alloc_list
);
1654 unlock_chunks(root
);
1655 root
->fs_info
->fs_devices
->rw_devices
--;
1659 mutex_unlock(&uuid_mutex
);
1660 ret
= btrfs_shrink_device(device
, 0);
1661 mutex_lock(&uuid_mutex
);
1666 * TODO: the superblock still includes this device in its num_devices
1667 * counter although write_all_supers() is not locked out. This
1668 * could give a filesystem state which requires a degraded mount.
1670 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1674 device
->in_fs_metadata
= 0;
1675 btrfs_scrub_cancel_dev(root
->fs_info
, device
);
1678 * the device list mutex makes sure that we don't change
1679 * the device list while someone else is writing out all
1680 * the device supers. Whoever is writing all supers, should
1681 * lock the device list mutex before getting the number of
1682 * devices in the super block (super_copy). Conversely,
1683 * whoever updates the number of devices in the super block
1684 * (super_copy) should hold the device list mutex.
1687 cur_devices
= device
->fs_devices
;
1688 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1689 list_del_rcu(&device
->dev_list
);
1691 device
->fs_devices
->num_devices
--;
1692 device
->fs_devices
->total_devices
--;
1694 if (device
->missing
)
1695 device
->fs_devices
->missing_devices
--;
1697 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1698 struct btrfs_device
, dev_list
);
1699 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1700 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1701 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1702 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1705 device
->fs_devices
->open_devices
--;
1706 /* remove sysfs entry */
1707 btrfs_kobj_rm_device(root
->fs_info
, device
);
1710 call_rcu(&device
->rcu
, free_device
);
1712 num_devices
= btrfs_super_num_devices(root
->fs_info
->super_copy
) - 1;
1713 btrfs_set_super_num_devices(root
->fs_info
->super_copy
, num_devices
);
1714 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1716 if (cur_devices
->open_devices
== 0) {
1717 struct btrfs_fs_devices
*fs_devices
;
1718 fs_devices
= root
->fs_info
->fs_devices
;
1719 while (fs_devices
) {
1720 if (fs_devices
->seed
== cur_devices
) {
1721 fs_devices
->seed
= cur_devices
->seed
;
1724 fs_devices
= fs_devices
->seed
;
1726 cur_devices
->seed
= NULL
;
1728 __btrfs_close_devices(cur_devices
);
1729 unlock_chunks(root
);
1730 free_fs_devices(cur_devices
);
1733 root
->fs_info
->num_tolerated_disk_barrier_failures
=
1734 btrfs_calc_num_tolerated_disk_barrier_failures(root
->fs_info
);
1737 * at this point, the device is zero sized. We want to
1738 * remove it from the devices list and zero out the old super
1740 if (clear_super
&& disk_super
) {
1744 /* make sure this device isn't detected as part of
1747 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1748 set_buffer_dirty(bh
);
1749 sync_dirty_buffer(bh
);
1751 /* clear the mirror copies of super block on the disk
1752 * being removed, 0th copy is been taken care above and
1753 * the below would take of the rest
1755 for (i
= 1; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
1756 bytenr
= btrfs_sb_offset(i
);
1757 if (bytenr
+ BTRFS_SUPER_INFO_SIZE
>=
1758 i_size_read(bdev
->bd_inode
))
1762 bh
= __bread(bdev
, bytenr
/ 4096,
1763 BTRFS_SUPER_INFO_SIZE
);
1767 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1769 if (btrfs_super_bytenr(disk_super
) != bytenr
||
1770 btrfs_super_magic(disk_super
) != BTRFS_MAGIC
) {
1773 memset(&disk_super
->magic
, 0,
1774 sizeof(disk_super
->magic
));
1775 set_buffer_dirty(bh
);
1776 sync_dirty_buffer(bh
);
1783 /* Notify udev that device has changed */
1784 btrfs_kobject_uevent(bdev
, KOBJ_CHANGE
);
1786 /* Update ctime/mtime for device path for libblkid */
1787 update_dev_time(device_path
);
1793 blkdev_put(bdev
, FMODE_READ
| FMODE_EXCL
);
1795 mutex_unlock(&uuid_mutex
);
1798 if (device
->writeable
) {
1800 list_add(&device
->dev_alloc_list
,
1801 &root
->fs_info
->fs_devices
->alloc_list
);
1802 unlock_chunks(root
);
1803 root
->fs_info
->fs_devices
->rw_devices
++;
1808 void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info
*fs_info
,
1809 struct btrfs_device
*srcdev
)
1811 struct btrfs_fs_devices
*fs_devices
;
1813 WARN_ON(!mutex_is_locked(&fs_info
->fs_devices
->device_list_mutex
));
1816 * in case of fs with no seed, srcdev->fs_devices will point
1817 * to fs_devices of fs_info. However when the dev being replaced is
1818 * a seed dev it will point to the seed's local fs_devices. In short
1819 * srcdev will have its correct fs_devices in both the cases.
1821 fs_devices
= srcdev
->fs_devices
;
1823 list_del_rcu(&srcdev
->dev_list
);
1824 list_del_rcu(&srcdev
->dev_alloc_list
);
1825 fs_devices
->num_devices
--;
1826 if (srcdev
->missing
) {
1827 fs_devices
->missing_devices
--;
1828 if (!fs_devices
->seeding
)
1829 fs_devices
->rw_devices
++;
1833 fs_devices
->open_devices
--;
1836 * zero out the old super if it is not writable
1837 * (e.g. seed device)
1839 if (srcdev
->writeable
)
1840 btrfs_scratch_superblock(srcdev
);
1843 call_rcu(&srcdev
->rcu
, free_device
);
1846 * unless fs_devices is seed fs, num_devices shouldn't go
1849 BUG_ON(!fs_devices
->num_devices
&& !fs_devices
->seeding
);
1851 /* if this is no devs we rather delete the fs_devices */
1852 if (!fs_devices
->num_devices
) {
1853 struct btrfs_fs_devices
*tmp_fs_devices
;
1855 tmp_fs_devices
= fs_info
->fs_devices
;
1856 while (tmp_fs_devices
) {
1857 if (tmp_fs_devices
->seed
== fs_devices
) {
1858 tmp_fs_devices
->seed
= fs_devices
->seed
;
1861 tmp_fs_devices
= tmp_fs_devices
->seed
;
1863 fs_devices
->seed
= NULL
;
1864 __btrfs_close_devices(fs_devices
);
1865 free_fs_devices(fs_devices
);
1869 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info
*fs_info
,
1870 struct btrfs_device
*tgtdev
)
1872 struct btrfs_device
*next_device
;
1875 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
1877 btrfs_scratch_superblock(tgtdev
);
1878 fs_info
->fs_devices
->open_devices
--;
1880 fs_info
->fs_devices
->num_devices
--;
1882 next_device
= list_entry(fs_info
->fs_devices
->devices
.next
,
1883 struct btrfs_device
, dev_list
);
1884 if (tgtdev
->bdev
== fs_info
->sb
->s_bdev
)
1885 fs_info
->sb
->s_bdev
= next_device
->bdev
;
1886 if (tgtdev
->bdev
== fs_info
->fs_devices
->latest_bdev
)
1887 fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1888 list_del_rcu(&tgtdev
->dev_list
);
1890 call_rcu(&tgtdev
->rcu
, free_device
);
1892 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
1895 static int btrfs_find_device_by_path(struct btrfs_root
*root
, char *device_path
,
1896 struct btrfs_device
**device
)
1899 struct btrfs_super_block
*disk_super
;
1902 struct block_device
*bdev
;
1903 struct buffer_head
*bh
;
1906 ret
= btrfs_get_bdev_and_sb(device_path
, FMODE_READ
,
1907 root
->fs_info
->bdev_holder
, 0, &bdev
, &bh
);
1910 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1911 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1912 dev_uuid
= disk_super
->dev_item
.uuid
;
1913 *device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
1918 blkdev_put(bdev
, FMODE_READ
);
1922 int btrfs_find_device_missing_or_by_path(struct btrfs_root
*root
,
1924 struct btrfs_device
**device
)
1927 if (strcmp(device_path
, "missing") == 0) {
1928 struct list_head
*devices
;
1929 struct btrfs_device
*tmp
;
1931 devices
= &root
->fs_info
->fs_devices
->devices
;
1933 * It is safe to read the devices since the volume_mutex
1934 * is held by the caller.
1936 list_for_each_entry(tmp
, devices
, dev_list
) {
1937 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1944 btrfs_err(root
->fs_info
, "no missing device found");
1950 return btrfs_find_device_by_path(root
, device_path
, device
);
1955 * does all the dirty work required for changing file system's UUID.
1957 static int btrfs_prepare_sprout(struct btrfs_root
*root
)
1959 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1960 struct btrfs_fs_devices
*old_devices
;
1961 struct btrfs_fs_devices
*seed_devices
;
1962 struct btrfs_super_block
*disk_super
= root
->fs_info
->super_copy
;
1963 struct btrfs_device
*device
;
1966 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1967 if (!fs_devices
->seeding
)
1970 seed_devices
= __alloc_fs_devices();
1971 if (IS_ERR(seed_devices
))
1972 return PTR_ERR(seed_devices
);
1974 old_devices
= clone_fs_devices(fs_devices
);
1975 if (IS_ERR(old_devices
)) {
1976 kfree(seed_devices
);
1977 return PTR_ERR(old_devices
);
1980 list_add(&old_devices
->list
, &fs_uuids
);
1982 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1983 seed_devices
->opened
= 1;
1984 INIT_LIST_HEAD(&seed_devices
->devices
);
1985 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1986 mutex_init(&seed_devices
->device_list_mutex
);
1988 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1989 list_splice_init_rcu(&fs_devices
->devices
, &seed_devices
->devices
,
1992 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1993 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1994 device
->fs_devices
= seed_devices
;
1997 fs_devices
->seeding
= 0;
1998 fs_devices
->num_devices
= 0;
1999 fs_devices
->open_devices
= 0;
2000 fs_devices
->missing_devices
= 0;
2001 fs_devices
->rotating
= 0;
2002 fs_devices
->seed
= seed_devices
;
2004 generate_random_uuid(fs_devices
->fsid
);
2005 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
2006 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
2007 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2009 super_flags
= btrfs_super_flags(disk_super
) &
2010 ~BTRFS_SUPER_FLAG_SEEDING
;
2011 btrfs_set_super_flags(disk_super
, super_flags
);
2017 * strore the expected generation for seed devices in device items.
2019 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
2020 struct btrfs_root
*root
)
2022 struct btrfs_path
*path
;
2023 struct extent_buffer
*leaf
;
2024 struct btrfs_dev_item
*dev_item
;
2025 struct btrfs_device
*device
;
2026 struct btrfs_key key
;
2027 u8 fs_uuid
[BTRFS_UUID_SIZE
];
2028 u8 dev_uuid
[BTRFS_UUID_SIZE
];
2032 path
= btrfs_alloc_path();
2036 root
= root
->fs_info
->chunk_root
;
2037 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
2039 key
.type
= BTRFS_DEV_ITEM_KEY
;
2042 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2046 leaf
= path
->nodes
[0];
2048 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
2049 ret
= btrfs_next_leaf(root
, path
);
2054 leaf
= path
->nodes
[0];
2055 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2056 btrfs_release_path(path
);
2060 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2061 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
2062 key
.type
!= BTRFS_DEV_ITEM_KEY
)
2065 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2066 struct btrfs_dev_item
);
2067 devid
= btrfs_device_id(leaf
, dev_item
);
2068 read_extent_buffer(leaf
, dev_uuid
, btrfs_device_uuid(dev_item
),
2070 read_extent_buffer(leaf
, fs_uuid
, btrfs_device_fsid(dev_item
),
2072 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
2074 BUG_ON(!device
); /* Logic error */
2076 if (device
->fs_devices
->seeding
) {
2077 btrfs_set_device_generation(leaf
, dev_item
,
2078 device
->generation
);
2079 btrfs_mark_buffer_dirty(leaf
);
2087 btrfs_free_path(path
);
2091 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
2093 struct request_queue
*q
;
2094 struct btrfs_trans_handle
*trans
;
2095 struct btrfs_device
*device
;
2096 struct block_device
*bdev
;
2097 struct list_head
*devices
;
2098 struct super_block
*sb
= root
->fs_info
->sb
;
2099 struct rcu_string
*name
;
2101 int seeding_dev
= 0;
2104 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
2107 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
2108 root
->fs_info
->bdev_holder
);
2110 return PTR_ERR(bdev
);
2112 if (root
->fs_info
->fs_devices
->seeding
) {
2114 down_write(&sb
->s_umount
);
2115 mutex_lock(&uuid_mutex
);
2118 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
2120 devices
= &root
->fs_info
->fs_devices
->devices
;
2122 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2123 list_for_each_entry(device
, devices
, dev_list
) {
2124 if (device
->bdev
== bdev
) {
2127 &root
->fs_info
->fs_devices
->device_list_mutex
);
2131 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2133 device
= btrfs_alloc_device(root
->fs_info
, NULL
, NULL
);
2134 if (IS_ERR(device
)) {
2135 /* we can safely leave the fs_devices entry around */
2136 ret
= PTR_ERR(device
);
2140 name
= rcu_string_strdup(device_path
, GFP_NOFS
);
2146 rcu_assign_pointer(device
->name
, name
);
2148 trans
= btrfs_start_transaction(root
, 0);
2149 if (IS_ERR(trans
)) {
2150 rcu_string_free(device
->name
);
2152 ret
= PTR_ERR(trans
);
2158 q
= bdev_get_queue(bdev
);
2159 if (blk_queue_discard(q
))
2160 device
->can_discard
= 1;
2161 device
->writeable
= 1;
2162 device
->generation
= trans
->transid
;
2163 device
->io_width
= root
->sectorsize
;
2164 device
->io_align
= root
->sectorsize
;
2165 device
->sector_size
= root
->sectorsize
;
2166 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
2167 device
->disk_total_bytes
= device
->total_bytes
;
2168 device
->commit_total_bytes
= device
->total_bytes
;
2169 device
->dev_root
= root
->fs_info
->dev_root
;
2170 device
->bdev
= bdev
;
2171 device
->in_fs_metadata
= 1;
2172 device
->is_tgtdev_for_dev_replace
= 0;
2173 device
->mode
= FMODE_EXCL
;
2174 device
->dev_stats_valid
= 1;
2175 set_blocksize(device
->bdev
, 4096);
2178 sb
->s_flags
&= ~MS_RDONLY
;
2179 ret
= btrfs_prepare_sprout(root
);
2180 BUG_ON(ret
); /* -ENOMEM */
2183 device
->fs_devices
= root
->fs_info
->fs_devices
;
2185 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2186 list_add_rcu(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
2187 list_add(&device
->dev_alloc_list
,
2188 &root
->fs_info
->fs_devices
->alloc_list
);
2189 root
->fs_info
->fs_devices
->num_devices
++;
2190 root
->fs_info
->fs_devices
->open_devices
++;
2191 root
->fs_info
->fs_devices
->rw_devices
++;
2192 root
->fs_info
->fs_devices
->total_devices
++;
2193 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
2195 spin_lock(&root
->fs_info
->free_chunk_lock
);
2196 root
->fs_info
->free_chunk_space
+= device
->total_bytes
;
2197 spin_unlock(&root
->fs_info
->free_chunk_lock
);
2199 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
2200 root
->fs_info
->fs_devices
->rotating
= 1;
2202 tmp
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
2203 btrfs_set_super_total_bytes(root
->fs_info
->super_copy
,
2204 tmp
+ device
->total_bytes
);
2206 tmp
= btrfs_super_num_devices(root
->fs_info
->super_copy
);
2207 btrfs_set_super_num_devices(root
->fs_info
->super_copy
,
2210 /* add sysfs device entry */
2211 btrfs_kobj_add_device(root
->fs_info
, device
);
2213 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2216 char fsid_buf
[BTRFS_UUID_UNPARSED_SIZE
];
2217 ret
= init_first_rw_device(trans
, root
, device
);
2219 btrfs_abort_transaction(trans
, root
, ret
);
2222 ret
= btrfs_finish_sprout(trans
, root
);
2224 btrfs_abort_transaction(trans
, root
, ret
);
2228 /* Sprouting would change fsid of the mounted root,
2229 * so rename the fsid on the sysfs
2231 snprintf(fsid_buf
, BTRFS_UUID_UNPARSED_SIZE
, "%pU",
2232 root
->fs_info
->fsid
);
2233 if (kobject_rename(&root
->fs_info
->super_kobj
, fsid_buf
))
2236 ret
= btrfs_add_device(trans
, root
, device
);
2238 btrfs_abort_transaction(trans
, root
, ret
);
2244 * we've got more storage, clear any full flags on the space
2247 btrfs_clear_space_info_full(root
->fs_info
);
2249 unlock_chunks(root
);
2250 root
->fs_info
->num_tolerated_disk_barrier_failures
=
2251 btrfs_calc_num_tolerated_disk_barrier_failures(root
->fs_info
);
2252 ret
= btrfs_commit_transaction(trans
, root
);
2255 mutex_unlock(&uuid_mutex
);
2256 up_write(&sb
->s_umount
);
2258 if (ret
) /* transaction commit */
2261 ret
= btrfs_relocate_sys_chunks(root
);
2263 btrfs_error(root
->fs_info
, ret
,
2264 "Failed to relocate sys chunks after "
2265 "device initialization. This can be fixed "
2266 "using the \"btrfs balance\" command.");
2267 trans
= btrfs_attach_transaction(root
);
2268 if (IS_ERR(trans
)) {
2269 if (PTR_ERR(trans
) == -ENOENT
)
2271 return PTR_ERR(trans
);
2273 ret
= btrfs_commit_transaction(trans
, root
);
2276 /* Update ctime/mtime for libblkid */
2277 update_dev_time(device_path
);
2281 unlock_chunks(root
);
2282 btrfs_end_transaction(trans
, root
);
2283 rcu_string_free(device
->name
);
2284 btrfs_kobj_rm_device(root
->fs_info
, device
);
2287 blkdev_put(bdev
, FMODE_EXCL
);
2289 mutex_unlock(&uuid_mutex
);
2290 up_write(&sb
->s_umount
);
2295 int btrfs_init_dev_replace_tgtdev(struct btrfs_root
*root
, char *device_path
,
2296 struct btrfs_device
*srcdev
,
2297 struct btrfs_device
**device_out
)
2299 struct request_queue
*q
;
2300 struct btrfs_device
*device
;
2301 struct block_device
*bdev
;
2302 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2303 struct list_head
*devices
;
2304 struct rcu_string
*name
;
2305 u64 devid
= BTRFS_DEV_REPLACE_DEVID
;
2309 if (fs_info
->fs_devices
->seeding
) {
2310 btrfs_err(fs_info
, "the filesystem is a seed filesystem!");
2314 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
2315 fs_info
->bdev_holder
);
2317 btrfs_err(fs_info
, "target device %s is invalid!", device_path
);
2318 return PTR_ERR(bdev
);
2321 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
2323 devices
= &fs_info
->fs_devices
->devices
;
2324 list_for_each_entry(device
, devices
, dev_list
) {
2325 if (device
->bdev
== bdev
) {
2326 btrfs_err(fs_info
, "target device is in the filesystem!");
2333 if (i_size_read(bdev
->bd_inode
) < srcdev
->total_bytes
) {
2334 btrfs_err(fs_info
, "target device is smaller than source device!");
2340 device
= btrfs_alloc_device(NULL
, &devid
, NULL
);
2341 if (IS_ERR(device
)) {
2342 ret
= PTR_ERR(device
);
2346 name
= rcu_string_strdup(device_path
, GFP_NOFS
);
2352 rcu_assign_pointer(device
->name
, name
);
2354 q
= bdev_get_queue(bdev
);
2355 if (blk_queue_discard(q
))
2356 device
->can_discard
= 1;
2357 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2358 device
->writeable
= 1;
2359 device
->generation
= 0;
2360 device
->io_width
= root
->sectorsize
;
2361 device
->io_align
= root
->sectorsize
;
2362 device
->sector_size
= root
->sectorsize
;
2363 device
->total_bytes
= srcdev
->total_bytes
;
2364 device
->disk_total_bytes
= srcdev
->disk_total_bytes
;
2365 ASSERT(list_empty(&srcdev
->resized_list
));
2366 device
->commit_total_bytes
= srcdev
->commit_total_bytes
;
2367 device
->bytes_used
= srcdev
->bytes_used
;
2368 device
->commit_bytes_used
= device
->bytes_used
;
2369 device
->dev_root
= fs_info
->dev_root
;
2370 device
->bdev
= bdev
;
2371 device
->in_fs_metadata
= 1;
2372 device
->is_tgtdev_for_dev_replace
= 1;
2373 device
->mode
= FMODE_EXCL
;
2374 device
->dev_stats_valid
= 1;
2375 set_blocksize(device
->bdev
, 4096);
2376 device
->fs_devices
= fs_info
->fs_devices
;
2377 list_add(&device
->dev_list
, &fs_info
->fs_devices
->devices
);
2378 fs_info
->fs_devices
->num_devices
++;
2379 fs_info
->fs_devices
->open_devices
++;
2380 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2382 *device_out
= device
;
2386 blkdev_put(bdev
, FMODE_EXCL
);
2390 void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info
*fs_info
,
2391 struct btrfs_device
*tgtdev
)
2393 WARN_ON(fs_info
->fs_devices
->rw_devices
== 0);
2394 tgtdev
->io_width
= fs_info
->dev_root
->sectorsize
;
2395 tgtdev
->io_align
= fs_info
->dev_root
->sectorsize
;
2396 tgtdev
->sector_size
= fs_info
->dev_root
->sectorsize
;
2397 tgtdev
->dev_root
= fs_info
->dev_root
;
2398 tgtdev
->in_fs_metadata
= 1;
2401 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
2402 struct btrfs_device
*device
)
2405 struct btrfs_path
*path
;
2406 struct btrfs_root
*root
;
2407 struct btrfs_dev_item
*dev_item
;
2408 struct extent_buffer
*leaf
;
2409 struct btrfs_key key
;
2411 root
= device
->dev_root
->fs_info
->chunk_root
;
2413 path
= btrfs_alloc_path();
2417 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
2418 key
.type
= BTRFS_DEV_ITEM_KEY
;
2419 key
.offset
= device
->devid
;
2421 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2430 leaf
= path
->nodes
[0];
2431 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
2433 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
2434 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
2435 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
2436 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
2437 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
2438 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
2439 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
2440 btrfs_mark_buffer_dirty(leaf
);
2443 btrfs_free_path(path
);
2447 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
2448 struct btrfs_device
*device
, u64 new_size
)
2450 struct btrfs_super_block
*super_copy
=
2451 device
->dev_root
->fs_info
->super_copy
;
2452 struct btrfs_fs_devices
*fs_devices
;
2453 u64 old_total
= btrfs_super_total_bytes(super_copy
);
2454 u64 diff
= new_size
- device
->total_bytes
;
2456 if (!device
->writeable
)
2458 if (new_size
<= device
->total_bytes
||
2459 device
->is_tgtdev_for_dev_replace
)
2462 fs_devices
= device
->dev_root
->fs_info
->fs_devices
;
2464 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
2465 device
->fs_devices
->total_rw_bytes
+= diff
;
2467 device
->total_bytes
= new_size
;
2468 device
->disk_total_bytes
= new_size
;
2469 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
2470 if (list_empty(&device
->resized_list
))
2471 list_add_tail(&device
->resized_list
,
2472 &fs_devices
->resized_devices
);
2474 return btrfs_update_device(trans
, device
);
2477 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
2478 struct btrfs_device
*device
, u64 new_size
)
2481 lock_chunks(device
->dev_root
);
2482 ret
= __btrfs_grow_device(trans
, device
, new_size
);
2483 unlock_chunks(device
->dev_root
);
2487 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
2488 struct btrfs_root
*root
,
2489 u64 chunk_tree
, u64 chunk_objectid
,
2493 struct btrfs_path
*path
;
2494 struct btrfs_key key
;
2496 root
= root
->fs_info
->chunk_root
;
2497 path
= btrfs_alloc_path();
2501 key
.objectid
= chunk_objectid
;
2502 key
.offset
= chunk_offset
;
2503 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2505 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2508 else if (ret
> 0) { /* Logic error or corruption */
2509 btrfs_error(root
->fs_info
, -ENOENT
,
2510 "Failed lookup while freeing chunk.");
2515 ret
= btrfs_del_item(trans
, root
, path
);
2517 btrfs_error(root
->fs_info
, ret
,
2518 "Failed to delete chunk item.");
2520 btrfs_free_path(path
);
2524 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
2527 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
2528 struct btrfs_disk_key
*disk_key
;
2529 struct btrfs_chunk
*chunk
;
2536 struct btrfs_key key
;
2538 array_size
= btrfs_super_sys_array_size(super_copy
);
2540 ptr
= super_copy
->sys_chunk_array
;
2543 while (cur
< array_size
) {
2544 disk_key
= (struct btrfs_disk_key
*)ptr
;
2545 btrfs_disk_key_to_cpu(&key
, disk_key
);
2547 len
= sizeof(*disk_key
);
2549 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
2550 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
2551 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
2552 len
+= btrfs_chunk_item_size(num_stripes
);
2557 if (key
.objectid
== chunk_objectid
&&
2558 key
.offset
== chunk_offset
) {
2559 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
2561 btrfs_set_super_sys_array_size(super_copy
, array_size
);
2570 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
2571 u64 chunk_tree
, u64 chunk_objectid
,
2574 struct extent_map_tree
*em_tree
;
2575 struct btrfs_root
*extent_root
;
2576 struct btrfs_trans_handle
*trans
;
2577 struct extent_map
*em
;
2578 struct map_lookup
*map
;
2582 root
= root
->fs_info
->chunk_root
;
2583 extent_root
= root
->fs_info
->extent_root
;
2584 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
2586 ret
= btrfs_can_relocate(extent_root
, chunk_offset
);
2590 /* step one, relocate all the extents inside this chunk */
2591 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
2595 trans
= btrfs_start_transaction(root
, 0);
2596 if (IS_ERR(trans
)) {
2597 ret
= PTR_ERR(trans
);
2598 btrfs_std_error(root
->fs_info
, ret
);
2605 * step two, delete the device extents and the
2606 * chunk tree entries
2608 read_lock(&em_tree
->lock
);
2609 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
2610 read_unlock(&em_tree
->lock
);
2612 BUG_ON(!em
|| em
->start
> chunk_offset
||
2613 em
->start
+ em
->len
< chunk_offset
);
2614 map
= (struct map_lookup
*)em
->bdev
;
2616 for (i
= 0; i
< map
->num_stripes
; i
++) {
2617 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
2618 map
->stripes
[i
].physical
);
2621 if (map
->stripes
[i
].dev
) {
2622 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
2626 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
2631 trace_btrfs_chunk_free(root
, map
, chunk_offset
, em
->len
);
2633 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2634 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
2638 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
2641 write_lock(&em_tree
->lock
);
2642 remove_extent_mapping(em_tree
, em
);
2643 write_unlock(&em_tree
->lock
);
2645 /* once for the tree */
2646 free_extent_map(em
);
2648 free_extent_map(em
);
2650 unlock_chunks(root
);
2651 btrfs_end_transaction(trans
, root
);
2655 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
2657 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
2658 struct btrfs_path
*path
;
2659 struct extent_buffer
*leaf
;
2660 struct btrfs_chunk
*chunk
;
2661 struct btrfs_key key
;
2662 struct btrfs_key found_key
;
2663 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
2665 bool retried
= false;
2669 path
= btrfs_alloc_path();
2674 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2675 key
.offset
= (u64
)-1;
2676 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2679 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2682 BUG_ON(ret
== 0); /* Corruption */
2684 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
2691 leaf
= path
->nodes
[0];
2692 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2694 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
2695 struct btrfs_chunk
);
2696 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2697 btrfs_release_path(path
);
2699 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2700 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
2709 if (found_key
.offset
== 0)
2711 key
.offset
= found_key
.offset
- 1;
2714 if (failed
&& !retried
) {
2718 } else if (WARN_ON(failed
&& retried
)) {
2722 btrfs_free_path(path
);
2726 static int insert_balance_item(struct btrfs_root
*root
,
2727 struct btrfs_balance_control
*bctl
)
2729 struct btrfs_trans_handle
*trans
;
2730 struct btrfs_balance_item
*item
;
2731 struct btrfs_disk_balance_args disk_bargs
;
2732 struct btrfs_path
*path
;
2733 struct extent_buffer
*leaf
;
2734 struct btrfs_key key
;
2737 path
= btrfs_alloc_path();
2741 trans
= btrfs_start_transaction(root
, 0);
2742 if (IS_ERR(trans
)) {
2743 btrfs_free_path(path
);
2744 return PTR_ERR(trans
);
2747 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2748 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2751 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2756 leaf
= path
->nodes
[0];
2757 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
2759 memset_extent_buffer(leaf
, 0, (unsigned long)item
, sizeof(*item
));
2761 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->data
);
2762 btrfs_set_balance_data(leaf
, item
, &disk_bargs
);
2763 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->meta
);
2764 btrfs_set_balance_meta(leaf
, item
, &disk_bargs
);
2765 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->sys
);
2766 btrfs_set_balance_sys(leaf
, item
, &disk_bargs
);
2768 btrfs_set_balance_flags(leaf
, item
, bctl
->flags
);
2770 btrfs_mark_buffer_dirty(leaf
);
2772 btrfs_free_path(path
);
2773 err
= btrfs_commit_transaction(trans
, root
);
2779 static int del_balance_item(struct btrfs_root
*root
)
2781 struct btrfs_trans_handle
*trans
;
2782 struct btrfs_path
*path
;
2783 struct btrfs_key key
;
2786 path
= btrfs_alloc_path();
2790 trans
= btrfs_start_transaction(root
, 0);
2791 if (IS_ERR(trans
)) {
2792 btrfs_free_path(path
);
2793 return PTR_ERR(trans
);
2796 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2797 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2800 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2808 ret
= btrfs_del_item(trans
, root
, path
);
2810 btrfs_free_path(path
);
2811 err
= btrfs_commit_transaction(trans
, root
);
2818 * This is a heuristic used to reduce the number of chunks balanced on
2819 * resume after balance was interrupted.
2821 static void update_balance_args(struct btrfs_balance_control
*bctl
)
2824 * Turn on soft mode for chunk types that were being converted.
2826 if (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2827 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2828 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2829 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2830 if (bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2831 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2834 * Turn on usage filter if is not already used. The idea is
2835 * that chunks that we have already balanced should be
2836 * reasonably full. Don't do it for chunks that are being
2837 * converted - that will keep us from relocating unconverted
2838 * (albeit full) chunks.
2840 if (!(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2841 !(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2842 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2843 bctl
->data
.usage
= 90;
2845 if (!(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2846 !(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2847 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2848 bctl
->sys
.usage
= 90;
2850 if (!(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2851 !(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2852 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2853 bctl
->meta
.usage
= 90;
2858 * Should be called with both balance and volume mutexes held to
2859 * serialize other volume operations (add_dev/rm_dev/resize) with
2860 * restriper. Same goes for unset_balance_control.
2862 static void set_balance_control(struct btrfs_balance_control
*bctl
)
2864 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
2866 BUG_ON(fs_info
->balance_ctl
);
2868 spin_lock(&fs_info
->balance_lock
);
2869 fs_info
->balance_ctl
= bctl
;
2870 spin_unlock(&fs_info
->balance_lock
);
2873 static void unset_balance_control(struct btrfs_fs_info
*fs_info
)
2875 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
2877 BUG_ON(!fs_info
->balance_ctl
);
2879 spin_lock(&fs_info
->balance_lock
);
2880 fs_info
->balance_ctl
= NULL
;
2881 spin_unlock(&fs_info
->balance_lock
);
2887 * Balance filters. Return 1 if chunk should be filtered out
2888 * (should not be balanced).
2890 static int chunk_profiles_filter(u64 chunk_type
,
2891 struct btrfs_balance_args
*bargs
)
2893 chunk_type
= chunk_to_extended(chunk_type
) &
2894 BTRFS_EXTENDED_PROFILE_MASK
;
2896 if (bargs
->profiles
& chunk_type
)
2902 static int chunk_usage_filter(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
,
2903 struct btrfs_balance_args
*bargs
)
2905 struct btrfs_block_group_cache
*cache
;
2906 u64 chunk_used
, user_thresh
;
2909 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
2910 chunk_used
= btrfs_block_group_used(&cache
->item
);
2912 if (bargs
->usage
== 0)
2914 else if (bargs
->usage
> 100)
2915 user_thresh
= cache
->key
.offset
;
2917 user_thresh
= div_factor_fine(cache
->key
.offset
,
2920 if (chunk_used
< user_thresh
)
2923 btrfs_put_block_group(cache
);
2927 static int chunk_devid_filter(struct extent_buffer
*leaf
,
2928 struct btrfs_chunk
*chunk
,
2929 struct btrfs_balance_args
*bargs
)
2931 struct btrfs_stripe
*stripe
;
2932 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2935 for (i
= 0; i
< num_stripes
; i
++) {
2936 stripe
= btrfs_stripe_nr(chunk
, i
);
2937 if (btrfs_stripe_devid(leaf
, stripe
) == bargs
->devid
)
2944 /* [pstart, pend) */
2945 static int chunk_drange_filter(struct extent_buffer
*leaf
,
2946 struct btrfs_chunk
*chunk
,
2948 struct btrfs_balance_args
*bargs
)
2950 struct btrfs_stripe
*stripe
;
2951 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2957 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
))
2960 if (btrfs_chunk_type(leaf
, chunk
) & (BTRFS_BLOCK_GROUP_DUP
|
2961 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
)) {
2962 factor
= num_stripes
/ 2;
2963 } else if (btrfs_chunk_type(leaf
, chunk
) & BTRFS_BLOCK_GROUP_RAID5
) {
2964 factor
= num_stripes
- 1;
2965 } else if (btrfs_chunk_type(leaf
, chunk
) & BTRFS_BLOCK_GROUP_RAID6
) {
2966 factor
= num_stripes
- 2;
2968 factor
= num_stripes
;
2971 for (i
= 0; i
< num_stripes
; i
++) {
2972 stripe
= btrfs_stripe_nr(chunk
, i
);
2973 if (btrfs_stripe_devid(leaf
, stripe
) != bargs
->devid
)
2976 stripe_offset
= btrfs_stripe_offset(leaf
, stripe
);
2977 stripe_length
= btrfs_chunk_length(leaf
, chunk
);
2978 do_div(stripe_length
, factor
);
2980 if (stripe_offset
< bargs
->pend
&&
2981 stripe_offset
+ stripe_length
> bargs
->pstart
)
2988 /* [vstart, vend) */
2989 static int chunk_vrange_filter(struct extent_buffer
*leaf
,
2990 struct btrfs_chunk
*chunk
,
2992 struct btrfs_balance_args
*bargs
)
2994 if (chunk_offset
< bargs
->vend
&&
2995 chunk_offset
+ btrfs_chunk_length(leaf
, chunk
) > bargs
->vstart
)
2996 /* at least part of the chunk is inside this vrange */
3002 static int chunk_soft_convert_filter(u64 chunk_type
,
3003 struct btrfs_balance_args
*bargs
)
3005 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_CONVERT
))
3008 chunk_type
= chunk_to_extended(chunk_type
) &
3009 BTRFS_EXTENDED_PROFILE_MASK
;
3011 if (bargs
->target
== chunk_type
)
3017 static int should_balance_chunk(struct btrfs_root
*root
,
3018 struct extent_buffer
*leaf
,
3019 struct btrfs_chunk
*chunk
, u64 chunk_offset
)
3021 struct btrfs_balance_control
*bctl
= root
->fs_info
->balance_ctl
;
3022 struct btrfs_balance_args
*bargs
= NULL
;
3023 u64 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
3026 if (!((chunk_type
& BTRFS_BLOCK_GROUP_TYPE_MASK
) &
3027 (bctl
->flags
& BTRFS_BALANCE_TYPE_MASK
))) {
3031 if (chunk_type
& BTRFS_BLOCK_GROUP_DATA
)
3032 bargs
= &bctl
->data
;
3033 else if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
)
3035 else if (chunk_type
& BTRFS_BLOCK_GROUP_METADATA
)
3036 bargs
= &bctl
->meta
;
3038 /* profiles filter */
3039 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_PROFILES
) &&
3040 chunk_profiles_filter(chunk_type
, bargs
)) {
3045 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
3046 chunk_usage_filter(bctl
->fs_info
, chunk_offset
, bargs
)) {
3051 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
) &&
3052 chunk_devid_filter(leaf
, chunk
, bargs
)) {
3056 /* drange filter, makes sense only with devid filter */
3057 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DRANGE
) &&
3058 chunk_drange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
3063 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_VRANGE
) &&
3064 chunk_vrange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
3068 /* soft profile changing mode */
3069 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_SOFT
) &&
3070 chunk_soft_convert_filter(chunk_type
, bargs
)) {
3075 * limited by count, must be the last filter
3077 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_LIMIT
)) {
3078 if (bargs
->limit
== 0)
3087 static int __btrfs_balance(struct btrfs_fs_info
*fs_info
)
3089 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3090 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
3091 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
3092 struct list_head
*devices
;
3093 struct btrfs_device
*device
;
3096 struct btrfs_chunk
*chunk
;
3097 struct btrfs_path
*path
;
3098 struct btrfs_key key
;
3099 struct btrfs_key found_key
;
3100 struct btrfs_trans_handle
*trans
;
3101 struct extent_buffer
*leaf
;
3104 int enospc_errors
= 0;
3105 bool counting
= true;
3106 u64 limit_data
= bctl
->data
.limit
;
3107 u64 limit_meta
= bctl
->meta
.limit
;
3108 u64 limit_sys
= bctl
->sys
.limit
;
3110 /* step one make some room on all the devices */
3111 devices
= &fs_info
->fs_devices
->devices
;
3112 list_for_each_entry(device
, devices
, dev_list
) {
3113 old_size
= device
->total_bytes
;
3114 size_to_free
= div_factor(old_size
, 1);
3115 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
3116 if (!device
->writeable
||
3117 device
->total_bytes
- device
->bytes_used
> size_to_free
||
3118 device
->is_tgtdev_for_dev_replace
)
3121 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
3126 trans
= btrfs_start_transaction(dev_root
, 0);
3127 BUG_ON(IS_ERR(trans
));
3129 ret
= btrfs_grow_device(trans
, device
, old_size
);
3132 btrfs_end_transaction(trans
, dev_root
);
3135 /* step two, relocate all the chunks */
3136 path
= btrfs_alloc_path();
3142 /* zero out stat counters */
3143 spin_lock(&fs_info
->balance_lock
);
3144 memset(&bctl
->stat
, 0, sizeof(bctl
->stat
));
3145 spin_unlock(&fs_info
->balance_lock
);
3148 bctl
->data
.limit
= limit_data
;
3149 bctl
->meta
.limit
= limit_meta
;
3150 bctl
->sys
.limit
= limit_sys
;
3152 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
3153 key
.offset
= (u64
)-1;
3154 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
3157 if ((!counting
&& atomic_read(&fs_info
->balance_pause_req
)) ||
3158 atomic_read(&fs_info
->balance_cancel_req
)) {
3163 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
3168 * this shouldn't happen, it means the last relocate
3172 BUG(); /* FIXME break ? */
3174 ret
= btrfs_previous_item(chunk_root
, path
, 0,
3175 BTRFS_CHUNK_ITEM_KEY
);
3181 leaf
= path
->nodes
[0];
3182 slot
= path
->slots
[0];
3183 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3185 if (found_key
.objectid
!= key
.objectid
)
3188 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
3191 spin_lock(&fs_info
->balance_lock
);
3192 bctl
->stat
.considered
++;
3193 spin_unlock(&fs_info
->balance_lock
);
3196 ret
= should_balance_chunk(chunk_root
, leaf
, chunk
,
3198 btrfs_release_path(path
);
3203 spin_lock(&fs_info
->balance_lock
);
3204 bctl
->stat
.expected
++;
3205 spin_unlock(&fs_info
->balance_lock
);
3209 ret
= btrfs_relocate_chunk(chunk_root
,
3210 chunk_root
->root_key
.objectid
,
3213 if (ret
&& ret
!= -ENOSPC
)
3215 if (ret
== -ENOSPC
) {
3218 spin_lock(&fs_info
->balance_lock
);
3219 bctl
->stat
.completed
++;
3220 spin_unlock(&fs_info
->balance_lock
);
3223 if (found_key
.offset
== 0)
3225 key
.offset
= found_key
.offset
- 1;
3229 btrfs_release_path(path
);
3234 btrfs_free_path(path
);
3235 if (enospc_errors
) {
3236 btrfs_info(fs_info
, "%d enospc errors during balance",
3246 * alloc_profile_is_valid - see if a given profile is valid and reduced
3247 * @flags: profile to validate
3248 * @extended: if true @flags is treated as an extended profile
3250 static int alloc_profile_is_valid(u64 flags
, int extended
)
3252 u64 mask
= (extended
? BTRFS_EXTENDED_PROFILE_MASK
:
3253 BTRFS_BLOCK_GROUP_PROFILE_MASK
);
3255 flags
&= ~BTRFS_BLOCK_GROUP_TYPE_MASK
;
3257 /* 1) check that all other bits are zeroed */
3261 /* 2) see if profile is reduced */
3263 return !extended
; /* "0" is valid for usual profiles */
3265 /* true if exactly one bit set */
3266 return (flags
& (flags
- 1)) == 0;
3269 static inline int balance_need_close(struct btrfs_fs_info
*fs_info
)
3271 /* cancel requested || normal exit path */
3272 return atomic_read(&fs_info
->balance_cancel_req
) ||
3273 (atomic_read(&fs_info
->balance_pause_req
) == 0 &&
3274 atomic_read(&fs_info
->balance_cancel_req
) == 0);
3277 static void __cancel_balance(struct btrfs_fs_info
*fs_info
)
3281 unset_balance_control(fs_info
);
3282 ret
= del_balance_item(fs_info
->tree_root
);
3284 btrfs_std_error(fs_info
, ret
);
3286 atomic_set(&fs_info
->mutually_exclusive_operation_running
, 0);
3290 * Should be called with both balance and volume mutexes held
3292 int btrfs_balance(struct btrfs_balance_control
*bctl
,
3293 struct btrfs_ioctl_balance_args
*bargs
)
3295 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
3302 if (btrfs_fs_closing(fs_info
) ||
3303 atomic_read(&fs_info
->balance_pause_req
) ||
3304 atomic_read(&fs_info
->balance_cancel_req
)) {
3309 allowed
= btrfs_super_incompat_flags(fs_info
->super_copy
);
3310 if (allowed
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
3314 * In case of mixed groups both data and meta should be picked,
3315 * and identical options should be given for both of them.
3317 allowed
= BTRFS_BALANCE_DATA
| BTRFS_BALANCE_METADATA
;
3318 if (mixed
&& (bctl
->flags
& allowed
)) {
3319 if (!(bctl
->flags
& BTRFS_BALANCE_DATA
) ||
3320 !(bctl
->flags
& BTRFS_BALANCE_METADATA
) ||
3321 memcmp(&bctl
->data
, &bctl
->meta
, sizeof(bctl
->data
))) {
3322 btrfs_err(fs_info
, "with mixed groups data and "
3323 "metadata balance options must be the same");
3329 num_devices
= fs_info
->fs_devices
->num_devices
;
3330 btrfs_dev_replace_lock(&fs_info
->dev_replace
);
3331 if (btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
)) {
3332 BUG_ON(num_devices
< 1);
3335 btrfs_dev_replace_unlock(&fs_info
->dev_replace
);
3336 allowed
= BTRFS_AVAIL_ALLOC_BIT_SINGLE
;
3337 if (num_devices
== 1)
3338 allowed
|= BTRFS_BLOCK_GROUP_DUP
;
3339 else if (num_devices
> 1)
3340 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
);
3341 if (num_devices
> 2)
3342 allowed
|= BTRFS_BLOCK_GROUP_RAID5
;
3343 if (num_devices
> 3)
3344 allowed
|= (BTRFS_BLOCK_GROUP_RAID10
|
3345 BTRFS_BLOCK_GROUP_RAID6
);
3346 if ((bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3347 (!alloc_profile_is_valid(bctl
->data
.target
, 1) ||
3348 (bctl
->data
.target
& ~allowed
))) {
3349 btrfs_err(fs_info
, "unable to start balance with target "
3350 "data profile %llu",
3355 if ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3356 (!alloc_profile_is_valid(bctl
->meta
.target
, 1) ||
3357 (bctl
->meta
.target
& ~allowed
))) {
3359 "unable to start balance with target metadata profile %llu",
3364 if ((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3365 (!alloc_profile_is_valid(bctl
->sys
.target
, 1) ||
3366 (bctl
->sys
.target
& ~allowed
))) {
3368 "unable to start balance with target system profile %llu",
3374 /* allow dup'ed data chunks only in mixed mode */
3375 if (!mixed
&& (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3376 (bctl
->data
.target
& BTRFS_BLOCK_GROUP_DUP
)) {
3377 btrfs_err(fs_info
, "dup for data is not allowed");
3382 /* allow to reduce meta or sys integrity only if force set */
3383 allowed
= BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
3384 BTRFS_BLOCK_GROUP_RAID10
|
3385 BTRFS_BLOCK_GROUP_RAID5
|
3386 BTRFS_BLOCK_GROUP_RAID6
;
3388 seq
= read_seqbegin(&fs_info
->profiles_lock
);
3390 if (((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3391 (fs_info
->avail_system_alloc_bits
& allowed
) &&
3392 !(bctl
->sys
.target
& allowed
)) ||
3393 ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3394 (fs_info
->avail_metadata_alloc_bits
& allowed
) &&
3395 !(bctl
->meta
.target
& allowed
))) {
3396 if (bctl
->flags
& BTRFS_BALANCE_FORCE
) {
3397 btrfs_info(fs_info
, "force reducing metadata integrity");
3399 btrfs_err(fs_info
, "balance will reduce metadata "
3400 "integrity, use force if you want this");
3405 } while (read_seqretry(&fs_info
->profiles_lock
, seq
));
3407 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3408 int num_tolerated_disk_barrier_failures
;
3409 u64 target
= bctl
->sys
.target
;
3411 num_tolerated_disk_barrier_failures
=
3412 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info
);
3413 if (num_tolerated_disk_barrier_failures
> 0 &&
3415 (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID0
|
3416 BTRFS_AVAIL_ALLOC_BIT_SINGLE
)))
3417 num_tolerated_disk_barrier_failures
= 0;
3418 else if (num_tolerated_disk_barrier_failures
> 1 &&
3420 (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
)))
3421 num_tolerated_disk_barrier_failures
= 1;
3423 fs_info
->num_tolerated_disk_barrier_failures
=
3424 num_tolerated_disk_barrier_failures
;
3427 ret
= insert_balance_item(fs_info
->tree_root
, bctl
);
3428 if (ret
&& ret
!= -EEXIST
)
3431 if (!(bctl
->flags
& BTRFS_BALANCE_RESUME
)) {
3432 BUG_ON(ret
== -EEXIST
);
3433 set_balance_control(bctl
);
3435 BUG_ON(ret
!= -EEXIST
);
3436 spin_lock(&fs_info
->balance_lock
);
3437 update_balance_args(bctl
);
3438 spin_unlock(&fs_info
->balance_lock
);
3441 atomic_inc(&fs_info
->balance_running
);
3442 mutex_unlock(&fs_info
->balance_mutex
);
3444 ret
= __btrfs_balance(fs_info
);
3446 mutex_lock(&fs_info
->balance_mutex
);
3447 atomic_dec(&fs_info
->balance_running
);
3449 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3450 fs_info
->num_tolerated_disk_barrier_failures
=
3451 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info
);
3455 memset(bargs
, 0, sizeof(*bargs
));
3456 update_ioctl_balance_args(fs_info
, 0, bargs
);
3459 if ((ret
&& ret
!= -ECANCELED
&& ret
!= -ENOSPC
) ||
3460 balance_need_close(fs_info
)) {
3461 __cancel_balance(fs_info
);
3464 wake_up(&fs_info
->balance_wait_q
);
3468 if (bctl
->flags
& BTRFS_BALANCE_RESUME
)
3469 __cancel_balance(fs_info
);
3472 atomic_set(&fs_info
->mutually_exclusive_operation_running
, 0);
3477 static int balance_kthread(void *data
)
3479 struct btrfs_fs_info
*fs_info
= data
;
3482 mutex_lock(&fs_info
->volume_mutex
);
3483 mutex_lock(&fs_info
->balance_mutex
);
3485 if (fs_info
->balance_ctl
) {
3486 btrfs_info(fs_info
, "continuing balance");
3487 ret
= btrfs_balance(fs_info
->balance_ctl
, NULL
);
3490 mutex_unlock(&fs_info
->balance_mutex
);
3491 mutex_unlock(&fs_info
->volume_mutex
);
3496 int btrfs_resume_balance_async(struct btrfs_fs_info
*fs_info
)
3498 struct task_struct
*tsk
;
3500 spin_lock(&fs_info
->balance_lock
);
3501 if (!fs_info
->balance_ctl
) {
3502 spin_unlock(&fs_info
->balance_lock
);
3505 spin_unlock(&fs_info
->balance_lock
);
3507 if (btrfs_test_opt(fs_info
->tree_root
, SKIP_BALANCE
)) {
3508 btrfs_info(fs_info
, "force skipping balance");
3512 tsk
= kthread_run(balance_kthread
, fs_info
, "btrfs-balance");
3513 return PTR_ERR_OR_ZERO(tsk
);
3516 int btrfs_recover_balance(struct btrfs_fs_info
*fs_info
)
3518 struct btrfs_balance_control
*bctl
;
3519 struct btrfs_balance_item
*item
;
3520 struct btrfs_disk_balance_args disk_bargs
;
3521 struct btrfs_path
*path
;
3522 struct extent_buffer
*leaf
;
3523 struct btrfs_key key
;
3526 path
= btrfs_alloc_path();
3530 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
3531 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
3534 ret
= btrfs_search_slot(NULL
, fs_info
->tree_root
, &key
, path
, 0, 0);
3537 if (ret
> 0) { /* ret = -ENOENT; */
3542 bctl
= kzalloc(sizeof(*bctl
), GFP_NOFS
);
3548 leaf
= path
->nodes
[0];
3549 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
3551 bctl
->fs_info
= fs_info
;
3552 bctl
->flags
= btrfs_balance_flags(leaf
, item
);
3553 bctl
->flags
|= BTRFS_BALANCE_RESUME
;
3555 btrfs_balance_data(leaf
, item
, &disk_bargs
);
3556 btrfs_disk_balance_args_to_cpu(&bctl
->data
, &disk_bargs
);
3557 btrfs_balance_meta(leaf
, item
, &disk_bargs
);
3558 btrfs_disk_balance_args_to_cpu(&bctl
->meta
, &disk_bargs
);
3559 btrfs_balance_sys(leaf
, item
, &disk_bargs
);
3560 btrfs_disk_balance_args_to_cpu(&bctl
->sys
, &disk_bargs
);
3562 WARN_ON(atomic_xchg(&fs_info
->mutually_exclusive_operation_running
, 1));
3564 mutex_lock(&fs_info
->volume_mutex
);
3565 mutex_lock(&fs_info
->balance_mutex
);
3567 set_balance_control(bctl
);
3569 mutex_unlock(&fs_info
->balance_mutex
);
3570 mutex_unlock(&fs_info
->volume_mutex
);
3572 btrfs_free_path(path
);
3576 int btrfs_pause_balance(struct btrfs_fs_info
*fs_info
)
3580 mutex_lock(&fs_info
->balance_mutex
);
3581 if (!fs_info
->balance_ctl
) {
3582 mutex_unlock(&fs_info
->balance_mutex
);
3586 if (atomic_read(&fs_info
->balance_running
)) {
3587 atomic_inc(&fs_info
->balance_pause_req
);
3588 mutex_unlock(&fs_info
->balance_mutex
);
3590 wait_event(fs_info
->balance_wait_q
,
3591 atomic_read(&fs_info
->balance_running
) == 0);
3593 mutex_lock(&fs_info
->balance_mutex
);
3594 /* we are good with balance_ctl ripped off from under us */
3595 BUG_ON(atomic_read(&fs_info
->balance_running
));
3596 atomic_dec(&fs_info
->balance_pause_req
);
3601 mutex_unlock(&fs_info
->balance_mutex
);
3605 int btrfs_cancel_balance(struct btrfs_fs_info
*fs_info
)
3607 if (fs_info
->sb
->s_flags
& MS_RDONLY
)
3610 mutex_lock(&fs_info
->balance_mutex
);
3611 if (!fs_info
->balance_ctl
) {
3612 mutex_unlock(&fs_info
->balance_mutex
);
3616 atomic_inc(&fs_info
->balance_cancel_req
);
3618 * if we are running just wait and return, balance item is
3619 * deleted in btrfs_balance in this case
3621 if (atomic_read(&fs_info
->balance_running
)) {
3622 mutex_unlock(&fs_info
->balance_mutex
);
3623 wait_event(fs_info
->balance_wait_q
,
3624 atomic_read(&fs_info
->balance_running
) == 0);
3625 mutex_lock(&fs_info
->balance_mutex
);
3627 /* __cancel_balance needs volume_mutex */
3628 mutex_unlock(&fs_info
->balance_mutex
);
3629 mutex_lock(&fs_info
->volume_mutex
);
3630 mutex_lock(&fs_info
->balance_mutex
);
3632 if (fs_info
->balance_ctl
)
3633 __cancel_balance(fs_info
);
3635 mutex_unlock(&fs_info
->volume_mutex
);
3638 BUG_ON(fs_info
->balance_ctl
|| atomic_read(&fs_info
->balance_running
));
3639 atomic_dec(&fs_info
->balance_cancel_req
);
3640 mutex_unlock(&fs_info
->balance_mutex
);
3644 static int btrfs_uuid_scan_kthread(void *data
)
3646 struct btrfs_fs_info
*fs_info
= data
;
3647 struct btrfs_root
*root
= fs_info
->tree_root
;
3648 struct btrfs_key key
;
3649 struct btrfs_key max_key
;
3650 struct btrfs_path
*path
= NULL
;
3652 struct extent_buffer
*eb
;
3654 struct btrfs_root_item root_item
;
3656 struct btrfs_trans_handle
*trans
= NULL
;
3658 path
= btrfs_alloc_path();
3665 key
.type
= BTRFS_ROOT_ITEM_KEY
;
3668 max_key
.objectid
= (u64
)-1;
3669 max_key
.type
= BTRFS_ROOT_ITEM_KEY
;
3670 max_key
.offset
= (u64
)-1;
3673 ret
= btrfs_search_forward(root
, &key
, path
, 0);
3680 if (key
.type
!= BTRFS_ROOT_ITEM_KEY
||
3681 (key
.objectid
< BTRFS_FIRST_FREE_OBJECTID
&&
3682 key
.objectid
!= BTRFS_FS_TREE_OBJECTID
) ||
3683 key
.objectid
> BTRFS_LAST_FREE_OBJECTID
)
3686 eb
= path
->nodes
[0];
3687 slot
= path
->slots
[0];
3688 item_size
= btrfs_item_size_nr(eb
, slot
);
3689 if (item_size
< sizeof(root_item
))
3692 read_extent_buffer(eb
, &root_item
,
3693 btrfs_item_ptr_offset(eb
, slot
),
3694 (int)sizeof(root_item
));
3695 if (btrfs_root_refs(&root_item
) == 0)
3698 if (!btrfs_is_empty_uuid(root_item
.uuid
) ||
3699 !btrfs_is_empty_uuid(root_item
.received_uuid
)) {
3703 btrfs_release_path(path
);
3705 * 1 - subvol uuid item
3706 * 1 - received_subvol uuid item
3708 trans
= btrfs_start_transaction(fs_info
->uuid_root
, 2);
3709 if (IS_ERR(trans
)) {
3710 ret
= PTR_ERR(trans
);
3718 if (!btrfs_is_empty_uuid(root_item
.uuid
)) {
3719 ret
= btrfs_uuid_tree_add(trans
, fs_info
->uuid_root
,
3721 BTRFS_UUID_KEY_SUBVOL
,
3724 btrfs_warn(fs_info
, "uuid_tree_add failed %d",
3730 if (!btrfs_is_empty_uuid(root_item
.received_uuid
)) {
3731 ret
= btrfs_uuid_tree_add(trans
, fs_info
->uuid_root
,
3732 root_item
.received_uuid
,
3733 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
3736 btrfs_warn(fs_info
, "uuid_tree_add failed %d",
3744 ret
= btrfs_end_transaction(trans
, fs_info
->uuid_root
);
3750 btrfs_release_path(path
);
3751 if (key
.offset
< (u64
)-1) {
3753 } else if (key
.type
< BTRFS_ROOT_ITEM_KEY
) {
3755 key
.type
= BTRFS_ROOT_ITEM_KEY
;
3756 } else if (key
.objectid
< (u64
)-1) {
3758 key
.type
= BTRFS_ROOT_ITEM_KEY
;
3767 btrfs_free_path(path
);
3768 if (trans
&& !IS_ERR(trans
))
3769 btrfs_end_transaction(trans
, fs_info
->uuid_root
);
3771 btrfs_warn(fs_info
, "btrfs_uuid_scan_kthread failed %d", ret
);
3773 fs_info
->update_uuid_tree_gen
= 1;
3774 up(&fs_info
->uuid_tree_rescan_sem
);
3779 * Callback for btrfs_uuid_tree_iterate().
3781 * 0 check succeeded, the entry is not outdated.
3782 * < 0 if an error occured.
3783 * > 0 if the check failed, which means the caller shall remove the entry.
3785 static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info
*fs_info
,
3786 u8
*uuid
, u8 type
, u64 subid
)
3788 struct btrfs_key key
;
3790 struct btrfs_root
*subvol_root
;
3792 if (type
!= BTRFS_UUID_KEY_SUBVOL
&&
3793 type
!= BTRFS_UUID_KEY_RECEIVED_SUBVOL
)
3796 key
.objectid
= subid
;
3797 key
.type
= BTRFS_ROOT_ITEM_KEY
;
3798 key
.offset
= (u64
)-1;
3799 subvol_root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
3800 if (IS_ERR(subvol_root
)) {
3801 ret
= PTR_ERR(subvol_root
);
3808 case BTRFS_UUID_KEY_SUBVOL
:
3809 if (memcmp(uuid
, subvol_root
->root_item
.uuid
, BTRFS_UUID_SIZE
))
3812 case BTRFS_UUID_KEY_RECEIVED_SUBVOL
:
3813 if (memcmp(uuid
, subvol_root
->root_item
.received_uuid
,
3823 static int btrfs_uuid_rescan_kthread(void *data
)
3825 struct btrfs_fs_info
*fs_info
= (struct btrfs_fs_info
*)data
;
3829 * 1st step is to iterate through the existing UUID tree and
3830 * to delete all entries that contain outdated data.
3831 * 2nd step is to add all missing entries to the UUID tree.
3833 ret
= btrfs_uuid_tree_iterate(fs_info
, btrfs_check_uuid_tree_entry
);
3835 btrfs_warn(fs_info
, "iterating uuid_tree failed %d", ret
);
3836 up(&fs_info
->uuid_tree_rescan_sem
);
3839 return btrfs_uuid_scan_kthread(data
);
3842 int btrfs_create_uuid_tree(struct btrfs_fs_info
*fs_info
)
3844 struct btrfs_trans_handle
*trans
;
3845 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
3846 struct btrfs_root
*uuid_root
;
3847 struct task_struct
*task
;
3854 trans
= btrfs_start_transaction(tree_root
, 2);
3856 return PTR_ERR(trans
);
3858 uuid_root
= btrfs_create_tree(trans
, fs_info
,
3859 BTRFS_UUID_TREE_OBJECTID
);
3860 if (IS_ERR(uuid_root
)) {
3861 btrfs_abort_transaction(trans
, tree_root
,
3862 PTR_ERR(uuid_root
));
3863 return PTR_ERR(uuid_root
);
3866 fs_info
->uuid_root
= uuid_root
;
3868 ret
= btrfs_commit_transaction(trans
, tree_root
);
3872 down(&fs_info
->uuid_tree_rescan_sem
);
3873 task
= kthread_run(btrfs_uuid_scan_kthread
, fs_info
, "btrfs-uuid");
3875 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
3876 btrfs_warn(fs_info
, "failed to start uuid_scan task");
3877 up(&fs_info
->uuid_tree_rescan_sem
);
3878 return PTR_ERR(task
);
3884 int btrfs_check_uuid_tree(struct btrfs_fs_info
*fs_info
)
3886 struct task_struct
*task
;
3888 down(&fs_info
->uuid_tree_rescan_sem
);
3889 task
= kthread_run(btrfs_uuid_rescan_kthread
, fs_info
, "btrfs-uuid");
3891 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
3892 btrfs_warn(fs_info
, "failed to start uuid_rescan task");
3893 up(&fs_info
->uuid_tree_rescan_sem
);
3894 return PTR_ERR(task
);
3901 * shrinking a device means finding all of the device extents past
3902 * the new size, and then following the back refs to the chunks.
3903 * The chunk relocation code actually frees the device extent
3905 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
3907 struct btrfs_trans_handle
*trans
;
3908 struct btrfs_root
*root
= device
->dev_root
;
3909 struct btrfs_dev_extent
*dev_extent
= NULL
;
3910 struct btrfs_path
*path
;
3918 bool retried
= false;
3919 struct extent_buffer
*l
;
3920 struct btrfs_key key
;
3921 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
3922 u64 old_total
= btrfs_super_total_bytes(super_copy
);
3923 u64 old_size
= device
->total_bytes
;
3924 u64 diff
= device
->total_bytes
- new_size
;
3926 if (device
->is_tgtdev_for_dev_replace
)
3929 path
= btrfs_alloc_path();
3937 device
->total_bytes
= new_size
;
3938 if (device
->writeable
) {
3939 device
->fs_devices
->total_rw_bytes
-= diff
;
3940 spin_lock(&root
->fs_info
->free_chunk_lock
);
3941 root
->fs_info
->free_chunk_space
-= diff
;
3942 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3944 unlock_chunks(root
);
3947 key
.objectid
= device
->devid
;
3948 key
.offset
= (u64
)-1;
3949 key
.type
= BTRFS_DEV_EXTENT_KEY
;
3952 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3956 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
3961 btrfs_release_path(path
);
3966 slot
= path
->slots
[0];
3967 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
3969 if (key
.objectid
!= device
->devid
) {
3970 btrfs_release_path(path
);
3974 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
3975 length
= btrfs_dev_extent_length(l
, dev_extent
);
3977 if (key
.offset
+ length
<= new_size
) {
3978 btrfs_release_path(path
);
3982 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
3983 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
3984 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
3985 btrfs_release_path(path
);
3987 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
3989 if (ret
&& ret
!= -ENOSPC
)
3993 } while (key
.offset
-- > 0);
3995 if (failed
&& !retried
) {
3999 } else if (failed
&& retried
) {
4003 device
->total_bytes
= old_size
;
4004 if (device
->writeable
)
4005 device
->fs_devices
->total_rw_bytes
+= diff
;
4006 spin_lock(&root
->fs_info
->free_chunk_lock
);
4007 root
->fs_info
->free_chunk_space
+= diff
;
4008 spin_unlock(&root
->fs_info
->free_chunk_lock
);
4009 unlock_chunks(root
);
4013 /* Shrinking succeeded, else we would be at "done". */
4014 trans
= btrfs_start_transaction(root
, 0);
4015 if (IS_ERR(trans
)) {
4016 ret
= PTR_ERR(trans
);
4021 device
->disk_total_bytes
= new_size
;
4022 if (list_empty(&device
->resized_list
))
4023 list_add_tail(&device
->resized_list
,
4024 &root
->fs_info
->fs_devices
->resized_devices
);
4026 /* Now btrfs_update_device() will change the on-disk size. */
4027 ret
= btrfs_update_device(trans
, device
);
4029 unlock_chunks(root
);
4030 btrfs_end_transaction(trans
, root
);
4033 WARN_ON(diff
> old_total
);
4034 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
4035 unlock_chunks(root
);
4036 btrfs_end_transaction(trans
, root
);
4038 btrfs_free_path(path
);
4042 static int btrfs_add_system_chunk(struct btrfs_root
*root
,
4043 struct btrfs_key
*key
,
4044 struct btrfs_chunk
*chunk
, int item_size
)
4046 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
4047 struct btrfs_disk_key disk_key
;
4051 array_size
= btrfs_super_sys_array_size(super_copy
);
4052 if (array_size
+ item_size
+ sizeof(disk_key
)
4053 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
4056 ptr
= super_copy
->sys_chunk_array
+ array_size
;
4057 btrfs_cpu_key_to_disk(&disk_key
, key
);
4058 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
4059 ptr
+= sizeof(disk_key
);
4060 memcpy(ptr
, chunk
, item_size
);
4061 item_size
+= sizeof(disk_key
);
4062 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
4067 * sort the devices in descending order by max_avail, total_avail
4069 static int btrfs_cmp_device_info(const void *a
, const void *b
)
4071 const struct btrfs_device_info
*di_a
= a
;
4072 const struct btrfs_device_info
*di_b
= b
;
4074 if (di_a
->max_avail
> di_b
->max_avail
)
4076 if (di_a
->max_avail
< di_b
->max_avail
)
4078 if (di_a
->total_avail
> di_b
->total_avail
)
4080 if (di_a
->total_avail
< di_b
->total_avail
)
4085 static struct btrfs_raid_attr btrfs_raid_array
[BTRFS_NR_RAID_TYPES
] = {
4086 [BTRFS_RAID_RAID10
] = {
4089 .devs_max
= 0, /* 0 == as many as possible */
4091 .devs_increment
= 2,
4094 [BTRFS_RAID_RAID1
] = {
4099 .devs_increment
= 2,
4102 [BTRFS_RAID_DUP
] = {
4107 .devs_increment
= 1,
4110 [BTRFS_RAID_RAID0
] = {
4115 .devs_increment
= 1,
4118 [BTRFS_RAID_SINGLE
] = {
4123 .devs_increment
= 1,
4126 [BTRFS_RAID_RAID5
] = {
4131 .devs_increment
= 1,
4134 [BTRFS_RAID_RAID6
] = {
4139 .devs_increment
= 1,
4144 static u32
find_raid56_stripe_len(u32 data_devices
, u32 dev_stripe_target
)
4146 /* TODO allow them to set a preferred stripe size */
4150 static void check_raid56_incompat_flag(struct btrfs_fs_info
*info
, u64 type
)
4152 if (!(type
& (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
)))
4155 btrfs_set_fs_incompat(info
, RAID56
);
4158 #define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4159 - sizeof(struct btrfs_item) \
4160 - sizeof(struct btrfs_chunk)) \
4161 / sizeof(struct btrfs_stripe) + 1)
4163 #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4164 - 2 * sizeof(struct btrfs_disk_key) \
4165 - 2 * sizeof(struct btrfs_chunk)) \
4166 / sizeof(struct btrfs_stripe) + 1)
4168 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
4169 struct btrfs_root
*extent_root
, u64 start
,
4172 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
4173 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
4174 struct list_head
*cur
;
4175 struct map_lookup
*map
= NULL
;
4176 struct extent_map_tree
*em_tree
;
4177 struct extent_map
*em
;
4178 struct btrfs_device_info
*devices_info
= NULL
;
4180 int num_stripes
; /* total number of stripes to allocate */
4181 int data_stripes
; /* number of stripes that count for
4183 int sub_stripes
; /* sub_stripes info for map */
4184 int dev_stripes
; /* stripes per dev */
4185 int devs_max
; /* max devs to use */
4186 int devs_min
; /* min devs needed */
4187 int devs_increment
; /* ndevs has to be a multiple of this */
4188 int ncopies
; /* how many copies to data has */
4190 u64 max_stripe_size
;
4194 u64 raid_stripe_len
= BTRFS_STRIPE_LEN
;
4200 BUG_ON(!alloc_profile_is_valid(type
, 0));
4202 if (list_empty(&fs_devices
->alloc_list
))
4205 index
= __get_raid_index(type
);
4207 sub_stripes
= btrfs_raid_array
[index
].sub_stripes
;
4208 dev_stripes
= btrfs_raid_array
[index
].dev_stripes
;
4209 devs_max
= btrfs_raid_array
[index
].devs_max
;
4210 devs_min
= btrfs_raid_array
[index
].devs_min
;
4211 devs_increment
= btrfs_raid_array
[index
].devs_increment
;
4212 ncopies
= btrfs_raid_array
[index
].ncopies
;
4214 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
4215 max_stripe_size
= 1024 * 1024 * 1024;
4216 max_chunk_size
= 10 * max_stripe_size
;
4218 devs_max
= BTRFS_MAX_DEVS(info
->chunk_root
);
4219 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
4220 /* for larger filesystems, use larger metadata chunks */
4221 if (fs_devices
->total_rw_bytes
> 50ULL * 1024 * 1024 * 1024)
4222 max_stripe_size
= 1024 * 1024 * 1024;
4224 max_stripe_size
= 256 * 1024 * 1024;
4225 max_chunk_size
= max_stripe_size
;
4227 devs_max
= BTRFS_MAX_DEVS(info
->chunk_root
);
4228 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
4229 max_stripe_size
= 32 * 1024 * 1024;
4230 max_chunk_size
= 2 * max_stripe_size
;
4232 devs_max
= BTRFS_MAX_DEVS_SYS_CHUNK
;
4234 btrfs_err(info
, "invalid chunk type 0x%llx requested",
4239 /* we don't want a chunk larger than 10% of writeable space */
4240 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
4243 devices_info
= kzalloc(sizeof(*devices_info
) * fs_devices
->rw_devices
,
4248 cur
= fs_devices
->alloc_list
.next
;
4251 * in the first pass through the devices list, we gather information
4252 * about the available holes on each device.
4255 while (cur
!= &fs_devices
->alloc_list
) {
4256 struct btrfs_device
*device
;
4260 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
4264 if (!device
->writeable
) {
4266 "BTRFS: read-only device in alloc_list\n");
4270 if (!device
->in_fs_metadata
||
4271 device
->is_tgtdev_for_dev_replace
)
4274 if (device
->total_bytes
> device
->bytes_used
)
4275 total_avail
= device
->total_bytes
- device
->bytes_used
;
4279 /* If there is no space on this device, skip it. */
4280 if (total_avail
== 0)
4283 ret
= find_free_dev_extent(trans
, device
,
4284 max_stripe_size
* dev_stripes
,
4285 &dev_offset
, &max_avail
);
4286 if (ret
&& ret
!= -ENOSPC
)
4290 max_avail
= max_stripe_size
* dev_stripes
;
4292 if (max_avail
< BTRFS_STRIPE_LEN
* dev_stripes
)
4295 if (ndevs
== fs_devices
->rw_devices
) {
4296 WARN(1, "%s: found more than %llu devices\n",
4297 __func__
, fs_devices
->rw_devices
);
4300 devices_info
[ndevs
].dev_offset
= dev_offset
;
4301 devices_info
[ndevs
].max_avail
= max_avail
;
4302 devices_info
[ndevs
].total_avail
= total_avail
;
4303 devices_info
[ndevs
].dev
= device
;
4308 * now sort the devices by hole size / available space
4310 sort(devices_info
, ndevs
, sizeof(struct btrfs_device_info
),
4311 btrfs_cmp_device_info
, NULL
);
4313 /* round down to number of usable stripes */
4314 ndevs
-= ndevs
% devs_increment
;
4316 if (ndevs
< devs_increment
* sub_stripes
|| ndevs
< devs_min
) {
4321 if (devs_max
&& ndevs
> devs_max
)
4324 * the primary goal is to maximize the number of stripes, so use as many
4325 * devices as possible, even if the stripes are not maximum sized.
4327 stripe_size
= devices_info
[ndevs
-1].max_avail
;
4328 num_stripes
= ndevs
* dev_stripes
;
4331 * this will have to be fixed for RAID1 and RAID10 over
4334 data_stripes
= num_stripes
/ ncopies
;
4336 if (type
& BTRFS_BLOCK_GROUP_RAID5
) {
4337 raid_stripe_len
= find_raid56_stripe_len(ndevs
- 1,
4338 btrfs_super_stripesize(info
->super_copy
));
4339 data_stripes
= num_stripes
- 1;
4341 if (type
& BTRFS_BLOCK_GROUP_RAID6
) {
4342 raid_stripe_len
= find_raid56_stripe_len(ndevs
- 2,
4343 btrfs_super_stripesize(info
->super_copy
));
4344 data_stripes
= num_stripes
- 2;
4348 * Use the number of data stripes to figure out how big this chunk
4349 * is really going to be in terms of logical address space,
4350 * and compare that answer with the max chunk size
4352 if (stripe_size
* data_stripes
> max_chunk_size
) {
4353 u64 mask
= (1ULL << 24) - 1;
4354 stripe_size
= max_chunk_size
;
4355 do_div(stripe_size
, data_stripes
);
4357 /* bump the answer up to a 16MB boundary */
4358 stripe_size
= (stripe_size
+ mask
) & ~mask
;
4360 /* but don't go higher than the limits we found
4361 * while searching for free extents
4363 if (stripe_size
> devices_info
[ndevs
-1].max_avail
)
4364 stripe_size
= devices_info
[ndevs
-1].max_avail
;
4367 do_div(stripe_size
, dev_stripes
);
4369 /* align to BTRFS_STRIPE_LEN */
4370 do_div(stripe_size
, raid_stripe_len
);
4371 stripe_size
*= raid_stripe_len
;
4373 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
4378 map
->num_stripes
= num_stripes
;
4380 for (i
= 0; i
< ndevs
; ++i
) {
4381 for (j
= 0; j
< dev_stripes
; ++j
) {
4382 int s
= i
* dev_stripes
+ j
;
4383 map
->stripes
[s
].dev
= devices_info
[i
].dev
;
4384 map
->stripes
[s
].physical
= devices_info
[i
].dev_offset
+
4388 map
->sector_size
= extent_root
->sectorsize
;
4389 map
->stripe_len
= raid_stripe_len
;
4390 map
->io_align
= raid_stripe_len
;
4391 map
->io_width
= raid_stripe_len
;
4393 map
->sub_stripes
= sub_stripes
;
4395 num_bytes
= stripe_size
* data_stripes
;
4397 trace_btrfs_chunk_alloc(info
->chunk_root
, map
, start
, num_bytes
);
4399 em
= alloc_extent_map();
4405 set_bit(EXTENT_FLAG_FS_MAPPING
, &em
->flags
);
4406 em
->bdev
= (struct block_device
*)map
;
4408 em
->len
= num_bytes
;
4409 em
->block_start
= 0;
4410 em
->block_len
= em
->len
;
4411 em
->orig_block_len
= stripe_size
;
4413 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
4414 write_lock(&em_tree
->lock
);
4415 ret
= add_extent_mapping(em_tree
, em
, 0);
4417 list_add_tail(&em
->list
, &trans
->transaction
->pending_chunks
);
4418 atomic_inc(&em
->refs
);
4420 write_unlock(&em_tree
->lock
);
4422 free_extent_map(em
);
4426 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
4427 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
4430 goto error_del_extent
;
4432 free_extent_map(em
);
4433 check_raid56_incompat_flag(extent_root
->fs_info
, type
);
4435 kfree(devices_info
);
4439 write_lock(&em_tree
->lock
);
4440 remove_extent_mapping(em_tree
, em
);
4441 write_unlock(&em_tree
->lock
);
4443 /* One for our allocation */
4444 free_extent_map(em
);
4445 /* One for the tree reference */
4446 free_extent_map(em
);
4448 kfree(devices_info
);
4452 int btrfs_finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
4453 struct btrfs_root
*extent_root
,
4454 u64 chunk_offset
, u64 chunk_size
)
4456 struct btrfs_key key
;
4457 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
4458 struct btrfs_device
*device
;
4459 struct btrfs_chunk
*chunk
;
4460 struct btrfs_stripe
*stripe
;
4461 struct extent_map_tree
*em_tree
;
4462 struct extent_map
*em
;
4463 struct map_lookup
*map
;
4470 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
4471 read_lock(&em_tree
->lock
);
4472 em
= lookup_extent_mapping(em_tree
, chunk_offset
, chunk_size
);
4473 read_unlock(&em_tree
->lock
);
4476 btrfs_crit(extent_root
->fs_info
, "unable to find logical "
4477 "%Lu len %Lu", chunk_offset
, chunk_size
);
4481 if (em
->start
!= chunk_offset
|| em
->len
!= chunk_size
) {
4482 btrfs_crit(extent_root
->fs_info
, "found a bad mapping, wanted"
4483 " %Lu-%Lu, found %Lu-%Lu", chunk_offset
,
4484 chunk_size
, em
->start
, em
->len
);
4485 free_extent_map(em
);
4489 map
= (struct map_lookup
*)em
->bdev
;
4490 item_size
= btrfs_chunk_item_size(map
->num_stripes
);
4491 stripe_size
= em
->orig_block_len
;
4493 chunk
= kzalloc(item_size
, GFP_NOFS
);
4499 for (i
= 0; i
< map
->num_stripes
; i
++) {
4500 device
= map
->stripes
[i
].dev
;
4501 dev_offset
= map
->stripes
[i
].physical
;
4503 device
->bytes_used
+= stripe_size
;
4504 ret
= btrfs_update_device(trans
, device
);
4507 ret
= btrfs_alloc_dev_extent(trans
, device
,
4508 chunk_root
->root_key
.objectid
,
4509 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
4510 chunk_offset
, dev_offset
,
4516 spin_lock(&extent_root
->fs_info
->free_chunk_lock
);
4517 extent_root
->fs_info
->free_chunk_space
-= (stripe_size
*
4519 spin_unlock(&extent_root
->fs_info
->free_chunk_lock
);
4521 stripe
= &chunk
->stripe
;
4522 for (i
= 0; i
< map
->num_stripes
; i
++) {
4523 device
= map
->stripes
[i
].dev
;
4524 dev_offset
= map
->stripes
[i
].physical
;
4526 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
4527 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
4528 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
4532 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
4533 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
4534 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
4535 btrfs_set_stack_chunk_type(chunk
, map
->type
);
4536 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
4537 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
4538 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
4539 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
4540 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
4542 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
4543 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
4544 key
.offset
= chunk_offset
;
4546 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
4547 if (ret
== 0 && map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
4549 * TODO: Cleanup of inserted chunk root in case of
4552 ret
= btrfs_add_system_chunk(chunk_root
, &key
, chunk
,
4558 free_extent_map(em
);
4563 * Chunk allocation falls into two parts. The first part does works
4564 * that make the new allocated chunk useable, but not do any operation
4565 * that modifies the chunk tree. The second part does the works that
4566 * require modifying the chunk tree. This division is important for the
4567 * bootstrap process of adding storage to a seed btrfs.
4569 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
4570 struct btrfs_root
*extent_root
, u64 type
)
4574 chunk_offset
= find_next_chunk(extent_root
->fs_info
);
4575 return __btrfs_alloc_chunk(trans
, extent_root
, chunk_offset
, type
);
4578 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
4579 struct btrfs_root
*root
,
4580 struct btrfs_device
*device
)
4583 u64 sys_chunk_offset
;
4585 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4586 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
4589 chunk_offset
= find_next_chunk(fs_info
);
4590 alloc_profile
= btrfs_get_alloc_profile(extent_root
, 0);
4591 ret
= __btrfs_alloc_chunk(trans
, extent_root
, chunk_offset
,
4596 sys_chunk_offset
= find_next_chunk(root
->fs_info
);
4597 alloc_profile
= btrfs_get_alloc_profile(fs_info
->chunk_root
, 0);
4598 ret
= __btrfs_alloc_chunk(trans
, extent_root
, sys_chunk_offset
,
4601 btrfs_abort_transaction(trans
, root
, ret
);
4605 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
4607 btrfs_abort_transaction(trans
, root
, ret
);
4612 static inline int btrfs_chunk_max_errors(struct map_lookup
*map
)
4616 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
4617 BTRFS_BLOCK_GROUP_RAID10
|
4618 BTRFS_BLOCK_GROUP_RAID5
|
4619 BTRFS_BLOCK_GROUP_DUP
)) {
4621 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
) {
4630 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
4632 struct extent_map
*em
;
4633 struct map_lookup
*map
;
4634 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
4639 read_lock(&map_tree
->map_tree
.lock
);
4640 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
4641 read_unlock(&map_tree
->map_tree
.lock
);
4645 map
= (struct map_lookup
*)em
->bdev
;
4646 for (i
= 0; i
< map
->num_stripes
; i
++) {
4647 if (map
->stripes
[i
].dev
->missing
) {
4652 if (!map
->stripes
[i
].dev
->writeable
) {
4659 * If the number of missing devices is larger than max errors,
4660 * we can not write the data into that chunk successfully, so
4663 if (miss_ndevs
> btrfs_chunk_max_errors(map
))
4666 free_extent_map(em
);
4670 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
4672 extent_map_tree_init(&tree
->map_tree
);
4675 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
4677 struct extent_map
*em
;
4680 write_lock(&tree
->map_tree
.lock
);
4681 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
4683 remove_extent_mapping(&tree
->map_tree
, em
);
4684 write_unlock(&tree
->map_tree
.lock
);
4688 free_extent_map(em
);
4689 /* once for the tree */
4690 free_extent_map(em
);
4694 int btrfs_num_copies(struct btrfs_fs_info
*fs_info
, u64 logical
, u64 len
)
4696 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
4697 struct extent_map
*em
;
4698 struct map_lookup
*map
;
4699 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4702 read_lock(&em_tree
->lock
);
4703 em
= lookup_extent_mapping(em_tree
, logical
, len
);
4704 read_unlock(&em_tree
->lock
);
4707 * We could return errors for these cases, but that could get ugly and
4708 * we'd probably do the same thing which is just not do anything else
4709 * and exit, so return 1 so the callers don't try to use other copies.
4712 btrfs_crit(fs_info
, "No mapping for %Lu-%Lu", logical
,
4717 if (em
->start
> logical
|| em
->start
+ em
->len
< logical
) {
4718 btrfs_crit(fs_info
, "Invalid mapping for %Lu-%Lu, got "
4719 "%Lu-%Lu", logical
, logical
+len
, em
->start
,
4720 em
->start
+ em
->len
);
4721 free_extent_map(em
);
4725 map
= (struct map_lookup
*)em
->bdev
;
4726 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
4727 ret
= map
->num_stripes
;
4728 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
4729 ret
= map
->sub_stripes
;
4730 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID5
)
4732 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
4736 free_extent_map(em
);
4738 btrfs_dev_replace_lock(&fs_info
->dev_replace
);
4739 if (btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
))
4741 btrfs_dev_replace_unlock(&fs_info
->dev_replace
);
4746 unsigned long btrfs_full_stripe_len(struct btrfs_root
*root
,
4747 struct btrfs_mapping_tree
*map_tree
,
4750 struct extent_map
*em
;
4751 struct map_lookup
*map
;
4752 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4753 unsigned long len
= root
->sectorsize
;
4755 read_lock(&em_tree
->lock
);
4756 em
= lookup_extent_mapping(em_tree
, logical
, len
);
4757 read_unlock(&em_tree
->lock
);
4760 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
4761 map
= (struct map_lookup
*)em
->bdev
;
4762 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
4763 BTRFS_BLOCK_GROUP_RAID6
)) {
4764 len
= map
->stripe_len
* nr_data_stripes(map
);
4766 free_extent_map(em
);
4770 int btrfs_is_parity_mirror(struct btrfs_mapping_tree
*map_tree
,
4771 u64 logical
, u64 len
, int mirror_num
)
4773 struct extent_map
*em
;
4774 struct map_lookup
*map
;
4775 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4778 read_lock(&em_tree
->lock
);
4779 em
= lookup_extent_mapping(em_tree
, logical
, len
);
4780 read_unlock(&em_tree
->lock
);
4783 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
4784 map
= (struct map_lookup
*)em
->bdev
;
4785 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
4786 BTRFS_BLOCK_GROUP_RAID6
))
4788 free_extent_map(em
);
4792 static int find_live_mirror(struct btrfs_fs_info
*fs_info
,
4793 struct map_lookup
*map
, int first
, int num
,
4794 int optimal
, int dev_replace_is_ongoing
)
4798 struct btrfs_device
*srcdev
;
4800 if (dev_replace_is_ongoing
&&
4801 fs_info
->dev_replace
.cont_reading_from_srcdev_mode
==
4802 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID
)
4803 srcdev
= fs_info
->dev_replace
.srcdev
;
4808 * try to avoid the drive that is the source drive for a
4809 * dev-replace procedure, only choose it if no other non-missing
4810 * mirror is available
4812 for (tolerance
= 0; tolerance
< 2; tolerance
++) {
4813 if (map
->stripes
[optimal
].dev
->bdev
&&
4814 (tolerance
|| map
->stripes
[optimal
].dev
!= srcdev
))
4816 for (i
= first
; i
< first
+ num
; i
++) {
4817 if (map
->stripes
[i
].dev
->bdev
&&
4818 (tolerance
|| map
->stripes
[i
].dev
!= srcdev
))
4823 /* we couldn't find one that doesn't fail. Just return something
4824 * and the io error handling code will clean up eventually
4829 static inline int parity_smaller(u64 a
, u64 b
)
4834 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4835 static void sort_parity_stripes(struct btrfs_bio
*bbio
, u64
*raid_map
)
4837 struct btrfs_bio_stripe s
;
4844 for (i
= 0; i
< bbio
->num_stripes
- 1; i
++) {
4845 if (parity_smaller(raid_map
[i
], raid_map
[i
+1])) {
4846 s
= bbio
->stripes
[i
];
4848 bbio
->stripes
[i
] = bbio
->stripes
[i
+1];
4849 raid_map
[i
] = raid_map
[i
+1];
4850 bbio
->stripes
[i
+1] = s
;
4858 static int __btrfs_map_block(struct btrfs_fs_info
*fs_info
, int rw
,
4859 u64 logical
, u64
*length
,
4860 struct btrfs_bio
**bbio_ret
,
4861 int mirror_num
, u64
**raid_map_ret
)
4863 struct extent_map
*em
;
4864 struct map_lookup
*map
;
4865 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
4866 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4869 u64 stripe_end_offset
;
4874 u64
*raid_map
= NULL
;
4880 struct btrfs_bio
*bbio
= NULL
;
4881 struct btrfs_dev_replace
*dev_replace
= &fs_info
->dev_replace
;
4882 int dev_replace_is_ongoing
= 0;
4883 int num_alloc_stripes
;
4884 int patch_the_first_stripe_for_dev_replace
= 0;
4885 u64 physical_to_patch_in_first_stripe
= 0;
4886 u64 raid56_full_stripe_start
= (u64
)-1;
4888 read_lock(&em_tree
->lock
);
4889 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
4890 read_unlock(&em_tree
->lock
);
4893 btrfs_crit(fs_info
, "unable to find logical %llu len %llu",
4898 if (em
->start
> logical
|| em
->start
+ em
->len
< logical
) {
4899 btrfs_crit(fs_info
, "found a bad mapping, wanted %Lu, "
4900 "found %Lu-%Lu", logical
, em
->start
,
4901 em
->start
+ em
->len
);
4902 free_extent_map(em
);
4906 map
= (struct map_lookup
*)em
->bdev
;
4907 offset
= logical
- em
->start
;
4909 stripe_len
= map
->stripe_len
;
4912 * stripe_nr counts the total number of stripes we have to stride
4913 * to get to this block
4915 do_div(stripe_nr
, stripe_len
);
4917 stripe_offset
= stripe_nr
* stripe_len
;
4918 BUG_ON(offset
< stripe_offset
);
4920 /* stripe_offset is the offset of this block in its stripe*/
4921 stripe_offset
= offset
- stripe_offset
;
4923 /* if we're here for raid56, we need to know the stripe aligned start */
4924 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
)) {
4925 unsigned long full_stripe_len
= stripe_len
* nr_data_stripes(map
);
4926 raid56_full_stripe_start
= offset
;
4928 /* allow a write of a full stripe, but make sure we don't
4929 * allow straddling of stripes
4931 do_div(raid56_full_stripe_start
, full_stripe_len
);
4932 raid56_full_stripe_start
*= full_stripe_len
;
4935 if (rw
& REQ_DISCARD
) {
4936 /* we don't discard raid56 yet */
4938 (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
)) {
4942 *length
= min_t(u64
, em
->len
- offset
, *length
);
4943 } else if (map
->type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) {
4945 /* For writes to RAID[56], allow a full stripeset across all disks.
4946 For other RAID types and for RAID[56] reads, just allow a single
4947 stripe (on a single disk). */
4948 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
) &&
4950 max_len
= stripe_len
* nr_data_stripes(map
) -
4951 (offset
- raid56_full_stripe_start
);
4953 /* we limit the length of each bio to what fits in a stripe */
4954 max_len
= stripe_len
- stripe_offset
;
4956 *length
= min_t(u64
, em
->len
- offset
, max_len
);
4958 *length
= em
->len
- offset
;
4961 /* This is for when we're called from btrfs_merge_bio_hook() and all
4962 it cares about is the length */
4966 btrfs_dev_replace_lock(dev_replace
);
4967 dev_replace_is_ongoing
= btrfs_dev_replace_is_ongoing(dev_replace
);
4968 if (!dev_replace_is_ongoing
)
4969 btrfs_dev_replace_unlock(dev_replace
);
4971 if (dev_replace_is_ongoing
&& mirror_num
== map
->num_stripes
+ 1 &&
4972 !(rw
& (REQ_WRITE
| REQ_DISCARD
| REQ_GET_READ_MIRRORS
)) &&
4973 dev_replace
->tgtdev
!= NULL
) {
4975 * in dev-replace case, for repair case (that's the only
4976 * case where the mirror is selected explicitly when
4977 * calling btrfs_map_block), blocks left of the left cursor
4978 * can also be read from the target drive.
4979 * For REQ_GET_READ_MIRRORS, the target drive is added as
4980 * the last one to the array of stripes. For READ, it also
4981 * needs to be supported using the same mirror number.
4982 * If the requested block is not left of the left cursor,
4983 * EIO is returned. This can happen because btrfs_num_copies()
4984 * returns one more in the dev-replace case.
4986 u64 tmp_length
= *length
;
4987 struct btrfs_bio
*tmp_bbio
= NULL
;
4988 int tmp_num_stripes
;
4989 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
4990 int index_srcdev
= 0;
4992 u64 physical_of_found
= 0;
4994 ret
= __btrfs_map_block(fs_info
, REQ_GET_READ_MIRRORS
,
4995 logical
, &tmp_length
, &tmp_bbio
, 0, NULL
);
4997 WARN_ON(tmp_bbio
!= NULL
);
5001 tmp_num_stripes
= tmp_bbio
->num_stripes
;
5002 if (mirror_num
> tmp_num_stripes
) {
5004 * REQ_GET_READ_MIRRORS does not contain this
5005 * mirror, that means that the requested area
5006 * is not left of the left cursor
5014 * process the rest of the function using the mirror_num
5015 * of the source drive. Therefore look it up first.
5016 * At the end, patch the device pointer to the one of the
5019 for (i
= 0; i
< tmp_num_stripes
; i
++) {
5020 if (tmp_bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
5022 * In case of DUP, in order to keep it
5023 * simple, only add the mirror with the
5024 * lowest physical address
5027 physical_of_found
<=
5028 tmp_bbio
->stripes
[i
].physical
)
5033 tmp_bbio
->stripes
[i
].physical
;
5038 mirror_num
= index_srcdev
+ 1;
5039 patch_the_first_stripe_for_dev_replace
= 1;
5040 physical_to_patch_in_first_stripe
= physical_of_found
;
5049 } else if (mirror_num
> map
->num_stripes
) {
5055 stripe_nr_orig
= stripe_nr
;
5056 stripe_nr_end
= ALIGN(offset
+ *length
, map
->stripe_len
);
5057 do_div(stripe_nr_end
, map
->stripe_len
);
5058 stripe_end_offset
= stripe_nr_end
* map
->stripe_len
-
5061 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
5062 if (rw
& REQ_DISCARD
)
5063 num_stripes
= min_t(u64
, map
->num_stripes
,
5064 stripe_nr_end
- stripe_nr_orig
);
5065 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
5066 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
5067 if (rw
& (REQ_WRITE
| REQ_DISCARD
| REQ_GET_READ_MIRRORS
))
5068 num_stripes
= map
->num_stripes
;
5069 else if (mirror_num
)
5070 stripe_index
= mirror_num
- 1;
5072 stripe_index
= find_live_mirror(fs_info
, map
, 0,
5074 current
->pid
% map
->num_stripes
,
5075 dev_replace_is_ongoing
);
5076 mirror_num
= stripe_index
+ 1;
5079 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
5080 if (rw
& (REQ_WRITE
| REQ_DISCARD
| REQ_GET_READ_MIRRORS
)) {
5081 num_stripes
= map
->num_stripes
;
5082 } else if (mirror_num
) {
5083 stripe_index
= mirror_num
- 1;
5088 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
5089 int factor
= map
->num_stripes
/ map
->sub_stripes
;
5091 stripe_index
= do_div(stripe_nr
, factor
);
5092 stripe_index
*= map
->sub_stripes
;
5094 if (rw
& (REQ_WRITE
| REQ_GET_READ_MIRRORS
))
5095 num_stripes
= map
->sub_stripes
;
5096 else if (rw
& REQ_DISCARD
)
5097 num_stripes
= min_t(u64
, map
->sub_stripes
*
5098 (stripe_nr_end
- stripe_nr_orig
),
5100 else if (mirror_num
)
5101 stripe_index
+= mirror_num
- 1;
5103 int old_stripe_index
= stripe_index
;
5104 stripe_index
= find_live_mirror(fs_info
, map
,
5106 map
->sub_stripes
, stripe_index
+
5107 current
->pid
% map
->sub_stripes
,
5108 dev_replace_is_ongoing
);
5109 mirror_num
= stripe_index
- old_stripe_index
+ 1;
5112 } else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
5113 BTRFS_BLOCK_GROUP_RAID6
)) {
5116 if (bbio_ret
&& ((rw
& REQ_WRITE
) || mirror_num
> 1)
5120 /* push stripe_nr back to the start of the full stripe */
5121 stripe_nr
= raid56_full_stripe_start
;
5122 do_div(stripe_nr
, stripe_len
);
5124 stripe_index
= do_div(stripe_nr
, nr_data_stripes(map
));
5126 /* RAID[56] write or recovery. Return all stripes */
5127 num_stripes
= map
->num_stripes
;
5128 max_errors
= nr_parity_stripes(map
);
5130 raid_map
= kmalloc_array(num_stripes
, sizeof(u64
),
5137 /* Work out the disk rotation on this stripe-set */
5139 rot
= do_div(tmp
, num_stripes
);
5141 /* Fill in the logical address of each stripe */
5142 tmp
= stripe_nr
* nr_data_stripes(map
);
5143 for (i
= 0; i
< nr_data_stripes(map
); i
++)
5144 raid_map
[(i
+rot
) % num_stripes
] =
5145 em
->start
+ (tmp
+ i
) * map
->stripe_len
;
5147 raid_map
[(i
+rot
) % map
->num_stripes
] = RAID5_P_STRIPE
;
5148 if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
5149 raid_map
[(i
+rot
+1) % num_stripes
] =
5152 *length
= map
->stripe_len
;
5157 * Mirror #0 or #1 means the original data block.
5158 * Mirror #2 is RAID5 parity block.
5159 * Mirror #3 is RAID6 Q block.
5161 stripe_index
= do_div(stripe_nr
, nr_data_stripes(map
));
5163 stripe_index
= nr_data_stripes(map
) +
5166 /* We distribute the parity blocks across stripes */
5167 tmp
= stripe_nr
+ stripe_index
;
5168 stripe_index
= do_div(tmp
, map
->num_stripes
);
5172 * after this do_div call, stripe_nr is the number of stripes
5173 * on this device we have to walk to find the data, and
5174 * stripe_index is the number of our device in the stripe array
5176 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
5177 mirror_num
= stripe_index
+ 1;
5179 BUG_ON(stripe_index
>= map
->num_stripes
);
5181 num_alloc_stripes
= num_stripes
;
5182 if (dev_replace_is_ongoing
) {
5183 if (rw
& (REQ_WRITE
| REQ_DISCARD
))
5184 num_alloc_stripes
<<= 1;
5185 if (rw
& REQ_GET_READ_MIRRORS
)
5186 num_alloc_stripes
++;
5188 bbio
= kzalloc(btrfs_bio_size(num_alloc_stripes
), GFP_NOFS
);
5194 atomic_set(&bbio
->error
, 0);
5196 if (rw
& REQ_DISCARD
) {
5198 int sub_stripes
= 0;
5199 u64 stripes_per_dev
= 0;
5200 u32 remaining_stripes
= 0;
5201 u32 last_stripe
= 0;
5204 (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID10
)) {
5205 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
5208 sub_stripes
= map
->sub_stripes
;
5210 factor
= map
->num_stripes
/ sub_stripes
;
5211 stripes_per_dev
= div_u64_rem(stripe_nr_end
-
5214 &remaining_stripes
);
5215 div_u64_rem(stripe_nr_end
- 1, factor
, &last_stripe
);
5216 last_stripe
*= sub_stripes
;
5219 for (i
= 0; i
< num_stripes
; i
++) {
5220 bbio
->stripes
[i
].physical
=
5221 map
->stripes
[stripe_index
].physical
+
5222 stripe_offset
+ stripe_nr
* map
->stripe_len
;
5223 bbio
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
5225 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
5226 BTRFS_BLOCK_GROUP_RAID10
)) {
5227 bbio
->stripes
[i
].length
= stripes_per_dev
*
5230 if (i
/ sub_stripes
< remaining_stripes
)
5231 bbio
->stripes
[i
].length
+=
5235 * Special for the first stripe and
5238 * |-------|...|-------|
5242 if (i
< sub_stripes
)
5243 bbio
->stripes
[i
].length
-=
5246 if (stripe_index
>= last_stripe
&&
5247 stripe_index
<= (last_stripe
+
5249 bbio
->stripes
[i
].length
-=
5252 if (i
== sub_stripes
- 1)
5255 bbio
->stripes
[i
].length
= *length
;
5258 if (stripe_index
== map
->num_stripes
) {
5259 /* This could only happen for RAID0/10 */
5265 for (i
= 0; i
< num_stripes
; i
++) {
5266 bbio
->stripes
[i
].physical
=
5267 map
->stripes
[stripe_index
].physical
+
5269 stripe_nr
* map
->stripe_len
;
5270 bbio
->stripes
[i
].dev
=
5271 map
->stripes
[stripe_index
].dev
;
5276 if (rw
& (REQ_WRITE
| REQ_GET_READ_MIRRORS
))
5277 max_errors
= btrfs_chunk_max_errors(map
);
5279 if (dev_replace_is_ongoing
&& (rw
& (REQ_WRITE
| REQ_DISCARD
)) &&
5280 dev_replace
->tgtdev
!= NULL
) {
5281 int index_where_to_add
;
5282 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
5285 * duplicate the write operations while the dev replace
5286 * procedure is running. Since the copying of the old disk
5287 * to the new disk takes place at run time while the
5288 * filesystem is mounted writable, the regular write
5289 * operations to the old disk have to be duplicated to go
5290 * to the new disk as well.
5291 * Note that device->missing is handled by the caller, and
5292 * that the write to the old disk is already set up in the
5295 index_where_to_add
= num_stripes
;
5296 for (i
= 0; i
< num_stripes
; i
++) {
5297 if (bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
5298 /* write to new disk, too */
5299 struct btrfs_bio_stripe
*new =
5300 bbio
->stripes
+ index_where_to_add
;
5301 struct btrfs_bio_stripe
*old
=
5304 new->physical
= old
->physical
;
5305 new->length
= old
->length
;
5306 new->dev
= dev_replace
->tgtdev
;
5307 index_where_to_add
++;
5311 num_stripes
= index_where_to_add
;
5312 } else if (dev_replace_is_ongoing
&& (rw
& REQ_GET_READ_MIRRORS
) &&
5313 dev_replace
->tgtdev
!= NULL
) {
5314 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
5315 int index_srcdev
= 0;
5317 u64 physical_of_found
= 0;
5320 * During the dev-replace procedure, the target drive can
5321 * also be used to read data in case it is needed to repair
5322 * a corrupt block elsewhere. This is possible if the
5323 * requested area is left of the left cursor. In this area,
5324 * the target drive is a full copy of the source drive.
5326 for (i
= 0; i
< num_stripes
; i
++) {
5327 if (bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
5329 * In case of DUP, in order to keep it
5330 * simple, only add the mirror with the
5331 * lowest physical address
5334 physical_of_found
<=
5335 bbio
->stripes
[i
].physical
)
5339 physical_of_found
= bbio
->stripes
[i
].physical
;
5343 u64 length
= map
->stripe_len
;
5345 if (physical_of_found
+ length
<=
5346 dev_replace
->cursor_left
) {
5347 struct btrfs_bio_stripe
*tgtdev_stripe
=
5348 bbio
->stripes
+ num_stripes
;
5350 tgtdev_stripe
->physical
= physical_of_found
;
5351 tgtdev_stripe
->length
=
5352 bbio
->stripes
[index_srcdev
].length
;
5353 tgtdev_stripe
->dev
= dev_replace
->tgtdev
;
5361 bbio
->num_stripes
= num_stripes
;
5362 bbio
->max_errors
= max_errors
;
5363 bbio
->mirror_num
= mirror_num
;
5366 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5367 * mirror_num == num_stripes + 1 && dev_replace target drive is
5368 * available as a mirror
5370 if (patch_the_first_stripe_for_dev_replace
&& num_stripes
> 0) {
5371 WARN_ON(num_stripes
> 1);
5372 bbio
->stripes
[0].dev
= dev_replace
->tgtdev
;
5373 bbio
->stripes
[0].physical
= physical_to_patch_in_first_stripe
;
5374 bbio
->mirror_num
= map
->num_stripes
+ 1;
5377 sort_parity_stripes(bbio
, raid_map
);
5378 *raid_map_ret
= raid_map
;
5381 if (dev_replace_is_ongoing
)
5382 btrfs_dev_replace_unlock(dev_replace
);
5383 free_extent_map(em
);
5387 int btrfs_map_block(struct btrfs_fs_info
*fs_info
, int rw
,
5388 u64 logical
, u64
*length
,
5389 struct btrfs_bio
**bbio_ret
, int mirror_num
)
5391 return __btrfs_map_block(fs_info
, rw
, logical
, length
, bbio_ret
,
5395 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
5396 u64 chunk_start
, u64 physical
, u64 devid
,
5397 u64
**logical
, int *naddrs
, int *stripe_len
)
5399 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
5400 struct extent_map
*em
;
5401 struct map_lookup
*map
;
5409 read_lock(&em_tree
->lock
);
5410 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
5411 read_unlock(&em_tree
->lock
);
5414 printk(KERN_ERR
"BTRFS: couldn't find em for chunk %Lu\n",
5419 if (em
->start
!= chunk_start
) {
5420 printk(KERN_ERR
"BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
5421 em
->start
, chunk_start
);
5422 free_extent_map(em
);
5425 map
= (struct map_lookup
*)em
->bdev
;
5428 rmap_len
= map
->stripe_len
;
5430 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
5431 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
5432 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
5433 do_div(length
, map
->num_stripes
);
5434 else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
5435 BTRFS_BLOCK_GROUP_RAID6
)) {
5436 do_div(length
, nr_data_stripes(map
));
5437 rmap_len
= map
->stripe_len
* nr_data_stripes(map
);
5440 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
5441 BUG_ON(!buf
); /* -ENOMEM */
5443 for (i
= 0; i
< map
->num_stripes
; i
++) {
5444 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
5446 if (map
->stripes
[i
].physical
> physical
||
5447 map
->stripes
[i
].physical
+ length
<= physical
)
5450 stripe_nr
= physical
- map
->stripes
[i
].physical
;
5451 do_div(stripe_nr
, map
->stripe_len
);
5453 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
5454 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
5455 do_div(stripe_nr
, map
->sub_stripes
);
5456 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
5457 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
5458 } /* else if RAID[56], multiply by nr_data_stripes().
5459 * Alternatively, just use rmap_len below instead of
5460 * map->stripe_len */
5462 bytenr
= chunk_start
+ stripe_nr
* rmap_len
;
5463 WARN_ON(nr
>= map
->num_stripes
);
5464 for (j
= 0; j
< nr
; j
++) {
5465 if (buf
[j
] == bytenr
)
5469 WARN_ON(nr
>= map
->num_stripes
);
5476 *stripe_len
= rmap_len
;
5478 free_extent_map(em
);
5482 static inline void btrfs_end_bbio(struct btrfs_bio
*bbio
, struct bio
*bio
, int err
)
5484 if (likely(bbio
->flags
& BTRFS_BIO_ORIG_BIO_SUBMITTED
))
5485 bio_endio_nodec(bio
, err
);
5487 bio_endio(bio
, err
);
5491 static void btrfs_end_bio(struct bio
*bio
, int err
)
5493 struct btrfs_bio
*bbio
= bio
->bi_private
;
5494 struct btrfs_device
*dev
= bbio
->stripes
[0].dev
;
5495 int is_orig_bio
= 0;
5498 atomic_inc(&bbio
->error
);
5499 if (err
== -EIO
|| err
== -EREMOTEIO
) {
5500 unsigned int stripe_index
=
5501 btrfs_io_bio(bio
)->stripe_index
;
5503 BUG_ON(stripe_index
>= bbio
->num_stripes
);
5504 dev
= bbio
->stripes
[stripe_index
].dev
;
5506 if (bio
->bi_rw
& WRITE
)
5507 btrfs_dev_stat_inc(dev
,
5508 BTRFS_DEV_STAT_WRITE_ERRS
);
5510 btrfs_dev_stat_inc(dev
,
5511 BTRFS_DEV_STAT_READ_ERRS
);
5512 if ((bio
->bi_rw
& WRITE_FLUSH
) == WRITE_FLUSH
)
5513 btrfs_dev_stat_inc(dev
,
5514 BTRFS_DEV_STAT_FLUSH_ERRS
);
5515 btrfs_dev_stat_print_on_error(dev
);
5520 if (bio
== bbio
->orig_bio
)
5523 btrfs_bio_counter_dec(bbio
->fs_info
);
5525 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
5528 bio
= bbio
->orig_bio
;
5531 bio
->bi_private
= bbio
->private;
5532 bio
->bi_end_io
= bbio
->end_io
;
5533 btrfs_io_bio(bio
)->mirror_num
= bbio
->mirror_num
;
5534 /* only send an error to the higher layers if it is
5535 * beyond the tolerance of the btrfs bio
5537 if (atomic_read(&bbio
->error
) > bbio
->max_errors
) {
5541 * this bio is actually up to date, we didn't
5542 * go over the max number of errors
5544 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
5548 btrfs_end_bbio(bbio
, bio
, err
);
5549 } else if (!is_orig_bio
) {
5555 * see run_scheduled_bios for a description of why bios are collected for
5558 * This will add one bio to the pending list for a device and make sure
5559 * the work struct is scheduled.
5561 static noinline
void btrfs_schedule_bio(struct btrfs_root
*root
,
5562 struct btrfs_device
*device
,
5563 int rw
, struct bio
*bio
)
5565 int should_queue
= 1;
5566 struct btrfs_pending_bios
*pending_bios
;
5568 if (device
->missing
|| !device
->bdev
) {
5569 bio_endio(bio
, -EIO
);
5573 /* don't bother with additional async steps for reads, right now */
5574 if (!(rw
& REQ_WRITE
)) {
5576 btrfsic_submit_bio(rw
, bio
);
5582 * nr_async_bios allows us to reliably return congestion to the
5583 * higher layers. Otherwise, the async bio makes it appear we have
5584 * made progress against dirty pages when we've really just put it
5585 * on a queue for later
5587 atomic_inc(&root
->fs_info
->nr_async_bios
);
5588 WARN_ON(bio
->bi_next
);
5589 bio
->bi_next
= NULL
;
5592 spin_lock(&device
->io_lock
);
5593 if (bio
->bi_rw
& REQ_SYNC
)
5594 pending_bios
= &device
->pending_sync_bios
;
5596 pending_bios
= &device
->pending_bios
;
5598 if (pending_bios
->tail
)
5599 pending_bios
->tail
->bi_next
= bio
;
5601 pending_bios
->tail
= bio
;
5602 if (!pending_bios
->head
)
5603 pending_bios
->head
= bio
;
5604 if (device
->running_pending
)
5607 spin_unlock(&device
->io_lock
);
5610 btrfs_queue_work(root
->fs_info
->submit_workers
,
5614 static int bio_size_ok(struct block_device
*bdev
, struct bio
*bio
,
5617 struct bio_vec
*prev
;
5618 struct request_queue
*q
= bdev_get_queue(bdev
);
5619 unsigned int max_sectors
= queue_max_sectors(q
);
5620 struct bvec_merge_data bvm
= {
5622 .bi_sector
= sector
,
5623 .bi_rw
= bio
->bi_rw
,
5626 if (WARN_ON(bio
->bi_vcnt
== 0))
5629 prev
= &bio
->bi_io_vec
[bio
->bi_vcnt
- 1];
5630 if (bio_sectors(bio
) > max_sectors
)
5633 if (!q
->merge_bvec_fn
)
5636 bvm
.bi_size
= bio
->bi_iter
.bi_size
- prev
->bv_len
;
5637 if (q
->merge_bvec_fn(q
, &bvm
, prev
) < prev
->bv_len
)
5642 static void submit_stripe_bio(struct btrfs_root
*root
, struct btrfs_bio
*bbio
,
5643 struct bio
*bio
, u64 physical
, int dev_nr
,
5646 struct btrfs_device
*dev
= bbio
->stripes
[dev_nr
].dev
;
5648 bio
->bi_private
= bbio
;
5649 btrfs_io_bio(bio
)->stripe_index
= dev_nr
;
5650 bio
->bi_end_io
= btrfs_end_bio
;
5651 bio
->bi_iter
.bi_sector
= physical
>> 9;
5654 struct rcu_string
*name
;
5657 name
= rcu_dereference(dev
->name
);
5658 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
5659 "(%s id %llu), size=%u\n", rw
,
5660 (u64
)bio
->bi_sector
, (u_long
)dev
->bdev
->bd_dev
,
5661 name
->str
, dev
->devid
, bio
->bi_size
);
5665 bio
->bi_bdev
= dev
->bdev
;
5667 btrfs_bio_counter_inc_noblocked(root
->fs_info
);
5670 btrfs_schedule_bio(root
, dev
, rw
, bio
);
5672 btrfsic_submit_bio(rw
, bio
);
5675 static int breakup_stripe_bio(struct btrfs_root
*root
, struct btrfs_bio
*bbio
,
5676 struct bio
*first_bio
, struct btrfs_device
*dev
,
5677 int dev_nr
, int rw
, int async
)
5679 struct bio_vec
*bvec
= first_bio
->bi_io_vec
;
5681 int nr_vecs
= bio_get_nr_vecs(dev
->bdev
);
5682 u64 physical
= bbio
->stripes
[dev_nr
].physical
;
5685 bio
= btrfs_bio_alloc(dev
->bdev
, physical
>> 9, nr_vecs
, GFP_NOFS
);
5689 while (bvec
<= (first_bio
->bi_io_vec
+ first_bio
->bi_vcnt
- 1)) {
5690 if (bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
5691 bvec
->bv_offset
) < bvec
->bv_len
) {
5692 u64 len
= bio
->bi_iter
.bi_size
;
5694 atomic_inc(&bbio
->stripes_pending
);
5695 submit_stripe_bio(root
, bbio
, bio
, physical
, dev_nr
,
5703 submit_stripe_bio(root
, bbio
, bio
, physical
, dev_nr
, rw
, async
);
5707 static void bbio_error(struct btrfs_bio
*bbio
, struct bio
*bio
, u64 logical
)
5709 atomic_inc(&bbio
->error
);
5710 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
5711 /* Shoud be the original bio. */
5712 WARN_ON(bio
!= bbio
->orig_bio
);
5714 bio
->bi_private
= bbio
->private;
5715 bio
->bi_end_io
= bbio
->end_io
;
5716 btrfs_io_bio(bio
)->mirror_num
= bbio
->mirror_num
;
5717 bio
->bi_iter
.bi_sector
= logical
>> 9;
5719 btrfs_end_bbio(bbio
, bio
, -EIO
);
5723 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
5724 int mirror_num
, int async_submit
)
5726 struct btrfs_device
*dev
;
5727 struct bio
*first_bio
= bio
;
5728 u64 logical
= (u64
)bio
->bi_iter
.bi_sector
<< 9;
5731 u64
*raid_map
= NULL
;
5735 struct btrfs_bio
*bbio
= NULL
;
5737 length
= bio
->bi_iter
.bi_size
;
5738 map_length
= length
;
5740 btrfs_bio_counter_inc_blocked(root
->fs_info
);
5741 ret
= __btrfs_map_block(root
->fs_info
, rw
, logical
, &map_length
, &bbio
,
5742 mirror_num
, &raid_map
);
5744 btrfs_bio_counter_dec(root
->fs_info
);
5748 total_devs
= bbio
->num_stripes
;
5749 bbio
->orig_bio
= first_bio
;
5750 bbio
->private = first_bio
->bi_private
;
5751 bbio
->end_io
= first_bio
->bi_end_io
;
5752 bbio
->fs_info
= root
->fs_info
;
5753 atomic_set(&bbio
->stripes_pending
, bbio
->num_stripes
);
5756 /* In this case, map_length has been set to the length of
5757 a single stripe; not the whole write */
5759 ret
= raid56_parity_write(root
, bio
, bbio
,
5760 raid_map
, map_length
);
5762 ret
= raid56_parity_recover(root
, bio
, bbio
,
5763 raid_map
, map_length
,
5767 * FIXME, replace dosen't support raid56 yet, please fix
5770 btrfs_bio_counter_dec(root
->fs_info
);
5774 if (map_length
< length
) {
5775 btrfs_crit(root
->fs_info
, "mapping failed logical %llu bio len %llu len %llu",
5776 logical
, length
, map_length
);
5780 while (dev_nr
< total_devs
) {
5781 dev
= bbio
->stripes
[dev_nr
].dev
;
5782 if (!dev
|| !dev
->bdev
|| (rw
& WRITE
&& !dev
->writeable
)) {
5783 bbio_error(bbio
, first_bio
, logical
);
5789 * Check and see if we're ok with this bio based on it's size
5790 * and offset with the given device.
5792 if (!bio_size_ok(dev
->bdev
, first_bio
,
5793 bbio
->stripes
[dev_nr
].physical
>> 9)) {
5794 ret
= breakup_stripe_bio(root
, bbio
, first_bio
, dev
,
5795 dev_nr
, rw
, async_submit
);
5801 if (dev_nr
< total_devs
- 1) {
5802 bio
= btrfs_bio_clone(first_bio
, GFP_NOFS
);
5803 BUG_ON(!bio
); /* -ENOMEM */
5806 bbio
->flags
|= BTRFS_BIO_ORIG_BIO_SUBMITTED
;
5809 submit_stripe_bio(root
, bbio
, bio
,
5810 bbio
->stripes
[dev_nr
].physical
, dev_nr
, rw
,
5814 btrfs_bio_counter_dec(root
->fs_info
);
5818 struct btrfs_device
*btrfs_find_device(struct btrfs_fs_info
*fs_info
, u64 devid
,
5821 struct btrfs_device
*device
;
5822 struct btrfs_fs_devices
*cur_devices
;
5824 cur_devices
= fs_info
->fs_devices
;
5825 while (cur_devices
) {
5827 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
5828 device
= __find_device(&cur_devices
->devices
,
5833 cur_devices
= cur_devices
->seed
;
5838 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
5839 u64 devid
, u8
*dev_uuid
)
5841 struct btrfs_device
*device
;
5842 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
5844 device
= btrfs_alloc_device(NULL
, &devid
, dev_uuid
);
5848 list_add(&device
->dev_list
, &fs_devices
->devices
);
5849 device
->fs_devices
= fs_devices
;
5850 fs_devices
->num_devices
++;
5852 device
->missing
= 1;
5853 fs_devices
->missing_devices
++;
5859 * btrfs_alloc_device - allocate struct btrfs_device
5860 * @fs_info: used only for generating a new devid, can be NULL if
5861 * devid is provided (i.e. @devid != NULL).
5862 * @devid: a pointer to devid for this device. If NULL a new devid
5864 * @uuid: a pointer to UUID for this device. If NULL a new UUID
5867 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
5868 * on error. Returned struct is not linked onto any lists and can be
5869 * destroyed with kfree() right away.
5871 struct btrfs_device
*btrfs_alloc_device(struct btrfs_fs_info
*fs_info
,
5875 struct btrfs_device
*dev
;
5878 if (WARN_ON(!devid
&& !fs_info
))
5879 return ERR_PTR(-EINVAL
);
5881 dev
= __alloc_device();
5890 ret
= find_next_devid(fs_info
, &tmp
);
5893 return ERR_PTR(ret
);
5899 memcpy(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
);
5901 generate_random_uuid(dev
->uuid
);
5903 btrfs_init_work(&dev
->work
, btrfs_submit_helper
,
5904 pending_bios_fn
, NULL
, NULL
);
5909 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
5910 struct extent_buffer
*leaf
,
5911 struct btrfs_chunk
*chunk
)
5913 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
5914 struct map_lookup
*map
;
5915 struct extent_map
*em
;
5919 u8 uuid
[BTRFS_UUID_SIZE
];
5924 logical
= key
->offset
;
5925 length
= btrfs_chunk_length(leaf
, chunk
);
5927 read_lock(&map_tree
->map_tree
.lock
);
5928 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
5929 read_unlock(&map_tree
->map_tree
.lock
);
5931 /* already mapped? */
5932 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
5933 free_extent_map(em
);
5936 free_extent_map(em
);
5939 em
= alloc_extent_map();
5942 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
5943 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
5945 free_extent_map(em
);
5949 set_bit(EXTENT_FLAG_FS_MAPPING
, &em
->flags
);
5950 em
->bdev
= (struct block_device
*)map
;
5951 em
->start
= logical
;
5954 em
->block_start
= 0;
5955 em
->block_len
= em
->len
;
5957 map
->num_stripes
= num_stripes
;
5958 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
5959 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
5960 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
5961 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
5962 map
->type
= btrfs_chunk_type(leaf
, chunk
);
5963 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
5964 for (i
= 0; i
< num_stripes
; i
++) {
5965 map
->stripes
[i
].physical
=
5966 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
5967 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
5968 read_extent_buffer(leaf
, uuid
, (unsigned long)
5969 btrfs_stripe_dev_uuid_nr(chunk
, i
),
5971 map
->stripes
[i
].dev
= btrfs_find_device(root
->fs_info
, devid
,
5973 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
5974 free_extent_map(em
);
5977 if (!map
->stripes
[i
].dev
) {
5978 map
->stripes
[i
].dev
=
5979 add_missing_dev(root
, devid
, uuid
);
5980 if (!map
->stripes
[i
].dev
) {
5981 free_extent_map(em
);
5985 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
5988 write_lock(&map_tree
->map_tree
.lock
);
5989 ret
= add_extent_mapping(&map_tree
->map_tree
, em
, 0);
5990 write_unlock(&map_tree
->map_tree
.lock
);
5991 BUG_ON(ret
); /* Tree corruption */
5992 free_extent_map(em
);
5997 static void fill_device_from_item(struct extent_buffer
*leaf
,
5998 struct btrfs_dev_item
*dev_item
,
5999 struct btrfs_device
*device
)
6003 device
->devid
= btrfs_device_id(leaf
, dev_item
);
6004 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
6005 device
->total_bytes
= device
->disk_total_bytes
;
6006 device
->commit_total_bytes
= device
->disk_total_bytes
;
6007 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
6008 device
->commit_bytes_used
= device
->bytes_used
;
6009 device
->type
= btrfs_device_type(leaf
, dev_item
);
6010 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
6011 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
6012 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
6013 WARN_ON(device
->devid
== BTRFS_DEV_REPLACE_DEVID
);
6014 device
->is_tgtdev_for_dev_replace
= 0;
6016 ptr
= btrfs_device_uuid(dev_item
);
6017 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
6020 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
6022 struct btrfs_fs_devices
*fs_devices
;
6025 BUG_ON(!mutex_is_locked(&uuid_mutex
));
6027 fs_devices
= root
->fs_info
->fs_devices
->seed
;
6028 while (fs_devices
) {
6029 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
6033 fs_devices
= fs_devices
->seed
;
6036 fs_devices
= find_fsid(fsid
);
6042 fs_devices
= clone_fs_devices(fs_devices
);
6043 if (IS_ERR(fs_devices
)) {
6044 ret
= PTR_ERR(fs_devices
);
6048 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
6049 root
->fs_info
->bdev_holder
);
6051 free_fs_devices(fs_devices
);
6055 if (!fs_devices
->seeding
) {
6056 __btrfs_close_devices(fs_devices
);
6057 free_fs_devices(fs_devices
);
6062 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
6063 root
->fs_info
->fs_devices
->seed
= fs_devices
;
6068 static int read_one_dev(struct btrfs_root
*root
,
6069 struct extent_buffer
*leaf
,
6070 struct btrfs_dev_item
*dev_item
)
6072 struct btrfs_device
*device
;
6075 u8 fs_uuid
[BTRFS_UUID_SIZE
];
6076 u8 dev_uuid
[BTRFS_UUID_SIZE
];
6078 devid
= btrfs_device_id(leaf
, dev_item
);
6079 read_extent_buffer(leaf
, dev_uuid
, btrfs_device_uuid(dev_item
),
6081 read_extent_buffer(leaf
, fs_uuid
, btrfs_device_fsid(dev_item
),
6084 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
6085 ret
= open_seed_devices(root
, fs_uuid
);
6086 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
6090 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
, fs_uuid
);
6091 if (!device
|| !device
->bdev
) {
6092 if (!btrfs_test_opt(root
, DEGRADED
))
6096 btrfs_warn(root
->fs_info
, "devid %llu missing", devid
);
6097 device
= add_missing_dev(root
, devid
, dev_uuid
);
6100 } else if (!device
->missing
) {
6102 * this happens when a device that was properly setup
6103 * in the device info lists suddenly goes bad.
6104 * device->bdev is NULL, and so we have to set
6105 * device->missing to one here
6107 root
->fs_info
->fs_devices
->missing_devices
++;
6108 device
->missing
= 1;
6112 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
6113 BUG_ON(device
->writeable
);
6114 if (device
->generation
!=
6115 btrfs_device_generation(leaf
, dev_item
))
6119 fill_device_from_item(leaf
, dev_item
, device
);
6120 device
->in_fs_metadata
= 1;
6121 if (device
->writeable
&& !device
->is_tgtdev_for_dev_replace
) {
6122 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
6123 spin_lock(&root
->fs_info
->free_chunk_lock
);
6124 root
->fs_info
->free_chunk_space
+= device
->total_bytes
-
6126 spin_unlock(&root
->fs_info
->free_chunk_lock
);
6132 int btrfs_read_sys_array(struct btrfs_root
*root
)
6134 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
6135 struct extent_buffer
*sb
;
6136 struct btrfs_disk_key
*disk_key
;
6137 struct btrfs_chunk
*chunk
;
6139 unsigned long sb_ptr
;
6145 struct btrfs_key key
;
6147 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
6148 BTRFS_SUPER_INFO_SIZE
);
6151 btrfs_set_buffer_uptodate(sb
);
6152 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, sb
, 0);
6154 * The sb extent buffer is artifical and just used to read the system array.
6155 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6156 * pages up-to-date when the page is larger: extent does not cover the
6157 * whole page and consequently check_page_uptodate does not find all
6158 * the page's extents up-to-date (the hole beyond sb),
6159 * write_extent_buffer then triggers a WARN_ON.
6161 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6162 * but sb spans only this function. Add an explicit SetPageUptodate call
6163 * to silence the warning eg. on PowerPC 64.
6165 if (PAGE_CACHE_SIZE
> BTRFS_SUPER_INFO_SIZE
)
6166 SetPageUptodate(sb
->pages
[0]);
6168 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
6169 array_size
= btrfs_super_sys_array_size(super_copy
);
6171 ptr
= super_copy
->sys_chunk_array
;
6172 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
6175 while (cur
< array_size
) {
6176 disk_key
= (struct btrfs_disk_key
*)ptr
;
6177 btrfs_disk_key_to_cpu(&key
, disk_key
);
6179 len
= sizeof(*disk_key
); ptr
+= len
;
6183 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
6184 chunk
= (struct btrfs_chunk
*)sb_ptr
;
6185 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
6188 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
6189 len
= btrfs_chunk_item_size(num_stripes
);
6198 free_extent_buffer(sb
);
6202 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
6204 struct btrfs_path
*path
;
6205 struct extent_buffer
*leaf
;
6206 struct btrfs_key key
;
6207 struct btrfs_key found_key
;
6211 root
= root
->fs_info
->chunk_root
;
6213 path
= btrfs_alloc_path();
6217 mutex_lock(&uuid_mutex
);
6221 * Read all device items, and then all the chunk items. All
6222 * device items are found before any chunk item (their object id
6223 * is smaller than the lowest possible object id for a chunk
6224 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
6226 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
6229 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
6233 leaf
= path
->nodes
[0];
6234 slot
= path
->slots
[0];
6235 if (slot
>= btrfs_header_nritems(leaf
)) {
6236 ret
= btrfs_next_leaf(root
, path
);
6243 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
6244 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
6245 struct btrfs_dev_item
*dev_item
;
6246 dev_item
= btrfs_item_ptr(leaf
, slot
,
6247 struct btrfs_dev_item
);
6248 ret
= read_one_dev(root
, leaf
, dev_item
);
6251 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
6252 struct btrfs_chunk
*chunk
;
6253 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
6254 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
6262 unlock_chunks(root
);
6263 mutex_unlock(&uuid_mutex
);
6265 btrfs_free_path(path
);
6269 void btrfs_init_devices_late(struct btrfs_fs_info
*fs_info
)
6271 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
6272 struct btrfs_device
*device
;
6274 while (fs_devices
) {
6275 mutex_lock(&fs_devices
->device_list_mutex
);
6276 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
)
6277 device
->dev_root
= fs_info
->dev_root
;
6278 mutex_unlock(&fs_devices
->device_list_mutex
);
6280 fs_devices
= fs_devices
->seed
;
6284 static void __btrfs_reset_dev_stats(struct btrfs_device
*dev
)
6288 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
6289 btrfs_dev_stat_reset(dev
, i
);
6292 int btrfs_init_dev_stats(struct btrfs_fs_info
*fs_info
)
6294 struct btrfs_key key
;
6295 struct btrfs_key found_key
;
6296 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
6297 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
6298 struct extent_buffer
*eb
;
6301 struct btrfs_device
*device
;
6302 struct btrfs_path
*path
= NULL
;
6305 path
= btrfs_alloc_path();
6311 mutex_lock(&fs_devices
->device_list_mutex
);
6312 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
6314 struct btrfs_dev_stats_item
*ptr
;
6317 key
.type
= BTRFS_DEV_STATS_KEY
;
6318 key
.offset
= device
->devid
;
6319 ret
= btrfs_search_slot(NULL
, dev_root
, &key
, path
, 0, 0);
6321 __btrfs_reset_dev_stats(device
);
6322 device
->dev_stats_valid
= 1;
6323 btrfs_release_path(path
);
6326 slot
= path
->slots
[0];
6327 eb
= path
->nodes
[0];
6328 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
6329 item_size
= btrfs_item_size_nr(eb
, slot
);
6331 ptr
= btrfs_item_ptr(eb
, slot
,
6332 struct btrfs_dev_stats_item
);
6334 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
6335 if (item_size
>= (1 + i
) * sizeof(__le64
))
6336 btrfs_dev_stat_set(device
, i
,
6337 btrfs_dev_stats_value(eb
, ptr
, i
));
6339 btrfs_dev_stat_reset(device
, i
);
6342 device
->dev_stats_valid
= 1;
6343 btrfs_dev_stat_print_on_load(device
);
6344 btrfs_release_path(path
);
6346 mutex_unlock(&fs_devices
->device_list_mutex
);
6349 btrfs_free_path(path
);
6350 return ret
< 0 ? ret
: 0;
6353 static int update_dev_stat_item(struct btrfs_trans_handle
*trans
,
6354 struct btrfs_root
*dev_root
,
6355 struct btrfs_device
*device
)
6357 struct btrfs_path
*path
;
6358 struct btrfs_key key
;
6359 struct extent_buffer
*eb
;
6360 struct btrfs_dev_stats_item
*ptr
;
6365 key
.type
= BTRFS_DEV_STATS_KEY
;
6366 key
.offset
= device
->devid
;
6368 path
= btrfs_alloc_path();
6370 ret
= btrfs_search_slot(trans
, dev_root
, &key
, path
, -1, 1);
6372 printk_in_rcu(KERN_WARNING
"BTRFS: "
6373 "error %d while searching for dev_stats item for device %s!\n",
6374 ret
, rcu_str_deref(device
->name
));
6379 btrfs_item_size_nr(path
->nodes
[0], path
->slots
[0]) < sizeof(*ptr
)) {
6380 /* need to delete old one and insert a new one */
6381 ret
= btrfs_del_item(trans
, dev_root
, path
);
6383 printk_in_rcu(KERN_WARNING
"BTRFS: "
6384 "delete too small dev_stats item for device %s failed %d!\n",
6385 rcu_str_deref(device
->name
), ret
);
6392 /* need to insert a new item */
6393 btrfs_release_path(path
);
6394 ret
= btrfs_insert_empty_item(trans
, dev_root
, path
,
6395 &key
, sizeof(*ptr
));
6397 printk_in_rcu(KERN_WARNING
"BTRFS: "
6398 "insert dev_stats item for device %s failed %d!\n",
6399 rcu_str_deref(device
->name
), ret
);
6404 eb
= path
->nodes
[0];
6405 ptr
= btrfs_item_ptr(eb
, path
->slots
[0], struct btrfs_dev_stats_item
);
6406 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
6407 btrfs_set_dev_stats_value(eb
, ptr
, i
,
6408 btrfs_dev_stat_read(device
, i
));
6409 btrfs_mark_buffer_dirty(eb
);
6412 btrfs_free_path(path
);
6417 * called from commit_transaction. Writes all changed device stats to disk.
6419 int btrfs_run_dev_stats(struct btrfs_trans_handle
*trans
,
6420 struct btrfs_fs_info
*fs_info
)
6422 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
6423 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
6424 struct btrfs_device
*device
;
6428 mutex_lock(&fs_devices
->device_list_mutex
);
6429 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
6430 if (!device
->dev_stats_valid
|| !btrfs_dev_stats_dirty(device
))
6433 stats_cnt
= atomic_read(&device
->dev_stats_ccnt
);
6434 ret
= update_dev_stat_item(trans
, dev_root
, device
);
6436 atomic_sub(stats_cnt
, &device
->dev_stats_ccnt
);
6438 mutex_unlock(&fs_devices
->device_list_mutex
);
6443 void btrfs_dev_stat_inc_and_print(struct btrfs_device
*dev
, int index
)
6445 btrfs_dev_stat_inc(dev
, index
);
6446 btrfs_dev_stat_print_on_error(dev
);
6449 static void btrfs_dev_stat_print_on_error(struct btrfs_device
*dev
)
6451 if (!dev
->dev_stats_valid
)
6453 printk_ratelimited_in_rcu(KERN_ERR
"BTRFS: "
6454 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
6455 rcu_str_deref(dev
->name
),
6456 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
6457 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
6458 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
6459 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_CORRUPTION_ERRS
),
6460 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_GENERATION_ERRS
));
6463 static void btrfs_dev_stat_print_on_load(struct btrfs_device
*dev
)
6467 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
6468 if (btrfs_dev_stat_read(dev
, i
) != 0)
6470 if (i
== BTRFS_DEV_STAT_VALUES_MAX
)
6471 return; /* all values == 0, suppress message */
6473 printk_in_rcu(KERN_INFO
"BTRFS: "
6474 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
6475 rcu_str_deref(dev
->name
),
6476 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
6477 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
6478 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
6479 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_CORRUPTION_ERRS
),
6480 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_GENERATION_ERRS
));
6483 int btrfs_get_dev_stats(struct btrfs_root
*root
,
6484 struct btrfs_ioctl_get_dev_stats
*stats
)
6486 struct btrfs_device
*dev
;
6487 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
6490 mutex_lock(&fs_devices
->device_list_mutex
);
6491 dev
= btrfs_find_device(root
->fs_info
, stats
->devid
, NULL
, NULL
);
6492 mutex_unlock(&fs_devices
->device_list_mutex
);
6495 btrfs_warn(root
->fs_info
, "get dev_stats failed, device not found");
6497 } else if (!dev
->dev_stats_valid
) {
6498 btrfs_warn(root
->fs_info
, "get dev_stats failed, not yet valid");
6500 } else if (stats
->flags
& BTRFS_DEV_STATS_RESET
) {
6501 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
6502 if (stats
->nr_items
> i
)
6504 btrfs_dev_stat_read_and_reset(dev
, i
);
6506 btrfs_dev_stat_reset(dev
, i
);
6509 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
6510 if (stats
->nr_items
> i
)
6511 stats
->values
[i
] = btrfs_dev_stat_read(dev
, i
);
6513 if (stats
->nr_items
> BTRFS_DEV_STAT_VALUES_MAX
)
6514 stats
->nr_items
= BTRFS_DEV_STAT_VALUES_MAX
;
6518 int btrfs_scratch_superblock(struct btrfs_device
*device
)
6520 struct buffer_head
*bh
;
6521 struct btrfs_super_block
*disk_super
;
6523 bh
= btrfs_read_dev_super(device
->bdev
);
6526 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
6528 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
6529 set_buffer_dirty(bh
);
6530 sync_dirty_buffer(bh
);
6537 * Update the size of all devices, which is used for writing out the
6540 void btrfs_update_commit_device_size(struct btrfs_fs_info
*fs_info
)
6542 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
6543 struct btrfs_device
*curr
, *next
;
6545 if (list_empty(&fs_devices
->resized_devices
))
6548 mutex_lock(&fs_devices
->device_list_mutex
);
6549 lock_chunks(fs_info
->dev_root
);
6550 list_for_each_entry_safe(curr
, next
, &fs_devices
->resized_devices
,
6552 list_del_init(&curr
->resized_list
);
6553 curr
->commit_total_bytes
= curr
->disk_total_bytes
;
6555 unlock_chunks(fs_info
->dev_root
);
6556 mutex_unlock(&fs_devices
->device_list_mutex
);
6559 /* Must be invoked during the transaction commit */
6560 void btrfs_update_commit_device_bytes_used(struct btrfs_root
*root
,
6561 struct btrfs_transaction
*transaction
)
6563 struct extent_map
*em
;
6564 struct map_lookup
*map
;
6565 struct btrfs_device
*dev
;
6568 if (list_empty(&transaction
->pending_chunks
))
6571 /* In order to kick the device replace finish process */
6573 list_for_each_entry(em
, &transaction
->pending_chunks
, list
) {
6574 map
= (struct map_lookup
*)em
->bdev
;
6576 for (i
= 0; i
< map
->num_stripes
; i
++) {
6577 dev
= map
->stripes
[i
].dev
;
6578 dev
->commit_bytes_used
= dev
->bytes_used
;
6581 unlock_chunks(root
);