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