1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
6 #include <linux/sched.h>
8 #include <linux/slab.h>
9 #include <linux/buffer_head.h>
10 #include <linux/blkdev.h>
11 #include <linux/ratelimit.h>
12 #include <linux/kthread.h>
13 #include <linux/raid/pq.h>
14 #include <linux/semaphore.h>
15 #include <linux/uuid.h>
16 #include <linux/list_sort.h>
18 #include "extent_map.h"
20 #include "transaction.h"
21 #include "print-tree.h"
24 #include "async-thread.h"
25 #include "check-integrity.h"
26 #include "rcu-string.h"
28 #include "dev-replace.h"
31 const struct btrfs_raid_attr btrfs_raid_array
[BTRFS_NR_RAID_TYPES
] = {
32 [BTRFS_RAID_RAID10
] = {
35 .devs_max
= 0, /* 0 == as many as possible */
37 .tolerated_failures
= 1,
40 .raid_name
= "raid10",
41 .bg_flag
= BTRFS_BLOCK_GROUP_RAID10
,
42 .mindev_error
= BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET
,
44 [BTRFS_RAID_RAID1
] = {
49 .tolerated_failures
= 1,
53 .bg_flag
= BTRFS_BLOCK_GROUP_RAID1
,
54 .mindev_error
= BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET
,
61 .tolerated_failures
= 0,
65 .bg_flag
= BTRFS_BLOCK_GROUP_DUP
,
68 [BTRFS_RAID_RAID0
] = {
73 .tolerated_failures
= 0,
77 .bg_flag
= BTRFS_BLOCK_GROUP_RAID0
,
80 [BTRFS_RAID_SINGLE
] = {
85 .tolerated_failures
= 0,
88 .raid_name
= "single",
92 [BTRFS_RAID_RAID5
] = {
97 .tolerated_failures
= 1,
100 .raid_name
= "raid5",
101 .bg_flag
= BTRFS_BLOCK_GROUP_RAID5
,
102 .mindev_error
= BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET
,
104 [BTRFS_RAID_RAID6
] = {
109 .tolerated_failures
= 2,
112 .raid_name
= "raid6",
113 .bg_flag
= BTRFS_BLOCK_GROUP_RAID6
,
114 .mindev_error
= BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET
,
118 const char *get_raid_name(enum btrfs_raid_types type
)
120 if (type
>= BTRFS_NR_RAID_TYPES
)
123 return btrfs_raid_array
[type
].raid_name
;
126 static int init_first_rw_device(struct btrfs_trans_handle
*trans
,
127 struct btrfs_fs_info
*fs_info
);
128 static int btrfs_relocate_sys_chunks(struct btrfs_fs_info
*fs_info
);
129 static void __btrfs_reset_dev_stats(struct btrfs_device
*dev
);
130 static void btrfs_dev_stat_print_on_error(struct btrfs_device
*dev
);
131 static void btrfs_dev_stat_print_on_load(struct btrfs_device
*device
);
132 static int __btrfs_map_block(struct btrfs_fs_info
*fs_info
,
133 enum btrfs_map_op op
,
134 u64 logical
, u64
*length
,
135 struct btrfs_bio
**bbio_ret
,
136 int mirror_num
, int need_raid_map
);
142 * There are several mutexes that protect manipulation of devices and low-level
143 * structures like chunks but not block groups, extents or files
145 * uuid_mutex (global lock)
146 * ------------------------
147 * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from
148 * the SCAN_DEV ioctl registration or from mount either implicitly (the first
149 * device) or requested by the device= mount option
151 * the mutex can be very coarse and can cover long-running operations
153 * protects: updates to fs_devices counters like missing devices, rw devices,
154 * seeding, structure cloning, openning/closing devices at mount/umount time
156 * global::fs_devs - add, remove, updates to the global list
158 * does not protect: manipulation of the fs_devices::devices list!
160 * btrfs_device::name - renames (write side), read is RCU
162 * fs_devices::device_list_mutex (per-fs, with RCU)
163 * ------------------------------------------------
164 * protects updates to fs_devices::devices, ie. adding and deleting
166 * simple list traversal with read-only actions can be done with RCU protection
168 * may be used to exclude some operations from running concurrently without any
169 * modifications to the list (see write_all_supers)
173 * protects balance structures (status, state) and context accessed from
174 * several places (internally, ioctl)
178 * protects chunks, adding or removing during allocation, trim or when a new
179 * device is added/removed
183 * a big lock that is held by the cleaner thread and prevents running subvolume
184 * cleaning together with relocation or delayed iputs
197 * Exclusive operations, BTRFS_FS_EXCL_OP
198 * ======================================
200 * Maintains the exclusivity of the following operations that apply to the
201 * whole filesystem and cannot run in parallel.
206 * - Device replace (*)
209 * The device operations (as above) can be in one of the following states:
215 * Only device operations marked with (*) can go into the Paused state for the
218 * - ioctl (only Balance can be Paused through ioctl)
219 * - filesystem remounted as read-only
220 * - filesystem unmounted and mounted as read-only
221 * - system power-cycle and filesystem mounted as read-only
222 * - filesystem or device errors leading to forced read-only
224 * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations.
225 * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set.
226 * A device operation in Paused or Running state can be canceled or resumed
227 * either by ioctl (Balance only) or when remounted as read-write.
228 * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or
232 DEFINE_MUTEX(uuid_mutex
);
233 static LIST_HEAD(fs_uuids
);
234 struct list_head
*btrfs_get_fs_uuids(void)
240 * alloc_fs_devices - allocate struct btrfs_fs_devices
241 * @fsid: if not NULL, copy the uuid to fs_devices::fsid
243 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
244 * The returned struct is not linked onto any lists and can be destroyed with
245 * kfree() right away.
247 static struct btrfs_fs_devices
*alloc_fs_devices(const u8
*fsid
)
249 struct btrfs_fs_devices
*fs_devs
;
251 fs_devs
= kzalloc(sizeof(*fs_devs
), GFP_KERNEL
);
253 return ERR_PTR(-ENOMEM
);
255 mutex_init(&fs_devs
->device_list_mutex
);
257 INIT_LIST_HEAD(&fs_devs
->devices
);
258 INIT_LIST_HEAD(&fs_devs
->resized_devices
);
259 INIT_LIST_HEAD(&fs_devs
->alloc_list
);
260 INIT_LIST_HEAD(&fs_devs
->fs_list
);
262 memcpy(fs_devs
->fsid
, fsid
, BTRFS_FSID_SIZE
);
267 void btrfs_free_device(struct btrfs_device
*device
)
269 rcu_string_free(device
->name
);
270 bio_put(device
->flush_bio
);
274 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
276 struct btrfs_device
*device
;
277 WARN_ON(fs_devices
->opened
);
278 while (!list_empty(&fs_devices
->devices
)) {
279 device
= list_entry(fs_devices
->devices
.next
,
280 struct btrfs_device
, dev_list
);
281 list_del(&device
->dev_list
);
282 btrfs_free_device(device
);
287 static void btrfs_kobject_uevent(struct block_device
*bdev
,
288 enum kobject_action action
)
292 ret
= kobject_uevent(&disk_to_dev(bdev
->bd_disk
)->kobj
, action
);
294 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
296 kobject_name(&disk_to_dev(bdev
->bd_disk
)->kobj
),
297 &disk_to_dev(bdev
->bd_disk
)->kobj
);
300 void __exit
btrfs_cleanup_fs_uuids(void)
302 struct btrfs_fs_devices
*fs_devices
;
304 while (!list_empty(&fs_uuids
)) {
305 fs_devices
= list_entry(fs_uuids
.next
,
306 struct btrfs_fs_devices
, fs_list
);
307 list_del(&fs_devices
->fs_list
);
308 free_fs_devices(fs_devices
);
313 * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
314 * Returned struct is not linked onto any lists and must be destroyed using
317 static struct btrfs_device
*__alloc_device(void)
319 struct btrfs_device
*dev
;
321 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
323 return ERR_PTR(-ENOMEM
);
326 * Preallocate a bio that's always going to be used for flushing device
327 * barriers and matches the device lifespan
329 dev
->flush_bio
= bio_alloc_bioset(GFP_KERNEL
, 0, NULL
);
330 if (!dev
->flush_bio
) {
332 return ERR_PTR(-ENOMEM
);
335 INIT_LIST_HEAD(&dev
->dev_list
);
336 INIT_LIST_HEAD(&dev
->dev_alloc_list
);
337 INIT_LIST_HEAD(&dev
->resized_list
);
339 spin_lock_init(&dev
->io_lock
);
341 atomic_set(&dev
->reada_in_flight
, 0);
342 atomic_set(&dev
->dev_stats_ccnt
, 0);
343 btrfs_device_data_ordered_init(dev
);
344 INIT_RADIX_TREE(&dev
->reada_zones
, GFP_NOFS
& ~__GFP_DIRECT_RECLAIM
);
345 INIT_RADIX_TREE(&dev
->reada_extents
, GFP_NOFS
& ~__GFP_DIRECT_RECLAIM
);
351 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
354 * If devid and uuid are both specified, the match must be exact, otherwise
355 * only devid is used.
357 static struct btrfs_device
*find_device(struct btrfs_fs_devices
*fs_devices
,
358 u64 devid
, const u8
*uuid
)
360 struct btrfs_device
*dev
;
362 list_for_each_entry(dev
, &fs_devices
->devices
, dev_list
) {
363 if (dev
->devid
== devid
&&
364 (!uuid
|| !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
))) {
371 static noinline
struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
373 struct btrfs_fs_devices
*fs_devices
;
375 list_for_each_entry(fs_devices
, &fs_uuids
, fs_list
) {
376 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
383 btrfs_get_bdev_and_sb(const char *device_path
, fmode_t flags
, void *holder
,
384 int flush
, struct block_device
**bdev
,
385 struct buffer_head
**bh
)
389 *bdev
= blkdev_get_by_path(device_path
, flags
, holder
);
392 ret
= PTR_ERR(*bdev
);
397 filemap_write_and_wait((*bdev
)->bd_inode
->i_mapping
);
398 ret
= set_blocksize(*bdev
, BTRFS_BDEV_BLOCKSIZE
);
400 blkdev_put(*bdev
, flags
);
403 invalidate_bdev(*bdev
);
404 *bh
= btrfs_read_dev_super(*bdev
);
407 blkdev_put(*bdev
, flags
);
419 static void requeue_list(struct btrfs_pending_bios
*pending_bios
,
420 struct bio
*head
, struct bio
*tail
)
423 struct bio
*old_head
;
425 old_head
= pending_bios
->head
;
426 pending_bios
->head
= head
;
427 if (pending_bios
->tail
)
428 tail
->bi_next
= old_head
;
430 pending_bios
->tail
= tail
;
434 * we try to collect pending bios for a device so we don't get a large
435 * number of procs sending bios down to the same device. This greatly
436 * improves the schedulers ability to collect and merge the bios.
438 * But, it also turns into a long list of bios to process and that is sure
439 * to eventually make the worker thread block. The solution here is to
440 * make some progress and then put this work struct back at the end of
441 * the list if the block device is congested. This way, multiple devices
442 * can make progress from a single worker thread.
444 static noinline
void run_scheduled_bios(struct btrfs_device
*device
)
446 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
448 struct backing_dev_info
*bdi
;
449 struct btrfs_pending_bios
*pending_bios
;
453 unsigned long num_run
;
454 unsigned long batch_run
= 0;
455 unsigned long last_waited
= 0;
457 int sync_pending
= 0;
458 struct blk_plug plug
;
461 * this function runs all the bios we've collected for
462 * a particular device. We don't want to wander off to
463 * another device without first sending all of these down.
464 * So, setup a plug here and finish it off before we return
466 blk_start_plug(&plug
);
468 bdi
= device
->bdev
->bd_bdi
;
471 spin_lock(&device
->io_lock
);
476 /* take all the bios off the list at once and process them
477 * later on (without the lock held). But, remember the
478 * tail and other pointers so the bios can be properly reinserted
479 * into the list if we hit congestion
481 if (!force_reg
&& device
->pending_sync_bios
.head
) {
482 pending_bios
= &device
->pending_sync_bios
;
485 pending_bios
= &device
->pending_bios
;
489 pending
= pending_bios
->head
;
490 tail
= pending_bios
->tail
;
491 WARN_ON(pending
&& !tail
);
494 * if pending was null this time around, no bios need processing
495 * at all and we can stop. Otherwise it'll loop back up again
496 * and do an additional check so no bios are missed.
498 * device->running_pending is used to synchronize with the
501 if (device
->pending_sync_bios
.head
== NULL
&&
502 device
->pending_bios
.head
== NULL
) {
504 device
->running_pending
= 0;
507 device
->running_pending
= 1;
510 pending_bios
->head
= NULL
;
511 pending_bios
->tail
= NULL
;
513 spin_unlock(&device
->io_lock
);
518 /* we want to work on both lists, but do more bios on the
519 * sync list than the regular list
522 pending_bios
!= &device
->pending_sync_bios
&&
523 device
->pending_sync_bios
.head
) ||
524 (num_run
> 64 && pending_bios
== &device
->pending_sync_bios
&&
525 device
->pending_bios
.head
)) {
526 spin_lock(&device
->io_lock
);
527 requeue_list(pending_bios
, pending
, tail
);
532 pending
= pending
->bi_next
;
535 BUG_ON(atomic_read(&cur
->__bi_cnt
) == 0);
538 * if we're doing the sync list, record that our
539 * plug has some sync requests on it
541 * If we're doing the regular list and there are
542 * sync requests sitting around, unplug before
545 if (pending_bios
== &device
->pending_sync_bios
) {
547 } else if (sync_pending
) {
548 blk_finish_plug(&plug
);
549 blk_start_plug(&plug
);
553 btrfsic_submit_bio(cur
);
560 * we made progress, there is more work to do and the bdi
561 * is now congested. Back off and let other work structs
564 if (pending
&& bdi_write_congested(bdi
) && batch_run
> 8 &&
565 fs_info
->fs_devices
->open_devices
> 1) {
566 struct io_context
*ioc
;
568 ioc
= current
->io_context
;
571 * the main goal here is that we don't want to
572 * block if we're going to be able to submit
573 * more requests without blocking.
575 * This code does two great things, it pokes into
576 * the elevator code from a filesystem _and_
577 * it makes assumptions about how batching works.
579 if (ioc
&& ioc
->nr_batch_requests
> 0 &&
580 time_before(jiffies
, ioc
->last_waited
+ HZ
/50UL) &&
582 ioc
->last_waited
== last_waited
)) {
584 * we want to go through our batch of
585 * requests and stop. So, we copy out
586 * the ioc->last_waited time and test
587 * against it before looping
589 last_waited
= ioc
->last_waited
;
593 spin_lock(&device
->io_lock
);
594 requeue_list(pending_bios
, pending
, tail
);
595 device
->running_pending
= 1;
597 spin_unlock(&device
->io_lock
);
598 btrfs_queue_work(fs_info
->submit_workers
,
608 spin_lock(&device
->io_lock
);
609 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
611 spin_unlock(&device
->io_lock
);
614 blk_finish_plug(&plug
);
617 static void pending_bios_fn(struct btrfs_work
*work
)
619 struct btrfs_device
*device
;
621 device
= container_of(work
, struct btrfs_device
, work
);
622 run_scheduled_bios(device
);
626 * Search and remove all stale (devices which are not mounted) devices.
627 * When both inputs are NULL, it will search and release all stale devices.
628 * path: Optional. When provided will it release all unmounted devices
629 * matching this path only.
630 * skip_dev: Optional. Will skip this device when searching for the stale
633 static void btrfs_free_stale_devices(const char *path
,
634 struct btrfs_device
*skip_device
)
636 struct btrfs_fs_devices
*fs_devices
, *tmp_fs_devices
;
637 struct btrfs_device
*device
, *tmp_device
;
639 list_for_each_entry_safe(fs_devices
, tmp_fs_devices
, &fs_uuids
, fs_list
) {
640 mutex_lock(&fs_devices
->device_list_mutex
);
641 if (fs_devices
->opened
) {
642 mutex_unlock(&fs_devices
->device_list_mutex
);
646 list_for_each_entry_safe(device
, tmp_device
,
647 &fs_devices
->devices
, dev_list
) {
650 if (skip_device
&& skip_device
== device
)
652 if (path
&& !device
->name
)
657 not_found
= strcmp(rcu_str_deref(device
->name
),
663 /* delete the stale device */
664 fs_devices
->num_devices
--;
665 list_del(&device
->dev_list
);
666 btrfs_free_device(device
);
668 if (fs_devices
->num_devices
== 0)
671 mutex_unlock(&fs_devices
->device_list_mutex
);
672 if (fs_devices
->num_devices
== 0) {
673 btrfs_sysfs_remove_fsid(fs_devices
);
674 list_del(&fs_devices
->fs_list
);
675 free_fs_devices(fs_devices
);
680 static int btrfs_open_one_device(struct btrfs_fs_devices
*fs_devices
,
681 struct btrfs_device
*device
, fmode_t flags
,
684 struct request_queue
*q
;
685 struct block_device
*bdev
;
686 struct buffer_head
*bh
;
687 struct btrfs_super_block
*disk_super
;
696 ret
= btrfs_get_bdev_and_sb(device
->name
->str
, flags
, holder
, 1,
701 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
702 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
703 if (devid
!= device
->devid
)
706 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
, BTRFS_UUID_SIZE
))
709 device
->generation
= btrfs_super_generation(disk_super
);
711 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
712 clear_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
);
713 fs_devices
->seeding
= 1;
715 if (bdev_read_only(bdev
))
716 clear_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
);
718 set_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
);
721 q
= bdev_get_queue(bdev
);
722 if (!blk_queue_nonrot(q
))
723 fs_devices
->rotating
= 1;
726 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA
, &device
->dev_state
);
727 device
->mode
= flags
;
729 fs_devices
->open_devices
++;
730 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
) &&
731 device
->devid
!= BTRFS_DEV_REPLACE_DEVID
) {
732 fs_devices
->rw_devices
++;
733 list_add_tail(&device
->dev_alloc_list
, &fs_devices
->alloc_list
);
741 blkdev_put(bdev
, flags
);
747 * Add new device to list of registered devices
750 * device pointer which was just added or updated when successful
751 * error pointer when failed
753 static noinline
struct btrfs_device
*device_list_add(const char *path
,
754 struct btrfs_super_block
*disk_super
,
755 bool *new_device_added
)
757 struct btrfs_device
*device
;
758 struct btrfs_fs_devices
*fs_devices
;
759 struct rcu_string
*name
;
760 u64 found_transid
= btrfs_super_generation(disk_super
);
761 u64 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
763 fs_devices
= find_fsid(disk_super
->fsid
);
765 fs_devices
= alloc_fs_devices(disk_super
->fsid
);
766 if (IS_ERR(fs_devices
))
767 return ERR_CAST(fs_devices
);
769 mutex_lock(&fs_devices
->device_list_mutex
);
770 list_add(&fs_devices
->fs_list
, &fs_uuids
);
774 mutex_lock(&fs_devices
->device_list_mutex
);
775 device
= find_device(fs_devices
, devid
,
776 disk_super
->dev_item
.uuid
);
780 if (fs_devices
->opened
) {
781 mutex_unlock(&fs_devices
->device_list_mutex
);
782 return ERR_PTR(-EBUSY
);
785 device
= btrfs_alloc_device(NULL
, &devid
,
786 disk_super
->dev_item
.uuid
);
787 if (IS_ERR(device
)) {
788 mutex_unlock(&fs_devices
->device_list_mutex
);
789 /* we can safely leave the fs_devices entry around */
793 name
= rcu_string_strdup(path
, GFP_NOFS
);
795 btrfs_free_device(device
);
796 mutex_unlock(&fs_devices
->device_list_mutex
);
797 return ERR_PTR(-ENOMEM
);
799 rcu_assign_pointer(device
->name
, name
);
801 list_add_rcu(&device
->dev_list
, &fs_devices
->devices
);
802 fs_devices
->num_devices
++;
804 device
->fs_devices
= fs_devices
;
805 *new_device_added
= true;
807 if (disk_super
->label
[0])
808 pr_info("BTRFS: device label %s devid %llu transid %llu %s\n",
809 disk_super
->label
, devid
, found_transid
, path
);
811 pr_info("BTRFS: device fsid %pU devid %llu transid %llu %s\n",
812 disk_super
->fsid
, devid
, found_transid
, path
);
814 } else if (!device
->name
|| strcmp(device
->name
->str
, path
)) {
816 * When FS is already mounted.
817 * 1. If you are here and if the device->name is NULL that
818 * means this device was missing at time of FS mount.
819 * 2. If you are here and if the device->name is different
820 * from 'path' that means either
821 * a. The same device disappeared and reappeared with
823 * b. The missing-disk-which-was-replaced, has
826 * We must allow 1 and 2a above. But 2b would be a spurious
829 * Further in case of 1 and 2a above, the disk at 'path'
830 * would have missed some transaction when it was away and
831 * in case of 2a the stale bdev has to be updated as well.
832 * 2b must not be allowed at all time.
836 * For now, we do allow update to btrfs_fs_device through the
837 * btrfs dev scan cli after FS has been mounted. We're still
838 * tracking a problem where systems fail mount by subvolume id
839 * when we reject replacement on a mounted FS.
841 if (!fs_devices
->opened
&& found_transid
< device
->generation
) {
843 * That is if the FS is _not_ mounted and if you
844 * are here, that means there is more than one
845 * disk with same uuid and devid.We keep the one
846 * with larger generation number or the last-in if
847 * generation are equal.
849 mutex_unlock(&fs_devices
->device_list_mutex
);
850 return ERR_PTR(-EEXIST
);
853 name
= rcu_string_strdup(path
, GFP_NOFS
);
855 mutex_unlock(&fs_devices
->device_list_mutex
);
856 return ERR_PTR(-ENOMEM
);
858 rcu_string_free(device
->name
);
859 rcu_assign_pointer(device
->name
, name
);
860 if (test_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
)) {
861 fs_devices
->missing_devices
--;
862 clear_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
);
867 * Unmount does not free the btrfs_device struct but would zero
868 * generation along with most of the other members. So just update
869 * it back. We need it to pick the disk with largest generation
872 if (!fs_devices
->opened
)
873 device
->generation
= found_transid
;
875 fs_devices
->total_devices
= btrfs_super_num_devices(disk_super
);
877 mutex_unlock(&fs_devices
->device_list_mutex
);
881 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
883 struct btrfs_fs_devices
*fs_devices
;
884 struct btrfs_device
*device
;
885 struct btrfs_device
*orig_dev
;
887 fs_devices
= alloc_fs_devices(orig
->fsid
);
888 if (IS_ERR(fs_devices
))
891 mutex_lock(&orig
->device_list_mutex
);
892 fs_devices
->total_devices
= orig
->total_devices
;
894 /* We have held the volume lock, it is safe to get the devices. */
895 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
896 struct rcu_string
*name
;
898 device
= btrfs_alloc_device(NULL
, &orig_dev
->devid
,
904 * This is ok to do without rcu read locked because we hold the
905 * uuid mutex so nothing we touch in here is going to disappear.
907 if (orig_dev
->name
) {
908 name
= rcu_string_strdup(orig_dev
->name
->str
,
911 btrfs_free_device(device
);
914 rcu_assign_pointer(device
->name
, name
);
917 list_add(&device
->dev_list
, &fs_devices
->devices
);
918 device
->fs_devices
= fs_devices
;
919 fs_devices
->num_devices
++;
921 mutex_unlock(&orig
->device_list_mutex
);
924 mutex_unlock(&orig
->device_list_mutex
);
925 free_fs_devices(fs_devices
);
926 return ERR_PTR(-ENOMEM
);
930 * After we have read the system tree and know devids belonging to
931 * this filesystem, remove the device which does not belong there.
933 void btrfs_free_extra_devids(struct btrfs_fs_devices
*fs_devices
, int step
)
935 struct btrfs_device
*device
, *next
;
936 struct btrfs_device
*latest_dev
= NULL
;
938 mutex_lock(&uuid_mutex
);
940 /* This is the initialized path, it is safe to release the devices. */
941 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
942 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA
,
943 &device
->dev_state
)) {
944 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT
,
945 &device
->dev_state
) &&
947 device
->generation
> latest_dev
->generation
)) {
953 if (device
->devid
== BTRFS_DEV_REPLACE_DEVID
) {
955 * In the first step, keep the device which has
956 * the correct fsid and the devid that is used
957 * for the dev_replace procedure.
958 * In the second step, the dev_replace state is
959 * read from the device tree and it is known
960 * whether the procedure is really active or
961 * not, which means whether this device is
962 * used or whether it should be removed.
964 if (step
== 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT
,
965 &device
->dev_state
)) {
970 blkdev_put(device
->bdev
, device
->mode
);
972 fs_devices
->open_devices
--;
974 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
)) {
975 list_del_init(&device
->dev_alloc_list
);
976 clear_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
);
977 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT
,
979 fs_devices
->rw_devices
--;
981 list_del_init(&device
->dev_list
);
982 fs_devices
->num_devices
--;
983 btrfs_free_device(device
);
986 if (fs_devices
->seed
) {
987 fs_devices
= fs_devices
->seed
;
991 fs_devices
->latest_bdev
= latest_dev
->bdev
;
993 mutex_unlock(&uuid_mutex
);
996 static void free_device_rcu(struct rcu_head
*head
)
998 struct btrfs_device
*device
;
1000 device
= container_of(head
, struct btrfs_device
, rcu
);
1001 btrfs_free_device(device
);
1004 static void btrfs_close_bdev(struct btrfs_device
*device
)
1009 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
)) {
1010 sync_blockdev(device
->bdev
);
1011 invalidate_bdev(device
->bdev
);
1014 blkdev_put(device
->bdev
, device
->mode
);
1017 static void btrfs_close_one_device(struct btrfs_device
*device
)
1019 struct btrfs_fs_devices
*fs_devices
= device
->fs_devices
;
1020 struct btrfs_device
*new_device
;
1021 struct rcu_string
*name
;
1024 fs_devices
->open_devices
--;
1026 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
) &&
1027 device
->devid
!= BTRFS_DEV_REPLACE_DEVID
) {
1028 list_del_init(&device
->dev_alloc_list
);
1029 fs_devices
->rw_devices
--;
1032 if (test_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
))
1033 fs_devices
->missing_devices
--;
1035 btrfs_close_bdev(device
);
1037 new_device
= btrfs_alloc_device(NULL
, &device
->devid
,
1039 BUG_ON(IS_ERR(new_device
)); /* -ENOMEM */
1041 /* Safe because we are under uuid_mutex */
1043 name
= rcu_string_strdup(device
->name
->str
, GFP_NOFS
);
1044 BUG_ON(!name
); /* -ENOMEM */
1045 rcu_assign_pointer(new_device
->name
, name
);
1048 list_replace_rcu(&device
->dev_list
, &new_device
->dev_list
);
1049 new_device
->fs_devices
= device
->fs_devices
;
1051 call_rcu(&device
->rcu
, free_device_rcu
);
1054 static int close_fs_devices(struct btrfs_fs_devices
*fs_devices
)
1056 struct btrfs_device
*device
, *tmp
;
1058 if (--fs_devices
->opened
> 0)
1061 mutex_lock(&fs_devices
->device_list_mutex
);
1062 list_for_each_entry_safe(device
, tmp
, &fs_devices
->devices
, dev_list
) {
1063 btrfs_close_one_device(device
);
1065 mutex_unlock(&fs_devices
->device_list_mutex
);
1067 WARN_ON(fs_devices
->open_devices
);
1068 WARN_ON(fs_devices
->rw_devices
);
1069 fs_devices
->opened
= 0;
1070 fs_devices
->seeding
= 0;
1075 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
1077 struct btrfs_fs_devices
*seed_devices
= NULL
;
1080 mutex_lock(&uuid_mutex
);
1081 ret
= close_fs_devices(fs_devices
);
1082 if (!fs_devices
->opened
) {
1083 seed_devices
= fs_devices
->seed
;
1084 fs_devices
->seed
= NULL
;
1086 mutex_unlock(&uuid_mutex
);
1088 while (seed_devices
) {
1089 fs_devices
= seed_devices
;
1090 seed_devices
= fs_devices
->seed
;
1091 close_fs_devices(fs_devices
);
1092 free_fs_devices(fs_devices
);
1097 static int open_fs_devices(struct btrfs_fs_devices
*fs_devices
,
1098 fmode_t flags
, void *holder
)
1100 struct btrfs_device
*device
;
1101 struct btrfs_device
*latest_dev
= NULL
;
1104 flags
|= FMODE_EXCL
;
1106 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
1107 /* Just open everything we can; ignore failures here */
1108 if (btrfs_open_one_device(fs_devices
, device
, flags
, holder
))
1112 device
->generation
> latest_dev
->generation
)
1113 latest_dev
= device
;
1115 if (fs_devices
->open_devices
== 0) {
1119 fs_devices
->opened
= 1;
1120 fs_devices
->latest_bdev
= latest_dev
->bdev
;
1121 fs_devices
->total_rw_bytes
= 0;
1126 static int devid_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
1128 struct btrfs_device
*dev1
, *dev2
;
1130 dev1
= list_entry(a
, struct btrfs_device
, dev_list
);
1131 dev2
= list_entry(b
, struct btrfs_device
, dev_list
);
1133 if (dev1
->devid
< dev2
->devid
)
1135 else if (dev1
->devid
> dev2
->devid
)
1140 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
1141 fmode_t flags
, void *holder
)
1145 lockdep_assert_held(&uuid_mutex
);
1147 mutex_lock(&fs_devices
->device_list_mutex
);
1148 if (fs_devices
->opened
) {
1149 fs_devices
->opened
++;
1152 list_sort(NULL
, &fs_devices
->devices
, devid_cmp
);
1153 ret
= open_fs_devices(fs_devices
, flags
, holder
);
1155 mutex_unlock(&fs_devices
->device_list_mutex
);
1160 static void btrfs_release_disk_super(struct page
*page
)
1166 static int btrfs_read_disk_super(struct block_device
*bdev
, u64 bytenr
,
1168 struct btrfs_super_block
**disk_super
)
1173 /* make sure our super fits in the device */
1174 if (bytenr
+ PAGE_SIZE
>= i_size_read(bdev
->bd_inode
))
1177 /* make sure our super fits in the page */
1178 if (sizeof(**disk_super
) > PAGE_SIZE
)
1181 /* make sure our super doesn't straddle pages on disk */
1182 index
= bytenr
>> PAGE_SHIFT
;
1183 if ((bytenr
+ sizeof(**disk_super
) - 1) >> PAGE_SHIFT
!= index
)
1186 /* pull in the page with our super */
1187 *page
= read_cache_page_gfp(bdev
->bd_inode
->i_mapping
,
1190 if (IS_ERR_OR_NULL(*page
))
1195 /* align our pointer to the offset of the super block */
1196 *disk_super
= p
+ (bytenr
& ~PAGE_MASK
);
1198 if (btrfs_super_bytenr(*disk_super
) != bytenr
||
1199 btrfs_super_magic(*disk_super
) != BTRFS_MAGIC
) {
1200 btrfs_release_disk_super(*page
);
1204 if ((*disk_super
)->label
[0] &&
1205 (*disk_super
)->label
[BTRFS_LABEL_SIZE
- 1])
1206 (*disk_super
)->label
[BTRFS_LABEL_SIZE
- 1] = '\0';
1212 * Look for a btrfs signature on a device. This may be called out of the mount path
1213 * and we are not allowed to call set_blocksize during the scan. The superblock
1214 * is read via pagecache
1216 struct btrfs_device
*btrfs_scan_one_device(const char *path
, fmode_t flags
,
1219 struct btrfs_super_block
*disk_super
;
1220 bool new_device_added
= false;
1221 struct btrfs_device
*device
= NULL
;
1222 struct block_device
*bdev
;
1226 lockdep_assert_held(&uuid_mutex
);
1229 * we would like to check all the supers, but that would make
1230 * a btrfs mount succeed after a mkfs from a different FS.
1231 * So, we need to add a special mount option to scan for
1232 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1234 bytenr
= btrfs_sb_offset(0);
1235 flags
|= FMODE_EXCL
;
1237 bdev
= blkdev_get_by_path(path
, flags
, holder
);
1239 return ERR_CAST(bdev
);
1241 if (btrfs_read_disk_super(bdev
, bytenr
, &page
, &disk_super
)) {
1242 device
= ERR_PTR(-EINVAL
);
1243 goto error_bdev_put
;
1246 device
= device_list_add(path
, disk_super
, &new_device_added
);
1247 if (!IS_ERR(device
)) {
1248 if (new_device_added
)
1249 btrfs_free_stale_devices(path
, device
);
1252 btrfs_release_disk_super(page
);
1255 blkdev_put(bdev
, flags
);
1260 static int contains_pending_extent(struct btrfs_transaction
*transaction
,
1261 struct btrfs_device
*device
,
1262 u64
*start
, u64 len
)
1264 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
1265 struct extent_map
*em
;
1266 struct list_head
*search_list
= &fs_info
->pinned_chunks
;
1268 u64 physical_start
= *start
;
1271 search_list
= &transaction
->pending_chunks
;
1273 list_for_each_entry(em
, search_list
, list
) {
1274 struct map_lookup
*map
;
1277 map
= em
->map_lookup
;
1278 for (i
= 0; i
< map
->num_stripes
; i
++) {
1281 if (map
->stripes
[i
].dev
!= device
)
1283 if (map
->stripes
[i
].physical
>= physical_start
+ len
||
1284 map
->stripes
[i
].physical
+ em
->orig_block_len
<=
1288 * Make sure that while processing the pinned list we do
1289 * not override our *start with a lower value, because
1290 * we can have pinned chunks that fall within this
1291 * device hole and that have lower physical addresses
1292 * than the pending chunks we processed before. If we
1293 * do not take this special care we can end up getting
1294 * 2 pending chunks that start at the same physical
1295 * device offsets because the end offset of a pinned
1296 * chunk can be equal to the start offset of some
1299 end
= map
->stripes
[i
].physical
+ em
->orig_block_len
;
1306 if (search_list
!= &fs_info
->pinned_chunks
) {
1307 search_list
= &fs_info
->pinned_chunks
;
1316 * find_free_dev_extent_start - find free space in the specified device
1317 * @device: the device which we search the free space in
1318 * @num_bytes: the size of the free space that we need
1319 * @search_start: the position from which to begin the search
1320 * @start: store the start of the free space.
1321 * @len: the size of the free space. that we find, or the size
1322 * of the max free space if we don't find suitable free space
1324 * this uses a pretty simple search, the expectation is that it is
1325 * called very infrequently and that a given device has a small number
1328 * @start is used to store the start of the free space if we find. But if we
1329 * don't find suitable free space, it will be used to store the start position
1330 * of the max free space.
1332 * @len is used to store the size of the free space that we find.
1333 * But if we don't find suitable free space, it is used to store the size of
1334 * the max free space.
1336 int find_free_dev_extent_start(struct btrfs_transaction
*transaction
,
1337 struct btrfs_device
*device
, u64 num_bytes
,
1338 u64 search_start
, u64
*start
, u64
*len
)
1340 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
1341 struct btrfs_root
*root
= fs_info
->dev_root
;
1342 struct btrfs_key key
;
1343 struct btrfs_dev_extent
*dev_extent
;
1344 struct btrfs_path
*path
;
1349 u64 search_end
= device
->total_bytes
;
1352 struct extent_buffer
*l
;
1355 * We don't want to overwrite the superblock on the drive nor any area
1356 * used by the boot loader (grub for example), so we make sure to start
1357 * at an offset of at least 1MB.
1359 search_start
= max_t(u64
, search_start
, SZ_1M
);
1361 path
= btrfs_alloc_path();
1365 max_hole_start
= search_start
;
1369 if (search_start
>= search_end
||
1370 test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
)) {
1375 path
->reada
= READA_FORWARD
;
1376 path
->search_commit_root
= 1;
1377 path
->skip_locking
= 1;
1379 key
.objectid
= device
->devid
;
1380 key
.offset
= search_start
;
1381 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1383 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1387 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
1394 slot
= path
->slots
[0];
1395 if (slot
>= btrfs_header_nritems(l
)) {
1396 ret
= btrfs_next_leaf(root
, path
);
1404 btrfs_item_key_to_cpu(l
, &key
, slot
);
1406 if (key
.objectid
< device
->devid
)
1409 if (key
.objectid
> device
->devid
)
1412 if (key
.type
!= BTRFS_DEV_EXTENT_KEY
)
1415 if (key
.offset
> search_start
) {
1416 hole_size
= key
.offset
- search_start
;
1419 * Have to check before we set max_hole_start, otherwise
1420 * we could end up sending back this offset anyway.
1422 if (contains_pending_extent(transaction
, device
,
1425 if (key
.offset
>= search_start
) {
1426 hole_size
= key
.offset
- search_start
;
1433 if (hole_size
> max_hole_size
) {
1434 max_hole_start
= search_start
;
1435 max_hole_size
= hole_size
;
1439 * If this free space is greater than which we need,
1440 * it must be the max free space that we have found
1441 * until now, so max_hole_start must point to the start
1442 * of this free space and the length of this free space
1443 * is stored in max_hole_size. Thus, we return
1444 * max_hole_start and max_hole_size and go back to the
1447 if (hole_size
>= num_bytes
) {
1453 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1454 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
1456 if (extent_end
> search_start
)
1457 search_start
= extent_end
;
1464 * At this point, search_start should be the end of
1465 * allocated dev extents, and when shrinking the device,
1466 * search_end may be smaller than search_start.
1468 if (search_end
> search_start
) {
1469 hole_size
= search_end
- search_start
;
1471 if (contains_pending_extent(transaction
, device
, &search_start
,
1473 btrfs_release_path(path
);
1477 if (hole_size
> max_hole_size
) {
1478 max_hole_start
= search_start
;
1479 max_hole_size
= hole_size
;
1484 if (max_hole_size
< num_bytes
)
1490 btrfs_free_path(path
);
1491 *start
= max_hole_start
;
1493 *len
= max_hole_size
;
1497 int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
1498 struct btrfs_device
*device
, u64 num_bytes
,
1499 u64
*start
, u64
*len
)
1501 /* FIXME use last free of some kind */
1502 return find_free_dev_extent_start(trans
->transaction
, device
,
1503 num_bytes
, 0, start
, len
);
1506 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
1507 struct btrfs_device
*device
,
1508 u64 start
, u64
*dev_extent_len
)
1510 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
1511 struct btrfs_root
*root
= fs_info
->dev_root
;
1513 struct btrfs_path
*path
;
1514 struct btrfs_key key
;
1515 struct btrfs_key found_key
;
1516 struct extent_buffer
*leaf
= NULL
;
1517 struct btrfs_dev_extent
*extent
= NULL
;
1519 path
= btrfs_alloc_path();
1523 key
.objectid
= device
->devid
;
1525 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1527 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1529 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
1530 BTRFS_DEV_EXTENT_KEY
);
1533 leaf
= path
->nodes
[0];
1534 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1535 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1536 struct btrfs_dev_extent
);
1537 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
1538 btrfs_dev_extent_length(leaf
, extent
) < start
);
1540 btrfs_release_path(path
);
1542 } else if (ret
== 0) {
1543 leaf
= path
->nodes
[0];
1544 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1545 struct btrfs_dev_extent
);
1547 btrfs_handle_fs_error(fs_info
, ret
, "Slot search failed");
1551 *dev_extent_len
= btrfs_dev_extent_length(leaf
, extent
);
1553 ret
= btrfs_del_item(trans
, root
, path
);
1555 btrfs_handle_fs_error(fs_info
, ret
,
1556 "Failed to remove dev extent item");
1558 set_bit(BTRFS_TRANS_HAVE_FREE_BGS
, &trans
->transaction
->flags
);
1561 btrfs_free_path(path
);
1565 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
1566 struct btrfs_device
*device
,
1567 u64 chunk_offset
, u64 start
, u64 num_bytes
)
1570 struct btrfs_path
*path
;
1571 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
1572 struct btrfs_root
*root
= fs_info
->dev_root
;
1573 struct btrfs_dev_extent
*extent
;
1574 struct extent_buffer
*leaf
;
1575 struct btrfs_key key
;
1577 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA
, &device
->dev_state
));
1578 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
));
1579 path
= btrfs_alloc_path();
1583 key
.objectid
= device
->devid
;
1585 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1586 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1591 leaf
= path
->nodes
[0];
1592 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1593 struct btrfs_dev_extent
);
1594 btrfs_set_dev_extent_chunk_tree(leaf
, extent
,
1595 BTRFS_CHUNK_TREE_OBJECTID
);
1596 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
,
1597 BTRFS_FIRST_CHUNK_TREE_OBJECTID
);
1598 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1600 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1601 btrfs_mark_buffer_dirty(leaf
);
1603 btrfs_free_path(path
);
1607 static u64
find_next_chunk(struct btrfs_fs_info
*fs_info
)
1609 struct extent_map_tree
*em_tree
;
1610 struct extent_map
*em
;
1614 em_tree
= &fs_info
->mapping_tree
.map_tree
;
1615 read_lock(&em_tree
->lock
);
1616 n
= rb_last(&em_tree
->map
.rb_root
);
1618 em
= rb_entry(n
, struct extent_map
, rb_node
);
1619 ret
= em
->start
+ em
->len
;
1621 read_unlock(&em_tree
->lock
);
1626 static noinline
int find_next_devid(struct btrfs_fs_info
*fs_info
,
1630 struct btrfs_key key
;
1631 struct btrfs_key found_key
;
1632 struct btrfs_path
*path
;
1634 path
= btrfs_alloc_path();
1638 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1639 key
.type
= BTRFS_DEV_ITEM_KEY
;
1640 key
.offset
= (u64
)-1;
1642 ret
= btrfs_search_slot(NULL
, fs_info
->chunk_root
, &key
, path
, 0, 0);
1646 BUG_ON(ret
== 0); /* Corruption */
1648 ret
= btrfs_previous_item(fs_info
->chunk_root
, path
,
1649 BTRFS_DEV_ITEMS_OBJECTID
,
1650 BTRFS_DEV_ITEM_KEY
);
1654 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1656 *devid_ret
= found_key
.offset
+ 1;
1660 btrfs_free_path(path
);
1665 * the device information is stored in the chunk root
1666 * the btrfs_device struct should be fully filled in
1668 static int btrfs_add_dev_item(struct btrfs_trans_handle
*trans
,
1669 struct btrfs_device
*device
)
1672 struct btrfs_path
*path
;
1673 struct btrfs_dev_item
*dev_item
;
1674 struct extent_buffer
*leaf
;
1675 struct btrfs_key key
;
1678 path
= btrfs_alloc_path();
1682 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1683 key
.type
= BTRFS_DEV_ITEM_KEY
;
1684 key
.offset
= device
->devid
;
1686 ret
= btrfs_insert_empty_item(trans
, trans
->fs_info
->chunk_root
, path
,
1687 &key
, sizeof(*dev_item
));
1691 leaf
= path
->nodes
[0];
1692 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1694 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1695 btrfs_set_device_generation(leaf
, dev_item
, 0);
1696 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1697 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1698 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1699 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1700 btrfs_set_device_total_bytes(leaf
, dev_item
,
1701 btrfs_device_get_disk_total_bytes(device
));
1702 btrfs_set_device_bytes_used(leaf
, dev_item
,
1703 btrfs_device_get_bytes_used(device
));
1704 btrfs_set_device_group(leaf
, dev_item
, 0);
1705 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1706 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1707 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1709 ptr
= btrfs_device_uuid(dev_item
);
1710 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1711 ptr
= btrfs_device_fsid(dev_item
);
1712 write_extent_buffer(leaf
, trans
->fs_info
->fsid
, ptr
, BTRFS_FSID_SIZE
);
1713 btrfs_mark_buffer_dirty(leaf
);
1717 btrfs_free_path(path
);
1722 * Function to update ctime/mtime for a given device path.
1723 * Mainly used for ctime/mtime based probe like libblkid.
1725 static void update_dev_time(const char *path_name
)
1729 filp
= filp_open(path_name
, O_RDWR
, 0);
1732 file_update_time(filp
);
1733 filp_close(filp
, NULL
);
1736 static int btrfs_rm_dev_item(struct btrfs_fs_info
*fs_info
,
1737 struct btrfs_device
*device
)
1739 struct btrfs_root
*root
= fs_info
->chunk_root
;
1741 struct btrfs_path
*path
;
1742 struct btrfs_key key
;
1743 struct btrfs_trans_handle
*trans
;
1745 path
= btrfs_alloc_path();
1749 trans
= btrfs_start_transaction(root
, 0);
1750 if (IS_ERR(trans
)) {
1751 btrfs_free_path(path
);
1752 return PTR_ERR(trans
);
1754 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1755 key
.type
= BTRFS_DEV_ITEM_KEY
;
1756 key
.offset
= device
->devid
;
1758 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1762 btrfs_abort_transaction(trans
, ret
);
1763 btrfs_end_transaction(trans
);
1767 ret
= btrfs_del_item(trans
, root
, path
);
1769 btrfs_abort_transaction(trans
, ret
);
1770 btrfs_end_transaction(trans
);
1774 btrfs_free_path(path
);
1776 ret
= btrfs_commit_transaction(trans
);
1781 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1782 * filesystem. It's up to the caller to adjust that number regarding eg. device
1785 static int btrfs_check_raid_min_devices(struct btrfs_fs_info
*fs_info
,
1793 seq
= read_seqbegin(&fs_info
->profiles_lock
);
1795 all_avail
= fs_info
->avail_data_alloc_bits
|
1796 fs_info
->avail_system_alloc_bits
|
1797 fs_info
->avail_metadata_alloc_bits
;
1798 } while (read_seqretry(&fs_info
->profiles_lock
, seq
));
1800 for (i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++) {
1801 if (!(all_avail
& btrfs_raid_array
[i
].bg_flag
))
1804 if (num_devices
< btrfs_raid_array
[i
].devs_min
) {
1805 int ret
= btrfs_raid_array
[i
].mindev_error
;
1815 static struct btrfs_device
* btrfs_find_next_active_device(
1816 struct btrfs_fs_devices
*fs_devs
, struct btrfs_device
*device
)
1818 struct btrfs_device
*next_device
;
1820 list_for_each_entry(next_device
, &fs_devs
->devices
, dev_list
) {
1821 if (next_device
!= device
&&
1822 !test_bit(BTRFS_DEV_STATE_MISSING
, &next_device
->dev_state
)
1823 && next_device
->bdev
)
1831 * Helper function to check if the given device is part of s_bdev / latest_bdev
1832 * and replace it with the provided or the next active device, in the context
1833 * where this function called, there should be always be another device (or
1834 * this_dev) which is active.
1836 void btrfs_assign_next_active_device(struct btrfs_device
*device
,
1837 struct btrfs_device
*this_dev
)
1839 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
1840 struct btrfs_device
*next_device
;
1843 next_device
= this_dev
;
1845 next_device
= btrfs_find_next_active_device(fs_info
->fs_devices
,
1847 ASSERT(next_device
);
1849 if (fs_info
->sb
->s_bdev
&&
1850 (fs_info
->sb
->s_bdev
== device
->bdev
))
1851 fs_info
->sb
->s_bdev
= next_device
->bdev
;
1853 if (fs_info
->fs_devices
->latest_bdev
== device
->bdev
)
1854 fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1858 * Return btrfs_fs_devices::num_devices excluding the device that's being
1859 * currently replaced.
1861 static u64
btrfs_num_devices(struct btrfs_fs_info
*fs_info
)
1863 u64 num_devices
= fs_info
->fs_devices
->num_devices
;
1865 btrfs_dev_replace_read_lock(&fs_info
->dev_replace
);
1866 if (btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
)) {
1867 ASSERT(num_devices
> 1);
1870 btrfs_dev_replace_read_unlock(&fs_info
->dev_replace
);
1875 int btrfs_rm_device(struct btrfs_fs_info
*fs_info
, const char *device_path
,
1878 struct btrfs_device
*device
;
1879 struct btrfs_fs_devices
*cur_devices
;
1880 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
1884 mutex_lock(&uuid_mutex
);
1886 num_devices
= btrfs_num_devices(fs_info
);
1888 ret
= btrfs_check_raid_min_devices(fs_info
, num_devices
- 1);
1892 device
= btrfs_find_device_by_devspec(fs_info
, devid
, device_path
);
1894 if (IS_ERR(device
)) {
1895 if (PTR_ERR(device
) == -ENOENT
&&
1896 strcmp(device_path
, "missing") == 0)
1897 ret
= BTRFS_ERROR_DEV_MISSING_NOT_FOUND
;
1899 ret
= PTR_ERR(device
);
1903 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
)) {
1904 ret
= BTRFS_ERROR_DEV_TGT_REPLACE
;
1908 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
) &&
1909 fs_info
->fs_devices
->rw_devices
== 1) {
1910 ret
= BTRFS_ERROR_DEV_ONLY_WRITABLE
;
1914 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
)) {
1915 mutex_lock(&fs_info
->chunk_mutex
);
1916 list_del_init(&device
->dev_alloc_list
);
1917 device
->fs_devices
->rw_devices
--;
1918 mutex_unlock(&fs_info
->chunk_mutex
);
1921 mutex_unlock(&uuid_mutex
);
1922 ret
= btrfs_shrink_device(device
, 0);
1923 mutex_lock(&uuid_mutex
);
1928 * TODO: the superblock still includes this device in its num_devices
1929 * counter although write_all_supers() is not locked out. This
1930 * could give a filesystem state which requires a degraded mount.
1932 ret
= btrfs_rm_dev_item(fs_info
, device
);
1936 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA
, &device
->dev_state
);
1937 btrfs_scrub_cancel_dev(fs_info
, device
);
1940 * the device list mutex makes sure that we don't change
1941 * the device list while someone else is writing out all
1942 * the device supers. Whoever is writing all supers, should
1943 * lock the device list mutex before getting the number of
1944 * devices in the super block (super_copy). Conversely,
1945 * whoever updates the number of devices in the super block
1946 * (super_copy) should hold the device list mutex.
1950 * In normal cases the cur_devices == fs_devices. But in case
1951 * of deleting a seed device, the cur_devices should point to
1952 * its own fs_devices listed under the fs_devices->seed.
1954 cur_devices
= device
->fs_devices
;
1955 mutex_lock(&fs_devices
->device_list_mutex
);
1956 list_del_rcu(&device
->dev_list
);
1958 cur_devices
->num_devices
--;
1959 cur_devices
->total_devices
--;
1960 /* Update total_devices of the parent fs_devices if it's seed */
1961 if (cur_devices
!= fs_devices
)
1962 fs_devices
->total_devices
--;
1964 if (test_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
))
1965 cur_devices
->missing_devices
--;
1967 btrfs_assign_next_active_device(device
, NULL
);
1970 cur_devices
->open_devices
--;
1971 /* remove sysfs entry */
1972 btrfs_sysfs_rm_device_link(fs_devices
, device
);
1975 num_devices
= btrfs_super_num_devices(fs_info
->super_copy
) - 1;
1976 btrfs_set_super_num_devices(fs_info
->super_copy
, num_devices
);
1977 mutex_unlock(&fs_devices
->device_list_mutex
);
1980 * at this point, the device is zero sized and detached from
1981 * the devices list. All that's left is to zero out the old
1982 * supers and free the device.
1984 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
))
1985 btrfs_scratch_superblocks(device
->bdev
, device
->name
->str
);
1987 btrfs_close_bdev(device
);
1988 call_rcu(&device
->rcu
, free_device_rcu
);
1990 if (cur_devices
->open_devices
== 0) {
1991 while (fs_devices
) {
1992 if (fs_devices
->seed
== cur_devices
) {
1993 fs_devices
->seed
= cur_devices
->seed
;
1996 fs_devices
= fs_devices
->seed
;
1998 cur_devices
->seed
= NULL
;
1999 close_fs_devices(cur_devices
);
2000 free_fs_devices(cur_devices
);
2004 mutex_unlock(&uuid_mutex
);
2008 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
)) {
2009 mutex_lock(&fs_info
->chunk_mutex
);
2010 list_add(&device
->dev_alloc_list
,
2011 &fs_devices
->alloc_list
);
2012 device
->fs_devices
->rw_devices
++;
2013 mutex_unlock(&fs_info
->chunk_mutex
);
2018 void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device
*srcdev
)
2020 struct btrfs_fs_devices
*fs_devices
;
2022 lockdep_assert_held(&srcdev
->fs_info
->fs_devices
->device_list_mutex
);
2025 * in case of fs with no seed, srcdev->fs_devices will point
2026 * to fs_devices of fs_info. However when the dev being replaced is
2027 * a seed dev it will point to the seed's local fs_devices. In short
2028 * srcdev will have its correct fs_devices in both the cases.
2030 fs_devices
= srcdev
->fs_devices
;
2032 list_del_rcu(&srcdev
->dev_list
);
2033 list_del(&srcdev
->dev_alloc_list
);
2034 fs_devices
->num_devices
--;
2035 if (test_bit(BTRFS_DEV_STATE_MISSING
, &srcdev
->dev_state
))
2036 fs_devices
->missing_devices
--;
2038 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &srcdev
->dev_state
))
2039 fs_devices
->rw_devices
--;
2042 fs_devices
->open_devices
--;
2045 void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info
*fs_info
,
2046 struct btrfs_device
*srcdev
)
2048 struct btrfs_fs_devices
*fs_devices
= srcdev
->fs_devices
;
2050 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &srcdev
->dev_state
)) {
2051 /* zero out the old super if it is writable */
2052 btrfs_scratch_superblocks(srcdev
->bdev
, srcdev
->name
->str
);
2055 btrfs_close_bdev(srcdev
);
2056 call_rcu(&srcdev
->rcu
, free_device_rcu
);
2058 /* if this is no devs we rather delete the fs_devices */
2059 if (!fs_devices
->num_devices
) {
2060 struct btrfs_fs_devices
*tmp_fs_devices
;
2063 * On a mounted FS, num_devices can't be zero unless it's a
2064 * seed. In case of a seed device being replaced, the replace
2065 * target added to the sprout FS, so there will be no more
2066 * device left under the seed FS.
2068 ASSERT(fs_devices
->seeding
);
2070 tmp_fs_devices
= fs_info
->fs_devices
;
2071 while (tmp_fs_devices
) {
2072 if (tmp_fs_devices
->seed
== fs_devices
) {
2073 tmp_fs_devices
->seed
= fs_devices
->seed
;
2076 tmp_fs_devices
= tmp_fs_devices
->seed
;
2078 fs_devices
->seed
= NULL
;
2079 close_fs_devices(fs_devices
);
2080 free_fs_devices(fs_devices
);
2084 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device
*tgtdev
)
2086 struct btrfs_fs_devices
*fs_devices
= tgtdev
->fs_info
->fs_devices
;
2089 mutex_lock(&fs_devices
->device_list_mutex
);
2091 btrfs_sysfs_rm_device_link(fs_devices
, tgtdev
);
2094 fs_devices
->open_devices
--;
2096 fs_devices
->num_devices
--;
2098 btrfs_assign_next_active_device(tgtdev
, NULL
);
2100 list_del_rcu(&tgtdev
->dev_list
);
2102 mutex_unlock(&fs_devices
->device_list_mutex
);
2105 * The update_dev_time() with in btrfs_scratch_superblocks()
2106 * may lead to a call to btrfs_show_devname() which will try
2107 * to hold device_list_mutex. And here this device
2108 * is already out of device list, so we don't have to hold
2109 * the device_list_mutex lock.
2111 btrfs_scratch_superblocks(tgtdev
->bdev
, tgtdev
->name
->str
);
2113 btrfs_close_bdev(tgtdev
);
2114 call_rcu(&tgtdev
->rcu
, free_device_rcu
);
2117 static struct btrfs_device
*btrfs_find_device_by_path(
2118 struct btrfs_fs_info
*fs_info
, const char *device_path
)
2121 struct btrfs_super_block
*disk_super
;
2124 struct block_device
*bdev
;
2125 struct buffer_head
*bh
;
2126 struct btrfs_device
*device
;
2128 ret
= btrfs_get_bdev_and_sb(device_path
, FMODE_READ
,
2129 fs_info
->bdev_holder
, 0, &bdev
, &bh
);
2131 return ERR_PTR(ret
);
2132 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
2133 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
2134 dev_uuid
= disk_super
->dev_item
.uuid
;
2135 device
= btrfs_find_device(fs_info
, devid
, dev_uuid
, disk_super
->fsid
);
2138 device
= ERR_PTR(-ENOENT
);
2139 blkdev_put(bdev
, FMODE_READ
);
2143 static struct btrfs_device
*btrfs_find_device_missing_or_by_path(
2144 struct btrfs_fs_info
*fs_info
, const char *device_path
)
2146 struct btrfs_device
*device
= NULL
;
2147 if (strcmp(device_path
, "missing") == 0) {
2148 struct list_head
*devices
;
2149 struct btrfs_device
*tmp
;
2151 devices
= &fs_info
->fs_devices
->devices
;
2152 list_for_each_entry(tmp
, devices
, dev_list
) {
2153 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA
,
2154 &tmp
->dev_state
) && !tmp
->bdev
) {
2161 return ERR_PTR(-ENOENT
);
2163 device
= btrfs_find_device_by_path(fs_info
, device_path
);
2170 * Lookup a device given by device id, or the path if the id is 0.
2172 struct btrfs_device
*btrfs_find_device_by_devspec(
2173 struct btrfs_fs_info
*fs_info
, u64 devid
, const char *devpath
)
2175 struct btrfs_device
*device
;
2178 device
= btrfs_find_device(fs_info
, devid
, NULL
, NULL
);
2180 return ERR_PTR(-ENOENT
);
2182 if (!devpath
|| !devpath
[0])
2183 return ERR_PTR(-EINVAL
);
2184 device
= btrfs_find_device_missing_or_by_path(fs_info
, devpath
);
2190 * does all the dirty work required for changing file system's UUID.
2192 static int btrfs_prepare_sprout(struct btrfs_fs_info
*fs_info
)
2194 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
2195 struct btrfs_fs_devices
*old_devices
;
2196 struct btrfs_fs_devices
*seed_devices
;
2197 struct btrfs_super_block
*disk_super
= fs_info
->super_copy
;
2198 struct btrfs_device
*device
;
2201 lockdep_assert_held(&uuid_mutex
);
2202 if (!fs_devices
->seeding
)
2205 seed_devices
= alloc_fs_devices(NULL
);
2206 if (IS_ERR(seed_devices
))
2207 return PTR_ERR(seed_devices
);
2209 old_devices
= clone_fs_devices(fs_devices
);
2210 if (IS_ERR(old_devices
)) {
2211 kfree(seed_devices
);
2212 return PTR_ERR(old_devices
);
2215 list_add(&old_devices
->fs_list
, &fs_uuids
);
2217 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
2218 seed_devices
->opened
= 1;
2219 INIT_LIST_HEAD(&seed_devices
->devices
);
2220 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
2221 mutex_init(&seed_devices
->device_list_mutex
);
2223 mutex_lock(&fs_devices
->device_list_mutex
);
2224 list_splice_init_rcu(&fs_devices
->devices
, &seed_devices
->devices
,
2226 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
)
2227 device
->fs_devices
= seed_devices
;
2229 mutex_lock(&fs_info
->chunk_mutex
);
2230 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
2231 mutex_unlock(&fs_info
->chunk_mutex
);
2233 fs_devices
->seeding
= 0;
2234 fs_devices
->num_devices
= 0;
2235 fs_devices
->open_devices
= 0;
2236 fs_devices
->missing_devices
= 0;
2237 fs_devices
->rotating
= 0;
2238 fs_devices
->seed
= seed_devices
;
2240 generate_random_uuid(fs_devices
->fsid
);
2241 memcpy(fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
2242 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
2243 mutex_unlock(&fs_devices
->device_list_mutex
);
2245 super_flags
= btrfs_super_flags(disk_super
) &
2246 ~BTRFS_SUPER_FLAG_SEEDING
;
2247 btrfs_set_super_flags(disk_super
, super_flags
);
2253 * Store the expected generation for seed devices in device items.
2255 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
2256 struct btrfs_fs_info
*fs_info
)
2258 struct btrfs_root
*root
= fs_info
->chunk_root
;
2259 struct btrfs_path
*path
;
2260 struct extent_buffer
*leaf
;
2261 struct btrfs_dev_item
*dev_item
;
2262 struct btrfs_device
*device
;
2263 struct btrfs_key key
;
2264 u8 fs_uuid
[BTRFS_FSID_SIZE
];
2265 u8 dev_uuid
[BTRFS_UUID_SIZE
];
2269 path
= btrfs_alloc_path();
2273 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
2275 key
.type
= BTRFS_DEV_ITEM_KEY
;
2278 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2282 leaf
= path
->nodes
[0];
2284 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
2285 ret
= btrfs_next_leaf(root
, path
);
2290 leaf
= path
->nodes
[0];
2291 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2292 btrfs_release_path(path
);
2296 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2297 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
2298 key
.type
!= BTRFS_DEV_ITEM_KEY
)
2301 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2302 struct btrfs_dev_item
);
2303 devid
= btrfs_device_id(leaf
, dev_item
);
2304 read_extent_buffer(leaf
, dev_uuid
, btrfs_device_uuid(dev_item
),
2306 read_extent_buffer(leaf
, fs_uuid
, btrfs_device_fsid(dev_item
),
2308 device
= btrfs_find_device(fs_info
, devid
, dev_uuid
, fs_uuid
);
2309 BUG_ON(!device
); /* Logic error */
2311 if (device
->fs_devices
->seeding
) {
2312 btrfs_set_device_generation(leaf
, dev_item
,
2313 device
->generation
);
2314 btrfs_mark_buffer_dirty(leaf
);
2322 btrfs_free_path(path
);
2326 int btrfs_init_new_device(struct btrfs_fs_info
*fs_info
, const char *device_path
)
2328 struct btrfs_root
*root
= fs_info
->dev_root
;
2329 struct request_queue
*q
;
2330 struct btrfs_trans_handle
*trans
;
2331 struct btrfs_device
*device
;
2332 struct block_device
*bdev
;
2333 struct super_block
*sb
= fs_info
->sb
;
2334 struct rcu_string
*name
;
2335 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
2336 u64 orig_super_total_bytes
;
2337 u64 orig_super_num_devices
;
2338 int seeding_dev
= 0;
2340 bool unlocked
= false;
2342 if (sb_rdonly(sb
) && !fs_devices
->seeding
)
2345 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
2346 fs_info
->bdev_holder
);
2348 return PTR_ERR(bdev
);
2350 if (fs_devices
->seeding
) {
2352 down_write(&sb
->s_umount
);
2353 mutex_lock(&uuid_mutex
);
2356 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
2358 mutex_lock(&fs_devices
->device_list_mutex
);
2359 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
2360 if (device
->bdev
== bdev
) {
2363 &fs_devices
->device_list_mutex
);
2367 mutex_unlock(&fs_devices
->device_list_mutex
);
2369 device
= btrfs_alloc_device(fs_info
, NULL
, NULL
);
2370 if (IS_ERR(device
)) {
2371 /* we can safely leave the fs_devices entry around */
2372 ret
= PTR_ERR(device
);
2376 name
= rcu_string_strdup(device_path
, GFP_KERNEL
);
2379 goto error_free_device
;
2381 rcu_assign_pointer(device
->name
, name
);
2383 trans
= btrfs_start_transaction(root
, 0);
2384 if (IS_ERR(trans
)) {
2385 ret
= PTR_ERR(trans
);
2386 goto error_free_device
;
2389 q
= bdev_get_queue(bdev
);
2390 set_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
);
2391 device
->generation
= trans
->transid
;
2392 device
->io_width
= fs_info
->sectorsize
;
2393 device
->io_align
= fs_info
->sectorsize
;
2394 device
->sector_size
= fs_info
->sectorsize
;
2395 device
->total_bytes
= round_down(i_size_read(bdev
->bd_inode
),
2396 fs_info
->sectorsize
);
2397 device
->disk_total_bytes
= device
->total_bytes
;
2398 device
->commit_total_bytes
= device
->total_bytes
;
2399 device
->fs_info
= fs_info
;
2400 device
->bdev
= bdev
;
2401 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA
, &device
->dev_state
);
2402 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
);
2403 device
->mode
= FMODE_EXCL
;
2404 device
->dev_stats_valid
= 1;
2405 set_blocksize(device
->bdev
, BTRFS_BDEV_BLOCKSIZE
);
2408 sb
->s_flags
&= ~SB_RDONLY
;
2409 ret
= btrfs_prepare_sprout(fs_info
);
2411 btrfs_abort_transaction(trans
, ret
);
2416 device
->fs_devices
= fs_devices
;
2418 mutex_lock(&fs_devices
->device_list_mutex
);
2419 mutex_lock(&fs_info
->chunk_mutex
);
2420 list_add_rcu(&device
->dev_list
, &fs_devices
->devices
);
2421 list_add(&device
->dev_alloc_list
, &fs_devices
->alloc_list
);
2422 fs_devices
->num_devices
++;
2423 fs_devices
->open_devices
++;
2424 fs_devices
->rw_devices
++;
2425 fs_devices
->total_devices
++;
2426 fs_devices
->total_rw_bytes
+= device
->total_bytes
;
2428 atomic64_add(device
->total_bytes
, &fs_info
->free_chunk_space
);
2430 if (!blk_queue_nonrot(q
))
2431 fs_devices
->rotating
= 1;
2433 orig_super_total_bytes
= btrfs_super_total_bytes(fs_info
->super_copy
);
2434 btrfs_set_super_total_bytes(fs_info
->super_copy
,
2435 round_down(orig_super_total_bytes
+ device
->total_bytes
,
2436 fs_info
->sectorsize
));
2438 orig_super_num_devices
= btrfs_super_num_devices(fs_info
->super_copy
);
2439 btrfs_set_super_num_devices(fs_info
->super_copy
,
2440 orig_super_num_devices
+ 1);
2442 /* add sysfs device entry */
2443 btrfs_sysfs_add_device_link(fs_devices
, device
);
2446 * we've got more storage, clear any full flags on the space
2449 btrfs_clear_space_info_full(fs_info
);
2451 mutex_unlock(&fs_info
->chunk_mutex
);
2452 mutex_unlock(&fs_devices
->device_list_mutex
);
2455 mutex_lock(&fs_info
->chunk_mutex
);
2456 ret
= init_first_rw_device(trans
, fs_info
);
2457 mutex_unlock(&fs_info
->chunk_mutex
);
2459 btrfs_abort_transaction(trans
, ret
);
2464 ret
= btrfs_add_dev_item(trans
, device
);
2466 btrfs_abort_transaction(trans
, ret
);
2471 char fsid_buf
[BTRFS_UUID_UNPARSED_SIZE
];
2473 ret
= btrfs_finish_sprout(trans
, fs_info
);
2475 btrfs_abort_transaction(trans
, ret
);
2479 /* Sprouting would change fsid of the mounted root,
2480 * so rename the fsid on the sysfs
2482 snprintf(fsid_buf
, BTRFS_UUID_UNPARSED_SIZE
, "%pU",
2484 if (kobject_rename(&fs_devices
->fsid_kobj
, fsid_buf
))
2486 "sysfs: failed to create fsid for sprout");
2489 ret
= btrfs_commit_transaction(trans
);
2492 mutex_unlock(&uuid_mutex
);
2493 up_write(&sb
->s_umount
);
2496 if (ret
) /* transaction commit */
2499 ret
= btrfs_relocate_sys_chunks(fs_info
);
2501 btrfs_handle_fs_error(fs_info
, ret
,
2502 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
2503 trans
= btrfs_attach_transaction(root
);
2504 if (IS_ERR(trans
)) {
2505 if (PTR_ERR(trans
) == -ENOENT
)
2507 ret
= PTR_ERR(trans
);
2511 ret
= btrfs_commit_transaction(trans
);
2514 /* Update ctime/mtime for libblkid */
2515 update_dev_time(device_path
);
2519 btrfs_sysfs_rm_device_link(fs_devices
, device
);
2520 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
2521 mutex_lock(&fs_info
->chunk_mutex
);
2522 list_del_rcu(&device
->dev_list
);
2523 list_del(&device
->dev_alloc_list
);
2524 fs_info
->fs_devices
->num_devices
--;
2525 fs_info
->fs_devices
->open_devices
--;
2526 fs_info
->fs_devices
->rw_devices
--;
2527 fs_info
->fs_devices
->total_devices
--;
2528 fs_info
->fs_devices
->total_rw_bytes
-= device
->total_bytes
;
2529 atomic64_sub(device
->total_bytes
, &fs_info
->free_chunk_space
);
2530 btrfs_set_super_total_bytes(fs_info
->super_copy
,
2531 orig_super_total_bytes
);
2532 btrfs_set_super_num_devices(fs_info
->super_copy
,
2533 orig_super_num_devices
);
2534 mutex_unlock(&fs_info
->chunk_mutex
);
2535 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
2538 sb
->s_flags
|= SB_RDONLY
;
2540 btrfs_end_transaction(trans
);
2542 btrfs_free_device(device
);
2544 blkdev_put(bdev
, FMODE_EXCL
);
2545 if (seeding_dev
&& !unlocked
) {
2546 mutex_unlock(&uuid_mutex
);
2547 up_write(&sb
->s_umount
);
2552 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
2553 struct btrfs_device
*device
)
2556 struct btrfs_path
*path
;
2557 struct btrfs_root
*root
= device
->fs_info
->chunk_root
;
2558 struct btrfs_dev_item
*dev_item
;
2559 struct extent_buffer
*leaf
;
2560 struct btrfs_key key
;
2562 path
= btrfs_alloc_path();
2566 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
2567 key
.type
= BTRFS_DEV_ITEM_KEY
;
2568 key
.offset
= device
->devid
;
2570 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2579 leaf
= path
->nodes
[0];
2580 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
2582 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
2583 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
2584 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
2585 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
2586 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
2587 btrfs_set_device_total_bytes(leaf
, dev_item
,
2588 btrfs_device_get_disk_total_bytes(device
));
2589 btrfs_set_device_bytes_used(leaf
, dev_item
,
2590 btrfs_device_get_bytes_used(device
));
2591 btrfs_mark_buffer_dirty(leaf
);
2594 btrfs_free_path(path
);
2598 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
2599 struct btrfs_device
*device
, u64 new_size
)
2601 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
2602 struct btrfs_super_block
*super_copy
= fs_info
->super_copy
;
2603 struct btrfs_fs_devices
*fs_devices
;
2607 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
))
2610 new_size
= round_down(new_size
, fs_info
->sectorsize
);
2612 mutex_lock(&fs_info
->chunk_mutex
);
2613 old_total
= btrfs_super_total_bytes(super_copy
);
2614 diff
= round_down(new_size
- device
->total_bytes
, fs_info
->sectorsize
);
2616 if (new_size
<= device
->total_bytes
||
2617 test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
)) {
2618 mutex_unlock(&fs_info
->chunk_mutex
);
2622 fs_devices
= fs_info
->fs_devices
;
2624 btrfs_set_super_total_bytes(super_copy
,
2625 round_down(old_total
+ diff
, fs_info
->sectorsize
));
2626 device
->fs_devices
->total_rw_bytes
+= diff
;
2628 btrfs_device_set_total_bytes(device
, new_size
);
2629 btrfs_device_set_disk_total_bytes(device
, new_size
);
2630 btrfs_clear_space_info_full(device
->fs_info
);
2631 if (list_empty(&device
->resized_list
))
2632 list_add_tail(&device
->resized_list
,
2633 &fs_devices
->resized_devices
);
2634 mutex_unlock(&fs_info
->chunk_mutex
);
2636 return btrfs_update_device(trans
, device
);
2639 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
, u64 chunk_offset
)
2641 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
2642 struct btrfs_root
*root
= fs_info
->chunk_root
;
2644 struct btrfs_path
*path
;
2645 struct btrfs_key key
;
2647 path
= btrfs_alloc_path();
2651 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2652 key
.offset
= chunk_offset
;
2653 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2655 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2658 else if (ret
> 0) { /* Logic error or corruption */
2659 btrfs_handle_fs_error(fs_info
, -ENOENT
,
2660 "Failed lookup while freeing chunk.");
2665 ret
= btrfs_del_item(trans
, root
, path
);
2667 btrfs_handle_fs_error(fs_info
, ret
,
2668 "Failed to delete chunk item.");
2670 btrfs_free_path(path
);
2674 static int btrfs_del_sys_chunk(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
)
2676 struct btrfs_super_block
*super_copy
= fs_info
->super_copy
;
2677 struct btrfs_disk_key
*disk_key
;
2678 struct btrfs_chunk
*chunk
;
2685 struct btrfs_key key
;
2687 mutex_lock(&fs_info
->chunk_mutex
);
2688 array_size
= btrfs_super_sys_array_size(super_copy
);
2690 ptr
= super_copy
->sys_chunk_array
;
2693 while (cur
< array_size
) {
2694 disk_key
= (struct btrfs_disk_key
*)ptr
;
2695 btrfs_disk_key_to_cpu(&key
, disk_key
);
2697 len
= sizeof(*disk_key
);
2699 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
2700 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
2701 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
2702 len
+= btrfs_chunk_item_size(num_stripes
);
2707 if (key
.objectid
== BTRFS_FIRST_CHUNK_TREE_OBJECTID
&&
2708 key
.offset
== chunk_offset
) {
2709 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
2711 btrfs_set_super_sys_array_size(super_copy
, array_size
);
2717 mutex_unlock(&fs_info
->chunk_mutex
);
2721 static struct extent_map
*get_chunk_map(struct btrfs_fs_info
*fs_info
,
2722 u64 logical
, u64 length
)
2724 struct extent_map_tree
*em_tree
;
2725 struct extent_map
*em
;
2727 em_tree
= &fs_info
->mapping_tree
.map_tree
;
2728 read_lock(&em_tree
->lock
);
2729 em
= lookup_extent_mapping(em_tree
, logical
, length
);
2730 read_unlock(&em_tree
->lock
);
2733 btrfs_crit(fs_info
, "unable to find logical %llu length %llu",
2735 return ERR_PTR(-EINVAL
);
2738 if (em
->start
> logical
|| em
->start
+ em
->len
< logical
) {
2740 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2741 logical
, length
, em
->start
, em
->start
+ em
->len
);
2742 free_extent_map(em
);
2743 return ERR_PTR(-EINVAL
);
2746 /* callers are responsible for dropping em's ref. */
2750 int btrfs_remove_chunk(struct btrfs_trans_handle
*trans
, u64 chunk_offset
)
2752 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
2753 struct extent_map
*em
;
2754 struct map_lookup
*map
;
2755 u64 dev_extent_len
= 0;
2757 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
2759 em
= get_chunk_map(fs_info
, chunk_offset
, 1);
2762 * This is a logic error, but we don't want to just rely on the
2763 * user having built with ASSERT enabled, so if ASSERT doesn't
2764 * do anything we still error out.
2769 map
= em
->map_lookup
;
2770 mutex_lock(&fs_info
->chunk_mutex
);
2771 check_system_chunk(trans
, map
->type
);
2772 mutex_unlock(&fs_info
->chunk_mutex
);
2775 * Take the device list mutex to prevent races with the final phase of
2776 * a device replace operation that replaces the device object associated
2777 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2779 mutex_lock(&fs_devices
->device_list_mutex
);
2780 for (i
= 0; i
< map
->num_stripes
; i
++) {
2781 struct btrfs_device
*device
= map
->stripes
[i
].dev
;
2782 ret
= btrfs_free_dev_extent(trans
, device
,
2783 map
->stripes
[i
].physical
,
2786 mutex_unlock(&fs_devices
->device_list_mutex
);
2787 btrfs_abort_transaction(trans
, ret
);
2791 if (device
->bytes_used
> 0) {
2792 mutex_lock(&fs_info
->chunk_mutex
);
2793 btrfs_device_set_bytes_used(device
,
2794 device
->bytes_used
- dev_extent_len
);
2795 atomic64_add(dev_extent_len
, &fs_info
->free_chunk_space
);
2796 btrfs_clear_space_info_full(fs_info
);
2797 mutex_unlock(&fs_info
->chunk_mutex
);
2800 if (map
->stripes
[i
].dev
) {
2801 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
2803 mutex_unlock(&fs_devices
->device_list_mutex
);
2804 btrfs_abort_transaction(trans
, ret
);
2809 mutex_unlock(&fs_devices
->device_list_mutex
);
2811 ret
= btrfs_free_chunk(trans
, chunk_offset
);
2813 btrfs_abort_transaction(trans
, ret
);
2817 trace_btrfs_chunk_free(fs_info
, map
, chunk_offset
, em
->len
);
2819 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2820 ret
= btrfs_del_sys_chunk(fs_info
, chunk_offset
);
2822 btrfs_abort_transaction(trans
, ret
);
2827 ret
= btrfs_remove_block_group(trans
, chunk_offset
, em
);
2829 btrfs_abort_transaction(trans
, ret
);
2835 free_extent_map(em
);
2839 static int btrfs_relocate_chunk(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
)
2841 struct btrfs_root
*root
= fs_info
->chunk_root
;
2842 struct btrfs_trans_handle
*trans
;
2846 * Prevent races with automatic removal of unused block groups.
2847 * After we relocate and before we remove the chunk with offset
2848 * chunk_offset, automatic removal of the block group can kick in,
2849 * resulting in a failure when calling btrfs_remove_chunk() below.
2851 * Make sure to acquire this mutex before doing a tree search (dev
2852 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2853 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2854 * we release the path used to search the chunk/dev tree and before
2855 * the current task acquires this mutex and calls us.
2857 lockdep_assert_held(&fs_info
->delete_unused_bgs_mutex
);
2859 ret
= btrfs_can_relocate(fs_info
, chunk_offset
);
2863 /* step one, relocate all the extents inside this chunk */
2864 btrfs_scrub_pause(fs_info
);
2865 ret
= btrfs_relocate_block_group(fs_info
, chunk_offset
);
2866 btrfs_scrub_continue(fs_info
);
2871 * We add the kobjects here (and after forcing data chunk creation)
2872 * since relocation is the only place we'll create chunks of a new
2873 * type at runtime. The only place where we'll remove the last
2874 * chunk of a type is the call immediately below this one. Even
2875 * so, we're protected against races with the cleaner thread since
2876 * we're covered by the delete_unused_bgs_mutex.
2878 btrfs_add_raid_kobjects(fs_info
);
2880 trans
= btrfs_start_trans_remove_block_group(root
->fs_info
,
2882 if (IS_ERR(trans
)) {
2883 ret
= PTR_ERR(trans
);
2884 btrfs_handle_fs_error(root
->fs_info
, ret
, NULL
);
2889 * step two, delete the device extents and the
2890 * chunk tree entries
2892 ret
= btrfs_remove_chunk(trans
, chunk_offset
);
2893 btrfs_end_transaction(trans
);
2897 static int btrfs_relocate_sys_chunks(struct btrfs_fs_info
*fs_info
)
2899 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
2900 struct btrfs_path
*path
;
2901 struct extent_buffer
*leaf
;
2902 struct btrfs_chunk
*chunk
;
2903 struct btrfs_key key
;
2904 struct btrfs_key found_key
;
2906 bool retried
= false;
2910 path
= btrfs_alloc_path();
2915 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2916 key
.offset
= (u64
)-1;
2917 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2920 mutex_lock(&fs_info
->delete_unused_bgs_mutex
);
2921 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2923 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
2926 BUG_ON(ret
== 0); /* Corruption */
2928 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
2931 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
2937 leaf
= path
->nodes
[0];
2938 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2940 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
2941 struct btrfs_chunk
);
2942 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2943 btrfs_release_path(path
);
2945 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2946 ret
= btrfs_relocate_chunk(fs_info
, found_key
.offset
);
2952 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
2954 if (found_key
.offset
== 0)
2956 key
.offset
= found_key
.offset
- 1;
2959 if (failed
&& !retried
) {
2963 } else if (WARN_ON(failed
&& retried
)) {
2967 btrfs_free_path(path
);
2972 * return 1 : allocate a data chunk successfully,
2973 * return <0: errors during allocating a data chunk,
2974 * return 0 : no need to allocate a data chunk.
2976 static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info
*fs_info
,
2979 struct btrfs_block_group_cache
*cache
;
2983 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
2985 chunk_type
= cache
->flags
;
2986 btrfs_put_block_group(cache
);
2988 if (chunk_type
& BTRFS_BLOCK_GROUP_DATA
) {
2989 spin_lock(&fs_info
->data_sinfo
->lock
);
2990 bytes_used
= fs_info
->data_sinfo
->bytes_used
;
2991 spin_unlock(&fs_info
->data_sinfo
->lock
);
2994 struct btrfs_trans_handle
*trans
;
2997 trans
= btrfs_join_transaction(fs_info
->tree_root
);
2999 return PTR_ERR(trans
);
3001 ret
= btrfs_force_chunk_alloc(trans
,
3002 BTRFS_BLOCK_GROUP_DATA
);
3003 btrfs_end_transaction(trans
);
3007 btrfs_add_raid_kobjects(fs_info
);
3015 static int insert_balance_item(struct btrfs_fs_info
*fs_info
,
3016 struct btrfs_balance_control
*bctl
)
3018 struct btrfs_root
*root
= fs_info
->tree_root
;
3019 struct btrfs_trans_handle
*trans
;
3020 struct btrfs_balance_item
*item
;
3021 struct btrfs_disk_balance_args disk_bargs
;
3022 struct btrfs_path
*path
;
3023 struct extent_buffer
*leaf
;
3024 struct btrfs_key key
;
3027 path
= btrfs_alloc_path();
3031 trans
= btrfs_start_transaction(root
, 0);
3032 if (IS_ERR(trans
)) {
3033 btrfs_free_path(path
);
3034 return PTR_ERR(trans
);
3037 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
3038 key
.type
= BTRFS_TEMPORARY_ITEM_KEY
;
3041 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
3046 leaf
= path
->nodes
[0];
3047 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
3049 memzero_extent_buffer(leaf
, (unsigned long)item
, sizeof(*item
));
3051 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->data
);
3052 btrfs_set_balance_data(leaf
, item
, &disk_bargs
);
3053 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->meta
);
3054 btrfs_set_balance_meta(leaf
, item
, &disk_bargs
);
3055 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->sys
);
3056 btrfs_set_balance_sys(leaf
, item
, &disk_bargs
);
3058 btrfs_set_balance_flags(leaf
, item
, bctl
->flags
);
3060 btrfs_mark_buffer_dirty(leaf
);
3062 btrfs_free_path(path
);
3063 err
= btrfs_commit_transaction(trans
);
3069 static int del_balance_item(struct btrfs_fs_info
*fs_info
)
3071 struct btrfs_root
*root
= fs_info
->tree_root
;
3072 struct btrfs_trans_handle
*trans
;
3073 struct btrfs_path
*path
;
3074 struct btrfs_key key
;
3077 path
= btrfs_alloc_path();
3081 trans
= btrfs_start_transaction(root
, 0);
3082 if (IS_ERR(trans
)) {
3083 btrfs_free_path(path
);
3084 return PTR_ERR(trans
);
3087 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
3088 key
.type
= BTRFS_TEMPORARY_ITEM_KEY
;
3091 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
3099 ret
= btrfs_del_item(trans
, root
, path
);
3101 btrfs_free_path(path
);
3102 err
= btrfs_commit_transaction(trans
);
3109 * This is a heuristic used to reduce the number of chunks balanced on
3110 * resume after balance was interrupted.
3112 static void update_balance_args(struct btrfs_balance_control
*bctl
)
3115 * Turn on soft mode for chunk types that were being converted.
3117 if (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
3118 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
3119 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
3120 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
3121 if (bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
3122 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
3125 * Turn on usage filter if is not already used. The idea is
3126 * that chunks that we have already balanced should be
3127 * reasonably full. Don't do it for chunks that are being
3128 * converted - that will keep us from relocating unconverted
3129 * (albeit full) chunks.
3131 if (!(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
3132 !(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_USAGE_RANGE
) &&
3133 !(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
3134 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
3135 bctl
->data
.usage
= 90;
3137 if (!(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
3138 !(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_USAGE_RANGE
) &&
3139 !(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
3140 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
3141 bctl
->sys
.usage
= 90;
3143 if (!(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
3144 !(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_USAGE_RANGE
) &&
3145 !(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
3146 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
3147 bctl
->meta
.usage
= 90;
3152 * Clear the balance status in fs_info and delete the balance item from disk.
3154 static void reset_balance_state(struct btrfs_fs_info
*fs_info
)
3156 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3159 BUG_ON(!fs_info
->balance_ctl
);
3161 spin_lock(&fs_info
->balance_lock
);
3162 fs_info
->balance_ctl
= NULL
;
3163 spin_unlock(&fs_info
->balance_lock
);
3166 ret
= del_balance_item(fs_info
);
3168 btrfs_handle_fs_error(fs_info
, ret
, NULL
);
3172 * Balance filters. Return 1 if chunk should be filtered out
3173 * (should not be balanced).
3175 static int chunk_profiles_filter(u64 chunk_type
,
3176 struct btrfs_balance_args
*bargs
)
3178 chunk_type
= chunk_to_extended(chunk_type
) &
3179 BTRFS_EXTENDED_PROFILE_MASK
;
3181 if (bargs
->profiles
& chunk_type
)
3187 static int chunk_usage_range_filter(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
,
3188 struct btrfs_balance_args
*bargs
)
3190 struct btrfs_block_group_cache
*cache
;
3192 u64 user_thresh_min
;
3193 u64 user_thresh_max
;
3196 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
3197 chunk_used
= btrfs_block_group_used(&cache
->item
);
3199 if (bargs
->usage_min
== 0)
3200 user_thresh_min
= 0;
3202 user_thresh_min
= div_factor_fine(cache
->key
.offset
,
3205 if (bargs
->usage_max
== 0)
3206 user_thresh_max
= 1;
3207 else if (bargs
->usage_max
> 100)
3208 user_thresh_max
= cache
->key
.offset
;
3210 user_thresh_max
= div_factor_fine(cache
->key
.offset
,
3213 if (user_thresh_min
<= chunk_used
&& chunk_used
< user_thresh_max
)
3216 btrfs_put_block_group(cache
);
3220 static int chunk_usage_filter(struct btrfs_fs_info
*fs_info
,
3221 u64 chunk_offset
, struct btrfs_balance_args
*bargs
)
3223 struct btrfs_block_group_cache
*cache
;
3224 u64 chunk_used
, user_thresh
;
3227 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
3228 chunk_used
= btrfs_block_group_used(&cache
->item
);
3230 if (bargs
->usage_min
== 0)
3232 else if (bargs
->usage
> 100)
3233 user_thresh
= cache
->key
.offset
;
3235 user_thresh
= div_factor_fine(cache
->key
.offset
,
3238 if (chunk_used
< user_thresh
)
3241 btrfs_put_block_group(cache
);
3245 static int chunk_devid_filter(struct extent_buffer
*leaf
,
3246 struct btrfs_chunk
*chunk
,
3247 struct btrfs_balance_args
*bargs
)
3249 struct btrfs_stripe
*stripe
;
3250 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
3253 for (i
= 0; i
< num_stripes
; i
++) {
3254 stripe
= btrfs_stripe_nr(chunk
, i
);
3255 if (btrfs_stripe_devid(leaf
, stripe
) == bargs
->devid
)
3262 /* [pstart, pend) */
3263 static int chunk_drange_filter(struct extent_buffer
*leaf
,
3264 struct btrfs_chunk
*chunk
,
3265 struct btrfs_balance_args
*bargs
)
3267 struct btrfs_stripe
*stripe
;
3268 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
3274 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
))
3277 if (btrfs_chunk_type(leaf
, chunk
) & (BTRFS_BLOCK_GROUP_DUP
|
3278 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
)) {
3279 factor
= num_stripes
/ 2;
3280 } else if (btrfs_chunk_type(leaf
, chunk
) & BTRFS_BLOCK_GROUP_RAID5
) {
3281 factor
= num_stripes
- 1;
3282 } else if (btrfs_chunk_type(leaf
, chunk
) & BTRFS_BLOCK_GROUP_RAID6
) {
3283 factor
= num_stripes
- 2;
3285 factor
= num_stripes
;
3288 for (i
= 0; i
< num_stripes
; i
++) {
3289 stripe
= btrfs_stripe_nr(chunk
, i
);
3290 if (btrfs_stripe_devid(leaf
, stripe
) != bargs
->devid
)
3293 stripe_offset
= btrfs_stripe_offset(leaf
, stripe
);
3294 stripe_length
= btrfs_chunk_length(leaf
, chunk
);
3295 stripe_length
= div_u64(stripe_length
, factor
);
3297 if (stripe_offset
< bargs
->pend
&&
3298 stripe_offset
+ stripe_length
> bargs
->pstart
)
3305 /* [vstart, vend) */
3306 static int chunk_vrange_filter(struct extent_buffer
*leaf
,
3307 struct btrfs_chunk
*chunk
,
3309 struct btrfs_balance_args
*bargs
)
3311 if (chunk_offset
< bargs
->vend
&&
3312 chunk_offset
+ btrfs_chunk_length(leaf
, chunk
) > bargs
->vstart
)
3313 /* at least part of the chunk is inside this vrange */
3319 static int chunk_stripes_range_filter(struct extent_buffer
*leaf
,
3320 struct btrfs_chunk
*chunk
,
3321 struct btrfs_balance_args
*bargs
)
3323 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
3325 if (bargs
->stripes_min
<= num_stripes
3326 && num_stripes
<= bargs
->stripes_max
)
3332 static int chunk_soft_convert_filter(u64 chunk_type
,
3333 struct btrfs_balance_args
*bargs
)
3335 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_CONVERT
))
3338 chunk_type
= chunk_to_extended(chunk_type
) &
3339 BTRFS_EXTENDED_PROFILE_MASK
;
3341 if (bargs
->target
== chunk_type
)
3347 static int should_balance_chunk(struct btrfs_fs_info
*fs_info
,
3348 struct extent_buffer
*leaf
,
3349 struct btrfs_chunk
*chunk
, u64 chunk_offset
)
3351 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3352 struct btrfs_balance_args
*bargs
= NULL
;
3353 u64 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
3356 if (!((chunk_type
& BTRFS_BLOCK_GROUP_TYPE_MASK
) &
3357 (bctl
->flags
& BTRFS_BALANCE_TYPE_MASK
))) {
3361 if (chunk_type
& BTRFS_BLOCK_GROUP_DATA
)
3362 bargs
= &bctl
->data
;
3363 else if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
)
3365 else if (chunk_type
& BTRFS_BLOCK_GROUP_METADATA
)
3366 bargs
= &bctl
->meta
;
3368 /* profiles filter */
3369 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_PROFILES
) &&
3370 chunk_profiles_filter(chunk_type
, bargs
)) {
3375 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
3376 chunk_usage_filter(fs_info
, chunk_offset
, bargs
)) {
3378 } else if ((bargs
->flags
& BTRFS_BALANCE_ARGS_USAGE_RANGE
) &&
3379 chunk_usage_range_filter(fs_info
, chunk_offset
, bargs
)) {
3384 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
) &&
3385 chunk_devid_filter(leaf
, chunk
, bargs
)) {
3389 /* drange filter, makes sense only with devid filter */
3390 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DRANGE
) &&
3391 chunk_drange_filter(leaf
, chunk
, bargs
)) {
3396 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_VRANGE
) &&
3397 chunk_vrange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
3401 /* stripes filter */
3402 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_STRIPES_RANGE
) &&
3403 chunk_stripes_range_filter(leaf
, chunk
, bargs
)) {
3407 /* soft profile changing mode */
3408 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_SOFT
) &&
3409 chunk_soft_convert_filter(chunk_type
, bargs
)) {
3414 * limited by count, must be the last filter
3416 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_LIMIT
)) {
3417 if (bargs
->limit
== 0)
3421 } else if ((bargs
->flags
& BTRFS_BALANCE_ARGS_LIMIT_RANGE
)) {
3423 * Same logic as the 'limit' filter; the minimum cannot be
3424 * determined here because we do not have the global information
3425 * about the count of all chunks that satisfy the filters.
3427 if (bargs
->limit_max
== 0)
3436 static int __btrfs_balance(struct btrfs_fs_info
*fs_info
)
3438 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3439 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
3440 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
3441 struct list_head
*devices
;
3442 struct btrfs_device
*device
;
3446 struct btrfs_chunk
*chunk
;
3447 struct btrfs_path
*path
= NULL
;
3448 struct btrfs_key key
;
3449 struct btrfs_key found_key
;
3450 struct btrfs_trans_handle
*trans
;
3451 struct extent_buffer
*leaf
;
3454 int enospc_errors
= 0;
3455 bool counting
= true;
3456 /* The single value limit and min/max limits use the same bytes in the */
3457 u64 limit_data
= bctl
->data
.limit
;
3458 u64 limit_meta
= bctl
->meta
.limit
;
3459 u64 limit_sys
= bctl
->sys
.limit
;
3463 int chunk_reserved
= 0;
3465 /* step one make some room on all the devices */
3466 devices
= &fs_info
->fs_devices
->devices
;
3467 list_for_each_entry(device
, devices
, dev_list
) {
3468 old_size
= btrfs_device_get_total_bytes(device
);
3469 size_to_free
= div_factor(old_size
, 1);
3470 size_to_free
= min_t(u64
, size_to_free
, SZ_1M
);
3471 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
) ||
3472 btrfs_device_get_total_bytes(device
) -
3473 btrfs_device_get_bytes_used(device
) > size_to_free
||
3474 test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
))
3477 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
3481 /* btrfs_shrink_device never returns ret > 0 */
3486 trans
= btrfs_start_transaction(dev_root
, 0);
3487 if (IS_ERR(trans
)) {
3488 ret
= PTR_ERR(trans
);
3489 btrfs_info_in_rcu(fs_info
,
3490 "resize: unable to start transaction after shrinking device %s (error %d), old size %llu, new size %llu",
3491 rcu_str_deref(device
->name
), ret
,
3492 old_size
, old_size
- size_to_free
);
3496 ret
= btrfs_grow_device(trans
, device
, old_size
);
3498 btrfs_end_transaction(trans
);
3499 /* btrfs_grow_device never returns ret > 0 */
3501 btrfs_info_in_rcu(fs_info
,
3502 "resize: unable to grow device after shrinking device %s (error %d), old size %llu, new size %llu",
3503 rcu_str_deref(device
->name
), ret
,
3504 old_size
, old_size
- size_to_free
);
3508 btrfs_end_transaction(trans
);
3511 /* step two, relocate all the chunks */
3512 path
= btrfs_alloc_path();
3518 /* zero out stat counters */
3519 spin_lock(&fs_info
->balance_lock
);
3520 memset(&bctl
->stat
, 0, sizeof(bctl
->stat
));
3521 spin_unlock(&fs_info
->balance_lock
);
3525 * The single value limit and min/max limits use the same bytes
3528 bctl
->data
.limit
= limit_data
;
3529 bctl
->meta
.limit
= limit_meta
;
3530 bctl
->sys
.limit
= limit_sys
;
3532 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
3533 key
.offset
= (u64
)-1;
3534 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
3537 if ((!counting
&& atomic_read(&fs_info
->balance_pause_req
)) ||
3538 atomic_read(&fs_info
->balance_cancel_req
)) {
3543 mutex_lock(&fs_info
->delete_unused_bgs_mutex
);
3544 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
3546 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3551 * this shouldn't happen, it means the last relocate
3555 BUG(); /* FIXME break ? */
3557 ret
= btrfs_previous_item(chunk_root
, path
, 0,
3558 BTRFS_CHUNK_ITEM_KEY
);
3560 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3565 leaf
= path
->nodes
[0];
3566 slot
= path
->slots
[0];
3567 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3569 if (found_key
.objectid
!= key
.objectid
) {
3570 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3574 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
3575 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
3578 spin_lock(&fs_info
->balance_lock
);
3579 bctl
->stat
.considered
++;
3580 spin_unlock(&fs_info
->balance_lock
);
3583 ret
= should_balance_chunk(fs_info
, leaf
, chunk
,
3586 btrfs_release_path(path
);
3588 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3593 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3594 spin_lock(&fs_info
->balance_lock
);
3595 bctl
->stat
.expected
++;
3596 spin_unlock(&fs_info
->balance_lock
);
3598 if (chunk_type
& BTRFS_BLOCK_GROUP_DATA
)
3600 else if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
)
3602 else if (chunk_type
& BTRFS_BLOCK_GROUP_METADATA
)
3609 * Apply limit_min filter, no need to check if the LIMITS
3610 * filter is used, limit_min is 0 by default
3612 if (((chunk_type
& BTRFS_BLOCK_GROUP_DATA
) &&
3613 count_data
< bctl
->data
.limit_min
)
3614 || ((chunk_type
& BTRFS_BLOCK_GROUP_METADATA
) &&
3615 count_meta
< bctl
->meta
.limit_min
)
3616 || ((chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) &&
3617 count_sys
< bctl
->sys
.limit_min
)) {
3618 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3622 if (!chunk_reserved
) {
3624 * We may be relocating the only data chunk we have,
3625 * which could potentially end up with losing data's
3626 * raid profile, so lets allocate an empty one in
3629 ret
= btrfs_may_alloc_data_chunk(fs_info
,
3632 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3634 } else if (ret
== 1) {
3639 ret
= btrfs_relocate_chunk(fs_info
, found_key
.offset
);
3640 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3641 if (ret
&& ret
!= -ENOSPC
)
3643 if (ret
== -ENOSPC
) {
3646 spin_lock(&fs_info
->balance_lock
);
3647 bctl
->stat
.completed
++;
3648 spin_unlock(&fs_info
->balance_lock
);
3651 if (found_key
.offset
== 0)
3653 key
.offset
= found_key
.offset
- 1;
3657 btrfs_release_path(path
);
3662 btrfs_free_path(path
);
3663 if (enospc_errors
) {
3664 btrfs_info(fs_info
, "%d enospc errors during balance",
3674 * alloc_profile_is_valid - see if a given profile is valid and reduced
3675 * @flags: profile to validate
3676 * @extended: if true @flags is treated as an extended profile
3678 static int alloc_profile_is_valid(u64 flags
, int extended
)
3680 u64 mask
= (extended
? BTRFS_EXTENDED_PROFILE_MASK
:
3681 BTRFS_BLOCK_GROUP_PROFILE_MASK
);
3683 flags
&= ~BTRFS_BLOCK_GROUP_TYPE_MASK
;
3685 /* 1) check that all other bits are zeroed */
3689 /* 2) see if profile is reduced */
3691 return !extended
; /* "0" is valid for usual profiles */
3693 /* true if exactly one bit set */
3694 return is_power_of_2(flags
);
3697 static inline int balance_need_close(struct btrfs_fs_info
*fs_info
)
3699 /* cancel requested || normal exit path */
3700 return atomic_read(&fs_info
->balance_cancel_req
) ||
3701 (atomic_read(&fs_info
->balance_pause_req
) == 0 &&
3702 atomic_read(&fs_info
->balance_cancel_req
) == 0);
3705 /* Non-zero return value signifies invalidity */
3706 static inline int validate_convert_profile(struct btrfs_balance_args
*bctl_arg
,
3709 return ((bctl_arg
->flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3710 (!alloc_profile_is_valid(bctl_arg
->target
, 1) ||
3711 (bctl_arg
->target
& ~allowed
)));
3715 * Should be called with balance mutexe held
3717 int btrfs_balance(struct btrfs_fs_info
*fs_info
,
3718 struct btrfs_balance_control
*bctl
,
3719 struct btrfs_ioctl_balance_args
*bargs
)
3721 u64 meta_target
, data_target
;
3728 if (btrfs_fs_closing(fs_info
) ||
3729 atomic_read(&fs_info
->balance_pause_req
) ||
3730 atomic_read(&fs_info
->balance_cancel_req
)) {
3735 allowed
= btrfs_super_incompat_flags(fs_info
->super_copy
);
3736 if (allowed
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
3740 * In case of mixed groups both data and meta should be picked,
3741 * and identical options should be given for both of them.
3743 allowed
= BTRFS_BALANCE_DATA
| BTRFS_BALANCE_METADATA
;
3744 if (mixed
&& (bctl
->flags
& allowed
)) {
3745 if (!(bctl
->flags
& BTRFS_BALANCE_DATA
) ||
3746 !(bctl
->flags
& BTRFS_BALANCE_METADATA
) ||
3747 memcmp(&bctl
->data
, &bctl
->meta
, sizeof(bctl
->data
))) {
3749 "balance: mixed groups data and metadata options must be the same");
3755 num_devices
= btrfs_num_devices(fs_info
);
3757 allowed
= BTRFS_AVAIL_ALLOC_BIT_SINGLE
| BTRFS_BLOCK_GROUP_DUP
;
3758 if (num_devices
> 1)
3759 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
);
3760 if (num_devices
> 2)
3761 allowed
|= BTRFS_BLOCK_GROUP_RAID5
;
3762 if (num_devices
> 3)
3763 allowed
|= (BTRFS_BLOCK_GROUP_RAID10
|
3764 BTRFS_BLOCK_GROUP_RAID6
);
3765 if (validate_convert_profile(&bctl
->data
, allowed
)) {
3766 int index
= btrfs_bg_flags_to_raid_index(bctl
->data
.target
);
3769 "balance: invalid convert data profile %s",
3770 get_raid_name(index
));
3774 if (validate_convert_profile(&bctl
->meta
, allowed
)) {
3775 int index
= btrfs_bg_flags_to_raid_index(bctl
->meta
.target
);
3778 "balance: invalid convert metadata profile %s",
3779 get_raid_name(index
));
3783 if (validate_convert_profile(&bctl
->sys
, allowed
)) {
3784 int index
= btrfs_bg_flags_to_raid_index(bctl
->sys
.target
);
3787 "balance: invalid convert system profile %s",
3788 get_raid_name(index
));
3793 /* allow to reduce meta or sys integrity only if force set */
3794 allowed
= BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
3795 BTRFS_BLOCK_GROUP_RAID10
|
3796 BTRFS_BLOCK_GROUP_RAID5
|
3797 BTRFS_BLOCK_GROUP_RAID6
;
3799 seq
= read_seqbegin(&fs_info
->profiles_lock
);
3801 if (((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3802 (fs_info
->avail_system_alloc_bits
& allowed
) &&
3803 !(bctl
->sys
.target
& allowed
)) ||
3804 ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3805 (fs_info
->avail_metadata_alloc_bits
& allowed
) &&
3806 !(bctl
->meta
.target
& allowed
))) {
3807 if (bctl
->flags
& BTRFS_BALANCE_FORCE
) {
3809 "balance: force reducing metadata integrity");
3812 "balance: reduces metadata integrity, use --force if you want this");
3817 } while (read_seqretry(&fs_info
->profiles_lock
, seq
));
3819 /* if we're not converting, the target field is uninitialized */
3820 meta_target
= (bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) ?
3821 bctl
->meta
.target
: fs_info
->avail_metadata_alloc_bits
;
3822 data_target
= (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) ?
3823 bctl
->data
.target
: fs_info
->avail_data_alloc_bits
;
3824 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target
) <
3825 btrfs_get_num_tolerated_disk_barrier_failures(data_target
)) {
3826 int meta_index
= btrfs_bg_flags_to_raid_index(meta_target
);
3827 int data_index
= btrfs_bg_flags_to_raid_index(data_target
);
3830 "balance: metadata profile %s has lower redundancy than data profile %s",
3831 get_raid_name(meta_index
), get_raid_name(data_index
));
3834 ret
= insert_balance_item(fs_info
, bctl
);
3835 if (ret
&& ret
!= -EEXIST
)
3838 if (!(bctl
->flags
& BTRFS_BALANCE_RESUME
)) {
3839 BUG_ON(ret
== -EEXIST
);
3840 BUG_ON(fs_info
->balance_ctl
);
3841 spin_lock(&fs_info
->balance_lock
);
3842 fs_info
->balance_ctl
= bctl
;
3843 spin_unlock(&fs_info
->balance_lock
);
3845 BUG_ON(ret
!= -EEXIST
);
3846 spin_lock(&fs_info
->balance_lock
);
3847 update_balance_args(bctl
);
3848 spin_unlock(&fs_info
->balance_lock
);
3851 ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
));
3852 set_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
);
3853 mutex_unlock(&fs_info
->balance_mutex
);
3855 ret
= __btrfs_balance(fs_info
);
3857 mutex_lock(&fs_info
->balance_mutex
);
3858 clear_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
);
3861 memset(bargs
, 0, sizeof(*bargs
));
3862 btrfs_update_ioctl_balance_args(fs_info
, bargs
);
3865 if ((ret
&& ret
!= -ECANCELED
&& ret
!= -ENOSPC
) ||
3866 balance_need_close(fs_info
)) {
3867 reset_balance_state(fs_info
);
3868 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
3871 wake_up(&fs_info
->balance_wait_q
);
3875 if (bctl
->flags
& BTRFS_BALANCE_RESUME
)
3876 reset_balance_state(fs_info
);
3879 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
3884 static int balance_kthread(void *data
)
3886 struct btrfs_fs_info
*fs_info
= data
;
3889 mutex_lock(&fs_info
->balance_mutex
);
3890 if (fs_info
->balance_ctl
) {
3891 btrfs_info(fs_info
, "balance: resuming");
3892 ret
= btrfs_balance(fs_info
, fs_info
->balance_ctl
, NULL
);
3894 mutex_unlock(&fs_info
->balance_mutex
);
3899 int btrfs_resume_balance_async(struct btrfs_fs_info
*fs_info
)
3901 struct task_struct
*tsk
;
3903 mutex_lock(&fs_info
->balance_mutex
);
3904 if (!fs_info
->balance_ctl
) {
3905 mutex_unlock(&fs_info
->balance_mutex
);
3908 mutex_unlock(&fs_info
->balance_mutex
);
3910 if (btrfs_test_opt(fs_info
, SKIP_BALANCE
)) {
3911 btrfs_info(fs_info
, "balance: resume skipped");
3916 * A ro->rw remount sequence should continue with the paused balance
3917 * regardless of who pauses it, system or the user as of now, so set
3920 spin_lock(&fs_info
->balance_lock
);
3921 fs_info
->balance_ctl
->flags
|= BTRFS_BALANCE_RESUME
;
3922 spin_unlock(&fs_info
->balance_lock
);
3924 tsk
= kthread_run(balance_kthread
, fs_info
, "btrfs-balance");
3925 return PTR_ERR_OR_ZERO(tsk
);
3928 int btrfs_recover_balance(struct btrfs_fs_info
*fs_info
)
3930 struct btrfs_balance_control
*bctl
;
3931 struct btrfs_balance_item
*item
;
3932 struct btrfs_disk_balance_args disk_bargs
;
3933 struct btrfs_path
*path
;
3934 struct extent_buffer
*leaf
;
3935 struct btrfs_key key
;
3938 path
= btrfs_alloc_path();
3942 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
3943 key
.type
= BTRFS_TEMPORARY_ITEM_KEY
;
3946 ret
= btrfs_search_slot(NULL
, fs_info
->tree_root
, &key
, path
, 0, 0);
3949 if (ret
> 0) { /* ret = -ENOENT; */
3954 bctl
= kzalloc(sizeof(*bctl
), GFP_NOFS
);
3960 leaf
= path
->nodes
[0];
3961 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
3963 bctl
->flags
= btrfs_balance_flags(leaf
, item
);
3964 bctl
->flags
|= BTRFS_BALANCE_RESUME
;
3966 btrfs_balance_data(leaf
, item
, &disk_bargs
);
3967 btrfs_disk_balance_args_to_cpu(&bctl
->data
, &disk_bargs
);
3968 btrfs_balance_meta(leaf
, item
, &disk_bargs
);
3969 btrfs_disk_balance_args_to_cpu(&bctl
->meta
, &disk_bargs
);
3970 btrfs_balance_sys(leaf
, item
, &disk_bargs
);
3971 btrfs_disk_balance_args_to_cpu(&bctl
->sys
, &disk_bargs
);
3974 * This should never happen, as the paused balance state is recovered
3975 * during mount without any chance of other exclusive ops to collide.
3977 * This gives the exclusive op status to balance and keeps in paused
3978 * state until user intervention (cancel or umount). If the ownership
3979 * cannot be assigned, show a message but do not fail. The balance
3980 * is in a paused state and must have fs_info::balance_ctl properly
3983 if (test_and_set_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
))
3985 "balance: cannot set exclusive op status, resume manually");
3987 mutex_lock(&fs_info
->balance_mutex
);
3988 BUG_ON(fs_info
->balance_ctl
);
3989 spin_lock(&fs_info
->balance_lock
);
3990 fs_info
->balance_ctl
= bctl
;
3991 spin_unlock(&fs_info
->balance_lock
);
3992 mutex_unlock(&fs_info
->balance_mutex
);
3994 btrfs_free_path(path
);
3998 int btrfs_pause_balance(struct btrfs_fs_info
*fs_info
)
4002 mutex_lock(&fs_info
->balance_mutex
);
4003 if (!fs_info
->balance_ctl
) {
4004 mutex_unlock(&fs_info
->balance_mutex
);
4008 if (test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
)) {
4009 atomic_inc(&fs_info
->balance_pause_req
);
4010 mutex_unlock(&fs_info
->balance_mutex
);
4012 wait_event(fs_info
->balance_wait_q
,
4013 !test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
));
4015 mutex_lock(&fs_info
->balance_mutex
);
4016 /* we are good with balance_ctl ripped off from under us */
4017 BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
));
4018 atomic_dec(&fs_info
->balance_pause_req
);
4023 mutex_unlock(&fs_info
->balance_mutex
);
4027 int btrfs_cancel_balance(struct btrfs_fs_info
*fs_info
)
4029 mutex_lock(&fs_info
->balance_mutex
);
4030 if (!fs_info
->balance_ctl
) {
4031 mutex_unlock(&fs_info
->balance_mutex
);
4036 * A paused balance with the item stored on disk can be resumed at
4037 * mount time if the mount is read-write. Otherwise it's still paused
4038 * and we must not allow cancelling as it deletes the item.
4040 if (sb_rdonly(fs_info
->sb
)) {
4041 mutex_unlock(&fs_info
->balance_mutex
);
4045 atomic_inc(&fs_info
->balance_cancel_req
);
4047 * if we are running just wait and return, balance item is
4048 * deleted in btrfs_balance in this case
4050 if (test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
)) {
4051 mutex_unlock(&fs_info
->balance_mutex
);
4052 wait_event(fs_info
->balance_wait_q
,
4053 !test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
));
4054 mutex_lock(&fs_info
->balance_mutex
);
4056 mutex_unlock(&fs_info
->balance_mutex
);
4058 * Lock released to allow other waiters to continue, we'll
4059 * reexamine the status again.
4061 mutex_lock(&fs_info
->balance_mutex
);
4063 if (fs_info
->balance_ctl
) {
4064 reset_balance_state(fs_info
);
4065 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
4066 btrfs_info(fs_info
, "balance: canceled");
4070 BUG_ON(fs_info
->balance_ctl
||
4071 test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
));
4072 atomic_dec(&fs_info
->balance_cancel_req
);
4073 mutex_unlock(&fs_info
->balance_mutex
);
4077 static int btrfs_uuid_scan_kthread(void *data
)
4079 struct btrfs_fs_info
*fs_info
= data
;
4080 struct btrfs_root
*root
= fs_info
->tree_root
;
4081 struct btrfs_key key
;
4082 struct btrfs_path
*path
= NULL
;
4084 struct extent_buffer
*eb
;
4086 struct btrfs_root_item root_item
;
4088 struct btrfs_trans_handle
*trans
= NULL
;
4090 path
= btrfs_alloc_path();
4097 key
.type
= BTRFS_ROOT_ITEM_KEY
;
4101 ret
= btrfs_search_forward(root
, &key
, path
,
4102 BTRFS_OLDEST_GENERATION
);
4109 if (key
.type
!= BTRFS_ROOT_ITEM_KEY
||
4110 (key
.objectid
< BTRFS_FIRST_FREE_OBJECTID
&&
4111 key
.objectid
!= BTRFS_FS_TREE_OBJECTID
) ||
4112 key
.objectid
> BTRFS_LAST_FREE_OBJECTID
)
4115 eb
= path
->nodes
[0];
4116 slot
= path
->slots
[0];
4117 item_size
= btrfs_item_size_nr(eb
, slot
);
4118 if (item_size
< sizeof(root_item
))
4121 read_extent_buffer(eb
, &root_item
,
4122 btrfs_item_ptr_offset(eb
, slot
),
4123 (int)sizeof(root_item
));
4124 if (btrfs_root_refs(&root_item
) == 0)
4127 if (!btrfs_is_empty_uuid(root_item
.uuid
) ||
4128 !btrfs_is_empty_uuid(root_item
.received_uuid
)) {
4132 btrfs_release_path(path
);
4134 * 1 - subvol uuid item
4135 * 1 - received_subvol uuid item
4137 trans
= btrfs_start_transaction(fs_info
->uuid_root
, 2);
4138 if (IS_ERR(trans
)) {
4139 ret
= PTR_ERR(trans
);
4147 if (!btrfs_is_empty_uuid(root_item
.uuid
)) {
4148 ret
= btrfs_uuid_tree_add(trans
, root_item
.uuid
,
4149 BTRFS_UUID_KEY_SUBVOL
,
4152 btrfs_warn(fs_info
, "uuid_tree_add failed %d",
4158 if (!btrfs_is_empty_uuid(root_item
.received_uuid
)) {
4159 ret
= btrfs_uuid_tree_add(trans
,
4160 root_item
.received_uuid
,
4161 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
4164 btrfs_warn(fs_info
, "uuid_tree_add failed %d",
4172 ret
= btrfs_end_transaction(trans
);
4178 btrfs_release_path(path
);
4179 if (key
.offset
< (u64
)-1) {
4181 } else if (key
.type
< BTRFS_ROOT_ITEM_KEY
) {
4183 key
.type
= BTRFS_ROOT_ITEM_KEY
;
4184 } else if (key
.objectid
< (u64
)-1) {
4186 key
.type
= BTRFS_ROOT_ITEM_KEY
;
4195 btrfs_free_path(path
);
4196 if (trans
&& !IS_ERR(trans
))
4197 btrfs_end_transaction(trans
);
4199 btrfs_warn(fs_info
, "btrfs_uuid_scan_kthread failed %d", ret
);
4201 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN
, &fs_info
->flags
);
4202 up(&fs_info
->uuid_tree_rescan_sem
);
4207 * Callback for btrfs_uuid_tree_iterate().
4209 * 0 check succeeded, the entry is not outdated.
4210 * < 0 if an error occurred.
4211 * > 0 if the check failed, which means the caller shall remove the entry.
4213 static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info
*fs_info
,
4214 u8
*uuid
, u8 type
, u64 subid
)
4216 struct btrfs_key key
;
4218 struct btrfs_root
*subvol_root
;
4220 if (type
!= BTRFS_UUID_KEY_SUBVOL
&&
4221 type
!= BTRFS_UUID_KEY_RECEIVED_SUBVOL
)
4224 key
.objectid
= subid
;
4225 key
.type
= BTRFS_ROOT_ITEM_KEY
;
4226 key
.offset
= (u64
)-1;
4227 subvol_root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
4228 if (IS_ERR(subvol_root
)) {
4229 ret
= PTR_ERR(subvol_root
);
4236 case BTRFS_UUID_KEY_SUBVOL
:
4237 if (memcmp(uuid
, subvol_root
->root_item
.uuid
, BTRFS_UUID_SIZE
))
4240 case BTRFS_UUID_KEY_RECEIVED_SUBVOL
:
4241 if (memcmp(uuid
, subvol_root
->root_item
.received_uuid
,
4251 static int btrfs_uuid_rescan_kthread(void *data
)
4253 struct btrfs_fs_info
*fs_info
= (struct btrfs_fs_info
*)data
;
4257 * 1st step is to iterate through the existing UUID tree and
4258 * to delete all entries that contain outdated data.
4259 * 2nd step is to add all missing entries to the UUID tree.
4261 ret
= btrfs_uuid_tree_iterate(fs_info
, btrfs_check_uuid_tree_entry
);
4263 btrfs_warn(fs_info
, "iterating uuid_tree failed %d", ret
);
4264 up(&fs_info
->uuid_tree_rescan_sem
);
4267 return btrfs_uuid_scan_kthread(data
);
4270 int btrfs_create_uuid_tree(struct btrfs_fs_info
*fs_info
)
4272 struct btrfs_trans_handle
*trans
;
4273 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
4274 struct btrfs_root
*uuid_root
;
4275 struct task_struct
*task
;
4282 trans
= btrfs_start_transaction(tree_root
, 2);
4284 return PTR_ERR(trans
);
4286 uuid_root
= btrfs_create_tree(trans
, fs_info
,
4287 BTRFS_UUID_TREE_OBJECTID
);
4288 if (IS_ERR(uuid_root
)) {
4289 ret
= PTR_ERR(uuid_root
);
4290 btrfs_abort_transaction(trans
, ret
);
4291 btrfs_end_transaction(trans
);
4295 fs_info
->uuid_root
= uuid_root
;
4297 ret
= btrfs_commit_transaction(trans
);
4301 down(&fs_info
->uuid_tree_rescan_sem
);
4302 task
= kthread_run(btrfs_uuid_scan_kthread
, fs_info
, "btrfs-uuid");
4304 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
4305 btrfs_warn(fs_info
, "failed to start uuid_scan task");
4306 up(&fs_info
->uuid_tree_rescan_sem
);
4307 return PTR_ERR(task
);
4313 int btrfs_check_uuid_tree(struct btrfs_fs_info
*fs_info
)
4315 struct task_struct
*task
;
4317 down(&fs_info
->uuid_tree_rescan_sem
);
4318 task
= kthread_run(btrfs_uuid_rescan_kthread
, fs_info
, "btrfs-uuid");
4320 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
4321 btrfs_warn(fs_info
, "failed to start uuid_rescan task");
4322 up(&fs_info
->uuid_tree_rescan_sem
);
4323 return PTR_ERR(task
);
4330 * shrinking a device means finding all of the device extents past
4331 * the new size, and then following the back refs to the chunks.
4332 * The chunk relocation code actually frees the device extent
4334 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
4336 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
4337 struct btrfs_root
*root
= fs_info
->dev_root
;
4338 struct btrfs_trans_handle
*trans
;
4339 struct btrfs_dev_extent
*dev_extent
= NULL
;
4340 struct btrfs_path
*path
;
4346 bool retried
= false;
4347 bool checked_pending_chunks
= false;
4348 struct extent_buffer
*l
;
4349 struct btrfs_key key
;
4350 struct btrfs_super_block
*super_copy
= fs_info
->super_copy
;
4351 u64 old_total
= btrfs_super_total_bytes(super_copy
);
4352 u64 old_size
= btrfs_device_get_total_bytes(device
);
4355 new_size
= round_down(new_size
, fs_info
->sectorsize
);
4356 diff
= round_down(old_size
- new_size
, fs_info
->sectorsize
);
4358 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
))
4361 path
= btrfs_alloc_path();
4365 path
->reada
= READA_BACK
;
4367 mutex_lock(&fs_info
->chunk_mutex
);
4369 btrfs_device_set_total_bytes(device
, new_size
);
4370 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
)) {
4371 device
->fs_devices
->total_rw_bytes
-= diff
;
4372 atomic64_sub(diff
, &fs_info
->free_chunk_space
);
4374 mutex_unlock(&fs_info
->chunk_mutex
);
4377 key
.objectid
= device
->devid
;
4378 key
.offset
= (u64
)-1;
4379 key
.type
= BTRFS_DEV_EXTENT_KEY
;
4382 mutex_lock(&fs_info
->delete_unused_bgs_mutex
);
4383 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4385 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
4389 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
4391 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
4396 btrfs_release_path(path
);
4401 slot
= path
->slots
[0];
4402 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
4404 if (key
.objectid
!= device
->devid
) {
4405 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
4406 btrfs_release_path(path
);
4410 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
4411 length
= btrfs_dev_extent_length(l
, dev_extent
);
4413 if (key
.offset
+ length
<= new_size
) {
4414 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
4415 btrfs_release_path(path
);
4419 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
4420 btrfs_release_path(path
);
4423 * We may be relocating the only data chunk we have,
4424 * which could potentially end up with losing data's
4425 * raid profile, so lets allocate an empty one in
4428 ret
= btrfs_may_alloc_data_chunk(fs_info
, chunk_offset
);
4430 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
4434 ret
= btrfs_relocate_chunk(fs_info
, chunk_offset
);
4435 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
4436 if (ret
&& ret
!= -ENOSPC
)
4440 } while (key
.offset
-- > 0);
4442 if (failed
&& !retried
) {
4446 } else if (failed
&& retried
) {
4451 /* Shrinking succeeded, else we would be at "done". */
4452 trans
= btrfs_start_transaction(root
, 0);
4453 if (IS_ERR(trans
)) {
4454 ret
= PTR_ERR(trans
);
4458 mutex_lock(&fs_info
->chunk_mutex
);
4461 * We checked in the above loop all device extents that were already in
4462 * the device tree. However before we have updated the device's
4463 * total_bytes to the new size, we might have had chunk allocations that
4464 * have not complete yet (new block groups attached to transaction
4465 * handles), and therefore their device extents were not yet in the
4466 * device tree and we missed them in the loop above. So if we have any
4467 * pending chunk using a device extent that overlaps the device range
4468 * that we can not use anymore, commit the current transaction and
4469 * repeat the search on the device tree - this way we guarantee we will
4470 * not have chunks using device extents that end beyond 'new_size'.
4472 if (!checked_pending_chunks
) {
4473 u64 start
= new_size
;
4474 u64 len
= old_size
- new_size
;
4476 if (contains_pending_extent(trans
->transaction
, device
,
4478 mutex_unlock(&fs_info
->chunk_mutex
);
4479 checked_pending_chunks
= true;
4482 ret
= btrfs_commit_transaction(trans
);
4489 btrfs_device_set_disk_total_bytes(device
, new_size
);
4490 if (list_empty(&device
->resized_list
))
4491 list_add_tail(&device
->resized_list
,
4492 &fs_info
->fs_devices
->resized_devices
);
4494 WARN_ON(diff
> old_total
);
4495 btrfs_set_super_total_bytes(super_copy
,
4496 round_down(old_total
- diff
, fs_info
->sectorsize
));
4497 mutex_unlock(&fs_info
->chunk_mutex
);
4499 /* Now btrfs_update_device() will change the on-disk size. */
4500 ret
= btrfs_update_device(trans
, device
);
4502 btrfs_abort_transaction(trans
, ret
);
4503 btrfs_end_transaction(trans
);
4505 ret
= btrfs_commit_transaction(trans
);
4508 btrfs_free_path(path
);
4510 mutex_lock(&fs_info
->chunk_mutex
);
4511 btrfs_device_set_total_bytes(device
, old_size
);
4512 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
))
4513 device
->fs_devices
->total_rw_bytes
+= diff
;
4514 atomic64_add(diff
, &fs_info
->free_chunk_space
);
4515 mutex_unlock(&fs_info
->chunk_mutex
);
4520 static int btrfs_add_system_chunk(struct btrfs_fs_info
*fs_info
,
4521 struct btrfs_key
*key
,
4522 struct btrfs_chunk
*chunk
, int item_size
)
4524 struct btrfs_super_block
*super_copy
= fs_info
->super_copy
;
4525 struct btrfs_disk_key disk_key
;
4529 mutex_lock(&fs_info
->chunk_mutex
);
4530 array_size
= btrfs_super_sys_array_size(super_copy
);
4531 if (array_size
+ item_size
+ sizeof(disk_key
)
4532 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
) {
4533 mutex_unlock(&fs_info
->chunk_mutex
);
4537 ptr
= super_copy
->sys_chunk_array
+ array_size
;
4538 btrfs_cpu_key_to_disk(&disk_key
, key
);
4539 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
4540 ptr
+= sizeof(disk_key
);
4541 memcpy(ptr
, chunk
, item_size
);
4542 item_size
+= sizeof(disk_key
);
4543 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
4544 mutex_unlock(&fs_info
->chunk_mutex
);
4550 * sort the devices in descending order by max_avail, total_avail
4552 static int btrfs_cmp_device_info(const void *a
, const void *b
)
4554 const struct btrfs_device_info
*di_a
= a
;
4555 const struct btrfs_device_info
*di_b
= b
;
4557 if (di_a
->max_avail
> di_b
->max_avail
)
4559 if (di_a
->max_avail
< di_b
->max_avail
)
4561 if (di_a
->total_avail
> di_b
->total_avail
)
4563 if (di_a
->total_avail
< di_b
->total_avail
)
4568 static void check_raid56_incompat_flag(struct btrfs_fs_info
*info
, u64 type
)
4570 if (!(type
& BTRFS_BLOCK_GROUP_RAID56_MASK
))
4573 btrfs_set_fs_incompat(info
, RAID56
);
4576 #define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info) \
4577 - sizeof(struct btrfs_chunk)) \
4578 / sizeof(struct btrfs_stripe) + 1)
4580 #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4581 - 2 * sizeof(struct btrfs_disk_key) \
4582 - 2 * sizeof(struct btrfs_chunk)) \
4583 / sizeof(struct btrfs_stripe) + 1)
4585 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
4586 u64 start
, u64 type
)
4588 struct btrfs_fs_info
*info
= trans
->fs_info
;
4589 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
4590 struct btrfs_device
*device
;
4591 struct map_lookup
*map
= NULL
;
4592 struct extent_map_tree
*em_tree
;
4593 struct extent_map
*em
;
4594 struct btrfs_device_info
*devices_info
= NULL
;
4596 int num_stripes
; /* total number of stripes to allocate */
4597 int data_stripes
; /* number of stripes that count for
4599 int sub_stripes
; /* sub_stripes info for map */
4600 int dev_stripes
; /* stripes per dev */
4601 int devs_max
; /* max devs to use */
4602 int devs_min
; /* min devs needed */
4603 int devs_increment
; /* ndevs has to be a multiple of this */
4604 int ncopies
; /* how many copies to data has */
4606 u64 max_stripe_size
;
4615 BUG_ON(!alloc_profile_is_valid(type
, 0));
4617 if (list_empty(&fs_devices
->alloc_list
)) {
4618 if (btrfs_test_opt(info
, ENOSPC_DEBUG
))
4619 btrfs_debug(info
, "%s: no writable device", __func__
);
4623 index
= btrfs_bg_flags_to_raid_index(type
);
4625 sub_stripes
= btrfs_raid_array
[index
].sub_stripes
;
4626 dev_stripes
= btrfs_raid_array
[index
].dev_stripes
;
4627 devs_max
= btrfs_raid_array
[index
].devs_max
;
4628 devs_min
= btrfs_raid_array
[index
].devs_min
;
4629 devs_increment
= btrfs_raid_array
[index
].devs_increment
;
4630 ncopies
= btrfs_raid_array
[index
].ncopies
;
4632 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
4633 max_stripe_size
= SZ_1G
;
4634 max_chunk_size
= BTRFS_MAX_DATA_CHUNK_SIZE
;
4636 devs_max
= BTRFS_MAX_DEVS(info
);
4637 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
4638 /* for larger filesystems, use larger metadata chunks */
4639 if (fs_devices
->total_rw_bytes
> 50ULL * SZ_1G
)
4640 max_stripe_size
= SZ_1G
;
4642 max_stripe_size
= SZ_256M
;
4643 max_chunk_size
= max_stripe_size
;
4645 devs_max
= BTRFS_MAX_DEVS(info
);
4646 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
4647 max_stripe_size
= SZ_32M
;
4648 max_chunk_size
= 2 * max_stripe_size
;
4650 devs_max
= BTRFS_MAX_DEVS_SYS_CHUNK
;
4652 btrfs_err(info
, "invalid chunk type 0x%llx requested",
4657 /* we don't want a chunk larger than 10% of writeable space */
4658 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
4661 devices_info
= kcalloc(fs_devices
->rw_devices
, sizeof(*devices_info
),
4667 * in the first pass through the devices list, we gather information
4668 * about the available holes on each device.
4671 list_for_each_entry(device
, &fs_devices
->alloc_list
, dev_alloc_list
) {
4675 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
)) {
4677 "BTRFS: read-only device in alloc_list\n");
4681 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA
,
4682 &device
->dev_state
) ||
4683 test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
))
4686 if (device
->total_bytes
> device
->bytes_used
)
4687 total_avail
= device
->total_bytes
- device
->bytes_used
;
4691 /* If there is no space on this device, skip it. */
4692 if (total_avail
== 0)
4695 ret
= find_free_dev_extent(trans
, device
,
4696 max_stripe_size
* dev_stripes
,
4697 &dev_offset
, &max_avail
);
4698 if (ret
&& ret
!= -ENOSPC
)
4702 max_avail
= max_stripe_size
* dev_stripes
;
4704 if (max_avail
< BTRFS_STRIPE_LEN
* dev_stripes
) {
4705 if (btrfs_test_opt(info
, ENOSPC_DEBUG
))
4707 "%s: devid %llu has no free space, have=%llu want=%u",
4708 __func__
, device
->devid
, max_avail
,
4709 BTRFS_STRIPE_LEN
* dev_stripes
);
4713 if (ndevs
== fs_devices
->rw_devices
) {
4714 WARN(1, "%s: found more than %llu devices\n",
4715 __func__
, fs_devices
->rw_devices
);
4718 devices_info
[ndevs
].dev_offset
= dev_offset
;
4719 devices_info
[ndevs
].max_avail
= max_avail
;
4720 devices_info
[ndevs
].total_avail
= total_avail
;
4721 devices_info
[ndevs
].dev
= device
;
4726 * now sort the devices by hole size / available space
4728 sort(devices_info
, ndevs
, sizeof(struct btrfs_device_info
),
4729 btrfs_cmp_device_info
, NULL
);
4731 /* round down to number of usable stripes */
4732 ndevs
= round_down(ndevs
, devs_increment
);
4734 if (ndevs
< devs_min
) {
4736 if (btrfs_test_opt(info
, ENOSPC_DEBUG
)) {
4738 "%s: not enough devices with free space: have=%d minimum required=%d",
4739 __func__
, ndevs
, devs_min
);
4744 ndevs
= min(ndevs
, devs_max
);
4747 * The primary goal is to maximize the number of stripes, so use as
4748 * many devices as possible, even if the stripes are not maximum sized.
4750 * The DUP profile stores more than one stripe per device, the
4751 * max_avail is the total size so we have to adjust.
4753 stripe_size
= div_u64(devices_info
[ndevs
- 1].max_avail
, dev_stripes
);
4754 num_stripes
= ndevs
* dev_stripes
;
4757 * this will have to be fixed for RAID1 and RAID10 over
4760 data_stripes
= num_stripes
/ ncopies
;
4762 if (type
& BTRFS_BLOCK_GROUP_RAID5
)
4763 data_stripes
= num_stripes
- 1;
4765 if (type
& BTRFS_BLOCK_GROUP_RAID6
)
4766 data_stripes
= num_stripes
- 2;
4769 * Use the number of data stripes to figure out how big this chunk
4770 * is really going to be in terms of logical address space,
4771 * and compare that answer with the max chunk size
4773 if (stripe_size
* data_stripes
> max_chunk_size
) {
4774 stripe_size
= div_u64(max_chunk_size
, data_stripes
);
4776 /* bump the answer up to a 16MB boundary */
4777 stripe_size
= round_up(stripe_size
, SZ_16M
);
4780 * But don't go higher than the limits we found while searching
4783 stripe_size
= min(devices_info
[ndevs
- 1].max_avail
,
4787 /* align to BTRFS_STRIPE_LEN */
4788 stripe_size
= round_down(stripe_size
, BTRFS_STRIPE_LEN
);
4790 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
4795 map
->num_stripes
= num_stripes
;
4797 for (i
= 0; i
< ndevs
; ++i
) {
4798 for (j
= 0; j
< dev_stripes
; ++j
) {
4799 int s
= i
* dev_stripes
+ j
;
4800 map
->stripes
[s
].dev
= devices_info
[i
].dev
;
4801 map
->stripes
[s
].physical
= devices_info
[i
].dev_offset
+
4805 map
->stripe_len
= BTRFS_STRIPE_LEN
;
4806 map
->io_align
= BTRFS_STRIPE_LEN
;
4807 map
->io_width
= BTRFS_STRIPE_LEN
;
4809 map
->sub_stripes
= sub_stripes
;
4811 num_bytes
= stripe_size
* data_stripes
;
4813 trace_btrfs_chunk_alloc(info
, map
, start
, num_bytes
);
4815 em
= alloc_extent_map();
4821 set_bit(EXTENT_FLAG_FS_MAPPING
, &em
->flags
);
4822 em
->map_lookup
= map
;
4824 em
->len
= num_bytes
;
4825 em
->block_start
= 0;
4826 em
->block_len
= em
->len
;
4827 em
->orig_block_len
= stripe_size
;
4829 em_tree
= &info
->mapping_tree
.map_tree
;
4830 write_lock(&em_tree
->lock
);
4831 ret
= add_extent_mapping(em_tree
, em
, 0);
4833 write_unlock(&em_tree
->lock
);
4834 free_extent_map(em
);
4838 list_add_tail(&em
->list
, &trans
->transaction
->pending_chunks
);
4839 refcount_inc(&em
->refs
);
4840 write_unlock(&em_tree
->lock
);
4842 ret
= btrfs_make_block_group(trans
, 0, type
, start
, num_bytes
);
4844 goto error_del_extent
;
4846 for (i
= 0; i
< map
->num_stripes
; i
++) {
4847 num_bytes
= map
->stripes
[i
].dev
->bytes_used
+ stripe_size
;
4848 btrfs_device_set_bytes_used(map
->stripes
[i
].dev
, num_bytes
);
4851 atomic64_sub(stripe_size
* map
->num_stripes
, &info
->free_chunk_space
);
4853 free_extent_map(em
);
4854 check_raid56_incompat_flag(info
, type
);
4856 kfree(devices_info
);
4860 write_lock(&em_tree
->lock
);
4861 remove_extent_mapping(em_tree
, em
);
4862 write_unlock(&em_tree
->lock
);
4864 /* One for our allocation */
4865 free_extent_map(em
);
4866 /* One for the tree reference */
4867 free_extent_map(em
);
4868 /* One for the pending_chunks list reference */
4869 free_extent_map(em
);
4871 kfree(devices_info
);
4875 int btrfs_finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
4876 u64 chunk_offset
, u64 chunk_size
)
4878 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
4879 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
4880 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
4881 struct btrfs_key key
;
4882 struct btrfs_device
*device
;
4883 struct btrfs_chunk
*chunk
;
4884 struct btrfs_stripe
*stripe
;
4885 struct extent_map
*em
;
4886 struct map_lookup
*map
;
4893 em
= get_chunk_map(fs_info
, chunk_offset
, chunk_size
);
4897 map
= em
->map_lookup
;
4898 item_size
= btrfs_chunk_item_size(map
->num_stripes
);
4899 stripe_size
= em
->orig_block_len
;
4901 chunk
= kzalloc(item_size
, GFP_NOFS
);
4908 * Take the device list mutex to prevent races with the final phase of
4909 * a device replace operation that replaces the device object associated
4910 * with the map's stripes, because the device object's id can change
4911 * at any time during that final phase of the device replace operation
4912 * (dev-replace.c:btrfs_dev_replace_finishing()).
4914 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
4915 for (i
= 0; i
< map
->num_stripes
; i
++) {
4916 device
= map
->stripes
[i
].dev
;
4917 dev_offset
= map
->stripes
[i
].physical
;
4919 ret
= btrfs_update_device(trans
, device
);
4922 ret
= btrfs_alloc_dev_extent(trans
, device
, chunk_offset
,
4923 dev_offset
, stripe_size
);
4928 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
4932 stripe
= &chunk
->stripe
;
4933 for (i
= 0; i
< map
->num_stripes
; i
++) {
4934 device
= map
->stripes
[i
].dev
;
4935 dev_offset
= map
->stripes
[i
].physical
;
4937 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
4938 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
4939 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
4942 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
4944 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
4945 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
4946 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
4947 btrfs_set_stack_chunk_type(chunk
, map
->type
);
4948 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
4949 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
4950 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
4951 btrfs_set_stack_chunk_sector_size(chunk
, fs_info
->sectorsize
);
4952 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
4954 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
4955 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
4956 key
.offset
= chunk_offset
;
4958 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
4959 if (ret
== 0 && map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
4961 * TODO: Cleanup of inserted chunk root in case of
4964 ret
= btrfs_add_system_chunk(fs_info
, &key
, chunk
, item_size
);
4969 free_extent_map(em
);
4974 * Chunk allocation falls into two parts. The first part does works
4975 * that make the new allocated chunk useable, but not do any operation
4976 * that modifies the chunk tree. The second part does the works that
4977 * require modifying the chunk tree. This division is important for the
4978 * bootstrap process of adding storage to a seed btrfs.
4980 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
, u64 type
)
4984 lockdep_assert_held(&trans
->fs_info
->chunk_mutex
);
4985 chunk_offset
= find_next_chunk(trans
->fs_info
);
4986 return __btrfs_alloc_chunk(trans
, chunk_offset
, type
);
4989 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
4990 struct btrfs_fs_info
*fs_info
)
4993 u64 sys_chunk_offset
;
4997 chunk_offset
= find_next_chunk(fs_info
);
4998 alloc_profile
= btrfs_metadata_alloc_profile(fs_info
);
4999 ret
= __btrfs_alloc_chunk(trans
, chunk_offset
, alloc_profile
);
5003 sys_chunk_offset
= find_next_chunk(fs_info
);
5004 alloc_profile
= btrfs_system_alloc_profile(fs_info
);
5005 ret
= __btrfs_alloc_chunk(trans
, sys_chunk_offset
, alloc_profile
);
5009 static inline int btrfs_chunk_max_errors(struct map_lookup
*map
)
5013 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
5014 BTRFS_BLOCK_GROUP_RAID10
|
5015 BTRFS_BLOCK_GROUP_RAID5
|
5016 BTRFS_BLOCK_GROUP_DUP
)) {
5018 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
) {
5027 int btrfs_chunk_readonly(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
)
5029 struct extent_map
*em
;
5030 struct map_lookup
*map
;
5035 em
= get_chunk_map(fs_info
, chunk_offset
, 1);
5039 map
= em
->map_lookup
;
5040 for (i
= 0; i
< map
->num_stripes
; i
++) {
5041 if (test_bit(BTRFS_DEV_STATE_MISSING
,
5042 &map
->stripes
[i
].dev
->dev_state
)) {
5046 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE
,
5047 &map
->stripes
[i
].dev
->dev_state
)) {
5054 * If the number of missing devices is larger than max errors,
5055 * we can not write the data into that chunk successfully, so
5058 if (miss_ndevs
> btrfs_chunk_max_errors(map
))
5061 free_extent_map(em
);
5065 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
5067 extent_map_tree_init(&tree
->map_tree
);
5070 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
5072 struct extent_map
*em
;
5075 write_lock(&tree
->map_tree
.lock
);
5076 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
5078 remove_extent_mapping(&tree
->map_tree
, em
);
5079 write_unlock(&tree
->map_tree
.lock
);
5083 free_extent_map(em
);
5084 /* once for the tree */
5085 free_extent_map(em
);
5089 int btrfs_num_copies(struct btrfs_fs_info
*fs_info
, u64 logical
, u64 len
)
5091 struct extent_map
*em
;
5092 struct map_lookup
*map
;
5095 em
= get_chunk_map(fs_info
, logical
, len
);
5098 * We could return errors for these cases, but that could get
5099 * ugly and we'd probably do the same thing which is just not do
5100 * anything else and exit, so return 1 so the callers don't try
5101 * to use other copies.
5105 map
= em
->map_lookup
;
5106 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
5107 ret
= map
->num_stripes
;
5108 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
5109 ret
= map
->sub_stripes
;
5110 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID5
)
5112 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
5114 * There could be two corrupted data stripes, we need
5115 * to loop retry in order to rebuild the correct data.
5117 * Fail a stripe at a time on every retry except the
5118 * stripe under reconstruction.
5120 ret
= map
->num_stripes
;
5123 free_extent_map(em
);
5125 btrfs_dev_replace_read_lock(&fs_info
->dev_replace
);
5126 if (btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
) &&
5127 fs_info
->dev_replace
.tgtdev
)
5129 btrfs_dev_replace_read_unlock(&fs_info
->dev_replace
);
5134 unsigned long btrfs_full_stripe_len(struct btrfs_fs_info
*fs_info
,
5137 struct extent_map
*em
;
5138 struct map_lookup
*map
;
5139 unsigned long len
= fs_info
->sectorsize
;
5141 em
= get_chunk_map(fs_info
, logical
, len
);
5143 if (!WARN_ON(IS_ERR(em
))) {
5144 map
= em
->map_lookup
;
5145 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
)
5146 len
= map
->stripe_len
* nr_data_stripes(map
);
5147 free_extent_map(em
);
5152 int btrfs_is_parity_mirror(struct btrfs_fs_info
*fs_info
, u64 logical
, u64 len
)
5154 struct extent_map
*em
;
5155 struct map_lookup
*map
;
5158 em
= get_chunk_map(fs_info
, logical
, len
);
5160 if(!WARN_ON(IS_ERR(em
))) {
5161 map
= em
->map_lookup
;
5162 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
)
5164 free_extent_map(em
);
5169 static int find_live_mirror(struct btrfs_fs_info
*fs_info
,
5170 struct map_lookup
*map
, int first
,
5171 int dev_replace_is_ongoing
)
5175 int preferred_mirror
;
5177 struct btrfs_device
*srcdev
;
5180 (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
)));
5182 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
5183 num_stripes
= map
->sub_stripes
;
5185 num_stripes
= map
->num_stripes
;
5187 preferred_mirror
= first
+ current
->pid
% num_stripes
;
5189 if (dev_replace_is_ongoing
&&
5190 fs_info
->dev_replace
.cont_reading_from_srcdev_mode
==
5191 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID
)
5192 srcdev
= fs_info
->dev_replace
.srcdev
;
5197 * try to avoid the drive that is the source drive for a
5198 * dev-replace procedure, only choose it if no other non-missing
5199 * mirror is available
5201 for (tolerance
= 0; tolerance
< 2; tolerance
++) {
5202 if (map
->stripes
[preferred_mirror
].dev
->bdev
&&
5203 (tolerance
|| map
->stripes
[preferred_mirror
].dev
!= srcdev
))
5204 return preferred_mirror
;
5205 for (i
= first
; i
< first
+ num_stripes
; i
++) {
5206 if (map
->stripes
[i
].dev
->bdev
&&
5207 (tolerance
|| map
->stripes
[i
].dev
!= srcdev
))
5212 /* we couldn't find one that doesn't fail. Just return something
5213 * and the io error handling code will clean up eventually
5215 return preferred_mirror
;
5218 static inline int parity_smaller(u64 a
, u64 b
)
5223 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
5224 static void sort_parity_stripes(struct btrfs_bio
*bbio
, int num_stripes
)
5226 struct btrfs_bio_stripe s
;
5233 for (i
= 0; i
< num_stripes
- 1; i
++) {
5234 if (parity_smaller(bbio
->raid_map
[i
],
5235 bbio
->raid_map
[i
+1])) {
5236 s
= bbio
->stripes
[i
];
5237 l
= bbio
->raid_map
[i
];
5238 bbio
->stripes
[i
] = bbio
->stripes
[i
+1];
5239 bbio
->raid_map
[i
] = bbio
->raid_map
[i
+1];
5240 bbio
->stripes
[i
+1] = s
;
5241 bbio
->raid_map
[i
+1] = l
;
5249 static struct btrfs_bio
*alloc_btrfs_bio(int total_stripes
, int real_stripes
)
5251 struct btrfs_bio
*bbio
= kzalloc(
5252 /* the size of the btrfs_bio */
5253 sizeof(struct btrfs_bio
) +
5254 /* plus the variable array for the stripes */
5255 sizeof(struct btrfs_bio_stripe
) * (total_stripes
) +
5256 /* plus the variable array for the tgt dev */
5257 sizeof(int) * (real_stripes
) +
5259 * plus the raid_map, which includes both the tgt dev
5262 sizeof(u64
) * (total_stripes
),
5263 GFP_NOFS
|__GFP_NOFAIL
);
5265 atomic_set(&bbio
->error
, 0);
5266 refcount_set(&bbio
->refs
, 1);
5271 void btrfs_get_bbio(struct btrfs_bio
*bbio
)
5273 WARN_ON(!refcount_read(&bbio
->refs
));
5274 refcount_inc(&bbio
->refs
);
5277 void btrfs_put_bbio(struct btrfs_bio
*bbio
)
5281 if (refcount_dec_and_test(&bbio
->refs
))
5285 /* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5287 * Please note that, discard won't be sent to target device of device
5290 static int __btrfs_map_block_for_discard(struct btrfs_fs_info
*fs_info
,
5291 u64 logical
, u64 length
,
5292 struct btrfs_bio
**bbio_ret
)
5294 struct extent_map
*em
;
5295 struct map_lookup
*map
;
5296 struct btrfs_bio
*bbio
;
5300 u64 stripe_end_offset
;
5307 u32 sub_stripes
= 0;
5308 u64 stripes_per_dev
= 0;
5309 u32 remaining_stripes
= 0;
5310 u32 last_stripe
= 0;
5314 /* discard always return a bbio */
5317 em
= get_chunk_map(fs_info
, logical
, length
);
5321 map
= em
->map_lookup
;
5322 /* we don't discard raid56 yet */
5323 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
5328 offset
= logical
- em
->start
;
5329 length
= min_t(u64
, em
->len
- offset
, length
);
5331 stripe_len
= map
->stripe_len
;
5333 * stripe_nr counts the total number of stripes we have to stride
5334 * to get to this block
5336 stripe_nr
= div64_u64(offset
, stripe_len
);
5338 /* stripe_offset is the offset of this block in its stripe */
5339 stripe_offset
= offset
- stripe_nr
* stripe_len
;
5341 stripe_nr_end
= round_up(offset
+ length
, map
->stripe_len
);
5342 stripe_nr_end
= div64_u64(stripe_nr_end
, map
->stripe_len
);
5343 stripe_cnt
= stripe_nr_end
- stripe_nr
;
5344 stripe_end_offset
= stripe_nr_end
* map
->stripe_len
-
5347 * after this, stripe_nr is the number of stripes on this
5348 * device we have to walk to find the data, and stripe_index is
5349 * the number of our device in the stripe array
5353 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
5354 BTRFS_BLOCK_GROUP_RAID10
)) {
5355 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
5358 sub_stripes
= map
->sub_stripes
;
5360 factor
= map
->num_stripes
/ sub_stripes
;
5361 num_stripes
= min_t(u64
, map
->num_stripes
,
5362 sub_stripes
* stripe_cnt
);
5363 stripe_nr
= div_u64_rem(stripe_nr
, factor
, &stripe_index
);
5364 stripe_index
*= sub_stripes
;
5365 stripes_per_dev
= div_u64_rem(stripe_cnt
, factor
,
5366 &remaining_stripes
);
5367 div_u64_rem(stripe_nr_end
- 1, factor
, &last_stripe
);
5368 last_stripe
*= sub_stripes
;
5369 } else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
5370 BTRFS_BLOCK_GROUP_DUP
)) {
5371 num_stripes
= map
->num_stripes
;
5373 stripe_nr
= div_u64_rem(stripe_nr
, map
->num_stripes
,
5377 bbio
= alloc_btrfs_bio(num_stripes
, 0);
5383 for (i
= 0; i
< num_stripes
; i
++) {
5384 bbio
->stripes
[i
].physical
=
5385 map
->stripes
[stripe_index
].physical
+
5386 stripe_offset
+ stripe_nr
* map
->stripe_len
;
5387 bbio
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
5389 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
5390 BTRFS_BLOCK_GROUP_RAID10
)) {
5391 bbio
->stripes
[i
].length
= stripes_per_dev
*
5394 if (i
/ sub_stripes
< remaining_stripes
)
5395 bbio
->stripes
[i
].length
+=
5399 * Special for the first stripe and
5402 * |-------|...|-------|
5406 if (i
< sub_stripes
)
5407 bbio
->stripes
[i
].length
-=
5410 if (stripe_index
>= last_stripe
&&
5411 stripe_index
<= (last_stripe
+
5413 bbio
->stripes
[i
].length
-=
5416 if (i
== sub_stripes
- 1)
5419 bbio
->stripes
[i
].length
= length
;
5423 if (stripe_index
== map
->num_stripes
) {
5430 bbio
->map_type
= map
->type
;
5431 bbio
->num_stripes
= num_stripes
;
5433 free_extent_map(em
);
5438 * In dev-replace case, for repair case (that's the only case where the mirror
5439 * is selected explicitly when calling btrfs_map_block), blocks left of the
5440 * left cursor can also be read from the target drive.
5442 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5444 * For READ, it also needs to be supported using the same mirror number.
5446 * If the requested block is not left of the left cursor, EIO is returned. This
5447 * can happen because btrfs_num_copies() returns one more in the dev-replace
5450 static int get_extra_mirror_from_replace(struct btrfs_fs_info
*fs_info
,
5451 u64 logical
, u64 length
,
5452 u64 srcdev_devid
, int *mirror_num
,
5455 struct btrfs_bio
*bbio
= NULL
;
5457 int index_srcdev
= 0;
5459 u64 physical_of_found
= 0;
5463 ret
= __btrfs_map_block(fs_info
, BTRFS_MAP_GET_READ_MIRRORS
,
5464 logical
, &length
, &bbio
, 0, 0);
5466 ASSERT(bbio
== NULL
);
5470 num_stripes
= bbio
->num_stripes
;
5471 if (*mirror_num
> num_stripes
) {
5473 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5474 * that means that the requested area is not left of the left
5477 btrfs_put_bbio(bbio
);
5482 * process the rest of the function using the mirror_num of the source
5483 * drive. Therefore look it up first. At the end, patch the device
5484 * pointer to the one of the target drive.
5486 for (i
= 0; i
< num_stripes
; i
++) {
5487 if (bbio
->stripes
[i
].dev
->devid
!= srcdev_devid
)
5491 * In case of DUP, in order to keep it simple, only add the
5492 * mirror with the lowest physical address
5495 physical_of_found
<= bbio
->stripes
[i
].physical
)
5500 physical_of_found
= bbio
->stripes
[i
].physical
;
5503 btrfs_put_bbio(bbio
);
5509 *mirror_num
= index_srcdev
+ 1;
5510 *physical
= physical_of_found
;
5514 static void handle_ops_on_dev_replace(enum btrfs_map_op op
,
5515 struct btrfs_bio
**bbio_ret
,
5516 struct btrfs_dev_replace
*dev_replace
,
5517 int *num_stripes_ret
, int *max_errors_ret
)
5519 struct btrfs_bio
*bbio
= *bbio_ret
;
5520 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
5521 int tgtdev_indexes
= 0;
5522 int num_stripes
= *num_stripes_ret
;
5523 int max_errors
= *max_errors_ret
;
5526 if (op
== BTRFS_MAP_WRITE
) {
5527 int index_where_to_add
;
5530 * duplicate the write operations while the dev replace
5531 * procedure is running. Since the copying of the old disk to
5532 * the new disk takes place at run time while the filesystem is
5533 * mounted writable, the regular write operations to the old
5534 * disk have to be duplicated to go to the new disk as well.
5536 * Note that device->missing is handled by the caller, and that
5537 * the write to the old disk is already set up in the stripes
5540 index_where_to_add
= num_stripes
;
5541 for (i
= 0; i
< num_stripes
; i
++) {
5542 if (bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
5543 /* write to new disk, too */
5544 struct btrfs_bio_stripe
*new =
5545 bbio
->stripes
+ index_where_to_add
;
5546 struct btrfs_bio_stripe
*old
=
5549 new->physical
= old
->physical
;
5550 new->length
= old
->length
;
5551 new->dev
= dev_replace
->tgtdev
;
5552 bbio
->tgtdev_map
[i
] = index_where_to_add
;
5553 index_where_to_add
++;
5558 num_stripes
= index_where_to_add
;
5559 } else if (op
== BTRFS_MAP_GET_READ_MIRRORS
) {
5560 int index_srcdev
= 0;
5562 u64 physical_of_found
= 0;
5565 * During the dev-replace procedure, the target drive can also
5566 * be used to read data in case it is needed to repair a corrupt
5567 * block elsewhere. This is possible if the requested area is
5568 * left of the left cursor. In this area, the target drive is a
5569 * full copy of the source drive.
5571 for (i
= 0; i
< num_stripes
; i
++) {
5572 if (bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
5574 * In case of DUP, in order to keep it simple,
5575 * only add the mirror with the lowest physical
5579 physical_of_found
<=
5580 bbio
->stripes
[i
].physical
)
5584 physical_of_found
= bbio
->stripes
[i
].physical
;
5588 struct btrfs_bio_stripe
*tgtdev_stripe
=
5589 bbio
->stripes
+ num_stripes
;
5591 tgtdev_stripe
->physical
= physical_of_found
;
5592 tgtdev_stripe
->length
=
5593 bbio
->stripes
[index_srcdev
].length
;
5594 tgtdev_stripe
->dev
= dev_replace
->tgtdev
;
5595 bbio
->tgtdev_map
[index_srcdev
] = num_stripes
;
5602 *num_stripes_ret
= num_stripes
;
5603 *max_errors_ret
= max_errors
;
5604 bbio
->num_tgtdevs
= tgtdev_indexes
;
5608 static bool need_full_stripe(enum btrfs_map_op op
)
5610 return (op
== BTRFS_MAP_WRITE
|| op
== BTRFS_MAP_GET_READ_MIRRORS
);
5613 static int __btrfs_map_block(struct btrfs_fs_info
*fs_info
,
5614 enum btrfs_map_op op
,
5615 u64 logical
, u64
*length
,
5616 struct btrfs_bio
**bbio_ret
,
5617 int mirror_num
, int need_raid_map
)
5619 struct extent_map
*em
;
5620 struct map_lookup
*map
;
5630 int tgtdev_indexes
= 0;
5631 struct btrfs_bio
*bbio
= NULL
;
5632 struct btrfs_dev_replace
*dev_replace
= &fs_info
->dev_replace
;
5633 int dev_replace_is_ongoing
= 0;
5634 int num_alloc_stripes
;
5635 int patch_the_first_stripe_for_dev_replace
= 0;
5636 u64 physical_to_patch_in_first_stripe
= 0;
5637 u64 raid56_full_stripe_start
= (u64
)-1;
5639 if (op
== BTRFS_MAP_DISCARD
)
5640 return __btrfs_map_block_for_discard(fs_info
, logical
,
5643 em
= get_chunk_map(fs_info
, logical
, *length
);
5647 map
= em
->map_lookup
;
5648 offset
= logical
- em
->start
;
5650 stripe_len
= map
->stripe_len
;
5653 * stripe_nr counts the total number of stripes we have to stride
5654 * to get to this block
5656 stripe_nr
= div64_u64(stripe_nr
, stripe_len
);
5658 stripe_offset
= stripe_nr
* stripe_len
;
5659 if (offset
< stripe_offset
) {
5661 "stripe math has gone wrong, stripe_offset=%llu, offset=%llu, start=%llu, logical=%llu, stripe_len=%llu",
5662 stripe_offset
, offset
, em
->start
, logical
,
5664 free_extent_map(em
);
5668 /* stripe_offset is the offset of this block in its stripe*/
5669 stripe_offset
= offset
- stripe_offset
;
5671 /* if we're here for raid56, we need to know the stripe aligned start */
5672 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
5673 unsigned long full_stripe_len
= stripe_len
* nr_data_stripes(map
);
5674 raid56_full_stripe_start
= offset
;
5676 /* allow a write of a full stripe, but make sure we don't
5677 * allow straddling of stripes
5679 raid56_full_stripe_start
= div64_u64(raid56_full_stripe_start
,
5681 raid56_full_stripe_start
*= full_stripe_len
;
5684 if (map
->type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) {
5686 /* For writes to RAID[56], allow a full stripeset across all disks.
5687 For other RAID types and for RAID[56] reads, just allow a single
5688 stripe (on a single disk). */
5689 if ((map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) &&
5690 (op
== BTRFS_MAP_WRITE
)) {
5691 max_len
= stripe_len
* nr_data_stripes(map
) -
5692 (offset
- raid56_full_stripe_start
);
5694 /* we limit the length of each bio to what fits in a stripe */
5695 max_len
= stripe_len
- stripe_offset
;
5697 *length
= min_t(u64
, em
->len
- offset
, max_len
);
5699 *length
= em
->len
- offset
;
5702 /* This is for when we're called from btrfs_merge_bio_hook() and all
5703 it cares about is the length */
5707 btrfs_dev_replace_read_lock(dev_replace
);
5708 dev_replace_is_ongoing
= btrfs_dev_replace_is_ongoing(dev_replace
);
5709 if (!dev_replace_is_ongoing
)
5710 btrfs_dev_replace_read_unlock(dev_replace
);
5712 btrfs_dev_replace_set_lock_blocking(dev_replace
);
5714 if (dev_replace_is_ongoing
&& mirror_num
== map
->num_stripes
+ 1 &&
5715 !need_full_stripe(op
) && dev_replace
->tgtdev
!= NULL
) {
5716 ret
= get_extra_mirror_from_replace(fs_info
, logical
, *length
,
5717 dev_replace
->srcdev
->devid
,
5719 &physical_to_patch_in_first_stripe
);
5723 patch_the_first_stripe_for_dev_replace
= 1;
5724 } else if (mirror_num
> map
->num_stripes
) {
5730 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
5731 stripe_nr
= div_u64_rem(stripe_nr
, map
->num_stripes
,
5733 if (!need_full_stripe(op
))
5735 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
5736 if (need_full_stripe(op
))
5737 num_stripes
= map
->num_stripes
;
5738 else if (mirror_num
)
5739 stripe_index
= mirror_num
- 1;
5741 stripe_index
= find_live_mirror(fs_info
, map
, 0,
5742 dev_replace_is_ongoing
);
5743 mirror_num
= stripe_index
+ 1;
5746 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
5747 if (need_full_stripe(op
)) {
5748 num_stripes
= map
->num_stripes
;
5749 } else if (mirror_num
) {
5750 stripe_index
= mirror_num
- 1;
5755 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
5756 u32 factor
= map
->num_stripes
/ map
->sub_stripes
;
5758 stripe_nr
= div_u64_rem(stripe_nr
, factor
, &stripe_index
);
5759 stripe_index
*= map
->sub_stripes
;
5761 if (need_full_stripe(op
))
5762 num_stripes
= map
->sub_stripes
;
5763 else if (mirror_num
)
5764 stripe_index
+= mirror_num
- 1;
5766 int old_stripe_index
= stripe_index
;
5767 stripe_index
= find_live_mirror(fs_info
, map
,
5769 dev_replace_is_ongoing
);
5770 mirror_num
= stripe_index
- old_stripe_index
+ 1;
5773 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
5774 if (need_raid_map
&& (need_full_stripe(op
) || mirror_num
> 1)) {
5775 /* push stripe_nr back to the start of the full stripe */
5776 stripe_nr
= div64_u64(raid56_full_stripe_start
,
5777 stripe_len
* nr_data_stripes(map
));
5779 /* RAID[56] write or recovery. Return all stripes */
5780 num_stripes
= map
->num_stripes
;
5781 max_errors
= nr_parity_stripes(map
);
5783 *length
= map
->stripe_len
;
5788 * Mirror #0 or #1 means the original data block.
5789 * Mirror #2 is RAID5 parity block.
5790 * Mirror #3 is RAID6 Q block.
5792 stripe_nr
= div_u64_rem(stripe_nr
,
5793 nr_data_stripes(map
), &stripe_index
);
5795 stripe_index
= nr_data_stripes(map
) +
5798 /* We distribute the parity blocks across stripes */
5799 div_u64_rem(stripe_nr
+ stripe_index
, map
->num_stripes
,
5801 if (!need_full_stripe(op
) && mirror_num
<= 1)
5806 * after this, stripe_nr is the number of stripes on this
5807 * device we have to walk to find the data, and stripe_index is
5808 * the number of our device in the stripe array
5810 stripe_nr
= div_u64_rem(stripe_nr
, map
->num_stripes
,
5812 mirror_num
= stripe_index
+ 1;
5814 if (stripe_index
>= map
->num_stripes
) {
5816 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
5817 stripe_index
, map
->num_stripes
);
5822 num_alloc_stripes
= num_stripes
;
5823 if (dev_replace_is_ongoing
&& dev_replace
->tgtdev
!= NULL
) {
5824 if (op
== BTRFS_MAP_WRITE
)
5825 num_alloc_stripes
<<= 1;
5826 if (op
== BTRFS_MAP_GET_READ_MIRRORS
)
5827 num_alloc_stripes
++;
5828 tgtdev_indexes
= num_stripes
;
5831 bbio
= alloc_btrfs_bio(num_alloc_stripes
, tgtdev_indexes
);
5836 if (dev_replace_is_ongoing
&& dev_replace
->tgtdev
!= NULL
)
5837 bbio
->tgtdev_map
= (int *)(bbio
->stripes
+ num_alloc_stripes
);
5839 /* build raid_map */
5840 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
&& need_raid_map
&&
5841 (need_full_stripe(op
) || mirror_num
> 1)) {
5845 bbio
->raid_map
= (u64
*)((void *)bbio
->stripes
+
5846 sizeof(struct btrfs_bio_stripe
) *
5848 sizeof(int) * tgtdev_indexes
);
5850 /* Work out the disk rotation on this stripe-set */
5851 div_u64_rem(stripe_nr
, num_stripes
, &rot
);
5853 /* Fill in the logical address of each stripe */
5854 tmp
= stripe_nr
* nr_data_stripes(map
);
5855 for (i
= 0; i
< nr_data_stripes(map
); i
++)
5856 bbio
->raid_map
[(i
+rot
) % num_stripes
] =
5857 em
->start
+ (tmp
+ i
) * map
->stripe_len
;
5859 bbio
->raid_map
[(i
+rot
) % map
->num_stripes
] = RAID5_P_STRIPE
;
5860 if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
5861 bbio
->raid_map
[(i
+rot
+1) % num_stripes
] =
5866 for (i
= 0; i
< num_stripes
; i
++) {
5867 bbio
->stripes
[i
].physical
=
5868 map
->stripes
[stripe_index
].physical
+
5870 stripe_nr
* map
->stripe_len
;
5871 bbio
->stripes
[i
].dev
=
5872 map
->stripes
[stripe_index
].dev
;
5876 if (need_full_stripe(op
))
5877 max_errors
= btrfs_chunk_max_errors(map
);
5880 sort_parity_stripes(bbio
, num_stripes
);
5882 if (dev_replace_is_ongoing
&& dev_replace
->tgtdev
!= NULL
&&
5883 need_full_stripe(op
)) {
5884 handle_ops_on_dev_replace(op
, &bbio
, dev_replace
, &num_stripes
,
5889 bbio
->map_type
= map
->type
;
5890 bbio
->num_stripes
= num_stripes
;
5891 bbio
->max_errors
= max_errors
;
5892 bbio
->mirror_num
= mirror_num
;
5895 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5896 * mirror_num == num_stripes + 1 && dev_replace target drive is
5897 * available as a mirror
5899 if (patch_the_first_stripe_for_dev_replace
&& num_stripes
> 0) {
5900 WARN_ON(num_stripes
> 1);
5901 bbio
->stripes
[0].dev
= dev_replace
->tgtdev
;
5902 bbio
->stripes
[0].physical
= physical_to_patch_in_first_stripe
;
5903 bbio
->mirror_num
= map
->num_stripes
+ 1;
5906 if (dev_replace_is_ongoing
) {
5907 ASSERT(atomic_read(&dev_replace
->blocking_readers
) > 0);
5908 btrfs_dev_replace_read_lock(dev_replace
);
5909 /* Barrier implied by atomic_dec_and_test */
5910 if (atomic_dec_and_test(&dev_replace
->blocking_readers
))
5911 cond_wake_up_nomb(&dev_replace
->read_lock_wq
);
5912 btrfs_dev_replace_read_unlock(dev_replace
);
5914 free_extent_map(em
);
5918 int btrfs_map_block(struct btrfs_fs_info
*fs_info
, enum btrfs_map_op op
,
5919 u64 logical
, u64
*length
,
5920 struct btrfs_bio
**bbio_ret
, int mirror_num
)
5922 return __btrfs_map_block(fs_info
, op
, logical
, length
, bbio_ret
,
5926 /* For Scrub/replace */
5927 int btrfs_map_sblock(struct btrfs_fs_info
*fs_info
, enum btrfs_map_op op
,
5928 u64 logical
, u64
*length
,
5929 struct btrfs_bio
**bbio_ret
)
5931 return __btrfs_map_block(fs_info
, op
, logical
, length
, bbio_ret
, 0, 1);
5934 int btrfs_rmap_block(struct btrfs_fs_info
*fs_info
, u64 chunk_start
,
5935 u64 physical
, u64
**logical
, int *naddrs
, int *stripe_len
)
5937 struct extent_map
*em
;
5938 struct map_lookup
*map
;
5946 em
= get_chunk_map(fs_info
, chunk_start
, 1);
5950 map
= em
->map_lookup
;
5952 rmap_len
= map
->stripe_len
;
5954 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
5955 length
= div_u64(length
, map
->num_stripes
/ map
->sub_stripes
);
5956 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
5957 length
= div_u64(length
, map
->num_stripes
);
5958 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
5959 length
= div_u64(length
, nr_data_stripes(map
));
5960 rmap_len
= map
->stripe_len
* nr_data_stripes(map
);
5963 buf
= kcalloc(map
->num_stripes
, sizeof(u64
), GFP_NOFS
);
5964 BUG_ON(!buf
); /* -ENOMEM */
5966 for (i
= 0; i
< map
->num_stripes
; i
++) {
5967 if (map
->stripes
[i
].physical
> physical
||
5968 map
->stripes
[i
].physical
+ length
<= physical
)
5971 stripe_nr
= physical
- map
->stripes
[i
].physical
;
5972 stripe_nr
= div64_u64(stripe_nr
, map
->stripe_len
);
5974 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
5975 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
5976 stripe_nr
= div_u64(stripe_nr
, map
->sub_stripes
);
5977 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
5978 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
5979 } /* else if RAID[56], multiply by nr_data_stripes().
5980 * Alternatively, just use rmap_len below instead of
5981 * map->stripe_len */
5983 bytenr
= chunk_start
+ stripe_nr
* rmap_len
;
5984 WARN_ON(nr
>= map
->num_stripes
);
5985 for (j
= 0; j
< nr
; j
++) {
5986 if (buf
[j
] == bytenr
)
5990 WARN_ON(nr
>= map
->num_stripes
);
5997 *stripe_len
= rmap_len
;
5999 free_extent_map(em
);
6003 static inline void btrfs_end_bbio(struct btrfs_bio
*bbio
, struct bio
*bio
)
6005 bio
->bi_private
= bbio
->private;
6006 bio
->bi_end_io
= bbio
->end_io
;
6009 btrfs_put_bbio(bbio
);
6012 static void btrfs_end_bio(struct bio
*bio
)
6014 struct btrfs_bio
*bbio
= bio
->bi_private
;
6015 int is_orig_bio
= 0;
6017 if (bio
->bi_status
) {
6018 atomic_inc(&bbio
->error
);
6019 if (bio
->bi_status
== BLK_STS_IOERR
||
6020 bio
->bi_status
== BLK_STS_TARGET
) {
6021 unsigned int stripe_index
=
6022 btrfs_io_bio(bio
)->stripe_index
;
6023 struct btrfs_device
*dev
;
6025 BUG_ON(stripe_index
>= bbio
->num_stripes
);
6026 dev
= bbio
->stripes
[stripe_index
].dev
;
6028 if (bio_op(bio
) == REQ_OP_WRITE
)
6029 btrfs_dev_stat_inc_and_print(dev
,
6030 BTRFS_DEV_STAT_WRITE_ERRS
);
6032 btrfs_dev_stat_inc_and_print(dev
,
6033 BTRFS_DEV_STAT_READ_ERRS
);
6034 if (bio
->bi_opf
& REQ_PREFLUSH
)
6035 btrfs_dev_stat_inc_and_print(dev
,
6036 BTRFS_DEV_STAT_FLUSH_ERRS
);
6041 if (bio
== bbio
->orig_bio
)
6044 btrfs_bio_counter_dec(bbio
->fs_info
);
6046 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
6049 bio
= bbio
->orig_bio
;
6052 btrfs_io_bio(bio
)->mirror_num
= bbio
->mirror_num
;
6053 /* only send an error to the higher layers if it is
6054 * beyond the tolerance of the btrfs bio
6056 if (atomic_read(&bbio
->error
) > bbio
->max_errors
) {
6057 bio
->bi_status
= BLK_STS_IOERR
;
6060 * this bio is actually up to date, we didn't
6061 * go over the max number of errors
6063 bio
->bi_status
= BLK_STS_OK
;
6066 btrfs_end_bbio(bbio
, bio
);
6067 } else if (!is_orig_bio
) {
6073 * see run_scheduled_bios for a description of why bios are collected for
6076 * This will add one bio to the pending list for a device and make sure
6077 * the work struct is scheduled.
6079 static noinline
void btrfs_schedule_bio(struct btrfs_device
*device
,
6082 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
6083 int should_queue
= 1;
6084 struct btrfs_pending_bios
*pending_bios
;
6086 if (test_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
) ||
6092 /* don't bother with additional async steps for reads, right now */
6093 if (bio_op(bio
) == REQ_OP_READ
) {
6094 btrfsic_submit_bio(bio
);
6098 WARN_ON(bio
->bi_next
);
6099 bio
->bi_next
= NULL
;
6101 spin_lock(&device
->io_lock
);
6102 if (op_is_sync(bio
->bi_opf
))
6103 pending_bios
= &device
->pending_sync_bios
;
6105 pending_bios
= &device
->pending_bios
;
6107 if (pending_bios
->tail
)
6108 pending_bios
->tail
->bi_next
= bio
;
6110 pending_bios
->tail
= bio
;
6111 if (!pending_bios
->head
)
6112 pending_bios
->head
= bio
;
6113 if (device
->running_pending
)
6116 spin_unlock(&device
->io_lock
);
6119 btrfs_queue_work(fs_info
->submit_workers
, &device
->work
);
6122 static void submit_stripe_bio(struct btrfs_bio
*bbio
, struct bio
*bio
,
6123 u64 physical
, int dev_nr
, int async
)
6125 struct btrfs_device
*dev
= bbio
->stripes
[dev_nr
].dev
;
6126 struct btrfs_fs_info
*fs_info
= bbio
->fs_info
;
6128 bio
->bi_private
= bbio
;
6129 btrfs_io_bio(bio
)->stripe_index
= dev_nr
;
6130 bio
->bi_end_io
= btrfs_end_bio
;
6131 bio
->bi_iter
.bi_sector
= physical
>> 9;
6132 btrfs_debug_in_rcu(fs_info
,
6133 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6134 bio_op(bio
), bio
->bi_opf
, (u64
)bio
->bi_iter
.bi_sector
,
6135 (u_long
)dev
->bdev
->bd_dev
, rcu_str_deref(dev
->name
), dev
->devid
,
6136 bio
->bi_iter
.bi_size
);
6137 bio_set_dev(bio
, dev
->bdev
);
6139 btrfs_bio_counter_inc_noblocked(fs_info
);
6142 btrfs_schedule_bio(dev
, bio
);
6144 btrfsic_submit_bio(bio
);
6147 static void bbio_error(struct btrfs_bio
*bbio
, struct bio
*bio
, u64 logical
)
6149 atomic_inc(&bbio
->error
);
6150 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
6151 /* Should be the original bio. */
6152 WARN_ON(bio
!= bbio
->orig_bio
);
6154 btrfs_io_bio(bio
)->mirror_num
= bbio
->mirror_num
;
6155 bio
->bi_iter
.bi_sector
= logical
>> 9;
6156 if (atomic_read(&bbio
->error
) > bbio
->max_errors
)
6157 bio
->bi_status
= BLK_STS_IOERR
;
6159 bio
->bi_status
= BLK_STS_OK
;
6160 btrfs_end_bbio(bbio
, bio
);
6164 blk_status_t
btrfs_map_bio(struct btrfs_fs_info
*fs_info
, struct bio
*bio
,
6165 int mirror_num
, int async_submit
)
6167 struct btrfs_device
*dev
;
6168 struct bio
*first_bio
= bio
;
6169 u64 logical
= (u64
)bio
->bi_iter
.bi_sector
<< 9;
6175 struct btrfs_bio
*bbio
= NULL
;
6177 length
= bio
->bi_iter
.bi_size
;
6178 map_length
= length
;
6180 btrfs_bio_counter_inc_blocked(fs_info
);
6181 ret
= __btrfs_map_block(fs_info
, btrfs_op(bio
), logical
,
6182 &map_length
, &bbio
, mirror_num
, 1);
6184 btrfs_bio_counter_dec(fs_info
);
6185 return errno_to_blk_status(ret
);
6188 total_devs
= bbio
->num_stripes
;
6189 bbio
->orig_bio
= first_bio
;
6190 bbio
->private = first_bio
->bi_private
;
6191 bbio
->end_io
= first_bio
->bi_end_io
;
6192 bbio
->fs_info
= fs_info
;
6193 atomic_set(&bbio
->stripes_pending
, bbio
->num_stripes
);
6195 if ((bbio
->map_type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) &&
6196 ((bio_op(bio
) == REQ_OP_WRITE
) || (mirror_num
> 1))) {
6197 /* In this case, map_length has been set to the length of
6198 a single stripe; not the whole write */
6199 if (bio_op(bio
) == REQ_OP_WRITE
) {
6200 ret
= raid56_parity_write(fs_info
, bio
, bbio
,
6203 ret
= raid56_parity_recover(fs_info
, bio
, bbio
,
6204 map_length
, mirror_num
, 1);
6207 btrfs_bio_counter_dec(fs_info
);
6208 return errno_to_blk_status(ret
);
6211 if (map_length
< length
) {
6213 "mapping failed logical %llu bio len %llu len %llu",
6214 logical
, length
, map_length
);
6218 for (dev_nr
= 0; dev_nr
< total_devs
; dev_nr
++) {
6219 dev
= bbio
->stripes
[dev_nr
].dev
;
6220 if (!dev
|| !dev
->bdev
||
6221 (bio_op(first_bio
) == REQ_OP_WRITE
&&
6222 !test_bit(BTRFS_DEV_STATE_WRITEABLE
, &dev
->dev_state
))) {
6223 bbio_error(bbio
, first_bio
, logical
);
6227 if (dev_nr
< total_devs
- 1)
6228 bio
= btrfs_bio_clone(first_bio
);
6232 submit_stripe_bio(bbio
, bio
, bbio
->stripes
[dev_nr
].physical
,
6233 dev_nr
, async_submit
);
6235 btrfs_bio_counter_dec(fs_info
);
6239 struct btrfs_device
*btrfs_find_device(struct btrfs_fs_info
*fs_info
, u64 devid
,
6242 struct btrfs_device
*device
;
6243 struct btrfs_fs_devices
*cur_devices
;
6245 cur_devices
= fs_info
->fs_devices
;
6246 while (cur_devices
) {
6248 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_FSID_SIZE
)) {
6249 device
= find_device(cur_devices
, devid
, uuid
);
6253 cur_devices
= cur_devices
->seed
;
6258 static struct btrfs_device
*add_missing_dev(struct btrfs_fs_devices
*fs_devices
,
6259 u64 devid
, u8
*dev_uuid
)
6261 struct btrfs_device
*device
;
6263 device
= btrfs_alloc_device(NULL
, &devid
, dev_uuid
);
6267 list_add(&device
->dev_list
, &fs_devices
->devices
);
6268 device
->fs_devices
= fs_devices
;
6269 fs_devices
->num_devices
++;
6271 set_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
);
6272 fs_devices
->missing_devices
++;
6278 * btrfs_alloc_device - allocate struct btrfs_device
6279 * @fs_info: used only for generating a new devid, can be NULL if
6280 * devid is provided (i.e. @devid != NULL).
6281 * @devid: a pointer to devid for this device. If NULL a new devid
6283 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6286 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
6287 * on error. Returned struct is not linked onto any lists and must be
6288 * destroyed with btrfs_free_device.
6290 struct btrfs_device
*btrfs_alloc_device(struct btrfs_fs_info
*fs_info
,
6294 struct btrfs_device
*dev
;
6297 if (WARN_ON(!devid
&& !fs_info
))
6298 return ERR_PTR(-EINVAL
);
6300 dev
= __alloc_device();
6309 ret
= find_next_devid(fs_info
, &tmp
);
6311 btrfs_free_device(dev
);
6312 return ERR_PTR(ret
);
6318 memcpy(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
);
6320 generate_random_uuid(dev
->uuid
);
6322 btrfs_init_work(&dev
->work
, btrfs_submit_helper
,
6323 pending_bios_fn
, NULL
, NULL
);
6328 /* Return -EIO if any error, otherwise return 0. */
6329 static int btrfs_check_chunk_valid(struct btrfs_fs_info
*fs_info
,
6330 struct extent_buffer
*leaf
,
6331 struct btrfs_chunk
*chunk
, u64 logical
)
6341 length
= btrfs_chunk_length(leaf
, chunk
);
6342 stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
6343 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
6344 sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
6345 type
= btrfs_chunk_type(leaf
, chunk
);
6348 btrfs_err(fs_info
, "invalid chunk num_stripes: %u",
6352 if (!IS_ALIGNED(logical
, fs_info
->sectorsize
)) {
6353 btrfs_err(fs_info
, "invalid chunk logical %llu", logical
);
6356 if (btrfs_chunk_sector_size(leaf
, chunk
) != fs_info
->sectorsize
) {
6357 btrfs_err(fs_info
, "invalid chunk sectorsize %u",
6358 btrfs_chunk_sector_size(leaf
, chunk
));
6361 if (!length
|| !IS_ALIGNED(length
, fs_info
->sectorsize
)) {
6362 btrfs_err(fs_info
, "invalid chunk length %llu", length
);
6365 if (!is_power_of_2(stripe_len
) || stripe_len
!= BTRFS_STRIPE_LEN
) {
6366 btrfs_err(fs_info
, "invalid chunk stripe length: %llu",
6370 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK
| BTRFS_BLOCK_GROUP_PROFILE_MASK
) &
6372 btrfs_err(fs_info
, "unrecognized chunk type: %llu",
6373 ~(BTRFS_BLOCK_GROUP_TYPE_MASK
|
6374 BTRFS_BLOCK_GROUP_PROFILE_MASK
) &
6375 btrfs_chunk_type(leaf
, chunk
));
6379 if ((type
& BTRFS_BLOCK_GROUP_TYPE_MASK
) == 0) {
6380 btrfs_err(fs_info
, "missing chunk type flag: 0x%llx", type
);
6384 if ((type
& BTRFS_BLOCK_GROUP_SYSTEM
) &&
6385 (type
& (BTRFS_BLOCK_GROUP_METADATA
| BTRFS_BLOCK_GROUP_DATA
))) {
6387 "system chunk with data or metadata type: 0x%llx", type
);
6391 features
= btrfs_super_incompat_flags(fs_info
->super_copy
);
6392 if (features
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
6396 if ((type
& BTRFS_BLOCK_GROUP_METADATA
) &&
6397 (type
& BTRFS_BLOCK_GROUP_DATA
)) {
6399 "mixed chunk type in non-mixed mode: 0x%llx", type
);
6404 if ((type
& BTRFS_BLOCK_GROUP_RAID10
&& sub_stripes
!= 2) ||
6405 (type
& BTRFS_BLOCK_GROUP_RAID1
&& num_stripes
< 1) ||
6406 (type
& BTRFS_BLOCK_GROUP_RAID5
&& num_stripes
< 2) ||
6407 (type
& BTRFS_BLOCK_GROUP_RAID6
&& num_stripes
< 3) ||
6408 (type
& BTRFS_BLOCK_GROUP_DUP
&& num_stripes
> 2) ||
6409 ((type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) == 0 &&
6410 num_stripes
!= 1)) {
6412 "invalid num_stripes:sub_stripes %u:%u for profile %llu",
6413 num_stripes
, sub_stripes
,
6414 type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
);
6421 static void btrfs_report_missing_device(struct btrfs_fs_info
*fs_info
,
6422 u64 devid
, u8
*uuid
, bool error
)
6425 btrfs_err_rl(fs_info
, "devid %llu uuid %pU is missing",
6428 btrfs_warn_rl(fs_info
, "devid %llu uuid %pU is missing",
6432 static int read_one_chunk(struct btrfs_fs_info
*fs_info
, struct btrfs_key
*key
,
6433 struct extent_buffer
*leaf
,
6434 struct btrfs_chunk
*chunk
)
6436 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
6437 struct map_lookup
*map
;
6438 struct extent_map
*em
;
6442 u8 uuid
[BTRFS_UUID_SIZE
];
6447 logical
= key
->offset
;
6448 length
= btrfs_chunk_length(leaf
, chunk
);
6449 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
6451 ret
= btrfs_check_chunk_valid(fs_info
, leaf
, chunk
, logical
);
6455 read_lock(&map_tree
->map_tree
.lock
);
6456 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
6457 read_unlock(&map_tree
->map_tree
.lock
);
6459 /* already mapped? */
6460 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
6461 free_extent_map(em
);
6464 free_extent_map(em
);
6467 em
= alloc_extent_map();
6470 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
6472 free_extent_map(em
);
6476 set_bit(EXTENT_FLAG_FS_MAPPING
, &em
->flags
);
6477 em
->map_lookup
= map
;
6478 em
->start
= logical
;
6481 em
->block_start
= 0;
6482 em
->block_len
= em
->len
;
6484 map
->num_stripes
= num_stripes
;
6485 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
6486 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
6487 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
6488 map
->type
= btrfs_chunk_type(leaf
, chunk
);
6489 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
6490 map
->verified_stripes
= 0;
6491 for (i
= 0; i
< num_stripes
; i
++) {
6492 map
->stripes
[i
].physical
=
6493 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
6494 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
6495 read_extent_buffer(leaf
, uuid
, (unsigned long)
6496 btrfs_stripe_dev_uuid_nr(chunk
, i
),
6498 map
->stripes
[i
].dev
= btrfs_find_device(fs_info
, devid
,
6500 if (!map
->stripes
[i
].dev
&&
6501 !btrfs_test_opt(fs_info
, DEGRADED
)) {
6502 free_extent_map(em
);
6503 btrfs_report_missing_device(fs_info
, devid
, uuid
, true);
6506 if (!map
->stripes
[i
].dev
) {
6507 map
->stripes
[i
].dev
=
6508 add_missing_dev(fs_info
->fs_devices
, devid
,
6510 if (IS_ERR(map
->stripes
[i
].dev
)) {
6511 free_extent_map(em
);
6513 "failed to init missing dev %llu: %ld",
6514 devid
, PTR_ERR(map
->stripes
[i
].dev
));
6515 return PTR_ERR(map
->stripes
[i
].dev
);
6517 btrfs_report_missing_device(fs_info
, devid
, uuid
, false);
6519 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA
,
6520 &(map
->stripes
[i
].dev
->dev_state
));
6524 write_lock(&map_tree
->map_tree
.lock
);
6525 ret
= add_extent_mapping(&map_tree
->map_tree
, em
, 0);
6526 write_unlock(&map_tree
->map_tree
.lock
);
6529 "failed to add chunk map, start=%llu len=%llu: %d",
6530 em
->start
, em
->len
, ret
);
6532 free_extent_map(em
);
6537 static void fill_device_from_item(struct extent_buffer
*leaf
,
6538 struct btrfs_dev_item
*dev_item
,
6539 struct btrfs_device
*device
)
6543 device
->devid
= btrfs_device_id(leaf
, dev_item
);
6544 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
6545 device
->total_bytes
= device
->disk_total_bytes
;
6546 device
->commit_total_bytes
= device
->disk_total_bytes
;
6547 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
6548 device
->commit_bytes_used
= device
->bytes_used
;
6549 device
->type
= btrfs_device_type(leaf
, dev_item
);
6550 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
6551 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
6552 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
6553 WARN_ON(device
->devid
== BTRFS_DEV_REPLACE_DEVID
);
6554 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
);
6556 ptr
= btrfs_device_uuid(dev_item
);
6557 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
6560 static struct btrfs_fs_devices
*open_seed_devices(struct btrfs_fs_info
*fs_info
,
6563 struct btrfs_fs_devices
*fs_devices
;
6566 lockdep_assert_held(&uuid_mutex
);
6569 fs_devices
= fs_info
->fs_devices
->seed
;
6570 while (fs_devices
) {
6571 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_FSID_SIZE
))
6574 fs_devices
= fs_devices
->seed
;
6577 fs_devices
= find_fsid(fsid
);
6579 if (!btrfs_test_opt(fs_info
, DEGRADED
))
6580 return ERR_PTR(-ENOENT
);
6582 fs_devices
= alloc_fs_devices(fsid
);
6583 if (IS_ERR(fs_devices
))
6586 fs_devices
->seeding
= 1;
6587 fs_devices
->opened
= 1;
6591 fs_devices
= clone_fs_devices(fs_devices
);
6592 if (IS_ERR(fs_devices
))
6595 ret
= open_fs_devices(fs_devices
, FMODE_READ
, fs_info
->bdev_holder
);
6597 free_fs_devices(fs_devices
);
6598 fs_devices
= ERR_PTR(ret
);
6602 if (!fs_devices
->seeding
) {
6603 close_fs_devices(fs_devices
);
6604 free_fs_devices(fs_devices
);
6605 fs_devices
= ERR_PTR(-EINVAL
);
6609 fs_devices
->seed
= fs_info
->fs_devices
->seed
;
6610 fs_info
->fs_devices
->seed
= fs_devices
;
6615 static int read_one_dev(struct btrfs_fs_info
*fs_info
,
6616 struct extent_buffer
*leaf
,
6617 struct btrfs_dev_item
*dev_item
)
6619 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
6620 struct btrfs_device
*device
;
6623 u8 fs_uuid
[BTRFS_FSID_SIZE
];
6624 u8 dev_uuid
[BTRFS_UUID_SIZE
];
6626 devid
= btrfs_device_id(leaf
, dev_item
);
6627 read_extent_buffer(leaf
, dev_uuid
, btrfs_device_uuid(dev_item
),
6629 read_extent_buffer(leaf
, fs_uuid
, btrfs_device_fsid(dev_item
),
6632 if (memcmp(fs_uuid
, fs_info
->fsid
, BTRFS_FSID_SIZE
)) {
6633 fs_devices
= open_seed_devices(fs_info
, fs_uuid
);
6634 if (IS_ERR(fs_devices
))
6635 return PTR_ERR(fs_devices
);
6638 device
= btrfs_find_device(fs_info
, devid
, dev_uuid
, fs_uuid
);
6640 if (!btrfs_test_opt(fs_info
, DEGRADED
)) {
6641 btrfs_report_missing_device(fs_info
, devid
,
6646 device
= add_missing_dev(fs_devices
, devid
, dev_uuid
);
6647 if (IS_ERR(device
)) {
6649 "failed to add missing dev %llu: %ld",
6650 devid
, PTR_ERR(device
));
6651 return PTR_ERR(device
);
6653 btrfs_report_missing_device(fs_info
, devid
, dev_uuid
, false);
6655 if (!device
->bdev
) {
6656 if (!btrfs_test_opt(fs_info
, DEGRADED
)) {
6657 btrfs_report_missing_device(fs_info
,
6658 devid
, dev_uuid
, true);
6661 btrfs_report_missing_device(fs_info
, devid
,
6665 if (!device
->bdev
&&
6666 !test_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
)) {
6668 * this happens when a device that was properly setup
6669 * in the device info lists suddenly goes bad.
6670 * device->bdev is NULL, and so we have to set
6671 * device->missing to one here
6673 device
->fs_devices
->missing_devices
++;
6674 set_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
);
6677 /* Move the device to its own fs_devices */
6678 if (device
->fs_devices
!= fs_devices
) {
6679 ASSERT(test_bit(BTRFS_DEV_STATE_MISSING
,
6680 &device
->dev_state
));
6682 list_move(&device
->dev_list
, &fs_devices
->devices
);
6683 device
->fs_devices
->num_devices
--;
6684 fs_devices
->num_devices
++;
6686 device
->fs_devices
->missing_devices
--;
6687 fs_devices
->missing_devices
++;
6689 device
->fs_devices
= fs_devices
;
6693 if (device
->fs_devices
!= fs_info
->fs_devices
) {
6694 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
));
6695 if (device
->generation
!=
6696 btrfs_device_generation(leaf
, dev_item
))
6700 fill_device_from_item(leaf
, dev_item
, device
);
6701 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA
, &device
->dev_state
);
6702 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
) &&
6703 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
)) {
6704 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
6705 atomic64_add(device
->total_bytes
- device
->bytes_used
,
6706 &fs_info
->free_chunk_space
);
6712 int btrfs_read_sys_array(struct btrfs_fs_info
*fs_info
)
6714 struct btrfs_root
*root
= fs_info
->tree_root
;
6715 struct btrfs_super_block
*super_copy
= fs_info
->super_copy
;
6716 struct extent_buffer
*sb
;
6717 struct btrfs_disk_key
*disk_key
;
6718 struct btrfs_chunk
*chunk
;
6720 unsigned long sb_array_offset
;
6727 struct btrfs_key key
;
6729 ASSERT(BTRFS_SUPER_INFO_SIZE
<= fs_info
->nodesize
);
6731 * This will create extent buffer of nodesize, superblock size is
6732 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6733 * overallocate but we can keep it as-is, only the first page is used.
6735 sb
= btrfs_find_create_tree_block(fs_info
, BTRFS_SUPER_INFO_OFFSET
);
6738 set_extent_buffer_uptodate(sb
);
6739 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, sb
, 0);
6741 * The sb extent buffer is artificial and just used to read the system array.
6742 * set_extent_buffer_uptodate() call does not properly mark all it's
6743 * pages up-to-date when the page is larger: extent does not cover the
6744 * whole page and consequently check_page_uptodate does not find all
6745 * the page's extents up-to-date (the hole beyond sb),
6746 * write_extent_buffer then triggers a WARN_ON.
6748 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6749 * but sb spans only this function. Add an explicit SetPageUptodate call
6750 * to silence the warning eg. on PowerPC 64.
6752 if (PAGE_SIZE
> BTRFS_SUPER_INFO_SIZE
)
6753 SetPageUptodate(sb
->pages
[0]);
6755 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
6756 array_size
= btrfs_super_sys_array_size(super_copy
);
6758 array_ptr
= super_copy
->sys_chunk_array
;
6759 sb_array_offset
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
6762 while (cur_offset
< array_size
) {
6763 disk_key
= (struct btrfs_disk_key
*)array_ptr
;
6764 len
= sizeof(*disk_key
);
6765 if (cur_offset
+ len
> array_size
)
6766 goto out_short_read
;
6768 btrfs_disk_key_to_cpu(&key
, disk_key
);
6771 sb_array_offset
+= len
;
6774 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
6775 chunk
= (struct btrfs_chunk
*)sb_array_offset
;
6777 * At least one btrfs_chunk with one stripe must be
6778 * present, exact stripe count check comes afterwards
6780 len
= btrfs_chunk_item_size(1);
6781 if (cur_offset
+ len
> array_size
)
6782 goto out_short_read
;
6784 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
6787 "invalid number of stripes %u in sys_array at offset %u",
6788 num_stripes
, cur_offset
);
6793 type
= btrfs_chunk_type(sb
, chunk
);
6794 if ((type
& BTRFS_BLOCK_GROUP_SYSTEM
) == 0) {
6796 "invalid chunk type %llu in sys_array at offset %u",
6802 len
= btrfs_chunk_item_size(num_stripes
);
6803 if (cur_offset
+ len
> array_size
)
6804 goto out_short_read
;
6806 ret
= read_one_chunk(fs_info
, &key
, sb
, chunk
);
6811 "unexpected item type %u in sys_array at offset %u",
6812 (u32
)key
.type
, cur_offset
);
6817 sb_array_offset
+= len
;
6820 clear_extent_buffer_uptodate(sb
);
6821 free_extent_buffer_stale(sb
);
6825 btrfs_err(fs_info
, "sys_array too short to read %u bytes at offset %u",
6827 clear_extent_buffer_uptodate(sb
);
6828 free_extent_buffer_stale(sb
);
6833 * Check if all chunks in the fs are OK for read-write degraded mount
6835 * If the @failing_dev is specified, it's accounted as missing.
6837 * Return true if all chunks meet the minimal RW mount requirements.
6838 * Return false if any chunk doesn't meet the minimal RW mount requirements.
6840 bool btrfs_check_rw_degradable(struct btrfs_fs_info
*fs_info
,
6841 struct btrfs_device
*failing_dev
)
6843 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
6844 struct extent_map
*em
;
6848 read_lock(&map_tree
->map_tree
.lock
);
6849 em
= lookup_extent_mapping(&map_tree
->map_tree
, 0, (u64
)-1);
6850 read_unlock(&map_tree
->map_tree
.lock
);
6851 /* No chunk at all? Return false anyway */
6857 struct map_lookup
*map
;
6862 map
= em
->map_lookup
;
6864 btrfs_get_num_tolerated_disk_barrier_failures(
6866 for (i
= 0; i
< map
->num_stripes
; i
++) {
6867 struct btrfs_device
*dev
= map
->stripes
[i
].dev
;
6869 if (!dev
|| !dev
->bdev
||
6870 test_bit(BTRFS_DEV_STATE_MISSING
, &dev
->dev_state
) ||
6871 dev
->last_flush_error
)
6873 else if (failing_dev
&& failing_dev
== dev
)
6876 if (missing
> max_tolerated
) {
6879 "chunk %llu missing %d devices, max tolerance is %d for writeable mount",
6880 em
->start
, missing
, max_tolerated
);
6881 free_extent_map(em
);
6885 next_start
= extent_map_end(em
);
6886 free_extent_map(em
);
6888 read_lock(&map_tree
->map_tree
.lock
);
6889 em
= lookup_extent_mapping(&map_tree
->map_tree
, next_start
,
6890 (u64
)(-1) - next_start
);
6891 read_unlock(&map_tree
->map_tree
.lock
);
6897 int btrfs_read_chunk_tree(struct btrfs_fs_info
*fs_info
)
6899 struct btrfs_root
*root
= fs_info
->chunk_root
;
6900 struct btrfs_path
*path
;
6901 struct extent_buffer
*leaf
;
6902 struct btrfs_key key
;
6903 struct btrfs_key found_key
;
6908 path
= btrfs_alloc_path();
6913 * uuid_mutex is needed only if we are mounting a sprout FS
6914 * otherwise we don't need it.
6916 mutex_lock(&uuid_mutex
);
6917 mutex_lock(&fs_info
->chunk_mutex
);
6920 * Read all device items, and then all the chunk items. All
6921 * device items are found before any chunk item (their object id
6922 * is smaller than the lowest possible object id for a chunk
6923 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
6925 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
6928 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
6932 leaf
= path
->nodes
[0];
6933 slot
= path
->slots
[0];
6934 if (slot
>= btrfs_header_nritems(leaf
)) {
6935 ret
= btrfs_next_leaf(root
, path
);
6942 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
6943 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
6944 struct btrfs_dev_item
*dev_item
;
6945 dev_item
= btrfs_item_ptr(leaf
, slot
,
6946 struct btrfs_dev_item
);
6947 ret
= read_one_dev(fs_info
, leaf
, dev_item
);
6951 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
6952 struct btrfs_chunk
*chunk
;
6953 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
6954 ret
= read_one_chunk(fs_info
, &found_key
, leaf
, chunk
);
6962 * After loading chunk tree, we've got all device information,
6963 * do another round of validation checks.
6965 if (total_dev
!= fs_info
->fs_devices
->total_devices
) {
6967 "super_num_devices %llu mismatch with num_devices %llu found here",
6968 btrfs_super_num_devices(fs_info
->super_copy
),
6973 if (btrfs_super_total_bytes(fs_info
->super_copy
) <
6974 fs_info
->fs_devices
->total_rw_bytes
) {
6976 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
6977 btrfs_super_total_bytes(fs_info
->super_copy
),
6978 fs_info
->fs_devices
->total_rw_bytes
);
6984 mutex_unlock(&fs_info
->chunk_mutex
);
6985 mutex_unlock(&uuid_mutex
);
6987 btrfs_free_path(path
);
6991 void btrfs_init_devices_late(struct btrfs_fs_info
*fs_info
)
6993 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
6994 struct btrfs_device
*device
;
6996 while (fs_devices
) {
6997 mutex_lock(&fs_devices
->device_list_mutex
);
6998 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
)
6999 device
->fs_info
= fs_info
;
7000 mutex_unlock(&fs_devices
->device_list_mutex
);
7002 fs_devices
= fs_devices
->seed
;
7006 static void __btrfs_reset_dev_stats(struct btrfs_device
*dev
)
7010 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
7011 btrfs_dev_stat_reset(dev
, i
);
7014 int btrfs_init_dev_stats(struct btrfs_fs_info
*fs_info
)
7016 struct btrfs_key key
;
7017 struct btrfs_key found_key
;
7018 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
7019 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
7020 struct extent_buffer
*eb
;
7023 struct btrfs_device
*device
;
7024 struct btrfs_path
*path
= NULL
;
7027 path
= btrfs_alloc_path();
7033 mutex_lock(&fs_devices
->device_list_mutex
);
7034 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
7036 struct btrfs_dev_stats_item
*ptr
;
7038 key
.objectid
= BTRFS_DEV_STATS_OBJECTID
;
7039 key
.type
= BTRFS_PERSISTENT_ITEM_KEY
;
7040 key
.offset
= device
->devid
;
7041 ret
= btrfs_search_slot(NULL
, dev_root
, &key
, path
, 0, 0);
7043 __btrfs_reset_dev_stats(device
);
7044 device
->dev_stats_valid
= 1;
7045 btrfs_release_path(path
);
7048 slot
= path
->slots
[0];
7049 eb
= path
->nodes
[0];
7050 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
7051 item_size
= btrfs_item_size_nr(eb
, slot
);
7053 ptr
= btrfs_item_ptr(eb
, slot
,
7054 struct btrfs_dev_stats_item
);
7056 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
7057 if (item_size
>= (1 + i
) * sizeof(__le64
))
7058 btrfs_dev_stat_set(device
, i
,
7059 btrfs_dev_stats_value(eb
, ptr
, i
));
7061 btrfs_dev_stat_reset(device
, i
);
7064 device
->dev_stats_valid
= 1;
7065 btrfs_dev_stat_print_on_load(device
);
7066 btrfs_release_path(path
);
7068 mutex_unlock(&fs_devices
->device_list_mutex
);
7071 btrfs_free_path(path
);
7072 return ret
< 0 ? ret
: 0;
7075 static int update_dev_stat_item(struct btrfs_trans_handle
*trans
,
7076 struct btrfs_device
*device
)
7078 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
7079 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
7080 struct btrfs_path
*path
;
7081 struct btrfs_key key
;
7082 struct extent_buffer
*eb
;
7083 struct btrfs_dev_stats_item
*ptr
;
7087 key
.objectid
= BTRFS_DEV_STATS_OBJECTID
;
7088 key
.type
= BTRFS_PERSISTENT_ITEM_KEY
;
7089 key
.offset
= device
->devid
;
7091 path
= btrfs_alloc_path();
7094 ret
= btrfs_search_slot(trans
, dev_root
, &key
, path
, -1, 1);
7096 btrfs_warn_in_rcu(fs_info
,
7097 "error %d while searching for dev_stats item for device %s",
7098 ret
, rcu_str_deref(device
->name
));
7103 btrfs_item_size_nr(path
->nodes
[0], path
->slots
[0]) < sizeof(*ptr
)) {
7104 /* need to delete old one and insert a new one */
7105 ret
= btrfs_del_item(trans
, dev_root
, path
);
7107 btrfs_warn_in_rcu(fs_info
,
7108 "delete too small dev_stats item for device %s failed %d",
7109 rcu_str_deref(device
->name
), ret
);
7116 /* need to insert a new item */
7117 btrfs_release_path(path
);
7118 ret
= btrfs_insert_empty_item(trans
, dev_root
, path
,
7119 &key
, sizeof(*ptr
));
7121 btrfs_warn_in_rcu(fs_info
,
7122 "insert dev_stats item for device %s failed %d",
7123 rcu_str_deref(device
->name
), ret
);
7128 eb
= path
->nodes
[0];
7129 ptr
= btrfs_item_ptr(eb
, path
->slots
[0], struct btrfs_dev_stats_item
);
7130 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
7131 btrfs_set_dev_stats_value(eb
, ptr
, i
,
7132 btrfs_dev_stat_read(device
, i
));
7133 btrfs_mark_buffer_dirty(eb
);
7136 btrfs_free_path(path
);
7141 * called from commit_transaction. Writes all changed device stats to disk.
7143 int btrfs_run_dev_stats(struct btrfs_trans_handle
*trans
,
7144 struct btrfs_fs_info
*fs_info
)
7146 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
7147 struct btrfs_device
*device
;
7151 mutex_lock(&fs_devices
->device_list_mutex
);
7152 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
7153 stats_cnt
= atomic_read(&device
->dev_stats_ccnt
);
7154 if (!device
->dev_stats_valid
|| stats_cnt
== 0)
7159 * There is a LOAD-LOAD control dependency between the value of
7160 * dev_stats_ccnt and updating the on-disk values which requires
7161 * reading the in-memory counters. Such control dependencies
7162 * require explicit read memory barriers.
7164 * This memory barriers pairs with smp_mb__before_atomic in
7165 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7166 * barrier implied by atomic_xchg in
7167 * btrfs_dev_stats_read_and_reset
7171 ret
= update_dev_stat_item(trans
, device
);
7173 atomic_sub(stats_cnt
, &device
->dev_stats_ccnt
);
7175 mutex_unlock(&fs_devices
->device_list_mutex
);
7180 void btrfs_dev_stat_inc_and_print(struct btrfs_device
*dev
, int index
)
7182 btrfs_dev_stat_inc(dev
, index
);
7183 btrfs_dev_stat_print_on_error(dev
);
7186 static void btrfs_dev_stat_print_on_error(struct btrfs_device
*dev
)
7188 if (!dev
->dev_stats_valid
)
7190 btrfs_err_rl_in_rcu(dev
->fs_info
,
7191 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
7192 rcu_str_deref(dev
->name
),
7193 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
7194 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
7195 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
7196 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_CORRUPTION_ERRS
),
7197 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_GENERATION_ERRS
));
7200 static void btrfs_dev_stat_print_on_load(struct btrfs_device
*dev
)
7204 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
7205 if (btrfs_dev_stat_read(dev
, i
) != 0)
7207 if (i
== BTRFS_DEV_STAT_VALUES_MAX
)
7208 return; /* all values == 0, suppress message */
7210 btrfs_info_in_rcu(dev
->fs_info
,
7211 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
7212 rcu_str_deref(dev
->name
),
7213 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
7214 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
7215 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
7216 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_CORRUPTION_ERRS
),
7217 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_GENERATION_ERRS
));
7220 int btrfs_get_dev_stats(struct btrfs_fs_info
*fs_info
,
7221 struct btrfs_ioctl_get_dev_stats
*stats
)
7223 struct btrfs_device
*dev
;
7224 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
7227 mutex_lock(&fs_devices
->device_list_mutex
);
7228 dev
= btrfs_find_device(fs_info
, stats
->devid
, NULL
, NULL
);
7229 mutex_unlock(&fs_devices
->device_list_mutex
);
7232 btrfs_warn(fs_info
, "get dev_stats failed, device not found");
7234 } else if (!dev
->dev_stats_valid
) {
7235 btrfs_warn(fs_info
, "get dev_stats failed, not yet valid");
7237 } else if (stats
->flags
& BTRFS_DEV_STATS_RESET
) {
7238 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
7239 if (stats
->nr_items
> i
)
7241 btrfs_dev_stat_read_and_reset(dev
, i
);
7243 btrfs_dev_stat_reset(dev
, i
);
7246 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
7247 if (stats
->nr_items
> i
)
7248 stats
->values
[i
] = btrfs_dev_stat_read(dev
, i
);
7250 if (stats
->nr_items
> BTRFS_DEV_STAT_VALUES_MAX
)
7251 stats
->nr_items
= BTRFS_DEV_STAT_VALUES_MAX
;
7255 void btrfs_scratch_superblocks(struct block_device
*bdev
, const char *device_path
)
7257 struct buffer_head
*bh
;
7258 struct btrfs_super_block
*disk_super
;
7264 for (copy_num
= 0; copy_num
< BTRFS_SUPER_MIRROR_MAX
;
7267 if (btrfs_read_dev_one_super(bdev
, copy_num
, &bh
))
7270 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
7272 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
7273 set_buffer_dirty(bh
);
7274 sync_dirty_buffer(bh
);
7278 /* Notify udev that device has changed */
7279 btrfs_kobject_uevent(bdev
, KOBJ_CHANGE
);
7281 /* Update ctime/mtime for device path for libblkid */
7282 update_dev_time(device_path
);
7286 * Update the size of all devices, which is used for writing out the
7289 void btrfs_update_commit_device_size(struct btrfs_fs_info
*fs_info
)
7291 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
7292 struct btrfs_device
*curr
, *next
;
7294 if (list_empty(&fs_devices
->resized_devices
))
7297 mutex_lock(&fs_devices
->device_list_mutex
);
7298 mutex_lock(&fs_info
->chunk_mutex
);
7299 list_for_each_entry_safe(curr
, next
, &fs_devices
->resized_devices
,
7301 list_del_init(&curr
->resized_list
);
7302 curr
->commit_total_bytes
= curr
->disk_total_bytes
;
7304 mutex_unlock(&fs_info
->chunk_mutex
);
7305 mutex_unlock(&fs_devices
->device_list_mutex
);
7308 /* Must be invoked during the transaction commit */
7309 void btrfs_update_commit_device_bytes_used(struct btrfs_transaction
*trans
)
7311 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
7312 struct extent_map
*em
;
7313 struct map_lookup
*map
;
7314 struct btrfs_device
*dev
;
7317 if (list_empty(&trans
->pending_chunks
))
7320 /* In order to kick the device replace finish process */
7321 mutex_lock(&fs_info
->chunk_mutex
);
7322 list_for_each_entry(em
, &trans
->pending_chunks
, list
) {
7323 map
= em
->map_lookup
;
7325 for (i
= 0; i
< map
->num_stripes
; i
++) {
7326 dev
= map
->stripes
[i
].dev
;
7327 dev
->commit_bytes_used
= dev
->bytes_used
;
7330 mutex_unlock(&fs_info
->chunk_mutex
);
7333 void btrfs_set_fs_info_ptr(struct btrfs_fs_info
*fs_info
)
7335 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
7336 while (fs_devices
) {
7337 fs_devices
->fs_info
= fs_info
;
7338 fs_devices
= fs_devices
->seed
;
7342 void btrfs_reset_fs_info_ptr(struct btrfs_fs_info
*fs_info
)
7344 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
7345 while (fs_devices
) {
7346 fs_devices
->fs_info
= NULL
;
7347 fs_devices
= fs_devices
->seed
;
7352 * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10.
7354 int btrfs_bg_type_to_factor(u64 flags
)
7356 if (flags
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
7357 BTRFS_BLOCK_GROUP_RAID10
))
7363 static u64
calc_stripe_length(u64 type
, u64 chunk_len
, int num_stripes
)
7365 int index
= btrfs_bg_flags_to_raid_index(type
);
7366 int ncopies
= btrfs_raid_array
[index
].ncopies
;
7369 switch (type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) {
7370 case BTRFS_BLOCK_GROUP_RAID5
:
7371 data_stripes
= num_stripes
- 1;
7373 case BTRFS_BLOCK_GROUP_RAID6
:
7374 data_stripes
= num_stripes
- 2;
7377 data_stripes
= num_stripes
/ ncopies
;
7380 return div_u64(chunk_len
, data_stripes
);
7383 static int verify_one_dev_extent(struct btrfs_fs_info
*fs_info
,
7384 u64 chunk_offset
, u64 devid
,
7385 u64 physical_offset
, u64 physical_len
)
7387 struct extent_map_tree
*em_tree
= &fs_info
->mapping_tree
.map_tree
;
7388 struct extent_map
*em
;
7389 struct map_lookup
*map
;
7395 read_lock(&em_tree
->lock
);
7396 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
7397 read_unlock(&em_tree
->lock
);
7401 "dev extent physical offset %llu on devid %llu doesn't have corresponding chunk",
7402 physical_offset
, devid
);
7407 map
= em
->map_lookup
;
7408 stripe_len
= calc_stripe_length(map
->type
, em
->len
, map
->num_stripes
);
7409 if (physical_len
!= stripe_len
) {
7411 "dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu",
7412 physical_offset
, devid
, em
->start
, physical_len
,
7418 for (i
= 0; i
< map
->num_stripes
; i
++) {
7419 if (map
->stripes
[i
].dev
->devid
== devid
&&
7420 map
->stripes
[i
].physical
== physical_offset
) {
7422 if (map
->verified_stripes
>= map
->num_stripes
) {
7424 "too many dev extents for chunk %llu found",
7429 map
->verified_stripes
++;
7435 "dev extent physical offset %llu devid %llu has no corresponding chunk",
7436 physical_offset
, devid
);
7440 free_extent_map(em
);
7444 static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info
*fs_info
)
7446 struct extent_map_tree
*em_tree
= &fs_info
->mapping_tree
.map_tree
;
7447 struct extent_map
*em
;
7448 struct rb_node
*node
;
7451 read_lock(&em_tree
->lock
);
7452 for (node
= rb_first_cached(&em_tree
->map
); node
; node
= rb_next(node
)) {
7453 em
= rb_entry(node
, struct extent_map
, rb_node
);
7454 if (em
->map_lookup
->num_stripes
!=
7455 em
->map_lookup
->verified_stripes
) {
7457 "chunk %llu has missing dev extent, have %d expect %d",
7458 em
->start
, em
->map_lookup
->verified_stripes
,
7459 em
->map_lookup
->num_stripes
);
7465 read_unlock(&em_tree
->lock
);
7470 * Ensure that all dev extents are mapped to correct chunk, otherwise
7471 * later chunk allocation/free would cause unexpected behavior.
7473 * NOTE: This will iterate through the whole device tree, which should be of
7474 * the same size level as the chunk tree. This slightly increases mount time.
7476 int btrfs_verify_dev_extents(struct btrfs_fs_info
*fs_info
)
7478 struct btrfs_path
*path
;
7479 struct btrfs_root
*root
= fs_info
->dev_root
;
7480 struct btrfs_key key
;
7484 key
.type
= BTRFS_DEV_EXTENT_KEY
;
7487 path
= btrfs_alloc_path();
7491 path
->reada
= READA_FORWARD
;
7492 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
7496 if (path
->slots
[0] >= btrfs_header_nritems(path
->nodes
[0])) {
7497 ret
= btrfs_next_item(root
, path
);
7500 /* No dev extents at all? Not good */
7507 struct extent_buffer
*leaf
= path
->nodes
[0];
7508 struct btrfs_dev_extent
*dext
;
7509 int slot
= path
->slots
[0];
7511 u64 physical_offset
;
7515 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
7516 if (key
.type
!= BTRFS_DEV_EXTENT_KEY
)
7518 devid
= key
.objectid
;
7519 physical_offset
= key
.offset
;
7521 dext
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dev_extent
);
7522 chunk_offset
= btrfs_dev_extent_chunk_offset(leaf
, dext
);
7523 physical_len
= btrfs_dev_extent_length(leaf
, dext
);
7525 ret
= verify_one_dev_extent(fs_info
, chunk_offset
, devid
,
7526 physical_offset
, physical_len
);
7529 ret
= btrfs_next_item(root
, path
);
7538 /* Ensure all chunks have corresponding dev extents */
7539 ret
= verify_chunk_dev_extent_mapping(fs_info
);
7541 btrfs_free_path(path
);