blob: b82b7095a67190196e6ca14f287a09983661d743 [file] [log] [blame]
NeilBrown9d09e662011-01-13 20:00:02 +00001/*
2 * Copyright (C) 2010-2011 Neil Brown
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01003 * Copyright (C) 2010-2017 Red Hat, Inc. All rights reserved.
NeilBrown9d09e662011-01-13 20:00:02 +00004 *
5 * This file is released under the GPL.
6 */
7
8#include <linux/slab.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -04009#include <linux/module.h>
NeilBrown9d09e662011-01-13 20:00:02 +000010
11#include "md.h"
Jonathan Brassow32737272011-08-02 12:32:07 +010012#include "raid1.h"
NeilBrown9d09e662011-01-13 20:00:02 +000013#include "raid5.h"
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -050014#include "raid10.h"
Mike Snitzer935fe092017-10-10 17:02:41 -040015#include "md-bitmap.h"
NeilBrown9d09e662011-01-13 20:00:02 +000016
Alasdair G Kergon3e8dbb72011-08-02 12:32:03 +010017#include <linux/device-mapper.h>
18
NeilBrown9d09e662011-01-13 20:00:02 +000019#define DM_MSG_PREFIX "raid"
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +020020#define MAX_RAID_DEVICES 253 /* md-raid kernel limit */
NeilBrown9d09e662011-01-13 20:00:02 +000021
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +020022/*
23 * Minimum sectors of free reshape space per raid device
24 */
25#define MIN_FREE_RESHAPE_SPACE to_sector(4*4096)
26
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +010027/*
28 * Minimum journal space 4 MiB in sectors.
29 */
30#define MIN_RAID456_JOURNAL_SPACE (4*2048)
31
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +020032static bool devices_handle_discard_safely = false;
33
NeilBrown9d09e662011-01-13 20:00:02 +000034/*
Jonathan Brassowb12d4372011-08-02 12:32:07 +010035 * 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.
NeilBrown9d09e662011-01-13 20:00:02 +000037 */
Mike Snitzer43157842016-05-30 13:03:37 -040038#define FirstUse 10 /* rdev flag */
NeilBrown9d09e662011-01-13 20:00:02 +000039
40struct raid_dev {
41 /*
42 * Two DM devices, one to hold metadata and one to hold the
Mike Snitzer43157842016-05-30 13:03:37 -040043 * actual data/parity. The reason for this is to not confuse
NeilBrown9d09e662011-01-13 20:00:02 +000044 * 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;
NeilBrown3cb03002011-10-11 16:45:26 +110055 struct md_rdev rdev;
NeilBrown9d09e662011-01-13 20:00:02 +000056};
57
58/*
Mike Snitzer42863252016-06-02 12:27:46 -040059 * Bits for establishing rs->ctr_flags
Heinz Mauelshagen702108d2016-05-19 18:49:26 +020060 *
61 * 1 = no flag value
62 * 2 = flag with value
NeilBrown9d09e662011-01-13 20:00:02 +000063 */
Mike Snitzer42863252016-06-02 12:27:46 -040064#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 Snitzer9b6e5422016-06-02 11:48:09 -040076/* New for v1.9.0 */
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +020077#define __CTR_FLAG_DELTA_DISKS 12 /* 2 */ /* Only with reshapable raid1/4/5/6/10! */
Mike Snitzer42863252016-06-02 12:27:46 -040078#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 Mauelshagen63c32ed2016-11-30 22:31:05 +010081/* New for v1.10.0 */
Heinz Mauelshagen6e536362017-03-22 17:44:38 +010082#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 Mauelshagen63c32ed2016-11-30 22:31:05 +010086
Mike Snitzer42863252016-06-02 12:27:46 -040087/*
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 Mauelshagen63c32ed2016-11-30 22:31:05 +0100105#define CTR_FLAG_JOURNAL_DEV (1 << __CTR_FLAG_JOURNAL_DEV)
Heinz Mauelshagen6e536362017-03-22 17:44:38 +0100106#define CTR_FLAG_JOURNAL_MODE (1 << __CTR_FLAG_JOURNAL_MODE)
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500107
Mike Snitzer2664f3c2017-02-28 15:31:44 -0500108#define RESUME_STAY_FROZEN_FLAGS (CTR_FLAG_DELTA_DISKS | CTR_FLAG_DATA_OFFSET)
109
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200110/*
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 Mauelshagen33e53f02016-05-19 18:49:30 +0200119#define CTR_FLAG_OPTIONS_NO_ARGS (CTR_FLAGS_ANY_SYNC | \
120 CTR_FLAG_RAID10_USE_NEAR_SETS)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200121
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 Mauelshagen33e53f02016-05-19 18:49:30 +0200132 CTR_FLAG_RAID10_FORMAT | \
133 CTR_FLAG_DELTA_DISKS | \
134 CTR_FLAG_DATA_OFFSET)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200135
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200136/* Valid options definitions per raid level... */
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200137
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200138/* "raid0" does only accept data offset */
139#define RAID0_VALID_FLAGS (CTR_FLAG_DATA_OFFSET)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200140
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200141/* "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 Mauelshagen7a7c3302016-06-30 13:57:08 +0200150 CTR_FLAG_DELTA_DISKS | \
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200151 CTR_FLAG_DATA_OFFSET)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200152
153/* "raid10" does not accept any raid1 or stripe cache options */
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200154#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 Mauelshagenf0902792016-05-19 18:49:27 +0200166/*
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 Mauelshagena30cbc02016-06-09 16:42:16 +0200173#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 Mauelshagena30cbc02016-06-09 16:42:16 +0200178 CTR_FLAG_STRIPE_CACHE | \
179 CTR_FLAG_REGION_SIZE | \
180 CTR_FLAG_DELTA_DISKS | \
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100181 CTR_FLAG_DATA_OFFSET | \
Heinz Mauelshagen6e536362017-03-22 17:44:38 +0100182 CTR_FLAG_JOURNAL_DEV | \
183 CTR_FLAG_JOURNAL_MODE)
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200184
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 Mauelshagena30cbc02016-06-09 16:42:16 +0200190 CTR_FLAG_STRIPE_CACHE | \
191 CTR_FLAG_REGION_SIZE | \
192 CTR_FLAG_DELTA_DISKS | \
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100193 CTR_FLAG_DATA_OFFSET | \
Heinz Mauelshagen6e536362017-03-22 17:44:38 +0100194 CTR_FLAG_JOURNAL_DEV | \
195 CTR_FLAG_JOURNAL_MODE)
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200196/* ...valid options definitions per raid level */
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200197
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200198/*
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 Mauelshagen40ba37e2016-06-13 17:55:13 +0200206#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 Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200210#define RT_FLAG_RESHAPE_RS 4
Heinz Mauelshagen0cf352e2017-07-13 17:34:24 +0200211#define RT_FLAG_RS_SUSPENDED 5
Heinz Mauelshagen242ea5a2017-12-02 01:03:54 +0100212#define RT_FLAG_RS_IN_SYNC 6
Heinz Mauelshagen4102d9d2017-12-02 01:03:55 +0100213#define RT_FLAG_RS_RESYNCING 7
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200214
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200215/* Array elements of 64 bit needed for rebuild/failed disk bits */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200216#define DISKS_ARRAY_ELEMS ((MAX_RAID_DEVICES + (sizeof(uint64_t) * 8 - 1)) / sizeof(uint64_t) / 8)
217
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200218/*
219 * raid set level, layout and chunk sectors backup/restore
220 */
221struct rs_layout {
222 int new_level;
223 int new_layout;
224 int new_chunk_sectors;
225};
226
NeilBrown9d09e662011-01-13 20:00:02 +0000227struct raid_set {
228 struct dm_target *ti;
229
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200230 uint32_t stripe_cache_entries;
Mike Snitzer42863252016-06-02 12:27:46 -0400231 unsigned long ctr_flags;
232 unsigned long runtime_flags;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200233
234 uint64_t rebuild_disks[DISKS_ARRAY_ELEMS];
NeilBrown9d09e662011-01-13 20:00:02 +0000235
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200236 int raid_disks;
237 int delta_disks;
Heinz Mauelshagen4763e542016-05-19 18:49:31 +0200238 int data_offset;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200239 int raid10_copies;
Heinz Mauelshagen4257e082016-06-14 01:46:03 +0200240 int requested_bitmap_chunk_sectors;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200241
NeilBrownfd01b882011-10-11 16:47:53 +1100242 struct mddev md;
NeilBrown9d09e662011-01-13 20:00:02 +0000243 struct raid_type *raid_type;
244 struct dm_target_callbacks callbacks;
245
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100246 /* Optional raid4/5/6 journal device */
247 struct journal_dev {
248 struct dm_dev *dev;
249 struct md_rdev rdev;
Heinz Mauelshagen6e536362017-03-22 17:44:38 +0100250 int mode;
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100251 } journal_dev;
252
NeilBrown9d09e662011-01-13 20:00:02 +0000253 struct raid_dev dev[0];
254};
255
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200256static void rs_config_backup(struct raid_set *rs, struct rs_layout *l)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200257{
258 struct mddev *mddev = &rs->md;
259
260 l->new_level = mddev->new_level;
261 l->new_layout = mddev->new_layout;
262 l->new_chunk_sectors = mddev->new_chunk_sectors;
263}
264
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200265static void rs_config_restore(struct raid_set *rs, struct rs_layout *l)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200266{
267 struct mddev *mddev = &rs->md;
268
269 mddev->new_level = l->new_level;
270 mddev->new_layout = l->new_layout;
271 mddev->new_chunk_sectors = l->new_chunk_sectors;
272}
273
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200274/* raid10 algorithms (i.e. formats) */
275#define ALGORITHM_RAID10_DEFAULT 0
276#define ALGORITHM_RAID10_NEAR 1
277#define ALGORITHM_RAID10_OFFSET 2
278#define ALGORITHM_RAID10_FAR 3
279
NeilBrown9d09e662011-01-13 20:00:02 +0000280/* Supported raid types and properties. */
281static struct raid_type {
282 const char *name; /* RAID algorithm. */
283 const char *descr; /* Descriptor text for logging. */
Heinz Mauelshagen094f3942016-07-19 14:03:51 +0200284 const unsigned int parity_devs; /* # of parity devices. */
285 const unsigned int minimal_devs;/* minimal # of devices in set. */
286 const unsigned int level; /* RAID level. */
287 const unsigned int algorithm; /* RAID algorithm. */
NeilBrown9d09e662011-01-13 20:00:02 +0000288} raid_types[] = {
Mike Snitzer43157842016-05-30 13:03:37 -0400289 {"raid0", "raid0 (striping)", 0, 2, 0, 0 /* NONE */},
290 {"raid1", "raid1 (mirroring)", 0, 2, 1, 0 /* NONE */},
291 {"raid10_far", "raid10 far (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_FAR},
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200292 {"raid10_offset", "raid10 offset (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_OFFSET},
Mike Snitzer43157842016-05-30 13:03:37 -0400293 {"raid10_near", "raid10 near (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_NEAR},
294 {"raid10", "raid10 (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_DEFAULT},
Heinz Mauelshagenb052b072016-10-17 21:20:07 +0200295 {"raid4", "raid4 (dedicated first parity disk)", 1, 2, 5, ALGORITHM_PARITY_0}, /* raid4 layout = raid5_0 */
Mike Snitzer43157842016-05-30 13:03:37 -0400296 {"raid5_n", "raid5 (dedicated last parity disk)", 1, 2, 5, ALGORITHM_PARITY_N},
297 {"raid5_ls", "raid5 (left symmetric)", 1, 2, 5, ALGORITHM_LEFT_SYMMETRIC},
298 {"raid5_rs", "raid5 (right symmetric)", 1, 2, 5, ALGORITHM_RIGHT_SYMMETRIC},
299 {"raid5_la", "raid5 (left asymmetric)", 1, 2, 5, ALGORITHM_LEFT_ASYMMETRIC},
300 {"raid5_ra", "raid5 (right asymmetric)", 1, 2, 5, ALGORITHM_RIGHT_ASYMMETRIC},
301 {"raid6_zr", "raid6 (zero restart)", 2, 4, 6, ALGORITHM_ROTATING_ZERO_RESTART},
302 {"raid6_nr", "raid6 (N restart)", 2, 4, 6, ALGORITHM_ROTATING_N_RESTART},
303 {"raid6_nc", "raid6 (N continue)", 2, 4, 6, ALGORITHM_ROTATING_N_CONTINUE},
304 {"raid6_n_6", "raid6 (dedicated parity/Q n/6)", 2, 4, 6, ALGORITHM_PARITY_N_6},
305 {"raid6_ls_6", "raid6 (left symmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_LEFT_SYMMETRIC_6},
306 {"raid6_rs_6", "raid6 (right symmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_RIGHT_SYMMETRIC_6},
307 {"raid6_la_6", "raid6 (left asymmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_LEFT_ASYMMETRIC_6},
308 {"raid6_ra_6", "raid6 (right asymmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_RIGHT_ASYMMETRIC_6}
NeilBrown9d09e662011-01-13 20:00:02 +0000309};
310
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200311/* True, if @v is in inclusive range [@min, @max] */
Mike Snitzerbb91a632016-06-02 12:06:54 -0400312static bool __within_range(long v, long min, long max)
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200313{
314 return v >= min && v <= max;
315}
316
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200317/* All table line arguments are defined here */
318static struct arg_name_flag {
Mike Snitzer42863252016-06-02 12:27:46 -0400319 const unsigned long flag;
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200320 const char *name;
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400321} __arg_name_flags[] = {
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200322 { CTR_FLAG_SYNC, "sync"},
323 { CTR_FLAG_NOSYNC, "nosync"},
324 { CTR_FLAG_REBUILD, "rebuild"},
325 { CTR_FLAG_DAEMON_SLEEP, "daemon_sleep"},
326 { CTR_FLAG_MIN_RECOVERY_RATE, "min_recovery_rate"},
327 { CTR_FLAG_MAX_RECOVERY_RATE, "max_recovery_rate"},
328 { CTR_FLAG_MAX_WRITE_BEHIND, "max_write_behind"},
Heinz Mauelshagen65359ee2016-06-24 21:32:25 +0200329 { CTR_FLAG_WRITE_MOSTLY, "write_mostly"},
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200330 { CTR_FLAG_STRIPE_CACHE, "stripe_cache"},
331 { CTR_FLAG_REGION_SIZE, "region_size"},
332 { CTR_FLAG_RAID10_COPIES, "raid10_copies"},
333 { CTR_FLAG_RAID10_FORMAT, "raid10_format"},
Heinz Mauelshagen4763e542016-05-19 18:49:31 +0200334 { CTR_FLAG_DATA_OFFSET, "data_offset"},
335 { CTR_FLAG_DELTA_DISKS, "delta_disks"},
336 { CTR_FLAG_RAID10_USE_NEAR_SETS, "raid10_use_near_sets"},
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100337 { CTR_FLAG_JOURNAL_DEV, "journal_dev" },
Heinz Mauelshagen6e536362017-03-22 17:44:38 +0100338 { CTR_FLAG_JOURNAL_MODE, "journal_mode" },
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200339};
340
341/* Return argument name string for given @flag */
Mike Snitzer3fa6cf32016-06-02 11:58:51 -0400342static const char *dm_raid_arg_name_by_flag(const uint32_t flag)
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200343{
344 if (hweight32(flag) == 1) {
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400345 struct arg_name_flag *anf = __arg_name_flags + ARRAY_SIZE(__arg_name_flags);
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200346
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400347 while (anf-- > __arg_name_flags)
Mike Snitzer42863252016-06-02 12:27:46 -0400348 if (flag & anf->flag)
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200349 return anf->name;
350
351 } else
352 DMERR("%s called with more than one flag!", __func__);
353
354 return NULL;
355}
356
Heinz Mauelshagen6e536362017-03-22 17:44:38 +0100357/* Define correlation of raid456 journal cache modes and dm-raid target line parameters */
358static struct {
359 const int mode;
360 const char *param;
361} _raid456_journal_mode[] = {
362 { R5C_JOURNAL_MODE_WRITE_THROUGH , "writethrough" },
363 { R5C_JOURNAL_MODE_WRITE_BACK , "writeback" }
364};
365
366/* Return MD raid4/5/6 journal mode for dm @journal_mode one */
367static int dm_raid_journal_mode_to_md(const char *mode)
368{
369 int m = ARRAY_SIZE(_raid456_journal_mode);
370
371 while (m--)
372 if (!strcasecmp(mode, _raid456_journal_mode[m].param))
373 return _raid456_journal_mode[m].mode;
374
375 return -EINVAL;
376}
377
378/* Return dm-raid raid4/5/6 journal mode string for @mode */
379static const char *md_journal_mode_to_dm_raid(const int mode)
380{
381 int m = ARRAY_SIZE(_raid456_journal_mode);
382
383 while (m--)
384 if (mode == _raid456_journal_mode[m].mode)
385 return _raid456_journal_mode[m].param;
386
387 return "unknown";
388}
389
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200390/*
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200391 * Bool helpers to test for various raid levels of a raid set.
392 * It's level as reported by the superblock rather than
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200393 * the requested raid_type passed to the constructor.
394 */
395/* Return true, if raid set in @rs is raid0 */
396static bool rs_is_raid0(struct raid_set *rs)
397{
398 return !rs->md.level;
399}
400
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200401/* Return true, if raid set in @rs is raid1 */
402static bool rs_is_raid1(struct raid_set *rs)
403{
404 return rs->md.level == 1;
405}
406
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200407/* Return true, if raid set in @rs is raid10 */
408static bool rs_is_raid10(struct raid_set *rs)
409{
410 return rs->md.level == 10;
411}
412
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +0200413/* Return true, if raid set in @rs is level 6 */
414static bool rs_is_raid6(struct raid_set *rs)
415{
416 return rs->md.level == 6;
417}
418
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +0200419/* Return true, if raid set in @rs is level 4, 5 or 6 */
420static bool rs_is_raid456(struct raid_set *rs)
421{
422 return __within_range(rs->md.level, 4, 6);
423}
424
425/* Return true, if raid set in @rs is reshapable */
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200426static bool __is_raid10_far(int layout);
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +0200427static bool rs_is_reshapable(struct raid_set *rs)
428{
429 return rs_is_raid456(rs) ||
430 (rs_is_raid10(rs) && !__is_raid10_far(rs->md.new_layout));
431}
432
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200433/* Return true, if raid set in @rs is recovering */
434static bool rs_is_recovering(struct raid_set *rs)
435{
Heinz Mauelshagen50c4feb2017-01-14 03:53:08 +0100436 return rs->md.recovery_cp < rs->md.dev_sectors;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200437}
438
439/* Return true, if raid set in @rs is reshaping */
440static bool rs_is_reshaping(struct raid_set *rs)
441{
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200442 return rs->md.reshape_position != MaxSector;
443}
444
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200445/*
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200446 * bool helpers to test for various raid levels of a raid type @rt
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200447 */
448
449/* Return true, if raid type in @rt is raid0 */
450static bool rt_is_raid0(struct raid_type *rt)
451{
452 return !rt->level;
453}
454
455/* Return true, if raid type in @rt is raid1 */
456static bool rt_is_raid1(struct raid_type *rt)
457{
458 return rt->level == 1;
459}
460
461/* Return true, if raid type in @rt is raid10 */
462static bool rt_is_raid10(struct raid_type *rt)
463{
464 return rt->level == 10;
465}
466
467/* Return true, if raid type in @rt is raid4/5 */
468static bool rt_is_raid45(struct raid_type *rt)
469{
Mike Snitzerbb91a632016-06-02 12:06:54 -0400470 return __within_range(rt->level, 4, 5);
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200471}
472
473/* Return true, if raid type in @rt is raid6 */
474static bool rt_is_raid6(struct raid_type *rt)
475{
476 return rt->level == 6;
477}
Heinz Mauelshagen676fa5a2016-05-19 18:49:29 +0200478
479/* Return true, if raid type in @rt is raid4/5/6 */
480static bool rt_is_raid456(struct raid_type *rt)
481{
Mike Snitzerbb91a632016-06-02 12:06:54 -0400482 return __within_range(rt->level, 4, 6);
Heinz Mauelshagen676fa5a2016-05-19 18:49:29 +0200483}
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200484/* END: raid level bools */
485
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200486/* Return valid ctr flags for the raid level of @rs */
487static unsigned long __valid_flags(struct raid_set *rs)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200488{
489 if (rt_is_raid0(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200490 return RAID0_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200491 else if (rt_is_raid1(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200492 return RAID1_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200493 else if (rt_is_raid10(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200494 return RAID10_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200495 else if (rt_is_raid45(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200496 return RAID45_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200497 else if (rt_is_raid6(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200498 return RAID6_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200499
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200500 return 0;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200501}
502
503/*
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200504 * Check for valid flags set on @rs
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200505 *
506 * Has to be called after parsing of the ctr flags!
507 */
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200508static int rs_check_for_valid_flags(struct raid_set *rs)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200509{
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200510 if (rs->ctr_flags & ~__valid_flags(rs)) {
Mike Snitzer42863252016-06-02 12:27:46 -0400511 rs->ti->error = "Invalid flags combination";
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400512 return -EINVAL;
513 }
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200514
515 return 0;
516}
517
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200518/* MD raid10 bit definitions and helpers */
519#define RAID10_OFFSET (1 << 16) /* stripes with data copies area adjacent on devices */
520#define RAID10_BROCKEN_USE_FAR_SETS (1 << 17) /* Broken in raid10.c: use sets instead of whole stripe rotation */
521#define RAID10_USE_FAR_SETS (1 << 18) /* Use sets instead of whole stripe rotation */
522#define RAID10_FAR_COPIES_SHIFT 8 /* raid10 # far copies shift (2nd byte of layout) */
523
524/* Return md raid10 near copies for @layout */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400525static unsigned int __raid10_near_copies(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200526{
527 return layout & 0xFF;
528}
529
530/* Return md raid10 far copies for @layout */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400531static unsigned int __raid10_far_copies(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200532{
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400533 return __raid10_near_copies(layout >> RAID10_FAR_COPIES_SHIFT);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200534}
535
536/* Return true if md raid10 offset for @layout */
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200537static bool __is_raid10_offset(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200538{
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200539 return !!(layout & RAID10_OFFSET);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200540}
541
542/* Return true if md raid10 near for @layout */
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200543static bool __is_raid10_near(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200544{
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400545 return !__is_raid10_offset(layout) && __raid10_near_copies(layout) > 1;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200546}
547
548/* Return true if md raid10 far for @layout */
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200549static bool __is_raid10_far(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200550{
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400551 return !__is_raid10_offset(layout) && __raid10_far_copies(layout) > 1;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200552}
553
554/* Return md raid10 layout string for @layout */
555static const char *raid10_md_layout_to_format(int layout)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100556{
557 /*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200558 * Bit 16 stands for "offset"
559 * (i.e. adjacent stripes hold copies)
560 *
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100561 * Refer to MD's raid10.c for details
562 */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400563 if (__is_raid10_offset(layout))
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100564 return "offset";
565
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400566 if (__raid10_near_copies(layout) > 1)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100567 return "near";
568
Heinz Mauelshagenbbac1e02017-07-13 17:33:22 +0200569 if (__raid10_far_copies(layout) > 1)
570 return "far";
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200571
Heinz Mauelshagenbbac1e02017-07-13 17:33:22 +0200572 return "unknown";
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100573}
574
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200575/* Return md raid10 algorithm for @name */
Arnd Bergmann68c1c4d2016-06-16 11:03:25 +0200576static int raid10_name_to_format(const char *name)
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500577{
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200578 if (!strcasecmp(name, "near"))
579 return ALGORITHM_RAID10_NEAR;
580 else if (!strcasecmp(name, "offset"))
581 return ALGORITHM_RAID10_OFFSET;
582 else if (!strcasecmp(name, "far"))
583 return ALGORITHM_RAID10_FAR;
584
585 return -EINVAL;
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500586}
587
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200588/* Return md raid10 copies for @layout */
589static unsigned int raid10_md_layout_to_copies(int layout)
590{
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200591 return max(__raid10_near_copies(layout), __raid10_far_copies(layout));
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200592}
593
594/* Return md raid10 format id for @format string */
595static int raid10_format_to_md_layout(struct raid_set *rs,
596 unsigned int algorithm,
597 unsigned int copies)
598{
599 unsigned int n = 1, f = 1, r = 0;
600
601 /*
602 * MD resilienece flaw:
603 *
604 * enabling use_far_sets for far/offset formats causes copies
605 * to be colocated on the same devs together with their origins!
606 *
607 * -> disable it for now in the definition above
608 */
609 if (algorithm == ALGORITHM_RAID10_DEFAULT ||
610 algorithm == ALGORITHM_RAID10_NEAR)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100611 n = copies;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200612
613 else if (algorithm == ALGORITHM_RAID10_OFFSET) {
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100614 f = copies;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200615 r = RAID10_OFFSET;
Mike Snitzer42863252016-06-02 12:27:46 -0400616 if (!test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200617 r |= RAID10_USE_FAR_SETS;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100618
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200619 } else if (algorithm == ALGORITHM_RAID10_FAR) {
620 f = copies;
621 r = !RAID10_OFFSET;
Mike Snitzer42863252016-06-02 12:27:46 -0400622 if (!test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200623 r |= RAID10_USE_FAR_SETS;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100624
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200625 } else
626 return -EINVAL;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100627
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200628 return r | (f << RAID10_FAR_COPIES_SHIFT) | n;
629}
630/* END: MD raid10 bit definitions and helpers */
631
632/* Check for any of the raid10 algorithms */
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200633static bool __got_raid10(struct raid_type *rtp, const int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200634{
635 if (rtp->level == 10) {
636 switch (rtp->algorithm) {
637 case ALGORITHM_RAID10_DEFAULT:
638 case ALGORITHM_RAID10_NEAR:
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400639 return __is_raid10_near(layout);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200640 case ALGORITHM_RAID10_OFFSET:
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400641 return __is_raid10_offset(layout);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200642 case ALGORITHM_RAID10_FAR:
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400643 return __is_raid10_far(layout);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200644 default:
645 break;
646 }
647 }
648
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200649 return false;
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500650}
651
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200652/* Return raid_type for @name */
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200653static struct raid_type *get_raid_type(const char *name)
NeilBrown9d09e662011-01-13 20:00:02 +0000654{
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200655 struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);
NeilBrown9d09e662011-01-13 20:00:02 +0000656
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200657 while (rtp-- > raid_types)
658 if (!strcasecmp(rtp->name, name))
659 return rtp;
NeilBrown9d09e662011-01-13 20:00:02 +0000660
661 return NULL;
662}
663
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200664/* Return raid_type for @name based derived from @level and @layout */
665static struct raid_type *get_raid_type_by_ll(const int level, const int layout)
666{
667 struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);
668
669 while (rtp-- > raid_types) {
670 /* RAID10 special checks based on @layout flags/properties */
671 if (rtp->level == level &&
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400672 (__got_raid10(rtp, layout) || rtp->algorithm == layout))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200673 return rtp;
674 }
675
676 return NULL;
677}
678
Heinz Mauelshagen61e06e22017-12-02 01:03:51 +0100679/* Adjust rdev sectors */
680static void rs_set_rdev_sectors(struct raid_set *rs)
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200681{
682 struct mddev *mddev = &rs->md;
Heinz Mauelshagenfbe63652016-06-24 00:36:08 +0200683 struct md_rdev *rdev;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200684
Heinz Mauelshagenfbe63652016-06-24 00:36:08 +0200685 /*
686 * raid10 sets rdev->sector to the device size, which
687 * is unintended in case of out-of-place reshaping
688 */
689 rdev_for_each(rdev, mddev)
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100690 if (!test_bit(Journal, &rdev->flags))
691 rdev->sectors = mddev->dev_sectors;
Heinz Mauelshagen61e06e22017-12-02 01:03:51 +0100692}
Heinz Mauelshagenfbe63652016-06-24 00:36:08 +0200693
Heinz Mauelshagen61e06e22017-12-02 01:03:51 +0100694/*
695 * Change bdev capacity of @rs in case of a disk add/remove reshape
696 */
697static void rs_set_capacity(struct raid_set *rs)
698{
699 struct gendisk *gendisk = dm_disk(dm_table_get_md(rs->ti->table));
700
701 set_capacity(gendisk, rs->md.array_sectors);
Heinz Mauelshagen0095dbc2016-06-24 00:10:12 +0200702 revalidate_disk(gendisk);
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200703}
704
705/*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +0200706 * Set the mddev properties in @rs to the current
707 * ones retrieved from the freshest superblock
708 */
709static void rs_set_cur(struct raid_set *rs)
710{
711 struct mddev *mddev = &rs->md;
712
713 mddev->new_level = mddev->level;
714 mddev->new_layout = mddev->layout;
715 mddev->new_chunk_sectors = mddev->chunk_sectors;
716}
717
718/*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200719 * Set the mddev properties in @rs to the new
720 * ones requested by the ctr
721 */
722static void rs_set_new(struct raid_set *rs)
723{
724 struct mddev *mddev = &rs->md;
725
726 mddev->level = mddev->new_level;
727 mddev->layout = mddev->new_layout;
728 mddev->chunk_sectors = mddev->new_chunk_sectors;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +0200729 mddev->raid_disks = rs->raid_disks;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200730 mddev->delta_disks = 0;
731}
732
Mike Snitzerbfcee0e2016-06-02 15:08:09 -0400733static struct raid_set *raid_set_alloc(struct dm_target *ti, struct raid_type *raid_type,
Heinz Mauelshagen094f3942016-07-19 14:03:51 +0200734 unsigned int raid_devs)
NeilBrown9d09e662011-01-13 20:00:02 +0000735{
Heinz Mauelshagen094f3942016-07-19 14:03:51 +0200736 unsigned int i;
NeilBrown9d09e662011-01-13 20:00:02 +0000737 struct raid_set *rs;
NeilBrown9d09e662011-01-13 20:00:02 +0000738
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400739 if (raid_devs <= raid_type->parity_devs) {
740 ti->error = "Insufficient number of devices";
741 return ERR_PTR(-EINVAL);
742 }
NeilBrown9d09e662011-01-13 20:00:02 +0000743
NeilBrown9d09e662011-01-13 20:00:02 +0000744 rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400745 if (!rs) {
746 ti->error = "Cannot allocate raid context";
747 return ERR_PTR(-ENOMEM);
748 }
NeilBrown9d09e662011-01-13 20:00:02 +0000749
750 mddev_init(&rs->md);
751
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200752 rs->raid_disks = raid_devs;
753 rs->delta_disks = 0;
754
NeilBrown9d09e662011-01-13 20:00:02 +0000755 rs->ti = ti;
756 rs->raid_type = raid_type;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200757 rs->stripe_cache_entries = 256;
NeilBrown9d09e662011-01-13 20:00:02 +0000758 rs->md.raid_disks = raid_devs;
759 rs->md.level = raid_type->level;
760 rs->md.new_level = rs->md.level;
NeilBrown9d09e662011-01-13 20:00:02 +0000761 rs->md.layout = raid_type->algorithm;
762 rs->md.new_layout = rs->md.layout;
763 rs->md.delta_disks = 0;
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +0200764 rs->md.recovery_cp = MaxSector;
NeilBrown9d09e662011-01-13 20:00:02 +0000765
766 for (i = 0; i < raid_devs; i++)
767 md_rdev_init(&rs->dev[i].rdev);
768
769 /*
770 * Remaining items to be initialized by further RAID params:
771 * rs->md.persistent
772 * rs->md.external
773 * rs->md.chunk_sectors
774 * rs->md.new_chunk_sectors
Jonathan E Brassowc039c332012-07-27 15:08:04 +0100775 * rs->md.dev_sectors
NeilBrown9d09e662011-01-13 20:00:02 +0000776 */
777
778 return rs;
779}
780
Mike Snitzerbfcee0e2016-06-02 15:08:09 -0400781static void raid_set_free(struct raid_set *rs)
NeilBrown9d09e662011-01-13 20:00:02 +0000782{
783 int i;
784
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100785 if (rs->journal_dev.dev) {
786 md_rdev_clear(&rs->journal_dev.rdev);
787 dm_put_device(rs->ti, rs->journal_dev.dev);
788 }
789
Heinz Mauelshagenffeeac72016-06-30 14:37:50 +0200790 for (i = 0; i < rs->raid_disks; i++) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100791 if (rs->dev[i].meta_dev)
792 dm_put_device(rs->ti, rs->dev[i].meta_dev);
NeilBrown545c8792012-05-22 13:54:30 +1000793 md_rdev_clear(&rs->dev[i].rdev);
NeilBrown9d09e662011-01-13 20:00:02 +0000794 if (rs->dev[i].data_dev)
795 dm_put_device(rs->ti, rs->dev[i].data_dev);
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100796 }
NeilBrown9d09e662011-01-13 20:00:02 +0000797
798 kfree(rs);
799}
800
801/*
802 * For every device we have two words
803 * <meta_dev>: meta device name or '-' if missing
804 * <data_dev>: data device name or '-' if missing
805 *
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100806 * The following are permitted:
807 * - -
808 * - <data_dev>
809 * <meta_dev> <data_dev>
810 *
811 * The following is not allowed:
812 * <meta_dev> -
813 *
814 * This code parses those words. If there is a failure,
Mike Snitzerbfcee0e2016-06-02 15:08:09 -0400815 * the caller must use raid_set_free() to unwind the operations.
NeilBrown9d09e662011-01-13 20:00:02 +0000816 */
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200817static int parse_dev_params(struct raid_set *rs, struct dm_arg_set *as)
NeilBrown9d09e662011-01-13 20:00:02 +0000818{
819 int i;
820 int rebuild = 0;
821 int metadata_available = 0;
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +0200822 int r = 0;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200823 const char *arg;
NeilBrown9d09e662011-01-13 20:00:02 +0000824
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200825 /* Put off the number of raid devices argument to get to dev pairs */
826 arg = dm_shift_arg(as);
827 if (!arg)
828 return -EINVAL;
829
Heinz Mauelshagenffeeac72016-06-30 14:37:50 +0200830 for (i = 0; i < rs->raid_disks; i++) {
NeilBrown9d09e662011-01-13 20:00:02 +0000831 rs->dev[i].rdev.raid_disk = i;
832
833 rs->dev[i].meta_dev = NULL;
834 rs->dev[i].data_dev = NULL;
835
836 /*
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100837 * There are no offsets initially.
838 * Out of place reshape will set them accordingly.
NeilBrown9d09e662011-01-13 20:00:02 +0000839 */
840 rs->dev[i].rdev.data_offset = 0;
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100841 rs->dev[i].rdev.new_data_offset = 0;
NeilBrown9d09e662011-01-13 20:00:02 +0000842 rs->dev[i].rdev.mddev = &rs->md;
843
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200844 arg = dm_shift_arg(as);
845 if (!arg)
846 return -EINVAL;
847
848 if (strcmp(arg, "-")) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400849 r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
850 &rs->dev[i].meta_dev);
851 if (r) {
852 rs->ti->error = "RAID metadata device lookup failure";
853 return r;
854 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100855
856 rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400857 if (!rs->dev[i].rdev.sb_page) {
858 rs->ti->error = "Failed to allocate superblock page";
859 return -ENOMEM;
860 }
NeilBrown9d09e662011-01-13 20:00:02 +0000861 }
862
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200863 arg = dm_shift_arg(as);
864 if (!arg)
865 return -EINVAL;
866
867 if (!strcmp(arg, "-")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000868 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) &&
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400869 (!rs->dev[i].rdev.recovery_offset)) {
870 rs->ti->error = "Drive designated for rebuild not specified";
871 return -EINVAL;
872 }
NeilBrown9d09e662011-01-13 20:00:02 +0000873
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400874 if (rs->dev[i].meta_dev) {
875 rs->ti->error = "No data device supplied with metadata device";
876 return -EINVAL;
877 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100878
NeilBrown9d09e662011-01-13 20:00:02 +0000879 continue;
880 }
881
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400882 r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
883 &rs->dev[i].data_dev);
884 if (r) {
885 rs->ti->error = "RAID device lookup failure";
886 return r;
887 }
NeilBrown9d09e662011-01-13 20:00:02 +0000888
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100889 if (rs->dev[i].meta_dev) {
890 metadata_available = 1;
891 rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev;
892 }
NeilBrown9d09e662011-01-13 20:00:02 +0000893 rs->dev[i].rdev.bdev = rs->dev[i].data_dev->bdev;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +0200894 list_add_tail(&rs->dev[i].rdev.same_set, &rs->md.disks);
NeilBrown9d09e662011-01-13 20:00:02 +0000895 if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
896 rebuild++;
897 }
898
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100899 if (rs->journal_dev.dev)
900 list_add_tail(&rs->journal_dev.rdev.same_set, &rs->md.disks);
901
NeilBrown9d09e662011-01-13 20:00:02 +0000902 if (metadata_available) {
903 rs->md.external = 0;
904 rs->md.persistent = 1;
905 rs->md.major_version = 2;
906 } else if (rebuild && !rs->md.recovery_cp) {
907 /*
908 * Without metadata, we will not be able to tell if the array
909 * is in-sync or not - we must assume it is not. Therefore,
910 * it is impossible to rebuild a drive.
911 *
912 * Even if there is metadata, the on-disk information may
913 * indicate that the array is not in-sync and it will then
914 * fail at that time.
915 *
916 * User could specify 'nosync' option if desperate.
917 */
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400918 rs->ti->error = "Unable to rebuild drive while array is not in-sync";
919 return -EINVAL;
NeilBrown9d09e662011-01-13 20:00:02 +0000920 }
921
922 return 0;
923}
924
925/*
Jonathan Brassowc1084562011-08-02 12:32:07 +0100926 * validate_region_size
927 * @rs
928 * @region_size: region size in sectors. If 0, pick a size (4MiB default).
929 *
930 * Set rs->md.bitmap_info.chunksize (which really refers to 'region size').
931 * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap.
932 *
933 * Returns: 0 on success, -EINVAL on failure.
934 */
935static int validate_region_size(struct raid_set *rs, unsigned long region_size)
936{
937 unsigned long min_region_size = rs->ti->len / (1 << 21);
938
Heinz Mauelshagen9e7d9362016-08-17 15:36:44 +0200939 if (rs_is_raid0(rs))
940 return 0;
941
Jonathan Brassowc1084562011-08-02 12:32:07 +0100942 if (!region_size) {
943 /*
Mike Snitzer43157842016-05-30 13:03:37 -0400944 * Choose a reasonable default. All figures in sectors.
Jonathan Brassowc1084562011-08-02 12:32:07 +0100945 */
946 if (min_region_size > (1 << 13)) {
Jonathan Brassow3a0f9aa2012-12-21 20:23:33 +0000947 /* If not a power of 2, make it the next power of 2 */
Mikulas Patocka042745e2015-10-02 11:17:37 -0400948 region_size = roundup_pow_of_two(min_region_size);
Jonathan Brassowc1084562011-08-02 12:32:07 +0100949 DMINFO("Choosing default region size of %lu sectors",
950 region_size);
Jonathan Brassowc1084562011-08-02 12:32:07 +0100951 } else {
952 DMINFO("Choosing default region size of 4MiB");
953 region_size = 1 << 13; /* sectors */
954 }
955 } else {
956 /*
957 * Validate user-supplied value.
958 */
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400959 if (region_size > rs->ti->len) {
960 rs->ti->error = "Supplied region size is too large";
961 return -EINVAL;
962 }
Jonathan Brassowc1084562011-08-02 12:32:07 +0100963
964 if (region_size < min_region_size) {
965 DMERR("Supplied region_size (%lu sectors) below minimum (%lu)",
966 region_size, min_region_size);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400967 rs->ti->error = "Supplied region size is too small";
968 return -EINVAL;
Jonathan Brassowc1084562011-08-02 12:32:07 +0100969 }
970
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400971 if (!is_power_of_2(region_size)) {
972 rs->ti->error = "Region size is not a power of 2";
973 return -EINVAL;
974 }
Jonathan Brassowc1084562011-08-02 12:32:07 +0100975
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400976 if (region_size < rs->md.chunk_sectors) {
977 rs->ti->error = "Region size is smaller than the chunk size";
978 return -EINVAL;
979 }
Jonathan Brassowc1084562011-08-02 12:32:07 +0100980 }
981
982 /*
983 * Convert sectors to bytes.
984 */
Heinz Mauelshagen89d3d9a2016-07-19 13:16:24 +0200985 rs->md.bitmap_info.chunksize = to_bytes(region_size);
Jonathan Brassowc1084562011-08-02 12:32:07 +0100986
987 return 0;
988}
989
990/*
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600991 * validate_raid_redundancy
Jonathan Brassoweb649122012-10-11 13:40:09 +1100992 * @rs
993 *
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600994 * Determine if there are enough devices in the array that haven't
995 * failed (or are being rebuilt) to form a usable array.
Jonathan Brassoweb649122012-10-11 13:40:09 +1100996 *
997 * Returns: 0 on success, -EINVAL on failure.
998 */
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600999static int validate_raid_redundancy(struct raid_set *rs)
Jonathan Brassoweb649122012-10-11 13:40:09 +11001000{
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02001001 unsigned int i, rebuild_cnt = 0;
1002 unsigned int rebuilds_per_group = 0, copies;
1003 unsigned int group_size, last_group_start;
Jonathan Brassoweb649122012-10-11 13:40:09 +11001004
Jonathan Brassoweb649122012-10-11 13:40:09 +11001005 for (i = 0; i < rs->md.raid_disks; i++)
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001006 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) ||
1007 !rs->dev[i].rdev.sb_page)
Jonathan Brassoweb649122012-10-11 13:40:09 +11001008 rebuild_cnt++;
1009
Heinz Mauelshagen53bf5382017-12-13 17:13:17 +01001010 switch (rs->md.level) {
Heinz Mauelshagen9e7d9362016-08-17 15:36:44 +02001011 case 0:
1012 break;
Jonathan Brassoweb649122012-10-11 13:40:09 +11001013 case 1:
1014 if (rebuild_cnt >= rs->md.raid_disks)
1015 goto too_many;
1016 break;
1017 case 4:
1018 case 5:
1019 case 6:
1020 if (rebuild_cnt > rs->raid_type->parity_devs)
1021 goto too_many;
1022 break;
1023 case 10:
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001024 copies = raid10_md_layout_to_copies(rs->md.new_layout);
Heinz Mauelshagen53bf5382017-12-13 17:13:17 +01001025 if (copies < 2) {
1026 DMERR("Bogus raid10 data copies < 2!");
1027 return -EINVAL;
1028 }
1029
Jonathan Brassow4ec1e362012-10-11 13:40:24 +11001030 if (rebuild_cnt < copies)
1031 break;
1032
1033 /*
1034 * It is possible to have a higher rebuild count for RAID10,
1035 * as long as the failed devices occur in different mirror
1036 * groups (i.e. different stripes).
1037 *
Jonathan Brassow4ec1e362012-10-11 13:40:24 +11001038 * When checking "near" format, make sure no adjacent devices
1039 * have failed beyond what can be handled. In addition to the
1040 * simple case where the number of devices is a multiple of the
1041 * number of copies, we must also handle cases where the number
1042 * of devices is not a multiple of the number of copies.
Mike Snitzer43157842016-05-30 13:03:37 -04001043 * E.g. dev1 dev2 dev3 dev4 dev5
1044 * A A B B C
1045 * C D D E E
Jonathan Brassow4ec1e362012-10-11 13:40:24 +11001046 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001047 if (__is_raid10_near(rs->md.new_layout)) {
Heinz Mauelshagenffeeac72016-06-30 14:37:50 +02001048 for (i = 0; i < rs->md.raid_disks; i++) {
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11001049 if (!(i % copies))
1050 rebuilds_per_group = 0;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001051 if ((!rs->dev[i].rdev.sb_page ||
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001052 !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11001053 (++rebuilds_per_group >= copies))
1054 goto too_many;
1055 }
1056 break;
1057 }
1058
1059 /*
1060 * When checking "far" and "offset" formats, we need to ensure
1061 * that the device that holds its copy is not also dead or
1062 * being rebuilt. (Note that "far" and "offset" formats only
1063 * support two copies right now. These formats also only ever
1064 * use the 'use_far_sets' variant.)
1065 *
1066 * This check is somewhat complicated by the need to account
Mike Snitzer43157842016-05-30 13:03:37 -04001067 * for arrays that are not a multiple of (far) copies. This
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11001068 * results in the need to treat the last (potentially larger)
1069 * set differently.
1070 */
1071 group_size = (rs->md.raid_disks / copies);
1072 last_group_start = (rs->md.raid_disks / group_size) - 1;
1073 last_group_start *= group_size;
1074 for (i = 0; i < rs->md.raid_disks; i++) {
1075 if (!(i % copies) && !(i > last_group_start))
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001076 rebuilds_per_group = 0;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11001077 if ((!rs->dev[i].rdev.sb_page ||
1078 !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
Jonathan Brassow4ec1e362012-10-11 13:40:24 +11001079 (++rebuilds_per_group >= copies))
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11001080 goto too_many;
Jonathan Brassow4ec1e362012-10-11 13:40:24 +11001081 }
1082 break;
Jonathan Brassoweb649122012-10-11 13:40:09 +11001083 default:
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001084 if (rebuild_cnt)
1085 return -EINVAL;
Jonathan Brassoweb649122012-10-11 13:40:09 +11001086 }
1087
1088 return 0;
1089
1090too_many:
Jonathan Brassoweb649122012-10-11 13:40:09 +11001091 return -EINVAL;
1092}
1093
1094/*
NeilBrown9d09e662011-01-13 20:00:02 +00001095 * Possible arguments are...
NeilBrown9d09e662011-01-13 20:00:02 +00001096 * <chunk_size> [optional_args]
1097 *
Jonathan Brassow32737272011-08-02 12:32:07 +01001098 * Argument definitions
1099 * <chunk_size> The number of sectors per disk that
Mike Snitzer43157842016-05-30 13:03:37 -04001100 * will form the "stripe"
Jonathan Brassow32737272011-08-02 12:32:07 +01001101 * [[no]sync] Force or prevent recovery of the
Mike Snitzer43157842016-05-30 13:03:37 -04001102 * entire array
NeilBrown9d09e662011-01-13 20:00:02 +00001103 * [rebuild <idx>] Rebuild the drive indicated by the index
Jonathan Brassow32737272011-08-02 12:32:07 +01001104 * [daemon_sleep <ms>] Time between bitmap daemon work to
Mike Snitzer43157842016-05-30 13:03:37 -04001105 * clear bits
NeilBrown9d09e662011-01-13 20:00:02 +00001106 * [min_recovery_rate <kB/sec/disk>] Throttle RAID initialization
1107 * [max_recovery_rate <kB/sec/disk>] Throttle RAID initialization
Jonathan Brassow46bed2b2011-08-02 12:32:07 +01001108 * [write_mostly <idx>] Indicate a write mostly drive via index
NeilBrown9d09e662011-01-13 20:00:02 +00001109 * [max_write_behind <sectors>] See '-write-behind=' (man mdadm)
1110 * [stripe_cache <sectors>] Stripe cache size for higher RAIDs
Mike Snitzer43157842016-05-30 13:03:37 -04001111 * [region_size <sectors>] Defines granularity of bitmap
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01001112 * [journal_dev <dev>] raid4/5/6 journaling deviice
1113 * (i.e. write hole closing log)
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001114 *
1115 * RAID10-only options:
Mike Snitzer43157842016-05-30 13:03:37 -04001116 * [raid10_copies <# copies>] Number of copies. (Default: 2)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11001117 * [raid10_format <near|far|offset>] Layout algorithm. (Default: near)
NeilBrown9d09e662011-01-13 20:00:02 +00001118 */
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001119static int parse_raid_params(struct raid_set *rs, struct dm_arg_set *as,
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02001120 unsigned int num_raid_params)
NeilBrown9d09e662011-01-13 20:00:02 +00001121{
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001122 int value, raid10_format = ALGORITHM_RAID10_DEFAULT;
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02001123 unsigned int raid10_copies = 2;
1124 unsigned int i, write_mostly = 0;
1125 unsigned int region_size = 0;
Mike Snitzer542f9032012-07-27 15:08:00 +01001126 sector_t max_io_len;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001127 const char *arg, *key;
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001128 struct raid_dev *rd;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001129 struct raid_type *rt = rs->raid_type;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001130
1131 arg = dm_shift_arg(as);
1132 num_raid_params--; /* Account for chunk_size argument */
1133
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001134 if (kstrtoint(arg, 10, &value) < 0) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001135 rs->ti->error = "Bad numerical argument given for chunk_size";
1136 return -EINVAL;
1137 }
NeilBrown9d09e662011-01-13 20:00:02 +00001138
1139 /*
1140 * First, parse the in-order required arguments
Jonathan Brassow32737272011-08-02 12:32:07 +01001141 * "chunk_size" is the only argument of this type.
NeilBrown9d09e662011-01-13 20:00:02 +00001142 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001143 if (rt_is_raid1(rt)) {
Jonathan Brassow32737272011-08-02 12:32:07 +01001144 if (value)
1145 DMERR("Ignoring chunk size parameter for RAID 1");
1146 value = 0;
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001147 } else if (!is_power_of_2(value)) {
1148 rs->ti->error = "Chunk size must be a power of 2";
1149 return -EINVAL;
1150 } else if (value < 8) {
1151 rs->ti->error = "Chunk size value is too small";
1152 return -EINVAL;
1153 }
NeilBrown9d09e662011-01-13 20:00:02 +00001154
1155 rs->md.new_chunk_sectors = rs->md.chunk_sectors = value;
NeilBrown9d09e662011-01-13 20:00:02 +00001156
1157 /*
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001158 * We set each individual device as In_sync with a completed
1159 * 'recovery_offset'. If there has been a device failure or
1160 * replacement then one of the following cases applies:
1161 *
1162 * 1) User specifies 'rebuild'.
Mike Snitzer43157842016-05-30 13:03:37 -04001163 * - Device is reset when param is read.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001164 * 2) A new device is supplied.
Mike Snitzer43157842016-05-30 13:03:37 -04001165 * - No matching superblock found, resets device.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001166 * 3) Device failure was transient and returns on reload.
Mike Snitzer43157842016-05-30 13:03:37 -04001167 * - Failure noticed, resets device for bitmap replay.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001168 * 4) Device hadn't completed recovery after previous failure.
Mike Snitzer43157842016-05-30 13:03:37 -04001169 * - Superblock is read and overrides recovery_offset.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001170 *
1171 * What is found in the superblocks of the devices is always
1172 * authoritative, unless 'rebuild' or '[no]sync' was specified.
1173 */
Heinz Mauelshagenffeeac72016-06-30 14:37:50 +02001174 for (i = 0; i < rs->raid_disks; i++) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001175 set_bit(In_sync, &rs->dev[i].rdev.flags);
1176 rs->dev[i].rdev.recovery_offset = MaxSector;
1177 }
1178
1179 /*
NeilBrown9d09e662011-01-13 20:00:02 +00001180 * Second, parse the unordered optional arguments
1181 */
NeilBrown9d09e662011-01-13 20:00:02 +00001182 for (i = 0; i < num_raid_params; i++) {
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001183 key = dm_shift_arg(as);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001184 if (!key) {
1185 rs->ti->error = "Not enough raid parameters given";
1186 return -EINVAL;
1187 }
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001188
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001189 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_NOSYNC))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001190 if (test_and_set_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001191 rs->ti->error = "Only one 'nosync' argument allowed";
1192 return -EINVAL;
1193 }
NeilBrown9d09e662011-01-13 20:00:02 +00001194 continue;
1195 }
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001196 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_SYNC))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001197 if (test_and_set_bit(__CTR_FLAG_SYNC, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001198 rs->ti->error = "Only one 'sync' argument allowed";
1199 return -EINVAL;
1200 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001201 continue;
1202 }
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001203 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_USE_NEAR_SETS))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001204 if (test_and_set_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001205 rs->ti->error = "Only one 'raid10_use_new_sets' argument allowed";
1206 return -EINVAL;
1207 }
NeilBrown9d09e662011-01-13 20:00:02 +00001208 continue;
1209 }
1210
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001211 arg = dm_shift_arg(as);
1212 i++; /* Account for the argument pairs */
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001213 if (!arg) {
1214 rs->ti->error = "Wrong number of raid parameters given";
1215 return -EINVAL;
1216 }
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001217
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001218 /*
1219 * Parameters that take a string value are checked here.
1220 */
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01001221 /* "raid10_format {near|offset|far} */
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001222 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_FORMAT))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001223 if (test_and_set_bit(__CTR_FLAG_RAID10_FORMAT, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001224 rs->ti->error = "Only one 'raid10_format' argument pair allowed";
1225 return -EINVAL;
1226 }
1227 if (!rt_is_raid10(rt)) {
1228 rs->ti->error = "'raid10_format' is an invalid parameter for this RAID type";
1229 return -EINVAL;
1230 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001231 raid10_format = raid10_name_to_format(arg);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001232 if (raid10_format < 0) {
1233 rs->ti->error = "Invalid 'raid10_format' value given";
1234 return raid10_format;
1235 }
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001236 continue;
1237 }
1238
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01001239 /* "journal_dev <dev>" */
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01001240 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_JOURNAL_DEV))) {
1241 int r;
1242 struct md_rdev *jdev;
1243
1244 if (test_and_set_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags)) {
1245 rs->ti->error = "Only one raid4/5/6 set journaling device allowed";
1246 return -EINVAL;
1247 }
1248 if (!rt_is_raid456(rt)) {
1249 rs->ti->error = "'journal_dev' is an invalid parameter for this RAID type";
1250 return -EINVAL;
1251 }
1252 r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
1253 &rs->journal_dev.dev);
1254 if (r) {
1255 rs->ti->error = "raid4/5/6 journal device lookup failure";
1256 return r;
1257 }
1258 jdev = &rs->journal_dev.rdev;
1259 md_rdev_init(jdev);
1260 jdev->mddev = &rs->md;
1261 jdev->bdev = rs->journal_dev.dev->bdev;
1262 jdev->sectors = to_sector(i_size_read(jdev->bdev->bd_inode));
1263 if (jdev->sectors < MIN_RAID456_JOURNAL_SPACE) {
1264 rs->ti->error = "No space for raid4/5/6 journal";
1265 return -ENOSPC;
1266 }
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01001267 rs->journal_dev.mode = R5C_JOURNAL_MODE_WRITE_THROUGH;
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01001268 set_bit(Journal, &jdev->flags);
1269 continue;
1270 }
1271
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01001272 /* "journal_mode <mode>" ("journal_dev" mandatory!) */
1273 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_JOURNAL_MODE))) {
1274 int r;
1275
1276 if (!test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags)) {
1277 rs->ti->error = "raid4/5/6 'journal_mode' is invalid without 'journal_dev'";
1278 return -EINVAL;
1279 }
1280 if (test_and_set_bit(__CTR_FLAG_JOURNAL_MODE, &rs->ctr_flags)) {
1281 rs->ti->error = "Only one raid4/5/6 'journal_mode' argument allowed";
1282 return -EINVAL;
1283 }
1284 r = dm_raid_journal_mode_to_md(arg);
1285 if (r < 0) {
1286 rs->ti->error = "Invalid 'journal_mode' argument";
1287 return r;
1288 }
1289 rs->journal_dev.mode = r;
1290 continue;
1291 }
1292
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01001293 /*
1294 * Parameters with number values from here on.
1295 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001296 if (kstrtoint(arg, 10, &value) < 0) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001297 rs->ti->error = "Bad numerical argument given in raid params";
1298 return -EINVAL;
1299 }
NeilBrown9d09e662011-01-13 20:00:02 +00001300
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001301 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_REBUILD))) {
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001302 /*
1303 * "rebuild" is being passed in by userspace to provide
1304 * indexes of replaced devices and to set up additional
1305 * devices on raid level takeover.
Mike Snitzer43157842016-05-30 13:03:37 -04001306 */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001307 if (!__within_range(value, 0, rs->raid_disks - 1)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001308 rs->ti->error = "Invalid rebuild index given";
1309 return -EINVAL;
1310 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001311
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001312 if (test_and_set_bit(value, (void *) rs->rebuild_disks)) {
1313 rs->ti->error = "rebuild for this index already given";
1314 return -EINVAL;
1315 }
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001316
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001317 rd = rs->dev + value;
1318 clear_bit(In_sync, &rd->rdev.flags);
1319 clear_bit(Faulty, &rd->rdev.flags);
1320 rd->rdev.recovery_offset = 0;
Mike Snitzer42863252016-06-02 12:27:46 -04001321 set_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags);
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001322 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_WRITE_MOSTLY))) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001323 if (!rt_is_raid1(rt)) {
1324 rs->ti->error = "write_mostly option is only valid for RAID1";
1325 return -EINVAL;
1326 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001327
Mike Snitzerbb91a632016-06-02 12:06:54 -04001328 if (!__within_range(value, 0, rs->md.raid_disks - 1)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001329 rs->ti->error = "Invalid write_mostly index given";
1330 return -EINVAL;
1331 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001332
Heinz Mauelshagen5fa146b2016-06-15 18:50:18 +02001333 write_mostly++;
Jonathan Brassow46bed2b2011-08-02 12:32:07 +01001334 set_bit(WriteMostly, &rs->dev[value].rdev.flags);
Mike Snitzer42863252016-06-02 12:27:46 -04001335 set_bit(__CTR_FLAG_WRITE_MOSTLY, &rs->ctr_flags);
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001336 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MAX_WRITE_BEHIND))) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001337 if (!rt_is_raid1(rt)) {
1338 rs->ti->error = "max_write_behind option is only valid for RAID1";
1339 return -EINVAL;
1340 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001341
Mike Snitzer42863252016-06-02 12:27:46 -04001342 if (test_and_set_bit(__CTR_FLAG_MAX_WRITE_BEHIND, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001343 rs->ti->error = "Only one max_write_behind argument pair allowed";
1344 return -EINVAL;
1345 }
NeilBrown9d09e662011-01-13 20:00:02 +00001346
1347 /*
1348 * In device-mapper, we specify things in sectors, but
1349 * MD records this value in kB
1350 */
1351 value /= 2;
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001352 if (value > COUNTER_MAX) {
1353 rs->ti->error = "Max write-behind limit out of range";
1354 return -EINVAL;
1355 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001356
NeilBrown9d09e662011-01-13 20:00:02 +00001357 rs->md.bitmap_info.max_write_behind = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001358 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DAEMON_SLEEP))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001359 if (test_and_set_bit(__CTR_FLAG_DAEMON_SLEEP, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001360 rs->ti->error = "Only one daemon_sleep argument pair allowed";
1361 return -EINVAL;
1362 }
1363 if (!value || (value > MAX_SCHEDULE_TIMEOUT)) {
1364 rs->ti->error = "daemon sleep period out of range";
1365 return -EINVAL;
1366 }
NeilBrown9d09e662011-01-13 20:00:02 +00001367 rs->md.bitmap_info.daemon_sleep = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001368 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DATA_OFFSET))) {
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001369 /* Userspace passes new data_offset after having extended the the data image LV */
Mike Snitzer42863252016-06-02 12:27:46 -04001370 if (test_and_set_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001371 rs->ti->error = "Only one data_offset argument pair allowed";
1372 return -EINVAL;
1373 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001374 /* Ensure sensible data offset */
Heinz Mauelshagen75dd3b92016-06-15 22:27:08 +02001375 if (value < 0 ||
1376 (value && (value < MIN_FREE_RESHAPE_SPACE || value % to_sector(PAGE_SIZE)))) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001377 rs->ti->error = "Bogus data_offset value";
1378 return -EINVAL;
1379 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001380 rs->data_offset = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001381 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DELTA_DISKS))) {
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001382 /* Define the +/-# of disks to add to/remove from the given raid set */
Mike Snitzer42863252016-06-02 12:27:46 -04001383 if (test_and_set_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001384 rs->ti->error = "Only one delta_disks argument pair allowed";
1385 return -EINVAL;
1386 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001387 /* Ensure MAX_RAID_DEVICES and raid type minimal_devs! */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001388 if (!__within_range(abs(value), 1, MAX_RAID_DEVICES - rt->minimal_devs)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001389 rs->ti->error = "Too many delta_disk requested";
1390 return -EINVAL;
1391 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001392
1393 rs->delta_disks = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001394 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_STRIPE_CACHE))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001395 if (test_and_set_bit(__CTR_FLAG_STRIPE_CACHE, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001396 rs->ti->error = "Only one stripe_cache argument pair allowed";
1397 return -EINVAL;
1398 }
1399
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001400 if (!rt_is_raid456(rt)) {
1401 rs->ti->error = "Inappropriate argument: stripe_cache";
1402 return -EINVAL;
1403 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001404
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001405 rs->stripe_cache_entries = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001406 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MIN_RECOVERY_RATE))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001407 if (test_and_set_bit(__CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001408 rs->ti->error = "Only one min_recovery_rate argument pair allowed";
1409 return -EINVAL;
1410 }
1411 if (value > INT_MAX) {
1412 rs->ti->error = "min_recovery_rate out of range";
1413 return -EINVAL;
1414 }
NeilBrown9d09e662011-01-13 20:00:02 +00001415 rs->md.sync_speed_min = (int)value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001416 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MAX_RECOVERY_RATE))) {
Heinz Mauelshagenf15f64d2016-07-27 23:34:01 +02001417 if (test_and_set_bit(__CTR_FLAG_MAX_RECOVERY_RATE, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001418 rs->ti->error = "Only one max_recovery_rate argument pair allowed";
1419 return -EINVAL;
1420 }
1421 if (value > INT_MAX) {
1422 rs->ti->error = "max_recovery_rate out of range";
1423 return -EINVAL;
1424 }
NeilBrown9d09e662011-01-13 20:00:02 +00001425 rs->md.sync_speed_max = (int)value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001426 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_REGION_SIZE))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001427 if (test_and_set_bit(__CTR_FLAG_REGION_SIZE, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001428 rs->ti->error = "Only one region_size argument pair allowed";
1429 return -EINVAL;
1430 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001431
Jonathan Brassowc1084562011-08-02 12:32:07 +01001432 region_size = value;
Heinz Mauelshagen4257e082016-06-14 01:46:03 +02001433 rs->requested_bitmap_chunk_sectors = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001434 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_COPIES))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001435 if (test_and_set_bit(__CTR_FLAG_RAID10_COPIES, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001436 rs->ti->error = "Only one raid10_copies argument pair allowed";
1437 return -EINVAL;
1438 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001439
Mike Snitzerbb91a632016-06-02 12:06:54 -04001440 if (!__within_range(value, 2, rs->md.raid_disks)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001441 rs->ti->error = "Bad value for 'raid10_copies'";
1442 return -EINVAL;
1443 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001444
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001445 raid10_copies = value;
NeilBrown9d09e662011-01-13 20:00:02 +00001446 } else {
1447 DMERR("Unable to parse RAID parameter: %s", key);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001448 rs->ti->error = "Unable to parse RAID parameter";
1449 return -EINVAL;
NeilBrown9d09e662011-01-13 20:00:02 +00001450 }
1451 }
1452
Heinz Mauelshagen0d851d12016-06-15 18:43:55 +02001453 if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags) &&
1454 test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) {
1455 rs->ti->error = "sync and nosync are mutually exclusive";
1456 return -EINVAL;
1457 }
1458
Heinz Mauelshagen37f10be2016-06-24 23:21:37 +02001459 if (test_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags) &&
1460 (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags) ||
1461 test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags))) {
1462 rs->ti->error = "sync/nosync and rebuild are mutually exclusive";
1463 return -EINVAL;
1464 }
1465
Heinz Mauelshagen5fa146b2016-06-15 18:50:18 +02001466 if (write_mostly >= rs->md.raid_disks) {
1467 rs->ti->error = "Can't set all raid1 devices to write_mostly";
1468 return -EINVAL;
1469 }
1470
Jonathan Brassowc1084562011-08-02 12:32:07 +01001471 if (validate_region_size(rs, region_size))
1472 return -EINVAL;
1473
1474 if (rs->md.chunk_sectors)
Mike Snitzer542f9032012-07-27 15:08:00 +01001475 max_io_len = rs->md.chunk_sectors;
Jonathan Brassowc1084562011-08-02 12:32:07 +01001476 else
Mike Snitzer542f9032012-07-27 15:08:00 +01001477 max_io_len = region_size;
Jonathan Brassowc1084562011-08-02 12:32:07 +01001478
Mike Snitzer542f9032012-07-27 15:08:00 +01001479 if (dm_set_target_max_io_len(rs->ti, max_io_len))
1480 return -EINVAL;
Jonathan Brassow32737272011-08-02 12:32:07 +01001481
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001482 if (rt_is_raid10(rt)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001483 if (raid10_copies > rs->md.raid_disks) {
1484 rs->ti->error = "Not enough devices to satisfy specification";
1485 return -EINVAL;
1486 }
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001487
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001488 rs->md.new_layout = raid10_format_to_md_layout(rs, raid10_format, raid10_copies);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001489 if (rs->md.new_layout < 0) {
1490 rs->ti->error = "Error getting raid10 format";
1491 return rs->md.new_layout;
1492 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001493
1494 rt = get_raid_type_by_ll(10, rs->md.new_layout);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001495 if (!rt) {
1496 rs->ti->error = "Failed to recognize new raid10 layout";
1497 return -EINVAL;
1498 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001499
1500 if ((rt->algorithm == ALGORITHM_RAID10_DEFAULT ||
1501 rt->algorithm == ALGORITHM_RAID10_NEAR) &&
Mike Snitzer42863252016-06-02 12:27:46 -04001502 test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001503 rs->ti->error = "RAID10 format 'near' and 'raid10_use_near_sets' are incompatible";
1504 return -EINVAL;
1505 }
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001506 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001507
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001508 rs->raid10_copies = raid10_copies;
Jonathan E Brassowc039c332012-07-27 15:08:04 +01001509
NeilBrown9d09e662011-01-13 20:00:02 +00001510 /* Assume there are no metadata devices until the drives are parsed */
1511 rs->md.persistent = 0;
1512 rs->md.external = 1;
1513
Heinz Mauelshagenf0902792016-05-19 18:49:27 +02001514 /* Check, if any invalid ctr arguments have been passed in for the raid level */
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +02001515 return rs_check_for_valid_flags(rs);
NeilBrown9d09e662011-01-13 20:00:02 +00001516}
1517
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001518/* Set raid4/5/6 cache size */
1519static int rs_set_raid456_stripe_cache(struct raid_set *rs)
1520{
1521 int r;
1522 struct r5conf *conf;
1523 struct mddev *mddev = &rs->md;
1524 uint32_t min_stripes = max(mddev->chunk_sectors, mddev->new_chunk_sectors) / 2;
1525 uint32_t nr_stripes = rs->stripe_cache_entries;
1526
1527 if (!rt_is_raid456(rs->raid_type)) {
1528 rs->ti->error = "Inappropriate raid level; cannot change stripe_cache size";
1529 return -EINVAL;
1530 }
1531
1532 if (nr_stripes < min_stripes) {
1533 DMINFO("Adjusting requested %u stripe cache entries to %u to suit stripe size",
1534 nr_stripes, min_stripes);
1535 nr_stripes = min_stripes;
1536 }
1537
1538 conf = mddev->private;
1539 if (!conf) {
1540 rs->ti->error = "Cannot change stripe_cache size on inactive RAID set";
1541 return -EINVAL;
1542 }
1543
1544 /* Try setting number of stripes in raid456 stripe cache */
1545 if (conf->min_nr_stripes != nr_stripes) {
1546 r = raid5_set_cache_size(mddev, nr_stripes);
1547 if (r) {
1548 rs->ti->error = "Failed to set raid4/5/6 stripe cache size";
1549 return r;
1550 }
1551
1552 DMINFO("%u stripe cache entries", nr_stripes);
1553 }
1554
1555 return 0;
1556}
1557
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02001558/* Return # of data stripes as kept in mddev as of @rs (i.e. as of superblock) */
1559static unsigned int mddev_data_stripes(struct raid_set *rs)
1560{
1561 return rs->md.raid_disks - rs->raid_type->parity_devs;
1562}
1563
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001564/* Return # of data stripes of @rs (i.e. as of ctr) */
1565static unsigned int rs_data_stripes(struct raid_set *rs)
1566{
1567 return rs->raid_disks - rs->raid_type->parity_devs;
1568}
1569
Heinz Mauelshagen50c4feb2017-01-14 03:53:08 +01001570/*
1571 * Retrieve rdev->sectors from any valid raid device of @rs
1572 * to allow userpace to pass in arbitray "- -" device tupples.
1573 */
1574static sector_t __rdev_sectors(struct raid_set *rs)
1575{
1576 int i;
1577
1578 for (i = 0; i < rs->md.raid_disks; i++) {
1579 struct md_rdev *rdev = &rs->dev[i].rdev;
1580
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01001581 if (!test_bit(Journal, &rdev->flags) &&
1582 rdev->bdev && rdev->sectors)
Heinz Mauelshagen50c4feb2017-01-14 03:53:08 +01001583 return rdev->sectors;
1584 }
1585
Heinz Mauelshagen4d49f1b2017-06-30 15:45:58 +02001586 return 0;
Heinz Mauelshagen50c4feb2017-01-14 03:53:08 +01001587}
1588
Heinz Mauelshagen188a2122017-12-02 01:03:59 +01001589/* Check that calculated dev_sectors fits all component devices. */
1590static int _check_data_dev_sectors(struct raid_set *rs)
1591{
1592 sector_t ds = ~0;
1593 struct md_rdev *rdev;
1594
1595 rdev_for_each(rdev, &rs->md)
1596 if (!test_bit(Journal, &rdev->flags) && rdev->bdev) {
1597 ds = min(ds, to_sector(i_size_read(rdev->bdev->bd_inode)));
1598 if (ds < rs->md.dev_sectors) {
1599 rs->ti->error = "Component device(s) too small";
1600 return -EINVAL;
1601 }
1602 }
1603
1604 return 0;
1605}
1606
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001607/* Calculate the sectors per device and per array used for @rs */
1608static int rs_set_dev_and_array_sectors(struct raid_set *rs, bool use_mddev)
1609{
1610 int delta_disks;
1611 unsigned int data_stripes;
1612 struct mddev *mddev = &rs->md;
1613 struct md_rdev *rdev;
1614 sector_t array_sectors = rs->ti->len, dev_sectors = rs->ti->len;
1615
1616 if (use_mddev) {
1617 delta_disks = mddev->delta_disks;
1618 data_stripes = mddev_data_stripes(rs);
1619 } else {
1620 delta_disks = rs->delta_disks;
1621 data_stripes = rs_data_stripes(rs);
1622 }
1623
1624 /* Special raid1 case w/o delta_disks support (yet) */
1625 if (rt_is_raid1(rs->raid_type))
1626 ;
1627 else if (rt_is_raid10(rs->raid_type)) {
1628 if (rs->raid10_copies < 2 ||
1629 delta_disks < 0) {
1630 rs->ti->error = "Bogus raid10 data copies or delta disks";
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02001631 return -EINVAL;
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001632 }
1633
1634 dev_sectors *= rs->raid10_copies;
1635 if (sector_div(dev_sectors, data_stripes))
1636 goto bad;
1637
1638 array_sectors = (data_stripes + delta_disks) * dev_sectors;
1639 if (sector_div(array_sectors, rs->raid10_copies))
1640 goto bad;
1641
1642 } else if (sector_div(dev_sectors, data_stripes))
1643 goto bad;
1644
1645 else
1646 /* Striped layouts */
1647 array_sectors = (data_stripes + delta_disks) * dev_sectors;
1648
1649 rdev_for_each(rdev, mddev)
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01001650 if (!test_bit(Journal, &rdev->flags))
1651 rdev->sectors = dev_sectors;
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001652
1653 mddev->array_sectors = array_sectors;
1654 mddev->dev_sectors = dev_sectors;
1655
Heinz Mauelshagen188a2122017-12-02 01:03:59 +01001656 return _check_data_dev_sectors(rs);
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001657bad:
1658 rs->ti->error = "Target length not divisible by number of data devices";
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02001659 return -EINVAL;
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001660}
1661
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02001662/* Setup recovery on @rs */
1663static void __rs_setup_recovery(struct raid_set *rs, sector_t dev_sectors)
1664{
1665 /* raid0 does not recover */
1666 if (rs_is_raid0(rs))
1667 rs->md.recovery_cp = MaxSector;
1668 /*
1669 * A raid6 set has to be recovered either
1670 * completely or for the grown part to
1671 * ensure proper parity and Q-Syndrome
1672 */
1673 else if (rs_is_raid6(rs))
1674 rs->md.recovery_cp = dev_sectors;
1675 /*
1676 * Other raid set types may skip recovery
1677 * depending on the 'nosync' flag.
1678 */
1679 else
1680 rs->md.recovery_cp = test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)
1681 ? MaxSector : dev_sectors;
1682}
1683
1684/* Setup recovery on @rs based on raid type, device size and 'nosync' flag */
1685static void rs_setup_recovery(struct raid_set *rs, sector_t dev_sectors)
1686{
1687 if (!dev_sectors)
1688 /* New raid set or 'sync' flag provided */
1689 __rs_setup_recovery(rs, 0);
1690 else if (dev_sectors == MaxSector)
1691 /* Prevent recovery */
1692 __rs_setup_recovery(rs, MaxSector);
Heinz Mauelshagen50c4feb2017-01-14 03:53:08 +01001693 else if (__rdev_sectors(rs) < dev_sectors)
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02001694 /* Grown raid set */
Heinz Mauelshagen50c4feb2017-01-14 03:53:08 +01001695 __rs_setup_recovery(rs, __rdev_sectors(rs));
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02001696 else
1697 __rs_setup_recovery(rs, MaxSector);
1698}
1699
NeilBrown9d09e662011-01-13 20:00:02 +00001700static void do_table_event(struct work_struct *ws)
1701{
1702 struct raid_set *rs = container_of(ws, struct raid_set, md.event_work);
1703
Heinz Mauelshagen9d9d9392016-06-16 03:15:49 +02001704 smp_rmb(); /* Make sure we access most actual mddev properties */
Heinz Mauelshagen61e06e22017-12-02 01:03:51 +01001705 if (!rs_is_reshaping(rs)) {
1706 if (rs_is_raid10(rs))
1707 rs_set_rdev_sectors(rs);
Heinz Mauelshagen9d9d9392016-06-16 03:15:49 +02001708 rs_set_capacity(rs);
Heinz Mauelshagen61e06e22017-12-02 01:03:51 +01001709 }
NeilBrown9d09e662011-01-13 20:00:02 +00001710 dm_table_event(rs->ti->table);
1711}
1712
1713static int raid_is_congested(struct dm_target_callbacks *cb, int bits)
1714{
1715 struct raid_set *rs = container_of(cb, struct raid_set, callbacks);
1716
NeilBrown5c675f82014-12-15 12:56:56 +11001717 return mddev_congested(&rs->md, bits);
NeilBrown9d09e662011-01-13 20:00:02 +00001718}
1719
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001720/*
1721 * Make sure a valid takover (level switch) is being requested on @rs
1722 *
1723 * Conversions of raid sets from one MD personality to another
1724 * have to conform to restrictions which are enforced here.
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001725 */
1726static int rs_check_takeover(struct raid_set *rs)
1727{
1728 struct mddev *mddev = &rs->md;
1729 unsigned int near_copies;
1730
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001731 if (rs->md.degraded) {
1732 rs->ti->error = "Can't takeover degraded raid set";
1733 return -EPERM;
1734 }
1735
1736 if (rs_is_reshaping(rs)) {
1737 rs->ti->error = "Can't takeover reshaping raid set";
1738 return -EPERM;
1739 }
1740
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001741 switch (mddev->level) {
1742 case 0:
1743 /* raid0 -> raid1/5 with one disk */
1744 if ((mddev->new_level == 1 || mddev->new_level == 5) &&
1745 mddev->raid_disks == 1)
1746 return 0;
1747
1748 /* raid0 -> raid10 */
1749 if (mddev->new_level == 10 &&
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001750 !(rs->raid_disks % mddev->raid_disks))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001751 return 0;
1752
1753 /* raid0 with multiple disks -> raid4/5/6 */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001754 if (__within_range(mddev->new_level, 4, 6) &&
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001755 mddev->new_layout == ALGORITHM_PARITY_N &&
1756 mddev->raid_disks > 1)
1757 return 0;
1758
1759 break;
1760
1761 case 10:
1762 /* Can't takeover raid10_offset! */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001763 if (__is_raid10_offset(mddev->layout))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001764 break;
1765
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001766 near_copies = __raid10_near_copies(mddev->layout);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001767
1768 /* raid10* -> raid0 */
1769 if (mddev->new_level == 0) {
1770 /* Can takeover raid10_near with raid disks divisable by data copies! */
1771 if (near_copies > 1 &&
1772 !(mddev->raid_disks % near_copies)) {
1773 mddev->raid_disks /= near_copies;
1774 mddev->delta_disks = mddev->raid_disks;
1775 return 0;
1776 }
1777
1778 /* Can takeover raid10_far */
1779 if (near_copies == 1 &&
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001780 __raid10_far_copies(mddev->layout) > 1)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001781 return 0;
1782
1783 break;
1784 }
1785
1786 /* raid10_{near,far} -> raid1 */
1787 if (mddev->new_level == 1 &&
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001788 max(near_copies, __raid10_far_copies(mddev->layout)) == mddev->raid_disks)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001789 return 0;
1790
1791 /* raid10_{near,far} with 2 disks -> raid4/5 */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001792 if (__within_range(mddev->new_level, 4, 5) &&
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001793 mddev->raid_disks == 2)
1794 return 0;
1795 break;
1796
1797 case 1:
1798 /* raid1 with 2 disks -> raid4/5 */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001799 if (__within_range(mddev->new_level, 4, 5) &&
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001800 mddev->raid_disks == 2) {
1801 mddev->degraded = 1;
1802 return 0;
1803 }
1804
1805 /* raid1 -> raid0 */
1806 if (mddev->new_level == 0 &&
1807 mddev->raid_disks == 1)
1808 return 0;
1809
1810 /* raid1 -> raid10 */
1811 if (mddev->new_level == 10)
1812 return 0;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001813 break;
1814
1815 case 4:
1816 /* raid4 -> raid0 */
1817 if (mddev->new_level == 0)
1818 return 0;
1819
1820 /* raid4 -> raid1/5 with 2 disks */
1821 if ((mddev->new_level == 1 || mddev->new_level == 5) &&
1822 mddev->raid_disks == 2)
1823 return 0;
1824
1825 /* raid4 -> raid5/6 with parity N */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001826 if (__within_range(mddev->new_level, 5, 6) &&
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001827 mddev->layout == ALGORITHM_PARITY_N)
1828 return 0;
1829 break;
1830
1831 case 5:
1832 /* raid5 with parity N -> raid0 */
1833 if (mddev->new_level == 0 &&
1834 mddev->layout == ALGORITHM_PARITY_N)
1835 return 0;
1836
1837 /* raid5 with parity N -> raid4 */
1838 if (mddev->new_level == 4 &&
1839 mddev->layout == ALGORITHM_PARITY_N)
1840 return 0;
1841
1842 /* raid5 with 2 disks -> raid1/4/10 */
1843 if ((mddev->new_level == 1 || mddev->new_level == 4 || mddev->new_level == 10) &&
1844 mddev->raid_disks == 2)
1845 return 0;
1846
Heinz Mauelshagen6ee0bae2016-06-15 22:29:09 +02001847 /* raid5_* -> raid6_*_6 with Q-Syndrome N (e.g. raid5_ra -> raid6_ra_6 */
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001848 if (mddev->new_level == 6 &&
1849 ((mddev->layout == ALGORITHM_PARITY_N && mddev->new_layout == ALGORITHM_PARITY_N) ||
Mike Snitzerbb91a632016-06-02 12:06:54 -04001850 __within_range(mddev->new_layout, ALGORITHM_LEFT_ASYMMETRIC_6, ALGORITHM_RIGHT_SYMMETRIC_6)))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001851 return 0;
1852 break;
1853
1854 case 6:
1855 /* raid6 with parity N -> raid0 */
1856 if (mddev->new_level == 0 &&
1857 mddev->layout == ALGORITHM_PARITY_N)
1858 return 0;
1859
1860 /* raid6 with parity N -> raid4 */
1861 if (mddev->new_level == 4 &&
1862 mddev->layout == ALGORITHM_PARITY_N)
1863 return 0;
1864
Heinz Mauelshagen6ee0bae2016-06-15 22:29:09 +02001865 /* raid6_*_n with Q-Syndrome N -> raid5_* */
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001866 if (mddev->new_level == 5 &&
1867 ((mddev->layout == ALGORITHM_PARITY_N && mddev->new_layout == ALGORITHM_PARITY_N) ||
Mike Snitzerbb91a632016-06-02 12:06:54 -04001868 __within_range(mddev->new_layout, ALGORITHM_LEFT_ASYMMETRIC, ALGORITHM_RIGHT_SYMMETRIC)))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001869 return 0;
1870
1871 default:
1872 break;
1873 }
1874
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001875 rs->ti->error = "takeover not possible";
1876 return -EINVAL;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001877}
1878
1879/* True if @rs requested to be taken over */
1880static bool rs_takeover_requested(struct raid_set *rs)
1881{
1882 return rs->md.new_level != rs->md.level;
1883}
1884
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001885/* True if @rs is requested to reshape by ctr */
1886static bool rs_reshape_requested(struct raid_set *rs)
1887{
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02001888 bool change;
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001889 struct mddev *mddev = &rs->md;
1890
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02001891 if (rs_takeover_requested(rs))
1892 return false;
1893
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001894 if (!mddev->level)
1895 return false;
1896
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02001897 change = mddev->new_layout != mddev->layout ||
1898 mddev->new_chunk_sectors != mddev->chunk_sectors ||
1899 rs->delta_disks;
1900
1901 /* Historical case to support raid1 reshape without delta disks */
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02001902 if (mddev->level == 1) {
1903 if (rs->delta_disks)
1904 return !!rs->delta_disks;
1905
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02001906 return !change &&
1907 mddev->raid_disks != rs->raid_disks;
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02001908 }
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02001909
1910 if (mddev->level == 10)
1911 return change &&
1912 !__is_raid10_far(mddev->new_layout) &&
1913 rs->delta_disks >= 0;
1914
1915 return change;
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001916}
1917
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001918/* Features */
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001919#define FEATURE_FLAG_SUPPORTS_V190 0x1 /* Supports extended superblock */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001920
1921/* State flags for sb->flags */
1922#define SB_FLAG_RESHAPE_ACTIVE 0x1
1923#define SB_FLAG_RESHAPE_BACKWARDS 0x2
1924
NeilBrown9d09e662011-01-13 20:00:02 +00001925/*
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001926 * This structure is never routinely used by userspace, unlike md superblocks.
1927 * Devices with this superblock should only ever be accessed via device-mapper.
1928 */
1929#define DM_RAID_MAGIC 0x64526D44
1930struct dm_raid_superblock {
1931 __le32 magic; /* "DmRd" */
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001932 __le32 compat_features; /* Used to indicate compatible features (like 1.9.0 ondisk metadata extension) */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001933
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001934 __le32 num_devices; /* Number of devices in this raid set. (Max 64) */
1935 __le32 array_position; /* The position of this drive in the raid set */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001936
1937 __le64 events; /* Incremented by md when superblock updated */
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001938 __le64 failed_devices; /* Pre 1.9.0 part of bit field of devices to */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001939 /* indicate failures (see extension below) */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001940
1941 /*
1942 * This offset tracks the progress of the repair or replacement of
1943 * an individual drive.
1944 */
1945 __le64 disk_recovery_offset;
1946
1947 /*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001948 * This offset tracks the progress of the initial raid set
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001949 * synchronisation/parity calculation.
1950 */
1951 __le64 array_resync_offset;
1952
1953 /*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001954 * raid characteristics
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001955 */
1956 __le32 level;
1957 __le32 layout;
1958 __le32 stripe_sectors;
1959
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001960 /********************************************************************
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001961 * BELOW FOLLOW V1.9.0 EXTENSIONS TO THE PRISTINE SUPERBLOCK FORMAT!!!
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001962 *
Heinz Mauelshagenc4d097d2017-06-23 17:27:01 +02001963 * FEATURE_FLAG_SUPPORTS_V190 in the compat_features member indicates that those exist
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001964 */
1965
1966 __le32 flags; /* Flags defining array states for reshaping */
1967
1968 /*
1969 * This offset tracks the progress of a raid
1970 * set reshape in order to be able to restart it
1971 */
1972 __le64 reshape_position;
1973
1974 /*
1975 * These define the properties of the array in case of an interrupted reshape
1976 */
1977 __le32 new_level;
1978 __le32 new_layout;
1979 __le32 new_stripe_sectors;
1980 __le32 delta_disks;
1981
1982 __le64 array_sectors; /* Array size in sectors */
1983
1984 /*
1985 * Sector offsets to data on devices (reshaping).
1986 * Needed to support out of place reshaping, thus
1987 * not writing over any stripes whilst converting
1988 * them from old to new layout
1989 */
1990 __le64 data_offset;
1991 __le64 new_data_offset;
1992
1993 __le64 sectors; /* Used device size in sectors */
1994
1995 /*
1996 * Additonal Bit field of devices indicating failures to support
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001997 * up to 256 devices with the 1.9.0 on-disk metadata format
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001998 */
1999 __le64 extended_failed_devices[DISKS_ARRAY_ELEMS - 1];
2000
2001 __le32 incompat_features; /* Used to indicate any incompatible features */
2002
2003 /* Always set rest up to logical block size to 0 when writing (see get_metadata_device() below). */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002004} __packed;
2005
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002006/*
2007 * Check for reshape constraints on raid set @rs:
2008 *
2009 * - reshape function non-existent
2010 * - degraded set
2011 * - ongoing recovery
2012 * - ongoing reshape
2013 *
2014 * Returns 0 if none or -EPERM if given constraint
2015 * and error message reference in @errmsg
2016 */
2017static int rs_check_reshape(struct raid_set *rs)
2018{
2019 struct mddev *mddev = &rs->md;
2020
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002021 if (!mddev->pers || !mddev->pers->check_reshape)
2022 rs->ti->error = "Reshape not supported";
2023 else if (mddev->degraded)
2024 rs->ti->error = "Can't reshape degraded raid set";
2025 else if (rs_is_recovering(rs))
2026 rs->ti->error = "Convert request on recovering raid set prohibited";
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002027 else if (rs_is_reshaping(rs))
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002028 rs->ti->error = "raid set already reshaping!";
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02002029 else if (!(rs_is_raid1(rs) || rs_is_raid10(rs) || rs_is_raid456(rs)))
2030 rs->ti->error = "Reshaping only supported for raid1/4/5/6/10";
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002031 else
2032 return 0;
2033
2034 return -EPERM;
2035}
2036
Heinz Mauelshagene2568462017-01-14 03:53:10 +01002037static int read_disk_sb(struct md_rdev *rdev, int size, bool force_reload)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002038{
2039 BUG_ON(!rdev->sb_page);
2040
Heinz Mauelshagene2568462017-01-14 03:53:10 +01002041 if (rdev->sb_loaded && !force_reload)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002042 return 0;
2043
Heinz Mauelshagene2568462017-01-14 03:53:10 +01002044 rdev->sb_loaded = 0;
2045
Heinz Mauelshagen0a7b8182016-06-15 18:45:56 +02002046 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true)) {
Jonathan E Brassow04475682012-03-28 18:41:26 +01002047 DMERR("Failed to read superblock of device at position %d",
2048 rdev->raid_disk);
Jonathan Brassowc32fb9e2012-05-22 13:55:31 +10002049 md_error(rdev->mddev, rdev);
Heinz Mauelshagene2568462017-01-14 03:53:10 +01002050 set_bit(Faulty, &rdev->flags);
2051 return -EIO;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002052 }
2053
2054 rdev->sb_loaded = 1;
2055
2056 return 0;
2057}
2058
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002059static void sb_retrieve_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices)
2060{
2061 failed_devices[0] = le64_to_cpu(sb->failed_devices);
2062 memset(failed_devices + 1, 0, sizeof(sb->extended_failed_devices));
2063
Mike Snitzer42863252016-06-02 12:27:46 -04002064 if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002065 int i = ARRAY_SIZE(sb->extended_failed_devices);
2066
2067 while (i--)
2068 failed_devices[i+1] = le64_to_cpu(sb->extended_failed_devices[i]);
2069 }
2070}
2071
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02002072static void sb_update_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices)
2073{
2074 int i = ARRAY_SIZE(sb->extended_failed_devices);
2075
2076 sb->failed_devices = cpu_to_le64(failed_devices[0]);
2077 while (i--)
2078 sb->extended_failed_devices[i] = cpu_to_le64(failed_devices[i+1]);
2079}
2080
2081/*
2082 * Synchronize the superblock members with the raid set properties
2083 *
2084 * All superblock data is little endian.
2085 */
NeilBrownfd01b882011-10-11 16:47:53 +11002086static void super_sync(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002087{
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02002088 bool update_failed_devices = false;
2089 unsigned int i;
2090 uint64_t failed_devices[DISKS_ARRAY_ELEMS];
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002091 struct dm_raid_superblock *sb;
Jonathan Brassow81f382f2012-05-22 13:55:30 +10002092 struct raid_set *rs = container_of(mddev, struct raid_set, md);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002093
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02002094 /* No metadata device, no superblock */
2095 if (!rdev->meta_bdev)
2096 return;
2097
2098 BUG_ON(!rdev->sb_page);
2099
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002100 sb = page_address(rdev->sb_page);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002101
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02002102 sb_retrieve_failed_devices(sb, failed_devices);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002103
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02002104 for (i = 0; i < rs->raid_disks; i++)
2105 if (!rs->dev[i].data_dev || test_bit(Faulty, &rs->dev[i].rdev.flags)) {
2106 update_failed_devices = true;
2107 set_bit(i, (void *) failed_devices);
2108 }
2109
2110 if (update_failed_devices)
2111 sb_update_failed_devices(sb, failed_devices);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002112
2113 sb->magic = cpu_to_le32(DM_RAID_MAGIC);
Mike Snitzer9b6e5422016-06-02 11:48:09 -04002114 sb->compat_features = cpu_to_le32(FEATURE_FLAG_SUPPORTS_V190);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002115
2116 sb->num_devices = cpu_to_le32(mddev->raid_disks);
2117 sb->array_position = cpu_to_le32(rdev->raid_disk);
2118
2119 sb->events = cpu_to_le64(mddev->events);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002120
2121 sb->disk_recovery_offset = cpu_to_le64(rdev->recovery_offset);
2122 sb->array_resync_offset = cpu_to_le64(mddev->recovery_cp);
2123
2124 sb->level = cpu_to_le32(mddev->level);
2125 sb->layout = cpu_to_le32(mddev->layout);
2126 sb->stripe_sectors = cpu_to_le32(mddev->chunk_sectors);
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02002127
Heinz Mauelshagenc4d097d2017-06-23 17:27:01 +02002128 /********************************************************************
2129 * BELOW FOLLOW V1.9.0 EXTENSIONS TO THE PRISTINE SUPERBLOCK FORMAT!!!
2130 *
2131 * FEATURE_FLAG_SUPPORTS_V190 in the compat_features member indicates that those exist
2132 */
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02002133 sb->new_level = cpu_to_le32(mddev->new_level);
2134 sb->new_layout = cpu_to_le32(mddev->new_layout);
2135 sb->new_stripe_sectors = cpu_to_le32(mddev->new_chunk_sectors);
2136
2137 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
2138
2139 smp_rmb(); /* Make sure we access most recent reshape position */
2140 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
2141 if (le64_to_cpu(sb->reshape_position) != MaxSector) {
2142 /* Flag ongoing reshape */
2143 sb->flags |= cpu_to_le32(SB_FLAG_RESHAPE_ACTIVE);
2144
2145 if (mddev->delta_disks < 0 || mddev->reshape_backwards)
2146 sb->flags |= cpu_to_le32(SB_FLAG_RESHAPE_BACKWARDS);
Mike Snitzer42863252016-06-02 12:27:46 -04002147 } else {
2148 /* Clear reshape flags */
2149 sb->flags &= ~(cpu_to_le32(SB_FLAG_RESHAPE_ACTIVE|SB_FLAG_RESHAPE_BACKWARDS));
2150 }
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02002151
2152 sb->array_sectors = cpu_to_le64(mddev->array_sectors);
2153 sb->data_offset = cpu_to_le64(rdev->data_offset);
2154 sb->new_data_offset = cpu_to_le64(rdev->new_data_offset);
2155 sb->sectors = cpu_to_le64(rdev->sectors);
Heinz Mauelshagenb2a48722016-08-03 17:47:04 +02002156 sb->incompat_features = cpu_to_le32(0);
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02002157
2158 /* Zero out the rest of the payload after the size of the superblock */
2159 memset(sb + 1, 0, rdev->sb_size - sizeof(*sb));
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002160}
2161
2162/*
2163 * super_load
2164 *
2165 * This function creates a superblock if one is not found on the device
2166 * and will decide which superblock to use if there's a choice.
2167 *
2168 * Return: 1 if use rdev, 0 if use refdev, -Exxx otherwise
2169 */
NeilBrown3cb03002011-10-11 16:45:26 +11002170static int super_load(struct md_rdev *rdev, struct md_rdev *refdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002171{
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002172 int r;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002173 struct dm_raid_superblock *sb;
2174 struct dm_raid_superblock *refsb;
2175 uint64_t events_sb, events_refsb;
2176
Heinz Mauelshagene2568462017-01-14 03:53:10 +01002177 r = read_disk_sb(rdev, rdev->sb_size, false);
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002178 if (r)
2179 return r;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002180
2181 sb = page_address(rdev->sb_page);
Jonathan E Brassow3aa3b2b2012-03-07 19:09:47 +00002182
2183 /*
2184 * Two cases that we want to write new superblocks and rebuild:
2185 * 1) New device (no matching magic number)
2186 * 2) Device specified for rebuild (!In_sync w/ offset == 0)
2187 */
2188 if ((sb->magic != cpu_to_le32(DM_RAID_MAGIC)) ||
2189 (!test_bit(In_sync, &rdev->flags) && !rdev->recovery_offset)) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002190 super_sync(rdev->mddev, rdev);
2191
2192 set_bit(FirstUse, &rdev->flags);
Mike Snitzer9b6e5422016-06-02 11:48:09 -04002193 sb->compat_features = cpu_to_le32(FEATURE_FLAG_SUPPORTS_V190);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002194
2195 /* Force writing of superblocks to disk */
Shaohua Li29530792016-12-08 15:48:19 -08002196 set_bit(MD_SB_CHANGE_DEVS, &rdev->mddev->sb_flags);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002197
2198 /* Any superblock is better than none, choose that if given */
2199 return refdev ? 0 : 1;
2200 }
2201
2202 if (!refdev)
2203 return 1;
2204
2205 events_sb = le64_to_cpu(sb->events);
2206
2207 refsb = page_address(refdev->sb_page);
2208 events_refsb = le64_to_cpu(refsb->events);
2209
2210 return (events_sb > events_refsb) ? 1 : 0;
2211}
2212
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002213static int super_init_validation(struct raid_set *rs, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002214{
2215 int role;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002216 unsigned int d;
2217 struct mddev *mddev = &rs->md;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002218 uint64_t events_sb;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002219 uint64_t failed_devices[DISKS_ARRAY_ELEMS];
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002220 struct dm_raid_superblock *sb;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002221 uint32_t new_devs = 0, rebuild_and_new = 0, rebuilds = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11002222 struct md_rdev *r;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002223 struct dm_raid_superblock *sb2;
2224
2225 sb = page_address(rdev->sb_page);
2226 events_sb = le64_to_cpu(sb->events);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002227
2228 /*
2229 * Initialise to 1 if this is a new superblock.
2230 */
2231 mddev->events = events_sb ? : 1;
2232
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002233 mddev->reshape_position = MaxSector;
2234
Heinz Mauelshagen453c2a82016-10-18 17:46:45 +02002235 mddev->raid_disks = le32_to_cpu(sb->num_devices);
2236 mddev->level = le32_to_cpu(sb->level);
2237 mddev->layout = le32_to_cpu(sb->layout);
2238 mddev->chunk_sectors = le32_to_cpu(sb->stripe_sectors);
2239
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002240 /*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002241 * Reshaping is supported, e.g. reshape_position is valid
2242 * in superblock and superblock content is authoritative.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002243 */
Mike Snitzer42863252016-06-02 12:27:46 -04002244 if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002245 /* Superblock is authoritative wrt given raid set layout! */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002246 mddev->new_level = le32_to_cpu(sb->new_level);
2247 mddev->new_layout = le32_to_cpu(sb->new_layout);
2248 mddev->new_chunk_sectors = le32_to_cpu(sb->new_stripe_sectors);
2249 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
2250 mddev->array_sectors = le64_to_cpu(sb->array_sectors);
2251
2252 /* raid was reshaping and got interrupted */
Mike Snitzer42863252016-06-02 12:27:46 -04002253 if (le32_to_cpu(sb->flags) & SB_FLAG_RESHAPE_ACTIVE) {
2254 if (test_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags)) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002255 DMERR("Reshape requested but raid set is still reshaping");
2256 return -EINVAL;
2257 }
2258
2259 if (mddev->delta_disks < 0 ||
Mike Snitzer42863252016-06-02 12:27:46 -04002260 (!mddev->delta_disks && (le32_to_cpu(sb->flags) & SB_FLAG_RESHAPE_BACKWARDS)))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002261 mddev->reshape_backwards = 1;
2262 else
2263 mddev->reshape_backwards = 0;
2264
2265 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
2266 rs->raid_type = get_raid_type_by_ll(mddev->level, mddev->layout);
2267 }
2268
2269 } else {
2270 /*
Mike Snitzer9b6e5422016-06-02 11:48:09 -04002271 * No takeover/reshaping, because we don't have the extended v1.9.0 metadata
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002272 */
Heinz Mauelshagen453c2a82016-10-18 17:46:45 +02002273 struct raid_type *rt_cur = get_raid_type_by_ll(mddev->level, mddev->layout);
2274 struct raid_type *rt_new = get_raid_type_by_ll(mddev->new_level, mddev->new_layout);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002275
Heinz Mauelshagen453c2a82016-10-18 17:46:45 +02002276 if (rs_takeover_requested(rs)) {
2277 if (rt_cur && rt_new)
2278 DMERR("Takeover raid sets from %s to %s not yet supported by metadata. (raid level change)",
2279 rt_cur->name, rt_new->name);
2280 else
2281 DMERR("Takeover raid sets not yet supported by metadata. (raid level change)");
2282 return -EINVAL;
2283 } else if (rs_reshape_requested(rs)) {
2284 DMERR("Reshaping raid sets not yet supported by metadata. (raid layout change keeping level)");
2285 if (mddev->layout != mddev->new_layout) {
2286 if (rt_cur && rt_new)
2287 DMERR(" current layout %s vs new layout %s",
2288 rt_cur->name, rt_new->name);
2289 else
2290 DMERR(" current layout 0x%X vs new layout 0x%X",
2291 le32_to_cpu(sb->layout), mddev->new_layout);
2292 }
2293 if (mddev->chunk_sectors != mddev->new_chunk_sectors)
2294 DMERR(" current stripe sectors %u vs new stripe sectors %u",
2295 mddev->chunk_sectors, mddev->new_chunk_sectors);
2296 if (rs->delta_disks)
2297 DMERR(" current %u disks vs new %u disks",
2298 mddev->raid_disks, mddev->raid_disks + rs->delta_disks);
2299 if (rs_is_raid10(rs)) {
2300 DMERR(" Old layout: %s w/ %u copies",
2301 raid10_md_layout_to_format(mddev->layout),
2302 raid10_md_layout_to_copies(mddev->layout));
2303 DMERR(" New layout: %s w/ %u copies",
2304 raid10_md_layout_to_format(mddev->new_layout),
2305 raid10_md_layout_to_copies(mddev->new_layout));
2306 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002307 return -EINVAL;
2308 }
2309
Heinz Mauelshagenb052b072016-10-17 21:20:07 +02002310 DMINFO("Discovered old metadata format; upgrading to extended metadata format");
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002311 }
2312
Mike Snitzer42863252016-06-02 12:27:46 -04002313 if (!test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002314 mddev->recovery_cp = le64_to_cpu(sb->array_resync_offset);
2315
2316 /*
2317 * During load, we set FirstUse if a new superblock was written.
2318 * There are two reasons we might not have a superblock:
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002319 * 1) The raid set is brand new - in which case, all of the
Mike Snitzer43157842016-05-30 13:03:37 -04002320 * devices must have their In_sync bit set. Also,
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002321 * recovery_cp must be 0, unless forced.
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002322 * 2) This is a new device being added to an old raid set
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002323 * and the new device needs to be rebuilt - in which
2324 * case the In_sync bit will /not/ be set and
2325 * recovery_cp must be MaxSector.
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002326 * 3) This is/are a new device(s) being added to an old
2327 * raid set during takeover to a higher raid level
2328 * to provide capacity for redundancy or during reshape
2329 * to add capacity to grow the raid set.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002330 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002331 d = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11002332 rdev_for_each(r, mddev) {
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01002333 if (test_bit(Journal, &rdev->flags))
2334 continue;
2335
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002336 if (test_bit(FirstUse, &r->flags))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002337 new_devs++;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002338
2339 if (!test_bit(In_sync, &r->flags)) {
2340 DMINFO("Device %d specified for rebuild; clearing superblock",
2341 r->raid_disk);
2342 rebuilds++;
2343
2344 if (test_bit(FirstUse, &r->flags))
2345 rebuild_and_new++;
2346 }
2347
2348 d++;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002349 }
2350
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002351 if (new_devs == rs->raid_disks || !rebuilds) {
2352 /* Replace a broken device */
2353 if (new_devs == 1 && !rs->delta_disks)
2354 ;
2355 if (new_devs == rs->raid_disks) {
2356 DMINFO("Superblocks created for new raid set");
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002357 set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002358 } else if (new_devs != rebuilds &&
2359 new_devs != rs->delta_disks) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002360 DMERR("New device injected into existing raid set without "
2361 "'delta_disks' or 'rebuild' parameter specified");
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002362 return -EINVAL;
2363 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002364 } else if (new_devs && new_devs != rebuilds) {
2365 DMERR("%u 'rebuild' devices cannot be injected into"
2366 " a raid set with %u other first-time devices",
2367 rebuilds, new_devs);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002368 return -EINVAL;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002369 } else if (rebuilds) {
2370 if (rebuild_and_new && rebuilds != rebuild_and_new) {
2371 DMERR("new device%s provided without 'rebuild'",
2372 new_devs > 1 ? "s" : "");
2373 return -EINVAL;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002374 } else if (rs_is_recovering(rs)) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002375 DMERR("'rebuild' specified while raid set is not in-sync (recovery_cp=%llu)",
2376 (unsigned long long) mddev->recovery_cp);
2377 return -EINVAL;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002378 } else if (rs_is_reshaping(rs)) {
2379 DMERR("'rebuild' specified while raid set is being reshaped (reshape_position=%llu)",
2380 (unsigned long long) mddev->reshape_position);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002381 return -EINVAL;
2382 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002383 }
2384
2385 /*
2386 * Now we set the Faulty bit for those devices that are
2387 * recorded in the superblock as failed.
2388 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002389 sb_retrieve_failed_devices(sb, failed_devices);
NeilBrowndafb20f2012-03-19 12:46:39 +11002390 rdev_for_each(r, mddev) {
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01002391 if (test_bit(Journal, &rdev->flags) ||
2392 !r->sb_page)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002393 continue;
2394 sb2 = page_address(r->sb_page);
2395 sb2->failed_devices = 0;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002396 memset(sb2->extended_failed_devices, 0, sizeof(sb2->extended_failed_devices));
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002397
2398 /*
2399 * Check for any device re-ordering.
2400 */
2401 if (!test_bit(FirstUse, &r->flags) && (r->raid_disk >= 0)) {
2402 role = le32_to_cpu(sb2->array_position);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002403 if (role < 0)
2404 continue;
2405
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002406 if (role != r->raid_disk) {
Heinz Mauelshagen453c2a82016-10-18 17:46:45 +02002407 if (rs_is_raid10(rs) && __is_raid10_near(mddev->layout)) {
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002408 if (mddev->raid_disks % __raid10_near_copies(mddev->layout) ||
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002409 rs->raid_disks % rs->raid10_copies) {
2410 rs->ti->error =
2411 "Cannot change raid10 near set to odd # of devices!";
2412 return -EINVAL;
2413 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002414
2415 sb2->array_position = cpu_to_le32(r->raid_disk);
2416
2417 } else if (!(rs_is_raid10(rs) && rt_is_raid0(rs->raid_type)) &&
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002418 !(rs_is_raid0(rs) && rt_is_raid10(rs->raid_type)) &&
2419 !rt_is_raid1(rs->raid_type)) {
2420 rs->ti->error = "Cannot change device positions in raid set";
2421 return -EINVAL;
2422 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002423
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002424 DMINFO("raid device #%d now at position #%d", role, r->raid_disk);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002425 }
2426
2427 /*
2428 * Partial recovery is performed on
2429 * returning failed devices.
2430 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002431 if (test_bit(role, (void *) failed_devices))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002432 set_bit(Faulty, &r->flags);
2433 }
2434 }
2435
2436 return 0;
2437}
2438
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002439static int super_validate(struct raid_set *rs, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002440{
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002441 struct mddev *mddev = &rs->md;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002442 struct dm_raid_superblock *sb;
2443
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01002444 if (rs_is_raid0(rs) || !rdev->sb_page || rdev->raid_disk < 0)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002445 return 0;
2446
2447 sb = page_address(rdev->sb_page);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002448
2449 /*
2450 * If mddev->events is not set, we know we have not yet initialized
2451 * the array.
2452 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002453 if (!mddev->events && super_init_validation(rs, rdev))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002454 return -EINVAL;
2455
Andy Whitcroft5c336772016-10-11 15:16:57 +01002456 if (le32_to_cpu(sb->compat_features) &&
2457 le32_to_cpu(sb->compat_features) != FEATURE_FLAG_SUPPORTS_V190) {
Mike Snitzer9b6e5422016-06-02 11:48:09 -04002458 rs->ti->error = "Unable to assemble array: Unknown flag(s) in compatible feature flags";
2459 return -EINVAL;
2460 }
2461
2462 if (sb->incompat_features) {
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002463 rs->ti->error = "Unable to assemble array: No incompatible feature flags supported yet";
Heinz Mauelshagen4c9971c2016-04-29 18:59:56 +02002464 return -EINVAL;
2465 }
2466
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002467 /* Enable bitmap creation for RAID levels != 0 */
Heinz Mauelshagen676fa5a2016-05-19 18:49:29 +02002468 mddev->bitmap_info.offset = rt_is_raid0(rs->raid_type) ? 0 : to_sector(4096);
Heinz Mauelshagen977f1a02017-01-14 03:53:11 +01002469 mddev->bitmap_info.default_offset = mddev->bitmap_info.offset;
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002470
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002471 if (!test_and_clear_bit(FirstUse, &rdev->flags)) {
Heinz Mauelshagenc4d097d2017-06-23 17:27:01 +02002472 /*
2473 * Retrieve rdev size stored in superblock to be prepared for shrink.
2474 * Check extended superblock members are present otherwise the size
2475 * will not be set!
2476 */
2477 if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190)
2478 rdev->sectors = le64_to_cpu(sb->sectors);
2479
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002480 rdev->recovery_offset = le64_to_cpu(sb->disk_recovery_offset);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002481 if (rdev->recovery_offset == MaxSector)
2482 set_bit(In_sync, &rdev->flags);
2483 /*
2484 * If no reshape in progress -> we're recovering single
2485 * disk(s) and have to set the device(s) to out-of-sync
2486 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002487 else if (!rs_is_reshaping(rs))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002488 clear_bit(In_sync, &rdev->flags); /* Mandatory for recovery */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002489 }
2490
2491 /*
2492 * If a device comes back, set it as not In_sync and no longer faulty.
2493 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002494 if (test_and_clear_bit(Faulty, &rdev->flags)) {
2495 rdev->recovery_offset = 0;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002496 clear_bit(In_sync, &rdev->flags);
2497 rdev->saved_raid_disk = rdev->raid_disk;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002498 }
2499
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002500 /* Reshape support -> restore repective data offsets */
2501 rdev->data_offset = le64_to_cpu(sb->data_offset);
2502 rdev->new_data_offset = le64_to_cpu(sb->new_data_offset);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002503
2504 return 0;
2505}
2506
2507/*
2508 * Analyse superblocks and select the freshest.
2509 */
2510static int analyse_superblocks(struct dm_target *ti, struct raid_set *rs)
2511{
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002512 int r;
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01002513 struct md_rdev *rdev, *freshest;
NeilBrownfd01b882011-10-11 16:47:53 +11002514 struct mddev *mddev = &rs->md;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002515
2516 freshest = NULL;
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01002517 rdev_for_each(rdev, mddev) {
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01002518 if (test_bit(Journal, &rdev->flags))
2519 continue;
2520
Heinz Mauelshagen23397842017-11-02 19:58:28 +01002521 if (!rdev->meta_bdev)
2522 continue;
2523
2524 /* Set superblock offset/size for metadata device. */
2525 rdev->sb_start = 0;
2526 rdev->sb_size = bdev_logical_block_size(rdev->meta_bdev);
2527 if (rdev->sb_size < sizeof(struct dm_raid_superblock) || rdev->sb_size > PAGE_SIZE) {
2528 DMERR("superblock size of a logical block is no longer valid");
2529 return -EINVAL;
2530 }
2531
Jonathan Brassow761becf2012-10-11 13:42:19 +11002532 /*
Heinz Mauelshagenc76d53f2015-04-29 14:03:00 +02002533 * Skipping super_load due to CTR_FLAG_SYNC will cause
Jonathan Brassow761becf2012-10-11 13:42:19 +11002534 * the array to undergo initialization again as
Mike Snitzer43157842016-05-30 13:03:37 -04002535 * though it were new. This is the intended effect
Jonathan Brassow761becf2012-10-11 13:42:19 +11002536 * of the "sync" directive.
2537 *
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01002538 * With reshaping capability added, we must ensure that
2539 * that the "sync" directive is disallowed during the reshape.
Jonathan Brassow761becf2012-10-11 13:42:19 +11002540 */
Mike Snitzer42863252016-06-02 12:27:46 -04002541 if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags))
Jonathan Brassow761becf2012-10-11 13:42:19 +11002542 continue;
2543
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002544 r = super_load(rdev, freshest);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002545
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002546 switch (r) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002547 case 1:
2548 freshest = rdev;
2549 break;
2550 case 0:
2551 break;
2552 default:
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01002553 /* This is a failure to read the superblock from the metadata device. */
Heinz Mauelshagen9e7d9362016-08-17 15:36:44 +02002554 /*
2555 * We have to keep any raid0 data/metadata device pairs or
2556 * the MD raid0 personality will fail to start the array.
2557 */
2558 if (rs_is_raid0(rs))
2559 continue;
2560
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002561 /*
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01002562 * We keep the dm_devs to be able to emit the device tuple
2563 * properly on the table line in raid_status() (rather than
2564 * mistakenly acting as if '- -' got passed into the constructor).
2565 *
2566 * The rdev has to stay on the same_set list to allow for
2567 * the attempt to restore faulty devices on second resume.
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002568 */
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01002569 rdev->raid_disk = rdev->saved_raid_disk = -1;
2570 break;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002571 }
2572 }
2573
2574 if (!freshest)
2575 return 0;
2576
2577 /*
2578 * Validation of the freshest device provides the source of
2579 * validation for the remaining devices.
2580 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002581 rs->ti->error = "Unable to assemble array: Invalid superblocks";
2582 if (super_validate(rs, freshest))
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002583 return -EINVAL;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002584
Heinz Mauelshagenf4af3f82017-07-13 17:36:12 +02002585 if (validate_raid_redundancy(rs)) {
2586 rs->ti->error = "Insufficient redundancy to activate array";
2587 return -EINVAL;
2588 }
2589
NeilBrowndafb20f2012-03-19 12:46:39 +11002590 rdev_for_each(rdev, mddev)
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01002591 if (!test_bit(Journal, &rdev->flags) &&
2592 rdev != freshest &&
2593 super_validate(rs, rdev))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002594 return -EINVAL;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002595 return 0;
2596}
2597
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002598/*
2599 * Adjust data_offset and new_data_offset on all disk members of @rs
2600 * for out of place reshaping if requested by contructor
2601 *
2602 * We need free space at the beginning of each raid disk for forward
2603 * and at the end for backward reshapes which userspace has to provide
2604 * via remapping/reordering of space.
2605 */
2606static int rs_adjust_data_offsets(struct raid_set *rs)
2607{
2608 sector_t data_offset = 0, new_data_offset = 0;
2609 struct md_rdev *rdev;
2610
2611 /* Constructor did not request data offset change */
2612 if (!test_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags)) {
2613 if (!rs_is_reshapable(rs))
2614 goto out;
2615
2616 return 0;
2617 }
2618
2619 /* HM FIXME: get InSync raid_dev? */
2620 rdev = &rs->dev[0].rdev;
2621
2622 if (rs->delta_disks < 0) {
2623 /*
2624 * Removing disks (reshaping backwards):
2625 *
2626 * - before reshape: data is at offset 0 and free space
2627 * is at end of each component LV
2628 *
2629 * - after reshape: data is at offset rs->data_offset != 0 on each component LV
2630 */
2631 data_offset = 0;
2632 new_data_offset = rs->data_offset;
2633
2634 } else if (rs->delta_disks > 0) {
2635 /*
2636 * Adding disks (reshaping forwards):
2637 *
2638 * - before reshape: data is at offset rs->data_offset != 0 and
2639 * free space is at begin of each component LV
2640 *
2641 * - after reshape: data is at offset 0 on each component LV
2642 */
2643 data_offset = rs->data_offset;
2644 new_data_offset = 0;
2645
2646 } else {
2647 /*
2648 * User space passes in 0 for data offset after having removed reshape space
2649 *
2650 * - or - (data offset != 0)
2651 *
2652 * Changing RAID layout or chunk size -> toggle offsets
2653 *
2654 * - before reshape: data is at offset rs->data_offset 0 and
2655 * free space is at end of each component LV
2656 * -or-
2657 * data is at offset rs->data_offset != 0 and
2658 * free space is at begin of each component LV
2659 *
Heinz Mauelshagen2527b562016-06-24 01:03:19 +02002660 * - after reshape: data is at offset 0 if it was at offset != 0
2661 * or at offset != 0 if it was at offset 0
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002662 * on each component LV
2663 *
2664 */
2665 data_offset = rs->data_offset ? rdev->data_offset : 0;
2666 new_data_offset = data_offset ? 0 : rs->data_offset;
2667 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
2668 }
2669
2670 /*
2671 * Make sure we got a minimum amount of free sectors per device
2672 */
2673 if (rs->data_offset &&
Heinz Mauelshagen052b2b12017-12-02 01:03:49 +01002674 to_sector(i_size_read(rdev->bdev->bd_inode)) - rs->md.dev_sectors < MIN_FREE_RESHAPE_SPACE) {
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002675 rs->ti->error = data_offset ? "No space for forward reshape" :
2676 "No space for backward reshape";
2677 return -ENOSPC;
2678 }
2679out:
Heinz Mauelshagen052b2b12017-12-02 01:03:49 +01002680 /*
2681 * Raise recovery_cp in case data_offset != 0 to
2682 * avoid false recovery positives in the constructor.
2683 */
2684 if (rs->md.recovery_cp < rs->md.dev_sectors)
2685 rs->md.recovery_cp += rs->dev[0].rdev.data_offset;
2686
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01002687 /* Adjust data offsets on all rdevs but on any raid4/5/6 journal device */
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002688 rdev_for_each(rdev, &rs->md) {
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01002689 if (!test_bit(Journal, &rdev->flags)) {
2690 rdev->data_offset = data_offset;
2691 rdev->new_data_offset = new_data_offset;
2692 }
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002693 }
2694
2695 return 0;
2696}
2697
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002698/* Userpace reordered disks -> adjust raid_disk indexes in @rs */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002699static void __reorder_raid_disk_indexes(struct raid_set *rs)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002700{
2701 int i = 0;
2702 struct md_rdev *rdev;
2703
2704 rdev_for_each(rdev, &rs->md) {
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01002705 if (!test_bit(Journal, &rdev->flags)) {
2706 rdev->raid_disk = i++;
2707 rdev->saved_raid_disk = rdev->new_raid_disk = -1;
2708 }
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002709 }
2710}
2711
2712/*
2713 * Setup @rs for takeover by a different raid level
2714 */
2715static int rs_setup_takeover(struct raid_set *rs)
2716{
2717 struct mddev *mddev = &rs->md;
2718 struct md_rdev *rdev;
2719 unsigned int d = mddev->raid_disks = rs->raid_disks;
2720 sector_t new_data_offset = rs->dev[0].rdev.data_offset ? 0 : rs->data_offset;
2721
2722 if (rt_is_raid10(rs->raid_type)) {
2723 if (mddev->level == 0) {
2724 /* Userpace reordered disks -> adjust raid_disk indexes */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002725 __reorder_raid_disk_indexes(rs);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002726
2727 /* raid0 -> raid10_far layout */
2728 mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_FAR,
2729 rs->raid10_copies);
2730 } else if (mddev->level == 1)
2731 /* raid1 -> raid10_near layout */
2732 mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_NEAR,
2733 rs->raid_disks);
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02002734 else
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002735 return -EINVAL;
2736
2737 }
2738
2739 clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
2740 mddev->recovery_cp = MaxSector;
2741
2742 while (d--) {
2743 rdev = &rs->dev[d].rdev;
2744
2745 if (test_bit(d, (void *) rs->rebuild_disks)) {
2746 clear_bit(In_sync, &rdev->flags);
2747 clear_bit(Faulty, &rdev->flags);
2748 mddev->recovery_cp = rdev->recovery_offset = 0;
2749 /* Bitmap has to be created when we do an "up" takeover */
2750 set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
2751 }
2752
2753 rdev->new_data_offset = new_data_offset;
2754 }
2755
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002756 return 0;
2757}
2758
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002759/* Prepare @rs for reshape */
2760static int rs_prepare_reshape(struct raid_set *rs)
2761{
2762 bool reshape;
2763 struct mddev *mddev = &rs->md;
2764
2765 if (rs_is_raid10(rs)) {
2766 if (rs->raid_disks != mddev->raid_disks &&
2767 __is_raid10_near(mddev->layout) &&
2768 rs->raid10_copies &&
2769 rs->raid10_copies != __raid10_near_copies(mddev->layout)) {
2770 /*
2771 * raid disk have to be multiple of data copies to allow this conversion,
2772 *
2773 * This is actually not a reshape it is a
2774 * rebuild of any additional mirrors per group
2775 */
2776 if (rs->raid_disks % rs->raid10_copies) {
2777 rs->ti->error = "Can't reshape raid10 mirror groups";
2778 return -EINVAL;
2779 }
2780
2781 /* Userpace reordered disks to add/remove mirrors -> adjust raid_disk indexes */
2782 __reorder_raid_disk_indexes(rs);
2783 mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_NEAR,
2784 rs->raid10_copies);
2785 mddev->new_layout = mddev->layout;
2786 reshape = false;
2787 } else
2788 reshape = true;
2789
2790 } else if (rs_is_raid456(rs))
2791 reshape = true;
2792
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002793 else if (rs_is_raid1(rs)) {
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02002794 if (rs->delta_disks) {
2795 /* Process raid1 via delta_disks */
2796 mddev->degraded = rs->delta_disks < 0 ? -rs->delta_disks : rs->delta_disks;
2797 reshape = true;
2798 } else {
2799 /* Process raid1 without delta_disks */
2800 mddev->raid_disks = rs->raid_disks;
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02002801 reshape = false;
2802 }
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002803 } else {
2804 rs->ti->error = "Called with bogus raid type";
2805 return -EINVAL;
2806 }
2807
2808 if (reshape) {
2809 set_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags);
2810 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02002811 } else if (mddev->raid_disks < rs->raid_disks)
2812 /* Create new superblocks and bitmaps, if any new disks */
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002813 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002814
2815 return 0;
2816}
2817
Heinz Mauelshagen052b2b12017-12-02 01:03:49 +01002818/* Get reshape sectors from data_offsets or raid set */
2819static sector_t _get_reshape_sectors(struct raid_set *rs)
2820{
2821 struct md_rdev *rdev;
2822 sector_t reshape_sectors = 0;
2823
2824 rdev_for_each(rdev, &rs->md)
2825 if (!test_bit(Journal, &rdev->flags)) {
2826 reshape_sectors = (rdev->data_offset > rdev->new_data_offset) ?
2827 rdev->data_offset - rdev->new_data_offset :
2828 rdev->new_data_offset - rdev->data_offset;
2829 break;
2830 }
2831
2832 return max(reshape_sectors, (sector_t) rs->data_offset);
2833}
2834
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002835/*
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002836 *
2837 * - change raid layout
2838 * - change chunk size
2839 * - add disks
2840 * - remove disks
2841 */
2842static int rs_setup_reshape(struct raid_set *rs)
2843{
2844 int r = 0;
2845 unsigned int cur_raid_devs, d;
Heinz Mauelshagen052b2b12017-12-02 01:03:49 +01002846 sector_t reshape_sectors = _get_reshape_sectors(rs);
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002847 struct mddev *mddev = &rs->md;
2848 struct md_rdev *rdev;
2849
2850 mddev->delta_disks = rs->delta_disks;
2851 cur_raid_devs = mddev->raid_disks;
2852
2853 /* Ignore impossible layout change whilst adding/removing disks */
2854 if (mddev->delta_disks &&
2855 mddev->layout != mddev->new_layout) {
2856 DMINFO("Ignoring invalid layout change with delta_disks=%d", rs->delta_disks);
2857 mddev->new_layout = mddev->layout;
2858 }
2859
2860 /*
2861 * Adjust array size:
2862 *
Heinz Mauelshagen052b2b12017-12-02 01:03:49 +01002863 * - in case of adding disk(s), array size has
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002864 * to grow after the disk adding reshape,
2865 * which'll hapen in the event handler;
2866 * reshape will happen forward, so space has to
2867 * be available at the beginning of each disk
2868 *
Heinz Mauelshagen052b2b12017-12-02 01:03:49 +01002869 * - in case of removing disk(s), array size
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002870 * has to shrink before starting the reshape,
2871 * which'll happen here;
2872 * reshape will happen backward, so space has to
2873 * be available at the end of each disk
2874 *
2875 * - data_offset and new_data_offset are
Heinz Mauelshagenae3c6cf2016-06-15 22:27:40 +02002876 * adjusted for aforementioned out of place
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002877 * reshaping based on userspace passing in
2878 * the "data_offset <sectors>" key/value
Heinz Mauelshagenae3c6cf2016-06-15 22:27:40 +02002879 * pair via the constructor
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002880 */
2881
2882 /* Add disk(s) */
2883 if (rs->delta_disks > 0) {
2884 /* Prepare disks for check in raid4/5/6/10 {check|start}_reshape */
2885 for (d = cur_raid_devs; d < rs->raid_disks; d++) {
2886 rdev = &rs->dev[d].rdev;
2887 clear_bit(In_sync, &rdev->flags);
2888
2889 /*
2890 * save_raid_disk needs to be -1, or recovery_offset will be set to 0
2891 * by md, which'll store that erroneously in the superblock on reshape
2892 */
2893 rdev->saved_raid_disk = -1;
2894 rdev->raid_disk = d;
2895
2896 rdev->sectors = mddev->dev_sectors;
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02002897 rdev->recovery_offset = rs_is_raid1(rs) ? 0 : MaxSector;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002898 }
2899
Heinz Mauelshagen052b2b12017-12-02 01:03:49 +01002900 mddev->reshape_backwards = 0; /* adding disk(s) -> forward reshape */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002901
2902 /* Remove disk(s) */
2903 } else if (rs->delta_disks < 0) {
2904 r = rs_set_dev_and_array_sectors(rs, true);
2905 mddev->reshape_backwards = 1; /* removing disk(s) -> backward reshape */
2906
2907 /* Change layout and/or chunk size */
2908 } else {
2909 /*
2910 * Reshape layout (e.g. raid5_ls -> raid5_n) and/or chunk size:
2911 *
2912 * keeping number of disks and do layout change ->
2913 *
2914 * toggle reshape_backward depending on data_offset:
2915 *
2916 * - free space upfront -> reshape forward
2917 *
2918 * - free space at the end -> reshape backward
2919 *
2920 *
2921 * This utilizes free reshape space avoiding the need
2922 * for userspace to move (parts of) LV segments in
2923 * case of layout/chunksize change (for disk
2924 * adding/removing reshape space has to be at
2925 * the proper address (see above with delta_disks):
2926 *
2927 * add disk(s) -> begin
2928 * remove disk(s)-> end
2929 */
2930 mddev->reshape_backwards = rs->dev[0].rdev.data_offset ? 0 : 1;
2931 }
2932
Heinz Mauelshagen052b2b12017-12-02 01:03:49 +01002933 /*
2934 * Adjust device size for forward reshape
2935 * because md_finish_reshape() reduces it.
2936 */
2937 if (!mddev->reshape_backwards)
2938 rdev_for_each(rdev, &rs->md)
2939 if (!test_bit(Journal, &rdev->flags))
2940 rdev->sectors += reshape_sectors;
2941
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002942 return r;
2943}
2944
2945/*
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002946 * Enable/disable discard support on RAID set depending on
2947 * RAID level and discard properties of underlying RAID members.
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002948 */
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002949static void configure_discard_support(struct raid_set *rs)
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002950{
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002951 int i;
2952 bool raid456;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002953 struct dm_target *ti = rs->ti;
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002954
Christoph Hellwig48920ff2017-04-05 19:21:23 +02002955 /*
2956 * XXX: RAID level 4,5,6 require zeroing for safety.
2957 */
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002958 raid456 = (rs->md.level == 4 || rs->md.level == 5 || rs->md.level == 6);
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002959
Heinz Mauelshagenffeeac72016-06-30 14:37:50 +02002960 for (i = 0; i < rs->raid_disks; i++) {
Heinz Mauelshagend20c4b02014-10-29 19:02:27 +01002961 struct request_queue *q;
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002962
Heinz Mauelshagend20c4b02014-10-29 19:02:27 +01002963 if (!rs->dev[i].rdev.bdev)
2964 continue;
2965
2966 q = bdev_get_queue(rs->dev[i].rdev.bdev);
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002967 if (!q || !blk_queue_discard(q))
2968 return;
2969
2970 if (raid456) {
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002971 if (!devices_handle_discard_safely) {
2972 DMERR("raid456 discard support disabled due to discard_zeroes_data uncertainty.");
2973 DMERR("Set dm-raid.devices_handle_discard_safely=Y to override.");
2974 return;
2975 }
2976 }
2977 }
2978
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002979 /*
2980 * RAID1 and RAID10 personalities require bio splitting,
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002981 * RAID0/4/5/6 don't and process large discard bios properly.
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002982 */
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002983 ti->split_discard_bios = !!(rs->md.level == 1 || rs->md.level == 10);
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002984 ti->num_discard_bios = 1;
2985}
2986
2987/*
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002988 * Construct a RAID0/1/10/4/5/6 mapping:
NeilBrown9d09e662011-01-13 20:00:02 +00002989 * Args:
Mike Snitzer43157842016-05-30 13:03:37 -04002990 * <raid_type> <#raid_params> <raid_params>{0,} \
2991 * <#raid_devs> [<meta_dev1> <dev1>]{1,}
NeilBrown9d09e662011-01-13 20:00:02 +00002992 *
Mike Snitzer43157842016-05-30 13:03:37 -04002993 * <raid_params> varies by <raid_type>. See 'parse_raid_params' for
NeilBrown9d09e662011-01-13 20:00:02 +00002994 * details on possible <raid_params>.
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002995 *
2996 * Userspace is free to initialize the metadata devices, hence the superblocks to
2997 * enforce recreation based on the passed in table parameters.
2998 *
NeilBrown9d09e662011-01-13 20:00:02 +00002999 */
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02003000static int raid_ctr(struct dm_target *ti, unsigned int argc, char **argv)
NeilBrown9d09e662011-01-13 20:00:02 +00003001{
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003002 int r;
Heinz Mauelshagen75015372017-12-02 01:03:50 +01003003 bool resize = false;
NeilBrown9d09e662011-01-13 20:00:02 +00003004 struct raid_type *rt;
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02003005 unsigned int num_raid_params, num_raid_devs;
Heinz Mauelshagen75015372017-12-02 01:03:50 +01003006 sector_t calculated_dev_sectors, rdev_sectors, reshape_sectors;
NeilBrown9d09e662011-01-13 20:00:02 +00003007 struct raid_set *rs = NULL;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02003008 const char *arg;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003009 struct rs_layout rs_layout;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02003010 struct dm_arg_set as = { argc, argv }, as_nrd;
3011 struct dm_arg _args[] = {
3012 { 0, as.argc, "Cannot understand number of raid parameters" },
3013 { 1, 254, "Cannot understand number of raid devices parameters" }
3014 };
NeilBrown9d09e662011-01-13 20:00:02 +00003015
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02003016 /* Must have <raid_type> */
3017 arg = dm_shift_arg(&as);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04003018 if (!arg) {
3019 ti->error = "No arguments";
3020 return -EINVAL;
3021 }
NeilBrown9d09e662011-01-13 20:00:02 +00003022
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02003023 rt = get_raid_type(arg);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04003024 if (!rt) {
3025 ti->error = "Unrecognised raid_type";
3026 return -EINVAL;
3027 }
NeilBrown9d09e662011-01-13 20:00:02 +00003028
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02003029 /* Must have <#raid_params> */
3030 if (dm_read_arg_group(_args, &as, &num_raid_params, &ti->error))
Mike Snitzer43157842016-05-30 13:03:37 -04003031 return -EINVAL;
NeilBrown9d09e662011-01-13 20:00:02 +00003032
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02003033 /* number of raid device tupples <meta_dev data_dev> */
3034 as_nrd = as;
3035 dm_consume_args(&as_nrd, num_raid_params);
3036 _args[1].max = (as_nrd.argc - 1) / 2;
3037 if (dm_read_arg(_args + 1, &as_nrd, &num_raid_devs, &ti->error))
Mike Snitzer43157842016-05-30 13:03:37 -04003038 return -EINVAL;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02003039
Mike Snitzerbb91a632016-06-02 12:06:54 -04003040 if (!__within_range(num_raid_devs, 1, MAX_RAID_DEVICES)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04003041 ti->error = "Invalid number of supplied raid devices";
3042 return -EINVAL;
3043 }
NeilBrown9d09e662011-01-13 20:00:02 +00003044
Mike Snitzerbfcee0e2016-06-02 15:08:09 -04003045 rs = raid_set_alloc(ti, rt, num_raid_devs);
NeilBrown9d09e662011-01-13 20:00:02 +00003046 if (IS_ERR(rs))
3047 return PTR_ERR(rs);
3048
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02003049 r = parse_raid_params(rs, &as, num_raid_params);
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003050 if (r)
NeilBrown9d09e662011-01-13 20:00:02 +00003051 goto bad;
3052
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02003053 r = parse_dev_params(rs, &as);
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003054 if (r)
NeilBrown9d09e662011-01-13 20:00:02 +00003055 goto bad;
3056
Jonathan Brassowb12d4372011-08-02 12:32:07 +01003057 rs->md.sync_super = super_sync;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003058
Heinz Mauelshagen2527b562016-06-24 01:03:19 +02003059 /*
3060 * Calculate ctr requested array and device sizes to allow
3061 * for superblock analysis needing device sizes defined.
3062 *
3063 * Any existing superblock will overwrite the array and device sizes
3064 */
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02003065 r = rs_set_dev_and_array_sectors(rs, false);
3066 if (r)
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02003067 goto bad;
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02003068
Heinz Mauelshagen50c4feb2017-01-14 03:53:08 +01003069 calculated_dev_sectors = rs->md.dev_sectors;
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02003070
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003071 /*
3072 * Backup any new raid set level, layout, ...
3073 * requested to be able to compare to superblock
3074 * members for conversion decisions.
3075 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003076 rs_config_backup(rs, &rs_layout);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003077
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003078 r = analyse_superblocks(ti, rs);
3079 if (r)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01003080 goto bad;
3081
Heinz Mauelshagen4d49f1b2017-06-30 15:45:58 +02003082 rdev_sectors = __rdev_sectors(rs);
3083 if (!rdev_sectors) {
3084 ti->error = "Invalid rdev size";
3085 r = -EINVAL;
3086 goto bad;
3087 }
3088
Heinz Mauelshagen75015372017-12-02 01:03:50 +01003089
3090 reshape_sectors = _get_reshape_sectors(rs);
3091 if (calculated_dev_sectors != rdev_sectors)
3092 resize = calculated_dev_sectors != (reshape_sectors ? rdev_sectors - reshape_sectors : rdev_sectors);
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02003093
NeilBrown9d09e662011-01-13 20:00:02 +00003094 INIT_WORK(&rs->md.event_work, do_table_event);
NeilBrown9d09e662011-01-13 20:00:02 +00003095 ti->private = rs;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003096 ti->num_flush_bios = 1;
NeilBrown9d09e662011-01-13 20:00:02 +00003097
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003098 /* Restore any requested new layout for conversion decision */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003099 rs_config_restore(rs, &rs_layout);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003100
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02003101 /*
3102 * Now that we have any superblock metadata available,
3103 * check for new, recovering, reshaping, to be taken over,
3104 * to be reshaped or an existing, unchanged raid set to
3105 * run in sequence.
3106 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003107 if (test_bit(MD_ARRAY_FIRST_USE, &rs->md.flags)) {
Heinz Mauelshagen2d92a3c2016-06-24 00:32:58 +02003108 /* A new raid6 set has to be recovered to ensure proper parity and Q-Syndrome */
3109 if (rs_is_raid6(rs) &&
3110 test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) {
3111 ti->error = "'nosync' not allowed for new raid6 set";
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02003112 r = -EINVAL;
3113 goto bad;
Heinz Mauelshagen2d92a3c2016-06-24 00:32:58 +02003114 }
3115 rs_setup_recovery(rs, 0);
Heinz Mauelshagen2a5556c2016-06-27 14:44:09 +02003116 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
3117 rs_set_new(rs);
3118 } else if (rs_is_recovering(rs)) {
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02003119 /* A recovering raid set may be resized */
Heinz Mauelshagen2a5556c2016-06-27 14:44:09 +02003120 ; /* skip setup rs */
3121 } else if (rs_is_reshaping(rs)) {
3122 /* Have to reject size change request during reshape */
3123 if (resize) {
3124 ti->error = "Can't resize a reshaping raid set";
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02003125 r = -EPERM;
3126 goto bad;
Heinz Mauelshagen2d92a3c2016-06-24 00:32:58 +02003127 }
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02003128 /* skip setup rs */
Heinz Mauelshagen2d92a3c2016-06-24 00:32:58 +02003129 } else if (rs_takeover_requested(rs)) {
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003130 if (rs_is_reshaping(rs)) {
3131 ti->error = "Can't takeover a reshaping raid set";
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02003132 r = -EPERM;
3133 goto bad;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003134 }
3135
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003136 /* We can't takeover a journaled raid4/5/6 */
3137 if (test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags)) {
3138 ti->error = "Can't takeover a journaled raid4/5/6 set";
3139 r = -EPERM;
3140 goto bad;
3141 }
3142
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003143 /*
Heinz Mauelshagen2527b562016-06-24 01:03:19 +02003144 * If a takeover is needed, userspace sets any additional
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02003145 * devices to rebuild and we can check for a valid request here.
3146 *
3147 * If acceptible, set the level to the new requested
Heinz Mauelshagen2a5556c2016-06-27 14:44:09 +02003148 * one, prohibit requesting recovery, allow the raid
3149 * set to run and store superblocks during resume.
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003150 */
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003151 r = rs_check_takeover(rs);
3152 if (r)
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02003153 goto bad;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003154
3155 r = rs_setup_takeover(rs);
3156 if (r)
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02003157 goto bad;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003158
Mike Snitzer42863252016-06-02 12:27:46 -04003159 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02003160 /* Takeover ain't recovery, so disable recovery */
Heinz Mauelshagen2a5556c2016-06-27 14:44:09 +02003161 rs_setup_recovery(rs, MaxSector);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003162 rs_set_new(rs);
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02003163 } else if (rs_reshape_requested(rs)) {
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003164 /*
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003165 * No need to check for 'ongoing' takeover here, because takeover
3166 * is an instant operation as oposed to an ongoing reshape.
3167 */
3168
3169 /* We can't reshape a journaled raid4/5/6 */
3170 if (test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags)) {
3171 ti->error = "Can't reshape a journaled raid4/5/6 set";
3172 r = -EPERM;
3173 goto bad;
3174 }
3175
3176 /*
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02003177 * We can only prepare for a reshape here, because the
3178 * raid set needs to run to provide the repective reshape
3179 * check functions via its MD personality instance.
3180 *
3181 * So do the reshape check after md_run() succeeded.
3182 */
3183 r = rs_prepare_reshape(rs);
3184 if (r)
3185 return r;
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04003186
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02003187 /* Reshaping ain't recovery, so disable recovery */
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02003188 rs_setup_recovery(rs, MaxSector);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003189 rs_set_cur(rs);
Heinz Mauelshagen2a5556c2016-06-27 14:44:09 +02003190 } else {
3191 /* May not set recovery when a device rebuild is requested */
Heinz Mauelshagen37f10be2016-06-24 23:21:37 +02003192 if (test_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags)) {
3193 rs_setup_recovery(rs, MaxSector);
3194 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
3195 } else
3196 rs_setup_recovery(rs, test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags) ?
Heinz Mauelshagen2a5556c2016-06-27 14:44:09 +02003197 0 : (resize ? calculated_dev_sectors : MaxSector));
3198 rs_set_cur(rs);
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02003199 }
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003200
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02003201 /* If constructor requested it, change data and new_data offsets */
3202 r = rs_adjust_data_offsets(rs);
3203 if (r)
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02003204 goto bad;
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02003205
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003206 /* Start raid set read-only and assumed clean to change in raid_resume() */
3207 rs->md.ro = 1;
3208 rs->md.in_sync = 1;
3209 set_bit(MD_RECOVERY_FROZEN, &rs->md.recovery);
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02003210
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02003211 /* Has to be held on running the array */
3212 mddev_lock_nointr(&rs->md);
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003213 r = md_run(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00003214 rs->md.in_sync = 0; /* Assume already marked dirty */
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003215 if (r) {
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003216 ti->error = "Failed to run raid array";
3217 mddev_unlock(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00003218 goto bad;
3219 }
3220
3221 rs->callbacks.congested_fn = raid_is_congested;
NeilBrown9d09e662011-01-13 20:00:02 +00003222 dm_table_add_target_callbacks(ti->table, &rs->callbacks);
3223
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01003224 /* If raid4/5/6 journal mode explictely requested (only possible with journal dev) -> set it */
3225 if (test_bit(__CTR_FLAG_JOURNAL_MODE, &rs->ctr_flags)) {
3226 r = r5c_journal_mode_set(&rs->md, rs->journal_dev.mode);
3227 if (r) {
3228 ti->error = "Failed to set raid4/5/6 journal mode";
3229 mddev_unlock(&rs->md);
3230 goto bad_journal_mode_set;
3231 }
3232 }
3233
Jonathan Brassow32737272011-08-02 12:32:07 +01003234 mddev_suspend(&rs->md);
Heinz Mauelshagen0cf352e2017-07-13 17:34:24 +02003235 set_bit(RT_FLAG_RS_SUSPENDED, &rs->runtime_flags);
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003236
3237 /* Try to adjust the raid4/5/6 stripe cache size to the stripe size */
3238 if (rs_is_raid456(rs)) {
3239 r = rs_set_raid456_stripe_cache(rs);
3240 if (r)
3241 goto bad_stripe_cache;
3242 }
3243
3244 /* Now do an early reshape check */
3245 if (test_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags)) {
3246 r = rs_check_reshape(rs);
3247 if (r)
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02003248 goto bad_check_reshape;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003249
3250 /* Restore new, ctr requested layout to perform check */
3251 rs_config_restore(rs, &rs_layout);
3252
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003253 if (rs->md.pers->start_reshape) {
3254 r = rs->md.pers->check_reshape(&rs->md);
3255 if (r) {
3256 ti->error = "Reshape check failed";
3257 goto bad_check_reshape;
3258 }
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003259 }
3260 }
3261
Heinz Mauelshagen11e29682016-11-29 22:37:30 +01003262 /* Disable/enable discard support on raid set. */
3263 configure_discard_support(rs);
3264
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003265 mddev_unlock(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00003266 return 0;
3267
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01003268bad_journal_mode_set:
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003269bad_stripe_cache:
3270bad_check_reshape:
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05003271 md_stop(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00003272bad:
Mike Snitzerbfcee0e2016-06-02 15:08:09 -04003273 raid_set_free(rs);
NeilBrown9d09e662011-01-13 20:00:02 +00003274
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003275 return r;
NeilBrown9d09e662011-01-13 20:00:02 +00003276}
3277
3278static void raid_dtr(struct dm_target *ti)
3279{
3280 struct raid_set *rs = ti->private;
3281
3282 list_del_init(&rs->callbacks.list);
3283 md_stop(&rs->md);
Mike Snitzerbfcee0e2016-06-02 15:08:09 -04003284 raid_set_free(rs);
NeilBrown9d09e662011-01-13 20:00:02 +00003285}
3286
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00003287static int raid_map(struct dm_target *ti, struct bio *bio)
NeilBrown9d09e662011-01-13 20:00:02 +00003288{
3289 struct raid_set *rs = ti->private;
NeilBrownfd01b882011-10-11 16:47:53 +11003290 struct mddev *mddev = &rs->md;
NeilBrown9d09e662011-01-13 20:00:02 +00003291
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003292 /*
3293 * If we're reshaping to add disk(s)), ti->len and
3294 * mddev->array_sectors will differ during the process
3295 * (ti->len > mddev->array_sectors), so we have to requeue
3296 * bios with addresses > mddev->array_sectors here or
Heinz Mauelshagen2527b562016-06-24 01:03:19 +02003297 * there will occur accesses past EOD of the component
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003298 * data images thus erroring the raid set.
3299 */
3300 if (unlikely(bio_end_sector(bio) > mddev->array_sectors))
3301 return DM_MAPIO_REQUEUE;
3302
Shaohua Lic4d6a1b2017-09-21 10:29:22 -07003303 md_handle_request(mddev, bio);
NeilBrown9d09e662011-01-13 20:00:02 +00003304
3305 return DM_MAPIO_SUBMITTED;
3306}
3307
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003308/* Return string describing the current sync action of @mddev */
Heinz Mauelshagen67143512017-12-02 01:03:53 +01003309static const char *decipher_sync_action(struct mddev *mddev, unsigned long recovery)
Jonathan Brassowbe836512013-04-24 11:42:43 +10003310{
Heinz Mauelshagen67143512017-12-02 01:03:53 +01003311 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
Jonathan Brassowbe836512013-04-24 11:42:43 +10003312 return "frozen";
3313
Heinz Mauelshagen4102d9d2017-12-02 01:03:55 +01003314 /* The MD sync thread can be done with io but still be running */
3315 if (!test_bit(MD_RECOVERY_DONE, &recovery) &&
3316 (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
3317 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery)))) {
Heinz Mauelshagen67143512017-12-02 01:03:53 +01003318 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
Jonathan Brassowbe836512013-04-24 11:42:43 +10003319 return "reshape";
3320
Heinz Mauelshagen67143512017-12-02 01:03:53 +01003321 if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
3322 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
Jonathan Brassowbe836512013-04-24 11:42:43 +10003323 return "resync";
Heinz Mauelshagen67143512017-12-02 01:03:53 +01003324 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
Jonathan Brassowbe836512013-04-24 11:42:43 +10003325 return "check";
3326 return "repair";
3327 }
3328
Heinz Mauelshagen67143512017-12-02 01:03:53 +01003329 if (test_bit(MD_RECOVERY_RECOVER, &recovery))
Jonathan Brassowbe836512013-04-24 11:42:43 +10003330 return "recover";
3331 }
3332
3333 return "idle";
3334}
3335
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003336/*
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003337 * Return status string for @rdev
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003338 *
3339 * Status characters:
3340 *
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003341 * 'D' = Dead/Failed raid set component or raid4/5/6 journal device
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01003342 * 'a' = Alive but not in-sync raid set component _or_ alive raid4/5/6 'write_back' journal device
3343 * 'A' = Alive and in-sync raid set component _or_ alive raid4/5/6 'write_through' journal device
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01003344 * '-' = Non-existing device (i.e. uspace passed '- -' into the ctr)
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003345 */
Heinz Mauelshagen242ea5a2017-12-02 01:03:54 +01003346static const char *__raid_dev_status(struct raid_set *rs, struct md_rdev *rdev)
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003347{
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01003348 if (!rdev->bdev)
3349 return "-";
3350 else if (test_bit(Faulty, &rdev->flags))
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003351 return "D";
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003352 else if (test_bit(Journal, &rdev->flags))
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01003353 return (rs->journal_dev.mode == R5C_JOURNAL_MODE_WRITE_THROUGH) ? "A" : "a";
Heinz Mauelshagen4102d9d2017-12-02 01:03:55 +01003354 else if (test_bit(RT_FLAG_RS_RESYNCING, &rs->runtime_flags) ||
3355 (!test_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags) &&
3356 !test_bit(In_sync, &rdev->flags)))
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003357 return "a";
3358 else
3359 return "A";
3360}
3361
Heinz Mauelshagen242ea5a2017-12-02 01:03:54 +01003362/* Helper to return resync/reshape progress for @rs and runtime flags for raid set in sync / resynching */
Heinz Mauelshagen67143512017-12-02 01:03:53 +01003363static sector_t rs_get_progress(struct raid_set *rs, unsigned long recovery,
Heinz Mauelshagen242ea5a2017-12-02 01:03:54 +01003364 sector_t resync_max_sectors)
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003365{
Heinz Mauelshagen4102d9d2017-12-02 01:03:55 +01003366 sector_t r;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003367 struct mddev *mddev = &rs->md;
3368
Heinz Mauelshagen242ea5a2017-12-02 01:03:54 +01003369 clear_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
Heinz Mauelshagen4102d9d2017-12-02 01:03:55 +01003370 clear_bit(RT_FLAG_RS_RESYNCING, &rs->runtime_flags);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003371
3372 if (rs_is_raid0(rs)) {
3373 r = resync_max_sectors;
Heinz Mauelshagen242ea5a2017-12-02 01:03:54 +01003374 set_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003375
3376 } else {
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003377 /* Reshape is relative to the array size */
Heinz Mauelshagen4102d9d2017-12-02 01:03:55 +01003378 if (test_bit(MD_RECOVERY_RESHAPE, &recovery)) {
3379 r = mddev->reshape_position;
3380 if (r != MaxSector) {
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003381 /* Got to reverse on backward reshape */
3382 if (mddev->reshape_backwards)
3383 r = mddev->array_sectors - r;
3384
Heinz Mauelshagen4102d9d2017-12-02 01:03:55 +01003385 /* Divide by # of data stripes unless raid1 */
3386 if (!rs_is_raid1(rs))
3387 sector_div(r, mddev_data_stripes(rs));
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003388 }
3389
Heinz Mauelshagen4102d9d2017-12-02 01:03:55 +01003390 /*
3391 * Sync/recover is relative to the component device size.
3392 *
3393 * MD_RECOVERY_NEEDED for https://bugzilla.redhat.com/show_bug.cgi?id=1508070
3394 */
3395 } else if (test_bit(MD_RECOVERY_NEEDED, &recovery) ||
3396 test_bit(MD_RECOVERY_RUNNING, &recovery))
3397 r = mddev->curr_resync_completed;
3398
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003399 else
Jonathan Brassow41dcf192017-10-02 17:17:35 -05003400 r = mddev->recovery_cp;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003401
Heinz Mauelshagen4102d9d2017-12-02 01:03:55 +01003402 if (r >= resync_max_sectors &&
3403 (!test_bit(MD_RECOVERY_REQUESTED, &recovery) ||
3404 (!test_bit(MD_RECOVERY_FROZEN, &recovery) &&
3405 !test_bit(MD_RECOVERY_NEEDED, &recovery) &&
3406 !test_bit(MD_RECOVERY_RUNNING, &recovery)))) {
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003407 /*
3408 * Sync complete.
3409 */
Heinz Mauelshagen4102d9d2017-12-02 01:03:55 +01003410 /* In case we have finished recovering, the array is in sync. */
3411 if (test_bit(MD_RECOVERY_RECOVER, &recovery))
3412 set_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
3413
3414 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery)) {
3415 /*
3416 * In case we are recovering, the array is not in sync
3417 * and health chars should show the recovering legs.
3418 */
3419 ;
3420
3421 } else if (test_bit(MD_RECOVERY_SYNC, &recovery) &&
3422 !test_bit(MD_RECOVERY_REQUESTED, &recovery)) {
3423 /*
3424 * If "resync" is occurring, the raid set
3425 * is or may be out of sync hence the health
3426 * characters shall be 'a'.
3427 */
3428 set_bit(RT_FLAG_RS_RESYNCING, &rs->runtime_flags);
3429
Heinz Mauelshagen67143512017-12-02 01:03:53 +01003430 } else if (test_bit(MD_RECOVERY_REQUESTED, &recovery)) {
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003431 /*
3432 * If "check" or "repair" is occurring, the raid set has
3433 * undergone an initial sync and the health characters
3434 * should not be 'a' anymore.
3435 */
Heinz Mauelshagen242ea5a2017-12-02 01:03:54 +01003436 set_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
Heinz Mauelshagen4102d9d2017-12-02 01:03:55 +01003437
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003438 } else {
3439 struct md_rdev *rdev;
3440
3441 /*
Heinz Mauelshagen4102d9d2017-12-02 01:03:55 +01003442 * We are idle and recovery is needed, prevent 'A' chars race
3443 * caused by components still set to in-sync by constrcuctor.
3444 */
3445 if (test_bit(MD_RECOVERY_NEEDED, &recovery))
3446 set_bit(RT_FLAG_RS_RESYNCING, &rs->runtime_flags);
3447
3448 /*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003449 * The raid set may be doing an initial sync, or it may
Mike Snitzer43157842016-05-30 13:03:37 -04003450 * be rebuilding individual components. If all the
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003451 * devices are In_sync, then it is the raid set that is
3452 * being initialized.
3453 */
Heinz Mauelshagen4102d9d2017-12-02 01:03:55 +01003454 set_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003455 rdev_for_each(rdev, mddev)
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003456 if (!test_bit(Journal, &rdev->flags) &&
Heinz Mauelshagen4102d9d2017-12-02 01:03:55 +01003457 !test_bit(In_sync, &rdev->flags)) {
3458 clear_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
3459 break;
3460 }
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003461 }
3462 }
3463
Heinz Mauelshagen4102d9d2017-12-02 01:03:55 +01003464 return min(r, resync_max_sectors);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003465}
3466
3467/* Helper to return @dev name or "-" if !@dev */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04003468static const char *__get_dev_name(struct dm_dev *dev)
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003469{
3470 return dev ? dev->name : "-";
3471}
3472
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003473static void raid_status(struct dm_target *ti, status_type_t type,
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003474 unsigned int status_flags, char *result, unsigned int maxlen)
NeilBrown9d09e662011-01-13 20:00:02 +00003475{
3476 struct raid_set *rs = ti->private;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003477 struct mddev *mddev = &rs->md;
3478 struct r5conf *conf = mddev->private;
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003479 int i, max_nr_stripes = conf ? conf->max_nr_stripes : 0;
Heinz Mauelshagen67143512017-12-02 01:03:53 +01003480 unsigned long recovery;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003481 unsigned int raid_param_cnt = 1; /* at least 1 for chunksize */
3482 unsigned int sz = 0;
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003483 unsigned int rebuild_disks;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003484 unsigned int write_mostly_params = 0;
3485 sector_t progress, resync_max_sectors, resync_mismatches;
3486 const char *sync_action;
3487 struct raid_type *rt;
NeilBrown9d09e662011-01-13 20:00:02 +00003488
3489 switch (type) {
3490 case STATUSTYPE_INFO:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003491 /* *Should* always succeed */
3492 rt = get_raid_type_by_ll(mddev->new_level, mddev->new_layout);
3493 if (!rt)
3494 return;
NeilBrown9d09e662011-01-13 20:00:02 +00003495
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003496 DMEMIT("%s %d ", rt->name, mddev->raid_disks);
NeilBrown9d09e662011-01-13 20:00:02 +00003497
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003498 /* Access most recent mddev properties for status output */
3499 smp_rmb();
Heinz Mauelshagen67143512017-12-02 01:03:53 +01003500 recovery = rs->md.recovery;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003501 /* Get sensible max sectors even if raid set not yet started */
Mike Snitzer42863252016-06-02 12:27:46 -04003502 resync_max_sectors = test_bit(RT_FLAG_RS_PRERESUMED, &rs->runtime_flags) ?
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003503 mddev->resync_max_sectors : mddev->dev_sectors;
Heinz Mauelshagen242ea5a2017-12-02 01:03:54 +01003504 progress = rs_get_progress(rs, recovery, resync_max_sectors);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003505 resync_mismatches = (mddev->last_sync_action && !strcasecmp(mddev->last_sync_action, "check")) ?
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003506 atomic64_read(&mddev->resync_mismatches) : 0;
Heinz Mauelshagen67143512017-12-02 01:03:53 +01003507 sync_action = decipher_sync_action(&rs->md, recovery);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003508
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01003509 /* HM FIXME: do we want another state char for raid0? It shows 'D'/'A'/'-' now */
3510 for (i = 0; i < rs->raid_disks; i++)
Heinz Mauelshagen242ea5a2017-12-02 01:03:54 +01003511 DMEMIT(__raid_dev_status(rs, &rs->dev[i].rdev));
Jonathan Brassowbe836512013-04-24 11:42:43 +10003512
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00003513 /*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003514 * In-sync/Reshape ratio:
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00003515 * The in-sync ratio shows the progress of:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003516 * - Initializing the raid set
3517 * - Rebuilding a subset of devices of the raid set
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00003518 * The user can distinguish between the two by referring
3519 * to the status characters.
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003520 *
3521 * The reshape ratio shows the progress of
3522 * changing the raid layout or the number of
3523 * disks of a raid set
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00003524 */
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003525 DMEMIT(" %llu/%llu", (unsigned long long) progress,
3526 (unsigned long long) resync_max_sectors);
NeilBrown9d09e662011-01-13 20:00:02 +00003527
Jonathan Brassowbe836512013-04-24 11:42:43 +10003528 /*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003529 * v1.5.0+:
3530 *
Jonathan Brassowbe836512013-04-24 11:42:43 +10003531 * Sync action:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003532 * See Documentation/device-mapper/dm-raid.txt for
Jonathan Brassowbe836512013-04-24 11:42:43 +10003533 * information on each of these states.
3534 */
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003535 DMEMIT(" %s", sync_action);
Jonathan Brassowbe836512013-04-24 11:42:43 +10003536
3537 /*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003538 * v1.5.0+:
3539 *
Jonathan Brassowbe836512013-04-24 11:42:43 +10003540 * resync_mismatches/mismatch_cnt
3541 * This field shows the number of discrepancies found when
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003542 * performing a "check" of the raid set.
Jonathan Brassowbe836512013-04-24 11:42:43 +10003543 */
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003544 DMEMIT(" %llu", (unsigned long long) resync_mismatches);
3545
3546 /*
Mike Snitzer9b6e5422016-06-02 11:48:09 -04003547 * v1.9.0+:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003548 *
3549 * data_offset (needed for out of space reshaping)
3550 * This field shows the data offset into the data
3551 * image LV where the first stripes data starts.
3552 *
3553 * We keep data_offset equal on all raid disks of the set,
3554 * so retrieving it from the first raid disk is sufficient.
3555 */
3556 DMEMIT(" %llu", (unsigned long long) rs->dev[0].rdev.data_offset);
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003557
3558 /*
3559 * v1.10.0+:
3560 */
3561 DMEMIT(" %s", test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags) ?
Heinz Mauelshagen242ea5a2017-12-02 01:03:54 +01003562 __raid_dev_status(rs, &rs->journal_dev.rdev) : "-");
NeilBrown9d09e662011-01-13 20:00:02 +00003563 break;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003564
NeilBrown9d09e662011-01-13 20:00:02 +00003565 case STATUSTYPE_TABLE:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003566 /* Report the table line string you would use to construct this raid set */
NeilBrown9d09e662011-01-13 20:00:02 +00003567
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003568 /* Calculate raid parameter count */
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003569 for (i = 0; i < rs->raid_disks; i++)
3570 if (test_bit(WriteMostly, &rs->dev[i].rdev.flags))
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003571 write_mostly_params += 2;
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003572 rebuild_disks = memweight(rs->rebuild_disks, DISKS_ARRAY_ELEMS * sizeof(*rs->rebuild_disks));
3573 raid_param_cnt += rebuild_disks * 2 +
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003574 write_mostly_params +
3575 hweight32(rs->ctr_flags & CTR_FLAG_OPTIONS_NO_ARGS) +
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003576 hweight32(rs->ctr_flags & CTR_FLAG_OPTIONS_ONE_ARG) * 2 +
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01003577 (test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags) ? 2 : 0) +
3578 (test_bit(__CTR_FLAG_JOURNAL_MODE, &rs->ctr_flags) ? 2 : 0);
Heinz Mauelshagen4464e362017-03-18 01:39:12 +01003579
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003580 /* Emit table line */
Heinz Mauelshagen4464e362017-03-18 01:39:12 +01003581 /* This has to be in the documented order for userspace! */
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003582 DMEMIT("%s %u %u", rs->raid_type->name, raid_param_cnt, mddev->new_chunk_sectors);
Mike Snitzer42863252016-06-02 12:27:46 -04003583 if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003584 DMEMIT(" %s", dm_raid_arg_name_by_flag(CTR_FLAG_SYNC));
Heinz Mauelshagen4464e362017-03-18 01:39:12 +01003585 if (test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags))
3586 DMEMIT(" %s", dm_raid_arg_name_by_flag(CTR_FLAG_NOSYNC));
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003587 if (rebuild_disks)
3588 for (i = 0; i < rs->raid_disks; i++)
3589 if (test_bit(rs->dev[i].rdev.raid_disk, (void *) rs->rebuild_disks))
3590 DMEMIT(" %s %u", dm_raid_arg_name_by_flag(CTR_FLAG_REBUILD),
3591 rs->dev[i].rdev.raid_disk);
Heinz Mauelshagen4464e362017-03-18 01:39:12 +01003592 if (test_bit(__CTR_FLAG_DAEMON_SLEEP, &rs->ctr_flags))
3593 DMEMIT(" %s %lu", dm_raid_arg_name_by_flag(CTR_FLAG_DAEMON_SLEEP),
3594 mddev->bitmap_info.daemon_sleep);
3595 if (test_bit(__CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags))
3596 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_MIN_RECOVERY_RATE),
3597 mddev->sync_speed_min);
3598 if (test_bit(__CTR_FLAG_MAX_RECOVERY_RATE, &rs->ctr_flags))
3599 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_MAX_RECOVERY_RATE),
3600 mddev->sync_speed_max);
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003601 if (write_mostly_params)
3602 for (i = 0; i < rs->raid_disks; i++)
3603 if (test_bit(WriteMostly, &rs->dev[i].rdev.flags))
3604 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_WRITE_MOSTLY),
3605 rs->dev[i].rdev.raid_disk);
Mike Snitzer42863252016-06-02 12:27:46 -04003606 if (test_bit(__CTR_FLAG_MAX_WRITE_BEHIND, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003607 DMEMIT(" %s %lu", dm_raid_arg_name_by_flag(CTR_FLAG_MAX_WRITE_BEHIND),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003608 mddev->bitmap_info.max_write_behind);
Heinz Mauelshagen4464e362017-03-18 01:39:12 +01003609 if (test_bit(__CTR_FLAG_STRIPE_CACHE, &rs->ctr_flags))
3610 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_STRIPE_CACHE),
3611 max_nr_stripes);
3612 if (test_bit(__CTR_FLAG_REGION_SIZE, &rs->ctr_flags))
3613 DMEMIT(" %s %llu", dm_raid_arg_name_by_flag(CTR_FLAG_REGION_SIZE),
3614 (unsigned long long) to_sector(mddev->bitmap_info.chunksize));
3615 if (test_bit(__CTR_FLAG_RAID10_COPIES, &rs->ctr_flags))
3616 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_COPIES),
3617 raid10_md_layout_to_copies(mddev->layout));
3618 if (test_bit(__CTR_FLAG_RAID10_FORMAT, &rs->ctr_flags))
3619 DMEMIT(" %s %s", dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_FORMAT),
3620 raid10_md_layout_to_format(mddev->layout));
3621 if (test_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags))
3622 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_DELTA_DISKS),
3623 max(rs->delta_disks, mddev->delta_disks));
3624 if (test_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags))
3625 DMEMIT(" %s %llu", dm_raid_arg_name_by_flag(CTR_FLAG_DATA_OFFSET),
3626 (unsigned long long) rs->data_offset);
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003627 if (test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags))
3628 DMEMIT(" %s %s", dm_raid_arg_name_by_flag(CTR_FLAG_JOURNAL_DEV),
3629 __get_dev_name(rs->journal_dev.dev));
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01003630 if (test_bit(__CTR_FLAG_JOURNAL_MODE, &rs->ctr_flags))
3631 DMEMIT(" %s %s", dm_raid_arg_name_by_flag(CTR_FLAG_JOURNAL_MODE),
3632 md_journal_mode_to_dm_raid(rs->journal_dev.mode));
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003633 DMEMIT(" %d", rs->raid_disks);
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003634 for (i = 0; i < rs->raid_disks; i++)
3635 DMEMIT(" %s %s", __get_dev_name(rs->dev[i].meta_dev),
3636 __get_dev_name(rs->dev[i].data_dev));
NeilBrown9d09e662011-01-13 20:00:02 +00003637 }
NeilBrown9d09e662011-01-13 20:00:02 +00003638}
3639
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02003640static int raid_message(struct dm_target *ti, unsigned int argc, char **argv)
Jonathan Brassowbe836512013-04-24 11:42:43 +10003641{
3642 struct raid_set *rs = ti->private;
3643 struct mddev *mddev = &rs->md;
3644
Jonathan Brassowbe836512013-04-24 11:42:43 +10003645 if (!mddev->pers || !mddev->pers->sync_request)
3646 return -EINVAL;
3647
3648 if (!strcasecmp(argv[0], "frozen"))
3649 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3650 else
3651 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3652
3653 if (!strcasecmp(argv[0], "idle") || !strcasecmp(argv[0], "frozen")) {
3654 if (mddev->sync_thread) {
3655 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3656 md_reap_sync_thread(mddev);
3657 }
3658 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3659 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
3660 return -EBUSY;
3661 else if (!strcasecmp(argv[0], "resync"))
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003662 ; /* MD_RECOVERY_NEEDED set below */
3663 else if (!strcasecmp(argv[0], "recover"))
Jonathan Brassowbe836512013-04-24 11:42:43 +10003664 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003665 else {
Mike Snitzerad470472017-02-28 11:55:16 -05003666 if (!strcasecmp(argv[0], "check")) {
Jonathan Brassowbe836512013-04-24 11:42:43 +10003667 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Mike Snitzerad470472017-02-28 11:55:16 -05003668 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3669 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3670 } else if (!strcasecmp(argv[0], "repair")) {
Mike Snitzer105db592017-01-06 15:38:08 -05003671 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3672 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3673 } else
Jonathan Brassowbe836512013-04-24 11:42:43 +10003674 return -EINVAL;
Jonathan Brassowbe836512013-04-24 11:42:43 +10003675 }
3676 if (mddev->ro == 2) {
3677 /* A write to sync_action is enough to justify
3678 * canceling read-auto mode
3679 */
3680 mddev->ro = 0;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003681 if (!mddev->suspended && mddev->sync_thread)
Jonathan Brassowbe836512013-04-24 11:42:43 +10003682 md_wakeup_thread(mddev->sync_thread);
3683 }
3684 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003685 if (!mddev->suspended && mddev->thread)
Jonathan Brassowbe836512013-04-24 11:42:43 +10003686 md_wakeup_thread(mddev->thread);
3687
3688 return 0;
3689}
3690
3691static int raid_iterate_devices(struct dm_target *ti,
3692 iterate_devices_callout_fn fn, void *data)
NeilBrown9d09e662011-01-13 20:00:02 +00003693{
3694 struct raid_set *rs = ti->private;
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02003695 unsigned int i;
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003696 int r = 0;
NeilBrown9d09e662011-01-13 20:00:02 +00003697
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003698 for (i = 0; !r && i < rs->md.raid_disks; i++)
NeilBrown9d09e662011-01-13 20:00:02 +00003699 if (rs->dev[i].data_dev)
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003700 r = fn(ti,
NeilBrown9d09e662011-01-13 20:00:02 +00003701 rs->dev[i].data_dev,
3702 0, /* No offset on data devs */
3703 rs->md.dev_sectors,
3704 data);
3705
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003706 return r;
NeilBrown9d09e662011-01-13 20:00:02 +00003707}
3708
3709static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits)
3710{
3711 struct raid_set *rs = ti->private;
Heinz Mauelshagen89d3d9a2016-07-19 13:16:24 +02003712 unsigned int chunk_size = to_bytes(rs->md.chunk_sectors);
NeilBrown9d09e662011-01-13 20:00:02 +00003713
3714 blk_limits_io_min(limits, chunk_size);
Heinz Mauelshagen89d3d9a2016-07-19 13:16:24 +02003715 blk_limits_io_opt(limits, chunk_size * mddev_data_stripes(rs));
NeilBrown9d09e662011-01-13 20:00:02 +00003716}
3717
NeilBrown9d09e662011-01-13 20:00:02 +00003718static void raid_postsuspend(struct dm_target *ti)
3719{
3720 struct raid_set *rs = ti->private;
3721
NeilBrown4d5324f2017-10-19 12:17:16 +11003722 if (!test_and_set_bit(RT_FLAG_RS_SUSPENDED, &rs->runtime_flags)) {
Heinz Mauelshagen1af20482017-12-02 01:03:48 +01003723 /* Writes have to be stopped before suspending to avoid deadlocks. */
3724 if (!test_bit(MD_RECOVERY_FROZEN, &rs->md.recovery))
3725 md_stop_writes(&rs->md);
3726
NeilBrown4d5324f2017-10-19 12:17:16 +11003727 mddev_lock_nointr(&rs->md);
Heinz Mauelshagen31e10a42016-08-10 02:45:59 +02003728 mddev_suspend(&rs->md);
NeilBrown4d5324f2017-10-19 12:17:16 +11003729 mddev_unlock(&rs->md);
3730 }
NeilBrown9d09e662011-01-13 20:00:02 +00003731}
3732
Jonathan Brassowf381e712013-05-08 17:57:13 -05003733static void attempt_restore_of_faulty_devices(struct raid_set *rs)
NeilBrown9d09e662011-01-13 20:00:02 +00003734{
Jonathan Brassow9092c022013-05-02 14:19:24 -05003735 int i;
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003736 uint64_t cleared_failed_devices[DISKS_ARRAY_ELEMS];
Jonathan Brassow9092c022013-05-02 14:19:24 -05003737 unsigned long flags;
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003738 bool cleared = false;
Jonathan Brassow9092c022013-05-02 14:19:24 -05003739 struct dm_raid_superblock *sb;
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003740 struct mddev *mddev = &rs->md;
Jonathan Brassow9092c022013-05-02 14:19:24 -05003741 struct md_rdev *r;
NeilBrown9d09e662011-01-13 20:00:02 +00003742
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003743 /* RAID personalities have to provide hot add/remove methods or we need to bail out. */
3744 if (!mddev->pers || !mddev->pers->hot_add_disk || !mddev->pers->hot_remove_disk)
3745 return;
3746
3747 memset(cleared_failed_devices, 0, sizeof(cleared_failed_devices));
3748
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01003749 for (i = 0; i < mddev->raid_disks; i++) {
Jonathan Brassowf381e712013-05-08 17:57:13 -05003750 r = &rs->dev[i].rdev;
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003751 /* HM FIXME: enhance journal device recovery processing */
3752 if (test_bit(Journal, &r->flags))
3753 continue;
3754
Heinz Mauelshagene2568462017-01-14 03:53:10 +01003755 if (test_bit(Faulty, &r->flags) &&
3756 r->meta_bdev && !read_disk_sb(r, r->sb_size, true)) {
Jonathan Brassowf381e712013-05-08 17:57:13 -05003757 DMINFO("Faulty %s device #%d has readable super block."
3758 " Attempting to revive it.",
3759 rs->raid_type->name, i);
Jonathan Brassowa4dc1632013-05-08 18:00:54 -05003760
3761 /*
3762 * Faulty bit may be set, but sometimes the array can
3763 * be suspended before the personalities can respond
3764 * by removing the device from the array (i.e. calling
Mike Snitzer43157842016-05-30 13:03:37 -04003765 * 'hot_remove_disk'). If they haven't yet removed
Jonathan Brassowa4dc1632013-05-08 18:00:54 -05003766 * the failed device, its 'raid_disk' number will be
3767 * '>= 0' - meaning we must call this function
3768 * ourselves.
3769 */
Jonathan Brassowf381e712013-05-08 17:57:13 -05003770 flags = r->flags;
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01003771 clear_bit(In_sync, &r->flags); /* Mandatory for hot remove. */
3772 if (r->raid_disk >= 0) {
3773 if (mddev->pers->hot_remove_disk(mddev, r)) {
3774 /* Failed to revive this device, try next */
3775 r->flags = flags;
3776 continue;
3777 }
3778 } else
3779 r->raid_disk = r->saved_raid_disk = i;
3780
Jonathan Brassowf381e712013-05-08 17:57:13 -05003781 clear_bit(Faulty, &r->flags);
3782 clear_bit(WriteErrorSeen, &r->flags);
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01003783
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003784 if (mddev->pers->hot_add_disk(mddev, r)) {
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01003785 /* Failed to revive this device, try next */
3786 r->raid_disk = r->saved_raid_disk = -1;
Jonathan Brassowf381e712013-05-08 17:57:13 -05003787 r->flags = flags;
3788 } else {
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01003789 clear_bit(In_sync, &r->flags);
Jonathan Brassowf381e712013-05-08 17:57:13 -05003790 r->recovery_offset = 0;
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003791 set_bit(i, (void *) cleared_failed_devices);
3792 cleared = true;
Jonathan Brassowf381e712013-05-08 17:57:13 -05003793 }
3794 }
3795 }
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003796
3797 /* If any failed devices could be cleared, update all sbs failed_devices bits */
3798 if (cleared) {
3799 uint64_t failed_devices[DISKS_ARRAY_ELEMS];
3800
Jonathan Brassowf381e712013-05-08 17:57:13 -05003801 rdev_for_each(r, &rs->md) {
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003802 if (test_bit(Journal, &r->flags))
3803 continue;
3804
Jonathan Brassowf381e712013-05-08 17:57:13 -05003805 sb = page_address(r->sb_page);
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003806 sb_retrieve_failed_devices(sb, failed_devices);
3807
3808 for (i = 0; i < DISKS_ARRAY_ELEMS; i++)
3809 failed_devices[i] &= ~cleared_failed_devices[i];
3810
3811 sb_update_failed_devices(sb, failed_devices);
Jonathan Brassowf381e712013-05-08 17:57:13 -05003812 }
3813 }
3814}
3815
Mike Snitzere6ca5e12016-06-02 15:27:22 -04003816static int __load_dirty_region_bitmap(struct raid_set *rs)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003817{
3818 int r = 0;
3819
3820 /* Try loading the bitmap unless "raid0", which does not have one */
3821 if (!rs_is_raid0(rs) &&
Mike Snitzer42863252016-06-02 12:27:46 -04003822 !test_and_set_bit(RT_FLAG_RS_BITMAP_LOADED, &rs->runtime_flags)) {
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003823 r = bitmap_load(&rs->md);
3824 if (r)
3825 DMERR("Failed to load bitmap");
3826 }
3827
3828 return r;
3829}
3830
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04003831/* Enforce updating all superblocks */
3832static void rs_update_sbs(struct raid_set *rs)
3833{
3834 struct mddev *mddev = &rs->md;
3835 int ro = mddev->ro;
3836
Shaohua Li29530792016-12-08 15:48:19 -08003837 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04003838 mddev->ro = 0;
3839 md_update_sb(mddev, 1);
3840 mddev->ro = ro;
3841}
3842
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003843/*
3844 * Reshape changes raid algorithm of @rs to new one within personality
3845 * (e.g. raid6_zr -> raid6_nc), changes stripe size, adds/removes
3846 * disks from a raid set thus growing/shrinking it or resizes the set
3847 *
3848 * Call mddev_lock_nointr() before!
3849 */
3850static int rs_start_reshape(struct raid_set *rs)
3851{
3852 int r;
3853 struct mddev *mddev = &rs->md;
3854 struct md_personality *pers = mddev->pers;
3855
3856 r = rs_setup_reshape(rs);
3857 if (r)
3858 return r;
3859
3860 /* Need to be resumed to be able to start reshape, recovery is frozen until raid_resume() though */
Heinz Mauelshagen0cf352e2017-07-13 17:34:24 +02003861 if (test_and_clear_bit(RT_FLAG_RS_SUSPENDED, &rs->runtime_flags))
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003862 mddev_resume(mddev);
3863
3864 /*
3865 * Check any reshape constraints enforced by the personalility
3866 *
3867 * May as well already kick the reshape off so that * pers->start_reshape() becomes optional.
3868 */
3869 r = pers->check_reshape(mddev);
3870 if (r) {
3871 rs->ti->error = "pers->check_reshape() failed";
3872 return r;
3873 }
3874
3875 /*
3876 * Personality may not provide start reshape method in which
3877 * case check_reshape above has already covered everything
3878 */
3879 if (pers->start_reshape) {
3880 r = pers->start_reshape(mddev);
3881 if (r) {
3882 rs->ti->error = "pers->start_reshape() failed";
3883 return r;
3884 }
3885 }
3886
3887 /* Suspend because a resume will happen in raid_resume() */
Heinz Mauelshagen0cf352e2017-07-13 17:34:24 +02003888 set_bit(RT_FLAG_RS_SUSPENDED, &rs->runtime_flags);
3889 mddev_suspend(mddev);
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003890
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04003891 /*
3892 * Now reshape got set up, update superblocks to
3893 * reflect the fact so that a table reload will
3894 * access proper superblock content in the ctr.
3895 */
3896 rs_update_sbs(rs);
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003897
3898 return 0;
3899}
3900
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003901static int raid_preresume(struct dm_target *ti)
3902{
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003903 int r;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003904 struct raid_set *rs = ti->private;
3905 struct mddev *mddev = &rs->md;
3906
3907 /* This is a resume after a suspend of the set -> it's already started */
Mike Snitzer42863252016-06-02 12:27:46 -04003908 if (test_and_set_bit(RT_FLAG_RS_PRERESUMED, &rs->runtime_flags))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003909 return 0;
3910
3911 /*
3912 * The superblocks need to be updated on disk if the
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04003913 * array is new or new devices got added (thus zeroed
3914 * out by userspace) or __load_dirty_region_bitmap
3915 * will overwrite them in core with old data or fail.
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003916 */
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04003917 if (test_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags))
3918 rs_update_sbs(rs);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003919
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003920 /* Load the bitmap from disk unless raid0 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003921 r = __load_dirty_region_bitmap(rs);
3922 if (r)
3923 return r;
3924
Heinz Mauelshagen4257e082016-06-14 01:46:03 +02003925 /* Resize bitmap to adjust to changed region size (aka MD bitmap chunksize) */
Dmitry Bilunov7a0c5c52017-03-30 18:14:26 +03003926 if (test_bit(RT_FLAG_RS_BITMAP_LOADED, &rs->runtime_flags) && mddev->bitmap &&
Heinz Mauelshagen4257e082016-06-14 01:46:03 +02003927 mddev->bitmap_info.chunksize != to_bytes(rs->requested_bitmap_chunk_sectors)) {
3928 r = bitmap_resize(mddev->bitmap, mddev->dev_sectors,
3929 to_bytes(rs->requested_bitmap_chunk_sectors), 0);
3930 if (r)
3931 DMERR("Failed to resize bitmap");
3932 }
3933
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003934 /* Check for any resize/reshape on @rs and adjust/initiate */
3935 /* Be prepared for mddev_resume() in raid_resume() */
3936 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3937 if (mddev->recovery_cp && mddev->recovery_cp < MaxSector) {
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003938 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3939 mddev->resync_min = mddev->recovery_cp;
3940 }
3941
Heinz Mauelshagen345a6cd2016-06-25 02:42:54 +02003942 /* Check for any reshape request unless new raid set */
Heinz Mauelshagend39f0012017-12-02 01:03:52 +01003943 if (test_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags)) {
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003944 /* Initiate a reshape. */
Heinz Mauelshagen61e06e22017-12-02 01:03:51 +01003945 rs_set_rdev_sectors(rs);
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003946 mddev_lock_nointr(mddev);
3947 r = rs_start_reshape(rs);
3948 mddev_unlock(mddev);
3949 if (r)
3950 DMWARN("Failed to check/start reshape, continuing without change");
3951 r = 0;
3952 }
3953
3954 return r;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003955}
3956
Jonathan Brassowf381e712013-05-08 17:57:13 -05003957static void raid_resume(struct dm_target *ti)
3958{
3959 struct raid_set *rs = ti->private;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003960 struct mddev *mddev = &rs->md;
Jonathan Brassowf381e712013-05-08 17:57:13 -05003961
Mike Snitzer42863252016-06-02 12:27:46 -04003962 if (test_and_set_bit(RT_FLAG_RS_RESUMED, &rs->runtime_flags)) {
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003963 /*
3964 * A secondary resume while the device is active.
3965 * Take this opportunity to check whether any failed
3966 * devices are reachable again.
3967 */
3968 attempt_restore_of_faulty_devices(rs);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003969 }
Heinz Mauelshagen31e10a42016-08-10 02:45:59 +02003970
Heinz Mauelshagen61e06e22017-12-02 01:03:51 +01003971 /* Only reduce raid set size before running a disk removing reshape. */
3972 if (mddev->delta_disks < 0)
3973 rs_set_capacity(rs);
3974
Heinz Mauelshagend36a1952017-02-28 19:17:49 +01003975 /*
3976 * Keep the RAID set frozen if reshape/rebuild flags are set.
3977 * The RAID set is unfrozen once the next table load/resume,
3978 * which clears the reshape/rebuild flags, occurs.
3979 * This ensures that the constructor for the inactive table
3980 * retrieves an up-to-date reshape_position.
3981 */
Heinz Mauelshagend39f0012017-12-02 01:03:52 +01003982 if (!test_and_clear_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags) &&
3983 !(rs->ctr_flags & RESUME_STAY_FROZEN_FLAGS)) {
3984 if (rs_is_reshapable(rs)) {
3985 if (!rs_is_reshaping(rs) || _get_reshape_sectors(rs))
3986 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3987 } else
3988 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3989 }
Heinz Mauelshagen31e10a42016-08-10 02:45:59 +02003990
NeilBrown4d5324f2017-10-19 12:17:16 +11003991 if (test_and_clear_bit(RT_FLAG_RS_SUSPENDED, &rs->runtime_flags)) {
3992 mddev_lock_nointr(mddev);
Heinz Mauelshagen78a75d12017-12-02 01:03:56 +01003993 mddev->ro = 0;
3994 mddev->in_sync = 0;
Heinz Mauelshagen31e10a42016-08-10 02:45:59 +02003995 mddev_resume(mddev);
NeilBrown4d5324f2017-10-19 12:17:16 +11003996 mddev_unlock(mddev);
3997 }
NeilBrown9d09e662011-01-13 20:00:02 +00003998}
3999
4000static struct target_type raid_target = {
4001 .name = "raid",
Mike Snitzerb84cf262017-12-04 10:26:21 -05004002 .version = {1, 13, 1},
NeilBrown9d09e662011-01-13 20:00:02 +00004003 .module = THIS_MODULE,
4004 .ctr = raid_ctr,
4005 .dtr = raid_dtr,
4006 .map = raid_map,
4007 .status = raid_status,
Jonathan Brassowbe836512013-04-24 11:42:43 +10004008 .message = raid_message,
NeilBrown9d09e662011-01-13 20:00:02 +00004009 .iterate_devices = raid_iterate_devices,
4010 .io_hints = raid_io_hints,
NeilBrown9d09e662011-01-13 20:00:02 +00004011 .postsuspend = raid_postsuspend,
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02004012 .preresume = raid_preresume,
NeilBrown9d09e662011-01-13 20:00:02 +00004013 .resume = raid_resume,
4014};
4015
4016static int __init dm_raid_init(void)
4017{
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11004018 DMINFO("Loading target version %u.%u.%u",
4019 raid_target.version[0],
4020 raid_target.version[1],
4021 raid_target.version[2]);
NeilBrown9d09e662011-01-13 20:00:02 +00004022 return dm_register_target(&raid_target);
4023}
4024
4025static void __exit dm_raid_exit(void)
4026{
4027 dm_unregister_target(&raid_target);
4028}
4029
4030module_init(dm_raid_init);
4031module_exit(dm_raid_exit);
4032
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02004033module_param(devices_handle_discard_safely, bool, 0644);
4034MODULE_PARM_DESC(devices_handle_discard_safely,
4035 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
4036
Mike Snitzeref9b85a2016-06-02 12:02:19 -04004037MODULE_DESCRIPTION(DM_NAME " raid0/1/10/4/5/6 target");
4038MODULE_ALIAS("dm-raid0");
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05004039MODULE_ALIAS("dm-raid1");
4040MODULE_ALIAS("dm-raid10");
NeilBrown9d09e662011-01-13 20:00:02 +00004041MODULE_ALIAS("dm-raid4");
4042MODULE_ALIAS("dm-raid5");
4043MODULE_ALIAS("dm-raid6");
4044MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>");
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02004045MODULE_AUTHOR("Heinz Mauelshagen <dm-devel@redhat.com>");
NeilBrown9d09e662011-01-13 20:00:02 +00004046MODULE_LICENSE("GPL");