blob: 14835ae064c109faaecb9c14d0f3d487b7c94420 [file] [log] [blame]
NeilBrown9d09e662011-01-13 20:00:02 +00001/*
2 * Copyright (C) 2010-2011 Neil Brown
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02003 * Copyright (C) 2010-2016 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"
NeilBrown9d09e662011-01-13 20:00:02 +000015#include "bitmap.h"
16
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 Mauelshagen48cf06b2014-09-24 17:47:19 +020027static bool devices_handle_discard_safely = false;
28
NeilBrown9d09e662011-01-13 20:00:02 +000029/*
Jonathan Brassowb12d4372011-08-02 12:32:07 +010030 * The following flags are used by dm-raid.c to set up the array state.
31 * They must be cleared before md_run is called.
NeilBrown9d09e662011-01-13 20:00:02 +000032 */
Mike Snitzer43157842016-05-30 13:03:37 -040033#define FirstUse 10 /* rdev flag */
NeilBrown9d09e662011-01-13 20:00:02 +000034
35struct raid_dev {
36 /*
37 * Two DM devices, one to hold metadata and one to hold the
Mike Snitzer43157842016-05-30 13:03:37 -040038 * actual data/parity. The reason for this is to not confuse
NeilBrown9d09e662011-01-13 20:00:02 +000039 * ti->len and give more flexibility in altering size and
40 * characteristics.
41 *
42 * While it is possible for this device to be associated
43 * with a different physical device than the data_dev, it
44 * is intended for it to be the same.
45 * |--------- Physical Device ---------|
46 * |- meta_dev -|------ data_dev ------|
47 */
48 struct dm_dev *meta_dev;
49 struct dm_dev *data_dev;
NeilBrown3cb03002011-10-11 16:45:26 +110050 struct md_rdev rdev;
NeilBrown9d09e662011-01-13 20:00:02 +000051};
52
53/*
Mike Snitzer42863252016-06-02 12:27:46 -040054 * Bits for establishing rs->ctr_flags
Heinz Mauelshagen702108d2016-05-19 18:49:26 +020055 *
56 * 1 = no flag value
57 * 2 = flag with value
NeilBrown9d09e662011-01-13 20:00:02 +000058 */
Mike Snitzer42863252016-06-02 12:27:46 -040059#define __CTR_FLAG_SYNC 0 /* 1 */ /* Not with raid0! */
60#define __CTR_FLAG_NOSYNC 1 /* 1 */ /* Not with raid0! */
61#define __CTR_FLAG_REBUILD 2 /* 2 */ /* Not with raid0! */
62#define __CTR_FLAG_DAEMON_SLEEP 3 /* 2 */ /* Not with raid0! */
63#define __CTR_FLAG_MIN_RECOVERY_RATE 4 /* 2 */ /* Not with raid0! */
64#define __CTR_FLAG_MAX_RECOVERY_RATE 5 /* 2 */ /* Not with raid0! */
65#define __CTR_FLAG_MAX_WRITE_BEHIND 6 /* 2 */ /* Only with raid1! */
66#define __CTR_FLAG_WRITE_MOSTLY 7 /* 2 */ /* Only with raid1! */
67#define __CTR_FLAG_STRIPE_CACHE 8 /* 2 */ /* Only with raid4/5/6! */
68#define __CTR_FLAG_REGION_SIZE 9 /* 2 */ /* Not with raid0! */
69#define __CTR_FLAG_RAID10_COPIES 10 /* 2 */ /* Only with raid10 */
70#define __CTR_FLAG_RAID10_FORMAT 11 /* 2 */ /* Only with raid10 */
Mike Snitzer9b6e5422016-06-02 11:48:09 -040071/* New for v1.9.0 */
Mike Snitzer42863252016-06-02 12:27:46 -040072#define __CTR_FLAG_DELTA_DISKS 12 /* 2 */ /* Only with reshapable raid4/5/6/10! */
73#define __CTR_FLAG_DATA_OFFSET 13 /* 2 */ /* Only with reshapable raid4/5/6/10! */
74#define __CTR_FLAG_RAID10_USE_NEAR_SETS 14 /* 2 */ /* Only with raid10! */
75
76/*
77 * Flags for rs->ctr_flags field.
78 */
79#define CTR_FLAG_SYNC (1 << __CTR_FLAG_SYNC)
80#define CTR_FLAG_NOSYNC (1 << __CTR_FLAG_NOSYNC)
81#define CTR_FLAG_REBUILD (1 << __CTR_FLAG_REBUILD)
82#define CTR_FLAG_DAEMON_SLEEP (1 << __CTR_FLAG_DAEMON_SLEEP)
83#define CTR_FLAG_MIN_RECOVERY_RATE (1 << __CTR_FLAG_MIN_RECOVERY_RATE)
84#define CTR_FLAG_MAX_RECOVERY_RATE (1 << __CTR_FLAG_MAX_RECOVERY_RATE)
85#define CTR_FLAG_MAX_WRITE_BEHIND (1 << __CTR_FLAG_MAX_WRITE_BEHIND)
86#define CTR_FLAG_WRITE_MOSTLY (1 << __CTR_FLAG_WRITE_MOSTLY)
87#define CTR_FLAG_STRIPE_CACHE (1 << __CTR_FLAG_STRIPE_CACHE)
88#define CTR_FLAG_REGION_SIZE (1 << __CTR_FLAG_REGION_SIZE)
89#define CTR_FLAG_RAID10_COPIES (1 << __CTR_FLAG_RAID10_COPIES)
90#define CTR_FLAG_RAID10_FORMAT (1 << __CTR_FLAG_RAID10_FORMAT)
91#define CTR_FLAG_DELTA_DISKS (1 << __CTR_FLAG_DELTA_DISKS)
92#define CTR_FLAG_DATA_OFFSET (1 << __CTR_FLAG_DATA_OFFSET)
93#define CTR_FLAG_RAID10_USE_NEAR_SETS (1 << __CTR_FLAG_RAID10_USE_NEAR_SETS)
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -050094
Heinz Mauelshagenf0902792016-05-19 18:49:27 +020095/*
96 * Definitions of various constructor flags to
97 * be used in checks of valid / invalid flags
98 * per raid level.
99 */
100/* Define all any sync flags */
101#define CTR_FLAGS_ANY_SYNC (CTR_FLAG_SYNC | CTR_FLAG_NOSYNC)
102
103/* Define flags for options without argument (e.g. 'nosync') */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200104#define CTR_FLAG_OPTIONS_NO_ARGS (CTR_FLAGS_ANY_SYNC | \
105 CTR_FLAG_RAID10_USE_NEAR_SETS)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200106
107/* Define flags for options with one argument (e.g. 'delta_disks +2') */
108#define CTR_FLAG_OPTIONS_ONE_ARG (CTR_FLAG_REBUILD | \
109 CTR_FLAG_WRITE_MOSTLY | \
110 CTR_FLAG_DAEMON_SLEEP | \
111 CTR_FLAG_MIN_RECOVERY_RATE | \
112 CTR_FLAG_MAX_RECOVERY_RATE | \
113 CTR_FLAG_MAX_WRITE_BEHIND | \
114 CTR_FLAG_STRIPE_CACHE | \
115 CTR_FLAG_REGION_SIZE | \
116 CTR_FLAG_RAID10_COPIES | \
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200117 CTR_FLAG_RAID10_FORMAT | \
118 CTR_FLAG_DELTA_DISKS | \
119 CTR_FLAG_DATA_OFFSET)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200120
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200121/* Valid options definitions per raid level... */
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200122
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200123/* "raid0" does only accept data offset */
124#define RAID0_VALID_FLAGS (CTR_FLAG_DATA_OFFSET)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200125
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200126/* "raid1" does not accept stripe cache, data offset, delta_disks or any raid10 options */
127#define RAID1_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \
128 CTR_FLAG_REBUILD | \
129 CTR_FLAG_WRITE_MOSTLY | \
130 CTR_FLAG_DAEMON_SLEEP | \
131 CTR_FLAG_MIN_RECOVERY_RATE | \
132 CTR_FLAG_MAX_RECOVERY_RATE | \
133 CTR_FLAG_MAX_WRITE_BEHIND | \
134 CTR_FLAG_REGION_SIZE | \
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200135 CTR_FLAG_DATA_OFFSET)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200136
137/* "raid10" does not accept any raid1 or stripe cache options */
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200138#define RAID10_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \
139 CTR_FLAG_REBUILD | \
140 CTR_FLAG_DAEMON_SLEEP | \
141 CTR_FLAG_MIN_RECOVERY_RATE | \
142 CTR_FLAG_MAX_RECOVERY_RATE | \
143 CTR_FLAG_REGION_SIZE | \
144 CTR_FLAG_RAID10_COPIES | \
145 CTR_FLAG_RAID10_FORMAT | \
146 CTR_FLAG_DELTA_DISKS | \
147 CTR_FLAG_DATA_OFFSET | \
148 CTR_FLAG_RAID10_USE_NEAR_SETS)
149
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200150/*
151 * "raid4/5/6" do not accept any raid1 or raid10 specific options
152 *
153 * "raid6" does not accept "nosync", because it is not guaranteed
154 * that both parity and q-syndrome are being written properly with
155 * any writes
156 */
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200157#define RAID45_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \
158 CTR_FLAG_REBUILD | \
159 CTR_FLAG_DAEMON_SLEEP | \
160 CTR_FLAG_MIN_RECOVERY_RATE | \
161 CTR_FLAG_MAX_RECOVERY_RATE | \
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200162 CTR_FLAG_MAX_WRITE_BEHIND | \
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200163 CTR_FLAG_STRIPE_CACHE | \
164 CTR_FLAG_REGION_SIZE | \
165 CTR_FLAG_DELTA_DISKS | \
166 CTR_FLAG_DATA_OFFSET)
167
168#define RAID6_VALID_FLAGS (CTR_FLAG_SYNC | \
169 CTR_FLAG_REBUILD | \
170 CTR_FLAG_DAEMON_SLEEP | \
171 CTR_FLAG_MIN_RECOVERY_RATE | \
172 CTR_FLAG_MAX_RECOVERY_RATE | \
173 CTR_FLAG_MAX_WRITE_BEHIND | \
174 CTR_FLAG_STRIPE_CACHE | \
175 CTR_FLAG_REGION_SIZE | \
176 CTR_FLAG_DELTA_DISKS | \
177 CTR_FLAG_DATA_OFFSET)
178/* ...valid options definitions per raid level */
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200179
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200180/*
181 * Flags for rs->runtime_flags field
182 * (RT_FLAG prefix meaning "runtime flag")
183 *
184 * These are all internal and used to define runtime state,
185 * e.g. to prevent another resume from preresume processing
186 * the raid set all over again.
187 */
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +0200188#define RT_FLAG_RS_PRERESUMED 0
189#define RT_FLAG_RS_RESUMED 1
190#define RT_FLAG_RS_BITMAP_LOADED 2
191#define RT_FLAG_UPDATE_SBS 3
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200192
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200193/* Array elements of 64 bit needed for rebuild/write_mostly bits */
194#define DISKS_ARRAY_ELEMS ((MAX_RAID_DEVICES + (sizeof(uint64_t) * 8 - 1)) / sizeof(uint64_t) / 8)
195
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200196/*
197 * raid set level, layout and chunk sectors backup/restore
198 */
199struct rs_layout {
200 int new_level;
201 int new_layout;
202 int new_chunk_sectors;
203};
204
NeilBrown9d09e662011-01-13 20:00:02 +0000205struct raid_set {
206 struct dm_target *ti;
207
Jonathan Brassow34f8ac6d2012-01-27 14:53:53 -0600208 uint32_t bitmap_loaded;
Mike Snitzer42863252016-06-02 12:27:46 -0400209 unsigned long ctr_flags;
210 unsigned long runtime_flags;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200211
212 uint64_t rebuild_disks[DISKS_ARRAY_ELEMS];
NeilBrown9d09e662011-01-13 20:00:02 +0000213
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200214 int raid_disks;
215 int delta_disks;
Heinz Mauelshagen4763e542016-05-19 18:49:31 +0200216 int data_offset;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200217 int raid10_copies;
218
NeilBrownfd01b882011-10-11 16:47:53 +1100219 struct mddev md;
NeilBrown9d09e662011-01-13 20:00:02 +0000220 struct raid_type *raid_type;
221 struct dm_target_callbacks callbacks;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200222 struct rs_layout rs_layout;
NeilBrown9d09e662011-01-13 20:00:02 +0000223
224 struct raid_dev dev[0];
225};
226
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400227static void rs_config_backup(struct raid_set *rs)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200228{
229 struct mddev *mddev = &rs->md;
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400230 struct rs_layout *l = &rs->rs_layout;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200231
232 l->new_level = mddev->new_level;
233 l->new_layout = mddev->new_layout;
234 l->new_chunk_sectors = mddev->new_chunk_sectors;
235}
236
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400237static void rs_config_restore(struct raid_set *rs)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200238{
239 struct mddev *mddev = &rs->md;
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400240 struct rs_layout *l = &rs->rs_layout;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200241
242 mddev->new_level = l->new_level;
243 mddev->new_layout = l->new_layout;
244 mddev->new_chunk_sectors = l->new_chunk_sectors;
245}
246
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200247/* raid10 algorithms (i.e. formats) */
248#define ALGORITHM_RAID10_DEFAULT 0
249#define ALGORITHM_RAID10_NEAR 1
250#define ALGORITHM_RAID10_OFFSET 2
251#define ALGORITHM_RAID10_FAR 3
252
NeilBrown9d09e662011-01-13 20:00:02 +0000253/* Supported raid types and properties. */
254static struct raid_type {
255 const char *name; /* RAID algorithm. */
256 const char *descr; /* Descriptor text for logging. */
257 const unsigned parity_devs; /* # of parity devices. */
258 const unsigned minimal_devs; /* minimal # of devices in set. */
259 const unsigned level; /* RAID level. */
260 const unsigned algorithm; /* RAID algorithm. */
261} raid_types[] = {
Mike Snitzer43157842016-05-30 13:03:37 -0400262 {"raid0", "raid0 (striping)", 0, 2, 0, 0 /* NONE */},
263 {"raid1", "raid1 (mirroring)", 0, 2, 1, 0 /* NONE */},
264 {"raid10_far", "raid10 far (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_FAR},
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200265 {"raid10_offset", "raid10 offset (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_OFFSET},
Mike Snitzer43157842016-05-30 13:03:37 -0400266 {"raid10_near", "raid10 near (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_NEAR},
267 {"raid10", "raid10 (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_DEFAULT},
268 {"raid4", "raid4 (dedicated last parity disk)", 1, 2, 4, ALGORITHM_PARITY_N}, /* raid4 layout = raid5_n */
269 {"raid5_n", "raid5 (dedicated last parity disk)", 1, 2, 5, ALGORITHM_PARITY_N},
270 {"raid5_ls", "raid5 (left symmetric)", 1, 2, 5, ALGORITHM_LEFT_SYMMETRIC},
271 {"raid5_rs", "raid5 (right symmetric)", 1, 2, 5, ALGORITHM_RIGHT_SYMMETRIC},
272 {"raid5_la", "raid5 (left asymmetric)", 1, 2, 5, ALGORITHM_LEFT_ASYMMETRIC},
273 {"raid5_ra", "raid5 (right asymmetric)", 1, 2, 5, ALGORITHM_RIGHT_ASYMMETRIC},
274 {"raid6_zr", "raid6 (zero restart)", 2, 4, 6, ALGORITHM_ROTATING_ZERO_RESTART},
275 {"raid6_nr", "raid6 (N restart)", 2, 4, 6, ALGORITHM_ROTATING_N_RESTART},
276 {"raid6_nc", "raid6 (N continue)", 2, 4, 6, ALGORITHM_ROTATING_N_CONTINUE},
277 {"raid6_n_6", "raid6 (dedicated parity/Q n/6)", 2, 4, 6, ALGORITHM_PARITY_N_6},
278 {"raid6_ls_6", "raid6 (left symmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_LEFT_SYMMETRIC_6},
279 {"raid6_rs_6", "raid6 (right symmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_RIGHT_SYMMETRIC_6},
280 {"raid6_la_6", "raid6 (left asymmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_LEFT_ASYMMETRIC_6},
281 {"raid6_ra_6", "raid6 (right asymmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_RIGHT_ASYMMETRIC_6}
NeilBrown9d09e662011-01-13 20:00:02 +0000282};
283
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200284/* True, if @v is in inclusive range [@min, @max] */
Mike Snitzerbb91a632016-06-02 12:06:54 -0400285static bool __within_range(long v, long min, long max)
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200286{
287 return v >= min && v <= max;
288}
289
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200290/* All table line arguments are defined here */
291static struct arg_name_flag {
Mike Snitzer42863252016-06-02 12:27:46 -0400292 const unsigned long flag;
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200293 const char *name;
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400294} __arg_name_flags[] = {
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200295 { CTR_FLAG_SYNC, "sync"},
296 { CTR_FLAG_NOSYNC, "nosync"},
297 { CTR_FLAG_REBUILD, "rebuild"},
298 { CTR_FLAG_DAEMON_SLEEP, "daemon_sleep"},
299 { CTR_FLAG_MIN_RECOVERY_RATE, "min_recovery_rate"},
300 { CTR_FLAG_MAX_RECOVERY_RATE, "max_recovery_rate"},
301 { CTR_FLAG_MAX_WRITE_BEHIND, "max_write_behind"},
302 { CTR_FLAG_WRITE_MOSTLY, "writemostly"},
303 { CTR_FLAG_STRIPE_CACHE, "stripe_cache"},
304 { CTR_FLAG_REGION_SIZE, "region_size"},
305 { CTR_FLAG_RAID10_COPIES, "raid10_copies"},
306 { CTR_FLAG_RAID10_FORMAT, "raid10_format"},
Heinz Mauelshagen4763e542016-05-19 18:49:31 +0200307 { CTR_FLAG_DATA_OFFSET, "data_offset"},
308 { CTR_FLAG_DELTA_DISKS, "delta_disks"},
309 { CTR_FLAG_RAID10_USE_NEAR_SETS, "raid10_use_near_sets"},
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200310};
311
312/* Return argument name string for given @flag */
Mike Snitzer3fa6cf32016-06-02 11:58:51 -0400313static const char *dm_raid_arg_name_by_flag(const uint32_t flag)
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200314{
315 if (hweight32(flag) == 1) {
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400316 struct arg_name_flag *anf = __arg_name_flags + ARRAY_SIZE(__arg_name_flags);
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200317
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400318 while (anf-- > __arg_name_flags)
Mike Snitzer42863252016-06-02 12:27:46 -0400319 if (flag & anf->flag)
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200320 return anf->name;
321
322 } else
323 DMERR("%s called with more than one flag!", __func__);
324
325 return NULL;
326}
327
328/*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200329 * bool helpers to test for various raid levels of a raid set,
330 * is. it's level as reported by the superblock rather than
331 * the requested raid_type passed to the constructor.
332 */
333/* Return true, if raid set in @rs is raid0 */
334static bool rs_is_raid0(struct raid_set *rs)
335{
336 return !rs->md.level;
337}
338
339/* Return true, if raid set in @rs is raid10 */
340static bool rs_is_raid10(struct raid_set *rs)
341{
342 return rs->md.level == 10;
343}
344
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +0200345/* Return true, if raid set in @rs is level 4, 5 or 6 */
346static bool rs_is_raid456(struct raid_set *rs)
347{
348 return __within_range(rs->md.level, 4, 6);
349}
350
351/* Return true, if raid set in @rs is reshapable */
352static unsigned int __is_raid10_far(int layout);
353static bool rs_is_reshapable(struct raid_set *rs)
354{
355 return rs_is_raid456(rs) ||
356 (rs_is_raid10(rs) && !__is_raid10_far(rs->md.new_layout));
357}
358
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200359/*
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200360 * bool helpers to test for various raid levels of a raid type
361 */
362
363/* Return true, if raid type in @rt is raid0 */
364static bool rt_is_raid0(struct raid_type *rt)
365{
366 return !rt->level;
367}
368
369/* Return true, if raid type in @rt is raid1 */
370static bool rt_is_raid1(struct raid_type *rt)
371{
372 return rt->level == 1;
373}
374
375/* Return true, if raid type in @rt is raid10 */
376static bool rt_is_raid10(struct raid_type *rt)
377{
378 return rt->level == 10;
379}
380
381/* Return true, if raid type in @rt is raid4/5 */
382static bool rt_is_raid45(struct raid_type *rt)
383{
Mike Snitzerbb91a632016-06-02 12:06:54 -0400384 return __within_range(rt->level, 4, 5);
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200385}
386
387/* Return true, if raid type in @rt is raid6 */
388static bool rt_is_raid6(struct raid_type *rt)
389{
390 return rt->level == 6;
391}
Heinz Mauelshagen676fa5a2016-05-19 18:49:29 +0200392
393/* Return true, if raid type in @rt is raid4/5/6 */
394static bool rt_is_raid456(struct raid_type *rt)
395{
Mike Snitzerbb91a632016-06-02 12:06:54 -0400396 return __within_range(rt->level, 4, 6);
Heinz Mauelshagen676fa5a2016-05-19 18:49:29 +0200397}
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200398/* END: raid level bools */
399
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200400/* Return valid ctr flags for the raid level of @rs */
401static unsigned long __valid_flags(struct raid_set *rs)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200402{
403 if (rt_is_raid0(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200404 return RAID0_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200405 else if (rt_is_raid1(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200406 return RAID1_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200407 else if (rt_is_raid10(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200408 return RAID10_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200409 else if (rt_is_raid45(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200410 return RAID45_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200411 else if (rt_is_raid6(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200412 return RAID6_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200413
414 return ~0;
415}
416
417/*
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200418 * Check for valid flags set on @rs
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200419 *
420 * Has to be called after parsing of the ctr flags!
421 */
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200422static int rs_check_for_valid_flags(struct raid_set *rs)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200423{
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200424 if (rs->ctr_flags & ~__valid_flags(rs)) {
Mike Snitzer42863252016-06-02 12:27:46 -0400425 rs->ti->error = "Invalid flags combination";
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400426 return -EINVAL;
427 }
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200428
429 return 0;
430}
431
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200432/* MD raid10 bit definitions and helpers */
433#define RAID10_OFFSET (1 << 16) /* stripes with data copies area adjacent on devices */
434#define RAID10_BROCKEN_USE_FAR_SETS (1 << 17) /* Broken in raid10.c: use sets instead of whole stripe rotation */
435#define RAID10_USE_FAR_SETS (1 << 18) /* Use sets instead of whole stripe rotation */
436#define RAID10_FAR_COPIES_SHIFT 8 /* raid10 # far copies shift (2nd byte of layout) */
437
438/* Return md raid10 near copies for @layout */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400439static unsigned int __raid10_near_copies(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200440{
441 return layout & 0xFF;
442}
443
444/* Return md raid10 far copies for @layout */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400445static unsigned int __raid10_far_copies(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200446{
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400447 return __raid10_near_copies(layout >> RAID10_FAR_COPIES_SHIFT);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200448}
449
450/* Return true if md raid10 offset for @layout */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400451static unsigned int __is_raid10_offset(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200452{
453 return layout & RAID10_OFFSET;
454}
455
456/* Return true if md raid10 near for @layout */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400457static unsigned int __is_raid10_near(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200458{
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400459 return !__is_raid10_offset(layout) && __raid10_near_copies(layout) > 1;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200460}
461
462/* Return true if md raid10 far for @layout */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400463static unsigned int __is_raid10_far(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200464{
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400465 return !__is_raid10_offset(layout) && __raid10_far_copies(layout) > 1;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200466}
467
468/* Return md raid10 layout string for @layout */
469static const char *raid10_md_layout_to_format(int layout)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100470{
471 /*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200472 * Bit 16 stands for "offset"
473 * (i.e. adjacent stripes hold copies)
474 *
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100475 * Refer to MD's raid10.c for details
476 */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400477 if (__is_raid10_offset(layout))
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100478 return "offset";
479
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400480 if (__raid10_near_copies(layout) > 1)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100481 return "near";
482
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400483 WARN_ON(__raid10_far_copies(layout) < 2);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200484
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100485 return "far";
486}
487
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200488/* Return md raid10 algorithm for @name */
489static const int raid10_name_to_format(const char *name)
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500490{
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200491 if (!strcasecmp(name, "near"))
492 return ALGORITHM_RAID10_NEAR;
493 else if (!strcasecmp(name, "offset"))
494 return ALGORITHM_RAID10_OFFSET;
495 else if (!strcasecmp(name, "far"))
496 return ALGORITHM_RAID10_FAR;
497
498 return -EINVAL;
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500499}
500
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200501/* Return md raid10 copies for @layout */
502static unsigned int raid10_md_layout_to_copies(int layout)
503{
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400504 return __raid10_near_copies(layout) > 1 ?
505 __raid10_near_copies(layout) : __raid10_far_copies(layout);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200506}
507
508/* Return md raid10 format id for @format string */
509static int raid10_format_to_md_layout(struct raid_set *rs,
510 unsigned int algorithm,
511 unsigned int copies)
512{
513 unsigned int n = 1, f = 1, r = 0;
514
515 /*
516 * MD resilienece flaw:
517 *
518 * enabling use_far_sets for far/offset formats causes copies
519 * to be colocated on the same devs together with their origins!
520 *
521 * -> disable it for now in the definition above
522 */
523 if (algorithm == ALGORITHM_RAID10_DEFAULT ||
524 algorithm == ALGORITHM_RAID10_NEAR)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100525 n = copies;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200526
527 else if (algorithm == ALGORITHM_RAID10_OFFSET) {
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100528 f = copies;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200529 r = RAID10_OFFSET;
Mike Snitzer42863252016-06-02 12:27:46 -0400530 if (!test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200531 r |= RAID10_USE_FAR_SETS;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100532
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200533 } else if (algorithm == ALGORITHM_RAID10_FAR) {
534 f = copies;
535 r = !RAID10_OFFSET;
Mike Snitzer42863252016-06-02 12:27:46 -0400536 if (!test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200537 r |= RAID10_USE_FAR_SETS;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100538
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200539 } else
540 return -EINVAL;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100541
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200542 return r | (f << RAID10_FAR_COPIES_SHIFT) | n;
543}
544/* END: MD raid10 bit definitions and helpers */
545
546/* Check for any of the raid10 algorithms */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400547static int __got_raid10(struct raid_type *rtp, const int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200548{
549 if (rtp->level == 10) {
550 switch (rtp->algorithm) {
551 case ALGORITHM_RAID10_DEFAULT:
552 case ALGORITHM_RAID10_NEAR:
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400553 return __is_raid10_near(layout);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200554 case ALGORITHM_RAID10_OFFSET:
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400555 return __is_raid10_offset(layout);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200556 case ALGORITHM_RAID10_FAR:
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400557 return __is_raid10_far(layout);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200558 default:
559 break;
560 }
561 }
562
563 return 0;
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500564}
565
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200566/* Return raid_type for @name */
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200567static struct raid_type *get_raid_type(const char *name)
NeilBrown9d09e662011-01-13 20:00:02 +0000568{
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200569 struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);
NeilBrown9d09e662011-01-13 20:00:02 +0000570
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200571 while (rtp-- > raid_types)
572 if (!strcasecmp(rtp->name, name))
573 return rtp;
NeilBrown9d09e662011-01-13 20:00:02 +0000574
575 return NULL;
576}
577
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200578/* Return raid_type for @name based derived from @level and @layout */
579static struct raid_type *get_raid_type_by_ll(const int level, const int layout)
580{
581 struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);
582
583 while (rtp-- > raid_types) {
584 /* RAID10 special checks based on @layout flags/properties */
585 if (rtp->level == level &&
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400586 (__got_raid10(rtp, layout) || rtp->algorithm == layout))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200587 return rtp;
588 }
589
590 return NULL;
591}
592
593/*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +0200594 * Set the mddev properties in @rs to the current
595 * ones retrieved from the freshest superblock
596 */
597static void rs_set_cur(struct raid_set *rs)
598{
599 struct mddev *mddev = &rs->md;
600
601 mddev->new_level = mddev->level;
602 mddev->new_layout = mddev->layout;
603 mddev->new_chunk_sectors = mddev->chunk_sectors;
604}
605
606/*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200607 * Set the mddev properties in @rs to the new
608 * ones requested by the ctr
609 */
610static void rs_set_new(struct raid_set *rs)
611{
612 struct mddev *mddev = &rs->md;
613
614 mddev->level = mddev->new_level;
615 mddev->layout = mddev->new_layout;
616 mddev->chunk_sectors = mddev->new_chunk_sectors;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +0200617 mddev->raid_disks = rs->raid_disks;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200618 mddev->delta_disks = 0;
619}
620
Mike Snitzerbfcee0e2016-06-02 15:08:09 -0400621static struct raid_set *raid_set_alloc(struct dm_target *ti, struct raid_type *raid_type,
622 unsigned raid_devs)
NeilBrown9d09e662011-01-13 20:00:02 +0000623{
624 unsigned i;
625 struct raid_set *rs;
NeilBrown9d09e662011-01-13 20:00:02 +0000626
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400627 if (raid_devs <= raid_type->parity_devs) {
628 ti->error = "Insufficient number of devices";
629 return ERR_PTR(-EINVAL);
630 }
NeilBrown9d09e662011-01-13 20:00:02 +0000631
NeilBrown9d09e662011-01-13 20:00:02 +0000632 rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400633 if (!rs) {
634 ti->error = "Cannot allocate raid context";
635 return ERR_PTR(-ENOMEM);
636 }
NeilBrown9d09e662011-01-13 20:00:02 +0000637
638 mddev_init(&rs->md);
639
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200640 rs->raid_disks = raid_devs;
641 rs->delta_disks = 0;
642
NeilBrown9d09e662011-01-13 20:00:02 +0000643 rs->ti = ti;
644 rs->raid_type = raid_type;
645 rs->md.raid_disks = raid_devs;
646 rs->md.level = raid_type->level;
647 rs->md.new_level = rs->md.level;
NeilBrown9d09e662011-01-13 20:00:02 +0000648 rs->md.layout = raid_type->algorithm;
649 rs->md.new_layout = rs->md.layout;
650 rs->md.delta_disks = 0;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200651 rs->md.recovery_cp = rs_is_raid0(rs) ? MaxSector : 0;
NeilBrown9d09e662011-01-13 20:00:02 +0000652
653 for (i = 0; i < raid_devs; i++)
654 md_rdev_init(&rs->dev[i].rdev);
655
656 /*
657 * Remaining items to be initialized by further RAID params:
658 * rs->md.persistent
659 * rs->md.external
660 * rs->md.chunk_sectors
661 * rs->md.new_chunk_sectors
Jonathan E Brassowc039c332012-07-27 15:08:04 +0100662 * rs->md.dev_sectors
NeilBrown9d09e662011-01-13 20:00:02 +0000663 */
664
665 return rs;
666}
667
Mike Snitzerbfcee0e2016-06-02 15:08:09 -0400668static void raid_set_free(struct raid_set *rs)
NeilBrown9d09e662011-01-13 20:00:02 +0000669{
670 int i;
671
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100672 for (i = 0; i < rs->md.raid_disks; i++) {
673 if (rs->dev[i].meta_dev)
674 dm_put_device(rs->ti, rs->dev[i].meta_dev);
NeilBrown545c8792012-05-22 13:54:30 +1000675 md_rdev_clear(&rs->dev[i].rdev);
NeilBrown9d09e662011-01-13 20:00:02 +0000676 if (rs->dev[i].data_dev)
677 dm_put_device(rs->ti, rs->dev[i].data_dev);
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100678 }
NeilBrown9d09e662011-01-13 20:00:02 +0000679
680 kfree(rs);
681}
682
683/*
684 * For every device we have two words
685 * <meta_dev>: meta device name or '-' if missing
686 * <data_dev>: data device name or '-' if missing
687 *
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100688 * The following are permitted:
689 * - -
690 * - <data_dev>
691 * <meta_dev> <data_dev>
692 *
693 * The following is not allowed:
694 * <meta_dev> -
695 *
696 * This code parses those words. If there is a failure,
Mike Snitzerbfcee0e2016-06-02 15:08:09 -0400697 * the caller must use raid_set_free() to unwind the operations.
NeilBrown9d09e662011-01-13 20:00:02 +0000698 */
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200699static int parse_dev_params(struct raid_set *rs, struct dm_arg_set *as)
NeilBrown9d09e662011-01-13 20:00:02 +0000700{
701 int i;
702 int rebuild = 0;
703 int metadata_available = 0;
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +0200704 int r = 0;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200705 const char *arg;
NeilBrown9d09e662011-01-13 20:00:02 +0000706
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200707 /* Put off the number of raid devices argument to get to dev pairs */
708 arg = dm_shift_arg(as);
709 if (!arg)
710 return -EINVAL;
711
712 for (i = 0; i < rs->md.raid_disks; i++) {
NeilBrown9d09e662011-01-13 20:00:02 +0000713 rs->dev[i].rdev.raid_disk = i;
714
715 rs->dev[i].meta_dev = NULL;
716 rs->dev[i].data_dev = NULL;
717
718 /*
719 * There are no offsets, since there is a separate device
720 * for data and metadata.
721 */
722 rs->dev[i].rdev.data_offset = 0;
723 rs->dev[i].rdev.mddev = &rs->md;
724
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200725 arg = dm_shift_arg(as);
726 if (!arg)
727 return -EINVAL;
728
729 if (strcmp(arg, "-")) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400730 r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
731 &rs->dev[i].meta_dev);
732 if (r) {
733 rs->ti->error = "RAID metadata device lookup failure";
734 return r;
735 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100736
737 rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400738 if (!rs->dev[i].rdev.sb_page) {
739 rs->ti->error = "Failed to allocate superblock page";
740 return -ENOMEM;
741 }
NeilBrown9d09e662011-01-13 20:00:02 +0000742 }
743
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200744 arg = dm_shift_arg(as);
745 if (!arg)
746 return -EINVAL;
747
748 if (!strcmp(arg, "-")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000749 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) &&
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400750 (!rs->dev[i].rdev.recovery_offset)) {
751 rs->ti->error = "Drive designated for rebuild not specified";
752 return -EINVAL;
753 }
NeilBrown9d09e662011-01-13 20:00:02 +0000754
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400755 if (rs->dev[i].meta_dev) {
756 rs->ti->error = "No data device supplied with metadata device";
757 return -EINVAL;
758 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100759
NeilBrown9d09e662011-01-13 20:00:02 +0000760 continue;
761 }
762
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400763 r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
764 &rs->dev[i].data_dev);
765 if (r) {
766 rs->ti->error = "RAID device lookup failure";
767 return r;
768 }
NeilBrown9d09e662011-01-13 20:00:02 +0000769
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100770 if (rs->dev[i].meta_dev) {
771 metadata_available = 1;
772 rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev;
773 }
NeilBrown9d09e662011-01-13 20:00:02 +0000774 rs->dev[i].rdev.bdev = rs->dev[i].data_dev->bdev;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +0200775 list_add_tail(&rs->dev[i].rdev.same_set, &rs->md.disks);
NeilBrown9d09e662011-01-13 20:00:02 +0000776 if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
777 rebuild++;
778 }
779
780 if (metadata_available) {
781 rs->md.external = 0;
782 rs->md.persistent = 1;
783 rs->md.major_version = 2;
784 } else if (rebuild && !rs->md.recovery_cp) {
785 /*
786 * Without metadata, we will not be able to tell if the array
787 * is in-sync or not - we must assume it is not. Therefore,
788 * it is impossible to rebuild a drive.
789 *
790 * Even if there is metadata, the on-disk information may
791 * indicate that the array is not in-sync and it will then
792 * fail at that time.
793 *
794 * User could specify 'nosync' option if desperate.
795 */
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400796 rs->ti->error = "Unable to rebuild drive while array is not in-sync";
797 return -EINVAL;
NeilBrown9d09e662011-01-13 20:00:02 +0000798 }
799
800 return 0;
801}
802
803/*
Jonathan Brassowc1084562011-08-02 12:32:07 +0100804 * validate_region_size
805 * @rs
806 * @region_size: region size in sectors. If 0, pick a size (4MiB default).
807 *
808 * Set rs->md.bitmap_info.chunksize (which really refers to 'region size').
809 * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap.
810 *
811 * Returns: 0 on success, -EINVAL on failure.
812 */
813static int validate_region_size(struct raid_set *rs, unsigned long region_size)
814{
815 unsigned long min_region_size = rs->ti->len / (1 << 21);
816
817 if (!region_size) {
818 /*
Mike Snitzer43157842016-05-30 13:03:37 -0400819 * Choose a reasonable default. All figures in sectors.
Jonathan Brassowc1084562011-08-02 12:32:07 +0100820 */
821 if (min_region_size > (1 << 13)) {
Jonathan Brassow3a0f9aa2012-12-21 20:23:33 +0000822 /* If not a power of 2, make it the next power of 2 */
Mikulas Patocka042745e2015-10-02 11:17:37 -0400823 region_size = roundup_pow_of_two(min_region_size);
Jonathan Brassowc1084562011-08-02 12:32:07 +0100824 DMINFO("Choosing default region size of %lu sectors",
825 region_size);
Jonathan Brassowc1084562011-08-02 12:32:07 +0100826 } else {
827 DMINFO("Choosing default region size of 4MiB");
828 region_size = 1 << 13; /* sectors */
829 }
830 } else {
831 /*
832 * Validate user-supplied value.
833 */
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400834 if (region_size > rs->ti->len) {
835 rs->ti->error = "Supplied region size is too large";
836 return -EINVAL;
837 }
Jonathan Brassowc1084562011-08-02 12:32:07 +0100838
839 if (region_size < min_region_size) {
840 DMERR("Supplied region_size (%lu sectors) below minimum (%lu)",
841 region_size, min_region_size);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400842 rs->ti->error = "Supplied region size is too small";
843 return -EINVAL;
Jonathan Brassowc1084562011-08-02 12:32:07 +0100844 }
845
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400846 if (!is_power_of_2(region_size)) {
847 rs->ti->error = "Region size is not a power of 2";
848 return -EINVAL;
849 }
Jonathan Brassowc1084562011-08-02 12:32:07 +0100850
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400851 if (region_size < rs->md.chunk_sectors) {
852 rs->ti->error = "Region size is smaller than the chunk size";
853 return -EINVAL;
854 }
Jonathan Brassowc1084562011-08-02 12:32:07 +0100855 }
856
857 /*
858 * Convert sectors to bytes.
859 */
860 rs->md.bitmap_info.chunksize = (region_size << 9);
861
862 return 0;
863}
864
865/*
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600866 * validate_raid_redundancy
Jonathan Brassoweb649122012-10-11 13:40:09 +1100867 * @rs
868 *
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600869 * Determine if there are enough devices in the array that haven't
870 * failed (or are being rebuilt) to form a usable array.
Jonathan Brassoweb649122012-10-11 13:40:09 +1100871 *
872 * Returns: 0 on success, -EINVAL on failure.
873 */
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600874static int validate_raid_redundancy(struct raid_set *rs)
Jonathan Brassoweb649122012-10-11 13:40:09 +1100875{
876 unsigned i, rebuild_cnt = 0;
NeilBrown3f6bbd32013-05-09 10:27:49 +1000877 unsigned rebuilds_per_group = 0, copies, d;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100878 unsigned group_size, last_group_start;
Jonathan Brassoweb649122012-10-11 13:40:09 +1100879
Jonathan Brassoweb649122012-10-11 13:40:09 +1100880 for (i = 0; i < rs->md.raid_disks; i++)
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600881 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) ||
882 !rs->dev[i].rdev.sb_page)
Jonathan Brassoweb649122012-10-11 13:40:09 +1100883 rebuild_cnt++;
884
885 switch (rs->raid_type->level) {
886 case 1:
887 if (rebuild_cnt >= rs->md.raid_disks)
888 goto too_many;
889 break;
890 case 4:
891 case 5:
892 case 6:
893 if (rebuild_cnt > rs->raid_type->parity_devs)
894 goto too_many;
895 break;
896 case 10:
Jonathan Brassow4ec1e362012-10-11 13:40:24 +1100897 copies = raid10_md_layout_to_copies(rs->md.layout);
898 if (rebuild_cnt < copies)
899 break;
900
901 /*
902 * It is possible to have a higher rebuild count for RAID10,
903 * as long as the failed devices occur in different mirror
904 * groups (i.e. different stripes).
905 *
Jonathan Brassow4ec1e362012-10-11 13:40:24 +1100906 * When checking "near" format, make sure no adjacent devices
907 * have failed beyond what can be handled. In addition to the
908 * simple case where the number of devices is a multiple of the
909 * number of copies, we must also handle cases where the number
910 * of devices is not a multiple of the number of copies.
Mike Snitzer43157842016-05-30 13:03:37 -0400911 * E.g. dev1 dev2 dev3 dev4 dev5
912 * A A B B C
913 * C D D E E
Jonathan Brassow4ec1e362012-10-11 13:40:24 +1100914 */
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100915 if (!strcmp("near", raid10_md_layout_to_format(rs->md.layout))) {
916 for (i = 0; i < rs->md.raid_disks * copies; i++) {
917 if (!(i % copies))
918 rebuilds_per_group = 0;
919 d = i % rs->md.raid_disks;
920 if ((!rs->dev[d].rdev.sb_page ||
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +0200921 !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100922 (++rebuilds_per_group >= copies))
923 goto too_many;
924 }
925 break;
926 }
927
928 /*
929 * When checking "far" and "offset" formats, we need to ensure
930 * that the device that holds its copy is not also dead or
931 * being rebuilt. (Note that "far" and "offset" formats only
932 * support two copies right now. These formats also only ever
933 * use the 'use_far_sets' variant.)
934 *
935 * This check is somewhat complicated by the need to account
Mike Snitzer43157842016-05-30 13:03:37 -0400936 * for arrays that are not a multiple of (far) copies. This
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100937 * results in the need to treat the last (potentially larger)
938 * set differently.
939 */
940 group_size = (rs->md.raid_disks / copies);
941 last_group_start = (rs->md.raid_disks / group_size) - 1;
942 last_group_start *= group_size;
943 for (i = 0; i < rs->md.raid_disks; i++) {
944 if (!(i % copies) && !(i > last_group_start))
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600945 rebuilds_per_group = 0;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100946 if ((!rs->dev[i].rdev.sb_page ||
947 !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
Jonathan Brassow4ec1e362012-10-11 13:40:24 +1100948 (++rebuilds_per_group >= copies))
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100949 goto too_many;
Jonathan Brassow4ec1e362012-10-11 13:40:24 +1100950 }
951 break;
Jonathan Brassoweb649122012-10-11 13:40:09 +1100952 default:
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600953 if (rebuild_cnt)
954 return -EINVAL;
Jonathan Brassoweb649122012-10-11 13:40:09 +1100955 }
956
957 return 0;
958
959too_many:
Jonathan Brassoweb649122012-10-11 13:40:09 +1100960 return -EINVAL;
961}
962
963/*
NeilBrown9d09e662011-01-13 20:00:02 +0000964 * Possible arguments are...
NeilBrown9d09e662011-01-13 20:00:02 +0000965 * <chunk_size> [optional_args]
966 *
Jonathan Brassow32737272011-08-02 12:32:07 +0100967 * Argument definitions
968 * <chunk_size> The number of sectors per disk that
Mike Snitzer43157842016-05-30 13:03:37 -0400969 * will form the "stripe"
Jonathan Brassow32737272011-08-02 12:32:07 +0100970 * [[no]sync] Force or prevent recovery of the
Mike Snitzer43157842016-05-30 13:03:37 -0400971 * entire array
NeilBrown9d09e662011-01-13 20:00:02 +0000972 * [rebuild <idx>] Rebuild the drive indicated by the index
Jonathan Brassow32737272011-08-02 12:32:07 +0100973 * [daemon_sleep <ms>] Time between bitmap daemon work to
Mike Snitzer43157842016-05-30 13:03:37 -0400974 * clear bits
NeilBrown9d09e662011-01-13 20:00:02 +0000975 * [min_recovery_rate <kB/sec/disk>] Throttle RAID initialization
976 * [max_recovery_rate <kB/sec/disk>] Throttle RAID initialization
Jonathan Brassow46bed2b2011-08-02 12:32:07 +0100977 * [write_mostly <idx>] Indicate a write mostly drive via index
NeilBrown9d09e662011-01-13 20:00:02 +0000978 * [max_write_behind <sectors>] See '-write-behind=' (man mdadm)
979 * [stripe_cache <sectors>] Stripe cache size for higher RAIDs
Mike Snitzer43157842016-05-30 13:03:37 -0400980 * [region_size <sectors>] Defines granularity of bitmap
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500981 *
982 * RAID10-only options:
Mike Snitzer43157842016-05-30 13:03:37 -0400983 * [raid10_copies <# copies>] Number of copies. (Default: 2)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100984 * [raid10_format <near|far|offset>] Layout algorithm. (Default: near)
NeilBrown9d09e662011-01-13 20:00:02 +0000985 */
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200986static int parse_raid_params(struct raid_set *rs, struct dm_arg_set *as,
NeilBrown9d09e662011-01-13 20:00:02 +0000987 unsigned num_raid_params)
988{
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200989 int raid10_format = ALGORITHM_RAID10_DEFAULT;
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500990 unsigned raid10_copies = 2;
Jonathan Brassoweb649122012-10-11 13:40:09 +1100991 unsigned i;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200992 unsigned value, region_size = 0;
Mike Snitzer542f9032012-07-27 15:08:00 +0100993 sector_t max_io_len;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200994 const char *arg, *key;
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200995 struct raid_dev *rd;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200996 struct raid_type *rt = rs->raid_type;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200997
998 arg = dm_shift_arg(as);
999 num_raid_params--; /* Account for chunk_size argument */
1000
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001001 if (kstrtouint(arg, 10, &value) < 0) {
1002 rs->ti->error = "Bad numerical argument given for chunk_size";
1003 return -EINVAL;
1004 }
NeilBrown9d09e662011-01-13 20:00:02 +00001005
1006 /*
1007 * First, parse the in-order required arguments
Jonathan Brassow32737272011-08-02 12:32:07 +01001008 * "chunk_size" is the only argument of this type.
NeilBrown9d09e662011-01-13 20:00:02 +00001009 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001010 if (rt_is_raid1(rt)) {
Jonathan Brassow32737272011-08-02 12:32:07 +01001011 if (value)
1012 DMERR("Ignoring chunk size parameter for RAID 1");
1013 value = 0;
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001014 } else if (!is_power_of_2(value)) {
1015 rs->ti->error = "Chunk size must be a power of 2";
1016 return -EINVAL;
1017 } else if (value < 8) {
1018 rs->ti->error = "Chunk size value is too small";
1019 return -EINVAL;
1020 }
NeilBrown9d09e662011-01-13 20:00:02 +00001021
1022 rs->md.new_chunk_sectors = rs->md.chunk_sectors = value;
NeilBrown9d09e662011-01-13 20:00:02 +00001023
1024 /*
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001025 * We set each individual device as In_sync with a completed
1026 * 'recovery_offset'. If there has been a device failure or
1027 * replacement then one of the following cases applies:
1028 *
1029 * 1) User specifies 'rebuild'.
Mike Snitzer43157842016-05-30 13:03:37 -04001030 * - Device is reset when param is read.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001031 * 2) A new device is supplied.
Mike Snitzer43157842016-05-30 13:03:37 -04001032 * - No matching superblock found, resets device.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001033 * 3) Device failure was transient and returns on reload.
Mike Snitzer43157842016-05-30 13:03:37 -04001034 * - Failure noticed, resets device for bitmap replay.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001035 * 4) Device hadn't completed recovery after previous failure.
Mike Snitzer43157842016-05-30 13:03:37 -04001036 * - Superblock is read and overrides recovery_offset.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001037 *
1038 * What is found in the superblocks of the devices is always
1039 * authoritative, unless 'rebuild' or '[no]sync' was specified.
1040 */
1041 for (i = 0; i < rs->md.raid_disks; i++) {
1042 set_bit(In_sync, &rs->dev[i].rdev.flags);
1043 rs->dev[i].rdev.recovery_offset = MaxSector;
1044 }
1045
1046 /*
NeilBrown9d09e662011-01-13 20:00:02 +00001047 * Second, parse the unordered optional arguments
1048 */
NeilBrown9d09e662011-01-13 20:00:02 +00001049 for (i = 0; i < num_raid_params; i++) {
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001050 key = dm_shift_arg(as);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001051 if (!key) {
1052 rs->ti->error = "Not enough raid parameters given";
1053 return -EINVAL;
1054 }
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001055
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001056 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_NOSYNC))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001057 if (test_and_set_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001058 rs->ti->error = "Only one 'nosync' argument allowed";
1059 return -EINVAL;
1060 }
NeilBrown9d09e662011-01-13 20:00:02 +00001061 rs->md.recovery_cp = MaxSector;
NeilBrown9d09e662011-01-13 20:00:02 +00001062 continue;
1063 }
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001064 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_SYNC))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001065 if (test_and_set_bit(__CTR_FLAG_SYNC, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001066 rs->ti->error = "Only one 'sync' argument allowed";
1067 return -EINVAL;
1068 }
NeilBrown9d09e662011-01-13 20:00:02 +00001069 rs->md.recovery_cp = 0;
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001070 continue;
1071 }
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001072 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_USE_NEAR_SETS))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001073 if (test_and_set_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001074 rs->ti->error = "Only one 'raid10_use_new_sets' argument allowed";
1075 return -EINVAL;
1076 }
NeilBrown9d09e662011-01-13 20:00:02 +00001077 continue;
1078 }
1079
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001080 arg = dm_shift_arg(as);
1081 i++; /* Account for the argument pairs */
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001082 if (!arg) {
1083 rs->ti->error = "Wrong number of raid parameters given";
1084 return -EINVAL;
1085 }
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001086
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001087 /*
1088 * Parameters that take a string value are checked here.
1089 */
1090
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001091 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_FORMAT))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001092 if (test_and_set_bit(__CTR_FLAG_RAID10_FORMAT, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001093 rs->ti->error = "Only one 'raid10_format' argument pair allowed";
1094 return -EINVAL;
1095 }
1096 if (!rt_is_raid10(rt)) {
1097 rs->ti->error = "'raid10_format' is an invalid parameter for this RAID type";
1098 return -EINVAL;
1099 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001100 raid10_format = raid10_name_to_format(arg);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001101 if (raid10_format < 0) {
1102 rs->ti->error = "Invalid 'raid10_format' value given";
1103 return raid10_format;
1104 }
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001105 continue;
1106 }
1107
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001108 if (kstrtouint(arg, 10, &value) < 0) {
1109 rs->ti->error = "Bad numerical argument given in raid params";
1110 return -EINVAL;
1111 }
NeilBrown9d09e662011-01-13 20:00:02 +00001112
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001113 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_REBUILD))) {
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001114 /*
1115 * "rebuild" is being passed in by userspace to provide
1116 * indexes of replaced devices and to set up additional
1117 * devices on raid level takeover.
Mike Snitzer43157842016-05-30 13:03:37 -04001118 */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001119 if (!__within_range(value, 0, rs->raid_disks - 1)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001120 rs->ti->error = "Invalid rebuild index given";
1121 return -EINVAL;
1122 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001123
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001124 if (test_and_set_bit(value, (void *) rs->rebuild_disks)) {
1125 rs->ti->error = "rebuild for this index already given";
1126 return -EINVAL;
1127 }
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001128
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001129 rd = rs->dev + value;
1130 clear_bit(In_sync, &rd->rdev.flags);
1131 clear_bit(Faulty, &rd->rdev.flags);
1132 rd->rdev.recovery_offset = 0;
Mike Snitzer42863252016-06-02 12:27:46 -04001133 set_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags);
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001134 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_WRITE_MOSTLY))) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001135 if (!rt_is_raid1(rt)) {
1136 rs->ti->error = "write_mostly option is only valid for RAID1";
1137 return -EINVAL;
1138 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001139
Mike Snitzerbb91a632016-06-02 12:06:54 -04001140 if (!__within_range(value, 0, rs->md.raid_disks - 1)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001141 rs->ti->error = "Invalid write_mostly index given";
1142 return -EINVAL;
1143 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001144
Jonathan Brassow46bed2b2011-08-02 12:32:07 +01001145 set_bit(WriteMostly, &rs->dev[value].rdev.flags);
Mike Snitzer42863252016-06-02 12:27:46 -04001146 set_bit(__CTR_FLAG_WRITE_MOSTLY, &rs->ctr_flags);
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001147 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MAX_WRITE_BEHIND))) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001148 if (!rt_is_raid1(rt)) {
1149 rs->ti->error = "max_write_behind option is only valid for RAID1";
1150 return -EINVAL;
1151 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001152
Mike Snitzer42863252016-06-02 12:27:46 -04001153 if (test_and_set_bit(__CTR_FLAG_MAX_WRITE_BEHIND, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001154 rs->ti->error = "Only one max_write_behind argument pair allowed";
1155 return -EINVAL;
1156 }
NeilBrown9d09e662011-01-13 20:00:02 +00001157
1158 /*
1159 * In device-mapper, we specify things in sectors, but
1160 * MD records this value in kB
1161 */
1162 value /= 2;
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001163 if (value > COUNTER_MAX) {
1164 rs->ti->error = "Max write-behind limit out of range";
1165 return -EINVAL;
1166 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001167
NeilBrown9d09e662011-01-13 20:00:02 +00001168 rs->md.bitmap_info.max_write_behind = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001169 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DAEMON_SLEEP))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001170 if (test_and_set_bit(__CTR_FLAG_DAEMON_SLEEP, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001171 rs->ti->error = "Only one daemon_sleep argument pair allowed";
1172 return -EINVAL;
1173 }
1174 if (!value || (value > MAX_SCHEDULE_TIMEOUT)) {
1175 rs->ti->error = "daemon sleep period out of range";
1176 return -EINVAL;
1177 }
NeilBrown9d09e662011-01-13 20:00:02 +00001178 rs->md.bitmap_info.daemon_sleep = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001179 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DATA_OFFSET))) {
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001180 /* Userspace passes new data_offset after having extended the the data image LV */
Mike Snitzer42863252016-06-02 12:27:46 -04001181 if (test_and_set_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001182 rs->ti->error = "Only one data_offset argument pair allowed";
1183 return -EINVAL;
1184 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001185 /* Ensure sensible data offset */
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001186 if (value < 0) {
1187 rs->ti->error = "Bogus data_offset value";
1188 return -EINVAL;
1189 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001190 rs->data_offset = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001191 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DELTA_DISKS))) {
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001192 /* Define the +/-# of disks to add to/remove from the given raid set */
Mike Snitzer42863252016-06-02 12:27:46 -04001193 if (test_and_set_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001194 rs->ti->error = "Only one delta_disks argument pair allowed";
1195 return -EINVAL;
1196 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001197 /* Ensure MAX_RAID_DEVICES and raid type minimal_devs! */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001198 if (!__within_range(abs(value), 1, MAX_RAID_DEVICES - rt->minimal_devs)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001199 rs->ti->error = "Too many delta_disk requested";
1200 return -EINVAL;
1201 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001202
1203 rs->delta_disks = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001204 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_STRIPE_CACHE))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001205 if (test_and_set_bit(__CTR_FLAG_STRIPE_CACHE, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001206 rs->ti->error = "Only one stripe_cache argument pair allowed";
1207 return -EINVAL;
1208 }
1209
NeilBrown9d09e662011-01-13 20:00:02 +00001210 /*
1211 * In device-mapper, we specify things in sectors, but
1212 * MD records this value in kB
1213 */
1214 value /= 2;
1215
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001216 if (!rt_is_raid456(rt)) {
1217 rs->ti->error = "Inappropriate argument: stripe_cache";
1218 return -EINVAL;
1219 }
1220 if (raid5_set_cache_size(&rs->md, (int)value)) {
1221 rs->ti->error = "Bad stripe_cache size";
1222 return -EINVAL;
1223 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001224
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001225 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MIN_RECOVERY_RATE))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001226 if (test_and_set_bit(__CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001227 rs->ti->error = "Only one min_recovery_rate argument pair allowed";
1228 return -EINVAL;
1229 }
1230 if (value > INT_MAX) {
1231 rs->ti->error = "min_recovery_rate out of range";
1232 return -EINVAL;
1233 }
NeilBrown9d09e662011-01-13 20:00:02 +00001234 rs->md.sync_speed_min = (int)value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001235 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MAX_RECOVERY_RATE))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001236 if (test_and_set_bit(__CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001237 rs->ti->error = "Only one max_recovery_rate argument pair allowed";
1238 return -EINVAL;
1239 }
1240 if (value > INT_MAX) {
1241 rs->ti->error = "max_recovery_rate out of range";
1242 return -EINVAL;
1243 }
NeilBrown9d09e662011-01-13 20:00:02 +00001244 rs->md.sync_speed_max = (int)value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001245 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_REGION_SIZE))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001246 if (test_and_set_bit(__CTR_FLAG_REGION_SIZE, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001247 rs->ti->error = "Only one region_size argument pair allowed";
1248 return -EINVAL;
1249 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001250
Jonathan Brassowc1084562011-08-02 12:32:07 +01001251 region_size = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001252 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_COPIES))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001253 if (test_and_set_bit(__CTR_FLAG_RAID10_COPIES, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001254 rs->ti->error = "Only one raid10_copies argument pair allowed";
1255 return -EINVAL;
1256 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001257
Mike Snitzerbb91a632016-06-02 12:06:54 -04001258 if (!__within_range(value, 2, rs->md.raid_disks)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001259 rs->ti->error = "Bad value for 'raid10_copies'";
1260 return -EINVAL;
1261 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001262
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001263 raid10_copies = value;
NeilBrown9d09e662011-01-13 20:00:02 +00001264 } else {
1265 DMERR("Unable to parse RAID parameter: %s", key);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001266 rs->ti->error = "Unable to parse RAID parameter";
1267 return -EINVAL;
NeilBrown9d09e662011-01-13 20:00:02 +00001268 }
1269 }
1270
Jonathan Brassowc1084562011-08-02 12:32:07 +01001271 if (validate_region_size(rs, region_size))
1272 return -EINVAL;
1273
1274 if (rs->md.chunk_sectors)
Mike Snitzer542f9032012-07-27 15:08:00 +01001275 max_io_len = rs->md.chunk_sectors;
Jonathan Brassowc1084562011-08-02 12:32:07 +01001276 else
Mike Snitzer542f9032012-07-27 15:08:00 +01001277 max_io_len = region_size;
Jonathan Brassowc1084562011-08-02 12:32:07 +01001278
Mike Snitzer542f9032012-07-27 15:08:00 +01001279 if (dm_set_target_max_io_len(rs->ti, max_io_len))
1280 return -EINVAL;
Jonathan Brassow32737272011-08-02 12:32:07 +01001281
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001282 if (rt_is_raid10(rt)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001283 if (raid10_copies > rs->md.raid_disks) {
1284 rs->ti->error = "Not enough devices to satisfy specification";
1285 return -EINVAL;
1286 }
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001287
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001288 rs->md.new_layout = raid10_format_to_md_layout(rs, raid10_format, raid10_copies);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001289 if (rs->md.new_layout < 0) {
1290 rs->ti->error = "Error getting raid10 format";
1291 return rs->md.new_layout;
1292 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001293
1294 rt = get_raid_type_by_ll(10, rs->md.new_layout);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001295 if (!rt) {
1296 rs->ti->error = "Failed to recognize new raid10 layout";
1297 return -EINVAL;
1298 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001299
1300 if ((rt->algorithm == ALGORITHM_RAID10_DEFAULT ||
1301 rt->algorithm == ALGORITHM_RAID10_NEAR) &&
Mike Snitzer42863252016-06-02 12:27:46 -04001302 test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001303 rs->ti->error = "RAID10 format 'near' and 'raid10_use_near_sets' are incompatible";
1304 return -EINVAL;
1305 }
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11001306
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001307 rs->md.layout = rs->md.new_layout;
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001308 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001309
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001310 rs->raid10_copies = raid10_copies;
Jonathan E Brassowc039c332012-07-27 15:08:04 +01001311
NeilBrown9d09e662011-01-13 20:00:02 +00001312 /* Assume there are no metadata devices until the drives are parsed */
1313 rs->md.persistent = 0;
1314 rs->md.external = 1;
1315
Heinz Mauelshagenf0902792016-05-19 18:49:27 +02001316 /* Check, if any invalid ctr arguments have been passed in for the raid level */
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +02001317 return rs_check_for_valid_flags(rs);
NeilBrown9d09e662011-01-13 20:00:02 +00001318}
1319
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02001320/* Return # of data stripes as kept in mddev as of @rs (i.e. as of superblock) */
1321static unsigned int mddev_data_stripes(struct raid_set *rs)
1322{
1323 return rs->md.raid_disks - rs->raid_type->parity_devs;
1324}
1325
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001326/* Return # of data stripes of @rs (i.e. as of ctr) */
1327static unsigned int rs_data_stripes(struct raid_set *rs)
1328{
1329 return rs->raid_disks - rs->raid_type->parity_devs;
1330}
1331
1332/* Calculate the sectors per device and per array used for @rs */
1333static int rs_set_dev_and_array_sectors(struct raid_set *rs, bool use_mddev)
1334{
1335 int delta_disks;
1336 unsigned int data_stripes;
1337 struct mddev *mddev = &rs->md;
1338 struct md_rdev *rdev;
1339 sector_t array_sectors = rs->ti->len, dev_sectors = rs->ti->len;
1340
1341 if (use_mddev) {
1342 delta_disks = mddev->delta_disks;
1343 data_stripes = mddev_data_stripes(rs);
1344 } else {
1345 delta_disks = rs->delta_disks;
1346 data_stripes = rs_data_stripes(rs);
1347 }
1348
1349 /* Special raid1 case w/o delta_disks support (yet) */
1350 if (rt_is_raid1(rs->raid_type))
1351 ;
1352 else if (rt_is_raid10(rs->raid_type)) {
1353 if (rs->raid10_copies < 2 ||
1354 delta_disks < 0) {
1355 rs->ti->error = "Bogus raid10 data copies or delta disks";
1356 return EINVAL;
1357 }
1358
1359 dev_sectors *= rs->raid10_copies;
1360 if (sector_div(dev_sectors, data_stripes))
1361 goto bad;
1362
1363 array_sectors = (data_stripes + delta_disks) * dev_sectors;
1364 if (sector_div(array_sectors, rs->raid10_copies))
1365 goto bad;
1366
1367 } else if (sector_div(dev_sectors, data_stripes))
1368 goto bad;
1369
1370 else
1371 /* Striped layouts */
1372 array_sectors = (data_stripes + delta_disks) * dev_sectors;
1373
1374 rdev_for_each(rdev, mddev)
1375 rdev->sectors = dev_sectors;
1376
1377 mddev->array_sectors = array_sectors;
1378 mddev->dev_sectors = dev_sectors;
1379
1380 return 0;
1381bad:
1382 rs->ti->error = "Target length not divisible by number of data devices";
1383 return EINVAL;
1384}
1385
NeilBrown9d09e662011-01-13 20:00:02 +00001386static void do_table_event(struct work_struct *ws)
1387{
1388 struct raid_set *rs = container_of(ws, struct raid_set, md.event_work);
1389
1390 dm_table_event(rs->ti->table);
1391}
1392
1393static int raid_is_congested(struct dm_target_callbacks *cb, int bits)
1394{
1395 struct raid_set *rs = container_of(cb, struct raid_set, callbacks);
1396
NeilBrown5c675f82014-12-15 12:56:56 +11001397 return mddev_congested(&rs->md, bits);
NeilBrown9d09e662011-01-13 20:00:02 +00001398}
1399
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001400/*
1401 * Make sure a valid takover (level switch) is being requested on @rs
1402 *
1403 * Conversions of raid sets from one MD personality to another
1404 * have to conform to restrictions which are enforced here.
1405 *
1406 * Degration is already checked for in rs_check_conversion() below.
1407 */
1408static int rs_check_takeover(struct raid_set *rs)
1409{
1410 struct mddev *mddev = &rs->md;
1411 unsigned int near_copies;
1412
1413 switch (mddev->level) {
1414 case 0:
1415 /* raid0 -> raid1/5 with one disk */
1416 if ((mddev->new_level == 1 || mddev->new_level == 5) &&
1417 mddev->raid_disks == 1)
1418 return 0;
1419
1420 /* raid0 -> raid10 */
1421 if (mddev->new_level == 10 &&
1422 !(rs->raid_disks % 2))
1423 return 0;
1424
1425 /* raid0 with multiple disks -> raid4/5/6 */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001426 if (__within_range(mddev->new_level, 4, 6) &&
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001427 mddev->new_layout == ALGORITHM_PARITY_N &&
1428 mddev->raid_disks > 1)
1429 return 0;
1430
1431 break;
1432
1433 case 10:
1434 /* Can't takeover raid10_offset! */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001435 if (__is_raid10_offset(mddev->layout))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001436 break;
1437
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001438 near_copies = __raid10_near_copies(mddev->layout);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001439
1440 /* raid10* -> raid0 */
1441 if (mddev->new_level == 0) {
1442 /* Can takeover raid10_near with raid disks divisable by data copies! */
1443 if (near_copies > 1 &&
1444 !(mddev->raid_disks % near_copies)) {
1445 mddev->raid_disks /= near_copies;
1446 mddev->delta_disks = mddev->raid_disks;
1447 return 0;
1448 }
1449
1450 /* Can takeover raid10_far */
1451 if (near_copies == 1 &&
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001452 __raid10_far_copies(mddev->layout) > 1)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001453 return 0;
1454
1455 break;
1456 }
1457
1458 /* raid10_{near,far} -> raid1 */
1459 if (mddev->new_level == 1 &&
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001460 max(near_copies, __raid10_far_copies(mddev->layout)) == mddev->raid_disks)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001461 return 0;
1462
1463 /* raid10_{near,far} with 2 disks -> raid4/5 */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001464 if (__within_range(mddev->new_level, 4, 5) &&
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001465 mddev->raid_disks == 2)
1466 return 0;
1467 break;
1468
1469 case 1:
1470 /* raid1 with 2 disks -> raid4/5 */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001471 if (__within_range(mddev->new_level, 4, 5) &&
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001472 mddev->raid_disks == 2) {
1473 mddev->degraded = 1;
1474 return 0;
1475 }
1476
1477 /* raid1 -> raid0 */
1478 if (mddev->new_level == 0 &&
1479 mddev->raid_disks == 1)
1480 return 0;
1481
1482 /* raid1 -> raid10 */
1483 if (mddev->new_level == 10)
1484 return 0;
1485
1486 break;
1487
1488 case 4:
1489 /* raid4 -> raid0 */
1490 if (mddev->new_level == 0)
1491 return 0;
1492
1493 /* raid4 -> raid1/5 with 2 disks */
1494 if ((mddev->new_level == 1 || mddev->new_level == 5) &&
1495 mddev->raid_disks == 2)
1496 return 0;
1497
1498 /* raid4 -> raid5/6 with parity N */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001499 if (__within_range(mddev->new_level, 5, 6) &&
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001500 mddev->layout == ALGORITHM_PARITY_N)
1501 return 0;
1502 break;
1503
1504 case 5:
1505 /* raid5 with parity N -> raid0 */
1506 if (mddev->new_level == 0 &&
1507 mddev->layout == ALGORITHM_PARITY_N)
1508 return 0;
1509
1510 /* raid5 with parity N -> raid4 */
1511 if (mddev->new_level == 4 &&
1512 mddev->layout == ALGORITHM_PARITY_N)
1513 return 0;
1514
1515 /* raid5 with 2 disks -> raid1/4/10 */
1516 if ((mddev->new_level == 1 || mddev->new_level == 4 || mddev->new_level == 10) &&
1517 mddev->raid_disks == 2)
1518 return 0;
1519
1520 /* raid5 with parity N -> raid6 with parity N */
1521 if (mddev->new_level == 6 &&
1522 ((mddev->layout == ALGORITHM_PARITY_N && mddev->new_layout == ALGORITHM_PARITY_N) ||
Mike Snitzerbb91a632016-06-02 12:06:54 -04001523 __within_range(mddev->new_layout, ALGORITHM_LEFT_ASYMMETRIC_6, ALGORITHM_RIGHT_SYMMETRIC_6)))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001524 return 0;
1525 break;
1526
1527 case 6:
1528 /* raid6 with parity N -> raid0 */
1529 if (mddev->new_level == 0 &&
1530 mddev->layout == ALGORITHM_PARITY_N)
1531 return 0;
1532
1533 /* raid6 with parity N -> raid4 */
1534 if (mddev->new_level == 4 &&
1535 mddev->layout == ALGORITHM_PARITY_N)
1536 return 0;
1537
1538 /* raid6_*_n with parity N -> raid5_* */
1539 if (mddev->new_level == 5 &&
1540 ((mddev->layout == ALGORITHM_PARITY_N && mddev->new_layout == ALGORITHM_PARITY_N) ||
Mike Snitzerbb91a632016-06-02 12:06:54 -04001541 __within_range(mddev->new_layout, ALGORITHM_LEFT_ASYMMETRIC, ALGORITHM_RIGHT_SYMMETRIC)))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001542 return 0;
1543
1544 default:
1545 break;
1546 }
1547
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001548 rs->ti->error = "takeover not possible";
1549 return -EINVAL;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001550}
1551
1552/* True if @rs requested to be taken over */
1553static bool rs_takeover_requested(struct raid_set *rs)
1554{
1555 return rs->md.new_level != rs->md.level;
1556}
1557
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001558/* True if @rs is requested to reshape by ctr */
1559static bool rs_reshape_requested(struct raid_set *rs)
1560{
1561 struct mddev *mddev = &rs->md;
1562
1563 if (!mddev->level)
1564 return false;
1565
1566 return !__is_raid10_far(mddev->new_layout) &&
1567 mddev->new_level == mddev->level &&
1568 (mddev->new_layout != mddev->layout ||
1569 mddev->new_chunk_sectors != mddev->chunk_sectors ||
1570 rs->raid_disks + rs->delta_disks != mddev->raid_disks);
1571}
1572
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001573/* Features */
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001574#define FEATURE_FLAG_SUPPORTS_V190 0x1 /* Supports extended superblock */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001575
1576/* State flags for sb->flags */
1577#define SB_FLAG_RESHAPE_ACTIVE 0x1
1578#define SB_FLAG_RESHAPE_BACKWARDS 0x2
1579
NeilBrown9d09e662011-01-13 20:00:02 +00001580/*
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001581 * This structure is never routinely used by userspace, unlike md superblocks.
1582 * Devices with this superblock should only ever be accessed via device-mapper.
1583 */
1584#define DM_RAID_MAGIC 0x64526D44
1585struct dm_raid_superblock {
1586 __le32 magic; /* "DmRd" */
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001587 __le32 compat_features; /* Used to indicate compatible features (like 1.9.0 ondisk metadata extension) */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001588
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001589 __le32 num_devices; /* Number of devices in this raid set. (Max 64) */
1590 __le32 array_position; /* The position of this drive in the raid set */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001591
1592 __le64 events; /* Incremented by md when superblock updated */
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001593 __le64 failed_devices; /* Pre 1.9.0 part of bit field of devices to */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001594 /* indicate failures (see extension below) */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001595
1596 /*
1597 * This offset tracks the progress of the repair or replacement of
1598 * an individual drive.
1599 */
1600 __le64 disk_recovery_offset;
1601
1602 /*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001603 * This offset tracks the progress of the initial raid set
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001604 * synchronisation/parity calculation.
1605 */
1606 __le64 array_resync_offset;
1607
1608 /*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001609 * raid characteristics
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001610 */
1611 __le32 level;
1612 __le32 layout;
1613 __le32 stripe_sectors;
1614
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001615 /********************************************************************
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001616 * BELOW FOLLOW V1.9.0 EXTENSIONS TO THE PRISTINE SUPERBLOCK FORMAT!!!
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001617 *
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001618 * FEATURE_FLAG_SUPPORTS_V190 in the features member indicates that those exist
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001619 */
1620
1621 __le32 flags; /* Flags defining array states for reshaping */
1622
1623 /*
1624 * This offset tracks the progress of a raid
1625 * set reshape in order to be able to restart it
1626 */
1627 __le64 reshape_position;
1628
1629 /*
1630 * These define the properties of the array in case of an interrupted reshape
1631 */
1632 __le32 new_level;
1633 __le32 new_layout;
1634 __le32 new_stripe_sectors;
1635 __le32 delta_disks;
1636
1637 __le64 array_sectors; /* Array size in sectors */
1638
1639 /*
1640 * Sector offsets to data on devices (reshaping).
1641 * Needed to support out of place reshaping, thus
1642 * not writing over any stripes whilst converting
1643 * them from old to new layout
1644 */
1645 __le64 data_offset;
1646 __le64 new_data_offset;
1647
1648 __le64 sectors; /* Used device size in sectors */
1649
1650 /*
1651 * Additonal Bit field of devices indicating failures to support
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001652 * up to 256 devices with the 1.9.0 on-disk metadata format
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001653 */
1654 __le64 extended_failed_devices[DISKS_ARRAY_ELEMS - 1];
1655
1656 __le32 incompat_features; /* Used to indicate any incompatible features */
1657
1658 /* Always set rest up to logical block size to 0 when writing (see get_metadata_device() below). */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001659} __packed;
1660
NeilBrown3cb03002011-10-11 16:45:26 +11001661static int read_disk_sb(struct md_rdev *rdev, int size)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001662{
1663 BUG_ON(!rdev->sb_page);
1664
1665 if (rdev->sb_loaded)
1666 return 0;
1667
Mike Christie796a5cf2016-06-05 14:32:07 -05001668 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, 1)) {
Jonathan E Brassow04475682012-03-28 18:41:26 +01001669 DMERR("Failed to read superblock of device at position %d",
1670 rdev->raid_disk);
Jonathan Brassowc32fb9e2012-05-22 13:55:31 +10001671 md_error(rdev->mddev, rdev);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001672 return -EINVAL;
1673 }
1674
1675 rdev->sb_loaded = 1;
1676
1677 return 0;
1678}
1679
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001680static void sb_retrieve_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices)
1681{
1682 failed_devices[0] = le64_to_cpu(sb->failed_devices);
1683 memset(failed_devices + 1, 0, sizeof(sb->extended_failed_devices));
1684
Mike Snitzer42863252016-06-02 12:27:46 -04001685 if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001686 int i = ARRAY_SIZE(sb->extended_failed_devices);
1687
1688 while (i--)
1689 failed_devices[i+1] = le64_to_cpu(sb->extended_failed_devices[i]);
1690 }
1691}
1692
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001693static void sb_update_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices)
1694{
1695 int i = ARRAY_SIZE(sb->extended_failed_devices);
1696
1697 sb->failed_devices = cpu_to_le64(failed_devices[0]);
1698 while (i--)
1699 sb->extended_failed_devices[i] = cpu_to_le64(failed_devices[i+1]);
1700}
1701
1702/*
1703 * Synchronize the superblock members with the raid set properties
1704 *
1705 * All superblock data is little endian.
1706 */
NeilBrownfd01b882011-10-11 16:47:53 +11001707static void super_sync(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001708{
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001709 bool update_failed_devices = false;
1710 unsigned int i;
1711 uint64_t failed_devices[DISKS_ARRAY_ELEMS];
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001712 struct dm_raid_superblock *sb;
Jonathan Brassow81f382f2012-05-22 13:55:30 +10001713 struct raid_set *rs = container_of(mddev, struct raid_set, md);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001714
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001715 /* No metadata device, no superblock */
1716 if (!rdev->meta_bdev)
1717 return;
1718
1719 BUG_ON(!rdev->sb_page);
1720
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001721 sb = page_address(rdev->sb_page);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001722
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001723 sb_retrieve_failed_devices(sb, failed_devices);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001724
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001725 for (i = 0; i < rs->raid_disks; i++)
1726 if (!rs->dev[i].data_dev || test_bit(Faulty, &rs->dev[i].rdev.flags)) {
1727 update_failed_devices = true;
1728 set_bit(i, (void *) failed_devices);
1729 }
1730
1731 if (update_failed_devices)
1732 sb_update_failed_devices(sb, failed_devices);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001733
1734 sb->magic = cpu_to_le32(DM_RAID_MAGIC);
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001735 sb->compat_features = cpu_to_le32(FEATURE_FLAG_SUPPORTS_V190);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001736
1737 sb->num_devices = cpu_to_le32(mddev->raid_disks);
1738 sb->array_position = cpu_to_le32(rdev->raid_disk);
1739
1740 sb->events = cpu_to_le64(mddev->events);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001741
1742 sb->disk_recovery_offset = cpu_to_le64(rdev->recovery_offset);
1743 sb->array_resync_offset = cpu_to_le64(mddev->recovery_cp);
1744
1745 sb->level = cpu_to_le32(mddev->level);
1746 sb->layout = cpu_to_le32(mddev->layout);
1747 sb->stripe_sectors = cpu_to_le32(mddev->chunk_sectors);
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001748
1749 sb->new_level = cpu_to_le32(mddev->new_level);
1750 sb->new_layout = cpu_to_le32(mddev->new_layout);
1751 sb->new_stripe_sectors = cpu_to_le32(mddev->new_chunk_sectors);
1752
1753 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1754
1755 smp_rmb(); /* Make sure we access most recent reshape position */
1756 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1757 if (le64_to_cpu(sb->reshape_position) != MaxSector) {
1758 /* Flag ongoing reshape */
1759 sb->flags |= cpu_to_le32(SB_FLAG_RESHAPE_ACTIVE);
1760
1761 if (mddev->delta_disks < 0 || mddev->reshape_backwards)
1762 sb->flags |= cpu_to_le32(SB_FLAG_RESHAPE_BACKWARDS);
Mike Snitzer42863252016-06-02 12:27:46 -04001763 } else {
1764 /* Clear reshape flags */
1765 sb->flags &= ~(cpu_to_le32(SB_FLAG_RESHAPE_ACTIVE|SB_FLAG_RESHAPE_BACKWARDS));
1766 }
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001767
1768 sb->array_sectors = cpu_to_le64(mddev->array_sectors);
1769 sb->data_offset = cpu_to_le64(rdev->data_offset);
1770 sb->new_data_offset = cpu_to_le64(rdev->new_data_offset);
1771 sb->sectors = cpu_to_le64(rdev->sectors);
1772
1773 /* Zero out the rest of the payload after the size of the superblock */
1774 memset(sb + 1, 0, rdev->sb_size - sizeof(*sb));
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001775}
1776
1777/*
1778 * super_load
1779 *
1780 * This function creates a superblock if one is not found on the device
1781 * and will decide which superblock to use if there's a choice.
1782 *
1783 * Return: 1 if use rdev, 0 if use refdev, -Exxx otherwise
1784 */
NeilBrown3cb03002011-10-11 16:45:26 +11001785static int super_load(struct md_rdev *rdev, struct md_rdev *refdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001786{
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02001787 int r;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001788 struct dm_raid_superblock *sb;
1789 struct dm_raid_superblock *refsb;
1790 uint64_t events_sb, events_refsb;
1791
1792 rdev->sb_start = 0;
Heinz Mauelshagen40d43c42014-10-17 13:38:50 +02001793 rdev->sb_size = bdev_logical_block_size(rdev->meta_bdev);
1794 if (rdev->sb_size < sizeof(*sb) || rdev->sb_size > PAGE_SIZE) {
1795 DMERR("superblock size of a logical block is no longer valid");
1796 return -EINVAL;
1797 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001798
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02001799 r = read_disk_sb(rdev, rdev->sb_size);
1800 if (r)
1801 return r;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001802
1803 sb = page_address(rdev->sb_page);
Jonathan E Brassow3aa3b2b2012-03-07 19:09:47 +00001804
1805 /*
1806 * Two cases that we want to write new superblocks and rebuild:
1807 * 1) New device (no matching magic number)
1808 * 2) Device specified for rebuild (!In_sync w/ offset == 0)
1809 */
1810 if ((sb->magic != cpu_to_le32(DM_RAID_MAGIC)) ||
1811 (!test_bit(In_sync, &rdev->flags) && !rdev->recovery_offset)) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001812 super_sync(rdev->mddev, rdev);
1813
1814 set_bit(FirstUse, &rdev->flags);
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001815 sb->compat_features = cpu_to_le32(FEATURE_FLAG_SUPPORTS_V190);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001816
1817 /* Force writing of superblocks to disk */
1818 set_bit(MD_CHANGE_DEVS, &rdev->mddev->flags);
1819
1820 /* Any superblock is better than none, choose that if given */
1821 return refdev ? 0 : 1;
1822 }
1823
1824 if (!refdev)
1825 return 1;
1826
1827 events_sb = le64_to_cpu(sb->events);
1828
1829 refsb = page_address(refdev->sb_page);
1830 events_refsb = le64_to_cpu(refsb->events);
1831
1832 return (events_sb > events_refsb) ? 1 : 0;
1833}
1834
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001835static int super_init_validation(struct raid_set *rs, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001836{
1837 int role;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001838 unsigned int d;
1839 struct mddev *mddev = &rs->md;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001840 uint64_t events_sb;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001841 uint64_t failed_devices[DISKS_ARRAY_ELEMS];
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001842 struct dm_raid_superblock *sb;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001843 uint32_t new_devs = 0, rebuild_and_new = 0, rebuilds = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001844 struct md_rdev *r;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001845 struct dm_raid_superblock *sb2;
1846
1847 sb = page_address(rdev->sb_page);
1848 events_sb = le64_to_cpu(sb->events);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001849
1850 /*
1851 * Initialise to 1 if this is a new superblock.
1852 */
1853 mddev->events = events_sb ? : 1;
1854
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001855 mddev->reshape_position = MaxSector;
1856
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001857 /*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001858 * Reshaping is supported, e.g. reshape_position is valid
1859 * in superblock and superblock content is authoritative.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001860 */
Mike Snitzer42863252016-06-02 12:27:46 -04001861 if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001862 /* Superblock is authoritative wrt given raid set layout! */
1863 mddev->raid_disks = le32_to_cpu(sb->num_devices);
1864 mddev->level = le32_to_cpu(sb->level);
1865 mddev->layout = le32_to_cpu(sb->layout);
1866 mddev->chunk_sectors = le32_to_cpu(sb->stripe_sectors);
1867 mddev->new_level = le32_to_cpu(sb->new_level);
1868 mddev->new_layout = le32_to_cpu(sb->new_layout);
1869 mddev->new_chunk_sectors = le32_to_cpu(sb->new_stripe_sectors);
1870 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1871 mddev->array_sectors = le64_to_cpu(sb->array_sectors);
1872
1873 /* raid was reshaping and got interrupted */
Mike Snitzer42863252016-06-02 12:27:46 -04001874 if (le32_to_cpu(sb->flags) & SB_FLAG_RESHAPE_ACTIVE) {
1875 if (test_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags)) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001876 DMERR("Reshape requested but raid set is still reshaping");
1877 return -EINVAL;
1878 }
1879
1880 if (mddev->delta_disks < 0 ||
Mike Snitzer42863252016-06-02 12:27:46 -04001881 (!mddev->delta_disks && (le32_to_cpu(sb->flags) & SB_FLAG_RESHAPE_BACKWARDS)))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001882 mddev->reshape_backwards = 1;
1883 else
1884 mddev->reshape_backwards = 0;
1885
1886 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1887 rs->raid_type = get_raid_type_by_ll(mddev->level, mddev->layout);
1888 }
1889
1890 } else {
1891 /*
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001892 * No takeover/reshaping, because we don't have the extended v1.9.0 metadata
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001893 */
1894 if (le32_to_cpu(sb->level) != mddev->level) {
1895 DMERR("Reshaping/takeover raid sets not yet supported. (raid level/stripes/size change)");
1896 return -EINVAL;
1897 }
1898 if (le32_to_cpu(sb->layout) != mddev->layout) {
1899 DMERR("Reshaping raid sets not yet supported. (raid layout change)");
Mike Snitzer43157842016-05-30 13:03:37 -04001900 DMERR(" 0x%X vs 0x%X", le32_to_cpu(sb->layout), mddev->layout);
1901 DMERR(" Old layout: %s w/ %d copies",
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001902 raid10_md_layout_to_format(le32_to_cpu(sb->layout)),
1903 raid10_md_layout_to_copies(le32_to_cpu(sb->layout)));
Mike Snitzer43157842016-05-30 13:03:37 -04001904 DMERR(" New layout: %s w/ %d copies",
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001905 raid10_md_layout_to_format(mddev->layout),
1906 raid10_md_layout_to_copies(mddev->layout));
1907 return -EINVAL;
1908 }
1909 if (le32_to_cpu(sb->stripe_sectors) != mddev->chunk_sectors) {
1910 DMERR("Reshaping raid sets not yet supported. (stripe sectors change)");
1911 return -EINVAL;
1912 }
1913
1914 /* We can only change the number of devices in raid1 with old (i.e. pre 1.0.7) metadata */
1915 if (!rt_is_raid1(rs->raid_type) &&
1916 (le32_to_cpu(sb->num_devices) != mddev->raid_disks)) {
1917 DMERR("Reshaping raid sets not yet supported. (device count change from %u to %u)",
1918 sb->num_devices, mddev->raid_disks);
1919 return -EINVAL;
1920 }
1921
1922 /* Table line is checked vs. authoritative superblock */
1923 rs_set_new(rs);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001924 }
1925
Mike Snitzer42863252016-06-02 12:27:46 -04001926 if (!test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001927 mddev->recovery_cp = le64_to_cpu(sb->array_resync_offset);
1928
1929 /*
1930 * During load, we set FirstUse if a new superblock was written.
1931 * There are two reasons we might not have a superblock:
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001932 * 1) The raid set is brand new - in which case, all of the
Mike Snitzer43157842016-05-30 13:03:37 -04001933 * devices must have their In_sync bit set. Also,
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001934 * recovery_cp must be 0, unless forced.
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001935 * 2) This is a new device being added to an old raid set
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001936 * and the new device needs to be rebuilt - in which
1937 * case the In_sync bit will /not/ be set and
1938 * recovery_cp must be MaxSector.
1939 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001940 d = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001941 rdev_for_each(r, mddev) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001942 if (test_bit(FirstUse, &r->flags))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001943 new_devs++;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001944
1945 if (!test_bit(In_sync, &r->flags)) {
1946 DMINFO("Device %d specified for rebuild; clearing superblock",
1947 r->raid_disk);
1948 rebuilds++;
1949
1950 if (test_bit(FirstUse, &r->flags))
1951 rebuild_and_new++;
1952 }
1953
1954 d++;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001955 }
1956
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001957 if (new_devs == rs->raid_disks || !rebuilds) {
1958 /* Replace a broken device */
1959 if (new_devs == 1 && !rs->delta_disks)
1960 ;
1961 if (new_devs == rs->raid_disks) {
1962 DMINFO("Superblocks created for new raid set");
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001963 set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
Mike Snitzer42863252016-06-02 12:27:46 -04001964 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001965 mddev->recovery_cp = 0;
1966 } else if (new_devs && new_devs != rs->raid_disks && !rebuilds) {
1967 DMERR("New device injected into existing raid set without "
1968 "'delta_disks' or 'rebuild' parameter specified");
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001969 return -EINVAL;
1970 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001971 } else if (new_devs && new_devs != rebuilds) {
1972 DMERR("%u 'rebuild' devices cannot be injected into"
1973 " a raid set with %u other first-time devices",
1974 rebuilds, new_devs);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001975 return -EINVAL;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001976 } else if (rebuilds) {
1977 if (rebuild_and_new && rebuilds != rebuild_and_new) {
1978 DMERR("new device%s provided without 'rebuild'",
1979 new_devs > 1 ? "s" : "");
1980 return -EINVAL;
1981 } else if (mddev->recovery_cp != MaxSector) {
1982 DMERR("'rebuild' specified while raid set is not in-sync (recovery_cp=%llu)",
1983 (unsigned long long) mddev->recovery_cp);
1984 return -EINVAL;
1985 } else if (mddev->reshape_position != MaxSector) {
1986 DMERR("'rebuild' specified while raid set is being reshaped");
1987 return -EINVAL;
1988 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001989 }
1990
1991 /*
1992 * Now we set the Faulty bit for those devices that are
1993 * recorded in the superblock as failed.
1994 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001995 sb_retrieve_failed_devices(sb, failed_devices);
NeilBrowndafb20f2012-03-19 12:46:39 +11001996 rdev_for_each(r, mddev) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001997 if (!r->sb_page)
1998 continue;
1999 sb2 = page_address(r->sb_page);
2000 sb2->failed_devices = 0;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002001 memset(sb2->extended_failed_devices, 0, sizeof(sb2->extended_failed_devices));
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002002
2003 /*
2004 * Check for any device re-ordering.
2005 */
2006 if (!test_bit(FirstUse, &r->flags) && (r->raid_disk >= 0)) {
2007 role = le32_to_cpu(sb2->array_position);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002008 if (role < 0)
2009 continue;
2010
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002011 if (role != r->raid_disk) {
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002012 if (__is_raid10_near(mddev->layout)) {
2013 if (mddev->raid_disks % __raid10_near_copies(mddev->layout) ||
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002014 rs->raid_disks % rs->raid10_copies) {
2015 rs->ti->error =
2016 "Cannot change raid10 near set to odd # of devices!";
2017 return -EINVAL;
2018 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002019
2020 sb2->array_position = cpu_to_le32(r->raid_disk);
2021
2022 } else if (!(rs_is_raid10(rs) && rt_is_raid0(rs->raid_type)) &&
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002023 !(rs_is_raid0(rs) && rt_is_raid10(rs->raid_type)) &&
2024 !rt_is_raid1(rs->raid_type)) {
2025 rs->ti->error = "Cannot change device positions in raid set";
2026 return -EINVAL;
2027 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002028
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002029 DMINFO("raid device #%d now at position #%d", role, r->raid_disk);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002030 }
2031
2032 /*
2033 * Partial recovery is performed on
2034 * returning failed devices.
2035 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002036 if (test_bit(role, (void *) failed_devices))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002037 set_bit(Faulty, &r->flags);
2038 }
2039 }
2040
2041 return 0;
2042}
2043
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002044static int super_validate(struct raid_set *rs, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002045{
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002046 struct mddev *mddev = &rs->md;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002047 struct dm_raid_superblock *sb;
2048
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002049 if (rs_is_raid0(rs) || !rdev->sb_page)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002050 return 0;
2051
2052 sb = page_address(rdev->sb_page);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002053
2054 /*
2055 * If mddev->events is not set, we know we have not yet initialized
2056 * the array.
2057 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002058 if (!mddev->events && super_init_validation(rs, rdev))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002059 return -EINVAL;
2060
Mike Snitzer9b6e5422016-06-02 11:48:09 -04002061 if (le32_to_cpu(sb->compat_features) != FEATURE_FLAG_SUPPORTS_V190) {
2062 rs->ti->error = "Unable to assemble array: Unknown flag(s) in compatible feature flags";
2063 return -EINVAL;
2064 }
2065
2066 if (sb->incompat_features) {
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002067 rs->ti->error = "Unable to assemble array: No incompatible feature flags supported yet";
Heinz Mauelshagen4c9971c2016-04-29 18:59:56 +02002068 return -EINVAL;
2069 }
2070
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002071 /* Enable bitmap creation for RAID levels != 0 */
Heinz Mauelshagen676fa5a2016-05-19 18:49:29 +02002072 mddev->bitmap_info.offset = rt_is_raid0(rs->raid_type) ? 0 : to_sector(4096);
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002073 rdev->mddev->bitmap_info.default_offset = mddev->bitmap_info.offset;
2074
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002075 if (!test_and_clear_bit(FirstUse, &rdev->flags)) {
2076 /* Retrieve device size stored in superblock to be prepared for shrink */
2077 rdev->sectors = le64_to_cpu(sb->sectors);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002078 rdev->recovery_offset = le64_to_cpu(sb->disk_recovery_offset);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002079 if (rdev->recovery_offset == MaxSector)
2080 set_bit(In_sync, &rdev->flags);
2081 /*
2082 * If no reshape in progress -> we're recovering single
2083 * disk(s) and have to set the device(s) to out-of-sync
2084 */
2085 else if (rs->md.reshape_position == MaxSector)
2086 clear_bit(In_sync, &rdev->flags); /* Mandatory for recovery */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002087 }
2088
2089 /*
2090 * If a device comes back, set it as not In_sync and no longer faulty.
2091 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002092 if (test_and_clear_bit(Faulty, &rdev->flags)) {
2093 rdev->recovery_offset = 0;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002094 clear_bit(In_sync, &rdev->flags);
2095 rdev->saved_raid_disk = rdev->raid_disk;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002096 }
2097
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002098 /* Reshape support -> restore repective data offsets */
2099 rdev->data_offset = le64_to_cpu(sb->data_offset);
2100 rdev->new_data_offset = le64_to_cpu(sb->new_data_offset);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002101
2102 return 0;
2103}
2104
2105/*
2106 * Analyse superblocks and select the freshest.
2107 */
2108static int analyse_superblocks(struct dm_target *ti, struct raid_set *rs)
2109{
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002110 int r;
Jonathan E Brassow04475682012-03-28 18:41:26 +01002111 struct raid_dev *dev;
Jonathan Brassowa9ad8522012-04-24 10:23:13 +10002112 struct md_rdev *rdev, *tmp, *freshest;
NeilBrownfd01b882011-10-11 16:47:53 +11002113 struct mddev *mddev = &rs->md;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002114
2115 freshest = NULL;
Jonathan Brassowa9ad8522012-04-24 10:23:13 +10002116 rdev_for_each_safe(rdev, tmp, mddev) {
Jonathan Brassow761becf2012-10-11 13:42:19 +11002117 /*
Heinz Mauelshagenc76d53f2015-04-29 14:03:00 +02002118 * Skipping super_load due to CTR_FLAG_SYNC will cause
Jonathan Brassow761becf2012-10-11 13:42:19 +11002119 * the array to undergo initialization again as
Mike Snitzer43157842016-05-30 13:03:37 -04002120 * though it were new. This is the intended effect
Jonathan Brassow761becf2012-10-11 13:42:19 +11002121 * of the "sync" directive.
2122 *
2123 * When reshaping capability is added, we must ensure
2124 * that the "sync" directive is disallowed during the
2125 * reshape.
2126 */
Mike Snitzer42863252016-06-02 12:27:46 -04002127 if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags))
Jonathan Brassow761becf2012-10-11 13:42:19 +11002128 continue;
2129
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002130 if (!rdev->meta_bdev)
2131 continue;
2132
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002133 r = super_load(rdev, freshest);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002134
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002135 switch (r) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002136 case 1:
2137 freshest = rdev;
2138 break;
2139 case 0:
2140 break;
2141 default:
Jonathan E Brassow04475682012-03-28 18:41:26 +01002142 dev = container_of(rdev, struct raid_dev, rdev);
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002143 if (dev->meta_dev)
2144 dm_put_device(ti, dev->meta_dev);
Jonathan E Brassow04475682012-03-28 18:41:26 +01002145
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002146 dev->meta_dev = NULL;
2147 rdev->meta_bdev = NULL;
Jonathan E Brassow04475682012-03-28 18:41:26 +01002148
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002149 if (rdev->sb_page)
2150 put_page(rdev->sb_page);
Jonathan E Brassow04475682012-03-28 18:41:26 +01002151
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002152 rdev->sb_page = NULL;
Jonathan E Brassow04475682012-03-28 18:41:26 +01002153
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002154 rdev->sb_loaded = 0;
Jonathan E Brassow04475682012-03-28 18:41:26 +01002155
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002156 /*
2157 * We might be able to salvage the data device
2158 * even though the meta device has failed. For
2159 * now, we behave as though '- -' had been
2160 * set for this device in the table.
2161 */
2162 if (dev->data_dev)
2163 dm_put_device(ti, dev->data_dev);
Jonathan E Brassow04475682012-03-28 18:41:26 +01002164
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002165 dev->data_dev = NULL;
2166 rdev->bdev = NULL;
Jonathan E Brassow04475682012-03-28 18:41:26 +01002167
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002168 list_del(&rdev->same_set);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002169 }
2170 }
2171
2172 if (!freshest)
2173 return 0;
2174
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002175 if (validate_raid_redundancy(rs)) {
2176 rs->ti->error = "Insufficient redundancy to activate array";
2177 return -EINVAL;
2178 }
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002179
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002180 /*
2181 * Validation of the freshest device provides the source of
2182 * validation for the remaining devices.
2183 */
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002184 if (super_validate(rs, freshest)) {
2185 rs->ti->error = "Unable to assemble array: Invalid superblocks";
2186 return -EINVAL;
2187 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002188
NeilBrowndafb20f2012-03-19 12:46:39 +11002189 rdev_for_each(rdev, mddev)
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002190 if ((rdev != freshest) && super_validate(rs, rdev))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002191 return -EINVAL;
2192
2193 return 0;
2194}
2195
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002196/*
2197 * Adjust data_offset and new_data_offset on all disk members of @rs
2198 * for out of place reshaping if requested by contructor
2199 *
2200 * We need free space at the beginning of each raid disk for forward
2201 * and at the end for backward reshapes which userspace has to provide
2202 * via remapping/reordering of space.
2203 */
2204static int rs_adjust_data_offsets(struct raid_set *rs)
2205{
2206 sector_t data_offset = 0, new_data_offset = 0;
2207 struct md_rdev *rdev;
2208
2209 /* Constructor did not request data offset change */
2210 if (!test_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags)) {
2211 if (!rs_is_reshapable(rs))
2212 goto out;
2213
2214 return 0;
2215 }
2216
2217 /* HM FIXME: get InSync raid_dev? */
2218 rdev = &rs->dev[0].rdev;
2219
2220 if (rs->delta_disks < 0) {
2221 /*
2222 * Removing disks (reshaping backwards):
2223 *
2224 * - before reshape: data is at offset 0 and free space
2225 * is at end of each component LV
2226 *
2227 * - after reshape: data is at offset rs->data_offset != 0 on each component LV
2228 */
2229 data_offset = 0;
2230 new_data_offset = rs->data_offset;
2231
2232 } else if (rs->delta_disks > 0) {
2233 /*
2234 * Adding disks (reshaping forwards):
2235 *
2236 * - before reshape: data is at offset rs->data_offset != 0 and
2237 * free space is at begin of each component LV
2238 *
2239 * - after reshape: data is at offset 0 on each component LV
2240 */
2241 data_offset = rs->data_offset;
2242 new_data_offset = 0;
2243
2244 } else {
2245 /*
2246 * User space passes in 0 for data offset after having removed reshape space
2247 *
2248 * - or - (data offset != 0)
2249 *
2250 * Changing RAID layout or chunk size -> toggle offsets
2251 *
2252 * - before reshape: data is at offset rs->data_offset 0 and
2253 * free space is at end of each component LV
2254 * -or-
2255 * data is at offset rs->data_offset != 0 and
2256 * free space is at begin of each component LV
2257 *
2258 * - after reshape: data is at offset 0 if i was at offset != 0
2259 * of at offset != 0 if it was at offset 0
2260 * on each component LV
2261 *
2262 */
2263 data_offset = rs->data_offset ? rdev->data_offset : 0;
2264 new_data_offset = data_offset ? 0 : rs->data_offset;
2265 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
2266 }
2267
2268 /*
2269 * Make sure we got a minimum amount of free sectors per device
2270 */
2271 if (rs->data_offset &&
2272 to_sector(i_size_read(rdev->bdev->bd_inode)) - rdev->sectors < MIN_FREE_RESHAPE_SPACE) {
2273 rs->ti->error = data_offset ? "No space for forward reshape" :
2274 "No space for backward reshape";
2275 return -ENOSPC;
2276 }
2277out:
2278 /* Adjust data offsets on all rdevs */
2279 rdev_for_each(rdev, &rs->md) {
2280 rdev->data_offset = data_offset;
2281 rdev->new_data_offset = new_data_offset;
2282 }
2283
2284 return 0;
2285}
2286
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002287/* Userpace reordered disks -> adjust raid_disk indexes in @rs */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002288static void __reorder_raid_disk_indexes(struct raid_set *rs)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002289{
2290 int i = 0;
2291 struct md_rdev *rdev;
2292
2293 rdev_for_each(rdev, &rs->md) {
2294 rdev->raid_disk = i++;
2295 rdev->saved_raid_disk = rdev->new_raid_disk = -1;
2296 }
2297}
2298
2299/*
2300 * Setup @rs for takeover by a different raid level
2301 */
2302static int rs_setup_takeover(struct raid_set *rs)
2303{
2304 struct mddev *mddev = &rs->md;
2305 struct md_rdev *rdev;
2306 unsigned int d = mddev->raid_disks = rs->raid_disks;
2307 sector_t new_data_offset = rs->dev[0].rdev.data_offset ? 0 : rs->data_offset;
2308
2309 if (rt_is_raid10(rs->raid_type)) {
2310 if (mddev->level == 0) {
2311 /* Userpace reordered disks -> adjust raid_disk indexes */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002312 __reorder_raid_disk_indexes(rs);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002313
2314 /* raid0 -> raid10_far layout */
2315 mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_FAR,
2316 rs->raid10_copies);
2317 } else if (mddev->level == 1)
2318 /* raid1 -> raid10_near layout */
2319 mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_NEAR,
2320 rs->raid_disks);
2321 else
2322 return -EINVAL;
2323
2324 }
2325
2326 clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
2327 mddev->recovery_cp = MaxSector;
2328
2329 while (d--) {
2330 rdev = &rs->dev[d].rdev;
2331
2332 if (test_bit(d, (void *) rs->rebuild_disks)) {
2333 clear_bit(In_sync, &rdev->flags);
2334 clear_bit(Faulty, &rdev->flags);
2335 mddev->recovery_cp = rdev->recovery_offset = 0;
2336 /* Bitmap has to be created when we do an "up" takeover */
2337 set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
2338 }
2339
2340 rdev->new_data_offset = new_data_offset;
2341 }
2342
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002343 return 0;
2344}
2345
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002346/*
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002347 * Enable/disable discard support on RAID set depending on
2348 * RAID level and discard properties of underlying RAID members.
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002349 */
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002350static void configure_discard_support(struct raid_set *rs)
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002351{
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002352 int i;
2353 bool raid456;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002354 struct dm_target *ti = rs->ti;
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002355
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002356 /* Assume discards not supported until after checks below. */
2357 ti->discards_supported = false;
2358
2359 /* RAID level 4,5,6 require discard_zeroes_data for data integrity! */
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002360 raid456 = (rs->md.level == 4 || rs->md.level == 5 || rs->md.level == 6);
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002361
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002362 for (i = 0; i < rs->md.raid_disks; i++) {
Heinz Mauelshagend20c4b02014-10-29 19:02:27 +01002363 struct request_queue *q;
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002364
Heinz Mauelshagend20c4b02014-10-29 19:02:27 +01002365 if (!rs->dev[i].rdev.bdev)
2366 continue;
2367
2368 q = bdev_get_queue(rs->dev[i].rdev.bdev);
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002369 if (!q || !blk_queue_discard(q))
2370 return;
2371
2372 if (raid456) {
2373 if (!q->limits.discard_zeroes_data)
2374 return;
2375 if (!devices_handle_discard_safely) {
2376 DMERR("raid456 discard support disabled due to discard_zeroes_data uncertainty.");
2377 DMERR("Set dm-raid.devices_handle_discard_safely=Y to override.");
2378 return;
2379 }
2380 }
2381 }
2382
2383 /* All RAID members properly support discards */
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002384 ti->discards_supported = true;
2385
2386 /*
2387 * RAID1 and RAID10 personalities require bio splitting,
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002388 * RAID0/4/5/6 don't and process large discard bios properly.
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002389 */
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002390 ti->split_discard_bios = !!(rs->md.level == 1 || rs->md.level == 10);
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002391 ti->num_discard_bios = 1;
2392}
2393
2394/*
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002395 * Construct a RAID0/1/10/4/5/6 mapping:
NeilBrown9d09e662011-01-13 20:00:02 +00002396 * Args:
Mike Snitzer43157842016-05-30 13:03:37 -04002397 * <raid_type> <#raid_params> <raid_params>{0,} \
2398 * <#raid_devs> [<meta_dev1> <dev1>]{1,}
NeilBrown9d09e662011-01-13 20:00:02 +00002399 *
Mike Snitzer43157842016-05-30 13:03:37 -04002400 * <raid_params> varies by <raid_type>. See 'parse_raid_params' for
NeilBrown9d09e662011-01-13 20:00:02 +00002401 * details on possible <raid_params>.
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002402 *
2403 * Userspace is free to initialize the metadata devices, hence the superblocks to
2404 * enforce recreation based on the passed in table parameters.
2405 *
NeilBrown9d09e662011-01-13 20:00:02 +00002406 */
2407static int raid_ctr(struct dm_target *ti, unsigned argc, char **argv)
2408{
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002409 int r;
NeilBrown9d09e662011-01-13 20:00:02 +00002410 struct raid_type *rt;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002411 unsigned num_raid_params, num_raid_devs;
NeilBrown9d09e662011-01-13 20:00:02 +00002412 struct raid_set *rs = NULL;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002413 const char *arg;
2414 struct dm_arg_set as = { argc, argv }, as_nrd;
2415 struct dm_arg _args[] = {
2416 { 0, as.argc, "Cannot understand number of raid parameters" },
2417 { 1, 254, "Cannot understand number of raid devices parameters" }
2418 };
NeilBrown9d09e662011-01-13 20:00:02 +00002419
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002420 /* Must have <raid_type> */
2421 arg = dm_shift_arg(&as);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002422 if (!arg) {
2423 ti->error = "No arguments";
2424 return -EINVAL;
2425 }
NeilBrown9d09e662011-01-13 20:00:02 +00002426
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002427 rt = get_raid_type(arg);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002428 if (!rt) {
2429 ti->error = "Unrecognised raid_type";
2430 return -EINVAL;
2431 }
NeilBrown9d09e662011-01-13 20:00:02 +00002432
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002433 /* Must have <#raid_params> */
2434 if (dm_read_arg_group(_args, &as, &num_raid_params, &ti->error))
Mike Snitzer43157842016-05-30 13:03:37 -04002435 return -EINVAL;
NeilBrown9d09e662011-01-13 20:00:02 +00002436
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002437 /* number of raid device tupples <meta_dev data_dev> */
2438 as_nrd = as;
2439 dm_consume_args(&as_nrd, num_raid_params);
2440 _args[1].max = (as_nrd.argc - 1) / 2;
2441 if (dm_read_arg(_args + 1, &as_nrd, &num_raid_devs, &ti->error))
Mike Snitzer43157842016-05-30 13:03:37 -04002442 return -EINVAL;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002443
Mike Snitzerbb91a632016-06-02 12:06:54 -04002444 if (!__within_range(num_raid_devs, 1, MAX_RAID_DEVICES)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002445 ti->error = "Invalid number of supplied raid devices";
2446 return -EINVAL;
2447 }
NeilBrown9d09e662011-01-13 20:00:02 +00002448
Mike Snitzerbfcee0e2016-06-02 15:08:09 -04002449 rs = raid_set_alloc(ti, rt, num_raid_devs);
NeilBrown9d09e662011-01-13 20:00:02 +00002450 if (IS_ERR(rs))
2451 return PTR_ERR(rs);
2452
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002453 r = parse_raid_params(rs, &as, num_raid_params);
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002454 if (r)
NeilBrown9d09e662011-01-13 20:00:02 +00002455 goto bad;
2456
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02002457 r = parse_dev_params(rs, &as);
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002458 if (r)
NeilBrown9d09e662011-01-13 20:00:02 +00002459 goto bad;
2460
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002461 rs->md.sync_super = super_sync;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002462
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002463 r = rs_set_dev_and_array_sectors(rs, false);
2464 if (r)
2465 return r;
2466
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002467 /*
2468 * Backup any new raid set level, layout, ...
2469 * requested to be able to compare to superblock
2470 * members for conversion decisions.
2471 */
2472 rs_config_backup(rs);
2473
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002474 r = analyse_superblocks(ti, rs);
2475 if (r)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002476 goto bad;
2477
NeilBrown9d09e662011-01-13 20:00:02 +00002478 INIT_WORK(&rs->md.event_work, do_table_event);
NeilBrown9d09e662011-01-13 20:00:02 +00002479 ti->private = rs;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002480 ti->num_flush_bios = 1;
NeilBrown9d09e662011-01-13 20:00:02 +00002481
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002482 /* Restore any requested new layout for conversion decision */
2483 rs_config_restore(rs);
2484
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002485 /*
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002486 * If a takeover is needed, just set the level to
2487 * the new requested one and allow the raid set to run.
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002488 */
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002489 if (rs_takeover_requested(rs)) {
2490 r = rs_check_takeover(rs);
2491 if (r)
2492 return r;
2493
2494 r = rs_setup_takeover(rs);
2495 if (r)
2496 return r;
2497
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002498 /* Tell preresume to update superblocks with new layout */
Mike Snitzer42863252016-06-02 12:27:46 -04002499 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002500 rs_set_new(rs);
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002501 } else if (rs_reshape_requested(rs)) {
2502 rs_set_cur(rs); /* Dummy to reject, fill in */
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002503 } else
2504 rs_set_cur(rs);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002505
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002506 /* If constructor requested it, change data and new_data offsets */
2507 r = rs_adjust_data_offsets(rs);
2508 if (r)
2509 return r;
2510
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002511 /* Start raid set read-only and assumed clean to change in raid_resume() */
2512 rs->md.ro = 1;
2513 rs->md.in_sync = 1;
2514 set_bit(MD_RECOVERY_FROZEN, &rs->md.recovery);
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002515
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002516 /* Has to be held on running the array */
2517 mddev_lock_nointr(&rs->md);
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002518 r = md_run(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00002519 rs->md.in_sync = 0; /* Assume already marked dirty */
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002520 mddev_unlock(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00002521
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002522 if (r) {
NeilBrown9d09e662011-01-13 20:00:02 +00002523 ti->error = "Fail to run raid array";
2524 goto bad;
2525 }
2526
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05002527 if (ti->len != rs->md.array_sectors) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002528 ti->error = "Array size does not match requested target length";
2529 r = -EINVAL;
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05002530 goto size_mismatch;
2531 }
NeilBrown9d09e662011-01-13 20:00:02 +00002532 rs->callbacks.congested_fn = raid_is_congested;
NeilBrown9d09e662011-01-13 20:00:02 +00002533 dm_table_add_target_callbacks(ti->table, &rs->callbacks);
2534
Jonathan Brassow32737272011-08-02 12:32:07 +01002535 mddev_suspend(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00002536 return 0;
2537
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05002538size_mismatch:
2539 md_stop(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00002540bad:
Mike Snitzerbfcee0e2016-06-02 15:08:09 -04002541 raid_set_free(rs);
NeilBrown9d09e662011-01-13 20:00:02 +00002542
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002543 return r;
NeilBrown9d09e662011-01-13 20:00:02 +00002544}
2545
2546static void raid_dtr(struct dm_target *ti)
2547{
2548 struct raid_set *rs = ti->private;
2549
2550 list_del_init(&rs->callbacks.list);
2551 md_stop(&rs->md);
Mike Snitzerbfcee0e2016-06-02 15:08:09 -04002552 raid_set_free(rs);
NeilBrown9d09e662011-01-13 20:00:02 +00002553}
2554
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002555static int raid_map(struct dm_target *ti, struct bio *bio)
NeilBrown9d09e662011-01-13 20:00:02 +00002556{
2557 struct raid_set *rs = ti->private;
NeilBrownfd01b882011-10-11 16:47:53 +11002558 struct mddev *mddev = &rs->md;
NeilBrown9d09e662011-01-13 20:00:02 +00002559
2560 mddev->pers->make_request(mddev, bio);
2561
2562 return DM_MAPIO_SUBMITTED;
2563}
2564
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002565/* Return string describing the current sync action of @mddev */
Jonathan Brassowbe836512013-04-24 11:42:43 +10002566static const char *decipher_sync_action(struct mddev *mddev)
2567{
2568 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
2569 return "frozen";
2570
2571 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2572 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
2573 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2574 return "reshape";
2575
2576 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2577 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2578 return "resync";
2579 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2580 return "check";
2581 return "repair";
2582 }
2583
2584 if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
2585 return "recover";
2586 }
2587
2588 return "idle";
2589}
2590
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002591/*
2592 * Return status string @rdev
2593 *
2594 * Status characters:
2595 *
2596 * 'D' = Dead/Failed device
2597 * 'a' = Alive but not in-sync
2598 * 'A' = Alive and in-sync
2599 */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002600static const char *__raid_dev_status(struct md_rdev *rdev, bool array_in_sync)
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002601{
2602 if (test_bit(Faulty, &rdev->flags))
2603 return "D";
2604 else if (!array_in_sync || !test_bit(In_sync, &rdev->flags))
2605 return "a";
2606 else
2607 return "A";
2608}
2609
2610/* Helper to return resync/reshape progress for @rs and @array_in_sync */
2611static sector_t rs_get_progress(struct raid_set *rs,
2612 sector_t resync_max_sectors, bool *array_in_sync)
2613{
2614 sector_t r, recovery_cp, curr_resync_completed;
2615 struct mddev *mddev = &rs->md;
2616
2617 curr_resync_completed = mddev->curr_resync_completed ?: mddev->recovery_cp;
2618 recovery_cp = mddev->recovery_cp;
2619 *array_in_sync = false;
2620
2621 if (rs_is_raid0(rs)) {
2622 r = resync_max_sectors;
2623 *array_in_sync = true;
2624
2625 } else {
2626 r = mddev->reshape_position;
2627
2628 /* Reshape is relative to the array size */
2629 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
2630 r != MaxSector) {
2631 if (r == MaxSector) {
2632 *array_in_sync = true;
2633 r = resync_max_sectors;
2634 } else {
2635 /* Got to reverse on backward reshape */
2636 if (mddev->reshape_backwards)
2637 r = mddev->array_sectors - r;
2638
2639 /* Devide by # of data stripes */
2640 sector_div(r, mddev_data_stripes(rs));
2641 }
2642
2643 /* Sync is relative to the component device size */
2644 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2645 r = curr_resync_completed;
2646 else
2647 r = recovery_cp;
2648
2649 if (r == MaxSector) {
2650 /*
2651 * Sync complete.
2652 */
2653 *array_in_sync = true;
2654 r = resync_max_sectors;
2655 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2656 /*
2657 * If "check" or "repair" is occurring, the raid set has
2658 * undergone an initial sync and the health characters
2659 * should not be 'a' anymore.
2660 */
2661 *array_in_sync = true;
2662 } else {
2663 struct md_rdev *rdev;
2664
2665 /*
2666 * The raid set may be doing an initial sync, or it may
Mike Snitzer43157842016-05-30 13:03:37 -04002667 * be rebuilding individual components. If all the
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002668 * devices are In_sync, then it is the raid set that is
2669 * being initialized.
2670 */
2671 rdev_for_each(rdev, mddev)
2672 if (!test_bit(In_sync, &rdev->flags))
2673 *array_in_sync = true;
2674#if 0
2675 r = 0; /* HM FIXME: TESTME: https://bugzilla.redhat.com/show_bug.cgi?id=1210637 ? */
2676#endif
2677 }
2678 }
2679
2680 return r;
2681}
2682
2683/* Helper to return @dev name or "-" if !@dev */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002684static const char *__get_dev_name(struct dm_dev *dev)
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002685{
2686 return dev ? dev->name : "-";
2687}
2688
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002689static void raid_status(struct dm_target *ti, status_type_t type,
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002690 unsigned int status_flags, char *result, unsigned int maxlen)
NeilBrown9d09e662011-01-13 20:00:02 +00002691{
2692 struct raid_set *rs = ti->private;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002693 struct mddev *mddev = &rs->md;
2694 struct r5conf *conf = mddev->private;
2695 int max_nr_stripes = conf ? conf->max_nr_stripes : 0;
2696 bool array_in_sync;
2697 unsigned int raid_param_cnt = 1; /* at least 1 for chunksize */
2698 unsigned int sz = 0;
2699 unsigned int write_mostly_params = 0;
2700 sector_t progress, resync_max_sectors, resync_mismatches;
2701 const char *sync_action;
2702 struct raid_type *rt;
2703 struct md_rdev *rdev;
NeilBrown9d09e662011-01-13 20:00:02 +00002704
2705 switch (type) {
2706 case STATUSTYPE_INFO:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002707 /* *Should* always succeed */
2708 rt = get_raid_type_by_ll(mddev->new_level, mddev->new_layout);
2709 if (!rt)
2710 return;
NeilBrown9d09e662011-01-13 20:00:02 +00002711
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002712 DMEMIT("%s %d ", rt ? rt->name : "unknown", mddev->raid_disks);
NeilBrown9d09e662011-01-13 20:00:02 +00002713
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002714 /* Access most recent mddev properties for status output */
2715 smp_rmb();
2716 /* Get sensible max sectors even if raid set not yet started */
Mike Snitzer42863252016-06-02 12:27:46 -04002717 resync_max_sectors = test_bit(RT_FLAG_RS_PRERESUMED, &rs->runtime_flags) ?
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002718 mddev->resync_max_sectors : mddev->dev_sectors;
2719 progress = rs_get_progress(rs, resync_max_sectors, &array_in_sync);
2720 resync_mismatches = (mddev->last_sync_action && !strcasecmp(mddev->last_sync_action, "check")) ?
2721 (unsigned int) atomic64_read(&mddev->resync_mismatches) : 0;
2722 sync_action = decipher_sync_action(&rs->md);
2723
2724 /* HM FIXME: do we want another state char for raid0? It shows 'D' or 'A' now */
2725 rdev_for_each(rdev, mddev)
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002726 DMEMIT(__raid_dev_status(rdev, array_in_sync));
Jonathan Brassowbe836512013-04-24 11:42:43 +10002727
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00002728 /*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002729 * In-sync/Reshape ratio:
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00002730 * The in-sync ratio shows the progress of:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002731 * - Initializing the raid set
2732 * - Rebuilding a subset of devices of the raid set
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00002733 * The user can distinguish between the two by referring
2734 * to the status characters.
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002735 *
2736 * The reshape ratio shows the progress of
2737 * changing the raid layout or the number of
2738 * disks of a raid set
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00002739 */
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002740 DMEMIT(" %llu/%llu", (unsigned long long) progress,
2741 (unsigned long long) resync_max_sectors);
NeilBrown9d09e662011-01-13 20:00:02 +00002742
Jonathan Brassowbe836512013-04-24 11:42:43 +10002743 /*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002744 * v1.5.0+:
2745 *
Jonathan Brassowbe836512013-04-24 11:42:43 +10002746 * Sync action:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002747 * See Documentation/device-mapper/dm-raid.txt for
Jonathan Brassowbe836512013-04-24 11:42:43 +10002748 * information on each of these states.
2749 */
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002750 DMEMIT(" %s", sync_action);
Jonathan Brassowbe836512013-04-24 11:42:43 +10002751
2752 /*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002753 * v1.5.0+:
2754 *
Jonathan Brassowbe836512013-04-24 11:42:43 +10002755 * resync_mismatches/mismatch_cnt
2756 * This field shows the number of discrepancies found when
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002757 * performing a "check" of the raid set.
Jonathan Brassowbe836512013-04-24 11:42:43 +10002758 */
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002759 DMEMIT(" %llu", (unsigned long long) resync_mismatches);
2760
2761 /*
Mike Snitzer9b6e5422016-06-02 11:48:09 -04002762 * v1.9.0+:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002763 *
2764 * data_offset (needed for out of space reshaping)
2765 * This field shows the data offset into the data
2766 * image LV where the first stripes data starts.
2767 *
2768 * We keep data_offset equal on all raid disks of the set,
2769 * so retrieving it from the first raid disk is sufficient.
2770 */
2771 DMEMIT(" %llu", (unsigned long long) rs->dev[0].rdev.data_offset);
NeilBrown9d09e662011-01-13 20:00:02 +00002772 break;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002773
NeilBrown9d09e662011-01-13 20:00:02 +00002774 case STATUSTYPE_TABLE:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002775 /* Report the table line string you would use to construct this raid set */
NeilBrown9d09e662011-01-13 20:00:02 +00002776
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002777 /* Calculate raid parameter count */
2778 rdev_for_each(rdev, mddev)
2779 if (test_bit(WriteMostly, &rdev->flags))
2780 write_mostly_params += 2;
2781 raid_param_cnt += memweight(rs->rebuild_disks,
2782 DISKS_ARRAY_ELEMS * sizeof(*rs->rebuild_disks)) * 2 +
2783 write_mostly_params +
2784 hweight32(rs->ctr_flags & CTR_FLAG_OPTIONS_NO_ARGS) +
2785 hweight32(rs->ctr_flags & CTR_FLAG_OPTIONS_ONE_ARG) * 2;
2786 /* Emit table line */
2787 DMEMIT("%s %u %u", rs->raid_type->name, raid_param_cnt, mddev->new_chunk_sectors);
Mike Snitzer42863252016-06-02 12:27:46 -04002788 if (test_bit(__CTR_FLAG_RAID10_FORMAT, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04002789 DMEMIT(" %s %s", dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_FORMAT),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002790 raid10_md_layout_to_format(mddev->layout));
Mike Snitzer42863252016-06-02 12:27:46 -04002791 if (test_bit(__CTR_FLAG_RAID10_COPIES, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04002792 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_COPIES),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002793 raid10_md_layout_to_copies(mddev->layout));
Mike Snitzer42863252016-06-02 12:27:46 -04002794 if (test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04002795 DMEMIT(" %s", dm_raid_arg_name_by_flag(CTR_FLAG_NOSYNC));
Mike Snitzer42863252016-06-02 12:27:46 -04002796 if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04002797 DMEMIT(" %s", dm_raid_arg_name_by_flag(CTR_FLAG_SYNC));
Mike Snitzer42863252016-06-02 12:27:46 -04002798 if (test_bit(__CTR_FLAG_REGION_SIZE, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04002799 DMEMIT(" %s %llu", dm_raid_arg_name_by_flag(CTR_FLAG_REGION_SIZE),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002800 (unsigned long long) to_sector(mddev->bitmap_info.chunksize));
Mike Snitzer42863252016-06-02 12:27:46 -04002801 if (test_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04002802 DMEMIT(" %s %llu", dm_raid_arg_name_by_flag(CTR_FLAG_DATA_OFFSET),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002803 (unsigned long long) rs->data_offset);
Mike Snitzer42863252016-06-02 12:27:46 -04002804 if (test_bit(__CTR_FLAG_DAEMON_SLEEP, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04002805 DMEMIT(" %s %lu", dm_raid_arg_name_by_flag(CTR_FLAG_DAEMON_SLEEP),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002806 mddev->bitmap_info.daemon_sleep);
Mike Snitzer42863252016-06-02 12:27:46 -04002807 if (test_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04002808 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_DELTA_DISKS),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002809 mddev->delta_disks);
Mike Snitzer42863252016-06-02 12:27:46 -04002810 if (test_bit(__CTR_FLAG_STRIPE_CACHE, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04002811 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_STRIPE_CACHE),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002812 max_nr_stripes);
2813 rdev_for_each(rdev, mddev)
2814 if (test_bit(rdev->raid_disk, (void *) rs->rebuild_disks))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04002815 DMEMIT(" %s %u", dm_raid_arg_name_by_flag(CTR_FLAG_REBUILD),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002816 rdev->raid_disk);
2817 rdev_for_each(rdev, mddev)
2818 if (test_bit(WriteMostly, &rdev->flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04002819 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_WRITE_MOSTLY),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002820 rdev->raid_disk);
Mike Snitzer42863252016-06-02 12:27:46 -04002821 if (test_bit(__CTR_FLAG_MAX_WRITE_BEHIND, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04002822 DMEMIT(" %s %lu", dm_raid_arg_name_by_flag(CTR_FLAG_MAX_WRITE_BEHIND),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002823 mddev->bitmap_info.max_write_behind);
Mike Snitzer42863252016-06-02 12:27:46 -04002824 if (test_bit(__CTR_FLAG_MAX_RECOVERY_RATE, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04002825 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_MAX_RECOVERY_RATE),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002826 mddev->sync_speed_max);
Mike Snitzer42863252016-06-02 12:27:46 -04002827 if (test_bit(__CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04002828 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_MIN_RECOVERY_RATE),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002829 mddev->sync_speed_min);
2830 DMEMIT(" %d", rs->raid_disks);
2831 rdev_for_each(rdev, mddev) {
2832 struct raid_dev *rd = container_of(rdev, struct raid_dev, rdev);
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05002833
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002834 DMEMIT(" %s %s", __get_dev_name(rd->meta_dev),
2835 __get_dev_name(rd->data_dev));
NeilBrown9d09e662011-01-13 20:00:02 +00002836 }
2837 }
NeilBrown9d09e662011-01-13 20:00:02 +00002838}
2839
Jonathan Brassowbe836512013-04-24 11:42:43 +10002840static int raid_message(struct dm_target *ti, unsigned argc, char **argv)
2841{
2842 struct raid_set *rs = ti->private;
2843 struct mddev *mddev = &rs->md;
2844
Jonathan Brassowbe836512013-04-24 11:42:43 +10002845 if (!mddev->pers || !mddev->pers->sync_request)
2846 return -EINVAL;
2847
2848 if (!strcasecmp(argv[0], "frozen"))
2849 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
2850 else
2851 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
2852
2853 if (!strcasecmp(argv[0], "idle") || !strcasecmp(argv[0], "frozen")) {
2854 if (mddev->sync_thread) {
2855 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2856 md_reap_sync_thread(mddev);
2857 }
2858 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2859 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
2860 return -EBUSY;
2861 else if (!strcasecmp(argv[0], "resync"))
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002862 ; /* MD_RECOVERY_NEEDED set below */
2863 else if (!strcasecmp(argv[0], "recover"))
Jonathan Brassowbe836512013-04-24 11:42:43 +10002864 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002865 else {
Jonathan Brassowbe836512013-04-24 11:42:43 +10002866 if (!strcasecmp(argv[0], "check"))
2867 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
2868 else if (!!strcasecmp(argv[0], "repair"))
2869 return -EINVAL;
2870 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2871 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
2872 }
2873 if (mddev->ro == 2) {
2874 /* A write to sync_action is enough to justify
2875 * canceling read-auto mode
2876 */
2877 mddev->ro = 0;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002878 if (!mddev->suspended && mddev->sync_thread)
Jonathan Brassowbe836512013-04-24 11:42:43 +10002879 md_wakeup_thread(mddev->sync_thread);
2880 }
2881 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002882 if (!mddev->suspended && mddev->thread)
Jonathan Brassowbe836512013-04-24 11:42:43 +10002883 md_wakeup_thread(mddev->thread);
2884
2885 return 0;
2886}
2887
2888static int raid_iterate_devices(struct dm_target *ti,
2889 iterate_devices_callout_fn fn, void *data)
NeilBrown9d09e662011-01-13 20:00:02 +00002890{
2891 struct raid_set *rs = ti->private;
2892 unsigned i;
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002893 int r = 0;
NeilBrown9d09e662011-01-13 20:00:02 +00002894
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002895 for (i = 0; !r && i < rs->md.raid_disks; i++)
NeilBrown9d09e662011-01-13 20:00:02 +00002896 if (rs->dev[i].data_dev)
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002897 r = fn(ti,
NeilBrown9d09e662011-01-13 20:00:02 +00002898 rs->dev[i].data_dev,
2899 0, /* No offset on data devs */
2900 rs->md.dev_sectors,
2901 data);
2902
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002903 return r;
NeilBrown9d09e662011-01-13 20:00:02 +00002904}
2905
2906static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits)
2907{
2908 struct raid_set *rs = ti->private;
2909 unsigned chunk_size = rs->md.chunk_sectors << 9;
NeilBrownd1688a62011-10-11 16:49:52 +11002910 struct r5conf *conf = rs->md.private;
NeilBrown9d09e662011-01-13 20:00:02 +00002911
2912 blk_limits_io_min(limits, chunk_size);
2913 blk_limits_io_opt(limits, chunk_size * (conf->raid_disks - conf->max_degraded));
2914}
2915
2916static void raid_presuspend(struct dm_target *ti)
2917{
2918 struct raid_set *rs = ti->private;
2919
2920 md_stop_writes(&rs->md);
2921}
2922
2923static void raid_postsuspend(struct dm_target *ti)
2924{
2925 struct raid_set *rs = ti->private;
2926
2927 mddev_suspend(&rs->md);
2928}
2929
Jonathan Brassowf381e712013-05-08 17:57:13 -05002930static void attempt_restore_of_faulty_devices(struct raid_set *rs)
NeilBrown9d09e662011-01-13 20:00:02 +00002931{
Jonathan Brassow9092c022013-05-02 14:19:24 -05002932 int i;
2933 uint64_t failed_devices, cleared_failed_devices = 0;
2934 unsigned long flags;
2935 struct dm_raid_superblock *sb;
Jonathan Brassow9092c022013-05-02 14:19:24 -05002936 struct md_rdev *r;
NeilBrown9d09e662011-01-13 20:00:02 +00002937
Jonathan Brassowf381e712013-05-08 17:57:13 -05002938 for (i = 0; i < rs->md.raid_disks; i++) {
2939 r = &rs->dev[i].rdev;
2940 if (test_bit(Faulty, &r->flags) && r->sb_page &&
Mike Christie796a5cf2016-06-05 14:32:07 -05002941 sync_page_io(r, 0, r->sb_size, r->sb_page, REQ_OP_READ, 0,
2942 1)) {
Jonathan Brassowf381e712013-05-08 17:57:13 -05002943 DMINFO("Faulty %s device #%d has readable super block."
2944 " Attempting to revive it.",
2945 rs->raid_type->name, i);
Jonathan Brassowa4dc1632013-05-08 18:00:54 -05002946
2947 /*
2948 * Faulty bit may be set, but sometimes the array can
2949 * be suspended before the personalities can respond
2950 * by removing the device from the array (i.e. calling
Mike Snitzer43157842016-05-30 13:03:37 -04002951 * 'hot_remove_disk'). If they haven't yet removed
Jonathan Brassowa4dc1632013-05-08 18:00:54 -05002952 * the failed device, its 'raid_disk' number will be
2953 * '>= 0' - meaning we must call this function
2954 * ourselves.
2955 */
2956 if ((r->raid_disk >= 0) &&
2957 (r->mddev->pers->hot_remove_disk(r->mddev, r) != 0))
2958 /* Failed to revive this device, try next */
2959 continue;
2960
Jonathan Brassowf381e712013-05-08 17:57:13 -05002961 r->raid_disk = i;
2962 r->saved_raid_disk = i;
2963 flags = r->flags;
2964 clear_bit(Faulty, &r->flags);
2965 clear_bit(WriteErrorSeen, &r->flags);
2966 clear_bit(In_sync, &r->flags);
2967 if (r->mddev->pers->hot_add_disk(r->mddev, r)) {
2968 r->raid_disk = -1;
2969 r->saved_raid_disk = -1;
2970 r->flags = flags;
2971 } else {
2972 r->recovery_offset = 0;
2973 cleared_failed_devices |= 1 << i;
2974 }
2975 }
2976 }
2977 if (cleared_failed_devices) {
2978 rdev_for_each(r, &rs->md) {
2979 sb = page_address(r->sb_page);
2980 failed_devices = le64_to_cpu(sb->failed_devices);
2981 failed_devices &= ~cleared_failed_devices;
2982 sb->failed_devices = cpu_to_le64(failed_devices);
2983 }
2984 }
2985}
2986
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002987static int __load_dirty_region_bitmap(struct raid_set *rs)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002988{
2989 int r = 0;
2990
2991 /* Try loading the bitmap unless "raid0", which does not have one */
2992 if (!rs_is_raid0(rs) &&
Mike Snitzer42863252016-06-02 12:27:46 -04002993 !test_and_set_bit(RT_FLAG_RS_BITMAP_LOADED, &rs->runtime_flags)) {
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002994 r = bitmap_load(&rs->md);
2995 if (r)
2996 DMERR("Failed to load bitmap");
2997 }
2998
2999 return r;
3000}
3001
3002static int raid_preresume(struct dm_target *ti)
3003{
3004 struct raid_set *rs = ti->private;
3005 struct mddev *mddev = &rs->md;
3006
3007 /* This is a resume after a suspend of the set -> it's already started */
Mike Snitzer42863252016-06-02 12:27:46 -04003008 if (test_and_set_bit(RT_FLAG_RS_PRERESUMED, &rs->runtime_flags))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003009 return 0;
3010
3011 /*
3012 * The superblocks need to be updated on disk if the
Mike Snitzere6ca5e12016-06-02 15:27:22 -04003013 * array is new or __load_dirty_region_bitmap will overwrite them
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003014 * in core with old data.
3015 *
3016 * In case the array got modified (takeover/reshape/resize)
3017 * or the data offsets on the component devices changed, they
3018 * have to be updated as well.
3019 *
3020 * Have to switch to readwrite and back in order to
3021 * allow for the superblock updates.
3022 */
Mike Snitzer42863252016-06-02 12:27:46 -04003023 if (test_and_clear_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags)) {
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003024 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3025 mddev->ro = 0;
3026 md_update_sb(mddev, 1);
3027 mddev->ro = 1;
3028 }
3029
3030 /*
3031 * Disable/enable discard support on raid set after any
3032 * conversion, because devices can have been added
3033 */
3034 configure_discard_support(rs);
3035
3036 /* Load the bitmap from disk unless raid0 */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04003037 return __load_dirty_region_bitmap(rs);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003038}
3039
Jonathan Brassowf381e712013-05-08 17:57:13 -05003040static void raid_resume(struct dm_target *ti)
3041{
3042 struct raid_set *rs = ti->private;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003043 struct mddev *mddev = &rs->md;
Jonathan Brassowf381e712013-05-08 17:57:13 -05003044
Mike Snitzer42863252016-06-02 12:27:46 -04003045 if (test_and_set_bit(RT_FLAG_RS_RESUMED, &rs->runtime_flags)) {
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003046 /*
3047 * A secondary resume while the device is active.
3048 * Take this opportunity to check whether any failed
3049 * devices are reachable again.
3050 */
3051 attempt_restore_of_faulty_devices(rs);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003052 }
Jonathan Brassow34f8ac6d2012-01-27 14:53:53 -06003053
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003054 mddev->ro = 0;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003055 mddev->in_sync = 0;
3056 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3057
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003058 if (mddev->suspended)
3059 mddev_resume(mddev);
NeilBrown9d09e662011-01-13 20:00:02 +00003060}
3061
3062static struct target_type raid_target = {
3063 .name = "raid",
Mike Snitzer9b6e5422016-06-02 11:48:09 -04003064 .version = {1, 9, 0},
NeilBrown9d09e662011-01-13 20:00:02 +00003065 .module = THIS_MODULE,
3066 .ctr = raid_ctr,
3067 .dtr = raid_dtr,
3068 .map = raid_map,
3069 .status = raid_status,
Jonathan Brassowbe836512013-04-24 11:42:43 +10003070 .message = raid_message,
NeilBrown9d09e662011-01-13 20:00:02 +00003071 .iterate_devices = raid_iterate_devices,
3072 .io_hints = raid_io_hints,
3073 .presuspend = raid_presuspend,
3074 .postsuspend = raid_postsuspend,
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003075 .preresume = raid_preresume,
NeilBrown9d09e662011-01-13 20:00:02 +00003076 .resume = raid_resume,
3077};
3078
3079static int __init dm_raid_init(void)
3080{
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11003081 DMINFO("Loading target version %u.%u.%u",
3082 raid_target.version[0],
3083 raid_target.version[1],
3084 raid_target.version[2]);
NeilBrown9d09e662011-01-13 20:00:02 +00003085 return dm_register_target(&raid_target);
3086}
3087
3088static void __exit dm_raid_exit(void)
3089{
3090 dm_unregister_target(&raid_target);
3091}
3092
3093module_init(dm_raid_init);
3094module_exit(dm_raid_exit);
3095
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02003096module_param(devices_handle_discard_safely, bool, 0644);
3097MODULE_PARM_DESC(devices_handle_discard_safely,
3098 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
3099
Mike Snitzeref9b85a2016-06-02 12:02:19 -04003100MODULE_DESCRIPTION(DM_NAME " raid0/1/10/4/5/6 target");
3101MODULE_ALIAS("dm-raid0");
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05003102MODULE_ALIAS("dm-raid1");
3103MODULE_ALIAS("dm-raid10");
NeilBrown9d09e662011-01-13 20:00:02 +00003104MODULE_ALIAS("dm-raid4");
3105MODULE_ALIAS("dm-raid5");
3106MODULE_ALIAS("dm-raid6");
3107MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>");
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003108MODULE_AUTHOR("Heinz Mauelshagen <dm-devel@redhat.com>");
NeilBrown9d09e662011-01-13 20:00:02 +00003109MODULE_LICENSE("GPL");