Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Copyright (C) STRATO AG 2012.  All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include <linux/bio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/math64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include "misc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include "ctree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include "extent_map.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include "disk-io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include "transaction.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include "print-tree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include "volumes.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include "async-thread.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include "check-integrity.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include "rcu-string.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include "dev-replace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include "sysfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26)  * Device replace overview
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28)  * [Objective]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29)  * To copy all extents (both new and on-disk) from source device to target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30)  * device, while still keeping the filesystem read-write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32)  * [Method]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33)  * There are two main methods involved:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35)  * - Write duplication
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37)  *   All new writes will be written to both target and source devices, so even
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38)  *   if replace gets canceled, sources device still contans up-to-date data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40)  *   Location:		handle_ops_on_dev_replace() from __btrfs_map_block()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41)  *   Start:		btrfs_dev_replace_start()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42)  *   End:		btrfs_dev_replace_finishing()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43)  *   Content:		Latest data/metadata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45)  * - Copy existing extents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47)  *   This happens by re-using scrub facility, as scrub also iterates through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48)  *   existing extents from commit root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50)  *   Location:		scrub_write_block_to_dev_replace() from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51)  *   			scrub_block_complete()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52)  *   Content:		Data/meta from commit root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54)  * Due to the content difference, we need to avoid nocow write when dev-replace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55)  * is happening.  This is done by marking the block group read-only and waiting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56)  * for NOCOW writes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58)  * After replace is done, the finishing part is done by swapping the target and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59)  * source devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61)  *   Location:		btrfs_dev_replace_update_device_in_mapping_tree() from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62)  *   			btrfs_dev_replace_finishing()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 				       int scrub_ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) static int btrfs_dev_replace_kthread(void *data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	struct btrfs_root *dev_root = fs_info->dev_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	struct extent_buffer *eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	struct btrfs_path *path = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	int item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	struct btrfs_dev_replace_item *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	u64 src_devid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	key.objectid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	key.type = BTRFS_DEV_REPLACE_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) no_valid_dev_replace_entry_found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 		 * We don't have a replace item or it's corrupted.  If there is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 		 * a replace target, fail the mount.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 		if (btrfs_find_device(fs_info->fs_devices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 				      BTRFS_DEV_REPLACE_DEVID, NULL, NULL, false)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 			btrfs_err(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 			"found replace target device without a valid replace item");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 			ret = -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 		dev_replace->replace_state =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 			BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 		dev_replace->cont_reading_from_srcdev_mode =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 		    BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_ALWAYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 		dev_replace->time_started = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 		dev_replace->time_stopped = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 		atomic64_set(&dev_replace->num_write_errors, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 		atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 		dev_replace->cursor_left = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 		dev_replace->committed_cursor_left = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 		dev_replace->cursor_left_last_write_of_item = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 		dev_replace->cursor_right = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 		dev_replace->srcdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 		dev_replace->tgtdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 		dev_replace->is_valid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 		dev_replace->item_needs_writeback = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	eb = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	item_size = btrfs_item_size_nr(eb, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	ptr = btrfs_item_ptr(eb, slot, struct btrfs_dev_replace_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	if (item_size != sizeof(struct btrfs_dev_replace_item)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 		btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 			"dev_replace entry found has unexpected size, ignore entry");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 		goto no_valid_dev_replace_entry_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	src_devid = btrfs_dev_replace_src_devid(eb, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	dev_replace->cont_reading_from_srcdev_mode =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 		btrfs_dev_replace_cont_reading_from_srcdev_mode(eb, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	dev_replace->replace_state = btrfs_dev_replace_replace_state(eb, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	dev_replace->time_started = btrfs_dev_replace_time_started(eb, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	dev_replace->time_stopped =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 		btrfs_dev_replace_time_stopped(eb, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	atomic64_set(&dev_replace->num_write_errors,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 		     btrfs_dev_replace_num_write_errors(eb, ptr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	atomic64_set(&dev_replace->num_uncorrectable_read_errors,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 		     btrfs_dev_replace_num_uncorrectable_read_errors(eb, ptr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	dev_replace->cursor_left = btrfs_dev_replace_cursor_left(eb, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	dev_replace->committed_cursor_left = dev_replace->cursor_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	dev_replace->cursor_left_last_write_of_item = dev_replace->cursor_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	dev_replace->cursor_right = btrfs_dev_replace_cursor_right(eb, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	dev_replace->is_valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	dev_replace->item_needs_writeback = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	switch (dev_replace->replace_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 		 * We don't have an active replace item but if there is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 		 * replace target, fail the mount.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 		if (btrfs_find_device(fs_info->fs_devices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 				      BTRFS_DEV_REPLACE_DEVID, NULL, NULL, false)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 			btrfs_err(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 			"replace devid present without an active replace item");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 			ret = -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 			dev_replace->srcdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 			dev_replace->tgtdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 		dev_replace->srcdev = btrfs_find_device(fs_info->fs_devices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 						src_devid, NULL, NULL, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 		dev_replace->tgtdev = btrfs_find_device(fs_info->fs_devices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 							BTRFS_DEV_REPLACE_DEVID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 							NULL, NULL, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 		 * allow 'btrfs dev replace_cancel' if src/tgt device is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 		 * missing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 		if (!dev_replace->srcdev &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 		    !btrfs_test_opt(fs_info, DEGRADED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 			ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 			btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 			   "cannot mount because device replace operation is ongoing and");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 			btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 			   "srcdev (devid %llu) is missing, need to run 'btrfs dev scan'?",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 			   src_devid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 		if (!dev_replace->tgtdev &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 		    !btrfs_test_opt(fs_info, DEGRADED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 			ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 			btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 			   "cannot mount because device replace operation is ongoing and");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 			btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 			   "tgtdev (devid %llu) is missing, need to run 'btrfs dev scan'?",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 				BTRFS_DEV_REPLACE_DEVID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		if (dev_replace->tgtdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 			if (dev_replace->srcdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 				dev_replace->tgtdev->total_bytes =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 					dev_replace->srcdev->total_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 				dev_replace->tgtdev->disk_total_bytes =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 					dev_replace->srcdev->disk_total_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 				dev_replace->tgtdev->commit_total_bytes =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 					dev_replace->srcdev->commit_total_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 				dev_replace->tgtdev->bytes_used =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 					dev_replace->srcdev->bytes_used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 				dev_replace->tgtdev->commit_bytes_used =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 					dev_replace->srcdev->commit_bytes_used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 			set_bit(BTRFS_DEV_STATE_REPLACE_TGT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 				&dev_replace->tgtdev->dev_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 			WARN_ON(fs_info->fs_devices->rw_devices == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 			dev_replace->tgtdev->io_width = fs_info->sectorsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 			dev_replace->tgtdev->io_align = fs_info->sectorsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 			dev_replace->tgtdev->sector_size = fs_info->sectorsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 			dev_replace->tgtdev->fs_info = fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 			set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 				&dev_replace->tgtdev->dev_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233)  * Initialize a new device for device replace target from a given source dev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234)  * and path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236)  * Return 0 and new device in @device_out, otherwise return < 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) static int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 				  const char *device_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 				  struct btrfs_device *srcdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 				  struct btrfs_device **device_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	struct btrfs_device *device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	struct block_device *bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	struct rcu_string *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	u64 devid = BTRFS_DEV_REPLACE_DEVID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	*device_out = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	if (srcdev->fs_devices->seeding) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 		btrfs_err(fs_info, "the filesystem is a seed filesystem!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 				  fs_info->bdev_holder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	if (IS_ERR(bdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 		btrfs_err(fs_info, "target device %s is invalid!", device_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 		return PTR_ERR(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	sync_blockdev(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 		if (device->bdev == bdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 			btrfs_err(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 				  "target device is in the filesystem!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 			ret = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	if (i_size_read(bdev->bd_inode) <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	    btrfs_device_get_total_bytes(srcdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 		btrfs_err(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 			  "target device is smaller than source device!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	device = btrfs_alloc_device(NULL, &devid, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	if (IS_ERR(device)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 		ret = PTR_ERR(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	name = rcu_string_strdup(device_path, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 		btrfs_free_device(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	rcu_assign_pointer(device->name, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	device->generation = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	device->io_width = fs_info->sectorsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	device->io_align = fs_info->sectorsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	device->sector_size = fs_info->sectorsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	device->total_bytes = btrfs_device_get_total_bytes(srcdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	device->bytes_used = btrfs_device_get_bytes_used(srcdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	device->commit_total_bytes = srcdev->commit_total_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	device->commit_bytes_used = device->bytes_used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	device->fs_info = fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	device->bdev = bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	set_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	device->mode = FMODE_EXCL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	device->dev_stats_valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	device->fs_devices = fs_info->fs_devices;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	list_add(&device->dev_list, &fs_info->fs_devices->devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	fs_info->fs_devices->num_devices++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	fs_info->fs_devices->open_devices++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	mutex_unlock(&fs_info->fs_devices->device_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	*device_out = device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	blkdev_put(bdev, FMODE_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331)  * called from commit_transaction. Writes changed device replace state to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332)  * disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) int btrfs_run_dev_replace(struct btrfs_trans_handle *trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	struct btrfs_root *dev_root = fs_info->dev_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	struct extent_buffer *eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	struct btrfs_dev_replace_item *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	down_read(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	if (!dev_replace->is_valid ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	    !dev_replace->item_needs_writeback) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 		up_read(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	up_read(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	key.objectid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	key.type = BTRFS_DEV_REPLACE_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 		btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 			   "error %d while searching for dev_replace item!",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 			   ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	if (ret == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	    btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 		 * need to delete old one and insert a new one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 		 * Since no attempt is made to recover any old state, if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 		 * dev_replace state is 'running', the data on the target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 		 * drive is lost.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 		 * It would be possible to recover the state: just make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 		 * that the beginning of the item is never changed and always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 		 * contains all the essential information. Then read this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 		 * minimal set of information and use it as a base for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 		 * new state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 		ret = btrfs_del_item(trans, dev_root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 		if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 			btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 				   "delete too small dev_replace item failed %d!",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 				   ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 		ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	if (ret == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 		/* need to insert a new item */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 		btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 		ret = btrfs_insert_empty_item(trans, dev_root, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 					      &key, sizeof(*ptr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 			btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 				   "insert dev_replace item failed %d!", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	eb = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	ptr = btrfs_item_ptr(eb, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 			     struct btrfs_dev_replace_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	down_write(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	if (dev_replace->srcdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 		btrfs_set_dev_replace_src_devid(eb, ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 			dev_replace->srcdev->devid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 		btrfs_set_dev_replace_src_devid(eb, ptr, (u64)-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	btrfs_set_dev_replace_cont_reading_from_srcdev_mode(eb, ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 		dev_replace->cont_reading_from_srcdev_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	btrfs_set_dev_replace_replace_state(eb, ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 		dev_replace->replace_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	btrfs_set_dev_replace_time_started(eb, ptr, dev_replace->time_started);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	btrfs_set_dev_replace_time_stopped(eb, ptr, dev_replace->time_stopped);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	btrfs_set_dev_replace_num_write_errors(eb, ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 		atomic64_read(&dev_replace->num_write_errors));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	btrfs_set_dev_replace_num_uncorrectable_read_errors(eb, ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		atomic64_read(&dev_replace->num_uncorrectable_read_errors));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	dev_replace->cursor_left_last_write_of_item =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 		dev_replace->cursor_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	btrfs_set_dev_replace_cursor_left(eb, ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 		dev_replace->cursor_left_last_write_of_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	btrfs_set_dev_replace_cursor_right(eb, ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 		dev_replace->cursor_right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	dev_replace->item_needs_writeback = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	up_write(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	btrfs_mark_buffer_dirty(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) static char* btrfs_dev_name(struct btrfs_device *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	if (!device || test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 		return "<missing disk>";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 		return rcu_str_deref(device->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) static int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 		const char *tgtdev_name, u64 srcdevid, const char *srcdev_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 		int read_src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	struct btrfs_root *root = fs_info->dev_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	struct btrfs_device *tgt_device = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	struct btrfs_device *src_device = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	src_device = btrfs_find_device_by_devspec(fs_info, srcdevid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 						  srcdev_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	if (IS_ERR(src_device))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 		return PTR_ERR(src_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	if (btrfs_pinned_by_swapfile(fs_info, src_device)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 		btrfs_warn_in_rcu(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	  "cannot replace device %s (devid %llu) due to active swapfile",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 			btrfs_dev_name(src_device), src_device->devid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 		return -ETXTBSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	 * Here we commit the transaction to make sure commit_total_bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	 * of all the devices are updated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	trans = btrfs_attach_transaction(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	if (!IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 		ret = btrfs_commit_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	} else if (PTR_ERR(trans) != -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 		return PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	ret = btrfs_init_dev_replace_tgtdev(fs_info, tgtdev_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 					    src_device, &tgt_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	down_write(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	switch (dev_replace->replace_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 		ASSERT(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 		ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_ALREADY_STARTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		up_write(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 		goto leave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	dev_replace->cont_reading_from_srcdev_mode = read_src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	dev_replace->srcdev = src_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	dev_replace->tgtdev = tgt_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	btrfs_info_in_rcu(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		      "dev_replace from %s (devid %llu) to %s started",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		      btrfs_dev_name(src_device),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		      src_device->devid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 		      rcu_str_deref(tgt_device->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	 * from now on, the writes to the srcdev are all duplicated to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	 * go to the tgtdev as well (refer to btrfs_map_block()).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	dev_replace->replace_state = BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	dev_replace->time_started = ktime_get_real_seconds();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	dev_replace->cursor_left = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	dev_replace->committed_cursor_left = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	dev_replace->cursor_left_last_write_of_item = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	dev_replace->cursor_right = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	dev_replace->is_valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	dev_replace->item_needs_writeback = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	atomic64_set(&dev_replace->num_write_errors, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	up_write(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	ret = btrfs_sysfs_add_device(tgt_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		btrfs_err(fs_info, "kobj add dev failed %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	/* Commit dev_replace state and reserve 1 item for it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	trans = btrfs_start_transaction(root, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 		down_write(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		dev_replace->replace_state =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 			BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 		dev_replace->srcdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 		dev_replace->tgtdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		up_write(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 		goto leave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	ret = btrfs_commit_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	WARN_ON(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	/* the disk copy procedure reuses the scrub code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	ret = btrfs_scrub_dev(fs_info, src_device->devid, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 			      btrfs_device_get_total_bytes(src_device),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 			      &dev_replace->scrub_progress, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	ret = btrfs_dev_replace_finishing(fs_info, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	if (ret == -EINPROGRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 		ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) leave:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	btrfs_destroy_dev_replace_tgtdev(tgt_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) int btrfs_dev_replace_by_ioctl(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 			    struct btrfs_ioctl_dev_replace_args *args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	switch (args->start.cont_reading_from_srcdev_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	if ((args->start.srcdevid == 0 && args->start.srcdev_name[0] == '\0') ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	    args->start.tgtdev_name[0] == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	ret = btrfs_dev_replace_start(fs_info, args->start.tgtdev_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 					args->start.srcdevid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 					args->start.srcdev_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 					args->start.cont_reading_from_srcdev_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	args->result = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	/* don't warn if EINPROGRESS, someone else might be running scrub */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	if (ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	    ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600)  * blocked until all in-flight bios operations are finished.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) static void btrfs_rm_dev_replace_blocked(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	set_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	wait_event(fs_info->dev_replace.replace_wait, !percpu_counter_sum(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 		   &fs_info->dev_replace.bio_counter));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610)  * we have removed target device, it is safe to allow new bios request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) static void btrfs_rm_dev_replace_unblocked(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	clear_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	wake_up(&fs_info->dev_replace.replace_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619)  * When finishing the device replace, before swapping the source device with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620)  * target device we must update the chunk allocation state in the target device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621)  * as it is empty because replace works by directly copying the chunks and not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622)  * through the normal chunk allocation path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) static int btrfs_set_target_alloc_state(struct btrfs_device *srcdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 					struct btrfs_device *tgtdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	struct extent_state *cached_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	u64 start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	u64 found_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	u64 found_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	lockdep_assert_held(&srcdev->fs_info->chunk_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	while (!find_first_extent_bit(&srcdev->alloc_state, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 				      &found_start, &found_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 				      CHUNK_ALLOCATED, &cached_state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		ret = set_extent_bits(&tgtdev->alloc_state, found_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 				      found_end, CHUNK_ALLOCATED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		start = found_end + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	free_extent_state(cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) static void btrfs_dev_replace_update_device_in_mapping_tree(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 						struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 						struct btrfs_device *srcdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 						struct btrfs_device *tgtdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	struct extent_map_tree *em_tree = &fs_info->mapping_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	struct map_lookup *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	u64 start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	write_lock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 		em = lookup_extent_mapping(em_tree, start, (u64)-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		if (!em)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 		map = em->map_lookup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 		for (i = 0; i < map->num_stripes; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 			if (srcdev == map->stripes[i].dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 				map->stripes[i].dev = tgtdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 		start = em->start + em->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 		free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	} while (start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	write_unlock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 				       int scrub_ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	struct btrfs_device *tgt_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	struct btrfs_device *src_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	struct btrfs_root *root = fs_info->tree_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	u8 uuid_tmp[BTRFS_UUID_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	/* don't allow cancel or unmount to disturb the finishing procedure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	down_read(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	/* was the operation canceled, or is it finished? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	if (dev_replace->replace_state !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	    BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		up_read(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	tgt_device = dev_replace->tgtdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	src_device = dev_replace->srcdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	up_read(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	 * flush all outstanding I/O and inode extent mappings before the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	 * copy operation is declared as being finished
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	ret = btrfs_start_delalloc_roots(fs_info, U64_MAX, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 		mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	if (!scrub_ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 		btrfs_reada_remove_dev(src_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	 * We have to use this loop approach because at this point src_device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	 * has to be available for transaction commit to complete, yet new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	 * chunks shouldn't be allocated on the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		trans = btrfs_start_transaction(root, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 			btrfs_reada_undo_remove_dev(src_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 			mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 			return PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 		ret = btrfs_commit_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 		WARN_ON(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 		/* Prevent write_all_supers() during the finishing procedure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 		mutex_lock(&fs_info->fs_devices->device_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 		/* Prevent new chunks being allocated on the source device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		mutex_lock(&fs_info->chunk_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 		if (!list_empty(&src_device->post_commit_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 			mutex_unlock(&fs_info->fs_devices->device_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 			mutex_unlock(&fs_info->chunk_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	down_write(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	dev_replace->replace_state =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 		scrub_ret ? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 			  : BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	dev_replace->tgtdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	dev_replace->srcdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	dev_replace->time_stopped = ktime_get_real_seconds();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 	dev_replace->item_needs_writeback = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	 * Update allocation state in the new device and replace the old device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	 * with the new one in the mapping tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	if (!scrub_ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 		scrub_ret = btrfs_set_target_alloc_state(src_device, tgt_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 		if (scrub_ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 		btrfs_dev_replace_update_device_in_mapping_tree(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 								src_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 								tgt_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 		if (scrub_ret != -ECANCELED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 			btrfs_err_in_rcu(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 				 "btrfs_scrub_dev(%s, %llu, %s) failed %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 				 btrfs_dev_name(src_device),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 				 src_device->devid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 				 rcu_str_deref(tgt_device->name), scrub_ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		up_write(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		mutex_unlock(&fs_info->chunk_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 		mutex_unlock(&fs_info->fs_devices->device_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 		btrfs_reada_undo_remove_dev(src_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 		btrfs_rm_dev_replace_blocked(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 		if (tgt_device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 			btrfs_destroy_dev_replace_tgtdev(tgt_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 		btrfs_rm_dev_replace_unblocked(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 		return scrub_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	btrfs_info_in_rcu(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 			  "dev_replace from %s (devid %llu) to %s finished",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 			  btrfs_dev_name(src_device),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 			  src_device->devid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 			  rcu_str_deref(tgt_device->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &tgt_device->dev_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	tgt_device->devid = src_device->devid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	src_device->devid = BTRFS_DEV_REPLACE_DEVID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	memcpy(uuid_tmp, tgt_device->uuid, sizeof(uuid_tmp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	memcpy(tgt_device->uuid, src_device->uuid, sizeof(tgt_device->uuid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	memcpy(src_device->uuid, uuid_tmp, sizeof(src_device->uuid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	btrfs_device_set_total_bytes(tgt_device, src_device->total_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	btrfs_device_set_disk_total_bytes(tgt_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 					  src_device->disk_total_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	btrfs_device_set_bytes_used(tgt_device, src_device->bytes_used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	tgt_device->commit_bytes_used = src_device->bytes_used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	btrfs_assign_next_active_device(src_device, tgt_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	list_add(&tgt_device->dev_alloc_list, &fs_info->fs_devices->alloc_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	fs_info->fs_devices->rw_devices++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	up_write(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	btrfs_rm_dev_replace_blocked(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	btrfs_rm_dev_replace_remove_srcdev(src_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	btrfs_rm_dev_replace_unblocked(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	 * Increment dev_stats_ccnt so that btrfs_run_dev_stats() will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	 * update on-disk dev stats value during commit transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	atomic_inc(&tgt_device->dev_stats_ccnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	 * this is again a consistent state where no dev_replace procedure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	 * is running, the target device is part of the filesystem, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	 * source device is not part of the filesystem anymore and its 1st
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	 * superblock is scratched out so that it is no longer marked to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	 * belong to this filesystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	mutex_unlock(&fs_info->chunk_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	mutex_unlock(&fs_info->fs_devices->device_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	/* replace the sysfs entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	btrfs_sysfs_remove_device(src_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	btrfs_sysfs_update_devid(tgt_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &src_device->dev_state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 		btrfs_scratch_superblocks(fs_info, src_device->bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 					  src_device->name->str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	/* write back the superblocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	trans = btrfs_start_transaction(root, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	if (!IS_ERR(trans))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 		btrfs_commit_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	btrfs_rm_dev_replace_free_srcdev(src_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850)  * Read progress of device replace status according to the state and last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851)  * stored position. The value format is the same as for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852)  * btrfs_dev_replace::progress_1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) static u64 btrfs_dev_replace_progress(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	u64 ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	switch (dev_replace->replace_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 		ret = 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 		ret = div64_u64(dev_replace->cursor_left,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 				div_u64(btrfs_device_get_total_bytes(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 						dev_replace->srcdev), 1000));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) void btrfs_dev_replace_status(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 			      struct btrfs_ioctl_dev_replace_args *args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	down_read(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	/* even if !dev_replace_is_valid, the values are good enough for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	 * the replace_status ioctl */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	args->status.replace_state = dev_replace->replace_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	args->status.time_started = dev_replace->time_started;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	args->status.time_stopped = dev_replace->time_stopped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	args->status.num_write_errors =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 		atomic64_read(&dev_replace->num_write_errors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	args->status.num_uncorrectable_read_errors =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		atomic64_read(&dev_replace->num_uncorrectable_read_errors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	args->status.progress_1000 = btrfs_dev_replace_progress(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	up_read(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) int btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	struct btrfs_device *tgt_device = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	struct btrfs_device *src_device = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	struct btrfs_root *root = fs_info->tree_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	if (sb_rdonly(fs_info->sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	down_write(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	switch (dev_replace->replace_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		up_write(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 		tgt_device = dev_replace->tgtdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		src_device = dev_replace->srcdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 		up_write(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 		ret = btrfs_scrub_cancel(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 			result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 			result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 			 * btrfs_dev_replace_finishing() will handle the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 			 * cleanup part
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 			btrfs_info_in_rcu(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 				"dev_replace from %s (devid %llu) to %s canceled",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 				btrfs_dev_name(src_device), src_device->devid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 				btrfs_dev_name(tgt_device));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 		 * Scrub doing the replace isn't running so we need to do the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 		 * cleanup step of btrfs_dev_replace_finishing() here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		tgt_device = dev_replace->tgtdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 		src_device = dev_replace->srcdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 		dev_replace->tgtdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 		dev_replace->srcdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 		dev_replace->replace_state =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 				BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 		dev_replace->time_stopped = ktime_get_real_seconds();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 		dev_replace->item_needs_writeback = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		up_write(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		/* Scrub for replace must not be running in suspended state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 		ret = btrfs_scrub_cancel(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 		ASSERT(ret != -ENOTCONN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 		trans = btrfs_start_transaction(root, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 		if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 			mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 			return PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		ret = btrfs_commit_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		WARN_ON(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		btrfs_info_in_rcu(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		"suspended dev_replace from %s (devid %llu) to %s canceled",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 			btrfs_dev_name(src_device), src_device->devid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 			btrfs_dev_name(tgt_device));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 		if (tgt_device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 			btrfs_destroy_dev_replace_tgtdev(tgt_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		up_write(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		result = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) void btrfs_dev_replace_suspend_for_unmount(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	down_write(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	switch (dev_replace->replace_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		dev_replace->replace_state =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 			BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 		dev_replace->time_stopped = ktime_get_real_seconds();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 		dev_replace->item_needs_writeback = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 		btrfs_info(fs_info, "suspending dev_replace for unmount");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	up_write(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) /* resume dev_replace procedure that was interrupted by unmount */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) int btrfs_resume_dev_replace_async(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	struct task_struct *task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	down_write(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	switch (dev_replace->replace_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 		up_write(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 		dev_replace->replace_state =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 			BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	if (!dev_replace->tgtdev || !dev_replace->tgtdev->bdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		btrfs_info(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 			   "cannot continue dev_replace, tgtdev is missing");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 		btrfs_info(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 			   "you may cancel the operation after 'mount -o degraded'");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 		dev_replace->replace_state =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 					BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 		up_write(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	up_write(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	 * This could collide with a paused balance, but the exclusive op logic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	 * should never allow both to start and pause. We don't want to allow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	 * dev-replace to start anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REPLACE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 		down_write(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 		dev_replace->replace_state =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 					BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 		up_write(&dev_replace->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 		btrfs_info(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 		"cannot resume dev-replace, other exclusive operation running");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	task = kthread_run(btrfs_dev_replace_kthread, fs_info, "btrfs-devrepl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	return PTR_ERR_OR_ZERO(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) static int btrfs_dev_replace_kthread(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	struct btrfs_fs_info *fs_info = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	u64 progress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	progress = btrfs_dev_replace_progress(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	progress = div_u64(progress, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	btrfs_info_in_rcu(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 		"continuing dev_replace from %s (devid %llu) to target %s @%u%%",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 		btrfs_dev_name(dev_replace->srcdev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 		dev_replace->srcdev->devid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 		btrfs_dev_name(dev_replace->tgtdev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 		(unsigned int)progress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	ret = btrfs_scrub_dev(fs_info, dev_replace->srcdev->devid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 			      dev_replace->committed_cursor_left,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 			      btrfs_device_get_total_bytes(dev_replace->srcdev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 			      &dev_replace->scrub_progress, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	ret = btrfs_dev_replace_finishing(fs_info, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	WARN_ON(ret && ret != -ECANCELED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	btrfs_exclop_finish(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) int __pure btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace *dev_replace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	if (!dev_replace->is_valid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	switch (dev_replace->replace_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 		 * return true even if tgtdev is missing (this is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 		 * something that can happen if the dev_replace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 		 * procedure is suspended by an umount and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 		 * the tgtdev is missing (or "btrfs dev scan") was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 		 * not called and the filesystem is remounted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 		 * in degraded state. This does not stop the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 		 * dev_replace procedure. It needs to be canceled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 		 * manually if the cancellation is wanted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) void btrfs_bio_counter_inc_noblocked(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	percpu_counter_inc(&fs_info->dev_replace.bio_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) void btrfs_bio_counter_sub(struct btrfs_fs_info *fs_info, s64 amount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	percpu_counter_sub(&fs_info->dev_replace.bio_counter, amount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	cond_wake_up_nomb(&fs_info->dev_replace.replace_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) void btrfs_bio_counter_inc_blocked(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 		percpu_counter_inc(&fs_info->dev_replace.bio_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 		if (likely(!test_bit(BTRFS_FS_STATE_DEV_REPLACING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 				     &fs_info->fs_state)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 		btrfs_bio_counter_dec(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 		wait_event(fs_info->dev_replace.replace_wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 			   !test_bit(BTRFS_FS_STATE_DEV_REPLACING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 				     &fs_info->fs_state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) }