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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2)  * Copyright (C) 2010-2011 Neil Brown
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * This file is released under the GPL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) 
^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/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include "md.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include "raid1.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include "raid5.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include "raid10.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include "md-bitmap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/device-mapper.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #define DM_MSG_PREFIX "raid"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #define	MAX_RAID_DEVICES	253 /* md-raid kernel limit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23)  * Minimum sectors of free reshape space per raid device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #define	MIN_FREE_RESHAPE_SPACE to_sector(4*4096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28)  * Minimum journal space 4 MiB in sectors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #define	MIN_RAID456_JOURNAL_SPACE (4*2048)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) static bool devices_handle_discard_safely = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35)  * The following flags are used by dm-raid.c to set up the array state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36)  * They must be cleared before md_run is called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #define FirstUse 10		/* rdev flag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) struct raid_dev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 	 * Two DM devices, one to hold metadata and one to hold the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 	 * actual data/parity.	The reason for this is to not confuse
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 	 * ti->len and give more flexibility in altering size and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 	 * characteristics.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 	 * While it is possible for this device to be associated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 	 * with a different physical device than the data_dev, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	 * is intended for it to be the same.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	 *    |--------- Physical Device ---------|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 	 *    |- meta_dev -|------ data_dev ------|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	struct dm_dev *meta_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 	struct dm_dev *data_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	struct md_rdev rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59)  * Bits for establishing rs->ctr_flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61)  * 1 = no flag value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62)  * 2 = flag with value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) #define __CTR_FLAG_SYNC			0  /* 1 */ /* Not with raid0! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) #define __CTR_FLAG_NOSYNC		1  /* 1 */ /* Not with raid0! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) #define __CTR_FLAG_REBUILD		2  /* 2 */ /* Not with raid0! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) #define __CTR_FLAG_DAEMON_SLEEP		3  /* 2 */ /* Not with raid0! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) #define __CTR_FLAG_MIN_RECOVERY_RATE	4  /* 2 */ /* Not with raid0! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) #define __CTR_FLAG_MAX_RECOVERY_RATE	5  /* 2 */ /* Not with raid0! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) #define __CTR_FLAG_MAX_WRITE_BEHIND	6  /* 2 */ /* Only with raid1! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) #define __CTR_FLAG_WRITE_MOSTLY		7  /* 2 */ /* Only with raid1! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) #define __CTR_FLAG_STRIPE_CACHE		8  /* 2 */ /* Only with raid4/5/6! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) #define __CTR_FLAG_REGION_SIZE		9  /* 2 */ /* Not with raid0! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) #define __CTR_FLAG_RAID10_COPIES	10 /* 2 */ /* Only with raid10 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) #define __CTR_FLAG_RAID10_FORMAT	11 /* 2 */ /* Only with raid10 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) /* New for v1.9.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) #define __CTR_FLAG_DELTA_DISKS		12 /* 2 */ /* Only with reshapable raid1/4/5/6/10! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) #define __CTR_FLAG_DATA_OFFSET		13 /* 2 */ /* Only with reshapable raid4/5/6/10! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) #define __CTR_FLAG_RAID10_USE_NEAR_SETS 14 /* 2 */ /* Only with raid10! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) /* New for v1.10.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) #define __CTR_FLAG_JOURNAL_DEV		15 /* 2 */ /* Only with raid4/5/6 (journal device)! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) /* New for v1.11.1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) #define __CTR_FLAG_JOURNAL_MODE		16 /* 2 */ /* Only with raid4/5/6 (journal mode)! */
^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)  * Flags for rs->ctr_flags field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) #define CTR_FLAG_SYNC			(1 << __CTR_FLAG_SYNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) #define CTR_FLAG_NOSYNC			(1 << __CTR_FLAG_NOSYNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) #define CTR_FLAG_REBUILD		(1 << __CTR_FLAG_REBUILD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) #define CTR_FLAG_DAEMON_SLEEP		(1 << __CTR_FLAG_DAEMON_SLEEP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) #define CTR_FLAG_MIN_RECOVERY_RATE	(1 << __CTR_FLAG_MIN_RECOVERY_RATE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) #define CTR_FLAG_MAX_RECOVERY_RATE	(1 << __CTR_FLAG_MAX_RECOVERY_RATE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) #define CTR_FLAG_MAX_WRITE_BEHIND	(1 << __CTR_FLAG_MAX_WRITE_BEHIND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) #define CTR_FLAG_WRITE_MOSTLY		(1 << __CTR_FLAG_WRITE_MOSTLY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) #define CTR_FLAG_STRIPE_CACHE		(1 << __CTR_FLAG_STRIPE_CACHE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) #define CTR_FLAG_REGION_SIZE		(1 << __CTR_FLAG_REGION_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) #define CTR_FLAG_RAID10_COPIES		(1 << __CTR_FLAG_RAID10_COPIES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) #define CTR_FLAG_RAID10_FORMAT		(1 << __CTR_FLAG_RAID10_FORMAT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) #define CTR_FLAG_DELTA_DISKS		(1 << __CTR_FLAG_DELTA_DISKS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) #define CTR_FLAG_DATA_OFFSET		(1 << __CTR_FLAG_DATA_OFFSET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) #define CTR_FLAG_RAID10_USE_NEAR_SETS	(1 << __CTR_FLAG_RAID10_USE_NEAR_SETS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) #define CTR_FLAG_JOURNAL_DEV		(1 << __CTR_FLAG_JOURNAL_DEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) #define CTR_FLAG_JOURNAL_MODE		(1 << __CTR_FLAG_JOURNAL_MODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109)  * Definitions of various constructor flags to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110)  * be used in checks of valid / invalid flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111)  * per raid level.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) /* Define all any sync flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) #define	CTR_FLAGS_ANY_SYNC		(CTR_FLAG_SYNC | CTR_FLAG_NOSYNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) /* Define flags for options without argument (e.g. 'nosync') */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) #define	CTR_FLAG_OPTIONS_NO_ARGS	(CTR_FLAGS_ANY_SYNC | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 					 CTR_FLAG_RAID10_USE_NEAR_SETS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) /* Define flags for options with one argument (e.g. 'delta_disks +2') */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) #define CTR_FLAG_OPTIONS_ONE_ARG (CTR_FLAG_REBUILD | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 				  CTR_FLAG_WRITE_MOSTLY | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 				  CTR_FLAG_DAEMON_SLEEP | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 				  CTR_FLAG_MIN_RECOVERY_RATE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 				  CTR_FLAG_MAX_RECOVERY_RATE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 				  CTR_FLAG_MAX_WRITE_BEHIND | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 				  CTR_FLAG_STRIPE_CACHE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 				  CTR_FLAG_REGION_SIZE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 				  CTR_FLAG_RAID10_COPIES | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 				  CTR_FLAG_RAID10_FORMAT | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 				  CTR_FLAG_DELTA_DISKS | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 				  CTR_FLAG_DATA_OFFSET | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 				  CTR_FLAG_JOURNAL_DEV | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 				  CTR_FLAG_JOURNAL_MODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) /* Valid options definitions per raid level... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) /* "raid0" does only accept data offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) #define RAID0_VALID_FLAGS	(CTR_FLAG_DATA_OFFSET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) /* "raid1" does not accept stripe cache, data offset, delta_disks or any raid10 options */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) #define RAID1_VALID_FLAGS	(CTR_FLAGS_ANY_SYNC | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 				 CTR_FLAG_REBUILD | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 				 CTR_FLAG_WRITE_MOSTLY | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 				 CTR_FLAG_DAEMON_SLEEP | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 				 CTR_FLAG_MIN_RECOVERY_RATE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 				 CTR_FLAG_MAX_RECOVERY_RATE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 				 CTR_FLAG_MAX_WRITE_BEHIND | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 				 CTR_FLAG_REGION_SIZE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 				 CTR_FLAG_DELTA_DISKS | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 				 CTR_FLAG_DATA_OFFSET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) /* "raid10" does not accept any raid1 or stripe cache options */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) #define RAID10_VALID_FLAGS	(CTR_FLAGS_ANY_SYNC | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 				 CTR_FLAG_REBUILD | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 				 CTR_FLAG_DAEMON_SLEEP | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 				 CTR_FLAG_MIN_RECOVERY_RATE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 				 CTR_FLAG_MAX_RECOVERY_RATE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 				 CTR_FLAG_REGION_SIZE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 				 CTR_FLAG_RAID10_COPIES | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 				 CTR_FLAG_RAID10_FORMAT | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 				 CTR_FLAG_DELTA_DISKS | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 				 CTR_FLAG_DATA_OFFSET | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 				 CTR_FLAG_RAID10_USE_NEAR_SETS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167)  * "raid4/5/6" do not accept any raid1 or raid10 specific options
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169)  * "raid6" does not accept "nosync", because it is not guaranteed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170)  * that both parity and q-syndrome are being written properly with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171)  * any writes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) #define RAID45_VALID_FLAGS	(CTR_FLAGS_ANY_SYNC | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 				 CTR_FLAG_REBUILD | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 				 CTR_FLAG_DAEMON_SLEEP | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 				 CTR_FLAG_MIN_RECOVERY_RATE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 				 CTR_FLAG_MAX_RECOVERY_RATE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 				 CTR_FLAG_STRIPE_CACHE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 				 CTR_FLAG_REGION_SIZE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 				 CTR_FLAG_DELTA_DISKS | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 				 CTR_FLAG_DATA_OFFSET | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 				 CTR_FLAG_JOURNAL_DEV | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 				 CTR_FLAG_JOURNAL_MODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) #define RAID6_VALID_FLAGS	(CTR_FLAG_SYNC | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 				 CTR_FLAG_REBUILD | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 				 CTR_FLAG_DAEMON_SLEEP | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 				 CTR_FLAG_MIN_RECOVERY_RATE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 				 CTR_FLAG_MAX_RECOVERY_RATE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 				 CTR_FLAG_STRIPE_CACHE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 				 CTR_FLAG_REGION_SIZE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 				 CTR_FLAG_DELTA_DISKS | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 				 CTR_FLAG_DATA_OFFSET | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 				 CTR_FLAG_JOURNAL_DEV | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 				 CTR_FLAG_JOURNAL_MODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) /* ...valid options definitions per raid level */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199)  * Flags for rs->runtime_flags field
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200)  * (RT_FLAG prefix meaning "runtime flag")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202)  * These are all internal and used to define runtime state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203)  * e.g. to prevent another resume from preresume processing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204)  * the raid set all over again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) #define RT_FLAG_RS_PRERESUMED		0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) #define RT_FLAG_RS_RESUMED		1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) #define RT_FLAG_RS_BITMAP_LOADED	2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) #define RT_FLAG_UPDATE_SBS		3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) #define RT_FLAG_RESHAPE_RS		4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) #define RT_FLAG_RS_SUSPENDED		5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) #define RT_FLAG_RS_IN_SYNC		6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) #define RT_FLAG_RS_RESYNCING		7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) #define RT_FLAG_RS_GROW			8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) /* Array elements of 64 bit needed for rebuild/failed disk bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) #define DISKS_ARRAY_ELEMS ((MAX_RAID_DEVICES + (sizeof(uint64_t) * 8 - 1)) / sizeof(uint64_t) / 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220)  * raid set level, layout and chunk sectors backup/restore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) struct rs_layout {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	int new_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	int new_layout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	int new_chunk_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) struct raid_set {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	struct dm_target *ti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	uint32_t stripe_cache_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	unsigned long ctr_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	unsigned long runtime_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	uint64_t rebuild_disks[DISKS_ARRAY_ELEMS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	int raid_disks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	int delta_disks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	int data_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	int raid10_copies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	int requested_bitmap_chunk_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	struct mddev md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	struct raid_type *raid_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	sector_t array_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	sector_t dev_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	/* Optional raid4/5/6 journal device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	struct journal_dev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 		struct dm_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 		struct md_rdev rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 		int mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	} journal_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	struct raid_dev dev[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) static void rs_config_backup(struct raid_set *rs, struct rs_layout *l)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	struct mddev *mddev = &rs->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	l->new_level = mddev->new_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	l->new_layout = mddev->new_layout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	l->new_chunk_sectors = mddev->new_chunk_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) static void rs_config_restore(struct raid_set *rs, struct rs_layout *l)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	struct mddev *mddev = &rs->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	mddev->new_level = l->new_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	mddev->new_layout = l->new_layout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	mddev->new_chunk_sectors = l->new_chunk_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) /* raid10 algorithms (i.e. formats) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) #define	ALGORITHM_RAID10_DEFAULT	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) #define	ALGORITHM_RAID10_NEAR		1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) #define	ALGORITHM_RAID10_OFFSET		2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) #define	ALGORITHM_RAID10_FAR		3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) /* Supported raid types and properties. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) static struct raid_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	const char *name;		/* RAID algorithm. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	const char *descr;		/* Descriptor text for logging. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	const unsigned int parity_devs;	/* # of parity devices. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	const unsigned int minimal_devs;/* minimal # of devices in set. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	const unsigned int level;	/* RAID level. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	const unsigned int algorithm;	/* RAID algorithm. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) } raid_types[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	{"raid0",	  "raid0 (striping)",			    0, 2, 0,  0 /* NONE */},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	{"raid1",	  "raid1 (mirroring)",			    0, 2, 1,  0 /* NONE */},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	{"raid10_far",	  "raid10 far (striped mirrors)",	    0, 2, 10, ALGORITHM_RAID10_FAR},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	{"raid10_offset", "raid10 offset (striped mirrors)",	    0, 2, 10, ALGORITHM_RAID10_OFFSET},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	{"raid10_near",	  "raid10 near (striped mirrors)",	    0, 2, 10, ALGORITHM_RAID10_NEAR},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	{"raid10",	  "raid10 (striped mirrors)",		    0, 2, 10, ALGORITHM_RAID10_DEFAULT},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	{"raid4",	  "raid4 (dedicated first parity disk)",    1, 2, 5,  ALGORITHM_PARITY_0}, /* raid4 layout = raid5_0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	{"raid5_n",	  "raid5 (dedicated last parity disk)",	    1, 2, 5,  ALGORITHM_PARITY_N},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	{"raid5_ls",	  "raid5 (left symmetric)",		    1, 2, 5,  ALGORITHM_LEFT_SYMMETRIC},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	{"raid5_rs",	  "raid5 (right symmetric)",		    1, 2, 5,  ALGORITHM_RIGHT_SYMMETRIC},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	{"raid5_la",	  "raid5 (left asymmetric)",		    1, 2, 5,  ALGORITHM_LEFT_ASYMMETRIC},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	{"raid5_ra",	  "raid5 (right asymmetric)",		    1, 2, 5,  ALGORITHM_RIGHT_ASYMMETRIC},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	{"raid6_zr",	  "raid6 (zero restart)",		    2, 4, 6,  ALGORITHM_ROTATING_ZERO_RESTART},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	{"raid6_nr",	  "raid6 (N restart)",			    2, 4, 6,  ALGORITHM_ROTATING_N_RESTART},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	{"raid6_nc",	  "raid6 (N continue)",			    2, 4, 6,  ALGORITHM_ROTATING_N_CONTINUE},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	{"raid6_n_6",	  "raid6 (dedicated parity/Q n/6)",	    2, 4, 6,  ALGORITHM_PARITY_N_6},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	{"raid6_ls_6",	  "raid6 (left symmetric dedicated Q 6)",   2, 4, 6,  ALGORITHM_LEFT_SYMMETRIC_6},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	{"raid6_rs_6",	  "raid6 (right symmetric dedicated Q 6)",  2, 4, 6,  ALGORITHM_RIGHT_SYMMETRIC_6},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	{"raid6_la_6",	  "raid6 (left asymmetric dedicated Q 6)",  2, 4, 6,  ALGORITHM_LEFT_ASYMMETRIC_6},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	{"raid6_ra_6",	  "raid6 (right asymmetric dedicated Q 6)", 2, 4, 6,  ALGORITHM_RIGHT_ASYMMETRIC_6}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) /* True, if @v is in inclusive range [@min, @max] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) static bool __within_range(long v, long min, long max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	return v >= min && v <= max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) /* All table line arguments are defined here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) static struct arg_name_flag {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	const unsigned long flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) } __arg_name_flags[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	{ CTR_FLAG_SYNC, "sync"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	{ CTR_FLAG_NOSYNC, "nosync"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	{ CTR_FLAG_REBUILD, "rebuild"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	{ CTR_FLAG_DAEMON_SLEEP, "daemon_sleep"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	{ CTR_FLAG_MIN_RECOVERY_RATE, "min_recovery_rate"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	{ CTR_FLAG_MAX_RECOVERY_RATE, "max_recovery_rate"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	{ CTR_FLAG_MAX_WRITE_BEHIND, "max_write_behind"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	{ CTR_FLAG_WRITE_MOSTLY, "write_mostly"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	{ CTR_FLAG_STRIPE_CACHE, "stripe_cache"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	{ CTR_FLAG_REGION_SIZE, "region_size"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	{ CTR_FLAG_RAID10_COPIES, "raid10_copies"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	{ CTR_FLAG_RAID10_FORMAT, "raid10_format"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	{ CTR_FLAG_DATA_OFFSET, "data_offset"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	{ CTR_FLAG_DELTA_DISKS, "delta_disks"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	{ CTR_FLAG_RAID10_USE_NEAR_SETS, "raid10_use_near_sets"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	{ CTR_FLAG_JOURNAL_DEV, "journal_dev" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	{ CTR_FLAG_JOURNAL_MODE, "journal_mode" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) /* Return argument name string for given @flag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) static const char *dm_raid_arg_name_by_flag(const uint32_t flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	if (hweight32(flag) == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 		struct arg_name_flag *anf = __arg_name_flags + ARRAY_SIZE(__arg_name_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 		while (anf-- > __arg_name_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 			if (flag & anf->flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 				return anf->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 		DMERR("%s called with more than one flag!", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) /* Define correlation of raid456 journal cache modes and dm-raid target line parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) static struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	const int mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	const char *param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) } _raid456_journal_mode[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	{ R5C_JOURNAL_MODE_WRITE_THROUGH , "writethrough" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	{ R5C_JOURNAL_MODE_WRITE_BACK    , "writeback" }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) /* Return MD raid4/5/6 journal mode for dm @journal_mode one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) static int dm_raid_journal_mode_to_md(const char *mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	int m = ARRAY_SIZE(_raid456_journal_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	while (m--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 		if (!strcasecmp(mode, _raid456_journal_mode[m].param))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 			return _raid456_journal_mode[m].mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) /* Return dm-raid raid4/5/6 journal mode string for @mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) static const char *md_journal_mode_to_dm_raid(const int mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	int m = ARRAY_SIZE(_raid456_journal_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	while (m--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 		if (mode == _raid456_journal_mode[m].mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 			return _raid456_journal_mode[m].param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	return "unknown";
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394)  * Bool helpers to test for various raid levels of a raid set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395)  * It's level as reported by the superblock rather than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396)  * the requested raid_type passed to the constructor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) /* Return true, if raid set in @rs is raid0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) static bool rs_is_raid0(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	return !rs->md.level;
^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) /* Return true, if raid set in @rs is raid1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) static bool rs_is_raid1(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	return rs->md.level == 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) /* Return true, if raid set in @rs is raid10 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) static bool rs_is_raid10(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	return rs->md.level == 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) /* Return true, if raid set in @rs is level 6 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) static bool rs_is_raid6(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	return rs->md.level == 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) /* Return true, if raid set in @rs is level 4, 5 or 6 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) static bool rs_is_raid456(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	return __within_range(rs->md.level, 4, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) /* Return true, if raid set in @rs is reshapable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) static bool __is_raid10_far(int layout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) static bool rs_is_reshapable(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	return rs_is_raid456(rs) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	       (rs_is_raid10(rs) && !__is_raid10_far(rs->md.new_layout));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) /* Return true, if raid set in @rs is recovering */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) static bool rs_is_recovering(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	return rs->md.recovery_cp < rs->md.dev_sectors;
^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) /* Return true, if raid set in @rs is reshaping */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) static bool rs_is_reshaping(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	return rs->md.reshape_position != MaxSector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449)  * bool helpers to test for various raid levels of a raid type @rt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) /* Return true, if raid type in @rt is raid0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) static bool rt_is_raid0(struct raid_type *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	return !rt->level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) /* Return true, if raid type in @rt is raid1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) static bool rt_is_raid1(struct raid_type *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	return rt->level == 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) /* Return true, if raid type in @rt is raid10 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) static bool rt_is_raid10(struct raid_type *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	return rt->level == 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) /* Return true, if raid type in @rt is raid4/5 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) static bool rt_is_raid45(struct raid_type *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	return __within_range(rt->level, 4, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) /* Return true, if raid type in @rt is raid6 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) static bool rt_is_raid6(struct raid_type *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	return rt->level == 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) /* Return true, if raid type in @rt is raid4/5/6 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) static bool rt_is_raid456(struct raid_type *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	return __within_range(rt->level, 4, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) /* END: raid level bools */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) /* Return valid ctr flags for the raid level of @rs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) static unsigned long __valid_flags(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	if (rt_is_raid0(rs->raid_type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 		return RAID0_VALID_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	else if (rt_is_raid1(rs->raid_type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 		return RAID1_VALID_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	else if (rt_is_raid10(rs->raid_type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 		return RAID10_VALID_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	else if (rt_is_raid45(rs->raid_type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 		return RAID45_VALID_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	else if (rt_is_raid6(rs->raid_type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		return RAID6_VALID_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507)  * Check for valid flags set on @rs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509)  * Has to be called after parsing of the ctr flags!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) static int rs_check_for_valid_flags(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	if (rs->ctr_flags & ~__valid_flags(rs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 		rs->ti->error = "Invalid flags combination";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) /* MD raid10 bit definitions and helpers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) #define RAID10_OFFSET			(1 << 16) /* stripes with data copies area adjacent on devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) #define RAID10_BROCKEN_USE_FAR_SETS	(1 << 17) /* Broken in raid10.c: use sets instead of whole stripe rotation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) #define RAID10_USE_FAR_SETS		(1 << 18) /* Use sets instead of whole stripe rotation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) #define RAID10_FAR_COPIES_SHIFT		8	  /* raid10 # far copies shift (2nd byte of layout) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) /* Return md raid10 near copies for @layout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) static unsigned int __raid10_near_copies(int layout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	return layout & 0xFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) /* Return md raid10 far copies for @layout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) static unsigned int __raid10_far_copies(int layout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	return __raid10_near_copies(layout >> RAID10_FAR_COPIES_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) /* Return true if md raid10 offset for @layout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) static bool __is_raid10_offset(int layout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	return !!(layout & RAID10_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) /* Return true if md raid10 near for @layout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) static bool __is_raid10_near(int layout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	return !__is_raid10_offset(layout) && __raid10_near_copies(layout) > 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) /* Return true if md raid10 far for @layout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) static bool __is_raid10_far(int layout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	return !__is_raid10_offset(layout) && __raid10_far_copies(layout) > 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) /* Return md raid10 layout string for @layout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) static const char *raid10_md_layout_to_format(int layout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	 * Bit 16 stands for "offset"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	 * (i.e. adjacent stripes hold copies)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	 * Refer to MD's raid10.c for details
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	if (__is_raid10_offset(layout))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 		return "offset";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	if (__raid10_near_copies(layout) > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 		return "near";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	if (__raid10_far_copies(layout) > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		return "far";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	return "unknown";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) /* Return md raid10 algorithm for @name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) static int raid10_name_to_format(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	if (!strcasecmp(name, "near"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 		return ALGORITHM_RAID10_NEAR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	else if (!strcasecmp(name, "offset"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 		return ALGORITHM_RAID10_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	else if (!strcasecmp(name, "far"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 		return ALGORITHM_RAID10_FAR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) /* Return md raid10 copies for @layout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) static unsigned int raid10_md_layout_to_copies(int layout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	return max(__raid10_near_copies(layout), __raid10_far_copies(layout));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) /* Return md raid10 format id for @format string */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) static int raid10_format_to_md_layout(struct raid_set *rs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 				      unsigned int algorithm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 				      unsigned int copies)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	unsigned int n = 1, f = 1, r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	 * MD resilienece flaw:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	 * enabling use_far_sets for far/offset formats causes copies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	 * to be colocated on the same devs together with their origins!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	 * -> disable it for now in the definition above
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	if (algorithm == ALGORITHM_RAID10_DEFAULT ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	    algorithm == ALGORITHM_RAID10_NEAR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		n = copies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	else if (algorithm == ALGORITHM_RAID10_OFFSET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 		f = copies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 		r = RAID10_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		if (!test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 			r |= RAID10_USE_FAR_SETS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	} else if (algorithm == ALGORITHM_RAID10_FAR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 		f = copies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		if (!test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 			r |= RAID10_USE_FAR_SETS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	return r | (f << RAID10_FAR_COPIES_SHIFT) | n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) /* END: MD raid10 bit definitions and helpers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) /* Check for any of the raid10 algorithms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) static bool __got_raid10(struct raid_type *rtp, const int layout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	if (rtp->level == 10) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		switch (rtp->algorithm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 		case ALGORITHM_RAID10_DEFAULT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 		case ALGORITHM_RAID10_NEAR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 			return __is_raid10_near(layout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		case ALGORITHM_RAID10_OFFSET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 			return __is_raid10_offset(layout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 		case ALGORITHM_RAID10_FAR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 			return __is_raid10_far(layout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) /* Return raid_type for @name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) static struct raid_type *get_raid_type(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	while (rtp-- > raid_types)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		if (!strcasecmp(rtp->name, name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 			return rtp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) /* Return raid_type for @name based derived from @level and @layout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) static struct raid_type *get_raid_type_by_ll(const int level, const int layout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	while (rtp-- > raid_types) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 		/* RAID10 special checks based on @layout flags/properties */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 		if (rtp->level == level &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 		    (__got_raid10(rtp, layout) || rtp->algorithm == layout))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 			return rtp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) /* Adjust rdev sectors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) static void rs_set_rdev_sectors(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	struct mddev *mddev = &rs->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	struct md_rdev *rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	 * raid10 sets rdev->sector to the device size, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	 * is unintended in case of out-of-place reshaping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	rdev_for_each(rdev, mddev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 		if (!test_bit(Journal, &rdev->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 			rdev->sectors = mddev->dev_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697)  * Change bdev capacity of @rs in case of a disk add/remove reshape
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) static void rs_set_capacity(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	struct gendisk *gendisk = dm_disk(dm_table_get_md(rs->ti->table));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	set_capacity(gendisk, rs->md.array_sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	revalidate_disk_size(gendisk, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708)  * Set the mddev properties in @rs to the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709)  * ones retrieved from the freshest superblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) static void rs_set_cur(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	struct mddev *mddev = &rs->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	mddev->new_level = mddev->level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	mddev->new_layout = mddev->layout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	mddev->new_chunk_sectors = mddev->chunk_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721)  * Set the mddev properties in @rs to the new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722)  * ones requested by the ctr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) static void rs_set_new(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	struct mddev *mddev = &rs->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	mddev->level = mddev->new_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	mddev->layout = mddev->new_layout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	mddev->chunk_sectors = mddev->new_chunk_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	mddev->raid_disks = rs->raid_disks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	mddev->delta_disks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) static struct raid_set *raid_set_alloc(struct dm_target *ti, struct raid_type *raid_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 				       unsigned int raid_devs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	struct raid_set *rs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	if (raid_devs <= raid_type->parity_devs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		ti->error = "Insufficient number of devices";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	rs = kzalloc(struct_size(rs, dev, raid_devs), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	if (!rs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		ti->error = "Cannot allocate raid context";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	mddev_init(&rs->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	rs->raid_disks = raid_devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	rs->delta_disks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	rs->ti = ti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	rs->raid_type = raid_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	rs->stripe_cache_entries = 256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	rs->md.raid_disks = raid_devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	rs->md.level = raid_type->level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	rs->md.new_level = rs->md.level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	rs->md.layout = raid_type->algorithm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	rs->md.new_layout = rs->md.layout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	rs->md.delta_disks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	rs->md.recovery_cp = MaxSector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	for (i = 0; i < raid_devs; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		md_rdev_init(&rs->dev[i].rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	 * Remaining items to be initialized by further RAID params:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	 *  rs->md.persistent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	 *  rs->md.external
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	 *  rs->md.chunk_sectors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	 *  rs->md.new_chunk_sectors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	 *  rs->md.dev_sectors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	return rs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) /* Free all @rs allocations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) static void raid_set_free(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	if (rs->journal_dev.dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		md_rdev_clear(&rs->journal_dev.rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		dm_put_device(rs->ti, rs->journal_dev.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	for (i = 0; i < rs->raid_disks; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 		if (rs->dev[i].meta_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 			dm_put_device(rs->ti, rs->dev[i].meta_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 		md_rdev_clear(&rs->dev[i].rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 		if (rs->dev[i].data_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 			dm_put_device(rs->ti, rs->dev[i].data_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	kfree(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805)  * For every device we have two words
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806)  *  <meta_dev>: meta device name or '-' if missing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807)  *  <data_dev>: data device name or '-' if missing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809)  * The following are permitted:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810)  *    - -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811)  *    - <data_dev>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812)  *    <meta_dev> <data_dev>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814)  * The following is not allowed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815)  *    <meta_dev> -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817)  * This code parses those words.  If there is a failure,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818)  * the caller must use raid_set_free() to unwind the operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) static int parse_dev_params(struct raid_set *rs, struct dm_arg_set *as)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	int rebuild = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	int metadata_available = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	int r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	const char *arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	/* Put off the number of raid devices argument to get to dev pairs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	arg = dm_shift_arg(as);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	if (!arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	for (i = 0; i < rs->raid_disks; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 		rs->dev[i].rdev.raid_disk = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		rs->dev[i].meta_dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		rs->dev[i].data_dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 		 * There are no offsets initially.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		 * Out of place reshape will set them accordingly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		rs->dev[i].rdev.data_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		rs->dev[i].rdev.new_data_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 		rs->dev[i].rdev.mddev = &rs->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		arg = dm_shift_arg(as);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 		if (!arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 		if (strcmp(arg, "-")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 			r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 					  &rs->dev[i].meta_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 			if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 				rs->ti->error = "RAID metadata device lookup failure";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 				return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 			rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 			if (!rs->dev[i].rdev.sb_page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 				rs->ti->error = "Failed to allocate superblock page";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 				return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 		arg = dm_shift_arg(as);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		if (!arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 		if (!strcmp(arg, "-")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 			if (!test_bit(In_sync, &rs->dev[i].rdev.flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 			    (!rs->dev[i].rdev.recovery_offset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 				rs->ti->error = "Drive designated for rebuild not specified";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 			if (rs->dev[i].meta_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 				rs->ti->error = "No data device supplied with metadata device";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 		r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 				  &rs->dev[i].data_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 		if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 			rs->ti->error = "RAID device lookup failure";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		if (rs->dev[i].meta_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 			metadata_available = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 			rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 		rs->dev[i].rdev.bdev = rs->dev[i].data_dev->bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 		list_add_tail(&rs->dev[i].rdev.same_set, &rs->md.disks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 		if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 			rebuild++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	if (rs->journal_dev.dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 		list_add_tail(&rs->journal_dev.rdev.same_set, &rs->md.disks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	if (metadata_available) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 		rs->md.external = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 		rs->md.persistent = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		rs->md.major_version = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	} else if (rebuild && !rs->md.recovery_cp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 		 * Without metadata, we will not be able to tell if the array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 		 * is in-sync or not - we must assume it is not.  Therefore,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 		 * it is impossible to rebuild a drive.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		 * Even if there is metadata, the on-disk information may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		 * indicate that the array is not in-sync and it will then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		 * fail at that time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 		 * User could specify 'nosync' option if desperate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 		rs->ti->error = "Unable to rebuild drive while array is not in-sync";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929)  * validate_region_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930)  * @rs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931)  * @region_size:  region size in sectors.  If 0, pick a size (4MiB default).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933)  * Set rs->md.bitmap_info.chunksize (which really refers to 'region size').
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934)  * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936)  * Returns: 0 on success, -EINVAL on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) static int validate_region_size(struct raid_set *rs, unsigned long region_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	unsigned long min_region_size = rs->ti->len / (1 << 21);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	if (rs_is_raid0(rs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	if (!region_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 		 * Choose a reasonable default.	 All figures in sectors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 		if (min_region_size > (1 << 13)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 			/* If not a power of 2, make it the next power of 2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 			region_size = roundup_pow_of_two(min_region_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 			DMINFO("Choosing default region size of %lu sectors",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 			       region_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 			DMINFO("Choosing default region size of 4MiB");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 			region_size = 1 << 13; /* sectors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 		 * Validate user-supplied value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 		if (region_size > rs->ti->len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 			rs->ti->error = "Supplied region size is too large";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 		if (region_size < min_region_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 			DMERR("Supplied region_size (%lu sectors) below minimum (%lu)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 			      region_size, min_region_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 			rs->ti->error = "Supplied region size is too small";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 		if (!is_power_of_2(region_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 			rs->ti->error = "Region size is not a power of 2";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 		if (region_size < rs->md.chunk_sectors) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 			rs->ti->error = "Region size is smaller than the chunk size";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 		}
^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) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	 * Convert sectors to bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	rs->md.bitmap_info.chunksize = to_bytes(region_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994)  * validate_raid_redundancy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995)  * @rs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997)  * Determine if there are enough devices in the array that haven't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998)  * failed (or are being rebuilt) to form a usable array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000)  * Returns: 0 on success, -EINVAL on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) static int validate_raid_redundancy(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	unsigned int i, rebuild_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	unsigned int rebuilds_per_group = 0, copies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	unsigned int group_size, last_group_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	for (i = 0; i < rs->md.raid_disks; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 		if (!test_bit(In_sync, &rs->dev[i].rdev.flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 		    !rs->dev[i].rdev.sb_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 			rebuild_cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	switch (rs->md.level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 		if (rebuild_cnt >= rs->md.raid_disks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 			goto too_many;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	case 4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	case 5:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	case 6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 		if (rebuild_cnt > rs->raid_type->parity_devs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 			goto too_many;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	case 10:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 		copies = raid10_md_layout_to_copies(rs->md.new_layout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 		if (copies < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 			DMERR("Bogus raid10 data copies < 2!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		if (rebuild_cnt < copies)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 		 * It is possible to have a higher rebuild count for RAID10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 		 * as long as the failed devices occur in different mirror
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 		 * groups (i.e. different stripes).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 		 * When checking "near" format, make sure no adjacent devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 		 * have failed beyond what can be handled.  In addition to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		 * simple case where the number of devices is a multiple of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 		 * number of copies, we must also handle cases where the number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 		 * of devices is not a multiple of the number of copies.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 		 * E.g.	   dev1 dev2 dev3 dev4 dev5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 		 *	    A	 A    B	   B	C
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 		 *	    C	 D    D	   E	E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 		if (__is_raid10_near(rs->md.new_layout)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 			for (i = 0; i < rs->md.raid_disks; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 				if (!(i % copies))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 					rebuilds_per_group = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 				if ((!rs->dev[i].rdev.sb_page ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 				    !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 				    (++rebuilds_per_group >= copies))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 					goto too_many;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 		}
^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) 		 * When checking "far" and "offset" formats, we need to ensure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 		 * that the device that holds its copy is not also dead or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 		 * being rebuilt.  (Note that "far" and "offset" formats only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 		 * support two copies right now.  These formats also only ever
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 		 * use the 'use_far_sets' variant.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 		 * This check is somewhat complicated by the need to account
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 		 * for arrays that are not a multiple of (far) copies.	This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 		 * results in the need to treat the last (potentially larger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 		 * set differently.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 		group_size = (rs->md.raid_disks / copies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 		last_group_start = (rs->md.raid_disks / group_size) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 		last_group_start *= group_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 		for (i = 0; i < rs->md.raid_disks; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 			if (!(i % copies) && !(i > last_group_start))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 				rebuilds_per_group = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 			if ((!rs->dev[i].rdev.sb_page ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 			     !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 			    (++rebuilds_per_group >= copies))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 					goto too_many;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 		if (rebuild_cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) too_many:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098)  * Possible arguments are...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099)  *	<chunk_size> [optional_args]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101)  * Argument definitions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102)  *    <chunk_size>			The number of sectors per disk that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103)  *					will form the "stripe"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104)  *    [[no]sync]			Force or prevent recovery of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105)  *					entire array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106)  *    [rebuild <idx>]			Rebuild the drive indicated by the index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107)  *    [daemon_sleep <ms>]		Time between bitmap daemon work to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108)  *					clear bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109)  *    [min_recovery_rate <kB/sec/disk>]	Throttle RAID initialization
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110)  *    [max_recovery_rate <kB/sec/disk>]	Throttle RAID initialization
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111)  *    [write_mostly <idx>]		Indicate a write mostly drive via index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112)  *    [max_write_behind <sectors>]	See '-write-behind=' (man mdadm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113)  *    [stripe_cache <sectors>]		Stripe cache size for higher RAIDs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114)  *    [region_size <sectors>]		Defines granularity of bitmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115)  *    [journal_dev <dev>]		raid4/5/6 journaling deviice
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116)  *    					(i.e. write hole closing log)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118)  * RAID10-only options:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119)  *    [raid10_copies <# copies>]	Number of copies.  (Default: 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120)  *    [raid10_format <near|far|offset>] Layout algorithm.  (Default: near)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) static int parse_raid_params(struct raid_set *rs, struct dm_arg_set *as,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 			     unsigned int num_raid_params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	int value, raid10_format = ALGORITHM_RAID10_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	unsigned int raid10_copies = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	unsigned int i, write_mostly = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	unsigned int region_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	sector_t max_io_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	const char *arg, *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	struct raid_dev *rd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	struct raid_type *rt = rs->raid_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	arg = dm_shift_arg(as);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	num_raid_params--; /* Account for chunk_size argument */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	if (kstrtoint(arg, 10, &value) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 		rs->ti->error = "Bad numerical argument given for chunk_size";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	 * First, parse the in-order required arguments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	 * "chunk_size" is the only argument of this type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	if (rt_is_raid1(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 		if (value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 			DMERR("Ignoring chunk size parameter for RAID 1");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 		value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	} else if (!is_power_of_2(value)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 		rs->ti->error = "Chunk size must be a power of 2";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	} else if (value < 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 		rs->ti->error = "Chunk size value is too small";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	rs->md.new_chunk_sectors = rs->md.chunk_sectors = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	 * We set each individual device as In_sync with a completed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	 * 'recovery_offset'.  If there has been a device failure or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	 * replacement then one of the following cases applies:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	 *   1) User specifies 'rebuild'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	 *	- Device is reset when param is read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	 *   2) A new device is supplied.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	 *	- No matching superblock found, resets device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	 *   3) Device failure was transient and returns on reload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	 *	- Failure noticed, resets device for bitmap replay.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	 *   4) Device hadn't completed recovery after previous failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	 *	- Superblock is read and overrides recovery_offset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	 * What is found in the superblocks of the devices is always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	 * authoritative, unless 'rebuild' or '[no]sync' was specified.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	for (i = 0; i < rs->raid_disks; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 		set_bit(In_sync, &rs->dev[i].rdev.flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 		rs->dev[i].rdev.recovery_offset = MaxSector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	 * Second, parse the unordered optional arguments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	for (i = 0; i < num_raid_params; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 		key = dm_shift_arg(as);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 		if (!key) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 			rs->ti->error = "Not enough raid parameters given";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 		if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_NOSYNC))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 			if (test_and_set_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 				rs->ti->error = "Only one 'nosync' argument allowed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 		if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_SYNC))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 			if (test_and_set_bit(__CTR_FLAG_SYNC, &rs->ctr_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 				rs->ti->error = "Only one 'sync' argument allowed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 		if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_USE_NEAR_SETS))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 			if (test_and_set_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 				rs->ti->error = "Only one 'raid10_use_new_sets' argument allowed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 		arg = dm_shift_arg(as);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 		i++; /* Account for the argument pairs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 		if (!arg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 			rs->ti->error = "Wrong number of raid parameters given";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 		 * Parameters that take a string value are checked here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 		/* "raid10_format {near|offset|far} */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 		if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_FORMAT))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 			if (test_and_set_bit(__CTR_FLAG_RAID10_FORMAT, &rs->ctr_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 				rs->ti->error = "Only one 'raid10_format' argument pair allowed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 			if (!rt_is_raid10(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 				rs->ti->error = "'raid10_format' is an invalid parameter for this RAID type";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 			raid10_format = raid10_name_to_format(arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 			if (raid10_format < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 				rs->ti->error = "Invalid 'raid10_format' value given";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 				return raid10_format;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 		/* "journal_dev <dev>" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 		if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_JOURNAL_DEV))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 			int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 			struct md_rdev *jdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 			if (test_and_set_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 				rs->ti->error = "Only one raid4/5/6 set journaling device allowed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 			if (!rt_is_raid456(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 				rs->ti->error = "'journal_dev' is an invalid parameter for this RAID type";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 			r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 					  &rs->journal_dev.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 			if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 				rs->ti->error = "raid4/5/6 journal device lookup failure";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 				return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 			jdev = &rs->journal_dev.rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 			md_rdev_init(jdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 			jdev->mddev = &rs->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 			jdev->bdev = rs->journal_dev.dev->bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 			jdev->sectors = to_sector(i_size_read(jdev->bdev->bd_inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 			if (jdev->sectors < MIN_RAID456_JOURNAL_SPACE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 				rs->ti->error = "No space for raid4/5/6 journal";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 				return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 			rs->journal_dev.mode = R5C_JOURNAL_MODE_WRITE_THROUGH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 			set_bit(Journal, &jdev->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 		/* "journal_mode <mode>" ("journal_dev" mandatory!) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 		if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_JOURNAL_MODE))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 			int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 			if (!test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 				rs->ti->error = "raid4/5/6 'journal_mode' is invalid without 'journal_dev'";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 			if (test_and_set_bit(__CTR_FLAG_JOURNAL_MODE, &rs->ctr_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 				rs->ti->error = "Only one raid4/5/6 'journal_mode' argument allowed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 			r = dm_raid_journal_mode_to_md(arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 			if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 				rs->ti->error = "Invalid 'journal_mode' argument";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 				return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 			rs->journal_dev.mode = r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 		 * Parameters with number values from here on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 		if (kstrtoint(arg, 10, &value) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 			rs->ti->error = "Bad numerical argument given in raid params";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 		if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_REBUILD))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 			 * "rebuild" is being passed in by userspace to provide
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 			 * indexes of replaced devices and to set up additional
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 			 * devices on raid level takeover.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 			if (!__within_range(value, 0, rs->raid_disks - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 				rs->ti->error = "Invalid rebuild index given";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 			if (test_and_set_bit(value, (void *) rs->rebuild_disks)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 				rs->ti->error = "rebuild for this index already given";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 			rd = rs->dev + value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 			clear_bit(In_sync, &rd->rdev.flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 			clear_bit(Faulty, &rd->rdev.flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 			rd->rdev.recovery_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 			set_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 		} else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_WRITE_MOSTLY))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 			if (!rt_is_raid1(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 				rs->ti->error = "write_mostly option is only valid for RAID1";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 			if (!__within_range(value, 0, rs->md.raid_disks - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 				rs->ti->error = "Invalid write_mostly index given";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 			write_mostly++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 			set_bit(WriteMostly, &rs->dev[value].rdev.flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 			set_bit(__CTR_FLAG_WRITE_MOSTLY, &rs->ctr_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 		} else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MAX_WRITE_BEHIND))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 			if (!rt_is_raid1(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 				rs->ti->error = "max_write_behind option is only valid for RAID1";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 			if (test_and_set_bit(__CTR_FLAG_MAX_WRITE_BEHIND, &rs->ctr_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 				rs->ti->error = "Only one max_write_behind argument pair allowed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 			 * In device-mapper, we specify things in sectors, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 			 * MD records this value in kB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 			if (value < 0 || value / 2 > COUNTER_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 				rs->ti->error = "Max write-behind limit out of range";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 			rs->md.bitmap_info.max_write_behind = value / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 		} else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DAEMON_SLEEP))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 			if (test_and_set_bit(__CTR_FLAG_DAEMON_SLEEP, &rs->ctr_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 				rs->ti->error = "Only one daemon_sleep argument pair allowed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 			if (value < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 				rs->ti->error = "daemon sleep period out of range";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 			rs->md.bitmap_info.daemon_sleep = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 		} else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DATA_OFFSET))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 			/* Userspace passes new data_offset after having extended the the data image LV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 			if (test_and_set_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 				rs->ti->error = "Only one data_offset argument pair allowed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 			/* Ensure sensible data offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 			if (value < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 			    (value && (value < MIN_FREE_RESHAPE_SPACE || value % to_sector(PAGE_SIZE)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 				rs->ti->error = "Bogus data_offset value";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 			rs->data_offset = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 		} else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DELTA_DISKS))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 			/* Define the +/-# of disks to add to/remove from the given raid set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 			if (test_and_set_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 				rs->ti->error = "Only one delta_disks argument pair allowed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 			/* Ensure MAX_RAID_DEVICES and raid type minimal_devs! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 			if (!__within_range(abs(value), 1, MAX_RAID_DEVICES - rt->minimal_devs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 				rs->ti->error = "Too many delta_disk requested";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 			rs->delta_disks = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 		} else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_STRIPE_CACHE))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 			if (test_and_set_bit(__CTR_FLAG_STRIPE_CACHE, &rs->ctr_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 				rs->ti->error = "Only one stripe_cache argument pair allowed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 			if (!rt_is_raid456(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 				rs->ti->error = "Inappropriate argument: stripe_cache";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 			if (value < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 				rs->ti->error = "Bogus stripe cache entries value";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 			rs->stripe_cache_entries = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 		} else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MIN_RECOVERY_RATE))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 			if (test_and_set_bit(__CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 				rs->ti->error = "Only one min_recovery_rate argument pair allowed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 			if (value < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 				rs->ti->error = "min_recovery_rate out of range";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 			rs->md.sync_speed_min = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 		} else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MAX_RECOVERY_RATE))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 			if (test_and_set_bit(__CTR_FLAG_MAX_RECOVERY_RATE, &rs->ctr_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 				rs->ti->error = "Only one max_recovery_rate argument pair allowed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 			if (value < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 				rs->ti->error = "max_recovery_rate out of range";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 			rs->md.sync_speed_max = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 		} else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_REGION_SIZE))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 			if (test_and_set_bit(__CTR_FLAG_REGION_SIZE, &rs->ctr_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 				rs->ti->error = "Only one region_size argument pair allowed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 			region_size = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 			rs->requested_bitmap_chunk_sectors = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 		} else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_COPIES))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 			if (test_and_set_bit(__CTR_FLAG_RAID10_COPIES, &rs->ctr_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 				rs->ti->error = "Only one raid10_copies argument pair allowed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 			if (!__within_range(value, 2, rs->md.raid_disks)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 				rs->ti->error = "Bad value for 'raid10_copies'";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 			raid10_copies = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 			DMERR("Unable to parse RAID parameter: %s", key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 			rs->ti->error = "Unable to parse RAID parameter";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	    test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 		rs->ti->error = "sync and nosync are mutually exclusive";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	if (test_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 	    (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 	     test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 		rs->ti->error = "sync/nosync and rebuild are mutually exclusive";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 	if (write_mostly >= rs->md.raid_disks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 		rs->ti->error = "Can't set all raid1 devices to write_mostly";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 	if (rs->md.sync_speed_max &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	    rs->md.sync_speed_min > rs->md.sync_speed_max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 		rs->ti->error = "Bogus recovery rates";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	if (validate_region_size(rs, region_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	if (rs->md.chunk_sectors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 		max_io_len = rs->md.chunk_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 		max_io_len = region_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	if (dm_set_target_max_io_len(rs->ti, max_io_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	if (rt_is_raid10(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 		if (raid10_copies > rs->md.raid_disks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 			rs->ti->error = "Not enough devices to satisfy specification";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 		rs->md.new_layout = raid10_format_to_md_layout(rs, raid10_format, raid10_copies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 		if (rs->md.new_layout < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 			rs->ti->error = "Error getting raid10 format";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 			return rs->md.new_layout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 		rt = get_raid_type_by_ll(10, rs->md.new_layout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 		if (!rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 			rs->ti->error = "Failed to recognize new raid10 layout";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 		if ((rt->algorithm == ALGORITHM_RAID10_DEFAULT ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 		     rt->algorithm == ALGORITHM_RAID10_NEAR) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 		    test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 			rs->ti->error = "RAID10 format 'near' and 'raid10_use_near_sets' are incompatible";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	rs->raid10_copies = raid10_copies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	/* Assume there are no metadata devices until the drives are parsed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 	rs->md.persistent = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	rs->md.external = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	/* Check, if any invalid ctr arguments have been passed in for the raid level */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	return rs_check_for_valid_flags(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) /* Set raid4/5/6 cache size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) static int rs_set_raid456_stripe_cache(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	struct r5conf *conf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	struct mddev *mddev = &rs->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	uint32_t min_stripes = max(mddev->chunk_sectors, mddev->new_chunk_sectors) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	uint32_t nr_stripes = rs->stripe_cache_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 	if (!rt_is_raid456(rs->raid_type)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 		rs->ti->error = "Inappropriate raid level; cannot change stripe_cache size";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 	if (nr_stripes < min_stripes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 		DMINFO("Adjusting requested %u stripe cache entries to %u to suit stripe size",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 		       nr_stripes, min_stripes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 		nr_stripes = min_stripes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 	conf = mddev->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 	if (!conf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 		rs->ti->error = "Cannot change stripe_cache size on inactive RAID set";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 	/* Try setting number of stripes in raid456 stripe cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	if (conf->min_nr_stripes != nr_stripes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 		r = raid5_set_cache_size(mddev, nr_stripes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 		if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 			rs->ti->error = "Failed to set raid4/5/6 stripe cache size";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 		DMINFO("%u stripe cache entries", nr_stripes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) /* Return # of data stripes as kept in mddev as of @rs (i.e. as of superblock) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) static unsigned int mddev_data_stripes(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	return rs->md.raid_disks - rs->raid_type->parity_devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) /* Return # of data stripes of @rs (i.e. as of ctr) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) static unsigned int rs_data_stripes(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 	return rs->raid_disks - rs->raid_type->parity_devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585)  * Retrieve rdev->sectors from any valid raid device of @rs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586)  * to allow userpace to pass in arbitray "- -" device tupples.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) static sector_t __rdev_sectors(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 	for (i = 0; i < rs->md.raid_disks; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 		struct md_rdev *rdev = &rs->dev[i].rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 		if (!test_bit(Journal, &rdev->flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 		    rdev->bdev && rdev->sectors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 			return rdev->sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) /* Check that calculated dev_sectors fits all component devices. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) static int _check_data_dev_sectors(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 	sector_t ds = ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	struct md_rdev *rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 	rdev_for_each(rdev, &rs->md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 		if (!test_bit(Journal, &rdev->flags) && rdev->bdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 			ds = min(ds, to_sector(i_size_read(rdev->bdev->bd_inode)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 			if (ds < rs->md.dev_sectors) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 				rs->ti->error = "Component device(s) too small";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) /* Calculate the sectors per device and per array used for @rs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) static int rs_set_dev_and_array_sectors(struct raid_set *rs, sector_t sectors, bool use_mddev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 	int delta_disks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 	unsigned int data_stripes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 	sector_t array_sectors = sectors, dev_sectors = sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 	struct mddev *mddev = &rs->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 	if (use_mddev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 		delta_disks = mddev->delta_disks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 		data_stripes = mddev_data_stripes(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 		delta_disks = rs->delta_disks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 		data_stripes = rs_data_stripes(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 	/* Special raid1 case w/o delta_disks support (yet) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 	if (rt_is_raid1(rs->raid_type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 		;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 	else if (rt_is_raid10(rs->raid_type)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 		if (rs->raid10_copies < 2 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 		    delta_disks < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 			rs->ti->error = "Bogus raid10 data copies or delta disks";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 		dev_sectors *= rs->raid10_copies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 		if (sector_div(dev_sectors, data_stripes))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 		array_sectors = (data_stripes + delta_disks) * dev_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 		if (sector_div(array_sectors, rs->raid10_copies))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 	} else if (sector_div(dev_sectors, data_stripes))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 		/* Striped layouts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 		array_sectors = (data_stripes + delta_disks) * dev_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 	mddev->array_sectors = array_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 	mddev->dev_sectors = dev_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 	rs_set_rdev_sectors(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 	return _check_data_dev_sectors(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 	rs->ti->error = "Target length not divisible by number of data devices";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) /* Setup recovery on @rs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) static void rs_setup_recovery(struct raid_set *rs, sector_t dev_sectors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 	/* raid0 does not recover */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 	if (rs_is_raid0(rs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 		rs->md.recovery_cp = MaxSector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 	 * A raid6 set has to be recovered either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 	 * completely or for the grown part to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 	 * ensure proper parity and Q-Syndrome
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 	else if (rs_is_raid6(rs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 		rs->md.recovery_cp = dev_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 	 * Other raid set types may skip recovery
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 	 * depending on the 'nosync' flag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 		rs->md.recovery_cp = test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 				     ? MaxSector : dev_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) static void do_table_event(struct work_struct *ws)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 	struct raid_set *rs = container_of(ws, struct raid_set, md.event_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 	smp_rmb(); /* Make sure we access most actual mddev properties */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	if (!rs_is_reshaping(rs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 		if (rs_is_raid10(rs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 			rs_set_rdev_sectors(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 		rs_set_capacity(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 	dm_table_event(rs->ti->table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708)  * Make sure a valid takover (level switch) is being requested on @rs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710)  * Conversions of raid sets from one MD personality to another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711)  * have to conform to restrictions which are enforced here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) static int rs_check_takeover(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 	struct mddev *mddev = &rs->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 	unsigned int near_copies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 	if (rs->md.degraded) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 		rs->ti->error = "Can't takeover degraded raid set";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 	if (rs_is_reshaping(rs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 		rs->ti->error = "Can't takeover reshaping raid set";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 	switch (mddev->level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 		/* raid0 -> raid1/5 with one disk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 		if ((mddev->new_level == 1 || mddev->new_level == 5) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 		    mddev->raid_disks == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 		/* raid0 -> raid10 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 		if (mddev->new_level == 10 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 		    !(rs->raid_disks % mddev->raid_disks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 		/* raid0 with multiple disks -> raid4/5/6 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 		if (__within_range(mddev->new_level, 4, 6) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 		    mddev->new_layout == ALGORITHM_PARITY_N &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 		    mddev->raid_disks > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 	case 10:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 		/* Can't takeover raid10_offset! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 		if (__is_raid10_offset(mddev->layout))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 		near_copies = __raid10_near_copies(mddev->layout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 		/* raid10* -> raid0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 		if (mddev->new_level == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 			/* Can takeover raid10_near with raid disks divisable by data copies! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 			if (near_copies > 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 			    !(mddev->raid_disks % near_copies)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 				mddev->raid_disks /= near_copies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 				mddev->delta_disks = mddev->raid_disks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 			/* Can takeover raid10_far */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 			if (near_copies == 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 			    __raid10_far_copies(mddev->layout) > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 		/* raid10_{near,far} -> raid1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 		if (mddev->new_level == 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 		    max(near_copies, __raid10_far_copies(mddev->layout)) == mddev->raid_disks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 		/* raid10_{near,far} with 2 disks -> raid4/5 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 		if (__within_range(mddev->new_level, 4, 5) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 		    mddev->raid_disks == 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 		/* raid1 with 2 disks -> raid4/5 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 		if (__within_range(mddev->new_level, 4, 5) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 		    mddev->raid_disks == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 			mddev->degraded = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 		/* raid1 -> raid0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 		if (mddev->new_level == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 		    mddev->raid_disks == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 		/* raid1 -> raid10 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 		if (mddev->new_level == 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 	case 4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 		/* raid4 -> raid0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 		if (mddev->new_level == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 		/* raid4 -> raid1/5 with 2 disks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 		if ((mddev->new_level == 1 || mddev->new_level == 5) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 		    mddev->raid_disks == 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 		/* raid4 -> raid5/6 with parity N */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 		if (__within_range(mddev->new_level, 5, 6) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 		    mddev->layout == ALGORITHM_PARITY_N)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 	case 5:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 		/* raid5 with parity N -> raid0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 		if (mddev->new_level == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 		    mddev->layout == ALGORITHM_PARITY_N)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 		/* raid5 with parity N -> raid4 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 		if (mddev->new_level == 4 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 		    mddev->layout == ALGORITHM_PARITY_N)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 		/* raid5 with 2 disks -> raid1/4/10 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 		if ((mddev->new_level == 1 || mddev->new_level == 4 || mddev->new_level == 10) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 		    mddev->raid_disks == 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 		/* raid5_* ->  raid6_*_6 with Q-Syndrome N (e.g. raid5_ra -> raid6_ra_6 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 		if (mddev->new_level == 6 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 		    ((mddev->layout == ALGORITHM_PARITY_N && mddev->new_layout == ALGORITHM_PARITY_N) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 		      __within_range(mddev->new_layout, ALGORITHM_LEFT_ASYMMETRIC_6, ALGORITHM_RIGHT_SYMMETRIC_6)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 	case 6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 		/* raid6 with parity N -> raid0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 		if (mddev->new_level == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 		    mddev->layout == ALGORITHM_PARITY_N)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 		/* raid6 with parity N -> raid4 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 		if (mddev->new_level == 4 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 		    mddev->layout == ALGORITHM_PARITY_N)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 		/* raid6_*_n with Q-Syndrome N -> raid5_* */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 		if (mddev->new_level == 5 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 		    ((mddev->layout == ALGORITHM_PARITY_N && mddev->new_layout == ALGORITHM_PARITY_N) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 		     __within_range(mddev->new_layout, ALGORITHM_LEFT_ASYMMETRIC, ALGORITHM_RIGHT_SYMMETRIC)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 	rs->ti->error = "takeover not possible";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) /* True if @rs requested to be taken over */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) static bool rs_takeover_requested(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 	return rs->md.new_level != rs->md.level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) /* True if layout is set to reshape. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) static bool rs_is_layout_change(struct raid_set *rs, bool use_mddev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 	return (use_mddev ? rs->md.delta_disks : rs->delta_disks) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 	       rs->md.new_layout != rs->md.layout ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 	       rs->md.new_chunk_sectors != rs->md.chunk_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) /* True if @rs is requested to reshape by ctr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) static bool rs_reshape_requested(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 	bool change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 	struct mddev *mddev = &rs->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 	if (rs_takeover_requested(rs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 	if (rs_is_raid0(rs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 	change = rs_is_layout_change(rs, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 	/* Historical case to support raid1 reshape without delta disks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 	if (rs_is_raid1(rs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 		if (rs->delta_disks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 			return !!rs->delta_disks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 		return !change &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 		       mddev->raid_disks != rs->raid_disks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 	if (rs_is_raid10(rs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 		return change &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 		       !__is_raid10_far(mddev->new_layout) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 		       rs->delta_disks >= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 	return change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) /*  Features */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) #define	FEATURE_FLAG_SUPPORTS_V190	0x1 /* Supports extended superblock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) /* State flags for sb->flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) #define	SB_FLAG_RESHAPE_ACTIVE		0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) #define	SB_FLAG_RESHAPE_BACKWARDS	0x2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919)  * This structure is never routinely used by userspace, unlike md superblocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920)  * Devices with this superblock should only ever be accessed via device-mapper.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) #define DM_RAID_MAGIC 0x64526D44
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) struct dm_raid_superblock {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 	__le32 magic;		/* "DmRd" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 	__le32 compat_features;	/* Used to indicate compatible features (like 1.9.0 ondisk metadata extension) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 	__le32 num_devices;	/* Number of devices in this raid set. (Max 64) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 	__le32 array_position;	/* The position of this drive in the raid set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 	__le64 events;		/* Incremented by md when superblock updated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 	__le64 failed_devices;	/* Pre 1.9.0 part of bit field of devices to */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 				/* indicate failures (see extension below) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 	 * This offset tracks the progress of the repair or replacement of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 	 * an individual drive.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 	__le64 disk_recovery_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 	 * This offset tracks the progress of the initial raid set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 	 * synchronisation/parity calculation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 	__le64 array_resync_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 	 * raid characteristics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 	__le32 level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 	__le32 layout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 	__le32 stripe_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 	/********************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 	 * BELOW FOLLOW V1.9.0 EXTENSIONS TO THE PRISTINE SUPERBLOCK FORMAT!!!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 	 * FEATURE_FLAG_SUPPORTS_V190 in the compat_features member indicates that those exist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 	__le32 flags; /* Flags defining array states for reshaping */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	 * This offset tracks the progress of a raid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 	 * set reshape in order to be able to restart it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 	__le64 reshape_position;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 	 * These define the properties of the array in case of an interrupted reshape
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 	__le32 new_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 	__le32 new_layout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 	__le32 new_stripe_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 	__le32 delta_disks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 	__le64 array_sectors; /* Array size in sectors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 	 * Sector offsets to data on devices (reshaping).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 	 * Needed to support out of place reshaping, thus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 	 * not writing over any stripes whilst converting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 	 * them from old to new layout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 	__le64 data_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 	__le64 new_data_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 	__le64 sectors; /* Used device size in sectors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 	 * Additonal Bit field of devices indicating failures to support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 	 * up to 256 devices with the 1.9.0 on-disk metadata format
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 	__le64 extended_failed_devices[DISKS_ARRAY_ELEMS - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 	__le32 incompat_features;	/* Used to indicate any incompatible features */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 	/* Always set rest up to logical block size to 0 when writing (see get_metadata_device() below). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000)  * Check for reshape constraints on raid set @rs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002)  * - reshape function non-existent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003)  * - degraded set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004)  * - ongoing recovery
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005)  * - ongoing reshape
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007)  * Returns 0 if none or -EPERM if given constraint
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008)  * and error message reference in @errmsg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) static int rs_check_reshape(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 	struct mddev *mddev = &rs->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 	if (!mddev->pers || !mddev->pers->check_reshape)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 		rs->ti->error = "Reshape not supported";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 	else if (mddev->degraded)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 		rs->ti->error = "Can't reshape degraded raid set";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 	else if (rs_is_recovering(rs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 		rs->ti->error = "Convert request on recovering raid set prohibited";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 	else if (rs_is_reshaping(rs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 		rs->ti->error = "raid set already reshaping!";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 	else if (!(rs_is_raid1(rs) || rs_is_raid10(rs) || rs_is_raid456(rs)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 		rs->ti->error = "Reshaping only supported for raid1/4/5/6/10";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 	return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) static int read_disk_sb(struct md_rdev *rdev, int size, bool force_reload)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 	BUG_ON(!rdev->sb_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 	if (rdev->sb_loaded && !force_reload)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 	rdev->sb_loaded = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 	if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 		DMERR("Failed to read superblock of device at position %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 		      rdev->raid_disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 		md_error(rdev->mddev, rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 		set_bit(Faulty, &rdev->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 	rdev->sb_loaded = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) static void sb_retrieve_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 	failed_devices[0] = le64_to_cpu(sb->failed_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 	memset(failed_devices + 1, 0, sizeof(sb->extended_failed_devices));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 	if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 		int i = ARRAY_SIZE(sb->extended_failed_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 		while (i--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 			failed_devices[i+1] = le64_to_cpu(sb->extended_failed_devices[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) static void sb_update_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 	int i = ARRAY_SIZE(sb->extended_failed_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 	sb->failed_devices = cpu_to_le64(failed_devices[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 	while (i--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 		sb->extended_failed_devices[i] = cpu_to_le64(failed_devices[i+1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075)  * Synchronize the superblock members with the raid set properties
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077)  * All superblock data is little endian.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) static void super_sync(struct mddev *mddev, struct md_rdev *rdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 	bool update_failed_devices = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 	uint64_t failed_devices[DISKS_ARRAY_ELEMS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 	struct dm_raid_superblock *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 	struct raid_set *rs = container_of(mddev, struct raid_set, md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 	/* No metadata device, no superblock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 	if (!rdev->meta_bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 	BUG_ON(!rdev->sb_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 	sb = page_address(rdev->sb_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 	sb_retrieve_failed_devices(sb, failed_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 	for (i = 0; i < rs->raid_disks; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 		if (!rs->dev[i].data_dev || test_bit(Faulty, &rs->dev[i].rdev.flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 			update_failed_devices = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 			set_bit(i, (void *) failed_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 	if (update_failed_devices)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 		sb_update_failed_devices(sb, failed_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 	sb->magic = cpu_to_le32(DM_RAID_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 	sb->compat_features = cpu_to_le32(FEATURE_FLAG_SUPPORTS_V190);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 	sb->num_devices = cpu_to_le32(mddev->raid_disks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 	sb->array_position = cpu_to_le32(rdev->raid_disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 	sb->events = cpu_to_le64(mddev->events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 	sb->disk_recovery_offset = cpu_to_le64(rdev->recovery_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 	sb->array_resync_offset = cpu_to_le64(mddev->recovery_cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 	sb->level = cpu_to_le32(mddev->level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 	sb->layout = cpu_to_le32(mddev->layout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 	sb->stripe_sectors = cpu_to_le32(mddev->chunk_sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 	/********************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 	 * BELOW FOLLOW V1.9.0 EXTENSIONS TO THE PRISTINE SUPERBLOCK FORMAT!!!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 	 * FEATURE_FLAG_SUPPORTS_V190 in the compat_features member indicates that those exist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 	sb->new_level = cpu_to_le32(mddev->new_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 	sb->new_layout = cpu_to_le32(mddev->new_layout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 	sb->new_stripe_sectors = cpu_to_le32(mddev->new_chunk_sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 	sb->delta_disks = cpu_to_le32(mddev->delta_disks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 	smp_rmb(); /* Make sure we access most recent reshape position */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 	sb->reshape_position = cpu_to_le64(mddev->reshape_position);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 	if (le64_to_cpu(sb->reshape_position) != MaxSector) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 		/* Flag ongoing reshape */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 		sb->flags |= cpu_to_le32(SB_FLAG_RESHAPE_ACTIVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 		if (mddev->delta_disks < 0 || mddev->reshape_backwards)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 			sb->flags |= cpu_to_le32(SB_FLAG_RESHAPE_BACKWARDS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 		/* Clear reshape flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 		sb->flags &= ~(cpu_to_le32(SB_FLAG_RESHAPE_ACTIVE|SB_FLAG_RESHAPE_BACKWARDS));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 	sb->array_sectors = cpu_to_le64(mddev->array_sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 	sb->data_offset = cpu_to_le64(rdev->data_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 	sb->new_data_offset = cpu_to_le64(rdev->new_data_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 	sb->sectors = cpu_to_le64(rdev->sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) 	sb->incompat_features = cpu_to_le32(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 	/* Zero out the rest of the payload after the size of the superblock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 	memset(sb + 1, 0, rdev->sb_size - sizeof(*sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156)  * super_load
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158)  * This function creates a superblock if one is not found on the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159)  * and will decide which superblock to use if there's a choice.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161)  * Return: 1 if use rdev, 0 if use refdev, -Exxx otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) static int super_load(struct md_rdev *rdev, struct md_rdev *refdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 	struct dm_raid_superblock *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 	struct dm_raid_superblock *refsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 	uint64_t events_sb, events_refsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 	r = read_disk_sb(rdev, rdev->sb_size, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 	sb = page_address(rdev->sb_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 	 * Two cases that we want to write new superblocks and rebuild:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 	 * 1) New device (no matching magic number)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 	 * 2) Device specified for rebuild (!In_sync w/ offset == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 	if ((sb->magic != cpu_to_le32(DM_RAID_MAGIC)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 	    (!test_bit(In_sync, &rdev->flags) && !rdev->recovery_offset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 		super_sync(rdev->mddev, rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 		set_bit(FirstUse, &rdev->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 		sb->compat_features = cpu_to_le32(FEATURE_FLAG_SUPPORTS_V190);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 		/* Force writing of superblocks to disk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 		set_bit(MD_SB_CHANGE_DEVS, &rdev->mddev->sb_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 		/* Any superblock is better than none, choose that if given */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 		return refdev ? 0 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 	if (!refdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 	events_sb = le64_to_cpu(sb->events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 	refsb = page_address(refdev->sb_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 	events_refsb = le64_to_cpu(refsb->events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 	return (events_sb > events_refsb) ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) static int super_init_validation(struct raid_set *rs, struct md_rdev *rdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 	int role;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 	unsigned int d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 	struct mddev *mddev = &rs->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 	uint64_t events_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 	uint64_t failed_devices[DISKS_ARRAY_ELEMS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) 	struct dm_raid_superblock *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 	uint32_t new_devs = 0, rebuild_and_new = 0, rebuilds = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 	struct md_rdev *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 	struct dm_raid_superblock *sb2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 	sb = page_address(rdev->sb_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 	events_sb = le64_to_cpu(sb->events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 	 * Initialise to 1 if this is a new superblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 	mddev->events = events_sb ? : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 	mddev->reshape_position = MaxSector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 	mddev->raid_disks = le32_to_cpu(sb->num_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 	mddev->level = le32_to_cpu(sb->level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 	mddev->layout = le32_to_cpu(sb->layout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 	mddev->chunk_sectors = le32_to_cpu(sb->stripe_sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 	 * Reshaping is supported, e.g. reshape_position is valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 	 * in superblock and superblock content is authoritative.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) 	if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 		/* Superblock is authoritative wrt given raid set layout! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 		mddev->new_level = le32_to_cpu(sb->new_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 		mddev->new_layout = le32_to_cpu(sb->new_layout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 		mddev->new_chunk_sectors = le32_to_cpu(sb->new_stripe_sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 		mddev->delta_disks = le32_to_cpu(sb->delta_disks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 		mddev->array_sectors = le64_to_cpu(sb->array_sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 		/* raid was reshaping and got interrupted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 		if (le32_to_cpu(sb->flags) & SB_FLAG_RESHAPE_ACTIVE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) 			if (test_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 				DMERR("Reshape requested but raid set is still reshaping");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 			if (mddev->delta_disks < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 			    (!mddev->delta_disks && (le32_to_cpu(sb->flags) & SB_FLAG_RESHAPE_BACKWARDS)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 				mddev->reshape_backwards = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 				mddev->reshape_backwards = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 			mddev->reshape_position = le64_to_cpu(sb->reshape_position);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 			rs->raid_type = get_raid_type_by_ll(mddev->level, mddev->layout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 		 * No takeover/reshaping, because we don't have the extended v1.9.0 metadata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 		struct raid_type *rt_cur = get_raid_type_by_ll(mddev->level, mddev->layout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 		struct raid_type *rt_new = get_raid_type_by_ll(mddev->new_level, mddev->new_layout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 		if (rs_takeover_requested(rs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 			if (rt_cur && rt_new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) 				DMERR("Takeover raid sets from %s to %s not yet supported by metadata. (raid level change)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 				      rt_cur->name, rt_new->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 				DMERR("Takeover raid sets not yet supported by metadata. (raid level change)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) 		} else if (rs_reshape_requested(rs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 			DMERR("Reshaping raid sets not yet supported by metadata. (raid layout change keeping level)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 			if (mddev->layout != mddev->new_layout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 				if (rt_cur && rt_new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 					DMERR("	 current layout %s vs new layout %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 					      rt_cur->name, rt_new->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 				else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 					DMERR("	 current layout 0x%X vs new layout 0x%X",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 					      le32_to_cpu(sb->layout), mddev->new_layout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 			if (mddev->chunk_sectors != mddev->new_chunk_sectors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) 				DMERR("	 current stripe sectors %u vs new stripe sectors %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 				      mddev->chunk_sectors, mddev->new_chunk_sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 			if (rs->delta_disks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) 				DMERR("	 current %u disks vs new %u disks",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 				      mddev->raid_disks, mddev->raid_disks + rs->delta_disks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 			if (rs_is_raid10(rs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 				DMERR("	 Old layout: %s w/ %u copies",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) 				      raid10_md_layout_to_format(mddev->layout),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) 				      raid10_md_layout_to_copies(mddev->layout));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 				DMERR("	 New layout: %s w/ %u copies",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 				      raid10_md_layout_to_format(mddev->new_layout),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) 				      raid10_md_layout_to_copies(mddev->new_layout));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 		DMINFO("Discovered old metadata format; upgrading to extended metadata format");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 	if (!test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 		mddev->recovery_cp = le64_to_cpu(sb->array_resync_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 	 * During load, we set FirstUse if a new superblock was written.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 	 * There are two reasons we might not have a superblock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) 	 * 1) The raid set is brand new - in which case, all of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 	 *    devices must have their In_sync bit set.	Also,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) 	 *    recovery_cp must be 0, unless forced.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) 	 * 2) This is a new device being added to an old raid set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) 	 *    and the new device needs to be rebuilt - in which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) 	 *    case the In_sync bit will /not/ be set and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) 	 *    recovery_cp must be MaxSector.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 	 * 3) This is/are a new device(s) being added to an old
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 	 *    raid set during takeover to a higher raid level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 	 *    to provide capacity for redundancy or during reshape
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 	 *    to add capacity to grow the raid set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 	d = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 	rdev_for_each(r, mddev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 		if (test_bit(Journal, &rdev->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 		if (test_bit(FirstUse, &r->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) 			new_devs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) 		if (!test_bit(In_sync, &r->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) 			DMINFO("Device %d specified for rebuild; clearing superblock",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) 				r->raid_disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) 			rebuilds++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) 			if (test_bit(FirstUse, &r->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 				rebuild_and_new++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 		d++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) 	if (new_devs == rs->raid_disks || !rebuilds) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) 		/* Replace a broken device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 		if (new_devs == rs->raid_disks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) 			DMINFO("Superblocks created for new raid set");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 			set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 		} else if (new_devs != rebuilds &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 			   new_devs != rs->delta_disks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 			DMERR("New device injected into existing raid set without "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 			      "'delta_disks' or 'rebuild' parameter specified");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 	} else if (new_devs && new_devs != rebuilds) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 		DMERR("%u 'rebuild' devices cannot be injected into"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 		      " a raid set with %u other first-time devices",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 		      rebuilds, new_devs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 	} else if (rebuilds) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 		if (rebuild_and_new && rebuilds != rebuild_and_new) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 			DMERR("new device%s provided without 'rebuild'",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 			      new_devs > 1 ? "s" : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 		} else if (!test_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags) && rs_is_recovering(rs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 			DMERR("'rebuild' specified while raid set is not in-sync (recovery_cp=%llu)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 			      (unsigned long long) mddev->recovery_cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 		} else if (rs_is_reshaping(rs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 			DMERR("'rebuild' specified while raid set is being reshaped (reshape_position=%llu)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 			      (unsigned long long) mddev->reshape_position);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 	 * Now we set the Faulty bit for those devices that are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 	 * recorded in the superblock as failed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 	sb_retrieve_failed_devices(sb, failed_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 	rdev_for_each(r, mddev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 		if (test_bit(Journal, &rdev->flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 		    !r->sb_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 		sb2 = page_address(r->sb_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 		sb2->failed_devices = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 		memset(sb2->extended_failed_devices, 0, sizeof(sb2->extended_failed_devices));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 		 * Check for any device re-ordering.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 		if (!test_bit(FirstUse, &r->flags) && (r->raid_disk >= 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 			role = le32_to_cpu(sb2->array_position);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) 			if (role < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 			if (role != r->raid_disk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 				if (rs_is_raid10(rs) && __is_raid10_near(mddev->layout)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) 					if (mddev->raid_disks % __raid10_near_copies(mddev->layout) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 					    rs->raid_disks % rs->raid10_copies) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) 						rs->ti->error =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) 							"Cannot change raid10 near set to odd # of devices!";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) 						return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) 					}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 					sb2->array_position = cpu_to_le32(r->raid_disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 				} else if (!(rs_is_raid10(rs) && rt_is_raid0(rs->raid_type)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 					   !(rs_is_raid0(rs) && rt_is_raid10(rs->raid_type)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 					   !rt_is_raid1(rs->raid_type)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 					rs->ti->error = "Cannot change device positions in raid set";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 					return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) 				DMINFO("raid device #%d now at position #%d", role, r->raid_disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 			 * Partial recovery is performed on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 			 * returning failed devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 			if (test_bit(role, (void *) failed_devices))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 				set_bit(Faulty, &r->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) static int super_validate(struct raid_set *rs, struct md_rdev *rdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) 	struct mddev *mddev = &rs->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) 	struct dm_raid_superblock *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) 	if (rs_is_raid0(rs) || !rdev->sb_page || rdev->raid_disk < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) 	sb = page_address(rdev->sb_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) 	 * If mddev->events is not set, we know we have not yet initialized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) 	 * the array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) 	if (!mddev->events && super_init_validation(rs, rdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) 	if (le32_to_cpu(sb->compat_features) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) 	    le32_to_cpu(sb->compat_features) != FEATURE_FLAG_SUPPORTS_V190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) 		rs->ti->error = "Unable to assemble array: Unknown flag(s) in compatible feature flags";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) 	if (sb->incompat_features) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) 		rs->ti->error = "Unable to assemble array: No incompatible feature flags supported yet";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) 	/* Enable bitmap creation on @rs unless no metadevs or raid0 or journaled raid4/5/6 set. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) 	mddev->bitmap_info.offset = (rt_is_raid0(rs->raid_type) || rs->journal_dev.dev) ? 0 : to_sector(4096);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) 	mddev->bitmap_info.default_offset = mddev->bitmap_info.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) 	if (!test_and_clear_bit(FirstUse, &rdev->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) 		 * Retrieve rdev size stored in superblock to be prepared for shrink.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) 		 * Check extended superblock members are present otherwise the size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) 		 * will not be set!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) 		if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) 			rdev->sectors = le64_to_cpu(sb->sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 		rdev->recovery_offset = le64_to_cpu(sb->disk_recovery_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) 		if (rdev->recovery_offset == MaxSector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) 			set_bit(In_sync, &rdev->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 		 * If no reshape in progress -> we're recovering single
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 		 * disk(s) and have to set the device(s) to out-of-sync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 		else if (!rs_is_reshaping(rs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 			clear_bit(In_sync, &rdev->flags); /* Mandatory for recovery */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 	 * If a device comes back, set it as not In_sync and no longer faulty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 	if (test_and_clear_bit(Faulty, &rdev->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 		rdev->recovery_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 		clear_bit(In_sync, &rdev->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 		rdev->saved_raid_disk = rdev->raid_disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 	/* Reshape support -> restore repective data offsets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 	rdev->data_offset = le64_to_cpu(sb->data_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 	rdev->new_data_offset = le64_to_cpu(sb->new_data_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499)  * Analyse superblocks and select the freshest.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) static int analyse_superblocks(struct dm_target *ti, struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 	struct md_rdev *rdev, *freshest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 	struct mddev *mddev = &rs->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) 	freshest = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) 	rdev_for_each(rdev, mddev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 		if (test_bit(Journal, &rdev->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) 		if (!rdev->meta_bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) 		/* Set superblock offset/size for metadata device. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) 		rdev->sb_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 		rdev->sb_size = bdev_logical_block_size(rdev->meta_bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) 		if (rdev->sb_size < sizeof(struct dm_raid_superblock) || rdev->sb_size > PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 			DMERR("superblock size of a logical block is no longer valid");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 		 * Skipping super_load due to CTR_FLAG_SYNC will cause
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) 		 * the array to undergo initialization again as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) 		 * though it were new.	This is the intended effect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) 		 * of the "sync" directive.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) 		 * With reshaping capability added, we must ensure that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) 		 * that the "sync" directive is disallowed during the reshape.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 		if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) 		r = super_load(rdev, freshest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) 		switch (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) 		case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 			freshest = rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) 		case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) 			/* This is a failure to read the superblock from the metadata device. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) 			 * We have to keep any raid0 data/metadata device pairs or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 			 * the MD raid0 personality will fail to start the array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) 			if (rs_is_raid0(rs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) 			 * We keep the dm_devs to be able to emit the device tuple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) 			 * properly on the table line in raid_status() (rather than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) 			 * mistakenly acting as if '- -' got passed into the constructor).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) 			 * The rdev has to stay on the same_set list to allow for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) 			 * the attempt to restore faulty devices on second resume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) 			rdev->raid_disk = rdev->saved_raid_disk = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) 	if (!freshest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) 	 * Validation of the freshest device provides the source of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) 	 * validation for the remaining devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) 	rs->ti->error = "Unable to assemble array: Invalid superblocks";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) 	if (super_validate(rs, freshest))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) 	if (validate_raid_redundancy(rs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) 		rs->ti->error = "Insufficient redundancy to activate array";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) 	rdev_for_each(rdev, mddev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) 		if (!test_bit(Journal, &rdev->flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) 		    rdev != freshest &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) 		    super_validate(rs, rdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590)  * Adjust data_offset and new_data_offset on all disk members of @rs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591)  * for out of place reshaping if requested by contructor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593)  * We need free space at the beginning of each raid disk for forward
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594)  * and at the end for backward reshapes which userspace has to provide
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595)  * via remapping/reordering of space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) static int rs_adjust_data_offsets(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) 	sector_t data_offset = 0, new_data_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) 	struct md_rdev *rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) 	/* Constructor did not request data offset change */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) 	if (!test_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) 		if (!rs_is_reshapable(rs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) 	/* HM FIXME: get In_Sync raid_dev? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) 	rdev = &rs->dev[0].rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) 	if (rs->delta_disks < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) 		 * Removing disks (reshaping backwards):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) 		 * - before reshape: data is at offset 0 and free space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) 		 *		     is at end of each component LV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) 		 * - after reshape: data is at offset rs->data_offset != 0 on each component LV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) 		data_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) 		new_data_offset = rs->data_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) 	} else if (rs->delta_disks > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) 		 * Adding disks (reshaping forwards):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) 		 * - before reshape: data is at offset rs->data_offset != 0 and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) 		 *		     free space is at begin of each component LV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) 		 * - after reshape: data is at offset 0 on each component LV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) 		data_offset = rs->data_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) 		new_data_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) 		 * User space passes in 0 for data offset after having removed reshape space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) 		 * - or - (data offset != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) 		 * Changing RAID layout or chunk size -> toggle offsets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) 		 * - before reshape: data is at offset rs->data_offset 0 and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) 		 *		     free space is at end of each component LV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) 		 *		     -or-
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) 		 *                   data is at offset rs->data_offset != 0 and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) 		 *		     free space is at begin of each component LV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) 		 * - after reshape: data is at offset 0 if it was at offset != 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) 		 *                  or at offset != 0 if it was at offset 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) 		 *                  on each component LV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) 		data_offset = rs->data_offset ? rdev->data_offset : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) 		new_data_offset = data_offset ? 0 : rs->data_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) 		set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) 	 * Make sure we got a minimum amount of free sectors per device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) 	if (rs->data_offset &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) 	    to_sector(i_size_read(rdev->bdev->bd_inode)) - rs->md.dev_sectors < MIN_FREE_RESHAPE_SPACE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) 		rs->ti->error = data_offset ? "No space for forward reshape" :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) 					      "No space for backward reshape";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) 		return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) 	 * Raise recovery_cp in case data_offset != 0 to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) 	 * avoid false recovery positives in the constructor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) 	if (rs->md.recovery_cp < rs->md.dev_sectors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) 		rs->md.recovery_cp += rs->dev[0].rdev.data_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) 	/* Adjust data offsets on all rdevs but on any raid4/5/6 journal device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) 	rdev_for_each(rdev, &rs->md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) 		if (!test_bit(Journal, &rdev->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) 			rdev->data_offset = data_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) 			rdev->new_data_offset = new_data_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) /* Userpace reordered disks -> adjust raid_disk indexes in @rs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) static void __reorder_raid_disk_indexes(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) 	int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) 	struct md_rdev *rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) 	rdev_for_each(rdev, &rs->md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) 		if (!test_bit(Journal, &rdev->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) 			rdev->raid_disk = i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) 			rdev->saved_raid_disk = rdev->new_raid_disk = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704)  * Setup @rs for takeover by a different raid level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) static int rs_setup_takeover(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) 	struct mddev *mddev = &rs->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) 	struct md_rdev *rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) 	unsigned int d = mddev->raid_disks = rs->raid_disks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) 	sector_t new_data_offset = rs->dev[0].rdev.data_offset ? 0 : rs->data_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) 	if (rt_is_raid10(rs->raid_type)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) 		if (rs_is_raid0(rs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) 			/* Userpace reordered disks -> adjust raid_disk indexes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) 			__reorder_raid_disk_indexes(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) 			/* raid0 -> raid10_far layout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) 			mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_FAR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) 								   rs->raid10_copies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) 		} else if (rs_is_raid1(rs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) 			/* raid1 -> raid10_near layout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) 			mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_NEAR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) 								   rs->raid_disks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) 	clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) 	mddev->recovery_cp = MaxSector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) 	while (d--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) 		rdev = &rs->dev[d].rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) 		if (test_bit(d, (void *) rs->rebuild_disks)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) 			clear_bit(In_sync, &rdev->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) 			clear_bit(Faulty, &rdev->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) 			mddev->recovery_cp = rdev->recovery_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) 			/* Bitmap has to be created when we do an "up" takeover */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) 			set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) 		rdev->new_data_offset = new_data_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) /* Prepare @rs for reshape */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) static int rs_prepare_reshape(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) 	bool reshape;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) 	struct mddev *mddev = &rs->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) 	if (rs_is_raid10(rs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) 		if (rs->raid_disks != mddev->raid_disks &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) 		    __is_raid10_near(mddev->layout) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) 		    rs->raid10_copies &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) 		    rs->raid10_copies != __raid10_near_copies(mddev->layout)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) 			 * raid disk have to be multiple of data copies to allow this conversion,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) 			 * This is actually not a reshape it is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) 			 * rebuild of any additional mirrors per group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) 			if (rs->raid_disks % rs->raid10_copies) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) 				rs->ti->error = "Can't reshape raid10 mirror groups";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) 			/* Userpace reordered disks to add/remove mirrors -> adjust raid_disk indexes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) 			__reorder_raid_disk_indexes(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) 			mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_NEAR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) 								   rs->raid10_copies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) 			mddev->new_layout = mddev->layout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) 			reshape = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) 			reshape = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) 	} else if (rs_is_raid456(rs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) 		reshape = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) 	else if (rs_is_raid1(rs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) 		if (rs->delta_disks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) 			/* Process raid1 via delta_disks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) 			mddev->degraded = rs->delta_disks < 0 ? -rs->delta_disks : rs->delta_disks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) 			reshape = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) 			/* Process raid1 without delta_disks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) 			mddev->raid_disks = rs->raid_disks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) 			reshape = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) 		rs->ti->error = "Called with bogus raid type";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) 	if (reshape) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) 		set_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) 		set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) 	} else if (mddev->raid_disks < rs->raid_disks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) 		/* Create new superblocks and bitmaps, if any new disks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) 		set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) /* Get reshape sectors from data_offsets or raid set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) static sector_t _get_reshape_sectors(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) 	struct md_rdev *rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) 	sector_t reshape_sectors = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) 	rdev_for_each(rdev, &rs->md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) 		if (!test_bit(Journal, &rdev->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) 			reshape_sectors = (rdev->data_offset > rdev->new_data_offset) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) 					rdev->data_offset - rdev->new_data_offset :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) 					rdev->new_data_offset - rdev->data_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) 	return max(reshape_sectors, (sector_t) rs->data_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827)  * Reshape:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828)  * - change raid layout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829)  * - change chunk size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830)  * - add disks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831)  * - remove disks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) static int rs_setup_reshape(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) 	int r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) 	unsigned int cur_raid_devs, d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) 	sector_t reshape_sectors = _get_reshape_sectors(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) 	struct mddev *mddev = &rs->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) 	struct md_rdev *rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) 	mddev->delta_disks = rs->delta_disks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) 	cur_raid_devs = mddev->raid_disks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) 	/* Ignore impossible layout change whilst adding/removing disks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) 	if (mddev->delta_disks &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) 	    mddev->layout != mddev->new_layout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) 		DMINFO("Ignoring invalid layout change with delta_disks=%d", rs->delta_disks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) 		mddev->new_layout = mddev->layout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) 	 * Adjust array size:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) 	 * - in case of adding disk(s), array size has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) 	 *   to grow after the disk adding reshape,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) 	 *   which'll hapen in the event handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) 	 *   reshape will happen forward, so space has to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) 	 *   be available at the beginning of each disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) 	 * - in case of removing disk(s), array size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) 	 *   has to shrink before starting the reshape,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) 	 *   which'll happen here;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) 	 *   reshape will happen backward, so space has to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) 	 *   be available at the end of each disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) 	 * - data_offset and new_data_offset are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) 	 *   adjusted for aforementioned out of place
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) 	 *   reshaping based on userspace passing in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) 	 *   the "data_offset <sectors>" key/value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) 	 *   pair via the constructor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) 	/* Add disk(s) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) 	if (rs->delta_disks > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) 		/* Prepare disks for check in raid4/5/6/10 {check|start}_reshape */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) 		for (d = cur_raid_devs; d < rs->raid_disks; d++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) 			rdev = &rs->dev[d].rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) 			clear_bit(In_sync, &rdev->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) 			 * save_raid_disk needs to be -1, or recovery_offset will be set to 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) 			 * by md, which'll store that erroneously in the superblock on reshape
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) 			rdev->saved_raid_disk = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) 			rdev->raid_disk = d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) 			rdev->sectors = mddev->dev_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) 			rdev->recovery_offset = rs_is_raid1(rs) ? 0 : MaxSector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) 		mddev->reshape_backwards = 0; /* adding disk(s) -> forward reshape */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) 	/* Remove disk(s) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) 	} else if (rs->delta_disks < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) 		r = rs_set_dev_and_array_sectors(rs, rs->ti->len, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) 		mddev->reshape_backwards = 1; /* removing disk(s) -> backward reshape */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) 	/* Change layout and/or chunk size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) 		 * Reshape layout (e.g. raid5_ls -> raid5_n) and/or chunk size:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) 		 * keeping number of disks and do layout change ->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) 		 * toggle reshape_backward depending on data_offset:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) 		 * - free space upfront -> reshape forward
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) 		 * - free space at the end -> reshape backward
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) 		 * This utilizes free reshape space avoiding the need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) 		 * for userspace to move (parts of) LV segments in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) 		 * case of layout/chunksize change  (for disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) 		 * adding/removing reshape space has to be at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) 		 * the proper address (see above with delta_disks):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) 		 * add disk(s)   -> begin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) 		 * remove disk(s)-> end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) 		mddev->reshape_backwards = rs->dev[0].rdev.data_offset ? 0 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) 	 * Adjust device size for forward reshape
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) 	 * because md_finish_reshape() reduces it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) 	if (!mddev->reshape_backwards)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) 		rdev_for_each(rdev, &rs->md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) 			if (!test_bit(Journal, &rdev->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) 				rdev->sectors += reshape_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937)  * If the md resync thread has updated superblock with max reshape position
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938)  * at the end of a reshape but not (yet) reset the layout configuration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939)  * changes -> reset the latter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) static void rs_reset_inconclusive_reshape(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) 	if (!rs_is_reshaping(rs) && rs_is_layout_change(rs, true)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) 		rs_set_cur(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) 		rs->md.delta_disks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) 		rs->md.reshape_backwards = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951)  * Enable/disable discard support on RAID set depending on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952)  * RAID level and discard properties of underlying RAID members.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) static void configure_discard_support(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) 	bool raid456;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) 	struct dm_target *ti = rs->ti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) 	 * XXX: RAID level 4,5,6 require zeroing for safety.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) 	raid456 = rs_is_raid456(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) 	for (i = 0; i < rs->raid_disks; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) 		struct request_queue *q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) 		if (!rs->dev[i].rdev.bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) 		q = bdev_get_queue(rs->dev[i].rdev.bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) 		if (!q || !blk_queue_discard(q))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) 		if (raid456) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) 			if (!devices_handle_discard_safely) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) 				DMERR("raid456 discard support disabled due to discard_zeroes_data uncertainty.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) 				DMERR("Set dm-raid.devices_handle_discard_safely=Y to override.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) 				return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) 	ti->num_discard_bios = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988)  * Construct a RAID0/1/10/4/5/6 mapping:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989)  * Args:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990)  *	<raid_type> <#raid_params> <raid_params>{0,}	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991)  *	<#raid_devs> [<meta_dev1> <dev1>]{1,}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993)  * <raid_params> varies by <raid_type>.	 See 'parse_raid_params' for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994)  * details on possible <raid_params>.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996)  * Userspace is free to initialize the metadata devices, hence the superblocks to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997)  * enforce recreation based on the passed in table parameters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) static int raid_ctr(struct dm_target *ti, unsigned int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003) 	bool resize = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) 	struct raid_type *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) 	unsigned int num_raid_params, num_raid_devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) 	sector_t sb_array_sectors, rdev_sectors, reshape_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) 	struct raid_set *rs = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) 	const char *arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) 	struct rs_layout rs_layout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) 	struct dm_arg_set as = { argc, argv }, as_nrd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) 	struct dm_arg _args[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) 		{ 0, as.argc, "Cannot understand number of raid parameters" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) 		{ 1, 254, "Cannot understand number of raid devices parameters" }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) 	arg = dm_shift_arg(&as);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) 	if (!arg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) 		ti->error = "No arguments";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) 	rt = get_raid_type(arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) 	if (!rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) 		ti->error = "Unrecognised raid_type";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) 	/* Must have <#raid_params> */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) 	if (dm_read_arg_group(_args, &as, &num_raid_params, &ti->error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) 	/* number of raid device tupples <meta_dev data_dev> */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) 	as_nrd = as;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) 	dm_consume_args(&as_nrd, num_raid_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) 	_args[1].max = (as_nrd.argc - 1) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036) 	if (dm_read_arg(_args + 1, &as_nrd, &num_raid_devs, &ti->error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) 	if (!__within_range(num_raid_devs, 1, MAX_RAID_DEVICES)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040) 		ti->error = "Invalid number of supplied raid devices";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) 	rs = raid_set_alloc(ti, rt, num_raid_devs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) 	if (IS_ERR(rs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) 		return PTR_ERR(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) 	r = parse_raid_params(rs, &as, num_raid_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) 	r = parse_dev_params(rs, &as);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) 	rs->md.sync_super = super_sync;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) 	 * Calculate ctr requested array and device sizes to allow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) 	 * for superblock analysis needing device sizes defined.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) 	 * Any existing superblock will overwrite the array and device sizes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) 	r = rs_set_dev_and_array_sectors(rs, rs->ti->len, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) 	/* Memorize just calculated, potentially larger sizes to grow the raid set in preresume */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) 	rs->array_sectors = rs->md.array_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) 	rs->dev_sectors = rs->md.dev_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) 	 * Backup any new raid set level, layout, ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) 	 * requested to be able to compare to superblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) 	 * members for conversion decisions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) 	rs_config_backup(rs, &rs_layout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) 	r = analyse_superblocks(ti, rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) 	/* All in-core metadata now as of current superblocks after calling analyse_superblocks() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) 	sb_array_sectors = rs->md.array_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) 	rdev_sectors = __rdev_sectors(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) 	if (!rdev_sectors) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087) 		ti->error = "Invalid rdev size";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) 		r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) 	reshape_sectors = _get_reshape_sectors(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) 	if (rs->dev_sectors != rdev_sectors) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) 		resize = (rs->dev_sectors != rdev_sectors - reshape_sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) 		if (rs->dev_sectors > rdev_sectors - reshape_sectors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) 			set_bit(RT_FLAG_RS_GROW, &rs->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) 	INIT_WORK(&rs->md.event_work, do_table_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) 	ti->private = rs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102) 	ti->num_flush_bios = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104) 	/* Restore any requested new layout for conversion decision */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) 	rs_config_restore(rs, &rs_layout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108) 	 * Now that we have any superblock metadata available,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) 	 * check for new, recovering, reshaping, to be taken over,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) 	 * to be reshaped or an existing, unchanged raid set to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) 	 * run in sequence.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) 	if (test_bit(MD_ARRAY_FIRST_USE, &rs->md.flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) 		/* A new raid6 set has to be recovered to ensure proper parity and Q-Syndrome */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) 		if (rs_is_raid6(rs) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) 		    test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) 			ti->error = "'nosync' not allowed for new raid6 set";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) 			r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) 		rs_setup_recovery(rs, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) 		set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) 		rs_set_new(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) 	} else if (rs_is_recovering(rs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) 		/* A recovering raid set may be resized */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126) 		goto size_check;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) 	} else if (rs_is_reshaping(rs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) 		/* Have to reject size change request during reshape */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) 		if (resize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) 			ti->error = "Can't resize a reshaping raid set";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) 			r = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134) 		/* skip setup rs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) 	} else if (rs_takeover_requested(rs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) 		if (rs_is_reshaping(rs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137) 			ti->error = "Can't takeover a reshaping raid set";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) 			r = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) 		/* We can't takeover a journaled raid4/5/6 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143) 		if (test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) 			ti->error = "Can't takeover a journaled raid4/5/6 set";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145) 			r = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) 		 * If a takeover is needed, userspace sets any additional
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) 		 * devices to rebuild and we can check for a valid request here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) 		 * If acceptible, set the level to the new requested
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154) 		 * one, prohibit requesting recovery, allow the raid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) 		 * set to run and store superblocks during resume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157) 		r = rs_check_takeover(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158) 		if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161) 		r = rs_setup_takeover(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162) 		if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165) 		set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166) 		/* Takeover ain't recovery, so disable recovery */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167) 		rs_setup_recovery(rs, MaxSector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168) 		rs_set_new(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169) 	} else if (rs_reshape_requested(rs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170) 		/* Only request grow on raid set size extensions, not on reshapes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171) 		clear_bit(RT_FLAG_RS_GROW, &rs->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174) 		 * No need to check for 'ongoing' takeover here, because takeover
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175) 		 * is an instant operation as oposed to an ongoing reshape.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178) 		/* We can't reshape a journaled raid4/5/6 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179) 		if (test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180) 			ti->error = "Can't reshape a journaled raid4/5/6 set";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181) 			r = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185) 		/* Out-of-place space has to be available to allow for a reshape unless raid1! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186) 		if (reshape_sectors || rs_is_raid1(rs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188) 			  * We can only prepare for a reshape here, because the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189) 			  * raid set needs to run to provide the repective reshape
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190) 			  * check functions via its MD personality instance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191) 			  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) 			  * So do the reshape check after md_run() succeeded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193) 			  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) 			r = rs_prepare_reshape(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) 			if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196) 				goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) 			/* Reshaping ain't recovery, so disable recovery */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) 			rs_setup_recovery(rs, MaxSector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201) 		rs_set_cur(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203) size_check:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204) 		/* May not set recovery when a device rebuild is requested */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) 		if (test_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206) 			clear_bit(RT_FLAG_RS_GROW, &rs->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207) 			set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) 			rs_setup_recovery(rs, MaxSector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209) 		} else if (test_bit(RT_FLAG_RS_GROW, &rs->runtime_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211) 			 * Set raid set to current size, i.e. size as of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) 			 * superblocks to grow to larger size in preresume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) 			r = rs_set_dev_and_array_sectors(rs, sb_array_sectors, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215) 			if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216) 				goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) 			rs_setup_recovery(rs, rs->md.recovery_cp < rs->md.dev_sectors ? rs->md.recovery_cp : rs->md.dev_sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) 			/* This is no size change or it is shrinking, update size and record in superblocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221) 			r = rs_set_dev_and_array_sectors(rs, rs->ti->len, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222) 			if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223) 				goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225) 			if (sb_array_sectors > rs->array_sectors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226) 				set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228) 		rs_set_cur(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231) 	/* If constructor requested it, change data and new_data offsets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232) 	r = rs_adjust_data_offsets(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236) 	/* Catch any inconclusive reshape superblock content. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237) 	rs_reset_inconclusive_reshape(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239) 	/* Start raid set read-only and assumed clean to change in raid_resume() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240) 	rs->md.ro = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241) 	rs->md.in_sync = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243) 	/* Keep array frozen until resume. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244) 	set_bit(MD_RECOVERY_FROZEN, &rs->md.recovery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246) 	/* Has to be held on running the array */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247) 	mddev_lock_nointr(&rs->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248) 	r = md_run(&rs->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249) 	rs->md.in_sync = 0; /* Assume already marked dirty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251) 		ti->error = "Failed to run raid array";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252) 		mddev_unlock(&rs->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) 	r = md_start(&rs->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258) 		ti->error = "Failed to start raid array";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259) 		mddev_unlock(&rs->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260) 		goto bad_md_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263) 	/* If raid4/5/6 journal mode explicitly requested (only possible with journal dev) -> set it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264) 	if (test_bit(__CTR_FLAG_JOURNAL_MODE, &rs->ctr_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) 		r = r5c_journal_mode_set(&rs->md, rs->journal_dev.mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266) 		if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267) 			ti->error = "Failed to set raid4/5/6 journal mode";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268) 			mddev_unlock(&rs->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269) 			goto bad_journal_mode_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273) 	mddev_suspend(&rs->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274) 	set_bit(RT_FLAG_RS_SUSPENDED, &rs->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276) 	/* Try to adjust the raid4/5/6 stripe cache size to the stripe size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277) 	if (rs_is_raid456(rs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278) 		r = rs_set_raid456_stripe_cache(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279) 		if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280) 			goto bad_stripe_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283) 	/* Now do an early reshape check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284) 	if (test_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285) 		r = rs_check_reshape(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286) 		if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287) 			goto bad_check_reshape;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289) 		/* Restore new, ctr requested layout to perform check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290) 		rs_config_restore(rs, &rs_layout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292) 		if (rs->md.pers->start_reshape) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293) 			r = rs->md.pers->check_reshape(&rs->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294) 			if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295) 				ti->error = "Reshape check failed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296) 				goto bad_check_reshape;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301) 	/* Disable/enable discard support on raid set. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302) 	configure_discard_support(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304) 	mddev_unlock(&rs->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307) bad_md_start:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308) bad_journal_mode_set:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309) bad_stripe_cache:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310) bad_check_reshape:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311) 	md_stop(&rs->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313) 	raid_set_free(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318) static void raid_dtr(struct dm_target *ti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320) 	struct raid_set *rs = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322) 	md_stop(&rs->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323) 	raid_set_free(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326) static int raid_map(struct dm_target *ti, struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328) 	struct raid_set *rs = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329) 	struct mddev *mddev = &rs->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332) 	 * If we're reshaping to add disk(s)), ti->len and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333) 	 * mddev->array_sectors will differ during the process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334) 	 * (ti->len > mddev->array_sectors), so we have to requeue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335) 	 * bios with addresses > mddev->array_sectors here or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336) 	 * there will occur accesses past EOD of the component
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337) 	 * data images thus erroring the raid set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339) 	if (unlikely(bio_end_sector(bio) > mddev->array_sectors))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340) 		return DM_MAPIO_REQUEUE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342) 	md_handle_request(mddev, bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344) 	return DM_MAPIO_SUBMITTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347) /* Return sync state string for @state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348) enum sync_state { st_frozen, st_reshape, st_resync, st_check, st_repair, st_recover, st_idle };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349) static const char *sync_str(enum sync_state state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351) 	/* Has to be in above sync_state order! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352) 	static const char *sync_strs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353) 		"frozen",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354) 		"reshape",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355) 		"resync",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356) 		"check",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357) 		"repair",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358) 		"recover",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359) 		"idle"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362) 	return __within_range(state, 0, ARRAY_SIZE(sync_strs) - 1) ? sync_strs[state] : "undef";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365) /* Return enum sync_state for @mddev derived from @recovery flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366) static enum sync_state decipher_sync_action(struct mddev *mddev, unsigned long recovery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368) 	if (test_bit(MD_RECOVERY_FROZEN, &recovery))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369) 		return st_frozen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371) 	/* The MD sync thread can be done with io or be interrupted but still be running */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372) 	if (!test_bit(MD_RECOVERY_DONE, &recovery) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373) 	    (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374) 	     (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375) 		if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376) 			return st_reshape;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378) 		if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379) 			if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380) 				return st_resync;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381) 			if (test_bit(MD_RECOVERY_CHECK, &recovery))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382) 				return st_check;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383) 			return st_repair;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386) 		if (test_bit(MD_RECOVERY_RECOVER, &recovery))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387) 			return st_recover;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389) 		if (mddev->reshape_position != MaxSector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390) 			return st_reshape;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393) 	return st_idle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397)  * Return status string for @rdev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399)  * Status characters:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401)  *  'D' = Dead/Failed raid set component or raid4/5/6 journal device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402)  *  'a' = Alive but not in-sync raid set component _or_ alive raid4/5/6 'write_back' journal device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403)  *  'A' = Alive and in-sync raid set component _or_ alive raid4/5/6 'write_through' journal device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404)  *  '-' = Non-existing device (i.e. uspace passed '- -' into the ctr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406) static const char *__raid_dev_status(struct raid_set *rs, struct md_rdev *rdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408) 	if (!rdev->bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409) 		return "-";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410) 	else if (test_bit(Faulty, &rdev->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411) 		return "D";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412) 	else if (test_bit(Journal, &rdev->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413) 		return (rs->journal_dev.mode == R5C_JOURNAL_MODE_WRITE_THROUGH) ? "A" : "a";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414) 	else if (test_bit(RT_FLAG_RS_RESYNCING, &rs->runtime_flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415) 		 (!test_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416) 		  !test_bit(In_sync, &rdev->flags)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417) 		return "a";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419) 		return "A";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3422) /* Helper to return resync/reshape progress for @rs and runtime flags for raid set in sync / resynching */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3423) static sector_t rs_get_progress(struct raid_set *rs, unsigned long recovery,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3424) 				enum sync_state state, sector_t resync_max_sectors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3425) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3426) 	sector_t r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3427) 	struct mddev *mddev = &rs->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3429) 	clear_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3430) 	clear_bit(RT_FLAG_RS_RESYNCING, &rs->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3432) 	if (rs_is_raid0(rs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3433) 		r = resync_max_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3434) 		set_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3436) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3437) 		if (state == st_idle && !test_bit(MD_RECOVERY_INTR, &recovery))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3438) 			r = mddev->recovery_cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3439) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3440) 			r = mddev->curr_resync_completed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3442) 		if (state == st_idle && r >= resync_max_sectors) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3443) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3444) 			 * Sync complete.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3445) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3446) 			/* In case we have finished recovering, the array is in sync. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3447) 			if (test_bit(MD_RECOVERY_RECOVER, &recovery))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3448) 				set_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3450) 		} else if (state == st_recover)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3451) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3452) 			 * In case we are recovering, the array is not in sync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3453) 			 * and health chars should show the recovering legs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3454) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3455) 			 * Already retrieved recovery offset from curr_resync_completed above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3456) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3457) 			;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3459) 		else if (state == st_resync || state == st_reshape)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3460) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3461) 			 * If "resync/reshape" is occurring, the raid set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3462) 			 * is or may be out of sync hence the health
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3463) 			 * characters shall be 'a'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3464) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3465) 			set_bit(RT_FLAG_RS_RESYNCING, &rs->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3467) 		else if (state == st_check || state == st_repair)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3468) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3469) 			 * If "check" or "repair" is occurring, the raid set has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3470) 			 * undergone an initial sync and the health characters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3471) 			 * should not be 'a' anymore.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3472) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3473) 			set_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3475) 		else if (test_bit(MD_RECOVERY_NEEDED, &recovery))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3476) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3477) 			 * We are idle and recovery is needed, prevent 'A' chars race
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3478) 			 * caused by components still set to in-sync by constructor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3479) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3480) 			set_bit(RT_FLAG_RS_RESYNCING, &rs->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3482) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3483) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3484) 			 * We are idle and the raid set may be doing an initial
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3485) 			 * sync, or it may be rebuilding individual components.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3486) 			 * If all the devices are In_sync, then it is the raid set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3487) 			 * that is being initialized.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3488) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3489) 			struct md_rdev *rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3491) 			set_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3492) 			rdev_for_each(rdev, mddev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3493) 				if (!test_bit(Journal, &rdev->flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3494) 				    !test_bit(In_sync, &rdev->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3495) 					clear_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3496) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3497) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3498) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3499) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3501) 	return min(r, resync_max_sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3504) /* Helper to return @dev name or "-" if !@dev */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3505) static const char *__get_dev_name(struct dm_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3506) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3507) 	return dev ? dev->name : "-";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3510) static void raid_status(struct dm_target *ti, status_type_t type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3511) 			unsigned int status_flags, char *result, unsigned int maxlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3513) 	struct raid_set *rs = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3514) 	struct mddev *mddev = &rs->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3515) 	struct r5conf *conf = mddev->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3516) 	int i, max_nr_stripes = conf ? conf->max_nr_stripes : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3517) 	unsigned long recovery;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3518) 	unsigned int raid_param_cnt = 1; /* at least 1 for chunksize */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3519) 	unsigned int sz = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3520) 	unsigned int rebuild_writemostly_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3521) 	sector_t progress, resync_max_sectors, resync_mismatches;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3522) 	enum sync_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3523) 	struct raid_type *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3525) 	switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3526) 	case STATUSTYPE_INFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3527) 		/* *Should* always succeed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3528) 		rt = get_raid_type_by_ll(mddev->new_level, mddev->new_layout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3529) 		if (!rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3530) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3532) 		DMEMIT("%s %d ", rt->name, mddev->raid_disks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3534) 		/* Access most recent mddev properties for status output */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3535) 		smp_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3536) 		/* Get sensible max sectors even if raid set not yet started */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3537) 		resync_max_sectors = test_bit(RT_FLAG_RS_PRERESUMED, &rs->runtime_flags) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3538) 				      mddev->resync_max_sectors : mddev->dev_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3539) 		recovery = rs->md.recovery;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3540) 		state = decipher_sync_action(mddev, recovery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3541) 		progress = rs_get_progress(rs, recovery, state, resync_max_sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3542) 		resync_mismatches = (mddev->last_sync_action && !strcasecmp(mddev->last_sync_action, "check")) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3543) 				    atomic64_read(&mddev->resync_mismatches) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3545) 		/* HM FIXME: do we want another state char for raid0? It shows 'D'/'A'/'-' now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3546) 		for (i = 0; i < rs->raid_disks; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3547) 			DMEMIT(__raid_dev_status(rs, &rs->dev[i].rdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3549) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3550) 		 * In-sync/Reshape ratio:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3551) 		 *  The in-sync ratio shows the progress of:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3552) 		 *   - Initializing the raid set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3553) 		 *   - Rebuilding a subset of devices of the raid set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3554) 		 *  The user can distinguish between the two by referring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3555) 		 *  to the status characters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3556) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3557) 		 *  The reshape ratio shows the progress of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3558) 		 *  changing the raid layout or the number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3559) 		 *  disks of a raid set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3560) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3561) 		DMEMIT(" %llu/%llu", (unsigned long long) progress,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3562) 				     (unsigned long long) resync_max_sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3564) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3565) 		 * v1.5.0+:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3566) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3567) 		 * Sync action:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3568) 		 *   See Documentation/admin-guide/device-mapper/dm-raid.rst for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3569) 		 *   information on each of these states.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3570) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3571) 		DMEMIT(" %s", sync_str(state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3573) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3574) 		 * v1.5.0+:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3575) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3576) 		 * resync_mismatches/mismatch_cnt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3577) 		 *   This field shows the number of discrepancies found when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3578) 		 *   performing a "check" of the raid set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3579) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3580) 		DMEMIT(" %llu", (unsigned long long) resync_mismatches);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3582) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3583) 		 * v1.9.0+:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3584) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3585) 		 * data_offset (needed for out of space reshaping)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3586) 		 *   This field shows the data offset into the data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3587) 		 *   image LV where the first stripes data starts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3588) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3589) 		 * We keep data_offset equal on all raid disks of the set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3590) 		 * so retrieving it from the first raid disk is sufficient.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3591) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3592) 		DMEMIT(" %llu", (unsigned long long) rs->dev[0].rdev.data_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3594) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3595) 		 * v1.10.0+:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3596) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3597) 		DMEMIT(" %s", test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3598) 			      __raid_dev_status(rs, &rs->journal_dev.rdev) : "-");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3599) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3601) 	case STATUSTYPE_TABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3602) 		/* Report the table line string you would use to construct this raid set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3604) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3605) 		 * Count any rebuild or writemostly argument pairs and subtract the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3606) 		 * hweight count being added below of any rebuild and writemostly ctr flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3607) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3608) 		for (i = 0; i < rs->raid_disks; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3609) 			rebuild_writemostly_count += (test_bit(i, (void *) rs->rebuild_disks) ? 2 : 0) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3610) 						     (test_bit(WriteMostly, &rs->dev[i].rdev.flags) ? 2 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3611) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3612) 		rebuild_writemostly_count -= (test_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags) ? 2 : 0) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3613) 					     (test_bit(__CTR_FLAG_WRITE_MOSTLY, &rs->ctr_flags) ? 2 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3614) 		/* Calculate raid parameter count based on ^ rebuild/writemostly argument counts and ctr flags set. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3615) 		raid_param_cnt += rebuild_writemostly_count +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3616) 				  hweight32(rs->ctr_flags & CTR_FLAG_OPTIONS_NO_ARGS) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3617) 				  hweight32(rs->ctr_flags & CTR_FLAG_OPTIONS_ONE_ARG) * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3618) 		/* Emit table line */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3619) 		/* This has to be in the documented order for userspace! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3620) 		DMEMIT("%s %u %u", rs->raid_type->name, raid_param_cnt, mddev->new_chunk_sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3621) 		if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3622) 			DMEMIT(" %s", dm_raid_arg_name_by_flag(CTR_FLAG_SYNC));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3623) 		if (test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3624) 			DMEMIT(" %s", dm_raid_arg_name_by_flag(CTR_FLAG_NOSYNC));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3625) 		if (test_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3626) 			for (i = 0; i < rs->raid_disks; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3627) 				if (test_bit(i, (void *) rs->rebuild_disks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3628) 					DMEMIT(" %s %u", dm_raid_arg_name_by_flag(CTR_FLAG_REBUILD), i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3629) 		if (test_bit(__CTR_FLAG_DAEMON_SLEEP, &rs->ctr_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3630) 			DMEMIT(" %s %lu", dm_raid_arg_name_by_flag(CTR_FLAG_DAEMON_SLEEP),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3631) 					  mddev->bitmap_info.daemon_sleep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3632) 		if (test_bit(__CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3633) 			DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_MIN_RECOVERY_RATE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3634) 					 mddev->sync_speed_min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3635) 		if (test_bit(__CTR_FLAG_MAX_RECOVERY_RATE, &rs->ctr_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3636) 			DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_MAX_RECOVERY_RATE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3637) 					 mddev->sync_speed_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3638) 		if (test_bit(__CTR_FLAG_WRITE_MOSTLY, &rs->ctr_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3639) 			for (i = 0; i < rs->raid_disks; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3640) 				if (test_bit(WriteMostly, &rs->dev[i].rdev.flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3641) 					DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_WRITE_MOSTLY),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3642) 					       rs->dev[i].rdev.raid_disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3643) 		if (test_bit(__CTR_FLAG_MAX_WRITE_BEHIND, &rs->ctr_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3644) 			DMEMIT(" %s %lu", dm_raid_arg_name_by_flag(CTR_FLAG_MAX_WRITE_BEHIND),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3645) 					  mddev->bitmap_info.max_write_behind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3646) 		if (test_bit(__CTR_FLAG_STRIPE_CACHE, &rs->ctr_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3647) 			DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_STRIPE_CACHE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3648) 					 max_nr_stripes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3649) 		if (test_bit(__CTR_FLAG_REGION_SIZE, &rs->ctr_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3650) 			DMEMIT(" %s %llu", dm_raid_arg_name_by_flag(CTR_FLAG_REGION_SIZE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3651) 					   (unsigned long long) to_sector(mddev->bitmap_info.chunksize));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3652) 		if (test_bit(__CTR_FLAG_RAID10_COPIES, &rs->ctr_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3653) 			DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_COPIES),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3654) 					 raid10_md_layout_to_copies(mddev->layout));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3655) 		if (test_bit(__CTR_FLAG_RAID10_FORMAT, &rs->ctr_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3656) 			DMEMIT(" %s %s", dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_FORMAT),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3657) 					 raid10_md_layout_to_format(mddev->layout));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3658) 		if (test_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3659) 			DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_DELTA_DISKS),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3660) 					 max(rs->delta_disks, mddev->delta_disks));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3661) 		if (test_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3662) 			DMEMIT(" %s %llu", dm_raid_arg_name_by_flag(CTR_FLAG_DATA_OFFSET),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3663) 					   (unsigned long long) rs->data_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3664) 		if (test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3665) 			DMEMIT(" %s %s", dm_raid_arg_name_by_flag(CTR_FLAG_JOURNAL_DEV),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3666) 					__get_dev_name(rs->journal_dev.dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3667) 		if (test_bit(__CTR_FLAG_JOURNAL_MODE, &rs->ctr_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3668) 			DMEMIT(" %s %s", dm_raid_arg_name_by_flag(CTR_FLAG_JOURNAL_MODE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3669) 					 md_journal_mode_to_dm_raid(rs->journal_dev.mode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3670) 		DMEMIT(" %d", rs->raid_disks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3671) 		for (i = 0; i < rs->raid_disks; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3672) 			DMEMIT(" %s %s", __get_dev_name(rs->dev[i].meta_dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3673) 					 __get_dev_name(rs->dev[i].data_dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3674) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3675) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3677) static int raid_message(struct dm_target *ti, unsigned int argc, char **argv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3678) 			char *result, unsigned maxlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3679) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3680) 	struct raid_set *rs = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3681) 	struct mddev *mddev = &rs->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3683) 	if (!mddev->pers || !mddev->pers->sync_request)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3684) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3686) 	if (!strcasecmp(argv[0], "frozen"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3687) 		set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3688) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3689) 		clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3691) 	if (!strcasecmp(argv[0], "idle") || !strcasecmp(argv[0], "frozen")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3692) 		if (mddev->sync_thread) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3693) 			set_bit(MD_RECOVERY_INTR, &mddev->recovery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3694) 			md_reap_sync_thread(mddev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3695) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3696) 	} else if (decipher_sync_action(mddev, mddev->recovery) != st_idle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3697) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3698) 	else if (!strcasecmp(argv[0], "resync"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3699) 		; /* MD_RECOVERY_NEEDED set below */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3700) 	else if (!strcasecmp(argv[0], "recover"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3701) 		set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3702) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3703) 		if (!strcasecmp(argv[0], "check")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3704) 			set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3705) 			set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3706) 			set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3707) 		} else if (!strcasecmp(argv[0], "repair")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3708) 			set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3709) 			set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3710) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3711) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3712) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3713) 	if (mddev->ro == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3714) 		/* A write to sync_action is enough to justify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3715) 		 * canceling read-auto mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3716) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3717) 		mddev->ro = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3718) 		if (!mddev->suspended && mddev->sync_thread)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3719) 			md_wakeup_thread(mddev->sync_thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3720) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3721) 	set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3722) 	if (!mddev->suspended && mddev->thread)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3723) 		md_wakeup_thread(mddev->thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3725) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3726) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3728) static int raid_iterate_devices(struct dm_target *ti,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3729) 				iterate_devices_callout_fn fn, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3730) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3731) 	struct raid_set *rs = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3732) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3733) 	int r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3735) 	for (i = 0; !r && i < rs->md.raid_disks; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3736) 		if (rs->dev[i].data_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3737) 			r = fn(ti,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3738) 				 rs->dev[i].data_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3739) 				 0, /* No offset on data devs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3740) 				 rs->md.dev_sectors,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3741) 				 data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3743) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3744) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3746) static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3747) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3748) 	struct raid_set *rs = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3749) 	unsigned int chunk_size_bytes = to_bytes(rs->md.chunk_sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3751) 	blk_limits_io_min(limits, chunk_size_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3752) 	blk_limits_io_opt(limits, chunk_size_bytes * mddev_data_stripes(rs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3754) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3755) 	 * RAID0 and RAID10 personalities require bio splitting,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3756) 	 * RAID1/4/5/6 don't and process large discard bios properly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3757) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3758) 	if (rs_is_raid0(rs) || rs_is_raid10(rs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3759) 		limits->discard_granularity = chunk_size_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3760) 		limits->max_discard_sectors = rs->md.chunk_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3761) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3762) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3764) static void raid_postsuspend(struct dm_target *ti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3765) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3766) 	struct raid_set *rs = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3768) 	if (!test_and_set_bit(RT_FLAG_RS_SUSPENDED, &rs->runtime_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3769) 		/* Writes have to be stopped before suspending to avoid deadlocks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3770) 		if (!test_bit(MD_RECOVERY_FROZEN, &rs->md.recovery))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3771) 			md_stop_writes(&rs->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3773) 		mddev_lock_nointr(&rs->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3774) 		mddev_suspend(&rs->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3775) 		mddev_unlock(&rs->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3776) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3777) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3779) static void attempt_restore_of_faulty_devices(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3780) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3781) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3782) 	uint64_t cleared_failed_devices[DISKS_ARRAY_ELEMS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3783) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3784) 	bool cleared = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3785) 	struct dm_raid_superblock *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3786) 	struct mddev *mddev = &rs->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3787) 	struct md_rdev *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3789) 	/* RAID personalities have to provide hot add/remove methods or we need to bail out. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3790) 	if (!mddev->pers || !mddev->pers->hot_add_disk || !mddev->pers->hot_remove_disk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3791) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3793) 	memset(cleared_failed_devices, 0, sizeof(cleared_failed_devices));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3795) 	for (i = 0; i < mddev->raid_disks; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3796) 		r = &rs->dev[i].rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3797) 		/* HM FIXME: enhance journal device recovery processing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3798) 		if (test_bit(Journal, &r->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3799) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3801) 		if (test_bit(Faulty, &r->flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3802) 		    r->meta_bdev && !read_disk_sb(r, r->sb_size, true)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3803) 			DMINFO("Faulty %s device #%d has readable super block."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3804) 			       "  Attempting to revive it.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3805) 			       rs->raid_type->name, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3807) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3808) 			 * Faulty bit may be set, but sometimes the array can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3809) 			 * be suspended before the personalities can respond
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3810) 			 * by removing the device from the array (i.e. calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3811) 			 * 'hot_remove_disk').	If they haven't yet removed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3812) 			 * the failed device, its 'raid_disk' number will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3813) 			 * '>= 0' - meaning we must call this function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3814) 			 * ourselves.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3815) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3816) 			flags = r->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3817) 			clear_bit(In_sync, &r->flags); /* Mandatory for hot remove. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3818) 			if (r->raid_disk >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3819) 				if (mddev->pers->hot_remove_disk(mddev, r)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3820) 					/* Failed to revive this device, try next */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3821) 					r->flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3822) 					continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3823) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3824) 			} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3825) 				r->raid_disk = r->saved_raid_disk = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3827) 			clear_bit(Faulty, &r->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3828) 			clear_bit(WriteErrorSeen, &r->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3830) 			if (mddev->pers->hot_add_disk(mddev, r)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3831) 				/* Failed to revive this device, try next */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3832) 				r->raid_disk = r->saved_raid_disk = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3833) 				r->flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3834) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3835) 				clear_bit(In_sync, &r->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3836) 				r->recovery_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3837) 				set_bit(i, (void *) cleared_failed_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3838) 				cleared = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3839) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3840) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3841) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3843) 	/* If any failed devices could be cleared, update all sbs failed_devices bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3844) 	if (cleared) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3845) 		uint64_t failed_devices[DISKS_ARRAY_ELEMS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3847) 		rdev_for_each(r, &rs->md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3848) 			if (test_bit(Journal, &r->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3849) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3851) 			sb = page_address(r->sb_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3852) 			sb_retrieve_failed_devices(sb, failed_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3854) 			for (i = 0; i < DISKS_ARRAY_ELEMS; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3855) 				failed_devices[i] &= ~cleared_failed_devices[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3857) 			sb_update_failed_devices(sb, failed_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3858) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3859) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3860) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3862) static int __load_dirty_region_bitmap(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3863) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3864) 	int r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3866) 	/* Try loading the bitmap unless "raid0", which does not have one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3867) 	if (!rs_is_raid0(rs) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3868) 	    !test_and_set_bit(RT_FLAG_RS_BITMAP_LOADED, &rs->runtime_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3869) 		r = md_bitmap_load(&rs->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3870) 		if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3871) 			DMERR("Failed to load bitmap");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3872) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3874) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3875) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3877) /* Enforce updating all superblocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3878) static void rs_update_sbs(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3879) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3880) 	struct mddev *mddev = &rs->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3881) 	int ro = mddev->ro;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3883) 	set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3884) 	mddev->ro = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3885) 	md_update_sb(mddev, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3886) 	mddev->ro = ro;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3889) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3890)  * Reshape changes raid algorithm of @rs to new one within personality
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3891)  * (e.g. raid6_zr -> raid6_nc), changes stripe size, adds/removes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3892)  * disks from a raid set thus growing/shrinking it or resizes the set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3893)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3894)  * Call mddev_lock_nointr() before!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3895)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3896) static int rs_start_reshape(struct raid_set *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3897) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3898) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3899) 	struct mddev *mddev = &rs->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3900) 	struct md_personality *pers = mddev->pers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3902) 	/* Don't allow the sync thread to work until the table gets reloaded. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3903) 	set_bit(MD_RECOVERY_WAIT, &mddev->recovery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3905) 	r = rs_setup_reshape(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3906) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3907) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3909) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3910) 	 * Check any reshape constraints enforced by the personalility
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3911) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3912) 	 * May as well already kick the reshape off so that * pers->start_reshape() becomes optional.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3913) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3914) 	r = pers->check_reshape(mddev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3915) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3916) 		rs->ti->error = "pers->check_reshape() failed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3917) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3918) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3920) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3921) 	 * Personality may not provide start reshape method in which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3922) 	 * case check_reshape above has already covered everything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3923) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3924) 	if (pers->start_reshape) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3925) 		r = pers->start_reshape(mddev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3926) 		if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3927) 			rs->ti->error = "pers->start_reshape() failed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3928) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3929) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3930) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3932) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3933) 	 * Now reshape got set up, update superblocks to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3934) 	 * reflect the fact so that a table reload will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3935) 	 * access proper superblock content in the ctr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3936) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3937) 	rs_update_sbs(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3939) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3940) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3942) static int raid_preresume(struct dm_target *ti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3943) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3944) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3945) 	struct raid_set *rs = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3946) 	struct mddev *mddev = &rs->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3948) 	/* This is a resume after a suspend of the set -> it's already started. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3949) 	if (test_and_set_bit(RT_FLAG_RS_PRERESUMED, &rs->runtime_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3950) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3952) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3953) 	 * The superblocks need to be updated on disk if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3954) 	 * array is new or new devices got added (thus zeroed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3955) 	 * out by userspace) or __load_dirty_region_bitmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3956) 	 * will overwrite them in core with old data or fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3957) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3958) 	if (test_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3959) 		rs_update_sbs(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3961) 	/* Load the bitmap from disk unless raid0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3962) 	r = __load_dirty_region_bitmap(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3963) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3964) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3966) 	/* We are extending the raid set size, adjust mddev/md_rdev sizes and set capacity. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3967) 	if (test_bit(RT_FLAG_RS_GROW, &rs->runtime_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3968) 		mddev->array_sectors = rs->array_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3969) 		mddev->dev_sectors = rs->dev_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3970) 		rs_set_rdev_sectors(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3971) 		rs_set_capacity(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3972) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3974) 	/* Resize bitmap to adjust to changed region size (aka MD bitmap chunksize) or grown device size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3975)         if (test_bit(RT_FLAG_RS_BITMAP_LOADED, &rs->runtime_flags) && mddev->bitmap &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3976) 	    (test_bit(RT_FLAG_RS_GROW, &rs->runtime_flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3977) 	     (rs->requested_bitmap_chunk_sectors &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3978) 	       mddev->bitmap_info.chunksize != to_bytes(rs->requested_bitmap_chunk_sectors)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3979) 		int chunksize = to_bytes(rs->requested_bitmap_chunk_sectors) ?: mddev->bitmap_info.chunksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3981) 		r = md_bitmap_resize(mddev->bitmap, mddev->dev_sectors, chunksize, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3982) 		if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3983) 			DMERR("Failed to resize bitmap");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3984) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3986) 	/* Check for any resize/reshape on @rs and adjust/initiate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3987) 	/* Be prepared for mddev_resume() in raid_resume() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3988) 	set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3989) 	if (mddev->recovery_cp && mddev->recovery_cp < MaxSector) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3990) 		set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3991) 		mddev->resync_min = mddev->recovery_cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3992) 		if (test_bit(RT_FLAG_RS_GROW, &rs->runtime_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3993) 			mddev->resync_max_sectors = mddev->dev_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3994) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3996) 	/* Check for any reshape request unless new raid set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3997) 	if (test_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3998) 		/* Initiate a reshape. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3999) 		rs_set_rdev_sectors(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4000) 		mddev_lock_nointr(mddev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4001) 		r = rs_start_reshape(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4002) 		mddev_unlock(mddev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4003) 		if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4004) 			DMWARN("Failed to check/start reshape, continuing without change");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4005) 		r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4006) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4008) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4009) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4011) static void raid_resume(struct dm_target *ti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4012) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4013) 	struct raid_set *rs = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4014) 	struct mddev *mddev = &rs->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4016) 	if (test_and_set_bit(RT_FLAG_RS_RESUMED, &rs->runtime_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4017) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4018) 		 * A secondary resume while the device is active.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4019) 		 * Take this opportunity to check whether any failed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4020) 		 * devices are reachable again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4021) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4022) 		attempt_restore_of_faulty_devices(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4023) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4025) 	if (test_and_clear_bit(RT_FLAG_RS_SUSPENDED, &rs->runtime_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4026) 		/* Only reduce raid set size before running a disk removing reshape. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4027) 		if (mddev->delta_disks < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4028) 			rs_set_capacity(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4030) 		mddev_lock_nointr(mddev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4031) 		clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4032) 		mddev->ro = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4033) 		mddev->in_sync = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4034) 		mddev_resume(mddev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4035) 		mddev_unlock(mddev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4036) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4037) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4039) static struct target_type raid_target = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4040) 	.name = "raid",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4041) 	.version = {1, 15, 1},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4042) 	.module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4043) 	.ctr = raid_ctr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4044) 	.dtr = raid_dtr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4045) 	.map = raid_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4046) 	.status = raid_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4047) 	.message = raid_message,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4048) 	.iterate_devices = raid_iterate_devices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4049) 	.io_hints = raid_io_hints,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4050) 	.postsuspend = raid_postsuspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4051) 	.preresume = raid_preresume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4052) 	.resume = raid_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4053) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4055) static int __init dm_raid_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4056) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4057) 	DMINFO("Loading target version %u.%u.%u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4058) 	       raid_target.version[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4059) 	       raid_target.version[1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4060) 	       raid_target.version[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4061) 	return dm_register_target(&raid_target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4062) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4063) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4064) static void __exit dm_raid_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4065) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4066) 	dm_unregister_target(&raid_target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4067) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4069) module_init(dm_raid_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4070) module_exit(dm_raid_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4072) module_param(devices_handle_discard_safely, bool, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4073) MODULE_PARM_DESC(devices_handle_discard_safely,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4074) 		 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4075) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4076) MODULE_DESCRIPTION(DM_NAME " raid0/1/10/4/5/6 target");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4077) MODULE_ALIAS("dm-raid0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4078) MODULE_ALIAS("dm-raid1");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4079) MODULE_ALIAS("dm-raid10");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4080) MODULE_ALIAS("dm-raid4");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4081) MODULE_ALIAS("dm-raid5");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4082) MODULE_ALIAS("dm-raid6");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4083) MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4084) MODULE_AUTHOR("Heinz Mauelshagen <dm-devel@redhat.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4085) MODULE_LICENSE("GPL");