blob: 668398dfba3268a6dd862f3eb72d014ea021aff4 [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 Mauelshagen48cf06b2014-09-24 17:47:19 +020022static bool devices_handle_discard_safely = false;
23
NeilBrown9d09e662011-01-13 20:00:02 +000024/*
Jonathan Brassowb12d4372011-08-02 12:32:07 +010025 * The following flags are used by dm-raid.c to set up the array state.
26 * They must be cleared before md_run is called.
NeilBrown9d09e662011-01-13 20:00:02 +000027 */
Jonathan Brassowb12d4372011-08-02 12:32:07 +010028#define FirstUse 10 /* rdev flag */
NeilBrown9d09e662011-01-13 20:00:02 +000029
30struct raid_dev {
31 /*
32 * Two DM devices, one to hold metadata and one to hold the
33 * actual data/parity. The reason for this is to not confuse
34 * ti->len and give more flexibility in altering size and
35 * characteristics.
36 *
37 * While it is possible for this device to be associated
38 * with a different physical device than the data_dev, it
39 * is intended for it to be the same.
40 * |--------- Physical Device ---------|
41 * |- meta_dev -|------ data_dev ------|
42 */
43 struct dm_dev *meta_dev;
44 struct dm_dev *data_dev;
NeilBrown3cb03002011-10-11 16:45:26 +110045 struct md_rdev rdev;
NeilBrown9d09e662011-01-13 20:00:02 +000046};
47
48/*
Heinz Mauelshagenc76d53f2015-04-29 14:03:00 +020049 * Flags for rs->ctr_flags field.
Heinz Mauelshagen702108d2016-05-19 18:49:26 +020050 *
51 * 1 = no flag value
52 * 2 = flag with value
NeilBrown9d09e662011-01-13 20:00:02 +000053 */
Heinz Mauelshagen702108d2016-05-19 18:49:26 +020054#define CTR_FLAG_SYNC 0x1 /* 1 */ /* Not with raid0! */
55#define CTR_FLAG_NOSYNC 0x2 /* 1 */ /* Not with raid0! */
56#define CTR_FLAG_REBUILD 0x4 /* 2 */ /* Not with raid0! */
57#define CTR_FLAG_DAEMON_SLEEP 0x8 /* 2 */ /* Not with raid0! */
58#define CTR_FLAG_MIN_RECOVERY_RATE 0x10 /* 2 */ /* Not with raid0! */
59#define CTR_FLAG_MAX_RECOVERY_RATE 0x20 /* 2 */ /* Not with raid0! */
60#define CTR_FLAG_MAX_WRITE_BEHIND 0x40 /* 2 */ /* Only with raid1! */
61#define CTR_FLAG_WRITE_MOSTLY 0x80 /* 2 */ /* Only with raid1! */
62#define CTR_FLAG_STRIPE_CACHE 0x100 /* 2 */ /* Only with raid4/5/6! */
63#define CTR_FLAG_REGION_SIZE 0x200 /* 2 */ /* Not with raid0! */
64#define CTR_FLAG_RAID10_COPIES 0x400 /* 2 */ /* Only with raid10 */
65#define CTR_FLAG_RAID10_FORMAT 0x800 /* 2 */ /* Only with raid10 */
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -050066
Heinz Mauelshagenf0902792016-05-19 18:49:27 +020067/*
68 * Definitions of various constructor flags to
69 * be used in checks of valid / invalid flags
70 * per raid level.
71 */
72/* Define all any sync flags */
73#define CTR_FLAGS_ANY_SYNC (CTR_FLAG_SYNC | CTR_FLAG_NOSYNC)
74
75/* Define flags for options without argument (e.g. 'nosync') */
76#define CTR_FLAG_OPTIONS_NO_ARGS CTR_FLAGS_ANY_SYNC
77
78/* Define flags for options with one argument (e.g. 'delta_disks +2') */
79#define CTR_FLAG_OPTIONS_ONE_ARG (CTR_FLAG_REBUILD | \
80 CTR_FLAG_WRITE_MOSTLY | \
81 CTR_FLAG_DAEMON_SLEEP | \
82 CTR_FLAG_MIN_RECOVERY_RATE | \
83 CTR_FLAG_MAX_RECOVERY_RATE | \
84 CTR_FLAG_MAX_WRITE_BEHIND | \
85 CTR_FLAG_STRIPE_CACHE | \
86 CTR_FLAG_REGION_SIZE | \
87 CTR_FLAG_RAID10_COPIES | \
88 CTR_FLAG_RAID10_FORMAT)
89
90/* All ctr optional arguments */
91#define ALL_CTR_FLAGS (CTR_FLAG_OPTIONS_NO_ARGS | \
92 CTR_FLAG_OPTIONS_ONE_ARG)
93
94/* Invalid options definitions per raid level... */
95
96/* "raid0" does not accept any options */
97#define RAID0_INVALID_FLAGS ALL_CTR_FLAGS
98
99/* "raid1" does not accept stripe cache or any raid10 options */
100#define RAID1_INVALID_FLAGS (CTR_FLAG_STRIPE_CACHE | \
101 CTR_FLAG_RAID10_COPIES | \
102 CTR_FLAG_RAID10_FORMAT)
103
104/* "raid10" does not accept any raid1 or stripe cache options */
105#define RAID10_INVALID_FLAGS (CTR_FLAG_WRITE_MOSTLY | \
106 CTR_FLAG_MAX_WRITE_BEHIND | \
107 CTR_FLAG_STRIPE_CACHE)
108/*
109 * "raid4/5/6" do not accept any raid1 or raid10 specific options
110 *
111 * "raid6" does not accept "nosync", because it is not guaranteed
112 * that both parity and q-syndrome are being written properly with
113 * any writes
114 */
115#define RAID45_INVALID_FLAGS (CTR_FLAG_WRITE_MOSTLY | \
116 CTR_FLAG_MAX_WRITE_BEHIND | \
117 CTR_FLAG_RAID10_FORMAT | \
118 CTR_FLAG_RAID10_COPIES)
119#define RAID6_INVALID_FLAGS (CTR_FLAG_NOSYNC | RAID45_INVALID_FLAGS)
120/* ...invalid options definitions per raid level */
121
NeilBrown9d09e662011-01-13 20:00:02 +0000122struct raid_set {
123 struct dm_target *ti;
124
Jonathan Brassow34f8ac6d2012-01-27 14:53:53 -0600125 uint32_t bitmap_loaded;
Heinz Mauelshagenc76d53f2015-04-29 14:03:00 +0200126 uint32_t ctr_flags;
NeilBrown9d09e662011-01-13 20:00:02 +0000127
NeilBrownfd01b882011-10-11 16:47:53 +1100128 struct mddev md;
NeilBrown9d09e662011-01-13 20:00:02 +0000129 struct raid_type *raid_type;
130 struct dm_target_callbacks callbacks;
131
132 struct raid_dev dev[0];
133};
134
135/* Supported raid types and properties. */
136static struct raid_type {
137 const char *name; /* RAID algorithm. */
138 const char *descr; /* Descriptor text for logging. */
139 const unsigned parity_devs; /* # of parity devices. */
140 const unsigned minimal_devs; /* minimal # of devices in set. */
141 const unsigned level; /* RAID level. */
142 const unsigned algorithm; /* RAID algorithm. */
143} raid_types[] = {
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +0200144 {"raid0", "RAID0 (striping)", 0, 2, 0, 0 /* NONE */},
Jonathan Brassow32737272011-08-02 12:32:07 +0100145 {"raid1", "RAID1 (mirroring)", 0, 2, 1, 0 /* NONE */},
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500146 {"raid10", "RAID10 (striped mirrors)", 0, 2, 10, UINT_MAX /* Varies */},
NeilBrown9d09e662011-01-13 20:00:02 +0000147 {"raid4", "RAID4 (dedicated parity disk)", 1, 2, 5, ALGORITHM_PARITY_0},
148 {"raid5_la", "RAID5 (left asymmetric)", 1, 2, 5, ALGORITHM_LEFT_ASYMMETRIC},
149 {"raid5_ra", "RAID5 (right asymmetric)", 1, 2, 5, ALGORITHM_RIGHT_ASYMMETRIC},
150 {"raid5_ls", "RAID5 (left symmetric)", 1, 2, 5, ALGORITHM_LEFT_SYMMETRIC},
151 {"raid5_rs", "RAID5 (right symmetric)", 1, 2, 5, ALGORITHM_RIGHT_SYMMETRIC},
152 {"raid6_zr", "RAID6 (zero restart)", 2, 4, 6, ALGORITHM_ROTATING_ZERO_RESTART},
153 {"raid6_nr", "RAID6 (N restart)", 2, 4, 6, ALGORITHM_ROTATING_N_RESTART},
154 {"raid6_nc", "RAID6 (N continue)", 2, 4, 6, ALGORITHM_ROTATING_N_CONTINUE}
155};
156
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200157/* True, if @v is in inclusive range [@min, @max] */
158static bool _in_range(long v, long min, long max)
159{
160 return v >= min && v <= max;
161}
162
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200163/* ctr flag bit manipulation... */
164/* Set single @flag in @flags */
165static void _set_flag(uint32_t flag, uint32_t *flags)
166{
167 WARN_ON_ONCE(hweight32(flag) != 1);
168 *flags |= flag;
169}
170
171/* Test single @flag in @flags */
172static bool _test_flag(uint32_t flag, uint32_t flags)
173{
174 WARN_ON_ONCE(hweight32(flag) != 1);
175 return (flag & flags) ? true : false;
176}
177
Heinz Mauelshagenad51d7f2016-05-19 18:49:28 +0200178/* Test multiple @flags in @all_flags */
179static bool _test_flags(uint32_t flags, uint32_t all_flags)
180{
181 return (flags & all_flags) ? true : false;
182}
183
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200184/* Return true if single @flag is set in @*flags, else set it and return false */
185static bool _test_and_set_flag(uint32_t flag, uint32_t *flags)
186{
187 if (_test_flag(flag, *flags))
188 return true;
189
190 _set_flag(flag, flags);
191 return false;
192}
193/* ...ctr and runtime flag bit manipulation */
194
195/* All table line arguments are defined here */
196static struct arg_name_flag {
197 const uint32_t flag;
198 const char *name;
199} _arg_name_flags[] = {
200 { CTR_FLAG_SYNC, "sync"},
201 { CTR_FLAG_NOSYNC, "nosync"},
202 { CTR_FLAG_REBUILD, "rebuild"},
203 { CTR_FLAG_DAEMON_SLEEP, "daemon_sleep"},
204 { CTR_FLAG_MIN_RECOVERY_RATE, "min_recovery_rate"},
205 { CTR_FLAG_MAX_RECOVERY_RATE, "max_recovery_rate"},
206 { CTR_FLAG_MAX_WRITE_BEHIND, "max_write_behind"},
207 { CTR_FLAG_WRITE_MOSTLY, "writemostly"},
208 { CTR_FLAG_STRIPE_CACHE, "stripe_cache"},
209 { CTR_FLAG_REGION_SIZE, "region_size"},
210 { CTR_FLAG_RAID10_COPIES, "raid10_copies"},
211 { CTR_FLAG_RAID10_FORMAT, "raid10_format"},
212};
213
214/* Return argument name string for given @flag */
215static const char *_argname_by_flag(const uint32_t flag)
216{
217 if (hweight32(flag) == 1) {
218 struct arg_name_flag *anf = _arg_name_flags + ARRAY_SIZE(_arg_name_flags);
219
220 while (anf-- > _arg_name_flags)
221 if (_test_flag(flag, anf->flag))
222 return anf->name;
223
224 } else
225 DMERR("%s called with more than one flag!", __func__);
226
227 return NULL;
228}
229
230/*
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200231 * bool helpers to test for various raid levels of a raid type
232 */
233
234/* Return true, if raid type in @rt is raid0 */
235static bool rt_is_raid0(struct raid_type *rt)
236{
237 return !rt->level;
238}
239
240/* Return true, if raid type in @rt is raid1 */
241static bool rt_is_raid1(struct raid_type *rt)
242{
243 return rt->level == 1;
244}
245
246/* Return true, if raid type in @rt is raid10 */
247static bool rt_is_raid10(struct raid_type *rt)
248{
249 return rt->level == 10;
250}
251
252/* Return true, if raid type in @rt is raid4/5 */
253static bool rt_is_raid45(struct raid_type *rt)
254{
255 return _in_range(rt->level, 4, 5);
256}
257
258/* Return true, if raid type in @rt is raid6 */
259static bool rt_is_raid6(struct raid_type *rt)
260{
261 return rt->level == 6;
262}
263/* END: raid level bools */
264
265/*
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200266 * Convenience functions to set ti->error to @errmsg and
267 * return @r in order to shorten code in a lot of places
268 */
269static int ti_error_ret(struct dm_target *ti, const char *errmsg, int r)
270{
271 ti->error = (char *) errmsg;
272 return r;
273}
274
275static int ti_error_einval(struct dm_target *ti, const char *errmsg)
276{
277 return ti_error_ret(ti, errmsg, -EINVAL);
278}
279/* END: convenience functions to set ti->error to @errmsg... */
280
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200281/* Return invalid ctr flags for the raid level of @rs */
282static uint32_t _invalid_flags(struct raid_set *rs)
283{
284 if (rt_is_raid0(rs->raid_type))
285 return RAID0_INVALID_FLAGS;
286 else if (rt_is_raid1(rs->raid_type))
287 return RAID1_INVALID_FLAGS;
288 else if (rt_is_raid10(rs->raid_type))
289 return RAID10_INVALID_FLAGS;
290 else if (rt_is_raid45(rs->raid_type))
291 return RAID45_INVALID_FLAGS;
292 else if (rt_is_raid6(rs->raid_type))
293 return RAID6_INVALID_FLAGS;
294
295 return ~0;
296}
297
298/*
299 * Check for any invalid flags set on @rs defined by bitset @invalid_flags
300 *
301 * Has to be called after parsing of the ctr flags!
302 */
303static int rs_check_for_invalid_flags(struct raid_set *rs)
304{
Heinz Mauelshagenad51d7f2016-05-19 18:49:28 +0200305 if (_test_flags(rs->ctr_flags, _invalid_flags(rs)))
306 return ti_error_einval(rs->ti, "Invalid flag combined");
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200307
308 return 0;
309}
310
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100311static char *raid10_md_layout_to_format(int layout)
312{
313 /*
314 * Bit 16 and 17 stand for "offset" and "use_far_sets"
315 * Refer to MD's raid10.c for details
316 */
317 if ((layout & 0x10000) && (layout & 0x20000))
318 return "offset";
319
320 if ((layout & 0xFF) > 1)
321 return "near";
322
323 return "far";
324}
325
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500326static unsigned raid10_md_layout_to_copies(int layout)
327{
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100328 if ((layout & 0xFF) > 1)
329 return layout & 0xFF;
330 return (layout >> 8) & 0xFF;
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500331}
332
333static int raid10_format_to_md_layout(char *format, unsigned copies)
334{
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100335 unsigned n = 1, f = 1;
336
Heinz Mauelshagenc76d53f2015-04-29 14:03:00 +0200337 if (!strcasecmp("near", format))
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100338 n = copies;
339 else
340 f = copies;
341
Heinz Mauelshagenc76d53f2015-04-29 14:03:00 +0200342 if (!strcasecmp("offset", format))
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100343 return 0x30000 | (f << 8) | n;
344
Heinz Mauelshagenc76d53f2015-04-29 14:03:00 +0200345 if (!strcasecmp("far", format))
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100346 return 0x20000 | (f << 8) | n;
347
348 return (f << 8) | n;
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500349}
350
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200351static struct raid_type *get_raid_type(const char *name)
NeilBrown9d09e662011-01-13 20:00:02 +0000352{
353 int i;
354
355 for (i = 0; i < ARRAY_SIZE(raid_types); i++)
356 if (!strcmp(raid_types[i].name, name))
357 return &raid_types[i];
358
359 return NULL;
360}
361
362static struct raid_set *context_alloc(struct dm_target *ti, struct raid_type *raid_type, unsigned raid_devs)
363{
364 unsigned i;
365 struct raid_set *rs;
NeilBrown9d09e662011-01-13 20:00:02 +0000366
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200367 if (raid_devs <= raid_type->parity_devs)
368 return ERR_PTR(ti_error_einval(ti, "Insufficient number of devices"));
NeilBrown9d09e662011-01-13 20:00:02 +0000369
NeilBrown9d09e662011-01-13 20:00:02 +0000370 rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL);
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200371 if (!rs)
372 return ERR_PTR(ti_error_ret(ti, "Cannot allocate raid context", -ENOMEM));
NeilBrown9d09e662011-01-13 20:00:02 +0000373
374 mddev_init(&rs->md);
375
376 rs->ti = ti;
377 rs->raid_type = raid_type;
378 rs->md.raid_disks = raid_devs;
379 rs->md.level = raid_type->level;
380 rs->md.new_level = rs->md.level;
NeilBrown9d09e662011-01-13 20:00:02 +0000381 rs->md.layout = raid_type->algorithm;
382 rs->md.new_layout = rs->md.layout;
383 rs->md.delta_disks = 0;
384 rs->md.recovery_cp = 0;
385
386 for (i = 0; i < raid_devs; i++)
387 md_rdev_init(&rs->dev[i].rdev);
388
389 /*
390 * Remaining items to be initialized by further RAID params:
391 * rs->md.persistent
392 * rs->md.external
393 * rs->md.chunk_sectors
394 * rs->md.new_chunk_sectors
Jonathan E Brassowc039c332012-07-27 15:08:04 +0100395 * rs->md.dev_sectors
NeilBrown9d09e662011-01-13 20:00:02 +0000396 */
397
398 return rs;
399}
400
401static void context_free(struct raid_set *rs)
402{
403 int i;
404
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100405 for (i = 0; i < rs->md.raid_disks; i++) {
406 if (rs->dev[i].meta_dev)
407 dm_put_device(rs->ti, rs->dev[i].meta_dev);
NeilBrown545c8792012-05-22 13:54:30 +1000408 md_rdev_clear(&rs->dev[i].rdev);
NeilBrown9d09e662011-01-13 20:00:02 +0000409 if (rs->dev[i].data_dev)
410 dm_put_device(rs->ti, rs->dev[i].data_dev);
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100411 }
NeilBrown9d09e662011-01-13 20:00:02 +0000412
413 kfree(rs);
414}
415
416/*
417 * For every device we have two words
418 * <meta_dev>: meta device name or '-' if missing
419 * <data_dev>: data device name or '-' if missing
420 *
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100421 * The following are permitted:
422 * - -
423 * - <data_dev>
424 * <meta_dev> <data_dev>
425 *
426 * The following is not allowed:
427 * <meta_dev> -
428 *
429 * This code parses those words. If there is a failure,
430 * the caller must use context_free to unwind the operations.
NeilBrown9d09e662011-01-13 20:00:02 +0000431 */
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200432static int parse_dev_params(struct raid_set *rs, struct dm_arg_set *as)
NeilBrown9d09e662011-01-13 20:00:02 +0000433{
434 int i;
435 int rebuild = 0;
436 int metadata_available = 0;
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +0200437 int r = 0;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200438 const char *arg;
NeilBrown9d09e662011-01-13 20:00:02 +0000439
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200440 /* Put off the number of raid devices argument to get to dev pairs */
441 arg = dm_shift_arg(as);
442 if (!arg)
443 return -EINVAL;
444
445 for (i = 0; i < rs->md.raid_disks; i++) {
NeilBrown9d09e662011-01-13 20:00:02 +0000446 rs->dev[i].rdev.raid_disk = i;
447
448 rs->dev[i].meta_dev = NULL;
449 rs->dev[i].data_dev = NULL;
450
451 /*
452 * There are no offsets, since there is a separate device
453 * for data and metadata.
454 */
455 rs->dev[i].rdev.data_offset = 0;
456 rs->dev[i].rdev.mddev = &rs->md;
457
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200458 arg = dm_shift_arg(as);
459 if (!arg)
460 return -EINVAL;
461
462 if (strcmp(arg, "-")) {
463 r = dm_get_device(rs->ti, arg,
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100464 dm_table_get_mode(rs->ti->table),
465 &rs->dev[i].meta_dev);
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +0200466 if (r)
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200467 return ti_error_ret(rs->ti, "RAID metadata device lookup failure", r);
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100468
469 rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL);
470 if (!rs->dev[i].rdev.sb_page)
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200471 return ti_error_ret(rs->ti, "Failed to allocate superblock page", -ENOMEM);
NeilBrown9d09e662011-01-13 20:00:02 +0000472 }
473
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200474 arg = dm_shift_arg(as);
475 if (!arg)
476 return -EINVAL;
477
478 if (!strcmp(arg, "-")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000479 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) &&
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200480 (!rs->dev[i].rdev.recovery_offset))
481 return ti_error_einval(rs->ti, "Drive designated for rebuild not specified");
NeilBrown9d09e662011-01-13 20:00:02 +0000482
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100483 if (rs->dev[i].meta_dev)
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200484 return ti_error_einval(rs->ti, "No data device supplied with metadata device");
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100485
NeilBrown9d09e662011-01-13 20:00:02 +0000486 continue;
487 }
488
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200489 r = dm_get_device(rs->ti, arg,
NeilBrown9d09e662011-01-13 20:00:02 +0000490 dm_table_get_mode(rs->ti->table),
491 &rs->dev[i].data_dev);
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200492 if (r)
493 return ti_error_ret(rs->ti, "RAID device lookup failure", r);
NeilBrown9d09e662011-01-13 20:00:02 +0000494
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100495 if (rs->dev[i].meta_dev) {
496 metadata_available = 1;
497 rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev;
498 }
NeilBrown9d09e662011-01-13 20:00:02 +0000499 rs->dev[i].rdev.bdev = rs->dev[i].data_dev->bdev;
500 list_add(&rs->dev[i].rdev.same_set, &rs->md.disks);
501 if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
502 rebuild++;
503 }
504
505 if (metadata_available) {
506 rs->md.external = 0;
507 rs->md.persistent = 1;
508 rs->md.major_version = 2;
509 } else if (rebuild && !rs->md.recovery_cp) {
510 /*
511 * Without metadata, we will not be able to tell if the array
512 * is in-sync or not - we must assume it is not. Therefore,
513 * it is impossible to rebuild a drive.
514 *
515 * Even if there is metadata, the on-disk information may
516 * indicate that the array is not in-sync and it will then
517 * fail at that time.
518 *
519 * User could specify 'nosync' option if desperate.
520 */
521 DMERR("Unable to rebuild drive while array is not in-sync");
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200522 return ti_error_einval(rs->ti, "Unable to rebuild drive while array is not in-sync");
NeilBrown9d09e662011-01-13 20:00:02 +0000523 }
524
525 return 0;
526}
527
528/*
Jonathan Brassowc1084562011-08-02 12:32:07 +0100529 * validate_region_size
530 * @rs
531 * @region_size: region size in sectors. If 0, pick a size (4MiB default).
532 *
533 * Set rs->md.bitmap_info.chunksize (which really refers to 'region size').
534 * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap.
535 *
536 * Returns: 0 on success, -EINVAL on failure.
537 */
538static int validate_region_size(struct raid_set *rs, unsigned long region_size)
539{
540 unsigned long min_region_size = rs->ti->len / (1 << 21);
541
542 if (!region_size) {
543 /*
544 * Choose a reasonable default. All figures in sectors.
545 */
546 if (min_region_size > (1 << 13)) {
Jonathan Brassow3a0f9aa2012-12-21 20:23:33 +0000547 /* If not a power of 2, make it the next power of 2 */
Mikulas Patocka042745e2015-10-02 11:17:37 -0400548 region_size = roundup_pow_of_two(min_region_size);
Jonathan Brassowc1084562011-08-02 12:32:07 +0100549 DMINFO("Choosing default region size of %lu sectors",
550 region_size);
Jonathan Brassowc1084562011-08-02 12:32:07 +0100551 } else {
552 DMINFO("Choosing default region size of 4MiB");
553 region_size = 1 << 13; /* sectors */
554 }
555 } else {
556 /*
557 * Validate user-supplied value.
558 */
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200559 if (region_size > rs->ti->len)
560 return ti_error_einval(rs->ti, "Supplied region size is too large");
Jonathan Brassowc1084562011-08-02 12:32:07 +0100561
562 if (region_size < min_region_size) {
563 DMERR("Supplied region_size (%lu sectors) below minimum (%lu)",
564 region_size, min_region_size);
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200565 return ti_error_einval(rs->ti, "Supplied region size is too small");
Jonathan Brassowc1084562011-08-02 12:32:07 +0100566 }
567
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200568 if (!is_power_of_2(region_size))
569 return ti_error_einval(rs->ti, "Region size is not a power of 2");
Jonathan Brassowc1084562011-08-02 12:32:07 +0100570
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200571 if (region_size < rs->md.chunk_sectors)
572 return ti_error_einval(rs->ti, "Region size is smaller than the chunk size");
Jonathan Brassowc1084562011-08-02 12:32:07 +0100573 }
574
575 /*
576 * Convert sectors to bytes.
577 */
578 rs->md.bitmap_info.chunksize = (region_size << 9);
579
580 return 0;
581}
582
583/*
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600584 * validate_raid_redundancy
Jonathan Brassoweb649122012-10-11 13:40:09 +1100585 * @rs
586 *
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600587 * Determine if there are enough devices in the array that haven't
588 * failed (or are being rebuilt) to form a usable array.
Jonathan Brassoweb649122012-10-11 13:40:09 +1100589 *
590 * Returns: 0 on success, -EINVAL on failure.
591 */
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600592static int validate_raid_redundancy(struct raid_set *rs)
Jonathan Brassoweb649122012-10-11 13:40:09 +1100593{
594 unsigned i, rebuild_cnt = 0;
NeilBrown3f6bbd32013-05-09 10:27:49 +1000595 unsigned rebuilds_per_group = 0, copies, d;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100596 unsigned group_size, last_group_start;
Jonathan Brassoweb649122012-10-11 13:40:09 +1100597
Jonathan Brassoweb649122012-10-11 13:40:09 +1100598 for (i = 0; i < rs->md.raid_disks; i++)
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600599 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) ||
600 !rs->dev[i].rdev.sb_page)
Jonathan Brassoweb649122012-10-11 13:40:09 +1100601 rebuild_cnt++;
602
603 switch (rs->raid_type->level) {
604 case 1:
605 if (rebuild_cnt >= rs->md.raid_disks)
606 goto too_many;
607 break;
608 case 4:
609 case 5:
610 case 6:
611 if (rebuild_cnt > rs->raid_type->parity_devs)
612 goto too_many;
613 break;
614 case 10:
Jonathan Brassow4ec1e362012-10-11 13:40:24 +1100615 copies = raid10_md_layout_to_copies(rs->md.layout);
616 if (rebuild_cnt < copies)
617 break;
618
619 /*
620 * It is possible to have a higher rebuild count for RAID10,
621 * as long as the failed devices occur in different mirror
622 * groups (i.e. different stripes).
623 *
Jonathan Brassow4ec1e362012-10-11 13:40:24 +1100624 * When checking "near" format, make sure no adjacent devices
625 * have failed beyond what can be handled. In addition to the
626 * simple case where the number of devices is a multiple of the
627 * number of copies, we must also handle cases where the number
628 * of devices is not a multiple of the number of copies.
629 * E.g. dev1 dev2 dev3 dev4 dev5
630 * A A B B C
631 * C D D E E
632 */
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100633 if (!strcmp("near", raid10_md_layout_to_format(rs->md.layout))) {
634 for (i = 0; i < rs->md.raid_disks * copies; i++) {
635 if (!(i % copies))
636 rebuilds_per_group = 0;
637 d = i % rs->md.raid_disks;
638 if ((!rs->dev[d].rdev.sb_page ||
639 !test_bit(In_sync, &rs->dev[d].rdev.flags)) &&
640 (++rebuilds_per_group >= copies))
641 goto too_many;
642 }
643 break;
644 }
645
646 /*
647 * When checking "far" and "offset" formats, we need to ensure
648 * that the device that holds its copy is not also dead or
649 * being rebuilt. (Note that "far" and "offset" formats only
650 * support two copies right now. These formats also only ever
651 * use the 'use_far_sets' variant.)
652 *
653 * This check is somewhat complicated by the need to account
654 * for arrays that are not a multiple of (far) copies. This
655 * results in the need to treat the last (potentially larger)
656 * set differently.
657 */
658 group_size = (rs->md.raid_disks / copies);
659 last_group_start = (rs->md.raid_disks / group_size) - 1;
660 last_group_start *= group_size;
661 for (i = 0; i < rs->md.raid_disks; i++) {
662 if (!(i % copies) && !(i > last_group_start))
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600663 rebuilds_per_group = 0;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100664 if ((!rs->dev[i].rdev.sb_page ||
665 !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
Jonathan Brassow4ec1e362012-10-11 13:40:24 +1100666 (++rebuilds_per_group >= copies))
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100667 goto too_many;
Jonathan Brassow4ec1e362012-10-11 13:40:24 +1100668 }
669 break;
Jonathan Brassoweb649122012-10-11 13:40:09 +1100670 default:
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600671 if (rebuild_cnt)
672 return -EINVAL;
Jonathan Brassoweb649122012-10-11 13:40:09 +1100673 }
674
675 return 0;
676
677too_many:
Jonathan Brassoweb649122012-10-11 13:40:09 +1100678 return -EINVAL;
679}
680
681/*
NeilBrown9d09e662011-01-13 20:00:02 +0000682 * Possible arguments are...
NeilBrown9d09e662011-01-13 20:00:02 +0000683 * <chunk_size> [optional_args]
684 *
Jonathan Brassow32737272011-08-02 12:32:07 +0100685 * Argument definitions
686 * <chunk_size> The number of sectors per disk that
687 * will form the "stripe"
688 * [[no]sync] Force or prevent recovery of the
689 * entire array
NeilBrown9d09e662011-01-13 20:00:02 +0000690 * [rebuild <idx>] Rebuild the drive indicated by the index
Jonathan Brassow32737272011-08-02 12:32:07 +0100691 * [daemon_sleep <ms>] Time between bitmap daemon work to
692 * clear bits
NeilBrown9d09e662011-01-13 20:00:02 +0000693 * [min_recovery_rate <kB/sec/disk>] Throttle RAID initialization
694 * [max_recovery_rate <kB/sec/disk>] Throttle RAID initialization
Jonathan Brassow46bed2b2011-08-02 12:32:07 +0100695 * [write_mostly <idx>] Indicate a write mostly drive via index
NeilBrown9d09e662011-01-13 20:00:02 +0000696 * [max_write_behind <sectors>] See '-write-behind=' (man mdadm)
697 * [stripe_cache <sectors>] Stripe cache size for higher RAIDs
Jonathan Brassowc1084562011-08-02 12:32:07 +0100698 * [region_size <sectors>] Defines granularity of bitmap
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500699 *
700 * RAID10-only options:
701 * [raid10_copies <# copies>] Number of copies. (Default: 2)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100702 * [raid10_format <near|far|offset>] Layout algorithm. (Default: near)
NeilBrown9d09e662011-01-13 20:00:02 +0000703 */
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200704static int parse_raid_params(struct raid_set *rs, struct dm_arg_set *as,
NeilBrown9d09e662011-01-13 20:00:02 +0000705 unsigned num_raid_params)
706{
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500707 char *raid10_format = "near";
708 unsigned raid10_copies = 2;
Jonathan Brassoweb649122012-10-11 13:40:09 +1100709 unsigned i;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200710 unsigned value, region_size = 0;
Jonathan E Brassowc039c332012-07-27 15:08:04 +0100711 sector_t sectors_per_dev = rs->ti->len;
Mike Snitzer542f9032012-07-27 15:08:00 +0100712 sector_t max_io_len;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200713 const char *arg, *key;
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200714 struct raid_dev *rd;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200715
716 arg = dm_shift_arg(as);
717 num_raid_params--; /* Account for chunk_size argument */
718
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200719 if (kstrtouint(arg, 10, &value) < 0)
720 return ti_error_einval(rs->ti, "Bad numerical argument given for chunk_size");
NeilBrown9d09e662011-01-13 20:00:02 +0000721
722 /*
723 * First, parse the in-order required arguments
Jonathan Brassow32737272011-08-02 12:32:07 +0100724 * "chunk_size" is the only argument of this type.
NeilBrown9d09e662011-01-13 20:00:02 +0000725 */
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200726 if (rs->raid_type->level == 1) {
Jonathan Brassow32737272011-08-02 12:32:07 +0100727 if (value)
728 DMERR("Ignoring chunk size parameter for RAID 1");
729 value = 0;
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200730 } else if (!is_power_of_2(value))
731 return ti_error_einval(rs->ti, "Chunk size must be a power of 2");
732 else if (value < 8)
733 return ti_error_einval(rs->ti, "Chunk size value is too small");
NeilBrown9d09e662011-01-13 20:00:02 +0000734
735 rs->md.new_chunk_sectors = rs->md.chunk_sectors = value;
NeilBrown9d09e662011-01-13 20:00:02 +0000736
737 /*
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100738 * We set each individual device as In_sync with a completed
739 * 'recovery_offset'. If there has been a device failure or
740 * replacement then one of the following cases applies:
741 *
742 * 1) User specifies 'rebuild'.
743 * - Device is reset when param is read.
744 * 2) A new device is supplied.
745 * - No matching superblock found, resets device.
746 * 3) Device failure was transient and returns on reload.
747 * - Failure noticed, resets device for bitmap replay.
748 * 4) Device hadn't completed recovery after previous failure.
749 * - Superblock is read and overrides recovery_offset.
750 *
751 * What is found in the superblocks of the devices is always
752 * authoritative, unless 'rebuild' or '[no]sync' was specified.
753 */
754 for (i = 0; i < rs->md.raid_disks; i++) {
755 set_bit(In_sync, &rs->dev[i].rdev.flags);
756 rs->dev[i].rdev.recovery_offset = MaxSector;
757 }
758
759 /*
NeilBrown9d09e662011-01-13 20:00:02 +0000760 * Second, parse the unordered optional arguments
761 */
NeilBrown9d09e662011-01-13 20:00:02 +0000762 for (i = 0; i < num_raid_params; i++) {
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200763 arg = dm_shift_arg(as);
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200764 if (!arg)
765 return ti_error_einval(rs->ti, "Not enough raid parameters given");
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200766
767 if (!strcasecmp(arg, "nosync")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000768 rs->md.recovery_cp = MaxSector;
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200769 _set_flag(CTR_FLAG_NOSYNC, &rs->ctr_flags);
NeilBrown9d09e662011-01-13 20:00:02 +0000770 continue;
771 }
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200772 if (!strcasecmp(arg, "sync")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000773 rs->md.recovery_cp = 0;
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200774 _set_flag(CTR_FLAG_SYNC, &rs->ctr_flags);
NeilBrown9d09e662011-01-13 20:00:02 +0000775 continue;
776 }
777
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200778 key = arg;
779 arg = dm_shift_arg(as);
780 i++; /* Account for the argument pairs */
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200781 if (!arg)
782 return ti_error_einval(rs->ti, "Wrong number of raid parameters given");
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500783
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200784 /*
785 * Parameters that take a string value are checked here.
786 */
787
788 if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_RAID10_FORMAT))) {
789 if (_test_and_set_flag(CTR_FLAG_RAID10_FORMAT, &rs->ctr_flags))
790 return ti_error_einval(rs->ti, "Only one raid10_format argument pair allowed");
791 if (rs->raid_type->level != 10)
792 return ti_error_einval(rs->ti, "'raid10_format' is an invalid parameter for this RAID type");
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200793 if (strcmp("near", arg) &&
794 strcmp("far", arg) &&
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200795 strcmp("offset", arg))
796 return ti_error_einval(rs->ti, "Invalid 'raid10_format' value given");
797
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200798 raid10_format = (char *) arg;
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500799 continue;
800 }
801
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200802 if (kstrtouint(arg, 10, &value) < 0)
803 return ti_error_einval(rs->ti, "Bad numerical argument given in raid params");
NeilBrown9d09e662011-01-13 20:00:02 +0000804
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200805 if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_REBUILD))) {
806 /*
807 * "rebuild" is being passed in by userspace to provide
808 * indexes of replaced devices and to set up additional
809 * devices on raid level takeover.
810 */
811 if (!_in_range(value, 0, rs->md.raid_disks - 1))
812 return ti_error_einval(rs->ti, "Invalid rebuild index given");
813
814 rd = rs->dev + value;
815 clear_bit(In_sync, &rd->rdev.flags);
816 clear_bit(Faulty, &rd->rdev.flags);
817 rd->rdev.recovery_offset = 0;
818 _set_flag(CTR_FLAG_REBUILD, &rs->ctr_flags);
819 } else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_WRITE_MOSTLY))) {
820 if (rs->raid_type->level != 1)
821 return ti_error_einval(rs->ti, "write_mostly option is only valid for RAID1");
822
823 if (!_in_range(value, 0, rs->md.raid_disks - 1))
824 return ti_error_einval(rs->ti, "Invalid write_mostly index given");
825
Jonathan Brassow46bed2b2011-08-02 12:32:07 +0100826 set_bit(WriteMostly, &rs->dev[value].rdev.flags);
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200827 _set_flag(CTR_FLAG_WRITE_MOSTLY, &rs->ctr_flags);
828 } else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_MAX_WRITE_BEHIND))) {
829 if (rs->raid_type->level != 1)
830 return ti_error_einval(rs->ti, "max_write_behind option is only valid for RAID1");
831
832 if (_test_and_set_flag(CTR_FLAG_MAX_WRITE_BEHIND, &rs->ctr_flags))
833 return ti_error_einval(rs->ti, "Only one max_write_behind argument pair allowed");
NeilBrown9d09e662011-01-13 20:00:02 +0000834
835 /*
836 * In device-mapper, we specify things in sectors, but
837 * MD records this value in kB
838 */
839 value /= 2;
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200840 if (value > COUNTER_MAX)
841 return ti_error_einval(rs->ti, "Max write-behind limit out of range");
842
NeilBrown9d09e662011-01-13 20:00:02 +0000843 rs->md.bitmap_info.max_write_behind = value;
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200844 } else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_DAEMON_SLEEP))) {
845 if (_test_and_set_flag(CTR_FLAG_DAEMON_SLEEP, &rs->ctr_flags))
846 return ti_error_einval(rs->ti, "Only one daemon_sleep argument pair allowed");
847 if (!value || (value > MAX_SCHEDULE_TIMEOUT))
848 return ti_error_einval(rs->ti, "daemon sleep period out of range");
NeilBrown9d09e662011-01-13 20:00:02 +0000849 rs->md.bitmap_info.daemon_sleep = value;
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200850 } else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_STRIPE_CACHE))) {
851 if (_test_and_set_flag(CTR_FLAG_STRIPE_CACHE, &rs->ctr_flags))
852 return ti_error_einval(rs->ti, "Only one stripe_cache argument pair allowed");
NeilBrown9d09e662011-01-13 20:00:02 +0000853 /*
854 * In device-mapper, we specify things in sectors, but
855 * MD records this value in kB
856 */
857 value /= 2;
858
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200859 if (!_in_range(rs->raid_type->level, 4, 6))
860 return ti_error_einval(rs->ti, "Inappropriate argument: stripe_cache");
861 if (raid5_set_cache_size(&rs->md, (int)value))
862 return ti_error_einval(rs->ti, "Bad stripe_cache size");
863
864 } else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_MIN_RECOVERY_RATE))) {
865 if (_test_and_set_flag(CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags))
866 return ti_error_einval(rs->ti, "Only one min_recovery_rate argument pair allowed");
867 if (value > INT_MAX)
868 return ti_error_einval(rs->ti, "min_recovery_rate out of range");
NeilBrown9d09e662011-01-13 20:00:02 +0000869 rs->md.sync_speed_min = (int)value;
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200870 } else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_MAX_RECOVERY_RATE))) {
871 if (_test_and_set_flag(CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags))
872 return ti_error_einval(rs->ti, "Only one max_recovery_rate argument pair allowed");
873 if (value > INT_MAX)
874 return ti_error_einval(rs->ti, "max_recovery_rate out of range");
NeilBrown9d09e662011-01-13 20:00:02 +0000875 rs->md.sync_speed_max = (int)value;
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200876 } else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_REGION_SIZE))) {
877 if (_test_and_set_flag(CTR_FLAG_REGION_SIZE, &rs->ctr_flags))
878 return ti_error_einval(rs->ti, "Only one region_size argument pair allowed");
879
Jonathan Brassowc1084562011-08-02 12:32:07 +0100880 region_size = value;
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200881 } else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_RAID10_COPIES))) {
882 if (_test_and_set_flag(CTR_FLAG_RAID10_COPIES, &rs->ctr_flags))
883 return ti_error_einval(rs->ti, "Only one raid10_copies argument pair allowed");
884
885 if (!_in_range(value, 2, rs->md.raid_disks))
886 return ti_error_einval(rs->ti, "Bad value for 'raid10_copies'");
887
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500888 raid10_copies = value;
NeilBrown9d09e662011-01-13 20:00:02 +0000889 } else {
890 DMERR("Unable to parse RAID parameter: %s", key);
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200891 return ti_error_einval(rs->ti, "Unable to parse RAID parameters");
NeilBrown9d09e662011-01-13 20:00:02 +0000892 }
893 }
894
Jonathan Brassowc1084562011-08-02 12:32:07 +0100895 if (validate_region_size(rs, region_size))
896 return -EINVAL;
897
898 if (rs->md.chunk_sectors)
Mike Snitzer542f9032012-07-27 15:08:00 +0100899 max_io_len = rs->md.chunk_sectors;
Jonathan Brassowc1084562011-08-02 12:32:07 +0100900 else
Mike Snitzer542f9032012-07-27 15:08:00 +0100901 max_io_len = region_size;
Jonathan Brassowc1084562011-08-02 12:32:07 +0100902
Mike Snitzer542f9032012-07-27 15:08:00 +0100903 if (dm_set_target_max_io_len(rs->ti, max_io_len))
904 return -EINVAL;
Jonathan Brassow32737272011-08-02 12:32:07 +0100905
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500906 if (rs->raid_type->level == 10) {
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200907 if (raid10_copies > rs->md.raid_disks)
908 return ti_error_einval(rs->ti, "Not enough devices to satisfy specification");
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500909
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100910 /*
911 * If the format is not "near", we only support
912 * two copies at the moment.
913 */
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200914 if (strcmp("near", raid10_format) && (raid10_copies > 2))
915 return ti_error_einval(rs->ti, "Too many copies for given RAID10 format.");
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100916
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500917 /* (Len * #mirrors) / #devices */
918 sectors_per_dev = rs->ti->len * raid10_copies;
919 sector_div(sectors_per_dev, rs->md.raid_disks);
920
921 rs->md.layout = raid10_format_to_md_layout(raid10_format,
922 raid10_copies);
923 rs->md.new_layout = rs->md.layout;
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +0200924 } else if ((!rs->raid_type->level || rs->raid_type->level > 1) &&
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500925 sector_div(sectors_per_dev,
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200926 (rs->md.raid_disks - rs->raid_type->parity_devs)))
927 return ti_error_einval(rs->ti, "Target length not divisible by number of data devices");
928
Jonathan E Brassowc039c332012-07-27 15:08:04 +0100929 rs->md.dev_sectors = sectors_per_dev;
930
NeilBrown9d09e662011-01-13 20:00:02 +0000931 /* Assume there are no metadata devices until the drives are parsed */
932 rs->md.persistent = 0;
933 rs->md.external = 1;
934
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200935 /* Check, if any invalid ctr arguments have been passed in for the raid level */
936 return rs_check_for_invalid_flags(rs);
NeilBrown9d09e662011-01-13 20:00:02 +0000937}
938
939static void do_table_event(struct work_struct *ws)
940{
941 struct raid_set *rs = container_of(ws, struct raid_set, md.event_work);
942
943 dm_table_event(rs->ti->table);
944}
945
946static int raid_is_congested(struct dm_target_callbacks *cb, int bits)
947{
948 struct raid_set *rs = container_of(cb, struct raid_set, callbacks);
949
NeilBrown5c675f82014-12-15 12:56:56 +1100950 return mddev_congested(&rs->md, bits);
NeilBrown9d09e662011-01-13 20:00:02 +0000951}
952
NeilBrown9d09e662011-01-13 20:00:02 +0000953/*
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100954 * This structure is never routinely used by userspace, unlike md superblocks.
955 * Devices with this superblock should only ever be accessed via device-mapper.
956 */
957#define DM_RAID_MAGIC 0x64526D44
958struct dm_raid_superblock {
959 __le32 magic; /* "DmRd" */
960 __le32 features; /* Used to indicate possible future changes */
961
962 __le32 num_devices; /* Number of devices in this array. (Max 64) */
963 __le32 array_position; /* The position of this drive in the array */
964
965 __le64 events; /* Incremented by md when superblock updated */
966 __le64 failed_devices; /* Bit field of devices to indicate failures */
967
968 /*
969 * This offset tracks the progress of the repair or replacement of
970 * an individual drive.
971 */
972 __le64 disk_recovery_offset;
973
974 /*
975 * This offset tracks the progress of the initial array
976 * synchronisation/parity calculation.
977 */
978 __le64 array_resync_offset;
979
980 /*
981 * RAID characteristics
982 */
983 __le32 level;
984 __le32 layout;
985 __le32 stripe_sectors;
986
Heinz Mauelshagen40d43c42014-10-17 13:38:50 +0200987 /* Remainder of a logical block is zero-filled when writing (see super_sync()). */
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100988} __packed;
989
NeilBrown3cb03002011-10-11 16:45:26 +1100990static int read_disk_sb(struct md_rdev *rdev, int size)
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100991{
992 BUG_ON(!rdev->sb_page);
993
994 if (rdev->sb_loaded)
995 return 0;
996
Mike Christie796a5cf2016-06-05 14:32:07 -0500997 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, 1)) {
Jonathan E Brassow04475682012-03-28 18:41:26 +0100998 DMERR("Failed to read superblock of device at position %d",
999 rdev->raid_disk);
Jonathan Brassowc32fb9e2012-05-22 13:55:31 +10001000 md_error(rdev->mddev, rdev);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001001 return -EINVAL;
1002 }
1003
1004 rdev->sb_loaded = 1;
1005
1006 return 0;
1007}
1008
NeilBrownfd01b882011-10-11 16:47:53 +11001009static void super_sync(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001010{
Jonathan Brassow81f382f2012-05-22 13:55:30 +10001011 int i;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001012 uint64_t failed_devices;
1013 struct dm_raid_superblock *sb;
Jonathan Brassow81f382f2012-05-22 13:55:30 +10001014 struct raid_set *rs = container_of(mddev, struct raid_set, md);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001015
1016 sb = page_address(rdev->sb_page);
1017 failed_devices = le64_to_cpu(sb->failed_devices);
1018
Jonathan Brassow81f382f2012-05-22 13:55:30 +10001019 for (i = 0; i < mddev->raid_disks; i++)
1020 if (!rs->dev[i].data_dev ||
1021 test_bit(Faulty, &(rs->dev[i].rdev.flags)))
1022 failed_devices |= (1ULL << i);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001023
Heinz Mauelshagen40d43c42014-10-17 13:38:50 +02001024 memset(sb + 1, 0, rdev->sb_size - sizeof(*sb));
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001025
1026 sb->magic = cpu_to_le32(DM_RAID_MAGIC);
1027 sb->features = cpu_to_le32(0); /* No features yet */
1028
1029 sb->num_devices = cpu_to_le32(mddev->raid_disks);
1030 sb->array_position = cpu_to_le32(rdev->raid_disk);
1031
1032 sb->events = cpu_to_le64(mddev->events);
1033 sb->failed_devices = cpu_to_le64(failed_devices);
1034
1035 sb->disk_recovery_offset = cpu_to_le64(rdev->recovery_offset);
1036 sb->array_resync_offset = cpu_to_le64(mddev->recovery_cp);
1037
1038 sb->level = cpu_to_le32(mddev->level);
1039 sb->layout = cpu_to_le32(mddev->layout);
1040 sb->stripe_sectors = cpu_to_le32(mddev->chunk_sectors);
1041}
1042
1043/*
1044 * super_load
1045 *
1046 * This function creates a superblock if one is not found on the device
1047 * and will decide which superblock to use if there's a choice.
1048 *
1049 * Return: 1 if use rdev, 0 if use refdev, -Exxx otherwise
1050 */
NeilBrown3cb03002011-10-11 16:45:26 +11001051static int super_load(struct md_rdev *rdev, struct md_rdev *refdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001052{
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02001053 int r;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001054 struct dm_raid_superblock *sb;
1055 struct dm_raid_superblock *refsb;
1056 uint64_t events_sb, events_refsb;
1057
1058 rdev->sb_start = 0;
Heinz Mauelshagen40d43c42014-10-17 13:38:50 +02001059 rdev->sb_size = bdev_logical_block_size(rdev->meta_bdev);
1060 if (rdev->sb_size < sizeof(*sb) || rdev->sb_size > PAGE_SIZE) {
1061 DMERR("superblock size of a logical block is no longer valid");
1062 return -EINVAL;
1063 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001064
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02001065 r = read_disk_sb(rdev, rdev->sb_size);
1066 if (r)
1067 return r;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001068
1069 sb = page_address(rdev->sb_page);
Jonathan E Brassow3aa3b2b2012-03-07 19:09:47 +00001070
1071 /*
1072 * Two cases that we want to write new superblocks and rebuild:
1073 * 1) New device (no matching magic number)
1074 * 2) Device specified for rebuild (!In_sync w/ offset == 0)
1075 */
1076 if ((sb->magic != cpu_to_le32(DM_RAID_MAGIC)) ||
1077 (!test_bit(In_sync, &rdev->flags) && !rdev->recovery_offset)) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001078 super_sync(rdev->mddev, rdev);
1079
1080 set_bit(FirstUse, &rdev->flags);
1081
1082 /* Force writing of superblocks to disk */
1083 set_bit(MD_CHANGE_DEVS, &rdev->mddev->flags);
1084
1085 /* Any superblock is better than none, choose that if given */
1086 return refdev ? 0 : 1;
1087 }
1088
1089 if (!refdev)
1090 return 1;
1091
1092 events_sb = le64_to_cpu(sb->events);
1093
1094 refsb = page_address(refdev->sb_page);
1095 events_refsb = le64_to_cpu(refsb->events);
1096
1097 return (events_sb > events_refsb) ? 1 : 0;
1098}
1099
NeilBrownfd01b882011-10-11 16:47:53 +11001100static int super_init_validation(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001101{
1102 int role;
1103 struct raid_set *rs = container_of(mddev, struct raid_set, md);
1104 uint64_t events_sb;
1105 uint64_t failed_devices;
1106 struct dm_raid_superblock *sb;
1107 uint32_t new_devs = 0;
1108 uint32_t rebuilds = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001109 struct md_rdev *r;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001110 struct dm_raid_superblock *sb2;
1111
1112 sb = page_address(rdev->sb_page);
1113 events_sb = le64_to_cpu(sb->events);
1114 failed_devices = le64_to_cpu(sb->failed_devices);
1115
1116 /*
1117 * Initialise to 1 if this is a new superblock.
1118 */
1119 mddev->events = events_sb ? : 1;
1120
1121 /*
1122 * Reshaping is not currently allowed
1123 */
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11001124 if (le32_to_cpu(sb->level) != mddev->level) {
1125 DMERR("Reshaping arrays not yet supported. (RAID level change)");
1126 return -EINVAL;
1127 }
1128 if (le32_to_cpu(sb->layout) != mddev->layout) {
1129 DMERR("Reshaping arrays not yet supported. (RAID layout change)");
1130 DMERR(" 0x%X vs 0x%X", le32_to_cpu(sb->layout), mddev->layout);
1131 DMERR(" Old layout: %s w/ %d copies",
1132 raid10_md_layout_to_format(le32_to_cpu(sb->layout)),
1133 raid10_md_layout_to_copies(le32_to_cpu(sb->layout)));
1134 DMERR(" New layout: %s w/ %d copies",
1135 raid10_md_layout_to_format(mddev->layout),
1136 raid10_md_layout_to_copies(mddev->layout));
1137 return -EINVAL;
1138 }
1139 if (le32_to_cpu(sb->stripe_sectors) != mddev->chunk_sectors) {
1140 DMERR("Reshaping arrays not yet supported. (stripe sectors change)");
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001141 return -EINVAL;
1142 }
1143
1144 /* We can only change the number of devices in RAID1 right now */
1145 if ((rs->raid_type->level != 1) &&
1146 (le32_to_cpu(sb->num_devices) != mddev->raid_disks)) {
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11001147 DMERR("Reshaping arrays not yet supported. (device count change)");
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001148 return -EINVAL;
1149 }
1150
Heinz Mauelshagenad51d7f2016-05-19 18:49:28 +02001151 if (!(_test_flags(CTR_FLAGS_ANY_SYNC, rs->ctr_flags)))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001152 mddev->recovery_cp = le64_to_cpu(sb->array_resync_offset);
1153
1154 /*
1155 * During load, we set FirstUse if a new superblock was written.
1156 * There are two reasons we might not have a superblock:
1157 * 1) The array is brand new - in which case, all of the
1158 * devices must have their In_sync bit set. Also,
1159 * recovery_cp must be 0, unless forced.
1160 * 2) This is a new device being added to an old array
1161 * and the new device needs to be rebuilt - in which
1162 * case the In_sync bit will /not/ be set and
1163 * recovery_cp must be MaxSector.
1164 */
NeilBrowndafb20f2012-03-19 12:46:39 +11001165 rdev_for_each(r, mddev) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001166 if (!test_bit(In_sync, &r->flags)) {
Jonathan E Brassow3aa3b2b2012-03-07 19:09:47 +00001167 DMINFO("Device %d specified for rebuild: "
1168 "Clearing superblock", r->raid_disk);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001169 rebuilds++;
1170 } else if (test_bit(FirstUse, &r->flags))
1171 new_devs++;
1172 }
1173
1174 if (!rebuilds) {
1175 if (new_devs == mddev->raid_disks) {
1176 DMINFO("Superblocks created for new array");
1177 set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
1178 } else if (new_devs) {
1179 DMERR("New device injected "
1180 "into existing array without 'rebuild' "
1181 "parameter specified");
1182 return -EINVAL;
1183 }
1184 } else if (new_devs) {
1185 DMERR("'rebuild' devices cannot be "
1186 "injected into an array with other first-time devices");
1187 return -EINVAL;
1188 } else if (mddev->recovery_cp != MaxSector) {
1189 DMERR("'rebuild' specified while array is not in-sync");
1190 return -EINVAL;
1191 }
1192
1193 /*
1194 * Now we set the Faulty bit for those devices that are
1195 * recorded in the superblock as failed.
1196 */
NeilBrowndafb20f2012-03-19 12:46:39 +11001197 rdev_for_each(r, mddev) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001198 if (!r->sb_page)
1199 continue;
1200 sb2 = page_address(r->sb_page);
1201 sb2->failed_devices = 0;
1202
1203 /*
1204 * Check for any device re-ordering.
1205 */
1206 if (!test_bit(FirstUse, &r->flags) && (r->raid_disk >= 0)) {
1207 role = le32_to_cpu(sb2->array_position);
1208 if (role != r->raid_disk) {
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001209 if (rs->raid_type->level != 1)
1210 return ti_error_einval(rs->ti, "Cannot change device "
1211 "positions in RAID array");
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001212 DMINFO("RAID1 device #%d now at position #%d",
1213 role, r->raid_disk);
1214 }
1215
1216 /*
1217 * Partial recovery is performed on
1218 * returning failed devices.
1219 */
1220 if (failed_devices & (1 << role))
1221 set_bit(Faulty, &r->flags);
1222 }
1223 }
1224
1225 return 0;
1226}
1227
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02001228static int super_validate(struct raid_set *rs, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001229{
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02001230 struct mddev *mddev = &rs->md;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001231 struct dm_raid_superblock *sb = page_address(rdev->sb_page);
1232
1233 /*
1234 * If mddev->events is not set, we know we have not yet initialized
1235 * the array.
1236 */
1237 if (!mddev->events && super_init_validation(mddev, rdev))
1238 return -EINVAL;
1239
Heinz Mauelshagen4c9971c2016-04-29 18:59:56 +02001240 if (le32_to_cpu(sb->features)) {
1241 rs->ti->error = "Unable to assemble array: No feature flags supported yet";
1242 return -EINVAL;
1243 }
1244
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02001245 /* Enable bitmap creation for RAID levels != 0 */
1246 mddev->bitmap_info.offset = (rs->raid_type->level) ? to_sector(4096) : 0;
1247 rdev->mddev->bitmap_info.default_offset = mddev->bitmap_info.offset;
1248
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001249 if (!test_bit(FirstUse, &rdev->flags)) {
1250 rdev->recovery_offset = le64_to_cpu(sb->disk_recovery_offset);
1251 if (rdev->recovery_offset != MaxSector)
1252 clear_bit(In_sync, &rdev->flags);
1253 }
1254
1255 /*
1256 * If a device comes back, set it as not In_sync and no longer faulty.
1257 */
1258 if (test_bit(Faulty, &rdev->flags)) {
1259 clear_bit(Faulty, &rdev->flags);
1260 clear_bit(In_sync, &rdev->flags);
1261 rdev->saved_raid_disk = rdev->raid_disk;
1262 rdev->recovery_offset = 0;
1263 }
1264
1265 clear_bit(FirstUse, &rdev->flags);
1266
1267 return 0;
1268}
1269
1270/*
1271 * Analyse superblocks and select the freshest.
1272 */
1273static int analyse_superblocks(struct dm_target *ti, struct raid_set *rs)
1274{
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02001275 int r;
Jonathan E Brassow04475682012-03-28 18:41:26 +01001276 struct raid_dev *dev;
Jonathan Brassowa9ad8522012-04-24 10:23:13 +10001277 struct md_rdev *rdev, *tmp, *freshest;
NeilBrownfd01b882011-10-11 16:47:53 +11001278 struct mddev *mddev = &rs->md;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001279
1280 freshest = NULL;
Jonathan Brassowa9ad8522012-04-24 10:23:13 +10001281 rdev_for_each_safe(rdev, tmp, mddev) {
Jonathan Brassow761becf2012-10-11 13:42:19 +11001282 /*
Heinz Mauelshagenc76d53f2015-04-29 14:03:00 +02001283 * Skipping super_load due to CTR_FLAG_SYNC will cause
Jonathan Brassow761becf2012-10-11 13:42:19 +11001284 * the array to undergo initialization again as
1285 * though it were new. This is the intended effect
1286 * of the "sync" directive.
1287 *
1288 * When reshaping capability is added, we must ensure
1289 * that the "sync" directive is disallowed during the
1290 * reshape.
1291 */
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02001292 rdev->sectors = to_sector(i_size_read(rdev->bdev->bd_inode));
1293
Heinz Mauelshagenad51d7f2016-05-19 18:49:28 +02001294 if (_test_flag(CTR_FLAG_SYNC, rs->ctr_flags))
Jonathan Brassow761becf2012-10-11 13:42:19 +11001295 continue;
1296
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001297 if (!rdev->meta_bdev)
1298 continue;
1299
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02001300 r = super_load(rdev, freshest);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001301
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02001302 switch (r) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001303 case 1:
1304 freshest = rdev;
1305 break;
1306 case 0:
1307 break;
1308 default:
Jonathan E Brassow04475682012-03-28 18:41:26 +01001309 dev = container_of(rdev, struct raid_dev, rdev);
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001310 if (dev->meta_dev)
1311 dm_put_device(ti, dev->meta_dev);
Jonathan E Brassow04475682012-03-28 18:41:26 +01001312
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001313 dev->meta_dev = NULL;
1314 rdev->meta_bdev = NULL;
Jonathan E Brassow04475682012-03-28 18:41:26 +01001315
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001316 if (rdev->sb_page)
1317 put_page(rdev->sb_page);
Jonathan E Brassow04475682012-03-28 18:41:26 +01001318
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001319 rdev->sb_page = NULL;
Jonathan E Brassow04475682012-03-28 18:41:26 +01001320
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001321 rdev->sb_loaded = 0;
Jonathan E Brassow04475682012-03-28 18:41:26 +01001322
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001323 /*
1324 * We might be able to salvage the data device
1325 * even though the meta device has failed. For
1326 * now, we behave as though '- -' had been
1327 * set for this device in the table.
1328 */
1329 if (dev->data_dev)
1330 dm_put_device(ti, dev->data_dev);
Jonathan E Brassow04475682012-03-28 18:41:26 +01001331
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001332 dev->data_dev = NULL;
1333 rdev->bdev = NULL;
Jonathan E Brassow04475682012-03-28 18:41:26 +01001334
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001335 list_del(&rdev->same_set);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001336 }
1337 }
1338
1339 if (!freshest)
1340 return 0;
1341
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001342 if (validate_raid_redundancy(rs))
1343 return ti_error_einval(rs->ti, "Insufficient redundancy to activate array");
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001344
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001345 /*
1346 * Validation of the freshest device provides the source of
1347 * validation for the remaining devices.
1348 */
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02001349 if (super_validate(rs, freshest))
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001350 return ti_error_einval(rs->ti, "Unable to assemble array: Invalid superblocks");
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001351
NeilBrowndafb20f2012-03-19 12:46:39 +11001352 rdev_for_each(rdev, mddev)
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02001353 if ((rdev != freshest) && super_validate(rs, rdev))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001354 return -EINVAL;
1355
1356 return 0;
1357}
1358
1359/*
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02001360 * Enable/disable discard support on RAID set depending on
1361 * RAID level and discard properties of underlying RAID members.
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02001362 */
1363static void configure_discard_support(struct dm_target *ti, struct raid_set *rs)
1364{
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02001365 int i;
1366 bool raid456;
1367
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02001368 /* Assume discards not supported until after checks below. */
1369 ti->discards_supported = false;
1370
1371 /* RAID level 4,5,6 require discard_zeroes_data for data integrity! */
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02001372 raid456 = (rs->md.level == 4 || rs->md.level == 5 || rs->md.level == 6);
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02001373
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02001374 for (i = 0; i < rs->md.raid_disks; i++) {
Heinz Mauelshagend20c4b02014-10-29 19:02:27 +01001375 struct request_queue *q;
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02001376
Heinz Mauelshagend20c4b02014-10-29 19:02:27 +01001377 if (!rs->dev[i].rdev.bdev)
1378 continue;
1379
1380 q = bdev_get_queue(rs->dev[i].rdev.bdev);
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02001381 if (!q || !blk_queue_discard(q))
1382 return;
1383
1384 if (raid456) {
1385 if (!q->limits.discard_zeroes_data)
1386 return;
1387 if (!devices_handle_discard_safely) {
1388 DMERR("raid456 discard support disabled due to discard_zeroes_data uncertainty.");
1389 DMERR("Set dm-raid.devices_handle_discard_safely=Y to override.");
1390 return;
1391 }
1392 }
1393 }
1394
1395 /* All RAID members properly support discards */
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02001396 ti->discards_supported = true;
1397
1398 /*
1399 * RAID1 and RAID10 personalities require bio splitting,
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02001400 * RAID0/4/5/6 don't and process large discard bios properly.
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02001401 */
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02001402 ti->split_discard_bios = !!(rs->md.level == 1 || rs->md.level == 10);
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02001403 ti->num_discard_bios = 1;
1404}
1405
1406/*
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02001407 * Construct a RAID0/1/10/4/5/6 mapping:
NeilBrown9d09e662011-01-13 20:00:02 +00001408 * Args:
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02001409 * <raid_type> <#raid_params> <raid_params>{0,} \
1410 * <#raid_devs> [<meta_dev1> <dev1>]{1,}
NeilBrown9d09e662011-01-13 20:00:02 +00001411 *
NeilBrown9d09e662011-01-13 20:00:02 +00001412 * <raid_params> varies by <raid_type>. See 'parse_raid_params' for
1413 * details on possible <raid_params>.
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02001414 *
1415 * Userspace is free to initialize the metadata devices, hence the superblocks to
1416 * enforce recreation based on the passed in table parameters.
1417 *
NeilBrown9d09e662011-01-13 20:00:02 +00001418 */
1419static int raid_ctr(struct dm_target *ti, unsigned argc, char **argv)
1420{
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02001421 int r;
NeilBrown9d09e662011-01-13 20:00:02 +00001422 struct raid_type *rt;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001423 unsigned num_raid_params, num_raid_devs;
NeilBrown9d09e662011-01-13 20:00:02 +00001424 struct raid_set *rs = NULL;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001425 const char *arg;
1426 struct dm_arg_set as = { argc, argv }, as_nrd;
1427 struct dm_arg _args[] = {
1428 { 0, as.argc, "Cannot understand number of raid parameters" },
1429 { 1, 254, "Cannot understand number of raid devices parameters" }
1430 };
NeilBrown9d09e662011-01-13 20:00:02 +00001431
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001432 /* Must have <raid_type> */
1433 arg = dm_shift_arg(&as);
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001434 if (!arg)
1435 return ti_error_einval(rs->ti, "No arguments");
NeilBrown9d09e662011-01-13 20:00:02 +00001436
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001437 rt = get_raid_type(arg);
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001438 if (!rt)
1439 return ti_error_einval(rs->ti, "Unrecognised raid_type");
NeilBrown9d09e662011-01-13 20:00:02 +00001440
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001441 /* Must have <#raid_params> */
1442 if (dm_read_arg_group(_args, &as, &num_raid_params, &ti->error))
1443 return -EINVAL;
NeilBrown9d09e662011-01-13 20:00:02 +00001444
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001445 /* number of raid device tupples <meta_dev data_dev> */
1446 as_nrd = as;
1447 dm_consume_args(&as_nrd, num_raid_params);
1448 _args[1].max = (as_nrd.argc - 1) / 2;
1449 if (dm_read_arg(_args + 1, &as_nrd, &num_raid_devs, &ti->error))
1450 return -EINVAL;
1451
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001452 if (!_in_range(num_raid_devs, 1, MAX_RAID_DEVICES))
1453 return ti_error_einval(rs->ti, "Invalid number of supplied raid devices");
NeilBrown9d09e662011-01-13 20:00:02 +00001454
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001455 rs = context_alloc(ti, rt, num_raid_devs);
NeilBrown9d09e662011-01-13 20:00:02 +00001456 if (IS_ERR(rs))
1457 return PTR_ERR(rs);
1458
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001459 r = parse_raid_params(rs, &as, num_raid_params);
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02001460 if (r)
NeilBrown9d09e662011-01-13 20:00:02 +00001461 goto bad;
1462
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001463 r = parse_dev_params(rs, &as);
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02001464 if (r)
NeilBrown9d09e662011-01-13 20:00:02 +00001465 goto bad;
1466
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001467 rs->md.sync_super = super_sync;
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02001468 r = analyse_superblocks(ti, rs);
1469 if (r)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001470 goto bad;
1471
NeilBrown9d09e662011-01-13 20:00:02 +00001472 INIT_WORK(&rs->md.event_work, do_table_event);
NeilBrown9d09e662011-01-13 20:00:02 +00001473 ti->private = rs;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001474 ti->num_flush_bios = 1;
NeilBrown9d09e662011-01-13 20:00:02 +00001475
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02001476 /*
1477 * Disable/enable discard support on RAID set.
1478 */
1479 configure_discard_support(ti, rs);
1480
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02001481 /* Has to be held on running the array */
1482 mddev_lock_nointr(&rs->md);
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02001483 r = md_run(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00001484 rs->md.in_sync = 0; /* Assume already marked dirty */
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02001485 mddev_unlock(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00001486
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02001487 if (r) {
NeilBrown9d09e662011-01-13 20:00:02 +00001488 ti->error = "Fail to run raid array";
1489 goto bad;
1490 }
1491
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001492 if (ti->len != rs->md.array_sectors) {
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001493 r = ti_error_einval(ti, "Array size does not match requested target length");
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001494 goto size_mismatch;
1495 }
NeilBrown9d09e662011-01-13 20:00:02 +00001496 rs->callbacks.congested_fn = raid_is_congested;
NeilBrown9d09e662011-01-13 20:00:02 +00001497 dm_table_add_target_callbacks(ti->table, &rs->callbacks);
1498
Jonathan Brassow32737272011-08-02 12:32:07 +01001499 mddev_suspend(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00001500 return 0;
1501
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001502size_mismatch:
1503 md_stop(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00001504bad:
1505 context_free(rs);
1506
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02001507 return r;
NeilBrown9d09e662011-01-13 20:00:02 +00001508}
1509
1510static void raid_dtr(struct dm_target *ti)
1511{
1512 struct raid_set *rs = ti->private;
1513
1514 list_del_init(&rs->callbacks.list);
1515 md_stop(&rs->md);
1516 context_free(rs);
1517}
1518
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001519static int raid_map(struct dm_target *ti, struct bio *bio)
NeilBrown9d09e662011-01-13 20:00:02 +00001520{
1521 struct raid_set *rs = ti->private;
NeilBrownfd01b882011-10-11 16:47:53 +11001522 struct mddev *mddev = &rs->md;
NeilBrown9d09e662011-01-13 20:00:02 +00001523
1524 mddev->pers->make_request(mddev, bio);
1525
1526 return DM_MAPIO_SUBMITTED;
1527}
1528
Jonathan Brassowbe836512013-04-24 11:42:43 +10001529static const char *decipher_sync_action(struct mddev *mddev)
1530{
1531 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
1532 return "frozen";
1533
1534 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1535 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
1536 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
1537 return "reshape";
1538
1539 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1540 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1541 return "resync";
1542 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1543 return "check";
1544 return "repair";
1545 }
1546
1547 if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
1548 return "recover";
1549 }
1550
1551 return "idle";
1552}
1553
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001554static void raid_status(struct dm_target *ti, status_type_t type,
1555 unsigned status_flags, char *result, unsigned maxlen)
NeilBrown9d09e662011-01-13 20:00:02 +00001556{
1557 struct raid_set *rs = ti->private;
1558 unsigned raid_param_cnt = 1; /* at least 1 for chunksize */
1559 unsigned sz = 0;
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00001560 int i, array_in_sync = 0;
NeilBrown9d09e662011-01-13 20:00:02 +00001561 sector_t sync;
1562
1563 switch (type) {
1564 case STATUSTYPE_INFO:
1565 DMEMIT("%s %d ", rs->raid_type->name, rs->md.raid_disks);
1566
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02001567 if (rs->raid_type->level) {
1568 if (test_bit(MD_RECOVERY_RUNNING, &rs->md.recovery))
1569 sync = rs->md.curr_resync_completed;
1570 else
1571 sync = rs->md.recovery_cp;
NeilBrown9d09e662011-01-13 20:00:02 +00001572
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02001573 if (sync >= rs->md.resync_max_sectors) {
1574 /*
1575 * Sync complete.
1576 */
1577 array_in_sync = 1;
1578 sync = rs->md.resync_max_sectors;
1579 } else if (test_bit(MD_RECOVERY_REQUESTED, &rs->md.recovery)) {
1580 /*
1581 * If "check" or "repair" is occurring, the array has
1582 * undergone and initial sync and the health characters
1583 * should not be 'a' anymore.
1584 */
1585 array_in_sync = 1;
1586 } else {
1587 /*
1588 * The array may be doing an initial sync, or it may
1589 * be rebuilding individual components. If all the
1590 * devices are In_sync, then it is the array that is
1591 * being initialized.
1592 */
1593 for (i = 0; i < rs->md.raid_disks; i++)
1594 if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
1595 array_in_sync = 1;
1596 }
1597 } else {
1598 /* RAID0 */
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00001599 array_in_sync = 1;
NeilBrown9d09e662011-01-13 20:00:02 +00001600 sync = rs->md.resync_max_sectors;
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00001601 }
Jonathan Brassowbe836512013-04-24 11:42:43 +10001602
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00001603 /*
1604 * Status characters:
1605 * 'D' = Dead/Failed device
1606 * 'a' = Alive but not in-sync
1607 * 'A' = Alive and in-sync
1608 */
1609 for (i = 0; i < rs->md.raid_disks; i++) {
1610 if (test_bit(Faulty, &rs->dev[i].rdev.flags))
1611 DMEMIT("D");
1612 else if (!array_in_sync ||
1613 !test_bit(In_sync, &rs->dev[i].rdev.flags))
1614 DMEMIT("a");
1615 else
1616 DMEMIT("A");
1617 }
NeilBrown9d09e662011-01-13 20:00:02 +00001618
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00001619 /*
1620 * In-sync ratio:
1621 * The in-sync ratio shows the progress of:
1622 * - Initializing the array
1623 * - Rebuilding a subset of devices of the array
1624 * The user can distinguish between the two by referring
1625 * to the status characters.
1626 */
NeilBrown9d09e662011-01-13 20:00:02 +00001627 DMEMIT(" %llu/%llu",
1628 (unsigned long long) sync,
1629 (unsigned long long) rs->md.resync_max_sectors);
1630
Jonathan Brassowbe836512013-04-24 11:42:43 +10001631 /*
1632 * Sync action:
1633 * See Documentation/device-mapper/dm-raid.c for
1634 * information on each of these states.
1635 */
1636 DMEMIT(" %s", decipher_sync_action(&rs->md));
1637
1638 /*
1639 * resync_mismatches/mismatch_cnt
1640 * This field shows the number of discrepancies found when
1641 * performing a "check" of the array.
1642 */
1643 DMEMIT(" %llu",
Jonathan Brassowc4a39552013-06-25 01:23:59 -05001644 (strcmp(rs->md.last_sync_action, "check")) ? 0 :
Jonathan Brassowbe836512013-04-24 11:42:43 +10001645 (unsigned long long)
1646 atomic64_read(&rs->md.resync_mismatches));
NeilBrown9d09e662011-01-13 20:00:02 +00001647 break;
1648 case STATUSTYPE_TABLE:
1649 /* The string you would use to construct this array */
Jonathan Brassow46bed2b2011-08-02 12:32:07 +01001650 for (i = 0; i < rs->md.raid_disks; i++) {
Heinz Mauelshagenad51d7f2016-05-19 18:49:28 +02001651 if (_test_flag(CTR_FLAG_REBUILD, rs->ctr_flags) &&
Jonathan Brassow13c87582011-08-02 12:32:03 +01001652 rs->dev[i].data_dev &&
NeilBrown9d09e662011-01-13 20:00:02 +00001653 !test_bit(In_sync, &rs->dev[i].rdev.flags))
Jonathan Brassow13c87582011-08-02 12:32:03 +01001654 raid_param_cnt += 2; /* for rebuilds */
Jonathan Brassow46bed2b2011-08-02 12:32:07 +01001655 if (rs->dev[i].data_dev &&
1656 test_bit(WriteMostly, &rs->dev[i].rdev.flags))
1657 raid_param_cnt += 2;
1658 }
NeilBrown9d09e662011-01-13 20:00:02 +00001659
Heinz Mauelshagenc76d53f2015-04-29 14:03:00 +02001660 raid_param_cnt += (hweight32(rs->ctr_flags & ~CTR_FLAG_REBUILD) * 2);
1661 if (rs->ctr_flags & (CTR_FLAG_SYNC | CTR_FLAG_NOSYNC))
NeilBrown9d09e662011-01-13 20:00:02 +00001662 raid_param_cnt--;
1663
1664 DMEMIT("%s %u %u", rs->raid_type->name,
1665 raid_param_cnt, rs->md.chunk_sectors);
1666
Heinz Mauelshagenad51d7f2016-05-19 18:49:28 +02001667 if (_test_flag(CTR_FLAG_SYNC, rs->ctr_flags) &&
1668 rs->md.recovery_cp == MaxSector)
NeilBrown9d09e662011-01-13 20:00:02 +00001669 DMEMIT(" sync");
Heinz Mauelshagenad51d7f2016-05-19 18:49:28 +02001670 if (_test_flag(CTR_FLAG_NOSYNC, rs->ctr_flags))
NeilBrown9d09e662011-01-13 20:00:02 +00001671 DMEMIT(" nosync");
1672
1673 for (i = 0; i < rs->md.raid_disks; i++)
Heinz Mauelshagenad51d7f2016-05-19 18:49:28 +02001674 if (_test_flag(CTR_FLAG_REBUILD, rs->ctr_flags) &&
Jonathan Brassow13c87582011-08-02 12:32:03 +01001675 rs->dev[i].data_dev &&
NeilBrown9d09e662011-01-13 20:00:02 +00001676 !test_bit(In_sync, &rs->dev[i].rdev.flags))
1677 DMEMIT(" rebuild %u", i);
1678
Heinz Mauelshagenad51d7f2016-05-19 18:49:28 +02001679 if (_test_flag(CTR_FLAG_DAEMON_SLEEP, rs->ctr_flags))
NeilBrown9d09e662011-01-13 20:00:02 +00001680 DMEMIT(" daemon_sleep %lu",
1681 rs->md.bitmap_info.daemon_sleep);
1682
Heinz Mauelshagenad51d7f2016-05-19 18:49:28 +02001683 if (_test_flag(CTR_FLAG_MIN_RECOVERY_RATE, rs->ctr_flags))
NeilBrown9d09e662011-01-13 20:00:02 +00001684 DMEMIT(" min_recovery_rate %d", rs->md.sync_speed_min);
1685
Heinz Mauelshagenad51d7f2016-05-19 18:49:28 +02001686 if (_test_flag(CTR_FLAG_MAX_RECOVERY_RATE, rs->ctr_flags))
NeilBrown9d09e662011-01-13 20:00:02 +00001687 DMEMIT(" max_recovery_rate %d", rs->md.sync_speed_max);
1688
Jonathan Brassow46bed2b2011-08-02 12:32:07 +01001689 for (i = 0; i < rs->md.raid_disks; i++)
1690 if (rs->dev[i].data_dev &&
1691 test_bit(WriteMostly, &rs->dev[i].rdev.flags))
1692 DMEMIT(" write_mostly %u", i);
1693
Heinz Mauelshagenad51d7f2016-05-19 18:49:28 +02001694 if (_test_flag(CTR_FLAG_MAX_WRITE_BEHIND, rs->ctr_flags))
NeilBrown9d09e662011-01-13 20:00:02 +00001695 DMEMIT(" max_write_behind %lu",
1696 rs->md.bitmap_info.max_write_behind);
1697
Heinz Mauelshagenad51d7f2016-05-19 18:49:28 +02001698 if (_test_flag(CTR_FLAG_STRIPE_CACHE, rs->ctr_flags)) {
NeilBrownd1688a62011-10-11 16:49:52 +11001699 struct r5conf *conf = rs->md.private;
NeilBrown9d09e662011-01-13 20:00:02 +00001700
1701 /* convert from kiB to sectors */
1702 DMEMIT(" stripe_cache %d",
1703 conf ? conf->max_nr_stripes * 2 : 0);
1704 }
1705
Heinz Mauelshagenad51d7f2016-05-19 18:49:28 +02001706 if (_test_flag(CTR_FLAG_REGION_SIZE, rs->ctr_flags))
Jonathan Brassowc1084562011-08-02 12:32:07 +01001707 DMEMIT(" region_size %lu",
1708 rs->md.bitmap_info.chunksize >> 9);
1709
Heinz Mauelshagenad51d7f2016-05-19 18:49:28 +02001710 if (_test_flag(CTR_FLAG_RAID10_COPIES, rs->ctr_flags))
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001711 DMEMIT(" raid10_copies %u",
1712 raid10_md_layout_to_copies(rs->md.layout));
1713
Heinz Mauelshagenad51d7f2016-05-19 18:49:28 +02001714 if (_test_flag(CTR_FLAG_RAID10_FORMAT, rs->ctr_flags))
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11001715 DMEMIT(" raid10_format %s",
1716 raid10_md_layout_to_format(rs->md.layout));
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001717
NeilBrown9d09e662011-01-13 20:00:02 +00001718 DMEMIT(" %d", rs->md.raid_disks);
1719 for (i = 0; i < rs->md.raid_disks; i++) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001720 if (rs->dev[i].meta_dev)
1721 DMEMIT(" %s", rs->dev[i].meta_dev->name);
1722 else
1723 DMEMIT(" -");
NeilBrown9d09e662011-01-13 20:00:02 +00001724
1725 if (rs->dev[i].data_dev)
1726 DMEMIT(" %s", rs->dev[i].data_dev->name);
1727 else
1728 DMEMIT(" -");
1729 }
1730 }
NeilBrown9d09e662011-01-13 20:00:02 +00001731}
1732
Jonathan Brassowbe836512013-04-24 11:42:43 +10001733static int raid_message(struct dm_target *ti, unsigned argc, char **argv)
1734{
1735 struct raid_set *rs = ti->private;
1736 struct mddev *mddev = &rs->md;
1737
1738 if (!strcasecmp(argv[0], "reshape")) {
1739 DMERR("Reshape not supported.");
1740 return -EINVAL;
1741 }
1742
1743 if (!mddev->pers || !mddev->pers->sync_request)
1744 return -EINVAL;
1745
1746 if (!strcasecmp(argv[0], "frozen"))
1747 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
1748 else
1749 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
1750
1751 if (!strcasecmp(argv[0], "idle") || !strcasecmp(argv[0], "frozen")) {
1752 if (mddev->sync_thread) {
1753 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1754 md_reap_sync_thread(mddev);
1755 }
1756 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1757 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
1758 return -EBUSY;
1759 else if (!strcasecmp(argv[0], "resync"))
1760 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1761 else if (!strcasecmp(argv[0], "recover")) {
1762 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
1763 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1764 } else {
1765 if (!strcasecmp(argv[0], "check"))
1766 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
1767 else if (!!strcasecmp(argv[0], "repair"))
1768 return -EINVAL;
1769 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
1770 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
1771 }
1772 if (mddev->ro == 2) {
1773 /* A write to sync_action is enough to justify
1774 * canceling read-auto mode
1775 */
1776 mddev->ro = 0;
1777 if (!mddev->suspended)
1778 md_wakeup_thread(mddev->sync_thread);
1779 }
1780 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1781 if (!mddev->suspended)
1782 md_wakeup_thread(mddev->thread);
1783
1784 return 0;
1785}
1786
1787static int raid_iterate_devices(struct dm_target *ti,
1788 iterate_devices_callout_fn fn, void *data)
NeilBrown9d09e662011-01-13 20:00:02 +00001789{
1790 struct raid_set *rs = ti->private;
1791 unsigned i;
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02001792 int r = 0;
NeilBrown9d09e662011-01-13 20:00:02 +00001793
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02001794 for (i = 0; !r && i < rs->md.raid_disks; i++)
NeilBrown9d09e662011-01-13 20:00:02 +00001795 if (rs->dev[i].data_dev)
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02001796 r = fn(ti,
NeilBrown9d09e662011-01-13 20:00:02 +00001797 rs->dev[i].data_dev,
1798 0, /* No offset on data devs */
1799 rs->md.dev_sectors,
1800 data);
1801
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02001802 return r;
NeilBrown9d09e662011-01-13 20:00:02 +00001803}
1804
1805static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits)
1806{
1807 struct raid_set *rs = ti->private;
1808 unsigned chunk_size = rs->md.chunk_sectors << 9;
NeilBrownd1688a62011-10-11 16:49:52 +11001809 struct r5conf *conf = rs->md.private;
NeilBrown9d09e662011-01-13 20:00:02 +00001810
1811 blk_limits_io_min(limits, chunk_size);
1812 blk_limits_io_opt(limits, chunk_size * (conf->raid_disks - conf->max_degraded));
1813}
1814
1815static void raid_presuspend(struct dm_target *ti)
1816{
1817 struct raid_set *rs = ti->private;
1818
1819 md_stop_writes(&rs->md);
1820}
1821
1822static void raid_postsuspend(struct dm_target *ti)
1823{
1824 struct raid_set *rs = ti->private;
1825
1826 mddev_suspend(&rs->md);
1827}
1828
Jonathan Brassowf381e712013-05-08 17:57:13 -05001829static void attempt_restore_of_faulty_devices(struct raid_set *rs)
NeilBrown9d09e662011-01-13 20:00:02 +00001830{
Jonathan Brassow9092c022013-05-02 14:19:24 -05001831 int i;
1832 uint64_t failed_devices, cleared_failed_devices = 0;
1833 unsigned long flags;
1834 struct dm_raid_superblock *sb;
Jonathan Brassow9092c022013-05-02 14:19:24 -05001835 struct md_rdev *r;
NeilBrown9d09e662011-01-13 20:00:02 +00001836
Jonathan Brassowf381e712013-05-08 17:57:13 -05001837 for (i = 0; i < rs->md.raid_disks; i++) {
1838 r = &rs->dev[i].rdev;
1839 if (test_bit(Faulty, &r->flags) && r->sb_page &&
Mike Christie796a5cf2016-06-05 14:32:07 -05001840 sync_page_io(r, 0, r->sb_size, r->sb_page, REQ_OP_READ, 0,
1841 1)) {
Jonathan Brassowf381e712013-05-08 17:57:13 -05001842 DMINFO("Faulty %s device #%d has readable super block."
1843 " Attempting to revive it.",
1844 rs->raid_type->name, i);
Jonathan Brassowa4dc1632013-05-08 18:00:54 -05001845
1846 /*
1847 * Faulty bit may be set, but sometimes the array can
1848 * be suspended before the personalities can respond
1849 * by removing the device from the array (i.e. calling
1850 * 'hot_remove_disk'). If they haven't yet removed
1851 * the failed device, its 'raid_disk' number will be
1852 * '>= 0' - meaning we must call this function
1853 * ourselves.
1854 */
1855 if ((r->raid_disk >= 0) &&
1856 (r->mddev->pers->hot_remove_disk(r->mddev, r) != 0))
1857 /* Failed to revive this device, try next */
1858 continue;
1859
Jonathan Brassowf381e712013-05-08 17:57:13 -05001860 r->raid_disk = i;
1861 r->saved_raid_disk = i;
1862 flags = r->flags;
1863 clear_bit(Faulty, &r->flags);
1864 clear_bit(WriteErrorSeen, &r->flags);
1865 clear_bit(In_sync, &r->flags);
1866 if (r->mddev->pers->hot_add_disk(r->mddev, r)) {
1867 r->raid_disk = -1;
1868 r->saved_raid_disk = -1;
1869 r->flags = flags;
1870 } else {
1871 r->recovery_offset = 0;
1872 cleared_failed_devices |= 1 << i;
1873 }
1874 }
1875 }
1876 if (cleared_failed_devices) {
1877 rdev_for_each(r, &rs->md) {
1878 sb = page_address(r->sb_page);
1879 failed_devices = le64_to_cpu(sb->failed_devices);
1880 failed_devices &= ~cleared_failed_devices;
1881 sb->failed_devices = cpu_to_le64(failed_devices);
1882 }
1883 }
1884}
1885
1886static void raid_resume(struct dm_target *ti)
1887{
1888 struct raid_set *rs = ti->private;
1889
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02001890 if (rs->raid_type->level) {
1891 set_bit(MD_CHANGE_DEVS, &rs->md.flags);
1892
1893 if (!rs->bitmap_loaded) {
1894 bitmap_load(&rs->md);
1895 rs->bitmap_loaded = 1;
1896 } else {
1897 /*
1898 * A secondary resume while the device is active.
1899 * Take this opportunity to check whether any failed
1900 * devices are reachable again.
1901 */
1902 attempt_restore_of_faulty_devices(rs);
1903 }
1904
1905 clear_bit(MD_RECOVERY_FROZEN, &rs->md.recovery);
Jonathan Brassow47525e52012-05-22 13:55:29 +10001906 }
Jonathan Brassow34f8ac6d2012-01-27 14:53:53 -06001907
NeilBrown9d09e662011-01-13 20:00:02 +00001908 mddev_resume(&rs->md);
1909}
1910
1911static struct target_type raid_target = {
1912 .name = "raid",
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001913 .version = {1, 8, 1},
NeilBrown9d09e662011-01-13 20:00:02 +00001914 .module = THIS_MODULE,
1915 .ctr = raid_ctr,
1916 .dtr = raid_dtr,
1917 .map = raid_map,
1918 .status = raid_status,
Jonathan Brassowbe836512013-04-24 11:42:43 +10001919 .message = raid_message,
NeilBrown9d09e662011-01-13 20:00:02 +00001920 .iterate_devices = raid_iterate_devices,
1921 .io_hints = raid_io_hints,
1922 .presuspend = raid_presuspend,
1923 .postsuspend = raid_postsuspend,
1924 .resume = raid_resume,
1925};
1926
1927static int __init dm_raid_init(void)
1928{
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11001929 DMINFO("Loading target version %u.%u.%u",
1930 raid_target.version[0],
1931 raid_target.version[1],
1932 raid_target.version[2]);
NeilBrown9d09e662011-01-13 20:00:02 +00001933 return dm_register_target(&raid_target);
1934}
1935
1936static void __exit dm_raid_exit(void)
1937{
1938 dm_unregister_target(&raid_target);
1939}
1940
1941module_init(dm_raid_init);
1942module_exit(dm_raid_exit);
1943
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02001944module_param(devices_handle_discard_safely, bool, 0644);
1945MODULE_PARM_DESC(devices_handle_discard_safely,
1946 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
1947
NeilBrown9d09e662011-01-13 20:00:02 +00001948MODULE_DESCRIPTION(DM_NAME " raid4/5/6 target");
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001949MODULE_ALIAS("dm-raid1");
1950MODULE_ALIAS("dm-raid10");
NeilBrown9d09e662011-01-13 20:00:02 +00001951MODULE_ALIAS("dm-raid4");
1952MODULE_ALIAS("dm-raid5");
1953MODULE_ALIAS("dm-raid6");
1954MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>");
1955MODULE_LICENSE("GPL");