NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010-2011 Neil Brown |
Heinz Mauelshagen | 702108d | 2016-05-19 18:49:26 +0200 | [diff] [blame] | 3 | * Copyright (C) 2010-2016 Red Hat, Inc. All rights reserved. |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 4 | * |
| 5 | * This file is released under the GPL. |
| 6 | */ |
| 7 | |
| 8 | #include <linux/slab.h> |
Paul Gortmaker | 056075c | 2011-07-03 13:58:33 -0400 | [diff] [blame] | 9 | #include <linux/module.h> |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 10 | |
| 11 | #include "md.h" |
Jonathan Brassow | 3273727 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 12 | #include "raid1.h" |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 13 | #include "raid5.h" |
Jonathan Brassow | 63f33b8d | 2012-07-31 21:44:26 -0500 | [diff] [blame] | 14 | #include "raid10.h" |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 15 | #include "bitmap.h" |
| 16 | |
Alasdair G Kergon | 3e8dbb7 | 2011-08-02 12:32:03 +0100 | [diff] [blame] | 17 | #include <linux/device-mapper.h> |
| 18 | |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 19 | #define DM_MSG_PREFIX "raid" |
Heinz Mauelshagen | 92c83d7 | 2016-05-19 18:49:25 +0200 | [diff] [blame] | 20 | #define MAX_RAID_DEVICES 253 /* md-raid kernel limit */ |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 21 | |
Heinz Mauelshagen | 40ba37e | 2016-06-13 17:55:13 +0200 | [diff] [blame] | 22 | /* |
| 23 | * Minimum sectors of free reshape space per raid device |
| 24 | */ |
| 25 | #define MIN_FREE_RESHAPE_SPACE to_sector(4*4096) |
| 26 | |
Heinz Mauelshagen | 48cf06b | 2014-09-24 17:47:19 +0200 | [diff] [blame] | 27 | static bool devices_handle_discard_safely = false; |
| 28 | |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 29 | /* |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 30 | * The following flags are used by dm-raid.c to set up the array state. |
| 31 | * They must be cleared before md_run is called. |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 32 | */ |
Mike Snitzer | 4315784 | 2016-05-30 13:03:37 -0400 | [diff] [blame] | 33 | #define FirstUse 10 /* rdev flag */ |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 34 | |
| 35 | struct raid_dev { |
| 36 | /* |
| 37 | * Two DM devices, one to hold metadata and one to hold the |
Mike Snitzer | 4315784 | 2016-05-30 13:03:37 -0400 | [diff] [blame] | 38 | * actual data/parity. The reason for this is to not confuse |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 39 | * ti->len and give more flexibility in altering size and |
| 40 | * characteristics. |
| 41 | * |
| 42 | * While it is possible for this device to be associated |
| 43 | * with a different physical device than the data_dev, it |
| 44 | * is intended for it to be the same. |
| 45 | * |--------- Physical Device ---------| |
| 46 | * |- meta_dev -|------ data_dev ------| |
| 47 | */ |
| 48 | struct dm_dev *meta_dev; |
| 49 | struct dm_dev *data_dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 50 | struct md_rdev rdev; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | /* |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 54 | * Bits for establishing rs->ctr_flags |
Heinz Mauelshagen | 702108d | 2016-05-19 18:49:26 +0200 | [diff] [blame] | 55 | * |
| 56 | * 1 = no flag value |
| 57 | * 2 = flag with value |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 58 | */ |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 59 | #define __CTR_FLAG_SYNC 0 /* 1 */ /* Not with raid0! */ |
| 60 | #define __CTR_FLAG_NOSYNC 1 /* 1 */ /* Not with raid0! */ |
| 61 | #define __CTR_FLAG_REBUILD 2 /* 2 */ /* Not with raid0! */ |
| 62 | #define __CTR_FLAG_DAEMON_SLEEP 3 /* 2 */ /* Not with raid0! */ |
| 63 | #define __CTR_FLAG_MIN_RECOVERY_RATE 4 /* 2 */ /* Not with raid0! */ |
| 64 | #define __CTR_FLAG_MAX_RECOVERY_RATE 5 /* 2 */ /* Not with raid0! */ |
| 65 | #define __CTR_FLAG_MAX_WRITE_BEHIND 6 /* 2 */ /* Only with raid1! */ |
| 66 | #define __CTR_FLAG_WRITE_MOSTLY 7 /* 2 */ /* Only with raid1! */ |
| 67 | #define __CTR_FLAG_STRIPE_CACHE 8 /* 2 */ /* Only with raid4/5/6! */ |
| 68 | #define __CTR_FLAG_REGION_SIZE 9 /* 2 */ /* Not with raid0! */ |
| 69 | #define __CTR_FLAG_RAID10_COPIES 10 /* 2 */ /* Only with raid10 */ |
| 70 | #define __CTR_FLAG_RAID10_FORMAT 11 /* 2 */ /* Only with raid10 */ |
Mike Snitzer | 9b6e542 | 2016-06-02 11:48:09 -0400 | [diff] [blame] | 71 | /* New for v1.9.0 */ |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 72 | #define __CTR_FLAG_DELTA_DISKS 12 /* 2 */ /* Only with reshapable raid4/5/6/10! */ |
| 73 | #define __CTR_FLAG_DATA_OFFSET 13 /* 2 */ /* Only with reshapable raid4/5/6/10! */ |
| 74 | #define __CTR_FLAG_RAID10_USE_NEAR_SETS 14 /* 2 */ /* Only with raid10! */ |
| 75 | |
| 76 | /* |
| 77 | * Flags for rs->ctr_flags field. |
| 78 | */ |
| 79 | #define CTR_FLAG_SYNC (1 << __CTR_FLAG_SYNC) |
| 80 | #define CTR_FLAG_NOSYNC (1 << __CTR_FLAG_NOSYNC) |
| 81 | #define CTR_FLAG_REBUILD (1 << __CTR_FLAG_REBUILD) |
| 82 | #define CTR_FLAG_DAEMON_SLEEP (1 << __CTR_FLAG_DAEMON_SLEEP) |
| 83 | #define CTR_FLAG_MIN_RECOVERY_RATE (1 << __CTR_FLAG_MIN_RECOVERY_RATE) |
| 84 | #define CTR_FLAG_MAX_RECOVERY_RATE (1 << __CTR_FLAG_MAX_RECOVERY_RATE) |
| 85 | #define CTR_FLAG_MAX_WRITE_BEHIND (1 << __CTR_FLAG_MAX_WRITE_BEHIND) |
| 86 | #define CTR_FLAG_WRITE_MOSTLY (1 << __CTR_FLAG_WRITE_MOSTLY) |
| 87 | #define CTR_FLAG_STRIPE_CACHE (1 << __CTR_FLAG_STRIPE_CACHE) |
| 88 | #define CTR_FLAG_REGION_SIZE (1 << __CTR_FLAG_REGION_SIZE) |
| 89 | #define CTR_FLAG_RAID10_COPIES (1 << __CTR_FLAG_RAID10_COPIES) |
| 90 | #define CTR_FLAG_RAID10_FORMAT (1 << __CTR_FLAG_RAID10_FORMAT) |
| 91 | #define CTR_FLAG_DELTA_DISKS (1 << __CTR_FLAG_DELTA_DISKS) |
| 92 | #define CTR_FLAG_DATA_OFFSET (1 << __CTR_FLAG_DATA_OFFSET) |
| 93 | #define CTR_FLAG_RAID10_USE_NEAR_SETS (1 << __CTR_FLAG_RAID10_USE_NEAR_SETS) |
Jonathan Brassow | 63f33b8d | 2012-07-31 21:44:26 -0500 | [diff] [blame] | 94 | |
Heinz Mauelshagen | f090279 | 2016-05-19 18:49:27 +0200 | [diff] [blame] | 95 | /* |
| 96 | * Definitions of various constructor flags to |
| 97 | * be used in checks of valid / invalid flags |
| 98 | * per raid level. |
| 99 | */ |
| 100 | /* Define all any sync flags */ |
| 101 | #define CTR_FLAGS_ANY_SYNC (CTR_FLAG_SYNC | CTR_FLAG_NOSYNC) |
| 102 | |
| 103 | /* Define flags for options without argument (e.g. 'nosync') */ |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 104 | #define CTR_FLAG_OPTIONS_NO_ARGS (CTR_FLAGS_ANY_SYNC | \ |
| 105 | CTR_FLAG_RAID10_USE_NEAR_SETS) |
Heinz Mauelshagen | f090279 | 2016-05-19 18:49:27 +0200 | [diff] [blame] | 106 | |
| 107 | /* Define flags for options with one argument (e.g. 'delta_disks +2') */ |
| 108 | #define CTR_FLAG_OPTIONS_ONE_ARG (CTR_FLAG_REBUILD | \ |
| 109 | CTR_FLAG_WRITE_MOSTLY | \ |
| 110 | CTR_FLAG_DAEMON_SLEEP | \ |
| 111 | CTR_FLAG_MIN_RECOVERY_RATE | \ |
| 112 | CTR_FLAG_MAX_RECOVERY_RATE | \ |
| 113 | CTR_FLAG_MAX_WRITE_BEHIND | \ |
| 114 | CTR_FLAG_STRIPE_CACHE | \ |
| 115 | CTR_FLAG_REGION_SIZE | \ |
| 116 | CTR_FLAG_RAID10_COPIES | \ |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 117 | CTR_FLAG_RAID10_FORMAT | \ |
| 118 | CTR_FLAG_DELTA_DISKS | \ |
| 119 | CTR_FLAG_DATA_OFFSET) |
Heinz Mauelshagen | f090279 | 2016-05-19 18:49:27 +0200 | [diff] [blame] | 120 | |
Heinz Mauelshagen | a30cbc0 | 2016-06-09 16:42:16 +0200 | [diff] [blame] | 121 | /* Valid options definitions per raid level... */ |
Heinz Mauelshagen | f090279 | 2016-05-19 18:49:27 +0200 | [diff] [blame] | 122 | |
Heinz Mauelshagen | a30cbc0 | 2016-06-09 16:42:16 +0200 | [diff] [blame] | 123 | /* "raid0" does only accept data offset */ |
| 124 | #define RAID0_VALID_FLAGS (CTR_FLAG_DATA_OFFSET) |
Heinz Mauelshagen | f090279 | 2016-05-19 18:49:27 +0200 | [diff] [blame] | 125 | |
Heinz Mauelshagen | a30cbc0 | 2016-06-09 16:42:16 +0200 | [diff] [blame] | 126 | /* "raid1" does not accept stripe cache, data offset, delta_disks or any raid10 options */ |
| 127 | #define RAID1_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \ |
| 128 | CTR_FLAG_REBUILD | \ |
| 129 | CTR_FLAG_WRITE_MOSTLY | \ |
| 130 | CTR_FLAG_DAEMON_SLEEP | \ |
| 131 | CTR_FLAG_MIN_RECOVERY_RATE | \ |
| 132 | CTR_FLAG_MAX_RECOVERY_RATE | \ |
| 133 | CTR_FLAG_MAX_WRITE_BEHIND | \ |
| 134 | CTR_FLAG_REGION_SIZE | \ |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 135 | CTR_FLAG_DATA_OFFSET) |
Heinz Mauelshagen | f090279 | 2016-05-19 18:49:27 +0200 | [diff] [blame] | 136 | |
| 137 | /* "raid10" does not accept any raid1 or stripe cache options */ |
Heinz Mauelshagen | a30cbc0 | 2016-06-09 16:42:16 +0200 | [diff] [blame] | 138 | #define RAID10_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \ |
| 139 | CTR_FLAG_REBUILD | \ |
| 140 | CTR_FLAG_DAEMON_SLEEP | \ |
| 141 | CTR_FLAG_MIN_RECOVERY_RATE | \ |
| 142 | CTR_FLAG_MAX_RECOVERY_RATE | \ |
| 143 | CTR_FLAG_REGION_SIZE | \ |
| 144 | CTR_FLAG_RAID10_COPIES | \ |
| 145 | CTR_FLAG_RAID10_FORMAT | \ |
| 146 | CTR_FLAG_DELTA_DISKS | \ |
| 147 | CTR_FLAG_DATA_OFFSET | \ |
| 148 | CTR_FLAG_RAID10_USE_NEAR_SETS) |
| 149 | |
Heinz Mauelshagen | f090279 | 2016-05-19 18:49:27 +0200 | [diff] [blame] | 150 | /* |
| 151 | * "raid4/5/6" do not accept any raid1 or raid10 specific options |
| 152 | * |
| 153 | * "raid6" does not accept "nosync", because it is not guaranteed |
| 154 | * that both parity and q-syndrome are being written properly with |
| 155 | * any writes |
| 156 | */ |
Heinz Mauelshagen | a30cbc0 | 2016-06-09 16:42:16 +0200 | [diff] [blame] | 157 | #define RAID45_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \ |
| 158 | CTR_FLAG_REBUILD | \ |
| 159 | CTR_FLAG_DAEMON_SLEEP | \ |
| 160 | CTR_FLAG_MIN_RECOVERY_RATE | \ |
| 161 | CTR_FLAG_MAX_RECOVERY_RATE | \ |
Heinz Mauelshagen | f090279 | 2016-05-19 18:49:27 +0200 | [diff] [blame] | 162 | CTR_FLAG_MAX_WRITE_BEHIND | \ |
Heinz Mauelshagen | a30cbc0 | 2016-06-09 16:42:16 +0200 | [diff] [blame] | 163 | CTR_FLAG_STRIPE_CACHE | \ |
| 164 | CTR_FLAG_REGION_SIZE | \ |
| 165 | CTR_FLAG_DELTA_DISKS | \ |
| 166 | CTR_FLAG_DATA_OFFSET) |
| 167 | |
| 168 | #define RAID6_VALID_FLAGS (CTR_FLAG_SYNC | \ |
| 169 | CTR_FLAG_REBUILD | \ |
| 170 | CTR_FLAG_DAEMON_SLEEP | \ |
| 171 | CTR_FLAG_MIN_RECOVERY_RATE | \ |
| 172 | CTR_FLAG_MAX_RECOVERY_RATE | \ |
| 173 | CTR_FLAG_MAX_WRITE_BEHIND | \ |
| 174 | CTR_FLAG_STRIPE_CACHE | \ |
| 175 | CTR_FLAG_REGION_SIZE | \ |
| 176 | CTR_FLAG_DELTA_DISKS | \ |
| 177 | CTR_FLAG_DATA_OFFSET) |
| 178 | /* ...valid options definitions per raid level */ |
Heinz Mauelshagen | f090279 | 2016-05-19 18:49:27 +0200 | [diff] [blame] | 179 | |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 180 | /* |
| 181 | * Flags for rs->runtime_flags field |
| 182 | * (RT_FLAG prefix meaning "runtime flag") |
| 183 | * |
| 184 | * These are all internal and used to define runtime state, |
| 185 | * e.g. to prevent another resume from preresume processing |
| 186 | * the raid set all over again. |
| 187 | */ |
Heinz Mauelshagen | 40ba37e | 2016-06-13 17:55:13 +0200 | [diff] [blame] | 188 | #define RT_FLAG_RS_PRERESUMED 0 |
| 189 | #define RT_FLAG_RS_RESUMED 1 |
| 190 | #define RT_FLAG_RS_BITMAP_LOADED 2 |
| 191 | #define RT_FLAG_UPDATE_SBS 3 |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 192 | #define RT_FLAG_RESHAPE_RS 4 |
Heinz Mauelshagen | 6e20902 | 2016-06-14 15:23:13 -0400 | [diff] [blame] | 193 | #define RT_FLAG_KEEP_RS_FROZEN 5 |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 194 | |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 195 | /* Array elements of 64 bit needed for rebuild/write_mostly bits */ |
| 196 | #define DISKS_ARRAY_ELEMS ((MAX_RAID_DEVICES + (sizeof(uint64_t) * 8 - 1)) / sizeof(uint64_t) / 8) |
| 197 | |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 198 | /* |
| 199 | * raid set level, layout and chunk sectors backup/restore |
| 200 | */ |
| 201 | struct rs_layout { |
| 202 | int new_level; |
| 203 | int new_layout; |
| 204 | int new_chunk_sectors; |
| 205 | }; |
| 206 | |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 207 | struct raid_set { |
| 208 | struct dm_target *ti; |
| 209 | |
Jonathan Brassow | 34f8ac6d | 2012-01-27 14:53:53 -0600 | [diff] [blame] | 210 | uint32_t bitmap_loaded; |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 211 | uint32_t stripe_cache_entries; |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 212 | unsigned long ctr_flags; |
| 213 | unsigned long runtime_flags; |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 214 | |
| 215 | uint64_t rebuild_disks[DISKS_ARRAY_ELEMS]; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 216 | |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 217 | int raid_disks; |
| 218 | int delta_disks; |
Heinz Mauelshagen | 4763e54 | 2016-05-19 18:49:31 +0200 | [diff] [blame] | 219 | int data_offset; |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 220 | int raid10_copies; |
Heinz Mauelshagen | 4257e08 | 2016-06-14 01:46:03 +0200 | [diff] [blame] | 221 | int requested_bitmap_chunk_sectors; |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 222 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 223 | struct mddev md; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 224 | struct raid_type *raid_type; |
| 225 | struct dm_target_callbacks callbacks; |
| 226 | |
| 227 | struct raid_dev dev[0]; |
| 228 | }; |
| 229 | |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 230 | static void rs_config_backup(struct raid_set *rs, struct rs_layout *l) |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 231 | { |
| 232 | struct mddev *mddev = &rs->md; |
| 233 | |
| 234 | l->new_level = mddev->new_level; |
| 235 | l->new_layout = mddev->new_layout; |
| 236 | l->new_chunk_sectors = mddev->new_chunk_sectors; |
| 237 | } |
| 238 | |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 239 | static void rs_config_restore(struct raid_set *rs, struct rs_layout *l) |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 240 | { |
| 241 | struct mddev *mddev = &rs->md; |
| 242 | |
| 243 | mddev->new_level = l->new_level; |
| 244 | mddev->new_layout = l->new_layout; |
| 245 | mddev->new_chunk_sectors = l->new_chunk_sectors; |
| 246 | } |
| 247 | |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 248 | /* raid10 algorithms (i.e. formats) */ |
| 249 | #define ALGORITHM_RAID10_DEFAULT 0 |
| 250 | #define ALGORITHM_RAID10_NEAR 1 |
| 251 | #define ALGORITHM_RAID10_OFFSET 2 |
| 252 | #define ALGORITHM_RAID10_FAR 3 |
| 253 | |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 254 | /* Supported raid types and properties. */ |
| 255 | static struct raid_type { |
| 256 | const char *name; /* RAID algorithm. */ |
| 257 | const char *descr; /* Descriptor text for logging. */ |
| 258 | const unsigned parity_devs; /* # of parity devices. */ |
| 259 | const unsigned minimal_devs; /* minimal # of devices in set. */ |
| 260 | const unsigned level; /* RAID level. */ |
| 261 | const unsigned algorithm; /* RAID algorithm. */ |
| 262 | } raid_types[] = { |
Mike Snitzer | 4315784 | 2016-05-30 13:03:37 -0400 | [diff] [blame] | 263 | {"raid0", "raid0 (striping)", 0, 2, 0, 0 /* NONE */}, |
| 264 | {"raid1", "raid1 (mirroring)", 0, 2, 1, 0 /* NONE */}, |
| 265 | {"raid10_far", "raid10 far (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_FAR}, |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 266 | {"raid10_offset", "raid10 offset (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_OFFSET}, |
Mike Snitzer | 4315784 | 2016-05-30 13:03:37 -0400 | [diff] [blame] | 267 | {"raid10_near", "raid10 near (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_NEAR}, |
| 268 | {"raid10", "raid10 (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_DEFAULT}, |
| 269 | {"raid4", "raid4 (dedicated last parity disk)", 1, 2, 4, ALGORITHM_PARITY_N}, /* raid4 layout = raid5_n */ |
| 270 | {"raid5_n", "raid5 (dedicated last parity disk)", 1, 2, 5, ALGORITHM_PARITY_N}, |
| 271 | {"raid5_ls", "raid5 (left symmetric)", 1, 2, 5, ALGORITHM_LEFT_SYMMETRIC}, |
| 272 | {"raid5_rs", "raid5 (right symmetric)", 1, 2, 5, ALGORITHM_RIGHT_SYMMETRIC}, |
| 273 | {"raid5_la", "raid5 (left asymmetric)", 1, 2, 5, ALGORITHM_LEFT_ASYMMETRIC}, |
| 274 | {"raid5_ra", "raid5 (right asymmetric)", 1, 2, 5, ALGORITHM_RIGHT_ASYMMETRIC}, |
| 275 | {"raid6_zr", "raid6 (zero restart)", 2, 4, 6, ALGORITHM_ROTATING_ZERO_RESTART}, |
| 276 | {"raid6_nr", "raid6 (N restart)", 2, 4, 6, ALGORITHM_ROTATING_N_RESTART}, |
| 277 | {"raid6_nc", "raid6 (N continue)", 2, 4, 6, ALGORITHM_ROTATING_N_CONTINUE}, |
| 278 | {"raid6_n_6", "raid6 (dedicated parity/Q n/6)", 2, 4, 6, ALGORITHM_PARITY_N_6}, |
| 279 | {"raid6_ls_6", "raid6 (left symmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_LEFT_SYMMETRIC_6}, |
| 280 | {"raid6_rs_6", "raid6 (right symmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_RIGHT_SYMMETRIC_6}, |
| 281 | {"raid6_la_6", "raid6 (left asymmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_LEFT_ASYMMETRIC_6}, |
| 282 | {"raid6_ra_6", "raid6 (right asymmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_RIGHT_ASYMMETRIC_6} |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 283 | }; |
| 284 | |
Heinz Mauelshagen | 92c83d7 | 2016-05-19 18:49:25 +0200 | [diff] [blame] | 285 | /* True, if @v is in inclusive range [@min, @max] */ |
Mike Snitzer | bb91a63 | 2016-06-02 12:06:54 -0400 | [diff] [blame] | 286 | static bool __within_range(long v, long min, long max) |
Heinz Mauelshagen | 92c83d7 | 2016-05-19 18:49:25 +0200 | [diff] [blame] | 287 | { |
| 288 | return v >= min && v <= max; |
| 289 | } |
| 290 | |
Heinz Mauelshagen | 702108d | 2016-05-19 18:49:26 +0200 | [diff] [blame] | 291 | /* All table line arguments are defined here */ |
| 292 | static struct arg_name_flag { |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 293 | const unsigned long flag; |
Heinz Mauelshagen | 702108d | 2016-05-19 18:49:26 +0200 | [diff] [blame] | 294 | const char *name; |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 295 | } __arg_name_flags[] = { |
Heinz Mauelshagen | 702108d | 2016-05-19 18:49:26 +0200 | [diff] [blame] | 296 | { CTR_FLAG_SYNC, "sync"}, |
| 297 | { CTR_FLAG_NOSYNC, "nosync"}, |
| 298 | { CTR_FLAG_REBUILD, "rebuild"}, |
| 299 | { CTR_FLAG_DAEMON_SLEEP, "daemon_sleep"}, |
| 300 | { CTR_FLAG_MIN_RECOVERY_RATE, "min_recovery_rate"}, |
| 301 | { CTR_FLAG_MAX_RECOVERY_RATE, "max_recovery_rate"}, |
| 302 | { CTR_FLAG_MAX_WRITE_BEHIND, "max_write_behind"}, |
| 303 | { CTR_FLAG_WRITE_MOSTLY, "writemostly"}, |
| 304 | { CTR_FLAG_STRIPE_CACHE, "stripe_cache"}, |
| 305 | { CTR_FLAG_REGION_SIZE, "region_size"}, |
| 306 | { CTR_FLAG_RAID10_COPIES, "raid10_copies"}, |
| 307 | { CTR_FLAG_RAID10_FORMAT, "raid10_format"}, |
Heinz Mauelshagen | 4763e54 | 2016-05-19 18:49:31 +0200 | [diff] [blame] | 308 | { CTR_FLAG_DATA_OFFSET, "data_offset"}, |
| 309 | { CTR_FLAG_DELTA_DISKS, "delta_disks"}, |
| 310 | { CTR_FLAG_RAID10_USE_NEAR_SETS, "raid10_use_near_sets"}, |
Heinz Mauelshagen | 702108d | 2016-05-19 18:49:26 +0200 | [diff] [blame] | 311 | }; |
| 312 | |
| 313 | /* Return argument name string for given @flag */ |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 314 | static const char *dm_raid_arg_name_by_flag(const uint32_t flag) |
Heinz Mauelshagen | 702108d | 2016-05-19 18:49:26 +0200 | [diff] [blame] | 315 | { |
| 316 | if (hweight32(flag) == 1) { |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 317 | struct arg_name_flag *anf = __arg_name_flags + ARRAY_SIZE(__arg_name_flags); |
Heinz Mauelshagen | 702108d | 2016-05-19 18:49:26 +0200 | [diff] [blame] | 318 | |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 319 | while (anf-- > __arg_name_flags) |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 320 | if (flag & anf->flag) |
Heinz Mauelshagen | 702108d | 2016-05-19 18:49:26 +0200 | [diff] [blame] | 321 | return anf->name; |
| 322 | |
| 323 | } else |
| 324 | DMERR("%s called with more than one flag!", __func__); |
| 325 | |
| 326 | return NULL; |
| 327 | } |
| 328 | |
| 329 | /* |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 330 | * bool helpers to test for various raid levels of a raid set, |
| 331 | * is. it's level as reported by the superblock rather than |
| 332 | * the requested raid_type passed to the constructor. |
| 333 | */ |
| 334 | /* Return true, if raid set in @rs is raid0 */ |
| 335 | static bool rs_is_raid0(struct raid_set *rs) |
| 336 | { |
| 337 | return !rs->md.level; |
| 338 | } |
| 339 | |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 340 | /* Return true, if raid set in @rs is raid1 */ |
| 341 | static bool rs_is_raid1(struct raid_set *rs) |
| 342 | { |
| 343 | return rs->md.level == 1; |
| 344 | } |
| 345 | |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 346 | /* Return true, if raid set in @rs is raid10 */ |
| 347 | static bool rs_is_raid10(struct raid_set *rs) |
| 348 | { |
| 349 | return rs->md.level == 10; |
| 350 | } |
| 351 | |
Heinz Mauelshagen | 40ba37e | 2016-06-13 17:55:13 +0200 | [diff] [blame] | 352 | /* Return true, if raid set in @rs is level 4, 5 or 6 */ |
| 353 | static bool rs_is_raid456(struct raid_set *rs) |
| 354 | { |
| 355 | return __within_range(rs->md.level, 4, 6); |
| 356 | } |
| 357 | |
| 358 | /* Return true, if raid set in @rs is reshapable */ |
| 359 | static unsigned int __is_raid10_far(int layout); |
| 360 | static bool rs_is_reshapable(struct raid_set *rs) |
| 361 | { |
| 362 | return rs_is_raid456(rs) || |
| 363 | (rs_is_raid10(rs) && !__is_raid10_far(rs->md.new_layout)); |
| 364 | } |
| 365 | |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 366 | /* Return true, if raid set in @rs is recovering */ |
| 367 | static bool rs_is_recovering(struct raid_set *rs) |
| 368 | { |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 369 | return rs->md.recovery_cp != MaxSector; |
| 370 | } |
| 371 | |
| 372 | /* Return true, if raid set in @rs is reshaping */ |
| 373 | static bool rs_is_reshaping(struct raid_set *rs) |
| 374 | { |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 375 | return rs->md.reshape_position != MaxSector; |
| 376 | } |
| 377 | |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 378 | /* |
Heinz Mauelshagen | f090279 | 2016-05-19 18:49:27 +0200 | [diff] [blame] | 379 | * bool helpers to test for various raid levels of a raid type |
| 380 | */ |
| 381 | |
| 382 | /* Return true, if raid type in @rt is raid0 */ |
| 383 | static bool rt_is_raid0(struct raid_type *rt) |
| 384 | { |
| 385 | return !rt->level; |
| 386 | } |
| 387 | |
| 388 | /* Return true, if raid type in @rt is raid1 */ |
| 389 | static bool rt_is_raid1(struct raid_type *rt) |
| 390 | { |
| 391 | return rt->level == 1; |
| 392 | } |
| 393 | |
| 394 | /* Return true, if raid type in @rt is raid10 */ |
| 395 | static bool rt_is_raid10(struct raid_type *rt) |
| 396 | { |
| 397 | return rt->level == 10; |
| 398 | } |
| 399 | |
| 400 | /* Return true, if raid type in @rt is raid4/5 */ |
| 401 | static bool rt_is_raid45(struct raid_type *rt) |
| 402 | { |
Mike Snitzer | bb91a63 | 2016-06-02 12:06:54 -0400 | [diff] [blame] | 403 | return __within_range(rt->level, 4, 5); |
Heinz Mauelshagen | f090279 | 2016-05-19 18:49:27 +0200 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | /* Return true, if raid type in @rt is raid6 */ |
| 407 | static bool rt_is_raid6(struct raid_type *rt) |
| 408 | { |
| 409 | return rt->level == 6; |
| 410 | } |
Heinz Mauelshagen | 676fa5a | 2016-05-19 18:49:29 +0200 | [diff] [blame] | 411 | |
| 412 | /* Return true, if raid type in @rt is raid4/5/6 */ |
| 413 | static bool rt_is_raid456(struct raid_type *rt) |
| 414 | { |
Mike Snitzer | bb91a63 | 2016-06-02 12:06:54 -0400 | [diff] [blame] | 415 | return __within_range(rt->level, 4, 6); |
Heinz Mauelshagen | 676fa5a | 2016-05-19 18:49:29 +0200 | [diff] [blame] | 416 | } |
Heinz Mauelshagen | f090279 | 2016-05-19 18:49:27 +0200 | [diff] [blame] | 417 | /* END: raid level bools */ |
| 418 | |
Heinz Mauelshagen | a30cbc0 | 2016-06-09 16:42:16 +0200 | [diff] [blame] | 419 | /* Return valid ctr flags for the raid level of @rs */ |
| 420 | static unsigned long __valid_flags(struct raid_set *rs) |
Heinz Mauelshagen | f090279 | 2016-05-19 18:49:27 +0200 | [diff] [blame] | 421 | { |
| 422 | if (rt_is_raid0(rs->raid_type)) |
Heinz Mauelshagen | a30cbc0 | 2016-06-09 16:42:16 +0200 | [diff] [blame] | 423 | return RAID0_VALID_FLAGS; |
Heinz Mauelshagen | f090279 | 2016-05-19 18:49:27 +0200 | [diff] [blame] | 424 | else if (rt_is_raid1(rs->raid_type)) |
Heinz Mauelshagen | a30cbc0 | 2016-06-09 16:42:16 +0200 | [diff] [blame] | 425 | return RAID1_VALID_FLAGS; |
Heinz Mauelshagen | f090279 | 2016-05-19 18:49:27 +0200 | [diff] [blame] | 426 | else if (rt_is_raid10(rs->raid_type)) |
Heinz Mauelshagen | a30cbc0 | 2016-06-09 16:42:16 +0200 | [diff] [blame] | 427 | return RAID10_VALID_FLAGS; |
Heinz Mauelshagen | f090279 | 2016-05-19 18:49:27 +0200 | [diff] [blame] | 428 | else if (rt_is_raid45(rs->raid_type)) |
Heinz Mauelshagen | a30cbc0 | 2016-06-09 16:42:16 +0200 | [diff] [blame] | 429 | return RAID45_VALID_FLAGS; |
Heinz Mauelshagen | f090279 | 2016-05-19 18:49:27 +0200 | [diff] [blame] | 430 | else if (rt_is_raid6(rs->raid_type)) |
Heinz Mauelshagen | a30cbc0 | 2016-06-09 16:42:16 +0200 | [diff] [blame] | 431 | return RAID6_VALID_FLAGS; |
Heinz Mauelshagen | f090279 | 2016-05-19 18:49:27 +0200 | [diff] [blame] | 432 | |
| 433 | return ~0; |
| 434 | } |
| 435 | |
| 436 | /* |
Heinz Mauelshagen | a30cbc0 | 2016-06-09 16:42:16 +0200 | [diff] [blame] | 437 | * Check for valid flags set on @rs |
Heinz Mauelshagen | f090279 | 2016-05-19 18:49:27 +0200 | [diff] [blame] | 438 | * |
| 439 | * Has to be called after parsing of the ctr flags! |
| 440 | */ |
Heinz Mauelshagen | a30cbc0 | 2016-06-09 16:42:16 +0200 | [diff] [blame] | 441 | static int rs_check_for_valid_flags(struct raid_set *rs) |
Heinz Mauelshagen | f090279 | 2016-05-19 18:49:27 +0200 | [diff] [blame] | 442 | { |
Heinz Mauelshagen | a30cbc0 | 2016-06-09 16:42:16 +0200 | [diff] [blame] | 443 | if (rs->ctr_flags & ~__valid_flags(rs)) { |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 444 | rs->ti->error = "Invalid flags combination"; |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 445 | return -EINVAL; |
| 446 | } |
Heinz Mauelshagen | f090279 | 2016-05-19 18:49:27 +0200 | [diff] [blame] | 447 | |
| 448 | return 0; |
| 449 | } |
| 450 | |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 451 | /* MD raid10 bit definitions and helpers */ |
| 452 | #define RAID10_OFFSET (1 << 16) /* stripes with data copies area adjacent on devices */ |
| 453 | #define RAID10_BROCKEN_USE_FAR_SETS (1 << 17) /* Broken in raid10.c: use sets instead of whole stripe rotation */ |
| 454 | #define RAID10_USE_FAR_SETS (1 << 18) /* Use sets instead of whole stripe rotation */ |
| 455 | #define RAID10_FAR_COPIES_SHIFT 8 /* raid10 # far copies shift (2nd byte of layout) */ |
| 456 | |
| 457 | /* Return md raid10 near copies for @layout */ |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 458 | static unsigned int __raid10_near_copies(int layout) |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 459 | { |
| 460 | return layout & 0xFF; |
| 461 | } |
| 462 | |
| 463 | /* Return md raid10 far copies for @layout */ |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 464 | static unsigned int __raid10_far_copies(int layout) |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 465 | { |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 466 | return __raid10_near_copies(layout >> RAID10_FAR_COPIES_SHIFT); |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /* Return true if md raid10 offset for @layout */ |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 470 | static unsigned int __is_raid10_offset(int layout) |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 471 | { |
| 472 | return layout & RAID10_OFFSET; |
| 473 | } |
| 474 | |
| 475 | /* Return true if md raid10 near for @layout */ |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 476 | static unsigned int __is_raid10_near(int layout) |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 477 | { |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 478 | return !__is_raid10_offset(layout) && __raid10_near_copies(layout) > 1; |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /* Return true if md raid10 far for @layout */ |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 482 | static unsigned int __is_raid10_far(int layout) |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 483 | { |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 484 | return !__is_raid10_offset(layout) && __raid10_far_copies(layout) > 1; |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | /* Return md raid10 layout string for @layout */ |
| 488 | static const char *raid10_md_layout_to_format(int layout) |
Jonathan Brassow | fe5d2f4 | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 489 | { |
| 490 | /* |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 491 | * Bit 16 stands for "offset" |
| 492 | * (i.e. adjacent stripes hold copies) |
| 493 | * |
Jonathan Brassow | fe5d2f4 | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 494 | * Refer to MD's raid10.c for details |
| 495 | */ |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 496 | if (__is_raid10_offset(layout)) |
Jonathan Brassow | fe5d2f4 | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 497 | return "offset"; |
| 498 | |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 499 | if (__raid10_near_copies(layout) > 1) |
Jonathan Brassow | fe5d2f4 | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 500 | return "near"; |
| 501 | |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 502 | WARN_ON(__raid10_far_copies(layout) < 2); |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 503 | |
Jonathan Brassow | fe5d2f4 | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 504 | return "far"; |
| 505 | } |
| 506 | |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 507 | /* Return md raid10 algorithm for @name */ |
Arnd Bergmann | 68c1c4d | 2016-06-16 11:03:25 +0200 | [diff] [blame] | 508 | static int raid10_name_to_format(const char *name) |
Jonathan Brassow | 63f33b8d | 2012-07-31 21:44:26 -0500 | [diff] [blame] | 509 | { |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 510 | if (!strcasecmp(name, "near")) |
| 511 | return ALGORITHM_RAID10_NEAR; |
| 512 | else if (!strcasecmp(name, "offset")) |
| 513 | return ALGORITHM_RAID10_OFFSET; |
| 514 | else if (!strcasecmp(name, "far")) |
| 515 | return ALGORITHM_RAID10_FAR; |
| 516 | |
| 517 | return -EINVAL; |
Jonathan Brassow | 63f33b8d | 2012-07-31 21:44:26 -0500 | [diff] [blame] | 518 | } |
| 519 | |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 520 | /* Return md raid10 copies for @layout */ |
| 521 | static unsigned int raid10_md_layout_to_copies(int layout) |
| 522 | { |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 523 | return __raid10_near_copies(layout) > 1 ? |
| 524 | __raid10_near_copies(layout) : __raid10_far_copies(layout); |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | /* Return md raid10 format id for @format string */ |
| 528 | static int raid10_format_to_md_layout(struct raid_set *rs, |
| 529 | unsigned int algorithm, |
| 530 | unsigned int copies) |
| 531 | { |
| 532 | unsigned int n = 1, f = 1, r = 0; |
| 533 | |
| 534 | /* |
| 535 | * MD resilienece flaw: |
| 536 | * |
| 537 | * enabling use_far_sets for far/offset formats causes copies |
| 538 | * to be colocated on the same devs together with their origins! |
| 539 | * |
| 540 | * -> disable it for now in the definition above |
| 541 | */ |
| 542 | if (algorithm == ALGORITHM_RAID10_DEFAULT || |
| 543 | algorithm == ALGORITHM_RAID10_NEAR) |
Jonathan Brassow | fe5d2f4 | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 544 | n = copies; |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 545 | |
| 546 | else if (algorithm == ALGORITHM_RAID10_OFFSET) { |
Jonathan Brassow | fe5d2f4 | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 547 | f = copies; |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 548 | r = RAID10_OFFSET; |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 549 | if (!test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags)) |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 550 | r |= RAID10_USE_FAR_SETS; |
Jonathan Brassow | fe5d2f4 | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 551 | |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 552 | } else if (algorithm == ALGORITHM_RAID10_FAR) { |
| 553 | f = copies; |
| 554 | r = !RAID10_OFFSET; |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 555 | if (!test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags)) |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 556 | r |= RAID10_USE_FAR_SETS; |
Jonathan Brassow | fe5d2f4 | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 557 | |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 558 | } else |
| 559 | return -EINVAL; |
Jonathan Brassow | fe5d2f4 | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 560 | |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 561 | return r | (f << RAID10_FAR_COPIES_SHIFT) | n; |
| 562 | } |
| 563 | /* END: MD raid10 bit definitions and helpers */ |
| 564 | |
| 565 | /* Check for any of the raid10 algorithms */ |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 566 | static int __got_raid10(struct raid_type *rtp, const int layout) |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 567 | { |
| 568 | if (rtp->level == 10) { |
| 569 | switch (rtp->algorithm) { |
| 570 | case ALGORITHM_RAID10_DEFAULT: |
| 571 | case ALGORITHM_RAID10_NEAR: |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 572 | return __is_raid10_near(layout); |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 573 | case ALGORITHM_RAID10_OFFSET: |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 574 | return __is_raid10_offset(layout); |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 575 | case ALGORITHM_RAID10_FAR: |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 576 | return __is_raid10_far(layout); |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 577 | default: |
| 578 | break; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | return 0; |
Jonathan Brassow | 63f33b8d | 2012-07-31 21:44:26 -0500 | [diff] [blame] | 583 | } |
| 584 | |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 585 | /* Return raid_type for @name */ |
Heinz Mauelshagen | 92c83d7 | 2016-05-19 18:49:25 +0200 | [diff] [blame] | 586 | static struct raid_type *get_raid_type(const char *name) |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 587 | { |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 588 | struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types); |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 589 | |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 590 | while (rtp-- > raid_types) |
| 591 | if (!strcasecmp(rtp->name, name)) |
| 592 | return rtp; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 593 | |
| 594 | return NULL; |
| 595 | } |
| 596 | |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 597 | /* Return raid_type for @name based derived from @level and @layout */ |
| 598 | static struct raid_type *get_raid_type_by_ll(const int level, const int layout) |
| 599 | { |
| 600 | struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types); |
| 601 | |
| 602 | while (rtp-- > raid_types) { |
| 603 | /* RAID10 special checks based on @layout flags/properties */ |
| 604 | if (rtp->level == level && |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 605 | (__got_raid10(rtp, layout) || rtp->algorithm == layout)) |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 606 | return rtp; |
| 607 | } |
| 608 | |
| 609 | return NULL; |
| 610 | } |
| 611 | |
| 612 | /* |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 613 | * Conditionally change bdev capacity of @rs |
| 614 | * in case of a disk add/remove reshape |
| 615 | */ |
| 616 | static void rs_set_capacity(struct raid_set *rs) |
| 617 | { |
| 618 | struct mddev *mddev = &rs->md; |
| 619 | |
| 620 | /* Make sure we access most actual mddev properties */ |
| 621 | smp_rmb(); |
| 622 | if (rs->ti->len != mddev->array_sectors && !rs_is_reshaping(rs)) { |
| 623 | struct gendisk *gendisk = dm_disk(dm_table_get_md(rs->ti->table)); |
| 624 | |
| 625 | set_capacity(gendisk, mddev->array_sectors); |
| 626 | revalidate_disk(gendisk); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | /* |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 631 | * Set the mddev properties in @rs to the current |
| 632 | * ones retrieved from the freshest superblock |
| 633 | */ |
| 634 | static void rs_set_cur(struct raid_set *rs) |
| 635 | { |
| 636 | struct mddev *mddev = &rs->md; |
| 637 | |
| 638 | mddev->new_level = mddev->level; |
| 639 | mddev->new_layout = mddev->layout; |
| 640 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
| 641 | } |
| 642 | |
| 643 | /* |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 644 | * Set the mddev properties in @rs to the new |
| 645 | * ones requested by the ctr |
| 646 | */ |
| 647 | static void rs_set_new(struct raid_set *rs) |
| 648 | { |
| 649 | struct mddev *mddev = &rs->md; |
| 650 | |
| 651 | mddev->level = mddev->new_level; |
| 652 | mddev->layout = mddev->new_layout; |
| 653 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 654 | mddev->raid_disks = rs->raid_disks; |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 655 | mddev->delta_disks = 0; |
| 656 | } |
| 657 | |
Mike Snitzer | bfcee0e | 2016-06-02 15:08:09 -0400 | [diff] [blame] | 658 | static struct raid_set *raid_set_alloc(struct dm_target *ti, struct raid_type *raid_type, |
| 659 | unsigned raid_devs) |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 660 | { |
| 661 | unsigned i; |
| 662 | struct raid_set *rs; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 663 | |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 664 | if (raid_devs <= raid_type->parity_devs) { |
| 665 | ti->error = "Insufficient number of devices"; |
| 666 | return ERR_PTR(-EINVAL); |
| 667 | } |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 668 | |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 669 | rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL); |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 670 | if (!rs) { |
| 671 | ti->error = "Cannot allocate raid context"; |
| 672 | return ERR_PTR(-ENOMEM); |
| 673 | } |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 674 | |
| 675 | mddev_init(&rs->md); |
| 676 | |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 677 | rs->raid_disks = raid_devs; |
| 678 | rs->delta_disks = 0; |
| 679 | |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 680 | rs->ti = ti; |
| 681 | rs->raid_type = raid_type; |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 682 | rs->stripe_cache_entries = 256; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 683 | rs->md.raid_disks = raid_devs; |
| 684 | rs->md.level = raid_type->level; |
| 685 | rs->md.new_level = rs->md.level; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 686 | rs->md.layout = raid_type->algorithm; |
| 687 | rs->md.new_layout = rs->md.layout; |
| 688 | rs->md.delta_disks = 0; |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 689 | rs->md.recovery_cp = rs_is_raid0(rs) ? MaxSector : 0; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 690 | |
| 691 | for (i = 0; i < raid_devs; i++) |
| 692 | md_rdev_init(&rs->dev[i].rdev); |
| 693 | |
| 694 | /* |
| 695 | * Remaining items to be initialized by further RAID params: |
| 696 | * rs->md.persistent |
| 697 | * rs->md.external |
| 698 | * rs->md.chunk_sectors |
| 699 | * rs->md.new_chunk_sectors |
Jonathan E Brassow | c039c33 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 700 | * rs->md.dev_sectors |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 701 | */ |
| 702 | |
| 703 | return rs; |
| 704 | } |
| 705 | |
Mike Snitzer | bfcee0e | 2016-06-02 15:08:09 -0400 | [diff] [blame] | 706 | static void raid_set_free(struct raid_set *rs) |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 707 | { |
| 708 | int i; |
| 709 | |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 710 | for (i = 0; i < rs->md.raid_disks; i++) { |
| 711 | if (rs->dev[i].meta_dev) |
| 712 | dm_put_device(rs->ti, rs->dev[i].meta_dev); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 713 | md_rdev_clear(&rs->dev[i].rdev); |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 714 | if (rs->dev[i].data_dev) |
| 715 | dm_put_device(rs->ti, rs->dev[i].data_dev); |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 716 | } |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 717 | |
| 718 | kfree(rs); |
| 719 | } |
| 720 | |
| 721 | /* |
| 722 | * For every device we have two words |
| 723 | * <meta_dev>: meta device name or '-' if missing |
| 724 | * <data_dev>: data device name or '-' if missing |
| 725 | * |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 726 | * The following are permitted: |
| 727 | * - - |
| 728 | * - <data_dev> |
| 729 | * <meta_dev> <data_dev> |
| 730 | * |
| 731 | * The following is not allowed: |
| 732 | * <meta_dev> - |
| 733 | * |
| 734 | * This code parses those words. If there is a failure, |
Mike Snitzer | bfcee0e | 2016-06-02 15:08:09 -0400 | [diff] [blame] | 735 | * the caller must use raid_set_free() to unwind the operations. |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 736 | */ |
Heinz Mauelshagen | 702108d | 2016-05-19 18:49:26 +0200 | [diff] [blame] | 737 | static int parse_dev_params(struct raid_set *rs, struct dm_arg_set *as) |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 738 | { |
| 739 | int i; |
| 740 | int rebuild = 0; |
| 741 | int metadata_available = 0; |
Heinz Mauelshagen | 73c6f23 | 2016-05-19 18:49:24 +0200 | [diff] [blame] | 742 | int r = 0; |
Heinz Mauelshagen | 92c83d7 | 2016-05-19 18:49:25 +0200 | [diff] [blame] | 743 | const char *arg; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 744 | |
Heinz Mauelshagen | 92c83d7 | 2016-05-19 18:49:25 +0200 | [diff] [blame] | 745 | /* Put off the number of raid devices argument to get to dev pairs */ |
| 746 | arg = dm_shift_arg(as); |
| 747 | if (!arg) |
| 748 | return -EINVAL; |
| 749 | |
| 750 | for (i = 0; i < rs->md.raid_disks; i++) { |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 751 | rs->dev[i].rdev.raid_disk = i; |
| 752 | |
| 753 | rs->dev[i].meta_dev = NULL; |
| 754 | rs->dev[i].data_dev = NULL; |
| 755 | |
| 756 | /* |
| 757 | * There are no offsets, since there is a separate device |
| 758 | * for data and metadata. |
| 759 | */ |
| 760 | rs->dev[i].rdev.data_offset = 0; |
| 761 | rs->dev[i].rdev.mddev = &rs->md; |
| 762 | |
Heinz Mauelshagen | 92c83d7 | 2016-05-19 18:49:25 +0200 | [diff] [blame] | 763 | arg = dm_shift_arg(as); |
| 764 | if (!arg) |
| 765 | return -EINVAL; |
| 766 | |
| 767 | if (strcmp(arg, "-")) { |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 768 | r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table), |
| 769 | &rs->dev[i].meta_dev); |
| 770 | if (r) { |
| 771 | rs->ti->error = "RAID metadata device lookup failure"; |
| 772 | return r; |
| 773 | } |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 774 | |
| 775 | rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL); |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 776 | if (!rs->dev[i].rdev.sb_page) { |
| 777 | rs->ti->error = "Failed to allocate superblock page"; |
| 778 | return -ENOMEM; |
| 779 | } |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Heinz Mauelshagen | 92c83d7 | 2016-05-19 18:49:25 +0200 | [diff] [blame] | 782 | arg = dm_shift_arg(as); |
| 783 | if (!arg) |
| 784 | return -EINVAL; |
| 785 | |
| 786 | if (!strcmp(arg, "-")) { |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 787 | if (!test_bit(In_sync, &rs->dev[i].rdev.flags) && |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 788 | (!rs->dev[i].rdev.recovery_offset)) { |
| 789 | rs->ti->error = "Drive designated for rebuild not specified"; |
| 790 | return -EINVAL; |
| 791 | } |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 792 | |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 793 | if (rs->dev[i].meta_dev) { |
| 794 | rs->ti->error = "No data device supplied with metadata device"; |
| 795 | return -EINVAL; |
| 796 | } |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 797 | |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 798 | continue; |
| 799 | } |
| 800 | |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 801 | r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table), |
| 802 | &rs->dev[i].data_dev); |
| 803 | if (r) { |
| 804 | rs->ti->error = "RAID device lookup failure"; |
| 805 | return r; |
| 806 | } |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 807 | |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 808 | if (rs->dev[i].meta_dev) { |
| 809 | metadata_available = 1; |
| 810 | rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev; |
| 811 | } |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 812 | rs->dev[i].rdev.bdev = rs->dev[i].data_dev->bdev; |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 813 | list_add_tail(&rs->dev[i].rdev.same_set, &rs->md.disks); |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 814 | if (!test_bit(In_sync, &rs->dev[i].rdev.flags)) |
| 815 | rebuild++; |
| 816 | } |
| 817 | |
| 818 | if (metadata_available) { |
| 819 | rs->md.external = 0; |
| 820 | rs->md.persistent = 1; |
| 821 | rs->md.major_version = 2; |
| 822 | } else if (rebuild && !rs->md.recovery_cp) { |
| 823 | /* |
| 824 | * Without metadata, we will not be able to tell if the array |
| 825 | * is in-sync or not - we must assume it is not. Therefore, |
| 826 | * it is impossible to rebuild a drive. |
| 827 | * |
| 828 | * Even if there is metadata, the on-disk information may |
| 829 | * indicate that the array is not in-sync and it will then |
| 830 | * fail at that time. |
| 831 | * |
| 832 | * User could specify 'nosync' option if desperate. |
| 833 | */ |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 834 | rs->ti->error = "Unable to rebuild drive while array is not in-sync"; |
| 835 | return -EINVAL; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | return 0; |
| 839 | } |
| 840 | |
| 841 | /* |
Jonathan Brassow | c108456 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 842 | * validate_region_size |
| 843 | * @rs |
| 844 | * @region_size: region size in sectors. If 0, pick a size (4MiB default). |
| 845 | * |
| 846 | * Set rs->md.bitmap_info.chunksize (which really refers to 'region size'). |
| 847 | * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap. |
| 848 | * |
| 849 | * Returns: 0 on success, -EINVAL on failure. |
| 850 | */ |
| 851 | static int validate_region_size(struct raid_set *rs, unsigned long region_size) |
| 852 | { |
| 853 | unsigned long min_region_size = rs->ti->len / (1 << 21); |
| 854 | |
| 855 | if (!region_size) { |
| 856 | /* |
Mike Snitzer | 4315784 | 2016-05-30 13:03:37 -0400 | [diff] [blame] | 857 | * Choose a reasonable default. All figures in sectors. |
Jonathan Brassow | c108456 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 858 | */ |
| 859 | if (min_region_size > (1 << 13)) { |
Jonathan Brassow | 3a0f9aa | 2012-12-21 20:23:33 +0000 | [diff] [blame] | 860 | /* If not a power of 2, make it the next power of 2 */ |
Mikulas Patocka | 042745e | 2015-10-02 11:17:37 -0400 | [diff] [blame] | 861 | region_size = roundup_pow_of_two(min_region_size); |
Jonathan Brassow | c108456 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 862 | DMINFO("Choosing default region size of %lu sectors", |
| 863 | region_size); |
Jonathan Brassow | c108456 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 864 | } else { |
| 865 | DMINFO("Choosing default region size of 4MiB"); |
| 866 | region_size = 1 << 13; /* sectors */ |
| 867 | } |
| 868 | } else { |
| 869 | /* |
| 870 | * Validate user-supplied value. |
| 871 | */ |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 872 | if (region_size > rs->ti->len) { |
| 873 | rs->ti->error = "Supplied region size is too large"; |
| 874 | return -EINVAL; |
| 875 | } |
Jonathan Brassow | c108456 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 876 | |
| 877 | if (region_size < min_region_size) { |
| 878 | DMERR("Supplied region_size (%lu sectors) below minimum (%lu)", |
| 879 | region_size, min_region_size); |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 880 | rs->ti->error = "Supplied region size is too small"; |
| 881 | return -EINVAL; |
Jonathan Brassow | c108456 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 882 | } |
| 883 | |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 884 | if (!is_power_of_2(region_size)) { |
| 885 | rs->ti->error = "Region size is not a power of 2"; |
| 886 | return -EINVAL; |
| 887 | } |
Jonathan Brassow | c108456 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 888 | |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 889 | if (region_size < rs->md.chunk_sectors) { |
| 890 | rs->ti->error = "Region size is smaller than the chunk size"; |
| 891 | return -EINVAL; |
| 892 | } |
Jonathan Brassow | c108456 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | /* |
| 896 | * Convert sectors to bytes. |
| 897 | */ |
| 898 | rs->md.bitmap_info.chunksize = (region_size << 9); |
| 899 | |
| 900 | return 0; |
| 901 | } |
| 902 | |
| 903 | /* |
Jonathan Brassow | 55ebbb5 | 2013-01-22 21:42:18 -0600 | [diff] [blame] | 904 | * validate_raid_redundancy |
Jonathan Brassow | eb64912 | 2012-10-11 13:40:09 +1100 | [diff] [blame] | 905 | * @rs |
| 906 | * |
Jonathan Brassow | 55ebbb5 | 2013-01-22 21:42:18 -0600 | [diff] [blame] | 907 | * Determine if there are enough devices in the array that haven't |
| 908 | * failed (or are being rebuilt) to form a usable array. |
Jonathan Brassow | eb64912 | 2012-10-11 13:40:09 +1100 | [diff] [blame] | 909 | * |
| 910 | * Returns: 0 on success, -EINVAL on failure. |
| 911 | */ |
Jonathan Brassow | 55ebbb5 | 2013-01-22 21:42:18 -0600 | [diff] [blame] | 912 | static int validate_raid_redundancy(struct raid_set *rs) |
Jonathan Brassow | eb64912 | 2012-10-11 13:40:09 +1100 | [diff] [blame] | 913 | { |
| 914 | unsigned i, rebuild_cnt = 0; |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 915 | unsigned rebuilds_per_group = 0, copies; |
Jonathan Brassow | fe5d2f4 | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 916 | unsigned group_size, last_group_start; |
Jonathan Brassow | eb64912 | 2012-10-11 13:40:09 +1100 | [diff] [blame] | 917 | |
Jonathan Brassow | eb64912 | 2012-10-11 13:40:09 +1100 | [diff] [blame] | 918 | for (i = 0; i < rs->md.raid_disks; i++) |
Jonathan Brassow | 55ebbb5 | 2013-01-22 21:42:18 -0600 | [diff] [blame] | 919 | if (!test_bit(In_sync, &rs->dev[i].rdev.flags) || |
| 920 | !rs->dev[i].rdev.sb_page) |
Jonathan Brassow | eb64912 | 2012-10-11 13:40:09 +1100 | [diff] [blame] | 921 | rebuild_cnt++; |
| 922 | |
| 923 | switch (rs->raid_type->level) { |
| 924 | case 1: |
| 925 | if (rebuild_cnt >= rs->md.raid_disks) |
| 926 | goto too_many; |
| 927 | break; |
| 928 | case 4: |
| 929 | case 5: |
| 930 | case 6: |
| 931 | if (rebuild_cnt > rs->raid_type->parity_devs) |
| 932 | goto too_many; |
| 933 | break; |
| 934 | case 10: |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 935 | copies = raid10_md_layout_to_copies(rs->md.new_layout); |
Jonathan Brassow | 4ec1e36 | 2012-10-11 13:40:24 +1100 | [diff] [blame] | 936 | if (rebuild_cnt < copies) |
| 937 | break; |
| 938 | |
| 939 | /* |
| 940 | * It is possible to have a higher rebuild count for RAID10, |
| 941 | * as long as the failed devices occur in different mirror |
| 942 | * groups (i.e. different stripes). |
| 943 | * |
Jonathan Brassow | 4ec1e36 | 2012-10-11 13:40:24 +1100 | [diff] [blame] | 944 | * When checking "near" format, make sure no adjacent devices |
| 945 | * have failed beyond what can be handled. In addition to the |
| 946 | * simple case where the number of devices is a multiple of the |
| 947 | * number of copies, we must also handle cases where the number |
| 948 | * of devices is not a multiple of the number of copies. |
Mike Snitzer | 4315784 | 2016-05-30 13:03:37 -0400 | [diff] [blame] | 949 | * E.g. dev1 dev2 dev3 dev4 dev5 |
| 950 | * A A B B C |
| 951 | * C D D E E |
Jonathan Brassow | 4ec1e36 | 2012-10-11 13:40:24 +1100 | [diff] [blame] | 952 | */ |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 953 | if (__is_raid10_near(rs->md.new_layout)) { |
| 954 | for (i = 0; i < rs->raid_disks; i++) { |
Jonathan Brassow | fe5d2f4 | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 955 | if (!(i % copies)) |
| 956 | rebuilds_per_group = 0; |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 957 | if ((!rs->dev[i].rdev.sb_page || |
Heinz Mauelshagen | 40ba37e | 2016-06-13 17:55:13 +0200 | [diff] [blame] | 958 | !test_bit(In_sync, &rs->dev[i].rdev.flags)) && |
Jonathan Brassow | fe5d2f4 | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 959 | (++rebuilds_per_group >= copies)) |
| 960 | goto too_many; |
| 961 | } |
| 962 | break; |
| 963 | } |
| 964 | |
| 965 | /* |
| 966 | * When checking "far" and "offset" formats, we need to ensure |
| 967 | * that the device that holds its copy is not also dead or |
| 968 | * being rebuilt. (Note that "far" and "offset" formats only |
| 969 | * support two copies right now. These formats also only ever |
| 970 | * use the 'use_far_sets' variant.) |
| 971 | * |
| 972 | * This check is somewhat complicated by the need to account |
Mike Snitzer | 4315784 | 2016-05-30 13:03:37 -0400 | [diff] [blame] | 973 | * for arrays that are not a multiple of (far) copies. This |
Jonathan Brassow | fe5d2f4 | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 974 | * results in the need to treat the last (potentially larger) |
| 975 | * set differently. |
| 976 | */ |
| 977 | group_size = (rs->md.raid_disks / copies); |
| 978 | last_group_start = (rs->md.raid_disks / group_size) - 1; |
| 979 | last_group_start *= group_size; |
| 980 | for (i = 0; i < rs->md.raid_disks; i++) { |
| 981 | if (!(i % copies) && !(i > last_group_start)) |
Jonathan Brassow | 55ebbb5 | 2013-01-22 21:42:18 -0600 | [diff] [blame] | 982 | rebuilds_per_group = 0; |
Jonathan Brassow | fe5d2f4 | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 983 | if ((!rs->dev[i].rdev.sb_page || |
| 984 | !test_bit(In_sync, &rs->dev[i].rdev.flags)) && |
Jonathan Brassow | 4ec1e36 | 2012-10-11 13:40:24 +1100 | [diff] [blame] | 985 | (++rebuilds_per_group >= copies)) |
Jonathan Brassow | fe5d2f4 | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 986 | goto too_many; |
Jonathan Brassow | 4ec1e36 | 2012-10-11 13:40:24 +1100 | [diff] [blame] | 987 | } |
| 988 | break; |
Jonathan Brassow | eb64912 | 2012-10-11 13:40:09 +1100 | [diff] [blame] | 989 | default: |
Jonathan Brassow | 55ebbb5 | 2013-01-22 21:42:18 -0600 | [diff] [blame] | 990 | if (rebuild_cnt) |
| 991 | return -EINVAL; |
Jonathan Brassow | eb64912 | 2012-10-11 13:40:09 +1100 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | return 0; |
| 995 | |
| 996 | too_many: |
Jonathan Brassow | eb64912 | 2012-10-11 13:40:09 +1100 | [diff] [blame] | 997 | return -EINVAL; |
| 998 | } |
| 999 | |
| 1000 | /* |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1001 | * Possible arguments are... |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1002 | * <chunk_size> [optional_args] |
| 1003 | * |
Jonathan Brassow | 3273727 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1004 | * Argument definitions |
| 1005 | * <chunk_size> The number of sectors per disk that |
Mike Snitzer | 4315784 | 2016-05-30 13:03:37 -0400 | [diff] [blame] | 1006 | * will form the "stripe" |
Jonathan Brassow | 3273727 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1007 | * [[no]sync] Force or prevent recovery of the |
Mike Snitzer | 4315784 | 2016-05-30 13:03:37 -0400 | [diff] [blame] | 1008 | * entire array |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1009 | * [rebuild <idx>] Rebuild the drive indicated by the index |
Jonathan Brassow | 3273727 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1010 | * [daemon_sleep <ms>] Time between bitmap daemon work to |
Mike Snitzer | 4315784 | 2016-05-30 13:03:37 -0400 | [diff] [blame] | 1011 | * clear bits |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1012 | * [min_recovery_rate <kB/sec/disk>] Throttle RAID initialization |
| 1013 | * [max_recovery_rate <kB/sec/disk>] Throttle RAID initialization |
Jonathan Brassow | 46bed2b | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1014 | * [write_mostly <idx>] Indicate a write mostly drive via index |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1015 | * [max_write_behind <sectors>] See '-write-behind=' (man mdadm) |
| 1016 | * [stripe_cache <sectors>] Stripe cache size for higher RAIDs |
Mike Snitzer | 4315784 | 2016-05-30 13:03:37 -0400 | [diff] [blame] | 1017 | * [region_size <sectors>] Defines granularity of bitmap |
Jonathan Brassow | 63f33b8d | 2012-07-31 21:44:26 -0500 | [diff] [blame] | 1018 | * |
| 1019 | * RAID10-only options: |
Mike Snitzer | 4315784 | 2016-05-30 13:03:37 -0400 | [diff] [blame] | 1020 | * [raid10_copies <# copies>] Number of copies. (Default: 2) |
Jonathan Brassow | fe5d2f4 | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 1021 | * [raid10_format <near|far|offset>] Layout algorithm. (Default: near) |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1022 | */ |
Heinz Mauelshagen | 92c83d7 | 2016-05-19 18:49:25 +0200 | [diff] [blame] | 1023 | static int parse_raid_params(struct raid_set *rs, struct dm_arg_set *as, |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1024 | unsigned num_raid_params) |
| 1025 | { |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 1026 | int value, raid10_format = ALGORITHM_RAID10_DEFAULT; |
Jonathan Brassow | 63f33b8d | 2012-07-31 21:44:26 -0500 | [diff] [blame] | 1027 | unsigned raid10_copies = 2; |
Heinz Mauelshagen | 5fa146b | 2016-06-15 18:50:18 +0200 | [diff] [blame] | 1028 | unsigned i, write_mostly = 0; |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 1029 | unsigned region_size = 0; |
Mike Snitzer | 542f903 | 2012-07-27 15:08:00 +0100 | [diff] [blame] | 1030 | sector_t max_io_len; |
Heinz Mauelshagen | 92c83d7 | 2016-05-19 18:49:25 +0200 | [diff] [blame] | 1031 | const char *arg, *key; |
Heinz Mauelshagen | 702108d | 2016-05-19 18:49:26 +0200 | [diff] [blame] | 1032 | struct raid_dev *rd; |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 1033 | struct raid_type *rt = rs->raid_type; |
Heinz Mauelshagen | 92c83d7 | 2016-05-19 18:49:25 +0200 | [diff] [blame] | 1034 | |
| 1035 | arg = dm_shift_arg(as); |
| 1036 | num_raid_params--; /* Account for chunk_size argument */ |
| 1037 | |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 1038 | if (kstrtoint(arg, 10, &value) < 0) { |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1039 | rs->ti->error = "Bad numerical argument given for chunk_size"; |
| 1040 | return -EINVAL; |
| 1041 | } |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1042 | |
| 1043 | /* |
| 1044 | * First, parse the in-order required arguments |
Jonathan Brassow | 3273727 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1045 | * "chunk_size" is the only argument of this type. |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1046 | */ |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 1047 | if (rt_is_raid1(rt)) { |
Jonathan Brassow | 3273727 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1048 | if (value) |
| 1049 | DMERR("Ignoring chunk size parameter for RAID 1"); |
| 1050 | value = 0; |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1051 | } else if (!is_power_of_2(value)) { |
| 1052 | rs->ti->error = "Chunk size must be a power of 2"; |
| 1053 | return -EINVAL; |
| 1054 | } else if (value < 8) { |
| 1055 | rs->ti->error = "Chunk size value is too small"; |
| 1056 | return -EINVAL; |
| 1057 | } |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1058 | |
| 1059 | rs->md.new_chunk_sectors = rs->md.chunk_sectors = value; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1060 | |
| 1061 | /* |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1062 | * We set each individual device as In_sync with a completed |
| 1063 | * 'recovery_offset'. If there has been a device failure or |
| 1064 | * replacement then one of the following cases applies: |
| 1065 | * |
| 1066 | * 1) User specifies 'rebuild'. |
Mike Snitzer | 4315784 | 2016-05-30 13:03:37 -0400 | [diff] [blame] | 1067 | * - Device is reset when param is read. |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1068 | * 2) A new device is supplied. |
Mike Snitzer | 4315784 | 2016-05-30 13:03:37 -0400 | [diff] [blame] | 1069 | * - No matching superblock found, resets device. |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1070 | * 3) Device failure was transient and returns on reload. |
Mike Snitzer | 4315784 | 2016-05-30 13:03:37 -0400 | [diff] [blame] | 1071 | * - Failure noticed, resets device for bitmap replay. |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1072 | * 4) Device hadn't completed recovery after previous failure. |
Mike Snitzer | 4315784 | 2016-05-30 13:03:37 -0400 | [diff] [blame] | 1073 | * - Superblock is read and overrides recovery_offset. |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1074 | * |
| 1075 | * What is found in the superblocks of the devices is always |
| 1076 | * authoritative, unless 'rebuild' or '[no]sync' was specified. |
| 1077 | */ |
| 1078 | for (i = 0; i < rs->md.raid_disks; i++) { |
| 1079 | set_bit(In_sync, &rs->dev[i].rdev.flags); |
| 1080 | rs->dev[i].rdev.recovery_offset = MaxSector; |
| 1081 | } |
| 1082 | |
| 1083 | /* |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1084 | * Second, parse the unordered optional arguments |
| 1085 | */ |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1086 | for (i = 0; i < num_raid_params; i++) { |
Heinz Mauelshagen | 4763e54 | 2016-05-19 18:49:31 +0200 | [diff] [blame] | 1087 | key = dm_shift_arg(as); |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1088 | if (!key) { |
| 1089 | rs->ti->error = "Not enough raid parameters given"; |
| 1090 | return -EINVAL; |
| 1091 | } |
Heinz Mauelshagen | 92c83d7 | 2016-05-19 18:49:25 +0200 | [diff] [blame] | 1092 | |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 1093 | if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_NOSYNC))) { |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 1094 | if (test_and_set_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) { |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1095 | rs->ti->error = "Only one 'nosync' argument allowed"; |
| 1096 | return -EINVAL; |
| 1097 | } |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1098 | rs->md.recovery_cp = MaxSector; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1099 | continue; |
| 1100 | } |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 1101 | if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_SYNC))) { |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 1102 | if (test_and_set_bit(__CTR_FLAG_SYNC, &rs->ctr_flags)) { |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1103 | rs->ti->error = "Only one 'sync' argument allowed"; |
| 1104 | return -EINVAL; |
| 1105 | } |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1106 | rs->md.recovery_cp = 0; |
Heinz Mauelshagen | 4763e54 | 2016-05-19 18:49:31 +0200 | [diff] [blame] | 1107 | continue; |
| 1108 | } |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 1109 | if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_USE_NEAR_SETS))) { |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 1110 | if (test_and_set_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags)) { |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1111 | rs->ti->error = "Only one 'raid10_use_new_sets' argument allowed"; |
| 1112 | return -EINVAL; |
| 1113 | } |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1114 | continue; |
| 1115 | } |
| 1116 | |
Heinz Mauelshagen | 92c83d7 | 2016-05-19 18:49:25 +0200 | [diff] [blame] | 1117 | arg = dm_shift_arg(as); |
| 1118 | i++; /* Account for the argument pairs */ |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1119 | if (!arg) { |
| 1120 | rs->ti->error = "Wrong number of raid parameters given"; |
| 1121 | return -EINVAL; |
| 1122 | } |
Jonathan Brassow | 63f33b8d | 2012-07-31 21:44:26 -0500 | [diff] [blame] | 1123 | |
Heinz Mauelshagen | 702108d | 2016-05-19 18:49:26 +0200 | [diff] [blame] | 1124 | /* |
| 1125 | * Parameters that take a string value are checked here. |
| 1126 | */ |
| 1127 | |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 1128 | if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_FORMAT))) { |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 1129 | if (test_and_set_bit(__CTR_FLAG_RAID10_FORMAT, &rs->ctr_flags)) { |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1130 | rs->ti->error = "Only one 'raid10_format' argument pair allowed"; |
| 1131 | return -EINVAL; |
| 1132 | } |
| 1133 | if (!rt_is_raid10(rt)) { |
| 1134 | rs->ti->error = "'raid10_format' is an invalid parameter for this RAID type"; |
| 1135 | return -EINVAL; |
| 1136 | } |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 1137 | raid10_format = raid10_name_to_format(arg); |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1138 | if (raid10_format < 0) { |
| 1139 | rs->ti->error = "Invalid 'raid10_format' value given"; |
| 1140 | return raid10_format; |
| 1141 | } |
Jonathan Brassow | 63f33b8d | 2012-07-31 21:44:26 -0500 | [diff] [blame] | 1142 | continue; |
| 1143 | } |
| 1144 | |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 1145 | if (kstrtoint(arg, 10, &value) < 0) { |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1146 | rs->ti->error = "Bad numerical argument given in raid params"; |
| 1147 | return -EINVAL; |
| 1148 | } |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1149 | |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 1150 | if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_REBUILD))) { |
Heinz Mauelshagen | 702108d | 2016-05-19 18:49:26 +0200 | [diff] [blame] | 1151 | /* |
| 1152 | * "rebuild" is being passed in by userspace to provide |
| 1153 | * indexes of replaced devices and to set up additional |
| 1154 | * devices on raid level takeover. |
Mike Snitzer | 4315784 | 2016-05-30 13:03:37 -0400 | [diff] [blame] | 1155 | */ |
Mike Snitzer | bb91a63 | 2016-06-02 12:06:54 -0400 | [diff] [blame] | 1156 | if (!__within_range(value, 0, rs->raid_disks - 1)) { |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1157 | rs->ti->error = "Invalid rebuild index given"; |
| 1158 | return -EINVAL; |
| 1159 | } |
Heinz Mauelshagen | 702108d | 2016-05-19 18:49:26 +0200 | [diff] [blame] | 1160 | |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1161 | if (test_and_set_bit(value, (void *) rs->rebuild_disks)) { |
| 1162 | rs->ti->error = "rebuild for this index already given"; |
| 1163 | return -EINVAL; |
| 1164 | } |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 1165 | |
Heinz Mauelshagen | 702108d | 2016-05-19 18:49:26 +0200 | [diff] [blame] | 1166 | rd = rs->dev + value; |
| 1167 | clear_bit(In_sync, &rd->rdev.flags); |
| 1168 | clear_bit(Faulty, &rd->rdev.flags); |
| 1169 | rd->rdev.recovery_offset = 0; |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 1170 | set_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags); |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 1171 | } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_WRITE_MOSTLY))) { |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1172 | if (!rt_is_raid1(rt)) { |
| 1173 | rs->ti->error = "write_mostly option is only valid for RAID1"; |
| 1174 | return -EINVAL; |
| 1175 | } |
Heinz Mauelshagen | 702108d | 2016-05-19 18:49:26 +0200 | [diff] [blame] | 1176 | |
Mike Snitzer | bb91a63 | 2016-06-02 12:06:54 -0400 | [diff] [blame] | 1177 | if (!__within_range(value, 0, rs->md.raid_disks - 1)) { |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1178 | rs->ti->error = "Invalid write_mostly index given"; |
| 1179 | return -EINVAL; |
| 1180 | } |
Heinz Mauelshagen | 702108d | 2016-05-19 18:49:26 +0200 | [diff] [blame] | 1181 | |
Heinz Mauelshagen | 5fa146b | 2016-06-15 18:50:18 +0200 | [diff] [blame] | 1182 | write_mostly++; |
Jonathan Brassow | 46bed2b | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1183 | set_bit(WriteMostly, &rs->dev[value].rdev.flags); |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 1184 | set_bit(__CTR_FLAG_WRITE_MOSTLY, &rs->ctr_flags); |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 1185 | } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MAX_WRITE_BEHIND))) { |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1186 | if (!rt_is_raid1(rt)) { |
| 1187 | rs->ti->error = "max_write_behind option is only valid for RAID1"; |
| 1188 | return -EINVAL; |
| 1189 | } |
Heinz Mauelshagen | 702108d | 2016-05-19 18:49:26 +0200 | [diff] [blame] | 1190 | |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 1191 | if (test_and_set_bit(__CTR_FLAG_MAX_WRITE_BEHIND, &rs->ctr_flags)) { |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1192 | rs->ti->error = "Only one max_write_behind argument pair allowed"; |
| 1193 | return -EINVAL; |
| 1194 | } |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1195 | |
| 1196 | /* |
| 1197 | * In device-mapper, we specify things in sectors, but |
| 1198 | * MD records this value in kB |
| 1199 | */ |
| 1200 | value /= 2; |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1201 | if (value > COUNTER_MAX) { |
| 1202 | rs->ti->error = "Max write-behind limit out of range"; |
| 1203 | return -EINVAL; |
| 1204 | } |
Heinz Mauelshagen | 702108d | 2016-05-19 18:49:26 +0200 | [diff] [blame] | 1205 | |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1206 | rs->md.bitmap_info.max_write_behind = value; |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 1207 | } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DAEMON_SLEEP))) { |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 1208 | if (test_and_set_bit(__CTR_FLAG_DAEMON_SLEEP, &rs->ctr_flags)) { |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1209 | rs->ti->error = "Only one daemon_sleep argument pair allowed"; |
| 1210 | return -EINVAL; |
| 1211 | } |
| 1212 | if (!value || (value > MAX_SCHEDULE_TIMEOUT)) { |
| 1213 | rs->ti->error = "daemon sleep period out of range"; |
| 1214 | return -EINVAL; |
| 1215 | } |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1216 | rs->md.bitmap_info.daemon_sleep = value; |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 1217 | } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DATA_OFFSET))) { |
Heinz Mauelshagen | 4763e54 | 2016-05-19 18:49:31 +0200 | [diff] [blame] | 1218 | /* Userspace passes new data_offset after having extended the the data image LV */ |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 1219 | if (test_and_set_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags)) { |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1220 | rs->ti->error = "Only one data_offset argument pair allowed"; |
| 1221 | return -EINVAL; |
| 1222 | } |
Heinz Mauelshagen | 4763e54 | 2016-05-19 18:49:31 +0200 | [diff] [blame] | 1223 | /* Ensure sensible data offset */ |
Heinz Mauelshagen | 75dd3b9 | 2016-06-15 22:27:08 +0200 | [diff] [blame^] | 1224 | if (value < 0 || |
| 1225 | (value && (value < MIN_FREE_RESHAPE_SPACE || value % to_sector(PAGE_SIZE)))) { |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1226 | rs->ti->error = "Bogus data_offset value"; |
| 1227 | return -EINVAL; |
| 1228 | } |
Heinz Mauelshagen | 4763e54 | 2016-05-19 18:49:31 +0200 | [diff] [blame] | 1229 | rs->data_offset = value; |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 1230 | } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DELTA_DISKS))) { |
Heinz Mauelshagen | 4763e54 | 2016-05-19 18:49:31 +0200 | [diff] [blame] | 1231 | /* Define the +/-# of disks to add to/remove from the given raid set */ |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 1232 | if (test_and_set_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags)) { |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1233 | rs->ti->error = "Only one delta_disks argument pair allowed"; |
| 1234 | return -EINVAL; |
| 1235 | } |
Heinz Mauelshagen | 4763e54 | 2016-05-19 18:49:31 +0200 | [diff] [blame] | 1236 | /* Ensure MAX_RAID_DEVICES and raid type minimal_devs! */ |
Mike Snitzer | bb91a63 | 2016-06-02 12:06:54 -0400 | [diff] [blame] | 1237 | if (!__within_range(abs(value), 1, MAX_RAID_DEVICES - rt->minimal_devs)) { |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1238 | rs->ti->error = "Too many delta_disk requested"; |
| 1239 | return -EINVAL; |
| 1240 | } |
Heinz Mauelshagen | 4763e54 | 2016-05-19 18:49:31 +0200 | [diff] [blame] | 1241 | |
| 1242 | rs->delta_disks = value; |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 1243 | } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_STRIPE_CACHE))) { |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 1244 | if (test_and_set_bit(__CTR_FLAG_STRIPE_CACHE, &rs->ctr_flags)) { |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1245 | rs->ti->error = "Only one stripe_cache argument pair allowed"; |
| 1246 | return -EINVAL; |
| 1247 | } |
| 1248 | |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1249 | if (!rt_is_raid456(rt)) { |
| 1250 | rs->ti->error = "Inappropriate argument: stripe_cache"; |
| 1251 | return -EINVAL; |
| 1252 | } |
Heinz Mauelshagen | 702108d | 2016-05-19 18:49:26 +0200 | [diff] [blame] | 1253 | |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 1254 | rs->stripe_cache_entries = value; |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 1255 | } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MIN_RECOVERY_RATE))) { |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 1256 | if (test_and_set_bit(__CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags)) { |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1257 | rs->ti->error = "Only one min_recovery_rate argument pair allowed"; |
| 1258 | return -EINVAL; |
| 1259 | } |
| 1260 | if (value > INT_MAX) { |
| 1261 | rs->ti->error = "min_recovery_rate out of range"; |
| 1262 | return -EINVAL; |
| 1263 | } |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1264 | rs->md.sync_speed_min = (int)value; |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 1265 | } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MAX_RECOVERY_RATE))) { |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 1266 | if (test_and_set_bit(__CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags)) { |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1267 | rs->ti->error = "Only one max_recovery_rate argument pair allowed"; |
| 1268 | return -EINVAL; |
| 1269 | } |
| 1270 | if (value > INT_MAX) { |
| 1271 | rs->ti->error = "max_recovery_rate out of range"; |
| 1272 | return -EINVAL; |
| 1273 | } |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1274 | rs->md.sync_speed_max = (int)value; |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 1275 | } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_REGION_SIZE))) { |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 1276 | if (test_and_set_bit(__CTR_FLAG_REGION_SIZE, &rs->ctr_flags)) { |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1277 | rs->ti->error = "Only one region_size argument pair allowed"; |
| 1278 | return -EINVAL; |
| 1279 | } |
Heinz Mauelshagen | 702108d | 2016-05-19 18:49:26 +0200 | [diff] [blame] | 1280 | |
Jonathan Brassow | c108456 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1281 | region_size = value; |
Heinz Mauelshagen | 4257e08 | 2016-06-14 01:46:03 +0200 | [diff] [blame] | 1282 | rs->requested_bitmap_chunk_sectors = value; |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 1283 | } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_COPIES))) { |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 1284 | if (test_and_set_bit(__CTR_FLAG_RAID10_COPIES, &rs->ctr_flags)) { |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1285 | rs->ti->error = "Only one raid10_copies argument pair allowed"; |
| 1286 | return -EINVAL; |
| 1287 | } |
Heinz Mauelshagen | 702108d | 2016-05-19 18:49:26 +0200 | [diff] [blame] | 1288 | |
Mike Snitzer | bb91a63 | 2016-06-02 12:06:54 -0400 | [diff] [blame] | 1289 | if (!__within_range(value, 2, rs->md.raid_disks)) { |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1290 | rs->ti->error = "Bad value for 'raid10_copies'"; |
| 1291 | return -EINVAL; |
| 1292 | } |
Heinz Mauelshagen | 702108d | 2016-05-19 18:49:26 +0200 | [diff] [blame] | 1293 | |
Jonathan Brassow | 63f33b8d | 2012-07-31 21:44:26 -0500 | [diff] [blame] | 1294 | raid10_copies = value; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1295 | } else { |
| 1296 | DMERR("Unable to parse RAID parameter: %s", key); |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1297 | rs->ti->error = "Unable to parse RAID parameter"; |
| 1298 | return -EINVAL; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1299 | } |
| 1300 | } |
| 1301 | |
Heinz Mauelshagen | 0d851d1 | 2016-06-15 18:43:55 +0200 | [diff] [blame] | 1302 | if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags) && |
| 1303 | test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) { |
| 1304 | rs->ti->error = "sync and nosync are mutually exclusive"; |
| 1305 | return -EINVAL; |
| 1306 | } |
| 1307 | |
Heinz Mauelshagen | 5fa146b | 2016-06-15 18:50:18 +0200 | [diff] [blame] | 1308 | if (write_mostly >= rs->md.raid_disks) { |
| 1309 | rs->ti->error = "Can't set all raid1 devices to write_mostly"; |
| 1310 | return -EINVAL; |
| 1311 | } |
| 1312 | |
Jonathan Brassow | c108456 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1313 | if (validate_region_size(rs, region_size)) |
| 1314 | return -EINVAL; |
| 1315 | |
| 1316 | if (rs->md.chunk_sectors) |
Mike Snitzer | 542f903 | 2012-07-27 15:08:00 +0100 | [diff] [blame] | 1317 | max_io_len = rs->md.chunk_sectors; |
Jonathan Brassow | c108456 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1318 | else |
Mike Snitzer | 542f903 | 2012-07-27 15:08:00 +0100 | [diff] [blame] | 1319 | max_io_len = region_size; |
Jonathan Brassow | c108456 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1320 | |
Mike Snitzer | 542f903 | 2012-07-27 15:08:00 +0100 | [diff] [blame] | 1321 | if (dm_set_target_max_io_len(rs->ti, max_io_len)) |
| 1322 | return -EINVAL; |
Jonathan Brassow | 3273727 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1323 | |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 1324 | if (rt_is_raid10(rt)) { |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1325 | if (raid10_copies > rs->md.raid_disks) { |
| 1326 | rs->ti->error = "Not enough devices to satisfy specification"; |
| 1327 | return -EINVAL; |
| 1328 | } |
Jonathan Brassow | 63f33b8d | 2012-07-31 21:44:26 -0500 | [diff] [blame] | 1329 | |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 1330 | rs->md.new_layout = raid10_format_to_md_layout(rs, raid10_format, raid10_copies); |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1331 | if (rs->md.new_layout < 0) { |
| 1332 | rs->ti->error = "Error getting raid10 format"; |
| 1333 | return rs->md.new_layout; |
| 1334 | } |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 1335 | |
| 1336 | rt = get_raid_type_by_ll(10, rs->md.new_layout); |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1337 | if (!rt) { |
| 1338 | rs->ti->error = "Failed to recognize new raid10 layout"; |
| 1339 | return -EINVAL; |
| 1340 | } |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 1341 | |
| 1342 | if ((rt->algorithm == ALGORITHM_RAID10_DEFAULT || |
| 1343 | rt->algorithm == ALGORITHM_RAID10_NEAR) && |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 1344 | test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags)) { |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1345 | rs->ti->error = "RAID10 format 'near' and 'raid10_use_near_sets' are incompatible"; |
| 1346 | return -EINVAL; |
| 1347 | } |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1348 | } |
Heinz Mauelshagen | 702108d | 2016-05-19 18:49:26 +0200 | [diff] [blame] | 1349 | |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 1350 | rs->raid10_copies = raid10_copies; |
Jonathan E Brassow | c039c33 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 1351 | |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1352 | /* Assume there are no metadata devices until the drives are parsed */ |
| 1353 | rs->md.persistent = 0; |
| 1354 | rs->md.external = 1; |
| 1355 | |
Heinz Mauelshagen | f090279 | 2016-05-19 18:49:27 +0200 | [diff] [blame] | 1356 | /* Check, if any invalid ctr arguments have been passed in for the raid level */ |
Heinz Mauelshagen | a30cbc0 | 2016-06-09 16:42:16 +0200 | [diff] [blame] | 1357 | return rs_check_for_valid_flags(rs); |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 1360 | /* Set raid4/5/6 cache size */ |
| 1361 | static int rs_set_raid456_stripe_cache(struct raid_set *rs) |
| 1362 | { |
| 1363 | int r; |
| 1364 | struct r5conf *conf; |
| 1365 | struct mddev *mddev = &rs->md; |
| 1366 | uint32_t min_stripes = max(mddev->chunk_sectors, mddev->new_chunk_sectors) / 2; |
| 1367 | uint32_t nr_stripes = rs->stripe_cache_entries; |
| 1368 | |
| 1369 | if (!rt_is_raid456(rs->raid_type)) { |
| 1370 | rs->ti->error = "Inappropriate raid level; cannot change stripe_cache size"; |
| 1371 | return -EINVAL; |
| 1372 | } |
| 1373 | |
| 1374 | if (nr_stripes < min_stripes) { |
| 1375 | DMINFO("Adjusting requested %u stripe cache entries to %u to suit stripe size", |
| 1376 | nr_stripes, min_stripes); |
| 1377 | nr_stripes = min_stripes; |
| 1378 | } |
| 1379 | |
| 1380 | conf = mddev->private; |
| 1381 | if (!conf) { |
| 1382 | rs->ti->error = "Cannot change stripe_cache size on inactive RAID set"; |
| 1383 | return -EINVAL; |
| 1384 | } |
| 1385 | |
| 1386 | /* Try setting number of stripes in raid456 stripe cache */ |
| 1387 | if (conf->min_nr_stripes != nr_stripes) { |
| 1388 | r = raid5_set_cache_size(mddev, nr_stripes); |
| 1389 | if (r) { |
| 1390 | rs->ti->error = "Failed to set raid4/5/6 stripe cache size"; |
| 1391 | return r; |
| 1392 | } |
| 1393 | |
| 1394 | DMINFO("%u stripe cache entries", nr_stripes); |
| 1395 | } |
| 1396 | |
| 1397 | return 0; |
| 1398 | } |
| 1399 | |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 1400 | /* Return # of data stripes as kept in mddev as of @rs (i.e. as of superblock) */ |
| 1401 | static unsigned int mddev_data_stripes(struct raid_set *rs) |
| 1402 | { |
| 1403 | return rs->md.raid_disks - rs->raid_type->parity_devs; |
| 1404 | } |
| 1405 | |
Heinz Mauelshagen | 40ba37e | 2016-06-13 17:55:13 +0200 | [diff] [blame] | 1406 | /* Return # of data stripes of @rs (i.e. as of ctr) */ |
| 1407 | static unsigned int rs_data_stripes(struct raid_set *rs) |
| 1408 | { |
| 1409 | return rs->raid_disks - rs->raid_type->parity_devs; |
| 1410 | } |
| 1411 | |
| 1412 | /* Calculate the sectors per device and per array used for @rs */ |
| 1413 | static int rs_set_dev_and_array_sectors(struct raid_set *rs, bool use_mddev) |
| 1414 | { |
| 1415 | int delta_disks; |
| 1416 | unsigned int data_stripes; |
| 1417 | struct mddev *mddev = &rs->md; |
| 1418 | struct md_rdev *rdev; |
| 1419 | sector_t array_sectors = rs->ti->len, dev_sectors = rs->ti->len; |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 1420 | sector_t cur_dev_sectors = rs->dev[0].rdev.sectors; |
Heinz Mauelshagen | 40ba37e | 2016-06-13 17:55:13 +0200 | [diff] [blame] | 1421 | |
| 1422 | if (use_mddev) { |
| 1423 | delta_disks = mddev->delta_disks; |
| 1424 | data_stripes = mddev_data_stripes(rs); |
| 1425 | } else { |
| 1426 | delta_disks = rs->delta_disks; |
| 1427 | data_stripes = rs_data_stripes(rs); |
| 1428 | } |
| 1429 | |
| 1430 | /* Special raid1 case w/o delta_disks support (yet) */ |
| 1431 | if (rt_is_raid1(rs->raid_type)) |
| 1432 | ; |
| 1433 | else if (rt_is_raid10(rs->raid_type)) { |
| 1434 | if (rs->raid10_copies < 2 || |
| 1435 | delta_disks < 0) { |
| 1436 | rs->ti->error = "Bogus raid10 data copies or delta disks"; |
| 1437 | return EINVAL; |
| 1438 | } |
| 1439 | |
| 1440 | dev_sectors *= rs->raid10_copies; |
| 1441 | if (sector_div(dev_sectors, data_stripes)) |
| 1442 | goto bad; |
| 1443 | |
| 1444 | array_sectors = (data_stripes + delta_disks) * dev_sectors; |
| 1445 | if (sector_div(array_sectors, rs->raid10_copies)) |
| 1446 | goto bad; |
| 1447 | |
| 1448 | } else if (sector_div(dev_sectors, data_stripes)) |
| 1449 | goto bad; |
| 1450 | |
| 1451 | else |
| 1452 | /* Striped layouts */ |
| 1453 | array_sectors = (data_stripes + delta_disks) * dev_sectors; |
| 1454 | |
| 1455 | rdev_for_each(rdev, mddev) |
| 1456 | rdev->sectors = dev_sectors; |
| 1457 | |
| 1458 | mddev->array_sectors = array_sectors; |
| 1459 | mddev->dev_sectors = dev_sectors; |
| 1460 | |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 1461 | if (!rs_is_raid0(rs) && dev_sectors > cur_dev_sectors) |
| 1462 | mddev->recovery_cp = dev_sectors; |
| 1463 | |
Heinz Mauelshagen | 40ba37e | 2016-06-13 17:55:13 +0200 | [diff] [blame] | 1464 | return 0; |
| 1465 | bad: |
| 1466 | rs->ti->error = "Target length not divisible by number of data devices"; |
| 1467 | return EINVAL; |
| 1468 | } |
| 1469 | |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1470 | static void do_table_event(struct work_struct *ws) |
| 1471 | { |
| 1472 | struct raid_set *rs = container_of(ws, struct raid_set, md.event_work); |
| 1473 | |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 1474 | rs_set_capacity(rs); |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1475 | dm_table_event(rs->ti->table); |
| 1476 | } |
| 1477 | |
| 1478 | static int raid_is_congested(struct dm_target_callbacks *cb, int bits) |
| 1479 | { |
| 1480 | struct raid_set *rs = container_of(cb, struct raid_set, callbacks); |
| 1481 | |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 1482 | return mddev_congested(&rs->md, bits); |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 1485 | /* |
| 1486 | * Make sure a valid takover (level switch) is being requested on @rs |
| 1487 | * |
| 1488 | * Conversions of raid sets from one MD personality to another |
| 1489 | * have to conform to restrictions which are enforced here. |
| 1490 | * |
| 1491 | * Degration is already checked for in rs_check_conversion() below. |
| 1492 | */ |
| 1493 | static int rs_check_takeover(struct raid_set *rs) |
| 1494 | { |
| 1495 | struct mddev *mddev = &rs->md; |
| 1496 | unsigned int near_copies; |
| 1497 | |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 1498 | if (rs->md.degraded) { |
| 1499 | rs->ti->error = "Can't takeover degraded raid set"; |
| 1500 | return -EPERM; |
| 1501 | } |
| 1502 | |
| 1503 | if (rs_is_reshaping(rs)) { |
| 1504 | rs->ti->error = "Can't takeover reshaping raid set"; |
| 1505 | return -EPERM; |
| 1506 | } |
| 1507 | |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 1508 | switch (mddev->level) { |
| 1509 | case 0: |
| 1510 | /* raid0 -> raid1/5 with one disk */ |
| 1511 | if ((mddev->new_level == 1 || mddev->new_level == 5) && |
| 1512 | mddev->raid_disks == 1) |
| 1513 | return 0; |
| 1514 | |
| 1515 | /* raid0 -> raid10 */ |
| 1516 | if (mddev->new_level == 10 && |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 1517 | !(rs->raid_disks % mddev->raid_disks)) |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 1518 | return 0; |
| 1519 | |
| 1520 | /* raid0 with multiple disks -> raid4/5/6 */ |
Mike Snitzer | bb91a63 | 2016-06-02 12:06:54 -0400 | [diff] [blame] | 1521 | if (__within_range(mddev->new_level, 4, 6) && |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 1522 | mddev->new_layout == ALGORITHM_PARITY_N && |
| 1523 | mddev->raid_disks > 1) |
| 1524 | return 0; |
| 1525 | |
| 1526 | break; |
| 1527 | |
| 1528 | case 10: |
| 1529 | /* Can't takeover raid10_offset! */ |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 1530 | if (__is_raid10_offset(mddev->layout)) |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 1531 | break; |
| 1532 | |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 1533 | near_copies = __raid10_near_copies(mddev->layout); |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 1534 | |
| 1535 | /* raid10* -> raid0 */ |
| 1536 | if (mddev->new_level == 0) { |
| 1537 | /* Can takeover raid10_near with raid disks divisable by data copies! */ |
| 1538 | if (near_copies > 1 && |
| 1539 | !(mddev->raid_disks % near_copies)) { |
| 1540 | mddev->raid_disks /= near_copies; |
| 1541 | mddev->delta_disks = mddev->raid_disks; |
| 1542 | return 0; |
| 1543 | } |
| 1544 | |
| 1545 | /* Can takeover raid10_far */ |
| 1546 | if (near_copies == 1 && |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 1547 | __raid10_far_copies(mddev->layout) > 1) |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 1548 | return 0; |
| 1549 | |
| 1550 | break; |
| 1551 | } |
| 1552 | |
| 1553 | /* raid10_{near,far} -> raid1 */ |
| 1554 | if (mddev->new_level == 1 && |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 1555 | max(near_copies, __raid10_far_copies(mddev->layout)) == mddev->raid_disks) |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 1556 | return 0; |
| 1557 | |
| 1558 | /* raid10_{near,far} with 2 disks -> raid4/5 */ |
Mike Snitzer | bb91a63 | 2016-06-02 12:06:54 -0400 | [diff] [blame] | 1559 | if (__within_range(mddev->new_level, 4, 5) && |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 1560 | mddev->raid_disks == 2) |
| 1561 | return 0; |
| 1562 | break; |
| 1563 | |
| 1564 | case 1: |
| 1565 | /* raid1 with 2 disks -> raid4/5 */ |
Mike Snitzer | bb91a63 | 2016-06-02 12:06:54 -0400 | [diff] [blame] | 1566 | if (__within_range(mddev->new_level, 4, 5) && |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 1567 | mddev->raid_disks == 2) { |
| 1568 | mddev->degraded = 1; |
| 1569 | return 0; |
| 1570 | } |
| 1571 | |
| 1572 | /* raid1 -> raid0 */ |
| 1573 | if (mddev->new_level == 0 && |
| 1574 | mddev->raid_disks == 1) |
| 1575 | return 0; |
| 1576 | |
| 1577 | /* raid1 -> raid10 */ |
| 1578 | if (mddev->new_level == 10) |
| 1579 | return 0; |
| 1580 | |
| 1581 | break; |
| 1582 | |
| 1583 | case 4: |
| 1584 | /* raid4 -> raid0 */ |
| 1585 | if (mddev->new_level == 0) |
| 1586 | return 0; |
| 1587 | |
| 1588 | /* raid4 -> raid1/5 with 2 disks */ |
| 1589 | if ((mddev->new_level == 1 || mddev->new_level == 5) && |
| 1590 | mddev->raid_disks == 2) |
| 1591 | return 0; |
| 1592 | |
| 1593 | /* raid4 -> raid5/6 with parity N */ |
Mike Snitzer | bb91a63 | 2016-06-02 12:06:54 -0400 | [diff] [blame] | 1594 | if (__within_range(mddev->new_level, 5, 6) && |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 1595 | mddev->layout == ALGORITHM_PARITY_N) |
| 1596 | return 0; |
| 1597 | break; |
| 1598 | |
| 1599 | case 5: |
| 1600 | /* raid5 with parity N -> raid0 */ |
| 1601 | if (mddev->new_level == 0 && |
| 1602 | mddev->layout == ALGORITHM_PARITY_N) |
| 1603 | return 0; |
| 1604 | |
| 1605 | /* raid5 with parity N -> raid4 */ |
| 1606 | if (mddev->new_level == 4 && |
| 1607 | mddev->layout == ALGORITHM_PARITY_N) |
| 1608 | return 0; |
| 1609 | |
| 1610 | /* raid5 with 2 disks -> raid1/4/10 */ |
| 1611 | if ((mddev->new_level == 1 || mddev->new_level == 4 || mddev->new_level == 10) && |
| 1612 | mddev->raid_disks == 2) |
| 1613 | return 0; |
| 1614 | |
| 1615 | /* raid5 with parity N -> raid6 with parity N */ |
| 1616 | if (mddev->new_level == 6 && |
| 1617 | ((mddev->layout == ALGORITHM_PARITY_N && mddev->new_layout == ALGORITHM_PARITY_N) || |
Mike Snitzer | bb91a63 | 2016-06-02 12:06:54 -0400 | [diff] [blame] | 1618 | __within_range(mddev->new_layout, ALGORITHM_LEFT_ASYMMETRIC_6, ALGORITHM_RIGHT_SYMMETRIC_6))) |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 1619 | return 0; |
| 1620 | break; |
| 1621 | |
| 1622 | case 6: |
| 1623 | /* raid6 with parity N -> raid0 */ |
| 1624 | if (mddev->new_level == 0 && |
| 1625 | mddev->layout == ALGORITHM_PARITY_N) |
| 1626 | return 0; |
| 1627 | |
| 1628 | /* raid6 with parity N -> raid4 */ |
| 1629 | if (mddev->new_level == 4 && |
| 1630 | mddev->layout == ALGORITHM_PARITY_N) |
| 1631 | return 0; |
| 1632 | |
| 1633 | /* raid6_*_n with parity N -> raid5_* */ |
| 1634 | if (mddev->new_level == 5 && |
| 1635 | ((mddev->layout == ALGORITHM_PARITY_N && mddev->new_layout == ALGORITHM_PARITY_N) || |
Mike Snitzer | bb91a63 | 2016-06-02 12:06:54 -0400 | [diff] [blame] | 1636 | __within_range(mddev->new_layout, ALGORITHM_LEFT_ASYMMETRIC, ALGORITHM_RIGHT_SYMMETRIC))) |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 1637 | return 0; |
| 1638 | |
| 1639 | default: |
| 1640 | break; |
| 1641 | } |
| 1642 | |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 1643 | rs->ti->error = "takeover not possible"; |
| 1644 | return -EINVAL; |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | /* True if @rs requested to be taken over */ |
| 1648 | static bool rs_takeover_requested(struct raid_set *rs) |
| 1649 | { |
| 1650 | return rs->md.new_level != rs->md.level; |
| 1651 | } |
| 1652 | |
Heinz Mauelshagen | 40ba37e | 2016-06-13 17:55:13 +0200 | [diff] [blame] | 1653 | /* True if @rs is requested to reshape by ctr */ |
| 1654 | static bool rs_reshape_requested(struct raid_set *rs) |
| 1655 | { |
| 1656 | struct mddev *mddev = &rs->md; |
| 1657 | |
| 1658 | if (!mddev->level) |
| 1659 | return false; |
| 1660 | |
| 1661 | return !__is_raid10_far(mddev->new_layout) && |
| 1662 | mddev->new_level == mddev->level && |
| 1663 | (mddev->new_layout != mddev->layout || |
| 1664 | mddev->new_chunk_sectors != mddev->chunk_sectors || |
| 1665 | rs->raid_disks + rs->delta_disks != mddev->raid_disks); |
| 1666 | } |
| 1667 | |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 1668 | /* Features */ |
Mike Snitzer | 9b6e542 | 2016-06-02 11:48:09 -0400 | [diff] [blame] | 1669 | #define FEATURE_FLAG_SUPPORTS_V190 0x1 /* Supports extended superblock */ |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 1670 | |
| 1671 | /* State flags for sb->flags */ |
| 1672 | #define SB_FLAG_RESHAPE_ACTIVE 0x1 |
| 1673 | #define SB_FLAG_RESHAPE_BACKWARDS 0x2 |
| 1674 | |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1675 | /* |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1676 | * This structure is never routinely used by userspace, unlike md superblocks. |
| 1677 | * Devices with this superblock should only ever be accessed via device-mapper. |
| 1678 | */ |
| 1679 | #define DM_RAID_MAGIC 0x64526D44 |
| 1680 | struct dm_raid_superblock { |
| 1681 | __le32 magic; /* "DmRd" */ |
Mike Snitzer | 9b6e542 | 2016-06-02 11:48:09 -0400 | [diff] [blame] | 1682 | __le32 compat_features; /* Used to indicate compatible features (like 1.9.0 ondisk metadata extension) */ |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1683 | |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 1684 | __le32 num_devices; /* Number of devices in this raid set. (Max 64) */ |
| 1685 | __le32 array_position; /* The position of this drive in the raid set */ |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1686 | |
| 1687 | __le64 events; /* Incremented by md when superblock updated */ |
Mike Snitzer | 9b6e542 | 2016-06-02 11:48:09 -0400 | [diff] [blame] | 1688 | __le64 failed_devices; /* Pre 1.9.0 part of bit field of devices to */ |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 1689 | /* indicate failures (see extension below) */ |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1690 | |
| 1691 | /* |
| 1692 | * This offset tracks the progress of the repair or replacement of |
| 1693 | * an individual drive. |
| 1694 | */ |
| 1695 | __le64 disk_recovery_offset; |
| 1696 | |
| 1697 | /* |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 1698 | * This offset tracks the progress of the initial raid set |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1699 | * synchronisation/parity calculation. |
| 1700 | */ |
| 1701 | __le64 array_resync_offset; |
| 1702 | |
| 1703 | /* |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 1704 | * raid characteristics |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1705 | */ |
| 1706 | __le32 level; |
| 1707 | __le32 layout; |
| 1708 | __le32 stripe_sectors; |
| 1709 | |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 1710 | /******************************************************************** |
Mike Snitzer | 9b6e542 | 2016-06-02 11:48:09 -0400 | [diff] [blame] | 1711 | * BELOW FOLLOW V1.9.0 EXTENSIONS TO THE PRISTINE SUPERBLOCK FORMAT!!! |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 1712 | * |
Mike Snitzer | 9b6e542 | 2016-06-02 11:48:09 -0400 | [diff] [blame] | 1713 | * FEATURE_FLAG_SUPPORTS_V190 in the features member indicates that those exist |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 1714 | */ |
| 1715 | |
| 1716 | __le32 flags; /* Flags defining array states for reshaping */ |
| 1717 | |
| 1718 | /* |
| 1719 | * This offset tracks the progress of a raid |
| 1720 | * set reshape in order to be able to restart it |
| 1721 | */ |
| 1722 | __le64 reshape_position; |
| 1723 | |
| 1724 | /* |
| 1725 | * These define the properties of the array in case of an interrupted reshape |
| 1726 | */ |
| 1727 | __le32 new_level; |
| 1728 | __le32 new_layout; |
| 1729 | __le32 new_stripe_sectors; |
| 1730 | __le32 delta_disks; |
| 1731 | |
| 1732 | __le64 array_sectors; /* Array size in sectors */ |
| 1733 | |
| 1734 | /* |
| 1735 | * Sector offsets to data on devices (reshaping). |
| 1736 | * Needed to support out of place reshaping, thus |
| 1737 | * not writing over any stripes whilst converting |
| 1738 | * them from old to new layout |
| 1739 | */ |
| 1740 | __le64 data_offset; |
| 1741 | __le64 new_data_offset; |
| 1742 | |
| 1743 | __le64 sectors; /* Used device size in sectors */ |
| 1744 | |
| 1745 | /* |
| 1746 | * Additonal Bit field of devices indicating failures to support |
Mike Snitzer | 9b6e542 | 2016-06-02 11:48:09 -0400 | [diff] [blame] | 1747 | * up to 256 devices with the 1.9.0 on-disk metadata format |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 1748 | */ |
| 1749 | __le64 extended_failed_devices[DISKS_ARRAY_ELEMS - 1]; |
| 1750 | |
| 1751 | __le32 incompat_features; /* Used to indicate any incompatible features */ |
| 1752 | |
| 1753 | /* Always set rest up to logical block size to 0 when writing (see get_metadata_device() below). */ |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1754 | } __packed; |
| 1755 | |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 1756 | /* |
| 1757 | * Check for reshape constraints on raid set @rs: |
| 1758 | * |
| 1759 | * - reshape function non-existent |
| 1760 | * - degraded set |
| 1761 | * - ongoing recovery |
| 1762 | * - ongoing reshape |
| 1763 | * |
| 1764 | * Returns 0 if none or -EPERM if given constraint |
| 1765 | * and error message reference in @errmsg |
| 1766 | */ |
| 1767 | static int rs_check_reshape(struct raid_set *rs) |
| 1768 | { |
| 1769 | struct mddev *mddev = &rs->md; |
| 1770 | |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 1771 | if (!mddev->pers || !mddev->pers->check_reshape) |
| 1772 | rs->ti->error = "Reshape not supported"; |
| 1773 | else if (mddev->degraded) |
| 1774 | rs->ti->error = "Can't reshape degraded raid set"; |
| 1775 | else if (rs_is_recovering(rs)) |
| 1776 | rs->ti->error = "Convert request on recovering raid set prohibited"; |
| 1777 | else if (mddev->reshape_position && rs_is_reshaping(rs)) |
| 1778 | rs->ti->error = "raid set already reshaping!"; |
| 1779 | else if (!(rs_is_raid10(rs) || rs_is_raid456(rs))) |
| 1780 | rs->ti->error = "Reshaping only supported for raid4/5/6/10"; |
| 1781 | else |
| 1782 | return 0; |
| 1783 | |
| 1784 | return -EPERM; |
| 1785 | } |
| 1786 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1787 | static int read_disk_sb(struct md_rdev *rdev, int size) |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1788 | { |
| 1789 | BUG_ON(!rdev->sb_page); |
| 1790 | |
| 1791 | if (rdev->sb_loaded) |
| 1792 | return 0; |
| 1793 | |
Heinz Mauelshagen | 0a7b818 | 2016-06-15 18:45:56 +0200 | [diff] [blame] | 1794 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true)) { |
Jonathan E Brassow | 0447568 | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 1795 | DMERR("Failed to read superblock of device at position %d", |
| 1796 | rdev->raid_disk); |
Jonathan Brassow | c32fb9e | 2012-05-22 13:55:31 +1000 | [diff] [blame] | 1797 | md_error(rdev->mddev, rdev); |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1798 | return -EINVAL; |
| 1799 | } |
| 1800 | |
| 1801 | rdev->sb_loaded = 1; |
| 1802 | |
| 1803 | return 0; |
| 1804 | } |
| 1805 | |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 1806 | static void sb_retrieve_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices) |
| 1807 | { |
| 1808 | failed_devices[0] = le64_to_cpu(sb->failed_devices); |
| 1809 | memset(failed_devices + 1, 0, sizeof(sb->extended_failed_devices)); |
| 1810 | |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 1811 | if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190) { |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 1812 | int i = ARRAY_SIZE(sb->extended_failed_devices); |
| 1813 | |
| 1814 | while (i--) |
| 1815 | failed_devices[i+1] = le64_to_cpu(sb->extended_failed_devices[i]); |
| 1816 | } |
| 1817 | } |
| 1818 | |
Heinz Mauelshagen | 7b34df7 | 2016-05-19 18:49:32 +0200 | [diff] [blame] | 1819 | static void sb_update_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices) |
| 1820 | { |
| 1821 | int i = ARRAY_SIZE(sb->extended_failed_devices); |
| 1822 | |
| 1823 | sb->failed_devices = cpu_to_le64(failed_devices[0]); |
| 1824 | while (i--) |
| 1825 | sb->extended_failed_devices[i] = cpu_to_le64(failed_devices[i+1]); |
| 1826 | } |
| 1827 | |
| 1828 | /* |
| 1829 | * Synchronize the superblock members with the raid set properties |
| 1830 | * |
| 1831 | * All superblock data is little endian. |
| 1832 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1833 | static void super_sync(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1834 | { |
Heinz Mauelshagen | 7b34df7 | 2016-05-19 18:49:32 +0200 | [diff] [blame] | 1835 | bool update_failed_devices = false; |
| 1836 | unsigned int i; |
| 1837 | uint64_t failed_devices[DISKS_ARRAY_ELEMS]; |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1838 | struct dm_raid_superblock *sb; |
Jonathan Brassow | 81f382f | 2012-05-22 13:55:30 +1000 | [diff] [blame] | 1839 | struct raid_set *rs = container_of(mddev, struct raid_set, md); |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1840 | |
Heinz Mauelshagen | 7b34df7 | 2016-05-19 18:49:32 +0200 | [diff] [blame] | 1841 | /* No metadata device, no superblock */ |
| 1842 | if (!rdev->meta_bdev) |
| 1843 | return; |
| 1844 | |
| 1845 | BUG_ON(!rdev->sb_page); |
| 1846 | |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1847 | sb = page_address(rdev->sb_page); |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1848 | |
Heinz Mauelshagen | 7b34df7 | 2016-05-19 18:49:32 +0200 | [diff] [blame] | 1849 | sb_retrieve_failed_devices(sb, failed_devices); |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1850 | |
Heinz Mauelshagen | 7b34df7 | 2016-05-19 18:49:32 +0200 | [diff] [blame] | 1851 | for (i = 0; i < rs->raid_disks; i++) |
| 1852 | if (!rs->dev[i].data_dev || test_bit(Faulty, &rs->dev[i].rdev.flags)) { |
| 1853 | update_failed_devices = true; |
| 1854 | set_bit(i, (void *) failed_devices); |
| 1855 | } |
| 1856 | |
| 1857 | if (update_failed_devices) |
| 1858 | sb_update_failed_devices(sb, failed_devices); |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1859 | |
| 1860 | sb->magic = cpu_to_le32(DM_RAID_MAGIC); |
Mike Snitzer | 9b6e542 | 2016-06-02 11:48:09 -0400 | [diff] [blame] | 1861 | sb->compat_features = cpu_to_le32(FEATURE_FLAG_SUPPORTS_V190); |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1862 | |
| 1863 | sb->num_devices = cpu_to_le32(mddev->raid_disks); |
| 1864 | sb->array_position = cpu_to_le32(rdev->raid_disk); |
| 1865 | |
| 1866 | sb->events = cpu_to_le64(mddev->events); |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1867 | |
| 1868 | sb->disk_recovery_offset = cpu_to_le64(rdev->recovery_offset); |
| 1869 | sb->array_resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1870 | |
| 1871 | sb->level = cpu_to_le32(mddev->level); |
| 1872 | sb->layout = cpu_to_le32(mddev->layout); |
| 1873 | sb->stripe_sectors = cpu_to_le32(mddev->chunk_sectors); |
Heinz Mauelshagen | 7b34df7 | 2016-05-19 18:49:32 +0200 | [diff] [blame] | 1874 | |
| 1875 | sb->new_level = cpu_to_le32(mddev->new_level); |
| 1876 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1877 | sb->new_stripe_sectors = cpu_to_le32(mddev->new_chunk_sectors); |
| 1878 | |
| 1879 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1880 | |
| 1881 | smp_rmb(); /* Make sure we access most recent reshape position */ |
| 1882 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1883 | if (le64_to_cpu(sb->reshape_position) != MaxSector) { |
| 1884 | /* Flag ongoing reshape */ |
| 1885 | sb->flags |= cpu_to_le32(SB_FLAG_RESHAPE_ACTIVE); |
| 1886 | |
| 1887 | if (mddev->delta_disks < 0 || mddev->reshape_backwards) |
| 1888 | sb->flags |= cpu_to_le32(SB_FLAG_RESHAPE_BACKWARDS); |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 1889 | } else { |
| 1890 | /* Clear reshape flags */ |
| 1891 | sb->flags &= ~(cpu_to_le32(SB_FLAG_RESHAPE_ACTIVE|SB_FLAG_RESHAPE_BACKWARDS)); |
| 1892 | } |
Heinz Mauelshagen | 7b34df7 | 2016-05-19 18:49:32 +0200 | [diff] [blame] | 1893 | |
| 1894 | sb->array_sectors = cpu_to_le64(mddev->array_sectors); |
| 1895 | sb->data_offset = cpu_to_le64(rdev->data_offset); |
| 1896 | sb->new_data_offset = cpu_to_le64(rdev->new_data_offset); |
| 1897 | sb->sectors = cpu_to_le64(rdev->sectors); |
| 1898 | |
| 1899 | /* Zero out the rest of the payload after the size of the superblock */ |
| 1900 | memset(sb + 1, 0, rdev->sb_size - sizeof(*sb)); |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1901 | } |
| 1902 | |
| 1903 | /* |
| 1904 | * super_load |
| 1905 | * |
| 1906 | * This function creates a superblock if one is not found on the device |
| 1907 | * and will decide which superblock to use if there's a choice. |
| 1908 | * |
| 1909 | * Return: 1 if use rdev, 0 if use refdev, -Exxx otherwise |
| 1910 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1911 | static int super_load(struct md_rdev *rdev, struct md_rdev *refdev) |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1912 | { |
Heinz Mauelshagen | 73c6f23 | 2016-05-19 18:49:24 +0200 | [diff] [blame] | 1913 | int r; |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1914 | struct dm_raid_superblock *sb; |
| 1915 | struct dm_raid_superblock *refsb; |
| 1916 | uint64_t events_sb, events_refsb; |
| 1917 | |
| 1918 | rdev->sb_start = 0; |
Heinz Mauelshagen | 40d43c4 | 2014-10-17 13:38:50 +0200 | [diff] [blame] | 1919 | rdev->sb_size = bdev_logical_block_size(rdev->meta_bdev); |
| 1920 | if (rdev->sb_size < sizeof(*sb) || rdev->sb_size > PAGE_SIZE) { |
| 1921 | DMERR("superblock size of a logical block is no longer valid"); |
| 1922 | return -EINVAL; |
| 1923 | } |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1924 | |
Heinz Mauelshagen | 73c6f23 | 2016-05-19 18:49:24 +0200 | [diff] [blame] | 1925 | r = read_disk_sb(rdev, rdev->sb_size); |
| 1926 | if (r) |
| 1927 | return r; |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1928 | |
| 1929 | sb = page_address(rdev->sb_page); |
Jonathan E Brassow | 3aa3b2b | 2012-03-07 19:09:47 +0000 | [diff] [blame] | 1930 | |
| 1931 | /* |
| 1932 | * Two cases that we want to write new superblocks and rebuild: |
| 1933 | * 1) New device (no matching magic number) |
| 1934 | * 2) Device specified for rebuild (!In_sync w/ offset == 0) |
| 1935 | */ |
| 1936 | if ((sb->magic != cpu_to_le32(DM_RAID_MAGIC)) || |
| 1937 | (!test_bit(In_sync, &rdev->flags) && !rdev->recovery_offset)) { |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1938 | super_sync(rdev->mddev, rdev); |
| 1939 | |
| 1940 | set_bit(FirstUse, &rdev->flags); |
Mike Snitzer | 9b6e542 | 2016-06-02 11:48:09 -0400 | [diff] [blame] | 1941 | sb->compat_features = cpu_to_le32(FEATURE_FLAG_SUPPORTS_V190); |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1942 | |
| 1943 | /* Force writing of superblocks to disk */ |
| 1944 | set_bit(MD_CHANGE_DEVS, &rdev->mddev->flags); |
| 1945 | |
| 1946 | /* Any superblock is better than none, choose that if given */ |
| 1947 | return refdev ? 0 : 1; |
| 1948 | } |
| 1949 | |
| 1950 | if (!refdev) |
| 1951 | return 1; |
| 1952 | |
| 1953 | events_sb = le64_to_cpu(sb->events); |
| 1954 | |
| 1955 | refsb = page_address(refdev->sb_page); |
| 1956 | events_refsb = le64_to_cpu(refsb->events); |
| 1957 | |
| 1958 | return (events_sb > events_refsb) ? 1 : 0; |
| 1959 | } |
| 1960 | |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 1961 | static int super_init_validation(struct raid_set *rs, struct md_rdev *rdev) |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1962 | { |
| 1963 | int role; |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 1964 | unsigned int d; |
| 1965 | struct mddev *mddev = &rs->md; |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1966 | uint64_t events_sb; |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 1967 | uint64_t failed_devices[DISKS_ARRAY_ELEMS]; |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1968 | struct dm_raid_superblock *sb; |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 1969 | uint32_t new_devs = 0, rebuild_and_new = 0, rebuilds = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1970 | struct md_rdev *r; |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1971 | struct dm_raid_superblock *sb2; |
| 1972 | |
| 1973 | sb = page_address(rdev->sb_page); |
| 1974 | events_sb = le64_to_cpu(sb->events); |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1975 | |
| 1976 | /* |
| 1977 | * Initialise to 1 if this is a new superblock. |
| 1978 | */ |
| 1979 | mddev->events = events_sb ? : 1; |
| 1980 | |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 1981 | mddev->reshape_position = MaxSector; |
| 1982 | |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1983 | /* |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 1984 | * Reshaping is supported, e.g. reshape_position is valid |
| 1985 | * in superblock and superblock content is authoritative. |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1986 | */ |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 1987 | if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190) { |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 1988 | /* Superblock is authoritative wrt given raid set layout! */ |
| 1989 | mddev->raid_disks = le32_to_cpu(sb->num_devices); |
| 1990 | mddev->level = le32_to_cpu(sb->level); |
| 1991 | mddev->layout = le32_to_cpu(sb->layout); |
| 1992 | mddev->chunk_sectors = le32_to_cpu(sb->stripe_sectors); |
| 1993 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1994 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
| 1995 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_stripe_sectors); |
| 1996 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1997 | mddev->array_sectors = le64_to_cpu(sb->array_sectors); |
| 1998 | |
| 1999 | /* raid was reshaping and got interrupted */ |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 2000 | if (le32_to_cpu(sb->flags) & SB_FLAG_RESHAPE_ACTIVE) { |
| 2001 | if (test_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags)) { |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2002 | DMERR("Reshape requested but raid set is still reshaping"); |
| 2003 | return -EINVAL; |
| 2004 | } |
| 2005 | |
| 2006 | if (mddev->delta_disks < 0 || |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 2007 | (!mddev->delta_disks && (le32_to_cpu(sb->flags) & SB_FLAG_RESHAPE_BACKWARDS))) |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2008 | mddev->reshape_backwards = 1; |
| 2009 | else |
| 2010 | mddev->reshape_backwards = 0; |
| 2011 | |
| 2012 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 2013 | rs->raid_type = get_raid_type_by_ll(mddev->level, mddev->layout); |
| 2014 | } |
| 2015 | |
| 2016 | } else { |
| 2017 | /* |
Mike Snitzer | 9b6e542 | 2016-06-02 11:48:09 -0400 | [diff] [blame] | 2018 | * No takeover/reshaping, because we don't have the extended v1.9.0 metadata |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2019 | */ |
| 2020 | if (le32_to_cpu(sb->level) != mddev->level) { |
| 2021 | DMERR("Reshaping/takeover raid sets not yet supported. (raid level/stripes/size change)"); |
| 2022 | return -EINVAL; |
| 2023 | } |
| 2024 | if (le32_to_cpu(sb->layout) != mddev->layout) { |
| 2025 | DMERR("Reshaping raid sets not yet supported. (raid layout change)"); |
Mike Snitzer | 4315784 | 2016-05-30 13:03:37 -0400 | [diff] [blame] | 2026 | DMERR(" 0x%X vs 0x%X", le32_to_cpu(sb->layout), mddev->layout); |
| 2027 | DMERR(" Old layout: %s w/ %d copies", |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2028 | raid10_md_layout_to_format(le32_to_cpu(sb->layout)), |
| 2029 | raid10_md_layout_to_copies(le32_to_cpu(sb->layout))); |
Mike Snitzer | 4315784 | 2016-05-30 13:03:37 -0400 | [diff] [blame] | 2030 | DMERR(" New layout: %s w/ %d copies", |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2031 | raid10_md_layout_to_format(mddev->layout), |
| 2032 | raid10_md_layout_to_copies(mddev->layout)); |
| 2033 | return -EINVAL; |
| 2034 | } |
| 2035 | if (le32_to_cpu(sb->stripe_sectors) != mddev->chunk_sectors) { |
| 2036 | DMERR("Reshaping raid sets not yet supported. (stripe sectors change)"); |
| 2037 | return -EINVAL; |
| 2038 | } |
| 2039 | |
| 2040 | /* We can only change the number of devices in raid1 with old (i.e. pre 1.0.7) metadata */ |
| 2041 | if (!rt_is_raid1(rs->raid_type) && |
| 2042 | (le32_to_cpu(sb->num_devices) != mddev->raid_disks)) { |
| 2043 | DMERR("Reshaping raid sets not yet supported. (device count change from %u to %u)", |
| 2044 | sb->num_devices, mddev->raid_disks); |
| 2045 | return -EINVAL; |
| 2046 | } |
| 2047 | |
| 2048 | /* Table line is checked vs. authoritative superblock */ |
| 2049 | rs_set_new(rs); |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2050 | } |
| 2051 | |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 2052 | if (!test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2053 | mddev->recovery_cp = le64_to_cpu(sb->array_resync_offset); |
| 2054 | |
| 2055 | /* |
| 2056 | * During load, we set FirstUse if a new superblock was written. |
| 2057 | * There are two reasons we might not have a superblock: |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2058 | * 1) The raid set is brand new - in which case, all of the |
Mike Snitzer | 4315784 | 2016-05-30 13:03:37 -0400 | [diff] [blame] | 2059 | * devices must have their In_sync bit set. Also, |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2060 | * recovery_cp must be 0, unless forced. |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2061 | * 2) This is a new device being added to an old raid set |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2062 | * and the new device needs to be rebuilt - in which |
| 2063 | * case the In_sync bit will /not/ be set and |
| 2064 | * recovery_cp must be MaxSector. |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 2065 | * 3) This is/are a new device(s) being added to an old |
| 2066 | * raid set during takeover to a higher raid level |
| 2067 | * to provide capacity for redundancy or during reshape |
| 2068 | * to add capacity to grow the raid set. |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2069 | */ |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2070 | d = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2071 | rdev_for_each(r, mddev) { |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2072 | if (test_bit(FirstUse, &r->flags)) |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2073 | new_devs++; |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2074 | |
| 2075 | if (!test_bit(In_sync, &r->flags)) { |
| 2076 | DMINFO("Device %d specified for rebuild; clearing superblock", |
| 2077 | r->raid_disk); |
| 2078 | rebuilds++; |
| 2079 | |
| 2080 | if (test_bit(FirstUse, &r->flags)) |
| 2081 | rebuild_and_new++; |
| 2082 | } |
| 2083 | |
| 2084 | d++; |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2085 | } |
| 2086 | |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2087 | if (new_devs == rs->raid_disks || !rebuilds) { |
| 2088 | /* Replace a broken device */ |
| 2089 | if (new_devs == 1 && !rs->delta_disks) |
| 2090 | ; |
| 2091 | if (new_devs == rs->raid_disks) { |
| 2092 | DMINFO("Superblocks created for new raid set"); |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2093 | set_bit(MD_ARRAY_FIRST_USE, &mddev->flags); |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2094 | mddev->recovery_cp = 0; |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 2095 | } else if (new_devs != rebuilds && |
| 2096 | new_devs != rs->delta_disks) { |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2097 | DMERR("New device injected into existing raid set without " |
| 2098 | "'delta_disks' or 'rebuild' parameter specified"); |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2099 | return -EINVAL; |
| 2100 | } |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2101 | } else if (new_devs && new_devs != rebuilds) { |
| 2102 | DMERR("%u 'rebuild' devices cannot be injected into" |
| 2103 | " a raid set with %u other first-time devices", |
| 2104 | rebuilds, new_devs); |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2105 | return -EINVAL; |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2106 | } else if (rebuilds) { |
| 2107 | if (rebuild_and_new && rebuilds != rebuild_and_new) { |
| 2108 | DMERR("new device%s provided without 'rebuild'", |
| 2109 | new_devs > 1 ? "s" : ""); |
| 2110 | return -EINVAL; |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 2111 | } else if (rs_is_recovering(rs)) { |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2112 | DMERR("'rebuild' specified while raid set is not in-sync (recovery_cp=%llu)", |
| 2113 | (unsigned long long) mddev->recovery_cp); |
| 2114 | return -EINVAL; |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 2115 | } else if (rs_is_reshaping(rs)) { |
| 2116 | DMERR("'rebuild' specified while raid set is being reshaped (reshape_position=%llu)", |
| 2117 | (unsigned long long) mddev->reshape_position); |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2118 | return -EINVAL; |
| 2119 | } |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2120 | } |
| 2121 | |
| 2122 | /* |
| 2123 | * Now we set the Faulty bit for those devices that are |
| 2124 | * recorded in the superblock as failed. |
| 2125 | */ |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2126 | sb_retrieve_failed_devices(sb, failed_devices); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2127 | rdev_for_each(r, mddev) { |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2128 | if (!r->sb_page) |
| 2129 | continue; |
| 2130 | sb2 = page_address(r->sb_page); |
| 2131 | sb2->failed_devices = 0; |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2132 | memset(sb2->extended_failed_devices, 0, sizeof(sb2->extended_failed_devices)); |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2133 | |
| 2134 | /* |
| 2135 | * Check for any device re-ordering. |
| 2136 | */ |
| 2137 | if (!test_bit(FirstUse, &r->flags) && (r->raid_disk >= 0)) { |
| 2138 | role = le32_to_cpu(sb2->array_position); |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2139 | if (role < 0) |
| 2140 | continue; |
| 2141 | |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2142 | if (role != r->raid_disk) { |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 2143 | if (__is_raid10_near(mddev->layout)) { |
| 2144 | if (mddev->raid_disks % __raid10_near_copies(mddev->layout) || |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 2145 | rs->raid_disks % rs->raid10_copies) { |
| 2146 | rs->ti->error = |
| 2147 | "Cannot change raid10 near set to odd # of devices!"; |
| 2148 | return -EINVAL; |
| 2149 | } |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2150 | |
| 2151 | sb2->array_position = cpu_to_le32(r->raid_disk); |
| 2152 | |
| 2153 | } else if (!(rs_is_raid10(rs) && rt_is_raid0(rs->raid_type)) && |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 2154 | !(rs_is_raid0(rs) && rt_is_raid10(rs->raid_type)) && |
| 2155 | !rt_is_raid1(rs->raid_type)) { |
| 2156 | rs->ti->error = "Cannot change device positions in raid set"; |
| 2157 | return -EINVAL; |
| 2158 | } |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2159 | |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 2160 | DMINFO("raid device #%d now at position #%d", role, r->raid_disk); |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2161 | } |
| 2162 | |
| 2163 | /* |
| 2164 | * Partial recovery is performed on |
| 2165 | * returning failed devices. |
| 2166 | */ |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2167 | if (test_bit(role, (void *) failed_devices)) |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2168 | set_bit(Faulty, &r->flags); |
| 2169 | } |
| 2170 | } |
| 2171 | |
| 2172 | return 0; |
| 2173 | } |
| 2174 | |
Heinz Mauelshagen | 0cf4503 | 2015-04-29 14:03:04 +0200 | [diff] [blame] | 2175 | static int super_validate(struct raid_set *rs, struct md_rdev *rdev) |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2176 | { |
Heinz Mauelshagen | 0cf4503 | 2015-04-29 14:03:04 +0200 | [diff] [blame] | 2177 | struct mddev *mddev = &rs->md; |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2178 | struct dm_raid_superblock *sb; |
| 2179 | |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 2180 | if (rs_is_raid0(rs) || !rdev->sb_page) |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2181 | return 0; |
| 2182 | |
| 2183 | sb = page_address(rdev->sb_page); |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2184 | |
| 2185 | /* |
| 2186 | * If mddev->events is not set, we know we have not yet initialized |
| 2187 | * the array. |
| 2188 | */ |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2189 | if (!mddev->events && super_init_validation(rs, rdev)) |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2190 | return -EINVAL; |
| 2191 | |
Mike Snitzer | 9b6e542 | 2016-06-02 11:48:09 -0400 | [diff] [blame] | 2192 | if (le32_to_cpu(sb->compat_features) != FEATURE_FLAG_SUPPORTS_V190) { |
| 2193 | rs->ti->error = "Unable to assemble array: Unknown flag(s) in compatible feature flags"; |
| 2194 | return -EINVAL; |
| 2195 | } |
| 2196 | |
| 2197 | if (sb->incompat_features) { |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 2198 | rs->ti->error = "Unable to assemble array: No incompatible feature flags supported yet"; |
Heinz Mauelshagen | 4c9971c | 2016-04-29 18:59:56 +0200 | [diff] [blame] | 2199 | return -EINVAL; |
| 2200 | } |
| 2201 | |
Heinz Mauelshagen | 0cf4503 | 2015-04-29 14:03:04 +0200 | [diff] [blame] | 2202 | /* Enable bitmap creation for RAID levels != 0 */ |
Heinz Mauelshagen | 676fa5a | 2016-05-19 18:49:29 +0200 | [diff] [blame] | 2203 | mddev->bitmap_info.offset = rt_is_raid0(rs->raid_type) ? 0 : to_sector(4096); |
Heinz Mauelshagen | 0cf4503 | 2015-04-29 14:03:04 +0200 | [diff] [blame] | 2204 | rdev->mddev->bitmap_info.default_offset = mddev->bitmap_info.offset; |
| 2205 | |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2206 | if (!test_and_clear_bit(FirstUse, &rdev->flags)) { |
| 2207 | /* Retrieve device size stored in superblock to be prepared for shrink */ |
| 2208 | rdev->sectors = le64_to_cpu(sb->sectors); |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2209 | rdev->recovery_offset = le64_to_cpu(sb->disk_recovery_offset); |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2210 | if (rdev->recovery_offset == MaxSector) |
| 2211 | set_bit(In_sync, &rdev->flags); |
| 2212 | /* |
| 2213 | * If no reshape in progress -> we're recovering single |
| 2214 | * disk(s) and have to set the device(s) to out-of-sync |
| 2215 | */ |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 2216 | else if (!rs_is_reshaping(rs)) |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2217 | clear_bit(In_sync, &rdev->flags); /* Mandatory for recovery */ |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2218 | } |
| 2219 | |
| 2220 | /* |
| 2221 | * If a device comes back, set it as not In_sync and no longer faulty. |
| 2222 | */ |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2223 | if (test_and_clear_bit(Faulty, &rdev->flags)) { |
| 2224 | rdev->recovery_offset = 0; |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2225 | clear_bit(In_sync, &rdev->flags); |
| 2226 | rdev->saved_raid_disk = rdev->raid_disk; |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2227 | } |
| 2228 | |
Heinz Mauelshagen | 33e53f0 | 2016-05-19 18:49:30 +0200 | [diff] [blame] | 2229 | /* Reshape support -> restore repective data offsets */ |
| 2230 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
| 2231 | rdev->new_data_offset = le64_to_cpu(sb->new_data_offset); |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2232 | |
| 2233 | return 0; |
| 2234 | } |
| 2235 | |
| 2236 | /* |
| 2237 | * Analyse superblocks and select the freshest. |
| 2238 | */ |
| 2239 | static int analyse_superblocks(struct dm_target *ti, struct raid_set *rs) |
| 2240 | { |
Heinz Mauelshagen | 73c6f23 | 2016-05-19 18:49:24 +0200 | [diff] [blame] | 2241 | int r; |
Jonathan E Brassow | 0447568 | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 2242 | struct raid_dev *dev; |
Jonathan Brassow | a9ad852 | 2012-04-24 10:23:13 +1000 | [diff] [blame] | 2243 | struct md_rdev *rdev, *tmp, *freshest; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2244 | struct mddev *mddev = &rs->md; |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2245 | |
| 2246 | freshest = NULL; |
Jonathan Brassow | a9ad852 | 2012-04-24 10:23:13 +1000 | [diff] [blame] | 2247 | rdev_for_each_safe(rdev, tmp, mddev) { |
Jonathan Brassow | 761becf | 2012-10-11 13:42:19 +1100 | [diff] [blame] | 2248 | /* |
Heinz Mauelshagen | c76d53f | 2015-04-29 14:03:00 +0200 | [diff] [blame] | 2249 | * Skipping super_load due to CTR_FLAG_SYNC will cause |
Jonathan Brassow | 761becf | 2012-10-11 13:42:19 +1100 | [diff] [blame] | 2250 | * the array to undergo initialization again as |
Mike Snitzer | 4315784 | 2016-05-30 13:03:37 -0400 | [diff] [blame] | 2251 | * though it were new. This is the intended effect |
Jonathan Brassow | 761becf | 2012-10-11 13:42:19 +1100 | [diff] [blame] | 2252 | * of the "sync" directive. |
| 2253 | * |
| 2254 | * When reshaping capability is added, we must ensure |
| 2255 | * that the "sync" directive is disallowed during the |
| 2256 | * reshape. |
| 2257 | */ |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 2258 | if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags)) |
Jonathan Brassow | 761becf | 2012-10-11 13:42:19 +1100 | [diff] [blame] | 2259 | continue; |
| 2260 | |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2261 | if (!rdev->meta_bdev) |
| 2262 | continue; |
| 2263 | |
Heinz Mauelshagen | 73c6f23 | 2016-05-19 18:49:24 +0200 | [diff] [blame] | 2264 | r = super_load(rdev, freshest); |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2265 | |
Heinz Mauelshagen | 73c6f23 | 2016-05-19 18:49:24 +0200 | [diff] [blame] | 2266 | switch (r) { |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2267 | case 1: |
| 2268 | freshest = rdev; |
| 2269 | break; |
| 2270 | case 0: |
| 2271 | break; |
| 2272 | default: |
Jonathan E Brassow | 0447568 | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 2273 | dev = container_of(rdev, struct raid_dev, rdev); |
Jonathan Brassow | 55ebbb5 | 2013-01-22 21:42:18 -0600 | [diff] [blame] | 2274 | if (dev->meta_dev) |
| 2275 | dm_put_device(ti, dev->meta_dev); |
Jonathan E Brassow | 0447568 | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 2276 | |
Jonathan Brassow | 55ebbb5 | 2013-01-22 21:42:18 -0600 | [diff] [blame] | 2277 | dev->meta_dev = NULL; |
| 2278 | rdev->meta_bdev = NULL; |
Jonathan E Brassow | 0447568 | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 2279 | |
Jonathan Brassow | 55ebbb5 | 2013-01-22 21:42:18 -0600 | [diff] [blame] | 2280 | if (rdev->sb_page) |
| 2281 | put_page(rdev->sb_page); |
Jonathan E Brassow | 0447568 | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 2282 | |
Jonathan Brassow | 55ebbb5 | 2013-01-22 21:42:18 -0600 | [diff] [blame] | 2283 | rdev->sb_page = NULL; |
Jonathan E Brassow | 0447568 | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 2284 | |
Jonathan Brassow | 55ebbb5 | 2013-01-22 21:42:18 -0600 | [diff] [blame] | 2285 | rdev->sb_loaded = 0; |
Jonathan E Brassow | 0447568 | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 2286 | |
Jonathan Brassow | 55ebbb5 | 2013-01-22 21:42:18 -0600 | [diff] [blame] | 2287 | /* |
| 2288 | * We might be able to salvage the data device |
| 2289 | * even though the meta device has failed. For |
| 2290 | * now, we behave as though '- -' had been |
| 2291 | * set for this device in the table. |
| 2292 | */ |
| 2293 | if (dev->data_dev) |
| 2294 | dm_put_device(ti, dev->data_dev); |
Jonathan E Brassow | 0447568 | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 2295 | |
Jonathan Brassow | 55ebbb5 | 2013-01-22 21:42:18 -0600 | [diff] [blame] | 2296 | dev->data_dev = NULL; |
| 2297 | rdev->bdev = NULL; |
Jonathan E Brassow | 0447568 | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 2298 | |
Jonathan Brassow | 55ebbb5 | 2013-01-22 21:42:18 -0600 | [diff] [blame] | 2299 | list_del(&rdev->same_set); |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2300 | } |
| 2301 | } |
| 2302 | |
| 2303 | if (!freshest) |
| 2304 | return 0; |
| 2305 | |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 2306 | if (validate_raid_redundancy(rs)) { |
| 2307 | rs->ti->error = "Insufficient redundancy to activate array"; |
| 2308 | return -EINVAL; |
| 2309 | } |
Jonathan Brassow | 55ebbb5 | 2013-01-22 21:42:18 -0600 | [diff] [blame] | 2310 | |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2311 | /* |
| 2312 | * Validation of the freshest device provides the source of |
| 2313 | * validation for the remaining devices. |
| 2314 | */ |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 2315 | rs->ti->error = "Unable to assemble array: Invalid superblocks"; |
| 2316 | if (super_validate(rs, freshest)) |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 2317 | return -EINVAL; |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2318 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2319 | rdev_for_each(rdev, mddev) |
Heinz Mauelshagen | 0cf4503 | 2015-04-29 14:03:04 +0200 | [diff] [blame] | 2320 | if ((rdev != freshest) && super_validate(rs, rdev)) |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2321 | return -EINVAL; |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2322 | return 0; |
| 2323 | } |
| 2324 | |
Heinz Mauelshagen | 40ba37e | 2016-06-13 17:55:13 +0200 | [diff] [blame] | 2325 | /* |
| 2326 | * Adjust data_offset and new_data_offset on all disk members of @rs |
| 2327 | * for out of place reshaping if requested by contructor |
| 2328 | * |
| 2329 | * We need free space at the beginning of each raid disk for forward |
| 2330 | * and at the end for backward reshapes which userspace has to provide |
| 2331 | * via remapping/reordering of space. |
| 2332 | */ |
| 2333 | static int rs_adjust_data_offsets(struct raid_set *rs) |
| 2334 | { |
| 2335 | sector_t data_offset = 0, new_data_offset = 0; |
| 2336 | struct md_rdev *rdev; |
| 2337 | |
| 2338 | /* Constructor did not request data offset change */ |
| 2339 | if (!test_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags)) { |
| 2340 | if (!rs_is_reshapable(rs)) |
| 2341 | goto out; |
| 2342 | |
| 2343 | return 0; |
| 2344 | } |
| 2345 | |
| 2346 | /* HM FIXME: get InSync raid_dev? */ |
| 2347 | rdev = &rs->dev[0].rdev; |
| 2348 | |
| 2349 | if (rs->delta_disks < 0) { |
| 2350 | /* |
| 2351 | * Removing disks (reshaping backwards): |
| 2352 | * |
| 2353 | * - before reshape: data is at offset 0 and free space |
| 2354 | * is at end of each component LV |
| 2355 | * |
| 2356 | * - after reshape: data is at offset rs->data_offset != 0 on each component LV |
| 2357 | */ |
| 2358 | data_offset = 0; |
| 2359 | new_data_offset = rs->data_offset; |
| 2360 | |
| 2361 | } else if (rs->delta_disks > 0) { |
| 2362 | /* |
| 2363 | * Adding disks (reshaping forwards): |
| 2364 | * |
| 2365 | * - before reshape: data is at offset rs->data_offset != 0 and |
| 2366 | * free space is at begin of each component LV |
| 2367 | * |
| 2368 | * - after reshape: data is at offset 0 on each component LV |
| 2369 | */ |
| 2370 | data_offset = rs->data_offset; |
| 2371 | new_data_offset = 0; |
| 2372 | |
| 2373 | } else { |
| 2374 | /* |
| 2375 | * User space passes in 0 for data offset after having removed reshape space |
| 2376 | * |
| 2377 | * - or - (data offset != 0) |
| 2378 | * |
| 2379 | * Changing RAID layout or chunk size -> toggle offsets |
| 2380 | * |
| 2381 | * - before reshape: data is at offset rs->data_offset 0 and |
| 2382 | * free space is at end of each component LV |
| 2383 | * -or- |
| 2384 | * data is at offset rs->data_offset != 0 and |
| 2385 | * free space is at begin of each component LV |
| 2386 | * |
| 2387 | * - after reshape: data is at offset 0 if i was at offset != 0 |
| 2388 | * of at offset != 0 if it was at offset 0 |
| 2389 | * on each component LV |
| 2390 | * |
| 2391 | */ |
| 2392 | data_offset = rs->data_offset ? rdev->data_offset : 0; |
| 2393 | new_data_offset = data_offset ? 0 : rs->data_offset; |
| 2394 | set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags); |
| 2395 | } |
| 2396 | |
| 2397 | /* |
| 2398 | * Make sure we got a minimum amount of free sectors per device |
| 2399 | */ |
| 2400 | if (rs->data_offset && |
| 2401 | to_sector(i_size_read(rdev->bdev->bd_inode)) - rdev->sectors < MIN_FREE_RESHAPE_SPACE) { |
| 2402 | rs->ti->error = data_offset ? "No space for forward reshape" : |
| 2403 | "No space for backward reshape"; |
| 2404 | return -ENOSPC; |
| 2405 | } |
| 2406 | out: |
| 2407 | /* Adjust data offsets on all rdevs */ |
| 2408 | rdev_for_each(rdev, &rs->md) { |
| 2409 | rdev->data_offset = data_offset; |
| 2410 | rdev->new_data_offset = new_data_offset; |
| 2411 | } |
| 2412 | |
| 2413 | return 0; |
| 2414 | } |
| 2415 | |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 2416 | /* Userpace reordered disks -> adjust raid_disk indexes in @rs */ |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 2417 | static void __reorder_raid_disk_indexes(struct raid_set *rs) |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 2418 | { |
| 2419 | int i = 0; |
| 2420 | struct md_rdev *rdev; |
| 2421 | |
| 2422 | rdev_for_each(rdev, &rs->md) { |
| 2423 | rdev->raid_disk = i++; |
| 2424 | rdev->saved_raid_disk = rdev->new_raid_disk = -1; |
| 2425 | } |
| 2426 | } |
| 2427 | |
| 2428 | /* |
| 2429 | * Setup @rs for takeover by a different raid level |
| 2430 | */ |
| 2431 | static int rs_setup_takeover(struct raid_set *rs) |
| 2432 | { |
| 2433 | struct mddev *mddev = &rs->md; |
| 2434 | struct md_rdev *rdev; |
| 2435 | unsigned int d = mddev->raid_disks = rs->raid_disks; |
| 2436 | sector_t new_data_offset = rs->dev[0].rdev.data_offset ? 0 : rs->data_offset; |
| 2437 | |
| 2438 | if (rt_is_raid10(rs->raid_type)) { |
| 2439 | if (mddev->level == 0) { |
| 2440 | /* Userpace reordered disks -> adjust raid_disk indexes */ |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 2441 | __reorder_raid_disk_indexes(rs); |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 2442 | |
| 2443 | /* raid0 -> raid10_far layout */ |
| 2444 | mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_FAR, |
| 2445 | rs->raid10_copies); |
| 2446 | } else if (mddev->level == 1) |
| 2447 | /* raid1 -> raid10_near layout */ |
| 2448 | mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_NEAR, |
| 2449 | rs->raid_disks); |
| 2450 | else |
| 2451 | return -EINVAL; |
| 2452 | |
| 2453 | } |
| 2454 | |
| 2455 | clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags); |
| 2456 | mddev->recovery_cp = MaxSector; |
| 2457 | |
| 2458 | while (d--) { |
| 2459 | rdev = &rs->dev[d].rdev; |
| 2460 | |
| 2461 | if (test_bit(d, (void *) rs->rebuild_disks)) { |
| 2462 | clear_bit(In_sync, &rdev->flags); |
| 2463 | clear_bit(Faulty, &rdev->flags); |
| 2464 | mddev->recovery_cp = rdev->recovery_offset = 0; |
| 2465 | /* Bitmap has to be created when we do an "up" takeover */ |
| 2466 | set_bit(MD_ARRAY_FIRST_USE, &mddev->flags); |
| 2467 | } |
| 2468 | |
| 2469 | rdev->new_data_offset = new_data_offset; |
| 2470 | } |
| 2471 | |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 2472 | return 0; |
| 2473 | } |
| 2474 | |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2475 | /* |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 2476 | * |
| 2477 | * - change raid layout |
| 2478 | * - change chunk size |
| 2479 | * - add disks |
| 2480 | * - remove disks |
| 2481 | */ |
| 2482 | static int rs_setup_reshape(struct raid_set *rs) |
| 2483 | { |
| 2484 | int r = 0; |
| 2485 | unsigned int cur_raid_devs, d; |
| 2486 | struct mddev *mddev = &rs->md; |
| 2487 | struct md_rdev *rdev; |
| 2488 | |
| 2489 | mddev->delta_disks = rs->delta_disks; |
| 2490 | cur_raid_devs = mddev->raid_disks; |
| 2491 | |
| 2492 | /* Ignore impossible layout change whilst adding/removing disks */ |
| 2493 | if (mddev->delta_disks && |
| 2494 | mddev->layout != mddev->new_layout) { |
| 2495 | DMINFO("Ignoring invalid layout change with delta_disks=%d", rs->delta_disks); |
| 2496 | mddev->new_layout = mddev->layout; |
| 2497 | } |
| 2498 | |
| 2499 | /* |
| 2500 | * Adjust array size: |
| 2501 | * |
| 2502 | * - in case of adding disks, array size has |
| 2503 | * to grow after the disk adding reshape, |
| 2504 | * which'll hapen in the event handler; |
| 2505 | * reshape will happen forward, so space has to |
| 2506 | * be available at the beginning of each disk |
| 2507 | * |
| 2508 | * - in case of removing disks, array size |
| 2509 | * has to shrink before starting the reshape, |
| 2510 | * which'll happen here; |
| 2511 | * reshape will happen backward, so space has to |
| 2512 | * be available at the end of each disk |
| 2513 | * |
| 2514 | * - data_offset and new_data_offset are |
| 2515 | * adjusted for afreentioned out of place |
| 2516 | * reshaping based on userspace passing in |
| 2517 | * the "data_offset <sectors>" key/value |
| 2518 | * pair via te constructor |
| 2519 | */ |
| 2520 | |
| 2521 | /* Add disk(s) */ |
| 2522 | if (rs->delta_disks > 0) { |
| 2523 | /* Prepare disks for check in raid4/5/6/10 {check|start}_reshape */ |
| 2524 | for (d = cur_raid_devs; d < rs->raid_disks; d++) { |
| 2525 | rdev = &rs->dev[d].rdev; |
| 2526 | clear_bit(In_sync, &rdev->flags); |
| 2527 | |
| 2528 | /* |
| 2529 | * save_raid_disk needs to be -1, or recovery_offset will be set to 0 |
| 2530 | * by md, which'll store that erroneously in the superblock on reshape |
| 2531 | */ |
| 2532 | rdev->saved_raid_disk = -1; |
| 2533 | rdev->raid_disk = d; |
| 2534 | |
| 2535 | rdev->sectors = mddev->dev_sectors; |
| 2536 | rdev->recovery_offset = MaxSector; |
| 2537 | } |
| 2538 | |
| 2539 | mddev->reshape_backwards = 0; /* adding disks -> forward reshape */ |
| 2540 | |
| 2541 | /* Remove disk(s) */ |
| 2542 | } else if (rs->delta_disks < 0) { |
| 2543 | r = rs_set_dev_and_array_sectors(rs, true); |
| 2544 | mddev->reshape_backwards = 1; /* removing disk(s) -> backward reshape */ |
| 2545 | |
| 2546 | /* Change layout and/or chunk size */ |
| 2547 | } else { |
| 2548 | /* |
| 2549 | * Reshape layout (e.g. raid5_ls -> raid5_n) and/or chunk size: |
| 2550 | * |
| 2551 | * keeping number of disks and do layout change -> |
| 2552 | * |
| 2553 | * toggle reshape_backward depending on data_offset: |
| 2554 | * |
| 2555 | * - free space upfront -> reshape forward |
| 2556 | * |
| 2557 | * - free space at the end -> reshape backward |
| 2558 | * |
| 2559 | * |
| 2560 | * This utilizes free reshape space avoiding the need |
| 2561 | * for userspace to move (parts of) LV segments in |
| 2562 | * case of layout/chunksize change (for disk |
| 2563 | * adding/removing reshape space has to be at |
| 2564 | * the proper address (see above with delta_disks): |
| 2565 | * |
| 2566 | * add disk(s) -> begin |
| 2567 | * remove disk(s)-> end |
| 2568 | */ |
| 2569 | mddev->reshape_backwards = rs->dev[0].rdev.data_offset ? 0 : 1; |
| 2570 | } |
| 2571 | |
| 2572 | return r; |
| 2573 | } |
| 2574 | |
| 2575 | /* |
Heinz Mauelshagen | 48cf06b | 2014-09-24 17:47:19 +0200 | [diff] [blame] | 2576 | * Enable/disable discard support on RAID set depending on |
| 2577 | * RAID level and discard properties of underlying RAID members. |
Heinz Mauelshagen | 75b8e04 | 2014-09-24 17:47:18 +0200 | [diff] [blame] | 2578 | */ |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 2579 | static void configure_discard_support(struct raid_set *rs) |
Heinz Mauelshagen | 75b8e04 | 2014-09-24 17:47:18 +0200 | [diff] [blame] | 2580 | { |
Heinz Mauelshagen | 48cf06b | 2014-09-24 17:47:19 +0200 | [diff] [blame] | 2581 | int i; |
| 2582 | bool raid456; |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 2583 | struct dm_target *ti = rs->ti; |
Heinz Mauelshagen | 48cf06b | 2014-09-24 17:47:19 +0200 | [diff] [blame] | 2584 | |
Heinz Mauelshagen | 75b8e04 | 2014-09-24 17:47:18 +0200 | [diff] [blame] | 2585 | /* Assume discards not supported until after checks below. */ |
| 2586 | ti->discards_supported = false; |
| 2587 | |
| 2588 | /* RAID level 4,5,6 require discard_zeroes_data for data integrity! */ |
Heinz Mauelshagen | 48cf06b | 2014-09-24 17:47:19 +0200 | [diff] [blame] | 2589 | raid456 = (rs->md.level == 4 || rs->md.level == 5 || rs->md.level == 6); |
Heinz Mauelshagen | 75b8e04 | 2014-09-24 17:47:18 +0200 | [diff] [blame] | 2590 | |
Heinz Mauelshagen | 48cf06b | 2014-09-24 17:47:19 +0200 | [diff] [blame] | 2591 | for (i = 0; i < rs->md.raid_disks; i++) { |
Heinz Mauelshagen | d20c4b0 | 2014-10-29 19:02:27 +0100 | [diff] [blame] | 2592 | struct request_queue *q; |
Heinz Mauelshagen | 48cf06b | 2014-09-24 17:47:19 +0200 | [diff] [blame] | 2593 | |
Heinz Mauelshagen | d20c4b0 | 2014-10-29 19:02:27 +0100 | [diff] [blame] | 2594 | if (!rs->dev[i].rdev.bdev) |
| 2595 | continue; |
| 2596 | |
| 2597 | q = bdev_get_queue(rs->dev[i].rdev.bdev); |
Heinz Mauelshagen | 48cf06b | 2014-09-24 17:47:19 +0200 | [diff] [blame] | 2598 | if (!q || !blk_queue_discard(q)) |
| 2599 | return; |
| 2600 | |
| 2601 | if (raid456) { |
| 2602 | if (!q->limits.discard_zeroes_data) |
| 2603 | return; |
| 2604 | if (!devices_handle_discard_safely) { |
| 2605 | DMERR("raid456 discard support disabled due to discard_zeroes_data uncertainty."); |
| 2606 | DMERR("Set dm-raid.devices_handle_discard_safely=Y to override."); |
| 2607 | return; |
| 2608 | } |
| 2609 | } |
| 2610 | } |
| 2611 | |
| 2612 | /* All RAID members properly support discards */ |
Heinz Mauelshagen | 75b8e04 | 2014-09-24 17:47:18 +0200 | [diff] [blame] | 2613 | ti->discards_supported = true; |
| 2614 | |
| 2615 | /* |
| 2616 | * RAID1 and RAID10 personalities require bio splitting, |
Heinz Mauelshagen | 48cf06b | 2014-09-24 17:47:19 +0200 | [diff] [blame] | 2617 | * RAID0/4/5/6 don't and process large discard bios properly. |
Heinz Mauelshagen | 75b8e04 | 2014-09-24 17:47:18 +0200 | [diff] [blame] | 2618 | */ |
Heinz Mauelshagen | 48cf06b | 2014-09-24 17:47:19 +0200 | [diff] [blame] | 2619 | ti->split_discard_bios = !!(rs->md.level == 1 || rs->md.level == 10); |
Heinz Mauelshagen | 75b8e04 | 2014-09-24 17:47:18 +0200 | [diff] [blame] | 2620 | ti->num_discard_bios = 1; |
| 2621 | } |
| 2622 | |
| 2623 | /* |
Heinz Mauelshagen | 73c6f23 | 2016-05-19 18:49:24 +0200 | [diff] [blame] | 2624 | * Construct a RAID0/1/10/4/5/6 mapping: |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2625 | * Args: |
Mike Snitzer | 4315784 | 2016-05-30 13:03:37 -0400 | [diff] [blame] | 2626 | * <raid_type> <#raid_params> <raid_params>{0,} \ |
| 2627 | * <#raid_devs> [<meta_dev1> <dev1>]{1,} |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2628 | * |
Mike Snitzer | 4315784 | 2016-05-30 13:03:37 -0400 | [diff] [blame] | 2629 | * <raid_params> varies by <raid_type>. See 'parse_raid_params' for |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2630 | * details on possible <raid_params>. |
Heinz Mauelshagen | 73c6f23 | 2016-05-19 18:49:24 +0200 | [diff] [blame] | 2631 | * |
| 2632 | * Userspace is free to initialize the metadata devices, hence the superblocks to |
| 2633 | * enforce recreation based on the passed in table parameters. |
| 2634 | * |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2635 | */ |
| 2636 | static int raid_ctr(struct dm_target *ti, unsigned argc, char **argv) |
| 2637 | { |
Heinz Mauelshagen | 73c6f23 | 2016-05-19 18:49:24 +0200 | [diff] [blame] | 2638 | int r; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2639 | struct raid_type *rt; |
Heinz Mauelshagen | 92c83d7 | 2016-05-19 18:49:25 +0200 | [diff] [blame] | 2640 | unsigned num_raid_params, num_raid_devs; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2641 | struct raid_set *rs = NULL; |
Heinz Mauelshagen | 92c83d7 | 2016-05-19 18:49:25 +0200 | [diff] [blame] | 2642 | const char *arg; |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 2643 | struct rs_layout rs_layout; |
Heinz Mauelshagen | 92c83d7 | 2016-05-19 18:49:25 +0200 | [diff] [blame] | 2644 | struct dm_arg_set as = { argc, argv }, as_nrd; |
| 2645 | struct dm_arg _args[] = { |
| 2646 | { 0, as.argc, "Cannot understand number of raid parameters" }, |
| 2647 | { 1, 254, "Cannot understand number of raid devices parameters" } |
| 2648 | }; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2649 | |
Heinz Mauelshagen | 92c83d7 | 2016-05-19 18:49:25 +0200 | [diff] [blame] | 2650 | /* Must have <raid_type> */ |
| 2651 | arg = dm_shift_arg(&as); |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 2652 | if (!arg) { |
| 2653 | ti->error = "No arguments"; |
| 2654 | return -EINVAL; |
| 2655 | } |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2656 | |
Heinz Mauelshagen | 92c83d7 | 2016-05-19 18:49:25 +0200 | [diff] [blame] | 2657 | rt = get_raid_type(arg); |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 2658 | if (!rt) { |
| 2659 | ti->error = "Unrecognised raid_type"; |
| 2660 | return -EINVAL; |
| 2661 | } |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2662 | |
Heinz Mauelshagen | 92c83d7 | 2016-05-19 18:49:25 +0200 | [diff] [blame] | 2663 | /* Must have <#raid_params> */ |
| 2664 | if (dm_read_arg_group(_args, &as, &num_raid_params, &ti->error)) |
Mike Snitzer | 4315784 | 2016-05-30 13:03:37 -0400 | [diff] [blame] | 2665 | return -EINVAL; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2666 | |
Heinz Mauelshagen | 92c83d7 | 2016-05-19 18:49:25 +0200 | [diff] [blame] | 2667 | /* number of raid device tupples <meta_dev data_dev> */ |
| 2668 | as_nrd = as; |
| 2669 | dm_consume_args(&as_nrd, num_raid_params); |
| 2670 | _args[1].max = (as_nrd.argc - 1) / 2; |
| 2671 | if (dm_read_arg(_args + 1, &as_nrd, &num_raid_devs, &ti->error)) |
Mike Snitzer | 4315784 | 2016-05-30 13:03:37 -0400 | [diff] [blame] | 2672 | return -EINVAL; |
Heinz Mauelshagen | 92c83d7 | 2016-05-19 18:49:25 +0200 | [diff] [blame] | 2673 | |
Mike Snitzer | bb91a63 | 2016-06-02 12:06:54 -0400 | [diff] [blame] | 2674 | if (!__within_range(num_raid_devs, 1, MAX_RAID_DEVICES)) { |
Mike Snitzer | bd83a4c | 2016-05-31 14:26:52 -0400 | [diff] [blame] | 2675 | ti->error = "Invalid number of supplied raid devices"; |
| 2676 | return -EINVAL; |
| 2677 | } |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2678 | |
Mike Snitzer | bfcee0e | 2016-06-02 15:08:09 -0400 | [diff] [blame] | 2679 | rs = raid_set_alloc(ti, rt, num_raid_devs); |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2680 | if (IS_ERR(rs)) |
| 2681 | return PTR_ERR(rs); |
| 2682 | |
Heinz Mauelshagen | 92c83d7 | 2016-05-19 18:49:25 +0200 | [diff] [blame] | 2683 | r = parse_raid_params(rs, &as, num_raid_params); |
Heinz Mauelshagen | 73c6f23 | 2016-05-19 18:49:24 +0200 | [diff] [blame] | 2684 | if (r) |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2685 | goto bad; |
| 2686 | |
Heinz Mauelshagen | 702108d | 2016-05-19 18:49:26 +0200 | [diff] [blame] | 2687 | r = parse_dev_params(rs, &as); |
Heinz Mauelshagen | 73c6f23 | 2016-05-19 18:49:24 +0200 | [diff] [blame] | 2688 | if (r) |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2689 | goto bad; |
| 2690 | |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2691 | rs->md.sync_super = super_sync; |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 2692 | |
Heinz Mauelshagen | 40ba37e | 2016-06-13 17:55:13 +0200 | [diff] [blame] | 2693 | r = rs_set_dev_and_array_sectors(rs, false); |
| 2694 | if (r) |
| 2695 | return r; |
| 2696 | |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 2697 | /* |
| 2698 | * Backup any new raid set level, layout, ... |
| 2699 | * requested to be able to compare to superblock |
| 2700 | * members for conversion decisions. |
| 2701 | */ |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 2702 | rs_config_backup(rs, &rs_layout); |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 2703 | |
Heinz Mauelshagen | 73c6f23 | 2016-05-19 18:49:24 +0200 | [diff] [blame] | 2704 | r = analyse_superblocks(ti, rs); |
| 2705 | if (r) |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2706 | goto bad; |
| 2707 | |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2708 | INIT_WORK(&rs->md.event_work, do_table_event); |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2709 | ti->private = rs; |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 2710 | ti->num_flush_bios = 1; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2711 | |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 2712 | /* Restore any requested new layout for conversion decision */ |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 2713 | rs_config_restore(rs, &rs_layout); |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 2714 | |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 2715 | if (test_bit(MD_ARRAY_FIRST_USE, &rs->md.flags)) { |
| 2716 | set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags); |
| 2717 | rs_set_new(rs); |
| 2718 | } else if (rs_is_reshaping(rs)) |
| 2719 | ; /* skip rs setup */ |
| 2720 | else if (rs_takeover_requested(rs)) { |
| 2721 | if (rs_is_reshaping(rs)) { |
| 2722 | ti->error = "Can't takeover a reshaping raid set"; |
| 2723 | return -EPERM; |
| 2724 | } |
| 2725 | |
| 2726 | /* |
| 2727 | * If a takeover is needed, just set the level to |
| 2728 | * the new requested one and allow the raid set to run. |
| 2729 | */ |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 2730 | r = rs_check_takeover(rs); |
| 2731 | if (r) |
| 2732 | return r; |
| 2733 | |
| 2734 | r = rs_setup_takeover(rs); |
| 2735 | if (r) |
| 2736 | return r; |
| 2737 | |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 2738 | set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags); |
Heinz Mauelshagen | 6e20902 | 2016-06-14 15:23:13 -0400 | [diff] [blame] | 2739 | set_bit(RT_FLAG_KEEP_RS_FROZEN, &rs->runtime_flags); |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 2740 | rs_set_new(rs); |
Heinz Mauelshagen | 40ba37e | 2016-06-13 17:55:13 +0200 | [diff] [blame] | 2741 | } else if (rs_reshape_requested(rs)) { |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 2742 | if (rs_is_reshaping(rs)) { |
| 2743 | ti->error = "raid set already reshaping!"; |
| 2744 | return -EPERM; |
| 2745 | } |
| 2746 | |
| 2747 | if (rs_is_raid10(rs)) { |
| 2748 | if (rs->raid_disks != rs->md.raid_disks && |
| 2749 | __is_raid10_near(rs->md.layout) && |
| 2750 | rs->raid10_copies && |
| 2751 | rs->raid10_copies != __raid10_near_copies(rs->md.layout)) { |
| 2752 | /* |
| 2753 | * raid disk have to be multiple of data copies to allow this conversion, |
| 2754 | * |
| 2755 | * This is actually not a reshape it is a |
| 2756 | * rebuild of any additional mirrors per group |
| 2757 | */ |
| 2758 | if (rs->raid_disks % rs->raid10_copies) { |
| 2759 | ti->error = "Can't reshape raid10 mirror groups"; |
| 2760 | return -EINVAL; |
| 2761 | } |
| 2762 | |
| 2763 | /* Userpace reordered disks to add/remove mirrors -> adjust raid_disk indexes */ |
| 2764 | __reorder_raid_disk_indexes(rs); |
| 2765 | rs->md.layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_NEAR, |
| 2766 | rs->raid10_copies); |
| 2767 | rs->md.new_layout = rs->md.layout; |
| 2768 | |
| 2769 | } else |
| 2770 | set_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags); |
| 2771 | |
| 2772 | } else if (rs_is_raid456(rs)) |
| 2773 | set_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags); |
| 2774 | |
| 2775 | /* |
| 2776 | * HM FIXME: process raid1 via delta_disks as well? |
| 2777 | * Would cause allocations in raid1->check_reshape |
| 2778 | * though, thus more issues with potential failures |
| 2779 | */ |
Heinz Mauelshagen | 6e20902 | 2016-06-14 15:23:13 -0400 | [diff] [blame] | 2780 | else if (rs_is_raid1(rs)) { |
| 2781 | set_bit(RT_FLAG_KEEP_RS_FROZEN, &rs->runtime_flags); |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 2782 | rs->md.raid_disks = rs->raid_disks; |
Heinz Mauelshagen | 6e20902 | 2016-06-14 15:23:13 -0400 | [diff] [blame] | 2783 | } |
| 2784 | |
| 2785 | if (test_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags)) { |
| 2786 | set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags); |
| 2787 | set_bit(RT_FLAG_KEEP_RS_FROZEN, &rs->runtime_flags); |
| 2788 | } |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 2789 | |
| 2790 | if (rs->md.raid_disks < rs->raid_disks) |
| 2791 | set_bit(MD_ARRAY_FIRST_USE, &rs->md.flags); |
| 2792 | |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 2793 | rs_set_cur(rs); |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 2794 | } else |
| 2795 | rs_set_cur(rs); |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 2796 | |
Heinz Mauelshagen | 40ba37e | 2016-06-13 17:55:13 +0200 | [diff] [blame] | 2797 | /* If constructor requested it, change data and new_data offsets */ |
| 2798 | r = rs_adjust_data_offsets(rs); |
| 2799 | if (r) |
| 2800 | return r; |
| 2801 | |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 2802 | /* Start raid set read-only and assumed clean to change in raid_resume() */ |
| 2803 | rs->md.ro = 1; |
| 2804 | rs->md.in_sync = 1; |
| 2805 | set_bit(MD_RECOVERY_FROZEN, &rs->md.recovery); |
Heinz Mauelshagen | 75b8e04 | 2014-09-24 17:47:18 +0200 | [diff] [blame] | 2806 | |
Heinz Mauelshagen | 0cf4503 | 2015-04-29 14:03:04 +0200 | [diff] [blame] | 2807 | /* Has to be held on running the array */ |
| 2808 | mddev_lock_nointr(&rs->md); |
Heinz Mauelshagen | 73c6f23 | 2016-05-19 18:49:24 +0200 | [diff] [blame] | 2809 | r = md_run(&rs->md); |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2810 | rs->md.in_sync = 0; /* Assume already marked dirty */ |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2811 | |
Heinz Mauelshagen | 73c6f23 | 2016-05-19 18:49:24 +0200 | [diff] [blame] | 2812 | if (r) { |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 2813 | ti->error = "Failed to run raid array"; |
| 2814 | mddev_unlock(&rs->md); |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2815 | goto bad; |
| 2816 | } |
| 2817 | |
| 2818 | rs->callbacks.congested_fn = raid_is_congested; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2819 | dm_table_add_target_callbacks(ti->table, &rs->callbacks); |
| 2820 | |
Jonathan Brassow | 3273727 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 2821 | mddev_suspend(&rs->md); |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 2822 | |
| 2823 | /* Try to adjust the raid4/5/6 stripe cache size to the stripe size */ |
| 2824 | if (rs_is_raid456(rs)) { |
| 2825 | r = rs_set_raid456_stripe_cache(rs); |
| 2826 | if (r) |
| 2827 | goto bad_stripe_cache; |
| 2828 | } |
| 2829 | |
| 2830 | /* Now do an early reshape check */ |
| 2831 | if (test_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags)) { |
| 2832 | r = rs_check_reshape(rs); |
| 2833 | if (r) |
| 2834 | return r; |
| 2835 | |
| 2836 | /* Restore new, ctr requested layout to perform check */ |
| 2837 | rs_config_restore(rs, &rs_layout); |
| 2838 | |
| 2839 | r = rs->md.pers->check_reshape(&rs->md); |
| 2840 | if (r) { |
| 2841 | ti->error = "Reshape check failed"; |
| 2842 | goto bad_check_reshape; |
| 2843 | } |
| 2844 | } |
| 2845 | |
| 2846 | mddev_unlock(&rs->md); |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2847 | return 0; |
| 2848 | |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 2849 | bad_stripe_cache: |
| 2850 | bad_check_reshape: |
Jonathan Brassow | 63f33b8d | 2012-07-31 21:44:26 -0500 | [diff] [blame] | 2851 | md_stop(&rs->md); |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2852 | bad: |
Mike Snitzer | bfcee0e | 2016-06-02 15:08:09 -0400 | [diff] [blame] | 2853 | raid_set_free(rs); |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2854 | |
Heinz Mauelshagen | 73c6f23 | 2016-05-19 18:49:24 +0200 | [diff] [blame] | 2855 | return r; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2856 | } |
| 2857 | |
| 2858 | static void raid_dtr(struct dm_target *ti) |
| 2859 | { |
| 2860 | struct raid_set *rs = ti->private; |
| 2861 | |
| 2862 | list_del_init(&rs->callbacks.list); |
| 2863 | md_stop(&rs->md); |
Mike Snitzer | bfcee0e | 2016-06-02 15:08:09 -0400 | [diff] [blame] | 2864 | raid_set_free(rs); |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2865 | } |
| 2866 | |
Mikulas Patocka | 7de3ee5 | 2012-12-21 20:23:41 +0000 | [diff] [blame] | 2867 | static int raid_map(struct dm_target *ti, struct bio *bio) |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2868 | { |
| 2869 | struct raid_set *rs = ti->private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2870 | struct mddev *mddev = &rs->md; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2871 | |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 2872 | /* |
| 2873 | * If we're reshaping to add disk(s)), ti->len and |
| 2874 | * mddev->array_sectors will differ during the process |
| 2875 | * (ti->len > mddev->array_sectors), so we have to requeue |
| 2876 | * bios with addresses > mddev->array_sectors here or |
| 2877 | * or there will occur accesses past EOD of the component |
| 2878 | * data images thus erroring the raid set. |
| 2879 | */ |
| 2880 | if (unlikely(bio_end_sector(bio) > mddev->array_sectors)) |
| 2881 | return DM_MAPIO_REQUEUE; |
| 2882 | |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 2883 | mddev->pers->make_request(mddev, bio); |
| 2884 | |
| 2885 | return DM_MAPIO_SUBMITTED; |
| 2886 | } |
| 2887 | |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 2888 | /* Return string describing the current sync action of @mddev */ |
Jonathan Brassow | be83651 | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 2889 | static const char *decipher_sync_action(struct mddev *mddev) |
| 2890 | { |
| 2891 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 2892 | return "frozen"; |
| 2893 | |
| 2894 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 2895 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
| 2896 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 2897 | return "reshape"; |
| 2898 | |
| 2899 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 2900 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 2901 | return "resync"; |
| 2902 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 2903 | return "check"; |
| 2904 | return "repair"; |
| 2905 | } |
| 2906 | |
| 2907 | if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
| 2908 | return "recover"; |
| 2909 | } |
| 2910 | |
| 2911 | return "idle"; |
| 2912 | } |
| 2913 | |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 2914 | /* |
| 2915 | * Return status string @rdev |
| 2916 | * |
| 2917 | * Status characters: |
| 2918 | * |
| 2919 | * 'D' = Dead/Failed device |
| 2920 | * 'a' = Alive but not in-sync |
| 2921 | * 'A' = Alive and in-sync |
| 2922 | */ |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 2923 | static const char *__raid_dev_status(struct md_rdev *rdev, bool array_in_sync) |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 2924 | { |
| 2925 | if (test_bit(Faulty, &rdev->flags)) |
| 2926 | return "D"; |
| 2927 | else if (!array_in_sync || !test_bit(In_sync, &rdev->flags)) |
| 2928 | return "a"; |
| 2929 | else |
| 2930 | return "A"; |
| 2931 | } |
| 2932 | |
| 2933 | /* Helper to return resync/reshape progress for @rs and @array_in_sync */ |
| 2934 | static sector_t rs_get_progress(struct raid_set *rs, |
| 2935 | sector_t resync_max_sectors, bool *array_in_sync) |
| 2936 | { |
| 2937 | sector_t r, recovery_cp, curr_resync_completed; |
| 2938 | struct mddev *mddev = &rs->md; |
| 2939 | |
| 2940 | curr_resync_completed = mddev->curr_resync_completed ?: mddev->recovery_cp; |
| 2941 | recovery_cp = mddev->recovery_cp; |
| 2942 | *array_in_sync = false; |
| 2943 | |
| 2944 | if (rs_is_raid0(rs)) { |
| 2945 | r = resync_max_sectors; |
| 2946 | *array_in_sync = true; |
| 2947 | |
| 2948 | } else { |
| 2949 | r = mddev->reshape_position; |
| 2950 | |
| 2951 | /* Reshape is relative to the array size */ |
| 2952 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) || |
| 2953 | r != MaxSector) { |
| 2954 | if (r == MaxSector) { |
| 2955 | *array_in_sync = true; |
| 2956 | r = resync_max_sectors; |
| 2957 | } else { |
| 2958 | /* Got to reverse on backward reshape */ |
| 2959 | if (mddev->reshape_backwards) |
| 2960 | r = mddev->array_sectors - r; |
| 2961 | |
| 2962 | /* Devide by # of data stripes */ |
| 2963 | sector_div(r, mddev_data_stripes(rs)); |
| 2964 | } |
| 2965 | |
| 2966 | /* Sync is relative to the component device size */ |
| 2967 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2968 | r = curr_resync_completed; |
| 2969 | else |
| 2970 | r = recovery_cp; |
| 2971 | |
| 2972 | if (r == MaxSector) { |
| 2973 | /* |
| 2974 | * Sync complete. |
| 2975 | */ |
| 2976 | *array_in_sync = true; |
| 2977 | r = resync_max_sectors; |
| 2978 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 2979 | /* |
| 2980 | * If "check" or "repair" is occurring, the raid set has |
| 2981 | * undergone an initial sync and the health characters |
| 2982 | * should not be 'a' anymore. |
| 2983 | */ |
| 2984 | *array_in_sync = true; |
| 2985 | } else { |
| 2986 | struct md_rdev *rdev; |
| 2987 | |
| 2988 | /* |
| 2989 | * The raid set may be doing an initial sync, or it may |
Mike Snitzer | 4315784 | 2016-05-30 13:03:37 -0400 | [diff] [blame] | 2990 | * be rebuilding individual components. If all the |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 2991 | * devices are In_sync, then it is the raid set that is |
| 2992 | * being initialized. |
| 2993 | */ |
| 2994 | rdev_for_each(rdev, mddev) |
| 2995 | if (!test_bit(In_sync, &rdev->flags)) |
| 2996 | *array_in_sync = true; |
| 2997 | #if 0 |
| 2998 | r = 0; /* HM FIXME: TESTME: https://bugzilla.redhat.com/show_bug.cgi?id=1210637 ? */ |
| 2999 | #endif |
| 3000 | } |
| 3001 | } |
| 3002 | |
| 3003 | return r; |
| 3004 | } |
| 3005 | |
| 3006 | /* Helper to return @dev name or "-" if !@dev */ |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 3007 | static const char *__get_dev_name(struct dm_dev *dev) |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3008 | { |
| 3009 | return dev ? dev->name : "-"; |
| 3010 | } |
| 3011 | |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 3012 | static void raid_status(struct dm_target *ti, status_type_t type, |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3013 | unsigned int status_flags, char *result, unsigned int maxlen) |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 3014 | { |
| 3015 | struct raid_set *rs = ti->private; |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3016 | struct mddev *mddev = &rs->md; |
| 3017 | struct r5conf *conf = mddev->private; |
| 3018 | int max_nr_stripes = conf ? conf->max_nr_stripes : 0; |
| 3019 | bool array_in_sync; |
| 3020 | unsigned int raid_param_cnt = 1; /* at least 1 for chunksize */ |
| 3021 | unsigned int sz = 0; |
| 3022 | unsigned int write_mostly_params = 0; |
| 3023 | sector_t progress, resync_max_sectors, resync_mismatches; |
| 3024 | const char *sync_action; |
| 3025 | struct raid_type *rt; |
| 3026 | struct md_rdev *rdev; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 3027 | |
| 3028 | switch (type) { |
| 3029 | case STATUSTYPE_INFO: |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3030 | /* *Should* always succeed */ |
| 3031 | rt = get_raid_type_by_ll(mddev->new_level, mddev->new_layout); |
| 3032 | if (!rt) |
| 3033 | return; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 3034 | |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 3035 | DMEMIT("%s %d ", rt->name, mddev->raid_disks); |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 3036 | |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3037 | /* Access most recent mddev properties for status output */ |
| 3038 | smp_rmb(); |
| 3039 | /* Get sensible max sectors even if raid set not yet started */ |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 3040 | resync_max_sectors = test_bit(RT_FLAG_RS_PRERESUMED, &rs->runtime_flags) ? |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3041 | mddev->resync_max_sectors : mddev->dev_sectors; |
| 3042 | progress = rs_get_progress(rs, resync_max_sectors, &array_in_sync); |
| 3043 | resync_mismatches = (mddev->last_sync_action && !strcasecmp(mddev->last_sync_action, "check")) ? |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 3044 | atomic64_read(&mddev->resync_mismatches) : 0; |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3045 | sync_action = decipher_sync_action(&rs->md); |
| 3046 | |
| 3047 | /* HM FIXME: do we want another state char for raid0? It shows 'D' or 'A' now */ |
| 3048 | rdev_for_each(rdev, mddev) |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 3049 | DMEMIT(__raid_dev_status(rdev, array_in_sync)); |
Jonathan Brassow | be83651 | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 3050 | |
Jonathan E Brassow | 2e727c3 | 2011-10-31 20:21:26 +0000 | [diff] [blame] | 3051 | /* |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3052 | * In-sync/Reshape ratio: |
Jonathan E Brassow | 2e727c3 | 2011-10-31 20:21:26 +0000 | [diff] [blame] | 3053 | * The in-sync ratio shows the progress of: |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3054 | * - Initializing the raid set |
| 3055 | * - Rebuilding a subset of devices of the raid set |
Jonathan E Brassow | 2e727c3 | 2011-10-31 20:21:26 +0000 | [diff] [blame] | 3056 | * The user can distinguish between the two by referring |
| 3057 | * to the status characters. |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3058 | * |
| 3059 | * The reshape ratio shows the progress of |
| 3060 | * changing the raid layout or the number of |
| 3061 | * disks of a raid set |
Jonathan E Brassow | 2e727c3 | 2011-10-31 20:21:26 +0000 | [diff] [blame] | 3062 | */ |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3063 | DMEMIT(" %llu/%llu", (unsigned long long) progress, |
| 3064 | (unsigned long long) resync_max_sectors); |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 3065 | |
Jonathan Brassow | be83651 | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 3066 | /* |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3067 | * v1.5.0+: |
| 3068 | * |
Jonathan Brassow | be83651 | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 3069 | * Sync action: |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3070 | * See Documentation/device-mapper/dm-raid.txt for |
Jonathan Brassow | be83651 | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 3071 | * information on each of these states. |
| 3072 | */ |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3073 | DMEMIT(" %s", sync_action); |
Jonathan Brassow | be83651 | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 3074 | |
| 3075 | /* |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3076 | * v1.5.0+: |
| 3077 | * |
Jonathan Brassow | be83651 | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 3078 | * resync_mismatches/mismatch_cnt |
| 3079 | * This field shows the number of discrepancies found when |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3080 | * performing a "check" of the raid set. |
Jonathan Brassow | be83651 | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 3081 | */ |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3082 | DMEMIT(" %llu", (unsigned long long) resync_mismatches); |
| 3083 | |
| 3084 | /* |
Mike Snitzer | 9b6e542 | 2016-06-02 11:48:09 -0400 | [diff] [blame] | 3085 | * v1.9.0+: |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3086 | * |
| 3087 | * data_offset (needed for out of space reshaping) |
| 3088 | * This field shows the data offset into the data |
| 3089 | * image LV where the first stripes data starts. |
| 3090 | * |
| 3091 | * We keep data_offset equal on all raid disks of the set, |
| 3092 | * so retrieving it from the first raid disk is sufficient. |
| 3093 | */ |
| 3094 | DMEMIT(" %llu", (unsigned long long) rs->dev[0].rdev.data_offset); |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 3095 | break; |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3096 | |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 3097 | case STATUSTYPE_TABLE: |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3098 | /* Report the table line string you would use to construct this raid set */ |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 3099 | |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3100 | /* Calculate raid parameter count */ |
| 3101 | rdev_for_each(rdev, mddev) |
| 3102 | if (test_bit(WriteMostly, &rdev->flags)) |
| 3103 | write_mostly_params += 2; |
| 3104 | raid_param_cnt += memweight(rs->rebuild_disks, |
| 3105 | DISKS_ARRAY_ELEMS * sizeof(*rs->rebuild_disks)) * 2 + |
| 3106 | write_mostly_params + |
| 3107 | hweight32(rs->ctr_flags & CTR_FLAG_OPTIONS_NO_ARGS) + |
| 3108 | hweight32(rs->ctr_flags & CTR_FLAG_OPTIONS_ONE_ARG) * 2; |
| 3109 | /* Emit table line */ |
| 3110 | DMEMIT("%s %u %u", rs->raid_type->name, raid_param_cnt, mddev->new_chunk_sectors); |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 3111 | if (test_bit(__CTR_FLAG_RAID10_FORMAT, &rs->ctr_flags)) |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 3112 | DMEMIT(" %s %s", dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_FORMAT), |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3113 | raid10_md_layout_to_format(mddev->layout)); |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 3114 | if (test_bit(__CTR_FLAG_RAID10_COPIES, &rs->ctr_flags)) |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 3115 | DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_COPIES), |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3116 | raid10_md_layout_to_copies(mddev->layout)); |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 3117 | if (test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 3118 | DMEMIT(" %s", dm_raid_arg_name_by_flag(CTR_FLAG_NOSYNC)); |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 3119 | if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags)) |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 3120 | DMEMIT(" %s", dm_raid_arg_name_by_flag(CTR_FLAG_SYNC)); |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 3121 | if (test_bit(__CTR_FLAG_REGION_SIZE, &rs->ctr_flags)) |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 3122 | DMEMIT(" %s %llu", dm_raid_arg_name_by_flag(CTR_FLAG_REGION_SIZE), |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3123 | (unsigned long long) to_sector(mddev->bitmap_info.chunksize)); |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 3124 | if (test_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags)) |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 3125 | DMEMIT(" %s %llu", dm_raid_arg_name_by_flag(CTR_FLAG_DATA_OFFSET), |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3126 | (unsigned long long) rs->data_offset); |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 3127 | if (test_bit(__CTR_FLAG_DAEMON_SLEEP, &rs->ctr_flags)) |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 3128 | DMEMIT(" %s %lu", dm_raid_arg_name_by_flag(CTR_FLAG_DAEMON_SLEEP), |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3129 | mddev->bitmap_info.daemon_sleep); |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 3130 | if (test_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags)) |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 3131 | DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_DELTA_DISKS), |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3132 | mddev->delta_disks); |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 3133 | if (test_bit(__CTR_FLAG_STRIPE_CACHE, &rs->ctr_flags)) |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 3134 | DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_STRIPE_CACHE), |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3135 | max_nr_stripes); |
| 3136 | rdev_for_each(rdev, mddev) |
| 3137 | if (test_bit(rdev->raid_disk, (void *) rs->rebuild_disks)) |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 3138 | DMEMIT(" %s %u", dm_raid_arg_name_by_flag(CTR_FLAG_REBUILD), |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3139 | rdev->raid_disk); |
| 3140 | rdev_for_each(rdev, mddev) |
| 3141 | if (test_bit(WriteMostly, &rdev->flags)) |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 3142 | DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_WRITE_MOSTLY), |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3143 | rdev->raid_disk); |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 3144 | if (test_bit(__CTR_FLAG_MAX_WRITE_BEHIND, &rs->ctr_flags)) |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 3145 | DMEMIT(" %s %lu", dm_raid_arg_name_by_flag(CTR_FLAG_MAX_WRITE_BEHIND), |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3146 | mddev->bitmap_info.max_write_behind); |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 3147 | if (test_bit(__CTR_FLAG_MAX_RECOVERY_RATE, &rs->ctr_flags)) |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 3148 | DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_MAX_RECOVERY_RATE), |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3149 | mddev->sync_speed_max); |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 3150 | if (test_bit(__CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags)) |
Mike Snitzer | 3fa6cf3 | 2016-06-02 11:58:51 -0400 | [diff] [blame] | 3151 | DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_MIN_RECOVERY_RATE), |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3152 | mddev->sync_speed_min); |
| 3153 | DMEMIT(" %d", rs->raid_disks); |
| 3154 | rdev_for_each(rdev, mddev) { |
| 3155 | struct raid_dev *rd = container_of(rdev, struct raid_dev, rdev); |
Jonathan Brassow | 63f33b8d | 2012-07-31 21:44:26 -0500 | [diff] [blame] | 3156 | |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 3157 | DMEMIT(" %s %s", __get_dev_name(rd->meta_dev), |
| 3158 | __get_dev_name(rd->data_dev)); |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 3159 | } |
| 3160 | } |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 3161 | } |
| 3162 | |
Jonathan Brassow | be83651 | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 3163 | static int raid_message(struct dm_target *ti, unsigned argc, char **argv) |
| 3164 | { |
| 3165 | struct raid_set *rs = ti->private; |
| 3166 | struct mddev *mddev = &rs->md; |
| 3167 | |
Jonathan Brassow | be83651 | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 3168 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3169 | return -EINVAL; |
| 3170 | |
| 3171 | if (!strcasecmp(argv[0], "frozen")) |
| 3172 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3173 | else |
| 3174 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3175 | |
| 3176 | if (!strcasecmp(argv[0], "idle") || !strcasecmp(argv[0], "frozen")) { |
| 3177 | if (mddev->sync_thread) { |
| 3178 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3179 | md_reap_sync_thread(mddev); |
| 3180 | } |
| 3181 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3182 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 3183 | return -EBUSY; |
| 3184 | else if (!strcasecmp(argv[0], "resync")) |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3185 | ; /* MD_RECOVERY_NEEDED set below */ |
| 3186 | else if (!strcasecmp(argv[0], "recover")) |
Jonathan Brassow | be83651 | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 3187 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3188 | else { |
Jonathan Brassow | be83651 | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 3189 | if (!strcasecmp(argv[0], "check")) |
| 3190 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 3191 | else if (!!strcasecmp(argv[0], "repair")) |
| 3192 | return -EINVAL; |
| 3193 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3194 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 3195 | } |
| 3196 | if (mddev->ro == 2) { |
| 3197 | /* A write to sync_action is enough to justify |
| 3198 | * canceling read-auto mode |
| 3199 | */ |
| 3200 | mddev->ro = 0; |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3201 | if (!mddev->suspended && mddev->sync_thread) |
Jonathan Brassow | be83651 | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 3202 | md_wakeup_thread(mddev->sync_thread); |
| 3203 | } |
| 3204 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3205 | if (!mddev->suspended && mddev->thread) |
Jonathan Brassow | be83651 | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 3206 | md_wakeup_thread(mddev->thread); |
| 3207 | |
| 3208 | return 0; |
| 3209 | } |
| 3210 | |
| 3211 | static int raid_iterate_devices(struct dm_target *ti, |
| 3212 | iterate_devices_callout_fn fn, void *data) |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 3213 | { |
| 3214 | struct raid_set *rs = ti->private; |
| 3215 | unsigned i; |
Heinz Mauelshagen | 73c6f23 | 2016-05-19 18:49:24 +0200 | [diff] [blame] | 3216 | int r = 0; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 3217 | |
Heinz Mauelshagen | 73c6f23 | 2016-05-19 18:49:24 +0200 | [diff] [blame] | 3218 | for (i = 0; !r && i < rs->md.raid_disks; i++) |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 3219 | if (rs->dev[i].data_dev) |
Heinz Mauelshagen | 73c6f23 | 2016-05-19 18:49:24 +0200 | [diff] [blame] | 3220 | r = fn(ti, |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 3221 | rs->dev[i].data_dev, |
| 3222 | 0, /* No offset on data devs */ |
| 3223 | rs->md.dev_sectors, |
| 3224 | data); |
| 3225 | |
Heinz Mauelshagen | 73c6f23 | 2016-05-19 18:49:24 +0200 | [diff] [blame] | 3226 | return r; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 3227 | } |
| 3228 | |
| 3229 | static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits) |
| 3230 | { |
| 3231 | struct raid_set *rs = ti->private; |
| 3232 | unsigned chunk_size = rs->md.chunk_sectors << 9; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3233 | struct r5conf *conf = rs->md.private; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 3234 | |
| 3235 | blk_limits_io_min(limits, chunk_size); |
| 3236 | blk_limits_io_opt(limits, chunk_size * (conf->raid_disks - conf->max_degraded)); |
| 3237 | } |
| 3238 | |
| 3239 | static void raid_presuspend(struct dm_target *ti) |
| 3240 | { |
| 3241 | struct raid_set *rs = ti->private; |
| 3242 | |
| 3243 | md_stop_writes(&rs->md); |
| 3244 | } |
| 3245 | |
| 3246 | static void raid_postsuspend(struct dm_target *ti) |
| 3247 | { |
| 3248 | struct raid_set *rs = ti->private; |
| 3249 | |
Heinz Mauelshagen | 6e20902 | 2016-06-14 15:23:13 -0400 | [diff] [blame] | 3250 | if (test_and_clear_bit(RT_FLAG_RS_RESUMED, &rs->runtime_flags)) { |
| 3251 | if (!rs->md.suspended) |
| 3252 | mddev_suspend(&rs->md); |
| 3253 | rs->md.ro = 1; |
| 3254 | } |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 3255 | } |
| 3256 | |
Jonathan Brassow | f381e71 | 2013-05-08 17:57:13 -0500 | [diff] [blame] | 3257 | static void attempt_restore_of_faulty_devices(struct raid_set *rs) |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 3258 | { |
Jonathan Brassow | 9092c02 | 2013-05-02 14:19:24 -0500 | [diff] [blame] | 3259 | int i; |
| 3260 | uint64_t failed_devices, cleared_failed_devices = 0; |
| 3261 | unsigned long flags; |
| 3262 | struct dm_raid_superblock *sb; |
Jonathan Brassow | 9092c02 | 2013-05-02 14:19:24 -0500 | [diff] [blame] | 3263 | struct md_rdev *r; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 3264 | |
Jonathan Brassow | f381e71 | 2013-05-08 17:57:13 -0500 | [diff] [blame] | 3265 | for (i = 0; i < rs->md.raid_disks; i++) { |
| 3266 | r = &rs->dev[i].rdev; |
| 3267 | if (test_bit(Faulty, &r->flags) && r->sb_page && |
Heinz Mauelshagen | 0a7b818 | 2016-06-15 18:45:56 +0200 | [diff] [blame] | 3268 | sync_page_io(r, 0, r->sb_size, r->sb_page, |
| 3269 | REQ_OP_READ, 0, true)) { |
Jonathan Brassow | f381e71 | 2013-05-08 17:57:13 -0500 | [diff] [blame] | 3270 | DMINFO("Faulty %s device #%d has readable super block." |
| 3271 | " Attempting to revive it.", |
| 3272 | rs->raid_type->name, i); |
Jonathan Brassow | a4dc163 | 2013-05-08 18:00:54 -0500 | [diff] [blame] | 3273 | |
| 3274 | /* |
| 3275 | * Faulty bit may be set, but sometimes the array can |
| 3276 | * be suspended before the personalities can respond |
| 3277 | * by removing the device from the array (i.e. calling |
Mike Snitzer | 4315784 | 2016-05-30 13:03:37 -0400 | [diff] [blame] | 3278 | * 'hot_remove_disk'). If they haven't yet removed |
Jonathan Brassow | a4dc163 | 2013-05-08 18:00:54 -0500 | [diff] [blame] | 3279 | * the failed device, its 'raid_disk' number will be |
| 3280 | * '>= 0' - meaning we must call this function |
| 3281 | * ourselves. |
| 3282 | */ |
| 3283 | if ((r->raid_disk >= 0) && |
| 3284 | (r->mddev->pers->hot_remove_disk(r->mddev, r) != 0)) |
| 3285 | /* Failed to revive this device, try next */ |
| 3286 | continue; |
| 3287 | |
Jonathan Brassow | f381e71 | 2013-05-08 17:57:13 -0500 | [diff] [blame] | 3288 | r->raid_disk = i; |
| 3289 | r->saved_raid_disk = i; |
| 3290 | flags = r->flags; |
| 3291 | clear_bit(Faulty, &r->flags); |
| 3292 | clear_bit(WriteErrorSeen, &r->flags); |
| 3293 | clear_bit(In_sync, &r->flags); |
| 3294 | if (r->mddev->pers->hot_add_disk(r->mddev, r)) { |
| 3295 | r->raid_disk = -1; |
| 3296 | r->saved_raid_disk = -1; |
| 3297 | r->flags = flags; |
| 3298 | } else { |
| 3299 | r->recovery_offset = 0; |
| 3300 | cleared_failed_devices |= 1 << i; |
| 3301 | } |
| 3302 | } |
| 3303 | } |
| 3304 | if (cleared_failed_devices) { |
| 3305 | rdev_for_each(r, &rs->md) { |
| 3306 | sb = page_address(r->sb_page); |
| 3307 | failed_devices = le64_to_cpu(sb->failed_devices); |
| 3308 | failed_devices &= ~cleared_failed_devices; |
| 3309 | sb->failed_devices = cpu_to_le64(failed_devices); |
| 3310 | } |
| 3311 | } |
| 3312 | } |
| 3313 | |
Mike Snitzer | e6ca5e1 | 2016-06-02 15:27:22 -0400 | [diff] [blame] | 3314 | static int __load_dirty_region_bitmap(struct raid_set *rs) |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 3315 | { |
| 3316 | int r = 0; |
| 3317 | |
| 3318 | /* Try loading the bitmap unless "raid0", which does not have one */ |
| 3319 | if (!rs_is_raid0(rs) && |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 3320 | !test_and_set_bit(RT_FLAG_RS_BITMAP_LOADED, &rs->runtime_flags)) { |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 3321 | r = bitmap_load(&rs->md); |
| 3322 | if (r) |
| 3323 | DMERR("Failed to load bitmap"); |
| 3324 | } |
| 3325 | |
| 3326 | return r; |
| 3327 | } |
| 3328 | |
Heinz Mauelshagen | 6e20902 | 2016-06-14 15:23:13 -0400 | [diff] [blame] | 3329 | /* Enforce updating all superblocks */ |
| 3330 | static void rs_update_sbs(struct raid_set *rs) |
| 3331 | { |
| 3332 | struct mddev *mddev = &rs->md; |
| 3333 | int ro = mddev->ro; |
| 3334 | |
| 3335 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 3336 | mddev->ro = 0; |
| 3337 | md_update_sb(mddev, 1); |
| 3338 | mddev->ro = ro; |
| 3339 | } |
| 3340 | |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 3341 | /* |
| 3342 | * Reshape changes raid algorithm of @rs to new one within personality |
| 3343 | * (e.g. raid6_zr -> raid6_nc), changes stripe size, adds/removes |
| 3344 | * disks from a raid set thus growing/shrinking it or resizes the set |
| 3345 | * |
| 3346 | * Call mddev_lock_nointr() before! |
| 3347 | */ |
| 3348 | static int rs_start_reshape(struct raid_set *rs) |
| 3349 | { |
| 3350 | int r; |
| 3351 | struct mddev *mddev = &rs->md; |
| 3352 | struct md_personality *pers = mddev->pers; |
| 3353 | |
| 3354 | r = rs_setup_reshape(rs); |
| 3355 | if (r) |
| 3356 | return r; |
| 3357 | |
| 3358 | /* Need to be resumed to be able to start reshape, recovery is frozen until raid_resume() though */ |
| 3359 | if (mddev->suspended) |
| 3360 | mddev_resume(mddev); |
| 3361 | |
| 3362 | /* |
| 3363 | * Check any reshape constraints enforced by the personalility |
| 3364 | * |
| 3365 | * May as well already kick the reshape off so that * pers->start_reshape() becomes optional. |
| 3366 | */ |
| 3367 | r = pers->check_reshape(mddev); |
| 3368 | if (r) { |
| 3369 | rs->ti->error = "pers->check_reshape() failed"; |
| 3370 | return r; |
| 3371 | } |
| 3372 | |
| 3373 | /* |
| 3374 | * Personality may not provide start reshape method in which |
| 3375 | * case check_reshape above has already covered everything |
| 3376 | */ |
| 3377 | if (pers->start_reshape) { |
| 3378 | r = pers->start_reshape(mddev); |
| 3379 | if (r) { |
| 3380 | rs->ti->error = "pers->start_reshape() failed"; |
| 3381 | return r; |
| 3382 | } |
| 3383 | } |
| 3384 | |
| 3385 | /* Suspend because a resume will happen in raid_resume() */ |
| 3386 | if (!mddev->suspended) |
| 3387 | mddev_suspend(mddev); |
| 3388 | |
Heinz Mauelshagen | 6e20902 | 2016-06-14 15:23:13 -0400 | [diff] [blame] | 3389 | /* |
| 3390 | * Now reshape got set up, update superblocks to |
| 3391 | * reflect the fact so that a table reload will |
| 3392 | * access proper superblock content in the ctr. |
| 3393 | */ |
| 3394 | rs_update_sbs(rs); |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 3395 | |
| 3396 | return 0; |
| 3397 | } |
| 3398 | |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 3399 | static int raid_preresume(struct dm_target *ti) |
| 3400 | { |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 3401 | int r; |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 3402 | struct raid_set *rs = ti->private; |
| 3403 | struct mddev *mddev = &rs->md; |
| 3404 | |
| 3405 | /* This is a resume after a suspend of the set -> it's already started */ |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 3406 | if (test_and_set_bit(RT_FLAG_RS_PRERESUMED, &rs->runtime_flags)) |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 3407 | return 0; |
| 3408 | |
| 3409 | /* |
| 3410 | * The superblocks need to be updated on disk if the |
Heinz Mauelshagen | 6e20902 | 2016-06-14 15:23:13 -0400 | [diff] [blame] | 3411 | * array is new or new devices got added (thus zeroed |
| 3412 | * out by userspace) or __load_dirty_region_bitmap |
| 3413 | * will overwrite them in core with old data or fail. |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 3414 | */ |
Heinz Mauelshagen | 6e20902 | 2016-06-14 15:23:13 -0400 | [diff] [blame] | 3415 | if (test_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags)) |
| 3416 | rs_update_sbs(rs); |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 3417 | |
| 3418 | /* |
| 3419 | * Disable/enable discard support on raid set after any |
| 3420 | * conversion, because devices can have been added |
| 3421 | */ |
| 3422 | configure_discard_support(rs); |
| 3423 | |
| 3424 | /* Load the bitmap from disk unless raid0 */ |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 3425 | r = __load_dirty_region_bitmap(rs); |
| 3426 | if (r) |
| 3427 | return r; |
| 3428 | |
Heinz Mauelshagen | 4257e08 | 2016-06-14 01:46:03 +0200 | [diff] [blame] | 3429 | /* Resize bitmap to adjust to changed region size (aka MD bitmap chunksize) */ |
| 3430 | if (test_bit(RT_FLAG_RS_BITMAP_LOADED, &rs->runtime_flags) && |
| 3431 | mddev->bitmap_info.chunksize != to_bytes(rs->requested_bitmap_chunk_sectors)) { |
| 3432 | r = bitmap_resize(mddev->bitmap, mddev->dev_sectors, |
| 3433 | to_bytes(rs->requested_bitmap_chunk_sectors), 0); |
| 3434 | if (r) |
| 3435 | DMERR("Failed to resize bitmap"); |
| 3436 | } |
| 3437 | |
Heinz Mauelshagen | 9dbd1aa | 2016-06-13 17:55:14 +0200 | [diff] [blame] | 3438 | /* Check for any resize/reshape on @rs and adjust/initiate */ |
| 3439 | /* Be prepared for mddev_resume() in raid_resume() */ |
| 3440 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3441 | if (mddev->recovery_cp && mddev->recovery_cp < MaxSector) { |
| 3442 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3443 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 3444 | mddev->resync_min = mddev->recovery_cp; |
| 3445 | } |
| 3446 | |
| 3447 | rs_set_capacity(rs); |
| 3448 | |
| 3449 | /* Check for any reshape request and region size change unless new raid set */ |
| 3450 | if (test_and_clear_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags)) { |
| 3451 | /* Initiate a reshape. */ |
| 3452 | mddev_lock_nointr(mddev); |
| 3453 | r = rs_start_reshape(rs); |
| 3454 | mddev_unlock(mddev); |
| 3455 | if (r) |
| 3456 | DMWARN("Failed to check/start reshape, continuing without change"); |
| 3457 | r = 0; |
| 3458 | } |
| 3459 | |
| 3460 | return r; |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 3461 | } |
| 3462 | |
Jonathan Brassow | f381e71 | 2013-05-08 17:57:13 -0500 | [diff] [blame] | 3463 | static void raid_resume(struct dm_target *ti) |
| 3464 | { |
| 3465 | struct raid_set *rs = ti->private; |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 3466 | struct mddev *mddev = &rs->md; |
Jonathan Brassow | f381e71 | 2013-05-08 17:57:13 -0500 | [diff] [blame] | 3467 | |
Mike Snitzer | 4286325 | 2016-06-02 12:27:46 -0400 | [diff] [blame] | 3468 | if (test_and_set_bit(RT_FLAG_RS_RESUMED, &rs->runtime_flags)) { |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 3469 | /* |
| 3470 | * A secondary resume while the device is active. |
| 3471 | * Take this opportunity to check whether any failed |
| 3472 | * devices are reachable again. |
| 3473 | */ |
| 3474 | attempt_restore_of_faulty_devices(rs); |
Heinz Mauelshagen | 6e20902 | 2016-06-14 15:23:13 -0400 | [diff] [blame] | 3475 | } else { |
| 3476 | mddev->ro = 0; |
| 3477 | mddev->in_sync = 0; |
| 3478 | |
| 3479 | /* |
| 3480 | * When passing in flags to the ctr, we expect userspace |
| 3481 | * to reset them because they made it to the superblocks |
| 3482 | * and reload the mapping anyway. |
| 3483 | * |
| 3484 | * -> only unfreeze recovery in case of a table reload or |
| 3485 | * we'll have a bogus recovery/reshape position |
| 3486 | * retrieved from the superblock by the ctr because |
| 3487 | * the ongoing recovery/reshape will change it after read. |
| 3488 | */ |
| 3489 | if (!test_bit(RT_FLAG_KEEP_RS_FROZEN, &rs->runtime_flags)) |
| 3490 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3491 | |
| 3492 | if (mddev->suspended) |
| 3493 | mddev_resume(mddev); |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 3494 | } |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 3495 | } |
| 3496 | |
| 3497 | static struct target_type raid_target = { |
| 3498 | .name = "raid", |
Mike Snitzer | 9b6e542 | 2016-06-02 11:48:09 -0400 | [diff] [blame] | 3499 | .version = {1, 9, 0}, |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 3500 | .module = THIS_MODULE, |
| 3501 | .ctr = raid_ctr, |
| 3502 | .dtr = raid_dtr, |
| 3503 | .map = raid_map, |
| 3504 | .status = raid_status, |
Jonathan Brassow | be83651 | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 3505 | .message = raid_message, |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 3506 | .iterate_devices = raid_iterate_devices, |
| 3507 | .io_hints = raid_io_hints, |
| 3508 | .presuspend = raid_presuspend, |
| 3509 | .postsuspend = raid_postsuspend, |
Heinz Mauelshagen | ecbfb9f | 2016-05-19 18:49:33 +0200 | [diff] [blame] | 3510 | .preresume = raid_preresume, |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 3511 | .resume = raid_resume, |
| 3512 | }; |
| 3513 | |
| 3514 | static int __init dm_raid_init(void) |
| 3515 | { |
Jonathan Brassow | fe5d2f4 | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 3516 | DMINFO("Loading target version %u.%u.%u", |
| 3517 | raid_target.version[0], |
| 3518 | raid_target.version[1], |
| 3519 | raid_target.version[2]); |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 3520 | return dm_register_target(&raid_target); |
| 3521 | } |
| 3522 | |
| 3523 | static void __exit dm_raid_exit(void) |
| 3524 | { |
| 3525 | dm_unregister_target(&raid_target); |
| 3526 | } |
| 3527 | |
| 3528 | module_init(dm_raid_init); |
| 3529 | module_exit(dm_raid_exit); |
| 3530 | |
Heinz Mauelshagen | 48cf06b | 2014-09-24 17:47:19 +0200 | [diff] [blame] | 3531 | module_param(devices_handle_discard_safely, bool, 0644); |
| 3532 | MODULE_PARM_DESC(devices_handle_discard_safely, |
| 3533 | "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions"); |
| 3534 | |
Mike Snitzer | ef9b85a | 2016-06-02 12:02:19 -0400 | [diff] [blame] | 3535 | MODULE_DESCRIPTION(DM_NAME " raid0/1/10/4/5/6 target"); |
| 3536 | MODULE_ALIAS("dm-raid0"); |
Jonathan Brassow | 63f33b8d | 2012-07-31 21:44:26 -0500 | [diff] [blame] | 3537 | MODULE_ALIAS("dm-raid1"); |
| 3538 | MODULE_ALIAS("dm-raid10"); |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 3539 | MODULE_ALIAS("dm-raid4"); |
| 3540 | MODULE_ALIAS("dm-raid5"); |
| 3541 | MODULE_ALIAS("dm-raid6"); |
| 3542 | MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>"); |
Heinz Mauelshagen | 3a1c1ef | 2016-05-19 18:49:34 +0200 | [diff] [blame] | 3543 | MODULE_AUTHOR("Heinz Mauelshagen <dm-devel@redhat.com>"); |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 3544 | MODULE_LICENSE("GPL"); |